summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-network/network/phosphor-network/0010-Enable-the-network-link-carrier-state-to-be-reported.patch
blob: 51957ffffd5cf54ce35c52c658e129a1c8d61629 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
From 8f6f3ccb1f5a4af8065485c2e683402ec2b38abf Mon Sep 17 00:00:00 2001
From: Johnathan Mantey <johnathanx.mantey@intel.com>
Date: Wed, 8 Jan 2020 10:38:58 -0800
Subject: [PATCH] Enable the network link carrier state to be reported.

This change allows networkd to keep track of, and report, the state of
the network carrier signal. When a NIC cable is pulled, or inserted, a
DBus client is able identify the condition.

Tested:
ip link set down dev eth0  # take eth0 down
Get bmc/EthernetInterfaces/eth0 from Redfish # LinkStatus = LinkDown
                                             # InterfaceEnabled = false
ip link set up dev eth0    # bring eth0 back
Get bmc/EthernetInterfaces/eth0 from Redfish # LinkStatus = Linkup
                                             # InterfaceEnabled = true
Pull eth0 cable
Get bmc/EthernetInterfaces/eth0 from Redfish # LinkStatus = LinkDown
                                             # InterfaceEnabled = true
Insert eth0 cable
Get bmc/EthernetInterfaces/eth0 from Redfish # LinkStatus = Linkup
                                             # InterfaceEnabled = true

Change-Id: I5530cf7882cfbfdba1436dd34b3219c735047c5e
Signed-off-by: Johnathan Mantey <johnathanx.mantey@intel.com>
---
 ethernet_interface.cpp | 141 +++++++++++++++++++++++++----------------
 ethernet_interface.hpp |   8 ++-
 2 files changed, 92 insertions(+), 57 deletions(-)

diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index 671e8c4..018f2e1 100644
--- a/ethernet_interface.cpp
+++ b/ethernet_interface.cpp
@@ -42,6 +42,28 @@ static constexpr const char* defaultChannelPriv = "priv-admin";
 std::map<std::string, std::string> mapDHCPToSystemd = {
     {"both", "true"}, {"v4", "ipv4"}, {"v6", "ipv6"}, {"none", "false"}};
 
+struct EthernetIntfSocket
+{
+    EthernetIntfSocket(int domain, int type, int protocol)
+    {
+        if ((sock = socket(domain, type, protocol)) < 0)
+        {
+            log<level::ERR>("socket creation failed:",
+                            entry("ERROR=%s", strerror(errno)));
+        }
+    }
+
+    ~EthernetIntfSocket()
+    {
+        if (sock > 0)
+        {
+            close(sock);
+        }
+    }
+
+    int sock{-1};
+};
+
 EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
                                      const std::string& objPath,
                                      DHCPConf dhcpEnabled, Manager& parent,
@@ -57,12 +79,12 @@ EthernetInterface::EthernetInterface(sdbusplus::bus::bus& bus,
     MacAddressIntf::mACAddress(getMACAddress(intfName));
     EthernetInterfaceIntf::nTPServers(getNTPServersFromConf());
     EthernetInterfaceIntf::nameservers(getNameServerFromConf());
-#if NIC_SUPPORTS_ETHTOOL
     InterfaceInfo ifInfo = EthernetInterface::getInterfaceInfo();
-
-    EthernetInterfaceIntf::nICEnabled(std::get<3>(ifInfo));
+#if NIC_SUPPORTS_ETHTOOL
     EthernetInterfaceIntf::autoNeg(std::get<2>(ifInfo));
     EthernetInterfaceIntf::speed(std::get<0>(ifInfo));
+    EthernetInterfaceIntf::nICEnabled(std::get<3>(ifInfo));
+    EthernetInterfaceIntf::linkUp(std::get<4>(ifInfo));
 #endif
     getChannelPrivilege(intfName);
 
@@ -286,63 +308,47 @@ ObjectPath EthernetInterface::neighbor(std::string iPAddress,
     return objectPath;
 }
 
-#if NIC_SUPPORTS_ETHTOOL
-/*
-  Enable this code if your NIC driver supports the ETHTOOL features.
-  Do this by adding the following to your phosphor-network*.bbappend file.
-     EXTRA_OECONF_append = " --enable-nic-ethtool=yes"
-  The default compile mode is to omit getInterfaceInfo()
-*/
 InterfaceInfo EthernetInterface::getInterfaceInfo() const
 {
-    int sock{-1};
-    ifreq ifr{0};
-    ethtool_cmd edata{0};
     LinkSpeed speed{0};
     Autoneg autoneg{0};
     DuplexMode duplex{0};
     NICEnabled nicEnabled{false};
-    do
-    {
-        sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
-        if (sock < 0)
-        {
-            log<level::ERR>("socket creation  failed:",
-                            entry("ERROR=%s", strerror(errno)));
-            break;
-        }
+    LinkUp linkState{false};
 
-        strcpy(ifr.ifr_name, interfaceName().c_str());
-        ifr.ifr_data = reinterpret_cast<char*>(&edata);
+#if NIC_SUPPORTS_ETHTOOL
+    /*
+      Enable this code if your NIC driver supports the ETHTOOL features.
+      Do this by adding the following to your phosphor-network*.bbappend
+      file. EXTRA_OECONF_append = " --enable-nic-ethtool=yes" The
+      default compile mode is to omit getInterfaceInfo()
+    */
+    ifreq ifr{0};
+    ethtool_cmd edata{0};
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
 
-        edata.cmd = ETHTOOL_GSET;
+    if (eifSocket.sock < 0)
+    {
+        return std::make_tuple(speed, duplex, autoneg, nicEnabled, linkState);
+    }
 
-        if (ioctl(sock, SIOCETHTOOL, &ifr) < 0)
-        {
-            log<level::ERR>("ioctl failed for SIOCETHTOOL:",
-                            entry("ERROR=%s", strerror(errno)));
-            break;
-        }
+    std::strncpy(ifr.ifr_name, interfaceName().c_str(), IFNAMSIZ - 1);
+    ifr.ifr_data = reinterpret_cast<char*>(&edata);
+
+    edata.cmd = ETHTOOL_GSET;
+    if (ioctl(eifSocket.sock, SIOCETHTOOL, &ifr) >= 0)
+    {
         speed = edata.speed;
         duplex = edata.duplex;
         autoneg = edata.autoneg;
+    }
+#endif
 
-        if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0)
-        {
-            log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
-                            entry("ERROR=%s", strerror(errno)));
-            break;
-        }
-        nicEnabled = static_cast<bool>(ifr.ifr_flags & IFF_UP);
-    } while (0);
+    nicEnabled = nICEnabled();
+    linkState = linkUp();
 
