xref: /dpdk/examples/ip_pipeline/link.c (revision 2e1141b6f16fbe41ed2da812e3a03810c5724ae7)
1133c2c65SJasvinder Singh /* SPDX-License-Identifier: BSD-3-Clause
2133c2c65SJasvinder Singh  * Copyright(c) 2010-2018 Intel Corporation
3133c2c65SJasvinder Singh  */
4133c2c65SJasvinder Singh 
5133c2c65SJasvinder Singh #include <stdlib.h>
6133c2c65SJasvinder Singh #include <string.h>
7133c2c65SJasvinder Singh 
8133c2c65SJasvinder Singh #include <rte_ethdev.h>
97959831bSJasvinder Singh #include <rte_string_fns.h>
10133c2c65SJasvinder Singh 
11133c2c65SJasvinder Singh #include "link.h"
12133c2c65SJasvinder Singh #include "mempool.h"
13133c2c65SJasvinder Singh 
14133c2c65SJasvinder Singh static struct link_list link_list;
15133c2c65SJasvinder Singh 
16133c2c65SJasvinder Singh int
17133c2c65SJasvinder Singh link_init(void)
18133c2c65SJasvinder Singh {
19133c2c65SJasvinder Singh 	TAILQ_INIT(&link_list);
20133c2c65SJasvinder Singh 
21133c2c65SJasvinder Singh 	return 0;
22133c2c65SJasvinder Singh }
23133c2c65SJasvinder Singh 
24133c2c65SJasvinder Singh struct link *
25133c2c65SJasvinder Singh link_find(const char *name)
26133c2c65SJasvinder Singh {
27133c2c65SJasvinder Singh 	struct link *link;
28133c2c65SJasvinder Singh 
29133c2c65SJasvinder Singh 	if (name == NULL)
30133c2c65SJasvinder Singh 		return NULL;
31133c2c65SJasvinder Singh 
32133c2c65SJasvinder Singh 	TAILQ_FOREACH(link, &link_list, node)
33133c2c65SJasvinder Singh 		if (strcmp(link->name, name) == 0)
34133c2c65SJasvinder Singh 			return link;
35133c2c65SJasvinder Singh 
36133c2c65SJasvinder Singh 	return NULL;
37133c2c65SJasvinder Singh }
38133c2c65SJasvinder Singh 
39ecfc2b1cSKevin Laatz struct link *
40ecfc2b1cSKevin Laatz link_next(struct link *link)
41ecfc2b1cSKevin Laatz {
42ecfc2b1cSKevin Laatz 	return (link == NULL) ? TAILQ_FIRST(&link_list) : TAILQ_NEXT(link, node);
43ecfc2b1cSKevin Laatz }
44ecfc2b1cSKevin Laatz 
45133c2c65SJasvinder Singh static struct rte_eth_conf port_conf_default = {
46133c2c65SJasvinder Singh 	.link_speeds = 0,
47133c2c65SJasvinder Singh 	.rxmode = {
48133c2c65SJasvinder Singh 		.mq_mode = ETH_MQ_RX_NONE,
49133c2c65SJasvinder Singh 		.max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
50133c2c65SJasvinder Singh 		.split_hdr_size = 0, /* Header split buffer size */
51c6698a3eSThomas Monjalon 		.offloads = DEV_RX_OFFLOAD_CRC_STRIP,
52133c2c65SJasvinder Singh 	},
53133c2c65SJasvinder Singh 	.rx_adv_conf = {
54133c2c65SJasvinder Singh 		.rss_conf = {
55133c2c65SJasvinder Singh 			.rss_key = NULL,
56133c2c65SJasvinder Singh 			.rss_key_len = 40,
57133c2c65SJasvinder Singh 			.rss_hf = 0,
58133c2c65SJasvinder Singh 		},
59133c2c65SJasvinder Singh 	},
60133c2c65SJasvinder Singh 	.txmode = {
61133c2c65SJasvinder Singh 		.mq_mode = ETH_MQ_TX_NONE,
62133c2c65SJasvinder Singh 	},
63133c2c65SJasvinder Singh 	.lpbk_mode = 0,
64133c2c65SJasvinder Singh };
65133c2c65SJasvinder Singh 
66133c2c65SJasvinder Singh #define RETA_CONF_SIZE     (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
67133c2c65SJasvinder Singh 
68133c2c65SJasvinder Singh static int
69133c2c65SJasvinder Singh rss_setup(uint16_t port_id,
70133c2c65SJasvinder Singh 	uint16_t reta_size,
71133c2c65SJasvinder Singh 	struct link_params_rss *rss)
72133c2c65SJasvinder Singh {
73133c2c65SJasvinder Singh 	struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
74133c2c65SJasvinder Singh 	uint32_t i;
75133c2c65SJasvinder Singh 	int status;
76133c2c65SJasvinder Singh 
77133c2c65SJasvinder Singh 	/* RETA setting */
78133c2c65SJasvinder Singh 	memset(reta_conf, 0, sizeof(reta_conf));
79133c2c65SJasvinder Singh 
80133c2c65SJasvinder Singh 	for (i = 0; i < reta_size; i++)
81133c2c65SJasvinder Singh 		reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
82133c2c65SJasvinder Singh 
83133c2c65SJasvinder Singh 	for (i = 0; i < reta_size; i++) {
84133c2c65SJasvinder Singh 		uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
85133c2c65SJasvinder Singh 		uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
86133c2c65SJasvinder Singh 		uint32_t rss_qs_pos = i % rss->n_queues;
87133c2c65SJasvinder Singh 
88133c2c65SJasvinder Singh 		reta_conf[reta_id].reta[reta_pos] =
89133c2c65SJasvinder Singh 			(uint16_t) rss->queue_id[rss_qs_pos];
90133c2c65SJasvinder Singh 	}
91133c2c65SJasvinder Singh 
92133c2c65SJasvinder Singh 	/* RETA update */
93133c2c65SJasvinder Singh 	status = rte_eth_dev_rss_reta_update(port_id,
94133c2c65SJasvinder Singh 		reta_conf,
95133c2c65SJasvinder Singh 		reta_size);
96133c2c65SJasvinder Singh 
97133c2c65SJasvinder Singh 	return status;
98133c2c65SJasvinder Singh }
99133c2c65SJasvinder Singh 
100133c2c65SJasvinder Singh struct link *
101133c2c65SJasvinder Singh link_create(const char *name, struct link_params *params)
102133c2c65SJasvinder Singh {
103133c2c65SJasvinder Singh 	struct rte_eth_dev_info port_info;
104133c2c65SJasvinder Singh 	struct rte_eth_conf port_conf;
105133c2c65SJasvinder Singh 	struct link *link;
106133c2c65SJasvinder Singh 	struct link_params_rss *rss;
107133c2c65SJasvinder Singh 	struct mempool *mempool;
108133c2c65SJasvinder Singh 	uint32_t cpu_id, i;
109133c2c65SJasvinder Singh 	int status;
110133c2c65SJasvinder Singh 	uint16_t port_id;
111133c2c65SJasvinder Singh 
112133c2c65SJasvinder Singh 	/* Check input params */
113133c2c65SJasvinder Singh 	if ((name == NULL) ||
114133c2c65SJasvinder Singh 		link_find(name) ||
115133c2c65SJasvinder Singh 		(params == NULL) ||
116133c2c65SJasvinder Singh 		(params->rx.n_queues == 0) ||
117133c2c65SJasvinder Singh 		(params->rx.queue_size == 0) ||
118133c2c65SJasvinder Singh 		(params->tx.n_queues == 0) ||
119133c2c65SJasvinder Singh 		(params->tx.queue_size == 0))
120133c2c65SJasvinder Singh 		return NULL;
121133c2c65SJasvinder Singh 
122133c2c65SJasvinder Singh 	port_id = params->port_id;
123133c2c65SJasvinder Singh 	if (params->dev_name) {
124133c2c65SJasvinder Singh 		status = rte_eth_dev_get_port_by_name(params->dev_name,
125133c2c65SJasvinder Singh 			&port_id);
126133c2c65SJasvinder Singh 
127133c2c65SJasvinder Singh 		if (status)
128133c2c65SJasvinder Singh 			return NULL;
129133c2c65SJasvinder Singh 	} else
130133c2c65SJasvinder Singh 		if (!rte_eth_dev_is_valid_port(port_id))
131133c2c65SJasvinder Singh 			return NULL;
132133c2c65SJasvinder Singh 
133133c2c65SJasvinder Singh 	rte_eth_dev_info_get(port_id, &port_info);
134133c2c65SJasvinder Singh 
135133c2c65SJasvinder Singh 	mempool = mempool_find(params->rx.mempool_name);
136133c2c65SJasvinder Singh 	if (mempool == NULL)
137133c2c65SJasvinder Singh 		return NULL;
138133c2c65SJasvinder Singh 
139133c2c65SJasvinder Singh 	rss = params->rx.rss;
140133c2c65SJasvinder Singh 	if (rss) {
141133c2c65SJasvinder Singh 		if ((port_info.reta_size == 0) ||
142133c2c65SJasvinder Singh 			(port_info.reta_size > ETH_RSS_RETA_SIZE_512))
143133c2c65SJasvinder Singh 			return NULL;
144133c2c65SJasvinder Singh 
145133c2c65SJasvinder Singh 		if ((rss->n_queues == 0) ||
146133c2c65SJasvinder Singh 			(rss->n_queues >= LINK_RXQ_RSS_MAX))
147133c2c65SJasvinder Singh 			return NULL;
148133c2c65SJasvinder Singh 
149133c2c65SJasvinder Singh 		for (i = 0; i < rss->n_queues; i++)
150133c2c65SJasvinder Singh 			if (rss->queue_id[i] >= port_info.max_rx_queues)
151133c2c65SJasvinder Singh 				return NULL;
152133c2c65SJasvinder Singh 	}
153133c2c65SJasvinder Singh 
154133c2c65SJasvinder Singh 	/**
155133c2c65SJasvinder Singh 	 * Resource create
156133c2c65SJasvinder Singh 	 */
157133c2c65SJasvinder Singh 	/* Port */
158133c2c65SJasvinder Singh 	memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
159133c2c65SJasvinder Singh 	if (rss) {
160133c2c65SJasvinder Singh 		port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
161*2e1141b6SCristian Dumitrescu 		port_conf.rx_adv_conf.rss_conf.rss_hf =
162*2e1141b6SCristian Dumitrescu 			(ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) &
163*2e1141b6SCristian Dumitrescu 			port_info.flow_type_rss_offloads;
164133c2c65SJasvinder Singh 	}
165133c2c65SJasvinder Singh 
166133c2c65SJasvinder Singh 	cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
167133c2c65SJasvinder Singh 	if (cpu_id == (uint32_t) SOCKET_ID_ANY)
168133c2c65SJasvinder Singh 		cpu_id = 0;
169133c2c65SJasvinder Singh 
170133c2c65SJasvinder Singh 	status = rte_eth_dev_configure(
171133c2c65SJasvinder Singh 		port_id,
172133c2c65SJasvinder Singh 		params->rx.n_queues,
173133c2c65SJasvinder Singh 		params->tx.n_queues,
174133c2c65SJasvinder Singh 		&port_conf);
175133c2c65SJasvinder Singh 
176133c2c65SJasvinder Singh 	if (status < 0)
177133c2c65SJasvinder Singh 		return NULL;
178133c2c65SJasvinder Singh 
179133c2c65SJasvinder Singh 	if (params->promiscuous)
180133c2c65SJasvinder Singh 		rte_eth_promiscuous_enable(port_id);
181133c2c65SJasvinder Singh 
182133c2c65SJasvinder Singh 	/* Port RX */
183133c2c65SJasvinder Singh 	for (i = 0; i < params->rx.n_queues; i++) {
184133c2c65SJasvinder Singh 		status = rte_eth_rx_queue_setup(
185133c2c65SJasvinder Singh 			port_id,
186133c2c65SJasvinder Singh 			i,
187133c2c65SJasvinder Singh 			params->rx.queue_size,
188133c2c65SJasvinder Singh 			cpu_id,
189133c2c65SJasvinder Singh 			NULL,
190133c2c65SJasvinder Singh 			mempool->m);
191133c2c65SJasvinder Singh 
192133c2c65SJasvinder Singh 		if (status < 0)
193133c2c65SJasvinder Singh 			return NULL;
194133c2c65SJasvinder Singh 	}
195133c2c65SJasvinder Singh 
196133c2c65SJasvinder Singh 	/* Port TX */
197133c2c65SJasvinder Singh 	for (i = 0; i < params->tx.n_queues; i++) {
198133c2c65SJasvinder Singh 		status = rte_eth_tx_queue_setup(
199133c2c65SJasvinder Singh 			port_id,
200133c2c65SJasvinder Singh 			i,
201133c2c65SJasvinder Singh 			params->tx.queue_size,
202133c2c65SJasvinder Singh 			cpu_id,
203133c2c65SJasvinder Singh 			NULL);
204133c2c65SJasvinder Singh 
205133c2c65SJasvinder Singh 		if (status < 0)
206133c2c65SJasvinder Singh 			return NULL;
207133c2c65SJasvinder Singh 	}
208133c2c65SJasvinder Singh 
209133c2c65SJasvinder Singh 	/* Port start */
210133c2c65SJasvinder Singh 	status = rte_eth_dev_start(port_id);
211133c2c65SJasvinder Singh 	if (status < 0)
212133c2c65SJasvinder Singh 		return NULL;
213133c2c65SJasvinder Singh 
214133c2c65SJasvinder Singh 	if (rss) {
215133c2c65SJasvinder Singh 		status = rss_setup(port_id, port_info.reta_size, rss);
216133c2c65SJasvinder Singh 
217133c2c65SJasvinder Singh 		if (status) {
218133c2c65SJasvinder Singh 			rte_eth_dev_stop(port_id);
219133c2c65SJasvinder Singh 			return NULL;
220133c2c65SJasvinder Singh 		}
221133c2c65SJasvinder Singh 	}
222133c2c65SJasvinder Singh 
223133c2c65SJasvinder Singh 	/* Port link up */
224133c2c65SJasvinder Singh 	status = rte_eth_dev_set_link_up(port_id);
225133c2c65SJasvinder Singh 	if ((status < 0) && (status != -ENOTSUP)) {
226133c2c65SJasvinder Singh 		rte_eth_dev_stop(port_id);
227133c2c65SJasvinder Singh 		return NULL;
228133c2c65SJasvinder Singh 	}
229133c2c65SJasvinder Singh 
230133c2c65SJasvinder Singh 	/* Node allocation */
231133c2c65SJasvinder Singh 	link = calloc(1, sizeof(struct link));
232133c2c65SJasvinder Singh 	if (link == NULL) {
233133c2c65SJasvinder Singh 		rte_eth_dev_stop(port_id);
234133c2c65SJasvinder Singh 		return NULL;
235133c2c65SJasvinder Singh 	}
236133c2c65SJasvinder Singh 
237133c2c65SJasvinder Singh 	/* Node fill in */
2387959831bSJasvinder Singh 	strlcpy(link->name, name, sizeof(link->name));
239133c2c65SJasvinder Singh 	link->port_id = port_id;
240133c2c65SJasvinder Singh 	link->n_rxq = params->rx.n_queues;
241133c2c65SJasvinder Singh 	link->n_txq = params->tx.n_queues;
242133c2c65SJasvinder Singh 
243133c2c65SJasvinder Singh 	/* Node add to list */
244133c2c65SJasvinder Singh 	TAILQ_INSERT_TAIL(&link_list, link, node);
245133c2c65SJasvinder Singh 
246133c2c65SJasvinder Singh 	return link;
247133c2c65SJasvinder Singh }
248133c2c65SJasvinder Singh 
249133c2c65SJasvinder Singh int
250133c2c65SJasvinder Singh link_is_up(const char *name)
251133c2c65SJasvinder Singh {
252133c2c65SJasvinder Singh 	struct rte_eth_link link_params;
253133c2c65SJasvinder Singh 	struct link *link;
254133c2c65SJasvinder Singh 
255133c2c65SJasvinder Singh 	/* Check input params */
256133c2c65SJasvinder Singh 	if (name == NULL)
257133c2c65SJasvinder Singh 		return 0;
258133c2c65SJasvinder Singh 
259133c2c65SJasvinder Singh 	link = link_find(name);
260133c2c65SJasvinder Singh 	if (link == NULL)
261133c2c65SJasvinder Singh 		return 0;
262133c2c65SJasvinder Singh 
263133c2c65SJasvinder Singh 	/* Resource */
264133c2c65SJasvinder Singh 	rte_eth_link_get(link->port_id, &link_params);
265133c2c65SJasvinder Singh 
266133c2c65SJasvinder Singh 	return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
267133c2c65SJasvinder Singh }
268