1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2015-2018 Atomic Rules LLC
7 #include <rte_string_fns.h>
8 #include <rte_malloc.h>
10 #include "ark_pktchkr.h"
13 static int set_arg(char *arg, char *val);
14 static int ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle);
16 #define ARK_MAX_STR_LEN 64
21 char STR[ARK_MAX_STR_LEN];
32 char opt[ARK_MAX_STR_LEN];
37 static struct OPTIONS toptions[] = {
38 {{"configure"}, OTBOOL, {1} },
39 {{"port"}, OTINT, {0} },
40 {{"mac-dump"}, OTBOOL, {0} },
41 {{"dg-mode"}, OTBOOL, {1} },
42 {{"run"}, OTBOOL, {0} },
43 {{"stop"}, OTBOOL, {0} },
44 {{"dump"}, OTBOOL, {0} },
45 {{"en_resync"}, OTBOOL, {0} },
46 {{"tuser_err_val"}, OTINT, {1} },
47 {{"gen_forever"}, OTBOOL, {0} },
48 {{"en_slaved_start"}, OTBOOL, {0} },
49 {{"vary_length"}, OTBOOL, {0} },
50 {{"incr_payload"}, OTINT, {0} },
51 {{"incr_first_byte"}, OTBOOL, {0} },
52 {{"ins_seq_num"}, OTBOOL, {0} },
53 {{"ins_time_stamp"}, OTBOOL, {1} },
54 {{"ins_udp_hdr"}, OTBOOL, {0} },
55 {{"num_pkts"}, OTLONG, .v.LONG = 10000000000000L},
56 {{"payload_byte"}, OTINT, {0x55} },
57 {{"pkt_spacing"}, OTINT, {60} },
58 {{"pkt_size_min"}, OTINT, {2005} },
59 {{"pkt_size_max"}, OTINT, {1514} },
60 {{"pkt_size_incr"}, OTINT, {1} },
61 {{"eth_type"}, OTINT, {0x0800} },
62 {{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
63 {{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
64 {{"hdr_dW0"}, OTINT, {0x0016e319} },
65 {{"hdr_dW1"}, OTINT, {0x27150004} },
66 {{"hdr_dW2"}, OTINT, {0x76967bda} },
67 {{"hdr_dW3"}, OTINT, {0x08004500} },
68 {{"hdr_dW4"}, OTINT, {0x005276ed} },
69 {{"hdr_dW5"}, OTINT, {0x40004006} },
70 {{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
71 {{"start_offset"}, OTINT, {0} },
72 {{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
73 {{"dst_port"}, OTINT, {65536} },
74 {{"src_port"}, OTINT, {65536} },
78 ark_pktchkr_init(void *addr, int ord, int l2_mode)
80 struct ark_pkt_chkr_inst *inst =
81 rte_malloc("ark_pkt_chkr_inst",
82 sizeof(struct ark_pkt_chkr_inst), 0);
84 ARK_PMD_LOG(ERR, "Failed to malloc ark_pkt_chkr_inst.\n");
87 inst->sregs = (struct ark_pkt_chkr_stat_regs *)addr;
89 (struct ark_pkt_chkr_ctl_regs *)(((uint8_t *)addr) + 0x100);
91 inst->l2_mode = l2_mode;
96 ark_pktchkr_uninit(ark_pkt_chkr_t handle)
102 ark_pktchkr_run(ark_pkt_chkr_t handle)
104 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
106 inst->sregs->pkt_start_stop = 0;
107 inst->sregs->pkt_start_stop = 0x1;
111 ark_pktchkr_stopped(ark_pkt_chkr_t handle)
113 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
114 uint32_t r = inst->sregs->pkt_start_stop;
116 return (((r >> 16) & 1) == 1);
120 ark_pktchkr_stop(ark_pkt_chkr_t handle)
122 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
125 inst->sregs->pkt_start_stop = 0;
126 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
129 ARK_PMD_LOG(DEBUG, "Waiting for pktchk %d to stop...\n",
132 ARK_PMD_LOG(DEBUG, "Pktchk %d stopped.\n", inst->ordinal);
136 ark_pktchkr_is_running(ark_pkt_chkr_t handle)
138 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
139 uint32_t r = inst->sregs->pkt_start_stop;
141 return ((r & 1) == 1);
145 ark_pktchkr_set_pkt_ctrl(ark_pkt_chkr_t handle,
146 uint32_t gen_forever,
147 uint32_t vary_length,
148 uint32_t incr_payload,
149 uint32_t incr_first_byte,
150 uint32_t ins_seq_num,
151 uint32_t ins_udp_hdr,
153 uint32_t tuser_err_val,
154 uint32_t ins_time_stamp)
156 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
157 uint32_t r = (tuser_err_val << 16) | (en_resync << 0);
159 inst->sregs->pkt_ctrl = r;
162 r = ((gen_forever << 24) |
163 (vary_length << 16) |
164 (incr_payload << 12) |
165 (incr_first_byte << 8) |
166 (ins_time_stamp << 5) |
169 inst->cregs->pkt_ctrl = r;
174 ark_pktchkr_is_gen_forever(ark_pkt_chkr_t handle)
176 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
177 uint32_t r = inst->cregs->pkt_ctrl;
179 return (((r >> 24) & 1) == 1);
183 ark_pktchkr_wait_done(ark_pkt_chkr_t handle)
185 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
187 if (ark_pktchkr_is_gen_forever(handle)) {
188 ARK_PMD_LOG(NOTICE, "Pktchk wait_done will not terminate"
189 " because gen_forever=1\n");
194 while (!ark_pktchkr_stopped(handle) && (wait_cycle > 0)) {
197 ARK_PMD_LOG(DEBUG, "Waiting for packet checker %d's"
198 " internal pktgen to finish sending...\n",
200 ARK_PMD_LOG(DEBUG, "Pktchk %d's pktgen done.\n",
207 ark_pktchkr_get_pkts_sent(ark_pkt_chkr_t handle)
209 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
211 return inst->cregs->pkts_sent;
215 ark_pktchkr_set_payload_byte(ark_pkt_chkr_t handle, uint32_t b)
217 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
219 inst->cregs->pkt_payload = b;
223 ark_pktchkr_set_pkt_size_min(ark_pkt_chkr_t handle, uint32_t x)
225 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
227 inst->cregs->pkt_size_min = x;
231 ark_pktchkr_set_pkt_size_max(ark_pkt_chkr_t handle, uint32_t x)
233 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
235 inst->cregs->pkt_size_max = x;
239 ark_pktchkr_set_pkt_size_incr(ark_pkt_chkr_t handle, uint32_t x)
241 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
243 inst->cregs->pkt_size_incr = x;
247 ark_pktchkr_set_num_pkts(ark_pkt_chkr_t handle, uint32_t x)
249 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
251 inst->cregs->num_pkts = x;
255 ark_pktchkr_set_src_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
257 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
259 inst->cregs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
260 inst->cregs->src_mac_addr_l = mac_addr & 0xffffffff;
264 ark_pktchkr_set_dst_mac_addr(ark_pkt_chkr_t handle, uint64_t mac_addr)
266 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
268 inst->cregs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
269 inst->cregs->dst_mac_addr_l = mac_addr & 0xffffffff;
273 ark_pktchkr_set_eth_type(ark_pkt_chkr_t handle, uint32_t x)
275 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
277 inst->cregs->eth_type = x;
281 ark_pktchkr_set_hdr_dW(ark_pkt_chkr_t handle, uint32_t *hdr)
284 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
286 for (i = 0; i < 7; i++)
287 inst->cregs->hdr_dw[i] = hdr[i];
291 ark_pktchkr_dump_stats(ark_pkt_chkr_t handle)
293 struct ark_pkt_chkr_inst *inst = (struct ark_pkt_chkr_inst *)handle;
295 ARK_PMD_LOG(INFO, "pkts_rcvd = (%'u)\n",
296 inst->sregs->pkts_rcvd);
297 ARK_PMD_LOG(INFO, "bytes_rcvd = (%'" PRIU64 ")\n",
298 inst->sregs->bytes_rcvd);
299 ARK_PMD_LOG(INFO, "pkts_ok = (%'u)\n",
300 inst->sregs->pkts_ok);
301 ARK_PMD_LOG(INFO, "pkts_mismatch = (%'u)\n",
302 inst->sregs->pkts_mismatch);
303 ARK_PMD_LOG(INFO, "pkts_err = (%'u)\n",
304 inst->sregs->pkts_err);
305 ARK_PMD_LOG(INFO, "first_mismatch = (%'u)\n",
306 inst->sregs->first_mismatch);
307 ARK_PMD_LOG(INFO, "resync_events = (%'u)\n",
308 inst->sregs->resync_events);
309 ARK_PMD_LOG(INFO, "pkts_missing = (%'u)\n",
310 inst->sregs->pkts_missing);
311 ARK_PMD_LOG(INFO, "min_latency = (%'u)\n",
312 inst->sregs->min_latency);
313 ARK_PMD_LOG(INFO, "max_latency = (%'u)\n",
314 inst->sregs->max_latency);
317 static struct OPTIONS *
318 options(const char *id)
322 for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
323 if (strcmp(id, toptions[i].opt) == 0)
327 "pktchkr: Could not find requested option!, option = %s\n",
333 set_arg(char *arg, char *val)
335 struct OPTIONS *o = options(arg);
341 o->v.INT = atoi(val);
344 o->v.INT = atoll(val);
347 strlcpy(o->v.STR, val, ARK_MAX_STR_LEN);
356 * Arg format = "opt0=v,opt_n=v ..."
359 ark_pktchkr_parse(char *args)
362 const char toks[] = "=\n\t\v\f \r";
363 argv = strtok(args, toks);
364 v = strtok(NULL, toks);
367 argv = strtok(NULL, toks);
368 v = strtok(NULL, toks);
372 static int32_t parse_ipv4_string(char const *ip_address);
374 parse_ipv4_string(char const *ip_address)
378 if (sscanf(ip_address, "%u.%u.%u.%u",
379 &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
381 return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
385 ark_pktchkr_setup(ark_pkt_chkr_t handle)
388 int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
390 if (!options("stop")->v.BOOL && options("configure")->v.BOOL) {
391 ark_pktchkr_set_payload_byte(handle,
392 options("payload_byte")->v.INT);
393 ark_pktchkr_set_src_mac_addr(handle,
394 options("src_mac_addr")->v.INT);
395 ark_pktchkr_set_dst_mac_addr(handle,
396 options("dst_mac_addr")->v.LONG);
398 ark_pktchkr_set_eth_type(handle,
399 options("eth_type")->v.INT);
400 if (options("dg-mode")->v.BOOL) {
401 hdr[0] = options("hdr_dW0")->v.INT;
402 hdr[1] = options("hdr_dW1")->v.INT;
403 hdr[2] = options("hdr_dW2")->v.INT;
404 hdr[3] = options("hdr_dW3")->v.INT;
405 hdr[4] = options("hdr_dW4")->v.INT;
406 hdr[5] = options("hdr_dW5")->v.INT;
407 hdr[6] = options("hdr_dW6")->v.INT;
410 hdr[1] = options("dst_port")->v.INT;
411 hdr[2] = options("src_port")->v.INT;
417 ark_pktchkr_set_hdr_dW(handle, hdr);
418 ark_pktchkr_set_num_pkts(handle,
419 options("num_pkts")->v.INT);
420 ark_pktchkr_set_pkt_size_min(handle,
421 options("pkt_size_min")->v.INT);
422 ark_pktchkr_set_pkt_size_max(handle,
423 options("pkt_size_max")->v.INT);
424 ark_pktchkr_set_pkt_size_incr(handle,
425 options("pkt_size_incr")->v.INT);
426 ark_pktchkr_set_pkt_ctrl(handle,
427 options("gen_forever")->v.BOOL,
428 options("vary_length")->v.BOOL,
429 options("incr_payload")->v.BOOL,
430 options("incr_first_byte")->v.BOOL,
431 options("ins_seq_num")->v.INT,
432 options("ins_udp_hdr")->v.BOOL,
433 options("en_resync")->v.BOOL,
434 options("tuser_err_val")->v.INT,
435 options("ins_time_stamp")->v.INT);
438 if (options("stop")->v.BOOL)
439 ark_pktchkr_stop(handle);
441 if (options("run")->v.BOOL) {
442 ARK_PMD_LOG(DEBUG, "Starting packet checker on port %d\n",
443 options("port")->v.INT);
444 ark_pktchkr_run(handle);