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);
114 PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_gen_inst.\n");
117 inst->regs = (struct ark_pkt_gen_regs *)adr;
119 inst->l2_mode = l2_mode;
124 ark_pktgen_uninit(ark_pkt_gen_t handle)
130 ark_pktgen_run(ark_pkt_gen_t handle)
132 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
134 inst->regs->pkt_start_stop = 1;
138 ark_pktgen_paused(ark_pkt_gen_t handle)
140 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
141 uint32_t r = inst->regs->pkt_start_stop;
143 return (((r >> 16) & 1) == 1);
147 ark_pktgen_pause(ark_pkt_gen_t handle)
149 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
152 inst->regs->pkt_start_stop = 0;
154 while (!ark_pktgen_paused(handle)) {
157 PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n",
162 PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
166 ark_pktgen_reset(ark_pkt_gen_t handle)
168 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
170 if (!ark_pktgen_is_running(handle) &&
171 !ark_pktgen_paused(handle)) {
172 PMD_DEBUG_LOG(DEBUG, "Pktgen %d is not running"
173 " and is not paused. No need to reset.\n",
178 if (ark_pktgen_is_running(handle) &&
179 !ark_pktgen_paused(handle)) {
181 "Pktgen %d is not paused. Pausing first.\n",
183 ark_pktgen_pause(handle);
186 PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
187 inst->regs->pkt_start_stop = (1 << 8);
191 ark_pktgen_tx_done(ark_pkt_gen_t handle)
193 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
194 uint32_t r = inst->regs->pkt_start_stop;
196 return (((r >> 24) & 1) == 1);
200 ark_pktgen_is_running(ark_pkt_gen_t handle)
202 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
203 uint32_t r = inst->regs->pkt_start_stop;
205 return ((r & 1) == 1);
209 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
211 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
212 uint32_t r = inst->regs->pkt_ctrl;
214 return (((r >> 24) & 1) == 1);
218 ark_pktgen_wait_done(ark_pkt_gen_t handle)
220 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
223 if (ark_pktgen_is_gen_forever(handle))
224 PMD_DRV_LOG(ERR, "Pktgen wait_done will not terminate"
225 " because gen_forever=1\n");
227 while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
231 "Waiting for pktgen %d to finish sending...\n",
234 PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
238 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
240 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
241 return inst->regs->pkts_sent;
245 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
247 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
248 inst->regs->pkt_payload = b;
252 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
254 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
255 inst->regs->pkt_spacing = x;
259 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
261 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
262 inst->regs->pkt_size_min = x;
266 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
268 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
269 inst->regs->pkt_size_max = x;
273 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
275 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
276 inst->regs->pkt_size_incr = x;
280 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
282 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
283 inst->regs->num_pkts = x;
287 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
289 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
290 inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
291 inst->regs->src_mac_addr_l = mac_addr & 0xffffffff;
295 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
297 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
298 inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
299 inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff;
303 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
305 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
306 inst->regs->eth_type = x;
310 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
313 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
315 for (i = 0; i < 7; i++)
316 inst->regs->hdr_dw[i] = hdr[i];
320 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
322 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
324 inst->regs->start_offset = x;
327 static struct OPTIONS *
328 options(const char *id)
332 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
333 if (strcmp(id, toptions[i].opt) == 0)
338 "Pktgen: Could not find requested option!, "
345 static int pmd_set_arg(char *arg, char *val);
347 pmd_set_arg(char *arg, char *val)
349 struct OPTIONS *o = options(arg);
355 o->v.INT = atoi(val);
358 o->v.INT = atoll(val);
361 snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
370 * Arg format = "opt0=v,opt_n=v ..."
373 ark_pktgen_parse(char *args)
376 const char toks[] = " =\n\t\v\f \r";
377 argv = strtok(args, toks);
378 v = strtok(NULL, toks);
380 pmd_set_arg(argv, v);
381 argv = strtok(NULL, toks);
382 v = strtok(NULL, toks);
386 static int32_t parse_ipv4_string(char const *ip_address);
388 parse_ipv4_string(char const *ip_address)
392 if (sscanf(ip_address, "%u.%u.%u.%u",
393 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
395 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
399 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle,
400 uint32_t gen_forever,
401 uint32_t en_slaved_start,
402 uint32_t vary_length,
403 uint32_t incr_payload,
404 uint32_t incr_first_byte,
405 uint32_t ins_seq_num,
406 uint32_t ins_udp_hdr,
407 uint32_t ins_time_stamp)
410 struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
415 r = ((gen_forever << 24) |
416 (en_slaved_start << 20) |
417 (vary_length << 16) |
418 (incr_payload << 12) |
419 (incr_first_byte << 8) |
420 (ins_time_stamp << 5) |
424 inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT;
425 if (options("shaping")->v.BOOL)
426 r = r | (1 << 28); /* enable shaping */
428 inst->regs->pkt_ctrl = r;
432 ark_pktgen_setup(ark_pkt_gen_t handle)
435 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
437 if (!options("pause")->v.BOOL &&
438 (!options("reset")->v.BOOL &&
439 (options("configure")->v.BOOL))) {
440 ark_pktgen_set_payload_byte(handle,
441 options("payload_byte")->v.INT);
442 ark_pktgen_set_src_mac_addr(handle,
443 options("src_mac_addr")->v.INT);
444 ark_pktgen_set_dst_mac_addr(handle,
445 options("dst_mac_addr")->v.LONG);
446 ark_pktgen_set_eth_type(handle,
447 options("eth_type")->v.INT);
449 if (options("dg-mode")->v.BOOL) {
450 hdr[0] = options("hdr_dW0")->v.INT;
451 hdr[1] = options("hdr_dW1")->v.INT;
452 hdr[2] = options("hdr_dW2")->v.INT;
453 hdr[3] = options("hdr_dW3")->v.INT;
454 hdr[4] = options("hdr_dW4")->v.INT;
455 hdr[5] = options("hdr_dW5")->v.INT;
456 hdr[6] = options("hdr_dW6")->v.INT;
459 hdr[1] = options("dst_port")->v.INT;
460 hdr[2] = options("src_port")->v.INT;
466 ark_pktgen_set_hdr_dW(handle, hdr);
467 ark_pktgen_set_num_pkts(handle,
468 options("num_pkts")->v.INT);
469 ark_pktgen_set_pkt_size_min(handle,
470 options("pkt_size_min")->v.INT);
471 ark_pktgen_set_pkt_size_max(handle,
472 options("pkt_size_max")->v.INT);
473 ark_pktgen_set_pkt_size_incr(handle,
474 options("pkt_size_incr")->v.INT);
475 ark_pktgen_set_pkt_spacing(handle,
476 options("pkt_spacing")->v.INT);
477 ark_pktgen_set_start_offset(handle,
478 options("start_offset")->v.INT);
479 ark_pktgen_set_pkt_ctrl(handle,
480 options("gen_forever")->v.BOOL,
481 options("en_slaved_start")->v.BOOL,
482 options("vary_length")->v.BOOL,
483 options("incr_payload")->v.BOOL,
484 options("incr_first_byte")->v.BOOL,
485 options("ins_seq_num")->v.INT,
486 options("ins_udp_hdr")->v.BOOL,
487 options("ins_time_stamp")->v.INT);
490 if (options("pause")->v.BOOL)
491 ark_pktgen_pause(handle);
493 if (options("reset")->v.BOOL)
494 ark_pktgen_reset(handle);
495 if (options("run")->v.BOOL) {
496 PMD_DEBUG_LOG(DEBUG, "Starting packet generator on port %d\n",
497 options("port")->v.INT);
498 ark_pktgen_run(handle);