diff options
| author | Ratan Gupta <ratagupt@in.ibm.com> | 2017-04-14 16:34:56 +0530 |
|---|---|---|
| committer | Patrick Williams <patrick@stwcx.xyz> | 2017-04-27 12:21:34 +0000 |
| commit | 6811f820cdc6ea4792e7a4057b3584dd128bbd16 (patch) | |
| tree | 64a39fca93225da25329ffca82b67a294679eb27 | |
| parent | bf9ba40c716867a8bf1e9c8d268d860ee9518b1b (diff) | |
| download | phosphor-networkd-6811f820cdc6ea4792e7a4057b3584dd128bbd16.tar.gz phosphor-networkd-6811f820cdc6ea4792e7a4057b3584dd128bbd16.zip | |
Implement NetworkManager
Change-Id: Ifa8169d903ecab1575662cfc0f9a1c950d56144f
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
| -rw-r--r-- | Makefile.am | 1 | ||||
| -rw-r--r-- | network_manager.cpp | 118 | ||||
| -rw-r--r-- | network_manager.hpp | 14 |
3 files changed, 133 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am index abc5067..d5bc9b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,6 +15,7 @@ noinst_HEADERS = \ phosphor_network_manager_SOURCES = \ ethernet_interface.cpp \ + network_manager.cpp \ network_manager_main.cpp \ xyz/openbmc_project/Network/VLAN/Create/server.cpp diff --git a/network_manager.cpp b/network_manager.cpp new file mode 100644 index 0000000..479e8a4 --- /dev/null +++ b/network_manager.cpp @@ -0,0 +1,118 @@ +#include "config.h" +#include "network_manager.hpp" + +#include <phosphor-logging/log.hpp> + +#include <algorithm> + +#include <arpa/inet.h> +#include <dirent.h> +#include <net/if.h> + + +namespace phosphor +{ +namespace network +{ +using namespace phosphor::logging; + +Manager::Manager(sdbusplus::bus::bus& bus, const char* objPath): + details::VLANCreateIface(bus, objPath, true) +{ + auto interfaceInfoList = getInterfaceAndaddrs(); + + for( const auto& intfInfo : interfaceInfoList ) + { + std::string objectPath = std::string(OBJ_NETWORK) + "/" + intfInfo.first; + + this->interfaces.emplace(std::make_pair( + intfInfo.first, + std::make_unique< + phosphor::network::EthernetInterface > + (bus, objectPath.c_str(), + false))); + } +} + +void Manager::vLAN(details::IntfName interfaceName, uint16_t id) +{ +} + +details::IntfAddrMap Manager::getInterfaceAndaddrs() const +{ + details::IntfAddrMap intfMap; + details::AddrList addrList; + struct ifaddrs* ifaddr; + // attempt to fill struct with ifaddrs + if (getifaddrs(&ifaddr) == -1) + { + log<level::ERR>("getifaddrs failed:", + entry("ERRNO=%s", strerror(errno))); + + //TODO: openbmc/openbmc#1462 <create the error log> + + return intfMap; + } + + details::AddrPtr ifaddrPtr(ifaddr); + ifaddr = nullptr; + + std::string intfName; + + for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) + { + // walk interfaces + if (ifa->ifa_addr == nullptr) + { + continue; + } + + // get only INET interfaces not ipv6 + if (ifa->ifa_addr->sa_family == AF_INET || + ifa->ifa_addr->sa_family == AF_INET6) + { + // if loopback, or not running ignore + if ((ifa->ifa_flags & IFF_LOOPBACK) || + !(ifa->ifa_flags & IFF_RUNNING)) + { + continue; + } + // if the interface name is not same as the previous + // iteration then add the addr list into + // the map. + if (intfName != "" && intfName != std::string(ifa->ifa_name)) + { + intfMap.emplace(intfName, addrList); + addrList.clear(); + } + intfName = ifa->ifa_name; + details::AddrInfo info; + char tmp[INET6_ADDRSTRLEN] = { 0 }; + + if (ifa->ifa_addr->sa_family == AF_INET) + { + + inet_ntop(ifa->ifa_addr->sa_family, + &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), + tmp, + sizeof(tmp)); + } + else + { + inet_ntop(ifa->ifa_addr->sa_family, + &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), + tmp, + sizeof(tmp)); + + } + + info.addrType = ifa->ifa_addr->sa_family; + info.ipaddress = tmp; + addrList.emplace_back(info); + } + } + intfMap.emplace(intfName, addrList); + return intfMap; +} +}//namespace network +}//namespace phosphor diff --git a/network_manager.hpp b/network_manager.hpp index 51e9d2c..2e90b23 100644 --- a/network_manager.hpp +++ b/network_manager.hpp @@ -4,8 +4,10 @@ #include "xyz/openbmc_project/Network/VLAN/Create/server.hpp" #include <sdbusplus/bus.hpp> +#include <ifaddrs.h> #include <list> +#include <memory> #include <string> #include <vector> @@ -31,6 +33,18 @@ struct AddrInfo { std::string ipaddress; }; +using Addr_t = ifaddrs*; + +struct AddrDeleter +{ + void operator()(Addr_t ptr) const + { + freeifaddrs(ptr); + } +}; + +using AddrPtr = std::unique_ptr<ifaddrs, AddrDeleter>; + using AddrList = std::list<AddrInfo>; using IntfAddrMap = std::map<IntfName, AddrList>; |

