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