blob: 1a1c61af0e16ae02fb9cc5ef85ce3b349c79c6ef [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>
Garrick Evansf4a93292019-03-13 14:19:43 +090015#include <base/lazy_instance.h>
Garrick Evans49879532018-12-03 13:15:36 +090016#include <base/logging.h>
Garrick Evans49879532018-12-03 13:15:36 +090017
Garrick Evans3388a032020-03-24 11:25:55 +090018#include "patchpanel/crostini_service.h"
19#include "patchpanel/net_util.h"
Garrick Evans49879532018-12-03 13:15:36 +090020
Garrick Evans3388a032020-03-24 11:25:55 +090021namespace patchpanel {
Garrick Evans49879532018-12-03 13:15:36 +090022
Garrick Evans6c7dcb82020-03-16 15:21:05 +090023Device::Config::Config(const MacAddress& mac_addr,
Garrick Evansf4a93292019-03-13 14:19:43 +090024 std::unique_ptr<Subnet> ipv4_subnet,
25 std::unique_ptr<SubnetAddress> host_ipv4_addr,
Garrick Evans47c19272019-11-21 10:58:21 +090026 std::unique_ptr<SubnetAddress> guest_ipv4_addr,
27 std::unique_ptr<Subnet> lxd_ipv4_subnet)
Garrick Evans6c7dcb82020-03-16 15:21:05 +090028 : mac_addr_(mac_addr),
Garrick Evansf4a93292019-03-13 14:19:43 +090029 ipv4_subnet_(std::move(ipv4_subnet)),
30 host_ipv4_addr_(std::move(host_ipv4_addr)),
Garrick Evans47c19272019-11-21 10:58:21 +090031 guest_ipv4_addr_(std::move(guest_ipv4_addr)),
32 lxd_ipv4_subnet_(std::move(lxd_ipv4_subnet)) {}
Garrick Evansf4a93292019-03-13 14:19:43 +090033
Garrick Evans6c7dcb82020-03-16 15:21:05 +090034Device::Device(const std::string& phys_ifname,
35 const std::string& host_ifname,
36 const std::string& guest_ifname,
Garrick Evansf4a93292019-03-13 14:19:43 +090037 std::unique_ptr<Device::Config> config,
Garrick Evansbcce09e2020-03-10 15:08:04 +090038 const Device::Options& options)
Garrick Evans6c7dcb82020-03-16 15:21:05 +090039 : phys_ifname_(phys_ifname),
40 host_ifname_(host_ifname),
41 guest_ifname_(guest_ifname),
42 config_(std::move(config)),
43 options_(options) {
Garrick Evansf4a93292019-03-13 14:19:43 +090044 DCHECK(config_);
Garrick Evans49879532018-12-03 13:15:36 +090045}
46
Garrick Evans894abc22019-06-07 10:49:02 +090047Device::Config& Device::config() const {
48 CHECK(config_);
49 return *config_.get();
50}
Garrick Evansd2bb8502019-02-20 15:59:35 +090051
Garrick Evans2961c7c2020-04-03 11:34:40 +090052void Device::Config::set_tap_ifname(const std::string& tap_ifname) {
53 tap_ = tap_ifname;
54}
55
56const std::string& Device::Config::tap_ifname() const {
57 return tap_;
58}
59
Garrick Evans86c7d9c2020-03-17 09:25:48 +090060std::unique_ptr<Device::Config> Device::release_config() {
61 return std::move(config_);
62}
63
Garrick Evans54861622019-07-19 09:05:09 +090064const Device::Options& Device::options() const {
65 return options_;
66}
Long Cheng494fc982019-09-24 19:09:03 +000067
Hugo Benichiee787ff2019-05-20 16:42:42 +090068std::ostream& operator<<(std::ostream& stream, const Device& device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +090069 stream << "{ ifname: " << device.phys_ifname_
70 << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090071 << device.config_->host_ipv4_addr_->ToCidrString()
Garrick Evans6c7dcb82020-03-16 15:21:05 +090072 << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090073 << device.config_->guest_ipv4_addr_->ToCidrString()
74 << ", guest_mac_addr: "
Garrick Evans6c7dcb82020-03-16 15:21:05 +090075 << MacAddressToString(device.config_->mac_addr())
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090076 << ", fwd_multicast: " << device.options_.fwd_multicast
Garrick Evans2961c7c2020-04-03 11:34:40 +090077 << ", ipv6_enabled: " << device.options_.ipv6_enabled
78 << ", tap_ifname: " << device.config_->tap_ifname() << '}';
Hugo Benichiee787ff2019-05-20 16:42:42 +090079 return stream;
80}
81
Garrick Evans3388a032020-03-24 11:25:55 +090082} // namespace patchpanel