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