1 /* A simple 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>
28 static int napi_weight
= 128;
29 module_param(napi_weight
, int, 0444);
31 static int csum
= 1, gso
= 1;
32 module_param(csum
, bool, 0444);
33 module_param(gso
, bool, 0444);
35 /* FIXME: MTU in config. */
36 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
40 struct virtio_device
*vdev
;
41 struct virtqueue
*rvq
, *svq
;
42 struct net_device
*dev
;
43 struct napi_struct napi
;
45 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff
*last_xmit_skb
;
48 /* If we need to free in a timer, this is it. */
49 struct timer_list xmit_free_timer
;
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num
, max
;
54 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet
;
58 /* I like... big packets and I cannot lie! */
61 /* Receive & send queues. */
62 struct sk_buff_head recv
;
63 struct sk_buff_head send
;
65 /* Chain pages by the private ptr. */
69 static inline struct virtio_net_hdr
*skb_vnet_hdr(struct sk_buff
*skb
)
71 return (struct virtio_net_hdr
*)skb
->cb
;
74 static inline void vnet_hdr_to_sg(struct scatterlist
*sg
, struct sk_buff
*skb
)
76 sg_init_one(sg
, skb_vnet_hdr(skb
), sizeof(struct virtio_net_hdr
));
79 static void give_a_page(struct virtnet_info
*vi
, struct page
*page
)
81 page
->private = (unsigned long)vi
->pages
;
85 static struct page
*get_a_page(struct virtnet_info
*vi
, gfp_t gfp_mask
)
87 struct page
*p
= vi
->pages
;
90 vi
->pages
= (struct page
*)p
->private;
92 p
= alloc_page(gfp_mask
);
96 static void skb_xmit_done(struct virtqueue
*svq
)
98 struct virtnet_info
*vi
= svq
->vdev
->priv
;
100 /* Suppress further interrupts. */
101 svq
->vq_ops
->disable_cb(svq
);
103 /* We were probably waiting for more output buffers. */
104 netif_wake_queue(vi
->dev
);
106 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
107 * queued, start_xmit won't be called. */
108 tasklet_schedule(&vi
->tasklet
);
111 static void receive_skb(struct net_device
*dev
, struct sk_buff
*skb
,
114 struct virtio_net_hdr
*hdr
= skb_vnet_hdr(skb
);
117 if (unlikely(len
< sizeof(struct virtio_net_hdr
) + ETH_HLEN
)) {
118 pr_debug("%s: short packet %i\n", dev
->name
, len
);
119 dev
->stats
.rx_length_errors
++;
122 len
-= sizeof(struct virtio_net_hdr
);
124 if (len
<= MAX_PACKET_LEN
) {
127 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++)
128 give_a_page(dev
->priv
, skb_shinfo(skb
)->frags
[i
].page
);
130 skb_shinfo(skb
)->nr_frags
= 0;
133 err
= pskb_trim(skb
, len
);
135 pr_debug("%s: pskb_trim failed %i %d\n", dev
->name
, len
, err
);
136 dev
->stats
.rx_dropped
++;
139 skb
->truesize
+= skb
->data_len
;
140 dev
->stats
.rx_bytes
+= skb
->len
;
141 dev
->stats
.rx_packets
++;
143 if (hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
144 pr_debug("Needs csum!\n");
145 if (!skb_partial_csum_set(skb
,hdr
->csum_start
,hdr
->csum_offset
))
149 skb
->protocol
= eth_type_trans(skb
, dev
);
150 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
151 ntohs(skb
->protocol
), skb
->len
, skb
->pkt_type
);
153 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
155 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
156 case VIRTIO_NET_HDR_GSO_TCPV4
:
157 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
159 case VIRTIO_NET_HDR_GSO_UDP
:
160 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
162 case VIRTIO_NET_HDR_GSO_TCPV6
:
163 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
167 printk(KERN_WARNING
"%s: bad gso type %u.\n",
168 dev
->name
, hdr
->gso_type
);
172 if (hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
173 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
175 skb_shinfo(skb
)->gso_size
= hdr
->gso_size
;
176 if (skb_shinfo(skb
)->gso_size
== 0) {
178 printk(KERN_WARNING
"%s: zero gso size.\n",
183 /* Header must be checked, and gso_segs computed. */
184 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
185 skb_shinfo(skb
)->gso_segs
= 0;
188 netif_receive_skb(skb
);
192 dev
->stats
.rx_frame_errors
++;
197 static void try_fill_recv(struct virtnet_info
*vi
)
200 struct scatterlist sg
[2+MAX_SKB_FRAGS
];
203 sg_init_table(sg
, 2+MAX_SKB_FRAGS
);
205 skb
= netdev_alloc_skb(vi
->dev
, MAX_PACKET_LEN
);
209 skb_put(skb
, MAX_PACKET_LEN
);
210 vnet_hdr_to_sg(sg
, skb
);
212 if (vi
->big_packets
) {
213 for (i
= 0; i
< MAX_SKB_FRAGS
; i
++) {
214 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
215 f
->page
= get_a_page(vi
, GFP_ATOMIC
);
222 skb
->data_len
+= PAGE_SIZE
;
223 skb
->len
+= PAGE_SIZE
;
225 skb_shinfo(skb
)->nr_frags
++;
229 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
230 skb_queue_head(&vi
->recv
, skb
);
232 err
= vi
->rvq
->vq_ops
->add_buf(vi
->rvq
, sg
, 0, num
, skb
);
234 skb_unlink(skb
, &vi
->recv
);
240 if (unlikely(vi
->num
> vi
->max
))
242 vi
->rvq
->vq_ops
->kick(vi
->rvq
);
245 static void skb_recv_done(struct virtqueue
*rvq
)
247 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
248 /* Schedule NAPI, Suppress further interrupts if successful. */
249 if (netif_rx_schedule_prep(vi
->dev
, &vi
->napi
)) {
250 rvq
->vq_ops
->disable_cb(rvq
);
251 __netif_rx_schedule(vi
->dev
, &vi
->napi
);
255 static int virtnet_poll(struct napi_struct
*napi
, int budget
)
257 struct virtnet_info
*vi
= container_of(napi
, struct virtnet_info
, napi
);
258 struct sk_buff
*skb
= NULL
;
259 unsigned int len
, received
= 0;
262 while (received
< budget
&&
263 (skb
= vi
->rvq
->vq_ops
->get_buf(vi
->rvq
, &len
)) != NULL
) {
264 __skb_unlink(skb
, &vi
->recv
);
265 receive_skb(vi
->dev
, skb
, len
);
270 /* FIXME: If we oom and completely run out of inbufs, we need
271 * to start a timer trying to fill more. */
272 if (vi
->num
< vi
->max
/ 2)
275 /* Out of packets? */
276 if (received
< budget
) {
277 netif_rx_complete(vi
->dev
, napi
);
278 if (unlikely(!vi
->rvq
->vq_ops
->enable_cb(vi
->rvq
))
279 && napi_schedule_prep(napi
)) {
280 vi
->rvq
->vq_ops
->disable_cb(vi
->rvq
);
281 __netif_rx_schedule(vi
->dev
, napi
);
289 static void free_old_xmit_skbs(struct virtnet_info
*vi
)
294 while ((skb
= vi
->svq
->vq_ops
->get_buf(vi
->svq
, &len
)) != NULL
) {
295 pr_debug("Sent skb %p\n", skb
);
296 __skb_unlink(skb
, &vi
->send
);
297 vi
->dev
->stats
.tx_bytes
+= skb
->len
;
298 vi
->dev
->stats
.tx_packets
++;
303 /* If the virtio transport doesn't always notify us when all in-flight packets
304 * are consumed, we fall back to using this function on a timer to free them. */
305 static void xmit_free(unsigned long data
)
307 struct virtnet_info
*vi
= (void *)data
;
309 netif_tx_lock(vi
->dev
);
311 free_old_xmit_skbs(vi
);
313 if (!skb_queue_empty(&vi
->send
))
314 mod_timer(&vi
->xmit_free_timer
, jiffies
+ (HZ
/10));
316 netif_tx_unlock(vi
->dev
);
319 static int xmit_skb(struct virtnet_info
*vi
, struct sk_buff
*skb
)
322 struct scatterlist sg
[2+MAX_SKB_FRAGS
];
323 struct virtio_net_hdr
*hdr
;
324 const unsigned char *dest
= ((struct ethhdr
*)skb
->data
)->h_dest
;
326 sg_init_table(sg
, 2+MAX_SKB_FRAGS
);
328 pr_debug("%s: xmit %p %pM\n", vi
->dev
->name
, skb
, dest
);
330 /* Encode metadata header at front. */
331 hdr
= skb_vnet_hdr(skb
);
332 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
333 hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
334 hdr
->csum_start
= skb
->csum_start
- skb_headroom(skb
);
335 hdr
->csum_offset
= skb
->csum_offset
;
338 hdr
->csum_offset
= hdr
->csum_start
= 0;
341 if (skb_is_gso(skb
)) {
342 hdr
->hdr_len
= skb_transport_header(skb
) - skb
->data
;
343 hdr
->gso_size
= skb_shinfo(skb
)->gso_size
;
344 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
345 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
346 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
347 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
348 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
349 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
352 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCP_ECN
)
353 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
355 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
356 hdr
->gso_size
= hdr
->hdr_len
= 0;
359 vnet_hdr_to_sg(sg
, skb
);
360 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
362 err
= vi
->svq
->vq_ops
->add_buf(vi
->svq
, sg
, num
, 0, skb
);
363 if (!err
&& !vi
->free_in_tasklet
)
364 mod_timer(&vi
->xmit_free_timer
, jiffies
+ (HZ
/10));
369 static void xmit_tasklet(unsigned long data
)
371 struct virtnet_info
*vi
= (void *)data
;
373 netif_tx_lock_bh(vi
->dev
);
374 if (vi
->last_xmit_skb
&& xmit_skb(vi
, vi
->last_xmit_skb
) == 0) {
375 vi
->svq
->vq_ops
->kick(vi
->svq
);
376 vi
->last_xmit_skb
= NULL
;
378 if (vi
->free_in_tasklet
)
379 free_old_xmit_skbs(vi
);
380 netif_tx_unlock_bh(vi
->dev
);
383 static int start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
385 struct virtnet_info
*vi
= netdev_priv(dev
);
388 /* Free up any pending old buffers before queueing new ones. */
389 free_old_xmit_skbs(vi
);
391 /* If we has a buffer left over from last time, send it now. */
392 if (unlikely(vi
->last_xmit_skb
) &&
393 xmit_skb(vi
, vi
->last_xmit_skb
) != 0)
396 vi
->last_xmit_skb
= NULL
;
398 /* Put new one in send queue and do transmit */
400 __skb_queue_head(&vi
->send
, skb
);
401 if (xmit_skb(vi
, skb
) != 0) {
402 vi
->last_xmit_skb
= skb
;
408 vi
->svq
->vq_ops
->kick(vi
->svq
);
412 pr_debug("%s: virtio not prepared to send\n", dev
->name
);
413 netif_stop_queue(dev
);
415 /* Activate callback for using skbs: if this returns false it
416 * means some were used in the meantime. */
417 if (unlikely(!vi
->svq
->vq_ops
->enable_cb(vi
->svq
))) {
418 vi
->svq
->vq_ops
->disable_cb(vi
->svq
);
419 netif_start_queue(dev
);
423 /* Drop this skb: we only queue one. */
424 vi
->dev
->stats
.tx_dropped
++;
430 #ifdef CONFIG_NET_POLL_CONTROLLER
431 static void virtnet_netpoll(struct net_device
*dev
)
433 struct virtnet_info
*vi
= netdev_priv(dev
);
435 napi_schedule(&vi
->napi
);
439 static int virtnet_open(struct net_device
*dev
)
441 struct virtnet_info
*vi
= netdev_priv(dev
);
443 napi_enable(&vi
->napi
);
445 /* If all buffers were filled by other side before we napi_enabled, we
446 * won't get another interrupt, so process any outstanding packets
447 * now. virtnet_poll wants re-enable the queue, so we disable here.
448 * We synchronize against interrupts via NAPI_STATE_SCHED */
449 if (netif_rx_schedule_prep(dev
, &vi
->napi
)) {
450 vi
->rvq
->vq_ops
->disable_cb(vi
->rvq
);
451 __netif_rx_schedule(dev
, &vi
->napi
);
456 static int virtnet_close(struct net_device
*dev
)
458 struct virtnet_info
*vi
= netdev_priv(dev
);
460 napi_disable(&vi
->napi
);
465 static int virtnet_set_tx_csum(struct net_device
*dev
, u32 data
)
467 struct virtnet_info
*vi
= netdev_priv(dev
);
468 struct virtio_device
*vdev
= vi
->vdev
;
470 if (data
&& !virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
))
473 return ethtool_op_set_tx_hw_csum(dev
, data
);
476 static struct ethtool_ops virtnet_ethtool_ops
= {
477 .set_tx_csum
= virtnet_set_tx_csum
,
478 .set_sg
= ethtool_op_set_sg
,
481 static int virtnet_probe(struct virtio_device
*vdev
)
484 struct net_device
*dev
;
485 struct virtnet_info
*vi
;
487 /* Allocate ourselves a network device with room for our info */
488 dev
= alloc_etherdev(sizeof(struct virtnet_info
));
492 /* Set up network device as normal. */
493 dev
->open
= virtnet_open
;
494 dev
->stop
= virtnet_close
;
495 dev
->hard_start_xmit
= start_xmit
;
496 dev
->features
= NETIF_F_HIGHDMA
;
497 #ifdef CONFIG_NET_POLL_CONTROLLER
498 dev
->poll_controller
= virtnet_netpoll
;
500 SET_ETHTOOL_OPS(dev
, &virtnet_ethtool_ops
);
501 SET_NETDEV_DEV(dev
, &vdev
->dev
);
503 /* Do we support "hardware" checksums? */
504 if (csum
&& virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
)) {
505 /* This opens up the world of extra features. */
506 dev
->features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
507 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_GSO
)) {
508 dev
->features
|= NETIF_F_TSO
| NETIF_F_UFO
509 | NETIF_F_TSO_ECN
| NETIF_F_TSO6
;
511 /* Individual feature bits: what can host handle? */
512 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO4
))
513 dev
->features
|= NETIF_F_TSO
;
514 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO6
))
515 dev
->features
|= NETIF_F_TSO6
;
516 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_ECN
))
517 dev
->features
|= NETIF_F_TSO_ECN
;
518 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_UFO
))
519 dev
->features
|= NETIF_F_UFO
;
522 /* Configuration may specify what MAC to use. Otherwise random. */
523 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MAC
)) {
524 vdev
->config
->get(vdev
,
525 offsetof(struct virtio_net_config
, mac
),
526 dev
->dev_addr
, dev
->addr_len
);
528 random_ether_addr(dev
->dev_addr
);
530 /* Set up our device-specific information */
531 vi
= netdev_priv(dev
);
532 netif_napi_add(dev
, &vi
->napi
, virtnet_poll
, napi_weight
);
538 /* If they give us a callback when all buffers are done, we don't need
540 vi
->free_in_tasklet
= virtio_has_feature(vdev
,VIRTIO_F_NOTIFY_ON_EMPTY
);
542 /* If we can receive ANY GSO packets, we must allocate large ones. */
543 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO4
)
544 || virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO6
)
545 || virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_ECN
))
546 vi
->big_packets
= true;
548 /* We expect two virtqueues, receive then send. */
549 vi
->rvq
= vdev
->config
->find_vq(vdev
, 0, skb_recv_done
);
550 if (IS_ERR(vi
->rvq
)) {
551 err
= PTR_ERR(vi
->rvq
);
555 vi
->svq
= vdev
->config
->find_vq(vdev
, 1, skb_xmit_done
);
556 if (IS_ERR(vi
->svq
)) {
557 err
= PTR_ERR(vi
->svq
);
561 /* Initialize our empty receive and send queues. */
562 skb_queue_head_init(&vi
->recv
);
563 skb_queue_head_init(&vi
->send
);
565 tasklet_init(&vi
->tasklet
, xmit_tasklet
, (unsigned long)vi
);
567 if (!vi
->free_in_tasklet
)
568 setup_timer(&vi
->xmit_free_timer
, xmit_free
, (unsigned long)vi
);
570 err
= register_netdev(dev
);
572 pr_debug("virtio_net: registering device failed\n");
576 /* Last of all, set up some receive buffers. */
579 /* If we didn't even get one input buffer, we're useless. */
585 pr_debug("virtnet: registered device %s\n", dev
->name
);
589 unregister_netdev(dev
);
591 vdev
->config
->del_vq(vi
->svq
);
593 vdev
->config
->del_vq(vi
->rvq
);
599 static void virtnet_remove(struct virtio_device
*vdev
)
601 struct virtnet_info
*vi
= vdev
->priv
;
604 /* Stop all the virtqueues. */
605 vdev
->config
->reset(vdev
);
607 if (!vi
->free_in_tasklet
)
608 del_timer_sync(&vi
->xmit_free_timer
);
610 /* Free our skbs in send and recv queues, if any. */
611 while ((skb
= __skb_dequeue(&vi
->recv
)) != NULL
) {
615 __skb_queue_purge(&vi
->send
);
617 BUG_ON(vi
->num
!= 0);
619 vdev
->config
->del_vq(vi
->svq
);
620 vdev
->config
->del_vq(vi
->rvq
);
621 unregister_netdev(vi
->dev
);
624 __free_pages(get_a_page(vi
, GFP_KERNEL
), 0);
626 free_netdev(vi
->dev
);
629 static struct virtio_device_id id_table
[] = {
630 { VIRTIO_ID_NET
, VIRTIO_DEV_ANY_ID
},
634 static unsigned int features
[] = {
635 VIRTIO_NET_F_CSUM
, VIRTIO_NET_F_GUEST_CSUM
,
636 VIRTIO_NET_F_GSO
, VIRTIO_NET_F_MAC
,
637 VIRTIO_NET_F_HOST_TSO4
, VIRTIO_NET_F_HOST_UFO
, VIRTIO_NET_F_HOST_TSO6
,
638 VIRTIO_NET_F_HOST_ECN
, VIRTIO_NET_F_GUEST_TSO4
, VIRTIO_NET_F_GUEST_TSO6
,
639 VIRTIO_NET_F_GUEST_ECN
, /* We don't yet handle UFO input. */
640 VIRTIO_F_NOTIFY_ON_EMPTY
,
643 static struct virtio_driver virtio_net
= {
644 .feature_table
= features
,
645 .feature_table_size
= ARRAY_SIZE(features
),
646 .driver
.name
= KBUILD_MODNAME
,
647 .driver
.owner
= THIS_MODULE
,
648 .id_table
= id_table
,
649 .probe
= virtnet_probe
,
650 .remove
= __devexit_p(virtnet_remove
),
653 static int __init
init(void)
655 return register_virtio_driver(&virtio_net
);
658 static void __exit
fini(void)
660 unregister_virtio_driver(&virtio_net
);
665 MODULE_DEVICE_TABLE(virtio
, id_table
);
666 MODULE_DESCRIPTION("Virtio network driver");
667 MODULE_LICENSE("GPL");