Add network static gateway configuration support

This commit enables static gateway configuration on EthernetInterface
Implements CreateStaticGateway method which creates a new d-bus object
with StaticGateway interface.

Tested By:
Run StaticGateway D-bus method and verified D-bus object and
configuration.
Delete StaticGateway object
Add static gateway
Delete static gateway

Change-Id: I3fbc6f85ede00b6c1949a0ac85f501037a69c831
Signed-off-by: Ravi Teja <raviteja28031990@gmail.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 10229ac..8e76931 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -7,6 +7,8 @@
 #include "system_queries.hpp"
 #include "util.hpp"
 
+#include <arpa/inet.h>
+#include <fcntl.h>
 #include <linux/rtnetlink.h>
 #include <net/if.h>
 #include <net/if_arp.h>
@@ -134,6 +136,10 @@
     {
         addStaticNeigh(neigh);
     }
+    for (const auto& [_, staticGateway] : info.staticGateways)
+    {
+        addStaticGateway(staticGateway);
+    }
 }
 
 void EthernetInterface::updateInfo(const InterfaceInfo& info, bool skipSignal)
@@ -224,6 +230,39 @@
     }
 }
 
+void EthernetInterface::addStaticGateway(const StaticGatewayInfo& info)
+{
+    if (!info.gateway)
+    {
+        lg2::error("Missing static gateway on {NET_INTF}", "NET_INTF",
+                   interfaceName());
+        return;
+    }
+
+    IP::Protocol protocolType;
+    if (*info.protocol == "IPv4")
+    {
+        protocolType = IP::Protocol::IPv4;
+    }
+    else if (*info.protocol == "IPv6")
+    {
+        protocolType = IP::Protocol::IPv6;
+    }
+
+    if (auto it = staticGateways.find(*info.gateway);
+        it != staticGateways.end())
+    {
+        it->second->StaticGatewayObj::gateway(*info.gateway);
+    }
+    else
+    {
+        staticGateways.emplace(*info.gateway,
+                               std::make_unique<StaticGateway>(
+                                   bus, std::string_view(objPath), *this,
+                                   *info.gateway, protocolType));
+    }
+}
+
 ObjectPath EthernetInterface::ip(IP::Protocol protType, std::string ipaddress,
                                  uint8_t prefixLength, std::string)
 {
@@ -347,6 +386,43 @@
     return it->second->getObjPath();
 }
 
+ObjectPath EthernetInterface::staticGateway(std::string gateway,
+                                            IP::Protocol protocolType)
+{
+    std::optional<stdplus::InAnyAddr> addr;
+    std::string route;
+    try
+    {
+        addr.emplace(stdplus::fromStr<stdplus::InAnyAddr>(gateway));
+        route = gateway;
+    }
+    catch (const std::exception& e)
+    {
+        lg2::error("Not a valid IP address {GATEWAY}: {ERROR}", "GATEWAY",
+                   gateway, "ERROR", e);
+        elog<InvalidArgument>(Argument::ARGUMENT_NAME("gateway"),
+                              Argument::ARGUMENT_VALUE(gateway.c_str()));
+    }
+
+    auto it = staticGateways.find(route);
+    if (it == staticGateways.end())
+    {
+        it = std::get<0>(staticGateways.emplace(
+            route,
+            std::make_unique<StaticGateway>(bus, std::string_view(objPath),
+                                            *this, gateway, protocolType)));
+    }
+    else
+    {
+        it->second->StaticGatewayObj::gateway(gateway);
+    }
+
+    writeConfigurationFile();
+    manager.get().reloadConfigs();
+
+    return it->second->getObjPath();
+}
+
 bool EthernetInterface::ipv6AcceptRA(bool value)
 {
     if (ipv6AcceptRA() != EthernetInterfaceIntf::ipv6AcceptRA(value))
@@ -788,6 +864,17 @@
         dhcp6["SendHostname"].emplace_back(
             tfStr(dhcp6Conf->sendHostNameEnabled()));
     }
+
+    {
+        auto& sroutes = config.map["Route"];
+        for (const auto& temp : staticGateways)
+        {
+            auto& staticGateway = sroutes.emplace_back();
+            staticGateway["Gateway"].emplace_back(temp.second->gateway());
+            staticGateway["GatewayOnLink"].emplace_back("true");
+        }
+    }
+
     auto path =
         config::pathForIntfConf(manager.get().getConfDir(), interfaceName());
     config.writeFile(path);
