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