blob: 4e52cc89817876be2fde07d5f7e4da143c78a620 [file] [log] [blame]
Brandon Kimdab96f12021-02-18 11:21:37 -08001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
William A. Kennington III7d6fa422021-02-08 17:04:02 -080015#include "net_config.h"
16
William A. Kennington III7d6fa422021-02-08 17:04:02 -080017#include <sys/types.h>
18#include <sys/wait.h>
19#include <unistd.h>
20
21#include <sdbusplus/bus.hpp>
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -070022#include <stdplus/fd/create.hpp>
23#include <stdplus/fd/ops.hpp>
Willy Tu253e6462023-12-28 09:57:27 -080024#include <stdplus/print.hpp>
William A. Kennington III7d6fa422021-02-08 17:04:02 -080025#include <stdplus/util/string.hpp>
26
27#include <cstdio>
28#include <cstring>
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -070029#include <filesystem>
Willy Tubb531612023-07-16 01:30:08 -070030#include <format>
William A. Kennington III7d6fa422021-02-08 17:04:02 -080031#include <utility>
32#include <variant>
33
34/* Most of the code for interacting with DBus is from
35 * phosphor-host-ipmid/utils.cpp
36 */
37
38namespace net
39{
40
41namespace
42{
43
44constexpr auto IFACE_ROOT = "/xyz/openbmc_project/network/";
45constexpr auto MAC_FORMAT = "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx";
46// 2 chars for every byte + 5 colons + Null byte
47constexpr auto MAC_FORMAT_LENGTH = 6 * 2 + 5 + 1;
48constexpr auto MAC_INTERFACE = "xyz.openbmc_project.Network.MACAddress";
49constexpr auto NETWORK_SERVICE = "xyz.openbmc_project.Network";
50constexpr auto PROP_INTERFACE = "org.freedesktop.DBus.Properties";
51
52int parse_mac(const std::string& mac_addr, mac_addr_t* mac)
53{
Patrick Williams2be45232023-05-10 07:51:22 -050054 int ret = sscanf(mac_addr.c_str(), MAC_FORMAT, mac->octet, mac->octet + 1,
55 mac->octet + 2, mac->octet + 3, mac->octet + 4,
56 mac->octet + 5);
William A. Kennington III7d6fa422021-02-08 17:04:02 -080057
58 return ret < 6 ? -1 : 0;
59}
60
61std::string format_mac(const mac_addr_t& mac)
62{
63 // 2 chars for every byte + 5 colons + Null byte
64 char mac_str[MAC_FORMAT_LENGTH];
65 snprintf(mac_str, sizeof(mac_str), MAC_FORMAT, mac.octet[0], mac.octet[1],
66 mac.octet[2], mac.octet[3], mac.octet[4], mac.octet[5]);
67
68 return std::string{mac_str};
69}
70
71} // namespace
72
73PhosphorConfig::PhosphorConfig(const std::string& iface_name) :
74 iface_name_{iface_name}, iface_path_{std::string(IFACE_ROOT) + iface_name},
75 shared_host_mac_(std::experimental::nullopt),
76 bus(sdbusplus::bus::new_default())
77{}
78
Patrick Williams59ac2c22022-07-22 19:26:57 -050079sdbusplus::message_t PhosphorConfig::new_networkd_call(sdbusplus::bus_t* dbus,
80 bool get) const
William A. Kennington III7d6fa422021-02-08 17:04:02 -080081{
82 auto networkd_call =
83 dbus->new_method_call(NETWORK_SERVICE, iface_path_.c_str(),
84 PROP_INTERFACE, get ? "Get" : "Set");
85
86 networkd_call.append(MAC_INTERFACE, "MACAddress");
87
88 return networkd_call;
89}
90
91int PhosphorConfig::get_mac_addr(mac_addr_t* mac)
92{
93 if (mac == nullptr)
94 {
Willy Tu253e6462023-12-28 09:57:27 -080095 stdplus::println(stderr, "mac is nullptr");
William A. Kennington III7d6fa422021-02-08 17:04:02 -080096 return -1;
97 }
98
99 // Cache hit: we have stored host MAC.
100 if (shared_host_mac_)
101 {
102 *mac = shared_host_mac_.value();
103 }
104 else // Cache miss: read MAC over DBus, and store in cache.
105 {
106 std::string mac_string;
107 try
108 {
109 auto networkd_call = new_networkd_call(&bus, true);
110 auto reply = bus.call(networkd_call);
111 std::variant<std::string> result;
112 reply.read(result);
113 mac_string = std::get<std::string>(result);
114 }
115 catch (const sdbusplus::exception::SdBusError& ex)
116 {
Willy Tu253e6462023-12-28 09:57:27 -0800117 stdplus::println(stderr, "Failed to get MACAddress: {}", ex.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800118 return -1;
119 }
120
121 if (parse_mac(mac_string, mac) < 0)
122 {
Willy Tu253e6462023-12-28 09:57:27 -0800123 stdplus::println(stderr, "Failed to parse MAC Address `{}`",
124 mac_string);
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800125 return -1;
126 }
127
128 shared_host_mac_ = *mac;
129 }
130
131 return 0;
132}
133
134int PhosphorConfig::set_mac_addr(const mac_addr_t& mac)
135{
136 auto networkd_call = new_networkd_call(&bus, false);
137 std::variant<std::string> mac_value(format_mac(mac));
138 networkd_call.append(mac_value);
139
140 try
141 {
Willy Tubb531612023-07-16 01:30:08 -0700142 auto netdir = std::format("/run/systemd/network/00-bmc-{}.network.d",
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700143 iface_name_);
144 std::filesystem::create_directories(netdir);
Willy Tubb531612023-07-16 01:30:08 -0700145 auto netfile = std::format("{}/60-ncsi-mac.conf", netdir);
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700146 auto fd = stdplus::fd::open(
147 netfile,
148 stdplus::fd::OpenFlags(stdplus::fd::OpenAccess::WriteOnly)
149 .set(stdplus::fd::OpenFlag::Create),
150 0644);
Willy Tubb531612023-07-16 01:30:08 -0700151 auto contents = std::format("[Link]\nMACAddress={}\n",
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700152 std::get<std::string>(mac_value));
153 stdplus::fd::writeExact(fd, contents);
154 }
155 catch (const std::exception& ex)
156 {
Willy Tu253e6462023-12-28 09:57:27 -0800157 stdplus::println(stderr, "Failed to set MAC Addr `{}` writing file: {}",
158 std::get<std::string>(mac_value), ex.what());
William A. Kennington IIIa5c9d7a2022-05-11 13:55:22 -0700159 return -1;
160 }
161
162 try
163 {
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800164 auto reply = bus.call(networkd_call);
165 }
166 catch (const sdbusplus::exception::SdBusError& ex)
167 {
Willy Tu253e6462023-12-28 09:57:27 -0800168 stdplus::println(stderr, "Failed to set MAC Addr `{}`: {}",
169 std::get<std::string>(mac_value), ex.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800170 return -1;
171 }
172
173 shared_host_mac_ = std::experimental::nullopt;
174 return 0;
175}
176
177int PhosphorConfig::set_nic_hostless(bool is_nic_hostless)
178{
179 // Ensure that we don't trigger the target multiple times. This is
180 // undesirable because it will cause any inactive services to re-trigger
181 // every time we run this code. Since the loop calling this executes this
182 // code every 1s, we don't want to keep re-executing services. A fresh
183 // start of the daemon will always trigger the service to ensure system
184 // consistency.
185 if (was_nic_hostless_ && is_nic_hostless == *was_nic_hostless_)
186 {
187 return 0;
188 }
189
190 static constexpr auto systemdService = "org.freedesktop.systemd1";
191 static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
192 static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
193
194 auto method = bus.new_method_call(systemdService, systemdRoot,
195 systemdInterface, "StartUnit");
196 if (is_nic_hostless)
197 {
198 method.append(
199 stdplus::util::strCat("nic-hostless@", iface_name_, ".target"));
200 }
201 else
202 {
203 method.append(
204 stdplus::util::strCat("nic-hostful@", iface_name_, ".target"));
205 }
206
207 // Specify --job-mode (see systemctl(1) for detail).
208 method.append("replace");
209
210 try
211 {
212 bus.call_noreply(method);
213 was_nic_hostless_ = is_nic_hostless;
214 return 0;
215 }
216 catch (const sdbusplus::exception::SdBusError& ex)
217 {
Willy Tu253e6462023-12-28 09:57:27 -0800218 stdplus::println(stderr, "Failed to set systemd nic status: {}",
219 ex.what());
William A. Kennington III7d6fa422021-02-08 17:04:02 -0800220 return 1;
221 }
222}
223
224} // namespace net