blob: 85fe3e5dc2a0c7d0018be0b9977884c26420e3b6 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <ChassisIntrusionSensor.hpp>
18#include <Utils.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080019#include <boost/algorithm/string/predicate.hpp>
Ed Tanous8a57ec02020-10-09 12:46:52 -070020#include <boost/asio/io_service.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
Patrick Williams0c42f402021-08-27 16:05:45 -050022#include <phosphor-logging/lg2.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/asio/sd_event.hpp>
26#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <sdbusplus/bus/match.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080028#include <sdbusplus/exception.hpp>
29#include <sdbusplus/server.hpp>
30#include <sdbusplus/timer.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031
32#include <array>
Ed Tanous8a57ec02020-10-09 12:46:52 -070033#include <charconv>
James Feist38fb5982020-05-28 10:09:54 -070034#include <chrono>
35#include <ctime>
36#include <fstream>
37#include <functional>
38#include <iostream>
39#include <memory>
Patrick Venture96e97db2019-10-31 13:44:38 -070040#include <stdexcept>
41#include <string>
42#include <utility>
43#include <vector>
Qiang XUe28d1fa2019-02-27 13:50:56 +080044
Ed Tanous8a57ec02020-10-09 12:46:52 -070045static constexpr bool debug = false;
Qiang XUe28d1fa2019-02-27 13:50:56 +080046
47static constexpr const char* sensorType =
48 "xyz.openbmc_project.Configuration.ChassisIntrusionSensor";
Qiang XU74ddf862019-09-12 17:12:13 +080049static constexpr const char* nicType = "xyz.openbmc_project.Configuration.NIC";
50static constexpr std::array<const char*, 1> nicTypes = {nicType};
Qiang XUe28d1fa2019-02-27 13:50:56 +080051
Qiang XU88b7f282019-08-14 22:51:43 +080052namespace fs = std::filesystem;
53
Qiang XUe28d1fa2019-02-27 13:50:56 +080054static bool getIntrusionSensorConfig(
Lei YUba637932021-03-17 10:35:00 +080055 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -080056 IntrusionSensorType* pType, int* pBusId, int* pSlaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +080057 bool* pGpioInverted)
58{
59 // find matched configuration according to sensor type
60 ManagedObjectType sensorConfigurations;
61 bool useCache = false;
62
63 if (!getSensorConfiguration(sensorType, dbusConnection,
64 sensorConfigurations, useCache))
65 {
66 std::cerr << "error communicating to entity manager\n";
67 return false;
68 }
69
70 const SensorData* sensorData = nullptr;
71 const std::pair<std::string,
72 boost::container::flat_map<std::string, BasicVariantType>>*
73 baseConfiguration = nullptr;
74
75 // Get bus and addr of matched configuration
76 for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor :
77 sensorConfigurations)
78 {
79 baseConfiguration = nullptr;
80 sensorData = &(sensor.second);
81
82 // match sensor type
83 auto sensorBase = sensorData->find(sensorType);
84 if (sensorBase == sensorData->end())
85 {
86 std::cerr << "error finding base configuration \n";
87 continue;
88 }
89
90 baseConfiguration = &(*sensorBase);
91
92 // judge class, "Gpio" or "I2C"
93 auto findClass = baseConfiguration->second.find("Class");
94 if (findClass != baseConfiguration->second.end() &&
Patrick Williams94733252020-05-13 11:44:58 -050095 std::get<std::string>(findClass->second) == "Gpio")
Qiang XUe28d1fa2019-02-27 13:50:56 +080096 {
97 *pType = IntrusionSensorType::gpio;
98 }
99 else
100 {
101 *pType = IntrusionSensorType::pch;
102 }
103
104 // case to find GPIO info
105 if (*pType == IntrusionSensorType::gpio)
106 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800107 auto findGpioPolarity =
108 baseConfiguration->second.find("GpioPolarity");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800109
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800110 if (findGpioPolarity == baseConfiguration->second.end())
Qiang XUe28d1fa2019-02-27 13:50:56 +0800111 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800112 std::cerr << "error finding gpio polarity in configuration \n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800113 continue;
114 }
115
116 try
117 {
Qiang XUe28d1fa2019-02-27 13:50:56 +0800118 *pGpioInverted =
Patrick Williams94733252020-05-13 11:44:58 -0500119 (std::get<std::string>(findGpioPolarity->second) == "Low");
Qiang XUe28d1fa2019-02-27 13:50:56 +0800120 }
121 catch (const std::bad_variant_access& e)
122 {
123 std::cerr << "invalid value for gpio info in config. \n";
124 continue;
125 }
126
Ed Tanous8a57ec02020-10-09 12:46:52 -0700127 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800128 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800129 std::cout << "find chassis intrusion sensor polarity inverted "
130 "flag is "
131 << *pGpioInverted << "\n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800132 }
133
134 return true;
135 }
136
137 // case to find I2C info
Ed Tanous8a57ec02020-10-09 12:46:52 -0700138 if (*pType == IntrusionSensorType::pch)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800139 {
140 auto findBus = baseConfiguration->second.find("Bus");
141 auto findAddress = baseConfiguration->second.find("Address");
142 if (findBus == baseConfiguration->second.end() ||
143 findAddress == baseConfiguration->second.end())
144 {
145 std::cerr << "error finding bus or address in configuration \n";
146 continue;
147 }
148
149 try
150 {
Patrick Williams94733252020-05-13 11:44:58 -0500151 *pBusId = std::get<uint64_t>(findBus->second);
152 *pSlaveAddr = std::get<uint64_t>(findAddress->second);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800153 }
154 catch (const std::bad_variant_access& e)
155 {
156 std::cerr << "invalid value for bus or address in config. \n";
157 continue;
158 }
159
Ed Tanous8a57ec02020-10-09 12:46:52 -0700160 if (debug)
Qiang XUe28d1fa2019-02-27 13:50:56 +0800161 {
162 std::cout << "find matched bus " << *pBusId
163 << ", matched slave addr " << *pSlaveAddr << "\n";
164 }
165 return true;
166 }
167 }
168
Qiang XU74ddf862019-09-12 17:12:13 +0800169 std::cerr << "can't find matched I2C or GPIO configuration for intrusion "
170 "sensor. \n";
Qiang XUe28d1fa2019-02-27 13:50:56 +0800171 *pBusId = -1;
172 *pSlaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800173 return false;
174}
175
Qiang XU88b7f282019-08-14 22:51:43 +0800176static constexpr bool debugLanLeash = false;
177boost::container::flat_map<int, bool> lanStatusMap;
Qiang XU74ddf862019-09-12 17:12:13 +0800178boost::container::flat_map<int, std::string> lanInfoMap;
Qiang XU88b7f282019-08-14 22:51:43 +0800179boost::container::flat_map<std::string, int> pathSuffixMap;
180
Lei YUba637932021-03-17 10:35:00 +0800181static void getNicNameInfo(
182 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
Qiang XU74ddf862019-09-12 17:12:13 +0800183{
184 auto getter = std::make_shared<GetSensorConfiguration>(
Ed Tanous8a17c302021-09-02 15:07:11 -0700185 dbusConnection, [](const ManagedObjectType& sensorConfigurations) {
Qiang XU74ddf862019-09-12 17:12:13 +0800186 // Get NIC name and save to map
187 lanInfoMap.clear();
188 for (const std::pair<sdbusplus::message::object_path, SensorData>&
189 sensor : sensorConfigurations)
190 {
191 const std::pair<
192 std::string,
193 boost::container::flat_map<std::string, BasicVariantType>>*
194 baseConfiguration = nullptr;
195
196 // find base configuration
197 auto sensorBase = sensor.second.find(nicType);
198 if (sensorBase == sensor.second.end())
199 {
200 continue;
201 }
202 baseConfiguration = &(*sensorBase);
203
204 auto findEthIndex = baseConfiguration->second.find("EthIndex");
205 auto findName = baseConfiguration->second.find("Name");
206
207 if (findEthIndex != baseConfiguration->second.end() &&
208 findName != baseConfiguration->second.end())
209 {
210 auto* pEthIndex =
211 std::get_if<uint64_t>(&findEthIndex->second);
212 auto* pName = std::get_if<std::string>(&findName->second);
213 if (pEthIndex != nullptr && pName != nullptr)
214 {
215 lanInfoMap[*pEthIndex] = *pName;
216 if (debugLanLeash)
217 {
218 std::cout << "find name of eth" << *pEthIndex
219 << " is " << *pName << "\n";
220 }
221 }
222 }
223 }
224
225 if (lanInfoMap.size() == 0)
226 {
227 std::cerr << "can't find matched NIC name. \n";
228 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700229 });
Qiang XU74ddf862019-09-12 17:12:13 +0800230
231 getter->getConfiguration(
232 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
233}
234
Qiang XU88b7f282019-08-14 22:51:43 +0800235static void processLanStatusChange(sdbusplus::message::message& message)
236{
237 const std::string& pathName = message.get_path();
238 std::string interfaceName;
239 boost::container::flat_map<std::string, BasicVariantType> properties;
240 message.read(interfaceName, properties);
241
242 auto findStateProperty = properties.find("OperationalState");
243 if (findStateProperty == properties.end())
244 {
245 return;
246 }
Qiang XU74ddf862019-09-12 17:12:13 +0800247 std::string* pState =
248 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800249 if (pState == nullptr)
250 {
251 std::cerr << "invalid OperationalState \n";
252 return;
253 }
254
255 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
256 *pState == "degraded");
257
258 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500259 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800260 if (pos == std::string::npos || pathName.length() <= pos + 2)
261 {
262 std::cerr << "unexpected path name " << pathName << "\n";
263 return;
264 }
265 std::string suffixStr = pathName.substr(pos + 2);
266
267 auto findEthNum = pathSuffixMap.find(suffixStr);
268 if (findEthNum == pathSuffixMap.end())
269 {
270 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
271 return;
272 }
273 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800274
275 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800276 auto findLanStatus = lanStatusMap.find(ethNum);
277 if (findLanStatus == lanStatusMap.end())
278 {
279 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
280 return;
281 }
282 bool oldLanConnected = findLanStatus->second;
283
Qiang XU74ddf862019-09-12 17:12:13 +0800284 // get lan info from map
285 std::string lanInfo = "";
286 if (lanInfoMap.size() > 0)
287 {
288 auto findLanInfo = lanInfoMap.find(ethNum);
289 if (findLanInfo == lanInfoMap.end())
290 {
291 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
292 }
293 else
294 {
295 lanInfo = "(" + findLanInfo->second + ")";
296 }
297 }
298
Qiang XU88b7f282019-08-14 22:51:43 +0800299 if (debugLanLeash)
300 {
301 std::cout << "ethNum = " << ethNum << ", state = " << *pState
302 << ", oldLanConnected = "
303 << (oldLanConnected ? "true" : "false")
304 << ", newLanConnected = "
305 << (newLanConnected ? "true" : "false") << "\n";
306 }
307
308 if (oldLanConnected != newLanConnected)
309 {
Qiang XU74ddf862019-09-12 17:12:13 +0800310 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
Patrick Williams0c42f402021-08-27 16:05:45 -0500311 auto strState = newLanConnected ? "connected" : "lost";
312 auto strMsgId =
Qiang XU74ddf862019-09-12 17:12:13 +0800313 newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
Patrick Williams0c42f402021-08-27 16:05:45 -0500314
315 lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE",
316 strState, "REDFISH_MESSAGE_ID", strMsgId,
317 "REDFISH_MESSAGE_ARGS", strEthNum);
318
Qiang XU88b7f282019-08-14 22:51:43 +0800319 lanStatusMap[ethNum] = newLanConnected;
Qiang XU88b7f282019-08-14 22:51:43 +0800320 }
321}
322
Lei YUdd68d4a2021-03-16 22:17:23 +0800323/** @brief Initialize the lan status.
324 *
325 * @return true on success and false on failure
326 */
Lei YUba637932021-03-17 10:35:00 +0800327static bool initializeLanStatus(
328 const std::shared_ptr<sdbusplus::asio::connection>& conn)
Qiang XU88b7f282019-08-14 22:51:43 +0800329{
Qiang XU74ddf862019-09-12 17:12:13 +0800330 // init lan port name from configuration
331 getNicNameInfo(conn);
332
333 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800334 std::vector<fs::path> files;
335 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
336 {
337 std::cerr << "No eth in system\n";
Lei YUdd68d4a2021-03-16 22:17:23 +0800338 return false;
Qiang XU88b7f282019-08-14 22:51:43 +0800339 }
340
341 // iterate through all found eth files, and save ifindex
Ed Tanous8a57ec02020-10-09 12:46:52 -0700342 for (const fs::path& fileName : files)
Qiang XU88b7f282019-08-14 22:51:43 +0800343 {
344 if (debugLanLeash)
345 {
346 std::cout << "Reading " << fileName << "\n";
347 }
348 std::ifstream sysFile(fileName);
349 if (!sysFile.good())
350 {
351 std::cerr << "Failure reading " << fileName << "\n";
352 continue;
353 }
354 std::string line;
355 getline(sysFile, line);
356 const uint8_t ifindex = std::stoi(line);
357 // pathSuffix is ASCII of ifindex
358 const std::string& pathSuffix = std::to_string(ifindex + 30);
359
360 // extract ethNum
361 const std::string& fileStr = fileName.string();
362 const int pos = fileStr.find("eth");
363 const std::string& ethNumStr = fileStr.substr(pos + 3);
364 int ethNum = 0;
Ed Tanous8a57ec02020-10-09 12:46:52 -0700365 std::from_chars_result r = std::from_chars(
366 ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum);
367 if (r.ec != std::errc())
Qiang XU88b7f282019-08-14 22:51:43 +0800368 {
369 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
370 continue;
371 }
372
373 // save pathSuffix
374 pathSuffixMap[pathSuffix] = ethNum;
375 if (debugLanLeash)
376 {
377 std::cout << "ethNum = " << std::to_string(ethNum)
378 << ", ifindex = " << line
379 << ", pathSuffix = " << pathSuffix << "\n";
380 }
381
Qiang XU74ddf862019-09-12 17:12:13 +0800382 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800383 conn->async_method_call(
384 [ethNum](boost::system::error_code ec,
385 const std::variant<std::string>& property) {
Qiang XU74ddf862019-09-12 17:12:13 +0800386 lanStatusMap[ethNum] = false;
Qiang XU88b7f282019-08-14 22:51:43 +0800387 if (ec)
388 {
Qiang XU74ddf862019-09-12 17:12:13 +0800389 std::cerr << "Error reading init status of eth" << ethNum
390 << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800391 return;
392 }
393 const std::string* pState = std::get_if<std::string>(&property);
394 if (pState == nullptr)
395 {
396 std::cerr << "Unable to read lan status value\n";
397 return;
398 }
399 bool isLanConnected =
400 (*pState == "routable" || *pState == "carrier" ||
401 *pState == "degraded");
402 if (debugLanLeash)
403 {
404 std::cout << "ethNum = " << std::to_string(ethNum)
405 << ", init LAN status = "
406 << (isLanConnected ? "true" : "false") << "\n";
407 }
408 lanStatusMap[ethNum] = isLanConnected;
409 },
410 "org.freedesktop.network1",
411 "/org/freedesktop/network1/link/_" + pathSuffix,
412 "org.freedesktop.DBus.Properties", "Get",
413 "org.freedesktop.network1.Link", "OperationalState");
414 }
Lei YUdd68d4a2021-03-16 22:17:23 +0800415 return true;
Qiang XU88b7f282019-08-14 22:51:43 +0800416}
417
Qiang XUe28d1fa2019-02-27 13:50:56 +0800418int main()
419{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800420 int busId = -1;
421 int slaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800422 bool gpioInverted = false;
423 IntrusionSensorType type = IntrusionSensorType::gpio;
424
425 // setup connection to dbus
426 boost::asio::io_service io;
427 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
428 auto objServer = sdbusplus::asio::object_server(systemBus);
429
430 // setup object server, define interface
431 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
432
433 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceChassis =
434 objServer.add_interface(
435 "/xyz/openbmc_project/Intrusion/Chassis_Intrusion",
436 "xyz.openbmc_project.Chassis.Intrusion");
437
438 ChassisIntrusionSensor chassisIntrusionSensor(io, ifaceChassis);
439
440 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800441 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800442 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800443 chassisIntrusionSensor.start(type, busId, slaveAddr, gpioInverted);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800444 }
445
446 // callback to handle configuration change
447 std::function<void(sdbusplus::message::message&)> eventHandler =
448 [&](sdbusplus::message::message& message) {
449 if (message.is_method_error())
450 {
451 std::cerr << "callback method error\n";
452 return;
453 }
454
455 std::cout << "rescan due to configuration change \n";
456 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800457 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800458 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800459 chassisIntrusionSensor.start(type, busId, slaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800460 gpioInverted);
461 }
462 };
463
Lei YUdd68d4a2021-03-16 22:17:23 +0800464 auto eventMatch = std::make_unique<sdbusplus::bus::match::match>(
Qiang XUe28d1fa2019-02-27 13:50:56 +0800465 static_cast<sdbusplus::bus::bus&>(*systemBus),
466 "type='signal',member='PropertiesChanged',path_namespace='" +
467 std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'",
468 eventHandler);
469
Lei YUdd68d4a2021-03-16 22:17:23 +0800470 if (initializeLanStatus(systemBus))
471 {
472 // add match to monitor lan status change
473 sdbusplus::bus::match::match lanStatusMatch(
474 static_cast<sdbusplus::bus::bus&>(*systemBus),
475 "type='signal', member='PropertiesChanged',"
476 "arg0namespace='org.freedesktop.network1.Link'",
477 [](sdbusplus::message::message& msg) {
478 processLanStatusChange(msg);
479 });
480
481 // add match to monitor entity manager signal about nic name config
482 // change
483 sdbusplus::bus::match::match lanConfigMatch(
484 static_cast<sdbusplus::bus::bus&>(*systemBus),
485 "type='signal', member='PropertiesChanged',path_namespace='" +
486 std::string(inventoryPath) + "',arg0namespace='" + nicType +
487 "'",
488 [&systemBus](sdbusplus::message::message& msg) {
489 if (msg.is_method_error())
490 {
491 std::cerr << "callback method error\n";
492 return;
493 }
494 getNicNameInfo(systemBus);
495 });
496 }
Qiang XU88b7f282019-08-14 22:51:43 +0800497
Qiang XUe28d1fa2019-02-27 13:50:56 +0800498 io.run();
499
500 return 0;
501}