blob: ad4794aba89d30993d27fb1470aadad39122ae4d [file] [log] [blame]
Qiang XUe28d1fa2019-02-27 13:50:56 +08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Qiang XU88b7f282019-08-14 22:51:43 +080017#include <systemd/sd-journal.h>
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <ChassisIntrusionSensor.hpp>
20#include <Utils.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080021#include <boost/algorithm/string/predicate.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070022#include <boost/asio/io_service.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/asio/sd_event.hpp>
27#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <sdbusplus/bus/match.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080029#include <sdbusplus/exception.hpp>
30#include <sdbusplus/server.hpp>
31#include <sdbusplus/timer.hpp>
James Feist38fb5982020-05-28 10:09:54 -070032
33#include <array>
Ed Tanous8a57ec02020-10-09 12:46:52 -070034#include <charconv>
James Feist38fb5982020-05-28 10:09:54 -070035#include <chrono>
36#include <ctime>
37#include <fstream>
38#include <functional>
39#include <iostream>
40#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070041#include <stdexcept>
42#include <string>
43#include <utility>
44#include <vector>
Qiang XUe28d1fa2019-02-27 13:50:56 +080045
Ed Tanous8a57ec02020-10-09 12:46:52 -070046static constexpr bool debug = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080047
48static constexpr const char* sensorType =
49 "xyz.openbmc_project.Configuration.ChassisIntrusionSensor";
Qiang XU74ddf862019-09-12 17:12:13 +080050static constexpr const char* nicType = "xyz.openbmc_project.Configuration.NIC";
51static constexpr std::array<const char*, 1> nicTypes = {nicType};
Qiang XUe28d1fa2019-02-27 13:50:56 +080052
Qiang XU88b7f282019-08-14 22:51:43 +080053namespace fs = std::filesystem;
54
Qiang XUe28d1fa2019-02-27 13:50:56 +080055static bool getIntrusionSensorConfig(
Lei YUba637932021-03-17 10:35:00 +080056 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -080057 IntrusionSensorType* pType, int* pBusId, int* pSlaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +080058 bool* pGpioInverted)
59{
60 // find matched configuration according to sensor type
61 ManagedObjectType sensorConfigurations;
62 bool useCache = false;
63
64 if (!getSensorConfiguration(sensorType, dbusConnection,
65 sensorConfigurations, useCache))
66 {
67 std::cerr << "error communicating to entity manager\n";
68 return false;
69 }
70
71 const SensorData* sensorData = nullptr;
72 const std::pair<std::string,
73 boost::container::flat_map<std::string, BasicVariantType>>*
74 baseConfiguration = nullptr;
75
76 // Get bus and addr of matched configuration
77 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
78 sensorConfigurations)
79 {
80 baseConfiguration = nullptr;
81 sensorData = &(sensor.second);
82
83 // match sensor type
84 auto sensorBase = sensorData->find(sensorType);
85 if (sensorBase == sensorData->end())
86 {
87 std::cerr << "error finding base configuration \n";
88 continue;
89 }
90
91 baseConfiguration = &(*sensorBase);
92
93 // judge class, "Gpio" or "I2C"
94 auto findClass = baseConfiguration->second.find("Class");
95 if (findClass != baseConfiguration->second.end() &&
Patrick Williams94733252020-05-13 11:44:58 -050096 std::get<std::string>(findClass->second) == "Gpio")
Qiang XUe28d1fa2019-02-27 13:50:56 +080097 {
98 *pType = IntrusionSensorType::gpio;
99 }
100 else
101 {
102 *pType = IntrusionSensorType::pch;
103 }
104
105 // case to find GPIO info
106 if (*pType == IntrusionSensorType::gpio)
107 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800108 auto findGpioPolarity =
109 baseConfiguration->second.find("GpioPolarity");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800110
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800111 if (findGpioPolarity == baseConfiguration->second.end())
Qiang XUe28d1fa2019-02-27 13:50:56 +0800112 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800113 std::cerr << "error finding gpio polarity in configuration \n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800114 continue;
115 }
116
117 try
118 {
Qiang XUe28d1fa2019-02-27 13:50:56 +0800119 *pGpioInverted =
Patrick Williams94733252020-05-13 11:44:58 -0500120 (std::get<std::string>(findGpioPolarity->second) == "Low");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800121 }
122 catch (const std::bad_variant_access& e)
123 {
124 std::cerr << "invalid value for gpio info in config. \n";
125 continue;
126 }
127
Ed Tanous8a57ec02020-10-09 12:46:52 -0700128 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800129 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800130 std::cout << "find chassis intrusion sensor polarity inverted "
131 "flag is "
132 << *pGpioInverted << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800133 }
134
135 return true;
136 }
137
138 // case to find I2C info
Ed Tanous8a57ec02020-10-09 12:46:52 -0700139 if (*pType == IntrusionSensorType::pch)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800140 {
141 auto findBus = baseConfiguration->second.find("Bus");
142 auto findAddress = baseConfiguration->second.find("Address");
143 if (findBus == baseConfiguration->second.end() ||
144 findAddress == baseConfiguration->second.end())
145 {
146 std::cerr << "error finding bus or address in configuration \n";
147 continue;
148 }
149
150 try
151 {
Patrick Williams94733252020-05-13 11:44:58 -0500152 *pBusId = std::get<uint64_t>(findBus->second);
153 *pSlaveAddr = std::get<uint64_t>(findAddress->second);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800154 }
155 catch (const std::bad_variant_access& e)
156 {
157 std::cerr << "invalid value for bus or address in config. \n";
158 continue;
159 }
160
Ed Tanous8a57ec02020-10-09 12:46:52 -0700161 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800162 {
163 std::cout << "find matched bus " << *pBusId
164 << ", matched slave addr " << *pSlaveAddr << "\n";
165 }
166 return true;
167 }
168 }
169
Qiang XU74ddf862019-09-12 17:12:13 +0800170 std::cerr << "can't find matched I2C or GPIO configuration for intrusion "
171 "sensor. \n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800172 *pBusId = -1;
173 *pSlaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800174 return false;
175}
176
Qiang XU88b7f282019-08-14 22:51:43 +0800177static constexpr bool debugLanLeash = false;
178boost::container::flat_map<int, bool> lanStatusMap;
Qiang XU74ddf862019-09-12 17:12:13 +0800179boost::container::flat_map<int, std::string> lanInfoMap;
Qiang XU88b7f282019-08-14 22:51:43 +0800180boost::container::flat_map<std::string, int> pathSuffixMap;
181
Lei YUba637932021-03-17 10:35:00 +0800182static void getNicNameInfo(
183 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Qiang XU74ddf862019-09-12 17:12:13 +0800184{
185 auto getter = std::make_shared<GetSensorConfiguration>(
Ed Tanous8a17c302021-09-02 15:07:11 -0700186 dbusConnection, [](const ManagedObjectType& sensorConfigurations) {
Qiang XU74ddf862019-09-12 17:12:13 +0800187 // Get NIC name and save to map
188 lanInfoMap.clear();
189 for (const std::pair<sdbusplus::message::object_path, SensorData>&
190 sensor : sensorConfigurations)
191 {
192 const std::pair<
193 std::string,
194 boost::container::flat_map<std::string, BasicVariantType>>*
195 baseConfiguration = nullptr;
196
197 // find base configuration
198 auto sensorBase = sensor.second.find(nicType);
199 if (sensorBase == sensor.second.end())
200 {
201 continue;
202 }
203 baseConfiguration = &(*sensorBase);
204
205 auto findEthIndex = baseConfiguration->second.find("EthIndex");
206 auto findName = baseConfiguration->second.find("Name");
207
208 if (findEthIndex != baseConfiguration->second.end() &&
209 findName != baseConfiguration->second.end())
210 {
211 auto* pEthIndex =
212 std::get_if<uint64_t>(&findEthIndex->second);
213 auto* pName = std::get_if<std::string>(&findName->second);
214 if (pEthIndex != nullptr && pName != nullptr)
215 {
216 lanInfoMap[*pEthIndex] = *pName;
217 if (debugLanLeash)
218 {
219 std::cout << "find name of eth" << *pEthIndex
220 << " is " << *pName << "\n";
221 }
222 }
223 }
224 }
225
226 if (lanInfoMap.size() == 0)
227 {
228 std::cerr << "can't find matched NIC name. \n";
229 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700230 });
Qiang XU74ddf862019-09-12 17:12:13 +0800231
232 getter->getConfiguration(
233 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
234}
235
Qiang XU88b7f282019-08-14 22:51:43 +0800236static void processLanStatusChange(sdbusplus::message::message& message)
237{
238 const std::string& pathName = message.get_path();
239 std::string interfaceName;
240 boost::container::flat_map<std::string, BasicVariantType> properties;
241 message.read(interfaceName, properties);
242
243 auto findStateProperty = properties.find("OperationalState");
244 if (findStateProperty == properties.end())
245 {
246 return;
247 }
Qiang XU74ddf862019-09-12 17:12:13 +0800248 std::string* pState =
249 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800250 if (pState == nullptr)
251 {
252 std::cerr << "invalid OperationalState \n";
253 return;
254 }
255
256 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
257 *pState == "degraded");
258
259 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500260 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800261 if (pos == std::string::npos || pathName.length() <= pos + 2)
262 {
263 std::cerr << "unexpected path name " << pathName << "\n";
264 return;
265 }
266 std::string suffixStr = pathName.substr(pos + 2);
267
268 auto findEthNum = pathSuffixMap.find(suffixStr);
269 if (findEthNum == pathSuffixMap.end())
270 {
271 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
272 return;
273 }
274 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800275
276 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800277 auto findLanStatus = lanStatusMap.find(ethNum);
278 if (findLanStatus == lanStatusMap.end())
279 {
280 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
281 return;
282 }
283 bool oldLanConnected = findLanStatus->second;
284
Qiang XU74ddf862019-09-12 17:12:13 +0800285 // get lan info from map
286 std::string lanInfo = "";
287 if (lanInfoMap.size() > 0)
288 {
289 auto findLanInfo = lanInfoMap.find(ethNum);
290 if (findLanInfo == lanInfoMap.end())
291 {
292 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
293 }
294 else
295 {
296 lanInfo = "(" + findLanInfo->second + ")";
297 }
298 }
299
Qiang XU88b7f282019-08-14 22:51:43 +0800300 if (debugLanLeash)
301 {
302 std::cout << "ethNum = " << ethNum << ", state = " << *pState
303 << ", oldLanConnected = "
304 << (oldLanConnected ? "true" : "false")
305 << ", newLanConnected = "
306 << (newLanConnected ? "true" : "false") << "\n";
307 }
308
309 if (oldLanConnected != newLanConnected)
310 {
Qiang XU74ddf862019-09-12 17:12:13 +0800311 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
312 std::string strEvent = strEthNum + " LAN leash " +
313 (newLanConnected ? "connected" : "lost");
314 std::string strMsgId =
315 newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
316 sd_journal_send("MESSAGE=%s", strEvent.c_str(), "PRIORITY=%i", LOG_INFO,
Qiang XU88b7f282019-08-14 22:51:43 +0800317 "REDFISH_MESSAGE_ID=%s", strMsgId.c_str(),
Qiang XU74ddf862019-09-12 17:12:13 +0800318 "REDFISH_MESSAGE_ARGS=%s", strEthNum.c_str(), NULL);
Qiang XU88b7f282019-08-14 22:51:43 +0800319 lanStatusMap[ethNum] = newLanConnected;
320 if (debugLanLeash)
321 {
Qiang XU74ddf862019-09-12 17:12:13 +0800322 std::cout << "log redfish event: " << strEvent << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800323 }
324 }
325}
326
Lei YUdd68d4a2021-03-16 22:17:23 +0800327/** @brief Initialize the lan status.
328 *
329 * @return true on success and false on failure
330 */
Lei YUba637932021-03-17 10:35:00 +0800331static bool initializeLanStatus(
332 const std::shared_ptr<sdbusplus::asio::connection>& conn)
Qiang XU88b7f282019-08-14 22:51:43 +0800333{
Qiang XU74ddf862019-09-12 17:12:13 +0800334 // init lan port name from configuration
335 getNicNameInfo(conn);
336
337 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800338 std::vector<fs::path> files;
339 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
340 {
341 std::cerr << "No eth in system\n";
Lei YUdd68d4a2021-03-16 22:17:23 +0800342 return false;
Qiang XU88b7f282019-08-14 22:51:43 +0800343 }
344
345 // iterate through all found eth files, and save ifindex
Ed Tanous8a57ec02020-10-09 12:46:52 -0700346 for (const fs::path& fileName : files)
Qiang XU88b7f282019-08-14 22:51:43 +0800347 {
348 if (debugLanLeash)
349 {
350 std::cout << "Reading " << fileName << "\n";
351 }
352 std::ifstream sysFile(fileName);
353 if (!sysFile.good())
354 {
355 std::cerr << "Failure reading " << fileName << "\n";
356 continue;
357 }
358 std::string line;
359 getline(sysFile, line);
360 const uint8_t ifindex = std::stoi(line);
361 // pathSuffix is ASCII of ifindex
362 const std::string& pathSuffix = std::to_string(ifindex + 30);
363
364 // extract ethNum
365 const std::string& fileStr = fileName.string();
366 const int pos = fileStr.find("eth");
367 const std::string& ethNumStr = fileStr.substr(pos + 3);
368 int ethNum = 0;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700369 std::from_chars_result r = std::from_chars(
370 ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
371 if (r.ec != std::errc())
Qiang XU88b7f282019-08-14 22:51:43 +0800372 {
373 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
374 continue;
375 }
376
377 // save pathSuffix
378 pathSuffixMap[pathSuffix] = ethNum;
379 if (debugLanLeash)
380 {
381 std::cout << "ethNum = " << std::to_string(ethNum)
382 << ", ifindex = " << line
383 << ", pathSuffix = " << pathSuffix << "\n";
384 }
385
Qiang XU74ddf862019-09-12 17:12:13 +0800386 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800387 conn->async_method_call(
388 [ethNum](boost::system::error_code ec,
389 const std::variant<std::string>& property) {
Qiang XU74ddf862019-09-12 17:12:13 +0800390 lanStatusMap[ethNum] = false;
Qiang XU88b7f282019-08-14 22:51:43 +0800391 if (ec)
392 {
Qiang XU74ddf862019-09-12 17:12:13 +0800393 std::cerr << "Error reading init status of eth" << ethNum
394 << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800395 return;
396 }
397 const std::string* pState = std::get_if<std::string>(&property);
398 if (pState == nullptr)
399 {
400 std::cerr << "Unable to read lan status value\n";
401 return;
402 }
403 bool isLanConnected =
404 (*pState == "routable" || *pState == "carrier" ||
405 *pState == "degraded");
406 if (debugLanLeash)
407 {
408 std::cout << "ethNum = " << std::to_string(ethNum)
409 << ", init LAN status = "
410 << (isLanConnected ? "true" : "false") << "\n";
411 }
412 lanStatusMap[ethNum] = isLanConnected;
413 },
414 "org.freedesktop.network1",
415 "/org/freedesktop/network1/link/_" + pathSuffix,
416 "org.freedesktop.DBus.Properties", "Get",
417 "org.freedesktop.network1.Link", "OperationalState");
418 }
Lei YUdd68d4a2021-03-16 22:17:23 +0800419 return true;
Qiang XU88b7f282019-08-14 22:51:43 +0800420}
421
Qiang XUe28d1fa2019-02-27 13:50:56 +0800422int main()
423{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800424 int busId = -1;
425 int slaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800426 bool gpioInverted = false;
427 IntrusionSensorType type = IntrusionSensorType::gpio;
428
429 // setup connection to dbus
430 boost::asio::io_service io;
431 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
432 auto objServer = sdbusplus::asio::object_server(systemBus);
433
434 // setup object server, define interface
435 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
436
437 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceChassis =
438 objServer.add_interface(
439 "/xyz/openbmc_project/Intrusion/Chassis_Intrusion",
440 "xyz.openbmc_project.Chassis.Intrusion");
441
442 ChassisIntrusionSensor chassisIntrusionSensor(io, ifaceChassis);
443
444 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800445 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800446 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800447 chassisIntrusionSensor.start(type, busId, slaveAddr, gpioInverted);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800448 }
449
450 // callback to handle configuration change
451 std::function<void(sdbusplus::message::message&)> eventHandler =
452 [&](sdbusplus::message::message& message) {
453 if (message.is_method_error())
454 {
455 std::cerr << "callback method error\n";
456 return;
457 }
458
459 std::cout << "rescan due to configuration change \n";
460 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800461 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800462 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800463 chassisIntrusionSensor.start(type, busId, slaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800464 gpioInverted);
465 }
466 };
467
Lei YUdd68d4a2021-03-16 22:17:23 +0800468 auto eventMatch = std::make_unique<sdbusplus::bus::match::match>(
Qiang XUe28d1fa2019-02-27 13:50:56 +0800469 static_cast<sdbusplus::bus::bus&>(*systemBus),
470 "type='signal',member='PropertiesChanged',path_namespace='" +
471 std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'",
472 eventHandler);
473
Lei YUdd68d4a2021-03-16 22:17:23 +0800474 if (initializeLanStatus(systemBus))
475 {
476 // add match to monitor lan status change
477 sdbusplus::bus::match::match lanStatusMatch(
478 static_cast<sdbusplus::bus::bus&>(*systemBus),
479 "type='signal', member='PropertiesChanged',"
480 "arg0namespace='org.freedesktop.network1.Link'",
481 [](sdbusplus::message::message& msg) {
482 processLanStatusChange(msg);
483 });
484
485 // add match to monitor entity manager signal about nic name config
486 // change
487 sdbusplus::bus::match::match lanConfigMatch(
488 static_cast<sdbusplus::bus::bus&>(*systemBus),
489 "type='signal', member='PropertiesChanged',path_namespace='" +
490 std::string(inventoryPath) + "',arg0namespace='" + nicType +
491 "'",
492 [&systemBus](sdbusplus::message::message& msg) {
493 if (msg.is_method_error())
494 {
495 std::cerr << "callback method error\n";
496 return;
497 }
498 getNicNameInfo(systemBus);
499 });
500 }
Qiang XU88b7f282019-08-14 22:51:43 +0800501
Qiang XUe28d1fa2019-02-27 13:50:56 +0800502 io.run();
503
504 return 0;
505}