xref: /dpdk/examples/ip_pipeline/link.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #include <stdlib.h>
6 #include <string.h>
7 
8 #include <rte_ethdev.h>
9 #include <rte_string_fns.h>
10 
11 #include "link.h"
12 #include "mempool.h"
13 
14 static struct link_list link_list;
15 
16 int
17 link_init(void)
18 {
19 	TAILQ_INIT(&link_list);
20 
21 	return 0;
22 }
23 
24 struct link *
25 link_find(const char *name)
26 {
27 	struct link *link;
28 
29 	if (name == NULL)
30 		return NULL;
31 
32 	TAILQ_FOREACH(link, &link_list, node)
33 		if (strcmp(link->name, name) == 0)
34 			return link;
35 
36 	return NULL;
37 }
38 
39 struct link *
40 link_next(struct link *link)
41 {
42 	return (link == NULL) ? TAILQ_FIRST(&link_list) : TAILQ_NEXT(link, node);
43 }
44 
45 static struct rte_eth_conf port_conf_default = {
46 	.link_speeds = 0,
47 	.rxmode = {
48 		.mq_mode = ETH_MQ_RX_NONE,
49 		.max_rx_pkt_len = 9000, /* Jumbo frame max packet len */
50 		.split_hdr_size = 0, /* Header split buffer size */
51 	},
52 	.rx_adv_conf = {
53 		.rss_conf = {
54 			.rss_key = NULL,
55 			.rss_key_len = 40,
56 			.rss_hf = 0,
57 		},
58 	},
59 	.txmode = {
60 		.mq_mode = ETH_MQ_TX_NONE,
61 	},
62 	.lpbk_mode = 0,
63 };
64 
65 #define RETA_CONF_SIZE     (ETH_RSS_RETA_SIZE_512 / RTE_RETA_GROUP_SIZE)
66 
67 static int
68 rss_setup(uint16_t port_id,
69 	uint16_t reta_size,
70 	struct link_params_rss *rss)
71 {
72 	struct rte_eth_rss_reta_entry64 reta_conf[RETA_CONF_SIZE];
73 	uint32_t i;
74 	int status;
75 
76 	/* RETA setting */
77 	memset(reta_conf, 0, sizeof(reta_conf));
78 
79 	for (i = 0; i < reta_size; i++)
80 		reta_conf[i / RTE_RETA_GROUP_SIZE].mask = UINT64_MAX;
81 
82 	for (i = 0; i < reta_size; i++) {
83 		uint32_t reta_id = i / RTE_RETA_GROUP_SIZE;
84 		uint32_t reta_pos = i % RTE_RETA_GROUP_SIZE;
85 		uint32_t rss_qs_pos = i % rss->n_queues;
86 
87 		reta_conf[reta_id].reta[reta_pos] =
88 			(uint16_t) rss->queue_id[rss_qs_pos];
89 	}
90 
91 	/* RETA update */
92 	status = rte_eth_dev_rss_reta_update(port_id,
93 		reta_conf,
94 		reta_size);
95 
96 	return status;
97 }
98 
99 struct link *
100 link_create(const char *name, struct link_params *params)
101 {
102 	struct rte_eth_dev_info port_info;
103 	struct rte_eth_conf port_conf;
104 	struct link *link;
105 	struct link_params_rss *rss;
106 	struct mempool *mempool;
107 	uint32_t cpu_id, i;
108 	int status;
109 	uint16_t port_id;
110 
111 	/* Check input params */
112 	if ((name == NULL) ||
113 		link_find(name) ||
114 		(params == NULL) ||
115 		(params->rx.n_queues == 0) ||
116 		(params->rx.queue_size == 0) ||
117 		(params->tx.n_queues == 0) ||
118 		(params->tx.queue_size == 0))
119 		return NULL;
120 
121 	port_id = params->port_id;
122 	if (params->dev_name) {
123 		status = rte_eth_dev_get_port_by_name(params->dev_name,
124 			&port_id);
125 
126 		if (status)
127 			return NULL;
128 	} else
129 		if (!rte_eth_dev_is_valid_port(port_id))
130 			return NULL;
131 
132 	rte_eth_dev_info_get(port_id, &port_info);
133 
134 	mempool = mempool_find(params->rx.mempool_name);
135 	if (mempool == NULL)
136 		return NULL;
137 
138 	rss = params->rx.rss;
139 	if (rss) {
140 		if ((port_info.reta_size == 0) ||
141 			(port_info.reta_size > ETH_RSS_RETA_SIZE_512))
142 			return NULL;
143 
144 		if ((rss->n_queues == 0) ||
145 			(rss->n_queues >= LINK_RXQ_RSS_MAX))
146 			return NULL;
147 
148 		for (i = 0; i < rss->n_queues; i++)
149 			if (rss->queue_id[i] >= port_info.max_rx_queues)
150 				return NULL;
151 	}
152 
153 	/**
154 	 * Resource create
155 	 */
156 	/* Port */
157 	memcpy(&port_conf, &port_conf_default, sizeof(port_conf));
158 	if (rss) {
159 		port_conf.rxmode.mq_mode = ETH_MQ_RX_RSS;
160 		port_conf.rx_adv_conf.rss_conf.rss_hf =
161 			(ETH_RSS_IP | ETH_RSS_TCP | ETH_RSS_UDP) &
162 			port_info.flow_type_rss_offloads;
163 	}
164 
165 	cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
166 	if (cpu_id == (uint32_t) SOCKET_ID_ANY)
167 		cpu_id = 0;
168 
169 	status = rte_eth_dev_configure(
170 		port_id,
171 		params->rx.n_queues,
172 		params->tx.n_queues,
173 		&port_conf);
174 
175 	if (status < 0)
176 		return NULL;
177 
178 	if (params->promiscuous)
179 		rte_eth_promiscuous_enable(port_id);
180 
181 	/* Port RX */
182 	for (i = 0; i < params->rx.n_queues; i++) {
183 		status = rte_eth_rx_queue_setup(
184 			port_id,
185 			i,
186 			params->rx.queue_size,
187 			cpu_id,
188 			NULL,
189 			mempool->m);
190 
191 		if (status < 0)
192 			return NULL;
193 	}
194 
195 	/* Port TX */
196 	for (i = 0; i < params->tx.n_queues; i++) {
197 		status = rte_eth_tx_queue_setup(
198 			port_id,
199 			i,
200 			params->tx.queue_size,
201 			cpu_id,
202 			NULL);
203 
204 		if (status < 0)
205 			return NULL;
206 	}
207 
208 	/* Port start */
209 	status = rte_eth_dev_start(port_id);
210 	if (status < 0)
211 		return NULL;
212 
213 	if (rss) {
214 		status = rss_setup(port_id, port_info.reta_size, rss);
215 
216 		if (status) {
217 			rte_eth_dev_stop(port_id);
218 			return NULL;
219 		}
220 	}
221 
222 	/* Port link up */
223 	status = rte_eth_dev_set_link_up(port_id);
224 	if ((status < 0) && (status != -ENOTSUP)) {
225 		rte_eth_dev_stop(port_id);
226 		return NULL;
227 	}
228 
229 	/* Node allocation */
230 	link = calloc(1, sizeof(struct link));
231 	if (link == NULL) {
232 		rte_eth_dev_stop(port_id);
233 		return NULL;
234 	}
235 
236 	/* Node fill in */
237 	strlcpy(link->name, name, sizeof(link->name));
238 	link->port_id = port_id;
239 	link->n_rxq = params->rx.n_queues;
240 	link->n_txq = params->tx.n_queues;
241 
242 	/* Node add to list */
243 	TAILQ_INSERT_TAIL(&link_list, link, node);
244 
245 	return link;
246 }
247 
248 int
249 link_is_up(const char *name)
250 {
251 	struct rte_eth_link link_params;
252 	struct link *link;
253 
254 	/* Check input params */
255 	if (name == NULL)
256 		return 0;
257 
258 	link = link_find(name);
259 	if (link == NULL)
260 		return 0;
261 
262 	/* Resource */
263 	rte_eth_link_get(link->port_id, &link_params);
264 
265 	return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
266 }
267