doc: whitespace changes in licenses
[dpdk.git] / examples / multi_process / client_server_mp / mp_server / args.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 /* global var for number of clients - extern in header */
49 uint8_t num_clients;
50
51 static const char *progname;
52
53 /**
54  * Prints out usage information to stdout
55  */
56 static void
57 usage(void)
58 {
59         printf(
60             "%s [EAL options] -- -p PORTMASK -n NUM_CLIENTS [-s NUM_SOCKETS]\n"
61             " -p PORTMASK: hexadecimal bitmask of ports to use\n"
62             " -n NUM_CLIENTS: number of client processes to use\n"
63             , progname);
64 }
65
66 /**
67  * The ports to be used by the application are passed in
68  * the form of a bitmask. This function parses the bitmask
69  * and places the port numbers to be used into the port[]
70  * array variable
71  */
72 static int
73 parse_portmask(uint8_t max_ports, const char *portmask)
74 {
75         char *end = NULL;
76         unsigned long pm;
77         uint8_t count = 0;
78
79         if (portmask == NULL || *portmask == '\0')
80                 return -1;
81
82         /* convert parameter to a number and verify */
83         pm = strtoul(portmask, &end, 16);
84         if (end == NULL || *end != '\0' || pm == 0)
85                 return -1;
86
87         /* loop through bits of the mask and mark ports */
88         while (pm != 0){
89                 if (pm & 0x01){ /* bit is set in mask, use port */
90                         if (count >= max_ports)
91                                 printf("WARNING: requested port %u not present"
92                                 " - ignoring\n", (unsigned)count);
93                         else
94                             ports->id[ports->num_ports++] = count;
95                 }
96                 pm = (pm >> 1);
97                 count++;
98         }
99
100         return 0;
101 }
102
103 /**
104  * Take the number of clients parameter passed to the app
105  * and convert to a number to store in the num_clients variable
106  */
107 static int
108 parse_num_clients(const char *clients)
109 {
110         char *end = NULL;
111         unsigned long temp;
112
113         if (clients == NULL || *clients == '\0')
114                 return -1;
115
116         temp = strtoul(clients, &end, 10);
117         if (end == NULL || *end != '\0' || temp == 0)
118                 return -1;
119
120         num_clients = (uint8_t)temp;
121         return 0;
122 }
123
124 /**
125  * The application specific arguments follow the DPDK-specific
126  * arguments which are stripped by the DPDK init. This function
127  * processes these application arguments, printing usage info
128  * on error.
129  */
130 int
131 parse_app_args(uint8_t max_ports, int argc, char *argv[])
132 {
133         int option_index, opt;
134         char **argvopt = argv;
135         static struct option lgopts[] = { /* no long options */
136                 {NULL, 0, 0, 0 }
137         };
138         progname = argv[0];
139
140         while ((opt = getopt_long(argc, argvopt, "n:p:", lgopts,
141                 &option_index)) != EOF){
142                 switch (opt){
143                         case 'p':
144                                 if (parse_portmask(max_ports, optarg) != 0){
145                                         usage();
146                                         return -1;
147                                 }
148                                 break;
149                         case 'n':
150                                 if (parse_num_clients(optarg) != 0){
151                                         usage();
152                                         return -1;
153                                 }
154                                 break;
155                         default:
156                                 printf("ERROR: Unknown option '%c'\n", opt);
157                                 usage();
158                                 return -1;
159                 }
160         }
161
162         if (ports->num_ports == 0 || num_clients == 0){
163                 usage();
164                 return -1;
165         }
166
167         if (ports->num_ports % 2 != 0){
168                 printf("ERROR: application requires an even number of ports to use\n");
169                 return -1;
170         }
171         return 0;
172 }
173