Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 1 | // Copyright 2018 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 | #include "patchpanel/device.h" |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 6 | |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 7 | #include <arpa/inet.h> |
| 8 | #include <sys/socket.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <map> |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 12 | #include <utility> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 13 | |
| 14 | #include <base/bind.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 15 | #include <base/check.h> |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 16 | #include <base/lazy_instance.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 17 | #include <base/logging.h> |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 18 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 19 | #include "patchpanel/crostini_service.h" |
| 20 | #include "patchpanel/net_util.h" |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 21 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 22 | namespace patchpanel { |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 23 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 24 | Device::Config::Config(const MacAddress& mac_addr, |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 25 | std::unique_ptr<Subnet> ipv4_subnet, |
| 26 | std::unique_ptr<SubnetAddress> host_ipv4_addr, |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 27 | std::unique_ptr<SubnetAddress> guest_ipv4_addr, |
| 28 | std::unique_ptr<Subnet> lxd_ipv4_subnet) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 29 | : mac_addr_(mac_addr), |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 30 | ipv4_subnet_(std::move(ipv4_subnet)), |
| 31 | host_ipv4_addr_(std::move(host_ipv4_addr)), |
Garrick Evans | 47c1927 | 2019-11-21 10:58:21 +0900 | [diff] [blame] | 32 | guest_ipv4_addr_(std::move(guest_ipv4_addr)), |
| 33 | lxd_ipv4_subnet_(std::move(lxd_ipv4_subnet)) {} |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 34 | |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 35 | Device::Device(const std::string& phys_ifname, |
| 36 | const std::string& host_ifname, |
| 37 | const std::string& guest_ifname, |
Hugo Benichi | 6f118a3 | 2021-03-01 12:28:14 +0900 | [diff] [blame] | 38 | std::unique_ptr<Device::Config> config) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 39 | : phys_ifname_(phys_ifname), |
| 40 | host_ifname_(host_ifname), |
| 41 | guest_ifname_(guest_ifname), |
Hugo Benichi | 6f118a3 | 2021-03-01 12:28:14 +0900 | [diff] [blame] | 42 | config_(std::move(config)) { |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 43 | DCHECK(config_); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 44 | } |
| 45 | |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 46 | Device::Config& Device::config() const { |
| 47 | CHECK(config_); |
| 48 | return *config_.get(); |
| 49 | } |
Garrick Evans | d2bb850 | 2019-02-20 15:59:35 +0900 | [diff] [blame] | 50 | |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 51 | void Device::Config::set_tap_ifname(const std::string& tap_ifname) { |
| 52 | tap_ = tap_ifname; |
| 53 | } |
| 54 | |
| 55 | const std::string& Device::Config::tap_ifname() const { |
| 56 | return tap_; |
| 57 | } |
| 58 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 59 | std::unique_ptr<Device::Config> Device::release_config() { |
| 60 | return std::move(config_); |
| 61 | } |
| 62 | |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 63 | std::ostream& operator<<(std::ostream& stream, const Device& device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 64 | stream << "{ ifname: " << device.phys_ifname_ |
| 65 | << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: " |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 66 | << device.config_->host_ipv4_addr_->ToCidrString() |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 67 | << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: " |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 68 | << device.config_->guest_ipv4_addr_->ToCidrString() |
| 69 | << ", guest_mac_addr: " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 70 | << MacAddressToString(device.config_->mac_addr()) |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 71 | << ", tap_ifname: " << device.config_->tap_ifname() << '}'; |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 72 | return stream; |
| 73 | } |
| 74 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 75 | } // namespace patchpanel |