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.
39 #include <rte_ethdev.h>
40 #include <rte_malloc.h>
42 #include "ark_pktchkr.h"
45 static int set_arg(char *arg, char *val);
46 static int ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle);
48 #define ARK_MAX_STR_LEN 64
53 char STR[ARK_MAX_STR_LEN];
64 char opt[ARK_MAX_STR_LEN];
69 static struct OPTIONS toptions[] = {
70 {{"configure"}, OTBOOL, {1} },
71 {{"port"}, OTINT, {0} },
72 {{"mac-dump"}, OTBOOL, {0} },
73 {{"dg-mode"}, OTBOOL, {1} },
74 {{"run"}, OTBOOL, {0} },
75 {{"stop"}, OTBOOL, {0} },
76 {{"dump"}, OTBOOL, {0} },
77 {{"en_resync"}, OTBOOL, {0} },
78 {{"tuser_err_val"}, OTINT, {1} },
79 {{"gen_forever"}, OTBOOL, {0} },
80 {{"en_slaved_start"}, OTBOOL, {0} },
81 {{"vary_length"}, OTBOOL, {0} },
82 {{"incr_payload"}, OTINT, {0} },
83 {{"incr_first_byte"}, OTBOOL, {0} },
84 {{"ins_seq_num"}, OTBOOL, {0} },
85 {{"ins_time_stamp"}, OTBOOL, {1} },
86 {{"ins_udp_hdr"}, OTBOOL, {0} },
87 {{"num_pkts"}, OTLONG, .v.LONG = 10000000000000L},
88 {{"payload_byte"}, OTINT, {0x55} },
89 {{"pkt_spacing"}, OTINT, {60} },
90 {{"pkt_size_min"}, OTINT, {2005} },
91 {{"pkt_size_max"}, OTINT, {1514} },
92 {{"pkt_size_incr"}, OTINT, {1} },
93 {{"eth_type"}, OTINT, {0x0800} },
94 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
95 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
96 {{"hdr_dW0"}, OTINT, {0x0016e319} },
97 {{"hdr_dW1"}, OTINT, {0x27150004} },
98 {{"hdr_dW2"}, OTINT, {0x76967bda} },
99 {{"hdr_dW3"}, OTINT, {0x08004500} },
100 {{"hdr_dW4"}, OTINT, {0x005276ed} },
101 {{"hdr_dW5"}, OTINT, {0x40004006} },
102 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
103 {{"start_offset"}, OTINT, {0} },
104 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
105 {{"dst_port"}, OTINT, {65536} },
106 {{"src_port"}, OTINT, {65536} },
110 ark_pktchkr_init(void *addr, int ord, int l2_mode)
112 struct ark_pkt_chkr_inst *inst =
113 rte_malloc("ark_pkt_chkr_inst",
114 sizeof(struct ark_pkt_chkr_inst), 0);
116 PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_chkr_inst.\n");
119 inst->sregs = (struct ark_pkt_chkr_stat_regs *)addr;
121 (struct ark_pkt_chkr_ctl_regs *)(((uint8_t *)addr) + 0x100);
123 inst->l2_mode = l2_mode;
128 ark_pktchkr_uninit(ark_pkt_chkr_t handle)
134 ark_pktchkr_run(ark_pkt_chkr_t handle)
136 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
138 inst->sregs->pkt_start_stop = 0;
139 inst->sregs->pkt_start_stop = 0x1;
143 ark_pktchkr_stopped(ark_pkt_chkr_t handle)
145 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
146 uint32_t r = inst->sregs->pkt_start_stop;
148 return (((r >> 16) & 1) == 1);
152 ark_pktchkr_stop(ark_pkt_chkr_t handle)
154 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
157 inst->sregs->pkt_start_stop = 0;
158 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
161 PMD_DEBUG_LOG(DEBUG, "Waiting for pktchk %d to stop...\n",
164 PMD_DEBUG_LOG(DEBUG, "Pktchk %d stopped.\n", inst->ordinal);
168 ark_pktchkr_is_running(ark_pkt_chkr_t handle)
170 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
171 uint32_t r = inst->sregs->pkt_start_stop;
173 return ((r & 1) == 1);
177 ark_pktchkr_set_pkt_ctrl(ark_pkt_chkr_t handle,
178 uint32_t gen_forever,
179 uint32_t vary_length,
180 uint32_t incr_payload,
181 uint32_t incr_first_byte,
182 uint32_t ins_seq_num,
183 uint32_t ins_udp_hdr,
185 uint32_t tuser_err_val,
186 uint32_t ins_time_stamp)
188 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
189 uint32_t r = (tuser_err_val << 16) | (en_resync << 0);
191 inst->sregs->pkt_ctrl = r;
194 r = ((gen_forever << 24) |
195 (vary_length << 16) |
196 (incr_payload << 12) |
197 (incr_first_byte << 8) |
198 (ins_time_stamp << 5) |
201 inst->cregs->pkt_ctrl = r;
206 ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle)
208 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
209 uint32_t r = inst->cregs->pkt_ctrl;
211 return (((r >> 24) & 1) == 1);
215 ark_pktchkr_wait_done(ark_pkt_chkr_t handle)
217 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
219 if (ark_pktchkr_is_gen_forever(handle)) {
220 PMD_DEBUG_LOG(ERR, "Pktchk wait_done will not terminate"
221 " because gen_forever=1\n");
226 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
229 PMD_DEBUG_LOG(DEBUG, "Waiting for packet checker %d's"
230 " internal pktgen to finish sending...\n",
232 PMD_DEBUG_LOG(DEBUG, "Pktchk %d's pktgen done.\n",
239 ark_pktchkr_get_pkts_sent(ark_pkt_chkr_t handle)
241 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
243 return inst->cregs->pkts_sent;
247 ark_pktchkr_set_payload_byte(ark_pkt_chkr_t handle, uint32_t b)
249 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
251 inst->cregs->pkt_payload = b;
255 ark_pktchkr_set_pkt_size_min(ark_pkt_chkr_t handle, uint32_t x)
257 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
259 inst->cregs->pkt_size_min = x;
263 ark_pktchkr_set_pkt_size_max(ark_pkt_chkr_t handle, uint32_t x)
265 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
267 inst->cregs->pkt_size_max = x;
271 ark_pktchkr_set_pkt_size_incr(ark_pkt_chkr_t handle, uint32_t x)
273 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
275 inst->cregs->pkt_size_incr = x;
279 ark_pktchkr_set_num_pkts(ark_pkt_chkr_t handle, uint32_t x)
281 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
283 inst->cregs->num_pkts = x;
287 ark_pktchkr_set_src_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
289 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
291 inst->cregs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
292 inst->cregs->src_mac_addr_l = mac_addr & 0xffffffff;
296 ark_pktchkr_set_dst_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
298 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
300 inst->cregs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
301 inst->cregs->dst_mac_addr_l = mac_addr & 0xffffffff;
305 ark_pktchkr_set_eth_type(ark_pkt_chkr_t handle, uint32_t x)
307 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
309 inst->cregs->eth_type = x;
313 ark_pktchkr_set_hdr_dW(ark_pkt_chkr_t handle, uint32_t *hdr)
316 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
318 for (i = 0; i < 7; i++)
319 inst->cregs->hdr_dw[i] = hdr[i];
323 ark_pktchkr_dump_stats(ark_pkt_chkr_t handle)
325 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
327 PMD_STATS_LOG(INFO, "pkts_rcvd = (%'u)\n",
328 inst->sregs->pkts_rcvd);
329 PMD_STATS_LOG(INFO, "bytes_rcvd = (%'" PRIU64 ")\n",
330 inst->sregs->bytes_rcvd);
331 PMD_STATS_LOG(INFO, "pkts_ok = (%'u)\n",
332 inst->sregs->pkts_ok);
333 PMD_STATS_LOG(INFO, "pkts_mismatch = (%'u)\n",
334 inst->sregs->pkts_mismatch);
335 PMD_STATS_LOG(INFO, "pkts_err = (%'u)\n",
336 inst->sregs->pkts_err);
337 PMD_STATS_LOG(INFO, "first_mismatch = (%'u)\n",
338 inst->sregs->first_mismatch);
339 PMD_STATS_LOG(INFO, "resync_events = (%'u)\n",
340 inst->sregs->resync_events);
341 PMD_STATS_LOG(INFO, "pkts_missing = (%'u)\n",
342 inst->sregs->pkts_missing);
343 PMD_STATS_LOG(INFO, "min_latency = (%'u)\n",
344 inst->sregs->min_latency);
345 PMD_STATS_LOG(INFO, "max_latency = (%'u)\n",
346 inst->sregs->max_latency);
349 static struct OPTIONS *
350 options(const char *id)
354 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
355 if (strcmp(id, toptions[i].opt) == 0)
359 "pktchkr: Could not find requested option!, option = %s\n",
365 set_arg(char *arg, char *val)
367 struct OPTIONS *o = options(arg);
373 o->v.INT = atoi(val);
376 o->v.INT = atoll(val);
379 snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
388 * Arg format = "opt0=v,opt_n=v ..."
391 ark_pktchkr_parse(char *args)
394 const char toks[] = "=\n\t\v\f \r";
395 argv = strtok(args, toks);
396 v = strtok(NULL, toks);
399 argv = strtok(NULL, toks);
400 v = strtok(NULL, toks);
404 static int32_t parse_ipv4_string(char const *ip_address);
406 parse_ipv4_string(char const *ip_address)
410 if (sscanf(ip_address, "%u.%u.%u.%u",
411 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
413 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
417 ark_pktchkr_setup(ark_pkt_chkr_t handle)
420 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
422 if (!options("stop")->v.BOOL && options("configure")->v.BOOL) {
423 ark_pktchkr_set_payload_byte(handle,
424 options("payload_byte")->v.INT);
425 ark_pktchkr_set_src_mac_addr(handle,
426 options("src_mac_addr")->v.INT);
427 ark_pktchkr_set_dst_mac_addr(handle,
428 options("dst_mac_addr")->v.LONG);
430 ark_pktchkr_set_eth_type(handle,
431 options("eth_type")->v.INT);
432 if (options("dg-mode")->v.BOOL) {
433 hdr[0] = options("hdr_dW0")->v.INT;
434 hdr[1] = options("hdr_dW1")->v.INT;
435 hdr[2] = options("hdr_dW2")->v.INT;
436 hdr[3] = options("hdr_dW3")->v.INT;
437 hdr[4] = options("hdr_dW4")->v.INT;
438 hdr[5] = options("hdr_dW5")->v.INT;
439 hdr[6] = options("hdr_dW6")->v.INT;
442 hdr[1] = options("dst_port")->v.INT;
443 hdr[2] = options("src_port")->v.INT;
449 ark_pktchkr_set_hdr_dW(handle, hdr);
450 ark_pktchkr_set_num_pkts(handle,
451 options("num_pkts")->v.INT);
452 ark_pktchkr_set_pkt_size_min(handle,
453 options("pkt_size_min")->v.INT);
454 ark_pktchkr_set_pkt_size_max(handle,
455 options("pkt_size_max")->v.INT);
456 ark_pktchkr_set_pkt_size_incr(handle,
457 options("pkt_size_incr")->v.INT);
458 ark_pktchkr_set_pkt_ctrl(handle,
459 options("gen_forever")->v.BOOL,
460 options("vary_length")->v.BOOL,
461 options("incr_payload")->v.BOOL,
462 options("incr_first_byte")->v.BOOL,
463 options("ins_seq_num")->v.INT,
464 options("ins_udp_hdr")->v.BOOL,
465 options("en_resync")->v.BOOL,
466 options("tuser_err_val")->v.INT,
467 options("ins_time_stamp")->v.INT);
470 if (options("stop")->v.BOOL)
471 ark_pktchkr_stop(handle);
473 if (options("run")->v.BOOL) {
474 PMD_DEBUG_LOG(DEBUG, "Starting packet checker on port %d\n",
475 options("port")->v.INT);
476 ark_pktchkr_run(handle);