1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
10 #include <rte_string_fns.h>
11 #include <rte_ethdev_driver.h>
12 #include <rte_malloc.h>
14 #include "ark_pktchkr.h"
17 static int set_arg(char *arg, char *val);
18 static int ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle);
20 #define ARK_MAX_STR_LEN 64
25 char STR[ARK_MAX_STR_LEN];
36 char opt[ARK_MAX_STR_LEN];
41 static struct OPTIONS toptions[] = {
42 {{"configure"}, OTBOOL, {1} },
43 {{"port"}, OTINT, {0} },
44 {{"mac-dump"}, OTBOOL, {0} },
45 {{"dg-mode"}, OTBOOL, {1} },
46 {{"run"}, OTBOOL, {0} },
47 {{"stop"}, OTBOOL, {0} },
48 {{"dump"}, OTBOOL, {0} },
49 {{"en_resync"}, OTBOOL, {0} },
50 {{"tuser_err_val"}, OTINT, {1} },
51 {{"gen_forever"}, OTBOOL, {0} },
52 {{"en_slaved_start"}, OTBOOL, {0} },
53 {{"vary_length"}, OTBOOL, {0} },
54 {{"incr_payload"}, OTINT, {0} },
55 {{"incr_first_byte"}, OTBOOL, {0} },
56 {{"ins_seq_num"}, OTBOOL, {0} },
57 {{"ins_time_stamp"}, OTBOOL, {1} },
58 {{"ins_udp_hdr"}, OTBOOL, {0} },
59 {{"num_pkts"}, OTLONG, .v.LONG = 10000000000000L},
60 {{"payload_byte"}, OTINT, {0x55} },
61 {{"pkt_spacing"}, OTINT, {60} },
62 {{"pkt_size_min"}, OTINT, {2005} },
63 {{"pkt_size_max"}, OTINT, {1514} },
64 {{"pkt_size_incr"}, OTINT, {1} },
65 {{"eth_type"}, OTINT, {0x0800} },
66 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
67 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
68 {{"hdr_dW0"}, OTINT, {0x0016e319} },
69 {{"hdr_dW1"}, OTINT, {0x27150004} },
70 {{"hdr_dW2"}, OTINT, {0x76967bda} },
71 {{"hdr_dW3"}, OTINT, {0x08004500} },
72 {{"hdr_dW4"}, OTINT, {0x005276ed} },
73 {{"hdr_dW5"}, OTINT, {0x40004006} },
74 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
75 {{"start_offset"}, OTINT, {0} },
76 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
77 {{"dst_port"}, OTINT, {65536} },
78 {{"src_port"}, OTINT, {65536} },
82 ark_pktchkr_init(void *addr, int ord, int l2_mode)
84 struct ark_pkt_chkr_inst *inst =
85 rte_malloc("ark_pkt_chkr_inst",
86 sizeof(struct ark_pkt_chkr_inst), 0);
88 PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_chkr_inst.\n");
91 inst->sregs = (struct ark_pkt_chkr_stat_regs *)addr;
93 (struct ark_pkt_chkr_ctl_regs *)(((uint8_t *)addr) + 0x100);
95 inst->l2_mode = l2_mode;
100 ark_pktchkr_uninit(ark_pkt_chkr_t handle)
106 ark_pktchkr_run(ark_pkt_chkr_t handle)
108 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
110 inst->sregs->pkt_start_stop = 0;
111 inst->sregs->pkt_start_stop = 0x1;
115 ark_pktchkr_stopped(ark_pkt_chkr_t handle)
117 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
118 uint32_t r = inst->sregs->pkt_start_stop;
120 return (((r >> 16) & 1) == 1);
124 ark_pktchkr_stop(ark_pkt_chkr_t handle)
126 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
129 inst->sregs->pkt_start_stop = 0;
130 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
133 PMD_DEBUG_LOG(DEBUG, "Waiting for pktchk %d to stop...\n",
136 PMD_DEBUG_LOG(DEBUG, "Pktchk %d stopped.\n", inst->ordinal);
140 ark_pktchkr_is_running(ark_pkt_chkr_t handle)
142 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
143 uint32_t r = inst->sregs->pkt_start_stop;
145 return ((r & 1) == 1);
149 ark_pktchkr_set_pkt_ctrl(ark_pkt_chkr_t handle,
150 uint32_t gen_forever,
151 uint32_t vary_length,
152 uint32_t incr_payload,
153 uint32_t incr_first_byte,
154 uint32_t ins_seq_num,
155 uint32_t ins_udp_hdr,
157 uint32_t tuser_err_val,
158 uint32_t ins_time_stamp)
160 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
161 uint32_t r = (tuser_err_val << 16) | (en_resync << 0);
163 inst->sregs->pkt_ctrl = r;
166 r = ((gen_forever << 24) |
167 (vary_length << 16) |
168 (incr_payload << 12) |
169 (incr_first_byte << 8) |
170 (ins_time_stamp << 5) |
173 inst->cregs->pkt_ctrl = r;
178 ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle)
180 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
181 uint32_t r = inst->cregs->pkt_ctrl;
183 return (((r >> 24) & 1) == 1);
187 ark_pktchkr_wait_done(ark_pkt_chkr_t handle)
189 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
191 if (ark_pktchkr_is_gen_forever(handle)) {
192 PMD_DEBUG_LOG(ERR, "Pktchk wait_done will not terminate"
193 " because gen_forever=1\n");
198 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
201 PMD_DEBUG_LOG(DEBUG, "Waiting for packet checker %d's"
202 " internal pktgen to finish sending...\n",
204 PMD_DEBUG_LOG(DEBUG, "Pktchk %d's pktgen done.\n",
211 ark_pktchkr_get_pkts_sent(ark_pkt_chkr_t handle)
213 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
215 return inst->cregs->pkts_sent;
219 ark_pktchkr_set_payload_byte(ark_pkt_chkr_t handle, uint32_t b)
221 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
223 inst->cregs->pkt_payload = b;
227 ark_pktchkr_set_pkt_size_min(ark_pkt_chkr_t handle, uint32_t x)
229 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
231 inst->cregs->pkt_size_min = x;
235 ark_pktchkr_set_pkt_size_max(ark_pkt_chkr_t handle, uint32_t x)
237 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
239 inst->cregs->pkt_size_max = x;
243 ark_pktchkr_set_pkt_size_incr(ark_pkt_chkr_t handle, uint32_t x)
245 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
247 inst->cregs->pkt_size_incr = x;
251 ark_pktchkr_set_num_pkts(ark_pkt_chkr_t handle, uint32_t x)
253 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
255 inst->cregs->num_pkts = x;
259 ark_pktchkr_set_src_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
261 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
263 inst->cregs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
264 inst->cregs->src_mac_addr_l = mac_addr & 0xffffffff;
268 ark_pktchkr_set_dst_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
270 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
272 inst->cregs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
273 inst->cregs->dst_mac_addr_l = mac_addr & 0xffffffff;
277 ark_pktchkr_set_eth_type(ark_pkt_chkr_t handle, uint32_t x)
279 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
281 inst->cregs->eth_type = x;
285 ark_pktchkr_set_hdr_dW(ark_pkt_chkr_t handle, uint32_t *hdr)
288 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
290 for (i = 0; i < 7; i++)
291 inst->cregs->hdr_dw[i] = hdr[i];
295 ark_pktchkr_dump_stats(ark_pkt_chkr_t handle)
297 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
299 PMD_STATS_LOG(INFO, "pkts_rcvd = (%'u)\n",
300 inst->sregs->pkts_rcvd);
301 PMD_STATS_LOG(INFO, "bytes_rcvd = (%'" PRIU64 ")\n",
302 inst->sregs->bytes_rcvd);
303 PMD_STATS_LOG(INFO, "pkts_ok = (%'u)\n",
304 inst->sregs->pkts_ok);
305 PMD_STATS_LOG(INFO, "pkts_mismatch = (%'u)\n",
306 inst->sregs->pkts_mismatch);
307 PMD_STATS_LOG(INFO, "pkts_err = (%'u)\n",
308 inst->sregs->pkts_err);
309 PMD_STATS_LOG(INFO, "first_mismatch = (%'u)\n",
310 inst->sregs->first_mismatch);
311 PMD_STATS_LOG(INFO, "resync_events = (%'u)\n",
312 inst->sregs->resync_events);
313 PMD_STATS_LOG(INFO, "pkts_missing = (%'u)\n",
314 inst->sregs->pkts_missing);
315 PMD_STATS_LOG(INFO, "min_latency = (%'u)\n",
316 inst->sregs->min_latency);
317 PMD_STATS_LOG(INFO, "max_latency = (%'u)\n",
318 inst->sregs->max_latency);
321 static struct OPTIONS *
322 options(const char *id)
326 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
327 if (strcmp(id, toptions[i].opt) == 0)
331 "pktchkr: Could not find requested option!, option = %s\n",
337 set_arg(char *arg, char *val)
339 struct OPTIONS *o = options(arg);
345 o->v.INT = atoi(val);
348 o->v.INT = atoll(val);
351 strlcpy(o->v.STR, val, ARK_MAX_STR_LEN);
360 * Arg format = "opt0=v,opt_n=v ..."
363 ark_pktchkr_parse(char *args)
366 const char toks[] = "=\n\t\v\f \r";
367 argv = strtok(args, toks);
368 v = strtok(NULL, toks);
371 argv = strtok(NULL, toks);
372 v = strtok(NULL, toks);
376 static int32_t parse_ipv4_string(char const *ip_address);
378 parse_ipv4_string(char const *ip_address)
382 if (sscanf(ip_address, "%u.%u.%u.%u",
383 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
385 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
389 ark_pktchkr_setup(ark_pkt_chkr_t handle)
392 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
394 if (!options("stop")->v.BOOL && options("configure")->v.BOOL) {
395 ark_pktchkr_set_payload_byte(handle,
396 options("payload_byte")->v.INT);
397 ark_pktchkr_set_src_mac_addr(handle,
398 options("src_mac_addr")->v.INT);
399 ark_pktchkr_set_dst_mac_addr(handle,
400 options("dst_mac_addr")->v.LONG);
402 ark_pktchkr_set_eth_type(handle,
403 options("eth_type")->v.INT);
404 if (options("dg-mode")->v.BOOL) {
405 hdr[0] = options("hdr_dW0")->v.INT;
406 hdr[1] = options("hdr_dW1")->v.INT;
407 hdr[2] = options("hdr_dW2")->v.INT;
408 hdr[3] = options("hdr_dW3")->v.INT;
409 hdr[4] = options("hdr_dW4")->v.INT;
410 hdr[5] = options("hdr_dW5")->v.INT;
411 hdr[6] = options("hdr_dW6")->v.INT;
414 hdr[1] = options("dst_port")->v.INT;
415 hdr[2] = options("src_port")->v.INT;
421 ark_pktchkr_set_hdr_dW(handle, hdr);
422 ark_pktchkr_set_num_pkts(handle,
423 options("num_pkts")->v.INT);
424 ark_pktchkr_set_pkt_size_min(handle,
425 options("pkt_size_min")->v.INT);
426 ark_pktchkr_set_pkt_size_max(handle,
427 options("pkt_size_max")->v.INT);
428 ark_pktchkr_set_pkt_size_incr(handle,
429 options("pkt_size_incr")->v.INT);
430 ark_pktchkr_set_pkt_ctrl(handle,
431 options("gen_forever")->v.BOOL,
432 options("vary_length")->v.BOOL,
433 options("incr_payload")->v.BOOL,
434 options("incr_first_byte")->v.BOOL,
435 options("ins_seq_num")->v.INT,
436 options("ins_udp_hdr")->v.BOOL,
437 options("en_resync")->v.BOOL,
438 options("tuser_err_val")->v.INT,
439 options("ins_time_stamp")->v.INT);
442 if (options("stop")->v.BOOL)
443 ark_pktchkr_stop(handle);
445 if (options("run")->v.BOOL) {
446 PMD_DEBUG_LOG(DEBUG, "Starting packet checker on port %d\n",
447 options("port")->v.INT);
448 ark_pktchkr_run(handle);