virtio net: Allow receiving SG packets
[deliverable/linux.git] / drivers / net / virtio_net.c
1 /* A simple network driver using virtio.
2 *
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
4 *
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.
9 *
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.
14 *
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
18 */
19 //#define DEBUG
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
28 static int napi_weight = 128;
29 module_param(napi_weight, int, 0444);
30
31 static int csum = 1, gso = 1;
32 module_param(csum, bool, 0444);
33 module_param(gso, bool, 0444);
34
35 /* FIXME: MTU in config. */
36 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
37
38 struct virtnet_info
39 {
40 struct virtio_device *vdev;
41 struct virtqueue *rvq, *svq;
42 struct net_device *dev;
43 struct napi_struct napi;
44
45 /* The skb we couldn't send because buffers were full. */
46 struct sk_buff *last_xmit_skb;
47
48 /* If we need to free in a timer, this is it. */
49 struct timer_list xmit_free_timer;
50
51 /* Number of input buffers, and max we've ever had. */
52 unsigned int num, max;
53
54 /* For cleaning up after transmission. */
55 struct tasklet_struct tasklet;
56 bool free_in_tasklet;
57
58 /* I like... big packets and I cannot lie! */
59 bool big_packets;
60
61 /* Receive & send queues. */
62 struct sk_buff_head recv;
63 struct sk_buff_head send;
64 };
65
66 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
67 {
68 return (struct virtio_net_hdr *)skb->cb;
69 }
70
71 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
72 {
73 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
74 }
75
76 static void skb_xmit_done(struct virtqueue *svq)
77 {
78 struct virtnet_info *vi = svq->vdev->priv;
79
80 /* Suppress further interrupts. */
81 svq->vq_ops->disable_cb(svq);
82
83 /* We were probably waiting for more output buffers. */
84 netif_wake_queue(vi->dev);
85
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);
89 }
90
91 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
92 unsigned len)
93 {
94 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
95 int err;
96
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++;
100 goto drop;
101 }
102 len -= sizeof(struct virtio_net_hdr);
103
104 err = pskb_trim(skb, len);
105 if (err) {
106 pr_debug("%s: pskb_trim failed %i %d\n", dev->name, len, err);
107 dev->stats.rx_dropped++;
108 goto drop;
109 }
110 skb->truesize += skb->data_len;
111 dev->stats.rx_bytes += skb->len;
112 dev->stats.rx_packets++;
113
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))
117 goto frame_err;
118 }
119
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);
123
124 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
125 pr_debug("GSO!\n");
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;
129 break;
130 case VIRTIO_NET_HDR_GSO_UDP:
131 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
132 break;
133 case VIRTIO_NET_HDR_GSO_TCPV6:
134 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
135 break;
136 default:
137 if (net_ratelimit())
138 printk(KERN_WARNING "%s: bad gso type %u.\n",
139 dev->name, hdr->gso_type);
140 goto frame_err;
141 }
142
143 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
144 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
145
146 skb_shinfo(skb)->gso_size = hdr->gso_size;
147 if (skb_shinfo(skb)->gso_size == 0) {
148 if (net_ratelimit())
149 printk(KERN_WARNING "%s: zero gso size.\n",
150 dev->name);
151 goto frame_err;
152 }
153
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;
157 }
158
159 netif_receive_skb(skb);
160 return;
161
162 frame_err:
163 dev->stats.rx_frame_errors++;
164 drop:
165 dev_kfree_skb(skb);
166 }
167
168 static void try_fill_recv(struct virtnet_info *vi)
169 {
170 struct sk_buff *skb;
171 struct scatterlist sg[2+MAX_SKB_FRAGS];
172 int num, err, i;
173
174 sg_init_table(sg, 2+MAX_SKB_FRAGS);
175 for (;;) {
176 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
177 if (unlikely(!skb))
178 break;
179
180 skb_put(skb, MAX_PACKET_LEN);
181 vnet_hdr_to_sg(sg, skb);
182
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);
187 if (!f->page)
188 break;
189
190 f->page_offset = 0;
191 f->size = PAGE_SIZE;
192
193 skb->data_len += PAGE_SIZE;
194 skb->len += PAGE_SIZE;
195
196 skb_shinfo(skb)->nr_frags++;
197 }
198 }
199
200 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
201 skb_queue_head(&vi->recv, skb);
202
203 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
204 if (err) {
205 skb_unlink(skb, &vi->recv);
206 kfree_skb(skb);
207 break;
208 }
209 vi->num++;
210 }
211 if (unlikely(vi->num > vi->max))
212 vi->max = vi->num;
213 vi->rvq->vq_ops->kick(vi->rvq);
214 }
215
216 static void skb_recv_done(struct virtqueue *rvq)
217 {
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);
223 }
224 }
225
226 static int virtnet_poll(struct napi_struct *napi, int budget)
227 {
228 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
229 struct sk_buff *skb = NULL;
230 unsigned int len, received = 0;
231
232 again:
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);
237 vi->num--;
238 received++;
239 }
240
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)
244 try_fill_recv(vi);
245
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);
253 goto again;
254 }
255 }
256
257 return received;
258 }
259
260 static void free_old_xmit_skbs(struct virtnet_info *vi)
261 {
262 struct sk_buff *skb;
263 unsigned int len;
264
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++;
270 kfree_skb(skb);
271 }
272 }
273
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)
277 {
278 struct virtnet_info *vi = (void *)data;
279
280 netif_tx_lock(vi->dev);
281
282 free_old_xmit_skbs(vi);
283
284 if (!skb_queue_empty(&vi->send))
285 mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10));
286
287 netif_tx_unlock(vi->dev);
288 }
289
290 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
291 {
292 int num, err;
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;
296
297 sg_init_table(sg, 2+MAX_SKB_FRAGS);
298
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]);
302
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;
309 } else {
310 hdr->flags = 0;
311 hdr->csum_offset = hdr->csum_start = 0;
312 }
313
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;
323 else
324 BUG();
325 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
326 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
327 } else {
328 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
329 hdr->gso_size = hdr->hdr_len = 0;
330 }
331
332 vnet_hdr_to_sg(sg, skb);
333 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
334
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));
338
339 return err;
340 }
341
342 static void xmit_tasklet(unsigned long data)
343 {
344 struct virtnet_info *vi = (void *)data;
345
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;
350 }
351 if (vi->free_in_tasklet)
352 free_old_xmit_skbs(vi);
353 netif_tx_unlock_bh(vi->dev);
354 }
355
356 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
357 {
358 struct virtnet_info *vi = netdev_priv(dev);
359
360 again:
361 /* Free up any pending old buffers before queueing new ones. */
362 free_old_xmit_skbs(vi);
363
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)
367 goto stop_queue;
368
369 vi->last_xmit_skb = NULL;
370
371 /* Put new one in send queue and do transmit */
372 if (likely(skb)) {
373 __skb_queue_head(&vi->send, skb);
374 if (xmit_skb(vi, skb) != 0) {
375 vi->last_xmit_skb = skb;
376 skb = NULL;
377 goto stop_queue;
378 }
379 }
380 done:
381 vi->svq->vq_ops->kick(vi->svq);
382 return NETDEV_TX_OK;
383
384 stop_queue:
385 pr_debug("%s: virtio not prepared to send\n", dev->name);
386 netif_stop_queue(dev);
387
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);
393 goto again;
394 }
395 if (skb) {
396 /* Drop this skb: we only queue one. */
397 vi->dev->stats.tx_dropped++;
398 kfree_skb(skb);
399 }
400 goto done;
401 }
402
403 #ifdef CONFIG_NET_POLL_CONTROLLER
404 static void virtnet_netpoll(struct net_device *dev)
405 {
406 struct virtnet_info *vi = netdev_priv(dev);
407
408 napi_schedule(&vi->napi);
409 }
410 #endif
411
412 static int virtnet_open(struct net_device *dev)
413 {
414 struct virtnet_info *vi = netdev_priv(dev);
415
416 napi_enable(&vi->napi);
417
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);
425 }
426 return 0;
427 }
428
429 static int virtnet_close(struct net_device *dev)
430 {
431 struct virtnet_info *vi = netdev_priv(dev);
432
433 napi_disable(&vi->napi);
434
435 return 0;
436 }
437
438 static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
439 {
440 struct virtnet_info *vi = netdev_priv(dev);
441 struct virtio_device *vdev = vi->vdev;
442
443 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
444 return -ENOSYS;
445
446 return ethtool_op_set_tx_hw_csum(dev, data);
447 }
448
449 static struct ethtool_ops virtnet_ethtool_ops = {
450 .set_tx_csum = virtnet_set_tx_csum,
451 .set_sg = ethtool_op_set_sg,
452 };
453
454 static int virtnet_probe(struct virtio_device *vdev)
455 {
456 int err;
457 struct net_device *dev;
458 struct virtnet_info *vi;
459
460 /* Allocate ourselves a network device with room for our info */
461 dev = alloc_etherdev(sizeof(struct virtnet_info));
462 if (!dev)
463 return -ENOMEM;
464
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;
472 #endif
473 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
474 SET_NETDEV_DEV(dev, &vdev->dev);
475
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;
483 }
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;
493 }
494
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);
500 } else
501 random_ether_addr(dev->dev_addr);
502
503 /* Set up our device-specific information */
504 vi = netdev_priv(dev);
505 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
506 vi->dev = dev;
507 vi->vdev = vdev;
508 vdev->priv = vi;
509
510 /* If they give us a callback when all buffers are done, we don't need
511 * the timer. */
512 vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY);
513
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;
519
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);
524 goto free;
525 }
526
527 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
528 if (IS_ERR(vi->svq)) {
529 err = PTR_ERR(vi->svq);
530 goto free_recv;
531 }
532
533 /* Initialize our empty receive and send queues. */
534 skb_queue_head_init(&vi->recv);
535 skb_queue_head_init(&vi->send);
536
537 tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi);
538
539 if (!vi->free_in_tasklet)
540 setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi);
541
542 err = register_netdev(dev);
543 if (err) {
544 pr_debug("virtio_net: registering device failed\n");
545 goto free_send;
546 }
547
548 /* Last of all, set up some receive buffers. */
549 try_fill_recv(vi);
550
551 /* If we didn't even get one input buffer, we're useless. */
552 if (vi->num == 0) {
553 err = -ENOMEM;
554 goto unregister;
555 }
556
557 pr_debug("virtnet: registered device %s\n", dev->name);
558 return 0;
559
560 unregister:
561 unregister_netdev(dev);
562 free_send:
563 vdev->config->del_vq(vi->svq);
564 free_recv:
565 vdev->config->del_vq(vi->rvq);
566 free:
567 free_netdev(dev);
568 return err;
569 }
570
571 static void virtnet_remove(struct virtio_device *vdev)
572 {
573 struct virtnet_info *vi = vdev->priv;
574 struct sk_buff *skb;
575
576 /* Stop all the virtqueues. */
577 vdev->config->reset(vdev);
578
579 if (!vi->free_in_tasklet)
580 del_timer_sync(&vi->xmit_free_timer);
581
582 /* Free our skbs in send and recv queues, if any. */
583 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
584 kfree_skb(skb);
585 vi->num--;
586 }
587 __skb_queue_purge(&vi->send);
588
589 BUG_ON(vi->num != 0);
590
591 vdev->config->del_vq(vi->svq);
592 vdev->config->del_vq(vi->rvq);
593 unregister_netdev(vi->dev);
594 free_netdev(vi->dev);
595 }
596
597 static struct virtio_device_id id_table[] = {
598 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
599 { 0 },
600 };
601
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,
609 };
610
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),
619 };
620
621 static int __init init(void)
622 {
623 return register_virtio_driver(&virtio_net);
624 }
625
626 static void __exit fini(void)
627 {
628 unregister_virtio_driver(&virtio_net);
629 }
630 module_init(init);
631 module_exit(fini);
632
633 MODULE_DEVICE_TABLE(virtio, id_table);
634 MODULE_DESCRIPTION("Virtio network driver");
635 MODULE_LICENSE("GPL");
This page took 0.069159 seconds and 6 git commands to generate.