1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
7 #include <rte_malloc.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_ethdev_vdev.h>
10 #include <rte_devargs.h>
11 #include <rte_kvargs.h>
12 #include <rte_bus_vdev.h>
14 #include "failsafe_private.h"
18 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME;
19 static const struct rte_eth_link eth_link = {
20 .link_speed = ETH_SPEED_NUM_10G,
21 .link_duplex = ETH_LINK_FULL_DUPLEX,
22 .link_status = ETH_LINK_UP,
23 .link_autoneg = ETH_LINK_AUTONEG,
27 fs_sub_device_alloc(struct rte_eth_dev *dev,
33 struct sub_device *sdev;
34 uint8_t sdev_iterator;
36 ret = failsafe_args_count_subdevice(dev, params);
39 if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) {
40 ERROR("Cannot allocate more than %d ports",
41 FAILSAFE_MAX_ETHPORTS);
44 nb_subs = PRIV(dev)->subs_tail;
45 PRIV(dev)->subs = rte_zmalloc(NULL,
46 sizeof(struct sub_device) * nb_subs,
48 if (PRIV(dev)->subs == NULL) {
49 ERROR("Could not allocate sub_devices");
52 /* Initiate static sub devices linked list. */
53 for (i = 1; i < nb_subs; i++)
54 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i;
55 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs;
57 FOREACH_SUBDEV(sdev, sdev_iterator, dev) {
58 sdev->sdev_port_id = RTE_MAX_ETHPORTS;
64 fs_sub_device_free(struct rte_eth_dev *dev)
66 rte_free(PRIV(dev)->subs);
69 static void fs_hotplug_alarm(void *arg);
72 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev)
78 if (PRIV(dev)->pending_alarm)
80 ret = rte_eal_alarm_set(failsafe_hotplug_poll * 1000,
84 ERROR("Could not set up plug-in event detection");
87 PRIV(dev)->pending_alarm = 1;
92 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev)
97 rte_eal_alarm_cancel(fs_hotplug_alarm, dev);
99 ERROR("rte_eal_alarm_cancel failed (errno: %s)",
100 strerror(rte_errno));
103 PRIV(dev)->pending_alarm = 0;
109 fs_hotplug_alarm(void *arg)
111 struct rte_eth_dev *dev = arg;
112 struct sub_device *sdev;
116 if (!PRIV(dev)->pending_alarm)
118 PRIV(dev)->pending_alarm = 0;
119 FOREACH_SUBDEV(sdev, i, dev)
120 if (sdev->state != PRIV(dev)->state)
122 /* if we have non-probed device */
123 if (i != PRIV(dev)->subs_tail) {
124 if (fs_lock(dev, 1) != 0)
126 ret = failsafe_eth_dev_state_sync(dev);
129 ERROR("Unable to synchronize sub_device state");
131 failsafe_dev_remove(dev);
133 ret = failsafe_hotplug_alarm_install(dev);
135 ERROR("Unable to set up next alarm");
139 fs_mutex_init(struct fs_priv *priv)
142 pthread_mutexattr_t attr;
144 ret = pthread_mutexattr_init(&attr);
146 ERROR("Cannot initiate mutex attributes - %s", strerror(ret));
149 /* Allow mutex relocks for the thread holding the mutex. */
150 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
152 ERROR("Cannot set mutex type - %s", strerror(ret));
155 ret = pthread_mutex_init(&priv->hotplug_mutex, &attr);
157 ERROR("Cannot initiate mutex - %s", strerror(ret));
164 fs_eth_dev_create(struct rte_vdev_device *vdev)
166 struct rte_eth_dev *dev;
167 struct ether_addr *mac;
168 struct fs_priv *priv;
169 struct sub_device *sdev;
171 unsigned int socket_id;
177 socket_id = rte_socket_id();
178 INFO("Creating fail-safe device on NUMA socket %u", socket_id);
179 params = rte_vdev_device_args(vdev);
180 if (params == NULL) {
181 ERROR("This PMD requires sub-devices, none provided");
184 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv));
186 ERROR("Unable to allocate rte_eth_dev");
190 priv->data = dev->data;
191 dev->dev_ops = &failsafe_ops;
192 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0];
193 dev->data->dev_link = eth_link;
194 PRIV(dev)->nb_mac_addr = 1;
195 TAILQ_INIT(&PRIV(dev)->flow_list);
196 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst;
197 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst;
198 ret = fs_sub_device_alloc(dev, params);
200 ERROR("Could not allocate sub_devices");
203 ret = failsafe_args_parse(dev, params);
206 ret = rte_eth_dev_owner_new(&priv->my_owner.id);
208 ERROR("Failed to get unique owner identifier");
211 snprintf(priv->my_owner.name, sizeof(priv->my_owner.name),
212 FAILSAFE_OWNER_NAME);
213 DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id,
214 priv->my_owner.name, priv->my_owner.id);
215 ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
216 failsafe_eth_new_event_callback,
219 ERROR("Failed to register NEW callback");
222 ret = failsafe_eal_init(dev);
224 goto unregister_new_callback;
225 ret = fs_mutex_init(priv);
227 goto unregister_new_callback;
228 ret = failsafe_hotplug_alarm_install(dev);
230 ERROR("Could not set up plug-in event detection");
231 goto unregister_new_callback;
233 mac = &dev->data->mac_addrs[0];
234 if (failsafe_mac_from_arg) {
236 * If MAC address was provided as a parameter,
237 * apply to all probed slaves.
239 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) {
240 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev),
243 ERROR("Failed to set default MAC address");
249 * Use the ether_addr from first probed
250 * device, either preferred or fallback.
252 FOREACH_SUBDEV(sdev, i, dev)
253 if (sdev->state >= DEV_PROBED) {
254 ether_addr_copy(Ð(sdev)->data->mac_addrs[0],
259 * If no device has been probed and no ether_addr
260 * has been provided on the command line, use a random
262 * It will be applied during future slave state syncs to
265 if (i == priv->subs_tail)
266 eth_random_addr(&mac->addr_bytes[0]);
268 INFO("MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
269 mac->addr_bytes[0], mac->addr_bytes[1],
270 mac->addr_bytes[2], mac->addr_bytes[3],
271 mac->addr_bytes[4], mac->addr_bytes[5]);
272 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
273 PRIV(dev)->intr_handle = (struct rte_intr_handle){
275 .type = RTE_INTR_HANDLE_EXT,
277 rte_eth_dev_probing_finish(dev);
280 failsafe_hotplug_alarm_cancel(dev);
281 unregister_new_callback:
282 rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
283 failsafe_eth_new_event_callback, dev);
285 failsafe_args_free(dev);
287 fs_sub_device_free(dev);
289 /* mac_addrs must not be freed alone because part of dev_private */
290 dev->data->mac_addrs = NULL;
291 rte_eth_dev_release_port(dev);
296 fs_rte_eth_free(const char *name)
298 struct rte_eth_dev *dev;
301 dev = rte_eth_dev_allocated(name);
304 rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW,
305 failsafe_eth_new_event_callback, dev);
306 ret = failsafe_eal_uninit(dev);
308 ERROR("Error while uninitializing sub-EAL");
309 failsafe_args_free(dev);
310 fs_sub_device_free(dev);
311 ret = pthread_mutex_destroy(&PRIV(dev)->hotplug_mutex);
313 ERROR("Error while destroying hotplug mutex");
314 rte_free(PRIV(dev)->mcast_addrs);
315 /* mac_addrs must not be freed alone because part of dev_private */
316 dev->data->mac_addrs = NULL;
317 rte_eth_dev_release_port(dev);
322 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev)
325 struct rte_eth_dev *eth_dev;
327 name = rte_vdev_device_name(vdev);
328 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s",
331 if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
332 strlen(rte_vdev_device_args(vdev)) == 0) {
333 eth_dev = rte_eth_dev_attach_secondary(name);
335 ERROR("Failed to probe %s", name);
338 /* TODO: request info from primary to set up Rx and Tx */
339 eth_dev->dev_ops = &failsafe_ops;
340 eth_dev->device = &vdev->device;
341 rte_eth_dev_probing_finish(eth_dev);
345 return fs_eth_dev_create(vdev);
349 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev)
353 name = rte_vdev_device_name(vdev);
354 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name);
355 return fs_rte_eth_free(name);
358 static struct rte_vdev_driver failsafe_drv = {
359 .probe = rte_pmd_failsafe_probe,
360 .remove = rte_pmd_failsafe_remove,
363 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
364 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING);
366 RTE_INIT(failsafe_init_log)
368 failsafe_logtype = rte_log_register("pmd.net.failsafe");
369 if (failsafe_logtype >= 0)
370 rte_log_set_level(failsafe_logtype, RTE_LOG_NOTICE);