Skip to content

Commit fb32856

Browse files
fengidridavem330
authored andcommitted
virtio-net: page_to_skb() use build_skb when there's sufficient tailroom
In page_to_skb(), if we have enough tailroom to save skb_shared_info, we can use build_skb to create skb directly. No need to alloc for additional space. And it can save a 'frags slot', which is very friendly to GRO. Here, if the payload of the received package is too small (less than GOOD_COPY_LEN), we still choose to copy it directly to the space got by napi_alloc_skb. So we can reuse these pages. Testing Machine: The four queues of the network card are bound to the cpu1. Test command: for ((i=0;i<5;++i)); do sockperf tp --ip 192.168.122.64 -m 1000 -t 150& done The size of the udp package is 1000, so in the case of this patch, there will always be enough tailroom to use build_skb. The sent udp packet will be discarded because there is no port to receive it. The irqsoftd of the machine is 100%, we observe the received quantity displayed by sar -n DEV 1: no build_skb: 956864.00 rxpck/s build_skb: 1158465.00 rxpck/s Signed-off-by: Xuan Zhuo <[email protected]> Suggested-by: Jason Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent fa588eb commit fb32856

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

drivers/net/virtio_net.c

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -379,36 +379,56 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
379379
struct receive_queue *rq,
380380
struct page *page, unsigned int offset,
381381
unsigned int len, unsigned int truesize,
382-
bool hdr_valid, unsigned int metasize)
382+
bool hdr_valid, unsigned int metasize,
383+
unsigned int headroom)
383384
{
384385
struct sk_buff *skb;
385386
struct virtio_net_hdr_mrg_rxbuf *hdr;
386387
unsigned int copy, hdr_len, hdr_padded_len;
387-
char *p;
388+
int tailroom, shinfo_size;
389+
char *p, *hdr_p;
388390

389391
p = page_address(page) + offset;
390-
391-
/* copy small packet so we can reuse these pages for small data */
392-
skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
393-
if (unlikely(!skb))
394-
return NULL;
395-
396-
hdr = skb_vnet_hdr(skb);
392+
hdr_p = p;
397393

398394
hdr_len = vi->hdr_len;
399395
if (vi->mergeable_rx_bufs)
400396
hdr_padded_len = sizeof(*hdr);
401397
else
402398
hdr_padded_len = sizeof(struct padded_vnet_hdr);
403399

404-
/* hdr_valid means no XDP, so we can copy the vnet header */
405-
if (hdr_valid)
406-
memcpy(hdr, p, hdr_len);
400+
/* If headroom is not 0, there is an offset between the beginning of the
401+
* data and the allocated space, otherwise the data and the allocated
402+
* space are aligned.
403+
*/
404+
if (headroom) {
405+
/* The actual allocated space size is PAGE_SIZE. */
406+
truesize = PAGE_SIZE;
407+
tailroom = truesize - len - offset;
408+
} else {
409+
tailroom = truesize - len;
410+
}
407411

408412
len -= hdr_len;
409413
offset += hdr_padded_len;
410414
p += hdr_padded_len;
411415

416+
shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
417+
418+
if (len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
419+
skb = build_skb(p, truesize);
420+
if (unlikely(!skb))
421+
return NULL;
422+
423+
skb_put(skb, len);
424+
goto ok;
425+
}
426+
427+
/* copy small packet so we can reuse these pages for small data */
428+
skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
429+
if (unlikely(!skb))
430+
return NULL;
431+
412432
/* Copy all frame if it fits skb->head, otherwise
413433
* we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
414434
*/
@@ -418,11 +438,6 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
418438
copy = ETH_HLEN + metasize;
419439
skb_put_data(skb, p, copy);
420440

421-
if (metasize) {
422-
__skb_pull(skb, metasize);
423-
skb_metadata_set(skb, metasize);
424-
}
425-
426441
len -= copy;
427442
offset += copy;
428443

@@ -431,7 +446,7 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
431446
skb_add_rx_frag(skb, 0, page, offset, len, truesize);
432447
else
433448
put_page(page);
434-
return skb;
449+
goto ok;
435450
}
436451

437452
/*
@@ -458,6 +473,18 @@ static struct sk_buff *page_to_skb(struct virtnet_info *vi,
458473
if (page)
459474
give_pages(rq, page);
460475

476+
ok:
477+
/* hdr_valid means no XDP, so we can copy the vnet header */
478+
if (hdr_valid) {
479+
hdr = skb_vnet_hdr(skb);
480+
memcpy(hdr, hdr_p, hdr_len);
481+
}
482+
483+
if (metasize) {
484+
__skb_pull(skb, metasize);
485+
skb_metadata_set(skb, metasize);
486+
}
487+
461488
return skb;
462489
}
463490

@@ -808,7 +835,7 @@ static struct sk_buff *receive_big(struct net_device *dev,
808835
{
809836
struct page *page = buf;
810837
struct sk_buff *skb =
811-
page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0);
838+
page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
812839

813840
stats->bytes += len - vi->hdr_len;
814841
if (unlikely(!skb))
@@ -922,7 +949,7 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
922949
put_page(page);
923950
head_skb = page_to_skb(vi, rq, xdp_page, offset,
924951
len, PAGE_SIZE, false,
925-
metasize);
952+
metasize, headroom);
926953
return head_skb;
927954
}
928955
break;
@@ -980,7 +1007,7 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
9801007
}
9811008

9821009
head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
983-
metasize);
1010+
metasize, headroom);
9841011
curr_skb = head_skb;
9851012

9861013
if (unlikely(!curr_skb))

0 commit comments

Comments
 (0)