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