blob: 90393a5210e43bc19cacc6f2f668c17e64a1fb97 [file] [log] [blame]
Alexander Hansen306542d2025-10-30 17:50:11 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2018 IBM Corporation
3
Ratan Guptaed5d7ff2018-03-23 00:27:52 +05304#include "argument.hpp"
5#include "ncsi_util.hpp"
6
Jeremy Kerrbad17c02024-11-21 16:44:39 +08007#include <string.h>
8#include <unistd.h>
9
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070010#include <phosphor-logging/lg2.hpp>
Jeremy Kerr147086d2024-08-27 13:46:38 +080011#include <stdplus/numeric/str.hpp>
12#include <stdplus/str/buf.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050013
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053014#include <string>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050015#include <vector>
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053016
17static void exitWithError(const char* err, char** argv)
18{
19 phosphor::network::ncsi::ArgumentParser::usage(argv);
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070020 lg2::error("ERROR: {ERROR}", "ERROR", err);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053021 exit(EXIT_FAILURE);
22}
23
Jeremy Kerr7f7c0852024-08-08 11:32:55 +080024static 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 Guptab38401b2018-03-16 12:44:26 +053068int main(int argc, char** argv)
69{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053070 using namespace phosphor::network;
71 using namespace phosphor::network::ncsi;
72 // Read arguments.
73 auto options = ArgumentParser(argc, argv);
Gunnar Mills57d9c502018-09-14 14:42:34 -050074 int packageInt{};
75 int channelInt{};
76 int indexInt{};
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053077
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 Mills57d9c502018-09-14 14:42:34 -050091 exitWithError("Interface value should be greater than equal to 0",
92 argv);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053093 }
94
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +080095 NetlinkInterface interface(indexInt);
Jeremy Kerr8d9af022024-07-26 16:47:16 +080096
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053097 // 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 Mills57d9c502018-09-14 14:42:34 -0500105 packageInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530106 }
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 Mills57d9c502018-09-14 14:42:34 -0500121 channelInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530122 }
123
124 if (channelInt < 0)
125 {
126 channelInt = DEFAULT_VALUE;
127 }
128
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500129 auto payloadStr = (options)["oem-payload"];
130 if (!payloadStr.empty())
131 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800132 if (payloadStr.size() % 2 || payloadStr.size() < 2)
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500133 exitWithError("Payload invalid: specify two hex digits per byte.",
134 argv);
135
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800136 // 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 Jamesfa1f5c02020-09-17 15:12:46 -0500142
143 if (packageInt == DEFAULT_VALUE)
144 {
145 exitWithError("Package not specified.", argv);
146 }
147
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800148 std::vector<std::string> args = {
149 "ncsi-cmd",
150 "-i",
151 std::to_string(indexInt),
152 "-p",
153 std::to_string(packageInt),
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800154 };
155
156 if (channelInt != DEFAULT_VALUE)
Jeremy Kerr147086d2024-08-27 13:46:38 +0800157 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800158 args.push_back("-c");
159 args.push_back(std::to_string(channelInt));
Jeremy Kerr147086d2024-08-27 13:46:38 +0800160 }
161
Jeremy Kerr0963c4b2025-03-06 14:51:37 +0800162 args.push_back("raw");
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800163 args.push_back(typeStr);
164 args.push_back(dataStr);
Jeremy Kerrb7885242024-09-16 12:43:36 +0800165
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800166 /* 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 Kerr147086d2024-08-27 13:46:38 +0800171 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800172 argv[i] = strdup(args[i].c_str());
Jeremy Kerr147086d2024-08-27 13:46:38 +0800173 }
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800174 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 Jamesfa1f5c02020-09-17 15:12:46 -0500186 }
187 else if ((options)["set"] == "true")
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530188 {
Gunnar Mills6af61442018-04-08 14:50:06 -0500189 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530190 if (packageInt == DEFAULT_VALUE)
191 {
192 exitWithError("Package not specified.", argv);
193 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800194 return interface.setChannel(packageInt, channelInt);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530195 }
196 else if ((options)["info"] == "true")
197 {
Jeremy Kerr7f7c0852024-08-08 11:32:55 +0800198 auto info = interface.getInfo(packageInt);
199 if (!info)
200 {
201 return EXIT_FAILURE;
202 }
203 printInfo(*info);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530204 }
205 else if ((options)["clear"] == "true")
206 {
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800207 return interface.clearInterface();
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530208 }
Johnathan Mantey5a456062024-02-15 08:45:08 -0800209 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 Kerrbc22f812024-07-29 17:43:35 +0800225 return interface.setPackageMask(mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800226 }
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 Kerrbc22f812024-07-29 17:43:35 +0800247 return interface.setChannelMask(packageInt, mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800248 }
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530249 else
250 {
251 exitWithError("No Command specified", argv);
252 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530253 return 0;
254}