1 /* 2 * BSD LICENSE 3 * 4 * Copyright (c) 2015-2017 Atomic Rules LLC 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 copyright holder 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 <getopt.h> 35 #include <sys/time.h> 36 #include <locale.h> 37 #include <unistd.h> 38 39 #include <rte_eal.h> 40 41 #include <rte_ethdev.h> 42 #include <rte_malloc.h> 43 44 #include "ark_pktgen.h" 45 #include "ark_logs.h" 46 47 #define ARK_MAX_STR_LEN 64 48 union OPTV { 49 int INT; 50 int BOOL; 51 uint64_t LONG; 52 char STR[ARK_MAX_STR_LEN]; 53 }; 54 55 enum OPTYPE { 56 OTINT, 57 OTLONG, 58 OTBOOL, 59 OTSTRING 60 }; 61 62 struct OPTIONS { 63 char opt[ARK_MAX_STR_LEN]; 64 enum OPTYPE t; 65 union OPTV v; 66 }; 67 68 static struct OPTIONS toptions[] = { 69 {{"configure"}, OTBOOL, {1} }, 70 {{"dg-mode"}, OTBOOL, {1} }, 71 {{"run"}, OTBOOL, {0} }, 72 {{"pause"}, OTBOOL, {0} }, 73 {{"reset"}, OTBOOL, {0} }, 74 {{"dump"}, OTBOOL, {0} }, 75 {{"gen_forever"}, OTBOOL, {0} }, 76 {{"en_slaved_start"}, OTBOOL, {0} }, 77 {{"vary_length"}, OTBOOL, {0} }, 78 {{"incr_payload"}, OTBOOL, {0} }, 79 {{"incr_first_byte"}, OTBOOL, {0} }, 80 {{"ins_seq_num"}, OTBOOL, {0} }, 81 {{"ins_time_stamp"}, OTBOOL, {1} }, 82 {{"ins_udp_hdr"}, OTBOOL, {0} }, 83 {{"num_pkts"}, OTLONG, .v.LONG = 100000000}, 84 {{"payload_byte"}, OTINT, {0x55} }, 85 {{"pkt_spacing"}, OTINT, {130} }, 86 {{"pkt_size_min"}, OTINT, {2006} }, 87 {{"pkt_size_max"}, OTINT, {1514} }, 88 {{"pkt_size_incr"}, OTINT, {1} }, 89 {{"eth_type"}, OTINT, {0x0800} }, 90 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L}, 91 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L}, 92 {{"hdr_dW0"}, OTINT, {0x0016e319} }, 93 {{"hdr_dW1"}, OTINT, {0x27150004} }, 94 {{"hdr_dW2"}, OTINT, {0x76967bda} }, 95 {{"hdr_dW3"}, OTINT, {0x08004500} }, 96 {{"hdr_dW4"}, OTINT, {0x005276ed} }, 97 {{"hdr_dW5"}, OTINT, {0x40004006} }, 98 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} }, 99 {{"start_offset"}, OTINT, {0} }, 100 {{"bytes_per_cycle"}, OTINT, {10} }, 101 {{"shaping"}, OTBOOL, {0} }, 102 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"}, 103 {{"dst_port"}, OTINT, {65536} }, 104 {{"src_port"}, OTINT, {65536} }, 105 }; 106 107 ark_pkt_gen_t 108 ark_pktgen_init(void *adr, int ord, int l2_mode) 109 { 110 struct ark_pkt_gen_inst *inst = 111 rte_malloc("ark_pkt_gen_inst_pmd", 112 sizeof(struct ark_pkt_gen_inst), 0); 113 inst->regs = (struct ark_pkt_gen_regs *)adr; 114 inst->ordinal = ord; 115 inst->l2_mode = l2_mode; 116 return inst; 117 } 118 119 void 120 ark_pktgen_uninit(ark_pkt_gen_t handle) 121 { 122 rte_free(handle); 123 } 124 125 void 126 ark_pktgen_run(ark_pkt_gen_t handle) 127 { 128 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 129 130 inst->regs->pkt_start_stop = 1; 131 } 132 133 uint32_t 134 ark_pktgen_paused(ark_pkt_gen_t handle) 135 { 136 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 137 uint32_t r = inst->regs->pkt_start_stop; 138 139 return (((r >> 16) & 1) == 1); 140 } 141 142 void 143 ark_pktgen_pause(ark_pkt_gen_t handle) 144 { 145 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 146 int cnt = 0; 147 148 inst->regs->pkt_start_stop = 0; 149 150 while (!ark_pktgen_paused(handle)) { 151 usleep(1000); 152 if (cnt++ > 100) { 153 PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n", 154 inst->ordinal); 155 break; 156 } 157 } 158 PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal); 159 } 160 161 void 162 ark_pktgen_reset(ark_pkt_gen_t handle) 163 { 164 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 165 166 if (!ark_pktgen_is_running(handle) && 167 !ark_pktgen_paused(handle)) { 168 PMD_DEBUG_LOG(DEBUG, "Pktgen %d is not running" 169 " and is not paused. No need to reset.\n", 170 inst->ordinal); 171 return; 172 } 173 174 if (ark_pktgen_is_running(handle) && 175 !ark_pktgen_paused(handle)) { 176 PMD_DEBUG_LOG(DEBUG, 177 "Pktgen %d is not paused. Pausing first.\n", 178 inst->ordinal); 179 ark_pktgen_pause(handle); 180 } 181 182 PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal); 183 inst->regs->pkt_start_stop = (1 << 8); 184 } 185 186 uint32_t 187 ark_pktgen_tx_done(ark_pkt_gen_t handle) 188 { 189 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 190 uint32_t r = inst->regs->pkt_start_stop; 191 192 return (((r >> 24) & 1) == 1); 193 } 194 195 uint32_t 196 ark_pktgen_is_running(ark_pkt_gen_t handle) 197 { 198 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 199 uint32_t r = inst->regs->pkt_start_stop; 200 201 return ((r & 1) == 1); 202 } 203 204 uint32_t 205 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle) 206 { 207 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 208 uint32_t r = inst->regs->pkt_ctrl; 209 210 return (((r >> 24) & 1) == 1); 211 } 212 213 void 214 ark_pktgen_wait_done(ark_pkt_gen_t handle) 215 { 216 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 217 int wait_cycle = 10; 218 219 if (ark_pktgen_is_gen_forever(handle)) 220 PMD_DRV_LOG(ERR, "Pktgen wait_done will not terminate" 221 " because gen_forever=1\n"); 222 223 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) { 224 usleep(1000); 225 wait_cycle--; 226 PMD_DEBUG_LOG(DEBUG, 227 "Waiting for pktgen %d to finish sending...\n", 228 inst->ordinal); 229 } 230 PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal); 231 } 232 233 uint32_t 234 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle) 235 { 236 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 237 return inst->regs->pkts_sent; 238 } 239 240 void 241 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b) 242 { 243 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 244 inst->regs->pkt_payload = b; 245 } 246 247 void 248 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x) 249 { 250 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 251 inst->regs->pkt_spacing = x; 252 } 253 254 void 255 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x) 256 { 257 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 258 inst->regs->pkt_size_min = x; 259 } 260 261 void 262 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x) 263 { 264 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 265 inst->regs->pkt_size_max = x; 266 } 267 268 void 269 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x) 270 { 271 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 272 inst->regs->pkt_size_incr = x; 273 } 274 275 void 276 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x) 277 { 278 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 279 inst->regs->num_pkts = x; 280 } 281 282 void 283 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr) 284 { 285 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 286 inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff; 287 inst->regs->src_mac_addr_l = mac_addr & 0xffffffff; 288 } 289 290 void 291 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr) 292 { 293 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 294 inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff; 295 inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff; 296 } 297 298 void 299 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x) 300 { 301 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 302 inst->regs->eth_type = x; 303 } 304 305 void 306 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr) 307 { 308 uint32_t i; 309 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 310 311 for (i = 0; i < 7; i++) 312 inst->regs->hdr_dw[i] = hdr[i]; 313 } 314 315 void 316 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x) 317 { 318 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 319 320 inst->regs->start_offset = x; 321 } 322 323 static struct OPTIONS * 324 options(const char *id) 325 { 326 unsigned int i; 327 328 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) { 329 if (strcmp(id, toptions[i].opt) == 0) 330 return &toptions[i]; 331 } 332 333 PMD_DRV_LOG(ERR, 334 "Pktgen: Could not find requested option!, " 335 "option = %s\n", 336 id 337 ); 338 return NULL; 339 } 340 341 static int pmd_set_arg(char *arg, char *val); 342 static int 343 pmd_set_arg(char *arg, char *val) 344 { 345 struct OPTIONS *o = options(arg); 346 347 if (o) { 348 switch (o->t) { 349 case OTINT: 350 case OTBOOL: 351 o->v.INT = atoi(val); 352 break; 353 case OTLONG: 354 o->v.INT = atoll(val); 355 break; 356 case OTSTRING: 357 strncpy(o->v.STR, val, ARK_MAX_STR_LEN); 358 break; 359 } 360 return 1; 361 } 362 return 0; 363 } 364 365 /****** 366 * Arg format = "opt0=v,opt_n=v ..." 367 ******/ 368 void 369 ark_pktgen_parse(char *args) 370 { 371 char *argv, *v; 372 const char toks[] = " =\n\t\v\f \r"; 373 argv = strtok(args, toks); 374 v = strtok(NULL, toks); 375 while (argv && v) { 376 pmd_set_arg(argv, v); 377 argv = strtok(NULL, toks); 378 v = strtok(NULL, toks); 379 } 380 } 381 382 static int32_t parse_ipv4_string(char const *ip_address); 383 static int32_t 384 parse_ipv4_string(char const *ip_address) 385 { 386 unsigned int ip[4]; 387 388 if (sscanf(ip_address, "%u.%u.%u.%u", 389 &ip[0], &ip[1], &ip[2], &ip[3]) != 4) 390 return 0; 391 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul; 392 } 393 394 static void 395 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle, 396 uint32_t gen_forever, 397 uint32_t en_slaved_start, 398 uint32_t vary_length, 399 uint32_t incr_payload, 400 uint32_t incr_first_byte, 401 uint32_t ins_seq_num, 402 uint32_t ins_udp_hdr, 403 uint32_t ins_time_stamp) 404 { 405 uint32_t r; 406 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle; 407 408 if (!inst->l2_mode) 409 ins_udp_hdr = 0; 410 411 r = ((gen_forever << 24) | 412 (en_slaved_start << 20) | 413 (vary_length << 16) | 414 (incr_payload << 12) | 415 (incr_first_byte << 8) | 416 (ins_time_stamp << 5) | 417 (ins_seq_num << 4) | 418 ins_udp_hdr); 419 420 inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT; 421 if (options("shaping")->v.BOOL) 422 r = r | (1 << 28); /* enable shaping */ 423 424 inst->regs->pkt_ctrl = r; 425 } 426 427 void 428 ark_pktgen_setup(ark_pkt_gen_t handle) 429 { 430 uint32_t hdr[7]; 431 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR); 432 433 if (!options("pause")->v.BOOL && 434 (!options("reset")->v.BOOL && 435 (options("configure")->v.BOOL))) { 436 ark_pktgen_set_payload_byte(handle, 437 options("payload_byte")->v.INT); 438 ark_pktgen_set_src_mac_addr(handle, 439 options("src_mac_addr")->v.INT); 440 ark_pktgen_set_dst_mac_addr(handle, 441 options("dst_mac_addr")->v.LONG); 442 ark_pktgen_set_eth_type(handle, 443 options("eth_type")->v.INT); 444 445 if (options("dg-mode")->v.BOOL) { 446 hdr[0] = options("hdr_dW0")->v.INT; 447 hdr[1] = options("hdr_dW1")->v.INT; 448 hdr[2] = options("hdr_dW2")->v.INT; 449 hdr[3] = options("hdr_dW3")->v.INT; 450 hdr[4] = options("hdr_dW4")->v.INT; 451 hdr[5] = options("hdr_dW5")->v.INT; 452 hdr[6] = options("hdr_dW6")->v.INT; 453 } else { 454 hdr[0] = dst_ip; 455 hdr[1] = options("dst_port")->v.INT; 456 hdr[2] = options("src_port")->v.INT; 457 hdr[3] = 0; 458 hdr[4] = 0; 459 hdr[5] = 0; 460 hdr[6] = 0; 461 } 462 ark_pktgen_set_hdr_dW(handle, hdr); 463 ark_pktgen_set_num_pkts(handle, 464 options("num_pkts")->v.INT); 465 ark_pktgen_set_pkt_size_min(handle, 466 options("pkt_size_min")->v.INT); 467 ark_pktgen_set_pkt_size_max(handle, 468 options("pkt_size_max")->v.INT); 469 ark_pktgen_set_pkt_size_incr(handle, 470 options("pkt_size_incr")->v.INT); 471 ark_pktgen_set_pkt_spacing(handle, 472 options("pkt_spacing")->v.INT); 473 ark_pktgen_set_start_offset(handle, 474 options("start_offset")->v.INT); 475 ark_pktgen_set_pkt_ctrl(handle, 476 options("gen_forever")->v.BOOL, 477 options("en_slaved_start")->v.BOOL, 478 options("vary_length")->v.BOOL, 479 options("incr_payload")->v.BOOL, 480 options("incr_first_byte")->v.BOOL, 481 options("ins_seq_num")->v.INT, 482 options("ins_udp_hdr")->v.BOOL, 483 options("ins_time_stamp")->v.INT); 484 } 485 486 if (options("pause")->v.BOOL) 487 ark_pktgen_pause(handle); 488 489 if (options("reset")->v.BOOL) 490 ark_pktgen_reset(handle); 491 if (options("run")->v.BOOL) { 492 PMD_DEBUG_LOG(DEBUG, "Starting packet generator on port %d\n", 493 options("port")->v.INT); 494 ark_pktgen_run(handle); 495 } 496 } 497