| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 1 | /** | 
|  | 2 | * Copyright © 2018 IBM 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 | */ | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 16 | #include "argument.hpp" | 
|  | 17 | #include "ncsi_util.hpp" | 
|  | 18 |  | 
| Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 19 | #include <phosphor-logging/lg2.hpp> | 
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 20 | #include <stdplus/numeric/str.hpp> | 
|  | 21 | #include <stdplus/str/buf.hpp> | 
| Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 22 |  | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 23 | #include <string> | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 24 | #include <vector> | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 25 |  | 
|  | 26 | static void exitWithError(const char* err, char** argv) | 
|  | 27 | { | 
|  | 28 | phosphor::network::ncsi::ArgumentParser::usage(argv); | 
| Jagpal Singh Gill | d423beb | 2023-04-18 11:28:03 -0700 | [diff] [blame] | 29 | lg2::error("ERROR: {ERROR}", "ERROR", err); | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 30 | exit(EXIT_FAILURE); | 
|  | 31 | } | 
|  | 32 |  | 
| Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 33 | static void printInfo(phosphor::network::ncsi::InterfaceInfo& info) | 
|  | 34 | { | 
|  | 35 | using namespace phosphor::network::ncsi; | 
|  | 36 |  | 
|  | 37 | for (PackageInfo& pkg : info.packages) | 
|  | 38 | { | 
|  | 39 | lg2::debug("Package id : {ID}", "ID", pkg.id); | 
|  | 40 | if (pkg.forced) | 
|  | 41 | { | 
|  | 42 | lg2::debug("  package is forced"); | 
|  | 43 | } | 
|  | 44 | for (ChannelInfo& chan : pkg.channels) | 
|  | 45 | { | 
|  | 46 | lg2::debug("    Channel id : {ID}", "ID", chan.id); | 
|  | 47 | if (chan.forced) | 
|  | 48 | { | 
|  | 49 | lg2::debug("    channel is forced"); | 
|  | 50 | } | 
|  | 51 | if (chan.active) | 
|  | 52 | { | 
|  | 53 | lg2::debug("    channel is active"); | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | lg2::debug("      version {MAJOR}.{MINOR} ({STR})", "MAJOR", | 
|  | 57 | chan.version_major, "MINOR", chan.version_minor, "STR", | 
|  | 58 | chan.version); | 
|  | 59 |  | 
|  | 60 | lg2::debug("      link state {LINK}", "LINK", lg2::hex, | 
|  | 61 | chan.link_state); | 
|  | 62 |  | 
|  | 63 | auto& vlans = chan.vlan_ids; | 
|  | 64 |  | 
|  | 65 | if (!vlans.empty()) | 
|  | 66 | { | 
|  | 67 | lg2::debug("      Actve VLAN IDs:"); | 
|  | 68 | for (uint16_t vlan : vlans) | 
|  | 69 | { | 
|  | 70 | lg2::debug("        VID: {VLAN_ID}", "VLAN_ID", vlan); | 
|  | 71 | } | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 77 | static stdplus::StrBuf toHexStr(std::span<const uint8_t> c) noexcept | 
|  | 78 | { | 
|  | 79 | stdplus::StrBuf ret; | 
|  | 80 | if (c.empty()) | 
|  | 81 | { | 
|  | 82 | return ret; | 
|  | 83 | } | 
|  | 84 | stdplus::IntToStr<16, uint8_t> its; | 
|  | 85 | auto oit = ret.append(c.size() * 3); | 
|  | 86 | auto cit = c.begin(); | 
|  | 87 | oit = its(oit, *cit++, 2); | 
|  | 88 | for (; cit != c.end(); ++cit) | 
|  | 89 | { | 
|  | 90 | *oit++ = ' '; | 
|  | 91 | oit = its(oit, *cit, 2); | 
|  | 92 | } | 
|  | 93 | *oit = 0; | 
|  | 94 | return ret; | 
|  | 95 | } | 
|  | 96 |  | 
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 97 | int main(int argc, char** argv) | 
|  | 98 | { | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 99 | using namespace phosphor::network; | 
|  | 100 | using namespace phosphor::network::ncsi; | 
|  | 101 | // Read arguments. | 
|  | 102 | auto options = ArgumentParser(argc, argv); | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | int packageInt{}; | 
|  | 104 | int channelInt{}; | 
|  | 105 | int indexInt{}; | 
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 106 | int operationInt{DEFAULT_VALUE}; | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 107 |  | 
|  | 108 | // Parse out interface argument. | 
|  | 109 | auto ifIndex = (options)["index"]; | 
|  | 110 | try | 
|  | 111 | { | 
|  | 112 | indexInt = stoi(ifIndex, nullptr); | 
|  | 113 | } | 
|  | 114 | catch (const std::exception& e) | 
|  | 115 | { | 
|  | 116 | exitWithError("Interface not specified.", argv); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | if (indexInt < 0) | 
|  | 120 | { | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 121 | exitWithError("Interface value should be greater than equal to 0", | 
|  | 122 | argv); | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 123 | } | 
|  | 124 |  | 
| Jeremy Kerr | 2d0b48d | 2024-09-16 13:03:26 +0800 | [diff] [blame^] | 125 | NetlinkInterface interface(indexInt); | 
| Jeremy Kerr | 8d9af02 | 2024-07-26 16:47:16 +0800 | [diff] [blame] | 126 |  | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 127 | // Parse out package argument. | 
|  | 128 | auto package = (options)["package"]; | 
|  | 129 | try | 
|  | 130 | { | 
|  | 131 | packageInt = stoi(package, nullptr); | 
|  | 132 | } | 
|  | 133 | catch (const std::exception& e) | 
|  | 134 | { | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 135 | packageInt = DEFAULT_VALUE; | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 136 | } | 
|  | 137 |  | 
|  | 138 | if (packageInt < 0) | 
|  | 139 | { | 
|  | 140 | packageInt = DEFAULT_VALUE; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | // Parse out channel argument. | 
|  | 144 | auto channel = (options)["channel"]; | 
|  | 145 | try | 
|  | 146 | { | 
|  | 147 | channelInt = stoi(channel, nullptr); | 
|  | 148 | } | 
|  | 149 | catch (const std::exception& e) | 
|  | 150 | { | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 151 | channelInt = DEFAULT_VALUE; | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | if (channelInt < 0) | 
|  | 155 | { | 
|  | 156 | channelInt = DEFAULT_VALUE; | 
|  | 157 | } | 
|  | 158 |  | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 159 | auto payloadStr = (options)["oem-payload"]; | 
|  | 160 | if (!payloadStr.empty()) | 
|  | 161 | { | 
|  | 162 | std::string byte(2, '\0'); | 
|  | 163 | std::vector<unsigned char> payload; | 
|  | 164 |  | 
|  | 165 | if (payloadStr.size() % 2) | 
|  | 166 | exitWithError("Payload invalid: specify two hex digits per byte.", | 
|  | 167 | argv); | 
|  | 168 |  | 
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 169 | // Parse the payload string (e.g. "50000001572100") to byte data | 
|  | 170 | // The first two characters (i.e. "50") represent the Send Cmd Operation | 
|  | 171 | // All remaining pairs, interpreted in hex radix, represent the command | 
|  | 172 | // payload | 
|  | 173 | int sendCmdSelect{}; | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 174 | for (unsigned int i = 1; i < payloadStr.size(); i += 2) | 
|  | 175 | { | 
|  | 176 | byte[0] = payloadStr[i - 1]; | 
|  | 177 | byte[1] = payloadStr[i]; | 
|  | 178 |  | 
|  | 179 | try | 
|  | 180 | { | 
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 181 | sendCmdSelect = stoi(byte, nullptr, 16); | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 182 | } | 
|  | 183 | catch (const std::exception& e) | 
|  | 184 | { | 
|  | 185 | exitWithError("Payload invalid.", argv); | 
|  | 186 | } | 
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 187 | if (i == 1) | 
|  | 188 | { | 
|  | 189 | operationInt = sendCmdSelect; | 
|  | 190 | } | 
|  | 191 | else | 
|  | 192 | { | 
|  | 193 | payload.push_back(sendCmdSelect); | 
|  | 194 | } | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Johnathan Mantey | 1ebea28 | 2024-02-15 10:26:06 -0800 | [diff] [blame] | 197 | if (operationInt == DEFAULT_VALUE) | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 198 | { | 
|  | 199 | exitWithError("No payload specified.", argv); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | if (packageInt == DEFAULT_VALUE) | 
|  | 203 | { | 
|  | 204 | exitWithError("Package not specified.", argv); | 
|  | 205 | } | 
|  | 206 |  | 
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 207 | if (!payload.empty()) | 
|  | 208 | { | 
|  | 209 | lg2::debug("Payload: {PAYLOAD}", "PAYLOAD", toHexStr(payload)); | 
|  | 210 | } | 
|  | 211 |  | 
| Jeremy Kerr | b788524 | 2024-09-16 12:43:36 +0800 | [diff] [blame] | 212 | std::optional<uint8_t> chan = channelInt != DEFAULT_VALUE | 
|  | 213 | ? std::make_optional(channelInt) | 
|  | 214 | : std::nullopt; | 
|  | 215 | NCSICommand cmd(operationInt, packageInt, chan, payload); | 
|  | 216 |  | 
|  | 217 | auto resp = interface.sendCommand(cmd); | 
| Jeremy Kerr | 147086d | 2024-08-27 13:46:38 +0800 | [diff] [blame] | 218 | if (!resp) | 
|  | 219 | { | 
|  | 220 | return EXIT_FAILURE; | 
|  | 221 | } | 
|  | 222 | lg2::debug("Response {DATA_LEN} bytes: {DATA}", "DATA_LEN", | 
| Jeremy Kerr | b788524 | 2024-09-16 12:43:36 +0800 | [diff] [blame] | 223 | resp->full_payload.size(), "DATA", | 
|  | 224 | toHexStr(resp->full_payload)); | 
| Eddie James | fa1f5c0 | 2020-09-17 15:12:46 -0500 | [diff] [blame] | 225 | } | 
|  | 226 | else if ((options)["set"] == "true") | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 227 | { | 
| Gunnar Mills | 6af6144 | 2018-04-08 14:50:06 -0500 | [diff] [blame] | 228 | // Can not perform set operation without package. | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 229 | if (packageInt == DEFAULT_VALUE) | 
|  | 230 | { | 
|  | 231 | exitWithError("Package not specified.", argv); | 
|  | 232 | } | 
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 233 | return interface.setChannel(packageInt, channelInt); | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 234 | } | 
|  | 235 | else if ((options)["info"] == "true") | 
|  | 236 | { | 
| Jeremy Kerr | 7f7c085 | 2024-08-08 11:32:55 +0800 | [diff] [blame] | 237 | auto info = interface.getInfo(packageInt); | 
|  | 238 | if (!info) | 
|  | 239 | { | 
|  | 240 | return EXIT_FAILURE; | 
|  | 241 | } | 
|  | 242 | printInfo(*info); | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 243 | } | 
|  | 244 | else if ((options)["clear"] == "true") | 
|  | 245 | { | 
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 246 | return interface.clearInterface(); | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 247 | } | 
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 248 | else if (!(options)["pmask"].empty()) | 
|  | 249 | { | 
|  | 250 | unsigned int mask{}; | 
|  | 251 | try | 
|  | 252 | { | 
|  | 253 | size_t lastChar{}; | 
|  | 254 | mask = std::stoul((options)["pmask"], &lastChar, 0); | 
|  | 255 | if (lastChar < (options["pmask"].size())) | 
|  | 256 | { | 
|  | 257 | exitWithError("Package mask value is not valid", argv); | 
|  | 258 | } | 
|  | 259 | } | 
|  | 260 | catch (const std::exception& e) | 
|  | 261 | { | 
|  | 262 | exitWithError("Package mask value is not valid", argv); | 
|  | 263 | } | 
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 264 | return interface.setPackageMask(mask); | 
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 265 | } | 
|  | 266 | else if (!(options)["cmask"].empty()) | 
|  | 267 | { | 
|  | 268 | if (packageInt == DEFAULT_VALUE) | 
|  | 269 | { | 
|  | 270 | exitWithError("Package is not specified", argv); | 
|  | 271 | } | 
|  | 272 | unsigned int mask{}; | 
|  | 273 | try | 
|  | 274 | { | 
|  | 275 | size_t lastChar{}; | 
|  | 276 | mask = stoul((options)["cmask"], &lastChar, 0); | 
|  | 277 | if (lastChar < (options["cmask"].size())) | 
|  | 278 | { | 
|  | 279 | exitWithError("Channel mask value is not valid", argv); | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 | catch (const std::exception& e) | 
|  | 283 | { | 
|  | 284 | exitWithError("Channel mask value is not valid", argv); | 
|  | 285 | } | 
| Jeremy Kerr | bc22f81 | 2024-07-29 17:43:35 +0800 | [diff] [blame] | 286 | return interface.setChannelMask(packageInt, mask); | 
| Johnathan Mantey | 5a45606 | 2024-02-15 08:45:08 -0800 | [diff] [blame] | 287 | } | 
| Ratan Gupta | ed5d7ff | 2018-03-23 00:27:52 +0530 | [diff] [blame] | 288 | else | 
|  | 289 | { | 
|  | 290 | exitWithError("No Command specified", argv); | 
|  | 291 | } | 
| Ratan Gupta | b38401b | 2018-03-16 12:44:26 +0530 | [diff] [blame] | 292 | return 0; | 
|  | 293 | } |