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
;
66 static inline struct virtio_net_hdr
*skb_vnet_hdr(struct sk_buff
*skb
)
68 return (struct virtio_net_hdr
*)skb
->cb
;
71 static inline void vnet_hdr_to_sg(struct scatterlist
*sg
, struct sk_buff
*skb
)
73 sg_init_one(sg
, skb_vnet_hdr(skb
), sizeof(struct virtio_net_hdr
));
76 static void skb_xmit_done(struct virtqueue
*svq
)
78 struct virtnet_info
*vi
= svq
->vdev
->priv
;
80 /* Suppress further interrupts. */
81 svq
->vq_ops
->disable_cb(svq
);
83 /* We were probably waiting for more output buffers. */
84 netif_wake_queue(vi
->dev
);
86 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
87 * queued, start_xmit won't be called. */
88 tasklet_schedule(&vi
->tasklet
);
91 static void receive_skb(struct net_device
*dev
, struct sk_buff
*skb
,
94 struct virtio_net_hdr
*hdr
= skb_vnet_hdr(skb
);
97 if (unlikely(len
< sizeof(struct virtio_net_hdr
) + ETH_HLEN
)) {
98 pr_debug("%s: short packet %i\n", dev
->name
, len
);
99 dev
->stats
.rx_length_errors
++;
102 len
-= sizeof(struct virtio_net_hdr
);
104 err
= pskb_trim(skb
, len
);
106 pr_debug("%s: pskb_trim failed %i %d\n", dev
->name
, len
, err
);
107 dev
->stats
.rx_dropped
++;
110 skb
->truesize
+= skb
->data_len
;
111 dev
->stats
.rx_bytes
+= skb
->len
;
112 dev
->stats
.rx_packets
++;
114 if (hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
115 pr_debug("Needs csum!\n");
116 if (!skb_partial_csum_set(skb
,hdr
->csum_start
,hdr
->csum_offset
))
120 skb
->protocol
= eth_type_trans(skb
, dev
);
121 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
122 ntohs(skb
->protocol
), skb
->len
, skb
->pkt_type
);
124 if (hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
126 switch (hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
127 case VIRTIO_NET_HDR_GSO_TCPV4
:
128 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV4
;
130 case VIRTIO_NET_HDR_GSO_UDP
:
131 skb_shinfo(skb
)->gso_type
= SKB_GSO_UDP
;
133 case VIRTIO_NET_HDR_GSO_TCPV6
:
134 skb_shinfo(skb
)->gso_type
= SKB_GSO_TCPV6
;
138 printk(KERN_WARNING
"%s: bad gso type %u.\n",
139 dev
->name
, hdr
->gso_type
);
143 if (hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
144 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TCP_ECN
;
146 skb_shinfo(skb
)->gso_size
= hdr
->gso_size
;
147 if (skb_shinfo(skb
)->gso_size
== 0) {
149 printk(KERN_WARNING
"%s: zero gso size.\n",
154 /* Header must be checked, and gso_segs computed. */
155 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
156 skb_shinfo(skb
)->gso_segs
= 0;
159 netif_receive_skb(skb
);
163 dev
->stats
.rx_frame_errors
++;
168 static void try_fill_recv(struct virtnet_info
*vi
)
171 struct scatterlist sg
[2+MAX_SKB_FRAGS
];
174 sg_init_table(sg
, 2+MAX_SKB_FRAGS
);
176 skb
= netdev_alloc_skb(vi
->dev
, MAX_PACKET_LEN
);
180 skb_put(skb
, MAX_PACKET_LEN
);
181 vnet_hdr_to_sg(sg
, skb
);
183 if (vi
->big_packets
) {
184 for (i
= 0; i
< MAX_SKB_FRAGS
; i
++) {
185 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
186 f
->page
= alloc_page(GFP_ATOMIC
);
193 skb
->data_len
+= PAGE_SIZE
;
194 skb
->len
+= PAGE_SIZE
;
196 skb_shinfo(skb
)->nr_frags
++;
200 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
201 skb_queue_head(&vi
->recv
, skb
);
203 err
= vi
->rvq
->vq_ops
->add_buf(vi
->rvq
, sg
, 0, num
, skb
);
205 skb_unlink(skb
, &vi
->recv
);
211 if (unlikely(vi
->num
> vi
->max
))
213 vi
->rvq
->vq_ops
->kick(vi
->rvq
);
216 static void skb_recv_done(struct virtqueue
*rvq
)
218 struct virtnet_info
*vi
= rvq
->vdev
->priv
;
219 /* Schedule NAPI, Suppress further interrupts if successful. */
220 if (netif_rx_schedule_prep(vi
->dev
, &vi
->napi
)) {
221 rvq
->vq_ops
->disable_cb(rvq
);
222 __netif_rx_schedule(vi
->dev
, &vi
->napi
);
226 static int virtnet_poll(struct napi_struct
*napi
, int budget
)
228 struct virtnet_info
*vi
= container_of(napi
, struct virtnet_info
, napi
);
229 struct sk_buff
*skb
= NULL
;
230 unsigned int len
, received
= 0;
233 while (received
< budget
&&
234 (skb
= vi
->rvq
->vq_ops
->get_buf(vi
->rvq
, &len
)) != NULL
) {
235 __skb_unlink(skb
, &vi
->recv
);
236 receive_skb(vi
->dev
, skb
, len
);
241 /* FIXME: If we oom and completely run out of inbufs, we need
242 * to start a timer trying to fill more. */
243 if (vi
->num
< vi
->max
/ 2)
246 /* Out of packets? */
247 if (received
< budget
) {
248 netif_rx_complete(vi
->dev
, napi
);
249 if (unlikely(!vi
->rvq
->vq_ops
->enable_cb(vi
->rvq
))
250 && napi_schedule_prep(napi
)) {
251 vi
->rvq
->vq_ops
->disable_cb(vi
->rvq
);
252 __netif_rx_schedule(vi
->dev
, napi
);
260 static void free_old_xmit_skbs(struct virtnet_info
*vi
)
265 while ((skb
= vi
->svq
->vq_ops
->get_buf(vi
->svq
, &len
)) != NULL
) {
266 pr_debug("Sent skb %p\n", skb
);
267 __skb_unlink(skb
, &vi
->send
);
268 vi
->dev
->stats
.tx_bytes
+= skb
->len
;
269 vi
->dev
->stats
.tx_packets
++;
274 /* If the virtio transport doesn't always notify us when all in-flight packets
275 * are consumed, we fall back to using this function on a timer to free them. */
276 static void xmit_free(unsigned long data
)
278 struct virtnet_info
*vi
= (void *)data
;
280 netif_tx_lock(vi
->dev
);
282 free_old_xmit_skbs(vi
);
284 if (!skb_queue_empty(&vi
->send
))
285 mod_timer(&vi
->xmit_free_timer
, jiffies
+ (HZ
/10));
287 netif_tx_unlock(vi
->dev
);
290 static int xmit_skb(struct virtnet_info
*vi
, struct sk_buff
*skb
)
293 struct scatterlist sg
[2+MAX_SKB_FRAGS
];
294 struct virtio_net_hdr
*hdr
;
295 const unsigned char *dest
= ((struct ethhdr
*)skb
->data
)->h_dest
;
297 sg_init_table(sg
, 2+MAX_SKB_FRAGS
);
299 pr_debug("%s: xmit %p " MAC_FMT
"\n", vi
->dev
->name
, skb
,
300 dest
[0], dest
[1], dest
[2],
301 dest
[3], dest
[4], dest
[5]);
303 /* Encode metadata header at front. */
304 hdr
= skb_vnet_hdr(skb
);
305 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
306 hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
307 hdr
->csum_start
= skb
->csum_start
- skb_headroom(skb
);
308 hdr
->csum_offset
= skb
->csum_offset
;
311 hdr
->csum_offset
= hdr
->csum_start
= 0;
314 if (skb_is_gso(skb
)) {
315 hdr
->hdr_len
= skb_transport_header(skb
) - skb
->data
;
316 hdr
->gso_size
= skb_shinfo(skb
)->gso_size
;
317 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV4
)
318 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
319 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCPV6
)
320 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
321 else if (skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
)
322 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
325 if (skb_shinfo(skb
)->gso_type
& SKB_GSO_TCP_ECN
)
326 hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
328 hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
329 hdr
->gso_size
= hdr
->hdr_len
= 0;
332 vnet_hdr_to_sg(sg
, skb
);
333 num
= skb_to_sgvec(skb
, sg
+1, 0, skb
->len
) + 1;
335 err
= vi
->svq
->vq_ops
->add_buf(vi
->svq
, sg
, num
, 0, skb
);
336 if (!err
&& !vi
->free_in_tasklet
)
337 mod_timer(&vi
->xmit_free_timer
, jiffies
+ (HZ
/10));
342 static void xmit_tasklet(unsigned long data
)
344 struct virtnet_info
*vi
= (void *)data
;
346 netif_tx_lock_bh(vi
->dev
);
347 if (vi
->last_xmit_skb
&& xmit_skb(vi
, vi
->last_xmit_skb
) == 0) {
348 vi
->svq
->vq_ops
->kick(vi
->svq
);
349 vi
->last_xmit_skb
= NULL
;
351 if (vi
->free_in_tasklet
)
352 free_old_xmit_skbs(vi
);
353 netif_tx_unlock_bh(vi
->dev
);
356 static int start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
358 struct virtnet_info
*vi
= netdev_priv(dev
);
361 /* Free up any pending old buffers before queueing new ones. */
362 free_old_xmit_skbs(vi
);
364 /* If we has a buffer left over from last time, send it now. */
365 if (unlikely(vi
->last_xmit_skb
) &&
366 xmit_skb(vi
, vi
->last_xmit_skb
) != 0)
369 vi
->last_xmit_skb
= NULL
;
371 /* Put new one in send queue and do transmit */
373 __skb_queue_head(&vi
->send
, skb
);
374 if (xmit_skb(vi
, skb
) != 0) {
375 vi
->last_xmit_skb
= skb
;
381 vi
->svq
->vq_ops
->kick(vi
->svq
);
385 pr_debug("%s: virtio not prepared to send\n", dev
->name
);
386 netif_stop_queue(dev
);
388 /* Activate callback for using skbs: if this returns false it
389 * means some were used in the meantime. */
390 if (unlikely(!vi
->svq
->vq_ops
->enable_cb(vi
->svq
))) {
391 vi
->svq
->vq_ops
->disable_cb(vi
->svq
);
392 netif_start_queue(dev
);
396 /* Drop this skb: we only queue one. */
397 vi
->dev
->stats
.tx_dropped
++;
403 #ifdef CONFIG_NET_POLL_CONTROLLER
404 static void virtnet_netpoll(struct net_device
*dev
)
406 struct virtnet_info
*vi
= netdev_priv(dev
);
408 napi_schedule(&vi
->napi
);
412 static int virtnet_open(struct net_device
*dev
)
414 struct virtnet_info
*vi
= netdev_priv(dev
);
416 napi_enable(&vi
->napi
);
418 /* If all buffers were filled by other side before we napi_enabled, we
419 * won't get another interrupt, so process any outstanding packets
420 * now. virtnet_poll wants re-enable the queue, so we disable here.
421 * We synchronize against interrupts via NAPI_STATE_SCHED */
422 if (netif_rx_schedule_prep(dev
, &vi
->napi
)) {
423 vi
->rvq
->vq_ops
->disable_cb(vi
->rvq
);
424 __netif_rx_schedule(dev
, &vi
->napi
);
429 static int virtnet_close(struct net_device
*dev
)
431 struct virtnet_info
*vi
= netdev_priv(dev
);
433 napi_disable(&vi
->napi
);
438 static int virtnet_set_tx_csum(struct net_device
*dev
, u32 data
)
440 struct virtnet_info
*vi
= netdev_priv(dev
);
441 struct virtio_device
*vdev
= vi
->vdev
;
443 if (data
&& !virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
))
446 return ethtool_op_set_tx_hw_csum(dev
, data
);
449 static struct ethtool_ops virtnet_ethtool_ops
= {
450 .set_tx_csum
= virtnet_set_tx_csum
,
451 .set_sg
= ethtool_op_set_sg
,
454 static int virtnet_probe(struct virtio_device
*vdev
)
457 struct net_device
*dev
;
458 struct virtnet_info
*vi
;
460 /* Allocate ourselves a network device with room for our info */
461 dev
= alloc_etherdev(sizeof(struct virtnet_info
));
465 /* Set up network device as normal. */
466 dev
->open
= virtnet_open
;
467 dev
->stop
= virtnet_close
;
468 dev
->hard_start_xmit
= start_xmit
;
469 dev
->features
= NETIF_F_HIGHDMA
;
470 #ifdef CONFIG_NET_POLL_CONTROLLER
471 dev
->poll_controller
= virtnet_netpoll
;
473 SET_ETHTOOL_OPS(dev
, &virtnet_ethtool_ops
);
474 SET_NETDEV_DEV(dev
, &vdev
->dev
);
476 /* Do we support "hardware" checksums? */
477 if (csum
&& virtio_has_feature(vdev
, VIRTIO_NET_F_CSUM
)) {
478 /* This opens up the world of extra features. */
479 dev
->features
|= NETIF_F_HW_CSUM
|NETIF_F_SG
|NETIF_F_FRAGLIST
;
480 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_GSO
)) {
481 dev
->features
|= NETIF_F_TSO
| NETIF_F_UFO
482 | NETIF_F_TSO_ECN
| NETIF_F_TSO6
;
484 /* Individual feature bits: what can host handle? */
485 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO4
))
486 dev
->features
|= NETIF_F_TSO
;
487 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_TSO6
))
488 dev
->features
|= NETIF_F_TSO6
;
489 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_ECN
))
490 dev
->features
|= NETIF_F_TSO_ECN
;
491 if (gso
&& virtio_has_feature(vdev
, VIRTIO_NET_F_HOST_UFO
))
492 dev
->features
|= NETIF_F_UFO
;
495 /* Configuration may specify what MAC to use. Otherwise random. */
496 if (virtio_has_feature(vdev
, VIRTIO_NET_F_MAC
)) {
497 vdev
->config
->get(vdev
,
498 offsetof(struct virtio_net_config
, mac
),
499 dev
->dev_addr
, dev
->addr_len
);
501 random_ether_addr(dev
->dev_addr
);
503 /* Set up our device-specific information */
504 vi
= netdev_priv(dev
);
505 netif_napi_add(dev
, &vi
->napi
, virtnet_poll
, napi_weight
);
510 /* If they give us a callback when all buffers are done, we don't need
512 vi
->free_in_tasklet
= virtio_has_feature(vdev
,VIRTIO_F_NOTIFY_ON_EMPTY
);
514 /* If we can receive ANY GSO packets, we must allocate large ones. */
515 if (virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO4
)
516 || virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_TSO6
)
517 || virtio_has_feature(vdev
, VIRTIO_NET_F_GUEST_ECN
))
518 vi
->big_packets
= true;
520 /* We expect two virtqueues, receive then send. */
521 vi
->rvq
= vdev
->config
->find_vq(vdev
, 0, skb_recv_done
);
522 if (IS_ERR(vi
->rvq
)) {
523 err
= PTR_ERR(vi
->rvq
);
527 vi
->svq
= vdev
->config
->find_vq(vdev
, 1, skb_xmit_done
);
528 if (IS_ERR(vi
->svq
)) {
529 err
= PTR_ERR(vi
->svq
);
533 /* Initialize our empty receive and send queues. */
534 skb_queue_head_init(&vi
->recv
);
535 skb_queue_head_init(&vi
->send
);
537 tasklet_init(&vi
->tasklet
, xmit_tasklet
, (unsigned long)vi
);
539 if (!vi
->free_in_tasklet
)
540 setup_timer(&vi
->xmit_free_timer
, xmit_free
, (unsigned long)vi
);
542 err
= register_netdev(dev
);
544 pr_debug("virtio_net: registering device failed\n");
548 /* Last of all, set up some receive buffers. */
551 /* If we didn't even get one input buffer, we're useless. */
557 pr_debug("virtnet: registered device %s\n", dev
->name
);
561 unregister_netdev(dev
);
563 vdev
->config
->del_vq(vi
->svq
);
565 vdev
->config
->del_vq(vi
->rvq
);
571 static void virtnet_remove(struct virtio_device
*vdev
)
573 struct virtnet_info
*vi
= vdev
->priv
;
576 /* Stop all the virtqueues. */
577 vdev
->config
->reset(vdev
);
579 if (!vi
->free_in_tasklet
)
580 del_timer_sync(&vi
->xmit_free_timer
);
582 /* Free our skbs in send and recv queues, if any. */
583 while ((skb
= __skb_dequeue(&vi
->recv
)) != NULL
) {
587 __skb_queue_purge(&vi
->send
);
589 BUG_ON(vi
->num
!= 0);
591 vdev
->config
->del_vq(vi
->svq
);
592 vdev
->config
->del_vq(vi
->rvq
);
593 unregister_netdev(vi
->dev
);
594 free_netdev(vi
->dev
);
597 static struct virtio_device_id id_table
[] = {
598 { VIRTIO_ID_NET
, VIRTIO_DEV_ANY_ID
},
602 static unsigned int features
[] = {
603 VIRTIO_NET_F_CSUM
, VIRTIO_NET_F_GUEST_CSUM
,
604 VIRTIO_NET_F_GSO
, VIRTIO_NET_F_MAC
,
605 VIRTIO_NET_F_HOST_TSO4
, VIRTIO_NET_F_HOST_UFO
, VIRTIO_NET_F_HOST_TSO6
,
606 VIRTIO_NET_F_HOST_ECN
, VIRTIO_NET_F_GUEST_TSO4
, VIRTIO_NET_F_GUEST_TSO6
,
607 VIRTIO_NET_F_GUEST_ECN
, /* We don't yet handle UFO input. */
608 VIRTIO_F_NOTIFY_ON_EMPTY
,
611 static struct virtio_driver virtio_net
= {
612 .feature_table
= features
,
613 .feature_table_size
= ARRAY_SIZE(features
),
614 .driver
.name
= KBUILD_MODNAME
,
615 .driver
.owner
= THIS_MODULE
,
616 .id_table
= id_table
,
617 .probe
= virtnet_probe
,
618 .remove
= __devexit_p(virtnet_remove
),
621 static int __init
init(void)
623 return register_virtio_driver(&virtio_net
);
626 static void __exit
fini(void)
628 unregister_virtio_driver(&virtio_net
);
633 MODULE_DEVICE_TABLE(virtio
, id_table
);
634 MODULE_DESCRIPTION("Virtio network driver");
635 MODULE_LICENSE("GPL");