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, see <http://www.gnu.org/licenses/>.
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/ethtool.h>
22 #include <linux/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/scatterlist.h>
26 #include <linux/if_vlan.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
30 static int napi_weight = NAPI_POLL_WEIGHT;
31 module_param(napi_weight, int, 0444);
33 static bool csum = true, gso = true;
34 module_param(csum, bool, 0444);
35 module_param(gso, bool, 0444);
37 /* FIXME: MTU in config. */
38 #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define MERGE_BUFFER_LEN (ALIGN(GOOD_PACKET_LEN + \
40 sizeof(struct virtio_net_hdr_mrg_rxbuf), \
42 #define GOOD_COPY_LEN 128
44 #define VIRTNET_DRIVER_VERSION "1.0.0"
46 struct virtnet_stats {
47 struct u64_stats_sync tx_syncp;
48 struct u64_stats_sync rx_syncp;
56 /* Internal representation of a send virtqueue */
58 /* Virtqueue associated with this send _queue */
61 /* TX: fragments + linear part + virtio header */
62 struct scatterlist sg[MAX_SKB_FRAGS + 2];
64 /* Name of the send queue: output.$index */
68 /* Internal representation of a receive virtqueue */
69 struct receive_queue {
70 /* Virtqueue associated with this receive_queue */
73 struct napi_struct napi;
75 /* Number of input buffers, and max we've ever had. */
76 unsigned int num, max;
78 /* Chain pages by the private ptr. */
81 /* RX: fragments + linear part + virtio header */
82 struct scatterlist sg[MAX_SKB_FRAGS + 2];
84 /* Name of this receive queue: input.$index */
89 struct virtio_device *vdev;
90 struct virtqueue *cvq;
91 struct net_device *dev;
92 struct send_queue *sq;
93 struct receive_queue *rq;
96 /* Max # of queue pairs supported by the device */
99 /* # of queue pairs currently used by the driver */
100 u16 curr_queue_pairs;
102 /* I like... big packets and I cannot lie! */
105 /* Host will merge rx buffers for big packets (shake it! shake it!) */
106 bool mergeable_rx_bufs;
108 /* Has control virtqueue */
111 /* Host can handle any s/g split between our header and packet data */
114 /* enable config space updates */
117 /* Active statistics */
118 struct virtnet_stats __percpu *stats;
120 /* Work struct for refilling if we run low on memory. */
121 struct delayed_work refill;
123 /* Work struct for config space updates */
124 struct work_struct config_work;
126 /* Lock for config space updates */
127 struct mutex config_lock;
129 /* Page_frag for GFP_KERNEL packet buffer allocation when we run
132 struct page_frag alloc_frag;
134 /* Does the affinity hint is set for virtqueues? */
135 bool affinity_hint_set;
137 /* CPU hot plug notifier */
138 struct notifier_block nb;
141 struct skb_vnet_hdr {
143 struct virtio_net_hdr hdr;
144 struct virtio_net_hdr_mrg_rxbuf mhdr;
148 struct padded_vnet_hdr {
149 struct virtio_net_hdr hdr;
151 * virtio_net_hdr should be in a separated sg buffer because of a
152 * QEMU bug, and data sg buffer shares same page with this header sg.
153 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
158 /* Converting between virtqueue no. and kernel tx/rx queue no.
159 * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
161 static int vq2txq(struct virtqueue *vq)
163 return (vq->index - 1) / 2;
166 static int txq2vq(int txq)
171 static int vq2rxq(struct virtqueue *vq)
173 return vq->index / 2;
176 static int rxq2vq(int rxq)
181 static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
183 return (struct skb_vnet_hdr *)skb->cb;
187 * private is used to chain pages for big packets, put the whole
188 * most recent used list in the beginning for reuse
190 static void give_pages(struct receive_queue *rq, struct page *page)
194 /* Find end of list, sew whole thing into vi->rq.pages. */
195 for (end = page; end->private; end = (struct page *)end->private);
196 end->private = (unsigned long)rq->pages;
200 static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
202 struct page *p = rq->pages;
205 rq->pages = (struct page *)p->private;
206 /* clear private here, it is used to chain pages */
209 p = alloc_page(gfp_mask);
213 static void skb_xmit_done(struct virtqueue *vq)
215 struct virtnet_info *vi = vq->vdev->priv;
217 /* Suppress further interrupts. */
218 virtqueue_disable_cb(vq);
220 /* We were probably waiting for more output buffers. */
221 netif_wake_subqueue(vi->dev, vq2txq(vq));
224 /* Called from bottom half context */
225 static struct sk_buff *page_to_skb(struct receive_queue *rq,
226 struct page *page, unsigned int offset,
227 unsigned int len, unsigned int truesize)
229 struct virtnet_info *vi = rq->vq->vdev->priv;
231 struct skb_vnet_hdr *hdr;
232 unsigned int copy, hdr_len, hdr_padded_len;
235 p = page_address(page) + offset;
237 /* copy small packet so we can reuse these pages for small data */
238 skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN);
242 hdr = skb_vnet_hdr(skb);
244 if (vi->mergeable_rx_bufs) {
245 hdr_len = sizeof hdr->mhdr;
246 hdr_padded_len = sizeof hdr->mhdr;
248 hdr_len = sizeof hdr->hdr;
249 hdr_padded_len = sizeof(struct padded_vnet_hdr);
252 memcpy(hdr, p, hdr_len);
255 offset += hdr_padded_len;
259 if (copy > skb_tailroom(skb))
260 copy = skb_tailroom(skb);
261 memcpy(skb_put(skb, copy), p, copy);
266 if (vi->mergeable_rx_bufs) {
268 skb_add_rx_frag(skb, 0, page, offset, len, truesize);
275 * Verify that we can indeed put this data into a skb.
276 * This is here to handle cases when the device erroneously
277 * tries to receive more than is possible. This is usually
278 * the case of a broken device.
280 if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
281 net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
285 BUG_ON(offset >= PAGE_SIZE);
287 unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
288 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
289 frag_size, truesize);
291 page = (struct page *)page->private;
296 give_pages(rq, page);
301 static struct sk_buff *receive_small(void *buf, unsigned int len)
303 struct sk_buff * skb = buf;
305 len -= sizeof(struct virtio_net_hdr);
311 static struct sk_buff *receive_big(struct net_device *dev,
312 struct receive_queue *rq,
316 struct page *page = buf;
317 struct sk_buff *skb = page_to_skb(rq, page, 0, len, PAGE_SIZE);
325 dev->stats.rx_dropped++;
326 give_pages(rq, page);
330 static struct sk_buff *receive_mergeable(struct net_device *dev,
331 struct receive_queue *rq,
335 struct skb_vnet_hdr *hdr = buf;
336 int num_buf = hdr->mhdr.num_buffers;
337 struct page *page = virt_to_head_page(buf);
338 int offset = buf - page_address(page);
339 struct sk_buff *head_skb = page_to_skb(rq, page, offset, len,
341 struct sk_buff *curr_skb = head_skb;
343 if (unlikely(!curr_skb))
349 buf = virtqueue_get_buf(rq->vq, &len);
350 if (unlikely(!buf)) {
351 pr_debug("%s: rx error: %d buffers out of %d missing\n",
352 dev->name, num_buf, hdr->mhdr.num_buffers);
353 dev->stats.rx_length_errors++;
356 if (unlikely(len > MERGE_BUFFER_LEN)) {
357 pr_debug("%s: rx error: merge buffer too long\n",
359 len = MERGE_BUFFER_LEN;
362 page = virt_to_head_page(buf);
365 num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
366 if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
367 struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
371 if (curr_skb == head_skb)
372 skb_shinfo(curr_skb)->frag_list = nskb;
374 curr_skb->next = nskb;
376 head_skb->truesize += nskb->truesize;
379 if (curr_skb != head_skb) {
380 head_skb->data_len += len;
381 head_skb->len += len;
382 head_skb->truesize += MERGE_BUFFER_LEN;
384 offset = buf - page_address(page);
385 if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
387 skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
388 len, MERGE_BUFFER_LEN);
390 skb_add_rx_frag(curr_skb, num_skb_frags, page,
391 offset, len, MERGE_BUFFER_LEN);
400 buf = virtqueue_get_buf(rq->vq, &len);
401 if (unlikely(!buf)) {
402 pr_debug("%s: rx error: %d buffers missing\n",
404 dev->stats.rx_length_errors++;
407 page = virt_to_head_page(buf);
412 dev->stats.rx_dropped++;
413 dev_kfree_skb(head_skb);
417 static void receive_buf(struct receive_queue *rq, void *buf, unsigned int len)
419 struct virtnet_info *vi = rq->vq->vdev->priv;
420 struct net_device *dev = vi->dev;
421 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
423 struct skb_vnet_hdr *hdr;
425 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
426 pr_debug("%s: short packet %i\n", dev->name, len);
427 dev->stats.rx_length_errors++;
428 if (vi->mergeable_rx_bufs)
429 put_page(virt_to_head_page(buf));
430 else if (vi->big_packets)
437 if (vi->mergeable_rx_bufs)
438 skb = receive_mergeable(dev, rq, buf, len);
439 else if (vi->big_packets)
440 skb = receive_big(dev, rq, buf, len);
442 skb = receive_small(buf, len);
447 hdr = skb_vnet_hdr(skb);
449 u64_stats_update_begin(&stats->rx_syncp);
450 stats->rx_bytes += skb->len;
452 u64_stats_update_end(&stats->rx_syncp);
454 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
455 pr_debug("Needs csum!\n");
456 if (!skb_partial_csum_set(skb,
458 hdr->hdr.csum_offset))
460 } else if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID) {
461 skb->ip_summed = CHECKSUM_UNNECESSARY;
464 skb->protocol = eth_type_trans(skb, dev);
465 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
466 ntohs(skb->protocol), skb->len, skb->pkt_type);
468 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
470 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
471 case VIRTIO_NET_HDR_GSO_TCPV4:
472 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
474 case VIRTIO_NET_HDR_GSO_UDP:
475 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
477 case VIRTIO_NET_HDR_GSO_TCPV6:
478 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
481 net_warn_ratelimited("%s: bad gso type %u.\n",
482 dev->name, hdr->hdr.gso_type);
486 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
487 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
489 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
490 if (skb_shinfo(skb)->gso_size == 0) {
491 net_warn_ratelimited("%s: zero gso size.\n", dev->name);
495 /* Header must be checked, and gso_segs computed. */
496 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
497 skb_shinfo(skb)->gso_segs = 0;
500 netif_receive_skb(skb);
504 dev->stats.rx_frame_errors++;
508 static int add_recvbuf_small(struct receive_queue *rq, gfp_t gfp)
510 struct virtnet_info *vi = rq->vq->vdev->priv;
512 struct skb_vnet_hdr *hdr;
515 skb = __netdev_alloc_skb_ip_align(vi->dev, GOOD_PACKET_LEN, gfp);
519 skb_put(skb, GOOD_PACKET_LEN);
521 hdr = skb_vnet_hdr(skb);
522 sg_set_buf(rq->sg, &hdr->hdr, sizeof hdr->hdr);
524 skb_to_sgvec(skb, rq->sg + 1, 0, skb->len);
526 err = virtqueue_add_inbuf(rq->vq, rq->sg, 2, skb, gfp);
533 static int add_recvbuf_big(struct receive_queue *rq, gfp_t gfp)
535 struct page *first, *list = NULL;
539 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
540 for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
541 first = get_a_page(rq, gfp);
544 give_pages(rq, list);
547 sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
549 /* chain new page in list head to match sg */
550 first->private = (unsigned long)list;
554 first = get_a_page(rq, gfp);
556 give_pages(rq, list);
559 p = page_address(first);
561 /* rq->sg[0], rq->sg[1] share the same page */
562 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
563 sg_set_buf(&rq->sg[0], p, sizeof(struct virtio_net_hdr));
565 /* rq->sg[1] for data packet, from offset */
566 offset = sizeof(struct padded_vnet_hdr);
567 sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
569 /* chain first in list head */
570 first->private = (unsigned long)list;
571 err = virtqueue_add_inbuf(rq->vq, rq->sg, MAX_SKB_FRAGS + 2,
574 give_pages(rq, first);
579 static int add_recvbuf_mergeable(struct receive_queue *rq, gfp_t gfp)
581 struct virtnet_info *vi = rq->vq->vdev->priv;
585 if (gfp & __GFP_WAIT) {
586 if (skb_page_frag_refill(MERGE_BUFFER_LEN, &vi->alloc_frag,
588 buf = (char *)page_address(vi->alloc_frag.page) +
589 vi->alloc_frag.offset;
590 get_page(vi->alloc_frag.page);
591 vi->alloc_frag.offset += MERGE_BUFFER_LEN;
594 buf = netdev_alloc_frag(MERGE_BUFFER_LEN);
599 sg_init_one(rq->sg, buf, MERGE_BUFFER_LEN);
600 err = virtqueue_add_inbuf(rq->vq, rq->sg, 1, buf, gfp);
602 put_page(virt_to_head_page(buf));
608 * Returns false if we couldn't fill entirely (OOM).
610 * Normally run in the receive path, but can also be run from ndo_open
611 * before we're receiving packets, or from refill_work which is
612 * careful to disable receiving (using napi_disable).
614 static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
616 struct virtnet_info *vi = rq->vq->vdev->priv;
621 if (vi->mergeable_rx_bufs)
622 err = add_recvbuf_mergeable(rq, gfp);
623 else if (vi->big_packets)
624 err = add_recvbuf_big(rq, gfp);
626 err = add_recvbuf_small(rq, gfp);
628 oom = err == -ENOMEM;
632 } while (rq->vq->num_free);
633 if (unlikely(rq->num > rq->max))
635 if (unlikely(!virtqueue_kick(rq->vq)))
640 static void skb_recv_done(struct virtqueue *rvq)
642 struct virtnet_info *vi = rvq->vdev->priv;
643 struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
645 /* Schedule NAPI, Suppress further interrupts if successful. */
646 if (napi_schedule_prep(&rq->napi)) {
647 virtqueue_disable_cb(rvq);
648 __napi_schedule(&rq->napi);
652 static void virtnet_napi_enable(struct receive_queue *rq)
654 napi_enable(&rq->napi);
656 /* If all buffers were filled by other side before we napi_enabled, we
657 * won't get another interrupt, so process any outstanding packets
658 * now. virtnet_poll wants re-enable the queue, so we disable here.
659 * We synchronize against interrupts via NAPI_STATE_SCHED */
660 if (napi_schedule_prep(&rq->napi)) {
661 virtqueue_disable_cb(rq->vq);
663 __napi_schedule(&rq->napi);
668 static void refill_work(struct work_struct *work)
670 struct virtnet_info *vi =
671 container_of(work, struct virtnet_info, refill.work);
675 for (i = 0; i < vi->curr_queue_pairs; i++) {
676 struct receive_queue *rq = &vi->rq[i];
678 napi_disable(&rq->napi);
679 still_empty = !try_fill_recv(rq, GFP_KERNEL);
680 virtnet_napi_enable(rq);
682 /* In theory, this can happen: if we don't get any buffers in
683 * we will *never* try to fill again.
686 schedule_delayed_work(&vi->refill, HZ/2);
690 static int virtnet_poll(struct napi_struct *napi, int budget)
692 struct receive_queue *rq =
693 container_of(napi, struct receive_queue, napi);
694 struct virtnet_info *vi = rq->vq->vdev->priv;
696 unsigned int r, len, received = 0;
699 while (received < budget &&
700 (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
701 receive_buf(rq, buf, len);
706 if (rq->num < rq->max / 2) {
707 if (!try_fill_recv(rq, GFP_ATOMIC))
708 schedule_delayed_work(&vi->refill, 0);
711 /* Out of packets? */
712 if (received < budget) {
713 r = virtqueue_enable_cb_prepare(rq->vq);
715 if (unlikely(virtqueue_poll(rq->vq, r)) &&
716 napi_schedule_prep(napi)) {
717 virtqueue_disable_cb(rq->vq);
718 __napi_schedule(napi);
726 static int virtnet_open(struct net_device *dev)
728 struct virtnet_info *vi = netdev_priv(dev);
731 for (i = 0; i < vi->max_queue_pairs; i++) {
732 if (i < vi->curr_queue_pairs)
733 /* Make sure we have some buffers: if oom use wq. */
734 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
735 schedule_delayed_work(&vi->refill, 0);
736 virtnet_napi_enable(&vi->rq[i]);
742 static void free_old_xmit_skbs(struct send_queue *sq)
746 struct virtnet_info *vi = sq->vq->vdev->priv;
747 struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
749 while ((skb = virtqueue_get_buf(sq->vq, &len)) != NULL) {
750 pr_debug("Sent skb %p\n", skb);
752 u64_stats_update_begin(&stats->tx_syncp);
753 stats->tx_bytes += skb->len;
755 u64_stats_update_end(&stats->tx_syncp);
757 dev_kfree_skb_any(skb);
761 static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
763 struct skb_vnet_hdr *hdr;
764 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
765 struct virtnet_info *vi = sq->vq->vdev->priv;
770 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
771 if (vi->mergeable_rx_bufs)
772 hdr_len = sizeof hdr->mhdr;
774 hdr_len = sizeof hdr->hdr;
776 can_push = vi->any_header_sg &&
777 !((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
778 !skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
779 /* Even if we can, don't push here yet as this would skew
780 * csum_start offset below. */
782 hdr = (struct skb_vnet_hdr *)(skb->data - hdr_len);
784 hdr = skb_vnet_hdr(skb);
786 if (skb->ip_summed == CHECKSUM_PARTIAL) {
787 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
788 hdr->hdr.csum_start = skb_checksum_start_offset(skb);
789 hdr->hdr.csum_offset = skb->csum_offset;
792 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
795 if (skb_is_gso(skb)) {
796 hdr->hdr.hdr_len = skb_headlen(skb);
797 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
798 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
799 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
800 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
801 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
802 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
803 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
806 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
807 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
809 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
810 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
813 if (vi->mergeable_rx_bufs)
814 hdr->mhdr.num_buffers = 0;
817 __skb_push(skb, hdr_len);
818 num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
819 /* Pull header back to avoid skew in tx bytes calculations. */
820 __skb_pull(skb, hdr_len);
822 sg_set_buf(sq->sg, hdr, hdr_len);
823 num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
825 return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
828 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
830 struct virtnet_info *vi = netdev_priv(dev);
831 int qnum = skb_get_queue_mapping(skb);
832 struct send_queue *sq = &vi->sq[qnum];
835 /* Free up any pending old buffers before queueing new ones. */
836 free_old_xmit_skbs(sq);
838 /* Try to transmit */
839 err = xmit_skb(sq, skb);
841 /* This should not happen! */
842 if (unlikely(err) || unlikely(!virtqueue_kick(sq->vq))) {
843 dev->stats.tx_fifo_errors++;
846 "Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
847 dev->stats.tx_dropped++;
852 /* Don't wait up for transmitted skbs to be freed. */
856 /* Apparently nice girls don't return TX_BUSY; stop the queue
857 * before it gets out of hand. Naturally, this wastes entries. */
858 if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
859 netif_stop_subqueue(dev, qnum);
860 if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
861 /* More just got used, free them then recheck. */
862 free_old_xmit_skbs(sq);
863 if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
864 netif_start_subqueue(dev, qnum);
865 virtqueue_disable_cb(sq->vq);
874 * Send command via the control virtqueue and check status. Commands
875 * supported by the hypervisor, as indicated by feature bits, should
876 * never fail unless improperly formatted.
878 static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
879 struct scatterlist *out)
881 struct scatterlist *sgs[4], hdr, stat;
882 struct virtio_net_ctrl_hdr ctrl;
883 virtio_net_ctrl_ack status = ~0;
884 unsigned out_num = 0, tmp;
886 /* Caller should know better */
887 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
892 sg_init_one(&hdr, &ctrl, sizeof(ctrl));
893 sgs[out_num++] = &hdr;
896 sgs[out_num++] = out;
898 /* Add return status. */
899 sg_init_one(&stat, &status, sizeof(status));
900 sgs[out_num] = &stat;
902 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
903 BUG_ON(virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC) < 0);
905 if (unlikely(!virtqueue_kick(vi->cvq)))
906 return status == VIRTIO_NET_OK;
908 /* Spin for a response, the kick causes an ioport write, trapping
909 * into the hypervisor, so the request should be handled immediately.
911 while (!virtqueue_get_buf(vi->cvq, &tmp) &&
912 !virtqueue_is_broken(vi->cvq))
915 return status == VIRTIO_NET_OK;
918 static int virtnet_set_mac_address(struct net_device *dev, void *p)
920 struct virtnet_info *vi = netdev_priv(dev);
921 struct virtio_device *vdev = vi->vdev;
923 struct sockaddr *addr = p;
924 struct scatterlist sg;
926 ret = eth_prepare_mac_addr_change(dev, p);
930 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
931 sg_init_one(&sg, addr->sa_data, dev->addr_len);
932 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
933 VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
935 "Failed to set mac address by vq command.\n");
938 } else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
941 /* Naturally, this has an atomicity problem. */
942 for (i = 0; i < dev->addr_len; i++)
944 offsetof(struct virtio_net_config, mac) +
945 i, addr->sa_data[i]);
948 eth_commit_mac_addr_change(dev, p);
953 static struct rtnl_link_stats64 *virtnet_stats(struct net_device *dev,
954 struct rtnl_link_stats64 *tot)
956 struct virtnet_info *vi = netdev_priv(dev);
960 for_each_possible_cpu(cpu) {
961 struct virtnet_stats *stats = per_cpu_ptr(vi->stats, cpu);
962 u64 tpackets, tbytes, rpackets, rbytes;
965 start = u64_stats_fetch_begin_bh(&stats->tx_syncp);
966 tpackets = stats->tx_packets;
967 tbytes = stats->tx_bytes;
968 } while (u64_stats_fetch_retry_bh(&stats->tx_syncp, start));
971 start = u64_stats_fetch_begin_bh(&stats->rx_syncp);
972 rpackets = stats->rx_packets;
973 rbytes = stats->rx_bytes;
974 } while (u64_stats_fetch_retry_bh(&stats->rx_syncp, start));
976 tot->rx_packets += rpackets;
977 tot->tx_packets += tpackets;
978 tot->rx_bytes += rbytes;
979 tot->tx_bytes += tbytes;
982 tot->tx_dropped = dev->stats.tx_dropped;
983 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
984 tot->rx_dropped = dev->stats.rx_dropped;
985 tot->rx_length_errors = dev->stats.rx_length_errors;
986 tot->rx_frame_errors = dev->stats.rx_frame_errors;
991 #ifdef CONFIG_NET_POLL_CONTROLLER
992 static void virtnet_netpoll(struct net_device *dev)
994 struct virtnet_info *vi = netdev_priv(dev);
997 for (i = 0; i < vi->curr_queue_pairs; i++)
998 napi_schedule(&vi->rq[i].napi);
1002 static void virtnet_ack_link_announce(struct virtnet_info *vi)
1005 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
1006 VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
1007 dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
1011 static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1013 struct scatterlist sg;
1014 struct virtio_net_ctrl_mq s;
1015 struct net_device *dev = vi->dev;
1017 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1020 s.virtqueue_pairs = queue_pairs;
1021 sg_init_one(&sg, &s, sizeof(s));
1023 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1024 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
1025 dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
1029 vi->curr_queue_pairs = queue_pairs;
1030 /* virtnet_open() will refill when device is going to up. */
1031 if (dev->flags & IFF_UP)
1032 schedule_delayed_work(&vi->refill, 0);
1038 static int virtnet_close(struct net_device *dev)
1040 struct virtnet_info *vi = netdev_priv(dev);
1043 /* Make sure refill_work doesn't re-enable napi! */
1044 cancel_delayed_work_sync(&vi->refill);
1046 for (i = 0; i < vi->max_queue_pairs; i++)
1047 napi_disable(&vi->rq[i].napi);
1052 static void virtnet_set_rx_mode(struct net_device *dev)
1054 struct virtnet_info *vi = netdev_priv(dev);
1055 struct scatterlist sg[2];
1056 u8 promisc, allmulti;
1057 struct virtio_net_ctrl_mac *mac_data;
1058 struct netdev_hw_addr *ha;
1064 /* We can't dynamically set ndo_set_rx_mode, so return gracefully */
1065 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1068 promisc = ((dev->flags & IFF_PROMISC) != 0);
1069 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1071 sg_init_one(sg, &promisc, sizeof(promisc));
1073 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1074 VIRTIO_NET_CTRL_RX_PROMISC, sg))
1075 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1076 promisc ? "en" : "dis");
1078 sg_init_one(sg, &allmulti, sizeof(allmulti));
1080 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1081 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1082 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1083 allmulti ? "en" : "dis");
1085 uc_count = netdev_uc_count(dev);
1086 mc_count = netdev_mc_count(dev);
1087 /* MAC filter - use one buffer for both lists */
1088 buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
1089 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
1094 sg_init_table(sg, 2);
1096 /* Store the unicast list and count in the front of the buffer */
1097 mac_data->entries = uc_count;
1099 netdev_for_each_uc_addr(ha, dev)
1100 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1102 sg_set_buf(&sg[0], mac_data,
1103 sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
1105 /* multicast list and count fill the end */
1106 mac_data = (void *)&mac_data->macs[uc_count][0];
1108 mac_data->entries = mc_count;
1110 netdev_for_each_mc_addr(ha, dev)
1111 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
1113 sg_set_buf(&sg[1], mac_data,
1114 sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
1116 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
1117 VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
1118 dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
1123 static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1124 __be16 proto, u16 vid)
1126 struct virtnet_info *vi = netdev_priv(dev);
1127 struct scatterlist sg;
1129 sg_init_one(&sg, &vid, sizeof(vid));
1131 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1132 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
1133 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
1137 static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1138 __be16 proto, u16 vid)
1140 struct virtnet_info *vi = netdev_priv(dev);
1141 struct scatterlist sg;
1143 sg_init_one(&sg, &vid, sizeof(vid));
1145 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1146 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
1147 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
1151 static void virtnet_clean_affinity(struct virtnet_info *vi, long hcpu)
1155 if (vi->affinity_hint_set) {
1156 for (i = 0; i < vi->max_queue_pairs; i++) {
1157 virtqueue_set_affinity(vi->rq[i].vq, -1);
1158 virtqueue_set_affinity(vi->sq[i].vq, -1);
1161 vi->affinity_hint_set = false;
1165 static void virtnet_set_affinity(struct virtnet_info *vi)
1170 /* In multiqueue mode, when the number of cpu is equal to the number of
1171 * queue pairs, we let the queue pairs to be private to one cpu by
1172 * setting the affinity hint to eliminate the contention.
1174 if (vi->curr_queue_pairs == 1 ||
1175 vi->max_queue_pairs != num_online_cpus()) {
1176 virtnet_clean_affinity(vi, -1);
1181 for_each_online_cpu(cpu) {
1182 virtqueue_set_affinity(vi->rq[i].vq, cpu);
1183 virtqueue_set_affinity(vi->sq[i].vq, cpu);
1184 netif_set_xps_queue(vi->dev, cpumask_of(cpu), i);
1188 vi->affinity_hint_set = true;
1191 static int virtnet_cpu_callback(struct notifier_block *nfb,
1192 unsigned long action, void *hcpu)
1194 struct virtnet_info *vi = container_of(nfb, struct virtnet_info, nb);
1196 switch(action & ~CPU_TASKS_FROZEN) {
1198 case CPU_DOWN_FAILED:
1200 virtnet_set_affinity(vi);
1202 case CPU_DOWN_PREPARE:
1203 virtnet_clean_affinity(vi, (long)hcpu);
1212 static void virtnet_get_ringparam(struct net_device *dev,
1213 struct ethtool_ringparam *ring)
1215 struct virtnet_info *vi = netdev_priv(dev);
1217 ring->rx_max_pending = virtqueue_get_vring_size(vi->rq[0].vq);
1218 ring->tx_max_pending = virtqueue_get_vring_size(vi->sq[0].vq);
1219 ring->rx_pending = ring->rx_max_pending;
1220 ring->tx_pending = ring->tx_max_pending;
1224 static void virtnet_get_drvinfo(struct net_device *dev,
1225 struct ethtool_drvinfo *info)
1227 struct virtnet_info *vi = netdev_priv(dev);
1228 struct virtio_device *vdev = vi->vdev;
1230 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1231 strlcpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
1232 strlcpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
1236 /* TODO: Eliminate OOO packets during switching */
1237 static int virtnet_set_channels(struct net_device *dev,
1238 struct ethtool_channels *channels)
1240 struct virtnet_info *vi = netdev_priv(dev);
1241 u16 queue_pairs = channels->combined_count;
1244 /* We don't support separate rx/tx channels.
1245 * We don't allow setting 'other' channels.
1247 if (channels->rx_count || channels->tx_count || channels->other_count)
1250 if (queue_pairs > vi->max_queue_pairs)
1254 err = virtnet_set_queues(vi, queue_pairs);
1256 netif_set_real_num_tx_queues(dev, queue_pairs);
1257 netif_set_real_num_rx_queues(dev, queue_pairs);
1259 virtnet_set_affinity(vi);
1266 static void virtnet_get_channels(struct net_device *dev,
1267 struct ethtool_channels *channels)
1269 struct virtnet_info *vi = netdev_priv(dev);
1271 channels->combined_count = vi->curr_queue_pairs;
1272 channels->max_combined = vi->max_queue_pairs;
1273 channels->max_other = 0;
1274 channels->rx_count = 0;
1275 channels->tx_count = 0;
1276 channels->other_count = 0;
1279 static const struct ethtool_ops virtnet_ethtool_ops = {
1280 .get_drvinfo = virtnet_get_drvinfo,
1281 .get_link = ethtool_op_get_link,
1282 .get_ringparam = virtnet_get_ringparam,
1283 .set_channels = virtnet_set_channels,
1284 .get_channels = virtnet_get_channels,
1288 #define MAX_MTU 65535
1290 static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
1292 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
1298 static const struct net_device_ops virtnet_netdev = {
1299 .ndo_open = virtnet_open,
1300 .ndo_stop = virtnet_close,
1301 .ndo_start_xmit = start_xmit,
1302 .ndo_validate_addr = eth_validate_addr,
1303 .ndo_set_mac_address = virtnet_set_mac_address,
1304 .ndo_set_rx_mode = virtnet_set_rx_mode,
1305 .ndo_change_mtu = virtnet_change_mtu,
1306 .ndo_get_stats64 = virtnet_stats,
1307 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
1308 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
1309 #ifdef CONFIG_NET_POLL_CONTROLLER
1310 .ndo_poll_controller = virtnet_netpoll,
1314 static void virtnet_config_changed_work(struct work_struct *work)
1316 struct virtnet_info *vi =
1317 container_of(work, struct virtnet_info, config_work);
1320 mutex_lock(&vi->config_lock);
1321 if (!vi->config_enable)
1324 if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
1325 struct virtio_net_config, status, &v) < 0)
1328 if (v & VIRTIO_NET_S_ANNOUNCE) {
1329 netdev_notify_peers(vi->dev);
1330 virtnet_ack_link_announce(vi);
1333 /* Ignore unknown (future) status bits */
1334 v &= VIRTIO_NET_S_LINK_UP;
1336 if (vi->status == v)
1341 if (vi->status & VIRTIO_NET_S_LINK_UP) {
1342 netif_carrier_on(vi->dev);
1343 netif_tx_wake_all_queues(vi->dev);
1345 netif_carrier_off(vi->dev);
1346 netif_tx_stop_all_queues(vi->dev);
1349 mutex_unlock(&vi->config_lock);
1352 static void virtnet_config_changed(struct virtio_device *vdev)
1354 struct virtnet_info *vi = vdev->priv;
1356 schedule_work(&vi->config_work);
1359 static void virtnet_free_queues(struct virtnet_info *vi)
1363 for (i = 0; i < vi->max_queue_pairs; i++)
1364 netif_napi_del(&vi->rq[i].napi);
1370 static void free_receive_bufs(struct virtnet_info *vi)
1374 for (i = 0; i < vi->max_queue_pairs; i++) {
1375 while (vi->rq[i].pages)
1376 __free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
1380 static void free_unused_bufs(struct virtnet_info *vi)
1385 for (i = 0; i < vi->max_queue_pairs; i++) {
1386 struct virtqueue *vq = vi->sq[i].vq;
1387 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
1391 for (i = 0; i < vi->max_queue_pairs; i++) {
1392 struct virtqueue *vq = vi->rq[i].vq;
1394 while ((buf = virtqueue_detach_unused_buf(vq)) != NULL) {
1395 if (vi->mergeable_rx_bufs)
1396 put_page(virt_to_head_page(buf));
1397 else if (vi->big_packets)
1398 give_pages(&vi->rq[i], buf);
1403 BUG_ON(vi->rq[i].num != 0);
1407 static void virtnet_del_vqs(struct virtnet_info *vi)
1409 struct virtio_device *vdev = vi->vdev;
1411 virtnet_clean_affinity(vi, -1);
1413 vdev->config->del_vqs(vdev);
1415 virtnet_free_queues(vi);
1418 static int virtnet_find_vqs(struct virtnet_info *vi)
1420 vq_callback_t **callbacks;
1421 struct virtqueue **vqs;
1426 /* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
1427 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
1428 * possible control vq.
1430 total_vqs = vi->max_queue_pairs * 2 +
1431 virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
1433 /* Allocate space for find_vqs parameters */
1434 vqs = kzalloc(total_vqs * sizeof(*vqs), GFP_KERNEL);
1437 callbacks = kmalloc(total_vqs * sizeof(*callbacks), GFP_KERNEL);
1440 names = kmalloc(total_vqs * sizeof(*names), GFP_KERNEL);
1444 /* Parameters for control virtqueue, if any */
1446 callbacks[total_vqs - 1] = NULL;
1447 names[total_vqs - 1] = "control";
1450 /* Allocate/initialize parameters for send/receive virtqueues */
1451 for (i = 0; i < vi->max_queue_pairs; i++) {
1452 callbacks[rxq2vq(i)] = skb_recv_done;
1453 callbacks[txq2vq(i)] = skb_xmit_done;
1454 sprintf(vi->rq[i].name, "input.%d", i);
1455 sprintf(vi->sq[i].name, "output.%d", i);
1456 names[rxq2vq(i)] = vi->rq[i].name;
1457 names[txq2vq(i)] = vi->sq[i].name;
1460 ret = vi->vdev->config->find_vqs(vi->vdev, total_vqs, vqs, callbacks,
1466 vi->cvq = vqs[total_vqs - 1];
1467 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
1468 vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1471 for (i = 0; i < vi->max_queue_pairs; i++) {
1472 vi->rq[i].vq = vqs[rxq2vq(i)];
1473 vi->sq[i].vq = vqs[txq2vq(i)];
1492 static int virtnet_alloc_queues(struct virtnet_info *vi)
1496 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
1499 vi->rq = kzalloc(sizeof(*vi->rq) * vi->max_queue_pairs, GFP_KERNEL);
1503 INIT_DELAYED_WORK(&vi->refill, refill_work);
1504 for (i = 0; i < vi->max_queue_pairs; i++) {
1505 vi->rq[i].pages = NULL;
1506 netif_napi_add(vi->dev, &vi->rq[i].napi, virtnet_poll,
1509 sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
1510 sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
1521 static int init_vqs(struct virtnet_info *vi)
1525 /* Allocate send & receive queues */
1526 ret = virtnet_alloc_queues(vi);
1530 ret = virtnet_find_vqs(vi);
1535 virtnet_set_affinity(vi);
1541 virtnet_free_queues(vi);
1546 static int virtnet_probe(struct virtio_device *vdev)
1549 struct net_device *dev;
1550 struct virtnet_info *vi;
1551 u16 max_queue_pairs;
1553 /* Find if host supports multiqueue virtio_net device */
1554 err = virtio_cread_feature(vdev, VIRTIO_NET_F_MQ,
1555 struct virtio_net_config,
1556 max_virtqueue_pairs, &max_queue_pairs);
1558 /* We need at least 2 queue's */
1559 if (err || max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
1560 max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
1561 !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1562 max_queue_pairs = 1;
1564 /* Allocate ourselves a network device with room for our info */
1565 dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
1569 /* Set up network device as normal. */
1570 dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE;
1571 dev->netdev_ops = &virtnet_netdev;
1572 dev->features = NETIF_F_HIGHDMA;
1574 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
1575 SET_NETDEV_DEV(dev, &vdev->dev);
1577 /* Do we support "hardware" checksums? */
1578 if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
1579 /* This opens up the world of extra features. */
1580 dev->hw_features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1582 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
1584 if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
1585 dev->hw_features |= NETIF_F_TSO | NETIF_F_UFO
1586 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
1588 /* Individual feature bits: what can host handle? */
1589 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
1590 dev->hw_features |= NETIF_F_TSO;
1591 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
1592 dev->hw_features |= NETIF_F_TSO6;
1593 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
1594 dev->hw_features |= NETIF_F_TSO_ECN;
1595 if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
1596 dev->hw_features |= NETIF_F_UFO;
1599 dev->features |= dev->hw_features & (NETIF_F_ALL_TSO|NETIF_F_UFO);
1600 /* (!csum && gso) case will be fixed by register_netdev() */
1602 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
1603 dev->features |= NETIF_F_RXCSUM;
1605 dev->vlan_features = dev->features;
1607 /* Configuration may specify what MAC to use. Otherwise random. */
1608 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
1609 virtio_cread_bytes(vdev,
1610 offsetof(struct virtio_net_config, mac),
1611 dev->dev_addr, dev->addr_len);
1613 eth_hw_addr_random(dev);
1615 /* Set up our device-specific information */
1616 vi = netdev_priv(dev);
1620 vi->stats = alloc_percpu(struct virtnet_stats);
1622 if (vi->stats == NULL)
1625 for_each_possible_cpu(i) {
1626 struct virtnet_stats *virtnet_stats;
1627 virtnet_stats = per_cpu_ptr(vi->stats, i);
1628 u64_stats_init(&virtnet_stats->tx_syncp);
1629 u64_stats_init(&virtnet_stats->rx_syncp);
1632 mutex_init(&vi->config_lock);
1633 vi->config_enable = true;
1634 INIT_WORK(&vi->config_work, virtnet_config_changed_work);
1636 /* If we can receive ANY GSO packets, we must allocate large ones. */
1637 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
1638 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) ||
1639 virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
1640 vi->big_packets = true;
1642 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
1643 vi->mergeable_rx_bufs = true;
1645 if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT))
1646 vi->any_header_sg = true;
1648 if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
1651 /* Use single tx/rx queue pair as default */
1652 vi->curr_queue_pairs = 1;
1653 vi->max_queue_pairs = max_queue_pairs;
1655 /* Allocate/initialize the rx/tx queues, and invoke find_vqs */
1660 netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
1661 netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
1663 err = register_netdev(dev);
1665 pr_debug("virtio_net: registering device failed\n");
1669 /* Last of all, set up some receive buffers. */
1670 for (i = 0; i < vi->curr_queue_pairs; i++) {
1671 try_fill_recv(&vi->rq[i], GFP_KERNEL);
1673 /* If we didn't even get one input buffer, we're useless. */
1674 if (vi->rq[i].num == 0) {
1675 free_unused_bufs(vi);
1677 goto free_recv_bufs;
1681 vi->nb.notifier_call = &virtnet_cpu_callback;
1682 err = register_hotcpu_notifier(&vi->nb);
1684 pr_debug("virtio_net: registering cpu notifier failed\n");
1685 goto free_recv_bufs;
1688 /* Assume link up if device can't report link status,
1689 otherwise get link status from config. */
1690 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
1691 netif_carrier_off(dev);
1692 schedule_work(&vi->config_work);
1694 vi->status = VIRTIO_NET_S_LINK_UP;
1695 netif_carrier_on(dev);
1698 pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
1699 dev->name, max_queue_pairs);
1704 free_receive_bufs(vi);
1705 unregister_netdev(dev);
1707 cancel_delayed_work_sync(&vi->refill);
1708 virtnet_del_vqs(vi);
1709 if (vi->alloc_frag.page)
1710 put_page(vi->alloc_frag.page);
1712 free_percpu(vi->stats);
1718 static void remove_vq_common(struct virtnet_info *vi)
1720 vi->vdev->config->reset(vi->vdev);
1722 /* Free unused buffers in both send and recv, if any. */
1723 free_unused_bufs(vi);
1725 free_receive_bufs(vi);
1727 virtnet_del_vqs(vi);
1730 static void virtnet_remove(struct virtio_device *vdev)
1732 struct virtnet_info *vi = vdev->priv;
1734 unregister_hotcpu_notifier(&vi->nb);
1736 /* Prevent config work handler from accessing the device. */
1737 mutex_lock(&vi->config_lock);
1738 vi->config_enable = false;
1739 mutex_unlock(&vi->config_lock);
1741 unregister_netdev(vi->dev);
1743 remove_vq_common(vi);
1744 if (vi->alloc_frag.page)
1745 put_page(vi->alloc_frag.page);
1747 flush_work(&vi->config_work);
1749 free_percpu(vi->stats);
1750 free_netdev(vi->dev);
1753 #ifdef CONFIG_PM_SLEEP
1754 static int virtnet_freeze(struct virtio_device *vdev)
1756 struct virtnet_info *vi = vdev->priv;
1759 unregister_hotcpu_notifier(&vi->nb);
1761 /* Prevent config work handler from accessing the device */
1762 mutex_lock(&vi->config_lock);
1763 vi->config_enable = false;
1764 mutex_unlock(&vi->config_lock);
1766 netif_device_detach(vi->dev);
1767 cancel_delayed_work_sync(&vi->refill);
1769 if (netif_running(vi->dev))
1770 for (i = 0; i < vi->max_queue_pairs; i++) {
1771 napi_disable(&vi->rq[i].napi);
1772 netif_napi_del(&vi->rq[i].napi);
1775 remove_vq_common(vi);
1777 flush_work(&vi->config_work);
1782 static int virtnet_restore(struct virtio_device *vdev)
1784 struct virtnet_info *vi = vdev->priv;
1791 if (netif_running(vi->dev))
1792 for (i = 0; i < vi->max_queue_pairs; i++)
1793 virtnet_napi_enable(&vi->rq[i]);
1795 netif_device_attach(vi->dev);
1797 for (i = 0; i < vi->curr_queue_pairs; i++)
1798 if (!try_fill_recv(&vi->rq[i], GFP_KERNEL))
1799 schedule_delayed_work(&vi->refill, 0);
1801 mutex_lock(&vi->config_lock);
1802 vi->config_enable = true;
1803 mutex_unlock(&vi->config_lock);
1806 virtnet_set_queues(vi, vi->curr_queue_pairs);
1809 err = register_hotcpu_notifier(&vi->nb);
1817 static struct virtio_device_id id_table[] = {
1818 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
1822 static unsigned int features[] = {
1823 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
1824 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
1825 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
1826 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
1827 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
1828 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
1829 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
1830 VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ,
1831 VIRTIO_NET_F_CTRL_MAC_ADDR,
1832 VIRTIO_F_ANY_LAYOUT,
1835 static struct virtio_driver virtio_net_driver = {
1836 .feature_table = features,
1837 .feature_table_size = ARRAY_SIZE(features),
1838 .driver.name = KBUILD_MODNAME,
1839 .driver.owner = THIS_MODULE,
1840 .id_table = id_table,
1841 .probe = virtnet_probe,
1842 .remove = virtnet_remove,
1843 .config_changed = virtnet_config_changed,
1844 #ifdef CONFIG_PM_SLEEP
1845 .freeze = virtnet_freeze,
1846 .restore = virtnet_restore,
1850 module_virtio_driver(virtio_net_driver);
1852 MODULE_DEVICE_TABLE(virtio, id_table);
1853 MODULE_DESCRIPTION("Virtio network driver");
1854 MODULE_LICENSE("GPL");