diff --git a/src/ethernet_interface.hpp b/src/ethernet_interface.hpp
index c7af241..3140528 100644
--- a/src/ethernet_interface.hpp
+++ b/src/ethernet_interface.hpp
@@ -2,9 +2,11 @@
 #include "dhcp_configuration.hpp"
 #include "ipaddress.hpp"
 #include "neighbor.hpp"
+#include "static_gateway.hpp"
 #include "types.hpp"
 #include "xyz/openbmc_project/Network/IP/Create/server.hpp"
 #include "xyz/openbmc_project/Network/Neighbor/CreateStatic/server.hpp"
+#include "xyz/openbmc_project/Network/StaticGateway/Create/server.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/server/object.hpp>
@@ -14,6 +16,7 @@
 #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
 #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
 #include <xyz/openbmc_project/Network/MACAddress/server.hpp>
+#include <xyz/openbmc_project/Network/StaticGateway/server.hpp>
 #include <xyz/openbmc_project/Network/VLAN/server.hpp>
 #include <xyz/openbmc_project/Object/Delete/server.hpp>
 
@@ -31,6 +34,7 @@
     sdbusplus::xyz::openbmc_project::Network::server::MACAddress,
     sdbusplus::xyz::openbmc_project::Network::IP::server::Create,
     sdbusplus::xyz::openbmc_project::Network::Neighbor::server::CreateStatic,
+    sdbusplus::xyz::openbmc_project::Network::StaticGateway::server::Create,
     sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll>;
 
 using VlanIfaces = sdbusplus::server::object_t<
@@ -45,6 +49,8 @@
     sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
 using MacAddressIntf =
     sdbusplus::xyz::openbmc_project::Network::server::MACAddress;
+using StaticGatewayIntf =
+    sdbusplus::xyz::openbmc_project::Network::server::StaticGateway;
 
 using ServerList = std::vector<std::string>;
 using ObjectPath = sdbusplus::message::object_path;
@@ -94,8 +100,13 @@
     std::unordered_map<stdplus::InAnyAddr, std::unique_ptr<Neighbor>>
         staticNeighbors;
 
+    /** @brief Persistent map of static route dbus objects and their names */
+    std::unordered_map<std::string, std::unique_ptr<StaticGateway>>
+        staticGateways;
+
     void addAddr(const AddressInfo& info);
     void addStaticNeigh(const NeighborInfo& info);
+    void addStaticGateway(const StaticGatewayInfo& info);
 
     /** @brief Updates the interface information based on new InterfaceInfo */
     void updateInfo(const InterfaceInfo& info, bool skipSignal = false);
@@ -123,6 +134,14 @@
      */
     ObjectPath neighbor(std::string ipAddress, std::string macAddress) override;
 
+    /** @brief Function to create static route dbus object.
+     *  @param[in] destination - Destination IP address.
+     *  @param[in] gateway - Gateway
+     *  @parma[in] prefixLength - Number of network bits.
+     */
+    ObjectPath staticGateway(std::string gateway,
+                             IP::Protocol protocolType) override;
+
     /** Set value of DHCPEnabled */
     DHCPConf dhcpEnabled() const override;
     DHCPConf dhcpEnabled(DHCPConf value) override;
diff --git a/src/meson.build b/src/meson.build
index ee9778d..1a5084b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -47,6 +47,7 @@
   'ethernet_interface.cpp',
   'neighbor.cpp',
   'ipaddress.cpp',
+  'static_gateway.cpp',
   'netlink.cpp',
   'network_manager.cpp',
   'rtnetlink.cpp',
