@@ -47,7 +47,8 @@ module_param(napi_tx, bool, 0644);
47
47
#define VIRTIO_XDP_TX BIT(0)
48
48
#define VIRTIO_XDP_REDIR BIT(1)
49
49
50
- #define VIRTIO_XDP_FLAG BIT(0)
50
+ #define VIRTIO_XDP_FLAG BIT(0)
51
+ #define VIRTIO_ORPHAN_FLAG BIT(1)
51
52
52
53
/* RX packet size EWMA. The average packet size is used to determine the packet
53
54
* buffer size when refilling RX rings. As the entire RX ring may be refilled
@@ -85,6 +86,8 @@ struct virtnet_stat_desc {
85
86
struct virtnet_sq_free_stats {
86
87
u64 packets ;
87
88
u64 bytes ;
89
+ u64 napi_packets ;
90
+ u64 napi_bytes ;
88
91
};
89
92
90
93
struct virtnet_sq_stats {
@@ -506,29 +509,50 @@ static struct xdp_frame *ptr_to_xdp(void *ptr)
506
509
return (struct xdp_frame * )((unsigned long )ptr & ~VIRTIO_XDP_FLAG );
507
510
}
508
511
509
- static void __free_old_xmit (struct send_queue * sq , bool in_napi ,
510
- struct virtnet_sq_free_stats * stats )
512
+ static bool is_orphan_skb (void * ptr )
513
+ {
514
+ return (unsigned long )ptr & VIRTIO_ORPHAN_FLAG ;
515
+ }
516
+
517
+ static void * skb_to_ptr (struct sk_buff * skb , bool orphan )
518
+ {
519
+ return (void * )((unsigned long )skb | (orphan ? VIRTIO_ORPHAN_FLAG : 0 ));
520
+ }
521
+
522
+ static struct sk_buff * ptr_to_skb (void * ptr )
523
+ {
524
+ return (struct sk_buff * )((unsigned long )ptr & ~VIRTIO_ORPHAN_FLAG );
525
+ }
526
+
527
+ static void __free_old_xmit (struct send_queue * sq , struct netdev_queue * txq ,
528
+ bool in_napi , struct virtnet_sq_free_stats * stats )
511
529
{
512
530
unsigned int len ;
513
531
void * ptr ;
514
532
515
533
while ((ptr = virtqueue_get_buf (sq -> vq , & len )) != NULL ) {
516
- ++ stats -> packets ;
517
-
518
534
if (!is_xdp_frame (ptr )) {
519
- struct sk_buff * skb = ptr ;
535
+ struct sk_buff * skb = ptr_to_skb ( ptr ) ;
520
536
521
537
pr_debug ("Sent skb %p\n" , skb );
522
538
523
- stats -> bytes += skb -> len ;
539
+ if (is_orphan_skb (ptr )) {
540
+ stats -> packets ++ ;
541
+ stats -> bytes += skb -> len ;
542
+ } else {
543
+ stats -> napi_packets ++ ;
544
+ stats -> napi_bytes += skb -> len ;
545
+ }
524
546
napi_consume_skb (skb , in_napi );
525
547
} else {
526
548
struct xdp_frame * frame = ptr_to_xdp (ptr );
527
549
550
+ stats -> packets ++ ;
528
551
stats -> bytes += xdp_get_frame_len (frame );
529
552
xdp_return_frame (frame );
530
553
}
531
554
}
555
+ netdev_tx_completed_queue (txq , stats -> napi_packets , stats -> napi_bytes );
532
556
}
533
557
534
558
/* Converting between virtqueue no. and kernel tx/rx queue no.
@@ -955,21 +979,22 @@ static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
955
979
virtnet_rq_free_buf (vi , rq , buf );
956
980
}
957
981
958
- static void free_old_xmit (struct send_queue * sq , bool in_napi )
982
+ static void free_old_xmit (struct send_queue * sq , struct netdev_queue * txq ,
983
+ bool in_napi )
959
984
{
960
985
struct virtnet_sq_free_stats stats = {0 };
961
986
962
- __free_old_xmit (sq , in_napi , & stats );
987
+ __free_old_xmit (sq , txq , in_napi , & stats );
963
988
964
989
/* Avoid overhead when no packets have been processed
965
990
* happens when called speculatively from start_xmit.
966
991
*/
967
- if (!stats .packets )
992
+ if (!stats .packets && ! stats . napi_packets )
968
993
return ;
969
994
970
995
u64_stats_update_begin (& sq -> stats .syncp );
971
- u64_stats_add (& sq -> stats .bytes , stats .bytes );
972
- u64_stats_add (& sq -> stats .packets , stats .packets );
996
+ u64_stats_add (& sq -> stats .bytes , stats .bytes + stats . napi_bytes );
997
+ u64_stats_add (& sq -> stats .packets , stats .packets + stats . napi_packets );
973
998
u64_stats_update_end (& sq -> stats .syncp );
974
999
}
975
1000
@@ -1003,7 +1028,9 @@ static void check_sq_full_and_disable(struct virtnet_info *vi,
1003
1028
* early means 16 slots are typically wasted.
1004
1029
*/
1005
1030
if (sq -> vq -> num_free < 2 + MAX_SKB_FRAGS ) {
1006
- netif_stop_subqueue (dev , qnum );
1031
+ struct netdev_queue * txq = netdev_get_tx_queue (dev , qnum );
1032
+
1033
+ netif_tx_stop_queue (txq );
1007
1034
u64_stats_update_begin (& sq -> stats .syncp );
1008
1035
u64_stats_inc (& sq -> stats .stop );
1009
1036
u64_stats_update_end (& sq -> stats .syncp );
@@ -1012,7 +1039,7 @@ static void check_sq_full_and_disable(struct virtnet_info *vi,
1012
1039
virtqueue_napi_schedule (& sq -> napi , sq -> vq );
1013
1040
} else if (unlikely (!virtqueue_enable_cb_delayed (sq -> vq ))) {
1014
1041
/* More just got used, free them then recheck. */
1015
- free_old_xmit (sq , false);
1042
+ free_old_xmit (sq , txq , false);
1016
1043
if (sq -> vq -> num_free >= 2 + MAX_SKB_FRAGS ) {
1017
1044
netif_start_subqueue (dev , qnum );
1018
1045
u64_stats_update_begin (& sq -> stats .syncp );
@@ -1138,7 +1165,8 @@ static int virtnet_xdp_xmit(struct net_device *dev,
1138
1165
}
1139
1166
1140
1167
/* Free up any pending old buffers before queueing new ones. */
1141
- __free_old_xmit (sq , false, & stats );
1168
+ __free_old_xmit (sq , netdev_get_tx_queue (dev , sq - vi -> sq ),
1169
+ false, & stats );
1142
1170
1143
1171
for (i = 0 ; i < n ; i ++ ) {
1144
1172
struct xdp_frame * xdpf = frames [i ];
@@ -2313,7 +2341,7 @@ static void virtnet_poll_cleantx(struct receive_queue *rq)
2313
2341
2314
2342
do {
2315
2343
virtqueue_disable_cb (sq -> vq );
2316
- free_old_xmit (sq , true);
2344
+ free_old_xmit (sq , txq , true);
2317
2345
} while (unlikely (!virtqueue_enable_cb_delayed (sq -> vq )));
2318
2346
2319
2347
if (sq -> vq -> num_free >= 2 + MAX_SKB_FRAGS ) {
@@ -2412,6 +2440,7 @@ static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2412
2440
goto err_xdp_reg_mem_model ;
2413
2441
2414
2442
virtnet_napi_enable (vi -> rq [qp_index ].vq , & vi -> rq [qp_index ].napi );
2443
+ netdev_tx_reset_queue (netdev_get_tx_queue (vi -> dev , qp_index ));
2415
2444
virtnet_napi_tx_enable (vi , vi -> sq [qp_index ].vq , & vi -> sq [qp_index ].napi );
2416
2445
2417
2446
return 0 ;
@@ -2471,7 +2500,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2471
2500
txq = netdev_get_tx_queue (vi -> dev , index );
2472
2501
__netif_tx_lock (txq , raw_smp_processor_id ());
2473
2502
virtqueue_disable_cb (sq -> vq );
2474
- free_old_xmit (sq , true);
2503
+ free_old_xmit (sq , txq , true);
2475
2504
2476
2505
if (sq -> vq -> num_free >= 2 + MAX_SKB_FRAGS ) {
2477
2506
if (netif_tx_queue_stopped (txq )) {
@@ -2505,7 +2534,7 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2505
2534
return 0 ;
2506
2535
}
2507
2536
2508
- static int xmit_skb (struct send_queue * sq , struct sk_buff * skb )
2537
+ static int xmit_skb (struct send_queue * sq , struct sk_buff * skb , bool orphan )
2509
2538
{
2510
2539
struct virtio_net_hdr_mrg_rxbuf * hdr ;
2511
2540
const unsigned char * dest = ((struct ethhdr * )skb -> data )-> h_dest ;
@@ -2549,7 +2578,8 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2549
2578
return num_sg ;
2550
2579
num_sg ++ ;
2551
2580
}
2552
- return virtqueue_add_outbuf (sq -> vq , sq -> sg , num_sg , skb , GFP_ATOMIC );
2581
+ return virtqueue_add_outbuf (sq -> vq , sq -> sg , num_sg ,
2582
+ skb_to_ptr (skb , orphan ), GFP_ATOMIC );
2553
2583
}
2554
2584
2555
2585
static netdev_tx_t start_xmit (struct sk_buff * skb , struct net_device * dev )
@@ -2559,24 +2589,25 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2559
2589
struct send_queue * sq = & vi -> sq [qnum ];
2560
2590
int err ;
2561
2591
struct netdev_queue * txq = netdev_get_tx_queue (dev , qnum );
2562
- bool kick = ! netdev_xmit_more ();
2592
+ bool xmit_more = netdev_xmit_more ();
2563
2593
bool use_napi = sq -> napi .weight ;
2594
+ bool kick ;
2564
2595
2565
2596
/* Free up any pending old buffers before queueing new ones. */
2566
2597
do {
2567
2598
if (use_napi )
2568
2599
virtqueue_disable_cb (sq -> vq );
2569
2600
2570
- free_old_xmit (sq , false);
2601
+ free_old_xmit (sq , txq , false);
2571
2602
2572
- } while (use_napi && kick &&
2603
+ } while (use_napi && ! xmit_more &&
2573
2604
unlikely (!virtqueue_enable_cb_delayed (sq -> vq )));
2574
2605
2575
2606
/* timestamp packet in software */
2576
2607
skb_tx_timestamp (skb );
2577
2608
2578
2609
/* Try to transmit */
2579
- err = xmit_skb (sq , skb );
2610
+ err = xmit_skb (sq , skb , ! use_napi );
2580
2611
2581
2612
/* This should not happen! */
2582
2613
if (unlikely (err )) {
@@ -2598,7 +2629,9 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2598
2629
2599
2630
check_sq_full_and_disable (vi , dev , sq );
2600
2631
2601
- if (kick || netif_xmit_stopped (txq )) {
2632
+ kick = use_napi ? __netdev_tx_sent_queue (txq , skb -> len , xmit_more ) :
2633
+ !xmit_more || netif_xmit_stopped (txq );
2634
+ if (kick ) {
2602
2635
if (virtqueue_kick_prepare (sq -> vq ) && virtqueue_notify (sq -> vq )) {
2603
2636
u64_stats_update_begin (& sq -> stats .syncp );
2604
2637
u64_stats_inc (& sq -> stats .kicks );
0 commit comments