blob: 09010fb067a6fb26afb36d177da211594326b845 [file] [log] [blame]
Ratan Guptab38401b2018-03-16 12:44:26 +05301/**
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 Guptaed5d7ff2018-03-23 00:27:52 +053016#include "argument.hpp"
17#include "ncsi_util.hpp"
18
Jeremy Kerrbad17c02024-11-21 16:44:39 +080019#include <string.h>
20#include <unistd.h>
21
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070022#include <phosphor-logging/lg2.hpp>
Jeremy Kerr147086d2024-08-27 13:46:38 +080023#include <stdplus/numeric/str.hpp>
24#include <stdplus/str/buf.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -050025
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053026#include <string>
Eddie Jamesfa1f5c02020-09-17 15:12:46 -050027#include <vector>
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053028
29static void exitWithError(const char* err, char** argv)
30{
31 phosphor::network::ncsi::ArgumentParser::usage(argv);
Jagpal Singh Gilld423beb2023-04-18 11:28:03 -070032 lg2::error("ERROR: {ERROR}", "ERROR", err);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053033 exit(EXIT_FAILURE);
34}
35
Jeremy Kerr7f7c0852024-08-08 11:32:55 +080036static void printInfo(phosphor::network::ncsi::InterfaceInfo& info)
37{
38 using namespace phosphor::network::ncsi;
39
40 for (PackageInfo& pkg : info.packages)
41 {
42 lg2::debug("Package id : {ID}", "ID", pkg.id);
43 if (pkg.forced)
44 {
45 lg2::debug(" package is forced");
46 }
47 for (ChannelInfo& chan : pkg.channels)
48 {
49 lg2::debug(" Channel id : {ID}", "ID", chan.id);
50 if (chan.forced)
51 {
52 lg2::debug(" channel is forced");
53 }
54 if (chan.active)
55 {
56 lg2::debug(" channel is active");
57 }
58
59 lg2::debug(" version {MAJOR}.{MINOR} ({STR})", "MAJOR",
60 chan.version_major, "MINOR", chan.version_minor, "STR",
61 chan.version);
62
63 lg2::debug(" link state {LINK}", "LINK", lg2::hex,
64 chan.link_state);
65
66 auto& vlans = chan.vlan_ids;
67
68 if (!vlans.empty())
69 {
70 lg2::debug(" Actve VLAN IDs:");
71 for (uint16_t vlan : vlans)
72 {
73 lg2::debug(" VID: {VLAN_ID}", "VLAN_ID", vlan);
74 }
75 }
76 }
77 }
78}
79
Ratan Guptab38401b2018-03-16 12:44:26 +053080int main(int argc, char** argv)
81{
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053082 using namespace phosphor::network;
83 using namespace phosphor::network::ncsi;
84 // Read arguments.
85 auto options = ArgumentParser(argc, argv);
Gunnar Mills57d9c502018-09-14 14:42:34 -050086 int packageInt{};
87 int channelInt{};
88 int indexInt{};
Ratan Guptaed5d7ff2018-03-23 00:27:52 +053089
90 // Parse out interface argument.
91 auto ifIndex = (options)["index"];
92 try
93 {
94 indexInt = stoi(ifIndex, nullptr);
95 }
96 catch (const std::exception& e)
97 {
98 exitWithError("Interface not specified.", argv);
99 }
100
101 if (indexInt < 0)
102 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103 exitWithError("Interface value should be greater than equal to 0",
104 argv);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530105 }
106
Jeremy Kerr2d0b48d2024-09-16 13:03:26 +0800107 NetlinkInterface interface(indexInt);
Jeremy Kerr8d9af022024-07-26 16:47:16 +0800108
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530109 // Parse out package argument.
110 auto package = (options)["package"];
111 try
112 {
113 packageInt = stoi(package, nullptr);
114 }
115 catch (const std::exception& e)
116 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500117 packageInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530118 }
119
120 if (packageInt < 0)
121 {
122 packageInt = DEFAULT_VALUE;
123 }
124
125 // Parse out channel argument.
126 auto channel = (options)["channel"];
127 try
128 {
129 channelInt = stoi(channel, nullptr);
130 }
131 catch (const std::exception& e)
132 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500133 channelInt = DEFAULT_VALUE;
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530134 }
135
136 if (channelInt < 0)
137 {
138 channelInt = DEFAULT_VALUE;
139 }
140
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500141 auto payloadStr = (options)["oem-payload"];
142 if (!payloadStr.empty())
143 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800144 if (payloadStr.size() % 2 || payloadStr.size() < 2)
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500145 exitWithError("Payload invalid: specify two hex digits per byte.",
146 argv);
147
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800148 // Payload string is in the format <type>[<payload>]
149 // (e.g. "50000001572100"), where the first two characters (i.e. "50")
150 // represent the command type, and the rest the payload. Split this
151 // up for the ncsi-cmd operation, which has these as separate arguments.
152 std::string typeStr(payloadStr.substr(0, 2));
153 std::string dataStr(payloadStr.substr(2));
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500154
155 if (packageInt == DEFAULT_VALUE)
156 {
157 exitWithError("Package not specified.", argv);
158 }
159
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800160 std::vector<std::string> args = {
161 "ncsi-cmd",
162 "-i",
163 std::to_string(indexInt),
164 "-p",
165 std::to_string(packageInt),
166 "raw",
167 };
168
169 if (channelInt != DEFAULT_VALUE)
Jeremy Kerr147086d2024-08-27 13:46:38 +0800170 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800171 args.push_back("-c");
172 args.push_back(std::to_string(channelInt));
Jeremy Kerr147086d2024-08-27 13:46:38 +0800173 }
174
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800175 args.push_back(typeStr);
176 args.push_back(dataStr);
Jeremy Kerrb7885242024-09-16 12:43:36 +0800177
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800178 /* Convert to C argv array. execvp()'s argv argument is not const,
179 * whereas .c_str() is, so we need to strdup here.
180 */
181 char** argv = new char*[args.size() + 1]();
182 for (size_t i = 0; i < args.size(); i++)
Jeremy Kerr147086d2024-08-27 13:46:38 +0800183 {
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800184 argv[i] = strdup(args[i].c_str());
Jeremy Kerr147086d2024-08-27 13:46:38 +0800185 }
Jeremy Kerrbad17c02024-11-21 16:44:39 +0800186 argv[args.size()] = NULL;
187
188 lg2::debug("ncsi-netlink [..] -o is deprecated by ncsi-cmd");
189 execvp(argv[0], argv);
190 lg2::error("exec failed; use ncsi-cmd directly");
191
192 for (size_t i = 0; i < args.size(); i++)
193 {
194 free(argv[i]);
195 }
196 delete[] argv;
197 return EXIT_FAILURE;
Eddie Jamesfa1f5c02020-09-17 15:12:46 -0500198 }
199 else if ((options)["set"] == "true")
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530200 {
Gunnar Mills6af61442018-04-08 14:50:06 -0500201 // Can not perform set operation without package.
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530202 if (packageInt == DEFAULT_VALUE)
203 {
204 exitWithError("Package not specified.", argv);
205 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800206 return interface.setChannel(packageInt, channelInt);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530207 }
208 else if ((options)["info"] == "true")
209 {
Jeremy Kerr7f7c0852024-08-08 11:32:55 +0800210 auto info = interface.getInfo(packageInt);
211 if (!info)
212 {
213 return EXIT_FAILURE;
214 }
215 printInfo(*info);
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530216 }
217 else if ((options)["clear"] == "true")
218 {
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800219 return interface.clearInterface();
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530220 }
Johnathan Mantey5a456062024-02-15 08:45:08 -0800221 else if (!(options)["pmask"].empty())
222 {
223 unsigned int mask{};
224 try
225 {
226 size_t lastChar{};
227 mask = std::stoul((options)["pmask"], &lastChar, 0);
228 if (lastChar < (options["pmask"].size()))
229 {
230 exitWithError("Package mask value is not valid", argv);
231 }
232 }
233 catch (const std::exception& e)
234 {
235 exitWithError("Package mask value is not valid", argv);
236 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800237 return interface.setPackageMask(mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800238 }
239 else if (!(options)["cmask"].empty())
240 {
241 if (packageInt == DEFAULT_VALUE)
242 {
243 exitWithError("Package is not specified", argv);
244 }
245 unsigned int mask{};
246 try
247 {
248 size_t lastChar{};
249 mask = stoul((options)["cmask"], &lastChar, 0);
250 if (lastChar < (options["cmask"].size()))
251 {
252 exitWithError("Channel mask value is not valid", argv);
253 }
254 }
255 catch (const std::exception& e)
256 {
257 exitWithError("Channel mask value is not valid", argv);
258 }
Jeremy Kerrbc22f812024-07-29 17:43:35 +0800259 return interface.setChannelMask(packageInt, mask);
Johnathan Mantey5a456062024-02-15 08:45:08 -0800260 }
Ratan Guptaed5d7ff2018-03-23 00:27:52 +0530261 else
262 {
263 exitWithError("No Command specified", argv);
264 }
Ratan Guptab38401b2018-03-16 12:44:26 +0530265 return 0;
266}