1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
9 #include <rte_malloc.h>
10 #include <rte_ethdev_driver.h>
11 #include <rte_ethdev_vdev.h>
12 #include <rte_devargs.h>
13 #include <rte_kvargs.h>
14 #include <rte_bus_vdev.h>
16 #include "failsafe_private.h"
20 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
21 static const struct rte_eth_link eth_link = {
22 .link_speed = ETH_SPEED_NUM_10G,
23 .link_duplex = ETH_LINK_FULL_DUPLEX,
24 .link_status = ETH_LINK_UP,
25 .link_autoneg = ETH_LINK_AUTONEG,
29 fs_sub_device_alloc(struct rte_eth_dev *dev,
35 struct sub_device *sdev;
36 uint8_t sdev_iterator;
38 ret = failsafe_args_count_subdevice(dev, params);
41 if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
42 ERROR("Cannot allocate more than %d ports",
43 FAILSAFE_MAX_ETHPORTS);
46 nb_subs = PRIV(dev)->subs_tail;
47 PRIV(dev)->subs = rte_zmalloc(NULL,
48 sizeof(struct sub_device) * nb_subs,
50 if (PRIV(dev)->subs == NULL) {
51 ERROR("Could not allocate sub_devices");
54 /* Initiate static sub devices linked list. */
55 for (i = 1; i < nb_subs; i++)
56 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i;
57 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs;
59 FOREACH_SUBDEV(sdev, sdev_iterator, dev) {
60 sdev->sdev_port_id = RTE_MAX_ETHPORTS;
66 fs_sub_device_free(struct rte_eth_dev *dev)
68 rte_free(PRIV(dev)->subs);
71 static void fs_hotplug_alarm(void *arg);
74 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
80 if (PRIV(dev)->pending_alarm)
82 ret = rte_eal_alarm_set(failsafe_hotplug_poll * 1000,
86 ERROR("Could not set up plug-in event detection");
89 PRIV(dev)->pending_alarm = 1;
94 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
99 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
101 ERROR("rte_eal_alarm_cancel failed (errno: %s)",
102 strerror(rte_errno));
105 PRIV(dev)->pending_alarm = 0;
111 fs_hotplug_alarm(void *arg)
113 struct rte_eth_dev *dev = arg;
114 struct sub_device *sdev;
118 if (!PRIV(dev)->pending_alarm)
120 PRIV(dev)->pending_alarm = 0;
121 FOREACH_SUBDEV(sdev, i, dev)
122 if (sdev->state != PRIV(dev)->state)
124 /* if we have non-probed device */
125 if (i != PRIV(dev)->subs_tail) {
126 if (fs_lock(dev, 1) != 0)
128 ret = failsafe_eth_dev_state_sync(dev);
131 ERROR("Unable to synchronize sub_device state");
133 failsafe_dev_remove(dev);
135 ret = failsafe_hotplug_alarm_install(dev);
137 ERROR("Unable to set up next alarm");
141 fs_mutex_init(struct fs_priv *priv)
144 pthread_mutexattr_t attr;
146 ret = pthread_mutexattr_init(&attr);
148 ERROR("Cannot initiate mutex attributes - %s", strerror(ret));
151 /* Allow mutex relocks for the thread holding the mutex. */
152 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
154 ERROR("Cannot set mutex type - %s", strerror(ret));
157 ret = pthread_mutex_init(&priv->hotplug_mutex, &attr);
159 ERROR("Cannot initiate mutex - %s", strerror(ret));
166 fs_eth_dev_create(struct rte_vdev_device *vdev)
168 struct rte_eth_dev *dev;
169 struct rte_ether_addr *mac;
170 struct fs_priv *priv;
171 struct sub_device *sdev;
173 unsigned int socket_id;
179 socket_id = rte_socket_id();
180 INFO("Creating fail-safe device on NUMA socket %u", socket_id);
181 params = rte_vdev_device_args(vdev);
182 if (params == NULL) {
183 ERROR("This PMD requires sub-devices, none provided");
186 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
188 ERROR("Unable to allocate rte_eth_dev");
192 priv->data = dev->data;
193 dev->dev_ops = &failsafe_ops;
194 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
195 dev->data->dev_link = eth_link;
196 PRIV(dev)->nb_mac_addr = 1;
197 TAILQ_INIT(&PRIV(dev)->flow_list);
198 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
199 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
200 ret = fs_sub_device_alloc(dev, params);
202 ERROR("Could not allocate sub_devices");
205 ret = failsafe_args_parse(dev, params);
208 ret = rte_eth_dev_owner_new(&priv->my_owner.id);
210 ERROR("Failed to get unique owner identifier");
213 snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
214 FAILSAFE_OWNER_NAME);
215 DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id,
216 priv->my_owner.name, priv->my_owner.id);
217 ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
218 failsafe_eth_new_event_callback,
221 ERROR("Failed to register NEW callback");
224 ret = failsafe_eal_init(dev);
226 goto unregister_new_callback;
227 ret = fs_mutex_init(priv);
229 goto unregister_new_callback;
230 ret = failsafe_hotplug_alarm_install(dev);
232 ERROR("Could not set up plug-in event detection");
233 goto unregister_new_callback;
235 mac = &dev->data->mac_addrs[0];
236 if (failsafe_mac_from_arg) {
238 * If MAC address was provided as a parameter,
239 * apply to all probed slaves.
241 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
242 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
245 ERROR("Failed to set default MAC address");
251 * Use the ether_addr from first probed
252 * device, either preferred or fallback.
254 FOREACH_SUBDEV(sdev, i, dev)
255 if (sdev->state >= DEV_PROBED) {
257 Ð(sdev)->data->mac_addrs[0], mac);
261 * If no device has been probed and no ether_addr
262 * has been provided on the command line, use a random
264 * It will be applied during future slave state syncs to
267 if (i == priv->subs_tail)
268 rte_eth_random_addr(&mac->addr_bytes[0]);
270 INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
271 mac->addr_bytes[0], mac->addr_bytes[1],
272 mac->addr_bytes[2], mac->addr_bytes[3],
273 mac->addr_bytes[4], mac->addr_bytes[5]);
274 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
275 PRIV(dev)->intr_handle = (struct rte_intr_handle){
277 .type = RTE_INTR_HANDLE_EXT,
279 rte_eth_dev_probing_finish(dev);
282 failsafe_hotplug_alarm_cancel(dev);
283 unregister_new_callback:
284 rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
285 failsafe_eth_new_event_callback, dev);
287 failsafe_args_free(dev);
289 fs_sub_device_free(dev);
291 /* mac_addrs must not be freed alone because part of dev_private */
292 dev->data->mac_addrs = NULL;
293 rte_eth_dev_release_port(dev);
298 fs_rte_eth_free(const char *name)
300 struct rte_eth_dev *dev;
303 dev = rte_eth_dev_allocated(name);
306 rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
307 failsafe_eth_new_event_callback, dev);
308 ret = failsafe_eal_uninit(dev);
310 ERROR("Error while uninitializing sub-EAL");
311 failsafe_args_free(dev);
312 fs_sub_device_free(dev);
313 ret = pthread_mutex_destroy(&PRIV(dev)->hotplug_mutex);
315 ERROR("Error while destroying hotplug mutex");
316 rte_free(PRIV(dev)->mcast_addrs);
317 /* mac_addrs must not be freed alone because part of dev_private */
318 dev->data->mac_addrs = NULL;
319 rte_eth_dev_release_port(dev);
324 devargs_already_listed(struct rte_devargs *devargs)
326 struct rte_devargs *list_da;
328 RTE_EAL_DEVARGS_FOREACH(devargs->bus->name, list_da) {
329 if (strcmp(list_da->name, devargs->name) == 0)
330 /* devargs already in the list */
337 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
340 struct rte_eth_dev *eth_dev;
341 struct sub_device *sdev;
342 struct rte_devargs devargs;
346 name = rte_vdev_device_name(vdev);
347 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
350 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
351 strlen(rte_vdev_device_args(vdev)) == 0) {
352 eth_dev = rte_eth_dev_attach_secondary(name);
354 ERROR("Failed to probe %s", name);
357 eth_dev->dev_ops = &failsafe_ops;
358 eth_dev->device = &vdev->device;
359 eth_dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
360 eth_dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
362 * Failsafe will attempt to probe all of its sub-devices.
363 * Any failure in sub-devices is not a fatal error.
364 * A sub-device can be plugged later.
366 FOREACH_SUBDEV(sdev, i, eth_dev) {
367 /* skip empty devargs */
368 if (sdev->devargs.name[0] == '\0')
371 /* rebuild devargs to be able to get the bus name. */
372 ret = rte_devargs_parse(&devargs,
375 ERROR("Failed to parse devargs %s",
379 if (!devargs_already_listed(&devargs)) {
380 ret = rte_dev_probe(devargs.name);
382 ERROR("Failed to probe devargs %s",
388 rte_eth_dev_probing_finish(eth_dev);
392 return fs_eth_dev_create(vdev);
396 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
400 name = rte_vdev_device_name(vdev);
401 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
402 return fs_rte_eth_free(name);
405 static struct rte_vdev_driver failsafe_drv = {
406 .probe = rte_pmd_failsafe_probe,
407 .remove = rte_pmd_failsafe_remove,
410 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
411 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);
413 RTE_INIT(failsafe_init_log)
415 failsafe_logtype = rte_log_register("pmd.net.failsafe");
416 if (failsafe_logtype >= 0)
417 rte_log_set_level(failsafe_logtype, RTE_LOG_NOTICE);