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