net/failsafe: support flow API
[dpdk.git] / drivers / net / failsafe / failsafe.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 <rte_alarm.h>
35 #include <rte_malloc.h>
36 #include <rte_ethdev.h>
37 #include <rte_ethdev_vdev.h>
38 #include <rte_devargs.h>
39 #include <rte_kvargs.h>
40 #include <rte_vdev.h>
41
42 #include "failsafe_private.h"
43
44 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
45 static const struct rte_eth_link eth_link = {
46         .link_speed = ETH_SPEED_NUM_10G,
47         .link_duplex = ETH_LINK_FULL_DUPLEX,
48         .link_status = ETH_LINK_UP,
49         .link_autoneg = ETH_LINK_SPEED_AUTONEG,
50 };
51
52 static int
53 fs_sub_device_alloc(struct rte_eth_dev *dev,
54                 const char *params)
55 {
56         uint8_t nb_subs;
57         int ret;
58
59         ret = failsafe_args_count_subdevice(dev, params);
60         if (ret)
61                 return ret;
62         if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
63                 ERROR("Cannot allocate more than %d ports",
64                         FAILSAFE_MAX_ETHPORTS);
65                 return -ENOSPC;
66         }
67         nb_subs = PRIV(dev)->subs_tail;
68         PRIV(dev)->subs = rte_zmalloc(NULL,
69                         sizeof(struct sub_device) * nb_subs,
70                         RTE_CACHE_LINE_SIZE);
71         if (PRIV(dev)->subs == NULL) {
72                 ERROR("Could not allocate sub_devices");
73                 return -ENOMEM;
74         }
75         return 0;
76 }
77
78 static void
79 fs_sub_device_free(struct rte_eth_dev *dev)
80 {
81         rte_free(PRIV(dev)->subs);
82 }
83
84 static void fs_hotplug_alarm(void *arg);
85
86 int
87 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
88 {
89         int ret;
90
91         if (dev == NULL)
92                 return -EINVAL;
93         if (PRIV(dev)->pending_alarm)
94                 return 0;
95         ret = rte_eal_alarm_set(hotplug_poll * 1000,
96                                 fs_hotplug_alarm,
97                                 dev);
98         if (ret) {
99                 ERROR("Could not set up plug-in event detection");
100                 return ret;
101         }
102         PRIV(dev)->pending_alarm = 1;
103         return 0;
104 }
105
106 int
107 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
108 {
109         int ret = 0;
110
111         if (PRIV(dev)->pending_alarm) {
112                 rte_errno = 0;
113                 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
114                 if (rte_errno) {
115                         ERROR("rte_eal_alarm_cancel failed (errno: %s)",
116                               strerror(rte_errno));
117                         ret = -rte_errno;
118                 } else {
119                         PRIV(dev)->pending_alarm = 0;
120                 }
121         }
122         return ret;
123 }
124
125 static void
126 fs_hotplug_alarm(void *arg)
127 {
128         struct rte_eth_dev *dev = arg;
129         struct sub_device *sdev;
130         int ret;
131         uint8_t i;
132
133         if (!PRIV(dev)->pending_alarm)
134                 return;
135         PRIV(dev)->pending_alarm = 0;
136         FOREACH_SUBDEV(sdev, i, dev)
137                 if (sdev->state != PRIV(dev)->state)
138                         break;
139         /* if we have non-probed device */
140         if (i != PRIV(dev)->subs_tail) {
141                 ret = failsafe_eth_dev_state_sync(dev);
142                 if (ret)
143                         ERROR("Unable to synchronize sub_device state");
144         }
145         ret = failsafe_hotplug_alarm_install(dev);
146         if (ret)
147                 ERROR("Unable to set up next alarm");
148 }
149
150 static int
151 fs_eth_dev_create(struct rte_vdev_device *vdev)
152 {
153         struct rte_eth_dev *dev;
154         struct ether_addr *mac;
155         struct fs_priv *priv;
156         struct sub_device *sdev;
157         const char *params;
158         unsigned int socket_id;
159         uint8_t i;
160         int ret;
161
162         dev = NULL;
163         priv = NULL;
164         socket_id = rte_socket_id();
165         INFO("Creating fail-safe device on NUMA socket %u", socket_id);
166         params = rte_vdev_device_args(vdev);
167         if (params == NULL) {
168                 ERROR("This PMD requires sub-devices, none provided");
169                 return -1;
170         }
171         dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
172         if (dev == NULL) {
173                 ERROR("Unable to allocate rte_eth_dev");
174                 return -1;
175         }
176         priv = PRIV(dev);
177         priv->dev = dev;
178         dev->dev_ops = &failsafe_ops;
179         dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
180         dev->data->dev_link = eth_link;
181         PRIV(dev)->nb_mac_addr = 1;
182         TAILQ_INIT(&PRIV(dev)->flow_list);
183         dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
184         dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
185         ret = fs_sub_device_alloc(dev, params);
186         if (ret) {
187                 ERROR("Could not allocate sub_devices");
188                 goto free_dev;
189         }
190         ret = failsafe_args_parse(dev, params);
191         if (ret)
192                 goto free_subs;
193         ret = failsafe_eal_init(dev);
194         if (ret)
195                 goto free_args;
196         ret = failsafe_hotplug_alarm_install(dev);
197         if (ret) {
198                 ERROR("Could not set up plug-in event detection");
199                 goto free_args;
200         }
201         mac = &dev->data->mac_addrs[0];
202         if (mac_from_arg) {
203                 /*
204                  * If MAC address was provided as a parameter,
205                  * apply to all probed slaves.
206                  */
207                 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
208                         ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
209                                                                mac);
210                         if (ret) {
211                                 ERROR("Failed to set default MAC address");
212                                 goto free_args;
213                         }
214                 }
215         } else {
216                 /*
217                  * Use the ether_addr from first probed
218                  * device, either preferred or fallback.
219                  */
220                 FOREACH_SUBDEV(sdev, i, dev)
221                         if (sdev->state >= DEV_PROBED) {
222                                 ether_addr_copy(&ETH(sdev)->data->mac_addrs[0],
223                                                 mac);
224                                 break;
225                         }
226                 /*
227                  * If no device has been probed and no ether_addr
228                  * has been provided on the command line, use a random
229                  * valid one.
230                  * It will be applied during future slave state syncs to
231                  * probed slaves.
232                  */
233                 if (i == priv->subs_tail)
234                         eth_random_addr(&mac->addr_bytes[0]);
235         }
236         INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
237                 mac->addr_bytes[0], mac->addr_bytes[1],
238                 mac->addr_bytes[2], mac->addr_bytes[3],
239                 mac->addr_bytes[4], mac->addr_bytes[5]);
240         return 0;
241 free_args:
242         failsafe_args_free(dev);
243 free_subs:
244         fs_sub_device_free(dev);
245 free_dev:
246         rte_free(PRIV(dev));
247         rte_eth_dev_release_port(dev);
248         return -1;
249 }
250
251 static int
252 fs_rte_eth_free(const char *name)
253 {
254         struct rte_eth_dev *dev;
255         int ret;
256
257         dev = rte_eth_dev_allocated(name);
258         if (dev == NULL)
259                 return -ENODEV;
260         ret = failsafe_eal_uninit(dev);
261         if (ret)
262                 ERROR("Error while uninitializing sub-EAL");
263         failsafe_args_free(dev);
264         fs_sub_device_free(dev);
265         rte_free(PRIV(dev));
266         rte_eth_dev_release_port(dev);
267         return ret;
268 }
269
270 static int
271 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
272 {
273         const char *name;
274
275         name = rte_vdev_device_name(vdev);
276         INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
277                         name);
278         return fs_eth_dev_create(vdev);
279 }
280
281 static int
282 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
283 {
284         const char *name;
285
286         name = rte_vdev_device_name(vdev);
287         INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
288         return fs_rte_eth_free(name);
289 }
290
291 static struct rte_vdev_driver failsafe_drv = {
292         .probe = rte_pmd_failsafe_probe,
293         .remove = rte_pmd_failsafe_remove,
294 };
295
296 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
297 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);