1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2017 6WIND S.A. 3 * Copyright 2017 Mellanox Technologies, Ltd 4 */ 5 6 #include <unistd.h> 7 8 #include <rte_flow.h> 9 #include <rte_flow_driver.h> 10 #include <rte_cycles.h> 11 12 #include "failsafe_private.h" 13 14 /** Print a message out of a flow error. */ 15 static int 16 fs_flow_complain(struct rte_flow_error *error) 17 { 18 static const char *const errstrlist[] = { 19 [RTE_FLOW_ERROR_TYPE_NONE] = "no error", 20 [RTE_FLOW_ERROR_TYPE_UNSPECIFIED] = "cause unspecified", 21 [RTE_FLOW_ERROR_TYPE_HANDLE] = "flow rule (handle)", 22 [RTE_FLOW_ERROR_TYPE_ATTR_GROUP] = "group field", 23 [RTE_FLOW_ERROR_TYPE_ATTR_PRIORITY] = "priority field", 24 [RTE_FLOW_ERROR_TYPE_ATTR_INGRESS] = "ingress field", 25 [RTE_FLOW_ERROR_TYPE_ATTR_EGRESS] = "egress field", 26 [RTE_FLOW_ERROR_TYPE_ATTR] = "attributes structure", 27 [RTE_FLOW_ERROR_TYPE_ITEM_NUM] = "pattern length", 28 [RTE_FLOW_ERROR_TYPE_ITEM] = "specific pattern item", 29 [RTE_FLOW_ERROR_TYPE_ACTION_NUM] = "number of actions", 30 [RTE_FLOW_ERROR_TYPE_ACTION] = "specific action", 31 }; 32 const char *errstr; 33 char buf[32]; 34 int err = rte_errno; 35 36 if ((unsigned int)error->type >= RTE_DIM(errstrlist) || 37 !errstrlist[error->type]) 38 errstr = "unknown type"; 39 else 40 errstr = errstrlist[error->type]; 41 ERROR("Caught error type %d (%s): %s%s\n", 42 error->type, errstr, 43 error->cause ? (snprintf(buf, sizeof(buf), "cause: %p, ", 44 error->cause), buf) : "", 45 error->message ? error->message : "(no stated reason)"); 46 return -err; 47 } 48 49 static int 50 eth_dev_flow_isolate_set(struct rte_eth_dev *dev, 51 struct sub_device *sdev) 52 { 53 struct rte_flow_error ferror; 54 int ret; 55 56 if (!PRIV(dev)->flow_isolated) { 57 DEBUG("Flow isolation already disabled"); 58 } else { 59 DEBUG("Enabling flow isolation"); 60 ret = rte_flow_isolate(PORT_ID(sdev), 61 PRIV(dev)->flow_isolated, 62 &ferror); 63 if (ret) { 64 fs_flow_complain(&ferror); 65 return ret; 66 } 67 } 68 return 0; 69 } 70 71 static int 72 fs_eth_dev_conf_apply(struct rte_eth_dev *dev, 73 struct sub_device *sdev) 74 { 75 struct rte_eth_dev *edev; 76 struct rte_vlan_filter_conf *vfc1; 77 struct rte_vlan_filter_conf *vfc2; 78 struct rte_flow *flow; 79 struct rte_flow_error ferror; 80 uint32_t i; 81 int ret; 82 83 edev = ETH(sdev); 84 /* RX queue setup */ 85 for (i = 0; i < dev->data->nb_rx_queues; i++) { 86 struct rxq *rxq; 87 88 rxq = dev->data->rx_queues[i]; 89 ret = rte_eth_rx_queue_setup(PORT_ID(sdev), i, 90 rxq->info.nb_desc, rxq->socket_id, 91 &rxq->info.conf, rxq->info.mp); 92 if (ret) { 93 ERROR("rx_queue_setup failed"); 94 return ret; 95 } 96 } 97 /* TX queue setup */ 98 for (i = 0; i < dev->data->nb_tx_queues; i++) { 99 struct txq *txq; 100 101 txq = dev->data->tx_queues[i]; 102 ret = rte_eth_tx_queue_setup(PORT_ID(sdev), i, 103 txq->info.nb_desc, txq->socket_id, 104 &txq->info.conf); 105 if (ret) { 106 ERROR("tx_queue_setup failed"); 107 return ret; 108 } 109 } 110 /* dev_link.link_status */ 111 if (dev->data->dev_link.link_status != 112 edev->data->dev_link.link_status) { 113 DEBUG("Configuring link_status"); 114 if (dev->data->dev_link.link_status) 115 ret = rte_eth_dev_set_link_up(PORT_ID(sdev)); 116 else 117 ret = rte_eth_dev_set_link_down(PORT_ID(sdev)); 118 if (ret) { 119 ERROR("Failed to apply link_status"); 120 return ret; 121 } 122 } else { 123 DEBUG("link_status already set"); 124 } 125 /* promiscuous */ 126 if (dev->data->promiscuous != edev->data->promiscuous) { 127 DEBUG("Configuring promiscuous"); 128 if (dev->data->promiscuous) 129 rte_eth_promiscuous_enable(PORT_ID(sdev)); 130 else 131 rte_eth_promiscuous_disable(PORT_ID(sdev)); 132 } else { 133 DEBUG("promiscuous already set"); 134 } 135 /* all_multicast */ 136 if (dev->data->all_multicast != edev->data->all_multicast) { 137 DEBUG("Configuring all_multicast"); 138 if (dev->data->all_multicast) 139 rte_eth_allmulticast_enable(PORT_ID(sdev)); 140 else 141 rte_eth_allmulticast_disable(PORT_ID(sdev)); 142 } else { 143 DEBUG("all_multicast already set"); 144 } 145 /* MTU */ 146 if (dev->data->mtu != edev->data->mtu) { 147 DEBUG("Configuring MTU"); 148 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), dev->data->mtu); 149 if (ret) { 150 ERROR("Failed to apply MTU"); 151 return ret; 152 } 153 } else { 154 DEBUG("MTU already set"); 155 } 156 /* default MAC */ 157 DEBUG("Configuring default MAC address"); 158 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), 159 &dev->data->mac_addrs[0]); 160 if (ret) { 161 ERROR("Setting default MAC address failed"); 162 return ret; 163 } 164 /* additional MAC */ 165 if (PRIV(dev)->nb_mac_addr > 1) 166 DEBUG("Configure additional MAC address%s", 167 (PRIV(dev)->nb_mac_addr > 2 ? "es" : "")); 168 for (i = 1; i < PRIV(dev)->nb_mac_addr; i++) { 169 struct ether_addr *ea; 170 171 ea = &dev->data->mac_addrs[i]; 172 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), ea, 173 PRIV(dev)->mac_addr_pool[i]); 174 if (ret) { 175 char ea_fmt[ETHER_ADDR_FMT_SIZE]; 176 177 ether_format_addr(ea_fmt, ETHER_ADDR_FMT_SIZE, ea); 178 ERROR("Adding MAC address %s failed", ea_fmt); 179 return ret; 180 } 181 } 182 /* 183 * Propagate multicast MAC addresses to sub-devices, 184 * if non zero number of addresses is set. 185 * The condition is required to avoid breakage of failsafe 186 * for sub-devices which do not support the operation 187 * if the feature is really not used. 188 */ 189 if (PRIV(dev)->nb_mcast_addr > 0) { 190 DEBUG("Configuring multicast MAC addresses"); 191 ret = rte_eth_dev_set_mc_addr_list(PORT_ID(sdev), 192 PRIV(dev)->mcast_addrs, 193 PRIV(dev)->nb_mcast_addr); 194 if (ret) { 195 ERROR("Failed to apply multicast MAC addresses"); 196 return ret; 197 } 198 } 199 /* VLAN filter */ 200 vfc1 = &dev->data->vlan_filter_conf; 201 vfc2 = &edev->data->vlan_filter_conf; 202 if (memcmp(vfc1, vfc2, sizeof(struct rte_vlan_filter_conf))) { 203 uint64_t vbit; 204 uint64_t ids; 205 size_t i; 206 uint16_t vlan_id; 207 208 DEBUG("Configuring VLAN filter"); 209 for (i = 0; i < RTE_DIM(vfc1->ids); i++) { 210 if (vfc1->ids[i] == 0) 211 continue; 212 ids = vfc1->ids[i]; 213 while (ids) { 214 vlan_id = 64 * i; 215 /* count trailing zeroes */ 216 vbit = ~ids & (ids - 1); 217 /* clear least significant bit set */ 218 ids ^= (ids ^ (ids - 1)) ^ vbit; 219 for (; vbit; vlan_id++) 220 vbit >>= 1; 221 ret = rte_eth_dev_vlan_filter( 222 PORT_ID(sdev), vlan_id, 1); 223 if (ret) { 224 ERROR("Failed to apply VLAN filter %hu", 225 vlan_id); 226 return ret; 227 } 228 } 229 } 230 } else { 231 DEBUG("VLAN filter already set"); 232 } 233 /* rte_flow */ 234 if (TAILQ_EMPTY(&PRIV(dev)->flow_list)) { 235 DEBUG("rte_flow already set"); 236 } else { 237 DEBUG("Resetting rte_flow configuration"); 238 ret = rte_flow_flush(PORT_ID(sdev), &ferror); 239 if (ret) { 240 fs_flow_complain(&ferror); 241 return ret; 242 } 243 i = 0; 244 rte_errno = 0; 245 DEBUG("Configuring rte_flow"); 246 TAILQ_FOREACH(flow, &PRIV(dev)->flow_list, next) { 247 DEBUG("Creating flow #%" PRIu32, i++); 248 flow->flows[SUB_ID(sdev)] = 249 rte_flow_create(PORT_ID(sdev), 250 flow->rule.attr, 251 flow->rule.pattern, 252 flow->rule.actions, 253 &ferror); 254 ret = rte_errno; 255 if (ret) 256 break; 257 } 258 if (ret) { 259 fs_flow_complain(&ferror); 260 return ret; 261 } 262 } 263 return 0; 264 } 265 266 static void 267 fs_dev_remove(struct sub_device *sdev) 268 { 269 int ret; 270 271 if (sdev == NULL) 272 return; 273 switch (sdev->state) { 274 case DEV_STARTED: 275 failsafe_rx_intr_uninstall_subdevice(sdev); 276 rte_eth_dev_stop(PORT_ID(sdev)); 277 sdev->state = DEV_ACTIVE; 278 /* fallthrough */ 279 case DEV_ACTIVE: 280 failsafe_eth_dev_unregister_callbacks(sdev); 281 rte_eth_dev_close(PORT_ID(sdev)); 282 sdev->state = DEV_PROBED; 283 /* fallthrough */ 284 case DEV_PROBED: 285 ret = rte_dev_remove(sdev->dev); 286 if (ret) { 287 ERROR("Bus detach failed for sub_device %u", 288 SUB_ID(sdev)); 289 } else { 290 rte_eth_dev_release_port(ETH(sdev)); 291 } 292 sdev->state = DEV_PARSED; 293 /* fallthrough */ 294 case DEV_PARSED: 295 case DEV_UNDEFINED: 296 sdev->state = DEV_UNDEFINED; 297 /* the end */ 298 break; 299 } 300 sdev->remove = 0; 301 failsafe_hotplug_alarm_install(sdev->fs_dev); 302 } 303 304 static void 305 fs_dev_stats_save(struct sub_device *sdev) 306 { 307 struct rte_eth_stats stats; 308 int err; 309 310 /* Attempt to read current stats. */ 311 err = rte_eth_stats_get(PORT_ID(sdev), &stats); 312 if (err) { 313 uint64_t timestamp = sdev->stats_snapshot.timestamp; 314 315 WARN("Could not access latest statistics from sub-device %d.\n", 316 SUB_ID(sdev)); 317 if (timestamp != 0) 318 WARN("Using latest snapshot taken before %"PRIu64" seconds.\n", 319 (rte_rdtsc() - timestamp) / rte_get_tsc_hz()); 320 } 321 failsafe_stats_increment(&PRIV(sdev->fs_dev)->stats_accumulator, 322 err ? &sdev->stats_snapshot.stats : &stats); 323 memset(&sdev->stats_snapshot, 0, sizeof(sdev->stats_snapshot)); 324 } 325 326 static inline int 327 fs_rxtx_clean(struct sub_device *sdev) 328 { 329 uint16_t i; 330 331 for (i = 0; i < ETH(sdev)->data->nb_rx_queues; i++) 332 if (FS_ATOMIC_RX(sdev, i)) 333 return 0; 334 for (i = 0; i < ETH(sdev)->data->nb_tx_queues; i++) 335 if (FS_ATOMIC_TX(sdev, i)) 336 return 0; 337 return 1; 338 } 339 340 void 341 failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev) 342 { 343 int ret; 344 345 if (sdev == NULL) 346 return; 347 if (sdev->rmv_callback) { 348 ret = rte_eth_dev_callback_unregister(PORT_ID(sdev), 349 RTE_ETH_EVENT_INTR_RMV, 350 failsafe_eth_rmv_event_callback, 351 sdev); 352 if (ret) 353 WARN("Failed to unregister RMV callback for sub_device" 354 " %d", SUB_ID(sdev)); 355 sdev->rmv_callback = 0; 356 } 357 if (sdev->lsc_callback) { 358 ret = rte_eth_dev_callback_unregister(PORT_ID(sdev), 359 RTE_ETH_EVENT_INTR_LSC, 360 failsafe_eth_lsc_event_callback, 361 sdev); 362 if (ret) 363 WARN("Failed to unregister LSC callback for sub_device" 364 " %d", SUB_ID(sdev)); 365 sdev->lsc_callback = 0; 366 } 367 } 368 369 void 370 failsafe_dev_remove(struct rte_eth_dev *dev) 371 { 372 struct sub_device *sdev; 373 uint8_t i; 374 375 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) 376 if (sdev->remove && fs_rxtx_clean(sdev)) { 377 if (fs_lock(dev, 1) != 0) 378 return; 379 fs_dev_stats_save(sdev); 380 fs_dev_remove(sdev); 381 fs_unlock(dev, 1); 382 } 383 } 384 385 static int 386 failsafe_eth_dev_rx_queues_sync(struct rte_eth_dev *dev) 387 { 388 struct rxq *rxq; 389 int ret; 390 uint16_t i; 391 392 for (i = 0; i < dev->data->nb_rx_queues; i++) { 393 rxq = dev->data->rx_queues[i]; 394 395 if (rxq->info.conf.rx_deferred_start && 396 dev->data->rx_queue_state[i] == 397 RTE_ETH_QUEUE_STATE_STARTED) { 398 /* 399 * The subdevice Rx queue does not launch on device 400 * start if deferred start flag is set. It needs to be 401 * started manually in case an appropriate failsafe Rx 402 * queue has been started earlier. 403 */ 404 ret = dev->dev_ops->rx_queue_start(dev, i); 405 if (ret) { 406 ERROR("Could not synchronize Rx queue %d", i); 407 return ret; 408 } 409 } else if (dev->data->rx_queue_state[i] == 410 RTE_ETH_QUEUE_STATE_STOPPED) { 411 /* 412 * The subdevice Rx queue needs to be stopped manually 413 * in case an appropriate failsafe Rx queue has been 414 * stopped earlier. 415 */ 416 ret = dev->dev_ops->rx_queue_stop(dev, i); 417 if (ret) { 418 ERROR("Could not synchronize Rx queue %d", i); 419 return ret; 420 } 421 } 422 } 423 return 0; 424 } 425 426 static int 427 failsafe_eth_dev_tx_queues_sync(struct rte_eth_dev *dev) 428 { 429 struct txq *txq; 430 int ret; 431 uint16_t i; 432 433 for (i = 0; i < dev->data->nb_tx_queues; i++) { 434 txq = dev->data->tx_queues[i]; 435 436 if (txq->info.conf.tx_deferred_start && 437 dev->data->tx_queue_state[i] == 438 RTE_ETH_QUEUE_STATE_STARTED) { 439 /* 440 * The subdevice Tx queue does not launch on device 441 * start if deferred start flag is set. It needs to be 442 * started manually in case an appropriate failsafe Tx 443 * queue has been started earlier. 444 */ 445 ret = dev->dev_ops->tx_queue_start(dev, i); 446 if (ret) { 447 ERROR("Could not synchronize Tx queue %d", i); 448 return ret; 449 } 450 } else if (dev->data->tx_queue_state[i] == 451 RTE_ETH_QUEUE_STATE_STOPPED) { 452 /* 453 * The subdevice Tx queue needs to be stopped manually 454 * in case an appropriate failsafe Tx queue has been 455 * stopped earlier. 456 */ 457 ret = dev->dev_ops->tx_queue_stop(dev, i); 458 if (ret) { 459 ERROR("Could not synchronize Tx queue %d", i); 460 return ret; 461 } 462 } 463 } 464 return 0; 465 } 466 467 int 468 failsafe_eth_dev_state_sync(struct rte_eth_dev *dev) 469 { 470 struct sub_device *sdev; 471 uint32_t inactive; 472 int ret; 473 uint8_t i; 474 475 if (PRIV(dev)->state < DEV_PARSED) 476 return 0; 477 478 ret = failsafe_args_parse_subs(dev); 479 if (ret) 480 goto err_remove; 481 482 if (PRIV(dev)->state < DEV_PROBED) 483 return 0; 484 ret = failsafe_eal_init(dev); 485 if (ret) 486 goto err_remove; 487 if (PRIV(dev)->state < DEV_ACTIVE) 488 return 0; 489 inactive = 0; 490 FOREACH_SUBDEV(sdev, i, dev) { 491 if (sdev->state == DEV_PROBED) { 492 inactive |= UINT32_C(1) << i; 493 ret = eth_dev_flow_isolate_set(dev, sdev); 494 if (ret) { 495 ERROR("Could not apply configuration to sub_device %d", 496 i); 497 goto err_remove; 498 } 499 } 500 } 501 ret = dev->dev_ops->dev_configure(dev); 502 if (ret) 503 goto err_remove; 504 FOREACH_SUBDEV(sdev, i, dev) { 505 if (inactive & (UINT32_C(1) << i)) { 506 ret = fs_eth_dev_conf_apply(dev, sdev); 507 if (ret) { 508 ERROR("Could not apply configuration to sub_device %d", 509 i); 510 goto err_remove; 511 } 512 } 513 } 514 /* 515 * If new devices have been configured, check if 516 * the link state has changed. 517 */ 518 if (inactive) 519 dev->dev_ops->link_update(dev, 1); 520 if (PRIV(dev)->state < DEV_STARTED) 521 return 0; 522 ret = dev->dev_ops->dev_start(dev); 523 if (ret) 524 goto err_remove; 525 ret = failsafe_eth_dev_rx_queues_sync(dev); 526 if (ret) 527 goto err_remove; 528 ret = failsafe_eth_dev_tx_queues_sync(dev); 529 if (ret) 530 goto err_remove; 531 return 0; 532 err_remove: 533 FOREACH_SUBDEV(sdev, i, dev) 534 if (sdev->state != PRIV(dev)->state) 535 sdev->remove = 1; 536 return ret; 537 } 538 539 void 540 failsafe_stats_increment(struct rte_eth_stats *to, struct rte_eth_stats *from) 541 { 542 uint32_t i; 543 544 RTE_ASSERT(to != NULL && from != NULL); 545 to->ipackets += from->ipackets; 546 to->opackets += from->opackets; 547 to->ibytes += from->ibytes; 548 to->obytes += from->obytes; 549 to->imissed += from->imissed; 550 to->ierrors += from->ierrors; 551 to->oerrors += from->oerrors; 552 to->rx_nombuf += from->rx_nombuf; 553 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) { 554 to->q_ipackets[i] += from->q_ipackets[i]; 555 to->q_opackets[i] += from->q_opackets[i]; 556 to->q_ibytes[i] += from->q_ibytes[i]; 557 to->q_obytes[i] += from->q_obytes[i]; 558 to->q_errors[i] += from->q_errors[i]; 559 } 560 } 561 562 int 563 failsafe_eth_rmv_event_callback(uint16_t port_id __rte_unused, 564 enum rte_eth_event_type event __rte_unused, 565 void *cb_arg, void *out __rte_unused) 566 { 567 struct sub_device *sdev = cb_arg; 568 569 fs_lock(sdev->fs_dev, 0); 570 /* Switch as soon as possible tx_dev. */ 571 fs_switch_dev(sdev->fs_dev, sdev); 572 /* Use safe bursts in any case. */ 573 failsafe_set_burst_fn(sdev->fs_dev, 1); 574 /* 575 * Async removal, the sub-PMD will try to unregister 576 * the callback at the source of the current thread context. 577 */ 578 sdev->remove = 1; 579 fs_unlock(sdev->fs_dev, 0); 580 return 0; 581 } 582 583 int 584 failsafe_eth_lsc_event_callback(uint16_t port_id __rte_unused, 585 enum rte_eth_event_type event __rte_unused, 586 void *cb_arg, void *out __rte_unused) 587 { 588 struct rte_eth_dev *dev = cb_arg; 589 int ret; 590 591 ret = dev->dev_ops->link_update(dev, 0); 592 /* We must pass on the LSC event */ 593 if (ret) 594 return _rte_eth_dev_callback_process(dev, 595 RTE_ETH_EVENT_INTR_LSC, 596 NULL); 597 else 598 return 0; 599 } 600 601 /* Take sub-device ownership before it becomes exposed to the application. */ 602 int 603 failsafe_eth_new_event_callback(uint16_t port_id, 604 enum rte_eth_event_type event __rte_unused, 605 void *cb_arg, void *out __rte_unused) 606 { 607 struct rte_eth_dev *fs_dev = cb_arg; 608 struct sub_device *sdev; 609 struct rte_eth_dev *dev = &rte_eth_devices[port_id]; 610 uint8_t i; 611 612 FOREACH_SUBDEV_STATE(sdev, i, fs_dev, DEV_PARSED) { 613 if (sdev->state >= DEV_PROBED) 614 continue; 615 if (strcmp(sdev->devargs.name, dev->device->name) != 0) 616 continue; 617 rte_eth_dev_owner_set(port_id, &PRIV(fs_dev)->my_owner); 618 /* The actual owner will be checked after the port probing. */ 619 break; 620 } 621 return 0; 622 } 623