Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 5 | #ifndef PATCHPANEL_MAC_ADDRESS_GENERATOR_H_ |
| 6 | #define PATCHPANEL_MAC_ADDRESS_GENERATOR_H_ |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <array> |
| 11 | #include <functional> |
| 12 | #include <unordered_set> |
| 13 | |
| 14 | #include <base/macros.h> |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 15 | #include <brillo/brillo_export.h> |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 16 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 17 | namespace patchpanel { |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 18 | |
| 19 | using MacAddress = std::array<uint8_t, 6>; |
| 20 | |
| 21 | // Generates locally managed EUI-48 MAC addresses and ensures no collisions |
| 22 | // with any previously generated addresses by this instance. |
Garrick Evans | 4b66f63 | 2019-01-24 15:09:16 +0900 | [diff] [blame] | 23 | class BRILLO_EXPORT MacAddressGenerator { |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 24 | public: |
| 25 | MacAddressGenerator() = default; |
| 26 | ~MacAddressGenerator() = default; |
| 27 | |
| 28 | // Generates a new EUI-48 MAC address and ensures that there are no |
| 29 | // collisions with any addresses previously generated by this instance of |
| 30 | // the generator. |
| 31 | MacAddress Generate(); |
| 32 | |
Garrick Evans | 2013a79 | 2020-04-02 11:27:57 +0900 | [diff] [blame] | 33 | // Returns a stable MAC address whose first 5 octets are fixed and using |id| |
| 34 | // as the sixth. The base address is itself random and was not generated from |
| 35 | // any particular device, physical or virtual. Additionally, the |id| should |
| 36 | // associated with any specific device either, and should be set indepedently. |
| 37 | MacAddress GetStable(uint8_t id) const; |
| 38 | |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 39 | private: |
| 40 | // The standard library sadly does not provide a hash function for std::array. |
| 41 | // So implement one here for MacAddress based off boost::hash_combine. |
| 42 | struct MacAddressHasher { |
| 43 | size_t operator()(const MacAddress& addr) const noexcept { |
| 44 | std::hash<uint8_t> hasher; |
| 45 | |
| 46 | size_t hash = 0; |
| 47 | for (uint8_t octet : addr) { |
| 48 | hash ^= hasher(octet) + 0x9e3779b9 + (hash << 6) + (hash >> 2); |
| 49 | } |
| 50 | |
| 51 | return hash; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | // Set of all addresses generated by this instance. This doesn't _need_ to be |
| 56 | // an unordered_set but making it one improves the performance of the |
| 57 | // "Duplicates" unit test by ~33% (~150 seconds -> ~100 seconds) and it |
| 58 | // doesn't have a huge impact in production use so that's why we use it here. |
| 59 | std::unordered_set<MacAddress, MacAddressHasher> addrs_; |
| 60 | |
| 61 | DISALLOW_COPY_AND_ASSIGN(MacAddressGenerator); |
| 62 | }; |
| 63 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 64 | } // namespace patchpanel |
Chirantan Ekbote | ad25b65 | 2017-12-11 18:37:09 -0800 | [diff] [blame] | 65 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 66 | #endif // PATCHPANEL_MAC_ADDRESS_GENERATOR_H_ |