blob: f6cf9aac3c33400840f25b50362809e7bf6db797 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "ChassisIntrusionSensor.hpp"
18#include "Utils.hpp"
19
Qiang XU88b7f282019-08-14 22:51:43 +080020#include <systemd/sd-journal.h>
21
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <array>
Qiang XUe28d1fa2019-02-27 13:50:56 +080023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/asio.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070025#include <boost/container/flat_map.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080026#include <chrono>
27#include <ctime>
Qiang XU88b7f282019-08-14 22:51:43 +080028#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <functional>
Qiang XUe28d1fa2019-02-27 13:50:56 +080030#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
Qiang XUe28d1fa2019-02-27 13:50:56 +080032#include <sdbusplus/asio/connection.hpp>
33#include <sdbusplus/asio/object_server.hpp>
34#include <sdbusplus/asio/sd_event.hpp>
35#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <sdbusplus/bus/match.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080037#include <sdbusplus/exception.hpp>
38#include <sdbusplus/server.hpp>
39#include <sdbusplus/timer.hpp>
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
45static constexpr bool DEBUG = false;
46
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(
55 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
127 if (DEBUG)
128 {
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
138 else if (*pType == IntrusionSensorType::pch)
139 {
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
160 if (DEBUG)
161 {
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
Qiang XU74ddf862019-09-12 17:12:13 +0800181static void
182 getNicNameInfo(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
183{
184 auto getter = std::make_shared<GetSensorConfiguration>(
185 dbusConnection,
186 std::move([](const ManagedObjectType& sensorConfigurations) {
187 // 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 }
230 }));
231
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
327static void
328 monitorLanStatusChange(std::shared_ptr<sdbusplus::asio::connection> conn)
329{
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";
338 return;
339 }
340
341 // iterate through all found eth files, and save ifindex
342 for (auto& fileName : files)
343 {
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;
365 try
366 {
367 ethNum = std::stoul(ethNumStr);
368 }
369 catch (const std::invalid_argument& err)
370 {
371 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
372 continue;
373 }
374
375 // save pathSuffix
376 pathSuffixMap[pathSuffix] = ethNum;
377 if (debugLanLeash)
378 {
379 std::cout << "ethNum = " << std::to_string(ethNum)
380 << ", ifindex = " << line
381 << ", pathSuffix = " << pathSuffix << "\n";
382 }
383
Qiang XU74ddf862019-09-12 17:12:13 +0800384 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800385 conn->async_method_call(
386 [ethNum](boost::system::error_code ec,
387 const std::variant<std::string>& property) {
Qiang XU74ddf862019-09-12 17:12:13 +0800388 lanStatusMap[ethNum] = false;
Qiang XU88b7f282019-08-14 22:51:43 +0800389 if (ec)
390 {
Qiang XU74ddf862019-09-12 17:12:13 +0800391 std::cerr << "Error reading init status of eth" << ethNum
392 << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800393 return;
394 }
395 const std::string* pState = std::get_if<std::string>(&property);
396 if (pState == nullptr)
397 {
398 std::cerr << "Unable to read lan status value\n";
399 return;
400 }
401 bool isLanConnected =
402 (*pState == "routable" || *pState == "carrier" ||
403 *pState == "degraded");
404 if (debugLanLeash)
405 {
406 std::cout << "ethNum = " << std::to_string(ethNum)
407 << ", init LAN status = "
408 << (isLanConnected ? "true" : "false") << "\n";
409 }
410 lanStatusMap[ethNum] = isLanConnected;
411 },
412 "org.freedesktop.network1",
413 "/org/freedesktop/network1/link/_" + pathSuffix,
414 "org.freedesktop.DBus.Properties", "Get",
415 "org.freedesktop.network1.Link", "OperationalState");
416 }
417
418 // add match to monitor lan status change
419 static sdbusplus::bus::match::match match(
420 static_cast<sdbusplus::bus::bus&>(*conn),
421 "type='signal', member='PropertiesChanged',"
422 "arg0namespace='org.freedesktop.network1.Link'",
423 [](sdbusplus::message::message& msg) { processLanStatusChange(msg); });
Qiang XU74ddf862019-09-12 17:12:13 +0800424
425 // add match to monitor entity manager signal about nic name config change
426 static sdbusplus::bus::match::match match2(
427 static_cast<sdbusplus::bus::bus&>(*conn),
428 "type='signal', member='PropertiesChanged',path_namespace='" +
429 std::string(inventoryPath) + "',arg0namespace='" + nicType + "'",
430 [&conn](sdbusplus::message::message& msg) {
431 if (msg.is_method_error())
432 {
433 std::cerr << "callback method error\n";
434 return;
435 }
436 getNicNameInfo(conn);
437 });
Qiang XU88b7f282019-08-14 22:51:43 +0800438}
439
Qiang XUe28d1fa2019-02-27 13:50:56 +0800440int main()
441{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800442 int busId = -1;
443 int slaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800444 bool gpioInverted = false;
445 IntrusionSensorType type = IntrusionSensorType::gpio;
446
447 // setup connection to dbus
448 boost::asio::io_service io;
449 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
450 auto objServer = sdbusplus::asio::object_server(systemBus);
451
452 // setup object server, define interface
453 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
454
455 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceChassis =
456 objServer.add_interface(
457 "/xyz/openbmc_project/Intrusion/Chassis_Intrusion",
458 "xyz.openbmc_project.Chassis.Intrusion");
459
460 ChassisIntrusionSensor chassisIntrusionSensor(io, ifaceChassis);
461
462 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800463 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800464 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800465 chassisIntrusionSensor.start(type, busId, slaveAddr, gpioInverted);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800466 }
467
468 // callback to handle configuration change
469 std::function<void(sdbusplus::message::message&)> eventHandler =
470 [&](sdbusplus::message::message& message) {
471 if (message.is_method_error())
472 {
473 std::cerr << "callback method error\n";
474 return;
475 }
476
477 std::cout << "rescan due to configuration change \n";
478 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800479 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800480 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800481 chassisIntrusionSensor.start(type, busId, slaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800482 gpioInverted);
483 }
484 };
485
486 auto match = std::make_unique<sdbusplus::bus::match::match>(
487 static_cast<sdbusplus::bus::bus&>(*systemBus),
488 "type='signal',member='PropertiesChanged',path_namespace='" +
489 std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'",
490 eventHandler);
491
Qiang XU88b7f282019-08-14 22:51:43 +0800492 monitorLanStatusChange(systemBus);
493
Qiang XUe28d1fa2019-02-27 13:50:56 +0800494 io.run();
495
496 return 0;
497}