1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2017 6WIND S.A. 3 * Copyright 2017 Mellanox Technologies, Ltd 4 */ 5 6 #include <stdbool.h> 7 8 #include <rte_alarm.h> 9 #include <rte_malloc.h> 10 #include <ethdev_driver.h> 11 #include <ethdev_vdev.h> 12 #include <rte_devargs.h> 13 #include <rte_kvargs.h> 14 #include <bus_driver.h> 15 #include <bus_vdev_driver.h> 16 17 #include "failsafe_private.h" 18 19 const char pmd_failsafe_driver_name[] = FAILSAFE_DRIVER_NAME; 20 static const struct rte_eth_link eth_link = { 21 .link_speed = RTE_ETH_SPEED_NUM_10G, 22 .link_duplex = RTE_ETH_LINK_FULL_DUPLEX, 23 .link_status = RTE_ETH_LINK_UP, 24 .link_autoneg = RTE_ETH_LINK_AUTONEG, 25 }; 26 27 static int 28 fs_sub_device_alloc(struct rte_eth_dev *dev, 29 const char *params) 30 { 31 uint8_t nb_subs; 32 int ret; 33 int i; 34 struct sub_device *sdev; 35 uint8_t sdev_iterator; 36 37 ret = failsafe_args_count_subdevice(dev, params); 38 if (ret) 39 return ret; 40 if (PRIV(dev)->subs_tail > FAILSAFE_MAX_ETHPORTS) { 41 ERROR("Cannot allocate more than %d ports", 42 FAILSAFE_MAX_ETHPORTS); 43 return -ENOSPC; 44 } 45 nb_subs = PRIV(dev)->subs_tail; 46 PRIV(dev)->subs = rte_zmalloc(NULL, 47 sizeof(struct sub_device) * nb_subs, 48 RTE_CACHE_LINE_SIZE); 49 if (PRIV(dev)->subs == NULL) { 50 ERROR("Could not allocate sub_devices"); 51 return -ENOMEM; 52 } 53 /* Initiate static sub devices linked list. */ 54 for (i = 1; i < nb_subs; i++) 55 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs + i; 56 PRIV(dev)->subs[i - 1].next = PRIV(dev)->subs; 57 58 FOREACH_SUBDEV(sdev, sdev_iterator, dev) { 59 sdev->sdev_port_id = RTE_MAX_ETHPORTS; 60 } 61 return 0; 62 } 63 64 static void fs_hotplug_alarm(void *arg); 65 66 int 67 failsafe_hotplug_alarm_install(struct rte_eth_dev *dev) 68 { 69 int ret; 70 71 if (dev == NULL) 72 return -EINVAL; 73 if (PRIV(dev)->pending_alarm) 74 return 0; 75 ret = rte_eal_alarm_set(failsafe_hotplug_poll * 1000, 76 fs_hotplug_alarm, 77 dev); 78 if (ret) { 79 ERROR("Could not set up plug-in event detection"); 80 return ret; 81 } 82 PRIV(dev)->pending_alarm = 1; 83 return 0; 84 } 85 86 int 87 failsafe_hotplug_alarm_cancel(struct rte_eth_dev *dev) 88 { 89 int ret = 0; 90 91 rte_errno = 0; 92 rte_eal_alarm_cancel(fs_hotplug_alarm, dev); 93 if (rte_errno) { 94 ERROR("rte_eal_alarm_cancel failed (errno: %s)", 95 strerror(rte_errno)); 96 ret = -rte_errno; 97 } else { 98 PRIV(dev)->pending_alarm = 0; 99 } 100 return ret; 101 } 102 103 static void 104 fs_hotplug_alarm(void *arg) 105 { 106 struct rte_eth_dev *dev = arg; 107 struct sub_device *sdev; 108 int ret; 109 uint8_t i; 110 111 if (!PRIV(dev)->pending_alarm) 112 return; 113 PRIV(dev)->pending_alarm = 0; 114 FOREACH_SUBDEV(sdev, i, dev) 115 if (sdev->state != PRIV(dev)->state) 116 break; 117 /* if we have non-probed device */ 118 if (i != PRIV(dev)->subs_tail) { 119 if (fs_lock(dev, 1) != 0) 120 goto reinstall; 121 ret = failsafe_eth_dev_state_sync(dev); 122 fs_unlock(dev, 1); 123 if (ret) 124 ERROR("Unable to synchronize sub_device state"); 125 } 126 failsafe_dev_remove(dev); 127 reinstall: 128 ret = failsafe_hotplug_alarm_install(dev); 129 if (ret) 130 ERROR("Unable to set up next alarm"); 131 } 132 133 static int 134 fs_mutex_init(struct fs_priv *priv) 135 { 136 int ret; 137 pthread_mutexattr_t attr; 138 139 ret = pthread_mutexattr_init(&attr); 140 if (ret) { 141 ERROR("Cannot initiate mutex attributes - %s", strerror(ret)); 142 return ret; 143 } 144 /* Allow mutex relocks for the thread holding the mutex. */ 145 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 146 if (ret) { 147 ERROR("Cannot set mutex type - %s", strerror(ret)); 148 return ret; 149 } 150 ret = pthread_mutex_init(&priv->hotplug_mutex, &attr); 151 if (ret) { 152 ERROR("Cannot initiate mutex - %s", strerror(ret)); 153 return ret; 154 } 155 return 0; 156 } 157 158 static int 159 fs_eth_dev_create(struct rte_vdev_device *vdev) 160 { 161 struct rte_eth_dev *dev; 162 struct rte_ether_addr *mac; 163 struct fs_priv *priv; 164 struct sub_device *sdev; 165 const char *params; 166 unsigned int socket_id; 167 uint8_t i; 168 int ret; 169 170 dev = NULL; 171 priv = NULL; 172 socket_id = rte_socket_id(); 173 INFO("Creating fail-safe device on NUMA socket %u", socket_id); 174 params = rte_vdev_device_args(vdev); 175 if (params == NULL) { 176 ERROR("This PMD requires sub-devices, none provided"); 177 return -1; 178 } 179 dev = rte_eth_vdev_allocate(vdev, sizeof(*priv)); 180 if (dev == NULL) { 181 ERROR("Unable to allocate rte_eth_dev"); 182 return -1; 183 } 184 priv = PRIV(dev); 185 priv->data = dev->data; 186 priv->rxp = FS_RX_PROXY_INIT; 187 dev->dev_ops = &failsafe_ops; 188 dev->data->mac_addrs = &PRIV(dev)->mac_addrs[0]; 189 dev->data->dev_link = eth_link; 190 PRIV(dev)->nb_mac_addr = 1; 191 TAILQ_INIT(&PRIV(dev)->flow_list); 192 dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst; 193 dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst; 194 ret = fs_sub_device_alloc(dev, params); 195 if (ret) { 196 ERROR("Could not allocate sub_devices"); 197 goto free_dev; 198 } 199 ret = failsafe_args_parse(dev, params); 200 if (ret) 201 goto free_subs; 202 ret = rte_eth_dev_owner_new(&priv->my_owner.id); 203 if (ret) { 204 ERROR("Failed to get unique owner identifier"); 205 goto free_args; 206 } 207 snprintf(priv->my_owner.name, sizeof(priv->my_owner.name), 208 FAILSAFE_OWNER_NAME); 209 DEBUG("Failsafe port %u owner info: %s_%016"PRIX64, dev->data->port_id, 210 priv->my_owner.name, priv->my_owner.id); 211 ret = rte_eth_dev_callback_register(RTE_ETH_ALL, RTE_ETH_EVENT_NEW, 212 failsafe_eth_new_event_callback, 213 dev); 214 if (ret) { 215 ERROR("Failed to register NEW callback"); 216 goto free_args; 217 } 218 ret = failsafe_eal_init(dev); 219 if (ret) 220 goto unregister_new_callback; 221 ret = fs_mutex_init(priv); 222 if (ret) 223 goto unregister_new_callback; 224 ret = failsafe_hotplug_alarm_install(dev); 225 if (ret) { 226 ERROR("Could not set up plug-in event detection"); 227 goto unregister_new_callback; 228 } 229 mac = &dev->data->mac_addrs[0]; 230 if (failsafe_mac_from_arg) { 231 /* 232 * If MAC address was provided as a parameter, 233 * apply to all probed subdevices. 234 */ 235 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_PROBED) { 236 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), 237 mac); 238 if (ret) { 239 ERROR("Failed to set default MAC address"); 240 goto cancel_alarm; 241 } 242 } 243 } else { 244 /* 245 * Use the ether_addr from first probed 246 * device, either preferred or fallback. 247 */ 248 FOREACH_SUBDEV(sdev, i, dev) 249 if (sdev->state >= DEV_PROBED) { 250 rte_ether_addr_copy( 251 Ð(sdev)->data->mac_addrs[0], mac); 252 break; 253 } 254 /* 255 * If no device has been probed and no ether_addr 256 * has been provided on the command line, use a random 257 * valid one. 258 * It will be applied during future state syncs to 259 * probed subdevices. 260 */ 261 if (i == priv->subs_tail) 262 rte_eth_random_addr(&mac->addr_bytes[0]); 263 } 264 INFO("MAC address is " RTE_ETHER_ADDR_PRT_FMT, 265 RTE_ETHER_ADDR_BYTES(mac)); 266 dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC | 267 RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS; 268 269 /* Allocate interrupt instance */ 270 PRIV(dev)->intr_handle = 271 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED); 272 if (PRIV(dev)->intr_handle == NULL) { 273 ERROR("Failed to allocate intr handle"); 274 goto cancel_alarm; 275 } 276 277 if (rte_intr_fd_set(PRIV(dev)->intr_handle, -1)) 278 goto cancel_alarm; 279 280 if (rte_intr_type_set(PRIV(dev)->intr_handle, RTE_INTR_HANDLE_EXT)) 281 goto cancel_alarm; 282 283 rte_eth_dev_probing_finish(dev); 284 285 return 0; 286 cancel_alarm: 287 failsafe_hotplug_alarm_cancel(dev); 288 unregister_new_callback: 289 rte_eth_dev_callback_unregister(RTE_ETH_ALL, RTE_ETH_EVENT_NEW, 290 failsafe_eth_new_event_callback, dev); 291 free_args: 292 failsafe_args_free(dev); 293 free_subs: 294 rte_free(PRIV(dev)->subs); 295 free_dev: 296 /* mac_addrs must not be freed alone because part of dev_private */ 297 dev->data->mac_addrs = NULL; 298 rte_eth_dev_release_port(dev); 299 return -1; 300 } 301 302 static int 303 fs_rte_eth_free(const char *name) 304 { 305 struct rte_eth_dev *dev; 306 int ret; 307 308 dev = rte_eth_dev_allocated(name); 309 if (dev == NULL) 310 return 0; /* port already released */ 311 ret = failsafe_eth_dev_close(dev); 312 rte_intr_instance_free(PRIV(dev)->intr_handle); 313 rte_eth_dev_release_port(dev); 314 return ret; 315 } 316 317 static bool 318 devargs_already_listed(struct rte_devargs *devargs) 319 { 320 struct rte_devargs *list_da; 321 322 RTE_EAL_DEVARGS_FOREACH(devargs->bus->name, list_da) { 323 if (strcmp(list_da->name, devargs->name) == 0) 324 /* devargs already in the list */ 325 return true; 326 } 327 return false; 328 } 329 330 static int 331 rte_pmd_failsafe_probe(struct rte_vdev_device *vdev) 332 { 333 const char *name; 334 struct rte_eth_dev *eth_dev; 335 struct sub_device *sdev; 336 struct rte_devargs devargs; 337 uint8_t i; 338 int ret; 339 340 name = rte_vdev_device_name(vdev); 341 INFO("Initializing " FAILSAFE_DRIVER_NAME " for %s", 342 name); 343 344 if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 345 eth_dev = rte_eth_dev_attach_secondary(name); 346 if (!eth_dev) { 347 ERROR("Failed to probe %s", name); 348 return -1; 349 } 350 eth_dev->dev_ops = &failsafe_ops; 351 eth_dev->device = &vdev->device; 352 eth_dev->rx_pkt_burst = (eth_rx_burst_t)&failsafe_rx_burst; 353 eth_dev->tx_pkt_burst = (eth_tx_burst_t)&failsafe_tx_burst; 354 /* 355 * Failsafe will attempt to probe all of its sub-devices. 356 * Any failure in sub-devices is not a fatal error. 357 * A sub-device can be plugged later. 358 */ 359 FOREACH_SUBDEV(sdev, i, eth_dev) { 360 /* skip empty devargs */ 361 if (sdev->devargs.name[0] == '\0') 362 continue; 363 364 /* rebuild devargs to be able to get the bus name. */ 365 ret = rte_devargs_parse(&devargs, 366 sdev->devargs.name); 367 if (ret != 0) { 368 ERROR("Failed to parse devargs %s", 369 devargs.name); 370 continue; 371 } 372 if (!devargs_already_listed(&devargs)) { 373 ret = rte_dev_probe(devargs.name); 374 if (ret < 0) { 375 ERROR("Failed to probe devargs %s", 376 devargs.name); 377 continue; 378 } 379 } 380 } 381 rte_eth_dev_probing_finish(eth_dev); 382 return 0; 383 } 384 385 return fs_eth_dev_create(vdev); 386 } 387 388 static int 389 rte_pmd_failsafe_remove(struct rte_vdev_device *vdev) 390 { 391 const char *name; 392 393 name = rte_vdev_device_name(vdev); 394 INFO("Uninitializing " FAILSAFE_DRIVER_NAME " for %s", name); 395 return fs_rte_eth_free(name); 396 } 397 398 static struct rte_vdev_driver failsafe_drv = { 399 .probe = rte_pmd_failsafe_probe, 400 .remove = rte_pmd_failsafe_remove, 401 }; 402 403 RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv); 404 RTE_PMD_REGISTER_PARAM_STRING(net_failsafe, PMD_FAILSAFE_PARAM_STRING); 405 RTE_LOG_REGISTER_DEFAULT(failsafe_logtype, NOTICE) 406