net/mlx5: fix TCI mask filter
[dpdk.git] / drivers / net / mlx5 / mlx5_trigger.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5
6 #include <unistd.h>
7
8 #include <rte_ether.h>
9 #include <rte_ethdev_driver.h>
10 #include <rte_interrupts.h>
11 #include <rte_alarm.h>
12
13 #include "mlx5.h"
14 #include "mlx5_rxtx.h"
15 #include "mlx5_utils.h"
16
17 /**
18  * Stop traffic on Tx queues.
19  *
20  * @param dev
21  *   Pointer to Ethernet device structure.
22  */
23 static void
24 mlx5_txq_stop(struct rte_eth_dev *dev)
25 {
26         struct priv *priv = dev->data->dev_private;
27         unsigned int i;
28
29         for (i = 0; i != priv->txqs_n; ++i)
30                 mlx5_txq_release(dev, i);
31 }
32
33 /**
34  * Start traffic on Tx queues.
35  *
36  * @param dev
37  *   Pointer to Ethernet device structure.
38  *
39  * @return
40  *   0 on success, a negative errno value otherwise and rte_errno is set.
41  */
42 static int
43 mlx5_txq_start(struct rte_eth_dev *dev)
44 {
45         struct priv *priv = dev->data->dev_private;
46         unsigned int i;
47         int ret;
48
49         /* Add memory regions to Tx queues. */
50         for (i = 0; i != priv->txqs_n; ++i) {
51                 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
52
53                 if (!txq_ctrl)
54                         continue;
55                 txq_alloc_elts(txq_ctrl);
56                 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
57                 if (!txq_ctrl->ibv) {
58                         rte_errno = ENOMEM;
59                         goto error;
60                 }
61         }
62         ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd);
63         if (ret)
64                 goto error;
65         return 0;
66 error:
67         ret = rte_errno; /* Save rte_errno before cleanup. */
68         mlx5_txq_stop(dev);
69         rte_errno = ret; /* Restore rte_errno. */
70         return -rte_errno;
71 }
72
73 /**
74  * Stop traffic on Rx queues.
75  *
76  * @param dev
77  *   Pointer to Ethernet device structure.
78  */
79 static void
80 mlx5_rxq_stop(struct rte_eth_dev *dev)
81 {
82         struct priv *priv = dev->data->dev_private;
83         unsigned int i;
84
85         for (i = 0; i != priv->rxqs_n; ++i)
86                 mlx5_rxq_release(dev, i);
87 }
88
89 /**
90  * Start traffic on Rx queues.
91  *
92  * @param dev
93  *   Pointer to Ethernet device structure.
94  *
95  * @return
96  *   0 on success, a negative errno value otherwise and rte_errno is set.
97  */
98 static int
99 mlx5_rxq_start(struct rte_eth_dev *dev)
100 {
101         struct priv *priv = dev->data->dev_private;
102         unsigned int i;
103         int ret = 0;
104
105         /* Allocate/reuse/resize mempool for Multi-Packet RQ. */
106         if (mlx5_mprq_alloc_mp(dev))
107                 goto error;
108         for (i = 0; i != priv->rxqs_n; ++i) {
109                 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
110                 struct rte_mempool *mp;
111
112                 if (!rxq_ctrl)
113                         continue;
114                 /* Pre-register Rx mempool. */
115                 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ?
116                      rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp;
117                 DRV_LOG(DEBUG,
118                         "port %u Rx queue %u registering"
119                         " mp %s having %u chunks",
120                         dev->data->port_id, rxq_ctrl->idx,
121                         mp->name, mp->nb_mem_chunks);
122                 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp);
123                 ret = rxq_alloc_elts(rxq_ctrl);
124                 if (ret)
125                         goto error;
126                 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
127                 if (!rxq_ctrl->ibv)
128                         goto error;
129         }
130         return 0;
131 error:
132         ret = rte_errno; /* Save rte_errno before cleanup. */
133         mlx5_rxq_stop(dev);
134         rte_errno = ret; /* Restore rte_errno. */
135         return -rte_errno;
136 }
137
138 /**
139  * DPDK callback to start the device.
140  *
141  * Simulate device start by attaching all configured flows.
142  *
143  * @param dev
144  *   Pointer to Ethernet device structure.
145  *
146  * @return
147  *   0 on success, a negative errno value otherwise and rte_errno is set.
148  */
149 int
150 mlx5_dev_start(struct rte_eth_dev *dev)
151 {
152         struct priv *priv = dev->data->dev_private;
153         int ret;
154
155         dev->data->dev_started = 1;
156         DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues",
157                 dev->data->port_id);
158         ret = mlx5_txq_start(dev);
159         if (ret) {
160                 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
161                         dev->data->port_id, strerror(rte_errno));
162                 goto error;
163         }
164         ret = mlx5_rxq_start(dev);
165         if (ret) {
166                 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
167                         dev->data->port_id, strerror(rte_errno));
168                 goto error;
169         }
170         ret = mlx5_rx_intr_vec_enable(dev);
171         if (ret) {
172                 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
173                         dev->data->port_id);
174                 goto error;
175         }
176         mlx5_xstats_init(dev);
177         ret = mlx5_traffic_enable(dev);
178         if (ret) {
179                 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
180                         dev->data->port_id);
181                 goto error;
182         }
183         ret = mlx5_flow_start(dev, &priv->flows);
184         if (ret) {
185                 DRV_LOG(DEBUG, "port %u failed to set flows",
186                         dev->data->port_id);
187                 goto error;
188         }
189         dev->tx_pkt_burst = mlx5_select_tx_function(dev);
190         dev->rx_pkt_burst = mlx5_select_rx_function(dev);
191         mlx5_dev_interrupt_handler_install(dev);
192         return 0;
193 error:
194         ret = rte_errno; /* Save rte_errno before cleanup. */
195         /* Rollback. */
196         dev->data->dev_started = 0;
197         mlx5_flow_stop(dev, &priv->flows);
198         mlx5_traffic_disable(dev);
199         mlx5_txq_stop(dev);
200         mlx5_rxq_stop(dev);
201         rte_errno = ret; /* Restore rte_errno. */
202         return -rte_errno;
203 }
204
205 /**
206  * DPDK callback to stop the device.
207  *
208  * Simulate device stop by detaching all configured flows.
209  *
210  * @param dev
211  *   Pointer to Ethernet device structure.
212  */
213 void
214 mlx5_dev_stop(struct rte_eth_dev *dev)
215 {
216         struct priv *priv = dev->data->dev_private;
217
218         dev->data->dev_started = 0;
219         /* Prevent crashes when queues are still in use. */
220         dev->rx_pkt_burst = removed_rx_burst;
221         dev->tx_pkt_burst = removed_tx_burst;
222         rte_wmb();
223         usleep(1000 * priv->rxqs_n);
224         DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues",
225                 dev->data->port_id);
226         mlx5_flow_stop(dev, &priv->flows);
227         mlx5_traffic_disable(dev);
228         mlx5_rx_intr_vec_disable(dev);
229         mlx5_dev_interrupt_handler_uninstall(dev);
230         mlx5_txq_stop(dev);
231         mlx5_rxq_stop(dev);
232 }
233
234 /**
235  * Enable traffic flows configured by control plane
236  *
237  * @param dev
238  *   Pointer to Ethernet device private data.
239  * @param dev
240  *   Pointer to Ethernet device structure.
241  *
242  * @return
243  *   0 on success, a negative errno value otherwise and rte_errno is set.
244  */
245 int
246 mlx5_traffic_enable(struct rte_eth_dev *dev)
247 {
248         struct priv *priv = dev->data->dev_private;
249         struct rte_flow_item_eth bcast = {
250                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
251         };
252         struct rte_flow_item_eth ipv6_multi_spec = {
253                 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
254         };
255         struct rte_flow_item_eth ipv6_multi_mask = {
256                 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
257         };
258         struct rte_flow_item_eth unicast = {
259                 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
260         };
261         struct rte_flow_item_eth unicast_mask = {
262                 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
263         };
264         const unsigned int vlan_filter_n = priv->vlan_filter_n;
265         const struct ether_addr cmp = {
266                 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
267         };
268         unsigned int i;
269         unsigned int j;
270         int ret;
271
272         if (priv->isolated)
273                 return 0;
274         if (dev->data->promiscuous) {
275                 struct rte_flow_item_eth promisc = {
276                         .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
277                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
278                         .type = 0,
279                 };
280
281                 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
282                 if (ret)
283                         goto error;
284         }
285         if (dev->data->all_multicast) {
286                 struct rte_flow_item_eth multicast = {
287                         .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
288                         .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
289                         .type = 0,
290                 };
291
292                 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
293                 if (ret)
294                         goto error;
295         } else {
296                 /* Add broadcast/multicast flows. */
297                 for (i = 0; i != vlan_filter_n; ++i) {
298                         uint16_t vlan = priv->vlan_filter[i];
299
300                         struct rte_flow_item_vlan vlan_spec = {
301                                 .tci = rte_cpu_to_be_16(vlan),
302                         };
303                         struct rte_flow_item_vlan vlan_mask =
304                                 rte_flow_item_vlan_mask;
305
306                         ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
307                                                   &vlan_spec, &vlan_mask);
308                         if (ret)
309                                 goto error;
310                         ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
311                                                   &ipv6_multi_mask,
312                                                   &vlan_spec, &vlan_mask);
313                         if (ret)
314                                 goto error;
315                 }
316                 if (!vlan_filter_n) {
317                         ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
318                         if (ret)
319                                 goto error;
320                         ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
321                                              &ipv6_multi_mask);
322                         if (ret)
323                                 goto error;
324                 }
325         }
326         /* Add MAC address flows. */
327         for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
328                 struct ether_addr *mac = &dev->data->mac_addrs[i];
329
330                 if (!memcmp(mac, &cmp, sizeof(*mac)))
331                         continue;
332                 memcpy(&unicast.dst.addr_bytes,
333                        mac->addr_bytes,
334                        ETHER_ADDR_LEN);
335                 for (j = 0; j != vlan_filter_n; ++j) {
336                         uint16_t vlan = priv->vlan_filter[j];
337
338                         struct rte_flow_item_vlan vlan_spec = {
339                                 .tci = rte_cpu_to_be_16(vlan),
340                         };
341                         struct rte_flow_item_vlan vlan_mask =
342                                 rte_flow_item_vlan_mask;
343
344                         ret = mlx5_ctrl_flow_vlan(dev, &unicast,
345                                                   &unicast_mask,
346                                                   &vlan_spec,
347                                                   &vlan_mask);
348                         if (ret)
349                                 goto error;
350                 }
351                 if (!vlan_filter_n) {
352                         ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
353                         if (ret)
354                                 goto error;
355                 }
356         }
357         return 0;
358 error:
359         ret = rte_errno; /* Save rte_errno before cleanup. */
360         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
361         rte_errno = ret; /* Restore rte_errno. */
362         return -rte_errno;
363 }
364
365
366 /**
367  * Disable traffic flows configured by control plane
368  *
369  * @param dev
370  *   Pointer to Ethernet device private data.
371  */
372 void
373 mlx5_traffic_disable(struct rte_eth_dev *dev)
374 {
375         struct priv *priv = dev->data->dev_private;
376
377         mlx5_flow_list_flush(dev, &priv->ctrl_flows);
378 }
379
380 /**
381  * Restart traffic flows configured by control plane
382  *
383  * @param dev
384  *   Pointer to Ethernet device private data.
385  *
386  * @return
387  *   0 on success, a negative errno value otherwise and rte_errno is set.
388  */
389 int
390 mlx5_traffic_restart(struct rte_eth_dev *dev)
391 {
392         if (dev->data->dev_started) {
393                 mlx5_traffic_disable(dev);
394                 return mlx5_traffic_enable(dev);
395         }
396         return 0;
397 }