1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 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 <inttypes.h> 38 #include <sys/types.h> 39 #include <string.h> 40 #include <sys/queue.h> 41 #include <stdarg.h> 42 #include <errno.h> 43 #include <getopt.h> 44 45 #include <rte_common.h> 46 #include <rte_byteorder.h> 47 #include <rte_log.h> 48 #include <rte_memory.h> 49 #include <rte_memcpy.h> 50 #include <rte_memzone.h> 51 #include <rte_eal.h> 52 #include <rte_per_lcore.h> 53 #include <rte_launch.h> 54 #include <rte_atomic.h> 55 #include <rte_cycles.h> 56 #include <rte_prefetch.h> 57 #include <rte_lcore.h> 58 #include <rte_per_lcore.h> 59 #include <rte_branch_prediction.h> 60 #include <rte_interrupts.h> 61 #include <rte_pci.h> 62 #include <rte_random.h> 63 #include <rte_debug.h> 64 #include <rte_ether.h> 65 #include <rte_ethdev.h> 66 #include <rte_mempool.h> 67 #include <rte_mbuf.h> 68 #include <rte_ip.h> 69 #include <rte_tcp.h> 70 #include <rte_lpm.h> 71 #include <rte_lpm6.h> 72 #include <rte_string_fns.h> 73 74 #include "main.h" 75 76 struct app_params app; 77 78 static const char usage[] = "\n"; 79 80 void 81 app_print_usage(void) 82 { 83 printf(usage); 84 } 85 86 static int 87 app_parse_port_mask(const char *arg) 88 { 89 char *end = NULL; 90 uint64_t port_mask; 91 uint32_t i; 92 93 if (arg[0] == '\0') 94 return -1; 95 96 port_mask = strtoul(arg, &end, 16); 97 if ((end == NULL) || (*end != '\0')) 98 return -2; 99 100 if (port_mask == 0) 101 return -3; 102 103 app.n_ports = 0; 104 for (i = 0; i < 64; i++) { 105 if ((port_mask & (1LLU << i)) == 0) 106 continue; 107 108 if (app.n_ports >= APP_MAX_PORTS) 109 return -4; 110 111 app.ports[app.n_ports] = i; 112 app.n_ports++; 113 } 114 115 if (!rte_is_power_of_2(app.n_ports)) 116 return -5; 117 118 return 0; 119 } 120 121 struct { 122 const char *name; 123 uint32_t value; 124 } app_args_table[] = { 125 {"none", e_APP_PIPELINE_NONE}, 126 {"stub", e_APP_PIPELINE_STUB}, 127 {"hash-8-ext", e_APP_PIPELINE_HASH_KEY8_EXT}, 128 {"hash-8-lru", e_APP_PIPELINE_HASH_KEY8_LRU}, 129 {"hash-16-ext", e_APP_PIPELINE_HASH_KEY16_EXT}, 130 {"hash-16-lru", e_APP_PIPELINE_HASH_KEY16_LRU}, 131 {"hash-32-ext", e_APP_PIPELINE_HASH_KEY32_EXT}, 132 {"hash-32-lru", e_APP_PIPELINE_HASH_KEY32_LRU}, 133 {"hash-spec-8-ext", e_APP_PIPELINE_HASH_SPEC_KEY8_EXT}, 134 {"hash-spec-8-lru", e_APP_PIPELINE_HASH_SPEC_KEY8_LRU}, 135 {"hash-spec-16-ext", e_APP_PIPELINE_HASH_SPEC_KEY16_EXT}, 136 {"hash-spec-16-lru", e_APP_PIPELINE_HASH_SPEC_KEY16_LRU}, 137 {"hash-spec-32-ext", e_APP_PIPELINE_HASH_SPEC_KEY32_EXT}, 138 {"hash-spec-32-lru", e_APP_PIPELINE_HASH_SPEC_KEY32_LRU}, 139 {"acl", e_APP_PIPELINE_ACL}, 140 {"lpm", e_APP_PIPELINE_LPM}, 141 {"lpm-ipv6", e_APP_PIPELINE_LPM_IPV6}, 142 }; 143 144 int 145 app_parse_args(int argc, char **argv) 146 { 147 int opt, ret; 148 char **argvopt; 149 int option_index; 150 char *prgname = argv[0]; 151 static struct option lgopts[] = { 152 {"none", 0, 0, 0}, 153 {"stub", 0, 0, 0}, 154 {"hash-8-ext", 0, 0, 0}, 155 {"hash-8-lru", 0, 0, 0}, 156 {"hash-16-ext", 0, 0, 0}, 157 {"hash-16-lru", 0, 0, 0}, 158 {"hash-32-ext", 0, 0, 0}, 159 {"hash-32-lru", 0, 0, 0}, 160 {"hash-spec-8-ext", 0, 0, 0}, 161 {"hash-spec-8-lru", 0, 0, 0}, 162 {"hash-spec-16-ext", 0, 0, 0}, 163 {"hash-spec-16-lru", 0, 0, 0}, 164 {"hash-spec-32-ext", 0, 0, 0}, 165 {"hash-spec-32-lru", 0, 0, 0}, 166 {"acl", 0, 0, 0}, 167 {"lpm", 0, 0, 0}, 168 {"lpm-ipv6", 0, 0, 0}, 169 {NULL, 0, 0, 0} 170 }; 171 uint32_t lcores[3], n_lcores, lcore_id, pipeline_type_provided; 172 173 /* EAL args */ 174 n_lcores = 0; 175 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 176 if (rte_lcore_is_enabled(lcore_id) == 0) 177 continue; 178 179 if (n_lcores >= 3) { 180 RTE_LOG(ERR, USER1, "Number of cores must be 3\n"); 181 app_print_usage(); 182 return -1; 183 } 184 185 lcores[n_lcores] = lcore_id; 186 n_lcores++; 187 } 188 189 if (n_lcores != 3) { 190 RTE_LOG(ERR, USER1, "Number of cores must be 3\n"); 191 app_print_usage(); 192 return -1; 193 } 194 195 app.core_rx = lcores[0]; 196 app.core_worker = lcores[1]; 197 app.core_tx = lcores[2]; 198 199 /* Non-EAL args */ 200 argvopt = argv; 201 202 app.pipeline_type = e_APP_PIPELINE_HASH_KEY16_LRU; 203 pipeline_type_provided = 0; 204 205 while ((opt = getopt_long(argc, argvopt, "p:", 206 lgopts, &option_index)) != EOF) { 207 switch (opt) { 208 case 'p': 209 if (app_parse_port_mask(optarg) < 0) { 210 app_print_usage(); 211 return -1; 212 } 213 break; 214 215 case 0: /* long options */ 216 if (!pipeline_type_provided) { 217 uint32_t i; 218 219 for (i = 0; i < e_APP_PIPELINES; i++) { 220 if (!strcmp(lgopts[option_index].name, 221 app_args_table[i].name)) { 222 app.pipeline_type = 223 app_args_table[i].value; 224 pipeline_type_provided = 1; 225 break; 226 } 227 } 228 229 break; 230 } 231 232 app_print_usage(); 233 return -1; 234 235 default: 236 return -1; 237 } 238 } 239 240 if (optind >= 0) 241 argv[optind - 1] = prgname; 242 243 ret = optind - 1; 244 optind = 0; /* reset getopt lib */ 245 return ret; 246 } 247