blob: 3638160ba1b98d94c4d0622b58ca0d6f8a72f1e3 [file] [log] [blame]
Ratan Gupta8804feb2017-05-25 10:49:57 +05301#pragma once
Ratan Gupta3681a502017-06-17 19:20:04 +05302#include "types.hpp"
Patrick Venture189d44e2018-07-09 12:30:59 -07003
William A. Kennington IIIfd862be2022-10-09 18:40:55 -07004#include <net/ethernet.h>
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -07005#include <netinet/in.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07006#include <unistd.h>
7
William A. Kennington III4966e962019-04-08 01:58:10 -07008#include <cstring>
William A. Kennington IIIa520a392022-08-08 12:17:34 -07009#include <filesystem>
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070010#include <optional>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <sdbusplus/bus.hpp>
William A. Kennington III01c816f2022-11-07 14:12:43 -080012#include <stdplus/raw.hpp>
William A. Kennington III69f45542022-09-24 23:28:14 -070013#include <stdplus/zstring.hpp>
William A. Kennington IIId27410f2019-01-30 17:15:43 -080014#include <string>
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080015#include <string_view>
William A. Kennington III95530ec2022-08-19 01:44:39 -070016#include <unordered_set>
Johnathan Mantey817012a2020-01-30 15:07:39 -080017#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053018
19namespace phosphor
20{
21namespace network
22{
William A. Kennington III20efa792022-10-04 17:19:08 -070023namespace config
24{
25class Parser;
26}
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -050027
Johnathan Mantey817012a2020-01-30 15:07:39 -080028using EthernetInterfaceIntf =
29 sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
30
Ratan Guptabd303b12017-08-18 17:10:07 +053031namespace mac_address
32{
33
Ratan Guptabd303b12017-08-18 17:10:07 +053034/** @brief gets the MAC address from the Inventory.
35 * @param[in] bus - DBUS Bus Object.
Alvin Wang38a63c32019-08-29 22:56:46 +080036 * @param[in] intfName - Interface name
Ratan Guptabd303b12017-08-18 17:10:07 +053037 */
Patrick Williamsc38b0712022-07-22 19:26:54 -050038ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName);
William A. Kennington III1137a972019-04-20 20:49:58 -070039
William A. Kennington III1137a972019-04-20 20:49:58 -070040/** @brief Determines if the mac address is empty
41 * @param[in] mac - The mac address
42 * @return True if 00:00:00:00:00:00
Ratan Guptabd303b12017-08-18 17:10:07 +053043 */
William A. Kennington III1137a972019-04-20 20:49:58 -070044bool isEmpty(const ether_addr& mac);
Ratan Guptabd303b12017-08-18 17:10:07 +053045
William A. Kennington III1137a972019-04-20 20:49:58 -070046/** @brief Determines if the mac address is a multicast address
47 * @param[in] mac - The mac address
48 * @return True if multicast bit is set
49 */
50bool isMulticast(const ether_addr& mac);
51
52/** @brief Determines if the mac address is a unicast address
53 * @param[in] mac - The mac address
54 * @return True if not multicast or empty
55 */
56bool isUnicast(const ether_addr& mac);
57
Gunnar Mills57d9c502018-09-14 14:42:34 -050058} // namespace mac_address
Ratan Gupta8804feb2017-05-25 10:49:57 +053059
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080060/* @brief converts a sockaddr for the specified address family into
61 * a type_safe InAddrAny.
William A. Kennington III97b5dc62022-10-07 14:01:29 -070062 * @param[in] family - The address family of the buf
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080063 * @param[in] buf - The network byte order address
64 */
William A. Kennington III01c816f2022-11-07 14:12:43 -080065constexpr InAddrAny addrFromBuf(int family, std::string_view buf)
66{
67 switch (family)
68 {
69 case AF_INET:
70 return stdplus::raw::copyFromStrict<in_addr>(buf);
71 case AF_INET6:
72 return stdplus::raw::copyFromStrict<in6_addr>(buf);
73 }
74 throw std::invalid_argument("Unrecognized family");
75}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -080076
Ratan Guptabc886292017-07-25 18:29:57 +053077/** @brief Delete the given interface.
78 * @param[in] intf - interface name.
79 */
William A. Kennington III69f45542022-09-24 23:28:14 -070080void deleteInterface(stdplus::const_zstring intf);
Ratan Guptabc886292017-07-25 18:29:57 +053081
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070082/** @brief Converts the interface name into a u-boot environment
83 * variable that would hold its ethernet address.
84 *
85 * @param[in] intf - interface name
86 * @return The name of th environment key
87 */
William A. Kennington III69f45542022-09-24 23:28:14 -070088std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070089
William A. Kennington IIIa520a392022-08-08 12:17:34 -070090/** @brief read the IPv6AcceptRA value from the configuration file
91 * @param[in] config - The parsed configuration.
Ratan Gupta56187e72017-08-13 09:40:14 +053092 */
William A. Kennington IIIa520a392022-08-08 12:17:34 -070093bool getIPv6AcceptRA(const config::Parser& config);
94
95/** @brief read the DHCP value from the configuration file
96 * @param[in] config - The parsed configuration.
97 */
William A. Kennington III8060c0d2022-08-18 19:19:34 -070098struct DHCPVal
99{
100 bool v4, v6;
101};
102DHCPVal getDHCPValue(const config::Parser& config);
Ratan Guptabc886292017-07-25 18:29:57 +0530103
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700104/** @brief Read a boolean DHCP property from a conf file
105 * @param[in] config - The parsed configuration.
106 * @param[in] key - The property name.
107 */
108bool getDHCPProp(const config::Parser& config, std::string_view key);
109
Ratan Guptabd303b12017-08-18 17:10:07 +0530110namespace internal
111{
112
113/* @brief runs the given command in child process.
114 * @param[in] path - path of the binary file which needs to be execeuted.
115 * @param[in] args - arguments of the command.
116 */
William A. Kennington III69f45542022-09-24 23:28:14 -0700117void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
Ratan Guptabd303b12017-08-18 17:10:07 +0530118
Lei YU307554e2021-03-18 14:56:50 +0800119/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700120std::string_view getIgnoredInterfacesEnv();
Lei YU307554e2021-03-18 14:56:50 +0800121
122/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700123std::unordered_set<std::string_view>
124 parseInterfaces(std::string_view interfaces);
Lei YU307554e2021-03-18 14:56:50 +0800125
126/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700127const std::unordered_set<std::string_view>& getIgnoredInterfaces();
Lei YU307554e2021-03-18 14:56:50 +0800128
Ratan Guptabd303b12017-08-18 17:10:07 +0530129} // namespace internal
130
131/* @brief runs the given command in child process.
132 * @param[in] path -path of the binary file which needs to be execeuted.
133 * @param[in] tArgs - arguments of the command.
134 */
Gunnar Mills57d9c502018-09-14 14:42:34 -0500135template <typename... ArgTypes>
William A. Kennington III69f45542022-09-24 23:28:14 -0700136void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
Ratan Guptabd303b12017-08-18 17:10:07 +0530137{
William A. Kennington III0420c6a2019-06-27 14:38:17 -0700138 using expandType = char*[];
Ratan Guptabd303b12017-08-18 17:10:07 +0530139
Gunnar Mills57d9c502018-09-14 14:42:34 -0500140 expandType args = {const_cast<char*>(tArgs)..., nullptr};
Ratan Guptabd303b12017-08-18 17:10:07 +0530141
142 internal::executeCommandinChildProcess(path, args);
143}
144
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145} // namespace network
Ratan Gupta8804feb2017-05-25 10:49:57 +0530146
Gunnar Mills57d9c502018-09-14 14:42:34 -0500147} // namespace phosphor