net: add rte prefix to ether defines
[dpdk.git] / app / test-eventdev / test_pipeline_common.c
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright 2017 Cavium, Inc.
4  */
5
6 #include "test_pipeline_common.h"
7
8 int
9 pipeline_test_result(struct evt_test *test, struct evt_options *opt)
10 {
11         RTE_SET_USED(opt);
12         int i;
13         uint64_t total = 0;
14         struct test_pipeline *t = evt_test_priv(test);
15
16         evt_info("Packet distribution across worker cores :");
17         for (i = 0; i < t->nb_workers; i++)
18                 total += t->worker[i].processed_pkts;
19         for (i = 0; i < t->nb_workers; i++)
20                 evt_info("Worker %d packets: "CLGRN"%"PRIx64""CLNRM" percentage:"
21                                 CLGRN" %3.2f"CLNRM, i,
22                                 t->worker[i].processed_pkts,
23                                 (((double)t->worker[i].processed_pkts)/total)
24                                 * 100);
25         return t->result;
26 }
27
28 void
29 pipeline_opt_dump(struct evt_options *opt, uint8_t nb_queues)
30 {
31         evt_dump("nb_worker_lcores", "%d", evt_nr_active_lcores(opt->wlcores));
32         evt_dump_worker_lcores(opt);
33         evt_dump_nb_stages(opt);
34         evt_dump("nb_evdev_ports", "%d", pipeline_nb_event_ports(opt));
35         evt_dump("nb_evdev_queues", "%d", nb_queues);
36         evt_dump_queue_priority(opt);
37         evt_dump_sched_type_list(opt);
38         evt_dump_producer_type(opt);
39 }
40
41 static inline uint64_t
42 processed_pkts(struct test_pipeline *t)
43 {
44         uint8_t i;
45         uint64_t total = 0;
46
47         rte_smp_rmb();
48         for (i = 0; i < t->nb_workers; i++)
49                 total += t->worker[i].processed_pkts;
50
51         return total;
52 }
53
54 int
55 pipeline_launch_lcores(struct evt_test *test, struct evt_options *opt,
56                 int (*worker)(void *))
57 {
58         int ret, lcore_id;
59         struct test_pipeline *t = evt_test_priv(test);
60
61         int port_idx = 0;
62         /* launch workers */
63         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
64                 if (!(opt->wlcores[lcore_id]))
65                         continue;
66
67                 ret = rte_eal_remote_launch(worker,
68                                  &t->worker[port_idx], lcore_id);
69                 if (ret) {
70                         evt_err("failed to launch worker %d", lcore_id);
71                         return ret;
72                 }
73                 port_idx++;
74         }
75
76         uint64_t perf_cycles = rte_get_timer_cycles();
77         const uint64_t perf_sample = rte_get_timer_hz();
78
79         static float total_mpps;
80         static uint64_t samples;
81
82         uint64_t prev_pkts = 0;
83
84         while (t->done == false) {
85                 const uint64_t new_cycles = rte_get_timer_cycles();
86
87                 if ((new_cycles - perf_cycles) > perf_sample) {
88                         const uint64_t curr_pkts = processed_pkts(t);
89
90                         float mpps = (float)(curr_pkts - prev_pkts)/1000000;
91
92                         prev_pkts = curr_pkts;
93                         perf_cycles = new_cycles;
94                         total_mpps += mpps;
95                         ++samples;
96                         printf(CLGRN"\r%.3f mpps avg %.3f mpps"CLNRM,
97                                         mpps, total_mpps/samples);
98                         fflush(stdout);
99                 }
100         }
101         printf("\n");
102         return 0;
103 }
104
105 int
106 pipeline_opt_check(struct evt_options *opt, uint64_t nb_queues)
107 {
108         unsigned int lcores;
109         /*
110          * N worker + 1 master
111          */
112         lcores = 2;
113
114         if (!rte_eth_dev_count_avail()) {
115                 evt_err("test needs minimum 1 ethernet dev");
116                 return -1;
117         }
118
119         if (rte_lcore_count() < lcores) {
120                 evt_err("test need minimum %d lcores", lcores);
121                 return -1;
122         }
123
124         /* Validate worker lcores */
125         if (evt_lcores_has_overlap(opt->wlcores, rte_get_master_lcore())) {
126                 evt_err("worker lcores overlaps with master lcore");
127                 return -1;
128         }
129         if (evt_has_disabled_lcore(opt->wlcores)) {
130                 evt_err("one or more workers lcores are not enabled");
131                 return -1;
132         }
133         if (!evt_has_active_lcore(opt->wlcores)) {
134                 evt_err("minimum one worker is required");
135                 return -1;
136         }
137
138         if (nb_queues > EVT_MAX_QUEUES) {
139                 evt_err("number of queues exceeds %d", EVT_MAX_QUEUES);
140                 return -1;
141         }
142         if (pipeline_nb_event_ports(opt) > EVT_MAX_PORTS) {
143                 evt_err("number of ports exceeds %d", EVT_MAX_PORTS);
144                 return -1;
145         }
146
147         if (evt_has_invalid_stage(opt))
148                 return -1;
149
150         if (evt_has_invalid_sched_type(opt))
151                 return -1;
152
153         return 0;
154 }
155
156 #define NB_RX_DESC                      128
157 #define NB_TX_DESC                      512
158 int
159 pipeline_ethdev_setup(struct evt_test *test, struct evt_options *opt)
160 {
161         uint16_t i;
162         uint8_t nb_queues = 1;
163         struct test_pipeline *t = evt_test_priv(test);
164         struct rte_eth_rxconf rx_conf;
165         struct rte_eth_conf port_conf = {
166                 .rxmode = {
167                         .mq_mode = ETH_MQ_RX_RSS,
168                         .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
169                 },
170                 .rx_adv_conf = {
171                         .rss_conf = {
172                                 .rss_key = NULL,
173                                 .rss_hf = ETH_RSS_IP,
174                         },
175                 },
176         };
177
178         RTE_SET_USED(opt);
179         if (!rte_eth_dev_count_avail()) {
180                 evt_err("No ethernet ports found.");
181                 return -ENODEV;
182         }
183
184         t->internal_port = 1;
185         RTE_ETH_FOREACH_DEV(i) {
186                 struct rte_eth_dev_info dev_info;
187                 struct rte_eth_conf local_port_conf = port_conf;
188                 uint32_t caps = 0;
189
190                 rte_event_eth_tx_adapter_caps_get(opt->dev_id, i, &caps);
191                 if (!(caps & RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT))
192                         t->internal_port = 0;
193
194                 rte_eth_dev_info_get(i, &dev_info);
195                 rx_conf = dev_info.default_rxconf;
196                 rx_conf.offloads = port_conf.rxmode.offloads;
197
198                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
199                         dev_info.flow_type_rss_offloads;
200                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
201                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
202                         evt_info("Port %u modified RSS hash function based on hardware support,"
203                                 "requested:%#"PRIx64" configured:%#"PRIx64"",
204                                 i,
205                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
206                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
207                 }
208
209                 if (rte_eth_dev_configure(i, nb_queues, nb_queues,
210                                         &local_port_conf)
211                                 < 0) {
212                         evt_err("Failed to configure eth port [%d]", i);
213                         return -EINVAL;
214                 }
215
216                 if (rte_eth_rx_queue_setup(i, 0, NB_RX_DESC,
217                                 rte_socket_id(), &rx_conf, t->pool) < 0) {
218                         evt_err("Failed to setup eth port [%d] rx_queue: %d.",
219                                         i, 0);
220                         return -EINVAL;
221                 }
222                 if (rte_eth_tx_queue_setup(i, 0, NB_TX_DESC,
223                                         rte_socket_id(), NULL) < 0) {
224                         evt_err("Failed to setup eth port [%d] tx_queue: %d.",
225                                         i, 0);
226                         return -EINVAL;
227                 }
228
229                 rte_eth_promiscuous_enable(i);
230         }
231
232         return 0;
233 }
234
235 int
236 pipeline_event_port_setup(struct evt_test *test, struct evt_options *opt,
237                 uint8_t *queue_arr, uint8_t nb_queues,
238                 const struct rte_event_port_conf p_conf)
239 {
240         int ret;
241         uint8_t port;
242         struct test_pipeline *t = evt_test_priv(test);
243
244
245         /* setup one port per worker, linking to all queues */
246         for (port = 0; port < evt_nr_active_lcores(opt->wlcores); port++) {
247                 struct worker_data *w = &t->worker[port];
248
249                 w->dev_id = opt->dev_id;
250                 w->port_id = port;
251                 w->t = t;
252                 w->processed_pkts = 0;
253
254                 ret = rte_event_port_setup(opt->dev_id, port, &p_conf);
255                 if (ret) {
256                         evt_err("failed to setup port %d", port);
257                         return ret;
258                 }
259
260                 if (rte_event_port_link(opt->dev_id, port, queue_arr, NULL,
261                                         nb_queues) != nb_queues)
262                         goto link_fail;
263         }
264
265         return 0;
266
267 link_fail:
268         evt_err("failed to link queues to port %d", port);
269         return -EINVAL;
270 }
271
272 int
273 pipeline_event_rx_adapter_setup(struct evt_options *opt, uint8_t stride,
274                 struct rte_event_port_conf prod_conf)
275 {
276         int ret = 0;
277         uint16_t prod;
278         struct rte_event_eth_rx_adapter_queue_conf queue_conf;
279
280         memset(&queue_conf, 0,
281                         sizeof(struct rte_event_eth_rx_adapter_queue_conf));
282         queue_conf.ev.sched_type = opt->sched_type_list[0];
283         RTE_ETH_FOREACH_DEV(prod) {
284                 uint32_t cap;
285
286                 ret = rte_event_eth_rx_adapter_caps_get(opt->dev_id,
287                                 prod, &cap);
288                 if (ret) {
289                         evt_err("failed to get event rx adapter[%d]"
290                                         " capabilities",
291                                         opt->dev_id);
292                         return ret;
293                 }
294                 queue_conf.ev.queue_id = prod * stride;
295                 ret = rte_event_eth_rx_adapter_create(prod, opt->dev_id,
296                                 &prod_conf);
297                 if (ret) {
298                         evt_err("failed to create rx adapter[%d]", prod);
299                         return ret;
300                 }
301                 ret = rte_event_eth_rx_adapter_queue_add(prod, prod, -1,
302                                 &queue_conf);
303                 if (ret) {
304                         evt_err("failed to add rx queues to adapter[%d]", prod);
305                         return ret;
306                 }
307
308                 if (!(cap & RTE_EVENT_ETH_RX_ADAPTER_CAP_INTERNAL_PORT)) {
309                         uint32_t service_id;
310
311                         rte_event_eth_rx_adapter_service_id_get(prod,
312                                         &service_id);
313                         ret = evt_service_setup(service_id);
314                         if (ret) {
315                                 evt_err("Failed to setup service core"
316                                                 " for Rx adapter");
317                                 return ret;
318                         }
319                 }
320
321                 evt_info("Port[%d] using Rx adapter[%d] configured", prod,
322                                 prod);
323         }
324
325         return ret;
326 }
327
328 int
329 pipeline_event_tx_adapter_setup(struct evt_options *opt,
330                 struct rte_event_port_conf port_conf)
331 {
332         int ret = 0;
333         uint16_t consm;
334
335         RTE_ETH_FOREACH_DEV(consm) {
336                 uint32_t cap;
337
338                 ret = rte_event_eth_tx_adapter_caps_get(opt->dev_id,
339                                 consm, &cap);
340                 if (ret) {
341                         evt_err("failed to get event tx adapter[%d] caps",
342                                         consm);
343                         return ret;
344                 }
345
346                 ret = rte_event_eth_tx_adapter_create(consm, opt->dev_id,
347                                 &port_conf);
348                 if (ret) {
349                         evt_err("failed to create tx adapter[%d]", consm);
350                         return ret;
351                 }
352
353                 ret = rte_event_eth_tx_adapter_queue_add(consm, consm, -1);
354                 if (ret) {
355                         evt_err("failed to add tx queues to adapter[%d]",
356                                         consm);
357                         return ret;
358                 }
359
360                 if (!(cap & RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT)) {
361                         uint32_t service_id;
362
363                         rte_event_eth_tx_adapter_service_id_get(consm,
364                                         &service_id);
365                         ret = evt_service_setup(service_id);
366                         if (ret) {
367                                 evt_err("Failed to setup service core"
368                                                 " for Tx adapter\n");
369                                 return ret;
370                         }
371                 }
372
373                 evt_info("Port[%d] using Tx adapter[%d] Configured", consm,
374                                 consm);
375         }
376
377         return ret;
378 }
379
380 void
381 pipeline_ethdev_destroy(struct evt_test *test, struct evt_options *opt)
382 {
383         uint16_t i;
384         RTE_SET_USED(test);
385         RTE_SET_USED(opt);
386
387         RTE_ETH_FOREACH_DEV(i) {
388                 rte_event_eth_rx_adapter_stop(i);
389                 rte_event_eth_tx_adapter_stop(i);
390                 rte_eth_dev_stop(i);
391         }
392 }
393
394 void
395 pipeline_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
396 {
397         RTE_SET_USED(test);
398
399         rte_event_dev_stop(opt->dev_id);
400         rte_event_dev_close(opt->dev_id);
401 }
402
403 int
404 pipeline_mempool_setup(struct evt_test *test, struct evt_options *opt)
405 {
406         struct test_pipeline *t = evt_test_priv(test);
407
408         t->pool = rte_pktmbuf_pool_create(test->name, /* mempool name */
409                         opt->pool_sz, /* number of elements*/
410                         512, /* cache size*/
411                         0,
412                         RTE_MBUF_DEFAULT_BUF_SIZE,
413                         opt->socket_id); /* flags */
414
415         if (t->pool == NULL) {
416                 evt_err("failed to create mempool");
417                 return -ENOMEM;
418         }
419
420         return 0;
421 }
422
423 void
424 pipeline_mempool_destroy(struct evt_test *test, struct evt_options *opt)
425 {
426         RTE_SET_USED(opt);
427         struct test_pipeline *t = evt_test_priv(test);
428
429         rte_mempool_free(t->pool);
430 }
431
432 int
433 pipeline_test_setup(struct evt_test *test, struct evt_options *opt)
434 {
435         void *test_pipeline;
436
437         test_pipeline = rte_zmalloc_socket(test->name,
438                         sizeof(struct test_pipeline), RTE_CACHE_LINE_SIZE,
439                         opt->socket_id);
440         if (test_pipeline  == NULL) {
441                 evt_err("failed to allocate test_pipeline memory");
442                 goto nomem;
443         }
444         test->test_priv = test_pipeline;
445
446         struct test_pipeline *t = evt_test_priv(test);
447
448         t->nb_workers = evt_nr_active_lcores(opt->wlcores);
449         t->outstand_pkts = opt->nb_pkts * evt_nr_active_lcores(opt->wlcores);
450         t->done = false;
451         t->nb_flows = opt->nb_flows;
452         t->result = EVT_TEST_FAILED;
453         t->opt = opt;
454         opt->prod_type = EVT_PROD_TYPE_ETH_RX_ADPTR;
455         memcpy(t->sched_type_list, opt->sched_type_list,
456                         sizeof(opt->sched_type_list));
457         return 0;
458 nomem:
459         return -ENOMEM;
460 }
461
462 void
463 pipeline_test_destroy(struct evt_test *test, struct evt_options *opt)
464 {
465         RTE_SET_USED(opt);
466
467         rte_free(test->test_priv);
468 }