examples: fix RSS hash function configuration
[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                 if (port_info.flow_type_rss_offloads & ETH_RSS_IPV4)
156                         port_conf.rx_adv_conf.rss_conf.rss_hf |=
157                                 ETH_RSS_IPV4;
158                 if (port_info.flow_type_rss_offloads & ETH_RSS_IPV6)
159                         port_conf.rx_adv_conf.rss_conf.rss_hf |=
160                                 ETH_RSS_IPV6;
161         }
162
163         cpu_id = (uint32_t) rte_eth_dev_socket_id(port_id);
164         if (cpu_id == (uint32_t) SOCKET_ID_ANY)
165                 cpu_id = 0;
166
167         status = rte_eth_dev_configure(
168                 port_id,
169                 params->rx.n_queues,
170                 params->tx.n_queues,
171                 &port_conf);
172
173         if (status < 0)
174                 return NULL;
175
176         if (params->promiscuous)
177                 rte_eth_promiscuous_enable(port_id);
178
179         /* Port RX */
180         for (i = 0; i < params->rx.n_queues; i++) {
181                 status = rte_eth_rx_queue_setup(
182                         port_id,
183                         i,
184                         params->rx.queue_size,
185                         cpu_id,
186                         NULL,
187                         mempool->m);
188
189                 if (status < 0)
190                         return NULL;
191         }
192
193         /* Port TX */
194         for (i = 0; i < params->tx.n_queues; i++) {
195                 status = rte_eth_tx_queue_setup(
196                         port_id,
197                         i,
198                         params->tx.queue_size,
199                         cpu_id,
200                         NULL);
201
202                 if (status < 0)
203                         return NULL;
204         }
205
206         /* Port start */
207         status = rte_eth_dev_start(port_id);
208         if (status < 0)
209                 return NULL;
210
211         if (rss) {
212                 status = rss_setup(port_id, port_info.reta_size, rss);
213
214                 if (status) {
215                         rte_eth_dev_stop(port_id);
216                         return NULL;
217                 }
218         }
219
220         /* Port link up */
221         status = rte_eth_dev_set_link_up(port_id);
222         if ((status < 0) && (status != -ENOTSUP)) {
223                 rte_eth_dev_stop(port_id);
224                 return NULL;
225         }
226
227         /* Node allocation */
228         link = calloc(1, sizeof(struct link));
229         if (link == NULL) {
230                 rte_eth_dev_stop(port_id);
231                 return NULL;
232         }
233
234         /* Node fill in */
235         strlcpy(link->name, name, sizeof(link->name));
236         link->port_id = port_id;
237         link->n_rxq = params->rx.n_queues;
238         link->n_txq = params->tx.n_queues;
239
240         /* Node add to list */
241         TAILQ_INSERT_TAIL(&link_list, link, node);
242
243         return link;
244 }
245
246 int
247 link_is_up(const char *name)
248 {
249         struct rte_eth_link link_params;
250         struct link *link;
251
252         /* Check input params */
253         if (name == NULL)
254                 return 0;
255
256         link = link_find(name);
257         if (link == NULL)
258                 return 0;
259
260         /* Resource */
261         rte_eth_link_get(link->port_id, &link_params);
262
263         return (link_params.link_status == ETH_LINK_DOWN) ? 0 : 1;
264 }