xref: /dpdk/drivers/net/mlx5/mlx5_vlan.c (revision ceb1ccd5d50c1a89ba8bdd97cc199e7f07422b98)
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 #include "mlx5_autoconf.h"
52 
53 /**
54  * Configure a VLAN filter.
55  *
56  * @param dev
57  *   Pointer to Ethernet device structure.
58  * @param vlan_id
59  *   VLAN ID to filter.
60  * @param on
61  *   Toggle filter.
62  *
63  * @return
64  *   0 on success, errno value on failure.
65  */
66 static int
67 vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
68 {
69 	struct priv *priv = dev->data->dev_private;
70 	unsigned int i;
71 
72 	DEBUG("%p: %s VLAN filter ID %" PRIu16,
73 	      (void *)dev, (on ? "enable" : "disable"), vlan_id);
74 	assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
75 	for (i = 0; (i != priv->vlan_filter_n); ++i)
76 		if (priv->vlan_filter[i] == vlan_id)
77 			break;
78 	/* Check if there's room for another VLAN filter. */
79 	if (i == RTE_DIM(priv->vlan_filter))
80 		return ENOMEM;
81 	if (i < priv->vlan_filter_n) {
82 		assert(priv->vlan_filter_n != 0);
83 		/* Enabling an existing VLAN filter has no effect. */
84 		if (on)
85 			return 0;
86 		/* Remove VLAN filter from list. */
87 		--priv->vlan_filter_n;
88 		memmove(&priv->vlan_filter[i],
89 			&priv->vlan_filter[i + 1],
90 			priv->vlan_filter_n - i);
91 		priv->vlan_filter[priv->vlan_filter_n] = 0;
92 	} else {
93 		assert(i == priv->vlan_filter_n);
94 		/* Disabling an unknown VLAN filter has no effect. */
95 		if (!on)
96 			return 0;
97 		/* Add new VLAN filter. */
98 		priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
99 		++priv->vlan_filter_n;
100 	}
101 	/* Rehash flows in all hash RX queues. */
102 	priv_mac_addrs_disable(priv);
103 	priv_special_flow_disable_all(priv);
104 	return priv_rehash_flows(priv);
105 }
106 
107 /**
108  * DPDK callback to configure a VLAN filter.
109  *
110  * @param dev
111  *   Pointer to Ethernet device structure.
112  * @param vlan_id
113  *   VLAN ID to filter.
114  * @param on
115  *   Toggle filter.
116  *
117  * @return
118  *   0 on success, negative errno value on failure.
119  */
120 int
121 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
122 {
123 	struct priv *priv = dev->data->dev_private;
124 	int ret;
125 
126 	priv_lock(priv);
127 	ret = vlan_filter_set(dev, vlan_id, on);
128 	priv_unlock(priv);
129 	assert(ret >= 0);
130 	return -ret;
131 }
132 
133 /**
134  * Set/reset VLAN stripping for a specific queue.
135  *
136  * @param priv
137  *   Pointer to private structure.
138  * @param idx
139  *   RX queue index.
140  * @param on
141  *   Enable/disable VLAN stripping.
142  */
143 static void
144 priv_vlan_strip_queue_set(struct priv *priv, uint16_t idx, int on)
145 {
146 	struct rxq *rxq = (*priv->rxqs)[idx];
147 #ifdef HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS
148 	struct ibv_exp_wq_attr mod;
149 	uint16_t vlan_offloads =
150 		(on ? IBV_EXP_RECEIVE_WQ_CVLAN_STRIP : 0) |
151 		0;
152 	int err;
153 
154 	DEBUG("set VLAN offloads 0x%x for port %d queue %d",
155 	      vlan_offloads, rxq->port_id, idx);
156 	mod = (struct ibv_exp_wq_attr){
157 		.attr_mask = IBV_EXP_WQ_ATTR_VLAN_OFFLOADS,
158 		.vlan_offloads = vlan_offloads,
159 	};
160 
161 	err = ibv_exp_modify_wq(rxq->wq, &mod);
162 	if (err) {
163 		ERROR("%p: failed to modified stripping mode: %s",
164 		      (void *)priv, strerror(err));
165 		return;
166 	}
167 
168 #endif /* HAVE_EXP_DEVICE_ATTR_VLAN_OFFLOADS */
169 
170 	/* Update related bits in RX queue. */
171 	rxq->vlan_strip = !!on;
172 }
173 
174 /**
175  * Callback to set/reset VLAN stripping for a specific queue.
176  *
177  * @param dev
178  *   Pointer to Ethernet device structure.
179  * @param queue
180  *   RX queue index.
181  * @param on
182  *   Enable/disable VLAN stripping.
183  */
184 void
185 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
186 {
187 	struct priv *priv = dev->data->dev_private;
188 
189 	/* Validate hw support */
190 	if (!priv->hw_vlan_strip) {
191 		ERROR("VLAN stripping is not supported");
192 		return;
193 	}
194 
195 	/* Validate queue number */
196 	if (queue >= priv->rxqs_n) {
197 		ERROR("VLAN stripping, invalid queue number %d", queue);
198 		return;
199 	}
200 
201 	priv_lock(priv);
202 	priv_vlan_strip_queue_set(priv, queue, on);
203 	priv_unlock(priv);
204 }
205 
206 /**
207  * Callback to set/reset VLAN offloads for a port.
208  *
209  * @param dev
210  *   Pointer to Ethernet device structure.
211  * @param mask
212  *   VLAN offload bit mask.
213  */
214 void
215 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
216 {
217 	struct priv *priv = dev->data->dev_private;
218 	unsigned int i;
219 
220 	if (mask & ETH_VLAN_STRIP_MASK) {
221 		int hw_vlan_strip = dev->data->dev_conf.rxmode.hw_vlan_strip;
222 
223 		if (!priv->hw_vlan_strip) {
224 			ERROR("VLAN stripping is not supported");
225 			return;
226 		}
227 
228 		/* Run on every RX queue and set/reset VLAN stripping. */
229 		priv_lock(priv);
230 		for (i = 0; (i != priv->rxqs_n); i++)
231 			priv_vlan_strip_queue_set(priv, i, hw_vlan_strip);
232 		priv_unlock(priv);
233 	}
234 }
235