+{
+ return XDP_PACKET_HEADROOM;
+}
+
static void tsnep_rx_ring_cleanup(struct tsnep_rx *rx)
{
struct device *dmadev = rx->adapter->dmadev;
@@ -1024,6 +1035,65 @@ static int tsnep_rx_refill(struct tsnep_rx *rx, int count, bool reuse)
return i;
}
+static int tsnep_xdp_run_prog(struct tsnep_rx *rx, struct bpf_prog *prog,
+ struct xdp_buff *xdp)
+{
+ unsigned int length;
+ unsigned int sync;
+ u32 act;
+
+ length = xdp->data_end - xdp->data_hard_start - tsnep_rx_offset_xdp();
+
+ act = bpf_prog_run_xdp(prog, xdp);
+
+ /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
+ sync = xdp->data_end - xdp->data_hard_start - tsnep_rx_offset_xdp();
+ sync = max(sync, length);
+
+ switch (act) {
+ case XDP_PASS:
+ return TSNEP_XDP_PASS;
+ case XDP_TX:
+ if (tsnep_xdp_xmit_back(rx->adapter, xdp) < 0)
+ goto out_failure;
+ return TSNEP_XDP_TX;
+ case XDP_REDIRECT:
+ if (xdp_do_redirect(rx->adapter->netdev, xdp, prog) < 0)
+ goto out_failure;
+ return TSNEP_XDP_REDIRECT;
+ default:
+ bpf_warn_invalid_xdp_action(rx->adapter->netdev, prog, act);
+ fallthrough;
+ case XDP_ABORTED:
+out_failure:
+ trace_xdp_exception(rx->adapter->netdev, prog, act);
+ fallthrough;
+ case XDP_DROP:
+ page_pool_put_page(rx->page_pool, virt_to_head_page(xdp->data),
+ sync, true);
+ return TSNEP_XDP_CONSUMED;
+ }
+}
+
+static void tsnep_finalize_xdp(struct tsnep_adapter *adapter, int status)
+{
+ int cpu = smp_processor_id();
+ int queue;
+ struct netdev_queue *nq;
+
+ if (status & TSNEP_XDP_TX) {
+ queue = cpu % adapter->num_tx_queues;
+ nq = netdev_get_tx_queue(adapter->netdev, queue);
+
+ __netif_tx_lock(nq, cpu);
+ tsnep_xdp_xmit_flush(&adapter->tx[queue]);
+ __netif_tx_unlock(nq);
+ }
+
+ if (status & TSNEP_XDP_REDIRECT)
+ xdp_do_flush();
+}
+
static struct sk_buff *tsnep_build_skb(struct tsnep_rx *rx, struct page *page,
int length)
{
@@ -1062,12 +1132,17 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,
int desc_available;
int done = 0;
enum dma_data_direction dma_dir;
+ struct bpf_prog *prog;
struct tsnep_rx_entry *entry;
+ struct xdp_buff xdp;
+ int xdp_status = 0;
struct sk_buff *skb;
int length;
+ int retval;
desc_available = tsnep_rx_desc_available(rx);
dma_dir = page_pool_get_dma_dir(rx->page_pool);
+ prog = READ_ONCE(rx->adapter->xdp_prog);
while (likely(done < budget) && (rx->read != rx->write)) {
entry = &rx->entry[rx->read];
@@ -1111,6 +1186,28 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,
rx->read = (rx->read + 1) % TSNEP_RING_SIZE;
desc_available++;
+ if (prog) {
+ xdp_init_buff(&xdp, PAGE_SIZE, &rx->xdp_rxq);
+ xdp_prepare_buff(&xdp, page_address(entry->page),
+ tsnep_rx_offset_xdp() + TSNEP_RX_INLINE_METADATA_SIZE,
+ length - TSNEP_RX_INLINE_METADATA_SIZE,
+ false);
+ retval = tsnep_xdp_run_prog(rx, prog, &xdp);
+ } else {
+ retval = TSNEP_XDP_PASS;
+ }
+ if (retval) {
+ if (retval & (TSNEP_XDP_TX | TSNEP_XDP_REDIRECT))
+ xdp_status |= retval;