Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "ChassisIntrusionSensor.hpp" |
| 18 | #include "Utils.hpp" |
| 19 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include <boost/asio/error.hpp> |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 21 | #include <boost/asio/io_context.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 22 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_map.hpp> |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/lg2.hpp> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 25 | #include <sdbusplus/asio/connection.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 27 | #include <sdbusplus/bus.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <sdbusplus/bus/match.hpp> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 29 | #include <sdbusplus/message.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 30 | |
| 31 | #include <array> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 32 | #include <charconv> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 33 | #include <chrono> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 34 | #include <cstdint> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 35 | #include <ctime> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 36 | #include <exception> |
| 37 | #include <filesystem> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 38 | #include <fstream> |
| 39 | #include <functional> |
| 40 | #include <iostream> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 41 | #include <map> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 42 | #include <memory> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 43 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 44 | #include <system_error> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 45 | #include <utility> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 46 | #include <variant> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 47 | #include <vector> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 48 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 49 | static constexpr bool debug = false; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 50 | |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 51 | static constexpr const char* sensorType = "ChassisIntrusionSensor"; |
| 52 | static constexpr const char* nicType = "NIC"; |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 53 | static constexpr auto nicTypes{std::to_array<const char*>({nicType})}; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 54 | |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 55 | static const std::map<std::string, std::string> compatibleHwmonNames = { |
| 56 | {"Aspeed2600_Hwmon", "intrusion0_alarm"} |
| 57 | // Add compatible strings here for new hwmon intrusion detection |
| 58 | // drivers that have different hwmon names but would also like to |
| 59 | // use the available Hwmon class. |
| 60 | }; |
| 61 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 62 | static void createSensorsFromConfig( |
| 63 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 64 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 65 | std::shared_ptr<ChassisIntrusionSensor>& pSensor) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 66 | { |
| 67 | // find matched configuration according to sensor type |
| 68 | ManagedObjectType sensorConfigurations; |
| 69 | bool useCache = false; |
| 70 | |
| 71 | if (!getSensorConfiguration(sensorType, dbusConnection, |
| 72 | sensorConfigurations, useCache)) |
| 73 | { |
| 74 | std::cerr << "error communicating to entity manager\n"; |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 75 | return; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | const SensorData* sensorData = nullptr; |
Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 79 | const std::pair<std::string, SensorBaseConfigMap>* baseConfiguration = |
| 80 | nullptr; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 81 | |
Zev Weiss | f343b8a | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 82 | for (const auto& [path, cfgData] : sensorConfigurations) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 83 | { |
| 84 | baseConfiguration = nullptr; |
Zev Weiss | f343b8a | 2022-08-12 18:21:01 -0700 | [diff] [blame] | 85 | sensorData = &cfgData; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 86 | |
| 87 | // match sensor type |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 88 | auto sensorBase = sensorData->find(configInterfaceName(sensorType)); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 89 | if (sensorBase == sensorData->end()) |
| 90 | { |
| 91 | std::cerr << "error finding base configuration \n"; |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | baseConfiguration = &(*sensorBase); |
| 96 | |
Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 97 | // Rearm defaults to "Automatic" mode |
| 98 | bool autoRearm = true; |
| 99 | auto findRearm = baseConfiguration->second.find("Rearm"); |
| 100 | if (findRearm != baseConfiguration->second.end()) |
| 101 | { |
| 102 | std::string rearmStr = std::get<std::string>(findRearm->second); |
| 103 | if (rearmStr != "Automatic" && rearmStr != "Manual") |
| 104 | { |
| 105 | std::cerr << "Wrong input for Rearm parameter\n"; |
| 106 | continue; |
| 107 | } |
| 108 | autoRearm = (rearmStr == "Automatic"); |
| 109 | } |
| 110 | |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 111 | // judge class, "Gpio", "Hwmon" or "I2C" |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 112 | auto findClass = baseConfiguration->second.find("Class"); |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 113 | if (findClass != baseConfiguration->second.end()) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 114 | { |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 115 | auto classString = std::get<std::string>(findClass->second); |
| 116 | if (classString == "Gpio") |
| 117 | { |
| 118 | auto findGpioPolarity = |
| 119 | baseConfiguration->second.find("GpioPolarity"); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 120 | |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 121 | if (findGpioPolarity == baseConfiguration->second.end()) |
| 122 | { |
| 123 | std::cerr |
| 124 | << "error finding gpio polarity in configuration \n"; |
| 125 | continue; |
| 126 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 127 | |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 128 | try |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 129 | { |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 130 | bool gpioInverted = |
| 131 | (std::get<std::string>(findGpioPolarity->second) == |
| 132 | "Low"); |
| 133 | pSensor = std::make_shared<ChassisIntrusionGpioSensor>( |
Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 134 | autoRearm, io, objServer, gpioInverted); |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 135 | pSensor->start(); |
| 136 | if (debug) |
| 137 | { |
| 138 | std::cout |
| 139 | << "find chassis intrusion sensor polarity inverted " |
| 140 | "flag is " |
| 141 | << gpioInverted << "\n"; |
| 142 | } |
| 143 | return; |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 144 | } |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 145 | catch (const std::bad_variant_access& e) |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 146 | { |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 147 | std::cerr << "invalid value for gpio info in config. \n"; |
| 148 | continue; |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 149 | } |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 150 | catch (const std::exception& e) |
| 151 | { |
| 152 | std::cerr << e.what() << std::endl; |
| 153 | continue; |
| 154 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 155 | } |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 156 | // If class string contains Hwmon string |
| 157 | else if (classString.find("Hwmon") != std::string::npos) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 158 | { |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 159 | std::string hwmonName; |
| 160 | std::map<std::string, std::string>::const_iterator |
| 161 | compatIterator = compatibleHwmonNames.find(classString); |
| 162 | |
| 163 | if (compatIterator == compatibleHwmonNames.end()) |
| 164 | { |
| 165 | std::cerr << "Hwmon Class string is not supported\n"; |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | hwmonName = compatIterator->second; |
| 170 | |
| 171 | try |
| 172 | { |
| 173 | pSensor = std::make_shared<ChassisIntrusionHwmonSensor>( |
Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 174 | autoRearm, io, objServer, hwmonName); |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 175 | pSensor->start(); |
| 176 | return; |
| 177 | } |
| 178 | catch (const std::exception& e) |
| 179 | { |
| 180 | std::cerr << e.what() << std::endl; |
| 181 | continue; |
| 182 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 183 | } |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 184 | else |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 185 | { |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 186 | auto findBus = baseConfiguration->second.find("Bus"); |
| 187 | auto findAddress = baseConfiguration->second.find("Address"); |
| 188 | if (findBus == baseConfiguration->second.end() || |
| 189 | findAddress == baseConfiguration->second.end()) |
| 190 | { |
| 191 | std::cerr |
| 192 | << "error finding bus or address in configuration \n"; |
| 193 | continue; |
| 194 | } |
| 195 | try |
| 196 | { |
| 197 | int busId = std::get<uint64_t>(findBus->second); |
| 198 | int slaveAddr = std::get<uint64_t>(findAddress->second); |
| 199 | pSensor = std::make_shared<ChassisIntrusionPchSensor>( |
Chau Ly | b318dca | 2022-10-26 04:12:52 +0000 | [diff] [blame] | 200 | autoRearm, io, objServer, busId, slaveAddr); |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 201 | pSensor->start(); |
| 202 | if (debug) |
| 203 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 204 | std::cout |
| 205 | << "find matched bus " << busId |
| 206 | << ", matched slave addr " << slaveAddr << "\n"; |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 207 | } |
| 208 | return; |
| 209 | } |
| 210 | catch (const std::bad_variant_access& e) |
| 211 | { |
| 212 | std::cerr |
| 213 | << "invalid value for bus or address in config. \n"; |
| 214 | continue; |
| 215 | } |
| 216 | catch (const std::exception& e) |
| 217 | { |
| 218 | std::cerr << e.what() << std::endl; |
| 219 | continue; |
| 220 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 221 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Chau Ly | 95f4993 | 2023-04-19 09:44:55 +0000 | [diff] [blame] | 225 | std::cerr << " Can't find matched I2C, GPIO or Hwmon configuration\n"; |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 226 | |
| 227 | // Make sure nothing runs when there's failure in configuration for the |
| 228 | // sensor after rescan |
| 229 | if (pSensor) |
| 230 | { |
| 231 | std::cerr << " Reset the occupied sensor pointer\n"; |
| 232 | pSensor = nullptr; |
| 233 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 234 | } |
| 235 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 236 | static constexpr bool debugLanLeash = false; |
| 237 | boost::container::flat_map<int, bool> lanStatusMap; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 238 | boost::container::flat_map<int, std::string> lanInfoMap; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 239 | boost::container::flat_map<std::string, int> pathSuffixMap; |
| 240 | |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 241 | static void getNicNameInfo( |
| 242 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 243 | { |
| 244 | auto getter = std::make_shared<GetSensorConfiguration>( |
Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 245 | dbusConnection, [](const ManagedObjectType& sensorConfigurations) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 246 | // Get NIC name and save to map |
| 247 | lanInfoMap.clear(); |
| 248 | for (const auto& [path, cfgData] : sensorConfigurations) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 249 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 250 | const std::pair<std::string, SensorBaseConfigMap>* |
| 251 | baseConfiguration = nullptr; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 252 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 253 | // find base configuration |
| 254 | auto sensorBase = cfgData.find(configInterfaceName(nicType)); |
| 255 | if (sensorBase == cfgData.end()) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 256 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 257 | continue; |
| 258 | } |
| 259 | baseConfiguration = &(*sensorBase); |
| 260 | |
| 261 | auto findEthIndex = baseConfiguration->second.find("EthIndex"); |
| 262 | auto findName = baseConfiguration->second.find("Name"); |
| 263 | |
| 264 | if (findEthIndex != baseConfiguration->second.end() && |
| 265 | findName != baseConfiguration->second.end()) |
| 266 | { |
| 267 | const auto* pEthIndex = |
| 268 | std::get_if<uint64_t>(&findEthIndex->second); |
| 269 | const auto* pName = |
| 270 | std::get_if<std::string>(&findName->second); |
| 271 | if (pEthIndex != nullptr && pName != nullptr) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 272 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 273 | lanInfoMap[*pEthIndex] = *pName; |
| 274 | if (debugLanLeash) |
| 275 | { |
| 276 | std::cout << "find name of eth" << *pEthIndex |
| 277 | << " is " << *pName << "\n"; |
| 278 | } |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 283 | if (lanInfoMap.empty()) |
| 284 | { |
| 285 | std::cerr << "can't find matched NIC name. \n"; |
| 286 | } |
| 287 | }); |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 288 | |
| 289 | getter->getConfiguration( |
| 290 | std::vector<std::string>{nicTypes.begin(), nicTypes.end()}); |
| 291 | } |
| 292 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 293 | static void processLanStatusChange(sdbusplus::message_t& message) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 294 | { |
| 295 | const std::string& pathName = message.get_path(); |
| 296 | std::string interfaceName; |
Zev Weiss | afd1504 | 2022-07-18 12:28:40 -0700 | [diff] [blame] | 297 | SensorBaseConfigMap properties; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 298 | message.read(interfaceName, properties); |
| 299 | |
| 300 | auto findStateProperty = properties.find("OperationalState"); |
| 301 | if (findStateProperty == properties.end()) |
| 302 | { |
| 303 | return; |
| 304 | } |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 305 | std::string* pState = |
| 306 | std::get_if<std::string>(&(findStateProperty->second)); |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 307 | if (pState == nullptr) |
| 308 | { |
| 309 | std::cerr << "invalid OperationalState \n"; |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | bool newLanConnected = (*pState == "routable" || *pState == "carrier" || |
| 314 | *pState == "degraded"); |
| 315 | |
| 316 | // get ethNum from path. /org/freedesktop/network1/link/_32 for eth0 |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 317 | size_t pos = pathName.find("/_"); |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 318 | if (pos == std::string::npos || pathName.length() <= pos + 2) |
| 319 | { |
| 320 | std::cerr << "unexpected path name " << pathName << "\n"; |
| 321 | return; |
| 322 | } |
| 323 | std::string suffixStr = pathName.substr(pos + 2); |
| 324 | |
| 325 | auto findEthNum = pathSuffixMap.find(suffixStr); |
| 326 | if (findEthNum == pathSuffixMap.end()) |
| 327 | { |
| 328 | std::cerr << "unexpected eth for suffixStr " << suffixStr << "\n"; |
| 329 | return; |
| 330 | } |
| 331 | int ethNum = findEthNum->second; |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 332 | |
| 333 | // get lan status from map |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 334 | auto findLanStatus = lanStatusMap.find(ethNum); |
| 335 | if (findLanStatus == lanStatusMap.end()) |
| 336 | { |
| 337 | std::cerr << "unexpected eth " << ethNum << " in lanStatusMap \n"; |
| 338 | return; |
| 339 | } |
| 340 | bool oldLanConnected = findLanStatus->second; |
| 341 | |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 342 | // get lan info from map |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 343 | std::string lanInfo; |
| 344 | if (!lanInfoMap.empty()) |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 345 | { |
| 346 | auto findLanInfo = lanInfoMap.find(ethNum); |
| 347 | if (findLanInfo == lanInfoMap.end()) |
| 348 | { |
| 349 | std::cerr << "unexpected eth " << ethNum << " in lanInfoMap \n"; |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | lanInfo = "(" + findLanInfo->second + ")"; |
| 354 | } |
| 355 | } |
| 356 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 357 | if (debugLanLeash) |
| 358 | { |
| 359 | std::cout << "ethNum = " << ethNum << ", state = " << *pState |
| 360 | << ", oldLanConnected = " |
| 361 | << (oldLanConnected ? "true" : "false") |
| 362 | << ", newLanConnected = " |
| 363 | << (newLanConnected ? "true" : "false") << "\n"; |
| 364 | } |
| 365 | |
| 366 | if (oldLanConnected != newLanConnected) |
| 367 | { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 368 | std::string strEthNum = "eth" + std::to_string(ethNum) + lanInfo; |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 369 | const auto* strState = newLanConnected ? "connected" : "lost"; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 370 | const auto* strMsgId = |
| 371 | newLanConnected ? "OpenBMC.0.1.LanRegained" : "OpenBMC.0.1.LanLost"; |
Patrick Williams | 0c42f40 | 2021-08-27 16:05:45 -0500 | [diff] [blame] | 372 | |
| 373 | lg2::info("{ETHDEV} LAN leash {STATE}", "ETHDEV", strEthNum, "STATE", |
| 374 | strState, "REDFISH_MESSAGE_ID", strMsgId, |
| 375 | "REDFISH_MESSAGE_ARGS", strEthNum); |
| 376 | |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 377 | lanStatusMap[ethNum] = newLanConnected; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 381 | /** @brief Initialize the lan status. |
| 382 | * |
| 383 | * @return true on success and false on failure |
| 384 | */ |
Lei YU | ba63793 | 2021-03-17 10:35:00 +0800 | [diff] [blame] | 385 | static bool initializeLanStatus( |
| 386 | const std::shared_ptr<sdbusplus::asio::connection>& conn) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 387 | { |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 388 | // init lan port name from configuration |
| 389 | getNicNameInfo(conn); |
| 390 | |
| 391 | // get eth info from sysfs |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 392 | std::vector<fs::path> files; |
| 393 | if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files)) |
| 394 | { |
| 395 | std::cerr << "No eth in system\n"; |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 396 | return false; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // iterate through all found eth files, and save ifindex |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 400 | for (const fs::path& fileName : files) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 401 | { |
| 402 | if (debugLanLeash) |
| 403 | { |
| 404 | std::cout << "Reading " << fileName << "\n"; |
| 405 | } |
| 406 | std::ifstream sysFile(fileName); |
| 407 | if (!sysFile.good()) |
| 408 | { |
| 409 | std::cerr << "Failure reading " << fileName << "\n"; |
| 410 | continue; |
| 411 | } |
| 412 | std::string line; |
| 413 | getline(sysFile, line); |
| 414 | const uint8_t ifindex = std::stoi(line); |
| 415 | // pathSuffix is ASCII of ifindex |
| 416 | const std::string& pathSuffix = std::to_string(ifindex + 30); |
| 417 | |
| 418 | // extract ethNum |
| 419 | const std::string& fileStr = fileName.string(); |
| 420 | const int pos = fileStr.find("eth"); |
| 421 | const std::string& ethNumStr = fileStr.substr(pos + 3); |
| 422 | int ethNum = 0; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 423 | std::from_chars_result r = std::from_chars( |
| 424 | ethNumStr.data(), ethNumStr.data() + ethNumStr.size(), ethNum); |
| 425 | if (r.ec != std::errc()) |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 426 | { |
| 427 | std::cerr << "invalid ethNum string: " << ethNumStr << "\n"; |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | // save pathSuffix |
| 432 | pathSuffixMap[pathSuffix] = ethNum; |
| 433 | if (debugLanLeash) |
| 434 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 435 | std::cout << "ethNum = " << std::to_string(ethNum) << ", ifindex = " |
| 436 | << line << ", pathSuffix = " << pathSuffix << "\n"; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 437 | } |
| 438 | |
Qiang XU | 74ddf86 | 2019-09-12 17:12:13 +0800 | [diff] [blame] | 439 | // init lan connected status from networkd |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 440 | conn->async_method_call( |
| 441 | [ethNum](boost::system::error_code ec, |
| 442 | const std::variant<std::string>& property) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 443 | lanStatusMap[ethNum] = false; |
| 444 | if (ec) |
| 445 | { |
| 446 | std::cerr |
| 447 | << "Error reading init status of eth" << ethNum << "\n"; |
| 448 | return; |
| 449 | } |
| 450 | const std::string* pState = std::get_if<std::string>(&property); |
| 451 | if (pState == nullptr) |
| 452 | { |
| 453 | std::cerr << "Unable to read lan status value\n"; |
| 454 | return; |
| 455 | } |
| 456 | bool isLanConnected = |
| 457 | (*pState == "routable" || *pState == "carrier" || |
| 458 | *pState == "degraded"); |
| 459 | if (debugLanLeash) |
| 460 | { |
| 461 | std::cout << "ethNum = " << std::to_string(ethNum) |
| 462 | << ", init LAN status = " |
| 463 | << (isLanConnected ? "true" : "false") << "\n"; |
| 464 | } |
| 465 | lanStatusMap[ethNum] = isLanConnected; |
| 466 | }, |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 467 | "org.freedesktop.network1", |
| 468 | "/org/freedesktop/network1/link/_" + pathSuffix, |
| 469 | "org.freedesktop.DBus.Properties", "Get", |
| 470 | "org.freedesktop.network1.Link", "OperationalState"); |
| 471 | } |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 472 | return true; |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 475 | int main() |
| 476 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 477 | std::shared_ptr<ChassisIntrusionSensor> intrusionSensor; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 478 | |
| 479 | // setup connection to dbus |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 480 | boost::asio::io_context io; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 481 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 482 | |
| 483 | // setup object server, define interface |
| 484 | systemBus->request_name("xyz.openbmc_project.IntrusionSensor"); |
| 485 | |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 486 | sdbusplus::asio::object_server objServer(systemBus, true); |
| 487 | |
Chau Ly | 889af82 | 2023-02-20 07:13:52 +0000 | [diff] [blame] | 488 | objServer.add_manager("/xyz/openbmc_project/Chassis"); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 489 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 490 | createSensorsFromConfig(io, objServer, systemBus, intrusionSensor); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 491 | |
| 492 | // callback to handle configuration change |
Ian Woloschin | 0df80a1 | 2023-11-06 14:27:44 +0000 | [diff] [blame] | 493 | boost::asio::steady_timer filterTimer(io); |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 494 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 495 | [&](sdbusplus::message_t& message) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 496 | if (message.is_method_error()) |
Ian Woloschin | 0df80a1 | 2023-11-06 14:27:44 +0000 | [diff] [blame] | 497 | { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 498 | std::cerr << "callback method error\n"; |
Ian Woloschin | 0df80a1 | 2023-11-06 14:27:44 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 501 | // this implicitly cancels the timer |
| 502 | filterTimer.expires_after(std::chrono::seconds(1)); |
| 503 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 504 | if (ec == boost::asio::error::operation_aborted) |
| 505 | { |
| 506 | // timer was cancelled |
| 507 | return; |
| 508 | } |
| 509 | std::cout << "rescan due to configuration change \n"; |
| 510 | createSensorsFromConfig(io, objServer, systemBus, |
| 511 | intrusionSensor); |
| 512 | }); |
| 513 | }; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 514 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 515 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 516 | setupPropertiesChangedMatches( |
| 517 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 518 | |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 519 | if (initializeLanStatus(systemBus)) |
| 520 | { |
| 521 | // add match to monitor lan status change |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 522 | sdbusplus::bus::match_t lanStatusMatch( |
| 523 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 524 | "type='signal', member='PropertiesChanged'," |
| 525 | "arg0namespace='org.freedesktop.network1.Link'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 526 | [](sdbusplus::message_t& msg) { processLanStatusChange(msg); }); |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 527 | |
| 528 | // add match to monitor entity manager signal about nic name config |
| 529 | // change |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 530 | sdbusplus::bus::match_t lanConfigMatch( |
| 531 | static_cast<sdbusplus::bus_t&>(*systemBus), |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 532 | "type='signal', member='PropertiesChanged',path_namespace='" + |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 533 | std::string(inventoryPath) + "',arg0namespace='" + |
| 534 | configInterfaceName(nicType) + "'", |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 535 | [&systemBus](sdbusplus::message_t& msg) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame] | 536 | if (msg.is_method_error()) |
| 537 | { |
| 538 | std::cerr << "callback method error\n"; |
| 539 | return; |
| 540 | } |
| 541 | getNicNameInfo(systemBus); |
| 542 | }); |
Lei YU | dd68d4a | 2021-03-16 22:17:23 +0800 | [diff] [blame] | 543 | } |
Qiang XU | 88b7f28 | 2019-08-14 22:51:43 +0800 | [diff] [blame] | 544 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 545 | io.run(); |
| 546 | |
| 547 | return 0; |
| 548 | } |