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