Bind AF_XDP UMEM per queue, switch the receive memory model, process XSK RX actions and zero-copy TX batches, and account completions by pool. Use the queue stop boundary before unmapping a pool. Avoid reading the program-attach extack union member during XSK setup. Preserve the initial TX NEED_WAKEUP state when an RX-only bound pool later gains a TX socket. Serialize RX and TX updates to the shared XSK wakeup byte. Clear RX NEED_WAKEUP only after successful descriptor publication, and require a frame large enough for two VLAN headers because XDP can disable hardware stripping after pool binding. At zero NAPI budget, complete only whole skb-owned WQ CQ spans; defer XDP/XSK and RX work to a positive-budget poll. Signed-off-by: Satish Kharat --- drivers/net/ethernet/cisco/enic/enic.h | 7 + drivers/net/ethernet/cisco/enic/enic_main.c | 118 +++++++- drivers/net/ethernet/cisco/enic/enic_rq.c | 50 +++- drivers/net/ethernet/cisco/enic/enic_wq.c | 12 +- drivers/net/ethernet/cisco/enic/enic_xdp.c | 402 +++++++++++++++++++++++++++- drivers/net/ethernet/cisco/enic/enic_xdp.h | 17 ++ 6 files changed, 587 insertions(+), 19 deletions(-) diff --git a/drivers/net/ethernet/cisco/enic/enic.h b/drivers/net/ethernet/cisco/enic/enic.h index 8a4d0194a5c9..3caa5ed77d50 100644 --- a/drivers/net/ethernet/cisco/enic/enic.h +++ b/drivers/net/ethernet/cisco/enic/enic.h @@ -243,6 +243,7 @@ struct enic_wq { spinlock_t lock; /* spinlock for wq */ struct vnic_wq vwq; struct enic_wq_stats stats; + struct xsk_buff_pool *xsk_pool; bool xdp_tx_pending; } ____cacheline_aligned; @@ -250,8 +251,13 @@ struct enic_rq { struct vnic_rq vrq; struct enic_rq_stats stats; struct page_pool *pool; + struct xsk_buff_pool *xsk_pool; struct xdp_rxq_info xdp_rxq; + spinlock_t xsk_wakeup_lock; /* serializes RX and TX wakeup bits */ + spinlock_t xsk_tx_done_lock; /* protects xsk_tx_done */ + struct list_head xsk_tx_done; bool xdp_enabled; + bool xsk_rx_active; bool xdp_flush_pending; } ____cacheline_aligned; @@ -321,6 +327,7 @@ struct enic { struct bpf_prog __rcu *xdp_prog; bool datapath_open; bool dma_blocked; + bool xsk_wakeup_enabled; struct vnic_intr *intr; unsigned int intr_avail; diff --git a/drivers/net/ethernet/cisco/enic/enic_main.c b/drivers/net/ethernet/cisco/enic/enic_main.c index 4c1ea75654bb..d14bc66f4178 100644 --- a/drivers/net/ethernet/cisco/enic/enic_main.c +++ b/drivers/net/ethernet/cisco/enic/enic_main.c @@ -1382,6 +1382,7 @@ static int enic_poll(struct napi_struct *napi, int budget) unsigned int rq_work_to_do = budget; unsigned int wq_work_to_do = ENIC_WQ_NAPI_BUDGET; unsigned int work_done, rq_work_done = 0, wq_work_done; + bool xsk_tx_done; int err; wq_work_done = enic_wq_cq_service(enic, cq_wq, wq_work_to_do, !budget); @@ -1391,11 +1392,13 @@ static int enic_poll(struct napi_struct *napi, int budget) 0, 0); return 0; } + enic_xsk_rx_tx_flush(enic, 0); if (budget > 0) rq_work_done = enic_rq_cq_service(enic, cq_rq, rq_work_to_do); enic_xdp_tx_flush(enic, 0); enic_xdp_redirect_flush(enic, 0); + xsk_tx_done = enic_xsk_tx_service(enic, 0); /* Accumulate intr event credits for this polling * cycle. An intr event is the completion of a @@ -1411,12 +1414,15 @@ static int enic_poll(struct napi_struct *napi, int budget) 0 /* don't reset intr timer */); err = vnic_rq_fill(&enic->rq[0].vrq, enic_rq_alloc_buf); + enic_xsk_rx_refill_done(&enic->rq[0], err); /* Buffer allocation failed. Stay in polling * mode so we can try to fill the ring again. */ - if (err) + if (err && !enic_xsk_rx_need_wakeup(&enic->rq[0])) + rq_work_done = rq_work_to_do; + if (!xsk_tx_done) rq_work_done = rq_work_to_do; if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce) /* Call the function which refreshes the intr coalescing timer @@ -1490,6 +1496,7 @@ static int enic_poll_msix_wq(struct napi_struct *napi, int budget) unsigned int wq_work_to_do = ENIC_WQ_NAPI_BUDGET; unsigned int wq_work_done; unsigned int wq_irq; + bool xsk_tx_done; wq_irq = wq->index; cq = enic_cq_wq(enic, wq_irq); @@ -1502,11 +1509,12 @@ static int enic_poll_msix_wq(struct napi_struct *napi, int budget) 0, 1); return 0; } + xsk_tx_done = enic_xsk_tx_service(enic, wq_index); vnic_intr_return_credits(&enic->intr[intr], wq_work_done, 0 /* don't unmask intr */, 1 /* reset intr timer */); - if (!wq_work_done) { + if (!wq_work_done && xsk_tx_done) { napi_complete(napi); vnic_intr_unmask(&enic->intr[intr]); return 0; @@ -1529,6 +1537,8 @@ static int enic_poll_msix_rq(struct napi_struct *napi, int budget) if (!budget) return 0; + enic_xsk_rx_tx_flush(enic, rq); + /* Service RQ */ @@ -1549,12 +1559,13 @@ static int enic_poll_msix_rq(struct napi_struct *napi, int budget) 0 /* don't reset intr timer */); err = vnic_rq_fill(&enic->rq[rq].vrq, enic_rq_alloc_buf); + enic_xsk_rx_refill_done(&enic->rq[rq], err); /* Buffer allocation failed. Stay in polling mode * so we can try to fill the ring again. */ - if (err) + if (err && !enic_xsk_rx_need_wakeup(&enic->rq[rq])) work_done = work_to_do; if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce) /* Call the function which refreshes the intr coalescing timer @@ -1851,6 +1862,8 @@ static void enic_clean_data_queues(struct enic *enic) WRITE_ONCE(enic->wq[i].xdp_tx_pending, false); spin_unlock_bh(&enic->wq[i].lock); } + for (i = 0; i < enic->rq_count; i++) + enic_xsk_rx_tx_flush(enic, i); for (i = 0; i < enic->rq_count; i++) { vnic_rq_clean(&enic->rq[i].vrq, enic_free_rq_buf); enic_xdp_rxq_teardown(&enic->rq[i]); @@ -1926,6 +1939,7 @@ static int enic_open(struct net_device *netdev) return -EBUSY; WRITE_ONCE(enic->datapath_open, false); + WRITE_ONCE(enic->xsk_wakeup_enabled, false); if (READ_ONCE(enic->dma_blocked)) return -EIO; if (!enic->vlan_rewrite_valid) { @@ -1966,15 +1980,21 @@ static int enic_open(struct net_device *netdev) } for (i = 0; i < enic->rq_count; i++) { + /* A bound XSK pool must not starve stack RX while no XDP + * program is attached. Keep ordinary page-backed RX active. + */ enic->rq[i].xdp_enabled = xdp_enabled; - /* create a page pool for each RQ */ - pp_params.napi = &enic->napi[i]; - pp_params.queue_idx = i; - enic->rq[i].pool = page_pool_create(&pp_params); - if (IS_ERR(enic->rq[i].pool)) { - err = PTR_ERR(enic->rq[i].pool); - enic->rq[i].pool = NULL; - goto err_out_free_rq; + enic->rq[i].xsk_rx_active = xdp_enabled && enic->rq[i].xsk_pool; + if (!enic->rq[i].xsk_rx_active) { + /* create a page pool for each page-backed RQ */ + pp_params.napi = &enic->napi[i]; + pp_params.queue_idx = i; + enic->rq[i].pool = page_pool_create(&pp_params); + if (IS_ERR(enic->rq[i].pool)) { + err = PTR_ERR(enic->rq[i].pool); + enic->rq[i].pool = NULL; + goto err_out_free_rq; + } } err = enic_xdp_rxq_setup(enic, i); if (err) @@ -1982,9 +2002,11 @@ static int enic_open(struct net_device *netdev) /* enable rq before updating rq desc */ vnic_rq_enable(&enic->rq[i].vrq); - vnic_rq_fill(&enic->rq[i].vrq, enic_rq_alloc_buf); + err = vnic_rq_fill(&enic->rq[i].vrq, enic_rq_alloc_buf); + enic_xsk_rx_refill_done(&enic->rq[i], err); /* Need at least one buffer on ring to get going */ - if (vnic_rq_desc_used(&enic->rq[i].vrq) == 0) { + if (!enic->rq[i].xsk_rx_active && + vnic_rq_desc_used(&enic->rq[i].vrq) == 0) { netdev_err(netdev, "Unable to alloc receive buffers\n"); err = -ENOMEM; goto err_out_free_rq; @@ -2016,6 +2038,7 @@ static int enic_open(struct net_device *netdev) for (i = 0; i < enic->intr_count; i++) vnic_intr_unmask(&enic->intr[i]); + WRITE_ONCE(enic->xsk_wakeup_enabled, true); WRITE_ONCE(enic->datapath_open, true); enic_notify_timer_start(enic); enic_rfs_timer_start(enic); @@ -2078,6 +2101,7 @@ static int __enic_stop(struct net_device *netdev, bool *reset_performed) if (reset_performed) *reset_performed = false; + WRITE_ONCE(enic->xsk_wakeup_enabled, false); if (!READ_ONCE(enic->datapath_open)) return 0; WRITE_ONCE(enic->datapath_open, false); @@ -2200,12 +2224,69 @@ int enic_xdp_reload(struct net_device *netdev, struct bpf_prog *old_prog, return err; } +int enic_xdp_reload_pool_swap(struct net_device *netdev, + unsigned int rq_index, + struct xsk_buff_pool *new_pool, + struct xsk_buff_pool **old_pool) +{ + struct enic *enic = netdev_priv(netdev); + int err, rollback_err; + + ASSERT_RTNL(); + + *old_pool = enic->rq[rq_index].xsk_pool; + if (!netif_running(netdev)) { + enic->rq[rq_index].xsk_pool = new_pool; + enic->wq[rq_index].xsk_pool = new_pool; + return 0; + } + + err = enic_stop(netdev); + if (err) + return err; + + /* NAPI and both hardware rings are now quiesced. */ + enic->rq[rq_index].xsk_pool = new_pool; + enic->wq[rq_index].xsk_pool = new_pool; + err = enic_open(netdev); + if (!err) + return 0; + if (!new_pool) { + if (READ_ONCE(enic->dma_blocked)) { + netdev_err(netdev, + "failed to reopen after XSK pool removal: PCI DMA is blocked\n"); + } else { + netdev_err(netdev, + "failed to reopen after XSK pool removal: %d; scheduling reset\n", + err); + schedule_work(&enic->reset); + } + return 0; + } + + enic->rq[rq_index].xsk_pool = *old_pool; + enic->wq[rq_index].xsk_pool = *old_pool; + rollback_err = enic_open(netdev); + if (rollback_err) + netdev_err(netdev, + "failed to restore interface after XSK pool setup error: %d\n", + rollback_err); + if (rollback_err && !READ_ONCE(enic->dma_blocked)) + schedule_work(&enic->reset); + + return err; +} + static int _enic_change_mtu(struct net_device *netdev, int new_mtu) { + struct enic *enic = netdev_priv(netdev); bool running = netif_running(netdev); int err = 0; ASSERT_RTNL(); + err = enic_xsk_validate_mtu(enic, new_mtu); + if (err) + return err; if (running) { err = enic_stop(netdev); @@ -3086,6 +3167,7 @@ static const struct net_device_ops enic_netdev_dynamic_ops = { .ndo_set_features = enic_set_features, .ndo_bpf = enic_xdp, .ndo_xdp_xmit = enic_xdp_xmit, + .ndo_xsk_wakeup = enic_xsk_wakeup, }; static const struct net_device_ops enic_netdev_ops = { @@ -3114,6 +3196,7 @@ static const struct net_device_ops enic_netdev_ops = { .ndo_set_features = enic_set_features, .ndo_bpf = enic_xdp, .ndo_xdp_xmit = enic_xdp_xmit, + .ndo_xsk_wakeup = enic_xsk_wakeup, }; static const struct netdev_stat_ops enic_netdev_stat_ops = { @@ -3750,6 +3833,11 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) for (i = 0; i < enic->wq_count; i++) spin_lock_init(&enic->wq[i].lock); + for (i = 0; i < enic->rq_count; i++) { + spin_lock_init(&enic->rq[i].xsk_wakeup_lock); + spin_lock_init(&enic->rq[i].xsk_tx_done_lock); + INIT_LIST_HEAD(&enic->rq[i].xsk_tx_done); + } /* Register net device */ @@ -3775,7 +3863,9 @@ static int enic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) netdev->stat_ops = &enic_netdev_stat_ops; if (enic_xdp_supported(enic)) { netdev->xdp_features = NETDEV_XDP_ACT_BASIC | - NETDEV_XDP_ACT_REDIRECT | NETDEV_XDP_ACT_NDO_XMIT; + NETDEV_XDP_ACT_REDIRECT | NETDEV_XDP_ACT_NDO_XMIT | + NETDEV_XDP_ACT_XSK_ZEROCOPY; + netdev->xdp_zc_max_segs = 1; } netdev->watchdog_timeo = 2 * HZ; diff --git a/drivers/net/ethernet/cisco/enic/enic_rq.c b/drivers/net/ethernet/cisco/enic/enic_rq.c index 350e42c556ad..f30de4757613 100644 --- a/drivers/net/ethernet/cisco/enic/enic_rq.c +++ b/drivers/net/ethernet/cisco/enic/enic_rq.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "enic.h" #include "enic_res.h" #include "enic_rq.h" @@ -282,6 +283,31 @@ int enic_rq_alloc_buf(struct vnic_rq *rq) struct vnic_rq_buf *buf = rq->to_use; struct page *page; unsigned int truesize = len + headroom; + struct xdp_buff *xdp; + + if (erq->xsk_rx_active) { + xdp = buf->os_buf; + if (!xdp) { + /* Set before checking the fill ring so userspace cannot + * miss need_wakeup when the ring is empty. + */ + if (xsk_uses_need_wakeup(erq->xsk_pool)) + enic_xsk_set_rx_need_wakeup(erq); + xdp = xsk_buff_alloc(erq->xsk_pool); + if (unlikely(!xdp)) { + rqstats->xsk_fill_empty++; + return -ENOMEM; + } + enic_xsk_buff_init(xdp); + } + + buf->offset = 0; + buf->truesize = xdp->frame_sz; + dma_addr = xsk_buff_xdp_get_dma(xdp); + enic_queue_rq_desc(rq, xdp, os_buf_index, dma_addr, len); + + return 0; + } if (erq->xdp_enabled) truesize += ENIC_XDP_TAILROOM; @@ -314,8 +340,11 @@ void enic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf) if (!buf->os_buf) return; - page_pool_put_full_page(erq->pool, (struct page *)buf->os_buf, - true); + if (erq->xsk_rx_active) + xsk_buff_free(buf->os_buf); + else + page_pool_put_full_page(erq->pool, (struct page *)buf->os_buf, + true); buf->os_buf = NULL; buf->dma_addr = 0; } @@ -355,6 +384,23 @@ static void enic_rq_indicate_buf(struct enic *enic, struct vnic_rq *rq, /* Good receive */ rqstats->bytes += bytes_written; + if (erq->xsk_rx_active) { + napi = &enic->napi[rq->index]; + skb = enic_xdp_run_xsk(enic, erq, buf, bytes_written); + if (!skb) + return; + + enic_rq_set_skb_flags(rq, type, rss_hash, rss_type, fcoe, + fcoe_fc_crc_ok, vlan_stripped, + csum_not_calc, tcp_udp_csum_ok, + ipv6, ipv4_csum_ok, vlan_tci, skb); + napi_gro_receive(napi, skb); + if (enic->rx_coalesce_setting.use_adaptive_rx_coalesce) + enic_intr_update_pkt_size(&cq->pkt_size_counter, + bytes_written); + return; + } + if (erq->xdp_enabled) { page_pool_dma_sync_for_cpu(erq->pool, buf->os_buf, buf->offset, diff --git a/drivers/net/ethernet/cisco/enic/enic_wq.c b/drivers/net/ethernet/cisco/enic/enic_wq.c index 0f01721fb9a2..b165335d292a 100644 --- a/drivers/net/ethernet/cisco/enic/enic_wq.c +++ b/drivers/net/ethernet/cisco/enic/enic_wq.c @@ -81,6 +81,16 @@ void enic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf) struct enic *enic = vnic_dev_priv(wq->vdev); struct xdp_frame *xdpf; + if (buf->type == VNIC_WQ_BUF_XSK_RX) { + if (WARN_ON_ONCE(!buf->os_buf)) { + enic_xsk_tx_buf_clear(buf); + return; + } + enic_xsk_rx_tx_complete(enic, buf->os_buf); + enic_xsk_tx_buf_clear(buf); + return; + } + if (buf->type == VNIC_WQ_BUF_XSK) { if (WARN_ON_ONCE(!buf->xsk_pool)) { enic_xsk_tx_buf_clear(buf); @@ -149,7 +159,7 @@ static bool enic_wq_service(struct vnic_dev *vdev, struct cq_desc *cq_desc, spin_lock(&enic->wq[q_number].lock); vwq = &enic->wq[q_number].vwq; if (skbs_only) { - /* A CQ entry can complete several descriptors. Defer the whole + /* A CQ entry can complete several descriptors. Defer the whole * entry if any buffer needs XDP, XSK, or page-pool completion. */ buf = vwq->to_clean; diff --git a/drivers/net/ethernet/cisco/enic/enic_xdp.c b/drivers/net/ethernet/cisco/enic/enic_xdp.c index dbb5f0e6c14f..b6989a4dd79b 100644 --- a/drivers/net/ethernet/cisco/enic/enic_xdp.c +++ b/drivers/net/ethernet/cisco/enic/enic_xdp.c @@ -9,12 +9,33 @@ #include #include #include +#include #include #include "enic.h" #include "enic_res.h" #include "enic_xdp.h" +#define ENIC_XSK_TX_BATCH 64 + +/* Overlay the driver-private XSK scratch space after struct xdp_buff. */ +struct enic_xsk_buff { + struct xdp_buff xdp; + struct list_head tx_done; +}; + +static struct enic_xsk_buff *enic_xsk_buff(struct xdp_buff *xdp) +{ + XSK_CHECK_PRIV_TYPE(struct enic_xsk_buff); + + return container_of(xdp, struct enic_xsk_buff, xdp); +} + +void enic_xsk_buff_init(struct xdp_buff *xdp) +{ + INIT_LIST_HEAD(&enic_xsk_buff(xdp)->tx_done); +} + bool enic_xdp_attached(struct enic *enic) { return rcu_access_pointer(enic->xdp_prog); @@ -33,8 +54,13 @@ int enic_xdp_rxq_setup(struct enic *enic, unsigned int rq_index) if (err) return err; - err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, - MEM_TYPE_PAGE_POOL, rq->pool); + if (rq->xsk_rx_active) + err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, + MEM_TYPE_XSK_BUFF_POOL, + rq->xsk_pool); + else + err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, + MEM_TYPE_PAGE_POOL, rq->pool); if (err) xdp_rxq_info_unreg(&rq->xdp_rxq); @@ -47,6 +73,7 @@ void enic_xdp_rxq_teardown(struct enic_rq *rq) xdp_rxq_info_unreg(&rq->xdp_rxq); rq->xdp_enabled = false; + rq->xsk_rx_active = false; rq->xdp_flush_pending = false; } @@ -94,6 +121,36 @@ static int enic_xdp_xmit_frame(struct enic *enic, unsigned int wq_index, return 0; } +static int enic_xdp_xmit_xsk_rx(struct enic *enic, unsigned int wq_index, + struct xdp_buff *xdp, dma_addr_t dma_addr, + unsigned int len) +{ + struct enic_wq *wq = &enic->wq[wq_index]; + struct vnic_wq_buf *buf; + + if (unlikely(!len || len > WQ_ENET_MAX_DESC_LEN)) + return -EINVAL; + + spin_lock(&wq->lock); + if (unlikely(!READ_ONCE(enic->datapath_open))) { + spin_unlock(&wq->lock); + return -ENETDOWN; + } + if (unlikely(vnic_wq_desc_avail(&wq->vwq) <= + MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS)) { + spin_unlock(&wq->lock); + return -ENOSPC; + } + + buf = wq->vwq.to_use; + enic_queue_wq_desc(&wq->vwq, xdp, dma_addr, len, 0, 0, 1, 0); + buf->type = VNIC_WQ_BUF_XSK_RX; + WRITE_ONCE(wq->xdp_tx_pending, true); + spin_unlock(&wq->lock); + + return 0; +} + static void enic_xdp_wq_flush(struct enic *enic, struct enic_wq *wq) { if (!READ_ONCE(wq->xdp_tx_pending)) @@ -225,6 +282,345 @@ bool enic_xdp_run(struct enic *enic, struct enic_rq *rq, return true; } +struct sk_buff *enic_xdp_run_xsk(struct enic *enic, struct enic_rq *rq, + struct vnic_rq_buf *buf, u16 len) +{ + struct xdp_buff *xdp = buf->os_buf; + struct bpf_prog *prog; + struct sk_buff *skb; + u32 act; + + xsk_buff_dma_sync_for_cpu(xdp); + xsk_buff_set_size(xdp, len); + rq->stats.xsk_rx_pkts++; + rq->stats.xsk_rx_bytes += len; + + rcu_read_lock(); + prog = rcu_dereference(enic->xdp_prog); + /* A pool may remain bound without an attached XDP program. */ + act = prog ? bpf_prog_run_xdp(prog, xdp) : XDP_PASS; + switch (act) { + case XDP_PASS: + skb = xdp_build_skb_from_zc(xdp); + if (unlikely(!skb)) { + rq->stats.no_skb++; + rq->stats.xdp_drop++; + goto out_free; + } + buf->os_buf = NULL; + buf->dma_addr = 0; + rq->stats.xdp_pass++; + rcu_read_unlock(); + return skb; + case XDP_TX: { + unsigned int xdp_len = xdp->data_end - xdp->data; + dma_addr_t dma_addr; + + if (unlikely(!xdp_len || xdp_len > WQ_ENET_MAX_DESC_LEN)) { + rq->stats.xdp_tx_drops++; + break; + } + + /* xdp->data may have moved. Derive DMA from the UMEM frame base + * and synchronize the exact post-BPF range for the device. + */ + dma_addr = xsk_buff_xdp_get_frame_dma(xdp) + + xsk_pool_get_headroom(rq->xsk_pool) - + XDP_PACKET_HEADROOM + + (xdp->data - xdp->data_hard_start); + xsk_buff_raw_dma_sync_for_device(rq->xsk_pool, dma_addr, xdp_len); + if (unlikely(!enic->wq_count || + enic_xdp_xmit_xsk_rx(enic, + rq->vrq.index % + enic->wq_count, + xdp, dma_addr, xdp_len))) { + rq->stats.xdp_tx_drops++; + } else { + buf->os_buf = NULL; + buf->dma_addr = 0; + rq->stats.xdp_tx++; + rcu_read_unlock(); + return NULL; + } + break; + } + case XDP_REDIRECT: + if (unlikely(xdp_do_redirect(enic->netdev, xdp, prog))) { + rq->stats.xdp_redirect_drops++; + break; + } + buf->os_buf = NULL; + buf->dma_addr = 0; + rq->stats.xdp_redirect++; + rq->xdp_flush_pending = true; + rcu_read_unlock(); + return NULL; + case XDP_DROP: + rq->stats.xdp_drop++; + break; + case XDP_ABORTED: + trace_xdp_exception(enic->netdev, prog, act); + rq->stats.xdp_aborted++; + break; + default: + bpf_warn_invalid_xdp_action(enic->netdev, prog, act); + trace_xdp_exception(enic->netdev, prog, act); + rq->stats.xdp_invalid++; + break; + } + +out_free: + xsk_buff_free(xdp); + buf->os_buf = NULL; + buf->dma_addr = 0; + rcu_read_unlock(); + return NULL; +} + +int enic_xsk_validate_mtu(struct enic *enic, unsigned int mtu) +{ + unsigned int frame_size, i; + + for (i = 0; i < enic->rq_count; i++) { + if (!enic->rq[i].xsk_pool) + continue; + + frame_size = xsk_pool_get_rx_frame_size(enic->rq[i].xsk_pool); + if (frame_size >= mtu + ENIC_RX_MAX_L2_LEN) + continue; + + netdev_err(enic->netdev, + "MTU %u requires an XSK frame of at least %u bytes on queue %u (have %u)\n", + mtu, mtu + ENIC_RX_MAX_L2_LEN, i, frame_size); + return -EINVAL; + } + + return 0; +} + +bool enic_xsk_rx_need_wakeup(struct enic_rq *rq) +{ + return rq->xsk_rx_active && xsk_uses_need_wakeup(rq->xsk_pool); +} + +void enic_xsk_set_rx_need_wakeup(struct enic_rq *rq) +{ + spin_lock_bh(&rq->xsk_wakeup_lock); + xsk_set_rx_need_wakeup(rq->xsk_pool); + spin_unlock_bh(&rq->xsk_wakeup_lock); +} + +void enic_xsk_rx_refill_done(struct enic_rq *rq, int err) +{ + if (err || !enic_xsk_rx_need_wakeup(rq)) + return; + + spin_lock_bh(&rq->xsk_wakeup_lock); + xsk_clear_rx_need_wakeup(rq->xsk_pool); + spin_unlock_bh(&rq->xsk_wakeup_lock); +} + +bool enic_xsk_tx_service(struct enic *enic, unsigned int wq_index) +{ + const unsigned int reserve = MAX_SKB_FRAGS + ENIC_DESC_MAX_SPLITS; + struct enic_wq *wq = &enic->wq[wq_index]; + struct xsk_buff_pool *pool = wq->xsk_pool; + struct xdp_desc *descs; + unsigned int capacity; + unsigned int sent = 0; + unsigned int i; + bool done; + + if (!pool) + return true; + if (!pool->tx_descs) + return true; + /* Set before checking carrier or the TX ring to close the producer race. + * Clear it again only when this NAPI remains in polling mode. + */ + if (xsk_uses_need_wakeup(pool)) { + spin_lock_bh(&enic->rq[wq_index].xsk_wakeup_lock); + xsk_set_tx_need_wakeup(pool); + spin_unlock_bh(&enic->rq[wq_index].xsk_wakeup_lock); + } + + if (unlikely(!netif_carrier_ok(enic->netdev))) + return true; + + spin_lock(&wq->lock); + if (unlikely(!READ_ONCE(enic->datapath_open))) { + done = true; + goto out_need_wakeup; + } + + capacity = vnic_wq_desc_avail(&wq->vwq); + if (capacity <= reserve) { + wq->stats.xsk_tx_no_room++; + /* Outstanding WQ descriptors will raise the next completion + * interrupt; do not spin this NAPI while waiting for room. + */ + done = true; + goto out_need_wakeup; + } + capacity = min(capacity - reserve, ENIC_XSK_TX_BATCH); + + /* The batch API transfers ownership of every returned descriptor. + * Bound the batch by available WQ space first, then post the whole batch + * while holding the same lock. + */ + sent = xsk_tx_peek_release_desc_batch(pool, capacity); + descs = pool->tx_descs; + for (i = 0; i < sent; i++) { + struct vnic_wq_buf *buf = wq->vwq.to_use; + dma_addr_t dma_addr; + + dma_addr = xsk_buff_raw_get_dma(pool, descs[i].addr); + xsk_buff_raw_dma_sync_for_device(pool, dma_addr, descs[i].len); + enic_queue_wq_desc(&wq->vwq, NULL, dma_addr, descs[i].len, + 0, 0, 1, 0); + buf->type = VNIC_WQ_BUF_XSK; + buf->xsk_pool = pool; + wq->stats.xsk_tx_pkts++; + wq->stats.xsk_tx_bytes += descs[i].len; + } + + if (sent) { + vnic_wq_doorbell(&wq->vwq); + WRITE_ONCE(wq->xdp_tx_pending, false); + } + done = sent < capacity; + +out_need_wakeup: + spin_unlock(&wq->lock); + if (!done && xsk_uses_need_wakeup(pool)) { + spin_lock_bh(&enic->rq[wq_index].xsk_wakeup_lock); + xsk_clear_tx_need_wakeup(pool); + spin_unlock_bh(&enic->rq[wq_index].xsk_wakeup_lock); + } + + return done; +} + +static void enic_xsk_schedule_napi(struct napi_struct *napi) +{ + if (!napi_if_scheduled_mark_missed(napi)) + napi_schedule(napi); +} + +void enic_xsk_rx_tx_complete(struct enic *enic, struct xdp_buff *xdp) +{ + struct enic_xsk_buff *xskb = enic_xsk_buff(xdp); + unsigned int rq_index = xdp->rxq->queue_index; + struct enic_rq *rq; + + if (WARN_ON_ONCE(rq_index >= enic->rq_count) || + WARN_ON_ONCE(!list_empty(&xskb->tx_done))) + return; + + rq = &enic->rq[rq_index]; + spin_lock_bh(&rq->xsk_tx_done_lock); + list_add_tail(&xskb->tx_done, &rq->xsk_tx_done); + spin_unlock_bh(&rq->xsk_tx_done_lock); + + if (READ_ONCE(enic->xsk_wakeup_enabled) && + vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX) + enic_xsk_schedule_napi(&enic->napi[rq_index]); +} + +void enic_xsk_rx_tx_flush(struct enic *enic, unsigned int rq_index) +{ + struct enic_rq *rq = &enic->rq[rq_index]; + struct enic_xsk_buff *xskb, *tmp; + LIST_HEAD(done); + + spin_lock_bh(&rq->xsk_tx_done_lock); + list_splice_init(&rq->xsk_tx_done, &done); + spin_unlock_bh(&rq->xsk_tx_done_lock); + + list_for_each_entry_safe(xskb, tmp, &done, tx_done) { + list_del_init(&xskb->tx_done); + xsk_buff_free(&xskb->xdp); + } +} + +int enic_xsk_wakeup(struct net_device *netdev, u32 queue_id, u32 flags) +{ + struct enic *enic = netdev_priv(netdev); + struct xsk_buff_pool *pool; + + if (flags & ~(XDP_WAKEUP_RX | XDP_WAKEUP_TX)) + return -EINVAL; + if (!flags) + flags = XDP_WAKEUP_RX | XDP_WAKEUP_TX; + if (!netif_running(netdev) || + !READ_ONCE(enic->xsk_wakeup_enabled)) + return -ENETDOWN; + if (queue_id >= enic->rq_count || queue_id >= enic->wq_count) + return -EINVAL; + + pool = READ_ONCE(enic->rq[queue_id].xsk_pool); + if (!pool || pool != READ_ONCE(enic->wq[queue_id].xsk_pool)) + return -ENXIO; + + if (vnic_dev_get_intr_mode(enic->vdev) != VNIC_DEV_INTR_MODE_MSIX) { + enic_xsk_schedule_napi(&enic->napi[0]); + return 0; + } + + if (flags & XDP_WAKEUP_RX) + enic_xsk_schedule_napi(&enic->napi[queue_id]); + if (flags & XDP_WAKEUP_TX) + enic_xsk_schedule_napi(&enic->napi[enic_cq_wq(enic, + queue_id)]); + + return 0; +} + +static int enic_xsk_setup_pool(struct net_device *netdev, + struct netdev_bpf *bpf) +{ + struct xsk_buff_pool *pool = bpf->xsk.pool; + struct xsk_buff_pool *old_pool; + struct enic *enic = netdev_priv(netdev); + unsigned int queue_id = bpf->xsk.queue_id; + unsigned int frame_size; + int err; + + ASSERT_RTNL(); + + if (pool && !enic_xdp_supported(enic)) + return -EOPNOTSUPP; + + if (queue_id >= enic->rq_count || queue_id >= enic->wq_count) + return -EINVAL; + if (pool == enic->rq[queue_id].xsk_pool) + return 0; + + if (pool) { + frame_size = xsk_pool_get_rx_frame_size(pool); + if (frame_size < netdev->mtu + ENIC_RX_MAX_L2_LEN) + return -EINVAL; + if (xsk_pool_get_chunk_size(pool) > WQ_ENET_MAX_DESC_LEN) + return -EINVAL; + + err = xsk_pool_dma_map(pool, &enic->pdev->dev, 0); + if (err) + return err; + } + + err = enic_xdp_reload_pool_swap(netdev, queue_id, pool, &old_pool); + if (err) { + if (pool) + xsk_pool_dma_unmap(pool, 0); + return err; + } + + if (old_pool) + xsk_pool_dma_unmap(old_pool, 0); + + return 0; +} + static int enic_xdp_setup(struct net_device *netdev, struct bpf_prog *prog, struct netlink_ext_ack *extack) { @@ -266,6 +662,8 @@ int enic_xdp(struct net_device *netdev, struct netdev_bpf *bpf) switch (bpf->command) { case XDP_SETUP_PROG: return enic_xdp_setup(netdev, bpf->prog, bpf->extack); + case XDP_SETUP_XSK_POOL: + return enic_xsk_setup_pool(netdev, bpf); default: return -EINVAL; } diff --git a/drivers/net/ethernet/cisco/enic/enic_xdp.h b/drivers/net/ethernet/cisco/enic/enic_xdp.h index de6abc9c4e65..2abdff4ba123 100644 --- a/drivers/net/ethernet/cisco/enic/enic_xdp.h +++ b/drivers/net/ethernet/cisco/enic/enic_xdp.h @@ -15,6 +15,7 @@ struct net_device; struct netdev_bpf; struct vnic_rq_buf; struct xdp_frame; +struct xsk_buff_pool; #define ENIC_XDP_HEADROOM XDP_PACKET_HEADROOM #define ENIC_XDP_TAILROOM SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) @@ -25,11 +26,27 @@ int enic_xdp_xmit(struct net_device *netdev, int n, struct xdp_frame **frames, u32 flags); int enic_xdp_reload(struct net_device *netdev, struct bpf_prog *old_prog, struct bpf_prog *new_prog); +int enic_xdp_reload_pool_swap(struct net_device *netdev, + unsigned int rq_index, + struct xsk_buff_pool *new_pool, + struct xsk_buff_pool **old_pool); +int enic_xsk_validate_mtu(struct enic *enic, unsigned int mtu); +bool enic_xsk_rx_need_wakeup(struct enic_rq *rq); +void enic_xsk_set_rx_need_wakeup(struct enic_rq *rq); +void enic_xsk_rx_refill_done(struct enic_rq *rq, int err); +bool enic_xsk_tx_service(struct enic *enic, unsigned int wq_index); +int enic_xsk_wakeup(struct net_device *netdev, u32 queue_id, u32 flags); +void enic_xsk_buff_init(struct xdp_buff *xdp); +void enic_xsk_rx_tx_complete(struct enic *enic, struct xdp_buff *xdp); +void enic_xsk_rx_tx_flush(struct enic *enic, unsigned int rq_index); + int enic_xdp_rxq_setup(struct enic *enic, unsigned int rq_index); void enic_xdp_rxq_teardown(struct enic_rq *rq); bool enic_xdp_run(struct enic *enic, struct enic_rq *rq, struct vnic_rq_buf *buf, u16 *len); +struct sk_buff *enic_xdp_run_xsk(struct enic *enic, struct enic_rq *rq, + struct vnic_rq_buf *buf, u16 len); void enic_xdp_tx_flush(struct enic *enic, unsigned int rq_index); void enic_xdp_redirect_flush(struct enic *enic, unsigned int rq_index); -- 2.43.0