1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
8 #include <rte_string_fns.h>
9 #include <rte_malloc.h>
11 #include "ark_pktgen.h"
14 #define ARK_MAX_STR_LEN 64
19 char STR[ARK_MAX_STR_LEN];
30 char opt[ARK_MAX_STR_LEN];
35 static struct OPTIONS toptions[] = {
36 {{"configure"}, OTBOOL, {1} },
37 {{"dg-mode"}, OTBOOL, {1} },
38 {{"run"}, OTBOOL, {0} },
39 {{"pause"}, OTBOOL, {0} },
40 {{"reset"}, OTBOOL, {0} },
41 {{"dump"}, OTBOOL, {0} },
42 {{"gen_forever"}, OTBOOL, {0} },
43 {{"en_slaved_start"}, OTBOOL, {0} },
44 {{"vary_length"}, OTBOOL, {0} },
45 {{"incr_payload"}, OTBOOL, {0} },
46 {{"incr_first_byte"}, OTBOOL, {0} },
47 {{"ins_seq_num"}, OTBOOL, {0} },
48 {{"ins_time_stamp"}, OTBOOL, {1} },
49 {{"ins_udp_hdr"}, OTBOOL, {0} },
50 {{"num_pkts"}, OTLONG, .v.LONG = 100000000},
51 {{"payload_byte"}, OTINT, {0x55} },
52 {{"pkt_spacing"}, OTINT, {130} },
53 {{"pkt_size_min"}, OTINT, {2006} },
54 {{"pkt_size_max"}, OTINT, {1514} },
55 {{"pkt_size_incr"}, OTINT, {1} },
56 {{"eth_type"}, OTINT, {0x0800} },
57 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
58 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
59 {{"hdr_dW0"}, OTINT, {0x0016e319} },
60 {{"hdr_dW1"}, OTINT, {0x27150004} },
61 {{"hdr_dW2"}, OTINT, {0x76967bda} },
62 {{"hdr_dW3"}, OTINT, {0x08004500} },
63 {{"hdr_dW4"}, OTINT, {0x005276ed} },
64 {{"hdr_dW5"}, OTINT, {0x40004006} },
65 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
66 {{"start_offset"}, OTINT, {0} },
67 {{"bytes_per_cycle"}, OTINT, {10} },
68 {{"shaping"}, OTBOOL, {0} },
69 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
70 {{"dst_port"}, OTINT, {65536} },
71 {{"src_port"}, OTINT, {65536} },
75 ark_pktgen_init(void *adr, int ord, int l2_mode)
77 struct ark_pkt_gen_inst *inst =
78 rte_malloc("ark_pkt_gen_inst_pmd",
79 sizeof(struct ark_pkt_gen_inst), 0);
81 ARK_PMD_LOG(ERR, "Failed to malloc ark_pkt_gen_inst.\n");
84 inst->regs = (struct ark_pkt_gen_regs *)adr;
86 inst->l2_mode = l2_mode;
91 ark_pktgen_uninit(ark_pkt_gen_t handle)
97 ark_pktgen_run(ark_pkt_gen_t handle)
99 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
101 inst->regs->pkt_start_stop = 1;
105 ark_pktgen_paused(ark_pkt_gen_t handle)
107 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
108 uint32_t r = inst->regs->pkt_start_stop;
110 return (((r >> 16) & 1) == 1);
114 ark_pktgen_pause(ark_pkt_gen_t handle)
116 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
119 inst->regs->pkt_start_stop = 0;
121 while (!ark_pktgen_paused(handle)) {
124 ARK_PMD_LOG(NOTICE, "Pktgen %d failed to pause.\n",
129 ARK_PMD_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
133 ark_pktgen_reset(ark_pkt_gen_t handle)
135 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
137 if (!ark_pktgen_is_running(handle) &&
138 !ark_pktgen_paused(handle)) {
139 ARK_PMD_LOG(DEBUG, "Pktgen %d is not running"
140 " and is not paused. No need to reset.\n",
145 if (ark_pktgen_is_running(handle) &&
146 !ark_pktgen_paused(handle)) {
148 "Pktgen %d is not paused. Pausing first.\n",
150 ark_pktgen_pause(handle);
153 ARK_PMD_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
154 inst->regs->pkt_start_stop = (1 << 8);
158 ark_pktgen_tx_done(ark_pkt_gen_t handle)
160 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
161 uint32_t r = inst->regs->pkt_start_stop;
163 return (((r >> 24) & 1) == 1);
167 ark_pktgen_is_running(ark_pkt_gen_t handle)
169 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
170 uint32_t r = inst->regs->pkt_start_stop;
172 return ((r & 1) == 1);
176 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
178 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
179 uint32_t r = inst->regs->pkt_ctrl;
181 return (((r >> 24) & 1) == 1);
185 ark_pktgen_wait_done(ark_pkt_gen_t handle)
187 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
190 if (ark_pktgen_is_gen_forever(handle))
191 ARK_PMD_LOG(NOTICE, "Pktgen wait_done will not terminate"
192 " because gen_forever=1\n");
194 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
198 "Waiting for pktgen %d to finish sending...\n",
201 ARK_PMD_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
205 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
207 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
208 return inst->regs->pkts_sent;
212 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
214 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
215 inst->regs->pkt_payload = b;
219 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
221 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
222 inst->regs->pkt_spacing = x;
226 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
228 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
229 inst->regs->pkt_size_min = x;
233 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
235 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
236 inst->regs->pkt_size_max = x;
240 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
242 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
243 inst->regs->pkt_size_incr = x;
247 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
249 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
250 inst->regs->num_pkts = x;
254 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
256 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
257 inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
258 inst->regs->src_mac_addr_l = mac_addr & 0xffffffff;
262 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
264 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
265 inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
266 inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff;
270 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
272 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
273 inst->regs->eth_type = x;
277 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
280 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
282 for (i = 0; i < 7; i++)
283 inst->regs->hdr_dw[i] = hdr[i];
287 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
289 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
291 inst->regs->start_offset = x;
294 static struct OPTIONS *
295 options(const char *id)
299 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
300 if (strcmp(id, toptions[i].opt) == 0)
305 "Pktgen: Could not find requested option!, "
312 static int pmd_set_arg(char *arg, char *val);
314 pmd_set_arg(char *arg, char *val)
316 struct OPTIONS *o = options(arg);
322 o->v.INT = atoi(val);
325 o->v.INT = atoll(val);
328 strlcpy(o->v.STR, val, ARK_MAX_STR_LEN);
337 * Arg format = "opt0=v,opt_n=v ..."
340 ark_pktgen_parse(char *args)
343 const char toks[] = " =\n\t\v\f \r";
344 argv = strtok(args, toks);
345 v = strtok(NULL, toks);
347 pmd_set_arg(argv, v);
348 argv = strtok(NULL, toks);
349 v = strtok(NULL, toks);
353 static int32_t parse_ipv4_string(char const *ip_address);
355 parse_ipv4_string(char const *ip_address)
359 if (sscanf(ip_address, "%u.%u.%u.%u",
360 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
362 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
366 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle,
367 uint32_t gen_forever,
368 uint32_t en_slaved_start,
369 uint32_t vary_length,
370 uint32_t incr_payload,
371 uint32_t incr_first_byte,
372 uint32_t ins_seq_num,
373 uint32_t ins_udp_hdr,
374 uint32_t ins_time_stamp)
377 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
382 r = ((gen_forever << 24) |
383 (en_slaved_start << 20) |
384 (vary_length << 16) |
385 (incr_payload << 12) |
386 (incr_first_byte << 8) |
387 (ins_time_stamp << 5) |
391 inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT;
392 if (options("shaping")->v.BOOL)
393 r = r | (1 << 28); /* enable shaping */
395 inst->regs->pkt_ctrl = r;
399 ark_pktgen_setup(ark_pkt_gen_t handle)
402 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
404 if (!options("pause")->v.BOOL &&
405 (!options("reset")->v.BOOL &&
406 (options("configure")->v.BOOL))) {
407 ark_pktgen_set_payload_byte(handle,
408 options("payload_byte")->v.INT);
409 ark_pktgen_set_src_mac_addr(handle,
410 options("src_mac_addr")->v.INT);
411 ark_pktgen_set_dst_mac_addr(handle,
412 options("dst_mac_addr")->v.LONG);
413 ark_pktgen_set_eth_type(handle,
414 options("eth_type")->v.INT);
416 if (options("dg-mode")->v.BOOL) {
417 hdr[0] = options("hdr_dW0")->v.INT;
418 hdr[1] = options("hdr_dW1")->v.INT;
419 hdr[2] = options("hdr_dW2")->v.INT;
420 hdr[3] = options("hdr_dW3")->v.INT;
421 hdr[4] = options("hdr_dW4")->v.INT;
422 hdr[5] = options("hdr_dW5")->v.INT;
423 hdr[6] = options("hdr_dW6")->v.INT;
426 hdr[1] = options("dst_port")->v.INT;
427 hdr[2] = options("src_port")->v.INT;
433 ark_pktgen_set_hdr_dW(handle, hdr);
434 ark_pktgen_set_num_pkts(handle,
435 options("num_pkts")->v.INT);
436 ark_pktgen_set_pkt_size_min(handle,
437 options("pkt_size_min")->v.INT);
438 ark_pktgen_set_pkt_size_max(handle,
439 options("pkt_size_max")->v.INT);
440 ark_pktgen_set_pkt_size_incr(handle,
441 options("pkt_size_incr")->v.INT);
442 ark_pktgen_set_pkt_spacing(handle,
443 options("pkt_spacing")->v.INT);
444 ark_pktgen_set_start_offset(handle,
445 options("start_offset")->v.INT);
446 ark_pktgen_set_pkt_ctrl(handle,
447 options("gen_forever")->v.BOOL,
448 options("en_slaved_start")->v.BOOL,
449 options("vary_length")->v.BOOL,
450 options("incr_payload")->v.BOOL,
451 options("incr_first_byte")->v.BOOL,
452 options("ins_seq_num")->v.INT,
453 options("ins_udp_hdr")->v.BOOL,
454 options("ins_time_stamp")->v.INT);
457 if (options("pause")->v.BOOL)
458 ark_pktgen_pause(handle);
460 if (options("reset")->v.BOOL)
461 ark_pktgen_reset(handle);
462 if (options("run")->v.BOOL) {
463 ARK_PMD_LOG(DEBUG, "Starting packet generator on port %d\n",
464 options("port")->v.INT);
465 ark_pktgen_run(handle);
470 ark_pktgen_delay_start(void *arg)
472 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)arg;
474 /* This function is used exclusively for regression testing, We
475 * perform a blind sleep here to ensure that the external test
476 * application has time to setup the test before we generate packets
478 pthread_detach(pthread_self());
480 ark_pktgen_run(inst);