diff --git a/src/static_gateway.cpp b/src/static_gateway.cpp
new file mode 100644
index 0000000..f1e9db4
--- /dev/null
+++ b/src/static_gateway.cpp
@@ -0,0 +1,77 @@
+#include "static_gateway.hpp"
+
+#include "ethernet_interface.hpp"
+#include "network_manager.hpp"
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <string>
+
+namespace phosphor
+{
+namespace network
+{
+
+static auto makeObjPath(std::string_view root, std::string addr)
+{
+    auto ret = sdbusplus::message::object_path(std::string(root));
+    ret /= addr;
+    return ret;
+}
+
+StaticGateway::StaticGateway(sdbusplus::bus_t& bus, std::string_view objRoot,
+                             stdplus::PinnedRef<EthernetInterface> parent,
+                             std::string gateway, IP::Protocol protocolType) :
+    StaticGateway(bus, makeObjPath(objRoot, gateway), parent, gateway,
+                  protocolType)
+{}
+
+StaticGateway::StaticGateway(sdbusplus::bus_t& bus,
+                             sdbusplus::message::object_path objPath,
+                             stdplus::PinnedRef<EthernetInterface> parent,
+                             std::string gateway, IP::Protocol protocolType) :
+    StaticGatewayObj(bus, objPath.str.c_str(),
+                     StaticGatewayObj::action::defer_emit),
+    parent(parent), objPath(std::move(objPath))
+{
+    StaticGatewayObj::gateway(gateway, true);
+    StaticGatewayObj::protocolType(protocolType, true);
+    emit_object_added();
+}
+
+void StaticGateway::delete_()
+{
+    auto& staticGateways = parent.get().staticGateways;
+    std::unique_ptr<StaticGateway> ptr;
+    for (auto it = staticGateways.begin(); it != staticGateways.end(); ++it)
+    {
+        if (it->second.get() == this)
+        {
+            ptr = std::move(it->second);
+            staticGateways.erase(it);
+            break;
+        }
+    }
+
+    parent.get().writeConfigurationFile();
+    parent.get().manager.get().reloadConfigs();
+}
+
+using sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
+using REASON =
+    phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON;
+using phosphor::logging::elog;
+
+std::string StaticGateway::gateway(std::string /*gateway*/)
+{
+    elog<NotAllowed>(REASON("Property update is not allowed"));
+}
+
+IP::Protocol StaticGateway::protocolType(IP::Protocol /*protocolType*/)
+{
+    elog<NotAllowed>(REASON("Property update is not allowed"));
+}
+} // namespace network
+} // namespace phosphor
diff --git a/src/static_gateway.hpp b/src/static_gateway.hpp
new file mode 100644
index 0000000..ee79067
--- /dev/null
+++ b/src/static_gateway.hpp
@@ -0,0 +1,74 @@
+#pragma once
+#include "types.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/message/native_types.hpp>
+#include <sdbusplus/server/object.hpp>
+#include <stdplus/pinned.hpp>
+#include <xyz/openbmc_project/Network/IP/server.hpp>
+#include <xyz/openbmc_project/Network/StaticGateway/server.hpp>
+#include <xyz/openbmc_project/Object/Delete/server.hpp>
+
+#include <string_view>
+
+namespace phosphor
+{
+namespace network
+{
+
+using StaticGatewayIntf =
+    sdbusplus::xyz::openbmc_project::Network::server::StaticGateway;
+
+using StaticGatewayObj = sdbusplus::server::object_t<
+    StaticGatewayIntf, sdbusplus::xyz::openbmc_project::Object::server::Delete>;
+
+using IP = sdbusplus::xyz::openbmc_project::Network::server::IP;
+
+class EthernetInterface;
+
+/** @class StaticGateway
+ *  @brief OpenBMC network static gateway implementation.
+ *  @details A concrete implementation for the
+ *  xyz.openbmc_project.Network.StaticGateway dbus interface.
+ */
+class StaticGateway : public StaticGatewayObj
+{
+  public:
+    /** @brief Constructor to put object onto bus at a dbus path.
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] objRoot - Path to attach at.
+     *  @param[in] parent - Parent object.
+     *  @param[in] gateway - Gateway address.
+     */
+    StaticGateway(sdbusplus::bus_t& bus, std::string_view objRoot,
+                  stdplus::PinnedRef<EthernetInterface> parent,
+                  std::string gateway, IP::Protocol protocolType);
+
+    /** @brief Delete this d-bus object.
+     */
+    void delete_() override;
+
+    using StaticGatewayObj::gateway;
+    std::string gateway(std::string) override;
+    using StaticGatewayObj::protocolType;
+    IP::Protocol protocolType(IP::Protocol) override;
+    inline const auto& getObjPath() const
+    {
+        return objPath;
+    }
+
+  private:
+    /** @brief Parent Object. */
+    stdplus::PinnedRef<EthernetInterface> parent;
+
+    /** @brief Dbus object path */
+    sdbusplus::message::object_path objPath;
+
+    StaticGateway(sdbusplus::bus_t& bus,
+                  sdbusplus::message::object_path objPath,
+                  stdplus::PinnedRef<EthernetInterface> parent,
+                  std::string gateway, IP::Protocol protocolType);
+};
+
+} // namespace network
+} // namespace phosphor
diff --git a/src/types.hpp b/src/types.hpp
index 6195c3f..b2e7bd6 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -79,6 +79,21 @@
     }
 };
 
