xref: /dpdk/app/test-eventdev/test_pipeline_common.c (revision 089e5ed727a15da2729cfee9b63533dd120bd04c)
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 	int ret;
163 	uint8_t nb_queues = 1;
164 	struct test_pipeline *t = evt_test_priv(test);
165 	struct rte_eth_rxconf rx_conf;
166 	struct rte_eth_conf port_conf = {
167 		.rxmode = {
168 			.mq_mode = ETH_MQ_RX_RSS,
169 			.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
170 		},
171 		.rx_adv_conf = {
172 			.rss_conf = {
173 				.rss_key = NULL,
174 				.rss_hf = ETH_RSS_IP,
175 			},
176 		},
177 	};
178 
179 	RTE_SET_USED(opt);
180 	if (!rte_eth_dev_count_avail()) {
181 		evt_err("No ethernet ports found.");
182 		return -ENODEV;
183 	}
184 
185 	t->internal_port = 1;
186 	RTE_ETH_FOREACH_DEV(i) {
187 		struct rte_eth_dev_info dev_info;
188 		struct rte_eth_conf local_port_conf = port_conf;
189 		uint32_t caps = 0;
190 
191 		rte_event_eth_tx_adapter_caps_get(opt->dev_id, i, &caps);
192 		if (!(caps & RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT))
193 			t->internal_port = 0;
194 
195 		ret = rte_eth_dev_info_get(i, &dev_info);
196 		if (ret != 0) {
197 			evt_err("Error during getting device (port %u) info: %s\n",
198 				i, strerror(-ret));
199 			return ret;
200 		}
201 
202 		rx_conf = dev_info.default_rxconf;
203 		rx_conf.offloads = port_conf.rxmode.offloads;
204 
205 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
206 			dev_info.flow_type_rss_offloads;
207 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
208 				port_conf.rx_adv_conf.rss_conf.rss_hf) {
209 			evt_info("Port %u modified RSS hash function based on hardware support,"
210 				"requested:%#"PRIx64" configured:%#"PRIx64"",
211 				i,
212 				port_conf.rx_adv_conf.rss_conf.rss_hf,
213 				local_port_conf.rx_adv_conf.rss_conf.rss_hf);
214 		}
215 
216 		if (rte_eth_dev_configure(i, nb_queues, nb_queues,
217 					&local_port_conf)
218 				< 0) {
219 			evt_err("Failed to configure eth port [%d]", i);
220 			return -EINVAL;
221 		}
222 
223 		if (rte_eth_rx_queue_setup(i, 0, NB_RX_DESC,
224 				rte_socket_id(), &rx_conf, t->pool) < 0) {
225 			evt_err("Failed to setup eth port [%d] rx_queue: %d.",
226 					i, 0);
227 			return -EINVAL;
228 		}
229 		if (rte_eth_tx_queue_setup(i, 0, NB_TX_DESC,
230 					rte_socket_id(), NULL) < 0) {
231 			evt_err("Failed to setup eth port [%d] tx_queue: %d.",
232 					i, 0);
233 			return -EINVAL;
234 		}
235 
236 		rte_eth_promiscuous_enable(i);
237 	}
238 
239 	return 0;
240 }
241 
242 int
243 pipeline_event_port_setup(struct evt_test *test, struct evt_options *opt,
244 		uint8_t *queue_arr, uint8_t nb_queues,
245 		const struct rte_event_port_conf p_conf)
246 {
247 	int ret;
248 	uint8_t port;
249 	struct test_pipeline *t = evt_test_priv(test);
250 
251 
252 	/* setup one port per worker, linking to all queues */
253 	for (port = 0; port < evt_nr_active_lcores(opt->wlcores); port++) {
254 		struct worker_data *w = &t->worker[port];
255 
256 		w->dev_id = opt->dev_id;
257 		w->port_id = port;
258 		w->t = t;
259 		w->processed_pkts = 0;
260 
261 		ret = rte_event_port_setup(opt->dev_id, port, &p_conf);
262 		if (ret) {
263 			evt_err("failed to setup port %d", port);
264 			return ret;
265 		}
266 
267 		if (rte_event_port_link(opt->dev_id, port, queue_arr, NULL,
268 					nb_queues) != nb_queues)
269 			goto link_fail;
270 	}
271 
272 	return 0;
273 
274 link_fail:
275 	evt_err("failed to link queues to port %d", port);
276 	return -EINVAL;
277 }
278 
279 int
280 pipeline_event_rx_adapter_setup(struct evt_options *opt, uint8_t stride,
281 		struct rte_event_port_conf prod_conf)
282 {
283 	int ret = 0;
284 	uint16_t prod;
285 	struct rte_event_eth_rx_adapter_queue_conf queue_conf;
286 
287 	memset(&queue_conf, 0,
288 			sizeof(struct rte_event_eth_rx_adapter_queue_conf));
289 	queue_conf.ev.sched_type = opt->sched_type_list[0];
290 	RTE_ETH_FOREACH_DEV(prod) {
291 		uint32_t cap;
292 
293 		ret = rte_event_eth_rx_adapter_caps_get(opt->dev_id,
294 				prod, &cap);
295 		if (ret) {
296 			evt_err("failed to get event rx adapter[%d]"
297 					" capabilities",
298 					opt->dev_id);
299 			return ret;
300 		}
301 		queue_conf.ev.queue_id = prod * stride;
302 		ret = rte_event_eth_rx_adapter_create(prod, opt->dev_id,
303 				&prod_conf);
304 		if (ret) {
305 			evt_err("failed to create rx adapter[%d]", prod);
306 			return ret;
307 		}
308 		ret = rte_event_eth_rx_adapter_queue_add(prod, prod, -1,
309 				&queue_conf);
310 		if (ret) {
311 			evt_err("failed to add rx queues to adapter[%d]", prod);
312 			return ret;
313 		}
314 
315 		if (!(cap & RTE_EVENT_ETH_RX_ADAPTER_CAP_INTERNAL_PORT)) {
316 			uint32_t service_id;
317 
318 			rte_event_eth_rx_adapter_service_id_get(prod,
319 					&service_id);
320 			ret = evt_service_setup(service_id);
321 			if (ret) {
322 				evt_err("Failed to setup service core"
323 						" for Rx adapter");
324 				return ret;
325 			}
326 		}
327 
328 		evt_info("Port[%d] using Rx adapter[%d] configured", prod,
329 				prod);
330 	}
331 
332 	return ret;
333 }
334 
335 int
336 pipeline_event_tx_adapter_setup(struct evt_options *opt,
337 		struct rte_event_port_conf port_conf)
338 {
339 	int ret = 0;
340 	uint16_t consm;
341 
342 	RTE_ETH_FOREACH_DEV(consm) {
343 		uint32_t cap;
344 
345 		ret = rte_event_eth_tx_adapter_caps_get(opt->dev_id,
346 				consm, &cap);
347 		if (ret) {
348 			evt_err("failed to get event tx adapter[%d] caps",
349 					consm);
350 			return ret;
351 		}
352 
353 		ret = rte_event_eth_tx_adapter_create(consm, opt->dev_id,
354 				&port_conf);
355 		if (ret) {
356 			evt_err("failed to create tx adapter[%d]", consm);
357 			return ret;
358 		}
359 
360 		ret = rte_event_eth_tx_adapter_queue_add(consm, consm, -1);
361 		if (ret) {
362 			evt_err("failed to add tx queues to adapter[%d]",
363 					consm);
364 			return ret;
365 		}
366 
367 		if (!(cap & RTE_EVENT_ETH_TX_ADAPTER_CAP_INTERNAL_PORT)) {
368 			uint32_t service_id;
369 
370 			rte_event_eth_tx_adapter_service_id_get(consm,
371 					&service_id);
372 			ret = evt_service_setup(service_id);
373 			if (ret) {
374 				evt_err("Failed to setup service core"
375 						" for Tx adapter\n");
376 				return ret;
377 			}
378 		}
379 
380 		evt_info("Port[%d] using Tx adapter[%d] Configured", consm,
381 				consm);
382 	}
383 
384 	return ret;
385 }
386 
387 void
388 pipeline_ethdev_destroy(struct evt_test *test, struct evt_options *opt)
389 {
390 	uint16_t i;
391 	RTE_SET_USED(test);
392 	RTE_SET_USED(opt);
393 
394 	RTE_ETH_FOREACH_DEV(i) {
395 		rte_event_eth_rx_adapter_stop(i);
396 		rte_event_eth_tx_adapter_stop(i);
397 		rte_eth_dev_stop(i);
398 	}
399 }
400 
401 void
402 pipeline_eventdev_destroy(struct evt_test *test, struct evt_options *opt)
403 {
404 	RTE_SET_USED(test);
405 
406 	rte_event_dev_stop(opt->dev_id);
407 	rte_event_dev_close(opt->dev_id);
408 }
409 
410 int
411 pipeline_mempool_setup(struct evt_test *test, struct evt_options *opt)
412 {
413 	struct test_pipeline *t = evt_test_priv(test);
414 
415 	t->pool = rte_pktmbuf_pool_create(test->name, /* mempool name */
416 			opt->pool_sz, /* number of elements*/
417 			512, /* cache size*/
418 			0,
419 			RTE_MBUF_DEFAULT_BUF_SIZE,
420 			opt->socket_id); /* flags */
421 
422 	if (t->pool == NULL) {
423 		evt_err("failed to create mempool");
424 		return -ENOMEM;
425 	}
426 
427 	return 0;
428 }
429 
430 void
431 pipeline_mempool_destroy(struct evt_test *test, struct evt_options *opt)
432 {
433 	RTE_SET_USED(opt);
434 	struct test_pipeline *t = evt_test_priv(test);
435 
436 	rte_mempool_free(t->pool);
437 }
438 
439 int
440 pipeline_test_setup(struct evt_test *test, struct evt_options *opt)
441 {
442 	void *test_pipeline;
443 
444 	test_pipeline = rte_zmalloc_socket(test->name,
445 			sizeof(struct test_pipeline), RTE_CACHE_LINE_SIZE,
446 			opt->socket_id);
447 	if (test_pipeline  == NULL) {
448 		evt_err("failed to allocate test_pipeline memory");
449 		goto nomem;
450 	}
451 	test->test_priv = test_pipeline;
452 
453 	struct test_pipeline *t = evt_test_priv(test);
454 
455 	t->nb_workers = evt_nr_active_lcores(opt->wlcores);
456 	t->outstand_pkts = opt->nb_pkts * evt_nr_active_lcores(opt->wlcores);
457 	t->done = false;
458 	t->nb_flows = opt->nb_flows;
459 	t->result = EVT_TEST_FAILED;
460 	t->opt = opt;
461 	opt->prod_type = EVT_PROD_TYPE_ETH_RX_ADPTR;
462 	memcpy(t->sched_type_list, opt->sched_type_list,
463 			sizeof(opt->sched_type_list));
464 	return 0;
465 nomem:
466 	return -ENOMEM;
467 }
468 
469 void
470 pipeline_test_destroy(struct evt_test *test, struct evt_options *opt)
471 {
472 	RTE_SET_USED(opt);
473 
474 	rte_free(test->test_priv);
475 }
476