d3ff1bbe4f94cc94d0d5b3320a4f7c49f09b6666
[dpdk.git] / examples / eventdev_pipeline / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4
5 #include <getopt.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <signal.h>
9 #include <sched.h>
10
11 #include "pipeline_common.h"
12
13 struct config_data cdata = {
14         .num_packets = (1L << 25), /* do ~32M packets */
15         .num_fids = 512,
16         .queue_type = RTE_SCHED_TYPE_ATOMIC,
17         .next_qid = {-1},
18         .qid = {-1},
19         .num_stages = 1,
20         .worker_cq_depth = 16
21 };
22
23 static bool
24 core_in_use(unsigned int lcore_id) {
25         return (fdata->rx_core[lcore_id] || fdata->sched_core[lcore_id] ||
26                 fdata->tx_core[lcore_id] || fdata->worker_core[lcore_id]);
27 }
28
29 /*
30  * Parse the coremask given as argument (hexadecimal string) and fill
31  * the global configuration (core role and core count) with the parsed
32  * value.
33  */
34 static int xdigit2val(unsigned char c)
35 {
36         int val;
37
38         if (isdigit(c))
39                 val = c - '0';
40         else if (isupper(c))
41                 val = c - 'A' + 10;
42         else
43                 val = c - 'a' + 10;
44         return val;
45 }
46
47 static uint64_t
48 parse_coremask(const char *coremask)
49 {
50         int i, j, idx = 0;
51         unsigned int count = 0;
52         char c;
53         int val;
54         uint64_t mask = 0;
55         const int32_t BITS_HEX = 4;
56
57         if (coremask == NULL)
58                 return -1;
59         /* Remove all blank characters ahead and after .
60          * Remove 0x/0X if exists.
61          */
62         while (isblank(*coremask))
63                 coremask++;
64         if (coremask[0] == '0' && ((coremask[1] == 'x')
65                 || (coremask[1] == 'X')))
66                 coremask += 2;
67         i = strlen(coremask);
68         while ((i > 0) && isblank(coremask[i - 1]))
69                 i--;
70         if (i == 0)
71                 return -1;
72
73         for (i = i - 1; i >= 0 && idx < MAX_NUM_CORE; i--) {
74                 c = coremask[i];
75                 if (isxdigit(c) == 0) {
76                         /* invalid characters */
77                         return -1;
78                 }
79                 val = xdigit2val(c);
80                 for (j = 0; j < BITS_HEX && idx < MAX_NUM_CORE; j++, idx++) {
81                         if ((1 << j) & val) {
82                                 mask |= (1UL << idx);
83                                 count++;
84                         }
85                 }
86         }
87         for (; i >= 0; i--)
88                 if (coremask[i] != '0')
89                         return -1;
90         if (count == 0)
91                 return -1;
92         return mask;
93 }
94
95 static struct option long_options[] = {
96         {"workers", required_argument, 0, 'w'},
97         {"packets", required_argument, 0, 'n'},
98         {"atomic-flows", required_argument, 0, 'f'},
99         {"num_stages", required_argument, 0, 's'},
100         {"rx-mask", required_argument, 0, 'r'},
101         {"tx-mask", required_argument, 0, 't'},
102         {"sched-mask", required_argument, 0, 'e'},
103         {"cq-depth", required_argument, 0, 'c'},
104         {"work-cycles", required_argument, 0, 'W'},
105         {"mempool-size", required_argument, 0, 'm'},
106         {"queue-priority", no_argument, 0, 'P'},
107         {"parallel", no_argument, 0, 'p'},
108         {"ordered", no_argument, 0, 'o'},
109         {"quiet", no_argument, 0, 'q'},
110         {"use-atq", no_argument, 0, 'a'},
111         {"dump", no_argument, 0, 'D'},
112         {0, 0, 0, 0}
113 };
114
115 static void
116 usage(void)
117 {
118         const char *usage_str =
119                 "  Usage: eventdev_demo [options]\n"
120                 "  Options:\n"
121                 "  -n, --packets=N              Send N packets (default ~32M), 0 implies no limit\n"
122                 "  -f, --atomic-flows=N         Use N random flows from 1 to N (default 16)\n"
123                 "  -s, --num_stages=N           Use N atomic stages (default 1)\n"
124                 "  -r, --rx-mask=core mask      Run NIC rx on CPUs in core mask\n"
125                 "  -w, --worker-mask=core mask  Run worker on CPUs in core mask\n"
126                 "  -t, --tx-mask=core mask      Run NIC tx on CPUs in core mask\n"
127                 "  -e  --sched-mask=core mask   Run scheduler on CPUs in core mask\n"
128                 "  -c  --cq-depth=N             Worker CQ depth (default 16)\n"
129                 "  -W  --work-cycles=N          Worker cycles (default 0)\n"
130                 "  -P  --queue-priority         Enable scheduler queue prioritization\n"
131                 "  -o, --ordered                Use ordered scheduling\n"
132                 "  -p, --parallel               Use parallel scheduling\n"
133                 "  -q, --quiet                  Minimize printed output\n"
134                 "  -a, --use-atq                Use all type queues\n"
135                 "  -m, --mempool-size=N         Dictate the mempool size\n"
136                 "  -D, --dump                   Print detailed statistics before exit"
137                 "\n";
138         fprintf(stderr, "%s", usage_str);
139         exit(1);
140 }
141
142 static void
143 parse_app_args(int argc, char **argv)
144 {
145         /* Parse cli options*/
146         int option_index;
147         int c;
148         opterr = 0;
149         uint64_t rx_lcore_mask = 0;
150         uint64_t tx_lcore_mask = 0;
151         uint64_t sched_lcore_mask = 0;
152         uint64_t worker_lcore_mask = 0;
153         int i;
154
155         for (;;) {
156                 c = getopt_long(argc, argv, "r:t:e:c:w:n:f:s:m:paoPqDW:",
157                                 long_options, &option_index);
158                 if (c == -1)
159                         break;
160
161                 int popcnt = 0;
162                 switch (c) {
163                 case 'n':
164                         cdata.num_packets = (int64_t)atol(optarg);
165                         if (cdata.num_packets == 0)
166                                 cdata.num_packets = INT64_MAX;
167                         break;
168                 case 'f':
169                         cdata.num_fids = (unsigned int)atoi(optarg);
170                         break;
171                 case 's':
172                         cdata.num_stages = (unsigned int)atoi(optarg);
173                         break;
174                 case 'c':
175                         cdata.worker_cq_depth = (unsigned int)atoi(optarg);
176                         break;
177                 case 'W':
178                         cdata.worker_cycles = (unsigned int)atoi(optarg);
179                         break;
180                 case 'P':
181                         cdata.enable_queue_priorities = 1;
182                         break;
183                 case 'o':
184                         cdata.queue_type = RTE_SCHED_TYPE_ORDERED;
185                         break;
186                 case 'p':
187                         cdata.queue_type = RTE_SCHED_TYPE_PARALLEL;
188                         break;
189                 case 'a':
190                         cdata.all_type_queues = 1;
191                         break;
192                 case 'q':
193                         cdata.quiet = 1;
194                         break;
195                 case 'D':
196                         cdata.dump_dev = 1;
197                         break;
198                 case 'w':
199                         worker_lcore_mask = parse_coremask(optarg);
200                         break;
201                 case 'r':
202                         rx_lcore_mask = parse_coremask(optarg);
203                         popcnt = __builtin_popcountll(rx_lcore_mask);
204                         fdata->rx_single = (popcnt == 1);
205                         break;
206                 case 't':
207                         tx_lcore_mask = parse_coremask(optarg);
208                         popcnt = __builtin_popcountll(tx_lcore_mask);
209                         fdata->tx_single = (popcnt == 1);
210                         break;
211                 case 'e':
212                         sched_lcore_mask = parse_coremask(optarg);
213                         popcnt = __builtin_popcountll(sched_lcore_mask);
214                         fdata->sched_single = (popcnt == 1);
215                         break;
216                 case 'm':
217                         cdata.num_mbuf = (uint64_t)atol(optarg);
218                         break;
219                 default:
220                         usage();
221                 }
222         }
223
224         cdata.worker_lcore_mask = worker_lcore_mask;
225         cdata.sched_lcore_mask = sched_lcore_mask;
226         cdata.rx_lcore_mask = rx_lcore_mask;
227         cdata.tx_lcore_mask = tx_lcore_mask;
228
229         if (cdata.num_stages == 0 || cdata.num_stages > MAX_NUM_STAGES)
230                 usage();
231
232         for (i = 0; i < MAX_NUM_CORE; i++) {
233                 fdata->rx_core[i] = !!(rx_lcore_mask & (1UL << i));
234                 fdata->tx_core[i] = !!(tx_lcore_mask & (1UL << i));
235                 fdata->sched_core[i] = !!(sched_lcore_mask & (1UL << i));
236                 fdata->worker_core[i] = !!(worker_lcore_mask & (1UL << i));
237
238                 if (fdata->worker_core[i])
239                         cdata.num_workers++;
240                 if (core_in_use(i))
241                         cdata.active_cores++;
242         }
243 }
244
245 static void
246 do_capability_setup(uint8_t eventdev_id)
247 {
248         int ret;
249         uint16_t i;
250         uint8_t generic_pipeline = 0;
251         uint8_t burst = 0;
252
253         RTE_ETH_FOREACH_DEV(i) {
254                 uint32_t caps = 0;
255
256                 ret = rte_event_eth_tx_adapter_caps_get(eventdev_id, i, &caps);
257                 if (ret)
258                         rte_exit(EXIT_FAILURE,
259                                 "Invalid capability for Tx adptr port %d\n", i);
260                 generic_pipeline |= !(caps &
261                                 RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT);
262         }
263
264         struct rte_event_dev_info eventdev_info;
265         memset(&eventdev_info, 0, sizeof(struct rte_event_dev_info));
266
267         rte_event_dev_info_get(eventdev_id, &eventdev_info);
268         burst = eventdev_info.event_dev_cap & RTE_EVENT_DEV_CAP_BURST_MODE ? 1 :
269                 0;
270
271         if (generic_pipeline)
272                 set_worker_generic_setup_data(&fdata->cap, burst);
273         else
274                 set_worker_tx_enq_setup_data(&fdata->cap, burst);
275 }
276
277 static void
278 signal_handler(int signum)
279 {
280         static uint8_t once;
281         uint16_t portid;
282
283         if (fdata->done)
284                 rte_exit(1, "Exiting on signal %d\n", signum);
285         if ((signum == SIGINT || signum == SIGTERM) && !once) {
286                 printf("\n\nSignal %d received, preparing to exit...\n",
287                                 signum);
288                 if (cdata.dump_dev)
289                         rte_event_dev_dump(0, stdout);
290                 once = 1;
291                 fdata->done = 1;
292                 rte_smp_wmb();
293
294                 RTE_ETH_FOREACH_DEV(portid) {
295                         rte_event_eth_rx_adapter_stop(portid);
296                         rte_event_eth_tx_adapter_stop(portid);
297                         rte_eth_dev_stop(portid);
298                 }
299
300                 rte_eal_mp_wait_lcore();
301
302                 RTE_ETH_FOREACH_DEV(portid) {
303                         rte_eth_dev_close(portid);
304                 }
305
306                 rte_event_dev_stop(0);
307                 rte_event_dev_close(0);
308         }
309         if (signum == SIGTSTP)
310                 rte_event_dev_dump(0, stdout);
311 }
312
313 static inline uint64_t
314 port_stat(int dev_id, int32_t p)
315 {
316         char statname[64];
317         snprintf(statname, sizeof(statname), "port_%u_rx", p);
318         return rte_event_dev_xstats_by_name_get(dev_id, statname, NULL);
319 }
320
321 int
322 main(int argc, char **argv)
323 {
324         struct worker_data *worker_data;
325         uint16_t num_ports;
326         uint16_t portid;
327         int lcore_id;
328         int err;
329
330         signal(SIGINT, signal_handler);
331         signal(SIGTERM, signal_handler);
332         signal(SIGTSTP, signal_handler);
333
334         err = rte_eal_init(argc, argv);
335         if (err < 0)
336                 rte_panic("Invalid EAL arguments\n");
337
338         argc -= err;
339         argv += err;
340
341         fdata = rte_malloc(NULL, sizeof(struct fastpath_data), 0);
342         if (fdata == NULL)
343                 rte_panic("Out of memory\n");
344
345         /* Parse cli options*/
346         parse_app_args(argc, argv);
347
348         num_ports = rte_eth_dev_count_avail();
349         if (num_ports == 0)
350                 rte_panic("No ethernet ports found\n");
351
352         const unsigned int cores_needed = cdata.active_cores;
353
354         if (!cdata.quiet) {
355                 printf("  Config:\n");
356                 printf("\tports: %u\n", num_ports);
357                 printf("\tworkers: %u\n", cdata.num_workers);
358                 printf("\tpackets: %"PRIi64"\n", cdata.num_packets);
359                 printf("\tQueue-prio: %u\n", cdata.enable_queue_priorities);
360                 if (cdata.queue_type == RTE_SCHED_TYPE_ORDERED)
361                         printf("\tqid0 type: ordered\n");
362                 if (cdata.queue_type == RTE_SCHED_TYPE_ATOMIC)
363                         printf("\tqid0 type: atomic\n");
364                 printf("\tCores available: %u\n", rte_lcore_count());
365                 printf("\tCores used: %u\n", cores_needed);
366         }
367
368         if (rte_lcore_count() < cores_needed)
369                 rte_panic("Too few cores (%d < %d)\n", rte_lcore_count(),
370                                 cores_needed);
371
372         const unsigned int ndevs = rte_event_dev_count();
373         if (ndevs == 0)
374                 rte_panic("No dev_id devs found. Pasl in a --vdev eventdev.\n");
375         if (ndevs > 1)
376                 fprintf(stderr, "Warning: More than one eventdev, using idx 0");
377
378
379         do_capability_setup(0);
380         fdata->cap.check_opt();
381
382         worker_data = rte_calloc(0, cdata.num_workers,
383                         sizeof(worker_data[0]), 0);
384         if (worker_data == NULL)
385                 rte_panic("rte_calloc failed\n");
386
387         int dev_id = fdata->cap.evdev_setup(worker_data);
388         if (dev_id < 0)
389                 rte_exit(EXIT_FAILURE, "Error setting up eventdev\n");
390
391         fdata->cap.adptr_setup(num_ports);
392
393         /* Start the Ethernet port. */
394         RTE_ETH_FOREACH_DEV(portid) {
395                 err = rte_eth_dev_start(portid);
396                 if (err < 0)
397                         rte_exit(EXIT_FAILURE, "Error starting ethdev %d\n",
398                                         portid);
399         }
400
401         int worker_idx = 0;
402         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
403                 if (lcore_id >= MAX_NUM_CORE)
404                         break;
405
406                 if (!fdata->rx_core[lcore_id] &&
407                         !fdata->worker_core[lcore_id] &&
408                         !fdata->tx_core[lcore_id] &&
409                         !fdata->sched_core[lcore_id])
410                         continue;
411
412                 if (fdata->rx_core[lcore_id])
413                         printf(
414                                 "[%s()] lcore %d executing NIC Rx\n",
415                                 __func__, lcore_id);
416
417                 if (fdata->tx_core[lcore_id])
418                         printf(
419                                 "[%s()] lcore %d executing NIC Tx\n",
420                                 __func__, lcore_id);
421
422                 if (fdata->sched_core[lcore_id])
423                         printf("[%s()] lcore %d executing scheduler\n",
424                                         __func__, lcore_id);
425
426                 if (fdata->worker_core[lcore_id])
427                         printf(
428                                 "[%s()] lcore %d executing worker, using eventdev port %u\n",
429                                 __func__, lcore_id,
430                                 worker_data[worker_idx].port_id);
431
432                 err = rte_eal_remote_launch(fdata->cap.worker,
433                                 &worker_data[worker_idx], lcore_id);
434                 if (err) {
435                         rte_panic("Failed to launch worker on core %d\n",
436                                         lcore_id);
437                         continue;
438                 }
439                 if (fdata->worker_core[lcore_id])
440                         worker_idx++;
441         }
442
443         lcore_id = rte_lcore_id();
444
445         if (core_in_use(lcore_id))
446                 fdata->cap.worker(&worker_data[worker_idx++]);
447
448         rte_eal_mp_wait_lcore();
449
450         if (!cdata.quiet && (port_stat(dev_id, worker_data[0].port_id) !=
451                         (uint64_t)-ENOTSUP)) {
452                 printf("\nPort Workload distribution:\n");
453                 uint32_t i;
454                 uint64_t tot_pkts = 0;
455                 uint64_t pkts_per_wkr[RTE_MAX_LCORE] = {0};
456                 for (i = 0; i < cdata.num_workers; i++) {
457                         pkts_per_wkr[i] =
458                                 port_stat(dev_id, worker_data[i].port_id);
459                         tot_pkts += pkts_per_wkr[i];
460                 }
461                 for (i = 0; i < cdata.num_workers; i++) {
462                         float pc = pkts_per_wkr[i]  * 100 /
463                                 ((float)tot_pkts);
464                         printf("worker %i :\t%.1f %% (%"PRIu64" pkts)\n",
465                                         i, pc, pkts_per_wkr[i]);
466                 }
467
468         }
469
470         return 0;
471 }