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/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/scatterlist.h>
27 static int napi_weight = 128;
28 module_param(napi_weight, int, 0444);
30 static int csum = 1, gso = 1;
31 module_param(csum, bool, 0444);
32 module_param(gso, bool, 0444);
34 /* FIXME: MTU in config. */
35 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
39 struct virtio_device *vdev;
40 struct virtqueue *rvq, *svq;
41 struct net_device *dev;
42 struct napi_struct napi;
44 /* The skb we couldn't send because buffers were full. */
45 struct sk_buff *last_xmit_skb;
47 /* Number of input buffers, and max we've ever had. */
48 unsigned int num, max;
50 /* For cleaning up after transmission. */
51 struct tasklet_struct tasklet;
53 /* Receive & send queues. */
54 struct sk_buff_head recv;
55 struct sk_buff_head send;
58 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
60 return (struct virtio_net_hdr *)skb->cb;
63 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
65 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
68 static void skb_xmit_done(struct virtqueue *svq)
70 struct virtnet_info *vi = svq->vdev->priv;
72 /* Suppress further interrupts. */
73 svq->vq_ops->disable_cb(svq);
75 /* We were waiting for more output buffers. */
76 netif_wake_queue(vi->dev);
78 /* Make sure we re-xmit last_xmit_skb: if there are no more packets
79 * queued, start_xmit won't be called. */
80 tasklet_schedule(&vi->tasklet);
83 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
86 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
88 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
89 pr_debug("%s: short packet %i\n", dev->name, len);
90 dev->stats.rx_length_errors++;
93 len -= sizeof(struct virtio_net_hdr);
94 BUG_ON(len > MAX_PACKET_LEN);
98 dev->stats.rx_bytes += skb->len;
99 dev->stats.rx_packets++;
101 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
102 pr_debug("Needs csum!\n");
103 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
107 skb->protocol = eth_type_trans(skb, dev);
108 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
109 ntohs(skb->protocol), skb->len, skb->pkt_type);
111 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
113 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
114 case VIRTIO_NET_HDR_GSO_TCPV4:
115 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
117 case VIRTIO_NET_HDR_GSO_UDP:
118 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
120 case VIRTIO_NET_HDR_GSO_TCPV6:
121 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
125 printk(KERN_WARNING "%s: bad gso type %u.\n",
126 dev->name, hdr->gso_type);
130 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
131 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
133 skb_shinfo(skb)->gso_size = hdr->gso_size;
134 if (skb_shinfo(skb)->gso_size == 0) {
136 printk(KERN_WARNING "%s: zero gso size.\n",
141 /* Header must be checked, and gso_segs computed. */
142 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
143 skb_shinfo(skb)->gso_segs = 0;
146 netif_receive_skb(skb);
150 dev->stats.rx_frame_errors++;
155 static void try_fill_recv(struct virtnet_info *vi)
158 struct scatterlist sg[2+MAX_SKB_FRAGS];
161 sg_init_table(sg, 2+MAX_SKB_FRAGS);
163 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
167 skb_put(skb, MAX_PACKET_LEN);
168 vnet_hdr_to_sg(sg, skb);
169 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
170 skb_queue_head(&vi->recv, skb);
172 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
174 skb_unlink(skb, &vi->recv);
180 if (unlikely(vi->num > vi->max))
182 vi->rvq->vq_ops->kick(vi->rvq);
185 static void skb_recv_done(struct virtqueue *rvq)
187 struct virtnet_info *vi = rvq->vdev->priv;
188 /* Schedule NAPI, Suppress further interrupts if successful. */
189 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
190 rvq->vq_ops->disable_cb(rvq);
191 __netif_rx_schedule(vi->dev, &vi->napi);
195 static int virtnet_poll(struct napi_struct *napi, int budget)
197 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
198 struct sk_buff *skb = NULL;
199 unsigned int len, received = 0;
202 while (received < budget &&
203 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
204 __skb_unlink(skb, &vi->recv);
205 receive_skb(vi->dev, skb, len);
210 /* FIXME: If we oom and completely run out of inbufs, we need
211 * to start a timer trying to fill more. */
212 if (vi->num < vi->max / 2)
215 /* Out of packets? */
216 if (received < budget) {
217 netif_rx_complete(vi->dev, napi);
218 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
219 && napi_schedule_prep(napi)) {
220 vi->rvq->vq_ops->disable_cb(vi->rvq);
221 __netif_rx_schedule(vi->dev, napi);
229 static void free_old_xmit_skbs(struct virtnet_info *vi)
234 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
235 pr_debug("Sent skb %p\n", skb);
236 __skb_unlink(skb, &vi->send);
237 vi->dev->stats.tx_bytes += skb->len;
238 vi->dev->stats.tx_packets++;
243 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
246 struct scatterlist sg[2+MAX_SKB_FRAGS];
247 struct virtio_net_hdr *hdr;
248 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
250 sg_init_table(sg, 2+MAX_SKB_FRAGS);
252 pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb,
253 dest[0], dest[1], dest[2],
254 dest[3], dest[4], dest[5]);
256 /* Encode metadata header at front. */
257 hdr = skb_vnet_hdr(skb);
258 if (skb->ip_summed == CHECKSUM_PARTIAL) {
259 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
260 hdr->csum_start = skb->csum_start - skb_headroom(skb);
261 hdr->csum_offset = skb->csum_offset;
264 hdr->csum_offset = hdr->csum_start = 0;
267 if (skb_is_gso(skb)) {
268 hdr->hdr_len = skb_transport_header(skb) - skb->data;
269 hdr->gso_size = skb_shinfo(skb)->gso_size;
270 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
271 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
272 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
273 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
274 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
275 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
278 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
279 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
281 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
282 hdr->gso_size = hdr->hdr_len = 0;
285 vnet_hdr_to_sg(sg, skb);
286 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
288 return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
291 static void xmit_tasklet(unsigned long data)
293 struct virtnet_info *vi = (void *)data;
295 netif_tx_lock_bh(vi->dev);
296 if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) {
297 vi->svq->vq_ops->kick(vi->svq);
298 vi->last_xmit_skb = NULL;
300 netif_tx_unlock_bh(vi->dev);
303 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
305 struct virtnet_info *vi = netdev_priv(dev);
308 /* Free up any pending old buffers before queueing new ones. */
309 free_old_xmit_skbs(vi);
311 /* If we has a buffer left over from last time, send it now. */
312 if (unlikely(vi->last_xmit_skb)) {
313 if (xmit_skb(vi, vi->last_xmit_skb) != 0) {
314 /* Drop this skb: we only queue one. */
315 vi->dev->stats.tx_dropped++;
320 vi->last_xmit_skb = NULL;
323 /* Put new one in send queue and do transmit */
325 __skb_queue_head(&vi->send, skb);
326 if (xmit_skb(vi, skb) != 0) {
327 vi->last_xmit_skb = skb;
333 vi->svq->vq_ops->kick(vi->svq);
337 pr_debug("%s: virtio not prepared to send\n", dev->name);
338 netif_stop_queue(dev);
340 /* Activate callback for using skbs: if this returns false it
341 * means some were used in the meantime. */
342 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
343 vi->svq->vq_ops->disable_cb(vi->svq);
344 netif_start_queue(dev);
350 #ifdef CONFIG_NET_POLL_CONTROLLER
351 static void virtnet_netpoll(struct net_device *dev)
353 struct virtnet_info *vi = netdev_priv(dev);
355 napi_schedule(&vi->napi);
359 static int virtnet_open(struct net_device *dev)
361 struct virtnet_info *vi = netdev_priv(dev);
363 napi_enable(&vi->napi);
365 /* If all buffers were filled by other side before we napi_enabled, we
366 * won't get another interrupt, so process any outstanding packets
367 * now. virtnet_poll wants re-enable the queue, so we disable here.
368 * We synchronize against interrupts via NAPI_STATE_SCHED */
369 if (netif_rx_schedule_prep(dev, &vi->napi)) {
370 vi->rvq->vq_ops->disable_cb(vi->rvq);
371 __netif_rx_schedule(dev, &vi->napi);
376 static int virtnet_close(struct net_device *dev)
378 struct virtnet_info *vi = netdev_priv(dev);
380 napi_disable(&vi->napi);
385 static int virtnet_probe(struct virtio_device *vdev)
388 struct net_device *dev;
389 struct virtnet_info *vi;
391 /* Allocate ourselves a network device with room for our info */
392 dev = alloc_etherdev(sizeof(struct virtnet_info));
396 /* Set up network device as normal. */
397 dev->open = virtnet_open;
398 dev->stop = virtnet_close;
399 dev->hard_start_xmit = start_xmit;
400 dev->features = NETIF_F_HIGHDMA;
401 #ifdef CONFIG_NET_POLL_CONTROLLER
402 dev->poll_controller = virtnet_netpoll;
404 SET_NETDEV_DEV(dev, &vdev->dev);
406 /* Do we support "hardware" checksums? */
407 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
408 /* This opens up the world of extra features. */
409 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
410 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
411 dev->features |= NETIF_F_TSO | NETIF_F_UFO
412 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
414 /* Individual feature bits: what can host handle? */
415 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
416 dev->features |= NETIF_F_TSO;
417 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
418 dev->features |= NETIF_F_TSO6;
419 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
420 dev->features |= NETIF_F_TSO_ECN;
421 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
422 dev->features |= NETIF_F_UFO;
425 /* Configuration may specify what MAC to use. Otherwise random. */
426 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
427 vdev->config->get(vdev,
428 offsetof(struct virtio_net_config, mac),
429 dev->dev_addr, dev->addr_len);
431 random_ether_addr(dev->dev_addr);
433 /* Set up our device-specific information */
434 vi = netdev_priv(dev);
435 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
440 /* We expect two virtqueues, receive then send. */
441 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
442 if (IS_ERR(vi->rvq)) {
443 err = PTR_ERR(vi->rvq);
447 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
448 if (IS_ERR(vi->svq)) {
449 err = PTR_ERR(vi->svq);
453 /* Initialize our empty receive and send queues. */
454 skb_queue_head_init(&vi->recv);
455 skb_queue_head_init(&vi->send);
457 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
459 err = register_netdev(dev);
461 pr_debug("virtio_net: registering device failed\n");
465 /* Last of all, set up some receive buffers. */
468 /* If we didn't even get one input buffer, we're useless. */
474 pr_debug("virtnet: registered device %s\n", dev->name);
478 unregister_netdev(dev);
480 vdev->config->del_vq(vi->svq);
482 vdev->config->del_vq(vi->rvq);
488 static void virtnet_remove(struct virtio_device *vdev)
490 struct virtnet_info *vi = vdev->priv;
493 /* Stop all the virtqueues. */
494 vdev->config->reset(vdev);
496 /* Free our skbs in send and recv queues, if any. */
497 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
501 __skb_queue_purge(&vi->send);
503 BUG_ON(vi->num != 0);
505 vdev->config->del_vq(vi->svq);
506 vdev->config->del_vq(vi->rvq);
507 unregister_netdev(vi->dev);
508 free_netdev(vi->dev);
511 static struct virtio_device_id id_table[] = {
512 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
516 static unsigned int features[] = {
517 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
518 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
519 VIRTIO_NET_F_HOST_ECN,
522 static struct virtio_driver virtio_net = {
523 .feature_table = features,
524 .feature_table_size = ARRAY_SIZE(features),
525 .driver.name = KBUILD_MODNAME,
526 .driver.owner = THIS_MODULE,
527 .id_table = id_table,
528 .probe = virtnet_probe,
529 .remove = __devexit_p(virtnet_remove),
532 static int __init init(void)
534 return register_virtio_driver(&virtio_net);
537 static void __exit fini(void)
539 unregister_virtio_driver(&virtio_net);
544 MODULE_DEVICE_TABLE(virtio, id_table);
545 MODULE_DESCRIPTION("Virtio network driver");
546 MODULE_LICENSE("GPL");