4 * Copyright (c) 2015-2017 Atomic Rules LLC
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
41 #include <rte_ethdev.h>
42 #include <rte_malloc.h>
44 #include "ark_pktgen.h"
47 #define ARK_MAX_STR_LEN 64
52 char STR[ARK_MAX_STR_LEN];
63 char opt[ARK_MAX_STR_LEN];
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} },
108 ark_pktgen_init(void *adr, int ord, int l2_mode)
110 struct ark_pkt_gen_inst *inst =
111 rte_malloc("ark_pkt_gen_inst_pmd",
112 sizeof(struct ark_pkt_gen_inst), 0);
113 inst->regs = (struct ark_pkt_gen_regs *)adr;
115 inst->l2_mode = l2_mode;
120 ark_pktgen_uninit(ark_pkt_gen_t handle)
126 ark_pktgen_run(ark_pkt_gen_t handle)
128 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
130 inst->regs->pkt_start_stop = 1;
134 ark_pktgen_paused(ark_pkt_gen_t handle)
136 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
137 uint32_t r = inst->regs->pkt_start_stop;
139 return (((r >> 16) & 1) == 1);
143 ark_pktgen_pause(ark_pkt_gen_t handle)
145 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
148 inst->regs->pkt_start_stop = 0;
150 while (!ark_pktgen_paused(handle)) {
153 PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n",
158 PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
162 ark_pktgen_reset(ark_pkt_gen_t handle)
164 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
166 if (!ark_pktgen_is_running(handle) &&
167 !ark_pktgen_paused(handle)) {
168 PMD_DEBUG_LOG(DEBUG, "Pktgen %d is not running"
169 " and is not paused. No need to reset.\n",
174 if (ark_pktgen_is_running(handle) &&
175 !ark_pktgen_paused(handle)) {
177 "Pktgen %d is not paused. Pausing first.\n",
179 ark_pktgen_pause(handle);
182 PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
183 inst->regs->pkt_start_stop = (1 << 8);
187 ark_pktgen_tx_done(ark_pkt_gen_t handle)
189 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
190 uint32_t r = inst->regs->pkt_start_stop;
192 return (((r >> 24) & 1) == 1);
196 ark_pktgen_is_running(ark_pkt_gen_t handle)
198 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
199 uint32_t r = inst->regs->pkt_start_stop;
201 return ((r & 1) == 1);
205 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
207 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
208 uint32_t r = inst->regs->pkt_ctrl;
210 return (((r >> 24) & 1) == 1);
214 ark_pktgen_wait_done(ark_pkt_gen_t handle)
216 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
219 if (ark_pktgen_is_gen_forever(handle))
220 PMD_DRV_LOG(ERR, "Pktgen wait_done will not terminate"
221 " because gen_forever=1\n");
223 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
227 "Waiting for pktgen %d to finish sending...\n",
230 PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
234 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
236 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
237 return inst->regs->pkts_sent;
241 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
243 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
244 inst->regs->pkt_payload = b;
248 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
250 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
251 inst->regs->pkt_spacing = x;
255 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
257 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
258 inst->regs->pkt_size_min = x;
262 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
264 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
265 inst->regs->pkt_size_max = x;
269 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
271 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
272 inst->regs->pkt_size_incr = x;
276 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
278 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
279 inst->regs->num_pkts = x;
283 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
285 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
286 inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
287 inst->regs->src_mac_addr_l = mac_addr & 0xffffffff;
291 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
293 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
294 inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
295 inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff;
299 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
301 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
302 inst->regs->eth_type = x;
306 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
309 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
311 for (i = 0; i < 7; i++)
312 inst->regs->hdr_dw[i] = hdr[i];
316 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
318 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
320 inst->regs->start_offset = x;
323 static struct OPTIONS *
324 options(const char *id)
328 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
329 if (strcmp(id, toptions[i].opt) == 0)
334 "Pktgen: Could not find requested option!, "
341 static int pmd_set_arg(char *arg, char *val);
343 pmd_set_arg(char *arg, char *val)
345 struct OPTIONS *o = options(arg);
351 o->v.INT = atoi(val);
354 o->v.INT = atoll(val);
357 snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
366 * Arg format = "opt0=v,opt_n=v ..."
369 ark_pktgen_parse(char *args)
372 const char toks[] = " =\n\t\v\f \r";
373 argv = strtok(args, toks);
374 v = strtok(NULL, toks);
376 pmd_set_arg(argv, v);
377 argv = strtok(NULL, toks);
378 v = strtok(NULL, toks);
382 static int32_t parse_ipv4_string(char const *ip_address);
384 parse_ipv4_string(char const *ip_address)
388 if (sscanf(ip_address, "%u.%u.%u.%u",
389 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
391 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
395 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle,
396 uint32_t gen_forever,
397 uint32_t en_slaved_start,
398 uint32_t vary_length,
399 uint32_t incr_payload,
400 uint32_t incr_first_byte,
401 uint32_t ins_seq_num,
402 uint32_t ins_udp_hdr,
403 uint32_t ins_time_stamp)
406 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
411 r = ((gen_forever << 24) |
412 (en_slaved_start << 20) |
413 (vary_length << 16) |
414 (incr_payload << 12) |
415 (incr_first_byte << 8) |
416 (ins_time_stamp << 5) |
420 inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT;
421 if (options("shaping")->v.BOOL)
422 r = r | (1 << 28); /* enable shaping */
424 inst->regs->pkt_ctrl = r;
428 ark_pktgen_setup(ark_pkt_gen_t handle)
431 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
433 if (!options("pause")->v.BOOL &&
434 (!options("reset")->v.BOOL &&
435 (options("configure")->v.BOOL))) {
436 ark_pktgen_set_payload_byte(handle,
437 options("payload_byte")->v.INT);
438 ark_pktgen_set_src_mac_addr(handle,
439 options("src_mac_addr")->v.INT);
440 ark_pktgen_set_dst_mac_addr(handle,
441 options("dst_mac_addr")->v.LONG);
442 ark_pktgen_set_eth_type(handle,
443 options("eth_type")->v.INT);
445 if (options("dg-mode")->v.BOOL) {
446 hdr[0] = options("hdr_dW0")->v.INT;
447 hdr[1] = options("hdr_dW1")->v.INT;
448 hdr[2] = options("hdr_dW2")->v.INT;
449 hdr[3] = options("hdr_dW3")->v.INT;
450 hdr[4] = options("hdr_dW4")->v.INT;
451 hdr[5] = options("hdr_dW5")->v.INT;
452 hdr[6] = options("hdr_dW6")->v.INT;
455 hdr[1] = options("dst_port")->v.INT;
456 hdr[2] = options("src_port")->v.INT;
462 ark_pktgen_set_hdr_dW(handle, hdr);
463 ark_pktgen_set_num_pkts(handle,
464 options("num_pkts")->v.INT);
465 ark_pktgen_set_pkt_size_min(handle,
466 options("pkt_size_min")->v.INT);
467 ark_pktgen_set_pkt_size_max(handle,
468 options("pkt_size_max")->v.INT);
469 ark_pktgen_set_pkt_size_incr(handle,
470 options("pkt_size_incr")->v.INT);
471 ark_pktgen_set_pkt_spacing(handle,
472 options("pkt_spacing")->v.INT);
473 ark_pktgen_set_start_offset(handle,
474 options("start_offset")->v.INT);
475 ark_pktgen_set_pkt_ctrl(handle,
476 options("gen_forever")->v.BOOL,
477 options("en_slaved_start")->v.BOOL,
478 options("vary_length")->v.BOOL,
479 options("incr_payload")->v.BOOL,
480 options("incr_first_byte")->v.BOOL,
481 options("ins_seq_num")->v.INT,
482 options("ins_udp_hdr")->v.BOOL,
483 options("ins_time_stamp")->v.INT);
486 if (options("pause")->v.BOOL)
487 ark_pktgen_pause(handle);
489 if (options("reset")->v.BOOL)
490 ark_pktgen_reset(handle);
491 if (options("run")->v.BOOL) {
492 PMD_DEBUG_LOG(DEBUG, "Starting packet generator on port %d\n",
493 options("port")->v.INT);
494 ark_pktgen_run(handle);