blob: 5a2e101710c5b21b666647cb34b3ec5259f0c42b [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
5#include "arc/network/device.h"
6
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
Taoyu Li1c96d272019-12-13 14:17:43 +090018#include "arc/network/crostini_service.h"
Hugo Benichi2ac4d072019-05-28 14:51:23 +090019#include "arc/network/net_util.h"
Garrick Evans49879532018-12-03 13:15:36 +090020
21namespace arc_networkd {
22
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 Evans86c7d9c2020-03-17 09:25:48 +090052std::unique_ptr<Device::Config> Device::release_config() {
53 return std::move(config_);
54}
55
Garrick Evans54861622019-07-19 09:05:09 +090056const Device::Options& Device::options() const {
57 return options_;
58}
Long Cheng494fc982019-09-24 19:09:03 +000059
Garrick Evansbcce09e2020-03-10 15:08:04 +090060void Device::set_tap_ifname(const std::string& tap_ifname) {
61 tap_ = tap_ifname;
Garrick Evans2c263102019-07-26 16:07:18 +090062}
63
Garrick Evansbcce09e2020-03-10 15:08:04 +090064const std::string& Device::tap_ifname() const {
65 return tap_;
Garrick Evansa1134d72019-12-02 14:25:37 +090066}
67
Garrick Evans8ff08452019-11-25 09:24:26 +090068bool Device::UsesDefaultInterface() const {
69 return options_.use_default_interface;
Garrick Evans54861622019-07-19 09:05:09 +090070}
71
Hugo Benichiee787ff2019-05-20 16:42:42 +090072std::ostream& operator<<(std::ostream& stream, const Device& device) {
Garrick Evans6c7dcb82020-03-16 15:21:05 +090073 stream << "{ ifname: " << device.phys_ifname_
74 << ", bridge_ifname: " << device.host_ifname_ << ", bridge_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090075 << device.config_->host_ipv4_addr_->ToCidrString()
Garrick Evans6c7dcb82020-03-16 15:21:05 +090076 << ", guest_ifname: " << device.guest_ifname_ << ", guest_ipv4_addr: "
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090077 << device.config_->guest_ipv4_addr_->ToCidrString()
78 << ", guest_mac_addr: "
Garrick Evans6c7dcb82020-03-16 15:21:05 +090079 << MacAddressToString(device.config_->mac_addr())
Hugo Benichibd8ec4d2019-05-28 12:52:49 +090080 << ", fwd_multicast: " << device.options_.fwd_multicast
Garrick Evansb05a7ff2020-02-18 12:59:55 +090081 << ", ipv6_enabled: " << device.options_.ipv6_enabled << '}';
Hugo Benichiee787ff2019-05-20 16:42:42 +090082 return stream;
83}
84
Garrick Evans49879532018-12-03 13:15:36 +090085} // namespace arc_networkd