mlx5: add special flows for broadcast and IPv6 multicast
[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 /* Initialization data for special flows. */
62 static const struct special_flow_init special_flow_init[] = {
63         [HASH_RXQ_FLOW_TYPE_PROMISC] = {
64                 .dst_mac_val = "\x00\x00\x00\x00\x00\x00",
65                 .dst_mac_mask = "\x00\x00\x00\x00\x00\x00",
66                 .hash_types =
67                         1 << HASH_RXQ_TCPV4 |
68                         1 << HASH_RXQ_UDPV4 |
69                         1 << HASH_RXQ_IPV4 |
70 #ifdef HAVE_FLOW_SPEC_IPV6
71                         1 << HASH_RXQ_TCPV6 |
72                         1 << HASH_RXQ_UDPV6 |
73                         1 << HASH_RXQ_IPV6 |
74 #endif /* HAVE_FLOW_SPEC_IPV6 */
75                         1 << HASH_RXQ_ETH |
76                         0,
77         },
78         [HASH_RXQ_FLOW_TYPE_ALLMULTI] = {
79                 .dst_mac_val = "\x01\x00\x00\x00\x00\x00",
80                 .dst_mac_mask = "\x01\x00\x00\x00\x00\x00",
81                 .hash_types =
82                         1 << HASH_RXQ_UDPV4 |
83                         1 << HASH_RXQ_IPV4 |
84 #ifdef HAVE_FLOW_SPEC_IPV6
85                         1 << HASH_RXQ_UDPV6 |
86                         1 << HASH_RXQ_IPV6 |
87 #endif /* HAVE_FLOW_SPEC_IPV6 */
88                         1 << HASH_RXQ_ETH |
89                         0,
90         },
91         [HASH_RXQ_FLOW_TYPE_BROADCAST] = {
92                 .dst_mac_val = "\xff\xff\xff\xff\xff\xff",
93                 .dst_mac_mask = "\xff\xff\xff\xff\xff\xff",
94                 .hash_types =
95                         1 << HASH_RXQ_UDPV4 |
96                         1 << HASH_RXQ_IPV4 |
97 #ifdef HAVE_FLOW_SPEC_IPV6
98                         1 << HASH_RXQ_UDPV6 |
99                         1 << HASH_RXQ_IPV6 |
100 #endif /* HAVE_FLOW_SPEC_IPV6 */
101                         1 << HASH_RXQ_ETH |
102                         0,
103         },
104 #ifdef HAVE_FLOW_SPEC_IPV6
105         [HASH_RXQ_FLOW_TYPE_IPV6MULTI] = {
106                 .dst_mac_val = "\x33\x33\x00\x00\x00\x00",
107                 .dst_mac_mask = "\xff\xff\x00\x00\x00\x00",
108                 .hash_types =
109                         1 << HASH_RXQ_UDPV6 |
110                         1 << HASH_RXQ_IPV6 |
111                         1 << HASH_RXQ_ETH |
112                         0,
113         },
114 #endif /* HAVE_FLOW_SPEC_IPV6 */
115 };
116
117 /**
118  * Enable a special flow in a hash RX queue.
119  *
120  * @param hash_rxq
121  *   Pointer to hash RX queue structure.
122  * @param flow_type
123  *   Special flow type.
124  *
125  * @return
126  *   0 on success, errno value on failure.
127  */
128 static int
129 hash_rxq_special_flow_enable(struct hash_rxq *hash_rxq,
130                              enum hash_rxq_flow_type flow_type)
131 {
132         struct ibv_exp_flow *flow;
133         FLOW_ATTR_SPEC_ETH(data, hash_rxq_flow_attr(hash_rxq, NULL, 0));
134         struct ibv_exp_flow_attr *attr = &data->attr;
135         struct ibv_exp_flow_spec_eth *spec = &data->spec;
136         const uint8_t *mac;
137         const uint8_t *mask;
138
139         /* Check if flow is relevant for this hash_rxq. */
140         if (!(special_flow_init[flow_type].hash_types & (1 << hash_rxq->type)))
141                 return 0;
142         /* Check if flow already exists. */
143         if (hash_rxq->special_flow[flow_type] != NULL)
144                 return 0;
145
146         /*
147          * No padding must be inserted by the compiler between attr and spec.
148          * This layout is expected by libibverbs.
149          */
150         assert(((uint8_t *)attr + sizeof(*attr)) == (uint8_t *)spec);
151         hash_rxq_flow_attr(hash_rxq, attr, sizeof(data));
152         /* The first specification must be Ethernet. */
153         assert(spec->type == IBV_EXP_FLOW_SPEC_ETH);
154         assert(spec->size == sizeof(*spec));
155
156         mac = special_flow_init[flow_type].dst_mac_val;
157         mask = special_flow_init[flow_type].dst_mac_mask;
158         *spec = (struct ibv_exp_flow_spec_eth){
159                 .type = IBV_EXP_FLOW_SPEC_ETH,
160                 .size = sizeof(*spec),
161                 .val = {
162                         .dst_mac = {
163                                 mac[0], mac[1], mac[2],
164                                 mac[3], mac[4], mac[5],
165                         },
166                 },
167                 .mask = {
168                         .dst_mac = {
169                                 mask[0], mask[1], mask[2],
170                                 mask[3], mask[4], mask[5],
171                         },
172                 },
173         };
174
175         errno = 0;
176         flow = ibv_exp_create_flow(hash_rxq->qp, attr);
177         if (flow == NULL) {
178                 /* It's not clear whether errno is always set in this case. */
179                 ERROR("%p: flow configuration failed, errno=%d: %s",
180                       (void *)hash_rxq, errno,
181                       (errno ? strerror(errno) : "Unknown error"));
182                 if (errno)
183                         return errno;
184                 return EINVAL;
185         }
186         hash_rxq->special_flow[flow_type] = flow;
187         DEBUG("%p: enabling special flow %s (%d)",
188               (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type);
189         return 0;
190 }
191
192 /**
193  * Disable a special flow in a hash RX queue.
194  *
195  * @param hash_rxq
196  *   Pointer to hash RX queue structure.
197  * @param flow_type
198  *   Special flow type.
199  */
200 static void
201 hash_rxq_special_flow_disable(struct hash_rxq *hash_rxq,
202                               enum hash_rxq_flow_type flow_type)
203 {
204         if (hash_rxq->special_flow[flow_type] == NULL)
205                 return;
206         DEBUG("%p: disabling special flow %s (%d)",
207               (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type);
208         claim_zero(ibv_exp_destroy_flow(hash_rxq->special_flow[flow_type]));
209         hash_rxq->special_flow[flow_type] = NULL;
210         DEBUG("%p: special flow %s (%d) disabled",
211               (void *)hash_rxq, hash_rxq_flow_type_str(flow_type), flow_type);
212 }
213
214 /**
215  * Enable a special flow in all hash RX queues.
216  *
217  * @param priv
218  *   Private structure.
219  * @param flow_type
220  *   Special flow type.
221  *
222  * @return
223  *   0 on success, errno value on failure.
224  */
225 int
226 priv_special_flow_enable(struct priv *priv, enum hash_rxq_flow_type flow_type)
227 {
228         unsigned int i;
229
230         if (!priv_allow_flow_type(priv, flow_type))
231                 return 0;
232         for (i = 0; (i != priv->hash_rxqs_n); ++i) {
233                 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
234                 int ret;
235
236                 ret = hash_rxq_special_flow_enable(hash_rxq, flow_type);
237                 if (!ret)
238                         continue;
239                 /* Failure, rollback. */
240                 while (i != 0) {
241                         hash_rxq = &(*priv->hash_rxqs)[--i];
242                         hash_rxq_special_flow_disable(hash_rxq, flow_type);
243                 }
244                 return ret;
245         }
246         return 0;
247 }
248
249 /**
250  * Disable a special flow in all hash RX queues.
251  *
252  * @param priv
253  *   Private structure.
254  * @param flow_type
255  *   Special flow type.
256  */
257 void
258 priv_special_flow_disable(struct priv *priv, enum hash_rxq_flow_type flow_type)
259 {
260         unsigned int i;
261
262         for (i = 0; (i != priv->hash_rxqs_n); ++i) {
263                 struct hash_rxq *hash_rxq = &(*priv->hash_rxqs)[i];
264
265                 hash_rxq_special_flow_disable(hash_rxq, flow_type);
266         }
267 }
268
269 /**
270  * DPDK callback to enable promiscuous mode.
271  *
272  * @param dev
273  *   Pointer to Ethernet device structure.
274  */
275 void
276 mlx5_promiscuous_enable(struct rte_eth_dev *dev)
277 {
278         struct priv *priv = dev->data->dev_private;
279         int ret;
280
281         priv_lock(priv);
282         priv->promisc_req = 1;
283         ret = priv_rehash_flows(priv);
284         if (ret)
285                 ERROR("error while enabling promiscuous mode: %s",
286                       strerror(ret));
287         priv_unlock(priv);
288 }
289
290 /**
291  * DPDK callback to disable promiscuous mode.
292  *
293  * @param dev
294  *   Pointer to Ethernet device structure.
295  */
296 void
297 mlx5_promiscuous_disable(struct rte_eth_dev *dev)
298 {
299         struct priv *priv = dev->data->dev_private;
300         int ret;
301
302         priv_lock(priv);
303         priv->promisc_req = 0;
304         ret = priv_rehash_flows(priv);
305         if (ret)
306                 ERROR("error while disabling promiscuous mode: %s",
307                       strerror(ret));
308         priv_unlock(priv);
309 }
310
311 /**
312  * DPDK callback to enable allmulti mode.
313  *
314  * @param dev
315  *   Pointer to Ethernet device structure.
316  */
317 void
318 mlx5_allmulticast_enable(struct rte_eth_dev *dev)
319 {
320         struct priv *priv = dev->data->dev_private;
321         int ret;
322
323         priv_lock(priv);
324         priv->allmulti_req = 1;
325         ret = priv_rehash_flows(priv);
326         if (ret)
327                 ERROR("error while enabling allmulticast mode: %s",
328                       strerror(ret));
329         priv_unlock(priv);
330 }
331
332 /**
333  * DPDK callback to disable allmulti mode.
334  *
335  * @param dev
336  *   Pointer to Ethernet device structure.
337  */
338 void
339 mlx5_allmulticast_disable(struct rte_eth_dev *dev)
340 {
341         struct priv *priv = dev->data->dev_private;
342         int ret;
343
344         priv_lock(priv);
345         priv->allmulti_req = 0;
346         ret = priv_rehash_flows(priv);
347         if (ret)
348                 ERROR("error while disabling allmulticast mode: %s",
349                       strerror(ret));
350         priv_unlock(priv);
351 }