Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.util.SparseBooleanArray; |
| 21 | |
| 22 | import com.android.internal.annotations.GuardedBy; |
Remi NGUYEN VAN | f8a4272 | 2019-08-01 16:25:50 +0900 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Class used to reserve and release net IDs. |
| 27 | * |
| 28 | * <p>Instances of this class are thread-safe. |
| 29 | */ |
| 30 | public class NetIdManager { |
| 31 | // Sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
| 32 | public static final int MIN_NET_ID = 100; // some reserved marks |
| 33 | // Top IDs reserved by IpSecService |
| 34 | public static final int MAX_NET_ID = 65535 - IpSecService.TUN_INTF_NETID_RANGE; |
| 35 | |
| 36 | @GuardedBy("mNetIdInUse") |
| 37 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
| 38 | |
| 39 | @GuardedBy("mNetIdInUse") |
| 40 | private int mLastNetId = MIN_NET_ID - 1; |
| 41 | |
Remi NGUYEN VAN | f8a4272 | 2019-08-01 16:25:50 +0900 | [diff] [blame] | 42 | private final int mMaxNetId; |
| 43 | |
| 44 | public NetIdManager() { |
| 45 | this(MAX_NET_ID); |
| 46 | } |
| 47 | |
| 48 | @VisibleForTesting |
| 49 | NetIdManager(int maxNetId) { |
| 50 | mMaxNetId = maxNetId; |
| 51 | } |
| 52 | |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 53 | /** |
| 54 | * Get the first netId that follows the provided lastId and is available. |
| 55 | */ |
Remi NGUYEN VAN | f8a4272 | 2019-08-01 16:25:50 +0900 | [diff] [blame] | 56 | private int getNextAvailableNetIdLocked( |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 57 | int lastId, @NonNull SparseBooleanArray netIdInUse) { |
| 58 | int netId = lastId; |
Remi NGUYEN VAN | f8a4272 | 2019-08-01 16:25:50 +0900 | [diff] [blame] | 59 | for (int i = MIN_NET_ID; i <= mMaxNetId; i++) { |
| 60 | netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID; |
Remi NGUYEN VAN | 95dc87e | 2019-06-13 16:12:02 +0900 | [diff] [blame] | 61 | if (!netIdInUse.get(netId)) { |
| 62 | return netId; |
| 63 | } |
| 64 | } |
| 65 | throw new IllegalStateException("No free netIds"); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Reserve a new ID for a network. |
| 70 | */ |
| 71 | public int reserveNetId() { |
| 72 | synchronized (mNetIdInUse) { |
| 73 | mLastNetId = getNextAvailableNetIdLocked(mLastNetId, mNetIdInUse); |
| 74 | // Make sure NetID unused. http://b/16815182 |
| 75 | mNetIdInUse.put(mLastNetId, true); |
| 76 | return mLastNetId; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Clear a previously reserved ID for a network. |
| 82 | */ |
| 83 | public void releaseNetId(int id) { |
| 84 | synchronized (mNetIdInUse) { |
| 85 | mNetIdInUse.delete(id); |
| 86 | } |
| 87 | } |
| 88 | } |