| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #pragma once | 
| Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "types.hpp" | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 |  | 
| William A. Kennington III | fd862be | 2022-10-09 18:40:55 -0700 | [diff] [blame] | 4 | #include <net/ethernet.h> | 
| William A. Kennington III | e5a48ab | 2019-04-22 03:55:23 -0700 | [diff] [blame] | 5 | #include <netinet/in.h> | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 6 | #include <unistd.h> | 
|  | 7 |  | 
| William A. Kennington III | 4966e96 | 2019-04-08 01:58:10 -0700 | [diff] [blame] | 8 | #include <cstring> | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 9 | #include <filesystem> | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 10 | #include <optional> | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <sdbusplus/bus.hpp> | 
| William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 12 | #include <stdplus/raw.hpp> | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 13 | #include <stdplus/zstring.hpp> | 
| William A. Kennington III | d27410f | 2019-01-30 17:15:43 -0800 | [diff] [blame] | 14 | #include <string> | 
| William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 15 | #include <string_view> | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 16 | #include <unordered_set> | 
| Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 18 |  | 
|  | 19 | namespace phosphor | 
|  | 20 | { | 
|  | 21 | namespace network | 
|  | 22 | { | 
| William A. Kennington III | 20efa79 | 2022-10-04 17:19:08 -0700 | [diff] [blame] | 23 | namespace config | 
|  | 24 | { | 
|  | 25 | class Parser; | 
|  | 26 | } | 
| Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 27 |  | 
| Johnathan Mantey | 817012a | 2020-01-30 15:07:39 -0800 | [diff] [blame] | 28 | using EthernetInterfaceIntf = | 
|  | 29 | sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; | 
|  | 30 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 31 | namespace mac_address | 
|  | 32 | { | 
|  | 33 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 34 | /** @brief gets the MAC address from the Inventory. | 
|  | 35 | *  @param[in] bus - DBUS Bus Object. | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 36 | *  @param[in] intfName - Interface name | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 37 | */ | 
| Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 38 | ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName); | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 39 |  | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 40 | /** @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 Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 43 | */ | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 44 | bool isEmpty(const ether_addr& mac); | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 45 |  | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 46 | /** @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 | */ | 
|  | 50 | bool 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 | */ | 
|  | 56 | bool isUnicast(const ether_addr& mac); | 
|  | 57 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 58 | } // namespace mac_address | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 59 |  | 
| William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 60 | /* @brief converts a sockaddr for the specified address family into | 
|  | 61 | *        a type_safe InAddrAny. | 
| William A. Kennington III | 97b5dc6 | 2022-10-07 14:01:29 -0700 | [diff] [blame] | 62 | * @param[in] family - The address family of the buf | 
| William A. Kennington III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 63 | * @param[in] buf - The network byte order address | 
|  | 64 | */ | 
| William A. Kennington III | 01c816f | 2022-11-07 14:12:43 -0800 | [diff] [blame] | 65 | constexpr 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 III | a00b1c3 | 2019-02-01 18:57:17 -0800 | [diff] [blame] | 76 |  | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 77 | /** @brief Delete the given interface. | 
|  | 78 | *  @param[in] intf - interface name. | 
|  | 79 | */ | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 80 | void deleteInterface(stdplus::const_zstring intf); | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 81 |  | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 82 | /** @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 III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 88 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf); | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 89 |  | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 90 | /** @brief read the IPv6AcceptRA value from the configuration file | 
|  | 91 | *  @param[in] config - The parsed configuration. | 
| Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 92 | */ | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 93 | bool 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 III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 98 | struct DHCPVal | 
|  | 99 | { | 
|  | 100 | bool v4, v6; | 
|  | 101 | }; | 
|  | 102 | DHCPVal getDHCPValue(const config::Parser& config); | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 103 |  | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 104 | /** @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 | */ | 
|  | 108 | bool getDHCPProp(const config::Parser& config, std::string_view key); | 
|  | 109 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 110 | namespace 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 III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 117 | void executeCommandinChildProcess(stdplus::const_zstring path, char** args); | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 118 |  | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 119 | /** @brief Get ignored interfaces from environment */ | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 120 | std::string_view getIgnoredInterfacesEnv(); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 121 |  | 
|  | 122 | /** @brief Parse the comma separated interface names */ | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 123 | std::unordered_set<std::string_view> | 
|  | 124 | parseInterfaces(std::string_view interfaces); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 125 |  | 
|  | 126 | /** @brief Get the ignored interfaces */ | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 127 | const std::unordered_set<std::string_view>& getIgnoredInterfaces(); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 128 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 129 | } // 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 Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 135 | template <typename... ArgTypes> | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 136 | void execute(stdplus::const_zstring path, ArgTypes&&... tArgs) | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 137 | { | 
| William A. Kennington III | 0420c6a | 2019-06-27 14:38:17 -0700 | [diff] [blame] | 138 | using expandType = char*[]; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 139 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 140 | expandType args = {const_cast<char*>(tArgs)..., nullptr}; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 141 |  | 
|  | 142 | internal::executeCommandinChildProcess(path, args); | 
|  | 143 | } | 
|  | 144 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 145 | } // namespace network | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 146 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 147 | } // namespace phosphor |