summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-network/network/phosphor-network/0011-Added-enable-disable-control-of-the-Network-Interfac.patch
blob: 58dcf3f2149828fffd0789e9648108f3bf924acc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
From 4bfb4ad5ff795d78e06fbeaf1664df6819880f50 Mon Sep 17 00:00:00 2001
From: Johnathan Mantey <johnathanx.mantey@intel.com>
Date: Tue, 29 Oct 2019 16:20:28 -0700
Subject: [PATCH] Added enable/disable control of the Network Interface Card

Implemented enable/disable function to perform
"ip link set eth(x) up"
"ip link set eth(x) down"
functionality from DBus.

Tested:

Confirmed Redfish PATCH commands on the InterfaceEnabled property
changes the NIC state. Confirmed the NIC is DOWN/UP using "ip link".
Confirmed "ip link" state changes can be obsserved from dbus-send
commands, and from Redfish GET actions.

Confirmed the link is inactive after a reboot.

Confirmed link stays down despite assigning an IP manually.

Confirmed link stays down despite enabling DHCP.

Change-Id: I4152b53055e6546f7a6ca81b5a5eef6f689bcc66
Signed-off-by: Johnathan Mantey <johnathanx.mantey@intel.com>
---
 ethernet_interface.cpp | 73 ++++++++++++++++++++++++++++++++++++++++--
 ethernet_interface.hpp | 11 ++++++-
 2 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 8b8f698..a2754a4 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -85,6 +85,7 @@ EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
     EthernetInterfaceIntf::autoNeg(std::get<2>(ifInfo));
     EthernetInterfaceIntf::speed(std::get<0>(ifInfo));
     EthernetInterfaceIntf::linkUp(std::get<3>(ifInfo));
+    EthernetInterfaceIntf::nICEnabled(std::get<4>(ifInfo));
 #endif
     getChannelPrivilege(intfName);
 
@@ -323,11 +324,12 @@ InterfaceInfo EthernetInterface::getInterfaceInfo() const
     Autoneg autoneg{0};
     DuplexMode duplex{0};
     LinkUp linkState{false};
+    NICEnabled nicEnabled{false};
     EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
 
     if (eifSocket.sock < 0)
     {
-        return std::make_tuple(speed, duplex, autoneg, linkState);
+        return std::make_tuple(speed, duplex, autoneg, linkState, nicEnabled);
     }
 
     std::strncpy(ifr.ifr_name, interfaceName().c_str(), IFNAMSIZ - 1);
@@ -341,9 +343,10 @@ InterfaceInfo EthernetInterface::getInterfaceInfo() const
         autoneg = edata.autoneg;
     }
 
+    nicEnabled = nICEnabled();
     linkState = linkUp();
 
-    return std::make_tuple(speed, duplex, autoneg, linkState);
+    return std::make_tuple(speed, duplex, autoneg, linkState, nicEnabled);
 }
 #endif
 
@@ -548,6 +551,67 @@ bool EthernetInterface::linkUp() const
         log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
                         entry("ERROR=%s", strerror(errno)));
     }
+    return value;
+}
+
+bool EthernetInterface::nICEnabled() const
+{
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+    bool value = EthernetInterfaceIntf::nICEnabled();
+
+    if (eifSocket.sock < 0)
+    {
+        return value;
+    }
+
+    ifreq ifr{0};
+    std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1);
+    if (ioctl(eifSocket.sock, SIOCGIFFLAGS, &ifr) == 0)
+    {
+        value = static_cast<bool>(ifr.ifr_flags & IFF_UP);
+    }
+    else
+    {
+        log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
+                        entry("ERROR=%s", strerror(errno)));
+    }
+    return value;
+}
+
+bool EthernetInterface::nICEnabled(bool value)
+{
+    if (value == EthernetInterfaceIntf::nICEnabled())
+    {
+        return value;
+    }
+
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+    if (eifSocket.sock < 0)
+    {
+        return EthernetInterfaceIntf::nICEnabled();
+    }
+
+    ifreq ifr{0};
+    std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1);
+    if (ioctl(eifSocket.sock, SIOCGIFFLAGS, &ifr) != 0)
+    {
+        log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
+                        entry("ERROR=%s", strerror(errno)));
+        return EthernetInterfaceIntf::nICEnabled();
+    }
+
+    ifr.ifr_flags &= ~IFF_UP;
+    ifr.ifr_flags |= value ? IFF_UP : 0;
+
+    if (ioctl(eifSocket.sock, SIOCSIFFLAGS, &ifr) != 0)
+    {
+        log<level::ERR>("ioctl failed for SIOCSIFFLAGS:",
+                        entry("ERROR=%s", strerror(errno)));
+        return EthernetInterfaceIntf::nICEnabled();
+    }
+    EthernetInterfaceIntf::nICEnabled(value);
+    writeConfigurationFile();
+    manager.restartSystemdUnit(networkdService);
 
     return value;
 }
@@ -742,6 +806,11 @@ void EthernetInterface::writeConfigurationFile()
         stream << "MACAddress=" << mac << "\n";
     }
 
+    if (!nICEnabled())
+    {
+        stream << "Unmanaged=yes\n";
+    }
+
     // write the network section
     stream << "[Network]\n";
 #ifdef LINK_LOCAL_AUTOCONFIGURATION
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index 4e36ae8..104750e 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -60,9 +60,11 @@ using LinkSpeed = uint16_t;
 using DuplexMode = uint8_t;
 using Autoneg = uint8_t;
 using LinkUp = bool;
+using NICEnabled = bool;
 using VlanId = uint32_t;
 using InterfaceName = std::string;
-using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp>;
+using InterfaceInfo =
+    std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp, NICEnabled>;
 using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
 using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
 using VlanInterfaceMap =
@@ -190,6 +192,12 @@ class EthernetInterface : public Ifaces
     /** Retrieve Link State */
     bool linkUp() const override;
 
+    /** Retrieve NIC State */
+    bool nICEnabled() const override;
+
+    /** Set value of NICEnabled */
+    bool nICEnabled(bool value) override;
+
     /** @brief sets the MAC address.
      *  @param[in] value - MAC address which needs to be set on the system.
      *  @returns macAddress of the interface or throws an error.
@@ -246,6 +254,7 @@ class EthernetInterface : public Ifaces
     using EthernetInterfaceIntf::dHCPEnabled;
     using EthernetInterfaceIntf::interfaceName;
     using EthernetInterfaceIntf::linkUp;
+    using EthernetInterfaceIntf::nICEnabled;
     using MacAddressIntf::mACAddress;
 
     /** @brief Absolute path of the resolv conf file */
-- 
2.24.1