blob: 097fb3ae47e3ee2eb74d4a69ecdff78d685db30e [file] [log] [blame]
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +09001/*
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
17package com.android.server;
18
19import android.annotation.NonNull;
20import android.util.SparseBooleanArray;
21
22import com.android.internal.annotations.GuardedBy;
Remi NGUYEN VANf8a42722019-08-01 16:25:50 +090023import com.android.internal.annotations.VisibleForTesting;
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +090024
25/**
26 * Class used to reserve and release net IDs.
27 *
28 * <p>Instances of this class are thread-safe.
29 */
30public 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 VANf8a42722019-08-01 16:25:50 +090042 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 VAN95dc87e2019-06-13 16:12:02 +090053 /**
54 * Get the first netId that follows the provided lastId and is available.
55 */
Remi NGUYEN VANf8a42722019-08-01 16:25:50 +090056 private int getNextAvailableNetIdLocked(
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +090057 int lastId, @NonNull SparseBooleanArray netIdInUse) {
58 int netId = lastId;
Remi NGUYEN VANf8a42722019-08-01 16:25:50 +090059 for (int i = MIN_NET_ID; i <= mMaxNetId; i++) {
60 netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID;
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +090061 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}