xref: /dpdk/drivers/net/mlx5/mlx5_rss.c (revision eb6d5a0af9a05bf940ba19ec1ddbe575b5e7540b)
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.h>
52 
53 #include "mlx5.h"
54 #include "mlx5_rxtx.h"
55 
56 /**
57  * DPDK callback to update the RSS hash configuration.
58  *
59  * @param dev
60  *   Pointer to Ethernet device structure.
61  * @param[in] rss_conf
62  *   RSS configuration data.
63  *
64  * @return
65  *   0 on success, negative errno value on failure.
66  */
67 int
68 mlx5_rss_hash_update(struct rte_eth_dev *dev,
69 		     struct rte_eth_rss_conf *rss_conf)
70 {
71 	struct priv *priv = dev->data->dev_private;
72 	int ret = 0;
73 
74 	priv_lock(priv);
75 	if (rss_conf->rss_key_len) {
76 		priv->rss_conf.rss_key = rte_realloc(priv->rss_conf.rss_key,
77 						     rss_conf->rss_key_len, 0);
78 		if (!priv->rss_conf.rss_key) {
79 			ret = -ENOMEM;
80 			goto out;
81 		}
82 		memcpy(&priv->rss_conf.rss_key, rss_conf->rss_key,
83 		       rss_conf->rss_key_len);
84 		priv->rss_conf.rss_key_len = rss_conf->rss_key_len;
85 	}
86 	priv->rss_conf.rss_hf = rss_conf->rss_hf;
87 out:
88 	priv_unlock(priv);
89 	return ret;
90 }
91 
92 /**
93  * DPDK callback to get the RSS hash configuration.
94  *
95  * @param dev
96  *   Pointer to Ethernet device structure.
97  * @param[in, out] rss_conf
98  *   RSS configuration data.
99  *
100  * @return
101  *   0 on success, negative errno value on failure.
102  */
103 int
104 mlx5_rss_hash_conf_get(struct rte_eth_dev *dev,
105 		       struct rte_eth_rss_conf *rss_conf)
106 {
107 	struct priv *priv = dev->data->dev_private;
108 	int ret = 0;
109 
110 	priv_lock(priv);
111 	if (!rss_conf->rss_key) {
112 		ret = -ENOMEM;
113 		goto out;
114 	}
115 	if (rss_conf->rss_key_len < priv->rss_conf.rss_key_len) {
116 		ret = -EINVAL;
117 		goto out;
118 	}
119 	memcpy(rss_conf->rss_key, priv->rss_conf.rss_key,
120 	       priv->rss_conf.rss_key_len);
121 out:
122 	priv_unlock(priv);
123 	return ret;
124 }
125 
126 /**
127  * Allocate/reallocate RETA index table.
128  *
129  * @param priv
130  *   Pointer to private structure.
131  * @praram reta_size
132  *   The size of the array to allocate.
133  *
134  * @return
135  *   0 on success, errno value on failure.
136  */
137 int
138 priv_rss_reta_index_resize(struct priv *priv, unsigned int reta_size)
139 {
140 	void *mem;
141 	unsigned int old_size = priv->reta_idx_n;
142 
143 	if (priv->reta_idx_n == reta_size)
144 		return 0;
145 
146 	mem = rte_realloc(priv->reta_idx,
147 			  reta_size * sizeof((*priv->reta_idx)[0]), 0);
148 	if (!mem)
149 		return ENOMEM;
150 	priv->reta_idx = mem;
151 	priv->reta_idx_n = reta_size;
152 
153 	if (old_size < reta_size)
154 		memset(&(*priv->reta_idx)[old_size], 0,
155 		       (reta_size - old_size) *
156 		       sizeof((*priv->reta_idx)[0]));
157 	return 0;
158 }
159 
160 /**
161  * Query RETA table.
162  *
163  * @param priv
164  *   Pointer to private structure.
165  * @param[in, out] reta_conf
166  *   Pointer to the first RETA configuration structure.
167  * @param reta_size
168  *   Number of entries.
169  *
170  * @return
171  *   0 on success, errno value on failure.
172  */
173 static int
174 priv_dev_rss_reta_query(struct priv *priv,
175 			struct rte_eth_rss_reta_entry64 *reta_conf,
176 			unsigned int reta_size)
177 {
178 	unsigned int idx;
179 	unsigned int i;
180 
181 	if (!reta_size || reta_size > priv->reta_idx_n)
182 		return EINVAL;
183 	/* Fill each entry of the table even if its bit is not set. */
184 	for (idx = 0, i = 0; (i != reta_size); ++i) {
185 		idx = i / RTE_RETA_GROUP_SIZE;
186 		reta_conf[idx].reta[i % RTE_RETA_GROUP_SIZE] =
187 			(*priv->reta_idx)[i];
188 	}
189 	return 0;
190 }
191 
192 /**
193  * Update RETA table.
194  *
195  * @param priv
196  *   Pointer to private structure.
197  * @param[in] reta_conf
198  *   Pointer to the first RETA configuration structure.
199  * @param reta_size
200  *   Number of entries.
201  *
202  * @return
203  *   0 on success, errno value on failure.
204  */
205 static int
206 priv_dev_rss_reta_update(struct priv *priv,
207 			 struct rte_eth_rss_reta_entry64 *reta_conf,
208 			 unsigned int reta_size)
209 {
210 	unsigned int idx;
211 	unsigned int i;
212 	unsigned int pos;
213 	int ret;
214 
215 	if (!reta_size)
216 		return EINVAL;
217 	ret = priv_rss_reta_index_resize(priv, reta_size);
218 	if (ret)
219 		return ret;
220 
221 	for (idx = 0, i = 0; (i != reta_size); ++i) {
222 		idx = i / RTE_RETA_GROUP_SIZE;
223 		pos = i % RTE_RETA_GROUP_SIZE;
224 		if (((reta_conf[idx].mask >> i) & 0x1) == 0)
225 			continue;
226 		assert(reta_conf[idx].reta[pos] < priv->rxqs_n);
227 		(*priv->reta_idx)[i] = reta_conf[idx].reta[pos];
228 	}
229 	return 0;
230 }
231 
232 /**
233  * DPDK callback to get the RETA indirection table.
234  *
235  * @param dev
236  *   Pointer to Ethernet device structure.
237  * @param reta_conf
238  *   Pointer to RETA configuration structure array.
239  * @param reta_size
240  *   Size of the RETA table.
241  *
242  * @return
243  *   0 on success, negative errno value on failure.
244  */
245 int
246 mlx5_dev_rss_reta_query(struct rte_eth_dev *dev,
247 			struct rte_eth_rss_reta_entry64 *reta_conf,
248 			uint16_t reta_size)
249 {
250 	int ret;
251 	struct priv *priv = dev->data->dev_private;
252 
253 	priv_lock(priv);
254 	ret = priv_dev_rss_reta_query(priv, reta_conf, reta_size);
255 	priv_unlock(priv);
256 	return -ret;
257 }
258 
259 /**
260  * DPDK callback to update the RETA indirection table.
261  *
262  * @param dev
263  *   Pointer to Ethernet device structure.
264  * @param reta_conf
265  *   Pointer to RETA configuration structure array.
266  * @param reta_size
267  *   Size of the RETA table.
268  *
269  * @return
270  *   0 on success, negative errno value on failure.
271  */
272 int
273 mlx5_dev_rss_reta_update(struct rte_eth_dev *dev,
274 			 struct rte_eth_rss_reta_entry64 *reta_conf,
275 			 uint16_t reta_size)
276 {
277 	int ret;
278 	struct priv *priv = dev->data->dev_private;
279 
280 	assert(!mlx5_is_secondary());
281 	priv_lock(priv);
282 	ret = priv_dev_rss_reta_update(priv, reta_conf, reta_size);
283 	priv_unlock(priv);
284 	if (dev->data->dev_started) {
285 		mlx5_dev_stop(dev);
286 		mlx5_dev_start(dev);
287 	}
288 	return -ret;
289 }
290