| Alexander Hansen | 306542d | 2025-10-30 17:50:11 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2018 IBM Corporation |
| 3 | |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 4 | #include "argument.hpp" |
| 5 | #include "ncsi_util.hpp" |
| 6 | |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <unistd.h> |
| 9 | |
| Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 11 | #include <stdplus/numeric/str.hpp> |
| 12 | #include <stdplus/str/buf.hpp> |
| Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 13 | |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 14 | #include <string> |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 15 | #include <vector> |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 16 | |
| 17 | static void exitWithError(const char* err, char** argv) |
| 18 | { |
| 19 | phosphor::network::ncsi::ArgumentParser::usage(argv); |
| Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 20 | lg2::error("ERROR: {ERROR}", "ERROR", err); |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 21 | exit(EXIT_FAILURE); |
| 22 | } |
| 23 | |
| Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 24 | static void printInfo(phosphor::network::ncsi::InterfaceInfo& info) |
| 25 | { |
| 26 | using namespace phosphor::network::ncsi; |
| 27 | |
| 28 | for (PackageInfo& pkg : info.packages) |
| 29 | { |
| 30 | lg2::debug("Package id : {ID}", "ID", pkg.id); |
| 31 | if (pkg.forced) |
| 32 | { |
| 33 | lg2::debug(" package is forced"); |
| 34 | } |
| 35 | for (ChannelInfo& chan : pkg.channels) |
| 36 | { |
| 37 | lg2::debug(" Channel id : {ID}", "ID", chan.id); |
| 38 | if (chan.forced) |
| 39 | { |
| 40 | lg2::debug(" channel is forced"); |
| 41 | } |
| 42 | if (chan.active) |
| 43 | { |
| 44 | lg2::debug(" channel is active"); |
| 45 | } |
| 46 | |
| 47 | lg2::debug(" version {MAJOR}.{MINOR} ({STR})", "MAJOR", |
| 48 | chan.version_major, "MINOR", chan.version_minor, "STR", |
| 49 | chan.version); |
| 50 | |
| 51 | lg2::debug(" link state {LINK}", "LINK", lg2::hex, |
| 52 | chan.link_state); |
| 53 | |
| 54 | auto& vlans = chan.vlan_ids; |
| 55 | |
| 56 | if (!vlans.empty()) |
| 57 | { |
| 58 | lg2::debug(" Actve VLAN IDs:"); |
| 59 | for (uint16_t vlan : vlans) |
| 60 | { |
| 61 | lg2::debug(" VID: {VLAN_ID}", "VLAN_ID", vlan); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 68 | int main(int argc, char** argv) |
| 69 | { |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 70 | using namespace phosphor::network; |
| 71 | using namespace phosphor::network::ncsi; |
| 72 | // Read arguments. |
| 73 | auto options = ArgumentParser(argc, argv); |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 74 | int packageInt{}; |
| 75 | int channelInt{}; |
| 76 | int indexInt{}; |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 77 | |
| 78 | // Parse out interface argument. |
| 79 | auto ifIndex = (options)["index"]; |
| 80 | try |
| 81 | { |
| 82 | indexInt = stoi(ifIndex, nullptr); |
| 83 | } |
| 84 | catch (const std::exception& e) |
| 85 | { |
| 86 | exitWithError("Interface not specified.", argv); |
| 87 | } |
| 88 | |
| 89 | if (indexInt < 0) |
| 90 | { |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | exitWithError("Interface value should be greater than equal to 0", |
| 92 | argv); |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| Jeremy Kerr | 2d0b48d | 2024-09-16 13:03:26 +0800 | [diff] [blame] | 95 | NetlinkInterface interface(indexInt); |
| Jeremy Kerr | 8d9af02 | 2024-07-26 16:47:16 +0800 | [diff] [blame] | 96 | |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 97 | // Parse out package argument. |
| 98 | auto package = (options)["package"]; |
| 99 | try |
| 100 | { |
| 101 | packageInt = stoi(package, nullptr); |
| 102 | } |
| 103 | catch (const std::exception& e) |
| 104 | { |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 105 | packageInt = DEFAULT_VALUE; |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (packageInt < 0) |
| 109 | { |
| 110 | packageInt = DEFAULT_VALUE; |
| 111 | } |
| 112 | |
| 113 | // Parse out channel argument. |
| 114 | auto channel = (options)["channel"]; |
| 115 | try |
| 116 | { |
| 117 | channelInt = stoi(channel, nullptr); |
| 118 | } |
| 119 | catch (const std::exception& e) |
| 120 | { |
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 121 | channelInt = DEFAULT_VALUE; |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | if (channelInt < 0) |
| 125 | { |
| 126 | channelInt = DEFAULT_VALUE; |
| 127 | } |
| 128 | |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 129 | auto payloadStr = (options)["oem-payload"]; |
| 130 | if (!payloadStr.empty()) |
| 131 | { |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 132 | if (payloadStr.size() % 2 || payloadStr.size() < 2) |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 133 | exitWithError("Payload invalid: specify two hex digits per byte.", |
| 134 | argv); |
| 135 | |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 136 | // Payload string is in the format <type>[<payload>] |
| 137 | // (e.g. "50000001572100"), where the first two characters (i.e. "50") |
| 138 | // represent the command type, and the rest the payload. Split this |
| 139 | // up for the ncsi-cmd operation, which has these as separate arguments. |
| 140 | std::string typeStr(payloadStr.substr(0, 2)); |
| 141 | std::string dataStr(payloadStr.substr(2)); |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 142 | |
| 143 | if (packageInt == DEFAULT_VALUE) |
| 144 | { |
| 145 | exitWithError("Package not specified.", argv); |
| 146 | } |
| 147 | |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 148 | std::vector<std::string> args = { |
| 149 | "ncsi-cmd", |
| 150 | "-i", |
| 151 | std::to_string(indexInt), |
| 152 | "-p", |
| 153 | std::to_string(packageInt), |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | if (channelInt != DEFAULT_VALUE) |
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 157 | { |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 158 | args.push_back("-c"); |
| 159 | args.push_back(std::to_string(channelInt)); |
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| Jeremy Kerr | 0963c4b | 2025-03-06 14:51:37 +0800 | [diff] [blame] | 162 | args.push_back("raw"); |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 163 | args.push_back(typeStr); |
| 164 | args.push_back(dataStr); |
| Jeremy Kerr | b788524 | 2024-09-16 12:43:36 +0800 | [diff] [blame] | 165 | |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 166 | /* Convert to C argv array. execvp()'s argv argument is not const, |
| 167 | * whereas .c_str() is, so we need to strdup here. |
| 168 | */ |
| 169 | char** argv = new char*[args.size() + 1](); |
| 170 | for (size_t i = 0; i < args.size(); i++) |
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 171 | { |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 172 | argv[i] = strdup(args[i].c_str()); |
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 173 | } |
| Jeremy Kerr | bad17c0 | 2024-11-21 16:44:39 +0800 | [diff] [blame] | 174 | argv[args.size()] = NULL; |
| 175 | |
| 176 | lg2::debug("ncsi-netlink [..] -o is deprecated by ncsi-cmd"); |
| 177 | execvp(argv[0], argv); |
| 178 | lg2::error("exec failed; use ncsi-cmd directly"); |
| 179 | |
| 180 | for (size_t i = 0; i < args.size(); i++) |
| 181 | { |
| 182 | free(argv[i]); |
| 183 | } |
| 184 | delete[] argv; |
| 185 | return EXIT_FAILURE; |
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 186 | } |
| 187 | else if ((options)["set"] == "true") |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 188 | { |
| Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 189 | // Can not perform set operation without package. |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 190 | if (packageInt == DEFAULT_VALUE) |
| 191 | { |
| 192 | exitWithError("Package not specified.", argv); |
| 193 | } |
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 194 | return interface.setChannel(packageInt, channelInt); |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 195 | } |
| 196 | else if ((options)["info"] == "true") |
| 197 | { |
| Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 198 | auto info = interface.getInfo(packageInt); |
| 199 | if (!info) |
| 200 | { |
| 201 | return EXIT_FAILURE; |
| 202 | } |
| 203 | printInfo(*info); |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 204 | } |
| 205 | else if ((options)["clear"] == "true") |
| 206 | { |
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 207 | return interface.clearInterface(); |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 208 | } |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 209 | else if (!(options)["pmask"].empty()) |
| 210 | { |
| 211 | unsigned int mask{}; |
| 212 | try |
| 213 | { |
| 214 | size_t lastChar{}; |
| 215 | mask = std::stoul((options)["pmask"], &lastChar, 0); |
| 216 | if (lastChar < (options["pmask"].size())) |
| 217 | { |
| 218 | exitWithError("Package mask value is not valid", argv); |
| 219 | } |
| 220 | } |
| 221 | catch (const std::exception& e) |
| 222 | { |
| 223 | exitWithError("Package mask value is not valid", argv); |
| 224 | } |
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 225 | return interface.setPackageMask(mask); |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 226 | } |
| 227 | else if (!(options)["cmask"].empty()) |
| 228 | { |
| 229 | if (packageInt == DEFAULT_VALUE) |
| 230 | { |
| 231 | exitWithError("Package is not specified", argv); |
| 232 | } |
| 233 | unsigned int mask{}; |
| 234 | try |
| 235 | { |
| 236 | size_t lastChar{}; |
| 237 | mask = stoul((options)["cmask"], &lastChar, 0); |
| 238 | if (lastChar < (options["cmask"].size())) |
| 239 | { |
| 240 | exitWithError("Channel mask value is not valid", argv); |
| 241 | } |
| 242 | } |
| 243 | catch (const std::exception& e) |
| 244 | { |
| 245 | exitWithError("Channel mask value is not valid", argv); |
| 246 | } |
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 247 | return interface.setChannelMask(packageInt, mask); |
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 248 | } |
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 249 | else |
| 250 | { |
| 251 | exitWithError("No Command specified", argv); |
| 252 | } |
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 253 | return 0; |
| 254 | } |