blob: 6b5c7c82506037dd37539243318b2eeb2db351e6 [file] [log] [blame]
Garrick Evans0dbd4182019-03-07 08:38:38 +09001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_ADDRESS_MANAGER_H_
6#define PATCHPANEL_ADDRESS_MANAGER_H_
Garrick Evans0dbd4182019-03-07 08:38:38 +09007
8#include <map>
9#include <memory>
10
11#include <base/callback.h>
12#include <base/macros.h>
13#include <base/memory/weak_ptr.h>
14#include <brillo/brillo_export.h>
15
Garrick Evans3388a032020-03-24 11:25:55 +090016#include "patchpanel/mac_address_generator.h"
17#include "patchpanel/subnet.h"
18#include "patchpanel/subnet_pool.h"
Garrick Evans0dbd4182019-03-07 08:38:38 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evans0dbd4182019-03-07 08:38:38 +090021
22// Responsible for address provisioning for guest networks.
23class BRILLO_EXPORT AddressManager {
24 public:
25 enum class Guest {
Hugo Benichiad1bdd92020-06-12 13:48:37 +090026 // ARC++ or ARCVM management interface.
Garrick Evans0dbd4182019-03-07 08:38:38 +090027 ARC,
Hugo Benichiad1bdd92020-06-12 13:48:37 +090028 // ARC++ or ARCVM virtual networks connected to shill Devices.
Garrick Evans0dbd4182019-03-07 08:38:38 +090029 ARC_NET,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090030 /// Crostini VM root namespace.
Garrick Evans0dbd4182019-03-07 08:38:38 +090031 VM_TERMINA,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090032 // Crostini plugin VMs.
Garrick Evans0dbd4182019-03-07 08:38:38 +090033 VM_PLUGIN,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090034 // Crostini VM user containers.
Garrick Evans0dbd4182019-03-07 08:38:38 +090035 CONTAINER,
Hugo Benichiadf1ec52020-01-17 16:23:58 +090036 // Other network namespaces hosting minijailed host processes.
37 MINIJAIL_NETNS,
Garrick Evans0dbd4182019-03-07 08:38:38 +090038 };
39
Garrick Evans4ee5ce22020-03-18 07:05:17 +090040 AddressManager();
Garrick Evansf4a93292019-03-13 14:19:43 +090041 virtual ~AddressManager() = default;
42
43 // Generates a MAC address guaranteed to be unique for the lifetime of this
44 // object.
Garrick Evans7d9a2322020-04-02 11:59:56 +090045 // If |index| is provided, a MAC address will be returned that is stable
46 // across all invocations and instantions.
Garrick Evansf4a93292019-03-13 14:19:43 +090047 // Virtual for testing only.
Garrick Evans7d9a2322020-04-02 11:59:56 +090048 virtual MacAddress GenerateMacAddress(uint8_t index = kAnySubnetIndex);
Garrick Evans0dbd4182019-03-07 08:38:38 +090049
50 // Allocates a subnet from the specified guest network pool if available.
51 // Returns nullptr if the guest was configured or no more subnets are
52 // available for allocation.
Garrick Evans43b4e2d2019-12-11 13:43:08 +090053 // |index| is used to acquire a particular subnet from the pool, if supported
Garrick Evans53a2a982020-02-05 10:53:35 +090054 // for |guest|, it is 1-based, so 0 indicates no preference.
Garrick Evans51d5b552020-01-30 10:42:06 +090055 std::unique_ptr<Subnet> AllocateIPv4Subnet(Guest guest,
Garrick Evans53a2a982020-02-05 10:53:35 +090056 uint32_t index = kAnySubnetIndex);
Garrick Evans0dbd4182019-03-07 08:38:38 +090057
58 private:
Garrick Evansf4a93292019-03-13 14:19:43 +090059 MacAddressGenerator mac_addrs_;
Garrick Evans0dbd4182019-03-07 08:38:38 +090060 std::map<Guest, std::unique_ptr<SubnetPool>> pools_;
61
62 base::WeakPtrFactory<AddressManager> weak_ptr_factory_{this};
63
64 DISALLOW_COPY_AND_ASSIGN(AddressManager);
65};
66
Garrick Evans3388a032020-03-24 11:25:55 +090067} // namespace patchpanel
Garrick Evans0dbd4182019-03-07 08:38:38 +090068
Garrick Evans3388a032020-03-24 11:25:55 +090069#endif // PATCHPANEL_ADDRESS_MANAGER_H_