+/** @class StaticGatewayInfo
+ *  @brief Information about a static gateway from the kernel
+ */
+struct StaticGatewayInfo
+{
+    unsigned ifidx;
+    std::optional<std::string> gateway;
+    std::optional<std::string> protocol;
+
+    constexpr bool operator==(const StaticGatewayInfo& rhs) const noexcept
+    {
+        return ifidx == rhs.ifidx && gateway == rhs.gateway;
+    }
+};
+
 /** @brief Contains all of the object information about the interface */
 struct AllIntfInfo
 {
@@ -87,6 +102,7 @@
     std::optional<stdplus::In6Addr> defgw6 = std::nullopt;
     std::unordered_map<stdplus::SubnetAny, AddressInfo> addrs = {};
     std::unordered_map<stdplus::InAnyAddr, NeighborInfo> staticNeighs = {};
+    std::unordered_map<std::string, StaticGatewayInfo> staticGateways = {};
 };
 
 } // namespace phosphor::network
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index 72579f3..146fb20 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -54,6 +54,12 @@
         return interface.ip(addressType, ipaddress, subnetMask, "");
     }
 
+    auto createStaticGatewayObject(const std::string& gateway,
+                                   IP::Protocol protocol)
+    {
+        return interface.staticGateway(gateway, protocol);
+    }
+
     void setNtpServers()
     {
         ServerList ntpServers = {"10.1.1.1", "10.2.2.2", "10.3.3.3"};
@@ -253,5 +259,47 @@
     set_test(DHCPConf::both, /*dhcp4=*/true, /*dhcp6=*/true, /*ra=*/true);
 }
 
+TEST_F(TestEthernetInterface, AddStaticGateway)
+{
+    createStaticGatewayObject("10.10.10.1", IP::Protocol::IPv4);
+    EXPECT_THAT(interface.staticGateways,
+                UnorderedElementsAre(Key(std::string("10.10.10.1"))));
+}
+
+TEST_F(TestEthernetInterface, AddMultipleStaticGateways)
+{
+    createStaticGatewayObject("10.10.10.1", IP::Protocol::IPv4);
+    createStaticGatewayObject("10.20.30.1", IP::Protocol::IPv4);
+    EXPECT_THAT(interface.staticGateways,
+                UnorderedElementsAre(Key(std::string("10.10.10.1")),
+                                     Key(std::string("10.20.30.1"))));
+}
+
+TEST_F(TestEthernetInterface, DeleteStaticGateway)
+{
+    createStaticGatewayObject("10.10.10.1", IP::Protocol::IPv4);
+    createStaticGatewayObject("10.20.30.1", IP::Protocol::IPv4);
+
+    interface.staticGateways.at(std::string("10.10.10.1"))->delete_();
+    interface.staticGateways.at(std::string("10.20.30.1"))->delete_();
+    EXPECT_EQ(interface.staticGateways.empty(), true);
+}
+
+TEST_F(TestEthernetInterface, AddIPv6StaticGateway)
+{
+    createStaticGatewayObject("2002:903:15f:325::1", IP::Protocol::IPv6);
+    EXPECT_THAT(interface.staticGateways,
+                UnorderedElementsAre(Key(std::string("2002:903:15f:325::1"))));
+}
+
+TEST_F(TestEthernetInterface, AddMultipleIPv6StaticGateways)
+{
+    createStaticGatewayObject("2003:903:15f:325::1", IP::Protocol::IPv6);
+    createStaticGatewayObject("2004:903:15f:325::1", IP::Protocol::IPv6);
+    EXPECT_THAT(interface.staticGateways,
+                UnorderedElementsAre(Key(std::string("2003:903:15f:325::1")),
+                                     Key(std::string("2004:903:15f:325::1"))));
+}
+
 } // namespace network
 } // namespace phosphor