1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
10 #include <rte_string_fns.h>
13 #include <rte_ethdev_driver.h>
14 #include <rte_malloc.h>
16 #include "ark_pktgen.h"
19 #define ARK_MAX_STR_LEN 64
24 char STR[ARK_MAX_STR_LEN];
35 char opt[ARK_MAX_STR_LEN];
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} },
80 ark_pktgen_init(void *adr, int ord, int l2_mode)
82 struct ark_pkt_gen_inst *inst =
83 rte_malloc("ark_pkt_gen_inst_pmd",
84 sizeof(struct ark_pkt_gen_inst), 0);
86 PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_gen_inst.\n");
89 inst->regs = (struct ark_pkt_gen_regs *)adr;
91 inst->l2_mode = l2_mode;
96 ark_pktgen_uninit(ark_pkt_gen_t handle)
102 ark_pktgen_run(ark_pkt_gen_t handle)
104 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
106 inst->regs->pkt_start_stop = 1;
110 ark_pktgen_paused(ark_pkt_gen_t handle)
112 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
113 uint32_t r = inst->regs->pkt_start_stop;
115 return (((r >> 16) & 1) == 1);
119 ark_pktgen_pause(ark_pkt_gen_t handle)
121 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
124 inst->regs->pkt_start_stop = 0;
126 while (!ark_pktgen_paused(handle)) {
129 PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n",
134 PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
138 ark_pktgen_reset(ark_pkt_gen_t handle)
140 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
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",
150 if (ark_pktgen_is_running(handle) &&
151 !ark_pktgen_paused(handle)) {
153 "Pktgen %d is not paused. Pausing first.\n",
155 ark_pktgen_pause(handle);
158 PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
159 inst->regs->pkt_start_stop = (1 << 8);
163 ark_pktgen_tx_done(ark_pkt_gen_t handle)
165 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
166 uint32_t r = inst->regs->pkt_start_stop;
168 return (((r >> 24) & 1) == 1);
172 ark_pktgen_is_running(ark_pkt_gen_t handle)
174 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
175 uint32_t r = inst->regs->pkt_start_stop;
177 return ((r & 1) == 1);
181 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
183 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
184 uint32_t r = inst->regs->pkt_ctrl;
186 return (((r >> 24) & 1) == 1);
190 ark_pktgen_wait_done(ark_pkt_gen_t handle)
192 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
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");
199 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
203 "Waiting for pktgen %d to finish sending...\n",
206 PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
210 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
212 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
213 return inst->regs->pkts_sent;
217 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
219 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
220 inst->regs->pkt_payload = b;
224 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
226 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
227 inst->regs->pkt_spacing = x;
231 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
233 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
234 inst->regs->pkt_size_min = x;
238 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
240 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
241 inst->regs->pkt_size_max = x;
245 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
247 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
248 inst->regs->pkt_size_incr = x;
252 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
254 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
255 inst->regs->num_pkts = x;
259 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
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;
267 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
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;
275 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
277 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
278 inst->regs->eth_type = x;
282 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
285 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
287 for (i = 0; i < 7; i++)
288 inst->regs->hdr_dw[i] = hdr[i];
292 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
294 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
296 inst->regs->start_offset = x;
299 static struct OPTIONS *
300 options(const char *id)
304 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
305 if (strcmp(id, toptions[i].opt) == 0)
310 "Pktgen: Could not find requested option!, "
317 static int pmd_set_arg(char *arg, char *val);
319 pmd_set_arg(char *arg, char *val)
321 struct OPTIONS *o = options(arg);
327 o->v.INT = atoi(val);
330 o->v.INT = atoll(val);
333 strlcpy(o->v.STR, val, ARK_MAX_STR_LEN);
342 * Arg format = "opt0=v,opt_n=v ..."
345 ark_pktgen_parse(char *args)
348 const char toks[] = " =\n\t\v\f \r";
349 argv = strtok(args, toks);
350 v = strtok(NULL, toks);
352 pmd_set_arg(argv, v);
353 argv = strtok(NULL, toks);
354 v = strtok(NULL, toks);
358 static int32_t parse_ipv4_string(char const *ip_address);
360 parse_ipv4_string(char const *ip_address)
364 if (sscanf(ip_address, "%u.%u.%u.%u",
365 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
367 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
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)
382 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
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) |
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 */
400 inst->regs->pkt_ctrl = r;
404 ark_pktgen_setup(ark_pkt_gen_t handle)
407 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
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);
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;
431 hdr[1] = options("dst_port")->v.INT;
432 hdr[2] = options("src_port")->v.INT;
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);
462 if (options("pause")->v.BOOL)
463 ark_pktgen_pause(handle);
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);