app/eventdev: add ethernet device setup helpers
[dpdk.git] / app / test-eventdev / test_perf_common.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Cavium, Inc
3  */
4
5 #include "test_perf_common.h"
6
7 int
8 perf_test_result(struct evt_test *test, struct evt_options *opt)
9 {
10         RTE_SET_USED(opt);
11         struct test_perf *t = evt_test_priv(test);
12
13         return t->result;
14 }
15
16 static inline int
17 perf_producer(void *arg)
18 {
19         struct prod_data *p  = arg;
20         struct test_perf *t = p->t;
21         struct evt_options *opt = t->opt;
22         const uint8_t dev_id = p->dev_id;
23         const uint8_t port = p->port_id;
24         struct rte_mempool *pool = t->pool;
25         const uint64_t nb_pkts = t->nb_pkts;
26         const uint32_t nb_flows = t->nb_flows;
27         uint32_t flow_counter = 0;
28         uint64_t count = 0;
29         struct perf_elt *m;
30         struct rte_event ev;
31
32         if (opt->verbose_level > 1)
33                 printf("%s(): lcore %d dev_id %d port=%d queue %d\n", __func__,
34                                 rte_lcore_id(), dev_id, port, p->queue_id);
35
36         ev.event = 0;
37         ev.op = RTE_EVENT_OP_NEW;
38         ev.queue_id = p->queue_id;
39         ev.sched_type = t->opt->sched_type_list[0];
40         ev.priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
41         ev.event_type =  RTE_EVENT_TYPE_CPU;
42         ev.sub_event_type = 0; /* stage 0 */
43
44         while (count < nb_pkts && t->done == false) {
45                 if (rte_mempool_get(pool, (void **)&m) < 0)
46                         continue;
47
48                 ev.flow_id = flow_counter++ % nb_flows;
49                 ev.event_ptr = m;
50                 m->timestamp = rte_get_timer_cycles();
51                 while (rte_event_enqueue_burst(dev_id, port, &ev, 1) != 1) {
52                         if (t->done)
53                                 break;
54                         rte_pause();
55                         m->timestamp = rte_get_timer_cycles();
56                 }
57                 count++;
58         }
59
60         return 0;
61 }
62
63 static int
64 perf_producer_wrapper(void *arg)
65 {
66         struct prod_data *p  = arg;
67         struct test_perf *t = p->t;
68         /* Launch the producer function only in case of synthetic producer. */
69         if (t->opt->prod_type == EVT_PROD_TYPE_SYNT)
70                 return perf_producer(arg);
71         return 0;
72 }
73
74 static inline uint64_t
75 processed_pkts(struct test_perf *t)
76 {
77         uint8_t i;
78         uint64_t total = 0;
79
80         rte_smp_rmb();
81         for (i = 0; i < t->nb_workers; i++)
82                 total += t->worker[i].processed_pkts;
83
84         return total;
85 }
86
87 static inline uint64_t
88 total_latency(struct test_perf *t)
89 {
90         uint8_t i;
91         uint64_t total = 0;
92
93         rte_smp_rmb();
94         for (i = 0; i < t->nb_workers; i++)
95                 total += t->worker[i].latency;
96
97         return total;
98 }
99
100
101 int
102 perf_launch_lcores(struct evt_test *test, struct evt_options *opt,
103                 int (*worker)(void *))
104 {
105         int ret, lcore_id;
106         struct test_perf *t = evt_test_priv(test);
107
108         int port_idx = 0;
109         /* launch workers */
110         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
111                 if (!(opt->wlcores[lcore_id]))
112                         continue;
113
114                 ret = rte_eal_remote_launch(worker,
115                                  &t->worker[port_idx], lcore_id);
116                 if (ret) {
117                         evt_err("failed to launch worker %d", lcore_id);
118                         return ret;
119                 }
120                 port_idx++;
121         }
122
123         /* launch producers */
124         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
125                 if (!(opt->plcores[lcore_id]))
126                         continue;
127
128                 ret = rte_eal_remote_launch(perf_producer_wrapper,
129                                 &t->prod[port_idx], lcore_id);
130                 if (ret) {
131                         evt_err("failed to launch perf_producer %d", lcore_id);
132                         return ret;
133                 }
134                 port_idx++;
135         }
136
137         const uint64_t total_pkts = opt->nb_pkts *
138                         evt_nr_active_lcores(opt->plcores);
139
140         uint64_t dead_lock_cycles = rte_get_timer_cycles();
141         int64_t dead_lock_remaining  =  total_pkts;
142         const uint64_t dead_lock_sample = rte_get_timer_hz() * 5;
143
144         uint64_t perf_cycles = rte_get_timer_cycles();
145         int64_t perf_remaining  = total_pkts;
146         const uint64_t perf_sample = rte_get_timer_hz();
147
148         static float total_mpps;
149         static uint64_t samples;
150
151         const uint64_t freq_mhz = rte_get_timer_hz() / 1000000;
152         int64_t remaining = t->outstand_pkts - processed_pkts(t);
153
154         while (t->done == false) {
155                 const uint64_t new_cycles = rte_get_timer_cycles();
156
157                 if ((new_cycles - perf_cycles) > perf_sample) {
158                         const uint64_t latency = total_latency(t);
159                         const uint64_t pkts = processed_pkts(t);
160
161                         remaining = t->outstand_pkts - pkts;
162                         float mpps = (float)(perf_remaining-remaining)/1000000;
163
164                         perf_remaining = remaining;
165                         perf_cycles = new_cycles;
166                         total_mpps += mpps;
167                         ++samples;
168                         if (opt->fwd_latency && pkts > 0) {
169                                 printf(CLGRN"\r%.3f mpps avg %.3f mpps [avg fwd latency %.3f us] "CLNRM,
170                                         mpps, total_mpps/samples,
171                                         (float)(latency/pkts)/freq_mhz);
172                         } else {
173                                 printf(CLGRN"\r%.3f mpps avg %.3f mpps"CLNRM,
174                                         mpps, total_mpps/samples);
175                         }
176                         fflush(stdout);
177
178                         if (remaining <= 0) {
179                                 t->result = EVT_TEST_SUCCESS;
180                                 if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
181                                         t->done = true;
182                                         rte_smp_wmb();
183                                         break;
184                                 }
185                         }
186                 }
187
188                 if (new_cycles - dead_lock_cycles > dead_lock_sample &&
189                                 opt->prod_type == EVT_PROD_TYPE_SYNT) {
190                         remaining = t->outstand_pkts - processed_pkts(t);
191                         if (dead_lock_remaining == remaining) {
192                                 rte_event_dev_dump(opt->dev_id, stdout);
193                                 evt_err("No schedules for seconds, deadlock");
194                                 t->done = true;
195                                 rte_smp_wmb();
196                                 break;
197                         }
198                         dead_lock_remaining = remaining;
199                         dead_lock_cycles = new_cycles;
200                 }
201         }
202         printf("\n");
203         return 0;
204 }
205
206 int
207 perf_event_dev_port_setup(struct evt_test *test, struct evt_options *opt,
208                                 uint8_t stride, uint8_t nb_queues)
209 {
210         struct test_perf *t = evt_test_priv(test);
211         uint8_t port, prod;
212         int ret = -1;
213
214         /* port configuration */
215         const struct rte_event_port_conf wkr_p_conf = {
216                         .dequeue_depth = opt->wkr_deq_dep,
217                         .enqueue_depth = 64,
218                         .new_event_threshold = 4096,
219         };
220
221         /* setup one port per worker, linking to all queues */
222         for (port = 0; port < evt_nr_active_lcores(opt->wlcores);
223                                 port++) {
224                 struct worker_data *w = &t->worker[port];
225
226                 w->dev_id = opt->dev_id;
227                 w->port_id = port;
228                 w->t = t;
229                 w->processed_pkts = 0;
230                 w->latency = 0;
231
232                 ret = rte_event_port_setup(opt->dev_id, port, &wkr_p_conf);
233                 if (ret) {
234                         evt_err("failed to setup port %d", port);
235                         return ret;
236                 }
237
238                 ret = rte_event_port_link(opt->dev_id, port, NULL, NULL, 0);
239                 if (ret != nb_queues) {
240                         evt_err("failed to link all queues to port %d", port);
241                         return -EINVAL;
242                 }
243         }
244
245         /* port for producers, no links */
246         const struct rte_event_port_conf prod_conf = {
247                         .dequeue_depth = 8,
248                         .enqueue_depth = 32,
249                         .new_event_threshold = 1200,
250         };
251         prod = 0;
252         for ( ; port < perf_nb_event_ports(opt); port++) {
253                 struct prod_data *p = &t->prod[port];
254
255                 p->dev_id = opt->dev_id;
256                 p->port_id = port;
257                 p->queue_id = prod * stride;
258                 p->t = t;
259
260                 ret = rte_event_port_setup(opt->dev_id, port, &prod_conf);
261                 if (ret) {
262                         evt_err("failed to setup port %d", port);
263                         return ret;
264                 }
265                 prod++;
266         }
267
268         return ret;
269 }
270
271 int
272 perf_opt_check(struct evt_options *opt, uint64_t nb_queues)
273 {
274         unsigned int lcores;
275
276         /* N producer + N worker + 1 master when producer cores are used
277          * Else N worker + 1 master when Rx adapter is used
278          */
279         lcores = opt->prod_type == EVT_PROD_TYPE_SYNT ? 3 : 2;
280
281         if (rte_lcore_count() < lcores) {
282                 evt_err("test need minimum %d lcores", lcores);
283                 return -1;
284         }
285
286         /* Validate worker lcores */
287         if (evt_lcores_has_overlap(opt->wlcores, rte_get_master_lcore())) {
288                 evt_err("worker lcores overlaps with master lcore");
289                 return -1;
290         }
291         if (evt_lcores_has_overlap_multi(opt->wlcores, opt->plcores)) {
292                 evt_err("worker lcores overlaps producer lcores");
293                 return -1;
294         }
295         if (evt_has_disabled_lcore(opt->wlcores)) {
296                 evt_err("one or more workers lcores are not enabled");
297                 return -1;
298         }
299         if (!evt_has_active_lcore(opt->wlcores)) {
300                 evt_err("minimum one worker is required");
301                 return -1;
302         }
303
304         if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
305                 /* Validate producer lcores */
306                 if (evt_lcores_has_overlap(opt->plcores,
307                                         rte_get_master_lcore())) {
308                         evt_err("producer lcores overlaps with master lcore");
309                         return -1;
310                 }
311                 if (evt_has_disabled_lcore(opt->plcores)) {
312                         evt_err("one or more producer lcores are not enabled");
313                         return -1;
314                 }
315                 if (!evt_has_active_lcore(opt->plcores)) {
316                         evt_err("minimum one producer is required");
317                         return -1;
318                 }
319         }
320
321         if (evt_has_invalid_stage(opt))
322                 return -1;
323
324         if (evt_has_invalid_sched_type(opt))
325                 return -1;
326
327         if (nb_queues > EVT_MAX_QUEUES) {
328                 evt_err("number of queues exceeds %d", EVT_MAX_QUEUES);
329                 return -1;
330         }
331         if (perf_nb_event_ports(opt) > EVT_MAX_PORTS) {
332                 evt_err("number of ports exceeds %d", EVT_MAX_PORTS);
333                 return -1;
334         }
335
336         /* Fixups */
337         if (opt->nb_stages == 1 && opt->fwd_latency) {
338                 evt_info("fwd_latency is valid when nb_stages > 1, disabling");
339                 opt->fwd_latency = 0;
340         }
341         if (opt->fwd_latency && !opt->q_priority) {
342                 evt_info("enabled queue priority for latency measurement");
343                 opt->q_priority = 1;
344         }
345         if (opt->nb_pkts == 0)
346                 opt->nb_pkts = INT64_MAX/evt_nr_active_lcores(opt->plcores);
347
348         return 0;
349 }
350
351 void
352 perf_opt_dump(struct evt_options *opt, uint8_t nb_queues)
353 {
354         evt_dump("nb_prod_lcores", "%d", evt_nr_active_lcores(opt->plcores));
355         evt_dump_producer_lcores(opt);
356         evt_dump("nb_worker_lcores", "%d", evt_nr_active_lcores(opt->wlcores));
357         evt_dump_worker_lcores(opt);
358         evt_dump_nb_stages(opt);
359         evt_dump("nb_evdev_ports", "%d", perf_nb_event_ports(opt));
360         evt_dump("nb_evdev_queues", "%d", nb_queues);
361         evt_dump_queue_priority(opt);
362         evt_dump_sched_type_list(opt);
363         evt_dump_producer_type(opt);
364 }
365
366 void
367 perf_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
368 {
369         RTE_SET_USED(test);
370
371         rte_event_dev_stop(opt->dev_id);
372         rte_event_dev_close(opt->dev_id);
373 }
374
375 static inline void
376 perf_elt_init(struct rte_mempool *mp, void *arg __rte_unused,
377             void *obj, unsigned i __rte_unused)
378 {
379         memset(obj, 0, mp->elt_size);
380 }
381
382 #define NB_RX_DESC                      128
383 #define NB_TX_DESC                      512
384 int
385 perf_ethdev_setup(struct evt_test *test, struct evt_options *opt)
386 {
387         int i;
388         struct test_perf *t = evt_test_priv(test);
389         struct rte_eth_conf port_conf = {
390                 .rxmode = {
391                         .mq_mode = ETH_MQ_RX_RSS,
392                         .max_rx_pkt_len = ETHER_MAX_LEN,
393                         .split_hdr_size = 0,
394                         .header_split   = 0,
395                         .hw_ip_checksum = 0,
396                         .hw_vlan_filter = 0,
397                         .hw_vlan_strip  = 0,
398                         .hw_vlan_extend = 0,
399                         .jumbo_frame    = 0,
400                         .hw_strip_crc   = 1,
401                 },
402                 .rx_adv_conf = {
403                         .rss_conf = {
404                                 .rss_key = NULL,
405                                 .rss_hf = ETH_RSS_IP,
406                         },
407                 },
408         };
409
410         if (opt->prod_type == EVT_PROD_TYPE_SYNT)
411                 return 0;
412
413         if (!rte_eth_dev_count()) {
414                 evt_err("No ethernet ports found.");
415                 return -ENODEV;
416         }
417
418         for (i = 0; i < rte_eth_dev_count(); i++) {
419
420                 if (rte_eth_dev_configure(i, 1, 1,
421                                         &port_conf)
422                                 < 0) {
423                         evt_err("Failed to configure eth port [%d]", i);
424                         return -EINVAL;
425                 }
426
427                 if (rte_eth_rx_queue_setup(i, 0, NB_RX_DESC,
428                                 rte_socket_id(), NULL, t->pool) < 0) {
429                         evt_err("Failed to setup eth port [%d] rx_queue: %d.",
430                                         i, 0);
431                         return -EINVAL;
432                 }
433
434                 if (rte_eth_tx_queue_setup(i, 0, NB_TX_DESC,
435                                         rte_socket_id(), NULL) < 0) {
436                         evt_err("Failed to setup eth port [%d] tx_queue: %d.",
437                                         i, 0);
438                         return -EINVAL;
439                 }
440
441                 rte_eth_promiscuous_enable(i);
442         }
443
444         return 0;
445 }
446
447 int
448 perf_mempool_setup(struct evt_test *test, struct evt_options *opt)
449 {
450         struct test_perf *t = evt_test_priv(test);
451
452         if (opt->prod_type == EVT_PROD_TYPE_SYNT) {
453                 t->pool = rte_mempool_create(test->name, /* mempool name */
454                                 opt->pool_sz, /* number of elements*/
455                                 sizeof(struct perf_elt), /* element size*/
456                                 512, /* cache size*/
457                                 0, NULL, NULL,
458                                 perf_elt_init, /* obj constructor */
459                                 NULL, opt->socket_id, 0); /* flags */
460         } else {
461                 t->pool = rte_pktmbuf_pool_create(test->name, /* mempool name */
462                                 opt->pool_sz, /* number of elements*/
463                                 512, /* cache size*/
464                                 0,
465                                 RTE_MBUF_DEFAULT_BUF_SIZE,
466                                 opt->socket_id); /* flags */
467
468         }
469
470         if (t->pool == NULL) {
471                 evt_err("failed to create mempool");
472                 return -ENOMEM;
473         }
474
475         return 0;
476 }
477
478 void
479 perf_mempool_destroy(struct evt_test *test, struct evt_options *opt)
480 {
481         RTE_SET_USED(opt);
482         struct test_perf *t = evt_test_priv(test);
483
484         rte_mempool_free(t->pool);
485 }
486
487 int
488 perf_test_setup(struct evt_test *test, struct evt_options *opt)
489 {
490         void *test_perf;
491
492         test_perf = rte_zmalloc_socket(test->name, sizeof(struct test_perf),
493                                 RTE_CACHE_LINE_SIZE, opt->socket_id);
494         if (test_perf  == NULL) {
495                 evt_err("failed to allocate test_perf memory");
496                 goto nomem;
497         }
498         test->test_priv = test_perf;
499
500         struct test_perf *t = evt_test_priv(test);
501
502         t->outstand_pkts = opt->nb_pkts * evt_nr_active_lcores(opt->plcores);
503         t->nb_workers = evt_nr_active_lcores(opt->wlcores);
504         t->done = false;
505         t->nb_pkts = opt->nb_pkts;
506         t->nb_flows = opt->nb_flows;
507         t->result = EVT_TEST_FAILED;
508         t->opt = opt;
509         memcpy(t->sched_type_list, opt->sched_type_list,
510                         sizeof(opt->sched_type_list));
511         return 0;
512 nomem:
513         return -ENOMEM;
514 }
515
516 void
517 perf_test_destroy(struct evt_test *test, struct evt_options *opt)
518 {
519         RTE_SET_USED(opt);
520
521         rte_free(test->test_priv);
522 }