blob: 01b544490a7df1e36ccc1f0daf4f52c8108eda82 [file] [log] [blame]
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -06001/******************************************************************************
2
3 R M N E T C L I . C
4
5Copyright (c) 2013, The Linux Foundation. All rights reserved.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32******************************************************************************/
33
34/******************************************************************************
35
36 @file rmnetcli.c
37 @brief command line interface to expose rmnet control API's
38
39 DESCRIPTION
40 File containing implementation of the command line interface to expose the
41 rmnet control configuration .
42
43******************************************************************************/
44
45/*===========================================================================
46 INCLUDE FILES
47===========================================================================*/
48
49#include <sys/socket.h>
50#include <stdint.h>
51#include <linux/netlink.h>
52#include <string.h>
53#include <stdio.h>
54#include <unistd.h>
55#include <stdlib.h>
56#include "rmnetcli.h"
57#include "librmnetctl.h"
58
59#define RMNET_MAX_STR_LEN 16
60
61#define _RMNETCLI_CHECKNULL(X) do { if (!X) { \
62print_rmnet_api_status(RMNETCTL_INVALID_ARG, RMNETCTL_CFG_FAILURE_NO_COMMAND); \
63 rmnetctl_cleanup(handle); \
64 return RMNETCTL_INVALID_ARG; \
65 } } while (0);
66#define _STRTOUI32(X) (uint32_t)strtoul(X, NULL, 0)
67#define _STRTOUI16(X) (uint16_t)strtoul(X, NULL, 0)
68#define _STRTOUI8(X) (uint8_t)strtoul(X, NULL, 0)
69#define _STRTOI32(X) (int32_t)strtol(X, NULL, 0)
70
71#define _5TABS "\n\t\t\t\t\t"
72#define _2TABS "\n\t\t"
73
74/*!
75* @brief Contains a list of error message from CLI
76*/
77char rmnetcfg_error_code_text
78[RMNETCFG_TOTAL_ERR_MSGS][RMNETCTL_ERR_MSG_SIZE] = {
79 "Help option Specified",
80 "ERROR: No\\Invalid command was specified\n",
81 "ERROR: Could not allocate buffer for Egress device\n"
82};
83
84/*!
85* @brief Method to display the syntax for the commands
86* @details Displays the syntax and usage for the commands
87* @param void
88* @return void
89*/
Harout Hedeshian04d69732013-10-02 09:29:11 -060090static void rmnet_api_usage(void)
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -060091{
92 printf("RmNet API Usage:\n\n");
93 printf("rmnetcli help Displays this help\n");
94 printf("\n");
95 printf("rmnetcli assocnetdev <dev_name> Registers the RmNet");
96 printf(_5TABS" data driver on a particular");
97 printf(_5TABS" device.dev_name cannot");
98 printf(_5TABS" be larger than 15");
99 printf(_5TABS" characters. Returns");
100 printf(_5TABS" the status code.\n\n");
101 printf("rmnetcli unassocnetdev <dev_name> Unregisters the");
102 printf(_5TABS" RmNet data driver on a particular");
103 printf(_5TABS" device. dev_name cannot");
104 printf(_5TABS" be larger than 15");
105 printf(_5TABS" characters. Returns");
106 printf(_5TABS" the status code.\n\n");
107 printf("rmnetcli getnetdevassoc <dev_name> Get if the RmNet");
108 printf(_5TABS" data driver is registered on");
109 printf(_5TABS" a particular device.");
110 printf(_5TABS" dev_name cannot be");
111 printf(_5TABS" larger than 15");
112 printf(_5TABS" characters. Returns 1");
113 printf(_5TABS" if is registered and");
114 printf(_5TABS" 0 if it is not");
115 printf(_5TABS" registered\n\n");
116 printf("rmnetcli setledf <egress_flags> Sets the egress data");
117 printf(_2TABS" <agg_size> format for a particular link.");
118 printf(_2TABS" <agg_count> dev_name cannot be larger");
119 printf(_2TABS" <dev_name> than 15 characters.");
120 printf(_5TABS" Returns the status code\n\n");
121 printf("rmnetcli getledf <dev_name> Gets the egress data");
122 printf(_5TABS" format for a particular link.");
123 printf(_5TABS" dev_name cannot be larger");
124 printf(_5TABS" than 15. Returns the 4");
125 printf(_5TABS" byte unsigned integer");
126 printf(_5TABS" egress_flags\n\n");
127 printf("rmnetcli setlidf <ingress_flags> Sets the ingress");
128 printf(_2TABS" <dev_name> data format for a particular");
129 printf(_5TABS" link. egress_flags is 4");
130 printf(_5TABS" byte unsigned integer.");
131 printf(_5TABS" dev_name cannot be");
132 printf(_5TABS" larger than 15.");
133 printf(_5TABS" characters. Returns");
134 printf(_5TABS" the status code\n\n");
135 printf("rmnetcli getlidf <dev_name> Gets the ingress");
136 printf(_5TABS" data format for a particular");
137 printf(_5TABS" link. dev_name cannot be");
138 printf(_5TABS" larger than 15. Returns");
139 printf(_5TABS" the 4 byte unsigned");
140 printf(_5TABS" integer ingress_flags\n\n");
141 printf("rmnetcli setlepc <logical_ep_id> Sets the logical");
142 printf(_2TABS" <rmnet_mode> endpoint configuration for");
143 printf(_2TABS" <dev_name> a particular link.");
144 printf(_2TABS" <egress_dev_name> logical_ep_id are 32bit");
145 printf(_5TABS" integers from -1 to 31.");
146 printf(_5TABS" rmnet_mode is a 1 byte");
147 printf(_5TABS" unsigned integer of");
148 printf(_5TABS" value none, vnd or");
149 printf(_5TABS" bridged. dev_name");
150 printf(_5TABS" and egress_dev_name");
151 printf(_5TABS" cannot be larger");
152 printf(_5TABS" than 15 tcharacters");
153 printf(_5TABS" Returns the status code\n\n");
154 printf("rmnetcli getlepc <logical_ep_id> Sets the logical");
155 printf(_2TABS" <dev_name> enpoint configuration for a");
156 printf(_5TABS" particular link.");
157 printf(_5TABS" logical_ep_id are 32bit");
158 printf(_5TABS" integers from -1 to 31.");
159 printf(_5TABS" Returns the rmnet_mode");
160 printf(_5TABS" and egress_dev_name.");
161 printf(_5TABS" rmnet_mode is a 1");
162 printf(_5TABS" byte unsigned integer");
163 printf(_5TABS" of value none, vnd or");
164 printf(_5TABS" bridged. dev_name and");
165 printf(_5TABS" egress_dev_name cannot be");
166 printf(_5TABS" larger than 15 ");
167 printf(_5TABS" characters. Returns the");
168 printf(_5TABS" status code\n\n");
Harout Hedeshian89a91e22013-10-09 08:59:47 -0600169 printf("rmnetcli newvnd <dev_id> Creates a new");
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600170 printf(_5TABS" virtual network device node.");
Harout Hedeshian89a91e22013-10-09 08:59:47 -0600171 printf(_5TABS" dev_id is an int");
172 printf(_5TABS" less than 32. Returns");
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600173 printf(_5TABS" the status code\n\n");
Harout Hedeshian89a91e22013-10-09 08:59:47 -0600174 printf("rmnetcli newvndprefix <dev_id> <name_prefix> Creates");
175 printf(_5TABS" virtual network device node.");
176 printf(_5TABS" dev_id is an int");
177 printf(_5TABS" less than 32. Prefix");
178 printf(_5TABS" must be less than");
179 printf(_5TABS" 15 chars. Returns");
180 printf(_5TABS" the status code\n\n");
181 printf("rmnetcli getvndname <dev_id> Get name of");
182 printf(_5TABS" network device node from id");
183 printf("rmnetcli freevnd <dev_id> Removes virtual");
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600184 printf(_5TABS" network device node. dev_name");
185 printf(_5TABS" cannot be larger than 15.");
186 printf(_5TABS" Returns the status code\n\n");
Harout Hedeshian97a1e982013-11-08 09:28:53 -0700187 printf("rmnetcli addvnctcflow <dev_id> Add a modem flow");
188 printf(_2TABS" <mdm_flow_hndl> handle - tc flow handle");
189 printf(_2TABS" <tc_flow_hndl> mapping for a virtual network");
190 printf(_2TABS" device node\n\n");
191 printf("rmnetcli delvnctcflow <dev_id> Delete a modem flow");
192 printf(_2TABS" <mdm_flow_hndl> handle - tc flow handle");
193 printf(_2TABS" <tc_flow_hndl> mapping for a virtual network");
194 printf(_2TABS" device node\n\n");
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600195}
196
197static void print_rmnetctl_lib_errors(uint16_t error_number) {
198 if ((error_number > RMNETCTL_API_SUCCESS) &&
199 (error_number < RMNETCTL_API_ERR_ENUM_LENGTH)) {
200 printf("%s", rmnetctl_error_code_text[error_number]);
201 }
202 if ((error_number >= RMNETCFG_ERR_NUM_START) &&
203 (error_number < RMNETCFG_ERR_NUM_START + RMNETCFG_TOTAL_ERR_MSGS)) {
204 printf("%s", rmnetcfg_error_code_text
205 [error_number - RMNETCFG_ERR_NUM_START]);
206 if ((error_number == RMNETCTL_CFG_SUCCESS_HELP_COMMAND) ||
207 (error_number == RMNETCTL_CFG_FAILURE_NO_COMMAND))
208 rmnet_api_usage();
209 }
210}
211
212/*!
213* @brief Method to check the error numbers generated from API calls
214* @details Displays the error messages based on each error code
215* @param error_number Error number returned from the API and the CLI
216* @return void
217*/
218static void print_rmnet_api_status(int return_code, uint16_t error_number)
219{
220 if (return_code == RMNETCTL_SUCCESS)
221 printf("SUCCESS\n");
222 else if (return_code == RMNETCTL_LIB_ERR)
223 printf("LIBRARY ");
224 else if (return_code == RMNETCTL_KERNEL_ERR)
225 printf("KERNEL : Error code %u\n", error_number);
226 else if (return_code == RMNETCTL_INVALID_ARG)
227 printf("INVALID_ARG\n");
228
229 if (return_code == RMNETCTL_LIB_ERR) {
230 print_rmnetctl_lib_errors(error_number);
231 }
232}
233
234/*!
235* @brief Method to make the API calls
236* @details Checks for each type of parameter and calls the appropriate
237* function based on the number of parameters and paramter type
238* @param argc Number of arguments which vary based on the commands
239* @param argv Value of the arguments which vary based on the commands
240* @return RMNETCTL_SUCCESS if successful. Relevant data might be printed
241* based on the message type
242* @return RMNETCTL_LIB_ERR if there was a library error. Error code will be
243* printed
244* @return RMNETCTL_KERNEL_ERR if there was a error in the kernel. Error code will be
245* printed
246* @return RMNETCTL_INVALID_ARG if invalid arguments were passed to the API
247*/
248
249static int rmnet_api_call(int argc, char *argv[])
250{
251 struct rmnetctl_hndl_s *handle = NULL;
252 uint16_t error_number = RMNETCTL_CFG_FAILURE_NO_COMMAND;
253 int return_code = RMNETCTL_LIB_ERR;
254 if ((!argc) || (!*argv)) {
255 print_rmnet_api_status(RMNETCTL_LIB_ERR,
256 RMNETCTL_CFG_FAILURE_NO_COMMAND);
257 return RMNETCTL_LIB_ERR;
258 }
259 if (!strcmp(*argv, "help")) {
260 print_rmnet_api_status(RMNETCTL_LIB_ERR,
261 RMNETCTL_CFG_SUCCESS_HELP_COMMAND);
262 return RMNETCTL_LIB_ERR;
263 }
264 return_code = rmnetctl_init(&handle, &error_number);
265 if (return_code!= RMNETCTL_SUCCESS) {
266 print_rmnet_api_status(return_code, error_number);
267 return RMNETCTL_LIB_ERR;
268 }
269 error_number = RMNETCTL_CFG_FAILURE_NO_COMMAND;
270 return_code = RMNETCTL_LIB_ERR;
271 if (!strcmp(*argv, "assocnetdev")) {
272 return_code = rmnet_associate_network_device(handle,
273 argv[1], &error_number, RMNETCTL_DEVICE_ASSOCIATE);
274 } else if (!strcmp(*argv, "unassocnetdev")) {
275 return_code = rmnet_associate_network_device(handle,
276 argv[1], &error_number, RMNETCTL_DEVICE_UNASSOCIATE);
277 } else if (!strcmp(*argv, "getnetdevassoc")) {
278 int register_status;
279 return_code = rmnet_get_network_device_associated(handle,
280 argv[1], &register_status, &error_number);
281 if (return_code == RMNETCTL_SUCCESS)
282 printf("register_status is %d\n", register_status);
283 } else if (!strcmp(*argv, "getledf")) {
284 uint32_t egress_flags;
285 uint16_t agg_size, agg_count;
286 return_code = rmnet_get_link_egress_data_format(handle,
287 argv[1], &egress_flags, &agg_size, &agg_count, &error_number);
288 if (return_code == RMNETCTL_SUCCESS) {
289 printf("egress_flags is %u\n", egress_flags);
290 printf("agg_size is %u\n", agg_size);
291 printf("agg_count is %u\n", agg_count);
292 }
293 } else if (!strcmp(*argv, "getlidf")) {
294 uint32_t ingress_flags;
295 return_code = rmnet_get_link_ingress_data_format(handle,
296 argv[1], &ingress_flags, &error_number);
297 if (return_code == RMNETCTL_SUCCESS) {
298 printf("ingress_flags is %u\n", ingress_flags);
299 }
Harout Hedeshian89a91e22013-10-09 08:59:47 -0600300 } else if (!strcmp(*argv, "newvndprefix")) {
301 _RMNETCLI_CHECKNULL(argv[1]);
302 _RMNETCLI_CHECKNULL(argv[2]);
303 return_code = rmnet_new_vnd_prefix(handle,
304 _STRTOUI32(argv[1]), &error_number, RMNETCTL_NEW_VND, argv[2]);
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600305 } else if (!strcmp(*argv, "newvnd")) {
306 _RMNETCLI_CHECKNULL(argv[1]);
307 return_code = rmnet_new_vnd(handle,
308 _STRTOUI32(argv[1]), &error_number, RMNETCTL_NEW_VND);
Harout Hedeshian89a91e22013-10-09 08:59:47 -0600309 } else if (!strcmp(*argv, "getvndname")) {
310 char buffer[32];
311 memset(buffer, 0, 32);
312 _RMNETCLI_CHECKNULL(argv[1]);
313 return_code = rmnet_get_vnd_name(handle, _STRTOUI32(argv[1]),
314 &error_number, buffer, 32);
315 if (return_code == RMNETCTL_SUCCESS) {
316 printf("VND name: %s\n", buffer);
317 }
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600318 } else if (!strcmp(*argv, "freevnd")) {
319 _RMNETCLI_CHECKNULL(argv[1]);
320 return_code = rmnet_new_vnd(handle,
321 _STRTOUI32(argv[1]), &error_number, RMNETCTL_FREE_VND);
322 } else if (!strcmp(*argv, "setlidf")) {
323 _RMNETCLI_CHECKNULL(argv[1]);
324 return_code = rmnet_set_link_ingress_data_format(handle,
325 _STRTOUI32(argv[1]), argv[2], &error_number);
Harout Hedeshian97a1e982013-11-08 09:28:53 -0700326 } else if (!strcmp(*argv, "delvnctcflow")) {
327 _RMNETCLI_CHECKNULL(argv[1]);
328 _RMNETCLI_CHECKNULL(argv[2]);
329 _RMNETCLI_CHECKNULL(argv[3]);
330 return_code = rmnet_add_del_vnd_tc_flow(handle,
331 _STRTOUI32(argv[1]), _STRTOUI32(argv[2]), _STRTOUI32(argv[3]),
332 RMNETCTL_DEL_FLOW, &error_number);
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600333 } else if (!strcmp(*argv, "getlepc")) {
334 _RMNETCLI_CHECKNULL(argv[1]);
335 uint8_t rmnet_mode;
336 char *egress_dev_name;
337 egress_dev_name = NULL;
338 egress_dev_name = (char *)malloc(RMNET_MAX_STR_LEN
339 * sizeof(char));
340 if (!egress_dev_name) {
341 print_rmnet_api_status(RMNETCTL_LIB_ERR,
342 RMNETCTL_CFG_FAILURE_EGRESS_DEV_NAME_NULL);
343 return RMNETCTL_LIB_ERR;
344 }
345 return_code = rmnet_get_logical_ep_config(handle,
346 _STRTOI32(argv[1]), argv[2], &rmnet_mode,
347 &egress_dev_name, &error_number);
348 if (return_code == RMNETCTL_SUCCESS) {
349 printf("rmnet_mode is %u\n", rmnet_mode);
350 printf("egress_dev_name is %s\n", egress_dev_name);
351 }
Harout Hedeshian97a1e982013-11-08 09:28:53 -0700352 } else if (!strcmp(*argv, "addvnctcflow")) {
353 _RMNETCLI_CHECKNULL(argv[1]);
354 _RMNETCLI_CHECKNULL(argv[2]);
355 _RMNETCLI_CHECKNULL(argv[3]);
356 return_code = rmnet_add_del_vnd_tc_flow(handle,
357 _STRTOUI32(argv[1]), _STRTOUI32(argv[2]), _STRTOUI32(argv[3]),
358 RMNETCTL_ADD_FLOW, &error_number);
Harout Hedeshianb2fc5b12013-09-03 13:49:00 -0600359 } else if (!strcmp(*argv, "setledf")) {
360 _RMNETCLI_CHECKNULL(argv[1]);
361 _RMNETCLI_CHECKNULL(argv[2]);
362 _RMNETCLI_CHECKNULL(argv[3]);
363 return_code = rmnet_set_link_egress_data_format(handle,
364 _STRTOUI32(argv[1]), _STRTOUI16(argv[2]), _STRTOUI16(argv[3]),
365 argv[4], &error_number);
366 } else if (!strcmp(*argv, "setlepc")) {
367 _RMNETCLI_CHECKNULL(argv[1]);
368 _RMNETCLI_CHECKNULL(argv[2]);
369 return_code = rmnet_set_logical_ep_config(handle,
370 _STRTOI32(argv[1]), _STRTOUI8(argv[2]), argv[3], argv[4],
371 &error_number);
372 }
373 print_rmnet_api_status(return_code, error_number);
374 rmnetctl_cleanup(handle);
375 return return_code;
376}
377
378/*!
379* @brief Method which serves as en entry point to the rmnetcli function
380* @details Entry point for the RmNet Netlink API. This is the command line
381* interface for the RmNet API
382* @param argc Number of arguments which vary based on the commands
383* @param argv Value of the arguments which vary based on the commands
384* @return RMNETCTL_SUCCESS if successful. Relevant data might be printed
385* based on the message type
386* @return RMNETCTL_LIB_ERR if there was a library error. Error code will be
387* printed
388* @return RMNETCTL_KERNEL_ERR if there was a error in the kernel. Error code will be
389* printed
390* @return RMNETCTL_INVALID_ARG if invalid arguments were passed to the API
391*/
392int main(int argc, char *argv[])
393{
394 argc--;
395 argv++;
396 return rmnet_api_call(argc, argv);
397}