Do not config Gateway when DHCP is enabled

When DHCP is enabled and the Gateway is in the network config,
systemd-networkd will try to set the gateway anyway, and if the gateway
could not be set, it fails with below error:

 systemd-networkd[459]: eth0: Could not set route: No route to host
 systemd-networkd[459]: eth0: Failed

The config should skip setting the gateway when the DHCP is enabled.

Tested: Verify the DHCP could be enabled from static config.

Fixes openbmc/phosphor-networkd#43

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I6486284133003afbe5c657ef22c27d1994544f17
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 1c88f2c..2ca9c18 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -1063,18 +1063,24 @@
         }
     }
 
-    auto gateway = EthernetInterfaceIntf::defaultGateway();
-    if (!gateway.empty())
+    if (!dhcpIsEnabled(IP::Protocol::IPv4))
     {
-        stream << "[Route]\n";
-        stream << "Gateway=" << gateway << "\n";
+        auto gateway = EthernetInterfaceIntf::defaultGateway();
+        if (!gateway.empty())
+        {
+            stream << "[Route]\n";
+            stream << "Gateway=" << gateway << "\n";
+        }
     }
 
-    auto gateway6 = EthernetInterfaceIntf::defaultGateway6();
-    if (!gateway6.empty())
+    if (!dhcpIsEnabled(IP::Protocol::IPv6))
     {
-        stream << "[Route]\n";
-        stream << "Gateway=" << gateway6 << "\n";
+        auto gateway6 = EthernetInterfaceIntf::defaultGateway6();
+        if (!gateway6.empty())
+        {
+            stream << "[Route]\n";
+            stream << "Gateway=" << gateway6 << "\n";
+        }
     }
 
     // Write the neighbor sections