tsnep: XDP socket zero-copy support
Subsystems (3)
v2 cover tsnep: XDP socket zero-copy support
Implement XDP socket zero-copy support for tsnep driver. I tried to follow existing drivers like igc as far as possible. But one main difference is that tsnep does not need any reconfiguration for XDP BPF program setup. So I decided to keep this behavior no matter if a XSK pool is used or not. As a result, tsnep starts using the XSK pool even if no XDP BPF program is available.
Another difference is that I tried to prevent potentially failing allocations during XSK pool setup. E.g. both memory models for page pool and XSK pool are registered all the time. Thus, XSK pool setup cannot end up with not working queues.
Some prework is done to reduce the last two XSK commits to actual XSK changes.
v2:
- eliminate modulo op in TX/RX path (Maciej Fijalkowski)
- eliminate retval variable in tsnep_rx_alloc_zc() (Maciej Fijalkowski)
- scope variable entry within loop (Maciej Fijalkowski)
- use DMA_ATTR_SKIP_CPU_SYNC directly (Maciej Fijalkowski)
- union for page/xdp in tsnep_rx_entry (Maciej Fijalkowski)
- provide performance numbers (Maciej Fijalkowski)
- use xsk_buff_alloc_batch() (Maciej Fijalkowski)
- use xsk_tx_peek_release_desc_batch() (Maciej Fijalkowski)
- don't call tsnep_rx_reopen() if netdev is down
- init adapter pointer of queue only once
Gerhard Engleder (6):
tsnep: Replace modulo operation with mask
tsnep: Rework TX/RX queue initialization
tsnep: Add functions for queue enable/disable
tsnep: Move skb receive action to separate function
tsnep: Add XDP socket zero-copy RX support
tsnep: Add XDP socket zero-copy TX support
drivers/net/ethernet/engleder/tsnep.h | 16 +- drivers/net/ethernet/engleder/tsnep_main.c | 864 ++++++++++++++++++--- drivers/net/ethernet/engleder/tsnep_xdp.c | 66 ++ 3 files changed, 823 insertions(+), 123 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/6 tsnep: Replace modulo operation with mask
TX/RX ring size is static and power of 2 to enable compiler to optimize modulo operation to mask operation. Make this optimization already in the code and don't rely on the compiler.
--- drivers/net/ethernet/engleder/tsnep.h | 1 + drivers/net/ethernet/engleder/tsnep_main.c | 28 +++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-)
drivers/net/ethernet/engleder/tsnep.h +1 -0
@@ -18,6 +18,7 @@#define TSNEP "tsnep"#define TSNEP_RING_SIZE 256 +#define TSNEP_RING_MASK (TSNEP_RING_SIZE - 1)#define TSNEP_RING_RX_REFILL 16#define TSNEP_RING_RX_REUSE (TSNEP_RING_SIZE - TSNEP_RING_SIZE / 4)#define TSNEP_RING_ENTRIES_PER_PAGE (PAGE_SIZE / TSNEP_DESC_SIZE)
drivers/net/ethernet/engleder/tsnep_main.c +14 -14
@@ -292,7 +292,7 @@ static int tsnep_tx_ring_init(struct tsnep_tx *tx) }for (i =0 ; i <TSNEP_RING_SIZE ; i++) { entry = &tx->entry[i]; - next_entry = &tx->entry[(i +1 ) %TSNEP_RING_SIZE ]; + next_entry = &tx->entry[(i +1 ) &TSNEP_RING_MASK ]; entry->desc->next =__cpu_to_le64 (next_entry->desc_dma); }
@@ -381,7 +381,7 @@ static int tsnep_tx_map(struct sk_buff *skb, struct tsnep_tx *tx, int count)int i;for (i =0 ; i < count; i++) { - entry = &tx->entry[(tx->write + i) %TSNEP_RING_SIZE ]; + entry = &tx->entry[(tx->write + i) &TSNEP_RING_MASK ];if (!i) { len =skb_headlen (skb);
@@ -419,7 +419,7 @@ static int tsnep_tx_unmap(struct tsnep_tx *tx, int index, int count)int i;for (i =0 ; i < count; i++) { - entry = &tx->entry[(index + i) %TSNEP_RING_SIZE ]; + entry = &tx->entry[(index + i) &TSNEP_RING_MASK ];if (entry->len) {if (entry->type &TSNEP_TX_TYPE_SKB )
@@ -481,9 +481,9 @@ static netdev_tx_t tsnep_xmit_frame_ring(struct sk_buff *skb,skb_shinfo (skb)->tx_flags |=SKBTX_IN_PROGRESS ;for (i =0 ; i < count; i++) -tsnep_tx_activate (tx, (tx->write + i) %TSNEP_RING_SIZE , length, +tsnep_tx_activate (tx, (tx->write + i) &TSNEP_RING_MASK , length, i == count -1 ); - tx->write = (tx->write + count) %TSNEP_RING_SIZE ; + tx->write = (tx->write + count) &TSNEP_RING_MASK ;skb_tx_timestamp (skb);
@@ -516,7 +516,7 @@ static int tsnep_xdp_tx_map(struct xdp_frame *xdpf, struct tsnep_tx *tx, frag =NULL ; len = xdpf->len;for (i =0 ; i < count; i++) { - entry = &tx->entry[(tx->write + i) %TSNEP_RING_SIZE ]; + entry = &tx->entry[(tx->write + i) &TSNEP_RING_MASK ];if (type &TSNEP_TX_TYPE_XDP_NDO ) { data =unlikely (frag) ?skb_frag_address (frag) : xdpf->data;
@@ -589,9 +589,9 @@ static bool tsnep_xdp_xmit_frame_ring(struct xdp_frame *xdpf, length = retval;for (i =0 ; i < count; i++) -tsnep_tx_activate (tx, (tx->write + i) %TSNEP_RING_SIZE , length, +tsnep_tx_activate (tx, (tx->write + i) &TSNEP_RING_MASK , length, i == count -1 ); - tx->write = (tx->write + count) %TSNEP_RING_SIZE ; + tx->write = (tx->write + count) &TSNEP_RING_MASK ;/* descriptor properties shall be valid before hardware is notified */ dma_wmb ();
@@ -691,7 +691,7 @@ static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget)/* xdpf is union with skb */ entry->skb =NULL ; - tx->read = (tx->read + count) %TSNEP_RING_SIZE ; + tx->read = (tx->read + count) &TSNEP_RING_MASK ; tx->packets++; tx->bytes += length +ETH_FCS_LEN ;
@@ -839,7 +839,7 @@ static int tsnep_rx_ring_init(struct tsnep_rx *rx)for (i =0 ; i <TSNEP_RING_SIZE ; i++) { entry = &rx->entry[i]; - next_entry = &rx->entry[(i +1 ) %TSNEP_RING_SIZE ]; + next_entry = &rx->entry[(i +1 ) &TSNEP_RING_MASK ]; entry->desc->next =__cpu_to_le64 (next_entry->desc_dma); }
@@ -925,7 +925,7 @@ static int tsnep_rx_refill(struct tsnep_rx *rx, int count, bool reuse)int retval;for (i =0 ; i < count && !alloc_failed; i++) { - index = (rx->write + i) %TSNEP_RING_SIZE ; + index = (rx->write + i) &TSNEP_RING_MASK ; retval =tsnep_rx_alloc_buffer (rx, index);if (unlikely (retval)) {
@@ -945,7 +945,7 @@ static int tsnep_rx_refill(struct tsnep_rx *rx, int count, bool reuse) }if (enable) { - rx->write = (rx->write + i) %TSNEP_RING_SIZE ; + rx->write = (rx->write + i) &TSNEP_RING_MASK ;/* descriptor properties shall be valid before hardware is * notified
@@ -1090,7 +1090,7 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,* empty RX ring, thus buffer cannot be used for * RX processing */ - rx->read = (rx->read +1 ) %TSNEP_RING_SIZE ; + rx->read = (rx->read +1 ) &TSNEP_RING_MASK ; desc_available++; rx->dropped++;
@@ -1117,7 +1117,7 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,*/ length -=TSNEP_RX_INLINE_METADATA_SIZE ; - rx->read = (rx->read +1 ) %TSNEP_RING_SIZE ; + rx->read = (rx->read +1 ) &TSNEP_RING_MASK ; desc_available++;if (prog) {
Signature · 1 line
2.30.2
PATCH v2 2/6 tsnep: Rework TX/RX queue initialization
In reply to Gerhard EnglederMake initialization of TX and RX queues less dynamic by moving some initialization from netdev open/close to device probing.
Additionally, move some initialization code to separate functions to enable future use in other execution paths.
This is done as preparation for queue reconfigure at runtime, which is necessary for XSK zero-copy support.
--- drivers/net/ethernet/engleder/tsnep_main.c | 94 ++++++++++++---------- 1 file changed, 51 insertions(+), 43 deletions(-)
drivers/net/ethernet/engleder/tsnep_main.c +51 -43
@@ -265,7 +265,7 @@ static void tsnep_tx_ring_cleanup(struct tsnep_tx *tx) } } -static int tsnep_tx_ring_init (struct tsnep_tx *tx) +static int tsnep_tx_ring_create (struct tsnep_tx *tx) {struct device *dmadev = tx->adapter->dmadev;struct tsnep_tx_entry *entry;
@@ -288,6 +288,7 @@ static int tsnep_tx_ring_init(struct tsnep_tx *tx) entry->desc = (struct tsnep_tx_desc *) (((u8 *)entry->desc_wb) +TSNEP_DESC_OFFSET ); entry->desc_dma = tx->page_dma[i] +TSNEP_DESC_SIZE * j; + entry->owner_user_flag = false; } }for (i =0 ; i <TSNEP_RING_SIZE ; i++) {
@@ -303,6 +304,19 @@ static int tsnep_tx_ring_init(struct tsnep_tx *tx)return retval; } +static void tsnep_tx_init (struct tsnep_tx *tx) +{ +dma_addr_t dma; + + dma = tx->entry[0 ].desc_dma |TSNEP_RESET_OWNER_COUNTER ; +iowrite32 (DMA_ADDR_LOW (dma), tx->addr +TSNEP_TX_DESC_ADDR_LOW ); +iowrite32 (DMA_ADDR_HIGH (dma), tx->addr +TSNEP_TX_DESC_ADDR_HIGH ); + tx->write =0 ; + tx->read =0 ; + tx->owner_counter =1 ; + tx->increment_owner_counter =TSNEP_RING_SIZE -1 ; +} +static void tsnep_tx_activate (struct tsnep_tx *tx,int index,int length,bool last) {
@@ -731,26 +745,15 @@ static bool tsnep_tx_pending(struct tsnep_tx *tx)return pending; } -static int tsnep_tx_open (struct tsnep_adapter *adapter,void __iomem *addr, -int queue_index,struct tsnep_tx *tx) +static int tsnep_tx_open (struct tsnep_tx *tx) { -dma_addr_t dma;int retval; -memset (tx,0 ,sizeof (*tx)); - tx->adapter = adapter; - tx->addr = addr; - tx->queue_index = queue_index; - - retval =tsnep_tx_ring_init (tx); + retval =tsnep_tx_ring_create (tx);if (retval)return retval; - dma = tx->entry[0 ].desc_dma |TSNEP_RESET_OWNER_COUNTER ; -iowrite32 (DMA_ADDR_LOW (dma), tx->addr +TSNEP_TX_DESC_ADDR_LOW ); -iowrite32 (DMA_ADDR_HIGH (dma), tx->addr +TSNEP_TX_DESC_ADDR_HIGH ); - tx->owner_counter =1 ; - tx->increment_owner_counter =TSNEP_RING_SIZE -1 ; +tsnep_tx_init (tx);return 0 ; }
@@ -795,7 +798,7 @@ static void tsnep_rx_ring_cleanup(struct tsnep_rx *rx) } } -static int tsnep_rx_ring_init (struct tsnep_rx *rx) +static int tsnep_rx_ring_create (struct tsnep_rx *rx) {struct device *dmadev = rx->adapter->dmadev;struct tsnep_rx_entry *entry;
@@ -850,6 +853,19 @@ static int tsnep_rx_ring_init(struct tsnep_rx *rx)return retval; } +static void tsnep_rx_init (struct tsnep_rx *rx) +{ +dma_addr_t dma; + + dma = rx->entry[0 ].desc_dma |TSNEP_RESET_OWNER_COUNTER ; +iowrite32 (DMA_ADDR_LOW (dma), rx->addr +TSNEP_RX_DESC_ADDR_LOW ); +iowrite32 (DMA_ADDR_HIGH (dma), rx->addr +TSNEP_RX_DESC_ADDR_HIGH ); + rx->write =0 ; + rx->read =0 ; + rx->owner_counter =1 ; + rx->increment_owner_counter =TSNEP_RING_SIZE -1 ; +} +static int tsnep_rx_desc_available (struct tsnep_rx *rx) {if (rx->read <= rx->write)
@@ -1181,26 +1197,15 @@ static bool tsnep_rx_pending(struct tsnep_rx *rx)return false; } -static int tsnep_rx_open (struct tsnep_adapter *adapter,void __iomem *addr, -int queue_index,struct tsnep_rx *rx) +static int tsnep_rx_open (struct tsnep_rx *rx) { -dma_addr_t dma;int retval; -memset (rx,0 ,sizeof (*rx)); - rx->adapter = adapter; - rx->addr = addr; - rx->queue_index = queue_index; - - retval =tsnep_rx_ring_init (rx); + retval =tsnep_rx_ring_create (rx);if (retval)return retval; - dma = rx->entry[0 ].desc_dma |TSNEP_RESET_OWNER_COUNTER ; -iowrite32 (DMA_ADDR_LOW (dma), rx->addr +TSNEP_RX_DESC_ADDR_LOW ); -iowrite32 (DMA_ADDR_HIGH (dma), rx->addr +TSNEP_RX_DESC_ADDR_HIGH ); - rx->owner_counter =1 ; - rx->increment_owner_counter =TSNEP_RING_SIZE -1 ; +tsnep_rx_init (rx);tsnep_rx_refill (rx,tsnep_rx_desc_available (rx), false);
@@ -1335,8 +1340,6 @@ static int tsnep_queue_open(struct tsnep_adapter *adapter,struct tsnep_tx *tx = queue->tx;int retval; - queue->adapter = adapter; -netif_napi_add (adapter->netdev, &queue->napi, tsnep_poll);if (rx) {
@@ -1377,27 +1380,18 @@ static int tsnep_queue_open(struct tsnep_adapter *adapter,static int tsnep_netdev_open (struct net_device *netdev) {struct tsnep_adapter *adapter =netdev_priv (netdev); -int tx_queue_index =0 ; -int rx_queue_index =0 ; -void __iomem *addr;int i, retval;for (i =0 ; i < adapter->num_queues; i++) {if (adapter->queue[i].tx) { - addr = adapter->addr +TSNEP_QUEUE (tx_queue_index); - retval =tsnep_tx_open (adapter, addr, tx_queue_index, - adapter->queue[i].tx); + retval =tsnep_tx_open (adapter->queue[i].tx);if (retval) goto failed; - tx_queue_index++; }if (adapter->queue[i].rx) { - addr = adapter->addr +TSNEP_QUEUE (rx_queue_index); - retval =tsnep_rx_open (adapter, addr, rx_queue_index, - adapter->queue[i].rx); + retval =tsnep_rx_open (adapter->queue[i].rx);if (retval) goto failed; - rx_queue_index++; } retval =tsnep_queue_open (adapter, &adapter->queue[i], i ==0 );
@@ -1796,9 +1790,16 @@ static int tsnep_queue_init(struct tsnep_adapter *adapter, int queue_count) adapter->num_tx_queues =1 ; adapter->num_rx_queues =1 ; adapter->num_queues =1 ; + adapter->queue[0 ].adapter = adapter; adapter->queue[0 ].irq = retval; adapter->queue[0 ].tx = &adapter->tx[0 ]; + adapter->queue[0 ].tx->adapter = adapter; + adapter->queue[0 ].tx->addr = adapter->addr +TSNEP_QUEUE (0 ); + adapter->queue[0 ].tx->queue_index =0 ; adapter->queue[0 ].rx = &adapter->rx[0 ]; + adapter->queue[0 ].rx->adapter = adapter; + adapter->queue[0 ].rx->addr = adapter->addr +TSNEP_QUEUE (0 ); + adapter->queue[0 ].rx->queue_index =0 ; adapter->queue[0 ].irq_mask = irq_mask; adapter->queue[0 ].irq_delay_addr = adapter->addr +ECM_INT_DELAY ; retval = tsnep_set_irq_coalesce(&adapter->queue[0],
@@ -1820,9 +1821,16 @@ static int tsnep_queue_init(struct tsnep_adapter *adapter, int queue_count) adapter->num_tx_queues++; adapter->num_rx_queues++; adapter->num_queues++; + adapter->queue[i].adapter = adapter; adapter->queue[i].irq = retval; adapter->queue[i].tx = &adapter->tx[i]; + adapter->queue[i].tx->adapter = adapter; + adapter->queue[i].tx->addr = adapter->addr +TSNEP_QUEUE (i); + adapter->queue[i].tx->queue_index = i; adapter->queue[i].rx = &adapter->rx[i]; + adapter->queue[i].rx->adapter = adapter; + adapter->queue[i].rx->addr = adapter->addr +TSNEP_QUEUE (i); + adapter->queue[i].rx->queue_index = i; adapter->queue[i].irq_mask = irq_mask << (ECM_INT_TXRX_SHIFT * i); adapter->queue[i].irq_delay_addr =
Signature · 1 line
2.30.2
PATCH v2 3/6 tsnep: Add functions for queue enable/disable
In reply to Gerhard EnglederMove queue enable and disable code to separate functions. This way the activation and deactivation of the queues are defined actions, which can be used in future execution paths.
This functions will be used for the queue reconfiguration at runtime, which is necessary for XSK zero-copy support.
--- drivers/net/ethernet/engleder/tsnep_main.c | 97 ++++++++++++++-------- 1 file changed, 64 insertions(+), 33 deletions(-)
drivers/net/ethernet/engleder/tsnep_main.c +64 -33
@@ -866,6 +866,24 @@ static void tsnep_rx_init(struct tsnep_rx *rx) rx->increment_owner_counter =TSNEP_RING_SIZE -1 ; } +static void tsnep_rx_enable (struct tsnep_rx *rx) +{ +/* descriptor properties shall be valid before hardware is notified */ +dma_wmb (); + +iowrite32 (TSNEP_CONTROL_RX_ENABLE , rx->addr +TSNEP_CONTROL ); +} + +static void tsnep_rx_disable (struct tsnep_rx *rx) +{ +u32 val; + +iowrite32 (TSNEP_CONTROL_RX_DISABLE , rx->addr +TSNEP_CONTROL ); +readx_poll_timeout (ioread32, rx->addr +TSNEP_CONTROL , val, + ((val &TSNEP_CONTROL_RX_ENABLE ) ==0 ),10000 , +1000000 ); +} +static int tsnep_rx_desc_available (struct tsnep_rx *rx) {if (rx->read <= rx->write)
@@ -932,19 +950,15 @@ static void tsnep_rx_activate(struct tsnep_rx *rx, int index) entry->desc->properties =__cpu_to_le32 (entry->properties); } -static int tsnep_rx_refill (struct tsnep_rx *rx,int count,bool reuse) +static int tsnep_rx_alloc (struct tsnep_rx *rx,int count,bool reuse) { -int index;bool alloc_failed = false; -bool enable = false; -int i; -int retval; +int i, index;for (i =0 ; i < count && !alloc_failed; i++) { index = (rx->write + i) &TSNEP_RING_MASK ; - retval =tsnep_rx_alloc_buffer (rx, index); -if (unlikely (retval)) { +if (unlikely (tsnep_rx_alloc_buffer (rx, index))) { rx->alloc_failed++; alloc_failed = true;
@@ -956,22 +970,23 @@ static int tsnep_rx_refill(struct tsnep_rx *rx, int count, bool reuse) }tsnep_rx_activate (rx, index); - - enable = true; } -if (enable) { +if (i) rx->write = (rx->write + i) &TSNEP_RING_MASK ; -/* descriptor properties shall be valid before hardware is -* notified -*/ -dma_wmb (); +return i; +} -iowrite32 (TSNEP_CONTROL_RX_ENABLE , rx->addr +TSNEP_CONTROL ); - } +static int tsnep_rx_refill (struct tsnep_rx *rx,int count,bool reuse) +{ +int desc_refilled; -return i; + desc_refilled =tsnep_rx_alloc (rx, count, reuse); +if (desc_refilled) +tsnep_rx_enable (rx); + +return desc_refilled; } static bool tsnep_xdp_run_prog(struct tsnep_rx *rx, struct bpf_prog *prog,
@@ -1199,6 +1214,7 @@ static bool tsnep_rx_pending(struct tsnep_rx *rx)static int tsnep_rx_open (struct tsnep_rx *rx) { +int desc_available;int retval; retval =tsnep_rx_ring_create (rx);
@@ -1207,20 +1223,19 @@ static int tsnep_rx_open(struct tsnep_rx *rx)tsnep_rx_init (rx); -tsnep_rx_refill (rx,tsnep_rx_desc_available (rx), false); + desc_available =tsnep_rx_desc_available (rx); + retval =tsnep_rx_alloc (rx, desc_available, false); +if (retval != desc_available) { +tsnep_rx_ring_cleanup (rx); + +return -ENOMEM ; + }return 0 ; }static void tsnep_rx_close (struct tsnep_rx *rx) { -u32 val; - -iowrite32 (TSNEP_CONTROL_RX_DISABLE , rx->addr +TSNEP_CONTROL ); -readx_poll_timeout (ioread32, rx->addr +TSNEP_CONTROL , val, - ((val &TSNEP_CONTROL_RX_ENABLE ) ==0 ),10000 , -1000000 ); -tsnep_rx_ring_cleanup (rx); }
@@ -1377,6 +1392,27 @@ static int tsnep_queue_open(struct tsnep_adapter *adapter,return retval; } +static void tsnep_queue_enable (struct tsnep_queue *queue) +{ +napi_enable (&queue->napi); +tsnep_enable_irq (queue->adapter, queue->irq_mask); + +if (queue->rx) +tsnep_rx_enable (queue->rx); +} + +static void tsnep_queue_disable (struct tsnep_queue *queue) +{ +napi_disable (&queue->napi); +tsnep_disable_irq (queue->adapter, queue->irq_mask); + +/* disable RX after NAPI polling has been disabled, because RX can be +* enabled during NAPI polling +*/ +if (queue->rx) +tsnep_rx_disable (queue->rx); +} +static int tsnep_netdev_open (struct net_device *netdev) {struct tsnep_adapter *adapter =netdev_priv (netdev);
@@ -1413,11 +1449,8 @@ static int tsnep_netdev_open(struct net_device *netdev)if (retval) goto phy_failed; -for (i =0 ; i < adapter->num_queues; i++) { -napi_enable (&adapter->queue[i].napi); - -tsnep_enable_irq (adapter, adapter->queue[i].irq_mask); - } +for (i =0 ; i < adapter->num_queues; i++) +tsnep_queue_enable (&adapter->queue[i]);return 0 ;
@@ -1444,9 +1477,7 @@ static int tsnep_netdev_close(struct net_device *netdev)tsnep_phy_close (adapter);for (i =0 ; i < adapter->num_queues; i++) { -tsnep_disable_irq (adapter, adapter->queue[i].irq_mask); - -napi_disable (&adapter->queue[i].napi); +tsnep_queue_disable (&adapter->queue[i]);tsnep_queue_close (&adapter->queue[i], i ==0 );
Signature · 1 line
2.30.2
PATCH v2 4/6 tsnep: Move skb receive action to separate function
In reply to Gerhard EnglederThe function tsnep_rx_poll() is already pretty long and the skb receive action can be reused for XSK zero-copy support. Move page based skb receive to separate function.
--- drivers/net/ethernet/engleder/tsnep_main.c | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-)
drivers/net/ethernet/engleder/tsnep_main.c +23 -16
@@ -1076,6 +1076,28 @@ static struct sk_buff *tsnep_build_skb(struct tsnep_rx *rx, struct page *page,return skb; } +static void tsnep_rx_page (struct tsnep_rx *rx,struct napi_struct *napi, +struct page *page,int length) +{ +struct sk_buff *skb; + + skb =tsnep_build_skb (rx, page, length); +if (skb) { +page_pool_release_page (rx->page_pool, page); + + rx->packets++; + rx->bytes += length; +if (skb->pkt_type ==PACKET_MULTICAST ) + rx->multicast++; + +napi_gro_receive (napi, skb); + }else { +page_pool_recycle_direct (rx->page_pool, page); + + rx->dropped++; + } +} +static int tsnep_rx_poll (struct tsnep_rx *rx,struct napi_struct *napi,int budget) {
@@ -1085,7 +1107,6 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,struct netdev_queue *tx_nq;struct bpf_prog *prog;struct xdp_buff xdp; -struct sk_buff *skb;struct tsnep_tx *tx;int desc_available;int xdp_status =0 ;
@@ -1170,21 +1191,7 @@ static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi, } } - skb =tsnep_build_skb (rx, entry->page, length); -if (skb) { -page_pool_release_page (rx->page_pool, entry->page); - - rx->packets++; - rx->bytes += length; -if (skb->pkt_type ==PACKET_MULTICAST ) - rx->multicast++; - -napi_gro_receive (napi, skb); - }else { -page_pool_recycle_direct (rx->page_pool, entry->page); - - rx->dropped++; - } +tsnep_rx_page (rx, napi, entry->page, length); entry->page =NULL ; }
Signature · 1 line
2.30.2
PATCH v2 5/6 tsnep: Add XDP socket zero-copy RX support
In reply to Gerhard EnglederAdd support for XSK zero-copy to RX path. The setup of the XSK pool can be done at runtime. If the netdev is running, then the queue must be disabled and enabled during reconfiguration. This can be done easily with functions introduced in previous commits.
A more important property is that, if the netdev is running, then the setup of the XSK pool shall not stop the netdev in case of errors. A broken netdev after a failed XSK pool setup is bad behavior. Therefore, the allocation and setup of resources during XSK pool setup is done only before any queue is disabled. Additionally, freeing and later allocation of resources is eliminated in some cases. Page pool entries are kept for later use. Two memory models are registered in parallel. As a result, the XSK pool setup cannot fail during queue reconfiguration.
In contrast to other drivers, XSK pool setup and XDP BPF program setup are separate actions. XSK pool setup can be done without any XDP BPF program. The XDP BPF program can be added, removed or changed without any reconfiguration of the XSK pool.
Test results with A53 1.2GHz:
xdpsock rxdrop copy mode:
pps pkts 1.00
rx 856,054 10,625,775
Two CPUs with both 100% utilization.
xdpsock rxdrop zero-copy mode:
pps pkts 1.00
rx 889,388 4,615,284
Two CPUs with 100% and 20% utilization.
xdpsock l2fwd copy mode:
pps pkts 1.00 rx 248,985 7,315,885 tx 248,921 7,315,885
Two CPUs with 100% and 10% utilization.
xdpsock l2fwd zero-copy mode:
pps pkts 1.00 rx 254,735 3,039,456 tx 254,735 3,039,456
Two CPUs with 100% and 4% utilization.
Packet rate increases and CPU utilization is reduced in both cases. 100% CPU load seems to the base load. This load is consumed by ksoftirqd just for dropping the generated packets without xdpsock running.
Using batch API reduced CPU utilization slightly, but measurements are not stable enough to provide meaningful numbers.
--- drivers/net/ethernet/engleder/tsnep.h | 13 +- drivers/net/ethernet/engleder/tsnep_main.c | 491 ++++++++++++++++++++- drivers/net/ethernet/engleder/tsnep_xdp.c | 66 +++ 3 files changed, 555 insertions(+), 15 deletions(-)
drivers/net/ethernet/engleder/tsnep.h +12 -1
@@ -101,7 +101,10 @@ struct tsnep_rx_entry {u32 properties; -struct page *page; +union { +struct page *page; +struct xdp_buff *xdp; + };size_t len;dma_addr_t dma; };
@@ -121,6 +124,9 @@ struct tsnep_rx {u32 owner_counter;int increment_owner_counter;struct page_pool *page_pool; +struct page **page_buffer; +struct xsk_buff_pool *xsk_pool; +struct xdp_buff **xdp_batch;u32 packets;u32 bytes;
@@ -129,6 +135,7 @@ struct tsnep_rx {u32 alloc_failed;struct xdp_rxq_info xdp_rxq; +struct xdp_rxq_info xdp_rxq_zc; };struct tsnep_queue {
@@ -214,6 +221,8 @@ int tsnep_rxnfc_del_rule(struct tsnep_adapter *adapter,int tsnep_xdp_setup_prog (struct tsnep_adapter *adapter,struct bpf_prog *prog,struct netlink_ext_ack *extack); +int tsnep_xdp_setup_pool (struct tsnep_adapter *adapter, +struct xsk_buff_pool *pool,u16 queue_id);#if IS_ENABLED (CONFIG_TSNEP_SELFTESTS )int tsnep_ethtool_get_test_count (void );
@@ -242,5 +251,7 @@ static inline void tsnep_ethtool_self_test(struct net_device *dev,void tsnep_get_system_time (struct tsnep_adapter *adapter,u64 *time);int tsnep_set_irq_coalesce (struct tsnep_queue *queue,u32 usecs);u32 tsnep_get_irq_coalesce (struct tsnep_queue *queue); +int tsnep_enable_xsk (struct tsnep_queue *queue,struct xsk_buff_pool *pool); +void tsnep_disable_xsk (struct tsnep_queue *queue);#endif /* _TSNEP_H */
drivers/net/ethernet/engleder/tsnep_main.c +477 -14
624 lines not on this page · raw message
drivers/net/ethernet/engleder/tsnep_xdp.c +66 -0
69 lines not on this page · raw message
Signature · 1 line
2.30.2
Paolo Abeni
maintainerRe: [PATCH net-next v2 5/6] tsnep: Add XDP socket zero-copy RX support
Quoted 15 lines · Gerhard Engleder
drivers/net/ethernet/engleder/tsnep_main.c +11
-0@@ -892,6 +900,37 @@ static int tsnep_rx_desc_available(struct tsnep_rx *rx)return rx->read - rx->write -1 ; } +static void tsnep_rx_free_page_buffer (struct tsnep_rx *rx) +{ +struct page **page; + + page = rx->page_buffer; +while (*page) { +page_pool_put_full_page (rx->page_pool, *page, false); + *page =NULL ; + page++; + } +}
[...]
Review
Quoted 4 lines
static void
tsnep_rx_close(struct tsnep_rx *rx)
{(quoted diff) +2
-0+ if (rx->xsk_pool) + tsnep_rx_free_page_buffer(rx);
It looks like the above could call tsnep_rx_free_page_buffer() with each page ptr in rx->page_buffer not zero. If so tsnep_rx_free_page_buffer() will do an out of bound access.
Also, why testing rx->xsk_pool instead of rx->page_buffer?
Thanks!
Paolo
Re: [PATCH net-next v2 5/6] tsnep: Add XDP socket zero-copy RX support
Quoted 19 lines · Paolo Abeni
Quoted 15 lines · Gerhard Engleder
drivers/net/ethernet/engleder/tsnep_main.c +11
-0@@ -892,6 +900,37 @@ static int tsnep_rx_desc_available(struct tsnep_rx *rx)return rx->read - rx->write -1 ; } +static void tsnep_rx_free_page_buffer (struct tsnep_rx *rx) +{ +struct page **page; + + page = rx->page_buffer; +while (*page) { +page_pool_put_full_page (rx->page_pool, *page, false); + *page =NULL ; + page++; + } +}[...]
Quoted 8 lines
Review
Quoted 4 lines
static void tsnep_rx_close(struct tsnep_rx *rx) {(quoted diff) +2
-0+ if (rx->xsk_pool) + tsnep_rx_free_page_buffer(rx);It looks like the above could call
tsnep_rx_free_page_buffer()with each page ptr inrx->page_buffernot zero. If sotsnep_rx_free_page_buffer()will do an out of bound access.
rx->page_buffer has space for up to TSNEP_RING_SIZE ptr's. The descriptor ring is filled with at most TSNEP_RING_SIZE - 1 pages. Thus, the last ptr in rx->page_buffer is always zero.
Quoted 1 line
Also, why testing
rx->xsk_poolinstead ofrx->page_buffer?
Testing for rx->xsk_pool is done for all code, which is only needed if XSK zero-copy is enabled. For me this is more consistent to the rest of the code.
Thanks!
Gerhard
PATCH v2 6/6 tsnep: Add XDP socket zero-copy TX support
In reply to Gerhard EnglederSend and complete XSK pool frames within TX NAPI context. NAPI context is triggered by ndo_xsk_wakeup.
Test results with A53 1.2GHz:
xdpsock txonly copy mode:
pps pkts 1.00
tx 284,409 11,398,144
Two CPUs with 100% and 10% utilization.
xdpsock txonly zero-copy mode:
pps pkts 1.00
tx 511,929 5,890,368
Two CPUs with 100% and 1% utilization.
Packet rate increases and CPU utilization is reduced.
--- drivers/net/ethernet/engleder/tsnep.h | 2 + drivers/net/ethernet/engleder/tsnep_main.c | 131 +++++++++++++++++++-- 2 files changed, 123 insertions(+), 10 deletions(-)
drivers/net/ethernet/engleder/tsnep.h +2 -0
@@ -70,6 +70,7 @@ struct tsnep_tx_entry {union {struct sk_buff *skb;struct xdp_frame *xdpf; +bool zc; };size_t len;DEFINE_DMA_UNMAP_ADDR (dma);
@@ -88,6 +89,7 @@ struct tsnep_tx {int read;u32 owner_counter;int increment_owner_counter; +struct xsk_buff_pool *xsk_pool;u32 packets;u32 bytes;
drivers/net/ethernet/engleder/tsnep_main.c +121 -10
208 lines not on this page · raw message
Signature · 1 line
2.30.2
Paolo Abeni
maintainerRe: [PATCH net-next v2 6/6] tsnep: Add XDP socket zero-copy TX support
Quoted 58 lines · Gerhard Engleder
Send and complete XSK pool frames within TX NAPI context. NAPI context is triggered by
ndo_xsk_wakeup.Test results with A53 1.2GHz:
xdpsock txonly copy mode:
pps pkts 1.00
tx 284,409 11,398,144
Two CPUs with 100% and 10% utilization.xdpsock txonly zero-copy mode:
pps pkts 1.00
tx 511,929 5,890,368
Two CPUs with 100% and 1% utilization.Packet rate increases and CPU utilization is reduced.
Signed-off-by Gerhard Engleder <[email protected]>--- drivers/net/ethernet/engleder/tsnep.h | 2 + drivers/net/ethernet/engleder/tsnep_main.c | 131 +++++++++++++++++++-- 2 files changed, 123 insertions(+), 10 deletions(-)drivers/net/ethernet/engleder/tsnep.h +2
-0@@ -70,6 +70,7 @@ struct tsnep_tx_entry {union {struct sk_buff *skb;struct xdp_frame *xdpf; +bool zc; };size_t len;DEFINE_DMA_UNMAP_ADDR (dma);@@ -88,6 +89,7 @@ struct tsnep_tx {int read;u32 owner_counter;int increment_owner_counter; +struct xsk_buff_pool *xsk_pool;u32 packets;u32 bytes;drivers/net/ethernet/engleder/tsnep_main.c +2
-0@@ -54,6 +54,8 @@#define TSNEP_TX_TYPE_SKB_FRAG BIT(1)#define TSNEP_TX_TYPE_XDP_TX BIT(2)#define TSNEP_TX_TYPE_XDP_NDO BIT(3) +#define TSNEP_TX_TYPE_XDP (TSNEP_TX_TYPE_XDP_TX | TSNEP_TX_TYPE_XDP_NDO) +#define TSNEP_TX_TYPE_XSK BIT(4)#define TSNEP_XDP_TX BIT(0)#define TSNEP_XDP_REDIRECT BIT(1)@@ -322,13 +324,51 @@ static void tsnep_tx_init(struct tsnep_tx *tx) tx->increment_owner_counter =TSNEP_RING_SIZE -1 ;
drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 334
Quoted 10 lines
drivers/net/ethernet/engleder/tsnep_main.c +8
-0} +static void tsnep_tx_enable (struct tsnep_tx *tx) +{ +struct netdev_queue *nq; + + nq =netdev_get_tx_queue (tx->adapter->netdev, tx->queue_index); + +local_bh_disable (); +__netif_tx_lock (nq,smp_processor_id ());
The above 2 statements could be replaced with:
__netif_tx_lock_bh()
drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 337
Quoted 3 lines
drivers/net/ethernet/engleder/tsnep_main.c +3
-0+netif_tx_wake_queue (nq); +__netif_tx_unlock (nq); +local_bh_enable ();
__netif_tx_unlock_bh()
drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 348
Quoted 11 lines
drivers/net/ethernet/engleder/tsnep_main.c +11
-0+} + +static void tsnep_tx_disable (struct tsnep_tx *tx,struct napi_struct *napi) +{ +struct netdev_queue *nq; +u32 val; + + nq =netdev_get_tx_queue (tx->adapter->netdev, tx->queue_index); + +local_bh_disable (); +__netif_tx_lock (nq,smp_processor_id ());
Same here.
Thanks!
Paolo
Re: [PATCH net-next v2 6/6] tsnep: Add XDP socket zero-copy TX support
Quoted 80 lines · Paolo Abeni
Quoted 58 lines · Gerhard Engleder
Send and complete XSK pool frames within TX NAPI context. NAPI context is triggered by
ndo_xsk_wakeup.Test results with A53 1.2GHz:
xdpsock txonly copy mode:
pps pkts 1.00
tx 284,409 11,398,144
Two CPUs with 100% and 10% utilization.xdpsock txonly zero-copy mode:
pps pkts 1.00
tx 511,929 5,890,368
Two CPUs with 100% and 1% utilization.Packet rate increases and CPU utilization is reduced.
Signed-off-by Gerhard Engleder <[email protected]>--- drivers/net/ethernet/engleder/tsnep.h | 2 + drivers/net/ethernet/engleder/tsnep_main.c | 131 +++++++++++++++++++-- 2 files changed, 123 insertions(+), 10 deletions(-)drivers/net/ethernet/engleder/tsnep.h +2
-0@@ -70,6 +70,7 @@ struct tsnep_tx_entry {union {struct sk_buff *skb;struct xdp_frame *xdpf; +bool zc; };size_t len;DEFINE_DMA_UNMAP_ADDR (dma);@@ -88,6 +89,7 @@ struct tsnep_tx {int read;u32 owner_counter;int increment_owner_counter; +struct xsk_buff_pool *xsk_pool;u32 packets;u32 bytes;drivers/net/ethernet/engleder/tsnep_main.c +2
-0@@ -54,6 +54,8 @@#define TSNEP_TX_TYPE_SKB_FRAG BIT(1)#define TSNEP_TX_TYPE_XDP_TX BIT(2)#define TSNEP_TX_TYPE_XDP_NDO BIT(3) +#define TSNEP_TX_TYPE_XDP (TSNEP_TX_TYPE_XDP_TX | TSNEP_TX_TYPE_XDP_NDO) +#define TSNEP_TX_TYPE_XSK BIT(4)#define TSNEP_XDP_TX BIT(0)#define TSNEP_XDP_REDIRECT BIT(1)@@ -322,13 +324,51 @@ static void tsnep_tx_init(struct tsnep_tx *tx) tx->increment_owner_counter =TSNEP_RING_SIZE -1 ;drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 334
Quoted 10 lines
drivers/net/ethernet/engleder/tsnep_main.c +8
-0} +static void tsnep_tx_enable (struct tsnep_tx *tx) +{ +struct netdev_queue *nq; + + nq =netdev_get_tx_queue (tx->adapter->netdev, tx->queue_index); + +local_bh_disable (); +__netif_tx_lock (nq,smp_processor_id ());The above 2 statements could be replaced with:
__netif_tx_lock_bh()drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 337
Quoted 3 lines
drivers/net/ethernet/engleder/tsnep_main.c +3
-0+netif_tx_wake_queue (nq); +__netif_tx_unlock (nq); +local_bh_enable ();
__netif_tx_unlock_bh()
Quoted 13 lines
drivers/net/ethernet/engleder/tsnep_main.c static void tsnep_tx_init(struct tsnep_tx *tx) · line 348
Quoted 11 lines
drivers/net/ethernet/engleder/tsnep_main.c +11
-0+} + +static void tsnep_tx_disable (struct tsnep_tx *tx,struct napi_struct *napi) +{ +struct netdev_queue *nq; +u32 val; + + nq =netdev_get_tx_queue (tx->adapter->netdev, tx->queue_index); + +local_bh_disable (); +__netif_tx_lock (nq,smp_processor_id ());Same here.
Will be done.
Thank you!
Gerhard