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