Skip to content

Commit b1dc24a

Browse files
fengidrimstsirkin
authored andcommitted
virtio_net: unify the code for recycling the xmit ptr
There are two completely similar and independent implementations. This is inconvenient for the subsequent addition of new types. So extract a function from this piece of code and call this function uniformly to recover old xmit ptr. Signed-off-by: Xuan Zhuo <xuanzhuo@linux.alibaba.com> Acked-by: Jason Wang <jasowang@redhat.com> Message-Id: <20240229072044.77388-18-xuanzhuo@linux.alibaba.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 0d197a1 commit b1dc24a

1 file changed

Lines changed: 39 additions & 43 deletions

File tree

drivers/net/virtio_net.c

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ struct virtnet_stat_desc {
8080
size_t offset;
8181
};
8282

83+
struct virtnet_sq_free_stats {
84+
u64 packets;
85+
u64 bytes;
86+
};
87+
8388
struct virtnet_sq_stats {
8489
struct u64_stats_sync syncp;
8590
u64_stats_t packets;
@@ -372,6 +377,31 @@ static struct xdp_frame *ptr_to_xdp(void *ptr)
372377
return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
373378
}
374379

380+
static void __free_old_xmit(struct send_queue *sq, bool in_napi,
381+
struct virtnet_sq_free_stats *stats)
382+
{
383+
unsigned int len;
384+
void *ptr;
385+
386+
while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
387+
++stats->packets;
388+
389+
if (!is_xdp_frame(ptr)) {
390+
struct sk_buff *skb = ptr;
391+
392+
pr_debug("Sent skb %p\n", skb);
393+
394+
stats->bytes += skb->len;
395+
napi_consume_skb(skb, in_napi);
396+
} else {
397+
struct xdp_frame *frame = ptr_to_xdp(ptr);
398+
399+
stats->bytes += xdp_get_frame_len(frame);
400+
xdp_return_frame(frame);
401+
}
402+
}
403+
}
404+
375405
/* Converting between virtqueue no. and kernel tx/rx queue no.
376406
* 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
377407
*/
@@ -798,37 +828,19 @@ static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
798828

799829
static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
800830
{
801-
unsigned int len;
802-
unsigned int packets = 0;
803-
unsigned int bytes = 0;
804-
void *ptr;
831+
struct virtnet_sq_free_stats stats = {0};
805832

806-
while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
807-
if (likely(!is_xdp_frame(ptr))) {
808-
struct sk_buff *skb = ptr;
809-
810-
pr_debug("Sent skb %p\n", skb);
811-
812-
bytes += skb->len;
813-
napi_consume_skb(skb, in_napi);
814-
} else {
815-
struct xdp_frame *frame = ptr_to_xdp(ptr);
816-
817-
bytes += xdp_get_frame_len(frame);
818-
xdp_return_frame(frame);
819-
}
820-
packets++;
821-
}
833+
__free_old_xmit(sq, in_napi, &stats);
822834

823835
/* Avoid overhead when no packets have been processed
824836
* happens when called speculatively from start_xmit.
825837
*/
826-
if (!packets)
838+
if (!stats.packets)
827839
return;
828840

829841
u64_stats_update_begin(&sq->stats.syncp);
830-
u64_stats_add(&sq->stats.bytes, bytes);
831-
u64_stats_add(&sq->stats.packets, packets);
842+
u64_stats_add(&sq->stats.bytes, stats.bytes);
843+
u64_stats_add(&sq->stats.packets, stats.packets);
832844
u64_stats_update_end(&sq->stats.syncp);
833845
}
834846

@@ -967,15 +979,12 @@ static int virtnet_xdp_xmit(struct net_device *dev,
967979
int n, struct xdp_frame **frames, u32 flags)
968980
{
969981
struct virtnet_info *vi = netdev_priv(dev);
982+
struct virtnet_sq_free_stats stats = {0};
970983
struct receive_queue *rq = vi->rq;
971984
struct bpf_prog *xdp_prog;
972985
struct send_queue *sq;
973-
unsigned int len;
974-
int packets = 0;
975-
int bytes = 0;
976986
int nxmit = 0;
977987
int kicks = 0;
978-
void *ptr;
979988
int ret;
980989
int i;
981990

@@ -994,20 +1003,7 @@ static int virtnet_xdp_xmit(struct net_device *dev,
9941003
}
9951004

9961005
/* Free up any pending old buffers before queueing new ones. */
997-
while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
998-
if (likely(is_xdp_frame(ptr))) {
999-
struct xdp_frame *frame = ptr_to_xdp(ptr);
1000-
1001-
bytes += xdp_get_frame_len(frame);
1002-
xdp_return_frame(frame);
1003-
} else {
1004-
struct sk_buff *skb = ptr;
1005-
1006-
bytes += skb->len;
1007-
napi_consume_skb(skb, false);
1008-
}
1009-
packets++;
1010-
}
1006+
__free_old_xmit(sq, false, &stats);
10111007

10121008
for (i = 0; i < n; i++) {
10131009
struct xdp_frame *xdpf = frames[i];
@@ -1027,8 +1023,8 @@ static int virtnet_xdp_xmit(struct net_device *dev,
10271023
}
10281024
out:
10291025
u64_stats_update_begin(&sq->stats.syncp);
1030-
u64_stats_add(&sq->stats.bytes, bytes);
1031-
u64_stats_add(&sq->stats.packets, packets);
1026+
u64_stats_add(&sq->stats.bytes, stats.bytes);
1027+
u64_stats_add(&sq->stats.packets, stats.packets);
10321028
u64_stats_add(&sq->stats.xdp_tx, n);
10331029
u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
10341030
u64_stats_add(&sq->stats.kicks, kicks);

0 commit comments

Comments
 (0)