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, |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 38 | std::unique_ptr<Device::Config> config, |
Garrick Evans | bcce09e | 2020-03-10 15:08:04 +0900 | [diff] [blame] | 39 | const Device::Options& options) |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 40 | : phys_ifname_(phys_ifname), |
| 41 | host_ifname_(host_ifname), |
| 42 | guest_ifname_(guest_ifname), |
| 43 | config_(std::move(config)), |
| 44 | options_(options) { |
Garrick Evans | f4a9329 | 2019-03-13 14:19:43 +0900 | [diff] [blame] | 45 | DCHECK(config_); |
Garrick Evans | 4987953 | 2018-12-03 13:15:36 +0900 | [diff] [blame] | 46 | } |
| 47 | |
Garrick Evans | 894abc2 | 2019-06-07 10:49:02 +0900 | [diff] [blame] | 48 | Device::Config& Device::config() const { |
| 49 | CHECK(config_); |
| 50 | return *config_.get(); |
| 51 | } |
Garrick Evans | d2bb850 | 2019-02-20 15:59:35 +0900 | [diff] [blame] | 52 | |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 53 | void Device::Config::set_tap_ifname(const std::string& tap_ifname) { |
| 54 | tap_ = tap_ifname; |
| 55 | } |
| 56 | |
| 57 | const std::string& Device::Config::tap_ifname() const { |
| 58 | return tap_; |
| 59 | } |
| 60 | |
Garrick Evans | 86c7d9c | 2020-03-17 09:25:48 +0900 | [diff] [blame] | 61 | std::unique_ptr<Device::Config> Device::release_config() { |
| 62 | return std::move(config_); |
| 63 | } |
| 64 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 65 | const Device::Options& Device::options() const { |
| 66 | return options_; |
| 67 | } |
Long Cheng | 494fc98 | 2019-09-24 19:09:03 +0000 | [diff] [blame] | 68 | |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 69 | std::ostream& operator<<(std::ostream& stream, const Device& device) { |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 70 | stream << "{ ifname: " << device.phys_ifname_ |
| 71 | << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: " |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 72 | << device.config_->host_ipv4_addr_->ToCidrString() |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 73 | << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: " |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 74 | << device.config_->guest_ipv4_addr_->ToCidrString() |
| 75 | << ", guest_mac_addr: " |
Garrick Evans | 6c7dcb8 | 2020-03-16 15:21:05 +0900 | [diff] [blame] | 76 | << MacAddressToString(device.config_->mac_addr()) |
Hugo Benichi | bd8ec4d | 2019-05-28 12:52:49 +0900 | [diff] [blame] | 77 | << ", fwd_multicast: " << device.options_.fwd_multicast |
Garrick Evans | 2961c7c | 2020-04-03 11:34:40 +0900 | [diff] [blame] | 78 | << ", ipv6_enabled: " << device.options_.ipv6_enabled |
| 79 | << ", tap_ifname: " << device.config_->tap_ifname() << '}'; |
Hugo Benichi | ee787ff | 2019-05-20 16:42:42 +0900 | [diff] [blame] | 80 | return stream; |
| 81 | } |
| 82 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 83 | } // namespace patchpanel |