xref: /dpdk/drivers/net/mlx5/mlx5_vlan.c (revision 25d11a86c56d50947af33d0b79ede622809bd8b9)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5 
6 #include <stddef.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <stdint.h>
10 
11 /*
12  * Not needed by this file; included to work around the lack of off_t
13  * definition for mlx5dv.h with unpatched rdma-core versions.
14  */
15 #include <sys/types.h>
16 
17 /* Verbs headers do not support -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/mlx5dv.h>
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26 
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29 
30 #include "mlx5_utils.h"
31 #include "mlx5.h"
32 #include "mlx5_autoconf.h"
33 #include "mlx5_glue.h"
34 
35 /**
36  * DPDK callback to configure a VLAN filter.
37  *
38  * @param dev
39  *   Pointer to Ethernet device structure.
40  * @param vlan_id
41  *   VLAN ID to filter.
42  * @param on
43  *   Toggle filter.
44  *
45  * @return
46  *   0 on success, a negative errno value otherwise and rte_errno is set.
47  */
48 int
49 mlx5_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
50 {
51 	struct priv *priv = dev->data->dev_private;
52 	unsigned int i;
53 
54 	DRV_LOG(DEBUG, "port %u %s VLAN filter ID %" PRIu16,
55 		dev->data->port_id, (on ? "enable" : "disable"), vlan_id);
56 	assert(priv->vlan_filter_n <= RTE_DIM(priv->vlan_filter));
57 	for (i = 0; (i != priv->vlan_filter_n); ++i)
58 		if (priv->vlan_filter[i] == vlan_id)
59 			break;
60 	/* Check if there's room for another VLAN filter. */
61 	if (i == RTE_DIM(priv->vlan_filter)) {
62 		rte_errno = ENOMEM;
63 		return -rte_errno;
64 	}
65 	if (i < priv->vlan_filter_n) {
66 		assert(priv->vlan_filter_n != 0);
67 		/* Enabling an existing VLAN filter has no effect. */
68 		if (on)
69 			goto out;
70 		/* Remove VLAN filter from list. */
71 		--priv->vlan_filter_n;
72 		memmove(&priv->vlan_filter[i],
73 			&priv->vlan_filter[i + 1],
74 			sizeof(priv->vlan_filter[i]) *
75 			(priv->vlan_filter_n - i));
76 		priv->vlan_filter[priv->vlan_filter_n] = 0;
77 	} else {
78 		assert(i == priv->vlan_filter_n);
79 		/* Disabling an unknown VLAN filter has no effect. */
80 		if (!on)
81 			goto out;
82 		/* Add new VLAN filter. */
83 		priv->vlan_filter[priv->vlan_filter_n] = vlan_id;
84 		++priv->vlan_filter_n;
85 	}
86 out:
87 	if (dev->data->dev_started)
88 		return mlx5_traffic_restart(dev);
89 	return 0;
90 }
91 
92 /**
93  * Callback to set/reset VLAN stripping for a specific queue.
94  *
95  * @param dev
96  *   Pointer to Ethernet device structure.
97  * @param queue
98  *   RX queue index.
99  * @param on
100  *   Enable/disable VLAN stripping.
101  */
102 void
103 mlx5_vlan_strip_queue_set(struct rte_eth_dev *dev, uint16_t queue, int on)
104 {
105 	struct priv *priv = dev->data->dev_private;
106 	struct mlx5_rxq_data *rxq = (*priv->rxqs)[queue];
107 	struct mlx5_rxq_ctrl *rxq_ctrl =
108 		container_of(rxq, struct mlx5_rxq_ctrl, rxq);
109 	struct ibv_wq_attr mod;
110 	uint16_t vlan_offloads =
111 		(on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
112 		0;
113 	int ret;
114 
115 	/* Validate hw support */
116 	if (!priv->config.hw_vlan_strip) {
117 		DRV_LOG(ERR, "port %u VLAN stripping is not supported",
118 			dev->data->port_id);
119 		return;
120 	}
121 	/* Validate queue number */
122 	if (queue >= priv->rxqs_n) {
123 		DRV_LOG(ERR, "port %u VLAN stripping, invalid queue number %d",
124 			dev->data->port_id, queue);
125 		return;
126 	}
127 	DRV_LOG(DEBUG, "port %u set VLAN offloads 0x%x for port %uqueue %d",
128 		dev->data->port_id, vlan_offloads, rxq->port_id, queue);
129 	if (!rxq_ctrl->ibv) {
130 		/* Update related bits in RX queue. */
131 		rxq->vlan_strip = !!on;
132 		return;
133 	}
134 	mod = (struct ibv_wq_attr){
135 		.attr_mask = IBV_WQ_ATTR_FLAGS,
136 		.flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
137 		.flags = vlan_offloads,
138 	};
139 	ret = mlx5_glue->modify_wq(rxq_ctrl->ibv->wq, &mod);
140 	if (ret) {
141 		DRV_LOG(ERR, "port %u failed to modified stripping mode: %s",
142 			dev->data->port_id, strerror(rte_errno));
143 		return;
144 	}
145 	/* Update related bits in RX queue. */
146 	rxq->vlan_strip = !!on;
147 }
148 
149 /**
150  * Callback to set/reset VLAN offloads for a port.
151  *
152  * @param dev
153  *   Pointer to Ethernet device structure.
154  * @param mask
155  *   VLAN offload bit mask.
156  *
157  * @return
158  *   0 on success, a negative errno value otherwise and rte_errno is set.
159  */
160 int
161 mlx5_vlan_offload_set(struct rte_eth_dev *dev, int mask)
162 {
163 	struct priv *priv = dev->data->dev_private;
164 	unsigned int i;
165 
166 	if (mask & ETH_VLAN_STRIP_MASK) {
167 		int hw_vlan_strip = !!(dev->data->dev_conf.rxmode.offloads &
168 				       DEV_RX_OFFLOAD_VLAN_STRIP);
169 
170 		if (!priv->config.hw_vlan_strip) {
171 			DRV_LOG(ERR, "port %u VLAN stripping is not supported",
172 				dev->data->port_id);
173 			return 0;
174 		}
175 		/* Run on every RX queue and set/reset VLAN stripping. */
176 		for (i = 0; (i != priv->rxqs_n); i++)
177 			mlx5_vlan_strip_queue_set(dev, i, hw_vlan_strip);
178 	}
179 	return 0;
180 }
181