mlx5: support promiscuous and allmulticast Rx modes
[dpdk.git] / drivers / net / mlx5 / mlx5_rxmode.c
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 <errno.h>
36 #include <string.h>
37
38 /* Verbs header. */
39 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic ignored "-pedantic"
42 #endif
43 #include <infiniband/verbs.h>
44 #ifdef PEDANTIC
45 #pragma GCC diagnostic error "-pedantic"
46 #endif
47
48 /* DPDK headers don't like -pedantic. */
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic ignored "-pedantic"
51 #endif
52 #include <rte_ethdev.h>
53 #ifdef PEDANTIC
54 #pragma GCC diagnostic error "-pedantic"
55 #endif
56
57 #include "mlx5.h"
58 #include "mlx5_rxtx.h"
59 #include "mlx5_utils.h"
60
61 /**
62  * Enable promiscuous mode in a RX queue.
63  *
64  * @param rxq
65  *   Pointer to RX queue structure.
66  *
67  * @return
68  *   0 on success, errno value on failure.
69  */
70 int
71 rxq_promiscuous_enable(struct rxq *rxq)
72 {
73         struct ibv_flow *flow;
74         struct ibv_flow_attr attr = {
75                 .type = IBV_FLOW_ATTR_ALL_DEFAULT,
76                 .num_of_specs = 0,
77                 .port = rxq->priv->port,
78                 .flags = 0
79         };
80
81         if (rxq->priv->vf)
82                 return 0;
83         if (rxq->promisc_flow != NULL)
84                 return 0;
85         DEBUG("%p: enabling promiscuous mode", (void *)rxq);
86         errno = 0;
87         flow = ibv_create_flow(rxq->qp, &attr);
88         if (flow == NULL) {
89                 /* It's not clear whether errno is always set in this case. */
90                 ERROR("%p: flow configuration failed, errno=%d: %s",
91                       (void *)rxq, errno,
92                       (errno ? strerror(errno) : "Unknown error"));
93                 if (errno)
94                         return errno;
95                 return EINVAL;
96         }
97         rxq->promisc_flow = flow;
98         DEBUG("%p: promiscuous mode enabled", (void *)rxq);
99         return 0;
100 }
101
102 /**
103  * DPDK callback to enable promiscuous mode.
104  *
105  * @param dev
106  *   Pointer to Ethernet device structure.
107  */
108 void
109 mlx5_promiscuous_enable(struct rte_eth_dev *dev)
110 {
111         struct priv *priv = dev->data->dev_private;
112         unsigned int i;
113         int ret;
114
115         priv_lock(priv);
116         priv->promisc_req = 1;
117         /* If device isn't started, this is all we need to do. */
118         if (!priv->started)
119                 goto end;
120         if (priv->rss) {
121                 ret = rxq_promiscuous_enable(&priv->rxq_parent);
122                 if (ret) {
123                         priv_unlock(priv);
124                         return;
125                 }
126                 goto end;
127         }
128         for (i = 0; (i != priv->rxqs_n); ++i) {
129                 if ((*priv->rxqs)[i] == NULL)
130                         continue;
131                 ret = rxq_promiscuous_enable((*priv->rxqs)[i]);
132                 if (!ret)
133                         continue;
134                 /* Failure, rollback. */
135                 while (i != 0)
136                         if ((*priv->rxqs)[--i] != NULL)
137                                 rxq_promiscuous_disable((*priv->rxqs)[i]);
138                 priv_unlock(priv);
139                 return;
140         }
141 end:
142         priv_unlock(priv);
143 }
144
145 /**
146  * Disable promiscuous mode in a RX queue.
147  *
148  * @param rxq
149  *   Pointer to RX queue structure.
150  */
151 void
152 rxq_promiscuous_disable(struct rxq *rxq)
153 {
154         if (rxq->priv->vf)
155                 return;
156         if (rxq->promisc_flow == NULL)
157                 return;
158         DEBUG("%p: disabling promiscuous mode", (void *)rxq);
159         claim_zero(ibv_destroy_flow(rxq->promisc_flow));
160         rxq->promisc_flow = NULL;
161         DEBUG("%p: promiscuous mode disabled", (void *)rxq);
162 }
163
164 /**
165  * DPDK callback to disable promiscuous mode.
166  *
167  * @param dev
168  *   Pointer to Ethernet device structure.
169  */
170 void
171 mlx5_promiscuous_disable(struct rte_eth_dev *dev)
172 {
173         struct priv *priv = dev->data->dev_private;
174         unsigned int i;
175
176         priv_lock(priv);
177         priv->promisc_req = 0;
178         if (priv->rss) {
179                 rxq_promiscuous_disable(&priv->rxq_parent);
180                 goto end;
181         }
182         for (i = 0; (i != priv->rxqs_n); ++i)
183                 if ((*priv->rxqs)[i] != NULL)
184                         rxq_promiscuous_disable((*priv->rxqs)[i]);
185 end:
186         priv_unlock(priv);
187 }
188
189 /**
190  * Enable allmulti mode in a RX queue.
191  *
192  * @param rxq
193  *   Pointer to RX queue structure.
194  *
195  * @return
196  *   0 on success, errno value on failure.
197  */
198 int
199 rxq_allmulticast_enable(struct rxq *rxq)
200 {
201         struct ibv_flow *flow;
202         struct ibv_flow_attr attr = {
203                 .type = IBV_FLOW_ATTR_MC_DEFAULT,
204                 .num_of_specs = 0,
205                 .port = rxq->priv->port,
206                 .flags = 0
207         };
208
209         if (rxq->allmulti_flow != NULL)
210                 return 0;
211         DEBUG("%p: enabling allmulticast mode", (void *)rxq);
212         errno = 0;
213         flow = ibv_create_flow(rxq->qp, &attr);
214         if (flow == NULL) {
215                 /* It's not clear whether errno is always set in this case. */
216                 ERROR("%p: flow configuration failed, errno=%d: %s",
217                       (void *)rxq, errno,
218                       (errno ? strerror(errno) : "Unknown error"));
219                 if (errno)
220                         return errno;
221                 return EINVAL;
222         }
223         rxq->allmulti_flow = flow;
224         DEBUG("%p: allmulticast mode enabled", (void *)rxq);
225         return 0;
226 }
227
228 /**
229  * DPDK callback to enable allmulti mode.
230  *
231  * @param dev
232  *   Pointer to Ethernet device structure.
233  */
234 void
235 mlx5_allmulticast_enable(struct rte_eth_dev *dev)
236 {
237         struct priv *priv = dev->data->dev_private;
238         unsigned int i;
239         int ret;
240
241         priv_lock(priv);
242         priv->allmulti_req = 1;
243         /* If device isn't started, this is all we need to do. */
244         if (!priv->started)
245                 goto end;
246         if (priv->rss) {
247                 ret = rxq_allmulticast_enable(&priv->rxq_parent);
248                 if (ret) {
249                         priv_unlock(priv);
250                         return;
251                 }
252                 goto end;
253         }
254         for (i = 0; (i != priv->rxqs_n); ++i) {
255                 if ((*priv->rxqs)[i] == NULL)
256                         continue;
257                 ret = rxq_allmulticast_enable((*priv->rxqs)[i]);
258                 if (!ret)
259                         continue;
260                 /* Failure, rollback. */
261                 while (i != 0)
262                         if ((*priv->rxqs)[--i] != NULL)
263                                 rxq_allmulticast_disable((*priv->rxqs)[i]);
264                 priv_unlock(priv);
265                 return;
266         }
267 end:
268         priv_unlock(priv);
269 }
270
271 /**
272  * Disable allmulti mode in a RX queue.
273  *
274  * @param rxq
275  *   Pointer to RX queue structure.
276  */
277 void
278 rxq_allmulticast_disable(struct rxq *rxq)
279 {
280         if (rxq->allmulti_flow == NULL)
281                 return;
282         DEBUG("%p: disabling allmulticast mode", (void *)rxq);
283         claim_zero(ibv_destroy_flow(rxq->allmulti_flow));
284         rxq->allmulti_flow = NULL;
285         DEBUG("%p: allmulticast mode disabled", (void *)rxq);
286 }
287
288 /**
289  * DPDK callback to disable allmulti mode.
290  *
291  * @param dev
292  *   Pointer to Ethernet device structure.
293  */
294 void
295 mlx5_allmulticast_disable(struct rte_eth_dev *dev)
296 {
297         struct priv *priv = dev->data->dev_private;
298         unsigned int i;
299
300         priv_lock(priv);
301         priv->allmulti_req = 0;
302         if (priv->rss) {
303                 rxq_allmulticast_disable(&priv->rxq_parent);
304                 goto end;
305         }
306         for (i = 0; (i != priv->rxqs_n); ++i)
307                 if ((*priv->rxqs)[i] != NULL)
308                         rxq_allmulticast_disable((*priv->rxqs)[i]);
309 end:
310         priv_unlock(priv);
311 }