blob: 200a877622ef8391b6461510466c267c6bbccdac [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,
Garrick Evansf4a93292019-03-13 14:19:43 +090038 std::unique_ptr<Device::Config> config,
Garrick Evansbcce09e2020-03-10 15:08:04 +090039 const Device::Options& options)
Garrick Evans6c7dcb82020-03-16 15:21:05 +090040 : phys_ifname_(phys_ifname),
41 host_ifname_(host_ifname),
42 guest_ifname_(guest_ifname),
43 config_(std::move(config)),
44 options_(options) {
Garrick Evansf4a93292019-03-13 14:19:43 +090045 DCHECK(config_);
Garrick Evans49879532018-12-03 13:15:36 +090046}
47
Garrick Evans894abc22019-06-07 10:49:02 +090048Device::Config& Device::config() const {
49 CHECK(config_);
50 return *config_.get();
51}
Garrick Evansd2bb8502019-02-20 15:59:35 +090052
Garrick Evans2961c7c2020-04-03 11:34:40 +090053void Device::Config::set_tap_ifname(const std::string& tap_ifname) {
54 tap_ = tap_ifname;
55}
56
57const std::string& Device::Config::tap_ifname() const {
58 return tap_;
59}
60
Garrick Evans86c7d9c2020-03-17 09:25:48 +090061std::unique_ptr<Device::Config> Device::release_config() {
62 return std::move(config_);
63}
64
Garrick Evans54861622019-07-19 09:05:09 +090065const Device::Options& Device::options() const {
66 return options_;
67}
Long Cheng494fc982019-09-24 19:09:03 +000068
Hugo Benichiee787ff2019-05-20 16:42:42 +090069std::ostream& operator<<(std::ostream& stream, const Device& device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +090070 stream << "{ ifname: " << device.phys_ifname_
71 << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090072 << device.config_->host_ipv4_addr_->ToCidrString()
Garrick Evans6c7dcb82020-03-16 15:21:05 +090073 << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090074 << device.config_->guest_ipv4_addr_->ToCidrString()
75 << ", guest_mac_addr: "
Garrick Evans6c7dcb82020-03-16 15:21:05 +090076 << MacAddressToString(device.config_->mac_addr())
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090077 << ", fwd_multicast: " << device.options_.fwd_multicast
Garrick Evans2961c7c2020-04-03 11:34:40 +090078 << ", ipv6_enabled: " << device.options_.ipv6_enabled
79 << ", tap_ifname: " << device.config_->tap_ifname() << '}';
Hugo Benichiee787ff2019-05-20 16:42:42 +090080 return stream;
81}
82
Garrick Evans3388a032020-03-24 11:25:55 +090083} // namespace patchpanel