blob: 615c282d6140808d2935093df60321a7107f246e [file] [log] [blame]
William A. Kennington III2e09d272022-10-14 17:15:00 -07001#pragma once
2#include "types.hpp"
3
William A. Kennington IIIfd862be2022-10-09 18:40:55 -07004#include <net/ethernet.h>
William A. Kennington III2e09d272022-10-14 17:15:00 -07005
6#include <cstdint>
7#include <optional>
8#include <stdplus/zstring.hpp>
9#include <stdplus/zstring_view.hpp>
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070010#include <string>
William A. Kennington III2e09d272022-10-14 17:15:00 -070011#include <string_view>
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070012#include <vector>
13
14struct nlmsghdr;
William A. Kennington III2e09d272022-10-14 17:15:00 -070015
16namespace phosphor::network::system
17{
William A. Kennington III2e09d272022-10-14 17:15:00 -070018struct EthInfo
19{
20 bool autoneg;
21 uint16_t speed;
22};
23EthInfo getEthInfo(stdplus::zstring_view ifname);
24
25bool intfIsRunning(std::string_view ifname);
26
William A. Kennington III2e09d272022-10-14 17:15:00 -070027std::optional<unsigned> getMTU(stdplus::zstring_view ifname);
28
29void setMTU(std::string_view ifname, unsigned mtu);
30
31void setNICUp(std::string_view ifname, bool up);
32
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070033/** @class InterfaceInfo
34 * @brief Information about interfaces from the kernel
35 */
36struct 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;
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070043 std::optional<unsigned> parent_idx = std::nullopt;
44 std::optional<std::string> kind = std::nullopt;
45 std::optional<uint16_t> vlan_id = std::nullopt;
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070046
47 constexpr bool operator==(const InterfaceInfo& rhs) const noexcept
48 {
49 return idx == rhs.idx && flags == rhs.flags && name == rhs.name &&
William A. Kennington III09f3a4a2022-10-25 02:59:16 -070050 mac == rhs.mac && mtu == rhs.mtu &&
51 parent_idx == rhs.parent_idx && kind == rhs.kind &&
52 vlan_id == rhs.vlan_id;
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070053 }
54};
55
William A. Kennington III6a923632022-11-06 18:17:33 -080056struct AddressFilter
57{
58 unsigned ifidx = 0;
59};
60
William A. Kennington IIIa8426902022-11-07 15:37:41 -080061struct NeighborFilter
62{
63 unsigned ifidx = 0;
64};
65
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070066namespace detail
67{
68InterfaceInfo parseInterface(const nlmsghdr& hdr, std::string_view msg);
69bool validateNewInterface(const InterfaceInfo& info);
William A. Kennington III6a923632022-11-06 18:17:33 -080070bool validateNewAddr(const AddressInfo& info,
71 const AddressFilter& filter) noexcept;
William A. Kennington IIIa8426902022-11-07 15:37:41 -080072bool validateNewNeigh(const NeighborInfo& info,
73 const NeighborFilter& filter) noexcept;
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070074} // namespace detail
75
William A. Kennington III2e09d272022-10-14 17:15:00 -070076/** @brief Get all the interfaces from the system.
77 * @returns list of interface names.
78 */
William A. Kennington IIIfd862be2022-10-09 18:40:55 -070079std::vector<InterfaceInfo> getInterfaces();
William A. Kennington III2e09d272022-10-14 17:15:00 -070080
William A. Kennington III6a923632022-11-06 18:17:33 -080081/** @brief Get all the addreses from the system.
82 * @returns list of addresses
83 */
84std::vector<AddressInfo> getAddresses(const AddressFilter& filter);
85
William A. Kennington IIIa8426902022-11-07 15:37:41 -080086/** @brief Returns a list of system neighbor table
87 */
88std::vector<NeighborInfo> getNeighbors(const NeighborFilter& filter);
89
William A. Kennington III2e09d272022-10-14 17:15:00 -070090} // namespace phosphor::network::system