xref: /dpdk/examples/ip_pipeline/link.c (revision c6698a3e8fa525ad8a5c10191b83c4bbeab75907)
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 
39133c2c65SJasvinder Singh static struct rte_eth_conf port_conf_default = {
40133c2c65SJasvinder Singh 	.link_speeds = 0,
41133c2c65SJasvinder Singh 	.rxmode = {
42133c2c65SJasvinder Singh 		.mq_mode = ETH_MQ_RX_NONE,
43133c2c65SJasvinder Singh 		.max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
44133c2c65SJasvinder Singh 		.split_hdr_size = 0, /* Header split buffer size */
45*c6698a3eSThomas Monjalon 		.offloads = DEV_RX_OFFLOAD_CRC_STRIP,
46133c2c65SJasvinder Singh 	},
47133c2c65SJasvinder Singh 	.rx_adv_conf = {
48133c2c65SJasvinder Singh 		.rss_conf = {
49133c2c65SJasvinder Singh 			.rss_key = NULL,
50133c2c65SJasvinder Singh 			.rss_key_len = 40,
51133c2c65SJasvinder Singh 			.rss_hf = 0,
52133c2c65SJasvinder Singh 		},
53133c2c65SJasvinder Singh 	},
54133c2c65SJasvinder Singh 	.txmode = {
55133c2c65SJasvinder Singh 		.mq_mode = ETH_MQ_TX_NONE,
56133c2c65SJasvinder Singh 	},
57133c2c65SJasvinder Singh 	.lpbk_mode = 0,
58133c2c65SJasvinder Singh };
59133c2c65SJasvinder Singh 
60133c2c65SJasvinder Singh #define RETA_CONF_SIZE     (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
61133c2c65SJasvinder Singh 
62133c2c65SJasvinder Singh static int
63133c2c65SJasvinder Singh rss_setup(uint16_t port_id,
64133c2c65SJasvinder Singh 	uint16_t reta_size,
65133c2c65SJasvinder Singh 	struct link_params_rss *rss)
66133c2c65SJasvinder Singh {
67133c2c65SJasvinder Singh 	struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
68133c2c65SJasvinder Singh 	uint32_t i;
69133c2c65SJasvinder Singh 	int status;
70133c2c65SJasvinder Singh 
71133c2c65SJasvinder Singh 	/* RETA setting */
72133c2c65SJasvinder Singh 	memset(reta_conf, 0, sizeof(reta_conf));
73133c2c65SJasvinder Singh 
74133c2c65SJasvinder Singh 	for (i = 0; i < reta_size; i++)
75133c2c65SJasvinder Singh 		reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
76133c2c65SJasvinder Singh 
77133c2c65SJasvinder Singh 	for (i = 0; i < reta_size; i++) {
78133c2c65SJasvinder Singh 		uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
79133c2c65SJasvinder Singh 		uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
80133c2c65SJasvinder Singh 		uint32_t rss_qs_pos = i % rss->n_queues;
81133c2c65SJasvinder Singh 
82133c2c65SJasvinder Singh 		reta_conf[reta_id].reta[reta_pos] =
83133c2c65SJasvinder Singh 			(uint16_t) rss->queue_id[rss_qs_pos];
84133c2c65SJasvinder Singh 	}
85133c2c65SJasvinder Singh 
86133c2c65SJasvinder Singh 	/* RETA update */
87133c2c65SJasvinder Singh 	status = rte_eth_dev_rss_reta_update(port_id,
88133c2c65SJasvinder Singh 		reta_conf,
89133c2c65SJasvinder Singh 		reta_size);
90133c2c65SJasvinder Singh 
91133c2c65SJasvinder Singh 	return status;
92133c2c65SJasvinder Singh }
93133c2c65SJasvinder Singh 
94133c2c65SJasvinder Singh struct link *
95133c2c65SJasvinder Singh link_create(const char *name, struct link_params *params)
96133c2c65SJasvinder Singh {
97133c2c65SJasvinder Singh 	struct rte_eth_dev_info port_info;
98133c2c65SJasvinder Singh 	struct rte_eth_conf port_conf;
99133c2c65SJasvinder Singh 	struct link *link;
100133c2c65SJasvinder Singh 	struct link_params_rss *rss;
101133c2c65SJasvinder Singh 	struct mempool *mempool;
102133c2c65SJasvinder Singh 	uint32_t cpu_id, i;
103133c2c65SJasvinder Singh 	int status;
104133c2c65SJasvinder Singh 	uint16_t port_id;
105133c2c65SJasvinder Singh 
106133c2c65SJasvinder Singh 	/* Check input params */
107133c2c65SJasvinder Singh 	if ((name == NULL) ||
108133c2c65SJasvinder Singh 		link_find(name) ||
109133c2c65SJasvinder Singh 		(params == NULL) ||
110133c2c65SJasvinder Singh 		(params->rx.n_queues == 0) ||
111133c2c65SJasvinder Singh 		(params->rx.queue_size == 0) ||
112133c2c65SJasvinder Singh 		(params->tx.n_queues == 0) ||
113133c2c65SJasvinder Singh 		(params->tx.queue_size == 0))
114133c2c65SJasvinder Singh 		return NULL;
115133c2c65SJasvinder Singh 
116133c2c65SJasvinder Singh 	port_id = params->port_id;
117133c2c65SJasvinder Singh 	if (params->dev_name) {
118133c2c65SJasvinder Singh 		status = rte_eth_dev_get_port_by_name(params->dev_name,
119133c2c65SJasvinder Singh 			&port_id);
120133c2c65SJasvinder Singh 
121133c2c65SJasvinder Singh 		if (status)
122133c2c65SJasvinder Singh 			return NULL;
123133c2c65SJasvinder Singh 	} else
124133c2c65SJasvinder Singh 		if (!rte_eth_dev_is_valid_port(port_id))
125133c2c65SJasvinder Singh 			return NULL;
126133c2c65SJasvinder Singh 
127133c2c65SJasvinder Singh 	rte_eth_dev_info_get(port_id, &port_info);
128133c2c65SJasvinder Singh 
129133c2c65SJasvinder Singh 	mempool = mempool_find(params->rx.mempool_name);
130133c2c65SJasvinder Singh 	if (mempool == NULL)
131133c2c65SJasvinder Singh 		return NULL;
132133c2c65SJasvinder Singh 
133133c2c65SJasvinder Singh 	rss = params->rx.rss;
134133c2c65SJasvinder Singh 	if (rss) {
135133c2c65SJasvinder Singh 		if ((port_info.reta_size == 0) ||
136133c2c65SJasvinder Singh 			(port_info.reta_size > ETH_RSS_RETA_SIZE_512))
137133c2c65SJasvinder Singh 			return NULL;
138133c2c65SJasvinder Singh 
139133c2c65SJasvinder Singh 		if ((rss->n_queues == 0) ||
140133c2c65SJasvinder Singh 			(rss->n_queues >= LINK_RXQ_RSS_MAX))
141133c2c65SJasvinder Singh 			return NULL;
142133c2c65SJasvinder Singh 
143133c2c65SJasvinder Singh 		for (i = 0; i < rss->n_queues; i++)
144133c2c65SJasvinder Singh 			if (rss->queue_id[i] >= port_info.max_rx_queues)
145133c2c65SJasvinder Singh 				return NULL;
146133c2c65SJasvinder Singh 	}
147133c2c65SJasvinder Singh 
148133c2c65SJasvinder Singh 	/**
149133c2c65SJasvinder Singh 	 * Resource create
150133c2c65SJasvinder Singh 	 */
151133c2c65SJasvinder Singh 	/* Port */
152133c2c65SJasvinder Singh 	memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
153133c2c65SJasvinder Singh 	if (rss) {
154133c2c65SJasvinder Singh 		port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
155133c2c65SJasvinder Singh 		port_conf.rx_adv_conf.rss_conf.rss_hf =
156133c2c65SJasvinder Singh 			ETH_RSS_IPV4 | ETH_RSS_IPV6;
157133c2c65SJasvinder Singh 	}
158133c2c65SJasvinder Singh 
159133c2c65SJasvinder Singh 	cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
160133c2c65SJasvinder Singh 	if (cpu_id == (uint32_t) SOCKET_ID_ANY)
161133c2c65SJasvinder Singh 		cpu_id = 0;
162133c2c65SJasvinder Singh 
163133c2c65SJasvinder Singh 	status = rte_eth_dev_configure(
164133c2c65SJasvinder Singh 		port_id,
165133c2c65SJasvinder Singh 		params->rx.n_queues,
166133c2c65SJasvinder Singh 		params->tx.n_queues,
167133c2c65SJasvinder Singh 		&port_conf);
168133c2c65SJasvinder Singh 
169133c2c65SJasvinder Singh 	if (status < 0)
170133c2c65SJasvinder Singh 		return NULL;
171133c2c65SJasvinder Singh 
172133c2c65SJasvinder Singh 	if (params->promiscuous)
173133c2c65SJasvinder Singh 		rte_eth_promiscuous_enable(port_id);
174133c2c65SJasvinder Singh 
175133c2c65SJasvinder Singh 	/* Port RX */
176133c2c65SJasvinder Singh 	for (i = 0; i < params->rx.n_queues; i++) {
177133c2c65SJasvinder Singh 		status = rte_eth_rx_queue_setup(
178133c2c65SJasvinder Singh 			port_id,
179133c2c65SJasvinder Singh 			i,
180133c2c65SJasvinder Singh 			params->rx.queue_size,
181133c2c65SJasvinder Singh 			cpu_id,
182133c2c65SJasvinder Singh 			NULL,
183133c2c65SJasvinder Singh 			mempool->m);
184133c2c65SJasvinder Singh 
185133c2c65SJasvinder Singh 		if (status < 0)
186133c2c65SJasvinder Singh 			return NULL;
187133c2c65SJasvinder Singh 	}
188133c2c65SJasvinder Singh 
189133c2c65SJasvinder Singh 	/* Port TX */
190133c2c65SJasvinder Singh 	for (i = 0; i < params->tx.n_queues; i++) {
191133c2c65SJasvinder Singh 		status = rte_eth_tx_queue_setup(
192133c2c65SJasvinder Singh 			port_id,
193133c2c65SJasvinder Singh 			i,
194133c2c65SJasvinder Singh 			params->tx.queue_size,
195133c2c65SJasvinder Singh 			cpu_id,
196133c2c65SJasvinder Singh 			NULL);
197133c2c65SJasvinder Singh 
198133c2c65SJasvinder Singh 		if (status < 0)
199133c2c65SJasvinder Singh 			return NULL;
200133c2c65SJasvinder Singh 	}
201133c2c65SJasvinder Singh 
202133c2c65SJasvinder Singh 	/* Port start */
203133c2c65SJasvinder Singh 	status = rte_eth_dev_start(port_id);
204133c2c65SJasvinder Singh 	if (status < 0)
205133c2c65SJasvinder Singh 		return NULL;
206133c2c65SJasvinder Singh 
207133c2c65SJasvinder Singh 	if (rss) {
208133c2c65SJasvinder Singh 		status = rss_setup(port_id, port_info.reta_size, rss);
209133c2c65SJasvinder Singh 
210133c2c65SJasvinder Singh 		if (status) {
211133c2c65SJasvinder Singh 			rte_eth_dev_stop(port_id);
212133c2c65SJasvinder Singh 			return NULL;
213133c2c65SJasvinder Singh 		}
214133c2c65SJasvinder Singh 	}
215133c2c65SJasvinder Singh 
216133c2c65SJasvinder Singh 	/* Port link up */
217133c2c65SJasvinder Singh 	status = rte_eth_dev_set_link_up(port_id);
218133c2c65SJasvinder Singh 	if ((status < 0) && (status != -ENOTSUP)) {
219133c2c65SJasvinder Singh 		rte_eth_dev_stop(port_id);
220133c2c65SJasvinder Singh 		return NULL;
221133c2c65SJasvinder Singh 	}
222133c2c65SJasvinder Singh 
223133c2c65SJasvinder Singh 	/* Node allocation */
224133c2c65SJasvinder Singh 	link = calloc(1, sizeof(struct link));
225133c2c65SJasvinder Singh 	if (link == NULL) {
226133c2c65SJasvinder Singh 		rte_eth_dev_stop(port_id);
227133c2c65SJasvinder Singh 		return NULL;
228133c2c65SJasvinder Singh 	}
229133c2c65SJasvinder Singh 
230133c2c65SJasvinder Singh 	/* Node fill in */
2317959831bSJasvinder Singh 	strlcpy(link->name, name, sizeof(link->name));
232133c2c65SJasvinder Singh 	link->port_id = port_id;
233133c2c65SJasvinder Singh 	link->n_rxq = params->rx.n_queues;
234133c2c65SJasvinder Singh 	link->n_txq = params->tx.n_queues;
235133c2c65SJasvinder Singh 
236133c2c65SJasvinder Singh 	/* Node add to list */
237133c2c65SJasvinder Singh 	TAILQ_INSERT_TAIL(&link_list, link, node);
238133c2c65SJasvinder Singh 
239133c2c65SJasvinder Singh 	return link;
240133c2c65SJasvinder Singh }
241133c2c65SJasvinder Singh 
242133c2c65SJasvinder Singh int
243133c2c65SJasvinder Singh link_is_up(const char *name)
244133c2c65SJasvinder Singh {
245133c2c65SJasvinder Singh 	struct rte_eth_link link_params;
246133c2c65SJasvinder Singh 	struct link *link;
247133c2c65SJasvinder Singh 
248133c2c65SJasvinder Singh 	/* Check input params */
249133c2c65SJasvinder Singh 	if (name == NULL)
250133c2c65SJasvinder Singh 		return 0;
251133c2c65SJasvinder Singh 
252133c2c65SJasvinder Singh 	link = link_find(name);
253133c2c65SJasvinder Singh 	if (link == NULL)
254133c2c65SJasvinder Singh 		return 0;
255133c2c65SJasvinder Singh 
256133c2c65SJasvinder Singh 	/* Resource */
257133c2c65SJasvinder Singh 	rte_eth_link_get(link->port_id, &link_params);
258133c2c65SJasvinder Singh 
259133c2c65SJasvinder Singh 	return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
260133c2c65SJasvinder Singh }
261