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