xref: /openbmc/phosphor-networkd/src/ncsi_netlink_main.cpp (revision 2d0b48dac602196e1f4d2f3d47830f6b88b5186f)
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  */
16 #include "argument.hpp"
17 #include "ncsi_util.hpp"
18 
19 #include <phosphor-logging/lg2.hpp>
20 #include <stdplus/numeric/str.hpp>
21 #include <stdplus/str/buf.hpp>
22 
23 #include <string>
24 #include <vector>
25 
exitWithError(const char * err,char ** argv)26 static void exitWithError(const char* err, char** argv)
27 {
28     phosphor::network::ncsi::ArgumentParser::usage(argv);
29     lg2::error("ERROR: {ERROR}", "ERROR", err);
30     exit(EXIT_FAILURE);
31 }
32 
printInfo(phosphor::network::ncsi::InterfaceInfo & info)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 
toHexStr(std::span<const uint8_t> c)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 
main(int argc,char ** argv)97 int main(int argc, char** argv)
98 {
99     using namespace phosphor::network;
100     using namespace phosphor::network::ncsi;
101     // Read arguments.
102     auto options = ArgumentParser(argc, argv);
103     int packageInt{};
104     int channelInt{};
105     int indexInt{};
106     int operationInt{DEFAULT_VALUE};
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     {
121         exitWithError("Interface value should be greater than equal to 0",
122                       argv);
123     }
124 
125     NetlinkInterface interface(indexInt);
126 
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     {
135         packageInt = DEFAULT_VALUE;
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     {
151         channelInt = DEFAULT_VALUE;
152     }
153 
154     if (channelInt < 0)
155     {
156         channelInt = DEFAULT_VALUE;
157     }
158 
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 
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{};
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             {
181                 sendCmdSelect = stoi(byte, nullptr, 16);
182             }
183             catch (const std::exception& e)
184             {
185                 exitWithError("Payload invalid.", argv);
186             }
187             if (i == 1)
188             {
189                 operationInt = sendCmdSelect;
190             }
191             else
192             {
193                 payload.push_back(sendCmdSelect);
194             }
195         }
196 
197         if (operationInt == DEFAULT_VALUE)
198         {
199             exitWithError("No payload specified.", argv);
200         }
201 
202         if (packageInt == DEFAULT_VALUE)
203         {
204             exitWithError("Package not specified.", argv);
205         }
206 
207         if (!payload.empty())
208         {
209             lg2::debug("Payload: {PAYLOAD}", "PAYLOAD", toHexStr(payload));
210         }
211 
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);
218         if (!resp)
219         {
220             return EXIT_FAILURE;
221         }
222         lg2::debug("Response {DATA_LEN} bytes: {DATA}", "DATA_LEN",
223                    resp->full_payload.size(), "DATA",
224                    toHexStr(resp->full_payload));
225     }
226     else if ((options)["set"] == "true")
227     {
228         // Can not perform set operation without package.
229         if (packageInt == DEFAULT_VALUE)
230         {
231             exitWithError("Package not specified.", argv);
232         }
233         return interface.setChannel(packageInt, channelInt);
234     }
235     else if ((options)["info"] == "true")
236     {
237         auto info = interface.getInfo(packageInt);
238         if (!info)
239         {
240             return EXIT_FAILURE;
241         }
242         printInfo(*info);
243     }
244     else if ((options)["clear"] == "true")
245     {
246         return interface.clearInterface();
247     }
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         }
264         return interface.setPackageMask(mask);
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         }
286         return interface.setChannelMask(packageInt, mask);
287     }
288     else
289     {
290         exitWithError("No Command specified", argv);
291     }
292     return 0;
293 }
294