1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
12 #include <rte_ethdev_driver.h>
13 #include <rte_malloc.h>
15 #include "ark_pktgen.h"
18 #define ARK_MAX_STR_LEN 64
23 char STR[ARK_MAX_STR_LEN];
34 char opt[ARK_MAX_STR_LEN];
39 static struct OPTIONS toptions[] = {
40 {{"configure"}, OTBOOL, {1} },
41 {{"dg-mode"}, OTBOOL, {1} },
42 {{"run"}, OTBOOL, {0} },
43 {{"pause"}, OTBOOL, {0} },
44 {{"reset"}, OTBOOL, {0} },
45 {{"dump"}, OTBOOL, {0} },
46 {{"gen_forever"}, OTBOOL, {0} },
47 {{"en_slaved_start"}, OTBOOL, {0} },
48 {{"vary_length"}, OTBOOL, {0} },
49 {{"incr_payload"}, OTBOOL, {0} },
50 {{"incr_first_byte"}, OTBOOL, {0} },
51 {{"ins_seq_num"}, OTBOOL, {0} },
52 {{"ins_time_stamp"}, OTBOOL, {1} },
53 {{"ins_udp_hdr"}, OTBOOL, {0} },
54 {{"num_pkts"}, OTLONG, .v.LONG = 100000000},
55 {{"payload_byte"}, OTINT, {0x55} },
56 {{"pkt_spacing"}, OTINT, {130} },
57 {{"pkt_size_min"}, OTINT, {2006} },
58 {{"pkt_size_max"}, OTINT, {1514} },
59 {{"pkt_size_incr"}, OTINT, {1} },
60 {{"eth_type"}, OTINT, {0x0800} },
61 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
62 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
63 {{"hdr_dW0"}, OTINT, {0x0016e319} },
64 {{"hdr_dW1"}, OTINT, {0x27150004} },
65 {{"hdr_dW2"}, OTINT, {0x76967bda} },
66 {{"hdr_dW3"}, OTINT, {0x08004500} },
67 {{"hdr_dW4"}, OTINT, {0x005276ed} },
68 {{"hdr_dW5"}, OTINT, {0x40004006} },
69 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
70 {{"start_offset"}, OTINT, {0} },
71 {{"bytes_per_cycle"}, OTINT, {10} },
72 {{"shaping"}, OTBOOL, {0} },
73 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
74 {{"dst_port"}, OTINT, {65536} },
75 {{"src_port"}, OTINT, {65536} },
79 ark_pktgen_init(void *adr, int ord, int l2_mode)
81 struct ark_pkt_gen_inst *inst =
82 rte_malloc("ark_pkt_gen_inst_pmd",
83 sizeof(struct ark_pkt_gen_inst), 0);
85 PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_gen_inst.\n");
88 inst->regs = (struct ark_pkt_gen_regs *)adr;
90 inst->l2_mode = l2_mode;
95 ark_pktgen_uninit(ark_pkt_gen_t handle)
101 ark_pktgen_run(ark_pkt_gen_t handle)
103 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
105 inst->regs->pkt_start_stop = 1;
109 ark_pktgen_paused(ark_pkt_gen_t handle)
111 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
112 uint32_t r = inst->regs->pkt_start_stop;
114 return (((r >> 16) & 1) == 1);
118 ark_pktgen_pause(ark_pkt_gen_t handle)
120 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
123 inst->regs->pkt_start_stop = 0;
125 while (!ark_pktgen_paused(handle)) {
128 PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n",
133 PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
137 ark_pktgen_reset(ark_pkt_gen_t handle)
139 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
141 if (!ark_pktgen_is_running(handle) &&
142 !ark_pktgen_paused(handle)) {
143 PMD_DEBUG_LOG(DEBUG, "Pktgen %d is not running"
144 " and is not paused. No need to reset.\n",
149 if (ark_pktgen_is_running(handle) &&
150 !ark_pktgen_paused(handle)) {
152 "Pktgen %d is not paused. Pausing first.\n",
154 ark_pktgen_pause(handle);
157 PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
158 inst->regs->pkt_start_stop = (1 << 8);
162 ark_pktgen_tx_done(ark_pkt_gen_t handle)
164 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
165 uint32_t r = inst->regs->pkt_start_stop;
167 return (((r >> 24) & 1) == 1);
171 ark_pktgen_is_running(ark_pkt_gen_t handle)
173 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
174 uint32_t r = inst->regs->pkt_start_stop;
176 return ((r & 1) == 1);
180 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
182 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
183 uint32_t r = inst->regs->pkt_ctrl;
185 return (((r >> 24) & 1) == 1);
189 ark_pktgen_wait_done(ark_pkt_gen_t handle)
191 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
194 if (ark_pktgen_is_gen_forever(handle))
195 PMD_DRV_LOG(ERR, "Pktgen wait_done will not terminate"
196 " because gen_forever=1\n");
198 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
202 "Waiting for pktgen %d to finish sending...\n",
205 PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
209 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
211 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
212 return inst->regs->pkts_sent;
216 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
218 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
219 inst->regs->pkt_payload = b;
223 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
225 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
226 inst->regs->pkt_spacing = x;
230 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
232 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
233 inst->regs->pkt_size_min = x;
237 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
239 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
240 inst->regs->pkt_size_max = x;
244 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
246 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
247 inst->regs->pkt_size_incr = x;
251 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
253 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
254 inst->regs->num_pkts = x;
258 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
260 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
261 inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
262 inst->regs->src_mac_addr_l = mac_addr & 0xffffffff;
266 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
268 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
269 inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
270 inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff;
274 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
276 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
277 inst->regs->eth_type = x;
281 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
284 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
286 for (i = 0; i < 7; i++)
287 inst->regs->hdr_dw[i] = hdr[i];
291 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
293 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
295 inst->regs->start_offset = x;
298 static struct OPTIONS *
299 options(const char *id)
303 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
304 if (strcmp(id, toptions[i].opt) == 0)
309 "Pktgen: Could not find requested option!, "
316 static int pmd_set_arg(char *arg, char *val);
318 pmd_set_arg(char *arg, char *val)
320 struct OPTIONS *o = options(arg);
326 o->v.INT = atoi(val);
329 o->v.INT = atoll(val);
332 snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
341 * Arg format = "opt0=v,opt_n=v ..."
344 ark_pktgen_parse(char *args)
347 const char toks[] = " =\n\t\v\f \r";
348 argv = strtok(args, toks);
349 v = strtok(NULL, toks);
351 pmd_set_arg(argv, v);
352 argv = strtok(NULL, toks);
353 v = strtok(NULL, toks);
357 static int32_t parse_ipv4_string(char const *ip_address);
359 parse_ipv4_string(char const *ip_address)
363 if (sscanf(ip_address, "%u.%u.%u.%u",
364 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
366 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
370 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle,
371 uint32_t gen_forever,
372 uint32_t en_slaved_start,
373 uint32_t vary_length,
374 uint32_t incr_payload,
375 uint32_t incr_first_byte,
376 uint32_t ins_seq_num,
377 uint32_t ins_udp_hdr,
378 uint32_t ins_time_stamp)
381 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
386 r = ((gen_forever << 24) |
387 (en_slaved_start << 20) |
388 (vary_length << 16) |
389 (incr_payload << 12) |
390 (incr_first_byte << 8) |
391 (ins_time_stamp << 5) |
395 inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT;
396 if (options("shaping")->v.BOOL)
397 r = r | (1 << 28); /* enable shaping */
399 inst->regs->pkt_ctrl = r;
403 ark_pktgen_setup(ark_pkt_gen_t handle)
406 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
408 if (!options("pause")->v.BOOL &&
409 (!options("reset")->v.BOOL &&
410 (options("configure")->v.BOOL))) {
411 ark_pktgen_set_payload_byte(handle,
412 options("payload_byte")->v.INT);
413 ark_pktgen_set_src_mac_addr(handle,
414 options("src_mac_addr")->v.INT);
415 ark_pktgen_set_dst_mac_addr(handle,
416 options("dst_mac_addr")->v.LONG);
417 ark_pktgen_set_eth_type(handle,
418 options("eth_type")->v.INT);
420 if (options("dg-mode")->v.BOOL) {
421 hdr[0] = options("hdr_dW0")->v.INT;
422 hdr[1] = options("hdr_dW1")->v.INT;
423 hdr[2] = options("hdr_dW2")->v.INT;
424 hdr[3] = options("hdr_dW3")->v.INT;
425 hdr[4] = options("hdr_dW4")->v.INT;
426 hdr[5] = options("hdr_dW5")->v.INT;
427 hdr[6] = options("hdr_dW6")->v.INT;
430 hdr[1] = options("dst_port")->v.INT;
431 hdr[2] = options("src_port")->v.INT;
437 ark_pktgen_set_hdr_dW(handle, hdr);
438 ark_pktgen_set_num_pkts(handle,
439 options("num_pkts")->v.INT);
440 ark_pktgen_set_pkt_size_min(handle,
441 options("pkt_size_min")->v.INT);
442 ark_pktgen_set_pkt_size_max(handle,
443 options("pkt_size_max")->v.INT);
444 ark_pktgen_set_pkt_size_incr(handle,
445 options("pkt_size_incr")->v.INT);
446 ark_pktgen_set_pkt_spacing(handle,
447 options("pkt_spacing")->v.INT);
448 ark_pktgen_set_start_offset(handle,
449 options("start_offset")->v.INT);
450 ark_pktgen_set_pkt_ctrl(handle,
451 options("gen_forever")->v.BOOL,
452 options("en_slaved_start")->v.BOOL,
453 options("vary_length")->v.BOOL,
454 options("incr_payload")->v.BOOL,
455 options("incr_first_byte")->v.BOOL,
456 options("ins_seq_num")->v.INT,
457 options("ins_udp_hdr")->v.BOOL,
458 options("ins_time_stamp")->v.INT);
461 if (options("pause")->v.BOOL)
462 ark_pktgen_pause(handle);
464 if (options("reset")->v.BOOL)
465 ark_pktgen_reset(handle);
466 if (options("run")->v.BOOL) {
467 PMD_DEBUG_LOG(DEBUG, "Starting packet generator on port %d\n",
468 options("port")->v.INT);
469 ark_pktgen_run(handle);