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: eb4efab8f96156526486bbce630258f62ab2fd0d (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
From 7e99cfbb5cdbf47cd0350d869be236c88f982fd3 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 | 104 ++++++++++++++++++++++++++++-------------
 ethernet_interface.hpp |   7 ++-
 2 files changed, 77 insertions(+), 34 deletions(-)

diff --git a/ethernet_interface.cpp b/ethernet_interface.cpp
index ba6195e..8b8f698 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,
@@ -62,6 +84,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));
 #endif
     getChannelPrivilege(intfName);
 
@@ -294,43 +317,33 @@ ObjectPath EthernetInterface::neighbor(std::string iPAddress,
 */
 InterfaceInfo EthernetInterface::getInterfaceInfo() const
 {
-    int sock{-1};
     ifreq ifr{0};
     ethtool_cmd edata{0};
     LinkSpeed speed{0};
     Autoneg autoneg{0};
     DuplexMode duplex{0};
-    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};
+    EthernetIntfSocket eifSocket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
 
-        strcpy(ifr.ifr_name, interfaceName().c_str());
-        ifr.ifr_data = reinterpret_cast<char*>(&edata);
+    if (eifSocket.sock < 0)
+    {
+        return std::make_tuple(speed, duplex, autoneg, linkState);
+    }
 
-        edata.cmd = ETHTOOL_GSET;
+    std::strncpy(ifr.ifr_name, interfaceName().c_str(), IFNAMSIZ - 1);
+    ifr.ifr_data = reinterpret_cast<char*>(&edata);
 
-        if (ioctl(sock, SIOCETHTOOL, &ifr) < 0)
-        {
-            log<level::ERR>("ioctl failed for SIOCETHTOOL:",
-                            entry("ERROR=%s", strerror(errno)));
-            break;
-        }
+    edata.cmd = ETHTOOL_GSET;
+    if (ioctl(eifSocket.sock, SIOCETHTOOL, &ifr) >= 0)
+    {
         speed = edata.speed;
         duplex = edata.duplex;
         autoneg = edata.autoneg;
-    } while (0);
-
-    if (sock)
-    {
-        close(sock);
     }
-    return std::make_tuple(speed, duplex, autoneg);
+
+    linkState = linkUp();
+
+    return std::make_tuple(speed, duplex, autoneg, linkState);
 }
 #endif
 
@@ -341,17 +354,17 @@ 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)));
@@ -514,6 +527,31 @@ EthernetInterface::DHCPConf EthernetInterface::dHCPEnabled(DHCPConf 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 a962751..4e36ae8 100644
--- a/ethernet_interface.hpp
+++ b/ethernet_interface.hpp
@@ -59,9 +59,10 @@ class Neighbor;
 using LinkSpeed = uint16_t;
 using DuplexMode = uint8_t;
 using Autoneg = uint8_t;
+using LinkUp = bool;
 using VlanId = uint32_t;
 using InterfaceName = std::string;
-using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg>;
+using InterfaceInfo = std::tuple<LinkSpeed, DuplexMode, Autoneg, LinkUp>;
 using AddressMap = std::map<std::string, std::shared_ptr<IPAddress>>;
 using NeighborMap = std::map<std::string, std::shared_ptr<Neighbor>>;
 using VlanInterfaceMap =
@@ -186,6 +187,9 @@ class EthernetInterface : public Ifaces
      */
     void disableDHCP(IP::Protocol protocol);
 
+    /** 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.
@@ -241,6 +245,7 @@ class EthernetInterface : public Ifaces
     using ChannelAccessIntf::maxPrivilege;
     using EthernetInterfaceIntf::dHCPEnabled;
     using EthernetInterfaceIntf::interfaceName;
+    using EthernetInterfaceIntf::linkUp;
     using MacAddressIntf::mACAddress;
 
     /** @brief Absolute path of the resolv conf file */
-- 
2.24.1