xref: /dpdk/drivers/net/ark/ark_pktgen.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2015-2018 Atomic Rules LLC
3  */
4 
5 #include <getopt.h>
6 #include <sys/time.h>
7 #include <locale.h>
8 #include <unistd.h>
9 
10 #include <rte_eal.h>
11 
12 #include <rte_ethdev_driver.h>
13 #include <rte_malloc.h>
14 
15 #include "ark_pktgen.h"
16 #include "ark_logs.h"
17 
18 #define ARK_MAX_STR_LEN 64
19 union OPTV {
20 	int INT;
21 	int BOOL;
22 	uint64_t LONG;
23 	char STR[ARK_MAX_STR_LEN];
24 };
25 
26 enum OPTYPE {
27 	OTINT,
28 	OTLONG,
29 	OTBOOL,
30 	OTSTRING
31 };
32 
33 struct OPTIONS {
34 	char opt[ARK_MAX_STR_LEN];
35 	enum OPTYPE t;
36 	union OPTV v;
37 };
38 
39 static struct OPTIONS toptions[] = {
40 	{{"configure"}, OTBOOL, {1} },
41 	{{"dg-mode"}, OTBOOL, {1} },
42 	{{"run"}, OTBOOL, {0} },
43 	{{"pause"}, OTBOOL, {0} },
44 	{{"reset"}, OTBOOL, {0} },
45 	{{"dump"}, OTBOOL, {0} },
46 	{{"gen_forever"}, OTBOOL, {0} },
47 	{{"en_slaved_start"}, OTBOOL, {0} },
48 	{{"vary_length"}, OTBOOL, {0} },
49 	{{"incr_payload"}, OTBOOL, {0} },
50 	{{"incr_first_byte"}, OTBOOL, {0} },
51 	{{"ins_seq_num"}, OTBOOL, {0} },
52 	{{"ins_time_stamp"}, OTBOOL, {1} },
53 	{{"ins_udp_hdr"}, OTBOOL, {0} },
54 	{{"num_pkts"}, OTLONG, .v.LONG = 100000000},
55 	{{"payload_byte"}, OTINT, {0x55} },
56 	{{"pkt_spacing"}, OTINT, {130} },
57 	{{"pkt_size_min"}, OTINT, {2006} },
58 	{{"pkt_size_max"}, OTINT, {1514} },
59 	{{"pkt_size_incr"}, OTINT, {1} },
60 	{{"eth_type"}, OTINT, {0x0800} },
61 	{{"src_mac_addr"}, OTLONG, .v.LONG = 0xdC3cF6425060L},
62 	{{"dst_mac_addr"}, OTLONG, .v.LONG = 0x112233445566L},
63 	{{"hdr_dW0"}, OTINT, {0x0016e319} },
64 	{{"hdr_dW1"}, OTINT, {0x27150004} },
65 	{{"hdr_dW2"}, OTINT, {0x76967bda} },
66 	{{"hdr_dW3"}, OTINT, {0x08004500} },
67 	{{"hdr_dW4"}, OTINT, {0x005276ed} },
68 	{{"hdr_dW5"}, OTINT, {0x40004006} },
69 	{{"hdr_dW6"}, OTINT, {0x56cfc0a8} },
70 	{{"start_offset"}, OTINT, {0} },
71 	{{"bytes_per_cycle"}, OTINT, {10} },
72 	{{"shaping"}, OTBOOL, {0} },
73 	{{"dst_ip"}, OTSTRING, .v.STR = "169.254.10.240"},
74 	{{"dst_port"}, OTINT, {65536} },
75 	{{"src_port"}, OTINT, {65536} },
76 };
77 
78 ark_pkt_gen_t
79 ark_pktgen_init(void *adr, int ord, int l2_mode)
80 {
81 	struct ark_pkt_gen_inst *inst =
82 		rte_malloc("ark_pkt_gen_inst_pmd",
83 			   sizeof(struct ark_pkt_gen_inst), 0);
84 	if (inst == NULL) {
85 		PMD_DRV_LOG(ERR, "Failed to malloc ark_pkt_gen_inst.\n");
86 		return inst;
87 	}
88 	inst->regs = (struct ark_pkt_gen_regs *)adr;
89 	inst->ordinal = ord;
90 	inst->l2_mode = l2_mode;
91 	return inst;
92 }
93 
94 void
95 ark_pktgen_uninit(ark_pkt_gen_t handle)
96 {
97 	rte_free(handle);
98 }
99 
100 void
101 ark_pktgen_run(ark_pkt_gen_t handle)
102 {
103 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
104 
105 	inst->regs->pkt_start_stop = 1;
106 }
107 
108 uint32_t
109 ark_pktgen_paused(ark_pkt_gen_t handle)
110 {
111 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
112 	uint32_t r = inst->regs->pkt_start_stop;
113 
114 	return (((r >> 16) & 1) == 1);
115 }
116 
117 void
118 ark_pktgen_pause(ark_pkt_gen_t handle)
119 {
120 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
121 	int cnt = 0;
122 
123 	inst->regs->pkt_start_stop = 0;
124 
125 	while (!ark_pktgen_paused(handle)) {
126 		usleep(1000);
127 		if (cnt++ > 100) {
128 			PMD_DRV_LOG(ERR, "Pktgen %d failed to pause.\n",
129 				    inst->ordinal);
130 			break;
131 		}
132 	}
133 	PMD_DEBUG_LOG(DEBUG, "Pktgen %d paused.\n", inst->ordinal);
134 }
135 
136 void
137 ark_pktgen_reset(ark_pkt_gen_t handle)
138 {
139 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
140 
141 	if (!ark_pktgen_is_running(handle) &&
142 	    !ark_pktgen_paused(handle)) {
143 		PMD_DEBUG_LOG(DEBUG, "Pktgen %d is not running"
144 			      " and is not paused. No need to reset.\n",
145 			      inst->ordinal);
146 		return;
147 	}
148 
149 	if (ark_pktgen_is_running(handle) &&
150 	    !ark_pktgen_paused(handle)) {
151 		PMD_DEBUG_LOG(DEBUG,
152 			      "Pktgen %d is not paused. Pausing first.\n",
153 			      inst->ordinal);
154 		ark_pktgen_pause(handle);
155 	}
156 
157 	PMD_DEBUG_LOG(DEBUG, "Resetting pktgen %d.\n", inst->ordinal);
158 	inst->regs->pkt_start_stop = (1 << 8);
159 }
160 
161 uint32_t
162 ark_pktgen_tx_done(ark_pkt_gen_t handle)
163 {
164 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
165 	uint32_t r = inst->regs->pkt_start_stop;
166 
167 	return (((r >> 24) & 1) == 1);
168 }
169 
170 uint32_t
171 ark_pktgen_is_running(ark_pkt_gen_t handle)
172 {
173 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
174 	uint32_t r = inst->regs->pkt_start_stop;
175 
176 	return ((r & 1) == 1);
177 }
178 
179 uint32_t
180 ark_pktgen_is_gen_forever(ark_pkt_gen_t handle)
181 {
182 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
183 	uint32_t r = inst->regs->pkt_ctrl;
184 
185 	return (((r >> 24) & 1) == 1);
186 }
187 
188 void
189 ark_pktgen_wait_done(ark_pkt_gen_t handle)
190 {
191 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
192 	int wait_cycle = 10;
193 
194 	if (ark_pktgen_is_gen_forever(handle))
195 		PMD_DRV_LOG(ERR, "Pktgen wait_done will not terminate"
196 			    " because gen_forever=1\n");
197 
198 	while (!ark_pktgen_tx_done(handle) && (wait_cycle > 0)) {
199 		usleep(1000);
200 		wait_cycle--;
201 		PMD_DEBUG_LOG(DEBUG,
202 			      "Waiting for pktgen %d to finish sending...\n",
203 			      inst->ordinal);
204 	}
205 	PMD_DEBUG_LOG(DEBUG, "Pktgen %d done.\n", inst->ordinal);
206 }
207 
208 uint32_t
209 ark_pktgen_get_pkts_sent(ark_pkt_gen_t handle)
210 {
211 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
212 	return inst->regs->pkts_sent;
213 }
214 
215 void
216 ark_pktgen_set_payload_byte(ark_pkt_gen_t handle, uint32_t b)
217 {
218 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
219 	inst->regs->pkt_payload = b;
220 }
221 
222 void
223 ark_pktgen_set_pkt_spacing(ark_pkt_gen_t handle, uint32_t x)
224 {
225 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
226 	inst->regs->pkt_spacing = x;
227 }
228 
229 void
230 ark_pktgen_set_pkt_size_min(ark_pkt_gen_t handle, uint32_t x)
231 {
232 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
233 	inst->regs->pkt_size_min = x;
234 }
235 
236 void
237 ark_pktgen_set_pkt_size_max(ark_pkt_gen_t handle, uint32_t x)
238 {
239 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
240 	inst->regs->pkt_size_max = x;
241 }
242 
243 void
244 ark_pktgen_set_pkt_size_incr(ark_pkt_gen_t handle, uint32_t x)
245 {
246 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
247 	inst->regs->pkt_size_incr = x;
248 }
249 
250 void
251 ark_pktgen_set_num_pkts(ark_pkt_gen_t handle, uint32_t x)
252 {
253 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
254 	inst->regs->num_pkts = x;
255 }
256 
257 void
258 ark_pktgen_set_src_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
259 {
260 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
261 	inst->regs->src_mac_addr_h = (mac_addr >> 32) & 0xffff;
262 	inst->regs->src_mac_addr_l = mac_addr & 0xffffffff;
263 }
264 
265 void
266 ark_pktgen_set_dst_mac_addr(ark_pkt_gen_t handle, uint64_t mac_addr)
267 {
268 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
269 	inst->regs->dst_mac_addr_h = (mac_addr >> 32) & 0xffff;
270 	inst->regs->dst_mac_addr_l = mac_addr & 0xffffffff;
271 }
272 
273 void
274 ark_pktgen_set_eth_type(ark_pkt_gen_t handle, uint32_t x)
275 {
276 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
277 	inst->regs->eth_type = x;
278 }
279 
280 void
281 ark_pktgen_set_hdr_dW(ark_pkt_gen_t handle, uint32_t *hdr)
282 {
283 	uint32_t i;
284 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
285 
286 	for (i = 0; i < 7; i++)
287 		inst->regs->hdr_dw[i] = hdr[i];
288 }
289 
290 void
291 ark_pktgen_set_start_offset(ark_pkt_gen_t handle, uint32_t x)
292 {
293 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
294 
295 	inst->regs->start_offset = x;
296 }
297 
298 static struct OPTIONS *
299 options(const char *id)
300 {
301 	unsigned int i;
302 
303 	for (i = 0; i < sizeof(toptions) / sizeof(struct OPTIONS); i++) {
304 		if (strcmp(id, toptions[i].opt) == 0)
305 			return &toptions[i];
306 	}
307 
308 	PMD_DRV_LOG(ERR,
309 		    "Pktgen: Could not find requested option!, "
310 		    "option = %s\n",
311 		    id
312 		    );
313 	return NULL;
314 }
315 
316 static int pmd_set_arg(char *arg, char *val);
317 static int
318 pmd_set_arg(char *arg, char *val)
319 {
320 	struct OPTIONS *o = options(arg);
321 
322 	if (o) {
323 		switch (o->t) {
324 		case OTINT:
325 		case OTBOOL:
326 			o->v.INT = atoi(val);
327 			break;
328 		case OTLONG:
329 			o->v.INT = atoll(val);
330 			break;
331 		case OTSTRING:
332 			snprintf(o->v.STR, ARK_MAX_STR_LEN, "%s", val);
333 			break;
334 		}
335 		return 1;
336 	}
337 	return 0;
338 }
339 
340 /******
341  * Arg format = "opt0=v,opt_n=v ..."
342  ******/
343 void
344 ark_pktgen_parse(char *args)
345 {
346 	char *argv, *v;
347 	const char toks[] = " =\n\t\v\f \r";
348 	argv = strtok(args, toks);
349 	v = strtok(NULL, toks);
350 	while (argv && v) {
351 		pmd_set_arg(argv, v);
352 		argv = strtok(NULL, toks);
353 		v = strtok(NULL, toks);
354 	}
355 }
356 
357 static int32_t parse_ipv4_string(char const *ip_address);
358 static int32_t
359 parse_ipv4_string(char const *ip_address)
360 {
361 	unsigned int ip[4];
362 
363 	if (sscanf(ip_address, "%u.%u.%u.%u",
364 		   &ip[0], &ip[1], &ip[2], &ip[3]) != 4)
365 		return 0;
366 	return ip[3] + ip[2] * 0x100 + ip[1] * 0x10000ul + ip[0] * 0x1000000ul;
367 }
368 
369 static void
370 ark_pktgen_set_pkt_ctrl(ark_pkt_gen_t handle,
371 			uint32_t gen_forever,
372 			uint32_t en_slaved_start,
373 			uint32_t vary_length,
374 			uint32_t incr_payload,
375 			uint32_t incr_first_byte,
376 			uint32_t ins_seq_num,
377 			uint32_t ins_udp_hdr,
378 			uint32_t ins_time_stamp)
379 {
380 	uint32_t r;
381 	struct ark_pkt_gen_inst *inst = (struct ark_pkt_gen_inst *)handle;
382 
383 	if (!inst->l2_mode)
384 		ins_udp_hdr = 0;
385 
386 	r = ((gen_forever << 24) |
387 	     (en_slaved_start << 20) |
388 	     (vary_length << 16) |
389 	     (incr_payload << 12) |
390 	     (incr_first_byte << 8) |
391 	     (ins_time_stamp << 5) |
392 	     (ins_seq_num << 4) |
393 	     ins_udp_hdr);
394 
395 	inst->regs->bytes_per_cycle = options("bytes_per_cycle")->v.INT;
396 	if (options("shaping")->v.BOOL)
397 		r = r | (1 << 28);	/* enable shaping */
398 
399 	inst->regs->pkt_ctrl = r;
400 }
401 
402 void
403 ark_pktgen_setup(ark_pkt_gen_t handle)
404 {
405 	uint32_t hdr[7];
406 	int32_t dst_ip = parse_ipv4_string(options("dst_ip")->v.STR);
407 
408 	if (!options("pause")->v.BOOL &&
409 	    (!options("reset")->v.BOOL &&
410 	     (options("configure")->v.BOOL))) {
411 		ark_pktgen_set_payload_byte(handle,
412 					    options("payload_byte")->v.INT);
413 		ark_pktgen_set_src_mac_addr(handle,
414 					    options("src_mac_addr")->v.INT);
415 		ark_pktgen_set_dst_mac_addr(handle,
416 					    options("dst_mac_addr")->v.LONG);
417 		ark_pktgen_set_eth_type(handle,
418 					options("eth_type")->v.INT);
419 
420 		if (options("dg-mode")->v.BOOL) {
421 			hdr[0] = options("hdr_dW0")->v.INT;
422 			hdr[1] = options("hdr_dW1")->v.INT;
423 			hdr[2] = options("hdr_dW2")->v.INT;
424 			hdr[3] = options("hdr_dW3")->v.INT;
425 			hdr[4] = options("hdr_dW4")->v.INT;
426 			hdr[5] = options("hdr_dW5")->v.INT;
427 			hdr[6] = options("hdr_dW6")->v.INT;
428 		} else {
429 			hdr[0] = dst_ip;
430 			hdr[1] = options("dst_port")->v.INT;
431 			hdr[2] = options("src_port")->v.INT;
432 			hdr[3] = 0;
433 			hdr[4] = 0;
434 			hdr[5] = 0;
435 			hdr[6] = 0;
436 		}
437 		ark_pktgen_set_hdr_dW(handle, hdr);
438 		ark_pktgen_set_num_pkts(handle,
439 					options("num_pkts")->v.INT);
440 		ark_pktgen_set_pkt_size_min(handle,
441 					    options("pkt_size_min")->v.INT);
442 		ark_pktgen_set_pkt_size_max(handle,
443 					    options("pkt_size_max")->v.INT);
444 		ark_pktgen_set_pkt_size_incr(handle,
445 					     options("pkt_size_incr")->v.INT);
446 		ark_pktgen_set_pkt_spacing(handle,
447 					   options("pkt_spacing")->v.INT);
448 		ark_pktgen_set_start_offset(handle,
449 					    options("start_offset")->v.INT);
450 		ark_pktgen_set_pkt_ctrl(handle,
451 					options("gen_forever")->v.BOOL,
452 					options("en_slaved_start")->v.BOOL,
453 					options("vary_length")->v.BOOL,
454 					options("incr_payload")->v.BOOL,
455 					options("incr_first_byte")->v.BOOL,
456 					options("ins_seq_num")->v.INT,
457 					options("ins_udp_hdr")->v.BOOL,
458 					options("ins_time_stamp")->v.INT);
459 	}
460 
461 	if (options("pause")->v.BOOL)
462 		ark_pktgen_pause(handle);
463 
464 	if (options("reset")->v.BOOL)
465 		ark_pktgen_reset(handle);
466 	if (options("run")->v.BOOL) {
467 		PMD_DEBUG_LOG(DEBUG, "Starting packet generator on port %d\n",
468 				options("port")->v.INT);
469 		ark_pktgen_run(handle);
470 	}
471 }
472