Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / net / virtio_net.c
blob296bb9342916a45815e19ecff1014c1a9cde30f3
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
19 //#define DEBUG
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)
37 struct virtnet_info
39 struct virtio_device *vdev;
40 struct virtqueue *rvq, *svq;
41 struct net_device *dev;
42 struct napi_struct napi;
44 /* Number of input buffers, and max we've ever had. */
45 unsigned int num, max;
47 /* Receive & send queues. */
48 struct sk_buff_head recv;
49 struct sk_buff_head send;
52 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
54 return (struct virtio_net_hdr *)skb->cb;
57 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
59 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
62 static void skb_xmit_done(struct virtqueue *svq)
64 struct virtnet_info *vi = svq->vdev->priv;
66 /* Suppress further interrupts. */
67 svq->vq_ops->disable_cb(svq);
68 /* We were waiting for more output buffers. */
69 netif_wake_queue(vi->dev);
72 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
73 unsigned len)
75 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
77 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
78 pr_debug("%s: short packet %i\n", dev->name, len);
79 dev->stats.rx_length_errors++;
80 goto drop;
82 len -= sizeof(struct virtio_net_hdr);
83 BUG_ON(len > MAX_PACKET_LEN);
85 skb_trim(skb, len);
86 skb->protocol = eth_type_trans(skb, dev);
87 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
88 ntohs(skb->protocol), skb->len, skb->pkt_type);
89 dev->stats.rx_bytes += skb->len;
90 dev->stats.rx_packets++;
92 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
93 pr_debug("Needs csum!\n");
94 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
95 goto frame_err;
98 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
99 pr_debug("GSO!\n");
100 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
101 case VIRTIO_NET_HDR_GSO_TCPV4:
102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
103 break;
104 case VIRTIO_NET_HDR_GSO_UDP:
105 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
106 break;
107 case VIRTIO_NET_HDR_GSO_TCPV6:
108 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
109 break;
110 default:
111 if (net_ratelimit())
112 printk(KERN_WARNING "%s: bad gso type %u.\n",
113 dev->name, hdr->gso_type);
114 goto frame_err;
117 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
118 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
120 skb_shinfo(skb)->gso_size = hdr->gso_size;
121 if (skb_shinfo(skb)->gso_size == 0) {
122 if (net_ratelimit())
123 printk(KERN_WARNING "%s: zero gso size.\n",
124 dev->name);
125 goto frame_err;
128 /* Header must be checked, and gso_segs computed. */
129 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
130 skb_shinfo(skb)->gso_segs = 0;
133 netif_receive_skb(skb);
134 return;
136 frame_err:
137 dev->stats.rx_frame_errors++;
138 drop:
139 dev_kfree_skb(skb);
142 static void try_fill_recv(struct virtnet_info *vi)
144 struct sk_buff *skb;
145 struct scatterlist sg[1+MAX_SKB_FRAGS];
146 int num, err;
148 sg_init_table(sg, 1+MAX_SKB_FRAGS);
149 for (;;) {
150 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
151 if (unlikely(!skb))
152 break;
154 skb_put(skb, MAX_PACKET_LEN);
155 vnet_hdr_to_sg(sg, skb);
156 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
157 skb_queue_head(&vi->recv, skb);
159 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
160 if (err) {
161 skb_unlink(skb, &vi->recv);
162 kfree_skb(skb);
163 break;
165 vi->num++;
167 if (unlikely(vi->num > vi->max))
168 vi->max = vi->num;
169 vi->rvq->vq_ops->kick(vi->rvq);
172 static void skb_recv_done(struct virtqueue *rvq)
174 struct virtnet_info *vi = rvq->vdev->priv;
175 /* Schedule NAPI, Suppress further interrupts if successful. */
176 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
177 rvq->vq_ops->disable_cb(rvq);
178 __netif_rx_schedule(vi->dev, &vi->napi);
182 static int virtnet_poll(struct napi_struct *napi, int budget)
184 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
185 struct sk_buff *skb = NULL;
186 unsigned int len, received = 0;
188 again:
189 while (received < budget &&
190 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
191 __skb_unlink(skb, &vi->recv);
192 receive_skb(vi->dev, skb, len);
193 vi->num--;
194 received++;
197 /* FIXME: If we oom and completely run out of inbufs, we need
198 * to start a timer trying to fill more. */
199 if (vi->num < vi->max / 2)
200 try_fill_recv(vi);
202 /* Out of packets? */
203 if (received < budget) {
204 netif_rx_complete(vi->dev, napi);
205 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
206 <<<<<<< HEAD:drivers/net/virtio_net.c
207 && netif_rx_reschedule(vi->dev, napi))
208 =======
209 && napi_schedule_prep(napi)) {
210 vi->rvq->vq_ops->disable_cb(vi->rvq);
211 __netif_rx_schedule(vi->dev, napi);
212 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
213 goto again;
214 <<<<<<< HEAD:drivers/net/virtio_net.c
215 =======
217 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
220 return received;
223 static void free_old_xmit_skbs(struct virtnet_info *vi)
225 struct sk_buff *skb;
226 unsigned int len;
228 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
229 pr_debug("Sent skb %p\n", skb);
230 __skb_unlink(skb, &vi->send);
231 vi->dev->stats.tx_bytes += len;
232 vi->dev->stats.tx_packets++;
233 kfree_skb(skb);
237 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
239 struct virtnet_info *vi = netdev_priv(dev);
240 int num, err;
241 struct scatterlist sg[1+MAX_SKB_FRAGS];
242 struct virtio_net_hdr *hdr;
243 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
244 DECLARE_MAC_BUF(mac);
246 sg_init_table(sg, 1+MAX_SKB_FRAGS);
248 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
250 /* Encode metadata header at front. */
251 hdr = skb_vnet_hdr(skb);
252 if (skb->ip_summed == CHECKSUM_PARTIAL) {
253 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
254 hdr->csum_start = skb->csum_start - skb_headroom(skb);
255 hdr->csum_offset = skb->csum_offset;
256 } else {
257 hdr->flags = 0;
258 hdr->csum_offset = hdr->csum_start = 0;
261 if (skb_is_gso(skb)) {
262 hdr->hdr_len = skb_transport_header(skb) - skb->data;
263 hdr->gso_size = skb_shinfo(skb)->gso_size;
264 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
265 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
266 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
267 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
268 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
269 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
270 else
271 BUG();
272 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
273 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
274 } else {
275 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
276 hdr->gso_size = hdr->hdr_len = 0;
279 vnet_hdr_to_sg(sg, skb);
280 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
281 __skb_queue_head(&vi->send, skb);
283 again:
284 /* Free up any pending old buffers before queueing new ones. */
285 free_old_xmit_skbs(vi);
286 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
287 if (err) {
288 pr_debug("%s: virtio not prepared to send\n", dev->name);
289 netif_stop_queue(dev);
291 <<<<<<< HEAD:drivers/net/virtio_net.c
292 /* Activate callback for using skbs: if this fails it
293 =======
294 /* Activate callback for using skbs: if this returns false it
295 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
296 * means some were used in the meantime. */
297 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
298 <<<<<<< HEAD:drivers/net/virtio_net.c
299 printk("Unlikely: restart svq failed\n");
300 =======
301 printk("Unlikely: restart svq race\n");
302 vi->svq->vq_ops->disable_cb(vi->svq);
303 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
304 netif_start_queue(dev);
305 goto again;
307 __skb_unlink(skb, &vi->send);
309 return NETDEV_TX_BUSY;
311 vi->svq->vq_ops->kick(vi->svq);
313 return 0;
316 <<<<<<< HEAD:drivers/net/virtio_net.c
317 =======
318 #ifdef CONFIG_NET_POLL_CONTROLLER
319 static void virtnet_netpoll(struct net_device *dev)
321 struct virtnet_info *vi = netdev_priv(dev);
323 napi_schedule(&vi->napi);
325 #endif
327 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
328 static int virtnet_open(struct net_device *dev)
330 struct virtnet_info *vi = netdev_priv(dev);
332 napi_enable(&vi->napi);
334 /* If all buffers were filled by other side before we napi_enabled, we
335 * won't get another interrupt, so process any outstanding packets
336 * now. virtnet_poll wants re-enable the queue, so we disable here.
337 * We synchronize against interrupts via NAPI_STATE_SCHED */
338 if (netif_rx_schedule_prep(dev, &vi->napi)) {
339 vi->rvq->vq_ops->disable_cb(vi->rvq);
340 __netif_rx_schedule(dev, &vi->napi);
342 return 0;
345 static int virtnet_close(struct net_device *dev)
347 struct virtnet_info *vi = netdev_priv(dev);
349 napi_disable(&vi->napi);
351 return 0;
354 static int virtnet_probe(struct virtio_device *vdev)
356 int err;
357 struct net_device *dev;
358 struct virtnet_info *vi;
360 /* Allocate ourselves a network device with room for our info */
361 dev = alloc_etherdev(sizeof(struct virtnet_info));
362 if (!dev)
363 return -ENOMEM;
365 /* Set up network device as normal. */
366 dev->open = virtnet_open;
367 dev->stop = virtnet_close;
368 dev->hard_start_xmit = start_xmit;
369 dev->features = NETIF_F_HIGHDMA;
370 <<<<<<< HEAD:drivers/net/virtio_net.c
371 =======
372 #ifdef CONFIG_NET_POLL_CONTROLLER
373 dev->poll_controller = virtnet_netpoll;
374 #endif
375 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
376 SET_NETDEV_DEV(dev, &vdev->dev);
378 /* Do we support "hardware" checksums? */
379 if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
380 /* This opens up the world of extra features. */
381 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
382 if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) {
383 dev->features |= NETIF_F_TSO | NETIF_F_UFO
384 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
388 /* Configuration may specify what MAC to use. Otherwise random. */
389 if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
390 vdev->config->get(vdev,
391 offsetof(struct virtio_net_config, mac),
392 dev->dev_addr, dev->addr_len);
393 } else
394 random_ether_addr(dev->dev_addr);
396 /* Set up our device-specific information */
397 vi = netdev_priv(dev);
398 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
399 vi->dev = dev;
400 vi->vdev = vdev;
401 <<<<<<< HEAD:drivers/net/virtio_net.c
402 =======
403 vdev->priv = vi;
404 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
406 /* We expect two virtqueues, receive then send. */
407 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
408 if (IS_ERR(vi->rvq)) {
409 err = PTR_ERR(vi->rvq);
410 goto free;
413 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
414 if (IS_ERR(vi->svq)) {
415 err = PTR_ERR(vi->svq);
416 goto free_recv;
419 /* Initialize our empty receive and send queues. */
420 skb_queue_head_init(&vi->recv);
421 skb_queue_head_init(&vi->send);
423 err = register_netdev(dev);
424 if (err) {
425 pr_debug("virtio_net: registering device failed\n");
426 goto free_send;
429 /* Last of all, set up some receive buffers. */
430 try_fill_recv(vi);
432 /* If we didn't even get one input buffer, we're useless. */
433 if (vi->num == 0) {
434 err = -ENOMEM;
435 goto unregister;
438 pr_debug("virtnet: registered device %s\n", dev->name);
439 <<<<<<< HEAD:drivers/net/virtio_net.c
440 vdev->priv = vi;
441 =======
442 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/net/virtio_net.c
443 return 0;
445 unregister:
446 unregister_netdev(dev);
447 free_send:
448 vdev->config->del_vq(vi->svq);
449 free_recv:
450 vdev->config->del_vq(vi->rvq);
451 free:
452 free_netdev(dev);
453 return err;
456 static void virtnet_remove(struct virtio_device *vdev)
458 struct virtnet_info *vi = vdev->priv;
459 struct sk_buff *skb;
461 /* Stop all the virtqueues. */
462 vdev->config->reset(vdev);
464 /* Free our skbs in send and recv queues, if any. */
465 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
466 kfree_skb(skb);
467 vi->num--;
469 while ((skb = __skb_dequeue(&vi->send)) != NULL)
470 kfree_skb(skb);
472 BUG_ON(vi->num != 0);
474 vdev->config->del_vq(vi->svq);
475 vdev->config->del_vq(vi->rvq);
476 unregister_netdev(vi->dev);
477 free_netdev(vi->dev);
480 static struct virtio_device_id id_table[] = {
481 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
482 { 0 },
485 static struct virtio_driver virtio_net = {
486 .driver.name = KBUILD_MODNAME,
487 .driver.owner = THIS_MODULE,
488 .id_table = id_table,
489 .probe = virtnet_probe,
490 .remove = __devexit_p(virtnet_remove),
493 static int __init init(void)
495 return register_virtio_driver(&virtio_net);
498 static void __exit fini(void)
500 unregister_virtio_driver(&virtio_net);
502 module_init(init);
503 module_exit(fini);
505 MODULE_DEVICE_TABLE(virtio, id_table);
506 MODULE_DESCRIPTION("Virtio network driver");
507 MODULE_LICENSE("GPL");