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