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>
30 static int napi_weight
= 128;
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 MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
39 #define GOOD_COPY_LEN 128
41 #define VIRTNET_SEND_COMMAND_SG_MAX 2
42 #define VIRTNET_DRIVER_VERSION "1.0.0"
44 struct virtnet_stats
{
45 struct u64_stats_sync tx_syncp
;
46 struct u64_stats_sync rx_syncp
;
54 /* Internal representation of a send virtqueue */
56 /* Virtqueue associated with this send _queue */
59 /* TX: fragments + linear part + virtio header */
60 struct scatterlist sg
[MAX_SKB_FRAGS
+ 2];
63 /* Internal representation of a receive virtqueue */
64 struct receive_queue
{
65 /* Virtqueue associated with this receive_queue */
68 struct napi_struct napi
;
70 /* Number of input buffers, and max we've ever had. */
71 unsigned int num
, max
;
73 /* Chain pages by the private ptr. */
76 /* RX: fragments + linear part + virtio header */
77 struct scatterlist sg
[MAX_SKB_FRAGS
+ 2];
81 struct virtio_device
*vdev
;
82 struct virtqueue
*cvq
;
83 struct net_device
*dev
;
85 struct receive_queue rq
;
88 /* I like... big packets and I cannot lie! */
91 /* Host will merge rx buffers for big packets (shake it! shake it!) */
92 bool mergeable_rx_bufs
;
94 /* enable config space updates */
97 /* Active statistics */
98 struct virtnet_stats __percpu
*stats
;
100 /* Work struct for refilling if we run low on memory. */
101 struct delayed_work refill
;
103 /* Work struct for config space updates */
104 struct work_struct config_work
;
106 /* Lock for config space updates */
107 struct mutex config_lock
;
110 struct skb_vnet_hdr
{
112 struct virtio_net_hdr hdr
;
113 struct virtio_net_hdr_mrg_rxbuf mhdr
;
118 struct padded_vnet_hdr
{
119 struct virtio_net_hdr hdr
;
121 * virtio_net_hdr should be in a separated sg buffer because of a
122 * QEMU bug, and data sg buffer shares same page with this header sg.
123 * This padding makes next sg 16 byte aligned after virtio_net_hdr.
128 static inline struct skb_vnet_hdr
*skb_vnet_hdr(struct sk_buff
*skb
)
130 return (struct skb_vnet_hdr
*)skb
->cb
;
134 * private is used to chain pages for big packets, put the whole
135 * most recent used list in the beginning for reuse
137 static void give_pages(struct receive_queue
*rq
, struct page
*page
)
141 /* Find end of list, sew whole thing into vi->rq.pages. */
142 for (end
= page
; end
->private; end
= (struct page
*)end
->private);
143 end
->private = (unsigned long)rq
->pages
;
147 static struct page
*get_a_page(struct receive_queue
*rq
, gfp_t gfp_mask
)
149 struct page
*p
= rq
->pages
;
152 rq
->pages
= (struct page
*)p
->private;
153 /* clear private here, it is used to chain pages */
156 p
= alloc_page(gfp_mask
);
160 static void skb_xmit_done(struct virtqueue
*vq
)
162 struct virtnet_info
*vi
= vq
->vdev
->priv
;
164 /* Suppress further interrupts. */
165 virtqueue_disable_cb(vq
);
167 /* We were probably waiting for more output buffers. */
168 netif_wake_queue(vi
->dev
);
171 static void set_skb_frag(struct sk_buff
*skb
, struct page
*page
,
172 unsigned int offset
, unsigned int *len
)
174 int size
= min((unsigned)PAGE_SIZE
- offset
, *len
);
175 int i
= skb_shinfo(skb
)->nr_frags
;
177 __skb_fill_page_desc(skb
, i
, page
, offset
, size
);
179 skb
->data_len
+= size
;
181 skb
->truesize
+= PAGE_SIZE
;
182 skb_shinfo(skb
)->nr_frags
++;
186 /* Called from bottom half context */
187 static struct sk_buff
*page_to_skb(struct receive_queue
*rq
,
188 struct page
*page
, unsigned int len
)
190 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
192 struct skb_vnet_hdr
*hdr
;
193 unsigned int copy
, hdr_len
, offset
;
196 p
= page_address(page
);
198 /* copy small packet so we can reuse these pages for small data */
199 skb
= netdev_alloc_skb_ip_align(vi
->dev
, GOOD_COPY_LEN
);
203 hdr
= skb_vnet_hdr(skb
);
205 if (vi
->mergeable_rx_bufs
) {
206 hdr_len
= sizeof hdr
->mhdr
;
209 hdr_len
= sizeof hdr
->hdr
;
210 offset
= sizeof(struct padded_vnet_hdr
);
213 memcpy(hdr
, p
, hdr_len
);
219 if (copy
> skb_tailroom(skb
))
220 copy
= skb_tailroom(skb
);
221 memcpy(skb_put(skb
, copy
), p
, copy
);
227 * Verify that we can indeed put this data into a skb.
228 * This is here to handle cases when the device erroneously
229 * tries to receive more than is possible. This is usually
230 * the case of a broken device.
232 if (unlikely(len
> MAX_SKB_FRAGS
* PAGE_SIZE
)) {
233 net_dbg_ratelimited("%s: too much data\n", skb
->dev
->name
);
239 set_skb_frag(skb
, page
, offset
, &len
);
240 page
= (struct page
*)page
->private;
245 give_pages(rq
, page
);
250 static int receive_mergeable(struct receive_queue
*rq
, struct sk_buff
*skb
)
252 struct skb_vnet_hdr
*hdr
= skb_vnet_hdr(skb
);
256 num_buf
= hdr
->mhdr
.num_buffers
;
258 i
= skb_shinfo(skb
)->nr_frags
;
259 if (i
>= MAX_SKB_FRAGS
) {
260 pr_debug("%s: packet too long\n", skb
->dev
->name
);
261 skb
->dev
->stats
.rx_length_errors
++;
264 page
= virtqueue_get_buf(rq
->vq
, &len
);
266 pr_debug("%s: rx error: %d buffers missing\n",
267 skb
->dev
->name
, hdr
->mhdr
.num_buffers
);
268 skb
->dev
->stats
.rx_length_errors
++;
275 set_skb_frag(skb
, page
, 0, &len
);
282 static void receive_buf(struct receive_queue
*rq
, void *buf
, unsigned int len
)
284 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
285 struct net_device
*dev
= vi
->dev
;
286 struct virtnet_stats
*stats
= this_cpu_ptr(vi
->stats
);
289 struct skb_vnet_hdr
*hdr
;
291 if (unlikely(len
< sizeof(struct virtio_net_hdr
) + ETH_HLEN
)) {
292 pr_debug("%s: short packet %i\n", dev
->name
, len
);
293 dev
->stats
.rx_length_errors
++;
294 if (vi
->mergeable_rx_bufs
|| vi
->big_packets
)
301 if (!vi
->mergeable_rx_bufs
&& !vi
->big_packets
) {
303 len
-= sizeof(struct virtio_net_hdr
);
307 skb
= page_to_skb(rq
, page
, len
);
308 if (unlikely(!skb
)) {
309 dev
->stats
.rx_dropped
++;
310 give_pages(rq
, page
);
313 if (vi
->mergeable_rx_bufs
)
314 if (receive_mergeable(rq
, skb
)) {
320 hdr
= skb_vnet_hdr(skb
);
322 u64_stats_update_begin(&stats
->rx_syncp
);
323 stats
->rx_bytes
+= skb
->len
;
325 u64_stats_update_end(&stats
->rx_syncp
);
327 if (hdr
->hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
328 pr_debug("Needs csum!\n");
329 if (!skb_partial_csum_set(skb
,
331 hdr
->hdr
.csum_offset
))
333 } else if (hdr
->hdr
.flags
& VIRTIO_NET_HDR_F_DATA_VALID
) {
334 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
337 skb
->protocol
= eth_type_trans(skb
, dev
);
338 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
339 ntohs(skb
->protocol
), skb
->len
, skb
->pkt_type
);
341 if (hdr
->hdr
.gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
343 switch (hdr
->hdr
.gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
344 case VIRTIO_NET_HDR_GSO_TCPV4
:
345 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
347 case VIRTIO_NET_HDR_GSO_UDP
:
348 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
350 case VIRTIO_NET_HDR_GSO_TCPV6
:
351 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
354 net_warn_ratelimited("%s: bad gso type %u.\n",
355 dev
->name
, hdr
->hdr
.gso_type
);
359 if (hdr
->hdr
.gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
360 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
362 skb_shinfo(skb
)->gso_size
= hdr
->hdr
.gso_size
;
363 if (skb_shinfo(skb
)->gso_size
== 0) {
364 net_warn_ratelimited("%s: zero gso size.\n", dev
->name
);
368 /* Header must be checked, and gso_segs computed. */
369 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
370 skb_shinfo(skb
)->gso_segs
= 0;
373 netif_receive_skb(skb
);
377 dev
->stats
.rx_frame_errors
++;
381 static int add_recvbuf_small(struct receive_queue
*rq
, gfp_t gfp
)
383 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
385 struct skb_vnet_hdr
*hdr
;
388 skb
= __netdev_alloc_skb_ip_align(vi
->dev
, MAX_PACKET_LEN
, gfp
);
392 skb_put(skb
, MAX_PACKET_LEN
);
394 hdr
= skb_vnet_hdr(skb
);
395 sg_set_buf(rq
->sg
, &hdr
->hdr
, sizeof hdr
->hdr
);
397 skb_to_sgvec(skb
, rq
->sg
+ 1, 0, skb
->len
);
399 err
= virtqueue_add_buf(rq
->vq
, rq
->sg
, 0, 2, skb
, gfp
);
406 static int add_recvbuf_big(struct receive_queue
*rq
, gfp_t gfp
)
408 struct page
*first
, *list
= NULL
;
412 /* page in rq->sg[MAX_SKB_FRAGS + 1] is list tail */
413 for (i
= MAX_SKB_FRAGS
+ 1; i
> 1; --i
) {
414 first
= get_a_page(rq
, gfp
);
417 give_pages(rq
, list
);
420 sg_set_buf(&rq
->sg
[i
], page_address(first
), PAGE_SIZE
);
422 /* chain new page in list head to match sg */
423 first
->private = (unsigned long)list
;
427 first
= get_a_page(rq
, gfp
);
429 give_pages(rq
, list
);
432 p
= page_address(first
);
434 /* rq->sg[0], rq->sg[1] share the same page */
435 /* a separated rq->sg[0] for virtio_net_hdr only due to QEMU bug */
436 sg_set_buf(&rq
->sg
[0], p
, sizeof(struct virtio_net_hdr
));
438 /* rq->sg[1] for data packet, from offset */
439 offset
= sizeof(struct padded_vnet_hdr
);
440 sg_set_buf(&rq
->sg
[1], p
+ offset
, PAGE_SIZE
- offset
);
442 /* chain first in list head */
443 first
->private = (unsigned long)list
;
444 err
= virtqueue_add_buf(rq
->vq
, rq
->sg
, 0, MAX_SKB_FRAGS
+ 2,
447 give_pages(rq
, first
);
452 static int add_recvbuf_mergeable(struct receive_queue
*rq
, gfp_t gfp
)
457 page
= get_a_page(rq
, gfp
);
461 sg_init_one(rq
->sg
, page_address(page
), PAGE_SIZE
);
463 err
= virtqueue_add_buf(rq
->vq
, rq
->sg
, 0, 1, page
, gfp
);
465 give_pages(rq
, page
);
471 * Returns false if we couldn't fill entirely (OOM).
473 * Normally run in the receive path, but can also be run from ndo_open
474 * before we're receiving packets, or from refill_work which is
475 * careful to disable receiving (using napi_disable).
477 static bool try_fill_recv(struct receive_queue
*rq
, gfp_t gfp
)
479 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
484 if (vi
->mergeable_rx_bufs
)
485 err
= add_recvbuf_mergeable(rq
, gfp
);
486 else if (vi
->big_packets
)
487 err
= add_recvbuf_big(rq
, gfp
);
489 err
= add_recvbuf_small(rq
, gfp
);
491 oom
= err
== -ENOMEM
;
496 if (unlikely(rq
->num
> rq
->max
))
498 virtqueue_kick(rq
->vq
);
502 static void skb_recv_done(struct virtqueue
*rvq
)
504 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
505 struct receive_queue
*rq
= &vi
->rq
;
507 /* Schedule NAPI, Suppress further interrupts if successful. */
508 if (napi_schedule_prep(&rq
->napi
)) {
509 virtqueue_disable_cb(rvq
);
510 __napi_schedule(&rq
->napi
);
514 static void virtnet_napi_enable(struct receive_queue
*rq
)
516 napi_enable(&rq
->napi
);
518 /* If all buffers were filled by other side before we napi_enabled, we
519 * won't get another interrupt, so process any outstanding packets
520 * now. virtnet_poll wants re-enable the queue, so we disable here.
521 * We synchronize against interrupts via NAPI_STATE_SCHED */
522 if (napi_schedule_prep(&rq
->napi
)) {
523 virtqueue_disable_cb(rq
->vq
);
525 __napi_schedule(&rq
->napi
);
530 static void refill_work(struct work_struct
*work
)
532 struct virtnet_info
*vi
=
533 container_of(work
, struct virtnet_info
, refill
.work
);
536 napi_disable(&vi
->rq
.napi
);
537 still_empty
= !try_fill_recv(&vi
->rq
, GFP_KERNEL
);
538 virtnet_napi_enable(&vi
->rq
);
540 /* In theory, this can happen: if we don't get any buffers in
541 * we will *never* try to fill again. */
543 schedule_delayed_work(&vi
->refill
, HZ
/2);
546 static int virtnet_poll(struct napi_struct
*napi
, int budget
)
548 struct receive_queue
*rq
=
549 container_of(napi
, struct receive_queue
, napi
);
550 struct virtnet_info
*vi
= rq
->vq
->vdev
->priv
;
552 unsigned int len
, received
= 0;
555 while (received
< budget
&&
556 (buf
= virtqueue_get_buf(rq
->vq
, &len
)) != NULL
) {
557 receive_buf(rq
, buf
, len
);
562 if (rq
->num
< rq
->max
/ 2) {
563 if (!try_fill_recv(rq
, GFP_ATOMIC
))
564 schedule_delayed_work(&vi
->refill
, 0);
567 /* Out of packets? */
568 if (received
< budget
) {
570 if (unlikely(!virtqueue_enable_cb(rq
->vq
)) &&
571 napi_schedule_prep(napi
)) {
572 virtqueue_disable_cb(rq
->vq
);
573 __napi_schedule(napi
);
581 static unsigned int free_old_xmit_skbs(struct send_queue
*sq
)
584 unsigned int len
, tot_sgs
= 0;
585 struct virtnet_info
*vi
= sq
->vq
->vdev
->priv
;
586 struct virtnet_stats
*stats
= this_cpu_ptr(vi
->stats
);
588 while ((skb
= virtqueue_get_buf(sq
->vq
, &len
)) != NULL
) {
589 pr_debug("Sent skb %p\n", skb
);
591 u64_stats_update_begin(&stats
->tx_syncp
);
592 stats
->tx_bytes
+= skb
->len
;
594 u64_stats_update_end(&stats
->tx_syncp
);
596 tot_sgs
+= skb_vnet_hdr(skb
)->num_sg
;
597 dev_kfree_skb_any(skb
);
602 static int xmit_skb(struct send_queue
*sq
, struct sk_buff
*skb
)
604 struct skb_vnet_hdr
*hdr
= skb_vnet_hdr(skb
);
605 const unsigned char *dest
= ((struct ethhdr
*)skb
->data
)->h_dest
;
606 struct virtnet_info
*vi
= sq
->vq
->vdev
->priv
;
608 pr_debug("%s: xmit %p %pM\n", vi
->dev
->name
, skb
, dest
);
610 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
611 hdr
->hdr
.flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
612 hdr
->hdr
.csum_start
= skb_checksum_start_offset(skb
);
613 hdr
->hdr
.csum_offset
= skb
->csum_offset
;
616 hdr
->hdr
.csum_offset
= hdr
->hdr
.csum_start
= 0;
619 if (skb_is_gso(skb
)) {
620 hdr
->hdr
.hdr_len
= skb_headlen(skb
);
621 hdr
->hdr
.gso_size
= skb_shinfo(skb
)->gso_size
;
622 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
623 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
624 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
625 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
626 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
627 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
630 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCP_ECN
)
631 hdr
->hdr
.gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
633 hdr
->hdr
.gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
634 hdr
->hdr
.gso_size
= hdr
->hdr
.hdr_len
= 0;
637 hdr
->mhdr
.num_buffers
= 0;
639 /* Encode metadata header at front. */
640 if (vi
->mergeable_rx_bufs
)
641 sg_set_buf(sq
->sg
, &hdr
->mhdr
, sizeof hdr
->mhdr
);
643 sg_set_buf(sq
->sg
, &hdr
->hdr
, sizeof hdr
->hdr
);
645 hdr
->num_sg
= skb_to_sgvec(skb
, sq
->sg
+ 1, 0, skb
->len
) + 1;
646 return virtqueue_add_buf(sq
->vq
, sq
->sg
, hdr
->num_sg
,
650 static netdev_tx_t
start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
652 struct virtnet_info
*vi
= netdev_priv(dev
);
653 struct send_queue
*sq
= &vi
->sq
;
656 /* Free up any pending old buffers before queueing new ones. */
657 free_old_xmit_skbs(sq
);
659 /* Try to transmit */
660 capacity
= xmit_skb(sq
, skb
);
662 /* This can happen with OOM and indirect buffers. */
663 if (unlikely(capacity
< 0)) {
664 if (likely(capacity
== -ENOMEM
)) {
667 "TX queue failure: out of memory\n");
669 dev
->stats
.tx_fifo_errors
++;
672 "Unexpected TX queue failure: %d\n",
675 dev
->stats
.tx_dropped
++;
679 virtqueue_kick(sq
->vq
);
681 /* Don't wait up for transmitted skbs to be freed. */
685 /* Apparently nice girls don't return TX_BUSY; stop the queue
686 * before it gets out of hand. Naturally, this wastes entries. */
687 if (capacity
< 2+MAX_SKB_FRAGS
) {
688 netif_stop_queue(dev
);
689 if (unlikely(!virtqueue_enable_cb_delayed(sq
->vq
))) {
690 /* More just got used, free them then recheck. */
691 capacity
+= free_old_xmit_skbs(sq
);
692 if (capacity
>= 2+MAX_SKB_FRAGS
) {
693 netif_start_queue(dev
);
694 virtqueue_disable_cb(sq
->vq
);
702 static int virtnet_set_mac_address(struct net_device
*dev
, void *p
)
704 struct virtnet_info
*vi
= netdev_priv(dev
);
705 struct virtio_device
*vdev
= vi
->vdev
;
708 ret
= eth_mac_addr(dev
, p
);
712 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MAC
))
713 vdev
->config
->set(vdev
, offsetof(struct virtio_net_config
, mac
),
714 dev
->dev_addr
, dev
->addr_len
);
719 static struct rtnl_link_stats64
*virtnet_stats(struct net_device
*dev
,
720 struct rtnl_link_stats64
*tot
)
722 struct virtnet_info
*vi
= netdev_priv(dev
);
726 for_each_possible_cpu(cpu
) {
727 struct virtnet_stats
*stats
= per_cpu_ptr(vi
->stats
, cpu
);
728 u64 tpackets
, tbytes
, rpackets
, rbytes
;
731 start
= u64_stats_fetch_begin_bh(&stats
->tx_syncp
);
732 tpackets
= stats
->tx_packets
;
733 tbytes
= stats
->tx_bytes
;
734 } while (u64_stats_fetch_retry_bh(&stats
->tx_syncp
, start
));
737 start
= u64_stats_fetch_begin_bh(&stats
->rx_syncp
);
738 rpackets
= stats
->rx_packets
;
739 rbytes
= stats
->rx_bytes
;
740 } while (u64_stats_fetch_retry_bh(&stats
->rx_syncp
, start
));
742 tot
->rx_packets
+= rpackets
;
743 tot
->tx_packets
+= tpackets
;
744 tot
->rx_bytes
+= rbytes
;
745 tot
->tx_bytes
+= tbytes
;
748 tot
->tx_dropped
= dev
->stats
.tx_dropped
;
749 tot
->tx_fifo_errors
= dev
->stats
.tx_fifo_errors
;
750 tot
->rx_dropped
= dev
->stats
.rx_dropped
;
751 tot
->rx_length_errors
= dev
->stats
.rx_length_errors
;
752 tot
->rx_frame_errors
= dev
->stats
.rx_frame_errors
;
757 #ifdef CONFIG_NET_POLL_CONTROLLER
758 static void virtnet_netpoll(struct net_device
*dev
)
760 struct virtnet_info
*vi
= netdev_priv(dev
);
762 napi_schedule(&vi
->rq
.napi
);
766 static int virtnet_open(struct net_device
*dev
)
768 struct virtnet_info
*vi
= netdev_priv(dev
);
770 /* Make sure we have some buffers: if oom use wq. */
771 if (!try_fill_recv(&vi
->rq
, GFP_KERNEL
))
772 schedule_delayed_work(&vi
->refill
, 0);
774 virtnet_napi_enable(&vi
->rq
);
779 * Send command via the control virtqueue and check status. Commands
780 * supported by the hypervisor, as indicated by feature bits, should
781 * never fail unless improperly formated.
783 static bool virtnet_send_command(struct virtnet_info
*vi
, u8
class, u8 cmd
,
784 struct scatterlist
*data
, int out
, int in
)
786 struct scatterlist
*s
, sg
[VIRTNET_SEND_COMMAND_SG_MAX
+ 2];
787 struct virtio_net_ctrl_hdr ctrl
;
788 virtio_net_ctrl_ack status
= ~0;
792 /* Caller should know better */
793 BUG_ON(!virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VQ
) ||
794 (out
+ in
> VIRTNET_SEND_COMMAND_SG_MAX
));
796 out
++; /* Add header */
797 in
++; /* Add return status */
802 sg_init_table(sg
, out
+ in
);
804 sg_set_buf(&sg
[0], &ctrl
, sizeof(ctrl
));
805 for_each_sg(data
, s
, out
+ in
- 2, i
)
806 sg_set_buf(&sg
[i
+ 1], sg_virt(s
), s
->length
);
807 sg_set_buf(&sg
[out
+ in
- 1], &status
, sizeof(status
));
809 BUG_ON(virtqueue_add_buf(vi
->cvq
, sg
, out
, in
, vi
, GFP_ATOMIC
) < 0);
811 virtqueue_kick(vi
->cvq
);
814 * Spin for a response, the kick causes an ioport write, trapping
815 * into the hypervisor, so the request should be handled immediately.
817 while (!virtqueue_get_buf(vi
->cvq
, &tmp
))
820 return status
== VIRTIO_NET_OK
;
823 static void virtnet_ack_link_announce(struct virtnet_info
*vi
)
826 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_ANNOUNCE
,
827 VIRTIO_NET_CTRL_ANNOUNCE_ACK
, NULL
,
829 dev_warn(&vi
->dev
->dev
, "Failed to ack link announce.\n");
833 static int virtnet_close(struct net_device
*dev
)
835 struct virtnet_info
*vi
= netdev_priv(dev
);
837 /* Make sure refill_work doesn't re-enable napi! */
838 cancel_delayed_work_sync(&vi
->refill
);
839 napi_disable(&vi
->rq
.napi
);
844 static void virtnet_set_rx_mode(struct net_device
*dev
)
846 struct virtnet_info
*vi
= netdev_priv(dev
);
847 struct scatterlist sg
[2];
848 u8 promisc
, allmulti
;
849 struct virtio_net_ctrl_mac
*mac_data
;
850 struct netdev_hw_addr
*ha
;
856 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
857 if (!virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_RX
))
860 promisc
= ((dev
->flags
& IFF_PROMISC
) != 0);
861 allmulti
= ((dev
->flags
& IFF_ALLMULTI
) != 0);
863 sg_init_one(sg
, &promisc
, sizeof(promisc
));
865 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_RX
,
866 VIRTIO_NET_CTRL_RX_PROMISC
,
868 dev_warn(&dev
->dev
, "Failed to %sable promisc mode.\n",
869 promisc
? "en" : "dis");
871 sg_init_one(sg
, &allmulti
, sizeof(allmulti
));
873 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_RX
,
874 VIRTIO_NET_CTRL_RX_ALLMULTI
,
876 dev_warn(&dev
->dev
, "Failed to %sable allmulti mode.\n",
877 allmulti
? "en" : "dis");
879 uc_count
= netdev_uc_count(dev
);
880 mc_count
= netdev_mc_count(dev
);
881 /* MAC filter - use one buffer for both lists */
882 buf
= kzalloc(((uc_count
+ mc_count
) * ETH_ALEN
) +
883 (2 * sizeof(mac_data
->entries
)), GFP_ATOMIC
);
886 dev_warn(&dev
->dev
, "No memory for MAC address buffer\n");
890 sg_init_table(sg
, 2);
892 /* Store the unicast list and count in the front of the buffer */
893 mac_data
->entries
= uc_count
;
895 netdev_for_each_uc_addr(ha
, dev
)
896 memcpy(&mac_data
->macs
[i
++][0], ha
->addr
, ETH_ALEN
);
898 sg_set_buf(&sg
[0], mac_data
,
899 sizeof(mac_data
->entries
) + (uc_count
* ETH_ALEN
));
901 /* multicast list and count fill the end */
902 mac_data
= (void *)&mac_data
->macs
[uc_count
][0];
904 mac_data
->entries
= mc_count
;
906 netdev_for_each_mc_addr(ha
, dev
)
907 memcpy(&mac_data
->macs
[i
++][0], ha
->addr
, ETH_ALEN
);
909 sg_set_buf(&sg
[1], mac_data
,
910 sizeof(mac_data
->entries
) + (mc_count
* ETH_ALEN
));
912 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_MAC
,
913 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
915 dev_warn(&dev
->dev
, "Failed to set MAC fitler table.\n");
920 static int virtnet_vlan_rx_add_vid(struct net_device
*dev
, u16 vid
)
922 struct virtnet_info
*vi
= netdev_priv(dev
);
923 struct scatterlist sg
;
925 sg_init_one(&sg
, &vid
, sizeof(vid
));
927 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_VLAN
,
928 VIRTIO_NET_CTRL_VLAN_ADD
, &sg
, 1, 0))
929 dev_warn(&dev
->dev
, "Failed to add VLAN ID %d.\n", vid
);
933 static int virtnet_vlan_rx_kill_vid(struct net_device
*dev
, u16 vid
)
935 struct virtnet_info
*vi
= netdev_priv(dev
);
936 struct scatterlist sg
;
938 sg_init_one(&sg
, &vid
, sizeof(vid
));
940 if (!virtnet_send_command(vi
, VIRTIO_NET_CTRL_VLAN
,
941 VIRTIO_NET_CTRL_VLAN_DEL
, &sg
, 1, 0))
942 dev_warn(&dev
->dev
, "Failed to kill VLAN ID %d.\n", vid
);
946 static void virtnet_get_ringparam(struct net_device
*dev
,
947 struct ethtool_ringparam
*ring
)
949 struct virtnet_info
*vi
= netdev_priv(dev
);
951 ring
->rx_max_pending
= virtqueue_get_vring_size(vi
->rq
.vq
);
952 ring
->tx_max_pending
= virtqueue_get_vring_size(vi
->sq
.vq
);
953 ring
->rx_pending
= ring
->rx_max_pending
;
954 ring
->tx_pending
= ring
->tx_max_pending
;
958 static void virtnet_get_drvinfo(struct net_device
*dev
,
959 struct ethtool_drvinfo
*info
)
961 struct virtnet_info
*vi
= netdev_priv(dev
);
962 struct virtio_device
*vdev
= vi
->vdev
;
964 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
965 strlcpy(info
->version
, VIRTNET_DRIVER_VERSION
, sizeof(info
->version
));
966 strlcpy(info
->bus_info
, virtio_bus_name(vdev
), sizeof(info
->bus_info
));
970 static const struct ethtool_ops virtnet_ethtool_ops
= {
971 .get_drvinfo
= virtnet_get_drvinfo
,
972 .get_link
= ethtool_op_get_link
,
973 .get_ringparam
= virtnet_get_ringparam
,
977 #define MAX_MTU 65535
979 static int virtnet_change_mtu(struct net_device
*dev
, int new_mtu
)
981 if (new_mtu
< MIN_MTU
|| new_mtu
> MAX_MTU
)
987 static const struct net_device_ops virtnet_netdev
= {
988 .ndo_open
= virtnet_open
,
989 .ndo_stop
= virtnet_close
,
990 .ndo_start_xmit
= start_xmit
,
991 .ndo_validate_addr
= eth_validate_addr
,
992 .ndo_set_mac_address
= virtnet_set_mac_address
,
993 .ndo_set_rx_mode
= virtnet_set_rx_mode
,
994 .ndo_change_mtu
= virtnet_change_mtu
,
995 .ndo_get_stats64
= virtnet_stats
,
996 .ndo_vlan_rx_add_vid
= virtnet_vlan_rx_add_vid
,
997 .ndo_vlan_rx_kill_vid
= virtnet_vlan_rx_kill_vid
,
998 #ifdef CONFIG_NET_POLL_CONTROLLER
999 .ndo_poll_controller
= virtnet_netpoll
,
1003 static void virtnet_config_changed_work(struct work_struct
*work
)
1005 struct virtnet_info
*vi
=
1006 container_of(work
, struct virtnet_info
, config_work
);
1009 mutex_lock(&vi
->config_lock
);
1010 if (!vi
->config_enable
)
1013 if (virtio_config_val(vi
->vdev
, VIRTIO_NET_F_STATUS
,
1014 offsetof(struct virtio_net_config
, status
),
1018 if (v
& VIRTIO_NET_S_ANNOUNCE
) {
1019 netdev_notify_peers(vi
->dev
);
1020 virtnet_ack_link_announce(vi
);
1023 /* Ignore unknown (future) status bits */
1024 v
&= VIRTIO_NET_S_LINK_UP
;
1026 if (vi
->status
== v
)
1031 if (vi
->status
& VIRTIO_NET_S_LINK_UP
) {
1032 netif_carrier_on(vi
->dev
);
1033 netif_wake_queue(vi
->dev
);
1035 netif_carrier_off(vi
->dev
);
1036 netif_stop_queue(vi
->dev
);
1039 mutex_unlock(&vi
->config_lock
);
1042 static void virtnet_config_changed(struct virtio_device
*vdev
)
1044 struct virtnet_info
*vi
= vdev
->priv
;
1046 schedule_work(&vi
->config_work
);
1049 static void virtnet_del_vqs(struct virtnet_info
*vi
)
1051 struct virtio_device
*vdev
= vi
->vdev
;
1053 vdev
->config
->del_vqs(vdev
);
1056 static int init_vqs(struct virtnet_info
*vi
)
1058 struct virtqueue
*vqs
[3];
1059 vq_callback_t
*callbacks
[] = { skb_recv_done
, skb_xmit_done
, NULL
};
1060 const char *names
[] = { "input", "output", "control" };
1063 /* We expect two virtqueues, receive then send,
1064 * and optionally control. */
1065 nvqs
= virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VQ
) ? 3 : 2;
1067 err
= vi
->vdev
->config
->find_vqs(vi
->vdev
, nvqs
, vqs
, callbacks
, names
);
1074 if (virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VQ
)) {
1077 if (virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_CTRL_VLAN
))
1078 vi
->dev
->features
|= NETIF_F_HW_VLAN_FILTER
;
1083 static int virtnet_probe(struct virtio_device
*vdev
)
1086 struct net_device
*dev
;
1087 struct virtnet_info
*vi
;
1089 /* Allocate ourselves a network device with room for our info */
1090 dev
= alloc_etherdev(sizeof(struct virtnet_info
));
1094 /* Set up network device as normal. */
1095 dev
->priv_flags
|= IFF_UNICAST_FLT
| IFF_LIVE_ADDR_CHANGE
;
1096 dev
->netdev_ops
= &virtnet_netdev
;
1097 dev
->features
= NETIF_F_HIGHDMA
;
1099 SET_ETHTOOL_OPS(dev
, &virtnet_ethtool_ops
);
1100 SET_NETDEV_DEV(dev
, &vdev
->dev
);
1102 /* Do we support "hardware" checksums? */
1103 if (virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
)) {
1104 /* This opens up the world of extra features. */
1105 dev
->hw_features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
1107 dev
->features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
1109 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GSO
)) {
1110 dev
->hw_features
|= NETIF_F_TSO
| NETIF_F_UFO
1111 | NETIF_F_TSO_ECN
| NETIF_F_TSO6
;
1113 /* Individual feature bits: what can host handle? */
1114 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO4
))
1115 dev
->hw_features
|= NETIF_F_TSO
;
1116 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO6
))
1117 dev
->hw_features
|= NETIF_F_TSO6
;
1118 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_ECN
))
1119 dev
->hw_features
|= NETIF_F_TSO_ECN
;
1120 if (virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_UFO
))
1121 dev
->hw_features
|= NETIF_F_UFO
;
1124 dev
->features
|= dev
->hw_features
& (NETIF_F_ALL_TSO
|NETIF_F_UFO
);
1125 /* (!csum && gso) case will be fixed by register_netdev() */
1128 /* Configuration may specify what MAC to use. Otherwise random. */
1129 if (virtio_config_val_len(vdev
, VIRTIO_NET_F_MAC
,
1130 offsetof(struct virtio_net_config
, mac
),
1131 dev
->dev_addr
, dev
->addr_len
) < 0)
1132 eth_hw_addr_random(dev
);
1134 /* Set up our device-specific information */
1135 vi
= netdev_priv(dev
);
1136 netif_napi_add(dev
, &vi
->rq
.napi
, virtnet_poll
, napi_weight
);
1140 vi
->rq
.pages
= NULL
;
1141 vi
->stats
= alloc_percpu(struct virtnet_stats
);
1143 if (vi
->stats
== NULL
)
1146 INIT_DELAYED_WORK(&vi
->refill
, refill_work
);
1147 mutex_init(&vi
->config_lock
);
1148 vi
->config_enable
= true;
1149 INIT_WORK(&vi
->config_work
, virtnet_config_changed_work
);
1150 sg_init_table(vi
->rq
.sg
, ARRAY_SIZE(vi
->rq
.sg
));
1151 sg_init_table(vi
->sq
.sg
, ARRAY_SIZE(vi
->sq
.sg
));
1153 /* If we can receive ANY GSO packets, we must allocate large ones. */
1154 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO4
) ||
1155 virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO6
) ||
1156 virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_ECN
))
1157 vi
->big_packets
= true;
1159 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MRG_RXBUF
))
1160 vi
->mergeable_rx_bufs
= true;
1166 err
= register_netdev(dev
);
1168 pr_debug("virtio_net: registering device failed\n");
1172 /* Last of all, set up some receive buffers. */
1173 try_fill_recv(&vi
->rq
, GFP_KERNEL
);
1175 /* If we didn't even get one input buffer, we're useless. */
1176 if (vi
->rq
.num
== 0) {
1181 /* Assume link up if device can't report link status,
1182 otherwise get link status from config. */
1183 if (virtio_has_feature(vi
->vdev
, VIRTIO_NET_F_STATUS
)) {
1184 netif_carrier_off(dev
);
1185 schedule_work(&vi
->config_work
);
1187 vi
->status
= VIRTIO_NET_S_LINK_UP
;
1188 netif_carrier_on(dev
);
1191 pr_debug("virtnet: registered device %s\n", dev
->name
);
1195 unregister_netdev(dev
);
1197 virtnet_del_vqs(vi
);
1199 free_percpu(vi
->stats
);
1205 static void free_unused_bufs(struct virtnet_info
*vi
)
1209 buf
= virtqueue_detach_unused_buf(vi
->sq
.vq
);
1215 buf
= virtqueue_detach_unused_buf(vi
->rq
.vq
);
1218 if (vi
->mergeable_rx_bufs
|| vi
->big_packets
)
1219 give_pages(&vi
->rq
, buf
);
1224 BUG_ON(vi
->rq
.num
!= 0);
1227 static void remove_vq_common(struct virtnet_info
*vi
)
1229 vi
->vdev
->config
->reset(vi
->vdev
);
1231 /* Free unused buffers in both send and recv, if any. */
1232 free_unused_bufs(vi
);
1234 virtnet_del_vqs(vi
);
1236 while (vi
->rq
.pages
)
1237 __free_pages(get_a_page(&vi
->rq
, GFP_KERNEL
), 0);
1240 static void virtnet_remove(struct virtio_device
*vdev
)
1242 struct virtnet_info
*vi
= vdev
->priv
;
1244 /* Prevent config work handler from accessing the device. */
1245 mutex_lock(&vi
->config_lock
);
1246 vi
->config_enable
= false;
1247 mutex_unlock(&vi
->config_lock
);
1249 unregister_netdev(vi
->dev
);
1251 remove_vq_common(vi
);
1253 flush_work(&vi
->config_work
);
1255 free_percpu(vi
->stats
);
1256 free_netdev(vi
->dev
);
1260 static int virtnet_freeze(struct virtio_device
*vdev
)
1262 struct virtnet_info
*vi
= vdev
->priv
;
1264 /* Prevent config work handler from accessing the device */
1265 mutex_lock(&vi
->config_lock
);
1266 vi
->config_enable
= false;
1267 mutex_unlock(&vi
->config_lock
);
1269 netif_device_detach(vi
->dev
);
1270 cancel_delayed_work_sync(&vi
->refill
);
1272 if (netif_running(vi
->dev
))
1273 napi_disable(&vi
->rq
.napi
);
1275 remove_vq_common(vi
);
1277 flush_work(&vi
->config_work
);
1282 static int virtnet_restore(struct virtio_device
*vdev
)
1284 struct virtnet_info
*vi
= vdev
->priv
;
1291 if (netif_running(vi
->dev
))
1292 virtnet_napi_enable(&vi
->rq
);
1294 netif_device_attach(vi
->dev
);
1296 if (!try_fill_recv(&vi
->rq
, GFP_KERNEL
))
1297 schedule_delayed_work(&vi
->refill
, 0);
1299 mutex_lock(&vi
->config_lock
);
1300 vi
->config_enable
= true;
1301 mutex_unlock(&vi
->config_lock
);
1307 static struct virtio_device_id id_table
[] = {
1308 { VIRTIO_ID_NET
, VIRTIO_DEV_ANY_ID
},
1312 static unsigned int features
[] = {
1313 VIRTIO_NET_F_CSUM
, VIRTIO_NET_F_GUEST_CSUM
,
1314 VIRTIO_NET_F_GSO
, VIRTIO_NET_F_MAC
,
1315 VIRTIO_NET_F_HOST_TSO4
, VIRTIO_NET_F_HOST_UFO
, VIRTIO_NET_F_HOST_TSO6
,
1316 VIRTIO_NET_F_HOST_ECN
, VIRTIO_NET_F_GUEST_TSO4
, VIRTIO_NET_F_GUEST_TSO6
,
1317 VIRTIO_NET_F_GUEST_ECN
, VIRTIO_NET_F_GUEST_UFO
,
1318 VIRTIO_NET_F_MRG_RXBUF
, VIRTIO_NET_F_STATUS
, VIRTIO_NET_F_CTRL_VQ
,
1319 VIRTIO_NET_F_CTRL_RX
, VIRTIO_NET_F_CTRL_VLAN
,
1320 VIRTIO_NET_F_GUEST_ANNOUNCE
,
1323 static struct virtio_driver virtio_net_driver
= {
1324 .feature_table
= features
,
1325 .feature_table_size
= ARRAY_SIZE(features
),
1326 .driver
.name
= KBUILD_MODNAME
,
1327 .driver
.owner
= THIS_MODULE
,
1328 .id_table
= id_table
,
1329 .probe
= virtnet_probe
,
1330 .remove
= virtnet_remove
,
1331 .config_changed
= virtnet_config_changed
,
1333 .freeze
= virtnet_freeze
,
1334 .restore
= virtnet_restore
,
1338 static int __init
init(void)
1340 return register_virtio_driver(&virtio_net_driver
);
1343 static void __exit
fini(void)
1345 unregister_virtio_driver(&virtio_net_driver
);
1350 MODULE_DEVICE_TABLE(virtio
, id_table
);
1351 MODULE_DESCRIPTION("Virtio network driver");
1352 MODULE_LICENSE("GPL");