blob: f3d95fb879c1172b0d7bb952601d066b92ef1348 [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
Qiang XUe28d1fa2019-02-27 13:50:56 +080022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/asio.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <boost/container/flat_map.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/asio/sd_event.hpp>
28#include <sdbusplus/bus.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <sdbusplus/bus/match.hpp>
Qiang XUe28d1fa2019-02-27 13:50:56 +080030#include <sdbusplus/exception.hpp>
31#include <sdbusplus/server.hpp>
32#include <sdbusplus/timer.hpp>
James Feist38fb5982020-05-28 10:09:54 -070033
34#include <array>
35#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
46static constexpr bool DEBUG = false;
47
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(
56 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
128 if (DEBUG)
129 {
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
139 else if (*pType == IntrusionSensorType::pch)
140 {
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
161 if (DEBUG)
162 {
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
Qiang XU74ddf862019-09-12 17:12:13 +0800182static void
183 getNicNameInfo(std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
184{
185 auto getter = std::make_shared<GetSensorConfiguration>(
186 dbusConnection,
187 std::move([](const ManagedObjectType& sensorConfigurations) {
188 // Get NIC name and save to map
189 lanInfoMap.clear();
190 for (const std::pair<sdbusplus::message::object_path, SensorData>&
191 sensor : sensorConfigurations)
192 {
193 const std::pair<
194 std::string,
195 boost::container::flat_map<std::string, BasicVariantType>>*
196 baseConfiguration = nullptr;
197
198 // find base configuration
199 auto sensorBase = sensor.second.find(nicType);
200 if (sensorBase == sensor.second.end())
201 {
202 continue;
203 }
204 baseConfiguration = &(*sensorBase);
205
206 auto findEthIndex = baseConfiguration->second.find("EthIndex");
207 auto findName = baseConfiguration->second.find("Name");
208
209 if (findEthIndex != baseConfiguration->second.end() &&
210 findName != baseConfiguration->second.end())
211 {
212 auto* pEthIndex =
213 std::get_if<uint64_t>(&findEthIndex->second);
214 auto* pName = std::get_if<std::string>(&findName->second);
215 if (pEthIndex != nullptr && pName != nullptr)
216 {
217 lanInfoMap[*pEthIndex] = *pName;
218 if (debugLanLeash)
219 {
220 std::cout << "find name of eth" << *pEthIndex
221 << " is " << *pName << "\n";
222 }
223 }
224 }
225 }
226
227 if (lanInfoMap.size() == 0)
228 {
229 std::cerr << "can't find matched NIC name. \n";
230 }
231 }));
232
233 getter->getConfiguration(
234 std::vector<std::string>{nicTypes.begin(), nicTypes.end()});
235}
236
Qiang XU88b7f282019-08-14 22:51:43 +0800237static void processLanStatusChange(sdbusplus::message::message& message)
238{
239 const std::string& pathName = message.get_path();
240 std::string interfaceName;
241 boost::container::flat_map<std::string, BasicVariantType> properties;
242 message.read(interfaceName, properties);
243
244 auto findStateProperty = properties.find("OperationalState");
245 if (findStateProperty == properties.end())
246 {
247 return;
248 }
Qiang XU74ddf862019-09-12 17:12:13 +0800249 std::string* pState =
250 std::get_if<std::string>(&(findStateProperty->second));
Qiang XU88b7f282019-08-14 22:51:43 +0800251 if (pState == nullptr)
252 {
253 std::cerr << "invalid OperationalState \n";
254 return;
255 }
256
257 bool newLanConnected = (*pState == "routable" || *pState == "carrier" ||
258 *pState == "degraded");
259
260 // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0
Brad Bishopfbb44ad2019-11-08 09:42:37 -0500261 size_t pos = pathName.find("/_");
Qiang XU88b7f282019-08-14 22:51:43 +0800262 if (pos == std::string::npos || pathName.length() <= pos + 2)
263 {
264 std::cerr << "unexpected path name " << pathName << "\n";
265 return;
266 }
267 std::string suffixStr = pathName.substr(pos + 2);
268
269 auto findEthNum = pathSuffixMap.find(suffixStr);
270 if (findEthNum == pathSuffixMap.end())
271 {
272 std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n";
273 return;
274 }
275 int ethNum = findEthNum->second;
Qiang XU74ddf862019-09-12 17:12:13 +0800276
277 // get lan status from map
Qiang XU88b7f282019-08-14 22:51:43 +0800278 auto findLanStatus = lanStatusMap.find(ethNum);
279 if (findLanStatus == lanStatusMap.end())
280 {
281 std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n";
282 return;
283 }
284 bool oldLanConnected = findLanStatus->second;
285
Qiang XU74ddf862019-09-12 17:12:13 +0800286 // get lan info from map
287 std::string lanInfo = "";
288 if (lanInfoMap.size() > 0)
289 {
290 auto findLanInfo = lanInfoMap.find(ethNum);
291 if (findLanInfo == lanInfoMap.end())
292 {
293 std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n";
294 }
295 else
296 {
297 lanInfo = "(" + findLanInfo->second + ")";
298 }
299 }
300
Qiang XU88b7f282019-08-14 22:51:43 +0800301 if (debugLanLeash)
302 {
303 std::cout << "ethNum = " << ethNum << ", state = " << *pState
304 << ", oldLanConnected = "
305 << (oldLanConnected ? "true" : "false")
306 << ", newLanConnected = "
307 << (newLanConnected ? "true" : "false") << "\n";
308 }
309
310 if (oldLanConnected != newLanConnected)
311 {
Qiang XU74ddf862019-09-12 17:12:13 +0800312 std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo;
313 std::string strEvent = strEthNum + " LAN leash " +
314 (newLanConnected ? "connected" : "lost");
315 std::string strMsgId =
316 newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost";
317 sd_journal_send("MESSAGE=%s", strEvent.c_str(), "PRIORITY=%i", LOG_INFO,
Qiang XU88b7f282019-08-14 22:51:43 +0800318 "REDFISH_MESSAGE_ID=%s", strMsgId.c_str(),
Qiang XU74ddf862019-09-12 17:12:13 +0800319 "REDFISH_MESSAGE_ARGS=%s", strEthNum.c_str(), NULL);
Qiang XU88b7f282019-08-14 22:51:43 +0800320 lanStatusMap[ethNum] = newLanConnected;
321 if (debugLanLeash)
322 {
Qiang XU74ddf862019-09-12 17:12:13 +0800323 std::cout << "log redfish event: " << strEvent << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800324 }
325 }
326}
327
328static void
329 monitorLanStatusChange(std::shared_ptr<sdbusplus::asio::connection> conn)
330{
Qiang XU74ddf862019-09-12 17:12:13 +0800331 // init lan port name from configuration
332 getNicNameInfo(conn);
333
334 // get eth info from sysfs
Qiang XU88b7f282019-08-14 22:51:43 +0800335 std::vector<fs::path> files;
336 if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
337 {
338 std::cerr << "No eth in system\n";
339 return;
340 }
341
342 // iterate through all found eth files, and save ifindex
343 for (auto& fileName : files)
344 {
345 if (debugLanLeash)
346 {
347 std::cout << "Reading " << fileName << "\n";
348 }
349 std::ifstream sysFile(fileName);
350 if (!sysFile.good())
351 {
352 std::cerr << "Failure reading " << fileName << "\n";
353 continue;
354 }
355 std::string line;
356 getline(sysFile, line);
357 const uint8_t ifindex = std::stoi(line);
358 // pathSuffix is ASCII of ifindex
359 const std::string& pathSuffix = std::to_string(ifindex + 30);
360
361 // extract ethNum
362 const std::string& fileStr = fileName.string();
363 const int pos = fileStr.find("eth");
364 const std::string& ethNumStr = fileStr.substr(pos + 3);
365 int ethNum = 0;
366 try
367 {
368 ethNum = std::stoul(ethNumStr);
369 }
370 catch (const std::invalid_argument& err)
371 {
372 std::cerr << "invalid ethNum string: " << ethNumStr << "\n";
373 continue;
374 }
375
376 // save pathSuffix
377 pathSuffixMap[pathSuffix] = ethNum;
378 if (debugLanLeash)
379 {
380 std::cout << "ethNum = " << std::to_string(ethNum)
381 << ", ifindex = " << line
382 << ", pathSuffix = " << pathSuffix << "\n";
383 }
384
Qiang XU74ddf862019-09-12 17:12:13 +0800385 // init lan connected status from networkd
Qiang XU88b7f282019-08-14 22:51:43 +0800386 conn->async_method_call(
387 [ethNum](boost::system::error_code ec,
388 const std::variant<std::string>& property) {
Qiang XU74ddf862019-09-12 17:12:13 +0800389 lanStatusMap[ethNum] = false;
Qiang XU88b7f282019-08-14 22:51:43 +0800390 if (ec)
391 {
Qiang XU74ddf862019-09-12 17:12:13 +0800392 std::cerr << "Error reading init status of eth" << ethNum
393 << "\n";
Qiang XU88b7f282019-08-14 22:51:43 +0800394 return;
395 }
396 const std::string* pState = std::get_if<std::string>(&property);
397 if (pState == nullptr)
398 {
399 std::cerr << "Unable to read lan status value\n";
400 return;
401 }
402 bool isLanConnected =
403 (*pState == "routable" || *pState == "carrier" ||
404 *pState == "degraded");
405 if (debugLanLeash)
406 {
407 std::cout << "ethNum = " << std::to_string(ethNum)
408 << ", init LAN status = "
409 << (isLanConnected ? "true" : "false") << "\n";
410 }
411 lanStatusMap[ethNum] = isLanConnected;
412 },
413 "org.freedesktop.network1",
414 "/org/freedesktop/network1/link/_" + pathSuffix,
415 "org.freedesktop.DBus.Properties", "Get",
416 "org.freedesktop.network1.Link", "OperationalState");
417 }
418
419 // add match to monitor lan status change
420 static sdbusplus::bus::match::match match(
421 static_cast<sdbusplus::bus::bus&>(*conn),
422 "type='signal', member='PropertiesChanged',"
423 "arg0namespace='org.freedesktop.network1.Link'",
424 [](sdbusplus::message::message& msg) { processLanStatusChange(msg); });
Qiang XU74ddf862019-09-12 17:12:13 +0800425
426 // add match to monitor entity manager signal about nic name config change
427 static sdbusplus::bus::match::match match2(
428 static_cast<sdbusplus::bus::bus&>(*conn),
429 "type='signal', member='PropertiesChanged',path_namespace='" +
430 std::string(inventoryPath) + "',arg0namespace='" + nicType + "'",
431 [&conn](sdbusplus::message::message& msg) {
432 if (msg.is_method_error())
433 {
434 std::cerr << "callback method error\n";
435 return;
436 }
437 getNicNameInfo(conn);
438 });
Qiang XU88b7f282019-08-14 22:51:43 +0800439}
440
Qiang XUe28d1fa2019-02-27 13:50:56 +0800441int main()
442{
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800443 int busId = -1;
444 int slaveAddr = -1;
Qiang XUe28d1fa2019-02-27 13:50:56 +0800445 bool gpioInverted = false;
446 IntrusionSensorType type = IntrusionSensorType::gpio;
447
448 // setup connection to dbus
449 boost::asio::io_service io;
450 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
451 auto objServer = sdbusplus::asio::object_server(systemBus);
452
453 // setup object server, define interface
454 systemBus->request_name("xyz.openbmc_project.IntrusionSensor");
455
456 std::shared_ptr<sdbusplus::asio::dbus_interface> ifaceChassis =
457 objServer.add_interface(
458 "/xyz/openbmc_project/Intrusion/Chassis_Intrusion",
459 "xyz.openbmc_project.Chassis.Intrusion");
460
461 ChassisIntrusionSensor chassisIntrusionSensor(io, ifaceChassis);
462
463 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800464 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800465 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800466 chassisIntrusionSensor.start(type, busId, slaveAddr, gpioInverted);
Qiang XUe28d1fa2019-02-27 13:50:56 +0800467 }
468
469 // callback to handle configuration change
470 std::function<void(sdbusplus::message::message&)> eventHandler =
471 [&](sdbusplus::message::message& message) {
472 if (message.is_method_error())
473 {
474 std::cerr << "callback method error\n";
475 return;
476 }
477
478 std::cout << "rescan due to configuration change \n";
479 if (getIntrusionSensorConfig(systemBus, &type, &busId, &slaveAddr,
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800480 &gpioInverted))
Qiang XUe28d1fa2019-02-27 13:50:56 +0800481 {
ZhikuiRenba8a8bf2020-01-09 15:55:43 -0800482 chassisIntrusionSensor.start(type, busId, slaveAddr,
Qiang XUe28d1fa2019-02-27 13:50:56 +0800483 gpioInverted);
484 }
485 };
486
487 auto match = std::make_unique<sdbusplus::bus::match::match>(
488 static_cast<sdbusplus::bus::bus&>(*systemBus),
489 "type='signal',member='PropertiesChanged',path_namespace='" +
490 std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'",
491 eventHandler);
492
Qiang XU88b7f282019-08-14 22:51:43 +0800493 monitorLanStatusChange(systemBus);
494
Qiang XUe28d1fa2019-02-27 13:50:56 +0800495 io.run();
496
497 return 0;
498}