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 <net/route.h>
23 #include <net/xdp.h>
24 #include <net/net_failover.h>
25 #include <net/netdev_rx_queue.h>
26
27 static int napi_weight = NAPI_POLL_WEIGHT;
28 module_param(napi_weight, int, 0444);
29
30 static bool csum = true, gso = true, napi_tx = true;
31 module_param(csum, bool, 0444);
32 module_param(gso, bool, 0444);
33 module_param(napi_tx, bool, 0644);
34
35 /* FIXME: MTU in config. */
36 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
37 #define GOOD_COPY_LEN 128
38
39 #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
40
41 /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
42 #define VIRTIO_XDP_HEADROOM 256
43
44 /* Separating two types of XDP xmit */
45 #define VIRTIO_XDP_TX BIT(0)
46 #define VIRTIO_XDP_REDIR BIT(1)
47
48 #define VIRTIO_XDP_FLAG BIT(0)
49
50 /* RX packet size EWMA. The average packet size is used to determine the packet
51 * buffer size when refilling RX rings. As the entire RX ring may be refilled
52 * at once, the weight is chosen so that the EWMA will be insensitive to short-
53 * term, transient changes in packet size.
54 */
55 DECLARE_EWMA(pkt_len, 0, 64)
56
57 #define VIRTNET_DRIVER_VERSION "1.0.0"
58
59 static const unsigned long guest_offloads[] = {
60 VIRTIO_NET_F_GUEST_TSO4,
61 VIRTIO_NET_F_GUEST_TSO6,
62 VIRTIO_NET_F_GUEST_ECN,
63 VIRTIO_NET_F_GUEST_UFO,
64 VIRTIO_NET_F_GUEST_CSUM,
65 VIRTIO_NET_F_GUEST_USO4,
66 VIRTIO_NET_F_GUEST_USO6,
67 VIRTIO_NET_F_GUEST_HDRLEN
68 };
69
70 #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
71 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
72 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
73 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
74 (1ULL << VIRTIO_NET_F_GUEST_USO4) | \
75 (1ULL << VIRTIO_NET_F_GUEST_USO6))
76
77 struct virtnet_stat_desc {
78 char desc[ETH_GSTRING_LEN];
79 size_t offset;
80 };
81
82 struct virtnet_sq_stats {
83 struct u64_stats_sync syncp;
84 u64_stats_t packets;
85 u64_stats_t bytes;
86 u64_stats_t xdp_tx;
87 u64_stats_t xdp_tx_drops;
88 u64_stats_t kicks;
89 u64_stats_t tx_timeouts;
90 };
91
92 struct virtnet_rq_stats {
93 struct u64_stats_sync syncp;
94 u64_stats_t packets;
95 u64_stats_t bytes;
96 u64_stats_t drops;
97 u64_stats_t xdp_packets;
98 u64_stats_t xdp_tx;
99 u64_stats_t xdp_redirects;
100 u64_stats_t xdp_drops;
101 u64_stats_t kicks;
102 };
103
104 #define VIRTNET_SQ_STAT(m) offsetof(struct virtnet_sq_stats, m)
105 #define VIRTNET_RQ_STAT(m) offsetof(struct virtnet_rq_stats, m)
106
107 static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
108 { "packets", VIRTNET_SQ_STAT(packets) },
109 { "bytes", VIRTNET_SQ_STAT(bytes) },
110 { "xdp_tx", VIRTNET_SQ_STAT(xdp_tx) },
111 { "xdp_tx_drops", VIRTNET_SQ_STAT(xdp_tx_drops) },
112 { "kicks", VIRTNET_SQ_STAT(kicks) },
113 { "tx_timeouts", VIRTNET_SQ_STAT(tx_timeouts) },
114 };
115
116 static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
117 { "packets", VIRTNET_RQ_STAT(packets) },
118 { "bytes", VIRTNET_RQ_STAT(bytes) },
119 { "drops", VIRTNET_RQ_STAT(drops) },
120 { "xdp_packets", VIRTNET_RQ_STAT(xdp_packets) },
121 { "xdp_tx", VIRTNET_RQ_STAT(xdp_tx) },
122 { "xdp_redirects", VIRTNET_RQ_STAT(xdp_redirects) },
123 { "xdp_drops", VIRTNET_RQ_STAT(xdp_drops) },
124 { "kicks", VIRTNET_RQ_STAT(kicks) },
125 };
126
127 #define VIRTNET_SQ_STATS_LEN ARRAY_SIZE(virtnet_sq_stats_desc)
128 #define VIRTNET_RQ_STATS_LEN ARRAY_SIZE(virtnet_rq_stats_desc)
129
130 struct virtnet_interrupt_coalesce {
131 u32 max_packets;
132 u32 max_usecs;
133 };
134
135 /* The dma information of pages allocated at a time. */
136 struct virtnet_rq_dma {
137 dma_addr_t addr;
138 u32 ref;
139 u16 len;
140 u16 need_sync;
141 };
142
143 /* Internal representation of a send virtqueue */
144 struct send_queue {
145 /* Virtqueue associated with this send _queue */
146 struct virtqueue *vq;
147
148 /* TX: fragments + linear part + virtio header */
149 struct scatterlist sg[MAX_SKB_FRAGS + 2];
150
151 /* Name of the send queue: output.$index */
152 char name[16];
153
154 struct virtnet_sq_stats stats;
155
156 struct virtnet_interrupt_coalesce intr_coal;
157
158 struct napi_struct napi;
159
160 /* Record whether sq is in reset state. */
161 bool reset;
162 };
163
164 /* Internal representation of a receive virtqueue */
165 struct receive_queue {
166 /* Virtqueue associated with this receive_queue */
167 struct virtqueue *vq;
168
169 struct napi_struct napi;
170
171 struct bpf_prog __rcu *xdp_prog;
172
173 struct virtnet_rq_stats stats;
174
175 struct virtnet_interrupt_coalesce intr_coal;
176
177 /* Chain pages by the private ptr. */
178 struct page *pages;
179
180 /* Average packet length for mergeable receive buffers. */
181 struct ewma_pkt_len mrg_avg_pkt_len;
182
183 /* Page frag for packet buffer allocation. */
184 struct page_frag alloc_frag;
185
186 /* RX: fragments + linear part + virtio header */
187 struct scatterlist sg[MAX_SKB_FRAGS + 2];
188
189 /* Min single buffer size for mergeable buffers case. */
190 unsigned int min_buf_len;
191
192 /* Name of this receive queue: input.$index */
193 char name[16];
194
195 struct xdp_rxq_info xdp_rxq;
196
197 /* Record the last dma info to free after new pages is allocated. */
198 struct virtnet_rq_dma *last_dma;
199
200 /* Do dma by self */
201 bool do_dma;
202 };
203
204 /* This structure can contain rss message with maximum settings for indirection table and keysize
205 * Note, that default structure that describes RSS configuration virtio_net_rss_config
206 * contains same info but can't handle table values.
207 * In any case, structure would be passed to virtio hw through sg_buf split by parts
208 * because table sizes may be differ according to the device configuration.
209 */
210 #define VIRTIO_NET_RSS_MAX_KEY_SIZE 40
211 #define VIRTIO_NET_RSS_MAX_TABLE_LEN 128
212 struct virtio_net_ctrl_rss {
213 u32 hash_types;
214 u16 indirection_table_mask;
215 u16 unclassified_queue;
216 u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
217 u16 max_tx_vq;
218 u8 hash_key_length;
219 u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
220 };
221
222 /* Control VQ buffers: protected by the rtnl lock */
223 struct control_buf {
224 struct virtio_net_ctrl_hdr hdr;
225 virtio_net_ctrl_ack status;
226 struct virtio_net_ctrl_mq mq;
227 u8 promisc;
228 u8 allmulti;
229 __virtio16 vid;
230 __virtio64 offloads;
231 struct virtio_net_ctrl_rss rss;
232 struct virtio_net_ctrl_coal_tx coal_tx;
233 struct virtio_net_ctrl_coal_rx coal_rx;
234 struct virtio_net_ctrl_coal_vq coal_vq;
235 };
236
237 struct virtnet_info {
238 struct virtio_device *vdev;
239 struct virtqueue *cvq;
240 struct net_device *dev;
241 struct send_queue *sq;
242 struct receive_queue *rq;
243 unsigned int status;
244
245 /* Max # of queue pairs supported by the device */
246 u16 max_queue_pairs;
247
248 /* # of queue pairs currently used by the driver */
249 u16 curr_queue_pairs;
250
251 /* # of XDP queue pairs currently used by the driver */
252 u16 xdp_queue_pairs;
253
254 /* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
255 bool xdp_enabled;
256
257 /* I like... big packets and I cannot lie! */
258 bool big_packets;
259
260 /* number of sg entries allocated for big packets */
261 unsigned int big_packets_num_skbfrags;
262
263 /* Host will merge rx buffers for big packets (shake it! shake it!) */
264 bool mergeable_rx_bufs;
265
266 /* Host supports rss and/or hash report */
267 bool has_rss;
268 bool has_rss_hash_report;
269 u8 rss_key_size;
270 u16 rss_indir_table_size;
271 u32 rss_hash_types_supported;
272 u32 rss_hash_types_saved;
273
274 /* Has control virtqueue */
275 bool has_cvq;
276
277 /* Host can handle any s/g split between our header and packet data */
278 bool any_header_sg;
279
280 /* Packet virtio header size */
281 u8 hdr_len;
282
283 /* Work struct for delayed refilling if we run low on memory. */
284 struct delayed_work refill;
285
286 /* Is delayed refill enabled? */
287 bool refill_enabled;
288
289 /* The lock to synchronize the access to refill_enabled */
290 spinlock_t refill_lock;
291
292 /* Work struct for config space updates */
293 struct work_struct config_work;
294
295 /* Does the affinity hint is set for virtqueues? */
296 bool affinity_hint_set;
297
298 /* CPU hotplug instances for online & dead */
299 struct hlist_node node;
300 struct hlist_node node_dead;
301
302 struct control_buf *ctrl;
303
304 /* Ethtool settings */
305 u8 duplex;
306 u32 speed;
307
308 /* Interrupt coalescing settings */
309 struct virtnet_interrupt_coalesce intr_coal_tx;
310 struct virtnet_interrupt_coalesce intr_coal_rx;
311
312 unsigned long guest_offloads;
313 unsigned long guest_offloads_capable;
314
315 /* failover when STANDBY feature enabled */
316 struct failover *failover;
317 };
318
319 struct padded_vnet_hdr {
320 struct virtio_net_hdr_v1_hash hdr;
321 /*
322 * hdr is in a separate sg buffer, and data sg buffer shares same page
323 * with this header sg. This padding makes next sg 16 byte aligned
324 * after the header.
325 */
326 char padding[12];
327 };
328
329 struct virtio_net_common_hdr {
330 union {
331 struct virtio_net_hdr hdr;
332 struct virtio_net_hdr_mrg_rxbuf mrg_hdr;
333 struct virtio_net_hdr_v1_hash hash_v1_hdr;
334 };
335 };
336
337 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
338
is_xdp_frame(void * ptr)339 static bool is_xdp_frame(void *ptr)
340 {
341 return (unsigned long)ptr & VIRTIO_XDP_FLAG;
342 }
343
xdp_to_ptr(struct xdp_frame * ptr)344 static void *xdp_to_ptr(struct xdp_frame *ptr)
345 {
346 return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
347 }
348
ptr_to_xdp(void * ptr)349 static struct xdp_frame *ptr_to_xdp(void *ptr)
350 {
351 return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
352 }
353
354 /* Converting between virtqueue no. and kernel tx/rx queue no.
355 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
356 */
vq2txq(struct virtqueue * vq)357 static int vq2txq(struct virtqueue *vq)
358 {
359 return (vq->index - 1) / 2;
360 }
361
txq2vq(int txq)362 static int txq2vq(int txq)
363 {
364 return txq * 2 + 1;
365 }
366
vq2rxq(struct virtqueue * vq)367 static int vq2rxq(struct virtqueue *vq)
368 {
369 return vq->index / 2;
370 }
371
rxq2vq(int rxq)372 static int rxq2vq(int rxq)
373 {
374 return rxq * 2;
375 }
376
377 static inline struct virtio_net_common_hdr *
skb_vnet_common_hdr(struct sk_buff * skb)378 skb_vnet_common_hdr(struct sk_buff *skb)
379 {
380 return (struct virtio_net_common_hdr *)skb->cb;
381 }
382
383 /*
384 * private is used to chain pages for big packets, put the whole
385 * most recent used list in the beginning for reuse
386 */
give_pages(struct receive_queue * rq,struct page * page)387 static void give_pages(struct receive_queue *rq, struct page *page)
388 {
389 struct page *end;
390
391 /* Find end of list, sew whole thing into vi->rq.pages. */
392 for (end = page; end->private; end = (struct page *)end->private);
393 end->private = (unsigned long)rq->pages;
394 rq->pages = page;
395 }
396
get_a_page(struct receive_queue * rq,gfp_t gfp_mask)397 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
398 {
399 struct page *p = rq->pages;
400
401 if (p) {
402 rq->pages = (struct page *)p->private;
403 /* clear private here, it is used to chain pages */
404 p->private = 0;
405 } else
406 p = alloc_page(gfp_mask);
407 return p;
408 }
409
virtnet_rq_free_buf(struct virtnet_info * vi,struct receive_queue * rq,void * buf)410 static void virtnet_rq_free_buf(struct virtnet_info *vi,
411 struct receive_queue *rq, void *buf)
412 {
413 if (vi->mergeable_rx_bufs)
414 put_page(virt_to_head_page(buf));
415 else if (vi->big_packets)
416 give_pages(rq, buf);
417 else
418 put_page(virt_to_head_page(buf));
419 }
420
enable_delayed_refill(struct virtnet_info * vi)421 static void enable_delayed_refill(struct virtnet_info *vi)
422 {
423 spin_lock_bh(&vi->refill_lock);
424 vi->refill_enabled = true;
425 spin_unlock_bh(&vi->refill_lock);
426 }
427
disable_delayed_refill(struct virtnet_info * vi)428 static void disable_delayed_refill(struct virtnet_info *vi)
429 {
430 spin_lock_bh(&vi->refill_lock);
431 vi->refill_enabled = false;
432 spin_unlock_bh(&vi->refill_lock);
433 }
434
virtqueue_napi_schedule(struct napi_struct * napi,struct virtqueue * vq)435 static void virtqueue_napi_schedule(struct napi_struct *napi,
436 struct virtqueue *vq)
437 {
438 if (napi_schedule_prep(napi)) {
439 virtqueue_disable_cb(vq);
440 __napi_schedule(napi);
441 }
442 }
443
virtqueue_napi_complete(struct napi_struct * napi,struct virtqueue * vq,int processed)444 static void virtqueue_napi_complete(struct napi_struct *napi,
445 struct virtqueue *vq, int processed)
446 {
447 int opaque;
448
449 opaque = virtqueue_enable_cb_prepare(vq);
450 if (napi_complete_done(napi, processed)) {
451 if (unlikely(virtqueue_poll(vq, opaque)))
452 virtqueue_napi_schedule(napi, vq);
453 } else {
454 virtqueue_disable_cb(vq);
455 }
456 }
457
skb_xmit_done(struct virtqueue * vq)458 static void skb_xmit_done(struct virtqueue *vq)
459 {
460 struct virtnet_info *vi = vq->vdev->priv;
461 struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
462
463 /* Suppress further interrupts. */
464 virtqueue_disable_cb(vq);
465
466 if (napi->weight)
467 virtqueue_napi_schedule(napi, vq);
468 else
469 /* We were probably waiting for more output buffers. */
470 netif_wake_subqueue(vi->dev, vq2txq(vq));
471 }
472
473 #define MRG_CTX_HEADER_SHIFT 22
mergeable_len_to_ctx(unsigned int truesize,unsigned int headroom)474 static void *mergeable_len_to_ctx(unsigned int truesize,
475 unsigned int headroom)
476 {
477 return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
478 }
479
mergeable_ctx_to_headroom(void * mrg_ctx)480 static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
481 {
482 return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
483 }
484
mergeable_ctx_to_truesize(void * mrg_ctx)485 static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
486 {
487 return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
488 }
489
check_mergeable_len(struct net_device * dev,void * mrg_ctx,unsigned int len)490 static int check_mergeable_len(struct net_device *dev, void *mrg_ctx,
491 unsigned int len)
492 {
493 unsigned int headroom, tailroom, room, truesize;
494
495 truesize = mergeable_ctx_to_truesize(mrg_ctx);
496 headroom = mergeable_ctx_to_headroom(mrg_ctx);
497 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
498 room = SKB_DATA_ALIGN(headroom + tailroom);
499
500 if (len > truesize - room) {
501 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
502 dev->name, len, (unsigned long)(truesize - room));
503 DEV_STATS_INC(dev, rx_length_errors);
504 return -1;
505 }
506
507 return 0;
508 }
509
virtnet_build_skb(void * buf,unsigned int buflen,unsigned int headroom,unsigned int len)510 static struct sk_buff *virtnet_build_skb(void *buf, unsigned int buflen,
511 unsigned int headroom,
512 unsigned int len)
513 {
514 struct sk_buff *skb;
515
516 skb = build_skb(buf, buflen);
517 if (unlikely(!skb))
518 return NULL;
519
520 skb_reserve(skb, headroom);
521 skb_put(skb, len);
522
523 return skb;
524 }
525
526 /* 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)527 static struct sk_buff *page_to_skb(struct virtnet_info *vi,
528 struct receive_queue *rq,
529 struct page *page, unsigned int offset,
530 unsigned int len, unsigned int truesize,
531 unsigned int headroom)
532 {
533 struct sk_buff *skb;
534 struct virtio_net_common_hdr *hdr;
535 unsigned int copy, hdr_len, hdr_padded_len;
536 struct page *page_to_free = NULL;
537 int tailroom, shinfo_size;
538 char *p, *hdr_p, *buf;
539
540 p = page_address(page) + offset;
541 hdr_p = p;
542
543 hdr_len = vi->hdr_len;
544 if (vi->mergeable_rx_bufs)
545 hdr_padded_len = hdr_len;
546 else
547 hdr_padded_len = sizeof(struct padded_vnet_hdr);
548
549 buf = p - headroom;
550 len -= hdr_len;
551 offset += hdr_padded_len;
552 p += hdr_padded_len;
553 tailroom = truesize - headroom - hdr_padded_len - len;
554
555 shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
556
557 /* copy small packet so we can reuse these pages */
558 if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
559 skb = virtnet_build_skb(buf, truesize, p - buf, len);
560 if (unlikely(!skb))
561 return NULL;
562
563 page = (struct page *)page->private;
564 if (page)
565 give_pages(rq, page);
566 goto ok;
567 }
568
569 /* copy small packet so we can reuse these pages for small data */
570 skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
571 if (unlikely(!skb))
572 return NULL;
573
574 /* Copy all frame if it fits skb->head, otherwise
575 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
576 */
577 if (len <= skb_tailroom(skb))
578 copy = len;
579 else
580 copy = ETH_HLEN;
581 skb_put_data(skb, p, copy);
582
583 len -= copy;
584 offset += copy;
585
586 if (vi->mergeable_rx_bufs) {
587 if (len)
588 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
589 else
590 page_to_free = page;
591 goto ok;
592 }
593
594 /*
595 * Verify that we can indeed put this data into a skb.
596 * This is here to handle cases when the device erroneously
597 * tries to receive more than is possible. This is usually
598 * the case of a broken device.
599 */
600 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
601 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
602 dev_kfree_skb(skb);
603 return NULL;
604 }
605 BUG_ON(offset >= PAGE_SIZE);
606 while (len) {
607 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
608 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
609 frag_size, truesize);
610 len -= frag_size;
611 page = (struct page *)page->private;
612 offset = 0;
613 }
614
615 if (page)
616 give_pages(rq, page);
617
618 ok:
619 hdr = skb_vnet_common_hdr(skb);
620 memcpy(hdr, hdr_p, hdr_len);
621 if (page_to_free)
622 put_page(page_to_free);
623
624 return skb;
625 }
626
virtnet_rq_unmap(struct receive_queue * rq,void * buf,u32 len)627 static void virtnet_rq_unmap(struct receive_queue *rq, void *buf, u32 len)
628 {
629 struct page *page = virt_to_head_page(buf);
630 struct virtnet_rq_dma *dma;
631 void *head;
632 int offset;
633
634 head = page_address(page);
635
636 dma = head;
637
638 --dma->ref;
639
640 if (dma->need_sync && len) {
641 offset = buf - (head + sizeof(*dma));
642
643 virtqueue_dma_sync_single_range_for_cpu(rq->vq, dma->addr,
644 offset, len,
645 DMA_FROM_DEVICE);
646 }
647
648 if (dma->ref)
649 return;
650
651 virtqueue_dma_unmap_single_attrs(rq->vq, dma->addr, dma->len,
652 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
653 put_page(page);
654 }
655
virtnet_rq_get_buf(struct receive_queue * rq,u32 * len,void ** ctx)656 static void *virtnet_rq_get_buf(struct receive_queue *rq, u32 *len, void **ctx)
657 {
658 void *buf;
659
660 buf = virtqueue_get_buf_ctx(rq->vq, len, ctx);
661 if (buf && rq->do_dma)
662 virtnet_rq_unmap(rq, buf, *len);
663
664 return buf;
665 }
666
virtnet_rq_init_one_sg(struct receive_queue * rq,void * buf,u32 len)667 static void virtnet_rq_init_one_sg(struct receive_queue *rq, void *buf, u32 len)
668 {
669 struct virtnet_rq_dma *dma;
670 dma_addr_t addr;
671 u32 offset;
672 void *head;
673
674 if (!rq->do_dma) {
675 sg_init_one(rq->sg, buf, len);
676 return;
677 }
678
679 head = page_address(rq->alloc_frag.page);
680
681 offset = buf - head;
682
683 dma = head;
684
685 addr = dma->addr - sizeof(*dma) + offset;
686
687 sg_init_table(rq->sg, 1);
688 rq->sg[0].dma_address = addr;
689 rq->sg[0].length = len;
690 }
691
virtnet_rq_alloc(struct receive_queue * rq,u32 size,gfp_t gfp)692 static void *virtnet_rq_alloc(struct receive_queue *rq, u32 size, gfp_t gfp)
693 {
694 struct page_frag *alloc_frag = &rq->alloc_frag;
695 struct virtnet_rq_dma *dma;
696 void *buf, *head;
697 dma_addr_t addr;
698
699 head = page_address(alloc_frag->page);
700
701 if (rq->do_dma) {
702 dma = head;
703
704 /* new pages */
705 if (!alloc_frag->offset) {
706 if (rq->last_dma) {
707 /* Now, the new page is allocated, the last dma
708 * will not be used. So the dma can be unmapped
709 * if the ref is 0.
710 */
711 virtnet_rq_unmap(rq, rq->last_dma, 0);
712 rq->last_dma = NULL;
713 }
714
715 dma->len = alloc_frag->size - sizeof(*dma);
716
717 addr = virtqueue_dma_map_single_attrs(rq->vq, dma + 1,
718 dma->len, DMA_FROM_DEVICE, 0);
719 if (virtqueue_dma_mapping_error(rq->vq, addr))
720 return NULL;
721
722 dma->addr = addr;
723 dma->need_sync = virtqueue_dma_need_sync(rq->vq, addr);
724
725 /* Add a reference to dma to prevent the entire dma from
726 * being released during error handling. This reference
727 * will be freed after the pages are no longer used.
728 */
729 get_page(alloc_frag->page);
730 dma->ref = 1;
731 alloc_frag->offset = sizeof(*dma);
732
733 rq->last_dma = dma;
734 }
735
736 ++dma->ref;
737 }
738
739 buf = head + alloc_frag->offset;
740
741 get_page(alloc_frag->page);
742 alloc_frag->offset += size;
743
744 return buf;
745 }
746
virtnet_rq_set_premapped(struct virtnet_info * vi)747 static void virtnet_rq_set_premapped(struct virtnet_info *vi)
748 {
749 int i;
750
751 /* disable for big mode */
752 if (!vi->mergeable_rx_bufs && vi->big_packets)
753 return;
754
755 for (i = 0; i < vi->max_queue_pairs; i++) {
756 if (virtqueue_set_dma_premapped(vi->rq[i].vq))
757 continue;
758
759 vi->rq[i].do_dma = true;
760 }
761 }
762
virtnet_rq_unmap_free_buf(struct virtqueue * vq,void * buf)763 static void virtnet_rq_unmap_free_buf(struct virtqueue *vq, void *buf)
764 {
765 struct virtnet_info *vi = vq->vdev->priv;
766 struct receive_queue *rq;
767 int i = vq2rxq(vq);
768
769 rq = &vi->rq[i];
770
771 if (rq->do_dma)
772 virtnet_rq_unmap(rq, buf, 0);
773
774 virtnet_rq_free_buf(vi, rq, buf);
775 }
776
free_old_xmit_skbs(struct send_queue * sq,bool in_napi)777 static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
778 {
779 unsigned int len;
780 unsigned int packets = 0;
781 unsigned int bytes = 0;
782 void *ptr;
783
784 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
785 if (likely(!is_xdp_frame(ptr))) {
786 struct sk_buff *skb = ptr;
787
788 pr_debug("Sent skb %p\n", skb);
789
790 bytes += skb->len;
791 napi_consume_skb(skb, in_napi);
792 } else {
793 struct xdp_frame *frame = ptr_to_xdp(ptr);
794
795 bytes += xdp_get_frame_len(frame);
796 xdp_return_frame(frame);
797 }
798 packets++;
799 }
800
801 /* Avoid overhead when no packets have been processed
802 * happens when called speculatively from start_xmit.
803 */
804 if (!packets)
805 return;
806
807 u64_stats_update_begin(&sq->stats.syncp);
808 u64_stats_add(&sq->stats.bytes, bytes);
809 u64_stats_add(&sq->stats.packets, packets);
810 u64_stats_update_end(&sq->stats.syncp);
811 }
812
is_xdp_raw_buffer_queue(struct virtnet_info * vi,int q)813 static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
814 {
815 if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
816 return false;
817 else if (q < vi->curr_queue_pairs)
818 return true;
819 else
820 return false;
821 }
822
check_sq_full_and_disable(struct virtnet_info * vi,struct net_device * dev,struct send_queue * sq)823 static void check_sq_full_and_disable(struct virtnet_info *vi,
824 struct net_device *dev,
825 struct send_queue *sq)
826 {
827 bool use_napi = sq->napi.weight;
828 int qnum;
829
830 qnum = sq - vi->sq;
831
832 /* If running out of space, stop queue to avoid getting packets that we
833 * are then unable to transmit.
834 * An alternative would be to force queuing layer to requeue the skb by
835 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
836 * returned in a normal path of operation: it means that driver is not
837 * maintaining the TX queue stop/start state properly, and causes
838 * the stack to do a non-trivial amount of useless work.
839 * Since most packets only take 1 or 2 ring slots, stopping the queue
840 * early means 16 slots are typically wasted.
841 */
842 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
843 netif_stop_subqueue(dev, qnum);
844 if (use_napi) {
845 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
846 virtqueue_napi_schedule(&sq->napi, sq->vq);
847 } else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
848 /* More just got used, free them then recheck. */
849 free_old_xmit_skbs(sq, false);
850 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
851 netif_start_subqueue(dev, qnum);
852 virtqueue_disable_cb(sq->vq);
853 }
854 }
855 }
856 }
857
__virtnet_xdp_xmit_one(struct virtnet_info * vi,struct send_queue * sq,struct xdp_frame * xdpf)858 static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
859 struct send_queue *sq,
860 struct xdp_frame *xdpf)
861 {
862 struct virtio_net_hdr_mrg_rxbuf *hdr;
863 struct skb_shared_info *shinfo;
864 u8 nr_frags = 0;
865 int err, i;
866
867 if (unlikely(xdpf->headroom < vi->hdr_len))
868 return -EOVERFLOW;
869
870 if (unlikely(xdp_frame_has_frags(xdpf))) {
871 shinfo = xdp_get_shared_info_from_frame(xdpf);
872 nr_frags = shinfo->nr_frags;
873 }
874
875 /* In wrapping function virtnet_xdp_xmit(), we need to free
876 * up the pending old buffers, where we need to calculate the
877 * position of skb_shared_info in xdp_get_frame_len() and
878 * xdp_return_frame(), which will involve to xdpf->data and
879 * xdpf->headroom. Therefore, we need to update the value of
880 * headroom synchronously here.
881 */
882 xdpf->headroom -= vi->hdr_len;
883 xdpf->data -= vi->hdr_len;
884 /* Zero header and leave csum up to XDP layers */
885 hdr = xdpf->data;
886 memset(hdr, 0, vi->hdr_len);
887 xdpf->len += vi->hdr_len;
888
889 sg_init_table(sq->sg, nr_frags + 1);
890 sg_set_buf(sq->sg, xdpf->data, xdpf->len);
891 for (i = 0; i < nr_frags; i++) {
892 skb_frag_t *frag = &shinfo->frags[i];
893
894 sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
895 skb_frag_size(frag), skb_frag_off(frag));
896 }
897
898 err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
899 xdp_to_ptr(xdpf), GFP_ATOMIC);
900 if (unlikely(err))
901 return -ENOSPC; /* Caller handle free/refcnt */
902
903 return 0;
904 }
905
906 /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
907 * the current cpu, so it does not need to be locked.
908 *
909 * Here we use marco instead of inline functions because we have to deal with
910 * three issues at the same time: 1. the choice of sq. 2. judge and execute the
911 * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
912 * functions to perfectly solve these three problems at the same time.
913 */
914 #define virtnet_xdp_get_sq(vi) ({ \
915 int cpu = smp_processor_id(); \
916 struct netdev_queue *txq; \
917 typeof(vi) v = (vi); \
918 unsigned int qp; \
919 \
920 if (v->curr_queue_pairs > nr_cpu_ids) { \
921 qp = v->curr_queue_pairs - v->xdp_queue_pairs; \
922 qp += cpu; \
923 txq = netdev_get_tx_queue(v->dev, qp); \
924 __netif_tx_acquire(txq); \
925 } else { \
926 qp = cpu % v->curr_queue_pairs; \
927 txq = netdev_get_tx_queue(v->dev, qp); \
928 __netif_tx_lock(txq, cpu); \
929 } \
930 v->sq + qp; \
931 })
932
933 #define virtnet_xdp_put_sq(vi, q) { \
934 struct netdev_queue *txq; \
935 typeof(vi) v = (vi); \
936 \
937 txq = netdev_get_tx_queue(v->dev, (q) - v->sq); \
938 if (v->curr_queue_pairs > nr_cpu_ids) \
939 __netif_tx_release(txq); \
940 else \
941 __netif_tx_unlock(txq); \
942 }
943
virtnet_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)944 static int virtnet_xdp_xmit(struct net_device *dev,
945 int n, struct xdp_frame **frames, u32 flags)
946 {
947 struct virtnet_info *vi = netdev_priv(dev);
948 struct receive_queue *rq = vi->rq;
949 struct bpf_prog *xdp_prog;
950 struct send_queue *sq;
951 unsigned int len;
952 int packets = 0;
953 int bytes = 0;
954 int nxmit = 0;
955 int kicks = 0;
956 void *ptr;
957 int ret;
958 int i;
959
960 /* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
961 * indicate XDP resources have been successfully allocated.
962 */
963 xdp_prog = rcu_access_pointer(rq->xdp_prog);
964 if (!xdp_prog)
965 return -ENXIO;
966
967 sq = virtnet_xdp_get_sq(vi);
968
969 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
970 ret = -EINVAL;
971 goto out;
972 }
973
974 /* Free up any pending old buffers before queueing new ones. */
975 while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
976 if (likely(is_xdp_frame(ptr))) {
977 struct xdp_frame *frame = ptr_to_xdp(ptr);
978
979 bytes += xdp_get_frame_len(frame);
980 xdp_return_frame(frame);
981 } else {
982 struct sk_buff *skb = ptr;
983
984 bytes += skb->len;
985 napi_consume_skb(skb, false);
986 }
987 packets++;
988 }
989
990 for (i = 0; i < n; i++) {
991 struct xdp_frame *xdpf = frames[i];
992
993 if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
994 break;
995 nxmit++;
996 }
997 ret = nxmit;
998
999 if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
1000 check_sq_full_and_disable(vi, dev, sq);
1001
1002 if (flags & XDP_XMIT_FLUSH) {
1003 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
1004 kicks = 1;
1005 }
1006 out:
1007 u64_stats_update_begin(&sq->stats.syncp);
1008 u64_stats_add(&sq->stats.bytes, bytes);
1009 u64_stats_add(&sq->stats.packets, packets);
1010 u64_stats_add(&sq->stats.xdp_tx, n);
1011 u64_stats_add(&sq->stats.xdp_tx_drops, n - nxmit);
1012 u64_stats_add(&sq->stats.kicks, kicks);
1013 u64_stats_update_end(&sq->stats.syncp);
1014
1015 virtnet_xdp_put_sq(vi, sq);
1016 return ret;
1017 }
1018
put_xdp_frags(struct xdp_buff * xdp)1019 static void put_xdp_frags(struct xdp_buff *xdp)
1020 {
1021 struct skb_shared_info *shinfo;
1022 struct page *xdp_page;
1023 int i;
1024
1025 if (xdp_buff_has_frags(xdp)) {
1026 shinfo = xdp_get_shared_info_from_buff(xdp);
1027 for (i = 0; i < shinfo->nr_frags; i++) {
1028 xdp_page = skb_frag_page(&shinfo->frags[i]);
1029 put_page(xdp_page);
1030 }
1031 }
1032 }
1033
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)1034 static int virtnet_xdp_handler(struct bpf_prog *xdp_prog, struct xdp_buff *xdp,
1035 struct net_device *dev,
1036 unsigned int *xdp_xmit,
1037 struct virtnet_rq_stats *stats)
1038 {
1039 struct xdp_frame *xdpf;
1040 int err;
1041 u32 act;
1042
1043 act = bpf_prog_run_xdp(xdp_prog, xdp);
1044 u64_stats_inc(&stats->xdp_packets);
1045
1046 switch (act) {
1047 case XDP_PASS:
1048 return act;
1049
1050 case XDP_TX:
1051 u64_stats_inc(&stats->xdp_tx);
1052 xdpf = xdp_convert_buff_to_frame(xdp);
1053 if (unlikely(!xdpf)) {
1054 netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1055 return XDP_DROP;
1056 }
1057
1058 err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1059 if (unlikely(!err)) {
1060 xdp_return_frame_rx_napi(xdpf);
1061 } else if (unlikely(err < 0)) {
1062 trace_xdp_exception(dev, xdp_prog, act);
1063 return XDP_DROP;
1064 }
1065 *xdp_xmit |= VIRTIO_XDP_TX;
1066 return act;
1067
1068 case XDP_REDIRECT:
1069 u64_stats_inc(&stats->xdp_redirects);
1070 err = xdp_do_redirect(dev, xdp, xdp_prog);
1071 if (err)
1072 return XDP_DROP;
1073
1074 *xdp_xmit |= VIRTIO_XDP_REDIR;
1075 return act;
1076
1077 default:
1078 bpf_warn_invalid_xdp_action(dev, xdp_prog, act);
1079 fallthrough;
1080 case XDP_ABORTED:
1081 trace_xdp_exception(dev, xdp_prog, act);
1082 fallthrough;
1083 case XDP_DROP:
1084 return XDP_DROP;
1085 }
1086 }
1087
virtnet_get_headroom(struct virtnet_info * vi)1088 static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
1089 {
1090 return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
1091 }
1092
1093 /* We copy the packet for XDP in the following cases:
1094 *
1095 * 1) Packet is scattered across multiple rx buffers.
1096 * 2) Headroom space is insufficient.
1097 *
1098 * This is inefficient but it's a temporary condition that
1099 * we hit right after XDP is enabled and until queue is refilled
1100 * with large buffers with sufficient headroom - so it should affect
1101 * at most queue size packets.
1102 * Afterwards, the conditions to enable
1103 * XDP should preclude the underlying device from sending packets
1104 * across multiple buffers (num_buf > 1), and we make sure buffers
1105 * have enough headroom.
1106 */
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)1107 static struct page *xdp_linearize_page(struct net_device *dev,
1108 struct receive_queue *rq,
1109 int *num_buf,
1110 struct page *p,
1111 int offset,
1112 int page_off,
1113 unsigned int *len)
1114 {
1115 int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1116 struct page *page;
1117
1118 if (page_off + *len + tailroom > PAGE_SIZE)
1119 return NULL;
1120
1121 page = alloc_page(GFP_ATOMIC);
1122 if (!page)
1123 return NULL;
1124
1125 memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
1126 page_off += *len;
1127
1128 /* Only mergeable mode can go inside this while loop. In small mode,
1129 * *num_buf == 1, so it cannot go inside.
1130 */
1131 while (--*num_buf) {
1132 unsigned int buflen;
1133 void *buf;
1134 void *ctx;
1135 int off;
1136
1137 buf = virtnet_rq_get_buf(rq, &buflen, &ctx);
1138 if (unlikely(!buf))
1139 goto err_buf;
1140
1141 p = virt_to_head_page(buf);
1142 off = buf - page_address(p);
1143
1144 if (check_mergeable_len(dev, ctx, buflen)) {
1145 put_page(p);
1146 goto err_buf;
1147 }
1148
1149 /* guard against a misconfigured or uncooperative backend that
1150 * is sending packet larger than the MTU.
1151 */
1152 if ((page_off + buflen + tailroom) > PAGE_SIZE) {
1153 put_page(p);
1154 goto err_buf;
1155 }
1156
1157 memcpy(page_address(page) + page_off,
1158 page_address(p) + off, buflen);
1159 page_off += buflen;
1160 put_page(p);
1161 }
1162
1163 /* Headroom does not contribute to packet length */
1164 *len = page_off - VIRTIO_XDP_HEADROOM;
1165 return page;
1166 err_buf:
1167 __free_pages(page, 0);
1168 return NULL;
1169 }
1170
receive_small_build_skb(struct virtnet_info * vi,unsigned int xdp_headroom,void * buf,unsigned int len)1171 static struct sk_buff *receive_small_build_skb(struct virtnet_info *vi,
1172 unsigned int xdp_headroom,
1173 void *buf,
1174 unsigned int len)
1175 {
1176 unsigned int header_offset;
1177 unsigned int headroom;
1178 unsigned int buflen;
1179 struct sk_buff *skb;
1180
1181 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1182 headroom = vi->hdr_len + header_offset;
1183 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1184 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1185
1186 skb = virtnet_build_skb(buf, buflen, headroom, len);
1187 if (unlikely(!skb))
1188 return NULL;
1189
1190 buf += header_offset;
1191 memcpy(skb_vnet_common_hdr(skb), buf, vi->hdr_len);
1192
1193 return skb;
1194 }
1195
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 * xdp_xmit,struct virtnet_rq_stats * stats)1196 static struct sk_buff *receive_small_xdp(struct net_device *dev,
1197 struct virtnet_info *vi,
1198 struct receive_queue *rq,
1199 struct bpf_prog *xdp_prog,
1200 void *buf,
1201 unsigned int xdp_headroom,
1202 unsigned int len,
1203 unsigned int *xdp_xmit,
1204 struct virtnet_rq_stats *stats)
1205 {
1206 unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
1207 unsigned int headroom = vi->hdr_len + header_offset;
1208 struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
1209 struct page *page = virt_to_head_page(buf);
1210 struct page *xdp_page;
1211 unsigned int buflen;
1212 struct xdp_buff xdp;
1213 struct sk_buff *skb;
1214 unsigned int metasize = 0;
1215 u32 act;
1216
1217 if (unlikely(hdr->hdr.gso_type))
1218 goto err_xdp;
1219
1220 /* Partially checksummed packets must be dropped. */
1221 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1222 goto err_xdp;
1223
1224 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1225 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1226
1227 if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
1228 int offset = buf - page_address(page) + header_offset;
1229 unsigned int tlen = len + vi->hdr_len;
1230 int num_buf = 1;
1231
1232 xdp_headroom = virtnet_get_headroom(vi);
1233 header_offset = VIRTNET_RX_PAD + xdp_headroom;
1234 headroom = vi->hdr_len + header_offset;
1235 buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
1236 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1237 xdp_page = xdp_linearize_page(dev, rq, &num_buf, page,
1238 offset, header_offset,
1239 &tlen);
1240 if (!xdp_page)
1241 goto err_xdp;
1242
1243 buf = page_address(xdp_page);
1244 put_page(page);
1245 page = xdp_page;
1246 }
1247
1248 xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
1249 xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
1250 xdp_headroom, len, true);
1251
1252 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1253
1254 switch (act) {
1255 case XDP_PASS:
1256 /* Recalculate length in case bpf program changed it */
1257 len = xdp.data_end - xdp.data;
1258 metasize = xdp.data - xdp.data_meta;
1259 break;
1260
1261 case XDP_TX:
1262 case XDP_REDIRECT:
1263 goto xdp_xmit;
1264
1265 default:
1266 goto err_xdp;
1267 }
1268
1269 skb = virtnet_build_skb(buf, buflen, xdp.data - buf, len);
1270 if (unlikely(!skb))
1271 goto err;
1272
1273 if (metasize)
1274 skb_metadata_set(skb, metasize);
1275
1276 return skb;
1277
1278 err_xdp:
1279 u64_stats_inc(&stats->xdp_drops);
1280 err:
1281 u64_stats_inc(&stats->drops);
1282 put_page(page);
1283 xdp_xmit:
1284 return NULL;
1285 }
1286
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)1287 static struct sk_buff *receive_small(struct net_device *dev,
1288 struct virtnet_info *vi,
1289 struct receive_queue *rq,
1290 void *buf, void *ctx,
1291 unsigned int len,
1292 unsigned int *xdp_xmit,
1293 struct virtnet_rq_stats *stats)
1294 {
1295 unsigned int xdp_headroom = (unsigned long)ctx;
1296 struct page *page = virt_to_head_page(buf);
1297 struct sk_buff *skb;
1298
1299 /* We passed the address of virtnet header to virtio-core,
1300 * so truncate the padding.
1301 */
1302 buf -= VIRTNET_RX_PAD + xdp_headroom;
1303
1304 len -= vi->hdr_len;
1305 u64_stats_add(&stats->bytes, len);
1306
1307 if (unlikely(len > GOOD_PACKET_LEN)) {
1308 pr_debug("%s: rx error: len %u exceeds max size %d\n",
1309 dev->name, len, GOOD_PACKET_LEN);
1310 DEV_STATS_INC(dev, rx_length_errors);
1311 goto err;
1312 }
1313
1314 if (unlikely(vi->xdp_enabled)) {
1315 struct bpf_prog *xdp_prog;
1316
1317 rcu_read_lock();
1318 xdp_prog = rcu_dereference(rq->xdp_prog);
1319 if (xdp_prog) {
1320 skb = receive_small_xdp(dev, vi, rq, xdp_prog, buf,
1321 xdp_headroom, len, xdp_xmit,
1322 stats);
1323 rcu_read_unlock();
1324 return skb;
1325 }
1326 rcu_read_unlock();
1327 }
1328
1329 skb = receive_small_build_skb(vi, xdp_headroom, buf, len);
1330 if (likely(skb))
1331 return skb;
1332
1333 err:
1334 u64_stats_inc(&stats->drops);
1335 put_page(page);
1336 return NULL;
1337 }
1338
receive_big(struct net_device * dev,struct virtnet_info * vi,struct receive_queue * rq,void * buf,unsigned int len,struct virtnet_rq_stats * stats)1339 static struct sk_buff *receive_big(struct net_device *dev,
1340 struct virtnet_info *vi,
1341 struct receive_queue *rq,
1342 void *buf,
1343 unsigned int len,
1344 struct virtnet_rq_stats *stats)
1345 {
1346 struct page *page = buf;
1347 struct sk_buff *skb =
1348 page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
1349
1350 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1351 if (unlikely(!skb))
1352 goto err;
1353
1354 return skb;
1355
1356 err:
1357 u64_stats_inc(&stats->drops);
1358 give_pages(rq, page);
1359 return NULL;
1360 }
1361
mergeable_buf_free(struct receive_queue * rq,int num_buf,struct net_device * dev,struct virtnet_rq_stats * stats)1362 static void mergeable_buf_free(struct receive_queue *rq, int num_buf,
1363 struct net_device *dev,
1364 struct virtnet_rq_stats *stats)
1365 {
1366 struct page *page;
1367 void *buf;
1368 int len;
1369
1370 while (num_buf-- > 1) {
1371 buf = virtnet_rq_get_buf(rq, &len, NULL);
1372 if (unlikely(!buf)) {
1373 pr_debug("%s: rx error: %d buffers missing\n",
1374 dev->name, num_buf);
1375 DEV_STATS_INC(dev, rx_length_errors);
1376 break;
1377 }
1378 u64_stats_add(&stats->bytes, len);
1379 page = virt_to_head_page(buf);
1380 put_page(page);
1381 }
1382 }
1383
1384 /* Why not use xdp_build_skb_from_frame() ?
1385 * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1386 * virtio-net there are 2 points that do not match its requirements:
1387 * 1. The size of the prefilled buffer is not fixed before xdp is set.
1388 * 2. xdp_build_skb_from_frame() does more checks that we don't need,
1389 * like eth_type_trans() (which virtio-net does in receive_buf()).
1390 */
build_skb_from_xdp_buff(struct net_device * dev,struct virtnet_info * vi,struct xdp_buff * xdp,unsigned int xdp_frags_truesz)1391 static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1392 struct virtnet_info *vi,
1393 struct xdp_buff *xdp,
1394 unsigned int xdp_frags_truesz)
1395 {
1396 struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1397 unsigned int headroom, data_len;
1398 struct sk_buff *skb;
1399 int metasize;
1400 u8 nr_frags;
1401
1402 if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1403 pr_debug("Error building skb as missing reserved tailroom for xdp");
1404 return NULL;
1405 }
1406
1407 if (unlikely(xdp_buff_has_frags(xdp)))
1408 nr_frags = sinfo->nr_frags;
1409
1410 skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1411 if (unlikely(!skb))
1412 return NULL;
1413
1414 headroom = xdp->data - xdp->data_hard_start;
1415 data_len = xdp->data_end - xdp->data;
1416 skb_reserve(skb, headroom);
1417 __skb_put(skb, data_len);
1418
1419 metasize = xdp->data - xdp->data_meta;
1420 metasize = metasize > 0 ? metasize : 0;
1421 if (metasize)
1422 skb_metadata_set(skb, metasize);
1423
1424 if (unlikely(xdp_buff_has_frags(xdp)))
1425 xdp_update_skb_shared_info(skb, nr_frags,
1426 sinfo->xdp_frags_size,
1427 xdp_frags_truesz,
1428 xdp_buff_is_frag_pfmemalloc(xdp));
1429
1430 return skb;
1431 }
1432
1433 /* 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)1434 static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1435 struct virtnet_info *vi,
1436 struct receive_queue *rq,
1437 struct xdp_buff *xdp,
1438 void *buf,
1439 unsigned int len,
1440 unsigned int frame_sz,
1441 int *num_buf,
1442 unsigned int *xdp_frags_truesize,
1443 struct virtnet_rq_stats *stats)
1444 {
1445 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1446 unsigned int headroom, tailroom, room;
1447 unsigned int truesize, cur_frag_size;
1448 struct skb_shared_info *shinfo;
1449 unsigned int xdp_frags_truesz = 0;
1450 struct page *page;
1451 skb_frag_t *frag;
1452 int offset;
1453 void *ctx;
1454
1455 xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1456 xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1457 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1458
1459 if (!*num_buf)
1460 return 0;
1461
1462 if (*num_buf > 1) {
1463 /* If we want to build multi-buffer xdp, we need
1464 * to specify that the flags of xdp_buff have the
1465 * XDP_FLAGS_HAS_FRAG bit.
1466 */
1467 if (!xdp_buff_has_frags(xdp))
1468 xdp_buff_set_frags_flag(xdp);
1469
1470 shinfo = xdp_get_shared_info_from_buff(xdp);
1471 shinfo->nr_frags = 0;
1472 shinfo->xdp_frags_size = 0;
1473 }
1474
1475 if (*num_buf > MAX_SKB_FRAGS + 1)
1476 return -EINVAL;
1477
1478 while (--*num_buf > 0) {
1479 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1480 if (unlikely(!buf)) {
1481 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1482 dev->name, *num_buf,
1483 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1484 DEV_STATS_INC(dev, rx_length_errors);
1485 goto err;
1486 }
1487
1488 u64_stats_add(&stats->bytes, len);
1489 page = virt_to_head_page(buf);
1490 offset = buf - page_address(page);
1491
1492 truesize = mergeable_ctx_to_truesize(ctx);
1493 headroom = mergeable_ctx_to_headroom(ctx);
1494 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1495 room = SKB_DATA_ALIGN(headroom + tailroom);
1496
1497 cur_frag_size = truesize;
1498 xdp_frags_truesz += cur_frag_size;
1499 if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1500 put_page(page);
1501 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1502 dev->name, len, (unsigned long)(truesize - room));
1503 DEV_STATS_INC(dev, rx_length_errors);
1504 goto err;
1505 }
1506
1507 frag = &shinfo->frags[shinfo->nr_frags++];
1508 skb_frag_fill_page_desc(frag, page, offset, len);
1509 if (page_is_pfmemalloc(page))
1510 xdp_buff_set_frag_pfmemalloc(xdp);
1511
1512 shinfo->xdp_frags_size += len;
1513 }
1514
1515 *xdp_frags_truesize = xdp_frags_truesz;
1516 return 0;
1517
1518 err:
1519 put_xdp_frags(xdp);
1520 return -EINVAL;
1521 }
1522
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)1523 static void *mergeable_xdp_get_buf(struct virtnet_info *vi,
1524 struct receive_queue *rq,
1525 struct bpf_prog *xdp_prog,
1526 void *ctx,
1527 unsigned int *frame_sz,
1528 int *num_buf,
1529 struct page **page,
1530 int offset,
1531 unsigned int *len,
1532 struct virtio_net_hdr_mrg_rxbuf *hdr)
1533 {
1534 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1535 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1536 struct page *xdp_page;
1537 unsigned int xdp_room;
1538
1539 /* Transient failure which in theory could occur if
1540 * in-flight packets from before XDP was enabled reach
1541 * the receive path after XDP is loaded.
1542 */
1543 if (unlikely(hdr->hdr.gso_type))
1544 return NULL;
1545
1546 /* Partially checksummed packets must be dropped. */
1547 if (unlikely(hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM))
1548 return NULL;
1549
1550 /* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1551 * with headroom may add hole in truesize, which
1552 * make their length exceed PAGE_SIZE. So we disabled the
1553 * hole mechanism for xdp. See add_recvbuf_mergeable().
1554 */
1555 *frame_sz = truesize;
1556
1557 if (likely(headroom >= virtnet_get_headroom(vi) &&
1558 (*num_buf == 1 || xdp_prog->aux->xdp_has_frags))) {
1559 return page_address(*page) + offset;
1560 }
1561
1562 /* This happens when headroom is not enough because
1563 * of the buffer was prefilled before XDP is set.
1564 * This should only happen for the first several packets.
1565 * In fact, vq reset can be used here to help us clean up
1566 * the prefilled buffers, but many existing devices do not
1567 * support it, and we don't want to bother users who are
1568 * using xdp normally.
1569 */
1570 if (!xdp_prog->aux->xdp_has_frags) {
1571 /* linearize data for XDP */
1572 xdp_page = xdp_linearize_page(vi->dev, rq, num_buf,
1573 *page, offset,
1574 VIRTIO_XDP_HEADROOM,
1575 len);
1576 if (!xdp_page)
1577 return NULL;
1578 } else {
1579 xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
1580 sizeof(struct skb_shared_info));
1581 if (*len + xdp_room > PAGE_SIZE)
1582 return NULL;
1583
1584 xdp_page = alloc_page(GFP_ATOMIC);
1585 if (!xdp_page)
1586 return NULL;
1587
1588 memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
1589 page_address(*page) + offset, *len);
1590 }
1591
1592 *frame_sz = PAGE_SIZE;
1593
1594 put_page(*page);
1595
1596 *page = xdp_page;
1597
1598 return page_address(*page) + VIRTIO_XDP_HEADROOM;
1599 }
1600
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)1601 static struct sk_buff *receive_mergeable_xdp(struct net_device *dev,
1602 struct virtnet_info *vi,
1603 struct receive_queue *rq,
1604 struct bpf_prog *xdp_prog,
1605 void *buf,
1606 void *ctx,
1607 unsigned int len,
1608 unsigned int *xdp_xmit,
1609 struct virtnet_rq_stats *stats)
1610 {
1611 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1612 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1613 struct page *page = virt_to_head_page(buf);
1614 int offset = buf - page_address(page);
1615 unsigned int xdp_frags_truesz = 0;
1616 struct sk_buff *head_skb;
1617 unsigned int frame_sz;
1618 struct xdp_buff xdp;
1619 void *data;
1620 u32 act;
1621 int err;
1622
1623 data = mergeable_xdp_get_buf(vi, rq, xdp_prog, ctx, &frame_sz, &num_buf, &page,
1624 offset, &len, hdr);
1625 if (unlikely(!data))
1626 goto err_xdp;
1627
1628 err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
1629 &num_buf, &xdp_frags_truesz, stats);
1630 if (unlikely(err))
1631 goto err_xdp;
1632
1633 act = virtnet_xdp_handler(xdp_prog, &xdp, dev, xdp_xmit, stats);
1634
1635 switch (act) {
1636 case XDP_PASS:
1637 head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
1638 if (unlikely(!head_skb))
1639 break;
1640 return head_skb;
1641
1642 case XDP_TX:
1643 case XDP_REDIRECT:
1644 return NULL;
1645
1646 default:
1647 break;
1648 }
1649
1650 put_xdp_frags(&xdp);
1651
1652 err_xdp:
1653 put_page(page);
1654 mergeable_buf_free(rq, num_buf, dev, stats);
1655
1656 u64_stats_inc(&stats->xdp_drops);
1657 u64_stats_inc(&stats->drops);
1658 return NULL;
1659 }
1660
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)1661 static struct sk_buff *receive_mergeable(struct net_device *dev,
1662 struct virtnet_info *vi,
1663 struct receive_queue *rq,
1664 void *buf,
1665 void *ctx,
1666 unsigned int len,
1667 unsigned int *xdp_xmit,
1668 struct virtnet_rq_stats *stats)
1669 {
1670 struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1671 int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
1672 struct page *page = virt_to_head_page(buf);
1673 int offset = buf - page_address(page);
1674 struct sk_buff *head_skb, *curr_skb;
1675 unsigned int truesize = mergeable_ctx_to_truesize(ctx);
1676 unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1677 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1678 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1679
1680 head_skb = NULL;
1681 u64_stats_add(&stats->bytes, len - vi->hdr_len);
1682
1683 if (unlikely(len > truesize - room)) {
1684 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1685 dev->name, len, (unsigned long)(truesize - room));
1686 DEV_STATS_INC(dev, rx_length_errors);
1687 goto err_skb;
1688 }
1689
1690 if (unlikely(vi->xdp_enabled)) {
1691 struct bpf_prog *xdp_prog;
1692
1693 rcu_read_lock();
1694 xdp_prog = rcu_dereference(rq->xdp_prog);
1695 if (xdp_prog) {
1696 head_skb = receive_mergeable_xdp(dev, vi, rq, xdp_prog, buf, ctx,
1697 len, xdp_xmit, stats);
1698 rcu_read_unlock();
1699 return head_skb;
1700 }
1701 rcu_read_unlock();
1702 }
1703
1704 head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1705 curr_skb = head_skb;
1706
1707 if (unlikely(!curr_skb))
1708 goto err_skb;
1709 while (--num_buf) {
1710 int num_skb_frags;
1711
1712 buf = virtnet_rq_get_buf(rq, &len, &ctx);
1713 if (unlikely(!buf)) {
1714 pr_debug("%s: rx error: %d buffers out of %d missing\n",
1715 dev->name, num_buf,
1716 virtio16_to_cpu(vi->vdev,
1717 hdr->num_buffers));
1718 DEV_STATS_INC(dev, rx_length_errors);
1719 goto err_buf;
1720 }
1721
1722 u64_stats_add(&stats->bytes, len);
1723 page = virt_to_head_page(buf);
1724
1725 truesize = mergeable_ctx_to_truesize(ctx);
1726 headroom = mergeable_ctx_to_headroom(ctx);
1727 tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1728 room = SKB_DATA_ALIGN(headroom + tailroom);
1729 if (unlikely(len > truesize - room)) {
1730 pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1731 dev->name, len, (unsigned long)(truesize - room));
1732 DEV_STATS_INC(dev, rx_length_errors);
1733 goto err_skb;
1734 }
1735
1736 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
1737 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
1738 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
1739
1740 if (unlikely(!nskb))
1741 goto err_skb;
1742 if (curr_skb == head_skb)
1743 skb_shinfo(curr_skb)->frag_list = nskb;
1744 else
1745 curr_skb->next = nskb;
1746 curr_skb = nskb;
1747 head_skb->truesize += nskb->truesize;
1748 num_skb_frags = 0;
1749 }
1750 if (curr_skb != head_skb) {
1751 head_skb->data_len += len;
1752 head_skb->len += len;
1753 head_skb->truesize += truesize;
1754 }
1755 offset = buf - page_address(page);
1756 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1757 put_page(page);
1758 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1759 len, truesize);
1760 } else {
1761 skb_add_rx_frag(curr_skb, num_skb_frags, page,
1762 offset, len, truesize);
1763 }
1764 }
1765
1766 ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
1767 return head_skb;
1768
1769 err_skb:
1770 put_page(page);
1771 mergeable_buf_free(rq, num_buf, dev, stats);
1772
1773 err_buf:
1774 u64_stats_inc(&stats->drops);
1775 dev_kfree_skb(head_skb);
1776 return NULL;
1777 }
1778
virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash * hdr_hash,struct sk_buff * skb)1779 static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
1780 struct sk_buff *skb)
1781 {
1782 enum pkt_hash_types rss_hash_type;
1783
1784 if (!hdr_hash || !skb)
1785 return;
1786
1787 switch (__le16_to_cpu(hdr_hash->hash_report)) {
1788 case VIRTIO_NET_HASH_REPORT_TCPv4:
1789 case VIRTIO_NET_HASH_REPORT_UDPv4:
1790 case VIRTIO_NET_HASH_REPORT_TCPv6:
1791 case VIRTIO_NET_HASH_REPORT_UDPv6:
1792 case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
1793 case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
1794 rss_hash_type = PKT_HASH_TYPE_L4;
1795 break;
1796 case VIRTIO_NET_HASH_REPORT_IPv4:
1797 case VIRTIO_NET_HASH_REPORT_IPv6:
1798 case VIRTIO_NET_HASH_REPORT_IPv6_EX:
1799 rss_hash_type = PKT_HASH_TYPE_L3;
1800 break;
1801 case VIRTIO_NET_HASH_REPORT_NONE:
1802 default:
1803 rss_hash_type = PKT_HASH_TYPE_NONE;
1804 }
1805 skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
1806 }
1807
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)1808 static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
1809 void *buf, unsigned int len, void **ctx,
1810 unsigned int *xdp_xmit,
1811 struct virtnet_rq_stats *stats)
1812 {
1813 struct net_device *dev = vi->dev;
1814 struct sk_buff *skb;
1815 struct virtio_net_common_hdr *hdr;
1816 u8 flags;
1817
1818 if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
1819 pr_debug("%s: short packet %i\n", dev->name, len);
1820 DEV_STATS_INC(dev, rx_length_errors);
1821 virtnet_rq_free_buf(vi, rq, buf);
1822 return;
1823 }
1824
1825 /* 1. Save the flags early, as the XDP program might overwrite them.
1826 * These flags ensure packets marked as VIRTIO_NET_HDR_F_DATA_VALID
1827 * stay valid after XDP processing.
1828 * 2. XDP doesn't work with partially checksummed packets (refer to
1829 * virtnet_xdp_set()), so packets marked as
1830 * VIRTIO_NET_HDR_F_NEEDS_CSUM get dropped during XDP processing.
1831 */
1832 flags = ((struct virtio_net_common_hdr *)buf)->hdr.flags;
1833
1834 if (vi->mergeable_rx_bufs)
1835 skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1836 stats);
1837 else if (vi->big_packets)
1838 skb = receive_big(dev, vi, rq, buf, len, stats);
1839 else
1840 skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1841
1842 if (unlikely(!skb))
1843 return;
1844
1845 hdr = skb_vnet_common_hdr(skb);
1846 if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
1847 virtio_skb_set_hash(&hdr->hash_v1_hdr, skb);
1848
1849 if (flags & VIRTIO_NET_HDR_F_DATA_VALID)
1850 skb->ip_summed = CHECKSUM_UNNECESSARY;
1851
1852 if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1853 virtio_is_little_endian(vi->vdev))) {
1854 net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1855 dev->name, hdr->hdr.gso_type,
1856 hdr->hdr.gso_size);
1857 goto frame_err;
1858 }
1859
1860 skb_record_rx_queue(skb, vq2rxq(rq->vq));
1861 skb->protocol = eth_type_trans(skb, dev);
1862 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1863 ntohs(skb->protocol), skb->len, skb->pkt_type);
1864
1865 napi_gro_receive(&rq->napi, skb);
1866 return;
1867
1868 frame_err:
1869 DEV_STATS_INC(dev, rx_frame_errors);
1870 dev_kfree_skb(skb);
1871 }
1872
1873 /* Unlike mergeable buffers, all buffers are allocated to the
1874 * same size, except for the headroom. For this reason we do
1875 * not need to use mergeable_len_to_ctx here - it is enough
1876 * to store the headroom as the context ignoring the truesize.
1877 */
add_recvbuf_small(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1878 static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1879 gfp_t gfp)
1880 {
1881 char *buf;
1882 unsigned int xdp_headroom = virtnet_get_headroom(vi);
1883 void *ctx = (void *)(unsigned long)xdp_headroom;
1884 int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
1885 int err;
1886
1887 len = SKB_DATA_ALIGN(len) +
1888 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1889
1890 if (unlikely(!skb_page_frag_refill(len, &rq->alloc_frag, gfp)))
1891 return -ENOMEM;
1892
1893 buf = virtnet_rq_alloc(rq, len, gfp);
1894 if (unlikely(!buf))
1895 return -ENOMEM;
1896
1897 buf += VIRTNET_RX_PAD + xdp_headroom;
1898
1899 virtnet_rq_init_one_sg(rq, buf, vi->hdr_len + GOOD_PACKET_LEN);
1900
1901 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
1902 if (err < 0) {
1903 if (rq->do_dma)
1904 virtnet_rq_unmap(rq, buf, 0);
1905 put_page(virt_to_head_page(buf));
1906 }
1907
1908 return err;
1909 }
1910
add_recvbuf_big(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1911 static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1912 gfp_t gfp)
1913 {
1914 struct page *first, *list = NULL;
1915 char *p;
1916 int i, err, offset;
1917
1918 sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1919
1920 /* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
1921 for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1922 first = get_a_page(rq, gfp);
1923 if (!first) {
1924 if (list)
1925 give_pages(rq, list);
1926 return -ENOMEM;
1927 }
1928 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
1929
1930 /* chain new page in list head to match sg */
1931 first->private = (unsigned long)list;
1932 list = first;
1933 }
1934
1935 first = get_a_page(rq, gfp);
1936 if (!first) {
1937 give_pages(rq, list);
1938 return -ENOMEM;
1939 }
1940 p = page_address(first);
1941
1942 /* rq->sg[0], rq->sg[1] share the same page */
1943 /* a separated rq->sg[0] for header - required in case !any_header_sg */
1944 sg_set_buf(&rq->sg[0], p, vi->hdr_len);
1945
1946 /* rq->sg[1] for data packet, from offset */
1947 offset = sizeof(struct padded_vnet_hdr);
1948 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
1949
1950 /* chain first in list head */
1951 first->private = (unsigned long)list;
1952 err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1953 first, gfp);
1954 if (err < 0)
1955 give_pages(rq, first);
1956
1957 return err;
1958 }
1959
get_mergeable_buf_len(struct receive_queue * rq,struct ewma_pkt_len * avg_pkt_len,unsigned int room)1960 static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
1961 struct ewma_pkt_len *avg_pkt_len,
1962 unsigned int room)
1963 {
1964 struct virtnet_info *vi = rq->vq->vdev->priv;
1965 const size_t hdr_len = vi->hdr_len;
1966 unsigned int len;
1967
1968 if (room)
1969 return PAGE_SIZE - room;
1970
1971 len = hdr_len + clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1972 rq->min_buf_len, PAGE_SIZE - hdr_len);
1973
1974 return ALIGN(len, L1_CACHE_BYTES);
1975 }
1976
add_recvbuf_mergeable(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)1977 static int add_recvbuf_mergeable(struct virtnet_info *vi,
1978 struct receive_queue *rq, gfp_t gfp)
1979 {
1980 struct page_frag *alloc_frag = &rq->alloc_frag;
1981 unsigned int headroom = virtnet_get_headroom(vi);
1982 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1983 unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1984 unsigned int len, hole;
1985 void *ctx;
1986 char *buf;
1987 int err;
1988
1989 /* Extra tailroom is needed to satisfy XDP's assumption. This
1990 * means rx frags coalescing won't work, but consider we've
1991 * disabled GSO for XDP, it won't be a big issue.
1992 */
1993 len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
1994
1995 if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
1996 return -ENOMEM;
1997
1998 if (!alloc_frag->offset && len + room + sizeof(struct virtnet_rq_dma) > alloc_frag->size)
1999 len -= sizeof(struct virtnet_rq_dma);
2000
2001 buf = virtnet_rq_alloc(rq, len + room, gfp);
2002 if (unlikely(!buf))
2003 return -ENOMEM;
2004
2005 buf += headroom; /* advance address leaving hole at front of pkt */
2006 hole = alloc_frag->size - alloc_frag->offset;
2007 if (hole < len + room) {
2008 /* To avoid internal fragmentation, if there is very likely not
2009 * enough space for another buffer, add the remaining space to
2010 * the current buffer.
2011 * XDP core assumes that frame_size of xdp_buff and the length
2012 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
2013 */
2014 if (!headroom)
2015 len += hole;
2016 alloc_frag->offset += hole;
2017 }
2018
2019 virtnet_rq_init_one_sg(rq, buf, len);
2020
2021 ctx = mergeable_len_to_ctx(len + room, headroom);
2022 err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
2023 if (err < 0) {
2024 if (rq->do_dma)
2025 virtnet_rq_unmap(rq, buf, 0);
2026 put_page(virt_to_head_page(buf));
2027 }
2028
2029 return err;
2030 }
2031
2032 /*
2033 * Returns false if we couldn't fill entirely (OOM).
2034 *
2035 * Normally run in the receive path, but can also be run from ndo_open
2036 * before we're receiving packets, or from refill_work which is
2037 * careful to disable receiving (using napi_disable).
2038 */
try_fill_recv(struct virtnet_info * vi,struct receive_queue * rq,gfp_t gfp)2039 static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
2040 gfp_t gfp)
2041 {
2042 int err;
2043 bool oom;
2044
2045 do {
2046 if (vi->mergeable_rx_bufs)
2047 err = add_recvbuf_mergeable(vi, rq, gfp);
2048 else if (vi->big_packets)
2049 err = add_recvbuf_big(vi, rq, gfp);
2050 else
2051 err = add_recvbuf_small(vi, rq, gfp);
2052
2053 oom = err == -ENOMEM;
2054 if (err)
2055 break;
2056 } while (rq->vq->num_free);
2057 if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
2058 unsigned long flags;
2059
2060 flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
2061 u64_stats_inc(&rq->stats.kicks);
2062 u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
2063 }
2064
2065 return !oom;
2066 }
2067
skb_recv_done(struct virtqueue * rvq)2068 static void skb_recv_done(struct virtqueue *rvq)
2069 {
2070 struct virtnet_info *vi = rvq->vdev->priv;
2071 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
2072
2073 virtqueue_napi_schedule(&rq->napi, rvq);
2074 }
2075
virtnet_napi_enable(struct virtqueue * vq,struct napi_struct * napi)2076 static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
2077 {
2078 napi_enable(napi);
2079
2080 /* If all buffers were filled by other side before we napi_enabled, we
2081 * won't get another interrupt, so process any outstanding packets now.
2082 * Call local_bh_enable after to trigger softIRQ processing.
2083 */
2084 local_bh_disable();
2085 virtqueue_napi_schedule(napi, vq);
2086 local_bh_enable();
2087 }
2088
virtnet_napi_tx_enable(struct virtnet_info * vi,struct virtqueue * vq,struct napi_struct * napi)2089 static void virtnet_napi_tx_enable(struct virtnet_info *vi,
2090 struct virtqueue *vq,
2091 struct napi_struct *napi)
2092 {
2093 if (!napi->weight)
2094 return;
2095
2096 /* Tx napi touches cachelines on the cpu handling tx interrupts. Only
2097 * enable the feature if this is likely affine with the transmit path.
2098 */
2099 if (!vi->affinity_hint_set) {
2100 napi->weight = 0;
2101 return;
2102 }
2103
2104 return virtnet_napi_enable(vq, napi);
2105 }
2106
virtnet_napi_tx_disable(struct napi_struct * napi)2107 static void virtnet_napi_tx_disable(struct napi_struct *napi)
2108 {
2109 if (napi->weight)
2110 napi_disable(napi);
2111 }
2112
refill_work(struct work_struct * work)2113 static void refill_work(struct work_struct *work)
2114 {
2115 struct virtnet_info *vi =
2116 container_of(work, struct virtnet_info, refill.work);
2117 bool still_empty;
2118 int i;
2119
2120 for (i = 0; i < vi->curr_queue_pairs; i++) {
2121 struct receive_queue *rq = &vi->rq[i];
2122
2123 napi_disable(&rq->napi);
2124 still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
2125 virtnet_napi_enable(rq->vq, &rq->napi);
2126
2127 /* In theory, this can happen: if we don't get any buffers in
2128 * we will *never* try to fill again.
2129 */
2130 if (still_empty)
2131 schedule_delayed_work(&vi->refill, HZ/2);
2132 }
2133 }
2134
virtnet_receive(struct receive_queue * rq,int budget,unsigned int * xdp_xmit)2135 static int virtnet_receive(struct receive_queue *rq, int budget,
2136 unsigned int *xdp_xmit)
2137 {
2138 struct virtnet_info *vi = rq->vq->vdev->priv;
2139 struct virtnet_rq_stats stats = {};
2140 unsigned int len;
2141 int packets = 0;
2142 void *buf;
2143 int i;
2144
2145 if (!vi->big_packets || vi->mergeable_rx_bufs) {
2146 void *ctx;
2147
2148 while (packets < budget &&
2149 (buf = virtnet_rq_get_buf(rq, &len, &ctx))) {
2150 receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
2151 packets++;
2152 }
2153 } else {
2154 while (packets < budget &&
2155 (buf = virtnet_rq_get_buf(rq, &len, NULL)) != NULL) {
2156 receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
2157 packets++;
2158 }
2159 }
2160
2161 if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
2162 if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
2163 spin_lock(&vi->refill_lock);
2164 if (vi->refill_enabled)
2165 schedule_delayed_work(&vi->refill, 0);
2166 spin_unlock(&vi->refill_lock);
2167 }
2168 }
2169
2170 u64_stats_set(&stats.packets, packets);
2171 u64_stats_update_begin(&rq->stats.syncp);
2172 for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
2173 size_t offset = virtnet_rq_stats_desc[i].offset;
2174 u64_stats_t *item, *src;
2175
2176 item = (u64_stats_t *)((u8 *)&rq->stats + offset);
2177 src = (u64_stats_t *)((u8 *)&stats + offset);
2178 u64_stats_add(item, u64_stats_read(src));
2179 }
2180 u64_stats_update_end(&rq->stats.syncp);
2181
2182 return packets;
2183 }
2184
virtnet_poll_cleantx(struct receive_queue * rq,int budget)2185 static void virtnet_poll_cleantx(struct receive_queue *rq, int budget)
2186 {
2187 struct virtnet_info *vi = rq->vq->vdev->priv;
2188 unsigned int index = vq2rxq(rq->vq);
2189 struct send_queue *sq = &vi->sq[index];
2190 struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
2191
2192 if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
2193 return;
2194
2195 if (__netif_tx_trylock(txq)) {
2196 if (sq->reset) {
2197 __netif_tx_unlock(txq);
2198 return;
2199 }
2200
2201 do {
2202 virtqueue_disable_cb(sq->vq);
2203 free_old_xmit_skbs(sq, !!budget);
2204 } while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2205
2206 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2207 netif_tx_wake_queue(txq);
2208
2209 __netif_tx_unlock(txq);
2210 }
2211 }
2212
virtnet_poll(struct napi_struct * napi,int budget)2213 static int virtnet_poll(struct napi_struct *napi, int budget)
2214 {
2215 struct receive_queue *rq =
2216 container_of(napi, struct receive_queue, napi);
2217 struct virtnet_info *vi = rq->vq->vdev->priv;
2218 struct send_queue *sq;
2219 unsigned int received;
2220 unsigned int xdp_xmit = 0;
2221
2222 virtnet_poll_cleantx(rq, budget);
2223
2224 received = virtnet_receive(rq, budget, &xdp_xmit);
2225
2226 if (xdp_xmit & VIRTIO_XDP_REDIR)
2227 xdp_do_flush();
2228
2229 /* Out of packets? */
2230 if (received < budget)
2231 virtqueue_napi_complete(napi, rq->vq, received);
2232
2233 if (xdp_xmit & VIRTIO_XDP_TX) {
2234 sq = virtnet_xdp_get_sq(vi);
2235 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2236 u64_stats_update_begin(&sq->stats.syncp);
2237 u64_stats_inc(&sq->stats.kicks);
2238 u64_stats_update_end(&sq->stats.syncp);
2239 }
2240 virtnet_xdp_put_sq(vi, sq);
2241 }
2242
2243 return received;
2244 }
2245
virtnet_disable_queue_pair(struct virtnet_info * vi,int qp_index)2246 static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
2247 {
2248 virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
2249 napi_disable(&vi->rq[qp_index].napi);
2250 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2251 }
2252
virtnet_enable_queue_pair(struct virtnet_info * vi,int qp_index)2253 static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
2254 {
2255 struct net_device *dev = vi->dev;
2256 int err;
2257
2258 err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
2259 vi->rq[qp_index].napi.napi_id);
2260 if (err < 0)
2261 return err;
2262
2263 err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
2264 MEM_TYPE_PAGE_SHARED, NULL);
2265 if (err < 0)
2266 goto err_xdp_reg_mem_model;
2267
2268 virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
2269 virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
2270
2271 return 0;
2272
2273 err_xdp_reg_mem_model:
2274 xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
2275 return err;
2276 }
2277
virtnet_open(struct net_device * dev)2278 static int virtnet_open(struct net_device *dev)
2279 {
2280 struct virtnet_info *vi = netdev_priv(dev);
2281 int i, err;
2282
2283 enable_delayed_refill(vi);
2284
2285 for (i = 0; i < vi->max_queue_pairs; i++) {
2286 if (i < vi->curr_queue_pairs)
2287 /* Make sure we have some buffers: if oom use wq. */
2288 if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
2289 schedule_delayed_work(&vi->refill, 0);
2290
2291 err = virtnet_enable_queue_pair(vi, i);
2292 if (err < 0)
2293 goto err_enable_qp;
2294 }
2295
2296 return 0;
2297
2298 err_enable_qp:
2299 disable_delayed_refill(vi);
2300 cancel_delayed_work_sync(&vi->refill);
2301
2302 for (i--; i >= 0; i--)
2303 virtnet_disable_queue_pair(vi, i);
2304 return err;
2305 }
2306
virtnet_poll_tx(struct napi_struct * napi,int budget)2307 static int virtnet_poll_tx(struct napi_struct *napi, int budget)
2308 {
2309 struct send_queue *sq = container_of(napi, struct send_queue, napi);
2310 struct virtnet_info *vi = sq->vq->vdev->priv;
2311 unsigned int index = vq2txq(sq->vq);
2312 struct netdev_queue *txq;
2313 int opaque;
2314 bool done;
2315
2316 if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
2317 /* We don't need to enable cb for XDP */
2318 napi_complete_done(napi, 0);
2319 return 0;
2320 }
2321
2322 txq = netdev_get_tx_queue(vi->dev, index);
2323 __netif_tx_lock(txq, raw_smp_processor_id());
2324 virtqueue_disable_cb(sq->vq);
2325 free_old_xmit_skbs(sq, !!budget);
2326
2327 if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
2328 netif_tx_wake_queue(txq);
2329
2330 opaque = virtqueue_enable_cb_prepare(sq->vq);
2331
2332 done = napi_complete_done(napi, 0);
2333
2334 if (!done)
2335 virtqueue_disable_cb(sq->vq);
2336
2337 __netif_tx_unlock(txq);
2338
2339 if (done) {
2340 if (unlikely(virtqueue_poll(sq->vq, opaque))) {
2341 if (napi_schedule_prep(napi)) {
2342 __netif_tx_lock(txq, raw_smp_processor_id());
2343 virtqueue_disable_cb(sq->vq);
2344 __netif_tx_unlock(txq);
2345 __napi_schedule(napi);
2346 }
2347 }
2348 }
2349
2350 return 0;
2351 }
2352
xmit_skb(struct send_queue * sq,struct sk_buff * skb)2353 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
2354 {
2355 struct virtio_net_hdr_mrg_rxbuf *hdr;
2356 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
2357 struct virtnet_info *vi = sq->vq->vdev->priv;
2358 int num_sg;
2359 unsigned hdr_len = vi->hdr_len;
2360 bool can_push;
2361
2362 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
2363
2364 can_push = vi->any_header_sg &&
2365 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
2366 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
2367 /* Even if we can, don't push here yet as this would skew
2368 * csum_start offset below. */
2369 if (can_push)
2370 hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
2371 else
2372 hdr = &skb_vnet_common_hdr(skb)->mrg_hdr;
2373
2374 if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2375 virtio_is_little_endian(vi->vdev), false,
2376 0))
2377 return -EPROTO;
2378
2379 if (vi->mergeable_rx_bufs)
2380 hdr->num_buffers = 0;
2381
2382 sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2383 if (can_push) {
2384 __skb_push(skb, hdr_len);
2385 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2386 if (unlikely(num_sg < 0))
2387 return num_sg;
2388 /* Pull header back to avoid skew in tx bytes calculations. */
2389 __skb_pull(skb, hdr_len);
2390 } else {
2391 sg_set_buf(sq->sg, hdr, hdr_len);
2392 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2393 if (unlikely(num_sg < 0))
2394 return num_sg;
2395 num_sg++;
2396 }
2397 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
2398 }
2399
start_xmit(struct sk_buff * skb,struct net_device * dev)2400 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
2401 {
2402 struct virtnet_info *vi = netdev_priv(dev);
2403 int qnum = skb_get_queue_mapping(skb);
2404 struct send_queue *sq = &vi->sq[qnum];
2405 int err;
2406 struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
2407 bool kick = !netdev_xmit_more();
2408 bool use_napi = sq->napi.weight;
2409
2410 /* Free up any pending old buffers before queueing new ones. */
2411 do {
2412 if (use_napi)
2413 virtqueue_disable_cb(sq->vq);
2414
2415 free_old_xmit_skbs(sq, false);
2416
2417 } while (use_napi && kick &&
2418 unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2419
2420 /* timestamp packet in software */
2421 skb_tx_timestamp(skb);
2422
2423 /* Try to transmit */
2424 err = xmit_skb(sq, skb);
2425
2426 /* This should not happen! */
2427 if (unlikely(err)) {
2428 DEV_STATS_INC(dev, tx_fifo_errors);
2429 if (net_ratelimit())
2430 dev_warn(&dev->dev,
2431 "Unexpected TXQ (%d) queue failure: %d\n",
2432 qnum, err);
2433 DEV_STATS_INC(dev, tx_dropped);
2434 dev_kfree_skb_any(skb);
2435 return NETDEV_TX_OK;
2436 }
2437
2438 /* Don't wait up for transmitted skbs to be freed. */
2439 if (!use_napi) {
2440 skb_orphan(skb);
2441 nf_reset_ct(skb);
2442 }
2443
2444 check_sq_full_and_disable(vi, dev, sq);
2445
2446 if (kick || netif_xmit_stopped(txq)) {
2447 if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2448 u64_stats_update_begin(&sq->stats.syncp);
2449 u64_stats_inc(&sq->stats.kicks);
2450 u64_stats_update_end(&sq->stats.syncp);
2451 }
2452 }
2453
2454 return NETDEV_TX_OK;
2455 }
2456
virtnet_rx_resize(struct virtnet_info * vi,struct receive_queue * rq,u32 ring_num)2457 static int virtnet_rx_resize(struct virtnet_info *vi,
2458 struct receive_queue *rq, u32 ring_num)
2459 {
2460 bool running = netif_running(vi->dev);
2461 int err, qindex;
2462
2463 qindex = rq - vi->rq;
2464
2465 if (running)
2466 napi_disable(&rq->napi);
2467
2468 err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_unmap_free_buf);
2469 if (err)
2470 netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
2471
2472 if (!try_fill_recv(vi, rq, GFP_KERNEL))
2473 schedule_delayed_work(&vi->refill, 0);
2474
2475 if (running)
2476 virtnet_napi_enable(rq->vq, &rq->napi);
2477 return err;
2478 }
2479
virtnet_tx_resize(struct virtnet_info * vi,struct send_queue * sq,u32 ring_num)2480 static int virtnet_tx_resize(struct virtnet_info *vi,
2481 struct send_queue *sq, u32 ring_num)
2482 {
2483 bool running = netif_running(vi->dev);
2484 struct netdev_queue *txq;
2485 int err, qindex;
2486
2487 qindex = sq - vi->sq;
2488
2489 if (running)
2490 virtnet_napi_tx_disable(&sq->napi);
2491
2492 txq = netdev_get_tx_queue(vi->dev, qindex);
2493
2494 /* 1. wait all ximt complete
2495 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2496 */
2497 __netif_tx_lock_bh(txq);
2498
2499 /* Prevent rx poll from accessing sq. */
2500 sq->reset = true;
2501
2502 /* Prevent the upper layer from trying to send packets. */
2503 netif_stop_subqueue(vi->dev, qindex);
2504
2505 __netif_tx_unlock_bh(txq);
2506
2507 err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2508 if (err)
2509 netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2510
2511 __netif_tx_lock_bh(txq);
2512 sq->reset = false;
2513 netif_tx_wake_queue(txq);
2514 __netif_tx_unlock_bh(txq);
2515
2516 if (running)
2517 virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2518 return err;
2519 }
2520
2521 /*
2522 * Send command via the control virtqueue and check status. Commands
2523 * supported by the hypervisor, as indicated by feature bits, should
2524 * never fail unless improperly formatted.
2525 */
virtnet_send_command(struct virtnet_info * vi,u8 class,u8 cmd,struct scatterlist * out)2526 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2527 struct scatterlist *out)
2528 {
2529 struct scatterlist *sgs[4], hdr, stat;
2530 unsigned out_num = 0, tmp;
2531 int ret;
2532
2533 /* Caller should know better */
2534 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
2535
2536 vi->ctrl->status = ~0;
2537 vi->ctrl->hdr.class = class;
2538 vi->ctrl->hdr.cmd = cmd;
2539 /* Add header */
2540 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2541 sgs[out_num++] = &hdr;
2542
2543 if (out)
2544 sgs[out_num++] = out;
2545
2546 /* Add return status. */
2547 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2548 sgs[out_num] = &stat;
2549
2550 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2551 ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2552 if (ret < 0) {
2553 dev_warn(&vi->vdev->dev,
2554 "Failed to add sgs for command vq: %d\n.", ret);
2555 return false;
2556 }
2557
2558 if (unlikely(!virtqueue_kick(vi->cvq)))
2559 return vi->ctrl->status == VIRTIO_NET_OK;
2560
2561 /* Spin for a response, the kick causes an ioport write, trapping
2562 * into the hypervisor, so the request should be handled immediately.
2563 */
2564 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2565 !virtqueue_is_broken(vi->cvq))
2566 cpu_relax();
2567
2568 return vi->ctrl->status == VIRTIO_NET_OK;
2569 }
2570
virtnet_set_mac_address(struct net_device * dev,void * p)2571 static int virtnet_set_mac_address(struct net_device *dev, void *p)
2572 {
2573 struct virtnet_info *vi = netdev_priv(dev);
2574 struct virtio_device *vdev = vi->vdev;
2575 int ret;
2576 struct sockaddr *addr;
2577 struct scatterlist sg;
2578
2579 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2580 return -EOPNOTSUPP;
2581
2582 addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2583 if (!addr)
2584 return -ENOMEM;
2585
2586 ret = eth_prepare_mac_addr_change(dev, addr);
2587 if (ret)
2588 goto out;
2589
2590 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
2591 sg_init_one(&sg, addr->sa_data, dev->addr_len);
2592 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2593 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
2594 dev_warn(&vdev->dev,
2595 "Failed to set mac address by vq command.\n");
2596 ret = -EINVAL;
2597 goto out;
2598 }
2599 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
2600 !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2601 unsigned int i;
2602
2603 /* Naturally, this has an atomicity problem. */
2604 for (i = 0; i < dev->addr_len; i++)
2605 virtio_cwrite8(vdev,
2606 offsetof(struct virtio_net_config, mac) +
2607 i, addr->sa_data[i]);
2608 }
2609
2610 eth_commit_mac_addr_change(dev, p);
2611 ret = 0;
2612
2613 out:
2614 kfree(addr);
2615 return ret;
2616 }
2617
virtnet_stats(struct net_device * dev,struct rtnl_link_stats64 * tot)2618 static void virtnet_stats(struct net_device *dev,
2619 struct rtnl_link_stats64 *tot)
2620 {
2621 struct virtnet_info *vi = netdev_priv(dev);
2622 unsigned int start;
2623 int i;
2624
2625 for (i = 0; i < vi->max_queue_pairs; i++) {
2626 u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2627 struct receive_queue *rq = &vi->rq[i];
2628 struct send_queue *sq = &vi->sq[i];
2629
2630 do {
2631 start = u64_stats_fetch_begin(&sq->stats.syncp);
2632 tpackets = u64_stats_read(&sq->stats.packets);
2633 tbytes = u64_stats_read(&sq->stats.bytes);
2634 terrors = u64_stats_read(&sq->stats.tx_timeouts);
2635 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2636
2637 do {
2638 start = u64_stats_fetch_begin(&rq->stats.syncp);
2639 rpackets = u64_stats_read(&rq->stats.packets);
2640 rbytes = u64_stats_read(&rq->stats.bytes);
2641 rdrops = u64_stats_read(&rq->stats.drops);
2642 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2643
2644 tot->rx_packets += rpackets;
2645 tot->tx_packets += tpackets;
2646 tot->rx_bytes += rbytes;
2647 tot->tx_bytes += tbytes;
2648 tot->rx_dropped += rdrops;
2649 tot->tx_errors += terrors;
2650 }
2651
2652 tot->tx_dropped = DEV_STATS_READ(dev, tx_dropped);
2653 tot->tx_fifo_errors = DEV_STATS_READ(dev, tx_fifo_errors);
2654 tot->rx_length_errors = DEV_STATS_READ(dev, rx_length_errors);
2655 tot->rx_frame_errors = DEV_STATS_READ(dev, rx_frame_errors);
2656 }
2657
virtnet_ack_link_announce(struct virtnet_info * vi)2658 static void virtnet_ack_link_announce(struct virtnet_info *vi)
2659 {
2660 rtnl_lock();
2661 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2662 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2663 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2664 rtnl_unlock();
2665 }
2666
_virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)2667 static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2668 {
2669 struct scatterlist sg;
2670 struct net_device *dev = vi->dev;
2671
2672 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2673 return 0;
2674
2675 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
2676 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2677
2678 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2679 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2680 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2681 queue_pairs);
2682 return -EINVAL;
2683 } else {
2684 vi->curr_queue_pairs = queue_pairs;
2685 /* virtnet_open() will refill when device is going to up. */
2686 if (dev->flags & IFF_UP)
2687 schedule_delayed_work(&vi->refill, 0);
2688 }
2689
2690 return 0;
2691 }
2692
virtnet_set_queues(struct virtnet_info * vi,u16 queue_pairs)2693 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2694 {
2695 int err;
2696
2697 rtnl_lock();
2698 err = _virtnet_set_queues(vi, queue_pairs);
2699 rtnl_unlock();
2700 return err;
2701 }
2702
virtnet_close(struct net_device * dev)2703 static int virtnet_close(struct net_device *dev)
2704 {
2705 struct virtnet_info *vi = netdev_priv(dev);
2706 int i;
2707
2708 /* Make sure NAPI doesn't schedule refill work */
2709 disable_delayed_refill(vi);
2710 /* Make sure refill_work doesn't re-enable napi! */
2711 cancel_delayed_work_sync(&vi->refill);
2712
2713 for (i = 0; i < vi->max_queue_pairs; i++)
2714 virtnet_disable_queue_pair(vi, i);
2715
2716 return 0;
2717 }
2718
virtnet_set_rx_mode(struct net_device * dev)2719 static void virtnet_set_rx_mode(struct net_device *dev)
2720 {
2721 struct virtnet_info *vi = netdev_priv(dev);
2722 struct scatterlist sg[2];
2723 struct virtio_net_ctrl_mac *mac_data;
2724 struct netdev_hw_addr *ha;
2725 int uc_count;
2726 int mc_count;
2727 void *buf;
2728 int i;
2729
2730 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
2731 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
2732 return;
2733
2734 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
2735 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
2736
2737 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
2738
2739 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2740 VIRTIO_NET_CTRL_RX_PROMISC, sg))
2741 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
2742 vi->ctrl->promisc ? "en" : "dis");
2743
2744 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
2745
2746 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2747 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
2748 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
2749 vi->ctrl->allmulti ? "en" : "dis");
2750
2751 uc_count = netdev_uc_count(dev);
2752 mc_count = netdev_mc_count(dev);
2753 /* MAC filter - use one buffer for both lists */
2754 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2755 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
2756 mac_data = buf;
2757 if (!buf)
2758 return;
2759
2760 sg_init_table(sg, 2);
2761
2762 /* Store the unicast list and count in the front of the buffer */
2763 mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2764 i = 0;
2765 netdev_for_each_uc_addr(ha, dev)
2766 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2767
2768 sg_set_buf(&sg[0], mac_data,
2769 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2770
2771 /* multicast list and count fill the end */
2772 mac_data = (void *)&mac_data->macs[uc_count][0];
2773
2774 mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2775 i = 0;
2776 netdev_for_each_mc_addr(ha, dev)
2777 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2778
2779 sg_set_buf(&sg[1], mac_data,
2780 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2781
2782 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2783 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
2784 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2785
2786 kfree(buf);
2787 }
2788
virtnet_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)2789 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
2790 __be16 proto, u16 vid)
2791 {
2792 struct virtnet_info *vi = netdev_priv(dev);
2793 struct scatterlist sg;
2794
2795 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2796 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2797
2798 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2799 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
2800 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
2801 return 0;
2802 }
2803
virtnet_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)2804 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
2805 __be16 proto, u16 vid)
2806 {
2807 struct virtnet_info *vi = netdev_priv(dev);
2808 struct scatterlist sg;
2809
2810 vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
2811 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
2812
2813 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2814 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
2815 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
2816 return 0;
2817 }
2818
virtnet_clean_affinity(struct virtnet_info * vi)2819 static void virtnet_clean_affinity(struct virtnet_info *vi)
2820 {
2821 int i;
2822
2823 if (vi->affinity_hint_set) {
2824 for (i = 0; i < vi->max_queue_pairs; i++) {
2825 virtqueue_set_affinity(vi->rq[i].vq, NULL);
2826 virtqueue_set_affinity(vi->sq[i].vq, NULL);
2827 }
2828
2829 vi->affinity_hint_set = false;
2830 }
2831 }
2832
virtnet_set_affinity(struct virtnet_info * vi)2833 static void virtnet_set_affinity(struct virtnet_info *vi)
2834 {
2835 cpumask_var_t mask;
2836 int stragglers;
2837 int group_size;
2838 int i, j, cpu;
2839 int num_cpu;
2840 int stride;
2841
2842 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2843 virtnet_clean_affinity(vi);
2844 return;
2845 }
2846
2847 num_cpu = num_online_cpus();
2848 stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
2849 stragglers = num_cpu >= vi->curr_queue_pairs ?
2850 num_cpu % vi->curr_queue_pairs :
2851 0;
2852 cpu = cpumask_first(cpu_online_mask);
2853
2854 for (i = 0; i < vi->curr_queue_pairs; i++) {
2855 group_size = stride + (i < stragglers ? 1 : 0);
2856
2857 for (j = 0; j < group_size; j++) {
2858 cpumask_set_cpu(cpu, mask);
2859 cpu = cpumask_next_wrap(cpu, cpu_online_mask,
2860 nr_cpu_ids, false);
2861 }
2862 virtqueue_set_affinity(vi->rq[i].vq, mask);
2863 virtqueue_set_affinity(vi->sq[i].vq, mask);
2864 __netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
2865 cpumask_clear(mask);
2866 }
2867
2868 vi->affinity_hint_set = true;
2869 free_cpumask_var(mask);
2870 }
2871
virtnet_cpu_online(unsigned int cpu,struct hlist_node * node)2872 static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
2873 {
2874 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2875 node);
2876 virtnet_set_affinity(vi);
2877 return 0;
2878 }
2879
virtnet_cpu_dead(unsigned int cpu,struct hlist_node * node)2880 static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
2881 {
2882 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2883 node_dead);
2884 virtnet_set_affinity(vi);
2885 return 0;
2886 }
2887
virtnet_cpu_down_prep(unsigned int cpu,struct hlist_node * node)2888 static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
2889 {
2890 struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
2891 node);
2892
2893 virtnet_clean_affinity(vi);
2894 return 0;
2895 }
2896
2897 static enum cpuhp_state virtionet_online;
2898
virtnet_cpu_notif_add(struct virtnet_info * vi)2899 static int virtnet_cpu_notif_add(struct virtnet_info *vi)
2900 {
2901 int ret;
2902
2903 ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
2904 if (ret)
2905 return ret;
2906 ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2907 &vi->node_dead);
2908 if (!ret)
2909 return ret;
2910 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2911 return ret;
2912 }
2913
virtnet_cpu_notif_remove(struct virtnet_info * vi)2914 static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
2915 {
2916 cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
2917 cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
2918 &vi->node_dead);
2919 }
2920
virtnet_get_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)2921 static void virtnet_get_ringparam(struct net_device *dev,
2922 struct ethtool_ringparam *ring,
2923 struct kernel_ethtool_ringparam *kernel_ring,
2924 struct netlink_ext_ack *extack)
2925 {
2926 struct virtnet_info *vi = netdev_priv(dev);
2927
2928 ring->rx_max_pending = vi->rq[0].vq->num_max;
2929 ring->tx_max_pending = vi->sq[0].vq->num_max;
2930 ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2931 ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2932 }
2933
2934 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
2935 u16 vqn, u32 max_usecs, u32 max_packets);
2936
virtnet_set_ringparam(struct net_device * dev,struct ethtool_ringparam * ring,struct kernel_ethtool_ringparam * kernel_ring,struct netlink_ext_ack * extack)2937 static int virtnet_set_ringparam(struct net_device *dev,
2938 struct ethtool_ringparam *ring,
2939 struct kernel_ethtool_ringparam *kernel_ring,
2940 struct netlink_ext_ack *extack)
2941 {
2942 struct virtnet_info *vi = netdev_priv(dev);
2943 u32 rx_pending, tx_pending;
2944 struct receive_queue *rq;
2945 struct send_queue *sq;
2946 int i, err;
2947
2948 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2949 return -EINVAL;
2950
2951 rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2952 tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2953
2954 if (ring->rx_pending == rx_pending &&
2955 ring->tx_pending == tx_pending)
2956 return 0;
2957
2958 if (ring->rx_pending > vi->rq[0].vq->num_max)
2959 return -EINVAL;
2960
2961 if (ring->tx_pending > vi->sq[0].vq->num_max)
2962 return -EINVAL;
2963
2964 for (i = 0; i < vi->max_queue_pairs; i++) {
2965 rq = vi->rq + i;
2966 sq = vi->sq + i;
2967
2968 if (ring->tx_pending != tx_pending) {
2969 err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2970 if (err)
2971 return err;
2972
2973 /* Upon disabling and re-enabling a transmit virtqueue, the device must
2974 * set the coalescing parameters of the virtqueue to those configured
2975 * through the VIRTIO_NET_CTRL_NOTF_COAL_TX_SET command, or, if the driver
2976 * did not set any TX coalescing parameters, to 0.
2977 */
2978 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(i),
2979 vi->intr_coal_tx.max_usecs,
2980 vi->intr_coal_tx.max_packets);
2981 if (err)
2982 return err;
2983
2984 vi->sq[i].intr_coal.max_usecs = vi->intr_coal_tx.max_usecs;
2985 vi->sq[i].intr_coal.max_packets = vi->intr_coal_tx.max_packets;
2986 }
2987
2988 if (ring->rx_pending != rx_pending) {
2989 err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2990 if (err)
2991 return err;
2992
2993 /* The reason is same as the transmit virtqueue reset */
2994 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(i),
2995 vi->intr_coal_rx.max_usecs,
2996 vi->intr_coal_rx.max_packets);
2997 if (err)
2998 return err;
2999
3000 vi->rq[i].intr_coal.max_usecs = vi->intr_coal_rx.max_usecs;
3001 vi->rq[i].intr_coal.max_packets = vi->intr_coal_rx.max_packets;
3002 }
3003 }
3004
3005 return 0;
3006 }
3007
virtnet_commit_rss_command(struct virtnet_info * vi)3008 static bool virtnet_commit_rss_command(struct virtnet_info *vi)
3009 {
3010 struct net_device *dev = vi->dev;
3011 struct scatterlist sgs[4];
3012 unsigned int sg_buf_size;
3013
3014 /* prepare sgs */
3015 sg_init_table(sgs, 4);
3016
3017 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
3018 sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
3019
3020 sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
3021 sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
3022
3023 sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
3024 - offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
3025 sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
3026
3027 sg_buf_size = vi->rss_key_size;
3028 sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
3029
3030 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
3031 vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
3032 : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
3033 dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
3034 return false;
3035 }
3036 return true;
3037 }
3038
virtnet_init_default_rss(struct virtnet_info * vi)3039 static void virtnet_init_default_rss(struct virtnet_info *vi)
3040 {
3041 u32 indir_val = 0;
3042 int i = 0;
3043
3044 vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
3045 vi->rss_hash_types_saved = vi->rss_hash_types_supported;
3046 vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
3047 ? vi->rss_indir_table_size - 1 : 0;
3048 vi->ctrl->rss.unclassified_queue = 0;
3049
3050 for (; i < vi->rss_indir_table_size; ++i) {
3051 indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
3052 vi->ctrl->rss.indirection_table[i] = indir_val;
3053 }
3054
3055 vi->ctrl->rss.max_tx_vq = vi->has_rss ? vi->curr_queue_pairs : 0;
3056 vi->ctrl->rss.hash_key_length = vi->rss_key_size;
3057
3058 netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
3059 }
3060
virtnet_get_hashflow(const struct virtnet_info * vi,struct ethtool_rxnfc * info)3061 static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
3062 {
3063 info->data = 0;
3064 switch (info->flow_type) {
3065 case TCP_V4_FLOW:
3066 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
3067 info->data = RXH_IP_SRC | RXH_IP_DST |
3068 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3069 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3070 info->data = RXH_IP_SRC | RXH_IP_DST;
3071 }
3072 break;
3073 case TCP_V6_FLOW:
3074 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
3075 info->data = RXH_IP_SRC | RXH_IP_DST |
3076 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3077 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3078 info->data = RXH_IP_SRC | RXH_IP_DST;
3079 }
3080 break;
3081 case UDP_V4_FLOW:
3082 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
3083 info->data = RXH_IP_SRC | RXH_IP_DST |
3084 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3085 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
3086 info->data = RXH_IP_SRC | RXH_IP_DST;
3087 }
3088 break;
3089 case UDP_V6_FLOW:
3090 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
3091 info->data = RXH_IP_SRC | RXH_IP_DST |
3092 RXH_L4_B_0_1 | RXH_L4_B_2_3;
3093 } else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
3094 info->data = RXH_IP_SRC | RXH_IP_DST;
3095 }
3096 break;
3097 case IPV4_FLOW:
3098 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
3099 info->data = RXH_IP_SRC | RXH_IP_DST;
3100
3101 break;
3102 case IPV6_FLOW:
3103 if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
3104 info->data = RXH_IP_SRC | RXH_IP_DST;
3105
3106 break;
3107 default:
3108 info->data = 0;
3109 break;
3110 }
3111 }
3112
virtnet_set_hashflow(struct virtnet_info * vi,struct ethtool_rxnfc * info)3113 static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
3114 {
3115 u32 new_hashtypes = vi->rss_hash_types_saved;
3116 bool is_disable = info->data & RXH_DISCARD;
3117 bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
3118
3119 /* supports only 'sd', 'sdfn' and 'r' */
3120 if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
3121 return false;
3122
3123 switch (info->flow_type) {
3124 case TCP_V4_FLOW:
3125 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
3126 if (!is_disable)
3127 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3128 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
3129 break;
3130 case UDP_V4_FLOW:
3131 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
3132 if (!is_disable)
3133 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
3134 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
3135 break;
3136 case IPV4_FLOW:
3137 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3138 if (!is_disable)
3139 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
3140 break;
3141 case TCP_V6_FLOW:
3142 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
3143 if (!is_disable)
3144 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3145 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
3146 break;
3147 case UDP_V6_FLOW:
3148 new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
3149 if (!is_disable)
3150 new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
3151 | (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
3152 break;
3153 case IPV6_FLOW:
3154 new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3155 if (!is_disable)
3156 new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
3157 break;
3158 default:
3159 /* unsupported flow */
3160 return false;
3161 }
3162
3163 /* if unsupported hashtype was set */
3164 if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
3165 return false;
3166
3167 if (new_hashtypes != vi->rss_hash_types_saved) {
3168 vi->rss_hash_types_saved = new_hashtypes;
3169 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3170 if (vi->dev->features & NETIF_F_RXHASH)
3171 return virtnet_commit_rss_command(vi);
3172 }
3173
3174 return true;
3175 }
3176
virtnet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)3177 static void virtnet_get_drvinfo(struct net_device *dev,
3178 struct ethtool_drvinfo *info)
3179 {
3180 struct virtnet_info *vi = netdev_priv(dev);
3181 struct virtio_device *vdev = vi->vdev;
3182
3183 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
3184 strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
3185 strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
3186
3187 }
3188
3189 /* TODO: Eliminate OOO packets during switching */
virtnet_set_channels(struct net_device * dev,struct ethtool_channels * channels)3190 static int virtnet_set_channels(struct net_device *dev,
3191 struct ethtool_channels *channels)
3192 {
3193 struct virtnet_info *vi = netdev_priv(dev);
3194 u16 queue_pairs = channels->combined_count;
3195 int err;
3196
3197 /* We don't support separate rx/tx channels.
3198 * We don't allow setting 'other' channels.
3199 */
3200 if (channels->rx_count || channels->tx_count || channels->other_count)
3201 return -EINVAL;
3202
3203 if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
3204 return -EINVAL;
3205
3206 /* For now we don't support modifying channels while XDP is loaded
3207 * also when XDP is loaded all RX queues have XDP programs so we only
3208 * need to check a single RX queue.
3209 */
3210 if (vi->rq[0].xdp_prog)
3211 return -EINVAL;
3212
3213 cpus_read_lock();
3214 err = _virtnet_set_queues(vi, queue_pairs);
3215 if (err) {
3216 cpus_read_unlock();
3217 goto err;
3218 }
3219 virtnet_set_affinity(vi);
3220 cpus_read_unlock();
3221
3222 netif_set_real_num_tx_queues(dev, queue_pairs);
3223 netif_set_real_num_rx_queues(dev, queue_pairs);
3224 err:
3225 return err;
3226 }
3227
virtnet_get_strings(struct net_device * dev,u32 stringset,u8 * data)3228 static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
3229 {
3230 struct virtnet_info *vi = netdev_priv(dev);
3231 unsigned int i, j;
3232 u8 *p = data;
3233
3234 switch (stringset) {
3235 case ETH_SS_STATS:
3236 for (i = 0; i < vi->curr_queue_pairs; i++) {
3237 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
3238 ethtool_sprintf(&p, "rx_queue_%u_%s", i,
3239 virtnet_rq_stats_desc[j].desc);
3240 }
3241
3242 for (i = 0; i < vi->curr_queue_pairs; i++) {
3243 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
3244 ethtool_sprintf(&p, "tx_queue_%u_%s", i,
3245 virtnet_sq_stats_desc[j].desc);
3246 }
3247 break;
3248 }
3249 }
3250
virtnet_get_sset_count(struct net_device * dev,int sset)3251 static int virtnet_get_sset_count(struct net_device *dev, int sset)
3252 {
3253 struct virtnet_info *vi = netdev_priv(dev);
3254
3255 switch (sset) {
3256 case ETH_SS_STATS:
3257 return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
3258 VIRTNET_SQ_STATS_LEN);
3259 default:
3260 return -EOPNOTSUPP;
3261 }
3262 }
3263
virtnet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)3264 static void virtnet_get_ethtool_stats(struct net_device *dev,
3265 struct ethtool_stats *stats, u64 *data)
3266 {
3267 struct virtnet_info *vi = netdev_priv(dev);
3268 unsigned int idx = 0, start, i, j;
3269 const u8 *stats_base;
3270 const u64_stats_t *p;
3271 size_t offset;
3272
3273 for (i = 0; i < vi->curr_queue_pairs; i++) {
3274 struct receive_queue *rq = &vi->rq[i];
3275
3276 stats_base = (const u8 *)&rq->stats;
3277 do {
3278 start = u64_stats_fetch_begin(&rq->stats.syncp);
3279 for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
3280 offset = virtnet_rq_stats_desc[j].offset;
3281 p = (const u64_stats_t *)(stats_base + offset);
3282 data[idx + j] = u64_stats_read(p);
3283 }
3284 } while (u64_stats_fetch_retry(&rq->stats.syncp, start));
3285 idx += VIRTNET_RQ_STATS_LEN;
3286 }
3287
3288 for (i = 0; i < vi->curr_queue_pairs; i++) {
3289 struct send_queue *sq = &vi->sq[i];
3290
3291 stats_base = (const u8 *)&sq->stats;
3292 do {
3293 start = u64_stats_fetch_begin(&sq->stats.syncp);
3294 for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
3295 offset = virtnet_sq_stats_desc[j].offset;
3296 p = (const u64_stats_t *)(stats_base + offset);
3297 data[idx + j] = u64_stats_read(p);
3298 }
3299 } while (u64_stats_fetch_retry(&sq->stats.syncp, start));
3300 idx += VIRTNET_SQ_STATS_LEN;
3301 }
3302 }
3303
virtnet_get_channels(struct net_device * dev,struct ethtool_channels * channels)3304 static void virtnet_get_channels(struct net_device *dev,
3305 struct ethtool_channels *channels)
3306 {
3307 struct virtnet_info *vi = netdev_priv(dev);
3308
3309 channels->combined_count = vi->curr_queue_pairs;
3310 channels->max_combined = vi->max_queue_pairs;
3311 channels->max_other = 0;
3312 channels->rx_count = 0;
3313 channels->tx_count = 0;
3314 channels->other_count = 0;
3315 }
3316
virtnet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)3317 static int virtnet_set_link_ksettings(struct net_device *dev,
3318 const struct ethtool_link_ksettings *cmd)
3319 {
3320 struct virtnet_info *vi = netdev_priv(dev);
3321
3322 return ethtool_virtdev_set_link_ksettings(dev, cmd,
3323 &vi->speed, &vi->duplex);
3324 }
3325
virtnet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)3326 static int virtnet_get_link_ksettings(struct net_device *dev,
3327 struct ethtool_link_ksettings *cmd)
3328 {
3329 struct virtnet_info *vi = netdev_priv(dev);
3330
3331 cmd->base.speed = vi->speed;
3332 cmd->base.duplex = vi->duplex;
3333 cmd->base.port = PORT_OTHER;
3334
3335 return 0;
3336 }
3337
virtnet_send_notf_coal_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec)3338 static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
3339 struct ethtool_coalesce *ec)
3340 {
3341 struct scatterlist sgs_tx, sgs_rx;
3342 int i;
3343
3344 vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
3345 vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
3346 sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
3347
3348 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3349 VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
3350 &sgs_tx))
3351 return -EINVAL;
3352
3353 /* Save parameters */
3354 vi->intr_coal_tx.max_usecs = ec->tx_coalesce_usecs;
3355 vi->intr_coal_tx.max_packets = ec->tx_max_coalesced_frames;
3356 for (i = 0; i < vi->max_queue_pairs; i++) {
3357 vi->sq[i].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3358 vi->sq[i].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3359 }
3360
3361 vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
3362 vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
3363 sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
3364
3365 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3366 VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
3367 &sgs_rx))
3368 return -EINVAL;
3369
3370 /* Save parameters */
3371 vi->intr_coal_rx.max_usecs = ec->rx_coalesce_usecs;
3372 vi->intr_coal_rx.max_packets = ec->rx_max_coalesced_frames;
3373 for (i = 0; i < vi->max_queue_pairs; i++) {
3374 vi->rq[i].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3375 vi->rq[i].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3376 }
3377
3378 return 0;
3379 }
3380
virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info * vi,u16 vqn,u32 max_usecs,u32 max_packets)3381 static int virtnet_send_ctrl_coal_vq_cmd(struct virtnet_info *vi,
3382 u16 vqn, u32 max_usecs, u32 max_packets)
3383 {
3384 struct scatterlist sgs;
3385
3386 vi->ctrl->coal_vq.vqn = cpu_to_le16(vqn);
3387 vi->ctrl->coal_vq.coal.max_usecs = cpu_to_le32(max_usecs);
3388 vi->ctrl->coal_vq.coal.max_packets = cpu_to_le32(max_packets);
3389 sg_init_one(&sgs, &vi->ctrl->coal_vq, sizeof(vi->ctrl->coal_vq));
3390
3391 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
3392 VIRTIO_NET_CTRL_NOTF_COAL_VQ_SET,
3393 &sgs))
3394 return -EINVAL;
3395
3396 return 0;
3397 }
3398
virtnet_send_notf_coal_vq_cmds(struct virtnet_info * vi,struct ethtool_coalesce * ec,u16 queue)3399 static int virtnet_send_notf_coal_vq_cmds(struct virtnet_info *vi,
3400 struct ethtool_coalesce *ec,
3401 u16 queue)
3402 {
3403 int err;
3404
3405 err = virtnet_send_ctrl_coal_vq_cmd(vi, rxq2vq(queue),
3406 ec->rx_coalesce_usecs,
3407 ec->rx_max_coalesced_frames);
3408 if (err)
3409 return err;
3410
3411 vi->rq[queue].intr_coal.max_usecs = ec->rx_coalesce_usecs;
3412 vi->rq[queue].intr_coal.max_packets = ec->rx_max_coalesced_frames;
3413
3414 err = virtnet_send_ctrl_coal_vq_cmd(vi, txq2vq(queue),
3415 ec->tx_coalesce_usecs,
3416 ec->tx_max_coalesced_frames);
3417 if (err)
3418 return err;
3419
3420 vi->sq[queue].intr_coal.max_usecs = ec->tx_coalesce_usecs;
3421 vi->sq[queue].intr_coal.max_packets = ec->tx_max_coalesced_frames;
3422
3423 return 0;
3424 }
3425
virtnet_coal_params_supported(struct ethtool_coalesce * ec)3426 static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
3427 {
3428 /* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
3429 * feature is negotiated.
3430 */
3431 if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
3432 return -EOPNOTSUPP;
3433
3434 if (ec->tx_max_coalesced_frames > 1 ||
3435 ec->rx_max_coalesced_frames != 1)
3436 return -EINVAL;
3437
3438 return 0;
3439 }
3440
virtnet_should_update_vq_weight(int dev_flags,int weight,int vq_weight,bool * should_update)3441 static int virtnet_should_update_vq_weight(int dev_flags, int weight,
3442 int vq_weight, bool *should_update)
3443 {
3444 if (weight ^ vq_weight) {
3445 if (dev_flags & IFF_UP)
3446 return -EBUSY;
3447 *should_update = true;
3448 }
3449
3450 return 0;
3451 }
3452
virtnet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3453 static int virtnet_set_coalesce(struct net_device *dev,
3454 struct ethtool_coalesce *ec,
3455 struct kernel_ethtool_coalesce *kernel_coal,
3456 struct netlink_ext_ack *extack)
3457 {
3458 struct virtnet_info *vi = netdev_priv(dev);
3459 int ret, queue_number, napi_weight;
3460 bool update_napi = false;
3461
3462 /* Can't change NAPI weight if the link is up */
3463 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3464 for (queue_number = 0; queue_number < vi->max_queue_pairs; queue_number++) {
3465 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3466 vi->sq[queue_number].napi.weight,
3467 &update_napi);
3468 if (ret)
3469 return ret;
3470
3471 if (update_napi) {
3472 /* All queues that belong to [queue_number, vi->max_queue_pairs] will be
3473 * updated for the sake of simplicity, which might not be necessary
3474 */
3475 break;
3476 }
3477 }
3478
3479 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
3480 ret = virtnet_send_notf_coal_cmds(vi, ec);
3481 else
3482 ret = virtnet_coal_params_supported(ec);
3483
3484 if (ret)
3485 return ret;
3486
3487 if (update_napi) {
3488 for (; queue_number < vi->max_queue_pairs; queue_number++)
3489 vi->sq[queue_number].napi.weight = napi_weight;
3490 }
3491
3492 return ret;
3493 }
3494
virtnet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)3495 static int virtnet_get_coalesce(struct net_device *dev,
3496 struct ethtool_coalesce *ec,
3497 struct kernel_ethtool_coalesce *kernel_coal,
3498 struct netlink_ext_ack *extack)
3499 {
3500 struct virtnet_info *vi = netdev_priv(dev);
3501
3502 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3503 ec->rx_coalesce_usecs = vi->intr_coal_rx.max_usecs;
3504 ec->tx_coalesce_usecs = vi->intr_coal_tx.max_usecs;
3505 ec->tx_max_coalesced_frames = vi->intr_coal_tx.max_packets;
3506 ec->rx_max_coalesced_frames = vi->intr_coal_rx.max_packets;
3507 } else {
3508 ec->rx_max_coalesced_frames = 1;
3509
3510 if (vi->sq[0].napi.weight)
3511 ec->tx_max_coalesced_frames = 1;
3512 }
3513
3514 return 0;
3515 }
3516
virtnet_set_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3517 static int virtnet_set_per_queue_coalesce(struct net_device *dev,
3518 u32 queue,
3519 struct ethtool_coalesce *ec)
3520 {
3521 struct virtnet_info *vi = netdev_priv(dev);
3522 int ret, napi_weight;
3523 bool update_napi = false;
3524
3525 if (queue >= vi->max_queue_pairs)
3526 return -EINVAL;
3527
3528 /* Can't change NAPI weight if the link is up */
3529 napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
3530 ret = virtnet_should_update_vq_weight(dev->flags, napi_weight,
3531 vi->sq[queue].napi.weight,
3532 &update_napi);
3533 if (ret)
3534 return ret;
3535
3536 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL))
3537 ret = virtnet_send_notf_coal_vq_cmds(vi, ec, queue);
3538 else
3539 ret = virtnet_coal_params_supported(ec);
3540
3541 if (ret)
3542 return ret;
3543
3544 if (update_napi)
3545 vi->sq[queue].napi.weight = napi_weight;
3546
3547 return 0;
3548 }
3549
virtnet_get_per_queue_coalesce(struct net_device * dev,u32 queue,struct ethtool_coalesce * ec)3550 static int virtnet_get_per_queue_coalesce(struct net_device *dev,
3551 u32 queue,
3552 struct ethtool_coalesce *ec)
3553 {
3554 struct virtnet_info *vi = netdev_priv(dev);
3555
3556 if (queue >= vi->max_queue_pairs)
3557 return -EINVAL;
3558
3559 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_VQ_NOTF_COAL)) {
3560 ec->rx_coalesce_usecs = vi->rq[queue].intr_coal.max_usecs;
3561 ec->tx_coalesce_usecs = vi->sq[queue].intr_coal.max_usecs;
3562 ec->tx_max_coalesced_frames = vi->sq[queue].intr_coal.max_packets;
3563 ec->rx_max_coalesced_frames = vi->rq[queue].intr_coal.max_packets;
3564 } else {
3565 ec->rx_max_coalesced_frames = 1;
3566
3567 if (vi->sq[queue].napi.weight)
3568 ec->tx_max_coalesced_frames = 1;
3569 }
3570
3571 return 0;
3572 }
3573
virtnet_init_settings(struct net_device * dev)3574 static void virtnet_init_settings(struct net_device *dev)
3575 {
3576 struct virtnet_info *vi = netdev_priv(dev);
3577
3578 vi->speed = SPEED_UNKNOWN;
3579 vi->duplex = DUPLEX_UNKNOWN;
3580 }
3581
virtnet_update_settings(struct virtnet_info * vi)3582 static void virtnet_update_settings(struct virtnet_info *vi)
3583 {
3584 u32 speed;
3585 u8 duplex;
3586
3587 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3588 return;
3589
3590 virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
3591
3592 if (ethtool_validate_speed(speed))
3593 vi->speed = speed;
3594
3595 virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
3596
3597 if (ethtool_validate_duplex(duplex))
3598 vi->duplex = duplex;
3599 }
3600
virtnet_get_rxfh_key_size(struct net_device * dev)3601 static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3602 {
3603 return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3604 }
3605
virtnet_get_rxfh_indir_size(struct net_device * dev)3606 static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3607 {
3608 return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3609 }
3610
virtnet_get_rxfh(struct net_device * dev,u32 * indir,u8 * key,u8 * hfunc)3611 static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3612 {
3613 struct virtnet_info *vi = netdev_priv(dev);
3614 int i;
3615
3616 if (indir) {
3617 for (i = 0; i < vi->rss_indir_table_size; ++i)
3618 indir[i] = vi->ctrl->rss.indirection_table[i];
3619 }
3620
3621 if (key)
3622 memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3623
3624 if (hfunc)
3625 *hfunc = ETH_RSS_HASH_TOP;
3626
3627 return 0;
3628 }
3629
virtnet_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * key,const u8 hfunc)3630 static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3631 {
3632 struct virtnet_info *vi = netdev_priv(dev);
3633 bool update = false;
3634 int i;
3635
3636 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3637 return -EOPNOTSUPP;
3638
3639 if (indir) {
3640 if (!vi->has_rss)
3641 return -EOPNOTSUPP;
3642
3643 for (i = 0; i < vi->rss_indir_table_size; ++i)
3644 vi->ctrl->rss.indirection_table[i] = indir[i];
3645 update = true;
3646 }
3647 if (key) {
3648 /* If either _F_HASH_REPORT or _F_RSS are negotiated, the
3649 * device provides hash calculation capabilities, that is,
3650 * hash_key is configured.
3651 */
3652 if (!vi->has_rss && !vi->has_rss_hash_report)
3653 return -EOPNOTSUPP;
3654
3655 memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
3656 update = true;
3657 }
3658
3659 if (update)
3660 virtnet_commit_rss_command(vi);
3661
3662 return 0;
3663 }
3664
virtnet_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rule_locs)3665 static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3666 {
3667 struct virtnet_info *vi = netdev_priv(dev);
3668 int rc = 0;
3669
3670 switch (info->cmd) {
3671 case ETHTOOL_GRXRINGS:
3672 info->data = vi->curr_queue_pairs;
3673 break;
3674 case ETHTOOL_GRXFH:
3675 virtnet_get_hashflow(vi, info);
3676 break;
3677 default:
3678 rc = -EOPNOTSUPP;
3679 }
3680
3681 return rc;
3682 }
3683
virtnet_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)3684 static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3685 {
3686 struct virtnet_info *vi = netdev_priv(dev);
3687 int rc = 0;
3688
3689 switch (info->cmd) {
3690 case ETHTOOL_SRXFH:
3691 if (!virtnet_set_hashflow(vi, info))
3692 rc = -EINVAL;
3693
3694 break;
3695 default:
3696 rc = -EOPNOTSUPP;
3697 }
3698
3699 return rc;
3700 }
3701
3702 static const struct ethtool_ops virtnet_ethtool_ops = {
3703 .supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3704 ETHTOOL_COALESCE_USECS,
3705 .get_drvinfo = virtnet_get_drvinfo,
3706 .get_link = ethtool_op_get_link,
3707 .get_ringparam = virtnet_get_ringparam,
3708 .set_ringparam = virtnet_set_ringparam,
3709 .get_strings = virtnet_get_strings,
3710 .get_sset_count = virtnet_get_sset_count,
3711 .get_ethtool_stats = virtnet_get_ethtool_stats,
3712 .set_channels = virtnet_set_channels,
3713 .get_channels = virtnet_get_channels,
3714 .get_ts_info = ethtool_op_get_ts_info,
3715 .get_link_ksettings = virtnet_get_link_ksettings,
3716 .set_link_ksettings = virtnet_set_link_ksettings,
3717 .set_coalesce = virtnet_set_coalesce,
3718 .get_coalesce = virtnet_get_coalesce,
3719 .set_per_queue_coalesce = virtnet_set_per_queue_coalesce,
3720 .get_per_queue_coalesce = virtnet_get_per_queue_coalesce,
3721 .get_rxfh_key_size = virtnet_get_rxfh_key_size,
3722 .get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3723 .get_rxfh = virtnet_get_rxfh,
3724 .set_rxfh = virtnet_set_rxfh,
3725 .get_rxnfc = virtnet_get_rxnfc,
3726 .set_rxnfc = virtnet_set_rxnfc,
3727 };
3728
virtnet_freeze_down(struct virtio_device * vdev)3729 static void virtnet_freeze_down(struct virtio_device *vdev)
3730 {
3731 struct virtnet_info *vi = vdev->priv;
3732
3733 /* Make sure no work handler is accessing the device */
3734 flush_work(&vi->config_work);
3735
3736 netif_tx_lock_bh(vi->dev);
3737 netif_device_detach(vi->dev);
3738 netif_tx_unlock_bh(vi->dev);
3739 if (netif_running(vi->dev))
3740 virtnet_close(vi->dev);
3741 }
3742
3743 static int init_vqs(struct virtnet_info *vi);
3744
virtnet_restore_up(struct virtio_device * vdev)3745 static int virtnet_restore_up(struct virtio_device *vdev)
3746 {
3747 struct virtnet_info *vi = vdev->priv;
3748 int err;
3749
3750 err = init_vqs(vi);
3751 if (err)
3752 return err;
3753
3754 virtio_device_ready(vdev);
3755
3756 enable_delayed_refill(vi);
3757
3758 if (netif_running(vi->dev)) {
3759 err = virtnet_open(vi->dev);
3760 if (err)
3761 return err;
3762 }
3763
3764 netif_tx_lock_bh(vi->dev);
3765 netif_device_attach(vi->dev);
3766 netif_tx_unlock_bh(vi->dev);
3767 return err;
3768 }
3769
virtnet_set_guest_offloads(struct virtnet_info * vi,u64 offloads)3770 static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
3771 {
3772 struct scatterlist sg;
3773 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
3774
3775 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
3776
3777 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
3778 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
3779 dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
3780 return -EINVAL;
3781 }
3782
3783 return 0;
3784 }
3785
virtnet_clear_guest_offloads(struct virtnet_info * vi)3786 static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
3787 {
3788 u64 offloads = 0;
3789
3790 if (!vi->guest_offloads)
3791 return 0;
3792
3793 return virtnet_set_guest_offloads(vi, offloads);
3794 }
3795
virtnet_restore_guest_offloads(struct virtnet_info * vi)3796 static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
3797 {
3798 u64 offloads = vi->guest_offloads;
3799
3800 if (!vi->guest_offloads)
3801 return 0;
3802
3803 return virtnet_set_guest_offloads(vi, offloads);
3804 }
3805
virtnet_xdp_set(struct net_device * dev,struct bpf_prog * prog,struct netlink_ext_ack * extack)3806 static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
3807 struct netlink_ext_ack *extack)
3808 {
3809 unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3810 sizeof(struct skb_shared_info));
3811 unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3812 struct virtnet_info *vi = netdev_priv(dev);
3813 struct bpf_prog *old_prog;
3814 u16 xdp_qp = 0, curr_qp;
3815 int i, err;
3816
3817 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
3818 && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3819 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
3820 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3821 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3822 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3823 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3824 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3825 NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3826 return -EOPNOTSUPP;
3827 }
3828
3829 if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
3830 NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3831 return -EINVAL;
3832 }
3833
3834 if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
3835 NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
3836 netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3837 return -EINVAL;
3838 }
3839
3840 curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3841 if (prog)
3842 xdp_qp = nr_cpu_ids;
3843
3844 /* XDP requires extra queues for XDP_TX */
3845 if (curr_qp + xdp_qp > vi->max_queue_pairs) {
3846 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",
3847 curr_qp + xdp_qp, vi->max_queue_pairs);
3848 xdp_qp = 0;
3849 }
3850
3851 old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
3852 if (!prog && !old_prog)
3853 return 0;
3854
3855 if (prog)
3856 bpf_prog_add(prog, vi->max_queue_pairs - 1);
3857
3858 /* Make sure NAPI is not using any XDP TX queues for RX. */
3859 if (netif_running(dev)) {
3860 for (i = 0; i < vi->max_queue_pairs; i++) {
3861 napi_disable(&vi->rq[i].napi);
3862 virtnet_napi_tx_disable(&vi->sq[i].napi);
3863 }
3864 }
3865
3866 if (!prog) {
3867 for (i = 0; i < vi->max_queue_pairs; i++) {
3868 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3869 if (i == 0)
3870 virtnet_restore_guest_offloads(vi);
3871 }
3872 synchronize_net();
3873 }
3874
3875 err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
3876 if (err)
3877 goto err;
3878 netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
3879 vi->xdp_queue_pairs = xdp_qp;
3880
3881 if (prog) {
3882 vi->xdp_enabled = true;
3883 for (i = 0; i < vi->max_queue_pairs; i++) {
3884 rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
3885 if (i == 0 && !old_prog)
3886 virtnet_clear_guest_offloads(vi);
3887 }
3888 if (!old_prog)
3889 xdp_features_set_redirect_target(dev, true);
3890 } else {
3891 xdp_features_clear_redirect_target(dev);
3892 vi->xdp_enabled = false;
3893 }
3894
3895 for (i = 0; i < vi->max_queue_pairs; i++) {
3896 if (old_prog)
3897 bpf_prog_put(old_prog);
3898 if (netif_running(dev)) {
3899 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3900 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3901 &vi->sq[i].napi);
3902 }
3903 }
3904
3905 return 0;
3906
3907 err:
3908 if (!prog) {
3909 virtnet_clear_guest_offloads(vi);
3910 for (i = 0; i < vi->max_queue_pairs; i++)
3911 rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
3912 }
3913
3914 if (netif_running(dev)) {
3915 for (i = 0; i < vi->max_queue_pairs; i++) {
3916 virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3917 virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3918 &vi->sq[i].napi);
3919 }
3920 }
3921 if (prog)
3922 bpf_prog_sub(prog, vi->max_queue_pairs - 1);
3923 return err;
3924 }
3925
virtnet_xdp(struct net_device * dev,struct netdev_bpf * xdp)3926 static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3927 {
3928 switch (xdp->command) {
3929 case XDP_SETUP_PROG:
3930 return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3931 default:
3932 return -EINVAL;
3933 }
3934 }
3935
virtnet_get_phys_port_name(struct net_device * dev,char * buf,size_t len)3936 static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3937 size_t len)
3938 {
3939 struct virtnet_info *vi = netdev_priv(dev);
3940 int ret;
3941
3942 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3943 return -EOPNOTSUPP;
3944
3945 ret = snprintf(buf, len, "sby");
3946 if (ret >= len)
3947 return -EOPNOTSUPP;
3948
3949 return 0;
3950 }
3951
virtnet_set_features(struct net_device * dev,netdev_features_t features)3952 static int virtnet_set_features(struct net_device *dev,
3953 netdev_features_t features)
3954 {
3955 struct virtnet_info *vi = netdev_priv(dev);
3956 u64 offloads;
3957 int err;
3958
3959 if ((dev->features ^ features) & NETIF_F_GRO_HW) {
3960 if (vi->xdp_enabled)
3961 return -EBUSY;
3962
3963 if (features & NETIF_F_GRO_HW)
3964 offloads = vi->guest_offloads_capable;
3965 else
3966 offloads = vi->guest_offloads_capable &
3967 ~GUEST_OFFLOAD_GRO_HW_MASK;
3968
3969 err = virtnet_set_guest_offloads(vi, offloads);
3970 if (err)
3971 return err;
3972 vi->guest_offloads = offloads;
3973 }
3974
3975 if ((dev->features ^ features) & NETIF_F_RXHASH) {
3976 if (features & NETIF_F_RXHASH)
3977 vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3978 else
3979 vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3980
3981 if (!virtnet_commit_rss_command(vi))
3982 return -EINVAL;
3983 }
3984
3985 return 0;
3986 }
3987
virtnet_tx_timeout(struct net_device * dev,unsigned int txqueue)3988 static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3989 {
3990 struct virtnet_info *priv = netdev_priv(dev);
3991 struct send_queue *sq = &priv->sq[txqueue];
3992 struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3993
3994 u64_stats_update_begin(&sq->stats.syncp);
3995 u64_stats_inc(&sq->stats.tx_timeouts);
3996 u64_stats_update_end(&sq->stats.syncp);
3997
3998 netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3999 txqueue, sq->name, sq->vq->index, sq->vq->name,
4000 jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
4001 }
4002
4003 static const struct net_device_ops virtnet_netdev = {
4004 .ndo_open = virtnet_open,
4005 .ndo_stop = virtnet_close,
4006 .ndo_start_xmit = start_xmit,
4007 .ndo_validate_addr = eth_validate_addr,
4008 .ndo_set_mac_address = virtnet_set_mac_address,
4009 .ndo_set_rx_mode = virtnet_set_rx_mode,
4010 .ndo_get_stats64 = virtnet_stats,
4011 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
4012 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
4013 .ndo_bpf = virtnet_xdp,
4014 .ndo_xdp_xmit = virtnet_xdp_xmit,
4015 .ndo_features_check = passthru_features_check,
4016 .ndo_get_phys_port_name = virtnet_get_phys_port_name,
4017 .ndo_set_features = virtnet_set_features,
4018 .ndo_tx_timeout = virtnet_tx_timeout,
4019 };
4020
virtnet_config_changed_work(struct work_struct * work)4021 static void virtnet_config_changed_work(struct work_struct *work)
4022 {
4023 struct virtnet_info *vi =
4024 container_of(work, struct virtnet_info, config_work);
4025 u16 v;
4026
4027 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
4028 struct virtio_net_config, status, &v) < 0)
4029 return;
4030
4031 if (v & VIRTIO_NET_S_ANNOUNCE) {
4032 netdev_notify_peers(vi->dev);
4033 virtnet_ack_link_announce(vi);
4034 }
4035
4036 /* Ignore unknown (future) status bits */
4037 v &= VIRTIO_NET_S_LINK_UP;
4038
4039 if (vi->status == v)
4040 return;
4041
4042 vi->status = v;
4043
4044 if (vi->status & VIRTIO_NET_S_LINK_UP) {
4045 virtnet_update_settings(vi);
4046 netif_carrier_on(vi->dev);
4047 netif_tx_wake_all_queues(vi->dev);
4048 } else {
4049 netif_carrier_off(vi->dev);
4050 netif_tx_stop_all_queues(vi->dev);
4051 }
4052 }
4053
virtnet_config_changed(struct virtio_device * vdev)4054 static void virtnet_config_changed(struct virtio_device *vdev)
4055 {
4056 struct virtnet_info *vi = vdev->priv;
4057
4058 schedule_work(&vi->config_work);
4059 }
4060
virtnet_free_queues(struct virtnet_info * vi)4061 static void virtnet_free_queues(struct virtnet_info *vi)
4062 {
4063 int i;
4064
4065 for (i = 0; i < vi->max_queue_pairs; i++) {
4066 __netif_napi_del(&vi->rq[i].napi);
4067 __netif_napi_del(&vi->sq[i].napi);
4068 }
4069
4070 /* We called __netif_napi_del(),
4071 * we need to respect an RCU grace period before freeing vi->rq
4072 */
4073 synchronize_net();
4074
4075 kfree(vi->rq);
4076 kfree(vi->sq);
4077 kfree(vi->ctrl);
4078 }
4079
_free_receive_bufs(struct virtnet_info * vi)4080 static void _free_receive_bufs(struct virtnet_info *vi)
4081 {
4082 struct bpf_prog *old_prog;
4083 int i;
4084
4085 for (i = 0; i < vi->max_queue_pairs; i++) {
4086 while (vi->rq[i].pages)
4087 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
4088
4089 old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
4090 RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
4091 if (old_prog)
4092 bpf_prog_put(old_prog);
4093 }
4094 }
4095
free_receive_bufs(struct virtnet_info * vi)4096 static void free_receive_bufs(struct virtnet_info *vi)
4097 {
4098 rtnl_lock();
4099 _free_receive_bufs(vi);
4100 rtnl_unlock();
4101 }
4102
free_receive_page_frags(struct virtnet_info * vi)4103 static void free_receive_page_frags(struct virtnet_info *vi)
4104 {
4105 int i;
4106 for (i = 0; i < vi->max_queue_pairs; i++)
4107 if (vi->rq[i].alloc_frag.page) {
4108 if (vi->rq[i].do_dma && vi->rq[i].last_dma)
4109 virtnet_rq_unmap(&vi->rq[i], vi->rq[i].last_dma, 0);
4110 put_page(vi->rq[i].alloc_frag.page);
4111 }
4112 }
4113
virtnet_sq_free_unused_buf(struct virtqueue * vq,void * buf)4114 static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
4115 {
4116 if (!is_xdp_frame(buf))
4117 dev_kfree_skb(buf);
4118 else
4119 xdp_return_frame(ptr_to_xdp(buf));
4120 }
4121
free_unused_bufs(struct virtnet_info * vi)4122 static void free_unused_bufs(struct virtnet_info *vi)
4123 {
4124 void *buf;
4125 int i;
4126
4127 for (i = 0; i < vi->max_queue_pairs; i++) {
4128 struct virtqueue *vq = vi->sq[i].vq;
4129 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4130 virtnet_sq_free_unused_buf(vq, buf);
4131 cond_resched();
4132 }
4133
4134 for (i = 0; i < vi->max_queue_pairs; i++) {
4135 struct virtqueue *vq = vi->rq[i].vq;
4136
4137 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
4138 virtnet_rq_unmap_free_buf(vq, buf);
4139 cond_resched();
4140 }
4141 }
4142
virtnet_del_vqs(struct virtnet_info * vi)4143 static void virtnet_del_vqs(struct virtnet_info *vi)
4144 {
4145 struct virtio_device *vdev = vi->vdev;
4146
4147 virtnet_clean_affinity(vi);
4148
4149 vdev->config->del_vqs(vdev);
4150
4151 virtnet_free_queues(vi);
4152 }
4153
4154 /* How large should a single buffer be so a queue full of these can fit at
4155 * least one full packet?
4156 * Logic below assumes the mergeable buffer header is used.
4157 */
mergeable_min_buf_len(struct virtnet_info * vi,struct virtqueue * vq)4158 static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
4159 {
4160 const unsigned int hdr_len = vi->hdr_len;
4161 unsigned int rq_size = virtqueue_get_vring_size(vq);
4162 unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
4163 unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
4164 unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
4165
4166 return max(max(min_buf_len, hdr_len) - hdr_len,
4167 (unsigned int)GOOD_PACKET_LEN);
4168 }
4169
virtnet_find_vqs(struct virtnet_info * vi)4170 static int virtnet_find_vqs(struct virtnet_info *vi)
4171 {
4172 vq_callback_t **callbacks;
4173 struct virtqueue **vqs;
4174 const char **names;
4175 int ret = -ENOMEM;
4176 int total_vqs;
4177 bool *ctx;
4178 u16 i;
4179
4180 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
4181 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
4182 * possible control vq.
4183 */
4184 total_vqs = vi->max_queue_pairs * 2 +
4185 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
4186
4187 /* Allocate space for find_vqs parameters */
4188 vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
4189 if (!vqs)
4190 goto err_vq;
4191 callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
4192 if (!callbacks)
4193 goto err_callback;
4194 names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
4195 if (!names)
4196 goto err_names;
4197 if (!vi->big_packets || vi->mergeable_rx_bufs) {
4198 ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
4199 if (!ctx)
4200 goto err_ctx;
4201 } else {
4202 ctx = NULL;
4203 }
4204
4205 /* Parameters for control virtqueue, if any */
4206 if (vi->has_cvq) {
4207 callbacks[total_vqs - 1] = NULL;
4208 names[total_vqs - 1] = "control";
4209 }
4210
4211 /* Allocate/initialize parameters for send/receive virtqueues */
4212 for (i = 0; i < vi->max_queue_pairs; i++) {
4213 callbacks[rxq2vq(i)] = skb_recv_done;
4214 callbacks[txq2vq(i)] = skb_xmit_done;
4215 sprintf(vi->rq[i].name, "input.%u", i);
4216 sprintf(vi->sq[i].name, "output.%u", i);
4217 names[rxq2vq(i)] = vi->rq[i].name;
4218 names[txq2vq(i)] = vi->sq[i].name;
4219 if (ctx)
4220 ctx[rxq2vq(i)] = true;
4221 }
4222
4223 ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
4224 names, ctx, NULL);
4225 if (ret)
4226 goto err_find;
4227
4228 if (vi->has_cvq) {
4229 vi->cvq = vqs[total_vqs - 1];
4230 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
4231 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4232 }
4233
4234 for (i = 0; i < vi->max_queue_pairs; i++) {
4235 vi->rq[i].vq = vqs[rxq2vq(i)];
4236 vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
4237 vi->sq[i].vq = vqs[txq2vq(i)];
4238 }
4239
4240 /* run here: ret == 0. */
4241
4242
4243 err_find:
4244 kfree(ctx);
4245 err_ctx:
4246 kfree(names);
4247 err_names:
4248 kfree(callbacks);
4249 err_callback:
4250 kfree(vqs);
4251 err_vq:
4252 return ret;
4253 }
4254
virtnet_alloc_queues(struct virtnet_info * vi)4255 static int virtnet_alloc_queues(struct virtnet_info *vi)
4256 {
4257 int i;
4258
4259 if (vi->has_cvq) {
4260 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
4261 if (!vi->ctrl)
4262 goto err_ctrl;
4263 } else {
4264 vi->ctrl = NULL;
4265 }
4266 vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
4267 if (!vi->sq)
4268 goto err_sq;
4269 vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
4270 if (!vi->rq)
4271 goto err_rq;
4272
4273 INIT_DELAYED_WORK(&vi->refill, refill_work);
4274 for (i = 0; i < vi->max_queue_pairs; i++) {
4275 vi->rq[i].pages = NULL;
4276 netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
4277 napi_weight);
4278 netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
4279 virtnet_poll_tx,
4280 napi_tx ? napi_weight : 0);
4281
4282 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
4283 ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
4284 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
4285
4286 u64_stats_init(&vi->rq[i].stats.syncp);
4287 u64_stats_init(&vi->sq[i].stats.syncp);
4288 }
4289
4290 return 0;
4291
4292 err_rq:
4293 kfree(vi->sq);
4294 err_sq:
4295 kfree(vi->ctrl);
4296 err_ctrl:
4297 return -ENOMEM;
4298 }
4299
init_vqs(struct virtnet_info * vi)4300 static int init_vqs(struct virtnet_info *vi)
4301 {
4302 int ret;
4303
4304 /* Allocate send & receive queues */
4305 ret = virtnet_alloc_queues(vi);
4306 if (ret)
4307 goto err;
4308
4309 ret = virtnet_find_vqs(vi);
4310 if (ret)
4311 goto err_free;
4312
4313 virtnet_rq_set_premapped(vi);
4314
4315 cpus_read_lock();
4316 virtnet_set_affinity(vi);
4317 cpus_read_unlock();
4318
4319 return 0;
4320
4321 err_free:
4322 virtnet_free_queues(vi);
4323 err:
4324 return ret;
4325 }
4326
4327 #ifdef CONFIG_SYSFS
mergeable_rx_buffer_size_show(struct netdev_rx_queue * queue,char * buf)4328 static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
4329 char *buf)
4330 {
4331 struct virtnet_info *vi = netdev_priv(queue->dev);
4332 unsigned int queue_index = get_netdev_rx_queue_index(queue);
4333 unsigned int headroom = virtnet_get_headroom(vi);
4334 unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
4335 struct ewma_pkt_len *avg;
4336
4337 BUG_ON(queue_index >= vi->max_queue_pairs);
4338 avg = &vi->rq[queue_index].mrg_avg_pkt_len;
4339 return sprintf(buf, "%u\n",
4340 get_mergeable_buf_len(&vi->rq[queue_index], avg,
4341 SKB_DATA_ALIGN(headroom + tailroom)));
4342 }
4343
4344 static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
4345 __ATTR_RO(mergeable_rx_buffer_size);
4346
4347 static struct attribute *virtio_net_mrg_rx_attrs[] = {
4348 &mergeable_rx_buffer_size_attribute.attr,
4349 NULL
4350 };
4351
4352 static const struct attribute_group virtio_net_mrg_rx_group = {
4353 .name = "virtio_net",
4354 .attrs = virtio_net_mrg_rx_attrs
4355 };
4356 #endif
4357
virtnet_fail_on_feature(struct virtio_device * vdev,unsigned int fbit,const char * fname,const char * dname)4358 static bool virtnet_fail_on_feature(struct virtio_device *vdev,
4359 unsigned int fbit,
4360 const char *fname, const char *dname)
4361 {
4362 if (!virtio_has_feature(vdev, fbit))
4363 return false;
4364
4365 dev_err(&vdev->dev, "device advertises feature %s but not %s",
4366 fname, dname);
4367
4368 return true;
4369 }
4370
4371 #define VIRTNET_FAIL_ON(vdev, fbit, dbit) \
4372 virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
4373
virtnet_validate_features(struct virtio_device * vdev)4374 static bool virtnet_validate_features(struct virtio_device *vdev)
4375 {
4376 if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
4377 (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
4378 "VIRTIO_NET_F_CTRL_VQ") ||
4379 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
4380 "VIRTIO_NET_F_CTRL_VQ") ||
4381 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
4382 "VIRTIO_NET_F_CTRL_VQ") ||
4383 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
4384 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
4385 "VIRTIO_NET_F_CTRL_VQ") ||
4386 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
4387 "VIRTIO_NET_F_CTRL_VQ") ||
4388 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
4389 "VIRTIO_NET_F_CTRL_VQ") ||
4390 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
4391 "VIRTIO_NET_F_CTRL_VQ") ||
4392 VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_VQ_NOTF_COAL,
4393 "VIRTIO_NET_F_CTRL_VQ"))) {
4394 return false;
4395 }
4396
4397 return true;
4398 }
4399
4400 #define MIN_MTU ETH_MIN_MTU
4401 #define MAX_MTU ETH_MAX_MTU
4402
virtnet_validate(struct virtio_device * vdev)4403 static int virtnet_validate(struct virtio_device *vdev)
4404 {
4405 if (!vdev->config->get) {
4406 dev_err(&vdev->dev, "%s failure: config access disabled\n",
4407 __func__);
4408 return -EINVAL;
4409 }
4410
4411 if (!virtnet_validate_features(vdev))
4412 return -EINVAL;
4413
4414 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4415 int mtu = virtio_cread16(vdev,
4416 offsetof(struct virtio_net_config,
4417 mtu));
4418 if (mtu < MIN_MTU)
4419 __virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
4420 }
4421
4422 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
4423 !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4424 dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
4425 __virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
4426 }
4427
4428 return 0;
4429 }
4430
virtnet_check_guest_gso(const struct virtnet_info * vi)4431 static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
4432 {
4433 return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4434 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
4435 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
4436 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
4437 (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
4438 virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
4439 }
4440
virtnet_set_big_packets(struct virtnet_info * vi,const int mtu)4441 static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
4442 {
4443 bool guest_gso = virtnet_check_guest_gso(vi);
4444
4445 /* If device can receive ANY guest GSO packets, regardless of mtu,
4446 * allocate packets of maximum size, otherwise limit it to only
4447 * mtu size worth only.
4448 */
4449 if (mtu > ETH_DATA_LEN || guest_gso) {
4450 vi->big_packets = true;
4451 vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
4452 }
4453 }
4454
virtnet_probe(struct virtio_device * vdev)4455 static int virtnet_probe(struct virtio_device *vdev)
4456 {
4457 int i, err = -ENOMEM;
4458 struct net_device *dev;
4459 struct virtnet_info *vi;
4460 u16 max_queue_pairs;
4461 int mtu = 0;
4462
4463 /* Find if host supports multiqueue/rss virtio_net device */
4464 max_queue_pairs = 1;
4465 if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4466 max_queue_pairs =
4467 virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
4468
4469 /* We need at least 2 queue's */
4470 if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
4471 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
4472 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4473 max_queue_pairs = 1;
4474
4475 /* Allocate ourselves a network device with room for our info */
4476 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
4477 if (!dev)
4478 return -ENOMEM;
4479
4480 /* Set up network device as normal. */
4481 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
4482 IFF_TX_SKB_NO_LINEAR;
4483 dev->netdev_ops = &virtnet_netdev;
4484 dev->features = NETIF_F_HIGHDMA;
4485
4486 dev->ethtool_ops = &virtnet_ethtool_ops;
4487 SET_NETDEV_DEV(dev, &vdev->dev);
4488
4489 /* Do we support "hardware" checksums? */
4490 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
4491 /* This opens up the world of extra features. */
4492 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4493 if (csum)
4494 dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
4495
4496 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
4497 dev->hw_features |= NETIF_F_TSO
4498 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
4499 }
4500 /* Individual feature bits: what can host handle? */
4501 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
4502 dev->hw_features |= NETIF_F_TSO;
4503 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
4504 dev->hw_features |= NETIF_F_TSO6;
4505 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
4506 dev->hw_features |= NETIF_F_TSO_ECN;
4507 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
4508 dev->hw_features |= NETIF_F_GSO_UDP_L4;
4509
4510 dev->features |= NETIF_F_GSO_ROBUST;
4511
4512 if (gso)
4513 dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
4514 /* (!csum && gso) case will be fixed by register_netdev() */
4515 }
4516
4517 /* 1. With VIRTIO_NET_F_GUEST_CSUM negotiation, the driver doesn't
4518 * need to calculate checksums for partially checksummed packets,
4519 * as they're considered valid by the upper layer.
4520 * 2. Without VIRTIO_NET_F_GUEST_CSUM negotiation, the driver only
4521 * receives fully checksummed packets. The device may assist in
4522 * validating these packets' checksums, so the driver won't have to.
4523 */
4524 dev->features |= NETIF_F_RXCSUM;
4525
4526 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
4527 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
4528 dev->features |= NETIF_F_GRO_HW;
4529 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
4530 dev->hw_features |= NETIF_F_GRO_HW;
4531
4532 dev->vlan_features = dev->features;
4533 dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
4534
4535 /* MTU range: 68 - 65535 */
4536 dev->min_mtu = MIN_MTU;
4537 dev->max_mtu = MAX_MTU;
4538
4539 /* Configuration may specify what MAC to use. Otherwise random. */
4540 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
4541 u8 addr[ETH_ALEN];
4542
4543 virtio_cread_bytes(vdev,
4544 offsetof(struct virtio_net_config, mac),
4545 addr, ETH_ALEN);
4546 eth_hw_addr_set(dev, addr);
4547 } else {
4548 eth_hw_addr_random(dev);
4549 dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
4550 dev->dev_addr);
4551 }
4552
4553 /* Set up our device-specific information */
4554 vi = netdev_priv(dev);
4555 vi->dev = dev;
4556 vi->vdev = vdev;
4557 vdev->priv = vi;
4558
4559 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
4560 spin_lock_init(&vi->refill_lock);
4561
4562 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
4563 vi->mergeable_rx_bufs = true;
4564 dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
4565 }
4566
4567 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4568 vi->intr_coal_rx.max_usecs = 0;
4569 vi->intr_coal_tx.max_usecs = 0;
4570 vi->intr_coal_tx.max_packets = 0;
4571 vi->intr_coal_rx.max_packets = 0;
4572 }
4573
4574 if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
4575 vi->has_rss_hash_report = true;
4576
4577 if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS)) {
4578 vi->has_rss = true;
4579
4580 vi->rss_indir_table_size =
4581 virtio_cread16(vdev, offsetof(struct virtio_net_config,
4582 rss_max_indirection_table_length));
4583 }
4584
4585 if (vi->has_rss || vi->has_rss_hash_report) {
4586 vi->rss_key_size =
4587 virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
4588 if (vi->rss_key_size > VIRTIO_NET_RSS_MAX_KEY_SIZE) {
4589 dev_err(&vdev->dev, "rss_max_key_size=%u exceeds the limit %u.\n",
4590 vi->rss_key_size, VIRTIO_NET_RSS_MAX_KEY_SIZE);
4591 err = -EINVAL;
4592 goto free;
4593 }
4594
4595 vi->rss_hash_types_supported =
4596 virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
4597 vi->rss_hash_types_supported &=
4598 ~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
4599 VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
4600 VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
4601
4602 dev->hw_features |= NETIF_F_RXHASH;
4603 }
4604
4605 if (vi->has_rss_hash_report)
4606 vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
4607 else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
4608 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4609 vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4610 else
4611 vi->hdr_len = sizeof(struct virtio_net_hdr);
4612
4613 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
4614 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4615 vi->any_header_sg = true;
4616
4617 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4618 vi->has_cvq = true;
4619
4620 if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
4621 mtu = virtio_cread16(vdev,
4622 offsetof(struct virtio_net_config,
4623 mtu));
4624 if (mtu < dev->min_mtu) {
4625 /* Should never trigger: MTU was previously validated
4626 * in virtnet_validate.
4627 */
4628 dev_err(&vdev->dev,
4629 "device MTU appears to have changed it is now %d < %d",
4630 mtu, dev->min_mtu);
4631 err = -EINVAL;
4632 goto free;
4633 }
4634
4635 dev->mtu = mtu;
4636 dev->max_mtu = mtu;
4637 }
4638
4639 virtnet_set_big_packets(vi, mtu);
4640
4641 if (vi->any_header_sg)
4642 dev->needed_headroom = vi->hdr_len;
4643
4644 /* Enable multiqueue by default */
4645 if (num_online_cpus() >= max_queue_pairs)
4646 vi->curr_queue_pairs = max_queue_pairs;
4647 else
4648 vi->curr_queue_pairs = num_online_cpus();
4649 vi->max_queue_pairs = max_queue_pairs;
4650
4651 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
4652 err = init_vqs(vi);
4653 if (err)
4654 goto free;
4655
4656 #ifdef CONFIG_SYSFS
4657 if (vi->mergeable_rx_bufs)
4658 dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4659 #endif
4660 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
4661 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4662
4663 virtnet_init_settings(dev);
4664
4665 if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4666 vi->failover = net_failover_create(vi->dev);
4667 if (IS_ERR(vi->failover)) {
4668 err = PTR_ERR(vi->failover);
4669 goto free_vqs;
4670 }
4671 }
4672
4673 if (vi->has_rss || vi->has_rss_hash_report)
4674 virtnet_init_default_rss(vi);
4675
4676 /* serialize netdev register + virtio_device_ready() with ndo_open() */
4677 rtnl_lock();
4678
4679 err = register_netdevice(dev);
4680 if (err) {
4681 pr_debug("virtio_net: registering device failed\n");
4682 rtnl_unlock();
4683 goto free_failover;
4684 }
4685
4686 virtio_device_ready(vdev);
4687
4688 _virtnet_set_queues(vi, vi->curr_queue_pairs);
4689
4690 /* a random MAC address has been assigned, notify the device.
4691 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
4692 * because many devices work fine without getting MAC explicitly
4693 */
4694 if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
4695 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
4696 struct scatterlist sg;
4697
4698 sg_init_one(&sg, dev->dev_addr, dev->addr_len);
4699 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
4700 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
4701 pr_debug("virtio_net: setting MAC address failed\n");
4702 rtnl_unlock();
4703 err = -EINVAL;
4704 goto free_unregister_netdev;
4705 }
4706 }
4707
4708 rtnl_unlock();
4709
4710 err = virtnet_cpu_notif_add(vi);
4711 if (err) {
4712 pr_debug("virtio_net: registering cpu notifier failed\n");
4713 goto free_unregister_netdev;
4714 }
4715
4716 /* Assume link up if device can't report link status,
4717 otherwise get link status from config. */
4718 netif_carrier_off(dev);
4719 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
4720 schedule_work(&vi->config_work);
4721 } else {
4722 vi->status = VIRTIO_NET_S_LINK_UP;
4723 virtnet_update_settings(vi);
4724 netif_carrier_on(dev);
4725 }
4726
4727 for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
4728 if (virtio_has_feature(vi->vdev, guest_offloads[i]))
4729 set_bit(guest_offloads[i], &vi->guest_offloads);
4730 vi->guest_offloads_capable = vi->guest_offloads;
4731
4732 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4733 dev->name, max_queue_pairs);
4734
4735 return 0;
4736
4737 free_unregister_netdev:
4738 unregister_netdev(dev);
4739 free_failover:
4740 net_failover_destroy(vi->failover);
4741 free_vqs:
4742 virtio_reset_device(vdev);
4743 cancel_delayed_work_sync(&vi->refill);
4744 free_receive_page_frags(vi);
4745 virtnet_del_vqs(vi);
4746 free:
4747 free_netdev(dev);
4748 return err;
4749 }
4750
remove_vq_common(struct virtnet_info * vi)4751 static void remove_vq_common(struct virtnet_info *vi)
4752 {
4753 virtio_reset_device(vi->vdev);
4754
4755 /* Free unused buffers in both send and recv, if any. */
4756 free_unused_bufs(vi);
4757
4758 free_receive_bufs(vi);
4759
4760 free_receive_page_frags(vi);
4761
4762 virtnet_del_vqs(vi);
4763 }
4764
virtnet_remove(struct virtio_device * vdev)4765 static void virtnet_remove(struct virtio_device *vdev)
4766 {
4767 struct virtnet_info *vi = vdev->priv;
4768
4769 virtnet_cpu_notif_remove(vi);
4770
4771 /* Make sure no work handler is accessing the device. */
4772 flush_work(&vi->config_work);
4773
4774 unregister_netdev(vi->dev);
4775
4776 net_failover_destroy(vi->failover);
4777
4778 remove_vq_common(vi);
4779
4780 free_netdev(vi->dev);
4781 }
4782
virtnet_freeze(struct virtio_device * vdev)4783 static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
4784 {
4785 struct virtnet_info *vi = vdev->priv;
4786
4787 virtnet_cpu_notif_remove(vi);
4788 virtnet_freeze_down(vdev);
4789 remove_vq_common(vi);
4790
4791 return 0;
4792 }
4793
virtnet_restore(struct virtio_device * vdev)4794 static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
4795 {
4796 struct virtnet_info *vi = vdev->priv;
4797 int err;
4798
4799 err = virtnet_restore_up(vdev);
4800 if (err)
4801 return err;
4802 virtnet_set_queues(vi, vi->curr_queue_pairs);
4803
4804 err = virtnet_cpu_notif_add(vi);
4805 if (err) {
4806 virtnet_freeze_down(vdev);
4807 remove_vq_common(vi);
4808 return err;
4809 }
4810
4811 return 0;
4812 }
4813
4814 static struct virtio_device_id id_table[] = {
4815 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4816 { 0 },
4817 };
4818
4819 #define VIRTNET_FEATURES \
4820 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4821 VIRTIO_NET_F_MAC, \
4822 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4823 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4824 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4825 VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4826 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4827 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4828 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4829 VIRTIO_NET_F_CTRL_MAC_ADDR, \
4830 VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4831 VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4832 VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
4833 VIRTIO_NET_F_VQ_NOTF_COAL, \
4834 VIRTIO_NET_F_GUEST_HDRLEN
4835
4836 static unsigned int features[] = {
4837 VIRTNET_FEATURES,
4838 };
4839
4840 static unsigned int features_legacy[] = {
4841 VIRTNET_FEATURES,
4842 VIRTIO_NET_F_GSO,
4843 VIRTIO_F_ANY_LAYOUT,
4844 };
4845
4846 static struct virtio_driver virtio_net_driver = {
4847 .feature_table = features,
4848 .feature_table_size = ARRAY_SIZE(features),
4849 .feature_table_legacy = features_legacy,
4850 .feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4851 .driver.name = KBUILD_MODNAME,
4852 .driver.owner = THIS_MODULE,
4853 .id_table = id_table,
4854 .validate = virtnet_validate,
4855 .probe = virtnet_probe,
4856 .remove = virtnet_remove,
4857 .config_changed = virtnet_config_changed,
4858 #ifdef CONFIG_PM_SLEEP
4859 .freeze = virtnet_freeze,
4860 .restore = virtnet_restore,
4861 #endif
4862 };
4863
virtio_net_driver_init(void)4864 static __init int virtio_net_driver_init(void)
4865 {
4866 int ret;
4867
4868 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
4869 virtnet_cpu_online,
4870 virtnet_cpu_down_prep);
4871 if (ret < 0)
4872 goto out;
4873 virtionet_online = ret;
4874 ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
4875 NULL, virtnet_cpu_dead);
4876 if (ret)
4877 goto err_dead;
4878 ret = register_virtio_driver(&virtio_net_driver);
4879 if (ret)
4880 goto err_virtio;
4881 return 0;
4882 err_virtio:
4883 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4884 err_dead:
4885 cpuhp_remove_multi_state(virtionet_online);
4886 out:
4887 return ret;
4888 }
4889 module_init(virtio_net_driver_init);
4890
virtio_net_driver_exit(void)4891 static __exit void virtio_net_driver_exit(void)
4892 {
4893 unregister_virtio_driver(&virtio_net_driver);
4894 cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
4895 cpuhp_remove_multi_state(virtionet_online);
4896 }
4897 module_exit(virtio_net_driver_exit);
4898
4899 MODULE_DEVICE_TABLE(virtio, id_table);
4900 MODULE_DESCRIPTION("Virtio network driver");
4901 MODULE_LICENSE("GPL");
4902