1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* A network driver using virtio.
3 *
4 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5 */
6 //#define DEBUG
7 #include <linux/netdevice.h>
8 #include <linux/etherdevice.h>
9 #include <linux/ethtool.h>
10 #include <linux/module.h>
11 #include <linux/virtio.h>
12 #include <linux/virtio_net.h>
13 #include <linux/bpf.h>
14 #include <linux/bpf_trace.h>
15 #include <linux/scatterlist.h>
16 #include <linux/if_vlan.h>
17 #include <linux/slab.h>
18 #include <linux/cpu.h>
19 #include <linux/average.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <linux/dim.h>
23 #include <net/route.h>
24 #include <net/xdp.h>
25 #include <net/net_failover.h>
26 #include <net/netdev_rx_queue.h>
27 #include <net/netdev_queues.h>
28 #include <net/xdp_sock_drv.h>
29 #include <net/page_pool/helpers.h>
30
31 static int napi_weight = NAPI_POLL_WEIGHT;
32 module_param(napi_weight, int, 0444);
33
34 static bool csum = true, gso = true, napi_tx = true;
35 module_param(csum, bool, 0444);
36 module_param(gso, bool, 0444);
37 module_param(napi_tx, bool, 0644);
38
39 #define VIRTIO_OFFLOAD_MAP_MIN 46
40 #define VIRTIO_OFFLOAD_MAP_MAX 47
41 #define VIRTIO_FEATURES_MAP_MIN 65
42 #define VIRTIO_O2F_DELTA (VIRTIO_FEATURES_MAP_MIN - \
43 VIRTIO_OFFLOAD_MAP_MIN)
44
virtio_is_mapped_offload(unsigned int obit)45 static bool virtio_is_mapped_offload(unsigned int obit)
46 {
47 return obit >= VIRTIO_OFFLOAD_MAP_MIN &&
48 obit <= VIRTIO_OFFLOAD_MAP_MAX;
49 }
50
virtio_offload_to_feature(unsigned int obit)51 static unsigned int virtio_offload_to_feature(unsigned int obit)
52 {
53 return virtio_is_mapped_offload(obit) ? obit + VIRTIO_O2F_DELTA : obit;
54 }
55
56 /* FIXME: MTU in config. */
57 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
58 #define GOOD_COPY_LEN 128
59
60 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
61
62 /* Separating two types of XDP xmit */
63 #define VIRTIO_XDP_TX BIT(0)
64 #define VIRTIO_XDP_REDIR BIT(1)
65
66 /* RX packet size EWMA. The average packet size is used to determine the packet
67 * buffer size when refilling RX rings. As the entire RX ring may be refilled
68 * at once, the weight is chosen so that the EWMA will be insensitive to short-
69 * term, transient changes in packet size.
70 */
71 DECLARE_EWMA(pkt_len, 0, 64)
72
73 #define VIRTNET_DRIVER_VERSION "1.0.0"
74
75 static const unsigned long guest_offloads[] = {
76 VIRTIO_NET_F_GUEST_TSO4,
77 VIRTIO_NET_F_GUEST_TSO6,
78 VIRTIO_NET_F_GUEST_ECN,
79 VIRTIO_NET_F_GUEST_UFO,
80 VIRTIO_NET_F_GUEST_CSUM,
81 VIRTIO_NET_F_GUEST_USO4,
82 VIRTIO_NET_F_GUEST_USO6,
83 VIRTIO_NET_F_GUEST_HDRLEN,
84 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_MAPPED,
85 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM_MAPPED,
86 };
87
88 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
89 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
90 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
91 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
92 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
93 (1ULL << VIRTIO_NET_F_GUEST_USO6) | \
94 (1ULL << VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_MAPPED) | \
95 (1ULL << VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM_MAPPED))
96
97 struct virtnet_stat_desc {
98 char desc[ETH_GSTRING_LEN];
99 size_t offset;
100 size_t qstat_offset;
101 };
102
103 struct virtnet_sq_free_stats {
104 u64 packets;
105 u64 bytes;
106 u64 napi_packets;
107 u64 napi_bytes;
108 u64 xsk;
109 };
110
111 struct virtnet_sq_stats {
112 struct u64_stats_sync syncp;
113 u64_stats_t packets;
114 u64_stats_t bytes;
115 u64_stats_t xdp_tx;
116 u64_stats_t xdp_tx_drops;
117 u64_stats_t kicks;
118 u64_stats_t tx_timeouts;
119 u64_stats_t stop;
120 u64_stats_t wake;
121 };
122
123 struct virtnet_rq_stats {
124 struct u64_stats_sync syncp;
125 u64_stats_t packets;
126 u64_stats_t bytes;
127 u64_stats_t drops;
128 u64_stats_t xdp_packets;
129 u64_stats_t xdp_tx;
130 u64_stats_t xdp_redirects;
131 u64_stats_t xdp_drops;
132 u64_stats_t kicks;
133 };
134
135 #define VIRTNET_SQ_STAT(name, m) {name, offsetof(struct virtnet_sq_stats, m), -1}
136 #define VIRTNET_RQ_STAT(name, m) {name, offsetof(struct virtnet_rq_stats, m), -1}
137
138 #define VIRTNET_SQ_STAT_QSTAT(name, m) \
139 { \
140 name, \
141 offsetof(struct virtnet_sq_stats, m), \
142 offsetof(struct netdev_queue_stats_tx, m), \
143 }
144
145 #define VIRTNET_RQ_STAT_QSTAT(name, m) \
146 { \
147 name, \
148 offsetof(struct virtnet_rq_stats, m), \
149 offsetof(struct netdev_queue_stats_rx, m), \
150 }
151
152 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
153 VIRTNET_SQ_STAT("xdp_tx", xdp_tx),
154 VIRTNET_SQ_STAT("xdp_tx_drops", xdp_tx_drops),
155 VIRTNET_SQ_STAT("kicks", kicks),
156 VIRTNET_SQ_STAT("tx_timeouts", tx_timeouts),
157 };
158
159 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
160 VIRTNET_RQ_STAT("drops", drops),
161 VIRTNET_RQ_STAT("xdp_packets", xdp_packets),
162 VIRTNET_RQ_STAT("xdp_tx", xdp_tx),
163 VIRTNET_RQ_STAT("xdp_redirects", xdp_redirects),
164 VIRTNET_RQ_STAT("xdp_drops", xdp_drops),
165 VIRTNET_RQ_STAT("kicks", kicks),
166 };
167
168 static const struct virtnet_stat_desc virtnet_sq_stats_desc_qstat[] = {
169 VIRTNET_SQ_STAT_QSTAT("packets", packets),
170 VIRTNET_SQ_STAT_QSTAT("bytes", bytes),
171 VIRTNET_SQ_STAT_QSTAT("stop", stop),
172 VIRTNET_SQ_STAT_QSTAT("wake", wake),
173 };
174
175 static const struct virtnet_stat_desc virtnet_rq_stats_desc_qstat[] = {
176 VIRTNET_RQ_STAT_QSTAT("packets", packets),
177 VIRTNET_RQ_STAT_QSTAT("bytes", bytes),
178 };
179
180 #define VIRTNET_STATS_DESC_CQ(name) \
181 {#name, offsetof(struct virtio_net_stats_cvq, name), -1}
182
183 #define VIRTNET_STATS_DESC_RX(class, name) \
184 {#name, offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), -1}
185
186 #define VIRTNET_STATS_DESC_TX(class, name) \
187 {#name, offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), -1}
188
189
190 static const struct virtnet_stat_desc virtnet_stats_cvq_desc[] = {
191 VIRTNET_STATS_DESC_CQ(command_num),
192 VIRTNET_STATS_DESC_CQ(ok_num),
193 };
194
195 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc[] = {
196 VIRTNET_STATS_DESC_RX(basic, packets),
197 VIRTNET_STATS_DESC_RX(basic, bytes),
198
199 VIRTNET_STATS_DESC_RX(basic, notifications),
200 VIRTNET_STATS_DESC_RX(basic, interrupts),
201 };
202
203 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc[] = {
204 VIRTNET_STATS_DESC_TX(basic, packets),
205 VIRTNET_STATS_DESC_TX(basic, bytes),
206
207 VIRTNET_STATS_DESC_TX(basic, notifications),
208 VIRTNET_STATS_DESC_TX(basic, interrupts),
209 };
210
211 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc[] = {
212 VIRTNET_STATS_DESC_RX(csum, needs_csum),
213 };
214
215 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc[] = {
216 VIRTNET_STATS_DESC_TX(gso, gso_packets_noseg),
217 VIRTNET_STATS_DESC_TX(gso, gso_bytes_noseg),
218 };
219
220 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc[] = {
221 VIRTNET_STATS_DESC_RX(speed, ratelimit_bytes),
222 };
223
224 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc[] = {
225 VIRTNET_STATS_DESC_TX(speed, ratelimit_bytes),
226 };
227
228 #define VIRTNET_STATS_DESC_RX_QSTAT(class, name, qstat_field) \
229 { \
230 #name, \
231 offsetof(struct virtio_net_stats_rx_ ## class, rx_ ## name), \
232 offsetof(struct netdev_queue_stats_rx, qstat_field), \
233 }
234
235 #define VIRTNET_STATS_DESC_TX_QSTAT(class, name, qstat_field) \
236 { \
237 #name, \
238 offsetof(struct virtio_net_stats_tx_ ## class, tx_ ## name), \
239 offsetof(struct netdev_queue_stats_tx, qstat_field), \
240 }
241
242 static const struct virtnet_stat_desc virtnet_stats_rx_basic_desc_qstat[] = {
243 VIRTNET_STATS_DESC_RX_QSTAT(basic, drops, hw_drops),
244 VIRTNET_STATS_DESC_RX_QSTAT(basic, drop_overruns, hw_drop_overruns),
245 };
246
247 static const struct virtnet_stat_desc virtnet_stats_tx_basic_desc_qstat[] = {
248 VIRTNET_STATS_DESC_TX_QSTAT(basic, drops, hw_drops),
249 VIRTNET_STATS_DESC_TX_QSTAT(basic, drop_malformed, hw_drop_errors),
250 };
251
252 static const struct virtnet_stat_desc virtnet_stats_rx_csum_desc_qstat[] = {
253 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_valid, csum_unnecessary),
254 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_none, csum_none),
255 VIRTNET_STATS_DESC_RX_QSTAT(csum, csum_bad, csum_bad),
256 };
257
258 static const struct virtnet_stat_desc virtnet_stats_tx_csum_desc_qstat[] = {
259 VIRTNET_STATS_DESC_TX_QSTAT(csum, csum_none, csum_none),
260 VIRTNET_STATS_DESC_TX_QSTAT(csum, needs_csum, needs_csum),
261 };
262
263 static const struct virtnet_stat_desc virtnet_stats_rx_gso_desc_qstat[] = {
264 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets, hw_gro_packets),
265 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes, hw_gro_bytes),
266 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_packets_coalesced, hw_gro_wire_packets),
267 VIRTNET_STATS_DESC_RX_QSTAT(gso, gso_bytes_coalesced, hw_gro_wire_bytes),
268 };
269
270 static const struct virtnet_stat_desc virtnet_stats_tx_gso_desc_qstat[] = {
271 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_packets, hw_gso_packets),
272 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_bytes, hw_gso_bytes),
273 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments, hw_gso_wire_packets),
274 VIRTNET_STATS_DESC_TX_QSTAT(gso, gso_segments_bytes, hw_gso_wire_bytes),
275 };
276
277 static const struct virtnet_stat_desc virtnet_stats_rx_speed_desc_qstat[] = {
278 VIRTNET_STATS_DESC_RX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
279 };
280
281 static const struct virtnet_stat_desc virtnet_stats_tx_speed_desc_qstat[] = {
282 VIRTNET_STATS_DESC_TX_QSTAT(speed, ratelimit_packets, hw_drop_ratelimits),
283 };
284
285 #define VIRTNET_Q_TYPE_RX 0
286 #define VIRTNET_Q_TYPE_TX 1
287 #define VIRTNET_Q_TYPE_CQ 2
288
289 struct virtnet_interrupt_coalesce {
290 u32 max_packets;
291 u32 max_usecs;
292 };
293
294 /* Internal representation of a send virtqueue */
295 struct send_queue {
296 /* Virtqueue associated with this send _queue */
297 struct virtqueue *vq;
298
299 /* TX: fragments + linear part + virtio header */
300 struct scatterlist sg[MAX_SKB_FRAGS + 2];
301
302 /* Name of the send queue: output.$index */
303 char name[16];
304
305 struct virtnet_sq_stats stats;
306
307 struct virtnet_interrupt_coalesce intr_coal;
308
309 struct napi_struct napi;
310
311 /* Record whether sq is in reset state. */
312 bool reset;
313
314 struct xsk_buff_pool *xsk_pool;
315
316 dma_addr_t xsk_hdr_dma_addr;
317 };
318
319 /* Internal representation of a receive virtqueue */
320 struct receive_queue {
321 /* Virtqueue associated with this receive_queue */
322 struct virtqueue *vq;
323
324 struct napi_struct napi;
325
326 struct bpf_prog __rcu *xdp_prog;
327
328 struct virtnet_rq_stats stats;
329
330 /* The number of rx notifications */
331 u16 calls;
332
333 /* Is dynamic interrupt moderation enabled? */
334 bool dim_enabled;
335
336 /* Used to protect dim_enabled and inter_coal */
337 struct mutex dim_lock;
338
339 /* Dynamic Interrupt Moderation */
340 struct dim dim;
341
342 u32 packets_in_napi;
343
344 struct virtnet_interrupt_coalesce intr_coal;
345
346 /* Chain pages by the private ptr. */
347 struct page *pages;
348
349 /* Average packet length for mergeable receive buffers. */
350 struct ewma_pkt_len mrg_avg_pkt_len;
351
352 struct page_pool *page_pool;
353
354 /* True if page_pool handles DMA mapping via PP_FLAG_DMA_MAP */
355 bool use_page_pool_dma;
356
357 /* RX: fragments + linear part + virtio header */
358 struct scatterlist sg[MAX_SKB_FRAGS + 2];
359
360 /* Min single buffer size for mergeable buffers case. */
361 unsigned int min_buf_len;
362
363 /* Name of this receive queue: input.$index */
364 char name[16];
365
366 struct xdp_rxq_info xdp_rxq;
367
368 struct xsk_buff_pool *xsk_pool;
369
370 /* xdp rxq used by xsk */
371 struct xdp_rxq_info xsk_rxq_info;
372
373 struct xdp_buff **xsk_buffs;
374 };
375
376 /* Control VQ buffers: protected by the rtnl lock */
377 struct control_buf {
378 struct virtio_net_ctrl_hdr hdr;
379 virtio_net_ctrl_ack status;
380 };
381
382 struct virtnet_info {
383 struct virtio_device *vdev;
384 struct virtqueue *cvq;
385 struct net_device *dev;
386 struct send_queue *sq;
387 struct receive_queue *rq;
388 unsigned int status;
389
390 /* Max # of queue pairs supported by the device */
391 u16 max_queue_pairs;
392
393 /* # of queue pairs currently used by the driver */
394 u16 curr_queue_pairs;
395
396 /* # of XDP queue pairs currently used by the driver */
397 u16 xdp_queue_pairs;
398
399 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
400 bool xdp_enabled;
401
402 /* I like... big packets and I cannot lie! */
403 bool big_packets;
404
405 /* number of sg entries allocated for big packets */
406 unsigned int big_packets_num_skbfrags;
407
408 /* Host will merge rx buffers for big packets (shake it! shake it!) */
409 bool mergeable_rx_bufs;
410
411 /* Host supports rss and/or hash report */
412 bool has_rss;
413 bool has_rss_hash_report;
414 u8 rss_key_size;
415 u16 rss_indir_table_size;
416 u32 rss_hash_types_supported;
417 u32 rss_hash_types_saved;
418
419 /* Has control virtqueue */
420 bool has_cvq;
421
422 /* Lock to protect the control VQ */
423 struct mutex cvq_lock;
424
425 /* Host can handle any s/g split between our header and packet data */
426 bool any_header_sg;
427
428 /* Packet virtio header size */
429 u8 hdr_len;
430
431 /* UDP tunnel support */
432 bool tx_tnl;
433
434 bool rx_tnl;
435
436 bool rx_tnl_csum;
437
438 /* Work struct for config space updates */
439 struct work_struct config_work;
440
441 /* Work struct for setting rx mode */
442 struct work_struct rx_mode_work;
443
444 /* OK to queue work setting RX mode? */
445 bool rx_mode_work_enabled;
446
447 /* Does the affinity hint is set for virtqueues? */
448 bool affinity_hint_set;
449
450 /* CPU hotplug instances for online & dead */
451 struct hlist_node node;
452 struct hlist_node node_dead;
453
454 struct control_buf *ctrl;
455
456 /* Ethtool settings */
457 u8 duplex;
458 u32 speed;
459
460 /* Is rx dynamic interrupt moderation enabled? */
461 bool rx_dim_enabled;
462
463 /* Interrupt coalescing settings */
464 struct virtnet_interrupt_coalesce intr_coal_tx;
465 struct virtnet_interrupt_coalesce intr_coal_rx;
466
467 unsigned long guest_offloads;
468 unsigned long guest_offloads_capable;
469
470 /* failover when STANDBY feature enabled */
471 struct failover *failover;
472
473 u64 device_stats_cap;
474
475 struct virtio_net_rss_config_hdr *rss_hdr;
476
477 /* Must be last as it ends in a flexible-array member. */
478 TRAILING_OVERLAP(struct virtio_net_rss_config_trailer, rss_trailer, hash_key_data,
479 u8 rss_hash_key_data[NETDEV_RSS_KEY_LEN];
480 );
481 };
482 static_assert(offsetof(struct virtnet_info, rss_trailer.hash_key_data) ==
483 offsetof(struct virtnet_info, rss_hash_key_data));
484
485 struct padded_vnet_hdr {
486 struct virtio_net_hdr_v1_hash hdr;
487 /*
488 * hdr is in a separate sg buffer, and data sg buffer shares same page
489 * with this header sg. This padding makes next sg 16 byte aligned
490 * after the header.
491 */
492 char padding[12];
493 };
494
495 struct virtio_net_common_hdr {
496 union {
497 struct virtio_net_hdr hdr;
498 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
499 struct virtio_net_hdr_v1_hash hash_v1_hdr;
500 struct virtio_net_hdr_v1_hash_tunnel tnl_hdr;
501 };
502 };
503
504 static struct virtio_net_common_hdr xsk_hdr;
505
506 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
507 static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq);
508 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
509 struct net_device *dev,
510 unsigned int *xdp_xmit,
511 struct virtnet_rq_stats *stats);
512 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
513 struct sk_buff *skb, u8 flags);
514 static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
515 struct sk_buff *head_skb,
516 struct sk_buff *curr_skb,
517 struct page *page, void *buf,
518 int len, int truesize);
519 static void virtnet_xsk_completed(struct send_queue *sq, int num);
520 static void free_unused_bufs(struct virtnet_info *vi);
521 static void virtnet_del_vqs(struct virtnet_info *vi);
522
523 enum virtnet_xmit_type {
524 VIRTNET_XMIT_TYPE_SKB,
525 VIRTNET_XMIT_TYPE_SKB_ORPHAN,
526 VIRTNET_XMIT_TYPE_XDP,
527 VIRTNET_XMIT_TYPE_XSK,
528 };
529
virtnet_rss_hdr_size(const struct virtnet_info * vi)530 static size_t virtnet_rss_hdr_size(const struct virtnet_info *vi)
531 {
532 u16 indir_table_size = vi->has_rss ? vi->rss_indir_table_size : 1;
533
534 return struct_size(vi->rss_hdr, indirection_table, indir_table_size);
535 }
536
virtnet_rss_trailer_size(const struct virtnet_info * vi)537 static size_t virtnet_rss_trailer_size(const struct virtnet_info *vi)
538 {
539 return struct_size(&vi->rss_trailer, hash_key_data, vi->rss_key_size);
540 }
541
542 /* We use the last two bits of the pointer to distinguish the xmit type. */
543 #define VIRTNET_XMIT_TYPE_MASK (BIT(0) | BIT(1))
544
545 #define VIRTIO_XSK_FLAG_OFFSET 2
546
virtnet_xmit_ptr_unpack(void ** ptr)547 static enum virtnet_xmit_type virtnet_xmit_ptr_unpack(void **ptr)
548 {
549 unsigned long p = (unsigned long)*ptr;
550
551 *ptr = (void *)(p & ~VIRTNET_XMIT_TYPE_MASK);
552
553 return p & VIRTNET_XMIT_TYPE_MASK;
554 }
555
virtnet_xmit_ptr_pack(void * ptr,enum virtnet_xmit_type type)556 static void *virtnet_xmit_ptr_pack(void *ptr, enum virtnet_xmit_type type)
557 {
558 return (void *)((unsigned long)ptr | type);
559 }
560
virtnet_add_outbuf(struct send_queue * sq,int num,void * data,enum virtnet_xmit_type type)561 static int virtnet_add_outbuf(struct send_queue *sq, int num, void *data,
562 enum virtnet_xmit_type type)
563 {
564 return virtqueue_add_outbuf(sq->vq, sq->sg, num,
565 virtnet_xmit_ptr_pack(data, type),
566 GFP_ATOMIC);
567 }
568
virtnet_ptr_to_xsk_buff_len(void * ptr)569 static u32 virtnet_ptr_to_xsk_buff_len(void *ptr)
570 {
571 return ((unsigned long)ptr) >> VIRTIO_XSK_FLAG_OFFSET;
572 }
573
sg_fill_dma(struct scatterlist * sg,dma_addr_t addr,u32 len)574 static void sg_fill_dma(struct scatterlist *sg, dma_addr_t addr, u32 len)
575 {
576 sg_dma_address(sg) = addr;
577 sg_dma_len(sg) = len;
578 }
579
__free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi,struct virtnet_sq_free_stats * stats)580 static void __free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
581 bool in_napi, struct virtnet_sq_free_stats *stats)
582 {
583 struct xdp_frame *frame;
584 struct sk_buff *skb;
585 unsigned int len;
586 void *ptr;
587
588 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
589 switch (virtnet_xmit_ptr_unpack(&ptr)) {
590 case VIRTNET_XMIT_TYPE_SKB:
591 skb = ptr;
592
593 pr_debug("Sent skb %p\n", skb);
594 stats->napi_packets++;
595 stats->napi_bytes += skb->len;
596 napi_consume_skb(skb, in_napi);
597 break;
598
599 case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
600 skb = ptr;
601
602 stats->packets++;
603 stats->bytes += skb->len;
604 napi_consume_skb(skb, in_napi);
605 break;
606
607 case VIRTNET_XMIT_TYPE_XDP:
608 frame = ptr;
609
610 stats->packets++;
611 stats->bytes += xdp_get_frame_len(frame);
612 xdp_return_frame(frame);
613 break;
614
615 case VIRTNET_XMIT_TYPE_XSK:
616 stats->bytes += virtnet_ptr_to_xsk_buff_len(ptr);
617 stats->xsk++;
618 break;
619 }
620 }
621 netdev_tx_completed_queue(txq, stats->napi_packets, stats->napi_bytes);
622 }
623
virtnet_free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi,struct virtnet_sq_free_stats * stats)624 static void virtnet_free_old_xmit(struct send_queue *sq,
625 struct netdev_queue *txq,
626 bool in_napi,
627 struct virtnet_sq_free_stats *stats)
628 {
629 __free_old_xmit(sq, txq, in_napi, stats);
630
631 if (stats->xsk)
632 virtnet_xsk_completed(sq, stats->xsk);
633 }
634
635 /* Converting between virtqueue no. and kernel tx/rx queue no.
636 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
637 */
vq2txq(struct virtqueue * vq)638 static int vq2txq(struct virtqueue *vq)
639 {
640 return (vq->index - 1) / 2;
641 }
642
txq2vq(int txq)643 static int txq2vq(int txq)
644 {
645 return txq * 2 + 1;
646 }
647
vq2rxq(struct virtqueue * vq)648 static int vq2rxq(struct virtqueue *vq)
649 {
650 return vq->index / 2;
651 }
652
rxq2vq(int rxq)653 static int rxq2vq(int rxq)
654 {
655 return rxq * 2;
656 }
657
vq_type(struct virtnet_info * vi,int qid)658 static int vq_type(struct virtnet_info *vi, int qid)
659 {
660 if (qid == vi->max_queue_pairs * 2)
661 return VIRTNET_Q_TYPE_CQ;
662
663 if (qid % 2)
664 return VIRTNET_Q_TYPE_TX;
665
666 return VIRTNET_Q_TYPE_RX;
667 }
668
669 static inline struct virtio_net_common_hdr *
skb_vnet_common_hdr(struct sk_buff * skb)670 skb_vnet_common_hdr(struct sk_buff *skb)
671 {
672 return (struct virtio_net_common_hdr *)skb->cb;
673 }
674
675 /*
676 * private is used to chain pages for big packets, put the whole
677 * most recent used list in the beginning for reuse
678 */
give_pages(struct receive_queue * rq,struct page * page)679 static void give_pages(struct receive_queue *rq, struct page *page)
680 {
681 struct page *end;
682
683 /* Find end of list, sew whole thing into vi->rq.pages. */
684 for (end = page; end->private; end = (struct page *)end->private);
685 end->private = (unsigned long)rq->pages;
686 rq->pages = page;
687 }
688
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)689 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
690 {
691 struct page *p = rq->pages;
692
693 if (p) {
694 rq->pages = (struct page *)p->private;
695 /* clear private here, it is used to chain pages */
696 p->private = 0;
697 } else
698 p = alloc_page(gfp_mask);
699 return p;
700 }
701
virtnet_rq_free_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf)702 static void virtnet_rq_free_buf(struct virtnet_info *vi,
703 struct receive_queue *rq, void *buf)
704 {
705 if (!rq->page_pool)
706 give_pages(rq, buf);
707 else
708 page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
709 }
710
enable_rx_mode_work(struct virtnet_info * vi)711 static void enable_rx_mode_work(struct virtnet_info *vi)
712 {
713 rtnl_lock();
714 vi->rx_mode_work_enabled = true;
715 rtnl_unlock();
716 }
717
disable_rx_mode_work(struct virtnet_info * vi)718 static void disable_rx_mode_work(struct virtnet_info *vi)
719 {
720 rtnl_lock();
721 vi->rx_mode_work_enabled = false;
722 rtnl_unlock();
723 }
724
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)725 static void virtqueue_napi_schedule(struct napi_struct *napi,
726 struct virtqueue *vq)
727 {
728 if (napi_schedule_prep(napi)) {
729 virtqueue_disable_cb(vq);
730 __napi_schedule(napi);
731 }
732 }
733
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)734 static bool virtqueue_napi_complete(struct napi_struct *napi,
735 struct virtqueue *vq, int processed)
736 {
737 int opaque;
738
739 opaque = virtqueue_enable_cb_prepare(vq);
740 if (napi_complete_done(napi, processed)) {
741 if (unlikely(virtqueue_poll(vq, opaque)))
742 virtqueue_napi_schedule(napi, vq);
743 else
744 return true;
745 } else {
746 virtqueue_disable_cb(vq);
747 }
748
749 return false;
750 }
751
virtnet_tx_wake_queue(struct virtnet_info * vi,struct send_queue * sq)752 static void virtnet_tx_wake_queue(struct virtnet_info *vi,
753 struct send_queue *sq)
754 {
755 unsigned int index = vq2txq(sq->vq);
756 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
757
758 if (netif_tx_queue_stopped(txq)) {
759 u64_stats_update_begin(&sq->stats.syncp);
760 u64_stats_inc(&sq->stats.wake);
761 u64_stats_update_end(&sq->stats.syncp);
762 netif_tx_wake_queue(txq);
763 }
764 }
765
skb_xmit_done(struct virtqueue * vq)766 static void skb_xmit_done(struct virtqueue *vq)
767 {
768 struct virtnet_info *vi = vq->vdev->priv;
769 unsigned int index = vq2txq(vq);
770 struct send_queue *sq = &vi->sq[index];
771 struct napi_struct *napi = &sq->napi;
772
773 /* Suppress further interrupts. */
774 virtqueue_disable_cb(vq);
775
776 if (napi->weight)
777 virtqueue_napi_schedule(napi, vq);
778 else
779 virtnet_tx_wake_queue(vi, sq);
780 }
781
782 #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)783 static void *mergeable_len_to_ctx(unsigned int truesize,
784 unsigned int headroom)
785 {
786 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
787 }
788
mergeable_ctx_to_headroom(void * mrg_ctx)789 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
790 {
791 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
792 }
793
mergeable_ctx_to_truesize(void * mrg_ctx)794 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
795 {
796 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
797 }
798
check_mergeable_len(struct net_device * dev,void * mrg_ctx,unsigned int len)799 static int check_mergeable_len(struct net_device *dev, void *mrg_ctx,
800 unsigned int len)
801 {
802 unsigned int headroom, tailroom, room, truesize;
803
804 truesize = mergeable_ctx_to_truesize(mrg_ctx);
805 headroom = mergeable_ctx_to_headroom(mrg_ctx);
806 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
807 room = SKB_DATA_ALIGN(headroom + tailroom);
808
809 if (len > truesize - room) {
810 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
811 dev->name, len, (unsigned long)(truesize - room));
812 DEV_STATS_INC(dev, rx_length_errors);
813 return -1;
814 }
815
816 return 0;
817 }
818
virtnet_build_skb(void * buf,unsigned int buflen,unsigned int headroom,unsigned int len)819 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
820 unsigned int headroom,
821 unsigned int len)
822 {
823 struct sk_buff *skb;
824
825 skb = build_skb(buf, buflen);
826 if (unlikely(!skb))
827 return NULL;
828
829 skb_reserve(skb, headroom);
830 skb_put(skb, len);
831
832 return skb;
833 }
834
835 /* Called from bottom half context */
page_to_skb(struct virtnet_info * vi,struct receive_queue * rq,struct page * page,unsigned int offset,unsigned int len,unsigned int truesize,unsigned int headroom)836 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
837 struct receive_queue *rq,
838 struct page *page, unsigned int offset,
839 unsigned int len, unsigned int truesize,
840 unsigned int headroom)
841 {
842 struct sk_buff *skb;
843 struct virtio_net_common_hdr *hdr;
844 unsigned int copy, hdr_len, hdr_padded_len;
845 struct page *page_to_free = NULL;
846 int tailroom, shinfo_size;
847 char *p, *hdr_p, *buf;
848
849 p = page_address(page) + offset;
850 hdr_p = p;
851
852 hdr_len = vi->hdr_len;
853 if (vi->mergeable_rx_bufs)
854 hdr_padded_len = hdr_len;
855 else
856 hdr_padded_len = sizeof(struct padded_vnet_hdr);
857
858 buf = p - headroom;
859 len -= hdr_len;
860 offset += hdr_padded_len;
861 p += hdr_padded_len;
862 tailroom = truesize - headroom - hdr_padded_len - len;
863
864 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
865
866 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
867 skb = virtnet_build_skb(buf, truesize, p - buf, len);
868 if (unlikely(!skb))
869 return NULL;
870 /* Big packets mode chains pages via page->private, which is
871 * incompatible with the way page_pool uses page->private.
872 * Currently, big packets mode doesn't use page pools.
873 */
874 if (!rq->page_pool) {
875 page = (struct page *)page->private;
876 if (page)
877 give_pages(rq, page);
878 }
879
880 goto ok;
881 }
882
883 /* copy small packet so we can reuse these pages for small data */
884 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
885 if (unlikely(!skb))
886 return NULL;
887
888 /* Copy all frame if it fits skb->head, otherwise
889 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
890 */
891 if (len <= skb_tailroom(skb))
892 copy = len;
893 else
894 copy = ETH_HLEN;
895 skb_put_data(skb, p, copy);
896
897 len -= copy;
898 offset += copy;
899
900 if (vi->mergeable_rx_bufs) {
901 if (len)
902 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
903 else
904 page_to_free = page;
905 goto ok;
906 }
907
908 BUG_ON(offset >= PAGE_SIZE);
909 while (len) {
910 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
911 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
912 frag_size, truesize);
913 len -= frag_size;
914 page = (struct page *)page->private;
915 offset = 0;
916 }
917
918 if (page)
919 give_pages(rq, page);
920
921 ok:
922 hdr = skb_vnet_common_hdr(skb);
923 memcpy(hdr, hdr_p, hdr_len);
924 if (page_to_free)
925 page_pool_put_page(rq->page_pool, page_to_free, -1, true);
926
927 return skb;
928 }
929
virtnet_rq_get_buf(struct receive_queue * rq,u32 * len,void ** ctx)930 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
931 {
932 BUG_ON(!rq->page_pool);
933
934 return virtqueue_get_buf_ctx(rq->vq, len, ctx);
935 }
936
virtnet_rq_unmap_free_buf(struct virtqueue * vq,void * buf)937 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
938 {
939 struct virtnet_info *vi = vq->vdev->priv;
940 struct receive_queue *rq;
941 int i = vq2rxq(vq);
942
943 rq = &vi->rq[i];
944
945 if (rq->xsk_pool) {
946 xsk_buff_free((struct xdp_buff *)buf);
947 return;
948 }
949
950 virtnet_rq_free_buf(vi, rq, buf);
951 }
952
free_old_xmit(struct send_queue * sq,struct netdev_queue * txq,bool in_napi)953 static void free_old_xmit(struct send_queue *sq, struct netdev_queue *txq,
954 bool in_napi)
955 {
956 struct virtnet_sq_free_stats stats = {0};
957
958 virtnet_free_old_xmit(sq, txq, in_napi, &stats);
959
960 /* Avoid overhead when no packets have been processed
961 * happens when called speculatively from start_xmit.
962 */
963 if (!stats.packets && !stats.napi_packets)
964 return;
965
966 u64_stats_update_begin(&sq->stats.syncp);
967 u64_stats_add(&sq->stats.bytes, stats.bytes + stats.napi_bytes);
968 u64_stats_add(&sq->stats.packets, stats.packets + stats.napi_packets);
969 u64_stats_update_end(&sq->stats.syncp);
970 }
971
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)972 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
973 {
974 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
975 return false;
976 else if (q < vi->curr_queue_pairs)
977 return true;
978 else
979 return false;
980 }
981
tx_may_stop(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)982 static bool tx_may_stop(struct virtnet_info *vi,
983 struct net_device *dev,
984 struct send_queue *sq)
985 {
986 int qnum;
987
988 qnum = sq - vi->sq;
989
990 /* If running out of space, stop queue to avoid getting packets that we
991 * are then unable to transmit.
992 * An alternative would be to force queuing layer to requeue the skb by
993 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
994 * returned in a normal path of operation: it means that driver is not
995 * maintaining the TX queue stop/start state properly, and causes
996 * the stack to do a non-trivial amount of useless work.
997 * Since most packets only take 1 or 2 ring slots, stopping the queue
998 * early means 16 slots are typically wasted.
999 */
1000 if (sq->vq->num_free < MAX_SKB_FRAGS + 2) {
1001 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1002
1003 netif_tx_stop_queue(txq);
1004 u64_stats_update_begin(&sq->stats.syncp);
1005 u64_stats_inc(&sq->stats.stop);
1006 u64_stats_update_end(&sq->stats.syncp);
1007
1008 return true;
1009 }
1010
1011 return false;
1012 }
1013
check_sq_full_and_disable(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)1014 static void check_sq_full_and_disable(struct virtnet_info *vi,
1015 struct net_device *dev,
1016 struct send_queue *sq)
1017 {
1018 bool use_napi = sq->napi.weight;
1019 int qnum;
1020
1021 qnum = sq - vi->sq;
1022
1023 if (tx_may_stop(vi, dev, sq)) {
1024 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
1025
1026 if (use_napi) {
1027 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
1028 virtqueue_napi_schedule(&sq->napi, sq->vq);
1029 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
1030 /* More just got used, free them then recheck. */
1031 free_old_xmit(sq, txq, false);
1032 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2) {
1033 netif_start_subqueue(dev, qnum);
1034 u64_stats_update_begin(&sq->stats.syncp);
1035 u64_stats_inc(&sq->stats.wake);
1036 u64_stats_update_end(&sq->stats.syncp);
1037 virtqueue_disable_cb(sq->vq);
1038 }
1039 }
1040 }
1041 }
1042
1043 /* Note that @len is the length of received data without virtio header */
buf_to_xdp(struct virtnet_info * vi,struct receive_queue * rq,void * buf,u32 len,bool first_buf)1044 static struct xdp_buff *buf_to_xdp(struct virtnet_info *vi,
1045 struct receive_queue *rq, void *buf,
1046 u32 len, bool first_buf)
1047 {
1048 struct xdp_buff *xdp;
1049 u32 bufsize;
1050
1051 xdp = (struct xdp_buff *)buf;
1052
1053 /* In virtnet_add_recvbuf_xsk, we use part of XDP_PACKET_HEADROOM for
1054 * virtio header and ask the vhost to fill data from
1055 * hard_start + XDP_PACKET_HEADROOM - vi->hdr_len
1056 * The first buffer has virtio header so the remaining region for frame
1057 * data is
1058 * xsk_pool_get_rx_frame_size()
1059 * While other buffers than the first one do not have virtio header, so
1060 * the maximum frame data's length can be
1061 * xsk_pool_get_rx_frame_size() + vi->hdr_len
1062 */
1063 bufsize = xsk_pool_get_rx_frame_size(rq->xsk_pool);
1064 if (!first_buf)
1065 bufsize += vi->hdr_len;
1066
1067 if (unlikely(len > bufsize)) {
1068 pr_debug("%s: rx error: len %u exceeds truesize %u\n",
1069 vi->dev->name, len, bufsize);
1070 DEV_STATS_INC(vi->dev, rx_length_errors);
1071 xsk_buff_free(xdp);
1072 return NULL;
1073 }
1074
1075 if (first_buf) {
1076 xsk_buff_set_size(xdp, len);
1077 } else {
1078 xdp_prepare_buff(xdp, xdp->data_hard_start,
1079 XDP_PACKET_HEADROOM - vi->hdr_len, len, 1);
1080 xdp->flags = 0;
1081 }
1082
1083 xsk_buff_dma_sync_for_cpu(xdp);
1084
1085 return xdp;
1086 }
1087
xsk_construct_skb(struct receive_queue * rq,struct xdp_buff * xdp)1088 static struct sk_buff *xsk_construct_skb(struct receive_queue *rq,
1089 struct xdp_buff *xdp)
1090 {
1091 unsigned int metasize = xdp->data - xdp->data_meta;
1092 struct sk_buff *skb;
1093 unsigned int size;
1094
1095 size = xdp->data_end - xdp->data_hard_start;
1096 skb = napi_alloc_skb(&rq->napi, size);
1097 if (unlikely(!skb)) {
1098 xsk_buff_free(xdp);
1099 return NULL;
1100 }
1101
1102 skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
1103
1104 size = xdp->data_end - xdp->data_meta;
1105 memcpy(__skb_put(skb, size), xdp->data_meta, size);
1106
1107 if (metasize) {
1108 __skb_pull(skb, metasize);
1109 skb_metadata_set(skb, metasize);
1110 }
1111
1112 xsk_buff_free(xdp);
1113
1114 return skb;
1115 }
1116
virtnet_receive_xsk_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1117 static struct sk_buff *virtnet_receive_xsk_small(struct net_device *dev, struct virtnet_info *vi,
1118 struct receive_queue *rq, struct xdp_buff *xdp,
1119 unsigned int *xdp_xmit,
1120 struct virtnet_rq_stats *stats)
1121 {
1122 struct bpf_prog *prog;
1123 u32 ret;
1124
1125 ret = XDP_PASS;
1126 rcu_read_lock();
1127 prog = rcu_dereference(rq->xdp_prog);
1128 if (prog)
1129 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit, stats);
1130 rcu_read_unlock();
1131
1132 switch (ret) {
1133 case XDP_PASS:
1134 return xsk_construct_skb(rq, xdp);
1135
1136 case XDP_TX:
1137 case XDP_REDIRECT:
1138 return NULL;
1139
1140 default:
1141 /* drop packet */
1142 xsk_buff_free(xdp);
1143 u64_stats_inc(&stats->drops);
1144 return NULL;
1145 }
1146 }
1147
xsk_drop_follow_bufs(struct net_device * dev,struct receive_queue * rq,u32 num_buf,struct virtnet_rq_stats * stats)1148 static void xsk_drop_follow_bufs(struct net_device *dev,
1149 struct receive_queue *rq,
1150 u32 num_buf,
1151 struct virtnet_rq_stats *stats)
1152 {
1153 struct xdp_buff *xdp;
1154 u32 len;
1155
1156 while (num_buf-- > 1) {
1157 xdp = virtqueue_get_buf(rq->vq, &len);
1158 if (unlikely(!xdp)) {
1159 pr_debug("%s: rx error: %d buffers missing\n",
1160 dev->name, num_buf);
1161 DEV_STATS_INC(dev, rx_length_errors);
1162 break;
1163 }
1164 u64_stats_add(&stats->bytes, len);
1165 xsk_buff_free(xdp);
1166 }
1167 }
1168
xsk_append_merge_buffer(struct virtnet_info * vi,struct receive_queue * rq,struct sk_buff * head_skb,u32 num_buf,struct virtio_net_hdr_mrg_rxbuf * hdr,struct virtnet_rq_stats * stats)1169 static int xsk_append_merge_buffer(struct virtnet_info *vi,
1170 struct receive_queue *rq,
1171 struct sk_buff *head_skb,
1172 u32 num_buf,
1173 struct virtio_net_hdr_mrg_rxbuf *hdr,
1174 struct virtnet_rq_stats *stats)
1175 {
1176 struct sk_buff *curr_skb;
1177 struct xdp_buff *xdp;
1178 u32 len, truesize;
1179 struct page *page;
1180 void *buf;
1181
1182 curr_skb = head_skb;
1183
1184 while (--num_buf) {
1185 buf = virtqueue_get_buf(rq->vq, &len);
1186 if (unlikely(!buf)) {
1187 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1188 vi->dev->name, num_buf,
1189 virtio16_to_cpu(vi->vdev,
1190 hdr->num_buffers));
1191 DEV_STATS_INC(vi->dev, rx_length_errors);
1192 return -EINVAL;
1193 }
1194
1195 u64_stats_add(&stats->bytes, len);
1196
1197 xdp = buf_to_xdp(vi, rq, buf, len, false);
1198 if (!xdp)
1199 goto err;
1200
1201 buf = napi_alloc_frag(len);
1202 if (!buf) {
1203 xsk_buff_free(xdp);
1204 goto err;
1205 }
1206
1207 memcpy(buf, xdp->data, len);
1208
1209 xsk_buff_free(xdp);
1210
1211 page = virt_to_page(buf);
1212
1213 truesize = len;
1214
1215 curr_skb = virtnet_skb_append_frag(rq, head_skb, curr_skb, page,
1216 buf, len, truesize);
1217 if (!curr_skb) {
1218 put_page(page);
1219 goto err;
1220 }
1221 }
1222
1223 return 0;
1224
1225 err:
1226 xsk_drop_follow_bufs(vi->dev, rq, num_buf, stats);
1227 return -EINVAL;
1228 }
1229
virtnet_receive_xsk_merge(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1230 static struct sk_buff *virtnet_receive_xsk_merge(struct net_device *dev, struct virtnet_info *vi,
1231 struct receive_queue *rq, struct xdp_buff *xdp,
1232 unsigned int *xdp_xmit,
1233 struct virtnet_rq_stats *stats)
1234 {
1235 struct virtio_net_hdr_mrg_rxbuf *hdr;
1236 struct bpf_prog *prog;
1237 struct sk_buff *skb;
1238 u32 ret, num_buf;
1239
1240 hdr = xdp->data - vi->hdr_len;
1241 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1242
1243 ret = XDP_PASS;
1244 rcu_read_lock();
1245 prog = rcu_dereference(rq->xdp_prog);
1246 if (prog) {
1247 /* TODO: support multi buffer. */
1248 if (num_buf == 1)
1249 ret = virtnet_xdp_handler(prog, xdp, dev, xdp_xmit,
1250 stats);
1251 else
1252 ret = XDP_ABORTED;
1253 }
1254 rcu_read_unlock();
1255
1256 switch (ret) {
1257 case XDP_PASS:
1258 skb = xsk_construct_skb(rq, xdp);
1259 if (!skb)
1260 goto drop_bufs;
1261
1262 if (xsk_append_merge_buffer(vi, rq, skb, num_buf, hdr, stats)) {
1263 dev_kfree_skb(skb);
1264 goto drop;
1265 }
1266
1267 return skb;
1268
1269 case XDP_TX:
1270 case XDP_REDIRECT:
1271 return NULL;
1272
1273 default:
1274 /* drop packet */
1275 xsk_buff_free(xdp);
1276 }
1277
1278 drop_bufs:
1279 xsk_drop_follow_bufs(dev, rq, num_buf, stats);
1280
1281 drop:
1282 u64_stats_inc(&stats->drops);
1283 return NULL;
1284 }
1285
virtnet_receive_xsk_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,u32 len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1286 static void virtnet_receive_xsk_buf(struct virtnet_info *vi, struct receive_queue *rq,
1287 void *buf, u32 len,
1288 unsigned int *xdp_xmit,
1289 struct virtnet_rq_stats *stats)
1290 {
1291 struct net_device *dev = vi->dev;
1292 struct sk_buff *skb = NULL;
1293 struct xdp_buff *xdp;
1294 u8 flags;
1295
1296 len -= vi->hdr_len;
1297
1298 u64_stats_add(&stats->bytes, len);
1299
1300 xdp = buf_to_xdp(vi, rq, buf, len, true);
1301 if (!xdp)
1302 return;
1303
1304 if (unlikely(len < ETH_HLEN)) {
1305 pr_debug("%s: short packet %i\n", dev->name, len);
1306 DEV_STATS_INC(dev, rx_length_errors);
1307 xsk_buff_free(xdp);
1308 return;
1309 }
1310
1311 flags = ((struct virtio_net_common_hdr *)(xdp->data - vi->hdr_len))->hdr.flags;
1312
1313 if (!vi->mergeable_rx_bufs)
1314 skb = virtnet_receive_xsk_small(dev, vi, rq, xdp, xdp_xmit, stats);
1315 else
1316 skb = virtnet_receive_xsk_merge(dev, vi, rq, xdp, xdp_xmit, stats);
1317
1318 if (skb)
1319 virtnet_receive_done(vi, rq, skb, flags);
1320 }
1321
virtnet_add_recvbuf_xsk(struct virtnet_info * vi,struct receive_queue * rq,struct xsk_buff_pool * pool,gfp_t gfp)1322 static int virtnet_add_recvbuf_xsk(struct virtnet_info *vi, struct receive_queue *rq,
1323 struct xsk_buff_pool *pool, gfp_t gfp)
1324 {
1325 struct xdp_buff **xsk_buffs;
1326 dma_addr_t addr;
1327 int err = 0;
1328 u32 len, i;
1329 int num;
1330
1331 xsk_buffs = rq->xsk_buffs;
1332
1333 num = xsk_buff_alloc_batch(pool, xsk_buffs, rq->vq->num_free);
1334 if (!num) {
1335 if (xsk_uses_need_wakeup(pool)) {
1336 xsk_set_rx_need_wakeup(pool);
1337 /* Return 0 instead of -ENOMEM so that NAPI is
1338 * descheduled.
1339 */
1340 return 0;
1341 }
1342
1343 return -ENOMEM;
1344 } else {
1345 xsk_clear_rx_need_wakeup(pool);
1346 }
1347
1348 len = xsk_pool_get_rx_frame_size(pool) + vi->hdr_len;
1349
1350 for (i = 0; i < num; ++i) {
1351 /* Use the part of XDP_PACKET_HEADROOM as the virtnet hdr space.
1352 * We assume XDP_PACKET_HEADROOM is larger than hdr->len.
1353 * (see function virtnet_xsk_pool_enable)
1354 */
1355 addr = xsk_buff_xdp_get_dma(xsk_buffs[i]) - vi->hdr_len;
1356
1357 sg_init_table(rq->sg, 1);
1358 sg_fill_dma(rq->sg, addr, len);
1359
1360 err = virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1,
1361 xsk_buffs[i], NULL, gfp);
1362 if (err)
1363 goto err;
1364 }
1365
1366 return num;
1367
1368 err:
1369 for (; i < num; ++i)
1370 xsk_buff_free(xsk_buffs[i]);
1371
1372 return err;
1373 }
1374
virtnet_xsk_to_ptr(u32 len)1375 static void *virtnet_xsk_to_ptr(u32 len)
1376 {
1377 unsigned long p;
1378
1379 p = len << VIRTIO_XSK_FLAG_OFFSET;
1380
1381 return virtnet_xmit_ptr_pack((void *)p, VIRTNET_XMIT_TYPE_XSK);
1382 }
1383
virtnet_xsk_xmit_one(struct send_queue * sq,struct xsk_buff_pool * pool,struct xdp_desc * desc)1384 static int virtnet_xsk_xmit_one(struct send_queue *sq,
1385 struct xsk_buff_pool *pool,
1386 struct xdp_desc *desc)
1387 {
1388 struct virtnet_info *vi;
1389 dma_addr_t addr;
1390
1391 vi = sq->vq->vdev->priv;
1392
1393 addr = xsk_buff_raw_get_dma(pool, desc->addr);
1394 xsk_buff_raw_dma_sync_for_device(pool, addr, desc->len);
1395
1396 sg_init_table(sq->sg, 2);
1397 sg_fill_dma(sq->sg, sq->xsk_hdr_dma_addr, vi->hdr_len);
1398 sg_fill_dma(sq->sg + 1, addr, desc->len);
1399
1400 return virtqueue_add_outbuf_premapped(sq->vq, sq->sg, 2,
1401 virtnet_xsk_to_ptr(desc->len),
1402 GFP_ATOMIC);
1403 }
1404
virtnet_xsk_xmit_batch(struct send_queue * sq,struct xsk_buff_pool * pool,unsigned int budget,u64 * kicks)1405 static int virtnet_xsk_xmit_batch(struct send_queue *sq,
1406 struct xsk_buff_pool *pool,
1407 unsigned int budget,
1408 u64 *kicks)
1409 {
1410 struct xdp_desc *descs = pool->tx_descs;
1411 bool kick = false;
1412 u32 nb_pkts, i;
1413 int err;
1414
1415 budget = min_t(u32, budget, sq->vq->num_free);
1416
1417 nb_pkts = xsk_tx_peek_release_desc_batch(pool, budget);
1418 if (!nb_pkts)
1419 return 0;
1420
1421 for (i = 0; i < nb_pkts; i++) {
1422 err = virtnet_xsk_xmit_one(sq, pool, &descs[i]);
1423 if (unlikely(err)) {
1424 xsk_tx_completed(sq->xsk_pool, nb_pkts - i);
1425 break;
1426 }
1427
1428 kick = true;
1429 }
1430
1431 if (kick && virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1432 (*kicks)++;
1433
1434 return i;
1435 }
1436
virtnet_xsk_xmit(struct send_queue * sq,struct xsk_buff_pool * pool,int budget)1437 static bool virtnet_xsk_xmit(struct send_queue *sq, struct xsk_buff_pool *pool,
1438 int budget)
1439 {
1440 struct virtnet_info *vi = sq->vq->vdev->priv;
1441 struct virtnet_sq_free_stats stats = {};
1442 struct net_device *dev = vi->dev;
1443 u64 kicks = 0;
1444 int sent;
1445
1446 /* Avoid to wakeup napi meanless, so call __free_old_xmit instead of
1447 * free_old_xmit().
1448 */
1449 __free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq), true, &stats);
1450
1451 if (stats.xsk)
1452 xsk_tx_completed(sq->xsk_pool, stats.xsk);
1453
1454 sent = virtnet_xsk_xmit_batch(sq, pool, budget, &kicks);
1455
1456 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1457 check_sq_full_and_disable(vi, vi->dev, sq);
1458
1459 if (sent) {
1460 struct netdev_queue *txq;
1461
1462 txq = netdev_get_tx_queue(vi->dev, sq - vi->sq);
1463 txq_trans_cond_update(txq);
1464 }
1465
1466 u64_stats_update_begin(&sq->stats.syncp);
1467 u64_stats_add(&sq->stats.packets, stats.packets);
1468 u64_stats_add(&sq->stats.bytes, stats.bytes);
1469 u64_stats_add(&sq->stats.kicks, kicks);
1470 u64_stats_add(&sq->stats.xdp_tx, sent);
1471 u64_stats_update_end(&sq->stats.syncp);
1472
1473 if (xsk_uses_need_wakeup(pool))
1474 xsk_set_tx_need_wakeup(pool);
1475
1476 return sent;
1477 }
1478
xsk_wakeup(struct napi_struct * napi,struct virtqueue * vq)1479 static void xsk_wakeup(struct napi_struct *napi, struct virtqueue *vq)
1480 {
1481 if (napi_if_scheduled_mark_missed(napi))
1482 return;
1483
1484 local_bh_disable();
1485 virtqueue_napi_schedule(napi, vq);
1486 local_bh_enable();
1487 }
1488
virtnet_xsk_wakeup(struct net_device * dev,u32 qid,u32 flag)1489 static int virtnet_xsk_wakeup(struct net_device *dev, u32 qid, u32 flag)
1490 {
1491 struct virtnet_info *vi = netdev_priv(dev);
1492
1493 if (!netif_running(dev))
1494 return -ENETDOWN;
1495
1496 if (qid >= vi->curr_queue_pairs)
1497 return -EINVAL;
1498
1499 if (flag & XDP_WAKEUP_TX) {
1500 struct send_queue *sq = &vi->sq[qid];
1501
1502 xsk_wakeup(&sq->napi, sq->vq);
1503 }
1504
1505 if (flag & XDP_WAKEUP_RX) {
1506 struct receive_queue *rq = &vi->rq[qid];
1507
1508 xsk_wakeup(&rq->napi, rq->vq);
1509 }
1510
1511 return 0;
1512 }
1513
virtnet_xsk_completed(struct send_queue * sq,int num)1514 static void virtnet_xsk_completed(struct send_queue *sq, int num)
1515 {
1516 xsk_tx_completed(sq->xsk_pool, num);
1517
1518 /* If this is called by rx poll, start_xmit and xdp xmit we should
1519 * wakeup the tx napi to consume the xsk tx queue, because the tx
1520 * interrupt may not be triggered.
1521 */
1522 xsk_wakeup(&sq->napi, sq->vq);
1523 }
1524
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)1525 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
1526 struct send_queue *sq,
1527 struct xdp_frame *xdpf)
1528 {
1529 struct virtio_net_hdr_mrg_rxbuf *hdr;
1530 struct skb_shared_info *shinfo;
1531 u8 nr_frags = 0;
1532 int err, i;
1533
1534 if (unlikely(xdpf->headroom < vi->hdr_len))
1535 return -EOVERFLOW;
1536
1537 if (unlikely(xdp_frame_has_frags(xdpf))) {
1538 shinfo = xdp_get_shared_info_from_frame(xdpf);
1539 nr_frags = shinfo->nr_frags;
1540 }
1541
1542 /* In wrapping function virtnet_xdp_xmit(), we need to free
1543 * up the pending old buffers, where we need to calculate the
1544 * position of skb_shared_info in xdp_get_frame_len() and
1545 * xdp_return_frame(), which will involve to xdpf->data and
1546 * xdpf->headroom. Therefore, we need to update the value of
1547 * headroom synchronously here.
1548 */
1549 xdpf->headroom -= vi->hdr_len;
1550 xdpf->data -= vi->hdr_len;
1551 /* Zero header and leave csum up to XDP layers */
1552 hdr = xdpf->data;
1553 memset(hdr, 0, vi->hdr_len);
1554 xdpf->len += vi->hdr_len;
1555
1556 sg_init_table(sq->sg, nr_frags + 1);
1557 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
1558 for (i = 0; i < nr_frags; i++) {
1559 skb_frag_t *frag = &shinfo->frags[i];
1560
1561 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
1562 skb_frag_size(frag), skb_frag_off(frag));
1563 }
1564
1565 err = virtnet_add_outbuf(sq, nr_frags + 1, xdpf, VIRTNET_XMIT_TYPE_XDP);
1566 if (unlikely(err))
1567 return -ENOSPC; /* Caller handle free/refcnt */
1568
1569 return 0;
1570 }
1571
1572 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
1573 * the current cpu, so it does not need to be locked.
1574 *
1575 * Here we use marco instead of inline functions because we have to deal with
1576 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
1577 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
1578 * functions to perfectly solve these three problems at the same time.
1579 */
1580 #define virtnet_xdp_get_sq(vi) ({ \
1581 int cpu = smp_processor_id(); \
1582 struct netdev_queue *txq; \
1583 typeof(vi) v = (vi); \
1584 unsigned int qp; \
1585 \
1586 if (v->curr_queue_pairs > nr_cpu_ids) { \
1587 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
1588 qp += cpu; \
1589 txq = netdev_get_tx_queue(v->dev, qp); \
1590 __netif_tx_acquire(txq); \
1591 } else { \
1592 qp = cpu % v->curr_queue_pairs; \
1593 txq = netdev_get_tx_queue(v->dev, qp); \
1594 __netif_tx_lock(txq, cpu); \
1595 } \
1596 v->sq + qp; \
1597 })
1598
1599 #define virtnet_xdp_put_sq(vi, q) { \
1600 struct netdev_queue *txq; \
1601 typeof(vi) v = (vi); \
1602 \
1603 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
1604 if (v->curr_queue_pairs > nr_cpu_ids) \
1605 __netif_tx_release(txq); \
1606 else \
1607 __netif_tx_unlock(txq); \
1608 }
1609
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)1610 static int virtnet_xdp_xmit(struct net_device *dev,
1611 int n, struct xdp_frame **frames, u32 flags)
1612 {
1613 struct virtnet_info *vi = netdev_priv(dev);
1614 struct virtnet_sq_free_stats stats = {0};
1615 struct receive_queue *rq = vi->rq;
1616 struct bpf_prog *xdp_prog;
1617 struct send_queue *sq;
1618 int nxmit = 0;
1619 int kicks = 0;
1620 int ret;
1621 int i;
1622
1623 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
1624 * indicate XDP resources have been successfully allocated.
1625 */
1626 xdp_prog = rcu_access_pointer(rq->xdp_prog);
1627 if (!xdp_prog)
1628 return -ENXIO;
1629
1630 sq = virtnet_xdp_get_sq(vi);
1631
1632 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
1633 ret = -EINVAL;
1634 goto out;
1635 }
1636
1637 /* Free up any pending old buffers before queueing new ones. */
1638 virtnet_free_old_xmit(sq, netdev_get_tx_queue(dev, sq - vi->sq),
1639 false, &stats);
1640
1641 for (i = 0; i < n; i++) {
1642 struct xdp_frame *xdpf = frames[i];
1643
1644 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
1645 break;
1646 nxmit++;
1647 }
1648 ret = nxmit;
1649
1650 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1651 check_sq_full_and_disable(vi, dev, sq);
1652
1653 if (flags & XDP_XMIT_FLUSH) {
1654 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1655 kicks = 1;
1656 }
1657 out:
1658 u64_stats_update_begin(&sq->stats.syncp);
1659 u64_stats_add(&sq->stats.bytes, stats.bytes);
1660 u64_stats_add(&sq->stats.packets, stats.packets);
1661 u64_stats_add(&sq->stats.xdp_tx, n);
1662 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
1663 u64_stats_add(&sq->stats.kicks, kicks);
1664 u64_stats_update_end(&sq->stats.syncp);
1665
1666 virtnet_xdp_put_sq(vi, sq);
1667 return ret;
1668 }
1669
put_xdp_frags(struct receive_queue * rq,struct xdp_buff * xdp)1670 static void put_xdp_frags(struct receive_queue *rq, struct xdp_buff *xdp)
1671 {
1672 struct skb_shared_info *shinfo;
1673 struct page *xdp_page;
1674 int i;
1675
1676 if (xdp_buff_has_frags(xdp)) {
1677 shinfo = xdp_get_shared_info_from_buff(xdp);
1678 for (i = 0; i < shinfo->nr_frags; i++) {
1679 xdp_page = skb_frag_page(&shinfo->frags[i]);
1680 page_pool_put_page(rq->page_pool, xdp_page, -1, true);
1681 }
1682 }
1683 }
1684
virtnet_xdp_handler(struct bpf_prog * xdp_prog,struct xdp_buff * xdp,struct net_device * dev,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1685 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1686 struct net_device *dev,
1687 unsigned int *xdp_xmit,
1688 struct virtnet_rq_stats *stats)
1689 {
1690 struct xdp_frame *xdpf;
1691 int err;
1692 u32 act;
1693
1694 act = bpf_prog_run_xdp(xdp_prog, xdp);
1695 u64_stats_inc(&stats->xdp_packets);
1696
1697 switch (act) {
1698 case XDP_PASS:
1699 return act;
1700
1701 case XDP_TX:
1702 u64_stats_inc(&stats->xdp_tx);
1703 xdpf = xdp_convert_buff_to_frame(xdp);
1704 if (unlikely(!xdpf)) {
1705 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1706 return XDP_DROP;
1707 }
1708
1709 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1710 if (unlikely(!err)) {
1711 xdp_return_frame_rx_napi(xdpf);
1712 } else if (unlikely(err < 0)) {
1713 trace_xdp_exception(dev, xdp_prog, act);
1714 return XDP_DROP;
1715 }
1716 *xdp_xmit |= VIRTIO_XDP_TX;
1717 return act;
1718
1719 case XDP_REDIRECT:
1720 u64_stats_inc(&stats->xdp_redirects);
1721 err = xdp_do_redirect(dev, xdp, xdp_prog);
1722 if (err)
1723 return XDP_DROP;
1724
1725 *xdp_xmit |= VIRTIO_XDP_REDIR;
1726 return act;
1727
1728 default:
1729 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1730 fallthrough;
1731 case XDP_ABORTED:
1732 trace_xdp_exception(dev, xdp_prog, act);
1733 fallthrough;
1734 case XDP_DROP:
1735 return XDP_DROP;
1736 }
1737 }
1738
virtnet_get_headroom(struct virtnet_info * vi)1739 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1740 {
1741 return vi->xdp_enabled ? XDP_PACKET_HEADROOM : 0;
1742 }
1743
1744 /* We copy the packet for XDP in the following cases:
1745 *
1746 * 1) Packet is scattered across multiple rx buffers.
1747 * 2) Headroom space is insufficient.
1748 *
1749 * This is inefficient but it's a temporary condition that
1750 * we hit right after XDP is enabled and until queue is refilled
1751 * with large buffers with sufficient headroom - so it should affect
1752 * at most queue size packets.
1753 * Afterwards, the conditions to enable
1754 * XDP should preclude the underlying device from sending packets
1755 * across multiple buffers (num_buf > 1), and we make sure buffers
1756 * have enough headroom.
1757 */
xdp_linearize_page(struct net_device * dev,struct receive_queue * rq,int * num_buf,struct page * p,int offset,int page_off,unsigned int * len)1758 static struct page *xdp_linearize_page(struct net_device *dev,
1759 struct receive_queue *rq,
1760 int *num_buf,
1761 struct page *p,
1762 int offset,
1763 int page_off,
1764 unsigned int *len)
1765 {
1766 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1767 struct page *page;
1768
1769 if (page_off + *len + tailroom > PAGE_SIZE)
1770 return NULL;
1771
1772 page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
1773 if (!page)
1774 return NULL;
1775
1776 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1777 page_off += *len;
1778
1779 /* Only mergeable mode can go inside this while loop. In small mode,
1780 * *num_buf == 1, so it cannot go inside.
1781 */
1782 while (--*num_buf) {
1783 unsigned int buflen;
1784 void *buf;
1785 void *ctx;
1786 int off;
1787
1788 buf = virtnet_rq_get_buf(rq, &buflen, &ctx);
1789 if (unlikely(!buf))
1790 goto err_buf;
1791
1792 p = virt_to_head_page(buf);
1793 off = buf - page_address(p);
1794
1795 if (rq->use_page_pool_dma)
1796 page_pool_dma_sync_for_cpu(rq->page_pool, p,
1797 off, buflen);
1798
1799 if (check_mergeable_len(dev, ctx, buflen)) {
1800 page_pool_put_page(rq->page_pool, p, -1, true);
1801 goto err_buf;
1802 }
1803
1804 /* guard against a misconfigured or uncooperative backend that
1805 * is sending packet larger than the MTU.
1806 */
1807 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1808 page_pool_put_page(rq->page_pool, p, -1, true);
1809 goto err_buf;
1810 }
1811
1812 memcpy(page_address(page) + page_off,
1813 page_address(p) + off, buflen);
1814 page_off += buflen;
1815 page_pool_put_page(rq->page_pool, p, -1, true);
1816 }
1817
1818 /* Headroom does not contribute to packet length */
1819 *len = page_off - XDP_PACKET_HEADROOM;
1820 return page;
1821 err_buf:
1822 page_pool_put_page(rq->page_pool, page, -1, true);
1823 return NULL;
1824 }
1825
receive_small_build_skb(struct virtnet_info * vi,unsigned int xdp_headroom,void * buf,unsigned int len,unsigned int buflen)1826 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1827 unsigned int xdp_headroom,
1828 void *buf,
1829 unsigned int len,
1830 unsigned int buflen)
1831 {
1832 unsigned int header_offset;
1833 unsigned int headroom;
1834 struct sk_buff *skb;
1835
1836 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1837 headroom = vi->hdr_len + header_offset;
1838
1839 skb = virtnet_build_skb(buf, buflen, headroom, len);
1840 if (unlikely(!skb))
1841 return NULL;
1842
1843 buf += header_offset;
1844 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1845
1846 return skb;
1847 }
1848
receive_small_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,unsigned int xdp_headroom,unsigned int len,unsigned int buflen,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1849 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1850 struct virtnet_info *vi,
1851 struct receive_queue *rq,
1852 struct bpf_prog *xdp_prog,
1853 void *buf,
1854 unsigned int xdp_headroom,
1855 unsigned int len,
1856 unsigned int buflen,
1857 unsigned int *xdp_xmit,
1858 struct virtnet_rq_stats *stats)
1859 {
1860 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1861 unsigned int headroom = vi->hdr_len + header_offset;
1862 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1863 struct page *page = virt_to_head_page(buf);
1864 struct page *xdp_page;
1865 struct xdp_buff xdp;
1866 struct sk_buff *skb;
1867 unsigned int metasize = 0;
1868 u32 act;
1869
1870 if (unlikely(hdr->hdr.gso_type))
1871 goto err_xdp;
1872
1873 /* Partially checksummed packets must be dropped. */
1874 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1875 goto err_xdp;
1876
1877 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1878 int offset = buf - page_address(page) + header_offset;
1879 unsigned int tlen = len + vi->hdr_len;
1880 int num_buf = 1;
1881
1882 xdp_headroom = virtnet_get_headroom(vi);
1883 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1884 headroom = vi->hdr_len + header_offset;
1885 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1886 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1887 xdp_page = xdp_linearize_page(dev, rq, &num_buf, page,
1888 offset, header_offset,
1889 &tlen);
1890 if (!xdp_page)
1891 goto err_xdp;
1892
1893 buf = page_address(xdp_page);
1894 page_pool_put_page(rq->page_pool, page, -1, true);
1895 page = xdp_page;
1896 }
1897
1898 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1899 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1900 xdp_headroom, len, true);
1901
1902 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1903
1904 switch (act) {
1905 case XDP_PASS:
1906 /* Recalculate length in case bpf program changed it */
1907 len = xdp.data_end - xdp.data;
1908 metasize = xdp.data - xdp.data_meta;
1909 break;
1910
1911 case XDP_TX:
1912 case XDP_REDIRECT:
1913 goto xdp_xmit;
1914
1915 default:
1916 goto err_xdp;
1917 }
1918
1919 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1920 if (unlikely(!skb))
1921 goto err;
1922
1923 if (metasize)
1924 skb_metadata_set(skb, metasize);
1925
1926 skb_mark_for_recycle(skb);
1927
1928 return skb;
1929
1930 err_xdp:
1931 u64_stats_inc(&stats->xdp_drops);
1932 err:
1933 u64_stats_inc(&stats->drops);
1934 page_pool_put_page(rq->page_pool, page, -1, true);
1935 xdp_xmit:
1936 return NULL;
1937 }
1938
receive_small(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)1939 static struct sk_buff *receive_small(struct net_device *dev,
1940 struct virtnet_info *vi,
1941 struct receive_queue *rq,
1942 void *buf, void *ctx,
1943 unsigned int len,
1944 unsigned int *xdp_xmit,
1945 struct virtnet_rq_stats *stats)
1946 {
1947 unsigned int xdp_headroom = mergeable_ctx_to_headroom(ctx);
1948 unsigned int buflen = mergeable_ctx_to_truesize(ctx);
1949 struct page *page = virt_to_head_page(buf);
1950 struct sk_buff *skb;
1951
1952 /* We passed the address of virtnet header to virtio-core,
1953 * so truncate the padding.
1954 */
1955 buf -= VIRTNET_RX_PAD + xdp_headroom;
1956
1957 len -= vi->hdr_len;
1958 u64_stats_add(&stats->bytes, len);
1959
1960 if (unlikely(len > GOOD_PACKET_LEN)) {
1961 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1962 dev->name, len, GOOD_PACKET_LEN);
1963 DEV_STATS_INC(dev, rx_length_errors);
1964 goto err;
1965 }
1966
1967 if (unlikely(vi->xdp_enabled)) {
1968 struct bpf_prog *xdp_prog;
1969
1970 rcu_read_lock();
1971 xdp_prog = rcu_dereference(rq->xdp_prog);
1972 if (xdp_prog) {
1973 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1974 xdp_headroom, len, buflen,
1975 xdp_xmit, stats);
1976 rcu_read_unlock();
1977 return skb;
1978 }
1979 rcu_read_unlock();
1980 }
1981
1982 skb = receive_small_build_skb(vi, xdp_headroom, buf, len, buflen);
1983 if (likely(skb)) {
1984 skb_mark_for_recycle(skb);
1985 return skb;
1986 }
1987
1988 err:
1989 u64_stats_inc(&stats->drops);
1990 page_pool_put_page(rq->page_pool, page, -1, true);
1991 return NULL;
1992 }
1993
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)1994 static struct sk_buff *receive_big(struct net_device *dev,
1995 struct virtnet_info *vi,
1996 struct receive_queue *rq,
1997 void *buf,
1998 unsigned int len,
1999 struct virtnet_rq_stats *stats)
2000 {
2001 struct page *page = buf;
2002 struct sk_buff *skb;
2003
2004 /* Make sure that len does not exceed the size allocated in
2005 * add_recvbuf_big.
2006 */
2007 if (unlikely(len > (vi->big_packets_num_skbfrags + 1) * PAGE_SIZE)) {
2008 pr_debug("%s: rx error: len %u exceeds allocated size %lu\n",
2009 dev->name, len,
2010 (vi->big_packets_num_skbfrags + 1) * PAGE_SIZE);
2011 goto err;
2012 }
2013
2014 skb = page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
2015 u64_stats_add(&stats->bytes, len - vi->hdr_len);
2016 if (unlikely(!skb))
2017 goto err;
2018
2019 return skb;
2020
2021 err:
2022 u64_stats_inc(&stats->drops);
2023 give_pages(rq, page);
2024 return NULL;
2025 }
2026
mergeable_buf_free(struct receive_queue * rq,int num_buf,struct net_device * dev,struct virtnet_rq_stats * stats)2027 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
2028 struct net_device *dev,
2029 struct virtnet_rq_stats *stats)
2030 {
2031 struct page *page;
2032 void *buf;
2033 int len;
2034
2035 while (num_buf-- > 1) {
2036 buf = virtnet_rq_get_buf(rq, &len, NULL);
2037 if (unlikely(!buf)) {
2038 pr_debug("%s: rx error: %d buffers missing\n",
2039 dev->name, num_buf);
2040 DEV_STATS_INC(dev, rx_length_errors);
2041 break;
2042 }
2043 u64_stats_add(&stats->bytes, len);
2044 page = virt_to_head_page(buf);
2045 page_pool_put_page(rq->page_pool, page, -1, true);
2046 }
2047 }
2048
2049 /* Why not use xdp_build_skb_from_frame() ?
2050 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
2051 * virtio-net there are 2 points that do not match its requirements:
2052 * 1. The size of the prefilled buffer is not fixed before xdp is set.
2053 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
2054 * like eth_type_trans() (which virtio-net does in receive_buf()).
2055 */
build_skb_from_xdp_buff(struct net_device * dev,struct virtnet_info * vi,struct xdp_buff * xdp,unsigned int xdp_frags_truesz)2056 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
2057 struct virtnet_info *vi,
2058 struct xdp_buff *xdp,
2059 unsigned int xdp_frags_truesz)
2060 {
2061 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2062 unsigned int headroom, data_len;
2063 struct sk_buff *skb;
2064 int metasize;
2065 u8 nr_frags;
2066
2067 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
2068 pr_debug("Error building skb as missing reserved tailroom for xdp");
2069 return NULL;
2070 }
2071
2072 if (unlikely(xdp_buff_has_frags(xdp)))
2073 nr_frags = sinfo->nr_frags;
2074
2075 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
2076 if (unlikely(!skb))
2077 return NULL;
2078
2079 headroom = xdp->data - xdp->data_hard_start;
2080 data_len = xdp->data_end - xdp->data;
2081 skb_reserve(skb, headroom);
2082 __skb_put(skb, data_len);
2083
2084 metasize = xdp->data - xdp->data_meta;
2085 metasize = metasize > 0 ? metasize : 0;
2086 if (metasize)
2087 skb_metadata_set(skb, metasize);
2088
2089 if (unlikely(xdp_buff_has_frags(xdp)))
2090 xdp_update_skb_frags_info(skb, nr_frags, sinfo->xdp_frags_size,
2091 xdp_frags_truesz,
2092 xdp_buff_get_skb_flags(xdp));
2093
2094 return skb;
2095 }
2096
2097 /* TODO: build xdp in big mode */
virtnet_build_xdp_buff_mrg(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct xdp_buff * xdp,void * buf,unsigned int len,unsigned int frame_sz,int * num_buf,unsigned int * xdp_frags_truesize,struct virtnet_rq_stats * stats)2098 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
2099 struct virtnet_info *vi,
2100 struct receive_queue *rq,
2101 struct xdp_buff *xdp,
2102 void *buf,
2103 unsigned int len,
2104 unsigned int frame_sz,
2105 int *num_buf,
2106 unsigned int *xdp_frags_truesize,
2107 struct virtnet_rq_stats *stats)
2108 {
2109 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2110 struct skb_shared_info *shinfo;
2111 unsigned int xdp_frags_truesz = 0;
2112 unsigned int truesize;
2113 struct page *page;
2114 skb_frag_t *frag;
2115 int offset;
2116 void *ctx;
2117
2118 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
2119 xdp_prepare_buff(xdp, buf - XDP_PACKET_HEADROOM,
2120 XDP_PACKET_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
2121
2122 if (!*num_buf)
2123 return 0;
2124
2125 if (*num_buf > 1) {
2126 /* If we want to build multi-buffer xdp, we need
2127 * to specify that the flags of xdp_buff have the
2128 * XDP_FLAGS_HAS_FRAG bit.
2129 */
2130 if (!xdp_buff_has_frags(xdp))
2131 xdp_buff_set_frags_flag(xdp);
2132
2133 shinfo = xdp_get_shared_info_from_buff(xdp);
2134 shinfo->nr_frags = 0;
2135 shinfo->xdp_frags_size = 0;
2136 }
2137
2138 if (*num_buf > MAX_SKB_FRAGS + 1)
2139 return -EINVAL;
2140
2141 while (--*num_buf > 0) {
2142 buf = virtnet_rq_get_buf(rq, &len, &ctx);
2143 if (unlikely(!buf)) {
2144 pr_debug("%s: rx error: %d buffers out of %d missing\n",
2145 dev->name, *num_buf,
2146 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
2147 DEV_STATS_INC(dev, rx_length_errors);
2148 goto err;
2149 }
2150
2151 u64_stats_add(&stats->bytes, len);
2152 page = virt_to_head_page(buf);
2153 offset = buf - page_address(page);
2154
2155 if (rq->use_page_pool_dma)
2156 page_pool_dma_sync_for_cpu(rq->page_pool, page,
2157 offset, len);
2158
2159 if (check_mergeable_len(dev, ctx, len)) {
2160 page_pool_put_page(rq->page_pool, page, -1, true);
2161 goto err;
2162 }
2163
2164 truesize = mergeable_ctx_to_truesize(ctx);
2165 xdp_frags_truesz += truesize;
2166
2167 frag = &shinfo->frags[shinfo->nr_frags++];
2168 skb_frag_fill_page_desc(frag, page, offset, len);
2169 if (page_is_pfmemalloc(page))
2170 xdp_buff_set_frag_pfmemalloc(xdp);
2171
2172 shinfo->xdp_frags_size += len;
2173 }
2174
2175 *xdp_frags_truesize = xdp_frags_truesz;
2176 return 0;
2177
2178 err:
2179 put_xdp_frags(rq, xdp);
2180 return -EINVAL;
2181 }
2182
mergeable_xdp_get_buf(struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * ctx,unsigned int * frame_sz,int * num_buf,struct page ** page,int offset,unsigned int * len,struct virtio_net_hdr_mrg_rxbuf * hdr)2183 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
2184 struct receive_queue *rq,
2185 struct bpf_prog *xdp_prog,
2186 void *ctx,
2187 unsigned int *frame_sz,
2188 int *num_buf,
2189 struct page **page,
2190 int offset,
2191 unsigned int *len,
2192 struct virtio_net_hdr_mrg_rxbuf *hdr)
2193 {
2194 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2195 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2196 struct page *xdp_page;
2197 unsigned int xdp_room;
2198
2199 /* Transient failure which in theory could occur if
2200 * in-flight packets from before XDP was enabled reach
2201 * the receive path after XDP is loaded.
2202 */
2203 if (unlikely(hdr->hdr.gso_type))
2204 return NULL;
2205
2206 /* Partially checksummed packets must be dropped. */
2207 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
2208 return NULL;
2209
2210 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
2211 * with headroom may add hole in truesize, which
2212 * make their length exceed PAGE_SIZE. So we disabled the
2213 * hole mechanism for xdp. See add_recvbuf_mergeable().
2214 */
2215 *frame_sz = truesize;
2216
2217 if (likely(headroom >= virtnet_get_headroom(vi) &&
2218 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
2219 return page_address(*page) + offset;
2220 }
2221
2222 /* This happens when headroom is not enough because
2223 * of the buffer was prefilled before XDP is set.
2224 * This should only happen for the first several packets.
2225 * In fact, vq reset can be used here to help us clean up
2226 * the prefilled buffers, but many existing devices do not
2227 * support it, and we don't want to bother users who are
2228 * using xdp normally.
2229 */
2230 if (!xdp_prog->aux->xdp_has_frags) {
2231 /* linearize data for XDP */
2232 xdp_page = xdp_linearize_page(vi->dev, rq, num_buf,
2233 *page, offset,
2234 XDP_PACKET_HEADROOM,
2235 len);
2236 if (!xdp_page)
2237 return NULL;
2238 } else {
2239 xdp_room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
2240 sizeof(struct skb_shared_info));
2241 if (*len + xdp_room > PAGE_SIZE)
2242 return NULL;
2243
2244 xdp_page = page_pool_alloc_pages(rq->page_pool, GFP_ATOMIC);
2245 if (!xdp_page)
2246 return NULL;
2247
2248 memcpy(page_address(xdp_page) + XDP_PACKET_HEADROOM,
2249 page_address(*page) + offset, *len);
2250 }
2251
2252 *frame_sz = PAGE_SIZE;
2253
2254 page_pool_put_page(rq->page_pool, *page, -1, true);
2255
2256 *page = xdp_page;
2257
2258 return page_address(*page) + XDP_PACKET_HEADROOM;
2259 }
2260
receive_mergeable_xdp(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,struct bpf_prog * xdp_prog,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2261 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
2262 struct virtnet_info *vi,
2263 struct receive_queue *rq,
2264 struct bpf_prog *xdp_prog,
2265 void *buf,
2266 void *ctx,
2267 unsigned int len,
2268 unsigned int *xdp_xmit,
2269 struct virtnet_rq_stats *stats)
2270 {
2271 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2272 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2273 struct page *page = virt_to_head_page(buf);
2274 int offset = buf - page_address(page);
2275 unsigned int xdp_frags_truesz = 0;
2276 struct sk_buff *head_skb;
2277 unsigned int frame_sz;
2278 struct xdp_buff xdp;
2279 void *data;
2280 u32 act;
2281 int err;
2282
2283 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
2284 offset, &len, hdr);
2285 if (unlikely(!data))
2286 goto err_xdp;
2287
2288 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
2289 &num_buf, &xdp_frags_truesz, stats);
2290 if (unlikely(err))
2291 goto err_xdp;
2292
2293 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
2294
2295 switch (act) {
2296 case XDP_PASS:
2297 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
2298 if (unlikely(!head_skb))
2299 break;
2300
2301 skb_mark_for_recycle(head_skb);
2302 return head_skb;
2303
2304 case XDP_TX:
2305 case XDP_REDIRECT:
2306 return NULL;
2307
2308 default:
2309 break;
2310 }
2311
2312 put_xdp_frags(rq, &xdp);
2313
2314 err_xdp:
2315 page_pool_put_page(rq->page_pool, page, -1, true);
2316 mergeable_buf_free(rq, num_buf, dev, stats);
2317
2318 u64_stats_inc(&stats->xdp_drops);
2319 u64_stats_inc(&stats->drops);
2320 return NULL;
2321 }
2322
virtnet_skb_append_frag(struct receive_queue * rq,struct sk_buff * head_skb,struct sk_buff * curr_skb,struct page * page,void * buf,int len,int truesize)2323 static struct sk_buff *virtnet_skb_append_frag(struct receive_queue *rq,
2324 struct sk_buff *head_skb,
2325 struct sk_buff *curr_skb,
2326 struct page *page, void *buf,
2327 int len, int truesize)
2328 {
2329 int num_skb_frags;
2330 int offset;
2331
2332 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
2333 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
2334 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
2335
2336 if (unlikely(!nskb))
2337 return NULL;
2338
2339 if (head_skb->pp_recycle)
2340 skb_mark_for_recycle(nskb);
2341
2342 if (curr_skb == head_skb)
2343 skb_shinfo(curr_skb)->frag_list = nskb;
2344 else
2345 curr_skb->next = nskb;
2346 curr_skb = nskb;
2347 head_skb->truesize += nskb->truesize;
2348 num_skb_frags = 0;
2349 }
2350
2351 if (curr_skb != head_skb) {
2352 head_skb->data_len += len;
2353 head_skb->len += len;
2354 head_skb->truesize += truesize;
2355 }
2356
2357 offset = buf - page_address(page);
2358 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
2359 if (head_skb->pp_recycle)
2360 page_pool_put_page(rq->page_pool, page, -1, true);
2361 else
2362 put_page(page);
2363 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
2364 len, truesize);
2365 } else {
2366 skb_add_rx_frag(curr_skb, num_skb_frags, page,
2367 offset, len, truesize);
2368 }
2369
2370 return curr_skb;
2371 }
2372
receive_mergeable(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,void * ctx,unsigned int len,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2373 static struct sk_buff *receive_mergeable(struct net_device *dev,
2374 struct virtnet_info *vi,
2375 struct receive_queue *rq,
2376 void *buf,
2377 void *ctx,
2378 unsigned int len,
2379 unsigned int *xdp_xmit,
2380 struct virtnet_rq_stats *stats)
2381 {
2382 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
2383 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
2384 struct page *page = virt_to_head_page(buf);
2385 int offset = buf - page_address(page);
2386 struct sk_buff *head_skb, *curr_skb;
2387 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
2388 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
2389
2390 head_skb = NULL;
2391
2392 u64_stats_add(&stats->bytes, len - vi->hdr_len);
2393
2394 if (check_mergeable_len(dev, ctx, len))
2395 goto err_skb;
2396
2397 if (unlikely(vi->xdp_enabled)) {
2398 struct bpf_prog *xdp_prog;
2399
2400 rcu_read_lock();
2401 xdp_prog = rcu_dereference(rq->xdp_prog);
2402 if (xdp_prog) {
2403 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
2404 len, xdp_xmit, stats);
2405 rcu_read_unlock();
2406 return head_skb;
2407 }
2408 rcu_read_unlock();
2409 }
2410
2411 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
2412 curr_skb = head_skb;
2413
2414 if (unlikely(!curr_skb))
2415 goto err_skb;
2416
2417 skb_mark_for_recycle(head_skb);
2418 while (--num_buf) {
2419 buf = virtnet_rq_get_buf(rq, &len, &ctx);
2420 if (unlikely(!buf)) {
2421 pr_debug("%s: rx error: %d buffers out of %d missing\n",
2422 dev->name, num_buf,
2423 virtio16_to_cpu(vi->vdev,
2424 hdr->num_buffers));
2425 DEV_STATS_INC(dev, rx_length_errors);
2426 goto err_buf;
2427 }
2428
2429 u64_stats_add(&stats->bytes, len);
2430 page = virt_to_head_page(buf);
2431
2432 if (rq->use_page_pool_dma) {
2433 offset = buf - page_address(page);
2434 page_pool_dma_sync_for_cpu(rq->page_pool, page,
2435 offset, len);
2436 }
2437
2438 if (check_mergeable_len(dev, ctx, len))
2439 goto err_skb;
2440
2441 truesize = mergeable_ctx_to_truesize(ctx);
2442 curr_skb = virtnet_skb_append_frag(rq, head_skb, curr_skb, page,
2443 buf, len, truesize);
2444 if (!curr_skb)
2445 goto err_skb;
2446 }
2447
2448 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
2449 return head_skb;
2450
2451 err_skb:
2452 page_pool_put_page(rq->page_pool, page, -1, true);
2453 mergeable_buf_free(rq, num_buf, dev, stats);
2454
2455 err_buf:
2456 u64_stats_inc(&stats->drops);
2457 dev_kfree_skb(head_skb);
2458 return NULL;
2459 }
2460
2461 static inline u32
virtio_net_hash_value(const struct virtio_net_hdr_v1_hash * hdr_hash)2462 virtio_net_hash_value(const struct virtio_net_hdr_v1_hash *hdr_hash)
2463 {
2464 return __le16_to_cpu(hdr_hash->hash_value_lo) |
2465 (__le16_to_cpu(hdr_hash->hash_value_hi) << 16);
2466 }
2467
virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash * hdr_hash,struct sk_buff * skb)2468 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
2469 struct sk_buff *skb)
2470 {
2471 enum pkt_hash_types rss_hash_type;
2472
2473 if (!hdr_hash || !skb)
2474 return;
2475
2476 switch (__le16_to_cpu(hdr_hash->hash_report)) {
2477 case VIRTIO_NET_HASH_REPORT_TCPv4:
2478 case VIRTIO_NET_HASH_REPORT_UDPv4:
2479 case VIRTIO_NET_HASH_REPORT_TCPv6:
2480 case VIRTIO_NET_HASH_REPORT_UDPv6:
2481 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
2482 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
2483 rss_hash_type = PKT_HASH_TYPE_L4;
2484 break;
2485 case VIRTIO_NET_HASH_REPORT_IPv4:
2486 case VIRTIO_NET_HASH_REPORT_IPv6:
2487 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
2488 rss_hash_type = PKT_HASH_TYPE_L3;
2489 break;
2490 case VIRTIO_NET_HASH_REPORT_NONE:
2491 default:
2492 rss_hash_type = PKT_HASH_TYPE_NONE;
2493 }
2494 skb_set_hash(skb, virtio_net_hash_value(hdr_hash), rss_hash_type);
2495 }
2496
virtnet_receive_done(struct virtnet_info * vi,struct receive_queue * rq,struct sk_buff * skb,u8 flags)2497 static void virtnet_receive_done(struct virtnet_info *vi, struct receive_queue *rq,
2498 struct sk_buff *skb, u8 flags)
2499 {
2500 struct virtio_net_common_hdr *hdr;
2501 struct net_device *dev = vi->dev;
2502
2503 hdr = skb_vnet_common_hdr(skb);
2504 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
2505 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
2506
2507 hdr->hdr.flags = flags;
2508 if (virtio_net_handle_csum_offload(skb, &hdr->hdr, vi->rx_tnl_csum)) {
2509 net_warn_ratelimited("%s: bad csum: flags: %x, gso_type: %x rx_tnl_csum %d\n",
2510 dev->name, hdr->hdr.flags,
2511 hdr->hdr.gso_type, vi->rx_tnl_csum);
2512 goto frame_err;
2513 }
2514
2515 if (virtio_net_hdr_tnl_to_skb(skb, &hdr->tnl_hdr, vi->rx_tnl,
2516 vi->rx_tnl_csum,
2517 virtio_is_little_endian(vi->vdev))) {
2518 net_warn_ratelimited("%s: bad gso: type: %x, size: %u, flags %x tunnel %d tnl csum %d\n",
2519 dev->name, hdr->hdr.gso_type,
2520 hdr->hdr.gso_size, hdr->hdr.flags,
2521 vi->rx_tnl, vi->rx_tnl_csum);
2522 goto frame_err;
2523 }
2524
2525 skb_record_rx_queue(skb, vq2rxq(rq->vq));
2526 skb->protocol = eth_type_trans(skb, dev);
2527 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
2528 ntohs(skb->protocol), skb->len, skb->pkt_type);
2529
2530 napi_gro_receive(&rq->napi, skb);
2531 return;
2532
2533 frame_err:
2534 DEV_STATS_INC(dev, rx_frame_errors);
2535 dev_kfree_skb(skb);
2536 }
2537
receive_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,void ** ctx,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2538 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
2539 void *buf, unsigned int len, void **ctx,
2540 unsigned int *xdp_xmit,
2541 struct virtnet_rq_stats *stats)
2542 {
2543 struct net_device *dev = vi->dev;
2544 struct sk_buff *skb;
2545 u8 flags;
2546
2547 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
2548 pr_debug("%s: short packet %i\n", dev->name, len);
2549 DEV_STATS_INC(dev, rx_length_errors);
2550 virtnet_rq_free_buf(vi, rq, buf);
2551 return;
2552 }
2553
2554 /* Sync the memory before touching anything through buf,
2555 * unless virtio core did it already.
2556 */
2557 if (rq->use_page_pool_dma) {
2558 struct page *page = virt_to_head_page(buf);
2559 int offset = buf - page_address(page);
2560
2561 page_pool_dma_sync_for_cpu(rq->page_pool, page, offset, len);
2562 }
2563
2564 /* About the flags below:
2565 * 1. Save the flags early, as the XDP program might overwrite them.
2566 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
2567 * stay valid after XDP processing.
2568 * 2. XDP doesn't work with partially checksummed packets (refer to
2569 * virtnet_xdp_set()), so packets marked as
2570 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
2571 */
2572
2573 if (vi->mergeable_rx_bufs) {
2574 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
2575 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
2576 stats);
2577 } else if (vi->big_packets) {
2578 void *p = page_address((struct page *)buf);
2579
2580 flags = ((struct virtio_net_common_hdr *)p)->hdr.flags;
2581 skb = receive_big(dev, vi, rq, buf, len, stats);
2582 } else {
2583 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
2584 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
2585 }
2586
2587 if (unlikely(!skb))
2588 return;
2589
2590 virtnet_receive_done(vi, rq, skb, flags);
2591 }
2592
virtnet_rq_submit(struct receive_queue * rq,char * buf,int len,void * ctx,gfp_t gfp)2593 static int virtnet_rq_submit(struct receive_queue *rq, char *buf,
2594 int len, void *ctx, gfp_t gfp)
2595 {
2596 if (rq->use_page_pool_dma) {
2597 struct page *page = virt_to_head_page(buf);
2598 dma_addr_t addr = page_pool_get_dma_addr(page) +
2599 (buf - (char *)page_address(page));
2600
2601 sg_init_table(rq->sg, 1);
2602 sg_fill_dma(rq->sg, addr, len);
2603 return virtqueue_add_inbuf_premapped(rq->vq, rq->sg, 1,
2604 buf, ctx, gfp);
2605 }
2606
2607 sg_init_one(rq->sg, buf, len);
2608 return virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2609 }
2610
2611 /* With page_pool, the actual allocation may exceed the requested size
2612 * when the remaining page fragment can't fit another buffer. Encode
2613 * the actual allocation size in ctx so build_skb() gets the correct
2614 * buflen for truesize accounting.
2615 */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2616 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
2617 gfp_t gfp)
2618 {
2619 unsigned int xdp_headroom = virtnet_get_headroom(vi);
2620 unsigned int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
2621 unsigned int alloc_len;
2622 char *buf;
2623 void *ctx;
2624 int err;
2625
2626 len = SKB_DATA_ALIGN(len) +
2627 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2628
2629 alloc_len = len;
2630 buf = page_pool_alloc_va(rq->page_pool, &alloc_len, gfp);
2631 if (unlikely(!buf))
2632 return -ENOMEM;
2633
2634 buf += VIRTNET_RX_PAD + xdp_headroom;
2635
2636 ctx = mergeable_len_to_ctx(alloc_len, xdp_headroom);
2637 err = virtnet_rq_submit(rq, buf, vi->hdr_len + GOOD_PACKET_LEN, ctx, gfp);
2638
2639 if (err < 0)
2640 page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
2641 return err;
2642 }
2643
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2644 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
2645 gfp_t gfp)
2646 {
2647 struct page *first, *list = NULL;
2648 char *p;
2649 int i, err, offset;
2650
2651 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
2652
2653 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
2654 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
2655 first = get_a_page(rq, gfp);
2656 if (!first) {
2657 if (list)
2658 give_pages(rq, list);
2659 return -ENOMEM;
2660 }
2661 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
2662
2663 /* chain new page in list head to match sg */
2664 first->private = (unsigned long)list;
2665 list = first;
2666 }
2667
2668 first = get_a_page(rq, gfp);
2669 if (!first) {
2670 give_pages(rq, list);
2671 return -ENOMEM;
2672 }
2673 p = page_address(first);
2674
2675 /* rq->sg[0], rq->sg[1] share the same page */
2676 /* a separated rq->sg[0] for header - required in case !any_header_sg */
2677 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
2678
2679 /* rq->sg[1] for data packet, from offset */
2680 offset = sizeof(struct padded_vnet_hdr);
2681 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
2682
2683 /* chain first in list head */
2684 first->private = (unsigned long)list;
2685 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
2686 first, gfp);
2687 if (err < 0)
2688 give_pages(rq, first);
2689
2690 return err;
2691 }
2692
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)2693 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
2694 struct ewma_pkt_len *avg_pkt_len,
2695 unsigned int room)
2696 {
2697 struct virtnet_info *vi = rq->vq->vdev->priv;
2698 const size_t hdr_len = vi->hdr_len;
2699 unsigned int len;
2700
2701 if (room)
2702 return PAGE_SIZE - room;
2703
2704 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
2705 rq->min_buf_len, PAGE_SIZE - hdr_len);
2706
2707 return ALIGN(len, L1_CACHE_BYTES);
2708 }
2709
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2710 static int add_recvbuf_mergeable(struct virtnet_info *vi,
2711 struct receive_queue *rq, gfp_t gfp)
2712 {
2713 unsigned int headroom = virtnet_get_headroom(vi);
2714 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
2715 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
2716 unsigned int len, alloc_len;
2717 char *buf;
2718 void *ctx;
2719 int err;
2720
2721 /* Extra tailroom is needed to satisfy XDP's assumption. This
2722 * means rx frags coalescing won't work, but consider we've
2723 * disabled GSO for XDP, it won't be a big issue.
2724 */
2725 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
2726
2727 alloc_len = len + room;
2728 buf = page_pool_alloc_va(rq->page_pool, &alloc_len, gfp);
2729 if (unlikely(!buf))
2730 return -ENOMEM;
2731
2732 buf += headroom; /* advance address leaving hole at front of pkt */
2733
2734 if (!headroom)
2735 len = alloc_len - room;
2736
2737 ctx = mergeable_len_to_ctx(len + room, headroom);
2738
2739 err = virtnet_rq_submit(rq, buf, len, ctx, gfp);
2740
2741 if (err < 0)
2742 page_pool_put_page(rq->page_pool, virt_to_head_page(buf), -1, false);
2743 return err;
2744 }
2745
2746 /*
2747 * Returns false if we couldn't fill entirely (OOM) and need to retry.
2748 * In XSK mode, it's when the receive buffer is not allocated and
2749 * xsk_use_need_wakeup is not set.
2750 *
2751 * Normally run in the receive path, but can also be run from ndo_open
2752 * before we're receiving packets, or from refill_work which is
2753 * careful to disable receiving (using napi_disable).
2754 */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2755 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2756 gfp_t gfp)
2757 {
2758 int err;
2759
2760 if (rq->xsk_pool) {
2761 err = virtnet_add_recvbuf_xsk(vi, rq, rq->xsk_pool, gfp);
2762 goto kick;
2763 }
2764
2765 do {
2766 if (vi->mergeable_rx_bufs)
2767 err = add_recvbuf_mergeable(vi, rq, gfp);
2768 else if (vi->big_packets)
2769 err = add_recvbuf_big(vi, rq, gfp);
2770 else
2771 err = add_recvbuf_small(vi, rq, gfp);
2772
2773 if (err)
2774 break;
2775 } while (rq->vq->num_free);
2776
2777 kick:
2778 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2779 unsigned long flags;
2780
2781 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2782 u64_stats_inc(&rq->stats.kicks);
2783 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2784 }
2785
2786 return err != -ENOMEM;
2787 }
2788
skb_recv_done(struct virtqueue * rvq)2789 static void skb_recv_done(struct virtqueue *rvq)
2790 {
2791 struct virtnet_info *vi = rvq->vdev->priv;
2792 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2793
2794 rq->calls++;
2795 virtqueue_napi_schedule(&rq->napi, rvq);
2796 }
2797
virtnet_napi_do_enable(struct virtqueue * vq,struct napi_struct * napi)2798 static void virtnet_napi_do_enable(struct virtqueue *vq,
2799 struct napi_struct *napi)
2800 {
2801 napi_enable(napi);
2802
2803 /* If all buffers were filled by other side before we napi_enabled, we
2804 * won't get another interrupt, so process any outstanding packets now.
2805 * Call local_bh_enable after to trigger softIRQ processing.
2806 */
2807 local_bh_disable();
2808 virtqueue_napi_schedule(napi, vq);
2809 local_bh_enable();
2810 }
2811
virtnet_napi_enable(struct receive_queue * rq)2812 static void virtnet_napi_enable(struct receive_queue *rq)
2813 {
2814 struct virtnet_info *vi = rq->vq->vdev->priv;
2815 int qidx = vq2rxq(rq->vq);
2816
2817 virtnet_napi_do_enable(rq->vq, &rq->napi);
2818 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, &rq->napi);
2819 }
2820
virtnet_napi_tx_enable(struct send_queue * sq)2821 static void virtnet_napi_tx_enable(struct send_queue *sq)
2822 {
2823 struct virtnet_info *vi = sq->vq->vdev->priv;
2824 struct napi_struct *napi = &sq->napi;
2825 int qidx = vq2txq(sq->vq);
2826
2827 if (!napi->weight)
2828 return;
2829
2830 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2831 * enable the feature if this is likely affine with the transmit path.
2832 */
2833 if (!vi->affinity_hint_set) {
2834 napi->weight = 0;
2835 return;
2836 }
2837
2838 virtnet_napi_do_enable(sq->vq, napi);
2839 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, napi);
2840 }
2841
virtnet_napi_tx_disable(struct send_queue * sq)2842 static void virtnet_napi_tx_disable(struct send_queue *sq)
2843 {
2844 struct virtnet_info *vi = sq->vq->vdev->priv;
2845 struct napi_struct *napi = &sq->napi;
2846 int qidx = vq2txq(sq->vq);
2847
2848 if (napi->weight) {
2849 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_TX, NULL);
2850 napi_disable(napi);
2851 }
2852 }
2853
virtnet_napi_disable(struct receive_queue * rq)2854 static void virtnet_napi_disable(struct receive_queue *rq)
2855 {
2856 struct virtnet_info *vi = rq->vq->vdev->priv;
2857 struct napi_struct *napi = &rq->napi;
2858 int qidx = vq2rxq(rq->vq);
2859
2860 netif_queue_set_napi(vi->dev, qidx, NETDEV_QUEUE_TYPE_RX, NULL);
2861 napi_disable(napi);
2862 }
2863
virtnet_receive_xsk_bufs(struct virtnet_info * vi,struct receive_queue * rq,int budget,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2864 static int virtnet_receive_xsk_bufs(struct virtnet_info *vi,
2865 struct receive_queue *rq,
2866 int budget,
2867 unsigned int *xdp_xmit,
2868 struct virtnet_rq_stats *stats)
2869 {
2870 unsigned int len;
2871 int packets = 0;
2872 void *buf;
2873
2874 while (packets < budget) {
2875 buf = virtqueue_get_buf(rq->vq, &len);
2876 if (!buf)
2877 break;
2878
2879 virtnet_receive_xsk_buf(vi, rq, buf, len, xdp_xmit, stats);
2880 packets++;
2881 }
2882
2883 return packets;
2884 }
2885
virtnet_receive_packets(struct virtnet_info * vi,struct receive_queue * rq,int budget,unsigned int * xdp_xmit,struct virtnet_rq_stats * stats)2886 static int virtnet_receive_packets(struct virtnet_info *vi,
2887 struct receive_queue *rq,
2888 int budget,
2889 unsigned int *xdp_xmit,
2890 struct virtnet_rq_stats *stats)
2891 {
2892 unsigned int len;
2893 int packets = 0;
2894 void *buf;
2895
2896 if (rq->page_pool) {
2897 void *ctx;
2898 while (packets < budget &&
2899 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2900 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, stats);
2901 packets++;
2902 }
2903 } else {
2904 while (packets < budget &&
2905 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
2906 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, stats);
2907 packets++;
2908 }
2909 }
2910
2911 return packets;
2912 }
2913
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)2914 static int virtnet_receive(struct receive_queue *rq, int budget,
2915 unsigned int *xdp_xmit)
2916 {
2917 struct virtnet_info *vi = rq->vq->vdev->priv;
2918 struct virtnet_rq_stats stats = {};
2919 int i, packets;
2920
2921 if (rq->xsk_pool)
2922 packets = virtnet_receive_xsk_bufs(vi, rq, budget, xdp_xmit, &stats);
2923 else
2924 packets = virtnet_receive_packets(vi, rq, budget, xdp_xmit, &stats);
2925
2926 u64_stats_set(&stats.packets, packets);
2927 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2928 if (!try_fill_recv(vi, rq, GFP_ATOMIC))
2929 /* We need to retry refilling in the next NAPI poll so
2930 * we must return budget to make sure the NAPI is
2931 * repolled.
2932 */
2933 packets = budget;
2934 }
2935
2936 u64_stats_update_begin(&rq->stats.syncp);
2937 for (i = 0; i < ARRAY_SIZE(virtnet_rq_stats_desc); i++) {
2938 size_t offset = virtnet_rq_stats_desc[i].offset;
2939 u64_stats_t *item, *src;
2940
2941 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2942 src = (u64_stats_t *)((u8 *)&stats + offset);
2943 u64_stats_add(item, u64_stats_read(src));
2944 }
2945
2946 u64_stats_add(&rq->stats.packets, u64_stats_read(&stats.packets));
2947 u64_stats_add(&rq->stats.bytes, u64_stats_read(&stats.bytes));
2948
2949 u64_stats_update_end(&rq->stats.syncp);
2950
2951 return packets;
2952 }
2953
virtnet_poll_cleantx(struct receive_queue * rq,int budget)2954 static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
2955 {
2956 struct virtnet_info *vi = rq->vq->vdev->priv;
2957 unsigned int index = vq2rxq(rq->vq);
2958 struct send_queue *sq = &vi->sq[index];
2959 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2960
2961 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2962 return;
2963
2964 if (__netif_tx_trylock(txq)) {
2965 if (sq->reset) {
2966 __netif_tx_unlock(txq);
2967 return;
2968 }
2969
2970 do {
2971 virtqueue_disable_cb(sq->vq);
2972 free_old_xmit(sq, txq, !!budget);
2973 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2974
2975 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2)
2976 virtnet_tx_wake_queue(vi, sq);
2977
2978 __netif_tx_unlock(txq);
2979 }
2980 }
2981
virtnet_rx_dim_update(struct virtnet_info * vi,struct receive_queue * rq)2982 static void virtnet_rx_dim_update(struct virtnet_info *vi, struct receive_queue *rq)
2983 {
2984 struct dim_sample cur_sample = {};
2985
2986 if (!rq->packets_in_napi)
2987 return;
2988
2989 /* Don't need protection when fetching stats, since fetcher and
2990 * updater of the stats are in same context
2991 */
2992 dim_update_sample(rq->calls,
2993 u64_stats_read(&rq->stats.packets),
2994 u64_stats_read(&rq->stats.bytes),
2995 &cur_sample);
2996
2997 net_dim(&rq->dim, &cur_sample);
2998 rq->packets_in_napi = 0;
2999 }
3000
virtnet_poll(struct napi_struct * napi,int budget)3001 static int virtnet_poll(struct napi_struct *napi, int budget)
3002 {
3003 struct receive_queue *rq =
3004 container_of(napi, struct receive_queue, napi);
3005 struct virtnet_info *vi = rq->vq->vdev->priv;
3006 struct send_queue *sq;
3007 unsigned int received;
3008 unsigned int xdp_xmit = 0;
3009 bool napi_complete;
3010
3011 virtnet_poll_cleantx(rq, budget);
3012
3013 received = virtnet_receive(rq, budget, &xdp_xmit);
3014 rq->packets_in_napi += received;
3015
3016 if (xdp_xmit & VIRTIO_XDP_REDIR)
3017 xdp_do_flush();
3018
3019 /* Out of packets? */
3020 if (received < budget) {
3021 napi_complete = virtqueue_napi_complete(napi, rq->vq, received);
3022 /* Intentionally not taking dim_lock here. This may result in a
3023 * spurious net_dim call. But if that happens virtnet_rx_dim_work
3024 * will not act on the scheduled work.
3025 */
3026 if (napi_complete && rq->dim_enabled)
3027 virtnet_rx_dim_update(vi, rq);
3028 }
3029
3030 if (xdp_xmit & VIRTIO_XDP_TX) {
3031 sq = virtnet_xdp_get_sq(vi);
3032 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
3033 u64_stats_update_begin(&sq->stats.syncp);
3034 u64_stats_inc(&sq->stats.kicks);
3035 u64_stats_update_end(&sq->stats.syncp);
3036 }
3037 virtnet_xdp_put_sq(vi, sq);
3038 }
3039
3040 return received;
3041 }
3042
virtnet_disable_queue_pair(struct virtnet_info * vi,int qp_index)3043 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
3044 {
3045 virtnet_napi_tx_disable(&vi->sq[qp_index]);
3046 virtnet_napi_disable(&vi->rq[qp_index]);
3047 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
3048 }
3049
virtnet_enable_queue_pair(struct virtnet_info * vi,int qp_index)3050 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
3051 {
3052 struct net_device *dev = vi->dev;
3053 int err;
3054
3055 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
3056 vi->rq[qp_index].napi.napi_id);
3057 if (err < 0)
3058 return err;
3059
3060 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
3061 vi->rq[qp_index].page_pool ?
3062 MEM_TYPE_PAGE_POOL :
3063 MEM_TYPE_PAGE_SHARED,
3064 vi->rq[qp_index].page_pool);
3065 if (err < 0)
3066 goto err_xdp_reg_mem_model;
3067
3068 virtnet_napi_enable(&vi->rq[qp_index]);
3069 virtnet_napi_tx_enable(&vi->sq[qp_index]);
3070
3071 return 0;
3072
3073 err_xdp_reg_mem_model:
3074 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
3075 return err;
3076 }
3077
virtnet_cancel_dim(struct virtnet_info * vi,struct dim * dim)3078 static void virtnet_cancel_dim(struct virtnet_info *vi, struct dim *dim)
3079 {
3080 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3081 return;
3082 net_dim_work_cancel(dim);
3083 }
3084
virtnet_update_settings(struct virtnet_info * vi)3085 static void virtnet_update_settings(struct virtnet_info *vi)
3086 {
3087 u32 speed;
3088 u8 duplex;
3089
3090 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3091 return;
3092
3093 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3094
3095 if (ethtool_validate_speed(speed))
3096 vi->speed = speed;
3097
3098 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3099
3100 if (ethtool_validate_duplex(duplex))
3101 vi->duplex = duplex;
3102 }
3103
virtnet_create_page_pools(struct virtnet_info * vi)3104 static int virtnet_create_page_pools(struct virtnet_info *vi)
3105 {
3106 int i, err;
3107
3108 if (vi->big_packets && !vi->mergeable_rx_bufs)
3109 return 0;
3110
3111 for (i = 0; i < vi->max_queue_pairs; i++) {
3112 struct receive_queue *rq = &vi->rq[i];
3113 struct page_pool_params pp_params = { 0 };
3114 struct device *dma_dev;
3115
3116 if (rq->page_pool)
3117 continue;
3118
3119 if (rq->xsk_pool)
3120 continue;
3121
3122 pp_params.order = 0;
3123 pp_params.pool_size = virtqueue_get_vring_size(rq->vq);
3124 pp_params.nid = dev_to_node(vi->vdev->dev.parent);
3125 pp_params.netdev = vi->dev;
3126 pp_params.napi = &rq->napi;
3127
3128 /* Use page_pool DMA mapping if backend supports DMA API.
3129 * DMA_SYNC_DEV is needed for non-coherent archs on recycle.
3130 */
3131 dma_dev = virtqueue_dma_dev(rq->vq);
3132 if (dma_dev) {
3133 pp_params.dev = dma_dev;
3134 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3135 pp_params.dma_dir = DMA_FROM_DEVICE;
3136 pp_params.max_len = PAGE_SIZE;
3137 pp_params.offset = 0;
3138 rq->use_page_pool_dma = true;
3139 } else {
3140 /* No DMA API (e.g., VDUSE): page_pool for allocation only. */
3141 pp_params.flags = 0;
3142 rq->use_page_pool_dma = false;
3143 }
3144
3145 rq->page_pool = page_pool_create(&pp_params);
3146 if (IS_ERR(rq->page_pool)) {
3147 err = PTR_ERR(rq->page_pool);
3148 rq->page_pool = NULL;
3149 goto err_cleanup;
3150 }
3151 }
3152 return 0;
3153
3154 err_cleanup:
3155 while (--i >= 0) {
3156 struct receive_queue *rq = &vi->rq[i];
3157
3158 if (rq->page_pool) {
3159 page_pool_destroy(rq->page_pool);
3160 rq->page_pool = NULL;
3161 }
3162 }
3163 return err;
3164 }
3165
virtnet_destroy_page_pools(struct virtnet_info * vi)3166 static void virtnet_destroy_page_pools(struct virtnet_info *vi)
3167 {
3168 int i;
3169
3170 for (i = 0; i < vi->max_queue_pairs; i++) {
3171 struct receive_queue *rq = &vi->rq[i];
3172
3173 if (rq->page_pool) {
3174 page_pool_destroy(rq->page_pool);
3175 rq->page_pool = NULL;
3176 }
3177 }
3178 }
3179
virtnet_open(struct net_device * dev)3180 static int virtnet_open(struct net_device *dev)
3181 {
3182 struct virtnet_info *vi = netdev_priv(dev);
3183 int i, err;
3184
3185 for (i = 0; i < vi->max_queue_pairs; i++) {
3186 if (i < vi->curr_queue_pairs)
3187 /* Pre-fill rq agressively, to make sure we are ready to
3188 * get packets immediately.
3189 */
3190 try_fill_recv(vi, &vi->rq[i], GFP_KERNEL);
3191
3192 err = virtnet_enable_queue_pair(vi, i);
3193 if (err < 0)
3194 goto err_enable_qp;
3195 }
3196
3197 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
3198 if (vi->status & VIRTIO_NET_S_LINK_UP)
3199 netif_carrier_on(vi->dev);
3200 virtio_config_driver_enable(vi->vdev);
3201 } else {
3202 vi->status = VIRTIO_NET_S_LINK_UP;
3203 netif_carrier_on(dev);
3204 }
3205
3206 return 0;
3207
3208 err_enable_qp:
3209 for (i--; i >= 0; i--) {
3210 virtnet_disable_queue_pair(vi, i);
3211 virtnet_cancel_dim(vi, &vi->rq[i].dim);
3212 }
3213
3214 return err;
3215 }
3216
virtnet_poll_tx(struct napi_struct * napi,int budget)3217 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
3218 {
3219 struct send_queue *sq = container_of(napi, struct send_queue, napi);
3220 struct virtnet_info *vi = sq->vq->vdev->priv;
3221 unsigned int index = vq2txq(sq->vq);
3222 struct netdev_queue *txq;
3223 int opaque, xsk_done = 0;
3224 bool done;
3225
3226 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
3227 /* We don't need to enable cb for XDP */
3228 napi_complete_done(napi, 0);
3229 return 0;
3230 }
3231
3232 txq = netdev_get_tx_queue(vi->dev, index);
3233 __netif_tx_lock(txq, raw_smp_processor_id());
3234 virtqueue_disable_cb(sq->vq);
3235
3236 if (sq->xsk_pool)
3237 xsk_done = virtnet_xsk_xmit(sq, sq->xsk_pool, budget);
3238 else
3239 free_old_xmit(sq, txq, !!budget);
3240
3241 if (sq->vq->num_free >= MAX_SKB_FRAGS + 2)
3242 virtnet_tx_wake_queue(vi, sq);
3243
3244 if (xsk_done >= budget) {
3245 __netif_tx_unlock(txq);
3246 return budget;
3247 }
3248
3249 opaque = virtqueue_enable_cb_prepare(sq->vq);
3250
3251 done = napi_complete_done(napi, 0);
3252
3253 if (!done)
3254 virtqueue_disable_cb(sq->vq);
3255
3256 __netif_tx_unlock(txq);
3257
3258 if (done) {
3259 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
3260 if (napi_schedule_prep(napi)) {
3261 __netif_tx_lock(txq, raw_smp_processor_id());
3262 virtqueue_disable_cb(sq->vq);
3263 __netif_tx_unlock(txq);
3264 __napi_schedule(napi);
3265 }
3266 }
3267 }
3268
3269 return 0;
3270 }
3271
xmit_skb(struct send_queue * sq,struct sk_buff * skb,bool orphan)3272 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb, bool orphan)
3273 {
3274 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
3275 struct virtnet_info *vi = sq->vq->vdev->priv;
3276 struct virtio_net_hdr_v1_hash_tunnel *hdr;
3277 int num_sg;
3278 unsigned hdr_len = vi->hdr_len;
3279 bool feature_hdrlen;
3280 bool can_push;
3281
3282 feature_hdrlen = virtio_has_feature(vi->vdev,
3283 VIRTIO_NET_F_GUEST_HDRLEN);
3284
3285 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
3286
3287 /* Make sure it's safe to cast between formats */
3288 BUILD_BUG_ON(__alignof__(*hdr) != __alignof__(hdr->hash_hdr));
3289 BUILD_BUG_ON(__alignof__(*hdr) != __alignof__(hdr->hash_hdr.hdr));
3290
3291 can_push = vi->any_header_sg &&
3292 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
3293 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
3294 /* Even if we can, don't push here yet as this would skew
3295 * csum_start offset below. */
3296 if (can_push)
3297 hdr = (struct virtio_net_hdr_v1_hash_tunnel *)(skb->data -
3298 hdr_len);
3299 else
3300 hdr = &skb_vnet_common_hdr(skb)->tnl_hdr;
3301
3302 if (virtio_net_hdr_tnl_from_skb(skb, hdr, vi->tx_tnl,
3303 virtio_is_little_endian(vi->vdev), 0,
3304 false, feature_hdrlen))
3305 return -EPROTO;
3306
3307 if (vi->mergeable_rx_bufs)
3308 hdr->hash_hdr.hdr.num_buffers = 0;
3309
3310 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
3311 if (can_push) {
3312 __skb_push(skb, hdr_len);
3313 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
3314 if (unlikely(num_sg < 0))
3315 return num_sg;
3316 /* Pull header back to avoid skew in tx bytes calculations. */
3317 __skb_pull(skb, hdr_len);
3318 } else {
3319 sg_set_buf(sq->sg, hdr, hdr_len);
3320 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
3321 if (unlikely(num_sg < 0))
3322 return num_sg;
3323 num_sg++;
3324 }
3325
3326 return virtnet_add_outbuf(sq, num_sg, skb,
3327 orphan ? VIRTNET_XMIT_TYPE_SKB_ORPHAN : VIRTNET_XMIT_TYPE_SKB);
3328 }
3329
start_xmit(struct sk_buff * skb,struct net_device * dev)3330 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
3331 {
3332 struct virtnet_info *vi = netdev_priv(dev);
3333 int qnum = skb_get_queue_mapping(skb);
3334 struct send_queue *sq = &vi->sq[qnum];
3335 int err;
3336 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
3337 bool xmit_more = netdev_xmit_more();
3338 bool use_napi = sq->napi.weight;
3339 bool kick;
3340
3341 if (!use_napi)
3342 free_old_xmit(sq, txq, false);
3343 else
3344 virtqueue_disable_cb(sq->vq);
3345
3346 /* timestamp packet in software */
3347 skb_tx_timestamp(skb);
3348
3349 /* Try to transmit */
3350 err = xmit_skb(sq, skb, !use_napi);
3351
3352 /* This should not happen! */
3353 if (unlikely(err)) {
3354 DEV_STATS_INC(dev, tx_fifo_errors);
3355 if (net_ratelimit())
3356 dev_warn(&dev->dev,
3357 "Unexpected TXQ (%d) queue failure: %d\n",
3358 qnum, err);
3359 DEV_STATS_INC(dev, tx_dropped);
3360 dev_kfree_skb_any(skb);
3361 return NETDEV_TX_OK;
3362 }
3363
3364 /* Don't wait up for transmitted skbs to be freed. */
3365 if (!use_napi) {
3366 skb_orphan(skb);
3367 skb_dst_drop(skb);
3368 nf_reset_ct(skb);
3369 }
3370
3371 if (use_napi)
3372 tx_may_stop(vi, dev, sq);
3373 else
3374 check_sq_full_and_disable(vi, dev,sq);
3375
3376 kick = use_napi ? __netdev_tx_sent_queue(txq, skb->len, xmit_more) :
3377 !xmit_more || netif_xmit_stopped(txq);
3378 if (kick) {
3379 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
3380 u64_stats_update_begin(&sq->stats.syncp);
3381 u64_stats_inc(&sq->stats.kicks);
3382 u64_stats_update_end(&sq->stats.syncp);
3383 }
3384 }
3385
3386 if (use_napi && kick && unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
3387 virtqueue_napi_schedule(&sq->napi, sq->vq);
3388
3389 return NETDEV_TX_OK;
3390 }
3391
virtnet_rx_pause(struct virtnet_info * vi,struct receive_queue * rq)3392 static void virtnet_rx_pause(struct virtnet_info *vi,
3393 struct receive_queue *rq)
3394 {
3395 bool running = netif_running(vi->dev);
3396
3397 if (running) {
3398 virtnet_napi_disable(rq);
3399 virtnet_cancel_dim(vi, &rq->dim);
3400 }
3401 }
3402
virtnet_rx_pause_all(struct virtnet_info * vi)3403 static void virtnet_rx_pause_all(struct virtnet_info *vi)
3404 {
3405 int i;
3406
3407 for (i = 0; i < vi->max_queue_pairs; i++)
3408 virtnet_rx_pause(vi, &vi->rq[i]);
3409 }
3410
virtnet_rx_resume(struct virtnet_info * vi,struct receive_queue * rq,bool refill)3411 static void virtnet_rx_resume(struct virtnet_info *vi,
3412 struct receive_queue *rq,
3413 bool refill)
3414 {
3415 if (netif_running(vi->dev)) {
3416 /* Pre-fill rq agressively, to make sure we are ready to get
3417 * packets immediately.
3418 */
3419 if (refill)
3420 try_fill_recv(vi, rq, GFP_KERNEL);
3421
3422 virtnet_napi_enable(rq);
3423 }
3424 }
3425
virtnet_rx_resume_all(struct virtnet_info * vi)3426 static void virtnet_rx_resume_all(struct virtnet_info *vi)
3427 {
3428 int i;
3429
3430 for (i = 0; i < vi->max_queue_pairs; i++) {
3431 if (i < vi->curr_queue_pairs)
3432 virtnet_rx_resume(vi, &vi->rq[i], true);
3433 else
3434 virtnet_rx_resume(vi, &vi->rq[i], false);
3435 }
3436 }
3437
virtnet_rx_resize(struct virtnet_info * vi,struct receive_queue * rq,u32 ring_num)3438 static int virtnet_rx_resize(struct virtnet_info *vi,
3439 struct receive_queue *rq, u32 ring_num)
3440 {
3441 int err, qindex;
3442
3443 qindex = rq - vi->rq;
3444
3445 virtnet_rx_pause(vi, rq);
3446
3447 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf, NULL);
3448 if (err)
3449 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
3450
3451 virtnet_rx_resume(vi, rq, true);
3452 return err;
3453 }
3454
virtnet_tx_pause(struct virtnet_info * vi,struct send_queue * sq)3455 static void virtnet_tx_pause(struct virtnet_info *vi, struct send_queue *sq)
3456 {
3457 bool running = netif_running(vi->dev);
3458 struct netdev_queue *txq;
3459 int qindex;
3460
3461 qindex = sq - vi->sq;
3462
3463 if (running)
3464 virtnet_napi_tx_disable(sq);
3465
3466 txq = netdev_get_tx_queue(vi->dev, qindex);
3467
3468 /* 1. wait all ximt complete
3469 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
3470 */
3471 __netif_tx_lock_bh(txq);
3472
3473 /* Prevent rx poll from accessing sq. */
3474 sq->reset = true;
3475
3476 /* Prevent the upper layer from trying to send packets. */
3477 netif_stop_subqueue(vi->dev, qindex);
3478 u64_stats_update_begin(&sq->stats.syncp);
3479 u64_stats_inc(&sq->stats.stop);
3480 u64_stats_update_end(&sq->stats.syncp);
3481
3482 __netif_tx_unlock_bh(txq);
3483 }
3484
virtnet_tx_resume(struct virtnet_info * vi,struct send_queue * sq)3485 static void virtnet_tx_resume(struct virtnet_info *vi, struct send_queue *sq)
3486 {
3487 bool running = netif_running(vi->dev);
3488 struct netdev_queue *txq;
3489 int qindex;
3490
3491 qindex = sq - vi->sq;
3492
3493 txq = netdev_get_tx_queue(vi->dev, qindex);
3494
3495 __netif_tx_lock_bh(txq);
3496 sq->reset = false;
3497 virtnet_tx_wake_queue(vi, sq);
3498 __netif_tx_unlock_bh(txq);
3499
3500 if (running)
3501 virtnet_napi_tx_enable(sq);
3502 }
3503
virtnet_tx_resize(struct virtnet_info * vi,struct send_queue * sq,u32 ring_num)3504 static int virtnet_tx_resize(struct virtnet_info *vi, struct send_queue *sq,
3505 u32 ring_num)
3506 {
3507 int qindex, err;
3508
3509 if (ring_num <= MAX_SKB_FRAGS + 2) {
3510 netdev_err(vi->dev, "tx size (%d) cannot be smaller than %d\n",
3511 ring_num, MAX_SKB_FRAGS + 2);
3512 return -EINVAL;
3513 }
3514
3515 qindex = sq - vi->sq;
3516
3517 virtnet_tx_pause(vi, sq);
3518
3519 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf,
3520 virtnet_sq_free_unused_buf_done);
3521 if (err)
3522 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
3523
3524 virtnet_tx_resume(vi, sq);
3525
3526 return err;
3527 }
3528
3529 /*
3530 * Send command via the control virtqueue and check status. Commands
3531 * supported by the hypervisor, as indicated by feature bits, should
3532 * never fail unless improperly formatted.
3533 */
virtnet_send_command_reply(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out,struct scatterlist * in)3534 static bool virtnet_send_command_reply(struct virtnet_info *vi, u8 class, u8 cmd,
3535 struct scatterlist *out,
3536 struct scatterlist *in)
3537 {
3538 struct scatterlist *sgs[5], hdr, stat;
3539 u32 out_num = 0, tmp, in_num = 0;
3540 bool ok;
3541 int ret;
3542
3543 /* Caller should know better */
3544 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
3545
3546 mutex_lock(&vi->cvq_lock);
3547 vi->ctrl->status = ~0;
3548 vi->ctrl->hdr.class = class;
3549 vi->ctrl->hdr.cmd = cmd;
3550 /* Add header */
3551 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
3552 sgs[out_num++] = &hdr;
3553
3554 if (out)
3555 sgs[out_num++] = out;
3556
3557 /* Add return status. */
3558 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
3559 sgs[out_num + in_num++] = &stat;
3560
3561 if (in)
3562 sgs[out_num + in_num++] = in;
3563
3564 BUG_ON(out_num + in_num > ARRAY_SIZE(sgs));
3565 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, in_num, vi, GFP_ATOMIC);
3566 if (ret < 0) {
3567 dev_warn(&vi->vdev->dev,
3568 "Failed to add sgs for command vq: %d\n.", ret);
3569 mutex_unlock(&vi->cvq_lock);
3570 return false;
3571 }
3572
3573 if (unlikely(!virtqueue_kick(vi->cvq)))
3574 goto unlock;
3575
3576 /* Spin for a response, the kick causes an ioport write, trapping
3577 * into the hypervisor, so the request should be handled immediately.
3578 */
3579 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
3580 !virtqueue_is_broken(vi->cvq)) {
3581 cond_resched();
3582 cpu_relax();
3583 }
3584
3585 unlock:
3586 ok = vi->ctrl->status == VIRTIO_NET_OK;
3587 mutex_unlock(&vi->cvq_lock);
3588 return ok;
3589 }
3590
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)3591 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
3592 struct scatterlist *out)
3593 {
3594 return virtnet_send_command_reply(vi, class, cmd, out, NULL);
3595 }
3596
virtnet_set_mac_address(struct net_device * dev,void * p)3597 static int virtnet_set_mac_address(struct net_device *dev, void *p)
3598 {
3599 struct virtnet_info *vi = netdev_priv(dev);
3600 struct virtio_device *vdev = vi->vdev;
3601 int ret;
3602 struct sockaddr *addr;
3603 struct scatterlist sg;
3604
3605 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3606 return -EOPNOTSUPP;
3607
3608 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
3609 if (!addr)
3610 return -ENOMEM;
3611
3612 ret = eth_prepare_mac_addr_change(dev, addr);
3613 if (ret)
3614 goto out;
3615
3616 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
3617 sg_init_one(&sg, addr->sa_data, dev->addr_len);
3618 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3619 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
3620 dev_warn(&vdev->dev,
3621 "Failed to set mac address by vq command.\n");
3622 ret = -EINVAL;
3623 goto out;
3624 }
3625 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
3626 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
3627 unsigned int i;
3628
3629 /* Naturally, this has an atomicity problem. */
3630 for (i = 0; i < dev->addr_len; i++)
3631 virtio_cwrite8(vdev,
3632 offsetof(struct virtio_net_config, mac) +
3633 i, addr->sa_data[i]);
3634 }
3635
3636 eth_commit_mac_addr_change(dev, p);
3637 ret = 0;
3638
3639 out:
3640 kfree(addr);
3641 return ret;
3642 }
3643
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)3644 static void virtnet_stats(struct net_device *dev,
3645 struct rtnl_link_stats64 *tot)
3646 {
3647 struct virtnet_info *vi = netdev_priv(dev);
3648 unsigned int start;
3649 int i;
3650
3651 for (i = 0; i < vi->max_queue_pairs; i++) {
3652 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
3653 struct receive_queue *rq = &vi->rq[i];
3654 struct send_queue *sq = &vi->sq[i];
3655
3656 do {
3657 start = u64_stats_fetch_begin(&sq->stats.syncp);
3658 tpackets = u64_stats_read(&sq->stats.packets);
3659 tbytes = u64_stats_read(&sq->stats.bytes);
3660 terrors = u64_stats_read(&sq->stats.tx_timeouts);
3661 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3662
3663 do {
3664 start = u64_stats_fetch_begin(&rq->stats.syncp);
3665 rpackets = u64_stats_read(&rq->stats.packets);
3666 rbytes = u64_stats_read(&rq->stats.bytes);
3667 rdrops = u64_stats_read(&rq->stats.drops);
3668 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3669
3670 tot->rx_packets += rpackets;
3671 tot->tx_packets += tpackets;
3672 tot->rx_bytes += rbytes;
3673 tot->tx_bytes += tbytes;
3674 tot->rx_dropped += rdrops;
3675 tot->tx_errors += terrors;
3676 }
3677
3678 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
3679 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
3680 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
3681 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
3682 }
3683
virtnet_ack_link_announce(struct virtnet_info * vi)3684 static void virtnet_ack_link_announce(struct virtnet_info *vi)
3685 {
3686 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
3687 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
3688 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
3689 }
3690
3691 static bool virtnet_commit_rss_command(struct virtnet_info *vi);
3692
virtnet_rss_update_by_qpairs(struct virtnet_info * vi,u16 queue_pairs)3693 static void virtnet_rss_update_by_qpairs(struct virtnet_info *vi, u16 queue_pairs)
3694 {
3695 u32 indir_val = 0;
3696 int i = 0;
3697
3698 for (; i < vi->rss_indir_table_size; ++i) {
3699 indir_val = ethtool_rxfh_indir_default(i, queue_pairs);
3700 vi->rss_hdr->indirection_table[i] = cpu_to_le16(indir_val);
3701 }
3702 vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
3703 }
3704
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)3705 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
3706 {
3707 struct virtio_net_ctrl_mq *mq __free(kfree) = NULL;
3708 struct virtio_net_rss_config_hdr *old_rss_hdr;
3709 struct virtio_net_rss_config_trailer old_rss_trailer;
3710 struct net_device *dev = vi->dev;
3711 struct scatterlist sg;
3712
3713 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
3714 return 0;
3715
3716 /* Firstly check if we need update rss. Do updating if both (1) rss enabled and
3717 * (2) no user configuration.
3718 *
3719 * During rss command processing, device updates queue_pairs using rss.max_tx_vq. That is,
3720 * the device updates queue_pairs together with rss, so we can skip the separate queue_pairs
3721 * update (VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET below) and return directly.
3722 */
3723 if (vi->has_rss && !netif_is_rxfh_configured(dev)) {
3724 old_rss_hdr = vi->rss_hdr;
3725 old_rss_trailer = vi->rss_trailer;
3726 vi->rss_hdr = devm_kzalloc(&vi->vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
3727 if (!vi->rss_hdr) {
3728 vi->rss_hdr = old_rss_hdr;
3729 return -ENOMEM;
3730 }
3731
3732 *vi->rss_hdr = *old_rss_hdr;
3733 virtnet_rss_update_by_qpairs(vi, queue_pairs);
3734
3735 if (!virtnet_commit_rss_command(vi)) {
3736 /* restore ctrl_rss if commit_rss_command failed */
3737 devm_kfree(&vi->vdev->dev, vi->rss_hdr);
3738 vi->rss_hdr = old_rss_hdr;
3739 vi->rss_trailer = old_rss_trailer;
3740
3741 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d, because committing RSS failed\n",
3742 queue_pairs);
3743 return -EINVAL;
3744 }
3745 devm_kfree(&vi->vdev->dev, old_rss_hdr);
3746 goto succ;
3747 }
3748
3749 mq = kzalloc_obj(*mq);
3750 if (!mq)
3751 return -ENOMEM;
3752
3753 mq->virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
3754 sg_init_one(&sg, mq, sizeof(*mq));
3755
3756 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3757 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
3758 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
3759 queue_pairs);
3760 return -EINVAL;
3761 }
3762
3763 /* Keep max_tx_vq in sync so that a later RSS command does not
3764 * revert queue_pairs to a stale value.
3765 */
3766 if (vi->has_rss)
3767 vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
3768 succ:
3769 vi->curr_queue_pairs = queue_pairs;
3770 if (dev->flags & IFF_UP) {
3771 local_bh_disable();
3772 for (int i = 0; i < vi->curr_queue_pairs; ++i)
3773 virtqueue_napi_schedule(&vi->rq[i].napi, vi->rq[i].vq);
3774 local_bh_enable();
3775 }
3776
3777 return 0;
3778 }
3779
virtnet_close(struct net_device * dev)3780 static int virtnet_close(struct net_device *dev)
3781 {
3782 struct virtnet_info *vi = netdev_priv(dev);
3783 int i;
3784
3785 /* Prevent the config change callback from changing carrier
3786 * after close
3787 */
3788 virtio_config_driver_disable(vi->vdev);
3789 /* Stop getting status/speed updates: we don't care until next
3790 * open
3791 */
3792 cancel_work_sync(&vi->config_work);
3793
3794 for (i = 0; i < vi->max_queue_pairs; i++) {
3795 virtnet_disable_queue_pair(vi, i);
3796 virtnet_cancel_dim(vi, &vi->rq[i].dim);
3797 }
3798
3799 netif_carrier_off(dev);
3800
3801 return 0;
3802 }
3803
virtnet_rx_mode_work(struct work_struct * work)3804 static void virtnet_rx_mode_work(struct work_struct *work)
3805 {
3806 struct virtnet_info *vi =
3807 container_of(work, struct virtnet_info, rx_mode_work);
3808 u8 *promisc_allmulti __free(kfree) = NULL;
3809 struct net_device *dev = vi->dev;
3810 struct scatterlist sg[2];
3811 struct virtio_net_ctrl_mac *mac_data;
3812 struct netdev_hw_addr *ha;
3813 int uc_count;
3814 int mc_count;
3815 void *buf;
3816 int i;
3817
3818 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
3819 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
3820 return;
3821
3822 promisc_allmulti = kzalloc_obj(*promisc_allmulti);
3823 if (!promisc_allmulti) {
3824 dev_warn(&dev->dev, "Failed to set RX mode, no memory.\n");
3825 return;
3826 }
3827
3828 rtnl_lock();
3829
3830 *promisc_allmulti = !!(dev->flags & IFF_PROMISC);
3831 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3832
3833 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3834 VIRTIO_NET_CTRL_RX_PROMISC, sg))
3835 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
3836 *promisc_allmulti ? "en" : "dis");
3837
3838 *promisc_allmulti = !!(dev->flags & IFF_ALLMULTI);
3839 sg_init_one(sg, promisc_allmulti, sizeof(*promisc_allmulti));
3840
3841 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
3842 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
3843 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
3844 *promisc_allmulti ? "en" : "dis");
3845
3846 netif_addr_lock_bh(dev);
3847
3848 uc_count = netdev_uc_count(dev);
3849 mc_count = netdev_mc_count(dev);
3850 /* MAC filter - use one buffer for both lists */
3851 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
3852 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
3853 mac_data = buf;
3854 if (!buf) {
3855 netif_addr_unlock_bh(dev);
3856 rtnl_unlock();
3857 return;
3858 }
3859
3860 sg_init_table(sg, 2);
3861
3862 /* Store the unicast list and count in the front of the buffer */
3863 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
3864 i = 0;
3865 netdev_for_each_uc_addr(ha, dev)
3866 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3867
3868 sg_set_buf(&sg[0], mac_data,
3869 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
3870
3871 /* multicast list and count fill the end */
3872 mac_data = (void *)&mac_data->macs[uc_count][0];
3873
3874 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
3875 i = 0;
3876 netdev_for_each_mc_addr(ha, dev)
3877 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
3878
3879 netif_addr_unlock_bh(dev);
3880
3881 sg_set_buf(&sg[1], mac_data,
3882 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
3883
3884 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
3885 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
3886 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
3887
3888 rtnl_unlock();
3889
3890 kfree(buf);
3891 }
3892
virtnet_set_rx_mode(struct net_device * dev)3893 static void virtnet_set_rx_mode(struct net_device *dev)
3894 {
3895 struct virtnet_info *vi = netdev_priv(dev);
3896
3897 if (vi->rx_mode_work_enabled)
3898 schedule_work(&vi->rx_mode_work);
3899 }
3900
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)3901 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
3902 __be16 proto, u16 vid)
3903 {
3904 struct virtnet_info *vi = netdev_priv(dev);
3905 __virtio16 *_vid __free(kfree) = NULL;
3906 struct scatterlist sg;
3907
3908 _vid = kzalloc_obj(*_vid);
3909 if (!_vid)
3910 return -ENOMEM;
3911
3912 *_vid = cpu_to_virtio16(vi->vdev, vid);
3913 sg_init_one(&sg, _vid, sizeof(*_vid));
3914
3915 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3916 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
3917 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
3918 return 0;
3919 }
3920
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)3921 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
3922 __be16 proto, u16 vid)
3923 {
3924 struct virtnet_info *vi = netdev_priv(dev);
3925 __virtio16 *_vid __free(kfree) = NULL;
3926 struct scatterlist sg;
3927
3928 _vid = kzalloc_obj(*_vid);
3929 if (!_vid)
3930 return -ENOMEM;
3931
3932 *_vid = cpu_to_virtio16(vi->vdev, vid);
3933 sg_init_one(&sg, _vid, sizeof(*_vid));
3934
3935 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
3936 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
3937 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
3938 return 0;
3939 }
3940
virtnet_clean_affinity(struct virtnet_info * vi)3941 static void virtnet_clean_affinity(struct virtnet_info *vi)
3942 {
3943 int i;
3944
3945 if (vi->affinity_hint_set) {
3946 for (i = 0; i < vi->max_queue_pairs; i++) {
3947 virtqueue_set_affinity(vi->rq[i].vq, NULL);
3948 virtqueue_set_affinity(vi->sq[i].vq, NULL);
3949 }
3950
3951 vi->affinity_hint_set = false;
3952 }
3953 }
3954
virtnet_set_affinity(struct virtnet_info * vi)3955 static void virtnet_set_affinity(struct virtnet_info *vi)
3956 {
3957 cpumask_var_t mask;
3958 int stragglers;
3959 int group_size;
3960 int i, start = 0, cpu;
3961 int num_cpu;
3962 int stride;
3963
3964 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
3965 virtnet_clean_affinity(vi);
3966 return;
3967 }
3968
3969 num_cpu = num_online_cpus();
3970 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
3971 stragglers = num_cpu >= vi->curr_queue_pairs ?
3972 num_cpu % vi->curr_queue_pairs :
3973 0;
3974
3975 for (i = 0; i < vi->curr_queue_pairs; i++) {
3976 group_size = stride + (i < stragglers ? 1 : 0);
3977
3978 for_each_online_cpu_wrap(cpu, start) {
3979 if (!group_size--) {
3980 start = cpu;
3981 break;
3982 }
3983 cpumask_set_cpu(cpu, mask);
3984 }
3985
3986 virtqueue_set_affinity(vi->rq[i].vq, mask);
3987 virtqueue_set_affinity(vi->sq[i].vq, mask);
3988 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
3989 cpumask_clear(mask);
3990 }
3991
3992 vi->affinity_hint_set = true;
3993 free_cpumask_var(mask);
3994 }
3995
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)3996 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
3997 {
3998 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
3999 node);
4000 virtnet_set_affinity(vi);
4001 return 0;
4002 }
4003
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)4004 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
4005 {
4006 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
4007 node_dead);
4008 virtnet_set_affinity(vi);
4009 return 0;
4010 }
4011
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)4012 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
4013 {
4014 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
4015 node);
4016
4017 virtnet_clean_affinity(vi);
4018 return 0;
4019 }
4020
4021 static enum cpuhp_state virtionet_online;
4022
virtnet_cpu_notif_add(struct virtnet_info * vi)4023 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
4024 {
4025 int ret;
4026
4027 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
4028 if (ret)
4029 return ret;
4030 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
4031 &vi->node_dead);
4032 if (!ret)
4033 return ret;
4034 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
4035 return ret;
4036 }
4037
virtnet_cpu_notif_remove(struct virtnet_info * vi)4038 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
4039 {
4040 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
4041 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
4042 &vi->node_dead);
4043 }
4044
virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 vqn,u32 max_usecs,u32 max_packets)4045 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4046 u16 vqn, u32 max_usecs, u32 max_packets)
4047 {
4048 struct virtio_net_ctrl_coal_vq *coal_vq __free(kfree) = NULL;
4049 struct scatterlist sgs;
4050
4051 coal_vq = kzalloc_obj(*coal_vq);
4052 if (!coal_vq)
4053 return -ENOMEM;
4054
4055 coal_vq->vqn = cpu_to_le16(vqn);
4056 coal_vq->coal.max_usecs = cpu_to_le32(max_usecs);
4057 coal_vq->coal.max_packets = cpu_to_le32(max_packets);
4058 sg_init_one(&sgs, coal_vq, sizeof(*coal_vq));
4059
4060 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
4061 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
4062 &sgs))
4063 return -EINVAL;
4064
4065 return 0;
4066 }
4067
virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 queue,u32 max_usecs,u32 max_packets)4068 static int virtnet_send_rx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4069 u16 queue, u32 max_usecs,
4070 u32 max_packets)
4071 {
4072 int err;
4073
4074 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4075 return -EOPNOTSUPP;
4076
4077 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
4078 max_usecs, max_packets);
4079 if (err)
4080 return err;
4081
4082 vi->rq[queue].intr_coal.max_usecs = max_usecs;
4083 vi->rq[queue].intr_coal.max_packets = max_packets;
4084
4085 return 0;
4086 }
4087
virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 queue,u32 max_usecs,u32 max_packets)4088 static int virtnet_send_tx_ctrl_coal_vq_cmd(struct virtnet_info *vi,
4089 u16 queue, u32 max_usecs,
4090 u32 max_packets)
4091 {
4092 int err;
4093
4094 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
4095 return -EOPNOTSUPP;
4096
4097 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
4098 max_usecs, max_packets);
4099 if (err)
4100 return err;
4101
4102 vi->sq[queue].intr_coal.max_usecs = max_usecs;
4103 vi->sq[queue].intr_coal.max_packets = max_packets;
4104
4105 return 0;
4106 }
4107
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)4108 static void virtnet_get_ringparam(struct net_device *dev,
4109 struct ethtool_ringparam *ring,
4110 struct kernel_ethtool_ringparam *kernel_ring,
4111 struct netlink_ext_ack *extack)
4112 {
4113 struct virtnet_info *vi = netdev_priv(dev);
4114
4115 ring->rx_max_pending = vi->rq[0].vq->num_max;
4116 ring->tx_max_pending = vi->sq[0].vq->num_max;
4117 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
4118 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
4119 }
4120
virtnet_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)4121 static int virtnet_set_ringparam(struct net_device *dev,
4122 struct ethtool_ringparam *ring,
4123 struct kernel_ethtool_ringparam *kernel_ring,
4124 struct netlink_ext_ack *extack)
4125 {
4126 struct virtnet_info *vi = netdev_priv(dev);
4127 u32 rx_pending, tx_pending;
4128 struct receive_queue *rq;
4129 struct send_queue *sq;
4130 int i, err;
4131
4132 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
4133 return -EINVAL;
4134
4135 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
4136 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
4137
4138 if (ring->rx_pending == rx_pending &&
4139 ring->tx_pending == tx_pending)
4140 return 0;
4141
4142 if (ring->rx_pending > vi->rq[0].vq->num_max)
4143 return -EINVAL;
4144
4145 if (ring->tx_pending > vi->sq[0].vq->num_max)
4146 return -EINVAL;
4147
4148 for (i = 0; i < vi->max_queue_pairs; i++) {
4149 rq = vi->rq + i;
4150 sq = vi->sq + i;
4151
4152 if (ring->tx_pending != tx_pending) {
4153 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
4154 if (err)
4155 return err;
4156
4157 /* Upon disabling and re-enabling a transmit virtqueue, the device must
4158 * set the coalescing parameters of the virtqueue to those configured
4159 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
4160 * did not set any TX coalescing parameters, to 0.
4161 */
4162 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, i,
4163 vi->intr_coal_tx.max_usecs,
4164 vi->intr_coal_tx.max_packets);
4165
4166 /* Don't break the tx resize action if the vq coalescing is not
4167 * supported. The same is true for rx resize below.
4168 */
4169 if (err && err != -EOPNOTSUPP)
4170 return err;
4171 }
4172
4173 if (ring->rx_pending != rx_pending) {
4174 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
4175 if (err)
4176 return err;
4177
4178 /* The reason is same as the transmit virtqueue reset */
4179 mutex_lock(&vi->rq[i].dim_lock);
4180 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, i,
4181 vi->intr_coal_rx.max_usecs,
4182 vi->intr_coal_rx.max_packets);
4183 mutex_unlock(&vi->rq[i].dim_lock);
4184 if (err && err != -EOPNOTSUPP)
4185 return err;
4186 }
4187 }
4188
4189 return 0;
4190 }
4191
virtnet_commit_rss_command(struct virtnet_info * vi)4192 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
4193 {
4194 struct net_device *dev = vi->dev;
4195 struct scatterlist sgs[2];
4196
4197 /* prepare sgs */
4198 sg_init_table(sgs, 2);
4199 sg_set_buf(&sgs[0], vi->rss_hdr, virtnet_rss_hdr_size(vi));
4200 sg_set_buf(&sgs[1], &vi->rss_trailer, virtnet_rss_trailer_size(vi));
4201
4202 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
4203 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
4204 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs))
4205 goto err;
4206
4207 return true;
4208
4209 err:
4210 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
4211 return false;
4212
4213 }
4214
virtnet_init_default_rss(struct virtnet_info * vi)4215 static void virtnet_init_default_rss(struct virtnet_info *vi)
4216 {
4217 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_supported);
4218 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
4219 vi->rss_hdr->indirection_table_mask = vi->rss_indir_table_size
4220 ? cpu_to_le16(vi->rss_indir_table_size - 1) : 0;
4221 vi->rss_hdr->unclassified_queue = 0;
4222
4223 virtnet_rss_update_by_qpairs(vi, vi->curr_queue_pairs);
4224
4225 vi->rss_trailer.hash_key_length = vi->rss_key_size;
4226
4227 netdev_rss_key_fill(vi->rss_hash_key_data, vi->rss_key_size);
4228 }
4229
virtnet_get_hashflow(struct net_device * dev,struct ethtool_rxfh_fields * info)4230 static int virtnet_get_hashflow(struct net_device *dev,
4231 struct ethtool_rxfh_fields *info)
4232 {
4233 struct virtnet_info *vi = netdev_priv(dev);
4234
4235 info->data = 0;
4236 switch (info->flow_type) {
4237 case TCP_V4_FLOW:
4238 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
4239 info->data = RXH_IP_SRC | RXH_IP_DST |
4240 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4241 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
4242 info->data = RXH_IP_SRC | RXH_IP_DST;
4243 }
4244 break;
4245 case TCP_V6_FLOW:
4246 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
4247 info->data = RXH_IP_SRC | RXH_IP_DST |
4248 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4249 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
4250 info->data = RXH_IP_SRC | RXH_IP_DST;
4251 }
4252 break;
4253 case UDP_V4_FLOW:
4254 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
4255 info->data = RXH_IP_SRC | RXH_IP_DST |
4256 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4257 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
4258 info->data = RXH_IP_SRC | RXH_IP_DST;
4259 }
4260 break;
4261 case UDP_V6_FLOW:
4262 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
4263 info->data = RXH_IP_SRC | RXH_IP_DST |
4264 RXH_L4_B_0_1 | RXH_L4_B_2_3;
4265 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
4266 info->data = RXH_IP_SRC | RXH_IP_DST;
4267 }
4268 break;
4269 case IPV4_FLOW:
4270 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
4271 info->data = RXH_IP_SRC | RXH_IP_DST;
4272
4273 break;
4274 case IPV6_FLOW:
4275 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
4276 info->data = RXH_IP_SRC | RXH_IP_DST;
4277
4278 break;
4279 default:
4280 info->data = 0;
4281 break;
4282 }
4283
4284 return 0;
4285 }
4286
virtnet_set_hashflow(struct net_device * dev,const struct ethtool_rxfh_fields * info,struct netlink_ext_ack * extack)4287 static int virtnet_set_hashflow(struct net_device *dev,
4288 const struct ethtool_rxfh_fields *info,
4289 struct netlink_ext_ack *extack)
4290 {
4291 struct virtnet_info *vi = netdev_priv(dev);
4292 u32 new_hashtypes = vi->rss_hash_types_saved;
4293 bool is_disable = info->data & RXH_DISCARD;
4294 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
4295
4296 /* supports only 'sd', 'sdfn' and 'r' */
4297 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
4298 return -EINVAL;
4299
4300 switch (info->flow_type) {
4301 case TCP_V4_FLOW:
4302 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
4303 if (!is_disable)
4304 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
4305 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
4306 break;
4307 case UDP_V4_FLOW:
4308 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
4309 if (!is_disable)
4310 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
4311 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
4312 break;
4313 case IPV4_FLOW:
4314 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
4315 if (!is_disable)
4316 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
4317 break;
4318 case TCP_V6_FLOW:
4319 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
4320 if (!is_disable)
4321 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
4322 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
4323 break;
4324 case UDP_V6_FLOW:
4325 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
4326 if (!is_disable)
4327 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
4328 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
4329 break;
4330 case IPV6_FLOW:
4331 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
4332 if (!is_disable)
4333 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
4334 break;
4335 default:
4336 /* unsupported flow */
4337 return -EINVAL;
4338 }
4339
4340 /* if unsupported hashtype was set */
4341 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
4342 return -EINVAL;
4343
4344 if (new_hashtypes != vi->rss_hash_types_saved) {
4345 vi->rss_hash_types_saved = new_hashtypes;
4346 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
4347 if (vi->dev->features & NETIF_F_RXHASH)
4348 if (!virtnet_commit_rss_command(vi))
4349 return -EINVAL;
4350 }
4351
4352 return 0;
4353 }
4354
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)4355 static void virtnet_get_drvinfo(struct net_device *dev,
4356 struct ethtool_drvinfo *info)
4357 {
4358 struct virtnet_info *vi = netdev_priv(dev);
4359 struct virtio_device *vdev = vi->vdev;
4360
4361 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
4362 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
4363 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
4364
4365 }
4366
4367 /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)4368 static int virtnet_set_channels(struct net_device *dev,
4369 struct ethtool_channels *channels)
4370 {
4371 struct virtnet_info *vi = netdev_priv(dev);
4372 u16 queue_pairs = channels->combined_count;
4373 int err;
4374
4375 /* We don't support separate rx/tx channels.
4376 * We don't allow setting 'other' channels.
4377 */
4378 if (channels->rx_count || channels->tx_count || channels->other_count)
4379 return -EINVAL;
4380
4381 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
4382 return -EINVAL;
4383
4384 /* For now we don't support modifying channels while XDP is loaded
4385 * also when XDP is loaded all RX queues have XDP programs so we only
4386 * need to check a single RX queue.
4387 */
4388 if (vi->rq[0].xdp_prog)
4389 return -EINVAL;
4390
4391 cpus_read_lock();
4392 err = virtnet_set_queues(vi, queue_pairs);
4393 if (err) {
4394 cpus_read_unlock();
4395 goto err;
4396 }
4397 virtnet_set_affinity(vi);
4398 cpus_read_unlock();
4399
4400 netif_set_real_num_tx_queues(dev, queue_pairs);
4401 netif_set_real_num_rx_queues(dev, queue_pairs);
4402 err:
4403 return err;
4404 }
4405
virtnet_stats_sprintf(u8 ** p,const char * fmt,const char * noq_fmt,int num,int qid,const struct virtnet_stat_desc * desc)4406 static void virtnet_stats_sprintf(u8 **p, const char *fmt, const char *noq_fmt,
4407 int num, int qid, const struct virtnet_stat_desc *desc)
4408 {
4409 int i;
4410
4411 if (qid < 0) {
4412 for (i = 0; i < num; ++i)
4413 ethtool_sprintf(p, noq_fmt, desc[i].desc);
4414 } else {
4415 for (i = 0; i < num; ++i)
4416 ethtool_sprintf(p, fmt, qid, desc[i].desc);
4417 }
4418 }
4419
4420 /* qid == -1: for rx/tx queue total field */
virtnet_get_stats_string(struct virtnet_info * vi,int type,int qid,u8 ** data)4421 static void virtnet_get_stats_string(struct virtnet_info *vi, int type, int qid, u8 **data)
4422 {
4423 const struct virtnet_stat_desc *desc;
4424 const char *fmt, *noq_fmt;
4425 u8 *p = *data;
4426 u32 num;
4427
4428 if (type == VIRTNET_Q_TYPE_CQ && qid >= 0) {
4429 noq_fmt = "cq_hw_%s";
4430
4431 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4432 desc = &virtnet_stats_cvq_desc[0];
4433 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4434
4435 virtnet_stats_sprintf(&p, NULL, noq_fmt, num, -1, desc);
4436 }
4437 }
4438
4439 if (type == VIRTNET_Q_TYPE_RX) {
4440 fmt = "rx%u_%s";
4441 noq_fmt = "rx_%s";
4442
4443 desc = &virtnet_rq_stats_desc[0];
4444 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4445
4446 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4447
4448 fmt = "rx%u_hw_%s";
4449 noq_fmt = "rx_hw_%s";
4450
4451 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4452 desc = &virtnet_stats_rx_basic_desc[0];
4453 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4454
4455 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4456 }
4457
4458 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4459 desc = &virtnet_stats_rx_csum_desc[0];
4460 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4461
4462 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4463 }
4464
4465 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4466 desc = &virtnet_stats_rx_speed_desc[0];
4467 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4468
4469 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4470 }
4471 }
4472
4473 if (type == VIRTNET_Q_TYPE_TX) {
4474 fmt = "tx%u_%s";
4475 noq_fmt = "tx_%s";
4476
4477 desc = &virtnet_sq_stats_desc[0];
4478 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4479
4480 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4481
4482 fmt = "tx%u_hw_%s";
4483 noq_fmt = "tx_hw_%s";
4484
4485 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4486 desc = &virtnet_stats_tx_basic_desc[0];
4487 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4488
4489 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4490 }
4491
4492 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4493 desc = &virtnet_stats_tx_gso_desc[0];
4494 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4495
4496 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4497 }
4498
4499 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4500 desc = &virtnet_stats_tx_speed_desc[0];
4501 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4502
4503 virtnet_stats_sprintf(&p, fmt, noq_fmt, num, qid, desc);
4504 }
4505 }
4506
4507 *data = p;
4508 }
4509
4510 struct virtnet_stats_ctx {
4511 /* The stats are write to qstats or ethtool -S */
4512 bool to_qstat;
4513
4514 /* Used to calculate the offset inside the output buffer. */
4515 u32 desc_num[3];
4516
4517 /* The actual supported stat types. */
4518 u64 bitmap[3];
4519
4520 /* Used to calculate the reply buffer size. */
4521 u32 size[3];
4522
4523 /* Record the output buffer. */
4524 u64 *data;
4525 };
4526
virtnet_stats_ctx_init(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,u64 * data,bool to_qstat)4527 static void virtnet_stats_ctx_init(struct virtnet_info *vi,
4528 struct virtnet_stats_ctx *ctx,
4529 u64 *data, bool to_qstat)
4530 {
4531 u32 queue_type;
4532
4533 ctx->data = data;
4534 ctx->to_qstat = to_qstat;
4535
4536 if (to_qstat) {
4537 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4538 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4539
4540 queue_type = VIRTNET_Q_TYPE_RX;
4541
4542 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4543 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4544 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4545 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4546 }
4547
4548 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4549 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4550 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4551 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4552 }
4553
4554 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4555 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_GSO;
4556 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4557 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_gso);
4558 }
4559
4560 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4561 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4562 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4563 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4564 }
4565
4566 queue_type = VIRTNET_Q_TYPE_TX;
4567
4568 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4569 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4570 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4571 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4572 }
4573
4574 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4575 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_CSUM;
4576 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4577 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_csum);
4578 }
4579
4580 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4581 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4582 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4583 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4584 }
4585
4586 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4587 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4588 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4589 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4590 }
4591
4592 return;
4593 }
4594
4595 ctx->desc_num[VIRTNET_Q_TYPE_RX] = ARRAY_SIZE(virtnet_rq_stats_desc);
4596 ctx->desc_num[VIRTNET_Q_TYPE_TX] = ARRAY_SIZE(virtnet_sq_stats_desc);
4597
4598 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_CVQ) {
4599 queue_type = VIRTNET_Q_TYPE_CQ;
4600
4601 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_CVQ;
4602 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_cvq_desc);
4603 ctx->size[queue_type] += sizeof(struct virtio_net_stats_cvq);
4604 }
4605
4606 queue_type = VIRTNET_Q_TYPE_RX;
4607
4608 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4609 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_BASIC;
4610 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4611 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_basic);
4612 }
4613
4614 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4615 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_CSUM;
4616 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4617 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_csum);
4618 }
4619
4620 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4621 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_RX_SPEED;
4622 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4623 ctx->size[queue_type] += sizeof(struct virtio_net_stats_rx_speed);
4624 }
4625
4626 queue_type = VIRTNET_Q_TYPE_TX;
4627
4628 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4629 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_BASIC;
4630 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4631 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_basic);
4632 }
4633
4634 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4635 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_GSO;
4636 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4637 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_gso);
4638 }
4639
4640 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4641 ctx->bitmap[queue_type] |= VIRTIO_NET_STATS_TYPE_TX_SPEED;
4642 ctx->desc_num[queue_type] += ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4643 ctx->size[queue_type] += sizeof(struct virtio_net_stats_tx_speed);
4644 }
4645 }
4646
4647 /* stats_sum_queue - Calculate the sum of the same fields in sq or rq.
4648 * @sum: the position to store the sum values
4649 * @num: field num
4650 * @q_value: the first queue fields
4651 * @q_num: number of the queues
4652 */
stats_sum_queue(u64 * sum,u32 num,u64 * q_value,u32 q_num)4653 static void stats_sum_queue(u64 *sum, u32 num, u64 *q_value, u32 q_num)
4654 {
4655 u32 step = num;
4656 int i, j;
4657 u64 *p;
4658
4659 for (i = 0; i < num; ++i) {
4660 p = sum + i;
4661 *p = 0;
4662
4663 for (j = 0; j < q_num; ++j)
4664 *p += *(q_value + i + j * step);
4665 }
4666 }
4667
virtnet_fill_total_fields(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx)4668 static void virtnet_fill_total_fields(struct virtnet_info *vi,
4669 struct virtnet_stats_ctx *ctx)
4670 {
4671 u64 *data, *first_rx_q, *first_tx_q;
4672 u32 num_cq, num_rx, num_tx;
4673
4674 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4675 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4676 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4677
4678 first_rx_q = ctx->data + num_rx + num_tx + num_cq;
4679 first_tx_q = first_rx_q + vi->curr_queue_pairs * num_rx;
4680
4681 data = ctx->data;
4682
4683 stats_sum_queue(data, num_rx, first_rx_q, vi->curr_queue_pairs);
4684
4685 data = ctx->data + num_rx;
4686
4687 stats_sum_queue(data, num_tx, first_tx_q, vi->curr_queue_pairs);
4688 }
4689
virtnet_fill_stats_qstat(struct virtnet_info * vi,u32 qid,struct virtnet_stats_ctx * ctx,const u8 * base,bool drv_stats,u8 reply_type)4690 static void virtnet_fill_stats_qstat(struct virtnet_info *vi, u32 qid,
4691 struct virtnet_stats_ctx *ctx,
4692 const u8 *base, bool drv_stats, u8 reply_type)
4693 {
4694 const struct virtnet_stat_desc *desc;
4695 const u64_stats_t *v_stat;
4696 u64 offset, bitmap;
4697 const __le64 *v;
4698 u32 queue_type;
4699 int i, num;
4700
4701 queue_type = vq_type(vi, qid);
4702 bitmap = ctx->bitmap[queue_type];
4703
4704 if (drv_stats) {
4705 if (queue_type == VIRTNET_Q_TYPE_RX) {
4706 desc = &virtnet_rq_stats_desc_qstat[0];
4707 num = ARRAY_SIZE(virtnet_rq_stats_desc_qstat);
4708 } else {
4709 desc = &virtnet_sq_stats_desc_qstat[0];
4710 num = ARRAY_SIZE(virtnet_sq_stats_desc_qstat);
4711 }
4712
4713 for (i = 0; i < num; ++i) {
4714 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4715 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4716 ctx->data[offset] = u64_stats_read(v_stat);
4717 }
4718 return;
4719 }
4720
4721 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4722 desc = &virtnet_stats_rx_basic_desc_qstat[0];
4723 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc_qstat);
4724 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4725 goto found;
4726 }
4727
4728 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4729 desc = &virtnet_stats_rx_csum_desc_qstat[0];
4730 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc_qstat);
4731 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4732 goto found;
4733 }
4734
4735 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
4736 desc = &virtnet_stats_rx_gso_desc_qstat[0];
4737 num = ARRAY_SIZE(virtnet_stats_rx_gso_desc_qstat);
4738 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_GSO)
4739 goto found;
4740 }
4741
4742 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4743 desc = &virtnet_stats_rx_speed_desc_qstat[0];
4744 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc_qstat);
4745 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4746 goto found;
4747 }
4748
4749 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4750 desc = &virtnet_stats_tx_basic_desc_qstat[0];
4751 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc_qstat);
4752 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4753 goto found;
4754 }
4755
4756 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
4757 desc = &virtnet_stats_tx_csum_desc_qstat[0];
4758 num = ARRAY_SIZE(virtnet_stats_tx_csum_desc_qstat);
4759 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_CSUM)
4760 goto found;
4761 }
4762
4763 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4764 desc = &virtnet_stats_tx_gso_desc_qstat[0];
4765 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc_qstat);
4766 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4767 goto found;
4768 }
4769
4770 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4771 desc = &virtnet_stats_tx_speed_desc_qstat[0];
4772 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc_qstat);
4773 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4774 goto found;
4775 }
4776
4777 return;
4778
4779 found:
4780 for (i = 0; i < num; ++i) {
4781 offset = desc[i].qstat_offset / sizeof(*ctx->data);
4782 v = (const __le64 *)(base + desc[i].offset);
4783 ctx->data[offset] = le64_to_cpu(*v);
4784 }
4785 }
4786
4787 /* virtnet_fill_stats - copy the stats to qstats or ethtool -S
4788 * The stats source is the device or the driver.
4789 *
4790 * @vi: virtio net info
4791 * @qid: the vq id
4792 * @ctx: stats ctx (initiated by virtnet_stats_ctx_init())
4793 * @base: pointer to the device reply or the driver stats structure.
4794 * @drv_stats: designate the base type (device reply, driver stats)
4795 * @type: the type of the device reply (if drv_stats is true, this must be zero)
4796 */
virtnet_fill_stats(struct virtnet_info * vi,u32 qid,struct virtnet_stats_ctx * ctx,const u8 * base,bool drv_stats,u8 reply_type)4797 static void virtnet_fill_stats(struct virtnet_info *vi, u32 qid,
4798 struct virtnet_stats_ctx *ctx,
4799 const u8 *base, bool drv_stats, u8 reply_type)
4800 {
4801 u32 queue_type, num_rx, num_tx, num_cq;
4802 const struct virtnet_stat_desc *desc;
4803 const u64_stats_t *v_stat;
4804 u64 offset, bitmap;
4805 const __le64 *v;
4806 int i, num;
4807
4808 if (ctx->to_qstat)
4809 return virtnet_fill_stats_qstat(vi, qid, ctx, base, drv_stats, reply_type);
4810
4811 num_cq = ctx->desc_num[VIRTNET_Q_TYPE_CQ];
4812 num_rx = ctx->desc_num[VIRTNET_Q_TYPE_RX];
4813 num_tx = ctx->desc_num[VIRTNET_Q_TYPE_TX];
4814
4815 queue_type = vq_type(vi, qid);
4816 bitmap = ctx->bitmap[queue_type];
4817
4818 /* skip the total fields of pairs */
4819 offset = num_rx + num_tx;
4820
4821 if (queue_type == VIRTNET_Q_TYPE_TX) {
4822 offset += num_cq + num_rx * vi->curr_queue_pairs + num_tx * (qid / 2);
4823
4824 num = ARRAY_SIZE(virtnet_sq_stats_desc);
4825 if (drv_stats) {
4826 desc = &virtnet_sq_stats_desc[0];
4827 goto drv_stats;
4828 }
4829
4830 offset += num;
4831
4832 } else if (queue_type == VIRTNET_Q_TYPE_RX) {
4833 offset += num_cq + num_rx * (qid / 2);
4834
4835 num = ARRAY_SIZE(virtnet_rq_stats_desc);
4836 if (drv_stats) {
4837 desc = &virtnet_rq_stats_desc[0];
4838 goto drv_stats;
4839 }
4840
4841 offset += num;
4842 }
4843
4844 if (bitmap & VIRTIO_NET_STATS_TYPE_CVQ) {
4845 desc = &virtnet_stats_cvq_desc[0];
4846 num = ARRAY_SIZE(virtnet_stats_cvq_desc);
4847 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_CVQ)
4848 goto found;
4849
4850 offset += num;
4851 }
4852
4853 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
4854 desc = &virtnet_stats_rx_basic_desc[0];
4855 num = ARRAY_SIZE(virtnet_stats_rx_basic_desc);
4856 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_BASIC)
4857 goto found;
4858
4859 offset += num;
4860 }
4861
4862 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
4863 desc = &virtnet_stats_rx_csum_desc[0];
4864 num = ARRAY_SIZE(virtnet_stats_rx_csum_desc);
4865 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_CSUM)
4866 goto found;
4867
4868 offset += num;
4869 }
4870
4871 if (bitmap & VIRTIO_NET_STATS_TYPE_RX_SPEED) {
4872 desc = &virtnet_stats_rx_speed_desc[0];
4873 num = ARRAY_SIZE(virtnet_stats_rx_speed_desc);
4874 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_RX_SPEED)
4875 goto found;
4876
4877 offset += num;
4878 }
4879
4880 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
4881 desc = &virtnet_stats_tx_basic_desc[0];
4882 num = ARRAY_SIZE(virtnet_stats_tx_basic_desc);
4883 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_BASIC)
4884 goto found;
4885
4886 offset += num;
4887 }
4888
4889 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
4890 desc = &virtnet_stats_tx_gso_desc[0];
4891 num = ARRAY_SIZE(virtnet_stats_tx_gso_desc);
4892 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_GSO)
4893 goto found;
4894
4895 offset += num;
4896 }
4897
4898 if (bitmap & VIRTIO_NET_STATS_TYPE_TX_SPEED) {
4899 desc = &virtnet_stats_tx_speed_desc[0];
4900 num = ARRAY_SIZE(virtnet_stats_tx_speed_desc);
4901 if (reply_type == VIRTIO_NET_STATS_TYPE_REPLY_TX_SPEED)
4902 goto found;
4903
4904 offset += num;
4905 }
4906
4907 return;
4908
4909 found:
4910 for (i = 0; i < num; ++i) {
4911 v = (const __le64 *)(base + desc[i].offset);
4912 ctx->data[offset + i] = le64_to_cpu(*v);
4913 }
4914
4915 return;
4916
4917 drv_stats:
4918 for (i = 0; i < num; ++i) {
4919 v_stat = (const u64_stats_t *)(base + desc[i].offset);
4920 ctx->data[offset + i] = u64_stats_read(v_stat);
4921 }
4922 }
4923
__virtnet_get_hw_stats(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,struct virtio_net_ctrl_queue_stats * req,int req_size,void * reply,int res_size)4924 static int __virtnet_get_hw_stats(struct virtnet_info *vi,
4925 struct virtnet_stats_ctx *ctx,
4926 struct virtio_net_ctrl_queue_stats *req,
4927 int req_size, void *reply, int res_size)
4928 {
4929 struct virtio_net_stats_reply_hdr *hdr;
4930 struct scatterlist sgs_in, sgs_out;
4931 void *p;
4932 u32 qid;
4933 int ok;
4934
4935 sg_init_one(&sgs_out, req, req_size);
4936 sg_init_one(&sgs_in, reply, res_size);
4937
4938 ok = virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
4939 VIRTIO_NET_CTRL_STATS_GET,
4940 &sgs_out, &sgs_in);
4941
4942 if (!ok)
4943 return ok;
4944
4945 for (p = reply; p - reply < res_size; p += le16_to_cpu(hdr->size)) {
4946 hdr = p;
4947 qid = le16_to_cpu(hdr->vq_index);
4948 virtnet_fill_stats(vi, qid, ctx, p, false, hdr->type);
4949 }
4950
4951 return 0;
4952 }
4953
virtnet_make_stat_req(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,struct virtio_net_ctrl_queue_stats * req,int qid,int * idx)4954 static void virtnet_make_stat_req(struct virtnet_info *vi,
4955 struct virtnet_stats_ctx *ctx,
4956 struct virtio_net_ctrl_queue_stats *req,
4957 int qid, int *idx)
4958 {
4959 int qtype = vq_type(vi, qid);
4960 u64 bitmap = ctx->bitmap[qtype];
4961
4962 if (!bitmap)
4963 return;
4964
4965 req->stats[*idx].vq_index = cpu_to_le16(qid);
4966 req->stats[*idx].types_bitmap[0] = cpu_to_le64(bitmap);
4967 *idx += 1;
4968 }
4969
4970 /* qid: -1: get stats of all vq.
4971 * > 0: get the stats for the special vq. This must not be cvq.
4972 */
virtnet_get_hw_stats(struct virtnet_info * vi,struct virtnet_stats_ctx * ctx,int qid)4973 static int virtnet_get_hw_stats(struct virtnet_info *vi,
4974 struct virtnet_stats_ctx *ctx, int qid)
4975 {
4976 int qnum, i, j, res_size, qtype, last_vq, first_vq;
4977 struct virtio_net_ctrl_queue_stats *req;
4978 bool enable_cvq;
4979 void *reply;
4980 int ok;
4981
4982 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS))
4983 return 0;
4984
4985 if (qid == -1) {
4986 last_vq = vi->curr_queue_pairs * 2 - 1;
4987 first_vq = 0;
4988 enable_cvq = true;
4989 } else {
4990 last_vq = qid;
4991 first_vq = qid;
4992 enable_cvq = false;
4993 }
4994
4995 qnum = 0;
4996 res_size = 0;
4997 for (i = first_vq; i <= last_vq ; ++i) {
4998 qtype = vq_type(vi, i);
4999 if (ctx->bitmap[qtype]) {
5000 ++qnum;
5001 res_size += ctx->size[qtype];
5002 }
5003 }
5004
5005 if (enable_cvq && ctx->bitmap[VIRTNET_Q_TYPE_CQ]) {
5006 res_size += ctx->size[VIRTNET_Q_TYPE_CQ];
5007 qnum += 1;
5008 }
5009
5010 req = kzalloc_objs(*req, qnum);
5011 if (!req)
5012 return -ENOMEM;
5013
5014 reply = kmalloc(res_size, GFP_KERNEL);
5015 if (!reply) {
5016 kfree(req);
5017 return -ENOMEM;
5018 }
5019
5020 j = 0;
5021 for (i = first_vq; i <= last_vq ; ++i)
5022 virtnet_make_stat_req(vi, ctx, req, i, &j);
5023
5024 if (enable_cvq)
5025 virtnet_make_stat_req(vi, ctx, req, vi->max_queue_pairs * 2, &j);
5026
5027 ok = __virtnet_get_hw_stats(vi, ctx, req, sizeof(*req) * j, reply, res_size);
5028
5029 kfree(req);
5030 kfree(reply);
5031
5032 return ok;
5033 }
5034
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)5035 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
5036 {
5037 struct virtnet_info *vi = netdev_priv(dev);
5038 unsigned int i;
5039 u8 *p = data;
5040
5041 switch (stringset) {
5042 case ETH_SS_STATS:
5043 /* Generate the total field names. */
5044 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, -1, &p);
5045 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, -1, &p);
5046
5047 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_CQ, 0, &p);
5048
5049 for (i = 0; i < vi->curr_queue_pairs; ++i)
5050 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_RX, i, &p);
5051
5052 for (i = 0; i < vi->curr_queue_pairs; ++i)
5053 virtnet_get_stats_string(vi, VIRTNET_Q_TYPE_TX, i, &p);
5054 break;
5055 }
5056 }
5057
virtnet_get_sset_count(struct net_device * dev,int sset)5058 static int virtnet_get_sset_count(struct net_device *dev, int sset)
5059 {
5060 struct virtnet_info *vi = netdev_priv(dev);
5061 struct virtnet_stats_ctx ctx = {0};
5062 u32 pair_count;
5063
5064 switch (sset) {
5065 case ETH_SS_STATS:
5066 virtnet_stats_ctx_init(vi, &ctx, NULL, false);
5067
5068 pair_count = ctx.desc_num[VIRTNET_Q_TYPE_RX] + ctx.desc_num[VIRTNET_Q_TYPE_TX];
5069
5070 return pair_count + ctx.desc_num[VIRTNET_Q_TYPE_CQ] +
5071 vi->curr_queue_pairs * pair_count;
5072 default:
5073 return -EOPNOTSUPP;
5074 }
5075 }
5076
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)5077 static void virtnet_get_ethtool_stats(struct net_device *dev,
5078 struct ethtool_stats *stats, u64 *data)
5079 {
5080 struct virtnet_info *vi = netdev_priv(dev);
5081 struct virtnet_stats_ctx ctx = {0};
5082 unsigned int start, i;
5083 const u8 *stats_base;
5084
5085 virtnet_stats_ctx_init(vi, &ctx, data, false);
5086 if (virtnet_get_hw_stats(vi, &ctx, -1))
5087 dev_warn(&vi->dev->dev, "Failed to get hw stats.\n");
5088
5089 for (i = 0; i < vi->curr_queue_pairs; i++) {
5090 struct receive_queue *rq = &vi->rq[i];
5091 struct send_queue *sq = &vi->sq[i];
5092
5093 stats_base = (const u8 *)&rq->stats;
5094 do {
5095 start = u64_stats_fetch_begin(&rq->stats.syncp);
5096 virtnet_fill_stats(vi, i * 2, &ctx, stats_base, true, 0);
5097 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
5098
5099 stats_base = (const u8 *)&sq->stats;
5100 do {
5101 start = u64_stats_fetch_begin(&sq->stats.syncp);
5102 virtnet_fill_stats(vi, i * 2 + 1, &ctx, stats_base, true, 0);
5103 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
5104 }
5105
5106 virtnet_fill_total_fields(vi, &ctx);
5107 }
5108
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)5109 static void virtnet_get_channels(struct net_device *dev,
5110 struct ethtool_channels *channels)
5111 {
5112 struct virtnet_info *vi = netdev_priv(dev);
5113
5114 channels->combined_count = vi->curr_queue_pairs;
5115 channels->max_combined = vi->max_queue_pairs;
5116 channels->max_other = 0;
5117 channels->rx_count = 0;
5118 channels->tx_count = 0;
5119 channels->other_count = 0;
5120 }
5121
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)5122 static int virtnet_set_link_ksettings(struct net_device *dev,
5123 const struct ethtool_link_ksettings *cmd)
5124 {
5125 struct virtnet_info *vi = netdev_priv(dev);
5126
5127 return ethtool_virtdev_set_link_ksettings(dev, cmd,
5128 &vi->speed, &vi->duplex);
5129 }
5130
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)5131 static int virtnet_get_link_ksettings(struct net_device *dev,
5132 struct ethtool_link_ksettings *cmd)
5133 {
5134 struct virtnet_info *vi = netdev_priv(dev);
5135
5136 cmd->base.speed = vi->speed;
5137 cmd->base.duplex = vi->duplex;
5138 cmd->base.port = PORT_OTHER;
5139
5140 return 0;
5141 }
5142
virtnet_send_tx_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5143 static int virtnet_send_tx_notf_coal_cmds(struct virtnet_info *vi,
5144 struct ethtool_coalesce *ec)
5145 {
5146 struct virtio_net_ctrl_coal_tx *coal_tx __free(kfree) = NULL;
5147 struct scatterlist sgs_tx;
5148 int i;
5149
5150 coal_tx = kzalloc_obj(*coal_tx);
5151 if (!coal_tx)
5152 return -ENOMEM;
5153
5154 coal_tx->tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
5155 coal_tx->tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
5156 sg_init_one(&sgs_tx, coal_tx, sizeof(*coal_tx));
5157
5158 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
5159 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
5160 &sgs_tx))
5161 return -EINVAL;
5162
5163 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
5164 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
5165 for (i = 0; i < vi->max_queue_pairs; i++) {
5166 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
5167 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
5168 }
5169
5170 return 0;
5171 }
5172
virtnet_send_rx_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5173 static int virtnet_send_rx_notf_coal_cmds(struct virtnet_info *vi,
5174 struct ethtool_coalesce *ec)
5175 {
5176 struct virtio_net_ctrl_coal_rx *coal_rx __free(kfree) = NULL;
5177 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
5178 struct scatterlist sgs_rx;
5179 int i;
5180
5181 if (rx_ctrl_dim_on && !virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5182 return -EOPNOTSUPP;
5183
5184 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != vi->intr_coal_rx.max_usecs ||
5185 ec->rx_max_coalesced_frames != vi->intr_coal_rx.max_packets))
5186 return -EINVAL;
5187
5188 if (rx_ctrl_dim_on && !vi->rx_dim_enabled) {
5189 vi->rx_dim_enabled = true;
5190 for (i = 0; i < vi->max_queue_pairs; i++) {
5191 mutex_lock(&vi->rq[i].dim_lock);
5192 vi->rq[i].dim_enabled = true;
5193 mutex_unlock(&vi->rq[i].dim_lock);
5194 }
5195 return 0;
5196 }
5197
5198 coal_rx = kzalloc_obj(*coal_rx);
5199 if (!coal_rx)
5200 return -ENOMEM;
5201
5202 if (!rx_ctrl_dim_on && vi->rx_dim_enabled) {
5203 vi->rx_dim_enabled = false;
5204 for (i = 0; i < vi->max_queue_pairs; i++) {
5205 mutex_lock(&vi->rq[i].dim_lock);
5206 vi->rq[i].dim_enabled = false;
5207 mutex_unlock(&vi->rq[i].dim_lock);
5208 }
5209 }
5210
5211 /* Since the per-queue coalescing params can be set,
5212 * we need apply the global new params even if they
5213 * are not updated.
5214 */
5215 coal_rx->rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
5216 coal_rx->rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
5217 sg_init_one(&sgs_rx, coal_rx, sizeof(*coal_rx));
5218
5219 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
5220 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
5221 &sgs_rx))
5222 return -EINVAL;
5223
5224 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
5225 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
5226 for (i = 0; i < vi->max_queue_pairs; i++) {
5227 mutex_lock(&vi->rq[i].dim_lock);
5228 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
5229 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
5230 mutex_unlock(&vi->rq[i].dim_lock);
5231 }
5232
5233 return 0;
5234 }
5235
virtnet_send_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)5236 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
5237 struct ethtool_coalesce *ec)
5238 {
5239 int err;
5240
5241 err = virtnet_send_tx_notf_coal_cmds(vi, ec);
5242 if (err)
5243 return err;
5244
5245 err = virtnet_send_rx_notf_coal_cmds(vi, ec);
5246 if (err)
5247 return err;
5248
5249 return 0;
5250 }
5251
virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)5252 static int virtnet_send_rx_notf_coal_vq_cmds(struct virtnet_info *vi,
5253 struct ethtool_coalesce *ec,
5254 u16 queue)
5255 {
5256 bool rx_ctrl_dim_on = !!ec->use_adaptive_rx_coalesce;
5257 u32 max_usecs, max_packets;
5258 bool cur_rx_dim;
5259 int err;
5260
5261 mutex_lock(&vi->rq[queue].dim_lock);
5262 cur_rx_dim = vi->rq[queue].dim_enabled;
5263 max_usecs = vi->rq[queue].intr_coal.max_usecs;
5264 max_packets = vi->rq[queue].intr_coal.max_packets;
5265
5266 if (rx_ctrl_dim_on && (ec->rx_coalesce_usecs != max_usecs ||
5267 ec->rx_max_coalesced_frames != max_packets)) {
5268 mutex_unlock(&vi->rq[queue].dim_lock);
5269 return -EINVAL;
5270 }
5271
5272 if (rx_ctrl_dim_on && !cur_rx_dim) {
5273 vi->rq[queue].dim_enabled = true;
5274 mutex_unlock(&vi->rq[queue].dim_lock);
5275 return 0;
5276 }
5277
5278 if (!rx_ctrl_dim_on && cur_rx_dim)
5279 vi->rq[queue].dim_enabled = false;
5280
5281 /* If no params are updated, userspace ethtool will
5282 * reject the modification.
5283 */
5284 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, queue,
5285 ec->rx_coalesce_usecs,
5286 ec->rx_max_coalesced_frames);
5287 mutex_unlock(&vi->rq[queue].dim_lock);
5288 return err;
5289 }
5290
virtnet_send_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)5291 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
5292 struct ethtool_coalesce *ec,
5293 u16 queue)
5294 {
5295 int err;
5296
5297 err = virtnet_send_rx_notf_coal_vq_cmds(vi, ec, queue);
5298 if (err)
5299 return err;
5300
5301 err = virtnet_send_tx_ctrl_coal_vq_cmd(vi, queue,
5302 ec->tx_coalesce_usecs,
5303 ec->tx_max_coalesced_frames);
5304 if (err)
5305 return err;
5306
5307 return 0;
5308 }
5309
virtnet_rx_dim_work(struct work_struct * work)5310 static void virtnet_rx_dim_work(struct work_struct *work)
5311 {
5312 struct dim *dim = container_of(work, struct dim, work);
5313 struct receive_queue *rq = container_of(dim,
5314 struct receive_queue, dim);
5315 struct virtnet_info *vi = rq->vq->vdev->priv;
5316 struct net_device *dev = vi->dev;
5317 struct dim_cq_moder update_moder;
5318 int qnum, err;
5319
5320 qnum = rq - vi->rq;
5321
5322 mutex_lock(&rq->dim_lock);
5323 if (!rq->dim_enabled)
5324 goto out;
5325
5326 update_moder = net_dim_get_rx_irq_moder(dev, dim);
5327 if (update_moder.usec != rq->intr_coal.max_usecs ||
5328 update_moder.pkts != rq->intr_coal.max_packets) {
5329 err = virtnet_send_rx_ctrl_coal_vq_cmd(vi, qnum,
5330 update_moder.usec,
5331 update_moder.pkts);
5332 if (err)
5333 pr_debug("%s: Failed to send dim parameters on rxq%d\n",
5334 dev->name, qnum);
5335 }
5336 out:
5337 dim->state = DIM_START_MEASURE;
5338 mutex_unlock(&rq->dim_lock);
5339 }
5340
virtnet_coal_params_supported(struct ethtool_coalesce * ec)5341 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
5342 {
5343 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
5344 * or VIRTIO_NET_F_VQ_NOTF_COAL feature is negotiated.
5345 */
5346 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
5347 return -EOPNOTSUPP;
5348
5349 if (ec->tx_max_coalesced_frames > 1 ||
5350 ec->rx_max_coalesced_frames != 1)
5351 return -EINVAL;
5352
5353 return 0;
5354 }
5355
virtnet_should_update_vq_weight(int dev_flags,int weight,int vq_weight,bool * should_update)5356 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
5357 int vq_weight, bool *should_update)
5358 {
5359 if (weight ^ vq_weight) {
5360 if (dev_flags & IFF_UP)
5361 return -EBUSY;
5362 *should_update = true;
5363 }
5364
5365 return 0;
5366 }
5367
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5368 static int virtnet_set_coalesce(struct net_device *dev,
5369 struct ethtool_coalesce *ec,
5370 struct kernel_ethtool_coalesce *kernel_coal,
5371 struct netlink_ext_ack *extack)
5372 {
5373 struct virtnet_info *vi = netdev_priv(dev);
5374 int ret, queue_number, napi_weight, i;
5375 bool update_napi = false;
5376
5377 /* Can't change NAPI weight if the link is up */
5378 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
5379 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
5380 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
5381 vi->sq[queue_number].napi.weight,
5382 &update_napi);
5383 if (ret)
5384 return ret;
5385
5386 if (update_napi) {
5387 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
5388 * updated for the sake of simplicity, which might not be necessary
5389 */
5390 break;
5391 }
5392 }
5393
5394 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
5395 ret = virtnet_send_notf_coal_cmds(vi, ec);
5396 else
5397 ret = virtnet_coal_params_supported(ec);
5398
5399 if (ret)
5400 return ret;
5401
5402 if (update_napi) {
5403 /* xsk xmit depends on the tx napi. So if xsk is active,
5404 * prevent modifications to tx napi.
5405 */
5406 for (i = queue_number; i < vi->max_queue_pairs; i++) {
5407 if (vi->sq[i].xsk_pool)
5408 return -EBUSY;
5409 }
5410
5411 for (; queue_number < vi->max_queue_pairs; queue_number++)
5412 vi->sq[queue_number].napi.weight = napi_weight;
5413 }
5414
5415 return ret;
5416 }
5417
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)5418 static int virtnet_get_coalesce(struct net_device *dev,
5419 struct ethtool_coalesce *ec,
5420 struct kernel_ethtool_coalesce *kernel_coal,
5421 struct netlink_ext_ack *extack)
5422 {
5423 struct virtnet_info *vi = netdev_priv(dev);
5424
5425 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
5426 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
5427 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
5428 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
5429 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
5430 ec->use_adaptive_rx_coalesce = vi->rx_dim_enabled;
5431 } else {
5432 ec->rx_max_coalesced_frames = 1;
5433
5434 if (vi->sq[0].napi.weight)
5435 ec->tx_max_coalesced_frames = 1;
5436 }
5437
5438 return 0;
5439 }
5440
virtnet_set_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)5441 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
5442 u32 queue,
5443 struct ethtool_coalesce *ec)
5444 {
5445 struct virtnet_info *vi = netdev_priv(dev);
5446 int ret, napi_weight;
5447 bool update_napi = false;
5448
5449 if (queue >= vi->max_queue_pairs)
5450 return -EINVAL;
5451
5452 /* Can't change NAPI weight if the link is up */
5453 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
5454 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
5455 vi->sq[queue].napi.weight,
5456 &update_napi);
5457 if (ret)
5458 return ret;
5459
5460 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
5461 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
5462 else
5463 ret = virtnet_coal_params_supported(ec);
5464
5465 if (ret)
5466 return ret;
5467
5468 if (update_napi)
5469 vi->sq[queue].napi.weight = napi_weight;
5470
5471 return 0;
5472 }
5473
virtnet_get_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)5474 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
5475 u32 queue,
5476 struct ethtool_coalesce *ec)
5477 {
5478 struct virtnet_info *vi = netdev_priv(dev);
5479
5480 if (queue >= vi->max_queue_pairs)
5481 return -EINVAL;
5482
5483 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
5484 mutex_lock(&vi->rq[queue].dim_lock);
5485 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
5486 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
5487 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
5488 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
5489 ec->use_adaptive_rx_coalesce = vi->rq[queue].dim_enabled;
5490 mutex_unlock(&vi->rq[queue].dim_lock);
5491 } else {
5492 ec->rx_max_coalesced_frames = 1;
5493
5494 if (vi->sq[queue].napi.weight)
5495 ec->tx_max_coalesced_frames = 1;
5496 }
5497
5498 return 0;
5499 }
5500
virtnet_init_settings(struct net_device * dev)5501 static void virtnet_init_settings(struct net_device *dev)
5502 {
5503 struct virtnet_info *vi = netdev_priv(dev);
5504
5505 vi->speed = SPEED_UNKNOWN;
5506 vi->duplex = DUPLEX_UNKNOWN;
5507 }
5508
virtnet_get_rxfh_key_size(struct net_device * dev)5509 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
5510 {
5511 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
5512 }
5513
virtnet_get_rxfh_indir_size(struct net_device * dev)5514 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
5515 {
5516 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
5517 }
5518
virtnet_get_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh)5519 static int virtnet_get_rxfh(struct net_device *dev,
5520 struct ethtool_rxfh_param *rxfh)
5521 {
5522 struct virtnet_info *vi = netdev_priv(dev);
5523 int i;
5524
5525 if (rxfh->indir) {
5526 for (i = 0; i < vi->rss_indir_table_size; ++i)
5527 rxfh->indir[i] = le16_to_cpu(vi->rss_hdr->indirection_table[i]);
5528 }
5529
5530 if (rxfh->key)
5531 memcpy(rxfh->key, vi->rss_hash_key_data, vi->rss_key_size);
5532
5533 rxfh->hfunc = ETH_RSS_HASH_TOP;
5534
5535 return 0;
5536 }
5537
virtnet_set_rxfh(struct net_device * dev,struct ethtool_rxfh_param * rxfh,struct netlink_ext_ack * extack)5538 static int virtnet_set_rxfh(struct net_device *dev,
5539 struct ethtool_rxfh_param *rxfh,
5540 struct netlink_ext_ack *extack)
5541 {
5542 struct virtnet_info *vi = netdev_priv(dev);
5543 bool update = false;
5544 int i;
5545
5546 if (rxfh->hfunc != ETH_RSS_HASH_NO_CHANGE &&
5547 rxfh->hfunc != ETH_RSS_HASH_TOP)
5548 return -EOPNOTSUPP;
5549
5550 if (rxfh->indir) {
5551 if (!vi->has_rss)
5552 return -EOPNOTSUPP;
5553
5554 for (i = 0; i < vi->rss_indir_table_size; ++i)
5555 vi->rss_hdr->indirection_table[i] = cpu_to_le16(rxfh->indir[i]);
5556 update = true;
5557 }
5558
5559 if (rxfh->key) {
5560 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
5561 * device provides hash calculation capabilities, that is,
5562 * hash_key is configured.
5563 */
5564 if (!vi->has_rss && !vi->has_rss_hash_report)
5565 return -EOPNOTSUPP;
5566
5567 memcpy(vi->rss_hash_key_data, rxfh->key, vi->rss_key_size);
5568 update = true;
5569 }
5570
5571 if (update)
5572 virtnet_commit_rss_command(vi);
5573
5574 return 0;
5575 }
5576
virtnet_get_rx_ring_count(struct net_device * dev)5577 static u32 virtnet_get_rx_ring_count(struct net_device *dev)
5578 {
5579 struct virtnet_info *vi = netdev_priv(dev);
5580
5581 return vi->curr_queue_pairs;
5582 }
5583
5584 static const struct ethtool_ops virtnet_ethtool_ops = {
5585 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
5586 ETHTOOL_COALESCE_USECS | ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
5587 .get_drvinfo = virtnet_get_drvinfo,
5588 .get_link = ethtool_op_get_link,
5589 .get_ringparam = virtnet_get_ringparam,
5590 .set_ringparam = virtnet_set_ringparam,
5591 .get_strings = virtnet_get_strings,
5592 .get_sset_count = virtnet_get_sset_count,
5593 .get_ethtool_stats = virtnet_get_ethtool_stats,
5594 .set_channels = virtnet_set_channels,
5595 .get_channels = virtnet_get_channels,
5596 .get_ts_info = ethtool_op_get_ts_info,
5597 .get_link_ksettings = virtnet_get_link_ksettings,
5598 .set_link_ksettings = virtnet_set_link_ksettings,
5599 .set_coalesce = virtnet_set_coalesce,
5600 .get_coalesce = virtnet_get_coalesce,
5601 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
5602 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
5603 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
5604 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
5605 .get_rxfh = virtnet_get_rxfh,
5606 .set_rxfh = virtnet_set_rxfh,
5607 .get_rxfh_fields = virtnet_get_hashflow,
5608 .set_rxfh_fields = virtnet_set_hashflow,
5609 .get_rx_ring_count = virtnet_get_rx_ring_count,
5610 };
5611
virtnet_get_queue_stats_rx(struct net_device * dev,int i,struct netdev_queue_stats_rx * stats)5612 static void virtnet_get_queue_stats_rx(struct net_device *dev, int i,
5613 struct netdev_queue_stats_rx *stats)
5614 {
5615 struct virtnet_info *vi = netdev_priv(dev);
5616 struct receive_queue *rq = &vi->rq[i];
5617 struct virtnet_stats_ctx ctx = {0};
5618
5619 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5620
5621 virtnet_get_hw_stats(vi, &ctx, i * 2);
5622 virtnet_fill_stats(vi, i * 2, &ctx, (void *)&rq->stats, true, 0);
5623 }
5624
virtnet_get_queue_stats_tx(struct net_device * dev,int i,struct netdev_queue_stats_tx * stats)5625 static void virtnet_get_queue_stats_tx(struct net_device *dev, int i,
5626 struct netdev_queue_stats_tx *stats)
5627 {
5628 struct virtnet_info *vi = netdev_priv(dev);
5629 struct send_queue *sq = &vi->sq[i];
5630 struct virtnet_stats_ctx ctx = {0};
5631
5632 virtnet_stats_ctx_init(vi, &ctx, (void *)stats, true);
5633
5634 virtnet_get_hw_stats(vi, &ctx, i * 2 + 1);
5635 virtnet_fill_stats(vi, i * 2 + 1, &ctx, (void *)&sq->stats, true, 0);
5636 }
5637
virtnet_get_base_stats(struct net_device * dev,struct netdev_queue_stats_rx * rx,struct netdev_queue_stats_tx * tx)5638 static void virtnet_get_base_stats(struct net_device *dev,
5639 struct netdev_queue_stats_rx *rx,
5640 struct netdev_queue_stats_tx *tx)
5641 {
5642 struct virtnet_info *vi = netdev_priv(dev);
5643
5644 /* The queue stats of the virtio-net will not be reset. So here we
5645 * return 0.
5646 */
5647 rx->bytes = 0;
5648 rx->packets = 0;
5649
5650 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_BASIC) {
5651 rx->hw_drops = 0;
5652 rx->hw_drop_overruns = 0;
5653 }
5654
5655 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_CSUM) {
5656 rx->csum_unnecessary = 0;
5657 rx->csum_none = 0;
5658 rx->csum_bad = 0;
5659 }
5660
5661 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_GSO) {
5662 rx->hw_gro_packets = 0;
5663 rx->hw_gro_bytes = 0;
5664 rx->hw_gro_wire_packets = 0;
5665 rx->hw_gro_wire_bytes = 0;
5666 }
5667
5668 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_RX_SPEED)
5669 rx->hw_drop_ratelimits = 0;
5670
5671 tx->bytes = 0;
5672 tx->packets = 0;
5673 tx->stop = 0;
5674 tx->wake = 0;
5675
5676 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_BASIC) {
5677 tx->hw_drops = 0;
5678 tx->hw_drop_errors = 0;
5679 }
5680
5681 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_CSUM) {
5682 tx->csum_none = 0;
5683 tx->needs_csum = 0;
5684 }
5685
5686 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_GSO) {
5687 tx->hw_gso_packets = 0;
5688 tx->hw_gso_bytes = 0;
5689 tx->hw_gso_wire_packets = 0;
5690 tx->hw_gso_wire_bytes = 0;
5691 }
5692
5693 if (vi->device_stats_cap & VIRTIO_NET_STATS_TYPE_TX_SPEED)
5694 tx->hw_drop_ratelimits = 0;
5695
5696 netdev_stat_queue_sum(dev,
5697 dev->real_num_rx_queues, vi->max_queue_pairs, rx,
5698 dev->real_num_tx_queues, vi->max_queue_pairs, tx);
5699 }
5700
5701 static const struct netdev_stat_ops virtnet_stat_ops = {
5702 .get_queue_stats_rx = virtnet_get_queue_stats_rx,
5703 .get_queue_stats_tx = virtnet_get_queue_stats_tx,
5704 .get_base_stats = virtnet_get_base_stats,
5705 };
5706
virtnet_freeze_down(struct virtio_device * vdev)5707 static void virtnet_freeze_down(struct virtio_device *vdev)
5708 {
5709 struct virtnet_info *vi = vdev->priv;
5710
5711 /* Make sure no work handler is accessing the device */
5712 flush_work(&vi->config_work);
5713 disable_rx_mode_work(vi);
5714 flush_work(&vi->rx_mode_work);
5715
5716 if (netif_running(vi->dev)) {
5717 rtnl_lock();
5718 virtnet_close(vi->dev);
5719 rtnl_unlock();
5720 }
5721
5722 netif_tx_lock_bh(vi->dev);
5723 netif_device_detach(vi->dev);
5724 netif_tx_unlock_bh(vi->dev);
5725 }
5726
5727 static int init_vqs(struct virtnet_info *vi);
5728
virtnet_restore_up(struct virtio_device * vdev)5729 static int virtnet_restore_up(struct virtio_device *vdev)
5730 {
5731 struct virtnet_info *vi = vdev->priv;
5732 int err;
5733
5734 err = init_vqs(vi);
5735 if (err)
5736 return err;
5737
5738 err = virtnet_create_page_pools(vi);
5739 if (err)
5740 goto err_del_vqs;
5741
5742 virtio_device_ready(vdev);
5743
5744 enable_rx_mode_work(vi);
5745
5746 if (netif_running(vi->dev)) {
5747 rtnl_lock();
5748 err = virtnet_open(vi->dev);
5749 rtnl_unlock();
5750 if (err)
5751 goto err_destroy_pools;
5752 }
5753
5754 netif_tx_lock_bh(vi->dev);
5755 netif_device_attach(vi->dev);
5756 netif_tx_unlock_bh(vi->dev);
5757 return 0;
5758
5759 err_destroy_pools:
5760 virtio_reset_device(vdev);
5761 free_unused_bufs(vi);
5762 virtnet_destroy_page_pools(vi);
5763 virtnet_del_vqs(vi);
5764 return err;
5765
5766 err_del_vqs:
5767 virtio_reset_device(vdev);
5768 virtnet_del_vqs(vi);
5769 return err;
5770 }
5771
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)5772 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
5773 {
5774 __virtio64 *_offloads __free(kfree) = NULL;
5775 struct scatterlist sg;
5776
5777 _offloads = kzalloc_obj(*_offloads);
5778 if (!_offloads)
5779 return -ENOMEM;
5780
5781 *_offloads = cpu_to_virtio64(vi->vdev, offloads);
5782
5783 sg_init_one(&sg, _offloads, sizeof(*_offloads));
5784
5785 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
5786 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
5787 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
5788 return -EINVAL;
5789 }
5790
5791 return 0;
5792 }
5793
virtnet_clear_guest_offloads(struct virtnet_info * vi)5794 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
5795 {
5796 u64 offloads = 0;
5797
5798 if (!vi->guest_offloads)
5799 return 0;
5800
5801 return virtnet_set_guest_offloads(vi, offloads);
5802 }
5803
virtnet_restore_guest_offloads(struct virtnet_info * vi)5804 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
5805 {
5806 u64 offloads = vi->guest_offloads;
5807
5808 if (!vi->guest_offloads)
5809 return 0;
5810
5811 return virtnet_set_guest_offloads(vi, offloads);
5812 }
5813
virtnet_rq_bind_xsk_pool(struct virtnet_info * vi,struct receive_queue * rq,struct xsk_buff_pool * pool)5814 static int virtnet_rq_bind_xsk_pool(struct virtnet_info *vi, struct receive_queue *rq,
5815 struct xsk_buff_pool *pool)
5816 {
5817 int err, qindex;
5818
5819 qindex = rq - vi->rq;
5820
5821 if (pool) {
5822 err = xdp_rxq_info_reg(&rq->xsk_rxq_info, vi->dev, qindex, rq->napi.napi_id);
5823 if (err < 0)
5824 return err;
5825
5826 err = xdp_rxq_info_reg_mem_model(&rq->xsk_rxq_info,
5827 MEM_TYPE_XSK_BUFF_POOL, NULL);
5828 if (err < 0)
5829 goto unreg;
5830
5831 xsk_pool_set_rxq_info(pool, &rq->xsk_rxq_info);
5832 }
5833
5834 virtnet_rx_pause(vi, rq);
5835
5836 err = virtqueue_reset(rq->vq, virtnet_rq_unmap_free_buf, NULL);
5837 if (err) {
5838 netdev_err(vi->dev, "reset rx fail: rx queue index: %d err: %d\n", qindex, err);
5839
5840 pool = NULL;
5841 }
5842
5843 rq->xsk_pool = pool;
5844
5845 virtnet_rx_resume(vi, rq, true);
5846
5847 if (pool)
5848 return 0;
5849
5850 unreg:
5851 xdp_rxq_info_unreg(&rq->xsk_rxq_info);
5852 return err;
5853 }
5854
virtnet_sq_bind_xsk_pool(struct virtnet_info * vi,struct send_queue * sq,struct xsk_buff_pool * pool)5855 static int virtnet_sq_bind_xsk_pool(struct virtnet_info *vi,
5856 struct send_queue *sq,
5857 struct xsk_buff_pool *pool)
5858 {
5859 int err, qindex;
5860
5861 qindex = sq - vi->sq;
5862
5863 virtnet_tx_pause(vi, sq);
5864
5865 err = virtqueue_reset(sq->vq, virtnet_sq_free_unused_buf,
5866 virtnet_sq_free_unused_buf_done);
5867 if (err) {
5868 netdev_err(vi->dev, "reset tx fail: tx queue index: %d err: %d\n", qindex, err);
5869 pool = NULL;
5870 }
5871
5872 sq->xsk_pool = pool;
5873
5874 virtnet_tx_resume(vi, sq);
5875
5876 return err;
5877 }
5878
virtnet_xsk_pool_enable(struct net_device * dev,struct xsk_buff_pool * pool,u16 qid)5879 static int virtnet_xsk_pool_enable(struct net_device *dev,
5880 struct xsk_buff_pool *pool,
5881 u16 qid)
5882 {
5883 struct virtnet_info *vi = netdev_priv(dev);
5884 struct receive_queue *rq;
5885 struct device *dma_dev;
5886 struct send_queue *sq;
5887 dma_addr_t hdr_dma;
5888 int err, size;
5889
5890 if (vi->hdr_len > xsk_pool_get_headroom(pool))
5891 return -EINVAL;
5892
5893 /* In big_packets mode, xdp cannot work, so there is no need to
5894 * initialize xsk of rq.
5895 */
5896 if (!vi->rq[qid].page_pool)
5897 return -ENOENT;
5898
5899 if (qid >= vi->curr_queue_pairs)
5900 return -EINVAL;
5901
5902 sq = &vi->sq[qid];
5903 rq = &vi->rq[qid];
5904
5905 /* xsk assumes that tx and rx must have the same dma device. The af-xdp
5906 * may use one buffer to receive from the rx and reuse this buffer to
5907 * send by the tx. So the dma dev of sq and rq must be the same one.
5908 *
5909 * But vq->dma_dev allows every vq has the respective dma dev. So I
5910 * check the dma dev of vq and sq is the same dev.
5911 */
5912 if (virtqueue_dma_dev(rq->vq) != virtqueue_dma_dev(sq->vq))
5913 return -EINVAL;
5914
5915 dma_dev = virtqueue_dma_dev(rq->vq);
5916 if (!dma_dev)
5917 return -EINVAL;
5918
5919 size = virtqueue_get_vring_size(rq->vq);
5920
5921 rq->xsk_buffs = kvzalloc_objs(*rq->xsk_buffs, size);
5922 if (!rq->xsk_buffs)
5923 return -ENOMEM;
5924
5925 hdr_dma = virtqueue_map_single_attrs(sq->vq, &xsk_hdr, vi->hdr_len,
5926 DMA_TO_DEVICE, 0);
5927 if (virtqueue_map_mapping_error(sq->vq, hdr_dma)) {
5928 err = -ENOMEM;
5929 goto err_free_buffs;
5930 }
5931
5932 err = xsk_pool_dma_map(pool, dma_dev, 0);
5933 if (err)
5934 goto err_xsk_map;
5935
5936 err = virtnet_rq_bind_xsk_pool(vi, rq, pool);
5937 if (err)
5938 goto err_rq;
5939
5940 err = virtnet_sq_bind_xsk_pool(vi, sq, pool);
5941 if (err)
5942 goto err_sq;
5943
5944 /* Now, we do not support tx offload(such as tx csum), so all the tx
5945 * virtnet hdr is zero. So all the tx packets can share a single hdr.
5946 */
5947 sq->xsk_hdr_dma_addr = hdr_dma;
5948
5949 return 0;
5950
5951 err_sq:
5952 virtnet_rq_bind_xsk_pool(vi, rq, NULL);
5953 err_rq:
5954 xsk_pool_dma_unmap(pool, 0);
5955 err_xsk_map:
5956 virtqueue_unmap_single_attrs(rq->vq, hdr_dma, vi->hdr_len,
5957 DMA_TO_DEVICE, 0);
5958 err_free_buffs:
5959 kvfree(rq->xsk_buffs);
5960 return err;
5961 }
5962
virtnet_xsk_pool_disable(struct net_device * dev,u16 qid)5963 static int virtnet_xsk_pool_disable(struct net_device *dev, u16 qid)
5964 {
5965 struct virtnet_info *vi = netdev_priv(dev);
5966 struct xsk_buff_pool *pool;
5967 struct receive_queue *rq;
5968 struct send_queue *sq;
5969 int err;
5970
5971 if (qid >= vi->curr_queue_pairs)
5972 return -EINVAL;
5973
5974 sq = &vi->sq[qid];
5975 rq = &vi->rq[qid];
5976
5977 pool = rq->xsk_pool;
5978
5979 err = virtnet_rq_bind_xsk_pool(vi, rq, NULL);
5980 err |= virtnet_sq_bind_xsk_pool(vi, sq, NULL);
5981
5982 xsk_pool_dma_unmap(pool, 0);
5983
5984 virtqueue_unmap_single_attrs(sq->vq, sq->xsk_hdr_dma_addr,
5985 vi->hdr_len, DMA_TO_DEVICE, 0);
5986 kvfree(rq->xsk_buffs);
5987
5988 return err;
5989 }
5990
virtnet_xsk_pool_setup(struct net_device * dev,struct netdev_bpf * xdp)5991 static int virtnet_xsk_pool_setup(struct net_device *dev, struct netdev_bpf *xdp)
5992 {
5993 if (xdp->xsk.pool)
5994 return virtnet_xsk_pool_enable(dev, xdp->xsk.pool,
5995 xdp->xsk.queue_id);
5996 else
5997 return virtnet_xsk_pool_disable(dev, xdp->xsk.queue_id);
5998 }
5999
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)6000 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
6001 struct netlink_ext_ack *extack)
6002 {
6003 unsigned int room = SKB_DATA_ALIGN(XDP_PACKET_HEADROOM +
6004 sizeof(struct skb_shared_info));
6005 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
6006 struct virtnet_info *vi = netdev_priv(dev);
6007 struct bpf_prog *old_prog;
6008 u16 xdp_qp = 0, curr_qp;
6009 int i, err;
6010
6011 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
6012 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6013 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
6014 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
6015 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
6016 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
6017 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
6018 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
6019 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
6020 return -EOPNOTSUPP;
6021 }
6022
6023 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
6024 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
6025 return -EINVAL;
6026 }
6027
6028 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
6029 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
6030 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
6031 return -EINVAL;
6032 }
6033
6034 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
6035 if (prog)
6036 xdp_qp = nr_cpu_ids;
6037
6038 /* XDP requires extra queues for XDP_TX */
6039 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
6040 netdev_warn_once(dev, "XDP request %i queues but max is %i. XDP_TX and XDP_REDIRECT will operate in a slower locked tx mode.\n",
6041 curr_qp + xdp_qp, vi->max_queue_pairs);
6042 xdp_qp = 0;
6043 }
6044
6045 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
6046 if (!prog && !old_prog)
6047 return 0;
6048
6049 if (prog)
6050 bpf_prog_add(prog, vi->max_queue_pairs - 1);
6051
6052 virtnet_rx_pause_all(vi);
6053
6054 /* Make sure NAPI is not using any XDP TX queues for RX. */
6055 if (netif_running(dev)) {
6056 for (i = 0; i < vi->max_queue_pairs; i++)
6057 virtnet_napi_tx_disable(&vi->sq[i]);
6058 }
6059
6060 if (!prog) {
6061 for (i = 0; i < vi->max_queue_pairs; i++) {
6062 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
6063 if (i == 0)
6064 virtnet_restore_guest_offloads(vi);
6065 }
6066 synchronize_net();
6067 }
6068
6069 err = virtnet_set_queues(vi, curr_qp + xdp_qp);
6070 if (err)
6071 goto err;
6072 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
6073 vi->xdp_queue_pairs = xdp_qp;
6074
6075 if (prog) {
6076 vi->xdp_enabled = true;
6077 for (i = 0; i < vi->max_queue_pairs; i++) {
6078 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
6079 if (i == 0 && !old_prog)
6080 virtnet_clear_guest_offloads(vi);
6081 }
6082 if (!old_prog)
6083 xdp_features_set_redirect_target(dev, true);
6084 } else {
6085 xdp_features_clear_redirect_target(dev);
6086 vi->xdp_enabled = false;
6087 }
6088
6089 virtnet_rx_resume_all(vi);
6090 for (i = 0; i < vi->max_queue_pairs; i++) {
6091 if (old_prog)
6092 bpf_prog_put(old_prog);
6093 if (netif_running(dev))
6094 virtnet_napi_tx_enable(&vi->sq[i]);
6095 }
6096
6097 return 0;
6098
6099 err:
6100 if (!prog) {
6101 virtnet_clear_guest_offloads(vi);
6102 for (i = 0; i < vi->max_queue_pairs; i++)
6103 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
6104 }
6105
6106 virtnet_rx_resume_all(vi);
6107 if (netif_running(dev)) {
6108 for (i = 0; i < vi->max_queue_pairs; i++)
6109 virtnet_napi_tx_enable(&vi->sq[i]);
6110 }
6111 if (prog)
6112 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
6113 return err;
6114 }
6115
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)6116 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
6117 {
6118 switch (xdp->command) {
6119 case XDP_SETUP_PROG:
6120 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
6121 case XDP_SETUP_XSK_POOL:
6122 return virtnet_xsk_pool_setup(dev, xdp);
6123 default:
6124 return -EINVAL;
6125 }
6126 }
6127
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)6128 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
6129 size_t len)
6130 {
6131 struct virtnet_info *vi = netdev_priv(dev);
6132 int ret;
6133
6134 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
6135 return -EOPNOTSUPP;
6136
6137 ret = snprintf(buf, len, "sby");
6138 if (ret >= len)
6139 return -EOPNOTSUPP;
6140
6141 return 0;
6142 }
6143
virtnet_set_features(struct net_device * dev,netdev_features_t features)6144 static int virtnet_set_features(struct net_device *dev,
6145 netdev_features_t features)
6146 {
6147 struct virtnet_info *vi = netdev_priv(dev);
6148 u64 offloads;
6149 int err;
6150
6151 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
6152 if (vi->xdp_enabled)
6153 return -EBUSY;
6154
6155 if (features & NETIF_F_GRO_HW)
6156 offloads = vi->guest_offloads_capable;
6157 else
6158 offloads = vi->guest_offloads_capable &
6159 ~GUEST_OFFLOAD_GRO_HW_MASK;
6160
6161 err = virtnet_set_guest_offloads(vi, offloads);
6162 if (err)
6163 return err;
6164 vi->guest_offloads = offloads;
6165 }
6166
6167 if ((dev->features ^ features) & NETIF_F_RXHASH) {
6168 if (features & NETIF_F_RXHASH)
6169 vi->rss_hdr->hash_types = cpu_to_le32(vi->rss_hash_types_saved);
6170 else
6171 vi->rss_hdr->hash_types = cpu_to_le32(VIRTIO_NET_HASH_REPORT_NONE);
6172
6173 if (!virtnet_commit_rss_command(vi))
6174 return -EINVAL;
6175 }
6176
6177 return 0;
6178 }
6179
virtnet_tx_timeout(struct net_device * dev,unsigned int txqueue)6180 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
6181 {
6182 struct virtnet_info *priv = netdev_priv(dev);
6183 struct send_queue *sq = &priv->sq[txqueue];
6184 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
6185
6186 u64_stats_update_begin(&sq->stats.syncp);
6187 u64_stats_inc(&sq->stats.tx_timeouts);
6188 u64_stats_update_end(&sq->stats.syncp);
6189
6190 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
6191 txqueue, sq->name, sq->vq->index, sq->vq->name,
6192 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
6193 }
6194
virtnet_init_irq_moder(struct virtnet_info * vi)6195 static int virtnet_init_irq_moder(struct virtnet_info *vi)
6196 {
6197 u8 profile_flags = 0, coal_flags = 0;
6198 int ret, i;
6199
6200 profile_flags |= DIM_PROFILE_RX;
6201 coal_flags |= DIM_COALESCE_USEC | DIM_COALESCE_PKTS;
6202 ret = net_dim_init_irq_moder(vi->dev, profile_flags, coal_flags,
6203 DIM_CQ_PERIOD_MODE_START_FROM_EQE,
6204 0, virtnet_rx_dim_work, NULL);
6205
6206 if (ret)
6207 return ret;
6208
6209 for (i = 0; i < vi->max_queue_pairs; i++)
6210 net_dim_setting(vi->dev, &vi->rq[i].dim, false);
6211
6212 return 0;
6213 }
6214
virtnet_free_irq_moder(struct virtnet_info * vi)6215 static void virtnet_free_irq_moder(struct virtnet_info *vi)
6216 {
6217 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
6218 return;
6219
6220 rtnl_lock();
6221 net_dim_free_irq_moder(vi->dev);
6222 rtnl_unlock();
6223 }
6224
6225 static const struct net_device_ops virtnet_netdev = {
6226 .ndo_open = virtnet_open,
6227 .ndo_stop = virtnet_close,
6228 .ndo_start_xmit = start_xmit,
6229 .ndo_validate_addr = eth_validate_addr,
6230 .ndo_set_mac_address = virtnet_set_mac_address,
6231 .ndo_set_rx_mode = virtnet_set_rx_mode,
6232 .ndo_get_stats64 = virtnet_stats,
6233 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
6234 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
6235 .ndo_bpf = virtnet_xdp,
6236 .ndo_xdp_xmit = virtnet_xdp_xmit,
6237 .ndo_xsk_wakeup = virtnet_xsk_wakeup,
6238 .ndo_features_check = passthru_features_check,
6239 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
6240 .ndo_set_features = virtnet_set_features,
6241 .ndo_tx_timeout = virtnet_tx_timeout,
6242 };
6243
virtnet_config_changed_work(struct work_struct * work)6244 static void virtnet_config_changed_work(struct work_struct *work)
6245 {
6246 struct virtnet_info *vi =
6247 container_of(work, struct virtnet_info, config_work);
6248 u16 v;
6249
6250 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
6251 struct virtio_net_config, status, &v) < 0)
6252 return;
6253
6254 if (v & VIRTIO_NET_S_ANNOUNCE) {
6255 netdev_notify_peers(vi->dev);
6256 virtnet_ack_link_announce(vi);
6257 }
6258
6259 /* Ignore unknown (future) status bits */
6260 v &= VIRTIO_NET_S_LINK_UP;
6261
6262 if (vi->status == v)
6263 return;
6264
6265 vi->status = v;
6266
6267 if (vi->status & VIRTIO_NET_S_LINK_UP) {
6268 virtnet_update_settings(vi);
6269 netif_carrier_on(vi->dev);
6270 netif_tx_wake_all_queues(vi->dev);
6271 } else {
6272 netif_carrier_off(vi->dev);
6273 netif_tx_stop_all_queues(vi->dev);
6274 }
6275 }
6276
virtnet_config_changed(struct virtio_device * vdev)6277 static void virtnet_config_changed(struct virtio_device *vdev)
6278 {
6279 struct virtnet_info *vi = vdev->priv;
6280
6281 schedule_work(&vi->config_work);
6282 }
6283
virtnet_free_queues(struct virtnet_info * vi)6284 static void virtnet_free_queues(struct virtnet_info *vi)
6285 {
6286 int i;
6287
6288 for (i = 0; i < vi->max_queue_pairs; i++) {
6289 __netif_napi_del(&vi->rq[i].napi);
6290 __netif_napi_del(&vi->sq[i].napi);
6291 }
6292
6293 /* We called __netif_napi_del(),
6294 * we need to respect an RCU grace period before freeing vi->rq
6295 */
6296 synchronize_net();
6297
6298 kfree(vi->rq);
6299 kfree(vi->sq);
6300 kfree(vi->ctrl);
6301 }
6302
_free_receive_bufs(struct virtnet_info * vi)6303 static void _free_receive_bufs(struct virtnet_info *vi)
6304 {
6305 struct bpf_prog *old_prog;
6306 int i;
6307
6308 for (i = 0; i < vi->max_queue_pairs; i++) {
6309 while (vi->rq[i].pages)
6310 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
6311
6312 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
6313 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
6314 if (old_prog)
6315 bpf_prog_put(old_prog);
6316 }
6317 }
6318
free_receive_bufs(struct virtnet_info * vi)6319 static void free_receive_bufs(struct virtnet_info *vi)
6320 {
6321 rtnl_lock();
6322 _free_receive_bufs(vi);
6323 rtnl_unlock();
6324 }
6325
virtnet_sq_free_unused_buf(struct virtqueue * vq,void * buf)6326 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
6327 {
6328 struct virtnet_info *vi = vq->vdev->priv;
6329 struct send_queue *sq;
6330 int i = vq2txq(vq);
6331
6332 sq = &vi->sq[i];
6333
6334 switch (virtnet_xmit_ptr_unpack(&buf)) {
6335 case VIRTNET_XMIT_TYPE_SKB:
6336 case VIRTNET_XMIT_TYPE_SKB_ORPHAN:
6337 dev_kfree_skb(buf);
6338 break;
6339
6340 case VIRTNET_XMIT_TYPE_XDP:
6341 xdp_return_frame(buf);
6342 break;
6343
6344 case VIRTNET_XMIT_TYPE_XSK:
6345 xsk_tx_completed(sq->xsk_pool, 1);
6346 break;
6347 }
6348 }
6349
virtnet_sq_free_unused_buf_done(struct virtqueue * vq)6350 static void virtnet_sq_free_unused_buf_done(struct virtqueue *vq)
6351 {
6352 struct virtnet_info *vi = vq->vdev->priv;
6353 int i = vq2txq(vq);
6354
6355 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
6356 }
6357
free_unused_bufs(struct virtnet_info * vi)6358 static void free_unused_bufs(struct virtnet_info *vi)
6359 {
6360 void *buf;
6361 int i;
6362
6363 for (i = 0; i < vi->max_queue_pairs; i++) {
6364 struct virtqueue *vq = vi->sq[i].vq;
6365 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
6366 virtnet_sq_free_unused_buf(vq, buf);
6367 cond_resched();
6368 }
6369
6370 for (i = 0; i < vi->max_queue_pairs; i++) {
6371 struct virtqueue *vq = vi->rq[i].vq;
6372
6373 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
6374 virtnet_rq_unmap_free_buf(vq, buf);
6375 cond_resched();
6376 }
6377 }
6378
virtnet_del_vqs(struct virtnet_info * vi)6379 static void virtnet_del_vqs(struct virtnet_info *vi)
6380 {
6381 struct virtio_device *vdev = vi->vdev;
6382
6383 virtnet_clean_affinity(vi);
6384
6385 vdev->config->del_vqs(vdev);
6386
6387 virtnet_free_queues(vi);
6388 }
6389
6390 /* How large should a single buffer be so a queue full of these can fit at
6391 * least one full packet?
6392 * Logic below assumes the mergeable buffer header is used.
6393 */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)6394 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
6395 {
6396 const unsigned int hdr_len = vi->hdr_len;
6397 unsigned int rq_size = virtqueue_get_vring_size(vq);
6398 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
6399 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
6400 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
6401
6402 return max(max(min_buf_len, hdr_len) - hdr_len,
6403 (unsigned int)GOOD_PACKET_LEN);
6404 }
6405
virtnet_find_vqs(struct virtnet_info * vi)6406 static int virtnet_find_vqs(struct virtnet_info *vi)
6407 {
6408 struct virtqueue_info *vqs_info;
6409 struct virtqueue **vqs;
6410 int ret = -ENOMEM;
6411 int total_vqs;
6412 bool *ctx;
6413 u16 i;
6414
6415 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
6416 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
6417 * possible control vq.
6418 */
6419 total_vqs = vi->max_queue_pairs * 2 +
6420 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
6421
6422 /* Allocate space for find_vqs parameters */
6423 vqs = kzalloc_objs(*vqs, total_vqs);
6424 if (!vqs)
6425 goto err_vq;
6426 vqs_info = kzalloc_objs(*vqs_info, total_vqs);
6427 if (!vqs_info)
6428 goto err_vqs_info;
6429 if (vi->mergeable_rx_bufs || !vi->big_packets) {
6430 ctx = kzalloc_objs(*ctx, total_vqs);
6431 if (!ctx)
6432 goto err_ctx;
6433 } else {
6434 ctx = NULL;
6435 }
6436
6437 /* Parameters for control virtqueue, if any */
6438 if (vi->has_cvq) {
6439 vqs_info[total_vqs - 1].name = "control";
6440 }
6441
6442 /* Allocate/initialize parameters for send/receive virtqueues */
6443 for (i = 0; i < vi->max_queue_pairs; i++) {
6444 vqs_info[rxq2vq(i)].callback = skb_recv_done;
6445 vqs_info[txq2vq(i)].callback = skb_xmit_done;
6446 sprintf(vi->rq[i].name, "input.%u", i);
6447 sprintf(vi->sq[i].name, "output.%u", i);
6448 vqs_info[rxq2vq(i)].name = vi->rq[i].name;
6449 vqs_info[txq2vq(i)].name = vi->sq[i].name;
6450 if (ctx)
6451 vqs_info[rxq2vq(i)].ctx = true;
6452 }
6453
6454 ret = virtio_find_vqs(vi->vdev, total_vqs, vqs, vqs_info, NULL);
6455 if (ret)
6456 goto err_find;
6457
6458 if (vi->has_cvq) {
6459 vi->cvq = vqs[total_vqs - 1];
6460 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
6461 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6462 }
6463
6464 for (i = 0; i < vi->max_queue_pairs; i++) {
6465 vi->rq[i].vq = vqs[rxq2vq(i)];
6466 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
6467 vi->sq[i].vq = vqs[txq2vq(i)];
6468 }
6469 /* run here: ret == 0. */
6470
6471 err_find:
6472 kfree(ctx);
6473 err_ctx:
6474 kfree(vqs_info);
6475 err_vqs_info:
6476 kfree(vqs);
6477 err_vq:
6478 return ret;
6479 }
6480
virtnet_alloc_queues(struct virtnet_info * vi)6481 static int virtnet_alloc_queues(struct virtnet_info *vi)
6482 {
6483 int i;
6484
6485 if (vi->has_cvq) {
6486 vi->ctrl = kzalloc_obj(*vi->ctrl);
6487 if (!vi->ctrl)
6488 goto err_ctrl;
6489 } else {
6490 vi->ctrl = NULL;
6491 }
6492 vi->sq = kzalloc_objs(*vi->sq, vi->max_queue_pairs);
6493 if (!vi->sq)
6494 goto err_sq;
6495 vi->rq = kzalloc_objs(*vi->rq, vi->max_queue_pairs);
6496 if (!vi->rq)
6497 goto err_rq;
6498
6499 for (i = 0; i < vi->max_queue_pairs; i++) {
6500 vi->rq[i].pages = NULL;
6501 netif_napi_add_config(vi->dev, &vi->rq[i].napi, virtnet_poll,
6502 i);
6503 vi->rq[i].napi.weight = napi_weight;
6504 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
6505 virtnet_poll_tx,
6506 napi_tx ? napi_weight : 0);
6507
6508 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
6509 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
6510 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
6511
6512 u64_stats_init(&vi->rq[i].stats.syncp);
6513 u64_stats_init(&vi->sq[i].stats.syncp);
6514 mutex_init(&vi->rq[i].dim_lock);
6515 }
6516
6517 return 0;
6518
6519 err_rq:
6520 kfree(vi->sq);
6521 err_sq:
6522 kfree(vi->ctrl);
6523 err_ctrl:
6524 return -ENOMEM;
6525 }
6526
init_vqs(struct virtnet_info * vi)6527 static int init_vqs(struct virtnet_info *vi)
6528 {
6529 int ret;
6530
6531 /* Allocate send & receive queues */
6532 ret = virtnet_alloc_queues(vi);
6533 if (ret)
6534 goto err;
6535
6536 ret = virtnet_find_vqs(vi);
6537 if (ret)
6538 goto err_free;
6539
6540 cpus_read_lock();
6541 virtnet_set_affinity(vi);
6542 cpus_read_unlock();
6543
6544 return 0;
6545
6546 err_free:
6547 virtnet_free_queues(vi);
6548 err:
6549 return ret;
6550 }
6551
6552 #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)6553 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
6554 char *buf)
6555 {
6556 struct virtnet_info *vi = netdev_priv(queue->dev);
6557 unsigned int queue_index = get_netdev_rx_queue_index(queue);
6558 unsigned int headroom = virtnet_get_headroom(vi);
6559 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
6560 struct ewma_pkt_len *avg;
6561
6562 BUG_ON(queue_index >= vi->max_queue_pairs);
6563 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
6564 return sprintf(buf, "%u\n",
6565 get_mergeable_buf_len(&vi->rq[queue_index], avg,
6566 SKB_DATA_ALIGN(headroom + tailroom)));
6567 }
6568
6569 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
6570 __ATTR_RO(mergeable_rx_buffer_size);
6571
6572 static struct attribute *virtio_net_mrg_rx_attrs[] = {
6573 &mergeable_rx_buffer_size_attribute.attr,
6574 NULL
6575 };
6576
6577 static const struct attribute_group virtio_net_mrg_rx_group = {
6578 .name = "virtio_net",
6579 .attrs = virtio_net_mrg_rx_attrs
6580 };
6581 #endif
6582
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)6583 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
6584 unsigned int fbit,
6585 const char *fname, const char *dname)
6586 {
6587 if (!virtio_has_feature(vdev, fbit))
6588 return false;
6589
6590 dev_err(&vdev->dev, "device advertises feature %s but not %s",
6591 fname, dname);
6592
6593 return true;
6594 }
6595
6596 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
6597 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
6598
virtnet_validate_features(struct virtio_device * vdev)6599 static bool virtnet_validate_features(struct virtio_device *vdev)
6600 {
6601 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
6602 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
6603 "VIRTIO_NET_F_CTRL_VQ") ||
6604 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
6605 "VIRTIO_NET_F_CTRL_VQ") ||
6606 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
6607 "VIRTIO_NET_F_CTRL_VQ") ||
6608 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
6609 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
6610 "VIRTIO_NET_F_CTRL_VQ") ||
6611 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
6612 "VIRTIO_NET_F_CTRL_VQ") ||
6613 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
6614 "VIRTIO_NET_F_CTRL_VQ") ||
6615 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
6616 "VIRTIO_NET_F_CTRL_VQ") ||
6617 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
6618 "VIRTIO_NET_F_CTRL_VQ"))) {
6619 return false;
6620 }
6621
6622 return true;
6623 }
6624
6625 #define MIN_MTU ETH_MIN_MTU
6626 #define MAX_MTU ETH_MAX_MTU
6627
virtnet_validate(struct virtio_device * vdev)6628 static int virtnet_validate(struct virtio_device *vdev)
6629 {
6630 if (!vdev->config->get) {
6631 dev_err(&vdev->dev, "%s failure: config access disabled\n",
6632 __func__);
6633 return -EINVAL;
6634 }
6635
6636 if (!virtnet_validate_features(vdev))
6637 return -EINVAL;
6638
6639 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6640 int mtu = virtio_cread16(vdev,
6641 offsetof(struct virtio_net_config,
6642 mtu));
6643 if (mtu < MIN_MTU)
6644 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
6645 }
6646
6647 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
6648 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6649 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
6650 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
6651 }
6652
6653 return 0;
6654 }
6655
virtnet_check_guest_gso(const struct virtnet_info * vi)6656 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
6657 {
6658 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6659 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
6660 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
6661 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
6662 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
6663 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
6664 }
6665
virtnet_set_big_packets(struct virtnet_info * vi,const int mtu)6666 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
6667 {
6668 bool guest_gso = virtnet_check_guest_gso(vi);
6669
6670 /* If device can receive ANY guest GSO packets, regardless of mtu,
6671 * allocate packets of maximum size, otherwise limit it to only
6672 * mtu size worth only.
6673 */
6674 if (mtu > ETH_DATA_LEN || guest_gso) {
6675 vi->big_packets = true;
6676 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
6677 }
6678 }
6679
6680 #define VIRTIO_NET_HASH_REPORT_MAX_TABLE 10
6681 static enum xdp_rss_hash_type
6682 virtnet_xdp_rss_type[VIRTIO_NET_HASH_REPORT_MAX_TABLE] = {
6683 [VIRTIO_NET_HASH_REPORT_NONE] = XDP_RSS_TYPE_NONE,
6684 [VIRTIO_NET_HASH_REPORT_IPv4] = XDP_RSS_TYPE_L3_IPV4,
6685 [VIRTIO_NET_HASH_REPORT_TCPv4] = XDP_RSS_TYPE_L4_IPV4_TCP,
6686 [VIRTIO_NET_HASH_REPORT_UDPv4] = XDP_RSS_TYPE_L4_IPV4_UDP,
6687 [VIRTIO_NET_HASH_REPORT_IPv6] = XDP_RSS_TYPE_L3_IPV6,
6688 [VIRTIO_NET_HASH_REPORT_TCPv6] = XDP_RSS_TYPE_L4_IPV6_TCP,
6689 [VIRTIO_NET_HASH_REPORT_UDPv6] = XDP_RSS_TYPE_L4_IPV6_UDP,
6690 [VIRTIO_NET_HASH_REPORT_IPv6_EX] = XDP_RSS_TYPE_L3_IPV6_EX,
6691 [VIRTIO_NET_HASH_REPORT_TCPv6_EX] = XDP_RSS_TYPE_L4_IPV6_TCP_EX,
6692 [VIRTIO_NET_HASH_REPORT_UDPv6_EX] = XDP_RSS_TYPE_L4_IPV6_UDP_EX
6693 };
6694
virtnet_xdp_rx_hash(const struct xdp_md * _ctx,u32 * hash,enum xdp_rss_hash_type * rss_type)6695 static int virtnet_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
6696 enum xdp_rss_hash_type *rss_type)
6697 {
6698 const struct xdp_buff *xdp = (void *)_ctx;
6699 struct virtio_net_hdr_v1_hash *hdr_hash;
6700 struct virtnet_info *vi;
6701 u16 hash_report;
6702
6703 if (!(xdp->rxq->dev->features & NETIF_F_RXHASH))
6704 return -ENODATA;
6705
6706 vi = netdev_priv(xdp->rxq->dev);
6707 hdr_hash = (struct virtio_net_hdr_v1_hash *)(xdp->data - vi->hdr_len);
6708 hash_report = __le16_to_cpu(hdr_hash->hash_report);
6709
6710 if (hash_report >= VIRTIO_NET_HASH_REPORT_MAX_TABLE)
6711 hash_report = VIRTIO_NET_HASH_REPORT_NONE;
6712
6713 *rss_type = virtnet_xdp_rss_type[hash_report];
6714 *hash = virtio_net_hash_value(hdr_hash);
6715 return 0;
6716 }
6717
6718 static const struct xdp_metadata_ops virtnet_xdp_metadata_ops = {
6719 .xmo_rx_hash = virtnet_xdp_rx_hash,
6720 };
6721
virtnet_probe(struct virtio_device * vdev)6722 static int virtnet_probe(struct virtio_device *vdev)
6723 {
6724 int i, err = -ENOMEM;
6725 struct net_device *dev;
6726 struct virtnet_info *vi;
6727 u16 max_queue_pairs;
6728 int mtu = 0;
6729 u16 key_sz;
6730
6731 /* Find if host supports multiqueue/rss virtio_net device */
6732 max_queue_pairs = 1;
6733 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
6734 max_queue_pairs =
6735 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
6736
6737 /* We need at least 2 queue's */
6738 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
6739 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
6740 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6741 max_queue_pairs = 1;
6742
6743 /* Allocate ourselves a network device with room for our info */
6744 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
6745 if (!dev)
6746 return -ENOMEM;
6747
6748 /* Set up network device as normal. */
6749 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
6750 IFF_TX_SKB_NO_LINEAR;
6751 dev->netdev_ops = &virtnet_netdev;
6752 dev->stat_ops = &virtnet_stat_ops;
6753 dev->features = NETIF_F_HIGHDMA;
6754
6755 dev->ethtool_ops = &virtnet_ethtool_ops;
6756 SET_NETDEV_DEV(dev, &vdev->dev);
6757
6758 /* Do we support "hardware" checksums? */
6759 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
6760 /* This opens up the world of extra features. */
6761 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6762 if (csum)
6763 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
6764
6765 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
6766 dev->hw_features |= NETIF_F_TSO
6767 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
6768 }
6769 /* Individual feature bits: what can host handle? */
6770 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
6771 dev->hw_features |= NETIF_F_TSO;
6772 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
6773 dev->hw_features |= NETIF_F_TSO6;
6774 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
6775 dev->hw_features |= NETIF_F_TSO_ECN;
6776 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
6777 dev->hw_features |= NETIF_F_GSO_UDP_L4;
6778
6779 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO)) {
6780 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
6781 dev->hw_enc_features = dev->hw_features;
6782 }
6783 if (dev->hw_features & NETIF_F_GSO_UDP_TUNNEL &&
6784 virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM)) {
6785 dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
6786 dev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
6787 }
6788
6789 dev->features |= NETIF_F_GSO_ROBUST;
6790
6791 if (gso)
6792 dev->features |= dev->hw_features;
6793 /* (!csum && gso) case will be fixed by register_netdev() */
6794 }
6795
6796 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
6797 * need to calculate checksums for partially checksummed packets,
6798 * as they're considered valid by the upper layer.
6799 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
6800 * receives fully checksummed packets. The device may assist in
6801 * validating these packets' checksums, so the driver won't have to.
6802 */
6803 dev->features |= NETIF_F_RXCSUM;
6804
6805 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
6806 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
6807 dev->features |= NETIF_F_GRO_HW;
6808
6809 dev->vlan_features = dev->features;
6810 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
6811 NETDEV_XDP_ACT_XSK_ZEROCOPY;
6812
6813 /* MTU range: 68 - 65535 */
6814 dev->min_mtu = MIN_MTU;
6815 dev->max_mtu = MAX_MTU;
6816
6817 /* Configuration may specify what MAC to use. Otherwise random. */
6818 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
6819 u8 addr[ETH_ALEN];
6820
6821 virtio_cread_bytes(vdev,
6822 offsetof(struct virtio_net_config, mac),
6823 addr, ETH_ALEN);
6824 eth_hw_addr_set(dev, addr);
6825 } else {
6826 eth_hw_addr_random(dev);
6827 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
6828 dev->dev_addr);
6829 }
6830
6831 /* Set up our device-specific information */
6832 vi = netdev_priv(dev);
6833 vi->dev = dev;
6834 vi->vdev = vdev;
6835 vdev->priv = vi;
6836
6837 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
6838 INIT_WORK(&vi->rx_mode_work, virtnet_rx_mode_work);
6839
6840 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
6841 vi->mergeable_rx_bufs = true;
6842 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
6843 }
6844
6845 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
6846 vi->has_rss_hash_report = true;
6847
6848 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
6849 vi->has_rss = true;
6850
6851 vi->rss_indir_table_size =
6852 virtio_cread16(vdev, offsetof(struct virtio_net_config,
6853 rss_max_indirection_table_length));
6854 }
6855 vi->rss_hdr = devm_kzalloc(&vdev->dev, virtnet_rss_hdr_size(vi), GFP_KERNEL);
6856 if (!vi->rss_hdr) {
6857 err = -ENOMEM;
6858 goto free;
6859 }
6860
6861 if (vi->has_rss || vi->has_rss_hash_report) {
6862 key_sz = virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
6863
6864 vi->rss_key_size = min_t(u16, key_sz, NETDEV_RSS_KEY_LEN);
6865 if (key_sz > vi->rss_key_size)
6866 dev_warn(&vdev->dev,
6867 "rss_max_key_size=%u exceeds driver limit %u, clamping\n",
6868 key_sz, vi->rss_key_size);
6869
6870 vi->rss_hash_types_supported =
6871 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
6872 vi->rss_hash_types_supported &=
6873 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
6874 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
6875 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
6876
6877 dev->hw_features |= NETIF_F_RXHASH;
6878 dev->xdp_metadata_ops = &virtnet_xdp_metadata_ops;
6879 }
6880
6881 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO) ||
6882 virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
6883 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash_tunnel);
6884 else if (vi->has_rss_hash_report)
6885 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
6886 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
6887 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6888 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
6889 else
6890 vi->hdr_len = sizeof(struct virtio_net_hdr);
6891
6892 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM))
6893 vi->rx_tnl_csum = true;
6894 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO))
6895 vi->rx_tnl = true;
6896 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO))
6897 vi->tx_tnl = true;
6898
6899 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
6900 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
6901 vi->any_header_sg = true;
6902
6903 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
6904 vi->has_cvq = true;
6905
6906 mutex_init(&vi->cvq_lock);
6907
6908 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
6909 mtu = virtio_cread16(vdev,
6910 offsetof(struct virtio_net_config,
6911 mtu));
6912 if (mtu < dev->min_mtu) {
6913 /* Should never trigger: MTU was previously validated
6914 * in virtnet_validate.
6915 */
6916 dev_err(&vdev->dev,
6917 "device MTU appears to have changed it is now %d < %d",
6918 mtu, dev->min_mtu);
6919 err = -EINVAL;
6920 goto free;
6921 }
6922
6923 dev->mtu = mtu;
6924 dev->max_mtu = mtu;
6925 }
6926
6927 virtnet_set_big_packets(vi, mtu);
6928
6929 if (vi->any_header_sg)
6930 dev->needed_headroom = vi->hdr_len;
6931
6932 /* Enable multiqueue by default */
6933 if (num_online_cpus() >= max_queue_pairs)
6934 vi->curr_queue_pairs = max_queue_pairs;
6935 else
6936 vi->curr_queue_pairs = num_online_cpus();
6937 vi->max_queue_pairs = max_queue_pairs;
6938
6939 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
6940 err = init_vqs(vi);
6941 if (err)
6942 goto free;
6943
6944 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
6945 vi->intr_coal_rx.max_usecs = 0;
6946 vi->intr_coal_tx.max_usecs = 0;
6947 vi->intr_coal_rx.max_packets = 0;
6948
6949 /* Keep the default values of the coalescing parameters
6950 * aligned with the default napi_tx state.
6951 */
6952 if (vi->sq[0].napi.weight)
6953 vi->intr_coal_tx.max_packets = 1;
6954 else
6955 vi->intr_coal_tx.max_packets = 0;
6956 }
6957
6958 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
6959 /* The reason is the same as VIRTIO_NET_F_NOTF_COAL. */
6960 for (i = 0; i < vi->max_queue_pairs; i++)
6961 if (vi->sq[i].napi.weight)
6962 vi->sq[i].intr_coal.max_packets = 1;
6963
6964 err = virtnet_init_irq_moder(vi);
6965 if (err)
6966 goto free;
6967 }
6968
6969 /* Create page pools for receive queues.
6970 * Page pools are created at probe time so they can be used
6971 * with premapped DMA addresses throughout the device lifetime.
6972 */
6973 err = virtnet_create_page_pools(vi);
6974 if (err)
6975 goto free_irq_moder;
6976
6977 #ifdef CONFIG_SYSFS
6978 if (vi->mergeable_rx_bufs)
6979 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
6980 #endif
6981 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
6982 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
6983
6984 virtnet_init_settings(dev);
6985
6986 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
6987 vi->failover = net_failover_create(vi->dev);
6988 if (IS_ERR(vi->failover)) {
6989 err = PTR_ERR(vi->failover);
6990 goto free_page_pools;
6991 }
6992 }
6993
6994 if (vi->has_rss || vi->has_rss_hash_report)
6995 virtnet_init_default_rss(vi);
6996
6997 enable_rx_mode_work(vi);
6998
6999 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++) {
7000 unsigned int fbit;
7001
7002 fbit = virtio_offload_to_feature(guest_offloads[i]);
7003 if (virtio_has_feature(vi->vdev, fbit))
7004 set_bit(guest_offloads[i], &vi->guest_offloads);
7005 }
7006 vi->guest_offloads_capable = vi->guest_offloads;
7007
7008 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) &&
7009 (vi->guest_offloads_capable & GUEST_OFFLOAD_GRO_HW_MASK))
7010 dev->hw_features |= NETIF_F_GRO_HW;
7011
7012 /* serialize netdev register + virtio_device_ready() with ndo_open() */
7013 rtnl_lock();
7014
7015 err = register_netdevice(dev);
7016 if (err) {
7017 pr_debug("virtio_net: registering device failed\n");
7018 rtnl_unlock();
7019 goto free_failover;
7020 }
7021
7022 /* Disable config change notification until ndo_open. */
7023 virtio_config_driver_disable(vi->vdev);
7024
7025 virtio_device_ready(vdev);
7026
7027 if (vi->has_rss || vi->has_rss_hash_report) {
7028 if (!virtnet_commit_rss_command(vi)) {
7029 dev_warn(&vdev->dev, "RSS disabled because committing failed.\n");
7030 dev->hw_features &= ~NETIF_F_RXHASH;
7031 vi->has_rss_hash_report = false;
7032 vi->has_rss = false;
7033 }
7034 }
7035
7036 virtnet_set_queues(vi, vi->curr_queue_pairs);
7037
7038 /* a random MAC address has been assigned, notify the device.
7039 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
7040 * because many devices work fine without getting MAC explicitly
7041 */
7042 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
7043 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
7044 struct scatterlist sg;
7045
7046 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
7047 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
7048 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
7049 pr_debug("virtio_net: setting MAC address failed\n");
7050 rtnl_unlock();
7051 err = -EINVAL;
7052 goto free_unregister_netdev;
7053 }
7054 }
7055
7056 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_DEVICE_STATS)) {
7057 struct virtio_net_stats_capabilities *stats_cap __free(kfree) = NULL;
7058 struct scatterlist sg;
7059 __le64 v;
7060
7061 stats_cap = kzalloc_obj(*stats_cap);
7062 if (!stats_cap) {
7063 rtnl_unlock();
7064 err = -ENOMEM;
7065 goto free_unregister_netdev;
7066 }
7067
7068 sg_init_one(&sg, stats_cap, sizeof(*stats_cap));
7069
7070 if (!virtnet_send_command_reply(vi, VIRTIO_NET_CTRL_STATS,
7071 VIRTIO_NET_CTRL_STATS_QUERY,
7072 NULL, &sg)) {
7073 pr_debug("virtio_net: fail to get stats capability\n");
7074 rtnl_unlock();
7075 err = -EINVAL;
7076 goto free_unregister_netdev;
7077 }
7078
7079 v = stats_cap->supported_stats_types[0];
7080 vi->device_stats_cap = le64_to_cpu(v);
7081 }
7082
7083 /* Assume link up if device can't report link status,
7084 otherwise get link status from config. */
7085 netif_carrier_off(dev);
7086 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
7087 virtio_config_changed(vi->vdev);
7088 } else {
7089 vi->status = VIRTIO_NET_S_LINK_UP;
7090 virtnet_update_settings(vi);
7091 netif_carrier_on(dev);
7092 }
7093
7094 rtnl_unlock();
7095
7096 err = virtnet_cpu_notif_add(vi);
7097 if (err) {
7098 pr_debug("virtio_net: registering cpu notifier failed\n");
7099 goto free_unregister_netdev;
7100 }
7101
7102 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
7103 dev->name, max_queue_pairs);
7104
7105 return 0;
7106
7107 free_unregister_netdev:
7108 unregister_netdev(dev);
7109 free_failover:
7110 net_failover_destroy(vi->failover);
7111 free_page_pools:
7112 virtnet_destroy_page_pools(vi);
7113 free_irq_moder:
7114 virtnet_free_irq_moder(vi);
7115 virtio_reset_device(vdev);
7116 virtnet_del_vqs(vi);
7117 free:
7118 free_netdev(dev);
7119 return err;
7120 }
7121
remove_vq_common(struct virtnet_info * vi)7122 static void remove_vq_common(struct virtnet_info *vi)
7123 {
7124 int i;
7125
7126 virtio_reset_device(vi->vdev);
7127
7128 /* Free unused buffers in both send and recv, if any. */
7129 free_unused_bufs(vi);
7130
7131 /*
7132 * Rule of thumb is netdev_tx_reset_queue() should follow any
7133 * skb freeing not followed by netdev_tx_completed_queue()
7134 */
7135 for (i = 0; i < vi->max_queue_pairs; i++)
7136 netdev_tx_reset_queue(netdev_get_tx_queue(vi->dev, i));
7137
7138 free_receive_bufs(vi);
7139
7140 virtnet_destroy_page_pools(vi);
7141
7142 virtnet_del_vqs(vi);
7143 }
7144
virtnet_remove(struct virtio_device * vdev)7145 static void virtnet_remove(struct virtio_device *vdev)
7146 {
7147 struct virtnet_info *vi = vdev->priv;
7148
7149 virtnet_cpu_notif_remove(vi);
7150
7151 /* Make sure no work handler is accessing the device. */
7152 flush_work(&vi->config_work);
7153 disable_rx_mode_work(vi);
7154 flush_work(&vi->rx_mode_work);
7155
7156 virtnet_free_irq_moder(vi);
7157
7158 unregister_netdev(vi->dev);
7159
7160 net_failover_destroy(vi->failover);
7161
7162 remove_vq_common(vi);
7163
7164 free_netdev(vi->dev);
7165 }
7166
virtnet_freeze(struct virtio_device * vdev)7167 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
7168 {
7169 struct virtnet_info *vi = vdev->priv;
7170
7171 virtnet_cpu_notif_remove(vi);
7172 virtnet_freeze_down(vdev);
7173 remove_vq_common(vi);
7174
7175 return 0;
7176 }
7177
virtnet_restore(struct virtio_device * vdev)7178 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
7179 {
7180 struct virtnet_info *vi = vdev->priv;
7181 int err;
7182
7183 err = virtnet_restore_up(vdev);
7184 if (err)
7185 return err;
7186 virtnet_set_queues(vi, vi->curr_queue_pairs);
7187
7188 err = virtnet_cpu_notif_add(vi);
7189 if (err) {
7190 virtnet_freeze_down(vdev);
7191 remove_vq_common(vi);
7192 return err;
7193 }
7194
7195 return 0;
7196 }
7197
7198 static struct virtio_device_id id_table[] = {
7199 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
7200 { 0 },
7201 };
7202
7203 #define VIRTNET_FEATURES \
7204 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
7205 VIRTIO_NET_F_MAC, \
7206 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
7207 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
7208 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
7209 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
7210 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
7211 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
7212 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
7213 VIRTIO_NET_F_CTRL_MAC_ADDR, \
7214 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
7215 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
7216 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
7217 VIRTIO_NET_F_VQ_NOTF_COAL, \
7218 VIRTIO_NET_F_GUEST_HDRLEN, VIRTIO_NET_F_DEVICE_STATS
7219
7220 static unsigned int features[] = {
7221 VIRTNET_FEATURES,
7222 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO,
7223 VIRTIO_NET_F_GUEST_UDP_TUNNEL_GSO_CSUM,
7224 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO,
7225 VIRTIO_NET_F_HOST_UDP_TUNNEL_GSO_CSUM,
7226 };
7227
7228 static unsigned int features_legacy[] = {
7229 VIRTNET_FEATURES,
7230 VIRTIO_NET_F_GSO,
7231 VIRTIO_F_ANY_LAYOUT,
7232 };
7233
7234 static struct virtio_driver virtio_net_driver = {
7235 .feature_table = features,
7236 .feature_table_size = ARRAY_SIZE(features),
7237 .feature_table_legacy = features_legacy,
7238 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
7239 .driver.name = KBUILD_MODNAME,
7240 .id_table = id_table,
7241 .validate = virtnet_validate,
7242 .probe = virtnet_probe,
7243 .remove = virtnet_remove,
7244 .config_changed = virtnet_config_changed,
7245 #ifdef CONFIG_PM_SLEEP
7246 .freeze = virtnet_freeze,
7247 .restore = virtnet_restore,
7248 #endif
7249 };
7250
virtio_net_driver_init(void)7251 static __init int virtio_net_driver_init(void)
7252 {
7253 int ret;
7254
7255 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
7256 virtnet_cpu_online,
7257 virtnet_cpu_down_prep);
7258 if (ret < 0)
7259 goto out;
7260 virtionet_online = ret;
7261 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
7262 NULL, virtnet_cpu_dead);
7263 if (ret)
7264 goto err_dead;
7265 ret = register_virtio_driver(&virtio_net_driver);
7266 if (ret)
7267 goto err_virtio;
7268 return 0;
7269 err_virtio:
7270 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
7271 err_dead:
7272 cpuhp_remove_multi_state(virtionet_online);
7273 out:
7274 return ret;
7275 }
7276 module_init(virtio_net_driver_init);
7277
virtio_net_driver_exit(void)7278 static __exit void virtio_net_driver_exit(void)
7279 {
7280 unregister_virtio_driver(&virtio_net_driver);
7281 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
7282 cpuhp_remove_multi_state(virtionet_online);
7283 }
7284 module_exit(virtio_net_driver_exit);
7285
7286 MODULE_DEVICE_TABLE(virtio, id_table);
7287 MODULE_DESCRIPTION("Virtio network driver");
7288 MODULE_LICENSE("GPL");
7289