mlx5: support VLAN filtering
[dpdk.git] / drivers / net / mlx5 / mlx5_vlan.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 <assert.h>
37 #include <stdint.h>
38
39 /* DPDK headers don't like -pedantic. */
40 #ifdef PEDANTIC
41 #pragma GCC diagnostic ignored "-pedantic"
42 #endif
43 #include <rte_ethdev.h>
44 #include <rte_common.h>
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic error "-pedantic"
47 #endif
48
49 #include "mlx5_utils.h"
50 #include "mlx5.h"
51
52 /**
53  * Configure a VLAN filter.
54  *
55  * @param dev
56  *   Pointer to Ethernet device structure.
57  * @param vlan_id
58  *   VLAN ID to filter.
59  * @param on
60  *   Toggle filter.
61  *
62  * @return
63  *   0 on success, errno value on failure.
64  */
65 static int
66 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
67 {
68         struct priv *priv = dev->data->dev_private;
69         unsigned int i;
70         unsigned int r;
71         struct rxq *rxq;
72
73         DEBUG("%p: %s VLAN filter ID %" PRIu16,
74               (void *)dev, (on ? "enable" : "disable"), vlan_id);
75         assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
76         for (i = 0; (i != priv->vlan_filter_n); ++i)
77                 if (priv->vlan_filter[i] == vlan_id)
78                         break;
79         /* Check if there's room for another VLAN filter. */
80         if (i == RTE_DIM(priv->vlan_filter))
81                 return ENOMEM;
82         if (i < priv->vlan_filter_n) {
83                 assert(priv->vlan_filter_n != 0);
84                 /* Enabling an existing VLAN filter has no effect. */
85                 if (on)
86                         return 0;
87                 /* Remove VLAN filter from list. */
88                 --priv->vlan_filter_n;
89                 memmove(&priv->vlan_filter[i],
90                         &priv->vlan_filter[i + 1],
91                         priv->vlan_filter_n - i);
92                 priv->vlan_filter[priv->vlan_filter_n] = 0;
93         } else {
94                 assert(i == priv->vlan_filter_n);
95                 /* Disabling an unknown VLAN filter has no effect. */
96                 if (!on)
97                         return 0;
98                 /* Add new VLAN filter. */
99                 priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
100                 ++priv->vlan_filter_n;
101         }
102         if (!priv->started)
103                 return 0;
104         /* Rehash MAC flows in all RX queues. */
105         if (priv->rss) {
106                 rxq = &priv->rxq_parent;
107                 r = 1;
108         } else {
109                 rxq = (*priv->rxqs)[0];
110                 r = priv->rxqs_n;
111         }
112         for (i = 0; (i < r); rxq = (*priv->rxqs)[++i]) {
113                 int ret;
114
115                 if (rxq == NULL)
116                         continue;
117                 rxq_mac_addrs_del(rxq);
118                 ret = rxq_mac_addrs_add(rxq);
119                 if (!ret)
120                         continue;
121                 /* Rollback. */
122                 while (i != 0) {
123                         rxq = (*priv->rxqs)[--i];
124                         if (rxq != NULL)
125                                 rxq_mac_addrs_del(rxq);
126                 }
127                 return ret;
128         }
129         return 0;
130 }
131
132 /**
133  * DPDK callback to configure a VLAN filter.
134  *
135  * @param dev
136  *   Pointer to Ethernet device structure.
137  * @param vlan_id
138  *   VLAN ID to filter.
139  * @param on
140  *   Toggle filter.
141  *
142  * @return
143  *   0 on success, negative errno value on failure.
144  */
145 int
146 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
147 {
148         struct priv *priv = dev->data->dev_private;
149         int ret;
150
151         priv_lock(priv);
152         ret = vlan_filter_set(dev, vlan_id, on);
153         priv_unlock(priv);
154         assert(ret >= 0);
155         return -ret;
156 }