xref: /dpdk/drivers/net/mlx5/mlx5_rss.c (revision 42c3576d44dd48602aba0820b98c07e2c4278c0e)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2015 6WIND S.A.
5  *   Copyright 2015 Mellanox.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of 6WIND S.A. nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stddef.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <assert.h>
39 
40 /* Verbs header. */
41 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
42 #ifdef PEDANTIC
43 #pragma GCC diagnostic ignored "-Wpedantic"
44 #endif
45 #include <infiniband/verbs.h>
46 #ifdef PEDANTIC
47 #pragma GCC diagnostic error "-Wpedantic"
48 #endif
49 
50 #include <rte_malloc.h>
51 #include <rte_ethdev_driver.h>
52 
53 #include "mlx5.h"
54 #include "mlx5_defs.h"
55 #include "mlx5_rxtx.h"
56 
57 /**
58  * DPDK callback to update the RSS hash configuration.
59  *
60  * @param dev
61  *   Pointer to Ethernet device structure.
62  * @param[in] rss_conf
63  *   RSS configuration data.
64  *
65  * @return
66  *   0 on success, negative errno value on failure.
67  */
68 int
69 mlx5_rss_hash_update(struct rte_eth_dev *dev,
70 		     struct rte_eth_rss_conf *rss_conf)
71 {
72 	struct priv *priv = dev->data->dev_private;
73 	int ret = 0;
74 
75 	priv_lock(priv);
76 	if (rss_conf->rss_hf & MLX5_RSS_HF_MASK) {
77 		ret = -EINVAL;
78 		goto out;
79 	}
80 	if (rss_conf->rss_key && rss_conf->rss_key_len) {
81 		priv->rss_conf.rss_key = rte_realloc(priv->rss_conf.rss_key,
82 						     rss_conf->rss_key_len, 0);
83 		if (!priv->rss_conf.rss_key) {
84 			ret = -ENOMEM;
85 			goto out;
86 		}
87 		memcpy(priv->rss_conf.rss_key, rss_conf->rss_key,
88 		       rss_conf->rss_key_len);
89 		priv->rss_conf.rss_key_len = rss_conf->rss_key_len;
90 	}
91 	priv->rss_conf.rss_hf = rss_conf->rss_hf;
92 out:
93 	priv_unlock(priv);
94 	return ret;
95 }
96 
97 /**
98  * DPDK callback to get the RSS hash configuration.
99  *
100  * @param dev
101  *   Pointer to Ethernet device structure.
102  * @param[in, out] rss_conf
103  *   RSS configuration data.
104  *
105  * @return
106  *   0 on success, negative errno value on failure.
107  */
108 int
109 mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
110 		       struct rte_eth_rss_conf *rss_conf)
111 {
112 	struct priv *priv = dev->data->dev_private;
113 
114 	if (!rss_conf)
115 		return -EINVAL;
116 	priv_lock(priv);
117 	if (rss_conf->rss_key &&
118 	    (rss_conf->rss_key_len >= priv->rss_conf.rss_key_len)) {
119 		memcpy(rss_conf->rss_key, priv->rss_conf.rss_key,
120 		       priv->rss_conf.rss_key_len);
121 	}
122 	rss_conf->rss_key_len = priv->rss_conf.rss_key_len;
123 	rss_conf->rss_hf = priv->rss_conf.rss_hf;
124 	priv_unlock(priv);
125 	return 0;
126 }
127 
128 /**
129  * Allocate/reallocate RETA index table.
130  *
131  * @param priv
132  *   Pointer to private structure.
133  * @praram reta_size
134  *   The size of the array to allocate.
135  *
136  * @return
137  *   0 on success, errno value on failure.
138  */
139 int
140 priv_rss_reta_index_resize(struct priv *priv, unsigned int reta_size)
141 {
142 	void *mem;
143 	unsigned int old_size = priv->reta_idx_n;
144 
145 	if (priv->reta_idx_n == reta_size)
146 		return 0;
147 
148 	mem = rte_realloc(priv->reta_idx,
149 			  reta_size * sizeof((*priv->reta_idx)[0]), 0);
150 	if (!mem)
151 		return ENOMEM;
152 	priv->reta_idx = mem;
153 	priv->reta_idx_n = reta_size;
154 
155 	if (old_size < reta_size)
156 		memset(&(*priv->reta_idx)[old_size], 0,
157 		       (reta_size - old_size) *
158 		       sizeof((*priv->reta_idx)[0]));
159 	return 0;
160 }
161 
162 /**
163  * Query RETA table.
164  *
165  * @param priv
166  *   Pointer to private structure.
167  * @param[in, out] reta_conf
168  *   Pointer to the first RETA configuration structure.
169  * @param reta_size
170  *   Number of entries.
171  *
172  * @return
173  *   0 on success, errno value on failure.
174  */
175 static int
176 priv_dev_rss_reta_query(struct priv *priv,
177 			struct rte_eth_rss_reta_entry64 *reta_conf,
178 			unsigned int reta_size)
179 {
180 	unsigned int idx;
181 	unsigned int i;
182 
183 	if (!reta_size || reta_size > priv->reta_idx_n)
184 		return EINVAL;
185 	/* Fill each entry of the table even if its bit is not set. */
186 	for (idx = 0, i = 0; (i != reta_size); ++i) {
187 		idx = i / RTE_RETA_GROUP_SIZE;
188 		reta_conf[idx].reta[i % RTE_RETA_GROUP_SIZE] =
189 			(*priv->reta_idx)[i];
190 	}
191 	return 0;
192 }
193 
194 /**
195  * Update RETA table.
196  *
197  * @param priv
198  *   Pointer to private structure.
199  * @param[in] reta_conf
200  *   Pointer to the first RETA configuration structure.
201  * @param reta_size
202  *   Number of entries.
203  *
204  * @return
205  *   0 on success, errno value on failure.
206  */
207 static int
208 priv_dev_rss_reta_update(struct priv *priv,
209 			 struct rte_eth_rss_reta_entry64 *reta_conf,
210 			 unsigned int reta_size)
211 {
212 	unsigned int idx;
213 	unsigned int i;
214 	unsigned int pos;
215 	int ret;
216 
217 	if (!reta_size)
218 		return EINVAL;
219 	ret = priv_rss_reta_index_resize(priv, reta_size);
220 	if (ret)
221 		return ret;
222 
223 	for (idx = 0, i = 0; (i != reta_size); ++i) {
224 		idx = i / RTE_RETA_GROUP_SIZE;
225 		pos = i % RTE_RETA_GROUP_SIZE;
226 		if (((reta_conf[idx].mask >> i) & 0x1) == 0)
227 			continue;
228 		assert(reta_conf[idx].reta[pos] < priv->rxqs_n);
229 		(*priv->reta_idx)[i] = reta_conf[idx].reta[pos];
230 	}
231 	return 0;
232 }
233 
234 /**
235  * DPDK callback to get the RETA indirection table.
236  *
237  * @param dev
238  *   Pointer to Ethernet device structure.
239  * @param reta_conf
240  *   Pointer to RETA configuration structure array.
241  * @param reta_size
242  *   Size of the RETA table.
243  *
244  * @return
245  *   0 on success, negative errno value on failure.
246  */
247 int
248 mlx5_dev_rss_reta_query(struct rte_eth_dev *dev,
249 			struct rte_eth_rss_reta_entry64 *reta_conf,
250 			uint16_t reta_size)
251 {
252 	int ret;
253 	struct priv *priv = dev->data->dev_private;
254 
255 	priv_lock(priv);
256 	ret = priv_dev_rss_reta_query(priv, reta_conf, reta_size);
257 	priv_unlock(priv);
258 	return -ret;
259 }
260 
261 /**
262  * DPDK callback to update the RETA indirection table.
263  *
264  * @param dev
265  *   Pointer to Ethernet device structure.
266  * @param reta_conf
267  *   Pointer to RETA configuration structure array.
268  * @param reta_size
269  *   Size of the RETA table.
270  *
271  * @return
272  *   0 on success, negative errno value on failure.
273  */
274 int
275 mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
276 			 struct rte_eth_rss_reta_entry64 *reta_conf,
277 			 uint16_t reta_size)
278 {
279 	int ret;
280 	struct priv *priv = dev->data->dev_private;
281 
282 	priv_lock(priv);
283 	ret = priv_dev_rss_reta_update(priv, reta_conf, reta_size);
284 	priv_unlock(priv);
285 	if (dev->data->dev_started) {
286 		mlx5_dev_stop(dev);
287 		mlx5_dev_start(dev);
288 	}
289 	return -ret;
290 }
291