net/failsafe: support link status change event
[dpdk.git] / drivers / net / failsafe / failsafe_ether.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 6WIND S.A.
5  *   Copyright 2017 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 <unistd.h>
35
36 #include <rte_flow.h>
37 #include <rte_flow_driver.h>
38
39 #include "failsafe_private.h"
40
41 /** Print a message out of a flow error. */
42 static int
43 fs_flow_complain(struct rte_flow_error *error)
44 {
45         static const char *const errstrlist[] = {
46                 [RTE_FLOW_ERROR_TYPE_NONE] = "no error",
47                 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified",
48                 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)",
49                 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field",
50                 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field",
51                 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field",
52                 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field",
53                 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure",
54                 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length",
55                 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item",
56                 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions",
57                 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action",
58         };
59         const char *errstr;
60         char buf[32];
61         int err = rte_errno;
62
63         if ((unsigned int)error->type >= RTE_DIM(errstrlist) ||
64                         !errstrlist[error->type])
65                 errstr = "unknown type";
66         else
67                 errstr = errstrlist[error->type];
68         ERROR("Caught error type %d (%s): %s%s\n",
69                 error->type, errstr,
70                 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ",
71                                 error->cause), buf) : "",
72                 error->message ? error->message : "(no stated reason)");
73         return -err;
74 }
75
76 static int
77 fs_eth_dev_conf_apply(struct rte_eth_dev *dev,
78                 struct sub_device *sdev)
79 {
80         struct rte_eth_dev *edev;
81         struct rte_vlan_filter_conf *vfc1;
82         struct rte_vlan_filter_conf *vfc2;
83         struct rte_flow *flow;
84         struct rte_flow_error ferror;
85         uint32_t i;
86         int ret;
87
88         edev = ETH(sdev);
89         /* RX queue setup */
90         for (i = 0; i < dev->data->nb_rx_queues; i++) {
91                 struct rxq *rxq;
92
93                 rxq = dev->data->rx_queues[i];
94                 ret = rte_eth_rx_queue_setup(PORT_ID(sdev), i,
95                                 rxq->info.nb_desc, rxq->socket_id,
96                                 &rxq->info.conf, rxq->info.mp);
97                 if (ret) {
98                         ERROR("rx_queue_setup failed");
99                         return ret;
100                 }
101         }
102         /* TX queue setup */
103         for (i = 0; i < dev->data->nb_tx_queues; i++) {
104                 struct txq *txq;
105
106                 txq = dev->data->tx_queues[i];
107                 ret = rte_eth_tx_queue_setup(PORT_ID(sdev), i,
108                                 txq->info.nb_desc, txq->socket_id,
109                                 &txq->info.conf);
110                 if (ret) {
111                         ERROR("tx_queue_setup failed");
112                         return ret;
113                 }
114         }
115         /* dev_link.link_status */
116         if (dev->data->dev_link.link_status !=
117             edev->data->dev_link.link_status) {
118                 DEBUG("Configuring link_status");
119                 if (dev->data->dev_link.link_status)
120                         ret = rte_eth_dev_set_link_up(PORT_ID(sdev));
121                 else
122                         ret = rte_eth_dev_set_link_down(PORT_ID(sdev));
123                 if (ret) {
124                         ERROR("Failed to apply link_status");
125                         return ret;
126                 }
127         } else {
128                 DEBUG("link_status already set");
129         }
130         /* promiscuous */
131         if (dev->data->promiscuous != edev->data->promiscuous) {
132                 DEBUG("Configuring promiscuous");
133                 if (dev->data->promiscuous)
134                         rte_eth_promiscuous_enable(PORT_ID(sdev));
135                 else
136                         rte_eth_promiscuous_disable(PORT_ID(sdev));
137         } else {
138                 DEBUG("promiscuous already set");
139         }
140         /* all_multicast */
141         if (dev->data->all_multicast != edev->data->all_multicast) {
142                 DEBUG("Configuring all_multicast");
143                 if (dev->data->all_multicast)
144                         rte_eth_allmulticast_enable(PORT_ID(sdev));
145                 else
146                         rte_eth_allmulticast_disable(PORT_ID(sdev));
147         } else {
148                 DEBUG("all_multicast already set");
149         }
150         /* MTU */
151         if (dev->data->mtu != edev->data->mtu) {
152                 DEBUG("Configuring MTU");
153                 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), dev->data->mtu);
154                 if (ret) {
155                         ERROR("Failed to apply MTU");
156                         return ret;
157                 }
158         } else {
159                 DEBUG("MTU already set");
160         }
161         /* default MAC */
162         DEBUG("Configuring default MAC address");
163         ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
164                         &dev->data->mac_addrs[0]);
165         if (ret) {
166                 ERROR("Setting default MAC address failed");
167                 return ret;
168         }
169         /* additional MAC */
170         if (PRIV(dev)->nb_mac_addr > 1)
171                 DEBUG("Configure additional MAC address%s",
172                         (PRIV(dev)->nb_mac_addr > 2 ? "es" : ""));
173         for (i = 1; i < PRIV(dev)->nb_mac_addr; i++) {
174                 struct ether_addr *ea;
175
176                 ea = &dev->data->mac_addrs[i];
177                 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), ea,
178                                 PRIV(dev)->mac_addr_pool[i]);
179                 if (ret) {
180                         char ea_fmt[ETHER_ADDR_FMT_SIZE];
181
182                         ether_format_addr(ea_fmt, ETHER_ADDR_FMT_SIZE, ea);
183                         ERROR("Adding MAC address %s failed", ea_fmt);
184                 }
185         }
186         /* VLAN filter */
187         vfc1 = &dev->data->vlan_filter_conf;
188         vfc2 = &edev->data->vlan_filter_conf;
189         if (memcmp(vfc1, vfc2, sizeof(struct rte_vlan_filter_conf))) {
190                 uint64_t vbit;
191                 uint64_t ids;
192                 size_t i;
193                 uint16_t vlan_id;
194
195                 DEBUG("Configuring VLAN filter");
196                 for (i = 0; i < RTE_DIM(vfc1->ids); i++) {
197                         if (vfc1->ids[i] == 0)
198                                 continue;
199                         ids = vfc1->ids[i];
200                         while (ids) {
201                                 vlan_id = 64 * i;
202                                 /* count trailing zeroes */
203                                 vbit = ~ids & (ids - 1);
204                                 /* clear least significant bit set */
205                                 ids ^= (ids ^ (ids - 1)) ^ vbit;
206                                 for (; vbit; vlan_id++)
207                                         vbit >>= 1;
208                                 ret = rte_eth_dev_vlan_filter(
209                                         PORT_ID(sdev), vlan_id, 1);
210                                 if (ret) {
211                                         ERROR("Failed to apply VLAN filter %hu",
212                                                 vlan_id);
213                                         return ret;
214                                 }
215                         }
216                 }
217         } else {
218                 DEBUG("VLAN filter already set");
219         }
220         /* rte_flow */
221         if (TAILQ_EMPTY(&PRIV(dev)->flow_list)) {
222                 DEBUG("rte_flow already set");
223         } else {
224                 DEBUG("Resetting rte_flow configuration");
225                 ret = rte_flow_flush(PORT_ID(sdev), &ferror);
226                 if (ret) {
227                         fs_flow_complain(&ferror);
228                         return ret;
229                 }
230                 i = 0;
231                 rte_errno = 0;
232                 DEBUG("Configuring rte_flow");
233                 TAILQ_FOREACH(flow, &PRIV(dev)->flow_list, next) {
234                         DEBUG("Creating flow #%" PRIu32, i++);
235                         flow->flows[SUB_ID(sdev)] =
236                                 rte_flow_create(PORT_ID(sdev),
237                                                 &flow->fd->attr,
238                                                 flow->fd->items,
239                                                 flow->fd->actions,
240                                                 &ferror);
241                         ret = rte_errno;
242                         if (ret)
243                                 break;
244                 }
245                 if (ret) {
246                         fs_flow_complain(&ferror);
247                         return ret;
248                 }
249         }
250         return 0;
251 }
252
253 static void
254 fs_dev_remove(struct sub_device *sdev)
255 {
256         int ret;
257
258         if (sdev == NULL)
259                 return;
260         switch (sdev->state) {
261         case DEV_STARTED:
262                 rte_eth_dev_stop(PORT_ID(sdev));
263                 sdev->state = DEV_ACTIVE;
264                 /* fallthrough */
265         case DEV_ACTIVE:
266                 rte_eth_dev_close(PORT_ID(sdev));
267                 sdev->state = DEV_PROBED;
268                 /* fallthrough */
269         case DEV_PROBED:
270                 ret = rte_eal_hotplug_remove(sdev->bus->name,
271                                              sdev->dev->name);
272                 if (ret) {
273                         ERROR("Bus detach failed for sub_device %u",
274                               SUB_ID(sdev));
275                 } else {
276                         ETH(sdev)->state = RTE_ETH_DEV_UNUSED;
277                 }
278                 sdev->state = DEV_PARSED;
279                 /* fallthrough */
280         case DEV_PARSED:
281         case DEV_UNDEFINED:
282                 sdev->state = DEV_UNDEFINED;
283                 /* the end */
284                 break;
285         }
286         failsafe_hotplug_alarm_install(sdev->fs_dev);
287 }
288
289 static inline int
290 fs_rxtx_clean(struct sub_device *sdev)
291 {
292         uint16_t i;
293
294         for (i = 0; i < ETH(sdev)->data->nb_rx_queues; i++)
295                 if (FS_ATOMIC_RX(sdev, i))
296                         return 0;
297         for (i = 0; i < ETH(sdev)->data->nb_tx_queues; i++)
298                 if (FS_ATOMIC_TX(sdev, i))
299                         return 0;
300         return 1;
301 }
302
303 void
304 failsafe_dev_remove(struct rte_eth_dev *dev)
305 {
306         struct sub_device *sdev;
307         uint8_t i;
308
309         FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE)
310                 if (sdev->remove && fs_rxtx_clean(sdev))
311                         fs_dev_remove(sdev);
312 }
313
314 int
315 failsafe_eth_dev_state_sync(struct rte_eth_dev *dev)
316 {
317         struct sub_device *sdev;
318         uint32_t inactive;
319         int ret;
320         uint8_t i;
321
322         if (PRIV(dev)->state < DEV_PARSED)
323                 return 0;
324
325         ret = failsafe_args_parse_subs(dev);
326         if (ret)
327                 goto err_remove;
328
329         if (PRIV(dev)->state < DEV_PROBED)
330                 return 0;
331         ret = failsafe_eal_init(dev);
332         if (ret)
333                 goto err_remove;
334         if (PRIV(dev)->state < DEV_ACTIVE)
335                 return 0;
336         inactive = 0;
337         FOREACH_SUBDEV(sdev, i, dev)
338                 if (sdev->state == DEV_PROBED)
339                         inactive |= UINT32_C(1) << i;
340         ret = dev->dev_ops->dev_configure(dev);
341         if (ret)
342                 goto err_remove;
343         FOREACH_SUBDEV(sdev, i, dev) {
344                 if (inactive & (UINT32_C(1) << i)) {
345                         ret = fs_eth_dev_conf_apply(dev, sdev);
346                         if (ret) {
347                                 ERROR("Could not apply configuration to sub_device %d",
348                                       i);
349                                 goto err_remove;
350                         }
351                 }
352         }
353         /*
354          * If new devices have been configured, check if
355          * the link state has changed.
356          */
357         if (inactive)
358                 dev->dev_ops->link_update(dev, 1);
359         if (PRIV(dev)->state < DEV_STARTED)
360                 return 0;
361         ret = dev->dev_ops->dev_start(dev);
362         if (ret)
363                 goto err_remove;
364         return 0;
365 err_remove:
366         FOREACH_SUBDEV(sdev, i, dev)
367                 if (sdev->state != PRIV(dev)->state)
368                         sdev->remove = 1;
369         return ret;
370 }
371
372 int
373 failsafe_eth_rmv_event_callback(uint8_t port_id __rte_unused,
374                                 enum rte_eth_event_type event __rte_unused,
375                                 void *cb_arg, void *out __rte_unused)
376 {
377         struct sub_device *sdev = cb_arg;
378
379         /* Switch as soon as possible tx_dev. */
380         fs_switch_dev(sdev->fs_dev, sdev);
381         /* Use safe bursts in any case. */
382         set_burst_fn(sdev->fs_dev, 1);
383         /*
384          * Async removal, the sub-PMD will try to unregister
385          * the callback at the source of the current thread context.
386          */
387         sdev->remove = 1;
388         return 0;
389 }
390
391 int
392 failsafe_eth_lsc_event_callback(uint8_t port_id __rte_unused,
393                                 enum rte_eth_event_type event __rte_unused,
394                                 void *cb_arg, void *out __rte_unused)
395 {
396         struct rte_eth_dev *dev = cb_arg;
397         int ret;
398
399         ret = dev->dev_ops->link_update(dev, 0);
400         /* We must pass on the LSC event */
401         if (ret)
402                 return _rte_eth_dev_callback_process(dev,
403                                                      RTE_ETH_EVENT_INTR_LSC,
404                                                      NULL, NULL);
405         else
406                 return 0;
407 }