blob: e39969ffa1b50956bbb070b62e321b91652af6e1 [file] [log] [blame]
Garrick Evans49879532018-12-03 13:15:36 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/device.h"
Garrick Evans49879532018-12-03 13:15:36 +09006
Garrick Evansf4a93292019-03-13 14:19:43 +09007#include <arpa/inet.h>
8#include <sys/socket.h>
Garrick Evans49879532018-12-03 13:15:36 +09009#include <sys/types.h>
10
11#include <map>
Garrick Evansf4a93292019-03-13 14:19:43 +090012#include <utility>
Garrick Evans49879532018-12-03 13:15:36 +090013
14#include <base/bind.h>
Qijiang Fan713061e2021-03-08 15:45:12 +090015#include <base/check.h>
Garrick Evansf4a93292019-03-13 14:19:43 +090016#include <base/lazy_instance.h>
Garrick Evans49879532018-12-03 13:15:36 +090017#include <base/logging.h>
Garrick Evans49879532018-12-03 13:15:36 +090018
Garrick Evans3388a032020-03-24 11:25:55 +090019#include "patchpanel/crostini_service.h"
20#include "patchpanel/net_util.h"
Garrick Evans49879532018-12-03 13:15:36 +090021
Garrick Evans3388a032020-03-24 11:25:55 +090022namespace patchpanel {
Garrick Evans49879532018-12-03 13:15:36 +090023
Garrick Evans6c7dcb82020-03-16 15:21:05 +090024Device::Config::Config(const MacAddress& mac_addr,
Garrick Evansf4a93292019-03-13 14:19:43 +090025 std::unique_ptr<Subnet> ipv4_subnet,
26 std::unique_ptr<SubnetAddress> host_ipv4_addr,
Garrick Evans47c19272019-11-21 10:58:21 +090027 std::unique_ptr<SubnetAddress> guest_ipv4_addr,
28 std::unique_ptr<Subnet> lxd_ipv4_subnet)
Garrick Evans6c7dcb82020-03-16 15:21:05 +090029 : mac_addr_(mac_addr),
Garrick Evansf4a93292019-03-13 14:19:43 +090030 ipv4_subnet_(std::move(ipv4_subnet)),
31 host_ipv4_addr_(std::move(host_ipv4_addr)),
Garrick Evans47c19272019-11-21 10:58:21 +090032 guest_ipv4_addr_(std::move(guest_ipv4_addr)),
33 lxd_ipv4_subnet_(std::move(lxd_ipv4_subnet)) {}
Garrick Evansf4a93292019-03-13 14:19:43 +090034
Garrick Evans6c7dcb82020-03-16 15:21:05 +090035Device::Device(const std::string& phys_ifname,
36 const std::string& host_ifname,
37 const std::string& guest_ifname,
Hugo Benichi6f118a32021-03-01 12:28:14 +090038 std::unique_ptr<Device::Config> config)
Garrick Evans6c7dcb82020-03-16 15:21:05 +090039 : phys_ifname_(phys_ifname),
40 host_ifname_(host_ifname),
41 guest_ifname_(guest_ifname),
Hugo Benichi6f118a32021-03-01 12:28:14 +090042 config_(std::move(config)) {
Garrick Evansf4a93292019-03-13 14:19:43 +090043 DCHECK(config_);
Garrick Evans49879532018-12-03 13:15:36 +090044}
45
Garrick Evans894abc22019-06-07 10:49:02 +090046Device::Config& Device::config() const {
47 CHECK(config_);
48 return *config_.get();
49}
Garrick Evansd2bb8502019-02-20 15:59:35 +090050
Garrick Evans2961c7c2020-04-03 11:34:40 +090051void Device::Config::set_tap_ifname(const std::string& tap_ifname) {
52 tap_ = tap_ifname;
53}
54
55const std::string& Device::Config::tap_ifname() const {
56 return tap_;
57}
58
Garrick Evans86c7d9c2020-03-17 09:25:48 +090059std::unique_ptr<Device::Config> Device::release_config() {
60 return std::move(config_);
61}
62
Hugo Benichiee787ff2019-05-20 16:42:42 +090063std::ostream& operator<<(std::ostream& stream, const Device& device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +090064 stream << "{ ifname: " << device.phys_ifname_
65 << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090066 << device.config_->host_ipv4_addr_->ToCidrString()
Garrick Evans6c7dcb82020-03-16 15:21:05 +090067 << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090068 << device.config_->guest_ipv4_addr_->ToCidrString()
69 << ", guest_mac_addr: "
Garrick Evans6c7dcb82020-03-16 15:21:05 +090070 << MacAddressToString(device.config_->mac_addr())
Garrick Evans2961c7c2020-04-03 11:34:40 +090071 << ", tap_ifname: " << device.config_->tap_ifname() << '}';
Hugo Benichiee787ff2019-05-20 16:42:42 +090072 return stream;
73}
74
Garrick Evans3388a032020-03-24 11:25:55 +090075} // namespace patchpanel