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 ret = rte_eth_promiscuous_enable(PORT_ID(sdev)); 130 else 131 ret = rte_eth_promiscuous_disable(PORT_ID(sdev)); 132 if (ret != 0) { 133 ERROR("Failed to apply promiscuous mode"); 134 return ret; 135 } 136 } else { 137 DEBUG("promiscuous already set"); 138 } 139 /* all_multicast */ 140 if (dev->data->all_multicast != edev->data->all_multicast) { 141 DEBUG("Configuring all_multicast"); 142 if (dev->data->all_multicast) 143 ret = rte_eth_allmulticast_enable(PORT_ID(sdev)); 144 else 145 ret = rte_eth_allmulticast_disable(PORT_ID(sdev)); 146 if (ret != 0) { 147 ERROR("Failed to apply allmulticast mode"); 148 return ret; 149 } 150 } else { 151 DEBUG("all_multicast already set"); 152 } 153 /* MTU */ 154 if (dev->data->mtu != edev->data->mtu) { 155 DEBUG("Configuring MTU"); 156 ret = rte_eth_dev_set_mtu(PORT_ID(sdev), dev->data->mtu); 157 if (ret) { 158 ERROR("Failed to apply MTU"); 159 return ret; 160 } 161 } else { 162 DEBUG("MTU already set"); 163 } 164 /* default MAC */ 165 DEBUG("Configuring default MAC address"); 166 ret = rte_eth_dev_default_mac_addr_set(PORT_ID(sdev), 167 &dev->data->mac_addrs[0]); 168 if (ret) { 169 ERROR("Setting default MAC address failed"); 170 return ret; 171 } 172 /* additional MAC */ 173 if (PRIV(dev)->nb_mac_addr > 1) 174 DEBUG("Configure additional MAC address%s", 175 (PRIV(dev)->nb_mac_addr > 2 ? "es" : "")); 176 for (i = 1; i < PRIV(dev)->nb_mac_addr; i++) { 177 struct rte_ether_addr *ea; 178 179 ea = &dev->data->mac_addrs[i]; 180 ret = rte_eth_dev_mac_addr_add(PORT_ID(sdev), ea, 181 PRIV(dev)->mac_addr_pool[i]); 182 if (ret) { 183 char ea_fmt[RTE_ETHER_ADDR_FMT_SIZE]; 184 185 rte_ether_format_addr(ea_fmt, 186 RTE_ETHER_ADDR_FMT_SIZE, ea); 187 ERROR("Adding MAC address %s failed", ea_fmt); 188 return ret; 189 } 190 } 191 /* 192 * Propagate multicast MAC addresses to sub-devices, 193 * if non zero number of addresses is set. 194 * The condition is required to avoid breakage of failsafe 195 * for sub-devices which do not support the operation 196 * if the feature is really not used. 197 */ 198 if (PRIV(dev)->nb_mcast_addr > 0) { 199 DEBUG("Configuring multicast MAC addresses"); 200 ret = rte_eth_dev_set_mc_addr_list(PORT_ID(sdev), 201 PRIV(dev)->mcast_addrs, 202 PRIV(dev)->nb_mcast_addr); 203 if (ret) { 204 ERROR("Failed to apply multicast MAC addresses"); 205 return ret; 206 } 207 } 208 /* VLAN filter */ 209 vfc1 = &dev->data->vlan_filter_conf; 210 vfc2 = &edev->data->vlan_filter_conf; 211 if (memcmp(vfc1, vfc2, sizeof(struct rte_vlan_filter_conf))) { 212 uint64_t vbit; 213 uint64_t ids; 214 size_t i; 215 uint16_t vlan_id; 216 217 DEBUG("Configuring VLAN filter"); 218 for (i = 0; i < RTE_DIM(vfc1->ids); i++) { 219 if (vfc1->ids[i] == 0) 220 continue; 221 ids = vfc1->ids[i]; 222 while (ids) { 223 vlan_id = 64 * i; 224 /* count trailing zeroes */ 225 vbit = ~ids & (ids - 1); 226 /* clear least significant bit set */ 227 ids ^= (ids ^ (ids - 1)) ^ vbit; 228 for (; vbit; vlan_id++) 229 vbit >>= 1; 230 ret = rte_eth_dev_vlan_filter( 231 PORT_ID(sdev), vlan_id, 1); 232 if (ret) { 233 ERROR("Failed to apply VLAN filter %hu", 234 vlan_id); 235 return ret; 236 } 237 } 238 } 239 } else { 240 DEBUG("VLAN filter already set"); 241 } 242 /* rte_flow */ 243 if (TAILQ_EMPTY(&PRIV(dev)->flow_list)) { 244 DEBUG("rte_flow already set"); 245 } else { 246 DEBUG("Resetting rte_flow configuration"); 247 ret = rte_flow_flush(PORT_ID(sdev), &ferror); 248 if (ret) { 249 fs_flow_complain(&ferror); 250 return ret; 251 } 252 i = 0; 253 rte_errno = 0; 254 DEBUG("Configuring rte_flow"); 255 TAILQ_FOREACH(flow, &PRIV(dev)->flow_list, next) { 256 DEBUG("Creating flow #%" PRIu32, i++); 257 flow->flows[SUB_ID(sdev)] = 258 rte_flow_create(PORT_ID(sdev), 259 flow->rule.attr, 260 flow->rule.pattern, 261 flow->rule.actions, 262 &ferror); 263 ret = rte_errno; 264 if (ret) 265 break; 266 } 267 if (ret) { 268 fs_flow_complain(&ferror); 269 return ret; 270 } 271 } 272 return 0; 273 } 274 275 static void 276 fs_dev_remove(struct sub_device *sdev) 277 { 278 int ret; 279 280 if (sdev == NULL) 281 return; 282 switch (sdev->state) { 283 case DEV_STARTED: 284 failsafe_rx_intr_uninstall_subdevice(sdev); 285 rte_eth_dev_stop(PORT_ID(sdev)); 286 sdev->state = DEV_ACTIVE; 287 /* fallthrough */ 288 case DEV_ACTIVE: 289 failsafe_eth_dev_unregister_callbacks(sdev); 290 rte_eth_dev_close(PORT_ID(sdev)); 291 sdev->state = DEV_PROBED; 292 /* fallthrough */ 293 case DEV_PROBED: 294 ret = rte_dev_remove(sdev->dev); 295 if (ret < 0) { 296 ERROR("Bus detach failed for sub_device %u", 297 SUB_ID(sdev)); 298 } else { 299 rte_eth_dev_release_port(ETH(sdev)); 300 } 301 sdev->state = DEV_PARSED; 302 /* fallthrough */ 303 case DEV_PARSED: 304 case DEV_UNDEFINED: 305 sdev->state = DEV_UNDEFINED; 306 sdev->sdev_port_id = RTE_MAX_ETHPORTS; 307 /* the end */ 308 break; 309 } 310 sdev->remove = 0; 311 failsafe_hotplug_alarm_install(fs_dev(sdev)); 312 } 313 314 static void 315 fs_dev_stats_save(struct sub_device *sdev) 316 { 317 struct rte_eth_stats stats; 318 int err; 319 320 /* Attempt to read current stats. */ 321 err = rte_eth_stats_get(PORT_ID(sdev), &stats); 322 if (err) { 323 uint64_t timestamp = sdev->stats_snapshot.timestamp; 324 325 WARN("Could not access latest statistics from sub-device %d.\n", 326 SUB_ID(sdev)); 327 if (timestamp != 0) 328 WARN("Using latest snapshot taken before %"PRIu64" seconds.\n", 329 (rte_rdtsc() - timestamp) / rte_get_tsc_hz()); 330 } 331 failsafe_stats_increment 332 (&PRIV(fs_dev(sdev))->stats_accumulator, 333 err ? &sdev->stats_snapshot.stats : &stats); 334 memset(&sdev->stats_snapshot, 0, sizeof(sdev->stats_snapshot)); 335 } 336 337 static inline int 338 fs_rxtx_clean(struct sub_device *sdev) 339 { 340 uint16_t i; 341 342 for (i = 0; i < ETH(sdev)->data->nb_rx_queues; i++) 343 if (FS_ATOMIC_RX(sdev, i)) 344 return 0; 345 for (i = 0; i < ETH(sdev)->data->nb_tx_queues; i++) 346 if (FS_ATOMIC_TX(sdev, i)) 347 return 0; 348 return 1; 349 } 350 351 void 352 failsafe_eth_dev_unregister_callbacks(struct sub_device *sdev) 353 { 354 int ret; 355 356 if (sdev == NULL) 357 return; 358 if (sdev->rmv_callback) { 359 ret = rte_eth_dev_callback_unregister(PORT_ID(sdev), 360 RTE_ETH_EVENT_INTR_RMV, 361 failsafe_eth_rmv_event_callback, 362 sdev); 363 if (ret) 364 WARN("Failed to unregister RMV callback for sub_device" 365 " %d", SUB_ID(sdev)); 366 sdev->rmv_callback = 0; 367 } 368 if (sdev->lsc_callback) { 369 ret = rte_eth_dev_callback_unregister(PORT_ID(sdev), 370 RTE_ETH_EVENT_INTR_LSC, 371 failsafe_eth_lsc_event_callback, 372 sdev); 373 if (ret) 374 WARN("Failed to unregister LSC callback for sub_device" 375 " %d", SUB_ID(sdev)); 376 sdev->lsc_callback = 0; 377 } 378 } 379 380 void 381 failsafe_dev_remove(struct rte_eth_dev *dev) 382 { 383 struct sub_device *sdev; 384 uint8_t i; 385 386 FOREACH_SUBDEV_STATE(sdev, i, dev, DEV_ACTIVE) 387 if (sdev->remove && fs_rxtx_clean(sdev)) { 388 if (fs_lock(dev, 1) != 0) 389 return; 390 fs_dev_stats_save(sdev); 391 fs_dev_remove(sdev); 392 fs_unlock(dev, 1); 393 } 394 } 395 396 static int 397 failsafe_eth_dev_rx_queues_sync(struct rte_eth_dev *dev) 398 { 399 struct rxq *rxq; 400 int ret; 401 uint16_t i; 402 403 for (i = 0; i < dev->data->nb_rx_queues; i++) { 404 rxq = dev->data->rx_queues[i]; 405 406 if (rxq->info.conf.rx_deferred_start && 407 dev->data->rx_queue_state[i] == 408 RTE_ETH_QUEUE_STATE_STARTED) { 409 /* 410 * The subdevice Rx queue does not launch on device 411 * start if deferred start flag is set. It needs to be 412 * started manually in case an appropriate failsafe Rx 413 * queue has been started earlier. 414 */ 415 ret = dev->dev_ops->rx_queue_start(dev, i); 416 if (ret) { 417 ERROR("Could not synchronize Rx queue %d", i); 418 return ret; 419 } 420 } else if (dev->data->rx_queue_state[i] == 421 RTE_ETH_QUEUE_STATE_STOPPED) { 422 /* 423 * The subdevice Rx queue needs to be stopped manually 424 * in case an appropriate failsafe Rx queue has been 425 * stopped earlier. 426 */ 427 ret = dev->dev_ops->rx_queue_stop(dev, i); 428 if (ret) { 429 ERROR("Could not synchronize Rx queue %d", i); 430 return ret; 431 } 432 } 433 } 434 return 0; 435 } 436 437 static int 438 failsafe_eth_dev_tx_queues_sync(struct rte_eth_dev *dev) 439 { 440 struct txq *txq; 441 int ret; 442 uint16_t i; 443 444 for (i = 0; i < dev->data->nb_tx_queues; i++) { 445 txq = dev->data->tx_queues[i]; 446 447 if (txq->info.conf.tx_deferred_start && 448 dev->data->tx_queue_state[i] == 449 RTE_ETH_QUEUE_STATE_STARTED) { 450 /* 451 * The subdevice Tx queue does not launch on device 452 * start if deferred start flag is set. It needs to be 453 * started manually in case an appropriate failsafe Tx 454 * queue has been started earlier. 455 */ 456 ret = dev->dev_ops->tx_queue_start(dev, i); 457 if (ret) { 458 ERROR("Could not synchronize Tx queue %d", i); 459 return ret; 460 } 461 } else if (dev->data->tx_queue_state[i] == 462 RTE_ETH_QUEUE_STATE_STOPPED) { 463 /* 464 * The subdevice Tx queue needs to be stopped manually 465 * in case an appropriate failsafe Tx queue has been 466 * stopped earlier. 467 */ 468 ret = dev->dev_ops->tx_queue_stop(dev, i); 469 if (ret) { 470 ERROR("Could not synchronize Tx queue %d", i); 471 return ret; 472 } 473 } 474 } 475 return 0; 476 } 477 478 int 479 failsafe_eth_dev_state_sync(struct rte_eth_dev *dev) 480 { 481 struct sub_device *sdev; 482 uint32_t inactive; 483 int ret; 484 uint8_t i; 485 486 if (PRIV(dev)->state < DEV_PARSED) 487 return 0; 488 489 ret = failsafe_args_parse_subs(dev); 490 if (ret) 491 goto err_remove; 492 493 if (PRIV(dev)->state < DEV_PROBED) 494 return 0; 495 ret = failsafe_eal_init(dev); 496 if (ret) 497 goto err_remove; 498 if (PRIV(dev)->state < DEV_ACTIVE) 499 return 0; 500 inactive = 0; 501 FOREACH_SUBDEV(sdev, i, dev) { 502 if (sdev->state == DEV_PROBED) { 503 inactive |= UINT32_C(1) << i; 504 ret = eth_dev_flow_isolate_set(dev, sdev); 505 if (ret) { 506 ERROR("Could not apply configuration to sub_device %d", 507 i); 508 goto err_remove; 509 } 510 } 511 } 512 ret = dev->dev_ops->dev_configure(dev); 513 if (ret) 514 goto err_remove; 515 FOREACH_SUBDEV(sdev, i, dev) { 516 if (inactive & (UINT32_C(1) << i)) { 517 ret = fs_eth_dev_conf_apply(dev, sdev); 518 if (ret) { 519 ERROR("Could not apply configuration to sub_device %d", 520 i); 521 goto err_remove; 522 } 523 } 524 } 525 /* 526 * If new devices have been configured, check if 527 * the link state has changed. 528 */ 529 if (inactive) 530 dev->dev_ops->link_update(dev, 1); 531 if (PRIV(dev)->state < DEV_STARTED) 532 return 0; 533 ret = dev->dev_ops->dev_start(dev); 534 if (ret) 535 goto err_remove; 536 ret = failsafe_eth_dev_rx_queues_sync(dev); 537 if (ret) 538 goto err_remove; 539 ret = failsafe_eth_dev_tx_queues_sync(dev); 540 if (ret) 541 goto err_remove; 542 return 0; 543 err_remove: 544 FOREACH_SUBDEV(sdev, i, dev) 545 if (sdev->state != PRIV(dev)->state) 546 sdev->remove = 1; 547 return ret; 548 } 549 550 void 551 failsafe_stats_increment(struct rte_eth_stats *to, struct rte_eth_stats *from) 552 { 553 uint32_t i; 554 555 RTE_ASSERT(to != NULL && from != NULL); 556 to->ipackets += from->ipackets; 557 to->opackets += from->opackets; 558 to->ibytes += from->ibytes; 559 to->obytes += from->obytes; 560 to->imissed += from->imissed; 561 to->ierrors += from->ierrors; 562 to->oerrors += from->oerrors; 563 to->rx_nombuf += from->rx_nombuf; 564 for (i = 0; i < RTE_ETHDEV_QUEUE_STAT_CNTRS; i++) { 565 to->q_ipackets[i] += from->q_ipackets[i]; 566 to->q_opackets[i] += from->q_opackets[i]; 567 to->q_ibytes[i] += from->q_ibytes[i]; 568 to->q_obytes[i] += from->q_obytes[i]; 569 to->q_errors[i] += from->q_errors[i]; 570 } 571 } 572 573 int 574 failsafe_eth_rmv_event_callback(uint16_t port_id __rte_unused, 575 enum rte_eth_event_type event __rte_unused, 576 void *cb_arg, void *out __rte_unused) 577 { 578 struct sub_device *sdev = cb_arg; 579 580 fs_lock(fs_dev(sdev), 0); 581 /* Switch as soon as possible tx_dev. */ 582 fs_switch_dev(fs_dev(sdev), sdev); 583 /* Use safe bursts in any case. */ 584 failsafe_set_burst_fn(fs_dev(sdev), 1); 585 /* 586 * Async removal, the sub-PMD will try to unregister 587 * the callback at the source of the current thread context. 588 */ 589 sdev->remove = 1; 590 fs_unlock(fs_dev(sdev), 0); 591 return 0; 592 } 593 594 int 595 failsafe_eth_lsc_event_callback(uint16_t port_id __rte_unused, 596 enum rte_eth_event_type event __rte_unused, 597 void *cb_arg, void *out __rte_unused) 598 { 599 struct rte_eth_dev *dev = cb_arg; 600 int ret; 601 602 ret = dev->dev_ops->link_update(dev, 0); 603 /* We must pass on the LSC event */ 604 if (ret) 605 return _rte_eth_dev_callback_process(dev, 606 RTE_ETH_EVENT_INTR_LSC, 607 NULL); 608 else 609 return 0; 610 } 611 612 /* Take sub-device ownership before it becomes exposed to the application. */ 613 int 614 failsafe_eth_new_event_callback(uint16_t port_id, 615 enum rte_eth_event_type event __rte_unused, 616 void *cb_arg, void *out __rte_unused) 617 { 618 struct rte_eth_dev *fs_dev = cb_arg; 619 struct sub_device *sdev; 620 struct rte_eth_dev *dev = &rte_eth_devices[port_id]; 621 uint8_t i; 622 623 FOREACH_SUBDEV_STATE(sdev, i, fs_dev, DEV_PARSED) { 624 if (sdev->state >= DEV_PROBED) 625 continue; 626 if (dev->device == NULL) { 627 WARN("Trying to probe malformed device %s.\n", 628 sdev->devargs.name); 629 continue; 630 } 631 if (strcmp(sdev->devargs.name, dev->device->name) != 0) 632 continue; 633 rte_eth_dev_owner_set(port_id, &PRIV(fs_dev)->my_owner); 634 /* The actual owner will be checked after the port probing. */ 635 break; 636 } 637 return 0; 638 } 639