1 /* A network driver using virtio.
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/ethtool.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
25 #include <linux/virtio_net.h>
26 #include <linux/scatterlist.h>
27 #include <linux/if_vlan.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
31 static int napi_weight
= NAPI_POLL_WEIGHT
;
32 module_param(napi_weight
, int, 0444);
34 static bool csum
= true, gso
= true;
35 module_param(csum
, bool, 0444);
36 module_param(gso
, bool, 0444);
38 /* FIXME: MTU in config. */
39 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
40 #define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
41 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
43 #define GOOD_COPY_LEN 128
45 #define VIRTNET_DRIVER_VERSION "1.0.0"
47 struct virtnet_stats
{
48 struct u64_stats_sync tx_syncp
;
49 struct u64_stats_sync rx_syncp
;
57 /* Internal representation of a send virtqueue */
59 /* Virtqueue associated with this send _queue */
62 /* TX: fragments + linear part + virtio header */
63 struct scatterlist sg
[MAX_SKB_FRAGS
+ 2];
65 /* Name of the send queue: output.$index */
69 /* Internal representation of a receive virtqueue */
70 struct receive_queue
{
71 /* Virtqueue associated with this receive_queue */
74 struct napi_struct napi
;
76 /* Number of input buffers, and max we've ever had. */
77 unsigned int num
, max
;
79 /* Chain pages by the private ptr. */
82 /* RX: fragments + linear part + virtio header */
83 struct scatterlist sg
[MAX_SKB_FRAGS
+ 2];
85 /* Name of this receive queue: input.$index */
90 struct virtio_device
*vdev
;
91 struct virtqueue
*cvq
;
92 struct net_device
*dev
;
93 struct send_queue
*sq
;
94 struct receive_queue
*rq
;
97 /* Max # of queue pairs supported by the device */
100 /* # of queue pairs currently used by the driver */
101 u16 curr_queue_pairs
;
103 /* I like... big packets and I cannot lie! */
106 /* Host will merge rx buffers for big packets (shake it! shake it!) */
107 bool mergeable_rx_bufs
;
109 /* Has control virtqueue */
112 /* Host can handle any s/g split between our header and packet data */
115 /* enable config space updates */
118 /* Active statistics */
119 struct virtnet_stats __percpu
*stats
;
121 /* Work struct for refilling if we run low on memory. */
122 struct delayed_work refill
;
124 /* Work struct for config space updates */
125 struct work_struct config_work
;
127 /* Lock for config space updates */
128 struct mutex config_lock
;
130 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
133 struct page_frag alloc_frag
;
135 /* Does the affinity hint is set for virtqueues? */
136 bool affinity_hint_set
;
138 /* CPU hot plug notifier */
139 struct notifier_block nb
;
142 struct skb_vnet_hdr
{
144 struct virtio_net_hdr hdr
;
145 struct virtio_net_hdr_mrg_rxbuf mhdr
;
149 struct padded_vnet_hdr
{
150 struct virtio_net_hdr hdr
;
152 * virtio_net_hdr should be in a separated sg buffer because of a
153 * QEMU bug, and data sg buffer shares same page with this header sg.
154 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
159 /* Converting between virtqueue no. and kernel tx/rx queue no.
160 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
162 static int vq2txq(struct virtqueue
*vq
)
164 return (vq
->index
- 1) / 2;
167 static int txq2vq(int txq
)
172 static int vq2rxq(struct virtqueue
*vq
)
174 return vq
->index
/ 2;
177 static int rxq2vq(int rxq
)
182 static inline struct skb_vnet_hdr
*skb_vnet_hdr(struct sk_buff
*skb
)
184 return (struct skb_vnet_hdr
*)skb
->cb
;
188 * private is used to chain pages for big packets, put the whole
189 * most recent used list in the beginning for reuse
191 static void give_pages(struct receive_queue
*rq
, struct page
*page
)
195 /* Find end of list, sew whole thing into vi->rq.pages. */
196 for (end
= page
; end
->private; end
= (struct page
*)end
->private);
197 end
->private = (unsigned long)rq
->pages
;
201 static struct page
*get_a_page(struct receive_queue
*rq
, gfp_t gfp_mask
)
203 struct page
*p
= rq
->pages
;
206 rq
->pages
= (struct page
*)p
->private;
207 /* clear private here, it is used to chain pages */
210 p
= alloc_page(gfp_mask
);
214 static void skb_xmit_done(struct virtqueue
*vq
)
216 struct virtnet_info
*vi
= vq
->vdev
->priv
;
218 /* Suppress further interrupts. */
219 virtqueue_disable_cb(vq
);
221 /* We were probably waiting for more output buffers. */
222 netif_wake_subqueue(vi
->dev
, vq2txq(vq
));
225 /* Called from bottom half context */
226 static struct sk_buff
*page_to_skb(struct receive_queue
*rq
,
227 struct page
*page
, unsigned int offset
,
228 unsigned int len
, unsigned int truesize
)
230 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
232 struct skb_vnet_hdr
*hdr
;
233 unsigned int copy
, hdr_len
, hdr_padded_len
;
236 p
= page_address(page
) + offset
;
238 /* copy small packet so we can reuse these pages for small data */
239 skb
= netdev_alloc_skb_ip_align(vi
->dev
, GOOD_COPY_LEN
);
243 hdr
= skb_vnet_hdr(skb
);
245 if (vi
->mergeable_rx_bufs
) {
246 hdr_len
= sizeof hdr
->mhdr
;
247 hdr_padded_len
= sizeof hdr
->mhdr
;
249 hdr_len
= sizeof hdr
->hdr
;
250 hdr_padded_len
= sizeof(struct padded_vnet_hdr
);
253 memcpy(hdr
, p
, hdr_len
);
256 offset
+= hdr_padded_len
;
260 if (copy
> skb_tailroom(skb
))
261 copy
= skb_tailroom(skb
);
262 memcpy(skb_put(skb
, copy
), p
, copy
);
267 if (vi
->mergeable_rx_bufs
) {
269 skb_add_rx_frag(skb
, 0, page
, offset
, len
, truesize
);
276 * Verify that we can indeed put this data into a skb.
277 * This is here to handle cases when the device erroneously
278 * tries to receive more than is possible. This is usually
279 * the case of a broken device.
281 if (unlikely(len
> MAX_SKB_FRAGS
* PAGE_SIZE
)) {
282 net_dbg_ratelimited("%s: too much data\n", skb
->dev
->name
);
286 BUG_ON(offset
>= PAGE_SIZE
);
288 unsigned int frag_size
= min((unsigned)PAGE_SIZE
- offset
, len
);
289 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
, offset
,
290 frag_size
, truesize
);
292 page
= (struct page
*)page
->private;
297 give_pages(rq
, page
);
302 static struct sk_buff
*receive_small(void *buf
, unsigned int len
)
304 struct sk_buff
* skb
= buf
;
306 len
-= sizeof(struct virtio_net_hdr
);
312 static struct sk_buff
*receive_big(struct net_device
*dev
,
313 struct receive_queue
*rq
,
317 struct page
*page
= buf
;
318 struct sk_buff
*skb
= page_to_skb(rq
, page
, 0, len
, PAGE_SIZE
);
326 dev
->stats
.rx_dropped
++;
327 give_pages(rq
, page
);
331 static struct sk_buff
*receive_mergeable(struct net_device
*dev
,
332 struct receive_queue
*rq
,
336 struct skb_vnet_hdr
*hdr
= buf
;
337 int num_buf
= hdr
->mhdr
.num_buffers
;
338 struct page
*page
= virt_to_head_page(buf
);
339 int offset
= buf
- page_address(page
);
340 struct sk_buff
*head_skb
= page_to_skb(rq
, page
, offset
, len
,
342 struct sk_buff
*curr_skb
= head_skb
;
344 if (unlikely(!curr_skb
))
350 buf
= virtqueue_get_buf(rq
->vq
, &len
);
351 if (unlikely(!buf
)) {
352 pr_debug("%s: rx error: %d buffers out of %d missing\n",
353 dev
->name
, num_buf
, hdr
->mhdr
.num_buffers
);
354 dev
->stats
.rx_length_errors
++;
357 if (unlikely(len
> MERGE_BUFFER_LEN
)) {
358 pr_debug("%s: rx error: merge buffer too long\n",
360 len
= MERGE_BUFFER_LEN
;
363 page
= virt_to_head_page(buf
);
366 num_skb_frags
= skb_shinfo(curr_skb
)->nr_frags
;
367 if (unlikely(num_skb_frags
== MAX_SKB_FRAGS
)) {
368 struct sk_buff
*nskb
= alloc_skb(0, GFP_ATOMIC
);
372 if (curr_skb
== head_skb
)
373 skb_shinfo(curr_skb
)->frag_list
= nskb
;
375 curr_skb
->next
= nskb
;
377 head_skb
->truesize
+= nskb
->truesize
;
380 if (curr_skb
!= head_skb
) {
381 head_skb
->data_len
+= len
;
382 head_skb
->len
+= len
;
383 head_skb
->truesize
+= MERGE_BUFFER_LEN
;
385 offset
= buf
- page_address(page
);
386 if (skb_can_coalesce(curr_skb
, num_skb_frags
, page
, offset
)) {
388 skb_coalesce_rx_frag(curr_skb
, num_skb_frags
- 1,
389 len
, MERGE_BUFFER_LEN
);
391 skb_add_rx_frag(curr_skb
, num_skb_frags
, page
,
392 offset
, len
, MERGE_BUFFER_LEN
);
401 buf
= virtqueue_get_buf(rq
->vq
, &len
);
402 if (unlikely(!buf
)) {
403 pr_debug("%s: rx error: %d buffers missing\n",
405 dev
->stats
.rx_length_errors
++;
408 page
= virt_to_head_page(buf
);
413 dev
->stats
.rx_dropped
++;
414 dev_kfree_skb(head_skb
);
418 static void receive_buf(struct receive_queue
*rq
, void *buf
, unsigned int len
)
420 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
421 struct net_device
*dev
= vi
->dev
;
422 struct virtnet_stats
*stats
= this_cpu_ptr(vi
->stats
);
424 struct skb_vnet_hdr
*hdr
;
426 if (unlikely(len
< sizeof(struct virtio_net_hdr
) + ETH_HLEN
)) {
427 pr_debug("%s: short packet %i\n", dev
->name
, len
);
428 dev
->stats
.rx_length_errors
++;
429 if (vi
->mergeable_rx_bufs
)
430 put_page(virt_to_head_page(buf
));
431 else if (vi
->big_packets
)
438 if (vi
->mergeable_rx_bufs
)
439 skb
= receive_mergeable(dev
, rq
, buf
, len
);
440 else if (vi
->big_packets
)
441 skb
= receive_big(dev
, rq
, buf
, len
);
443 skb
= receive_small(buf
, len
);
448 hdr
= skb_vnet_hdr(skb
);
450 u64_stats_update_begin(&stats
->rx_syncp
);
451 stats
->rx_bytes
+= skb
->len
;
453 u64_stats_update_end(&stats
->rx_syncp
);
455 if (hdr
->hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
456 pr_debug("Needs csum!\n");
457 if (!skb_partial_csum_set(skb
,
459 hdr
->hdr
.csum_offset
))
461 } else if (hdr
->hdr
.flags
& VIRTIO_NET_HDR_F_DATA_VALID
) {
462 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
465 skb
->protocol
= eth_type_trans(skb
, dev
);
466 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
467 ntohs(skb
->protocol
), skb
->len
, skb
->pkt_type
);
469 if (hdr
->hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
471 switch (hdr
->hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
472 case VIRTIO_NET_HDR_GSO_TCPV4
:
473 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
475 case VIRTIO_NET_HDR_GSO_UDP
:
476 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
478 case VIRTIO_NET_HDR_GSO_TCPV6
:
479 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
482 net_warn_ratelimited("%s: bad gso type %u.\n",
483 dev
->name
, hdr
->hdr
.gso_type
);
487 if (hdr
->hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
488 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
490 skb_shinfo(skb
)->gso_size
= hdr
->hdr
.gso_size
;
491 if (skb_shinfo(skb
)->gso_size
== 0) {
492 net_warn_ratelimited("%s: zero gso size.\n", dev
->name
);
496 /* Header must be checked, and gso_segs computed. */
497 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
498 skb_shinfo(skb
)->gso_segs
= 0;
501 netif_receive_skb(skb
);
505 dev
->stats
.rx_frame_errors
++;
509 static int add_recvbuf_small(struct receive_queue
*rq
, gfp_t gfp
)
511 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
513 struct skb_vnet_hdr
*hdr
;
516 skb
= __netdev_alloc_skb_ip_align(vi
->dev
, GOOD_PACKET_LEN
, gfp
);
520 skb_put(skb
, GOOD_PACKET_LEN
);
522 hdr
= skb_vnet_hdr(skb
);
523 sg_set_buf(rq
->sg
, &hdr
->hdr
, sizeof hdr
->hdr
);
525 skb_to_sgvec(skb
, rq
->sg
+ 1, 0, skb
->len
);
527 err
= virtqueue_add_inbuf(rq
->vq
, rq
->sg
, 2, skb
, gfp
);
534 static int add_recvbuf_big(struct receive_queue
*rq
, gfp_t gfp
)
536 struct page
*first
, *list
= NULL
;
540 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
541 for (i
= MAX_SKB_FRAGS
+ 1; i
> 1; --i
) {
542 first
= get_a_page(rq
, gfp
);
545 give_pages(rq
, list
);
548 sg_set_buf(&rq
->sg
[i
], page_address(first
), PAGE_SIZE
);
550 /* chain new page in list head to match sg */
551 first
->private = (unsigned long)list
;
555 first
= get_a_page(rq
, gfp
);
557 give_pages(rq
, list
);
560 p
= page_address(first
);
562 /* rq->sg[0], rq->sg[1] share the same page */
563 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
564 sg_set_buf(&rq
->sg
[0], p
, sizeof(struct virtio_net_hdr
));
566 /* rq->sg[1] for data packet, from offset */
567 offset
= sizeof(struct padded_vnet_hdr
);
568 sg_set_buf(&rq
->sg
[1], p
+ offset
, PAGE_SIZE
- offset
);
570 /* chain first in list head */
571 first
->private = (unsigned long)list
;
572 err
= virtqueue_add_inbuf(rq
->vq
, rq
->sg
, MAX_SKB_FRAGS
+ 2,
575 give_pages(rq
, first
);
580 static int add_recvbuf_mergeable(struct receive_queue
*rq
, gfp_t gfp
)
582 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
586 if (gfp
& __GFP_WAIT
) {
587 if (skb_page_frag_refill(MERGE_BUFFER_LEN
, &vi
->alloc_frag
,
589 buf
= (char *)page_address(vi
->alloc_frag
.page
) +
590 vi
->alloc_frag
.offset
;
591 get_page(vi
->alloc_frag
.page
);
592 vi
->alloc_frag
.offset
+= MERGE_BUFFER_LEN
;
595 buf
= netdev_alloc_frag(MERGE_BUFFER_LEN
);
600 sg_init_one(rq
->sg
, buf
, MERGE_BUFFER_LEN
);
601 err
= virtqueue_add_inbuf(rq
->vq
, rq
->sg
, 1, buf
, gfp
);
603 put_page(virt_to_head_page(buf
));
609 * Returns false if we couldn't fill entirely (OOM).
611 * Normally run in the receive path, but can also be run from ndo_open
612 * before we're receiving packets, or from refill_work which is
613 * careful to disable receiving (using napi_disable).
615 static bool try_fill_recv(struct receive_queue
*rq
, gfp_t gfp
)
617 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
622 if (vi
->mergeable_rx_bufs
)
623 err
= add_recvbuf_mergeable(rq
, gfp
);
624 else if (vi
->big_packets
)
625 err
= add_recvbuf_big(rq
, gfp
);
627 err
= add_recvbuf_small(rq
, gfp
);
629 oom
= err
== -ENOMEM
;
633 } while (rq
->vq
->num_free
);
634 if (unlikely(rq
->num
> rq
->max
))
636 if (unlikely(!virtqueue_kick(rq
->vq
)))
641 static void skb_recv_done(struct virtqueue
*rvq
)
643 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
644 struct receive_queue
*rq
= &vi
->rq
[vq2rxq(rvq
)];
646 /* Schedule NAPI, Suppress further interrupts if successful. */
647 if (napi_schedule_prep(&rq
->napi
)) {
648 virtqueue_disable_cb(rvq
);
649 __napi_schedule(&rq
->napi
);
653 static void virtnet_napi_enable(struct receive_queue
*rq
)
655 napi_enable(&rq
->napi
);
657 /* If all buffers were filled by other side before we napi_enabled, we
658 * won't get another interrupt, so process any outstanding packets
659 * now. virtnet_poll wants re-enable the queue, so we disable here.
660 * We synchronize against interrupts via NAPI_STATE_SCHED */
661 if (napi_schedule_prep(&rq
->napi
)) {
662 virtqueue_disable_cb(rq
->vq
);
664 __napi_schedule(&rq
->napi
);
669 static void refill_work(struct work_struct
*work
)
671 struct virtnet_info
*vi
=
672 container_of(work
, struct virtnet_info
, refill
.work
);
676 for (i
= 0; i
< vi
->curr_queue_pairs
; i
++) {
677 struct receive_queue
*rq
= &vi
->rq
[i
];
679 napi_disable(&rq
->napi
);
680 still_empty
= !try_fill_recv(rq
, GFP_KERNEL
);
681 virtnet_napi_enable(rq
);
683 /* In theory, this can happen: if we don't get any buffers in
684 * we will *never* try to fill again.
687 schedule_delayed_work(&vi
->refill
, HZ
/2);
691 static int virtnet_poll(struct napi_struct
*napi
, int budget
)
693 struct receive_queue
*rq
=
694 container_of(napi
, struct receive_queue
, napi
);
695 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
697 unsigned int r
, len
, received
= 0;
700 while (received
< budget
&&
701 (buf
= virtqueue_get_buf(rq
->vq
, &len
)) != NULL
) {
702 receive_buf(rq
, buf
, len
);
707 if (rq
->num
< rq
->max
/ 2) {
708 if (!try_fill_recv(rq
, GFP_ATOMIC
))
709 schedule_delayed_work(&vi
->refill
, 0);
712 /* Out of packets? */
713 if (received
< budget
) {
714 r
= virtqueue_enable_cb_prepare(rq
->vq
);
716 if (unlikely(virtqueue_poll(rq
->vq
, r
)) &&
717 napi_schedule_prep(napi
)) {
718 virtqueue_disable_cb(rq
->vq
);
719 __napi_schedule(napi
);
727 static int virtnet_open(struct net_device
*dev
)
729 struct virtnet_info
*vi
= netdev_priv(dev
);
732 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
733 if (i
< vi
->curr_queue_pairs
)
734 /* Make sure we have some buffers: if oom use wq. */
735 if (!try_fill_recv(&vi
->rq
[i
], GFP_KERNEL
))
736 schedule_delayed_work(&vi
->refill
, 0);
737 virtnet_napi_enable(&vi
->rq
[i
]);
743 static void free_old_xmit_skbs(struct send_queue
*sq
)
747 struct virtnet_info
*vi
= sq
->vq
->vdev
->priv
;
748 struct virtnet_stats
*stats
= this_cpu_ptr(vi
->stats
);
750 while ((skb
= virtqueue_get_buf(sq
->vq
, &len
)) != NULL
) {
751 pr_debug("Sent skb %p\n", skb
);
753 u64_stats_update_begin(&stats
->tx_syncp
);
754 stats
->tx_bytes
+= skb
->len
;
756 u64_stats_update_end(&stats
->tx_syncp
);
758 dev_kfree_skb_any(skb
);
762 static int xmit_skb(struct send_queue
*sq
, struct sk_buff
*skb
)
764 struct skb_vnet_hdr
*hdr
;
765 const unsigned char *dest
= ((struct ethhdr
*)skb
->data
)->h_dest
;
766 struct virtnet_info
*vi
= sq
->vq
->vdev
->priv
;
771 pr_debug("%s: xmit %p %pM\n", vi
->dev
->name
, skb
, dest
);
772 if (vi
->mergeable_rx_bufs
)
773 hdr_len
= sizeof hdr
->mhdr
;
775 hdr_len
= sizeof hdr
->hdr
;
777 can_push
= vi
->any_header_sg
&&
778 !((unsigned long)skb
->data
& (__alignof__(*hdr
) - 1)) &&
779 !skb_header_cloned(skb
) && skb_headroom(skb
) >= hdr_len
;
780 /* Even if we can, don't push here yet as this would skew
781 * csum_start offset below. */
783 hdr
= (struct skb_vnet_hdr
*)(skb
->data
- hdr_len
);
785 hdr
= skb_vnet_hdr(skb
);
787 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
788 hdr
->hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
789 hdr
->hdr
.csum_start
= skb_checksum_start_offset(skb
);
790 hdr
->hdr
.csum_offset
= skb
->csum_offset
;
793 hdr
->hdr
.csum_offset
= hdr
->hdr
.csum_start
= 0;
796 if (skb_is_gso(skb
)) {
797 hdr
->hdr
.hdr_len
= skb_headlen(skb
);
798 hdr
->hdr
.gso_size
= skb_shinfo(skb
)->gso_size
;
799 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
800 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
801 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
802 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
803 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
804 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
807 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCP_ECN
)
808 hdr
->hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
810 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
811 hdr
->hdr
.gso_size
= hdr
->hdr
.hdr_len
= 0;
814 if (vi
->mergeable_rx_bufs
)
815 hdr
->mhdr
.num_buffers
= 0;
818 __skb_push(skb
, hdr_len
);
819 num_sg
= skb_to_sgvec(skb
, sq
->sg
, 0, skb
->len
);
820 /* Pull header back to avoid skew in tx bytes calculations. */
821 __skb_pull(skb
, hdr_len
);
823 sg_set_buf(sq
->sg
, hdr
, hdr_len
);
824 num_sg
= skb_to_sgvec(skb
, sq
->sg
+ 1, 0, skb
->len
) + 1;
826 return virtqueue_add_outbuf(sq
->vq
, sq
->sg
, num_sg
, skb
, GFP_ATOMIC
);
829 static netdev_tx_t
start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
831 struct virtnet_info
*vi
= netdev_priv(dev
);
832 int qnum
= skb_get_queue_mapping(skb
);
833 struct send_queue
*sq
= &vi
->sq
[qnum
];
836 /* Free up any pending old buffers before queueing new ones. */
837 free_old_xmit_skbs(sq
);
839 /* Try to transmit */
840 err
= xmit_skb(sq
, skb
);
842 /* This should not happen! */
843 if (unlikely(err
) || unlikely(!virtqueue_kick(sq
->vq
))) {
844 dev
->stats
.tx_fifo_errors
++;
847 "Unexpected TXQ (%d) queue failure: %d\n", qnum
, err
);
848 dev
->stats
.tx_dropped
++;
853 /* Don't wait up for transmitted skbs to be freed. */
857 /* Apparently nice girls don't return TX_BUSY; stop the queue
858 * before it gets out of hand. Naturally, this wastes entries. */
859 if (sq
->vq
->num_free
< 2+MAX_SKB_FRAGS
) {
860 netif_stop_subqueue(dev
, qnum
);
861 if (unlikely(!virtqueue_enable_cb_delayed(sq
->vq
))) {
862 /* More just got used, free them then recheck. */
863 free_old_xmit_skbs(sq
);
864 if (sq
->vq
->num_free
>= 2+MAX_SKB_FRAGS
) {
865 netif_start_subqueue(dev
, qnum
);
866 virtqueue_disable_cb(sq
->vq
);
875 * Send command via the control virtqueue and check status. Commands
876 * supported by the hypervisor, as indicated by feature bits, should
877 * never fail unless improperly formated.
879 static bool virtnet_send_command(struct virtnet_info
*vi
, u8
class, u8 cmd
,
880 struct scatterlist
*out
,
881 struct scatterlist
*in
)
883 struct scatterlist
*sgs
[4], hdr
, stat
;
884 struct virtio_net_ctrl_hdr ctrl
;
885 virtio_net_ctrl_ack status
= ~0;
886 unsigned out_num
= 0, in_num
= 0, tmp
;
888 /* Caller should know better */
889 BUG_ON(!virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VQ
));
894 sg_init_one(&hdr
, &ctrl
, sizeof(ctrl
));
895 sgs
[out_num
++] = &hdr
;
898 sgs
[out_num
++] = out
;
900 sgs
[out_num
+ in_num
++] = in
;
902 /* Add return status. */
903 sg_init_one(&stat
, &status
, sizeof(status
));
904 sgs
[out_num
+ in_num
++] = &stat
;
906 BUG_ON(out_num
+ in_num
> ARRAY_SIZE(sgs
));
907 BUG_ON(virtqueue_add_sgs(vi
->cvq
, sgs
, out_num
, in_num
, vi
, GFP_ATOMIC
)
910 if (unlikely(!virtqueue_kick(vi
->cvq
)))
911 return status
== VIRTIO_NET_OK
;
913 /* Spin for a response, the kick causes an ioport write, trapping
914 * into the hypervisor, so the request should be handled immediately.
916 while (!virtqueue_get_buf(vi
->cvq
, &tmp
) &&
917 !virtqueue_is_broken(vi
->cvq
))
920 return status
== VIRTIO_NET_OK
;
923 static int virtnet_set_mac_address(struct net_device
*dev
, void *p
)
925 struct virtnet_info
*vi
= netdev_priv(dev
);
926 struct virtio_device
*vdev
= vi
->vdev
;
928 struct sockaddr
*addr
= p
;
929 struct scatterlist sg
;
931 ret
= eth_prepare_mac_addr_change(dev
, p
);
935 if (virtio_has_feature(vdev
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
936 sg_init_one(&sg
, addr
->sa_data
, dev
->addr_len
);
937 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_MAC
,
938 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
941 "Failed to set mac address by vq command.\n");
944 } else if (virtio_has_feature(vdev
, VIRTIO_NET_F_MAC
)) {
947 /* Naturally, this has an atomicity problem. */
948 for (i
= 0; i
< dev
->addr_len
; i
++)
950 offsetof(struct virtio_net_config
, mac
) +
951 i
, addr
->sa_data
[i
]);
954 eth_commit_mac_addr_change(dev
, p
);
959 static struct rtnl_link_stats64
*virtnet_stats(struct net_device
*dev
,
960 struct rtnl_link_stats64
*tot
)
962 struct virtnet_info
*vi
= netdev_priv(dev
);
966 for_each_possible_cpu(cpu
) {
967 struct virtnet_stats
*stats
= per_cpu_ptr(vi
->stats
, cpu
);
968 u64 tpackets
, tbytes
, rpackets
, rbytes
;
971 start
= u64_stats_fetch_begin_bh(&stats
->tx_syncp
);
972 tpackets
= stats
->tx_packets
;
973 tbytes
= stats
->tx_bytes
;
974 } while (u64_stats_fetch_retry_bh(&stats
->tx_syncp
, start
));
977 start
= u64_stats_fetch_begin_bh(&stats
->rx_syncp
);
978 rpackets
= stats
->rx_packets
;
979 rbytes
= stats
->rx_bytes
;
980 } while (u64_stats_fetch_retry_bh(&stats
->rx_syncp
, start
));
982 tot
->rx_packets
+= rpackets
;
983 tot
->tx_packets
+= tpackets
;
984 tot
->rx_bytes
+= rbytes
;
985 tot
->tx_bytes
+= tbytes
;
988 tot
->tx_dropped
= dev
->stats
.tx_dropped
;
989 tot
->tx_fifo_errors
= dev
->stats
.tx_fifo_errors
;
990 tot
->rx_dropped
= dev
->stats
.rx_dropped
;
991 tot
->rx_length_errors
= dev
->stats
.rx_length_errors
;
992 tot
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
997 #ifdef CONFIG_NET_POLL_CONTROLLER
998 static void virtnet_netpoll(struct net_device
*dev
)
1000 struct virtnet_info
*vi
= netdev_priv(dev
);
1003 for (i
= 0; i
< vi
->curr_queue_pairs
; i
++)
1004 napi_schedule(&vi
->rq
[i
].napi
);
1008 static void virtnet_ack_link_announce(struct virtnet_info
*vi
)
1011 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_ANNOUNCE
,
1012 VIRTIO_NET_CTRL_ANNOUNCE_ACK
, NULL
, NULL
))
1013 dev_warn(&vi
->dev
->dev
, "Failed to ack link announce.\n");
1017 static int virtnet_set_queues(struct virtnet_info
*vi
, u16 queue_pairs
)
1019 struct scatterlist sg
;
1020 struct virtio_net_ctrl_mq s
;
1021 struct net_device
*dev
= vi
->dev
;
1023 if (!vi
->has_cvq
|| !virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_MQ
))
1026 s
.virtqueue_pairs
= queue_pairs
;
1027 sg_init_one(&sg
, &s
, sizeof(s
));
1029 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_MQ
,
1030 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
, &sg
, NULL
)) {
1031 dev_warn(&dev
->dev
, "Fail to set num of queue pairs to %d\n",
1035 vi
->curr_queue_pairs
= queue_pairs
;
1036 /* virtnet_open() will refill when device is going to up. */
1037 if (dev
->flags
& IFF_UP
)
1038 schedule_delayed_work(&vi
->refill
, 0);
1044 static int virtnet_close(struct net_device
*dev
)
1046 struct virtnet_info
*vi
= netdev_priv(dev
);
1049 /* Make sure refill_work doesn't re-enable napi! */
1050 cancel_delayed_work_sync(&vi
->refill
);
1052 for (i
= 0; i
< vi
->max_queue_pairs
; i
++)
1053 napi_disable(&vi
->rq
[i
].napi
);
1058 static void virtnet_set_rx_mode(struct net_device
*dev
)
1060 struct virtnet_info
*vi
= netdev_priv(dev
);
1061 struct scatterlist sg
[2];
1062 u8 promisc
, allmulti
;
1063 struct virtio_net_ctrl_mac
*mac_data
;
1064 struct netdev_hw_addr
*ha
;
1070 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
1071 if (!virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_RX
))
1074 promisc
= ((dev
->flags
& IFF_PROMISC
) != 0);
1075 allmulti
= ((dev
->flags
& IFF_ALLMULTI
) != 0);
1077 sg_init_one(sg
, &promisc
, sizeof(promisc
));
1079 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_RX
,
1080 VIRTIO_NET_CTRL_RX_PROMISC
,
1082 dev_warn(&dev
->dev
, "Failed to %sable promisc mode.\n",
1083 promisc
? "en" : "dis");
1085 sg_init_one(sg
, &allmulti
, sizeof(allmulti
));
1087 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_RX
,
1088 VIRTIO_NET_CTRL_RX_ALLMULTI
,
1090 dev_warn(&dev
->dev
, "Failed to %sable allmulti mode.\n",
1091 allmulti
? "en" : "dis");
1093 uc_count
= netdev_uc_count(dev
);
1094 mc_count
= netdev_mc_count(dev
);
1095 /* MAC filter - use one buffer for both lists */
1096 buf
= kzalloc(((uc_count
+ mc_count
) * ETH_ALEN
) +
1097 (2 * sizeof(mac_data
->entries
)), GFP_ATOMIC
);
1102 sg_init_table(sg
, 2);
1104 /* Store the unicast list and count in the front of the buffer */
1105 mac_data
->entries
= uc_count
;
1107 netdev_for_each_uc_addr(ha
, dev
)
1108 memcpy(&mac_data
->macs
[i
++][0], ha
->addr
, ETH_ALEN
);
1110 sg_set_buf(&sg
[0], mac_data
,
1111 sizeof(mac_data
->entries
) + (uc_count
* ETH_ALEN
));
1113 /* multicast list and count fill the end */
1114 mac_data
= (void *)&mac_data
->macs
[uc_count
][0];
1116 mac_data
->entries
= mc_count
;
1118 netdev_for_each_mc_addr(ha
, dev
)
1119 memcpy(&mac_data
->macs
[i
++][0], ha
->addr
, ETH_ALEN
);
1121 sg_set_buf(&sg
[1], mac_data
,
1122 sizeof(mac_data
->entries
) + (mc_count
* ETH_ALEN
));
1124 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_MAC
,
1125 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
1127 dev_warn(&dev
->dev
, "Failed to set MAC filter table.\n");
1132 static int virtnet_vlan_rx_add_vid(struct net_device
*dev
,
1133 __be16 proto
, u16 vid
)
1135 struct virtnet_info
*vi
= netdev_priv(dev
);
1136 struct scatterlist sg
;
1138 sg_init_one(&sg
, &vid
, sizeof(vid
));
1140 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_VLAN
,
1141 VIRTIO_NET_CTRL_VLAN_ADD
, &sg
, NULL
))
1142 dev_warn(&dev
->dev
, "Failed to add VLAN ID %d.\n", vid
);
1146 static int virtnet_vlan_rx_kill_vid(struct net_device
*dev
,
1147 __be16 proto
, u16 vid
)
1149 struct virtnet_info
*vi
= netdev_priv(dev
);
1150 struct scatterlist sg
;
1152 sg_init_one(&sg
, &vid
, sizeof(vid
));
1154 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_VLAN
,
1155 VIRTIO_NET_CTRL_VLAN_DEL
, &sg
, NULL
))
1156 dev_warn(&dev
->dev
, "Failed to kill VLAN ID %d.\n", vid
);
1160 static void virtnet_clean_affinity(struct virtnet_info
*vi
, long hcpu
)
1164 if (vi
->affinity_hint_set
) {
1165 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1166 virtqueue_set_affinity(vi
->rq
[i
].vq
, -1);
1167 virtqueue_set_affinity(vi
->sq
[i
].vq
, -1);
1170 vi
->affinity_hint_set
= false;
1174 static void virtnet_set_affinity(struct virtnet_info
*vi
)
1179 /* In multiqueue mode, when the number of cpu is equal to the number of
1180 * queue pairs, we let the queue pairs to be private to one cpu by
1181 * setting the affinity hint to eliminate the contention.
1183 if (vi
->curr_queue_pairs
== 1 ||
1184 vi
->max_queue_pairs
!= num_online_cpus()) {
1185 virtnet_clean_affinity(vi
, -1);
1190 for_each_online_cpu(cpu
) {
1191 virtqueue_set_affinity(vi
->rq
[i
].vq
, cpu
);
1192 virtqueue_set_affinity(vi
->sq
[i
].vq
, cpu
);
1193 netif_set_xps_queue(vi
->dev
, cpumask_of(cpu
), i
);
1197 vi
->affinity_hint_set
= true;
1200 static int virtnet_cpu_callback(struct notifier_block
*nfb
,
1201 unsigned long action
, void *hcpu
)
1203 struct virtnet_info
*vi
= container_of(nfb
, struct virtnet_info
, nb
);
1205 switch(action
& ~CPU_TASKS_FROZEN
) {
1207 case CPU_DOWN_FAILED
:
1209 virtnet_set_affinity(vi
);
1211 case CPU_DOWN_PREPARE
:
1212 virtnet_clean_affinity(vi
, (long)hcpu
);
1221 static void virtnet_get_ringparam(struct net_device
*dev
,
1222 struct ethtool_ringparam
*ring
)
1224 struct virtnet_info
*vi
= netdev_priv(dev
);
1226 ring
->rx_max_pending
= virtqueue_get_vring_size(vi
->rq
[0].vq
);
1227 ring
->tx_max_pending
= virtqueue_get_vring_size(vi
->sq
[0].vq
);
1228 ring
->rx_pending
= ring
->rx_max_pending
;
1229 ring
->tx_pending
= ring
->tx_max_pending
;
1233 static void virtnet_get_drvinfo(struct net_device
*dev
,
1234 struct ethtool_drvinfo
*info
)
1236 struct virtnet_info
*vi
= netdev_priv(dev
);
1237 struct virtio_device
*vdev
= vi
->vdev
;
1239 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
1240 strlcpy(info
->version
, VIRTNET_DRIVER_VERSION
, sizeof(info
->version
));
1241 strlcpy(info
->bus_info
, virtio_bus_name(vdev
), sizeof(info
->bus_info
));
1245 /* TODO: Eliminate OOO packets during switching */
1246 static int virtnet_set_channels(struct net_device
*dev
,
1247 struct ethtool_channels
*channels
)
1249 struct virtnet_info
*vi
= netdev_priv(dev
);
1250 u16 queue_pairs
= channels
->combined_count
;
1253 /* We don't support separate rx/tx channels.
1254 * We don't allow setting 'other' channels.
1256 if (channels
->rx_count
|| channels
->tx_count
|| channels
->other_count
)
1259 if (queue_pairs
> vi
->max_queue_pairs
)
1263 err
= virtnet_set_queues(vi
, queue_pairs
);
1265 netif_set_real_num_tx_queues(dev
, queue_pairs
);
1266 netif_set_real_num_rx_queues(dev
, queue_pairs
);
1268 virtnet_set_affinity(vi
);
1275 static void virtnet_get_channels(struct net_device
*dev
,
1276 struct ethtool_channels
*channels
)
1278 struct virtnet_info
*vi
= netdev_priv(dev
);
1280 channels
->combined_count
= vi
->curr_queue_pairs
;
1281 channels
->max_combined
= vi
->max_queue_pairs
;
1282 channels
->max_other
= 0;
1283 channels
->rx_count
= 0;
1284 channels
->tx_count
= 0;
1285 channels
->other_count
= 0;
1288 static const struct ethtool_ops virtnet_ethtool_ops
= {
1289 .get_drvinfo
= virtnet_get_drvinfo
,
1290 .get_link
= ethtool_op_get_link
,
1291 .get_ringparam
= virtnet_get_ringparam
,
1292 .set_channels
= virtnet_set_channels
,
1293 .get_channels
= virtnet_get_channels
,
1297 #define MAX_MTU 65535
1299 static int virtnet_change_mtu(struct net_device
*dev
, int new_mtu
)
1301 if (new_mtu
< MIN_MTU
|| new_mtu
> MAX_MTU
)
1307 static const struct net_device_ops virtnet_netdev
= {
1308 .ndo_open
= virtnet_open
,
1309 .ndo_stop
= virtnet_close
,
1310 .ndo_start_xmit
= start_xmit
,
1311 .ndo_validate_addr
= eth_validate_addr
,
1312 .ndo_set_mac_address
= virtnet_set_mac_address
,
1313 .ndo_set_rx_mode
= virtnet_set_rx_mode
,
1314 .ndo_change_mtu
= virtnet_change_mtu
,
1315 .ndo_get_stats64
= virtnet_stats
,
1316 .ndo_vlan_rx_add_vid
= virtnet_vlan_rx_add_vid
,
1317 .ndo_vlan_rx_kill_vid
= virtnet_vlan_rx_kill_vid
,
1318 #ifdef CONFIG_NET_POLL_CONTROLLER
1319 .ndo_poll_controller
= virtnet_netpoll
,
1323 static void virtnet_config_changed_work(struct work_struct
*work
)
1325 struct virtnet_info
*vi
=
1326 container_of(work
, struct virtnet_info
, config_work
);
1329 mutex_lock(&vi
->config_lock
);
1330 if (!vi
->config_enable
)
1333 if (virtio_cread_feature(vi
->vdev
, VIRTIO_NET_F_STATUS
,
1334 struct virtio_net_config
, status
, &v
) < 0)
1337 if (v
& VIRTIO_NET_S_ANNOUNCE
) {
1338 netdev_notify_peers(vi
->dev
);
1339 virtnet_ack_link_announce(vi
);
1342 /* Ignore unknown (future) status bits */
1343 v
&= VIRTIO_NET_S_LINK_UP
;
1345 if (vi
->status
== v
)
1350 if (vi
->status
& VIRTIO_NET_S_LINK_UP
) {
1351 netif_carrier_on(vi
->dev
);
1352 netif_tx_wake_all_queues(vi
->dev
);
1354 netif_carrier_off(vi
->dev
);
1355 netif_tx_stop_all_queues(vi
->dev
);
1358 mutex_unlock(&vi
->config_lock
);
1361 static void virtnet_config_changed(struct virtio_device
*vdev
)
1363 struct virtnet_info
*vi
= vdev
->priv
;
1365 schedule_work(&vi
->config_work
);
1368 static void virtnet_free_queues(struct virtnet_info
*vi
)
1372 for (i
= 0; i
< vi
->max_queue_pairs
; i
++)
1373 netif_napi_del(&vi
->rq
[i
].napi
);
1379 static void free_receive_bufs(struct virtnet_info
*vi
)
1383 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1384 while (vi
->rq
[i
].pages
)
1385 __free_pages(get_a_page(&vi
->rq
[i
], GFP_KERNEL
), 0);
1389 static void free_unused_bufs(struct virtnet_info
*vi
)
1394 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1395 struct virtqueue
*vq
= vi
->sq
[i
].vq
;
1396 while ((buf
= virtqueue_detach_unused_buf(vq
)) != NULL
)
1400 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1401 struct virtqueue
*vq
= vi
->rq
[i
].vq
;
1403 while ((buf
= virtqueue_detach_unused_buf(vq
)) != NULL
) {
1404 if (vi
->mergeable_rx_bufs
)
1405 put_page(virt_to_head_page(buf
));
1406 else if (vi
->big_packets
)
1407 give_pages(&vi
->rq
[i
], buf
);
1412 BUG_ON(vi
->rq
[i
].num
!= 0);
1416 static void virtnet_del_vqs(struct virtnet_info
*vi
)
1418 struct virtio_device
*vdev
= vi
->vdev
;
1420 virtnet_clean_affinity(vi
, -1);
1422 vdev
->config
->del_vqs(vdev
);
1424 virtnet_free_queues(vi
);
1427 static int virtnet_find_vqs(struct virtnet_info
*vi
)
1429 vq_callback_t
**callbacks
;
1430 struct virtqueue
**vqs
;
1435 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1436 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1437 * possible control vq.
1439 total_vqs
= vi
->max_queue_pairs
* 2 +
1440 virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VQ
);
1442 /* Allocate space for find_vqs parameters */
1443 vqs
= kzalloc(total_vqs
* sizeof(*vqs
), GFP_KERNEL
);
1446 callbacks
= kmalloc(total_vqs
* sizeof(*callbacks
), GFP_KERNEL
);
1449 names
= kmalloc(total_vqs
* sizeof(*names
), GFP_KERNEL
);
1453 /* Parameters for control virtqueue, if any */
1455 callbacks
[total_vqs
- 1] = NULL
;
1456 names
[total_vqs
- 1] = "control";
1459 /* Allocate/initialize parameters for send/receive virtqueues */
1460 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1461 callbacks
[rxq2vq(i
)] = skb_recv_done
;
1462 callbacks
[txq2vq(i
)] = skb_xmit_done
;
1463 sprintf(vi
->rq
[i
].name
, "input.%d", i
);
1464 sprintf(vi
->sq
[i
].name
, "output.%d", i
);
1465 names
[rxq2vq(i
)] = vi
->rq
[i
].name
;
1466 names
[txq2vq(i
)] = vi
->sq
[i
].name
;
1469 ret
= vi
->vdev
->config
->find_vqs(vi
->vdev
, total_vqs
, vqs
, callbacks
,
1475 vi
->cvq
= vqs
[total_vqs
- 1];
1476 if (virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VLAN
))
1477 vi
->dev
->features
|= NETIF_F_HW_VLAN_CTAG_FILTER
;
1480 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1481 vi
->rq
[i
].vq
= vqs
[rxq2vq(i
)];
1482 vi
->sq
[i
].vq
= vqs
[txq2vq(i
)];
1501 static int virtnet_alloc_queues(struct virtnet_info
*vi
)
1505 vi
->sq
= kzalloc(sizeof(*vi
->sq
) * vi
->max_queue_pairs
, GFP_KERNEL
);
1508 vi
->rq
= kzalloc(sizeof(*vi
->rq
) * vi
->max_queue_pairs
, GFP_KERNEL
);
1512 INIT_DELAYED_WORK(&vi
->refill
, refill_work
);
1513 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1514 vi
->rq
[i
].pages
= NULL
;
1515 netif_napi_add(vi
->dev
, &vi
->rq
[i
].napi
, virtnet_poll
,
1518 sg_init_table(vi
->rq
[i
].sg
, ARRAY_SIZE(vi
->rq
[i
].sg
));
1519 sg_init_table(vi
->sq
[i
].sg
, ARRAY_SIZE(vi
->sq
[i
].sg
));
1530 static int init_vqs(struct virtnet_info
*vi
)
1534 /* Allocate send & receive queues */
1535 ret
= virtnet_alloc_queues(vi
);
1539 ret
= virtnet_find_vqs(vi
);
1544 virtnet_set_affinity(vi
);
1550 virtnet_free_queues(vi
);
1555 static int virtnet_probe(struct virtio_device
*vdev
)
1558 struct net_device
*dev
;
1559 struct virtnet_info
*vi
;
1560 u16 max_queue_pairs
;
1562 /* Find if host supports multiqueue virtio_net device */
1563 err
= virtio_cread_feature(vdev
, VIRTIO_NET_F_MQ
,
1564 struct virtio_net_config
,
1565 max_virtqueue_pairs
, &max_queue_pairs
);
1567 /* We need at least 2 queue's */
1568 if (err
|| max_queue_pairs
< VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN
||
1569 max_queue_pairs
> VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
||
1570 !virtio_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
))
1571 max_queue_pairs
= 1;
1573 /* Allocate ourselves a network device with room for our info */
1574 dev
= alloc_etherdev_mq(sizeof(struct virtnet_info
), max_queue_pairs
);
1578 /* Set up network device as normal. */
1579 dev
->priv_flags
|= IFF_UNICAST_FLT
| IFF_LIVE_ADDR_CHANGE
;
1580 dev
->netdev_ops
= &virtnet_netdev
;
1581 dev
->features
= NETIF_F_HIGHDMA
;
1583 SET_ETHTOOL_OPS(dev
, &virtnet_ethtool_ops
);
1584 SET_NETDEV_DEV(dev
, &vdev
->dev
);
1586 /* Do we support "hardware" checksums? */
1587 if (virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
)) {
1588 /* This opens up the world of extra features. */
1589 dev
->hw_features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
1591 dev
->features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
1593 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GSO
)) {
1594 dev
->hw_features
|= NETIF_F_TSO
| NETIF_F_UFO
1595 | NETIF_F_TSO_ECN
| NETIF_F_TSO6
;
1597 /* Individual feature bits: what can host handle? */
1598 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO4
))
1599 dev
->hw_features
|= NETIF_F_TSO
;
1600 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO6
))
1601 dev
->hw_features
|= NETIF_F_TSO6
;
1602 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_ECN
))
1603 dev
->hw_features
|= NETIF_F_TSO_ECN
;
1604 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_UFO
))
1605 dev
->hw_features
|= NETIF_F_UFO
;
1608 dev
->features
|= dev
->hw_features
& (NETIF_F_ALL_TSO
|NETIF_F_UFO
);
1609 /* (!csum && gso) case will be fixed by register_netdev() */
1611 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_CSUM
))
1612 dev
->features
|= NETIF_F_RXCSUM
;
1614 dev
->vlan_features
= dev
->features
;
1616 /* Configuration may specify what MAC to use. Otherwise random. */
1617 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MAC
))
1618 virtio_cread_bytes(vdev
,
1619 offsetof(struct virtio_net_config
, mac
),
1620 dev
->dev_addr
, dev
->addr_len
);
1622 eth_hw_addr_random(dev
);
1624 /* Set up our device-specific information */
1625 vi
= netdev_priv(dev
);
1629 vi
->stats
= alloc_percpu(struct virtnet_stats
);
1631 if (vi
->stats
== NULL
)
1634 for_each_possible_cpu(i
) {
1635 struct virtnet_stats
*virtnet_stats
;
1636 virtnet_stats
= per_cpu_ptr(vi
->stats
, i
);
1637 u64_stats_init(&virtnet_stats
->tx_syncp
);
1638 u64_stats_init(&virtnet_stats
->rx_syncp
);
1641 mutex_init(&vi
->config_lock
);
1642 vi
->config_enable
= true;
1643 INIT_WORK(&vi
->config_work
, virtnet_config_changed_work
);
1645 /* If we can receive ANY GSO packets, we must allocate large ones. */
1646 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO4
) ||
1647 virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO6
) ||
1648 virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_ECN
))
1649 vi
->big_packets
= true;
1651 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MRG_RXBUF
))
1652 vi
->mergeable_rx_bufs
= true;
1654 if (virtio_has_feature(vdev
, VIRTIO_F_ANY_LAYOUT
))
1655 vi
->any_header_sg
= true;
1657 if (virtio_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
))
1660 /* Use single tx/rx queue pair as default */
1661 vi
->curr_queue_pairs
= 1;
1662 vi
->max_queue_pairs
= max_queue_pairs
;
1664 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
1669 netif_set_real_num_tx_queues(dev
, vi
->curr_queue_pairs
);
1670 netif_set_real_num_rx_queues(dev
, vi
->curr_queue_pairs
);
1672 err
= register_netdev(dev
);
1674 pr_debug("virtio_net: registering device failed\n");
1678 /* Last of all, set up some receive buffers. */
1679 for (i
= 0; i
< vi
->curr_queue_pairs
; i
++) {
1680 try_fill_recv(&vi
->rq
[i
], GFP_KERNEL
);
1682 /* If we didn't even get one input buffer, we're useless. */
1683 if (vi
->rq
[i
].num
== 0) {
1684 free_unused_bufs(vi
);
1686 goto free_recv_bufs
;
1690 vi
->nb
.notifier_call
= &virtnet_cpu_callback
;
1691 err
= register_hotcpu_notifier(&vi
->nb
);
1693 pr_debug("virtio_net: registering cpu notifier failed\n");
1694 goto free_recv_bufs
;
1697 /* Assume link up if device can't report link status,
1698 otherwise get link status from config. */
1699 if (virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_STATUS
)) {
1700 netif_carrier_off(dev
);
1701 schedule_work(&vi
->config_work
);
1703 vi
->status
= VIRTIO_NET_S_LINK_UP
;
1704 netif_carrier_on(dev
);
1707 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1708 dev
->name
, max_queue_pairs
);
1713 free_receive_bufs(vi
);
1714 unregister_netdev(dev
);
1716 cancel_delayed_work_sync(&vi
->refill
);
1717 virtnet_del_vqs(vi
);
1718 if (vi
->alloc_frag
.page
)
1719 put_page(vi
->alloc_frag
.page
);
1721 free_percpu(vi
->stats
);
1727 static void remove_vq_common(struct virtnet_info
*vi
)
1729 vi
->vdev
->config
->reset(vi
->vdev
);
1731 /* Free unused buffers in both send and recv, if any. */
1732 free_unused_bufs(vi
);
1734 free_receive_bufs(vi
);
1736 virtnet_del_vqs(vi
);
1739 static void virtnet_remove(struct virtio_device
*vdev
)
1741 struct virtnet_info
*vi
= vdev
->priv
;
1743 unregister_hotcpu_notifier(&vi
->nb
);
1745 /* Prevent config work handler from accessing the device. */
1746 mutex_lock(&vi
->config_lock
);
1747 vi
->config_enable
= false;
1748 mutex_unlock(&vi
->config_lock
);
1750 unregister_netdev(vi
->dev
);
1752 remove_vq_common(vi
);
1753 if (vi
->alloc_frag
.page
)
1754 put_page(vi
->alloc_frag
.page
);
1756 flush_work(&vi
->config_work
);
1758 free_percpu(vi
->stats
);
1759 free_netdev(vi
->dev
);
1762 #ifdef CONFIG_PM_SLEEP
1763 static int virtnet_freeze(struct virtio_device
*vdev
)
1765 struct virtnet_info
*vi
= vdev
->priv
;
1768 unregister_hotcpu_notifier(&vi
->nb
);
1770 /* Prevent config work handler from accessing the device */
1771 mutex_lock(&vi
->config_lock
);
1772 vi
->config_enable
= false;
1773 mutex_unlock(&vi
->config_lock
);
1775 netif_device_detach(vi
->dev
);
1776 cancel_delayed_work_sync(&vi
->refill
);
1778 if (netif_running(vi
->dev
))
1779 for (i
= 0; i
< vi
->max_queue_pairs
; i
++) {
1780 napi_disable(&vi
->rq
[i
].napi
);
1781 netif_napi_del(&vi
->rq
[i
].napi
);
1784 remove_vq_common(vi
);
1786 flush_work(&vi
->config_work
);
1791 static int virtnet_restore(struct virtio_device
*vdev
)
1793 struct virtnet_info
*vi
= vdev
->priv
;
1800 if (netif_running(vi
->dev
)) {
1801 for (i
= 0; i
< vi
->curr_queue_pairs
; i
++)
1802 if (!try_fill_recv(&vi
->rq
[i
], GFP_KERNEL
))
1803 schedule_delayed_work(&vi
->refill
, 0);
1805 for (i
= 0; i
< vi
->max_queue_pairs
; i
++)
1806 virtnet_napi_enable(&vi
->rq
[i
]);
1809 netif_device_attach(vi
->dev
);
1811 mutex_lock(&vi
->config_lock
);
1812 vi
->config_enable
= true;
1813 mutex_unlock(&vi
->config_lock
);
1816 virtnet_set_queues(vi
, vi
->curr_queue_pairs
);
1819 err
= register_hotcpu_notifier(&vi
->nb
);
1827 static struct virtio_device_id id_table
[] = {
1828 { VIRTIO_ID_NET
, VIRTIO_DEV_ANY_ID
},
1832 static unsigned int features
[] = {
1833 VIRTIO_NET_F_CSUM
, VIRTIO_NET_F_GUEST_CSUM
,
1834 VIRTIO_NET_F_GSO
, VIRTIO_NET_F_MAC
,
1835 VIRTIO_NET_F_HOST_TSO4
, VIRTIO_NET_F_HOST_UFO
, VIRTIO_NET_F_HOST_TSO6
,
1836 VIRTIO_NET_F_HOST_ECN
, VIRTIO_NET_F_GUEST_TSO4
, VIRTIO_NET_F_GUEST_TSO6
,
1837 VIRTIO_NET_F_GUEST_ECN
, VIRTIO_NET_F_GUEST_UFO
,
1838 VIRTIO_NET_F_MRG_RXBUF
, VIRTIO_NET_F_STATUS
, VIRTIO_NET_F_CTRL_VQ
,
1839 VIRTIO_NET_F_CTRL_RX
, VIRTIO_NET_F_CTRL_VLAN
,
1840 VIRTIO_NET_F_GUEST_ANNOUNCE
, VIRTIO_NET_F_MQ
,
1841 VIRTIO_NET_F_CTRL_MAC_ADDR
,
1842 VIRTIO_F_ANY_LAYOUT
,
1845 static struct virtio_driver virtio_net_driver
= {
1846 .feature_table
= features
,
1847 .feature_table_size
= ARRAY_SIZE(features
),
1848 .driver
.name
= KBUILD_MODNAME
,
1849 .driver
.owner
= THIS_MODULE
,
1850 .id_table
= id_table
,
1851 .probe
= virtnet_probe
,
1852 .remove
= virtnet_remove
,
1853 .config_changed
= virtnet_config_changed
,
1854 #ifdef CONFIG_PM_SLEEP
1855 .freeze
= virtnet_freeze
,
1856 .restore
= virtnet_restore
,
1860 module_virtio_driver(virtio_net_driver
);
1862 MODULE_DEVICE_TABLE(virtio
, id_table
);
1863 MODULE_DESCRIPTION("Virtio network driver");
1864 MODULE_LICENSE("GPL");