-    if (sock >= 0)
-    {
-        close(sock);
-    }
-    return std::make_tuple(speed, duplex, autoneg, nicEnabled);
+    return std::make_tuple(speed, duplex, autoneg, nicEnabled, linkState);
 }
-#endif
 
 /** @brief get the mac address of the interface.
  *  @return macaddress on success
@@ -351,25 +357,23 @@ InterfaceInfo EthernetInterface::getInterfaceInfo() const
 std::string
     EthernetInterface::getMACAddress(const std::string& interfaceName) const
 {
-    ifreq ifr{};
-    int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
-    if (sock < 0)
+    std::string activeMACAddr = MacAddressIntf::mACAddress();
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+
+    if (eifSocket.sock < 0)
     {
-        log<level::ERR>("socket creation  failed:",
-                        entry("ERROR=%s", strerror(errno)));
-        elog<InternalFailure>();
+        return activeMACAddr;
     }
 
-    std::strcpy(ifr.ifr_name, interfaceName.c_str());
-    if (ioctl(sock, SIOCGIFHWADDR, &ifr) != 0)
+    ifreq ifr{0};
+    std::strncpy(ifr.ifr_name, interfaceName.c_str(), IFNAMSIZ - 1);
+    if (ioctl(eifSocket.sock, SIOCGIFHWADDR, &ifr) != 0)
     {
         log<level::ERR>("ioctl failed for SIOCGIFHWADDR:",
                         entry("ERROR=%s", strerror(errno)));
-        close(sock);
         elog<InternalFailure>();
     }
 
-    close(sock);
     static_assert(sizeof(ifr.ifr_hwaddr.sa_data) >= sizeof(ether_addr));
     std::string_view hwaddr(reinterpret_cast<char*>(ifr.ifr_hwaddr.sa_data),
                             sizeof(ifr.ifr_hwaddr.sa_data));
@@ -546,7 +550,7 @@ bool EthernetInterface::nICEnabled(bool value)
 
     do
     {
-        std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE);
+        std::strncpy(ifr.ifr_name, interfaceName().c_str(), IF_NAMESIZE - 1);
         if (ioctl(sock, SIOCGIFFLAGS, &ifr) != 0)
         {
             log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
@@ -575,6 +579,31 @@ bool EthernetInterface::nICEnabled(bool value)
     return value;
 }
 
+bool EthernetInterface::linkUp() const
+{
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+    bool value = EthernetInterfaceIntf::linkUp();
+
+    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_RUNNING);
+    }
+    else
+    {
+        log<level::ERR>("ioctl failed for SIOCGIFFLAGS:",
+                        entry("ERROR=%s", strerror(errno)));
+    }
+
+    return value;
+}
+
 ServerList EthernetInterface::nameservers(ServerList value)
 {
     for (const auto& nameserverip : value)
diff --git a/ethernet_interface.hpp b/ethernet_interface.hpp
index 3dee311..83d7cb5 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 NICEnabled = bool;
+using LinkUp = bool;
 using VlanId = uint32_t;
 using InterfaceName = std::string;
-using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg, NICEnabled>;
+using InterfaceInfo =
+    std::tuple<LinkSpeed, DuplexMode, Autoneg, NICEnabled, LinkUp>;
 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,9 @@ class EthernetInterface : public Ifaces
     /** Set value of NICEnabled */
     bool nICEnabled(bool value) override;
 
+    /** Retrieve Link State */
+    bool linkUp() const 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.
@@ -245,6 +250,7 @@ class EthernetInterface : public Ifaces
     using ChannelAccessIntf::maxPrivilege;
     using EthernetInterfaceIntf::dHCPEnabled;
     using EthernetInterfaceIntf::interfaceName;
+    using EthernetInterfaceIntf::linkUp;
     using EthernetInterfaceIntf::nICEnabled;
     using MacAddressIntf::mACAddress;
 
-- 
2.24.1