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