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