ee2920383154f3fd0355bfcfaab9889763a93d88
[deb_dpdk.git] / examples / server_node_efd / server / args.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37 #include <getopt.h>
38 #include <stdarg.h>
39 #include <errno.h>
40
41 #include <rte_memory.h>
42 #include <rte_string_fns.h>
43
44 #include "common.h"
45 #include "args.h"
46 #include "init.h"
47
48 /* 1M flows by default */
49 #define DEFAULT_NUM_FLOWS    0x100000
50
51 /* global var for number of nodes - extern in header */
52 uint8_t num_nodes;
53 /* global var for number of flows - extern in header */
54 uint32_t num_flows = DEFAULT_NUM_FLOWS;
55
56 static const char *progname;
57
58 /**
59  * Prints out usage information to stdout
60  */
61 static void
62 usage(void)
63 {
64         printf("%s [EAL options] -- -p PORTMASK -n NUM_NODES -f NUM_FLOWS\n"
65                 " -p PORTMASK: hexadecimal bitmask of ports to use\n"
66                 " -n NUM_NODES: number of node processes to use\n"
67                 " -f NUM_FLOWS: number of flows to be added in the EFD table\n",
68                 progname);
69 }
70
71 /**
72  * The ports to be used by the application are passed in
73  * the form of a bitmask. This function parses the bitmask
74  * and places the port numbers to be used into the port[]
75  * array variable
76  */
77 static int
78 parse_portmask(uint8_t max_ports, const char *portmask)
79 {
80         char *end = NULL;
81         unsigned long pm;
82         uint8_t count = 0;
83
84         if (portmask == NULL || *portmask == '\0')
85                 return -1;
86
87         /* convert parameter to a number and verify */
88         pm = strtoul(portmask, &end, 16);
89         if (end == NULL || *end != '\0' || pm == 0)
90                 return -1;
91
92         /* loop through bits of the mask and mark ports */
93         while (pm != 0) {
94                 if (pm & 0x01) { /* bit is set in mask, use port */
95                         if (count >= max_ports)
96                                 printf("WARNING: requested port %u not present"
97                                 " - ignoring\n", (unsigned int)count);
98                         else
99                             info->id[info->num_ports++] = count;
100                 }
101                 pm = (pm >> 1);
102                 count++;
103         }
104
105         return 0;
106 }
107
108 /**
109  * Take the number of nodes parameter passed to the app
110  * and convert to a number to store in the num_nodes variable
111  */
112 static int
113 parse_num_nodes(const char *nodes)
114 {
115         char *end = NULL;
116         unsigned long temp;
117
118         if (nodes == NULL || *nodes == '\0')
119                 return -1;
120
121         temp = strtoul(nodes, &end, 10);
122         if (end == NULL || *end != '\0' || temp == 0)
123                 return -1;
124
125         num_nodes = (uint8_t)temp;
126         return 0;
127 }
128
129 static int
130 parse_num_flows(const char *flows)
131 {
132         char *end = NULL;
133
134         /* parse hexadecimal string */
135         num_flows = strtoul(flows, &end, 16);
136         if ((flows[0] == '\0') || (end == NULL) || (*end != '\0'))
137                 return -1;
138
139         if (num_flows == 0)
140                 return -1;
141
142         return 0;
143 }
144
145 /**
146  * The application specific arguments follow the DPDK-specific
147  * arguments which are stripped by the DPDK init. This function
148  * processes these application arguments, printing usage info
149  * on error.
150  */
151 int
152 parse_app_args(uint8_t max_ports, int argc, char *argv[])
153 {
154         int option_index, opt;
155         char **argvopt = argv;
156         static struct option lgopts[] = { /* no long options */
157                 {NULL, 0, 0, 0 }
158         };
159         progname = argv[0];
160
161         while ((opt = getopt_long(argc, argvopt, "n:f:p:", lgopts,
162                         &option_index)) != EOF) {
163                 switch (opt) {
164                 case 'p':
165                         if (parse_portmask(max_ports, optarg) != 0) {
166                                 usage();
167                                 return -1;
168                         }
169                         break;
170                 case 'n':
171                         if (parse_num_nodes(optarg) != 0) {
172                                 usage();
173                                 return -1;
174                         }
175                         break;
176                 case 'f':
177                         if (parse_num_flows(optarg) != 0) {
178                                 usage();
179                                 return -1;
180                         }
181                         break;
182                 default:
183                         printf("ERROR: Unknown option '%c'\n", opt);
184                         usage();
185                         return -1;
186                 }
187         }
188
189         if (info->num_ports == 0 || num_nodes == 0) {
190                 usage();
191                 return -1;
192         }
193
194         if (info->num_ports % 2 != 0) {
195                 printf("ERROR: application requires an even "
196                                 "number of ports to use\n");
197                 return -1;
198         }
199         return 0;
200 }