1 #pragma once 2 #include "types.hpp" 3 4 #include <net/ethernet.h> 5 6 #include <cstdint> 7 #include <optional> 8 #include <stdplus/zstring.hpp> 9 #include <stdplus/zstring_view.hpp> 10 #include <string> 11 #include <string_view> 12 #include <vector> 13 14 struct nlmsghdr; 15 16 namespace phosphor::network::system 17 { 18 struct EthInfo 19 { 20 bool autoneg; 21 uint16_t speed; 22 }; 23 EthInfo getEthInfo(stdplus::zstring_view ifname); 24 25 bool intfIsRunning(std::string_view ifname); 26 27 std::optional<unsigned> getMTU(stdplus::zstring_view ifname); 28 29 void setMTU(std::string_view ifname, unsigned mtu); 30 31 void setNICUp(std::string_view ifname, bool up); 32 33 /** @class InterfaceInfo 34 * @brief Information about interfaces from the kernel 35 */ 36 struct InterfaceInfo 37 { 38 unsigned idx; 39 unsigned flags; 40 std::optional<std::string> name = std::nullopt; 41 std::optional<ether_addr> mac = std::nullopt; 42 std::optional<unsigned> mtu = std::nullopt; 43 std::optional<unsigned> parent_idx = std::nullopt; 44 std::optional<std::string> kind = std::nullopt; 45 std::optional<uint16_t> vlan_id = std::nullopt; 46 47 constexpr bool operator==(const InterfaceInfo& rhs) const noexcept 48 { 49 return idx == rhs.idx && flags == rhs.flags && name == rhs.name && 50 mac == rhs.mac && mtu == rhs.mtu && 51 parent_idx == rhs.parent_idx && kind == rhs.kind && 52 vlan_id == rhs.vlan_id; 53 } 54 }; 55 56 struct AddressFilter 57 { 58 unsigned ifidx = 0; 59 }; 60 61 struct NeighborFilter 62 { 63 unsigned ifidx = 0; 64 }; 65 66 namespace detail 67 { 68 InterfaceInfo parseInterface(const nlmsghdr& hdr, std::string_view msg); 69 bool validateNewInterface(const InterfaceInfo& info); 70 bool validateNewAddr(const AddressInfo& info, 71 const AddressFilter& filter) noexcept; 72 bool validateNewNeigh(const NeighborInfo& info, 73 const NeighborFilter& filter) noexcept; 74 } // namespace detail 75 76 /** @brief Get all the interfaces from the system. 77 * @returns list of interface names. 78 */ 79 std::vector<InterfaceInfo> getInterfaces(); 80 81 /** @brief Get all the addreses from the system. 82 * @returns list of addresses 83 */ 84 std::vector<AddressInfo> getAddresses(const AddressFilter& filter); 85 86 /** @brief Returns a list of system neighbor table 87 */ 88 std::vector<NeighborInfo> getNeighbors(const NeighborFilter& filter); 89 90 } // namespace phosphor::network::system 91