]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/virtio_net.c
virtio: free transmit skbs when notified, not on next xmit.
[linux-2.6-omap-h63xx.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/module.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_net.h>
25 #include <linux/scatterlist.h>
26
27 static int csum = 1, gso = 1;
28 module_param(csum, bool, 0444);
29 module_param(gso, bool, 0444);
30
31 /* FIXME: MTU in config. */
32 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
33
34 struct virtnet_info
35 {
36         struct virtio_device *vdev;
37         struct virtqueue *rvq, *svq;
38         struct net_device *dev;
39         struct napi_struct napi;
40
41         /* Number of input buffers, and max we've ever had. */
42         unsigned int num, max;
43
44         /* Receive & send queues. */
45         struct sk_buff_head recv;
46         struct sk_buff_head send;
47 };
48
49 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
50 {
51         return (struct virtio_net_hdr *)skb->cb;
52 }
53
54 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
55 {
56         sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
57 }
58
59 static void skb_xmit_done(struct virtqueue *svq)
60 {
61         struct virtnet_info *vi = svq->vdev->priv;
62
63         /* Suppress further interrupts. */
64         svq->vq_ops->disable_cb(svq);
65         /* We were waiting for more output buffers. */
66         netif_wake_queue(vi->dev);
67 }
68
69 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
70                         unsigned len)
71 {
72         struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
73
74         if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
75                 pr_debug("%s: short packet %i\n", dev->name, len);
76                 dev->stats.rx_length_errors++;
77                 goto drop;
78         }
79         len -= sizeof(struct virtio_net_hdr);
80         BUG_ON(len > MAX_PACKET_LEN);
81
82         skb_trim(skb, len);
83         skb->protocol = eth_type_trans(skb, dev);
84         pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
85                  ntohs(skb->protocol), skb->len, skb->pkt_type);
86         dev->stats.rx_bytes += skb->len;
87         dev->stats.rx_packets++;
88
89         if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
90                 pr_debug("Needs csum!\n");
91                 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
92                         goto frame_err;
93         }
94
95         if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
96                 pr_debug("GSO!\n");
97                 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
98                 case VIRTIO_NET_HDR_GSO_TCPV4:
99                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
100                         break;
101                 case VIRTIO_NET_HDR_GSO_UDP:
102                         skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
103                         break;
104                 case VIRTIO_NET_HDR_GSO_TCPV6:
105                         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
106                         break;
107                 default:
108                         if (net_ratelimit())
109                                 printk(KERN_WARNING "%s: bad gso type %u.\n",
110                                        dev->name, hdr->gso_type);
111                         goto frame_err;
112                 }
113
114                 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
115                         skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
116
117                 skb_shinfo(skb)->gso_size = hdr->gso_size;
118                 if (skb_shinfo(skb)->gso_size == 0) {
119                         if (net_ratelimit())
120                                 printk(KERN_WARNING "%s: zero gso size.\n",
121                                        dev->name);
122                         goto frame_err;
123                 }
124
125                 /* Header must be checked, and gso_segs computed. */
126                 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
127                 skb_shinfo(skb)->gso_segs = 0;
128         }
129
130         netif_receive_skb(skb);
131         return;
132
133 frame_err:
134         dev->stats.rx_frame_errors++;
135 drop:
136         dev_kfree_skb(skb);
137 }
138
139 static void try_fill_recv(struct virtnet_info *vi)
140 {
141         struct sk_buff *skb;
142         struct scatterlist sg[1+MAX_SKB_FRAGS];
143         int num, err;
144
145         sg_init_table(sg, 1+MAX_SKB_FRAGS);
146         for (;;) {
147                 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
148                 if (unlikely(!skb))
149                         break;
150
151                 skb_put(skb, MAX_PACKET_LEN);
152                 vnet_hdr_to_sg(sg, skb);
153                 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
154                 skb_queue_head(&vi->recv, skb);
155
156                 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
157                 if (err) {
158                         skb_unlink(skb, &vi->recv);
159                         kfree_skb(skb);
160                         break;
161                 }
162                 vi->num++;
163         }
164         if (unlikely(vi->num > vi->max))
165                 vi->max = vi->num;
166         vi->rvq->vq_ops->kick(vi->rvq);
167 }
168
169 static void skb_recv_done(struct virtqueue *rvq)
170 {
171         struct virtnet_info *vi = rvq->vdev->priv;
172         /* Schedule NAPI, Suppress further interrupts if successful. */
173         if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
174                 rvq->vq_ops->disable_cb(rvq);
175                 __netif_rx_schedule(vi->dev, &vi->napi);
176         }
177 }
178
179 static int virtnet_poll(struct napi_struct *napi, int budget)
180 {
181         struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
182         struct sk_buff *skb = NULL;
183         unsigned int len, received = 0;
184
185 again:
186         while (received < budget &&
187                (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
188                 __skb_unlink(skb, &vi->recv);
189                 receive_skb(vi->dev, skb, len);
190                 vi->num--;
191                 received++;
192         }
193
194         /* FIXME: If we oom and completely run out of inbufs, we need
195          * to start a timer trying to fill more. */
196         if (vi->num < vi->max / 2)
197                 try_fill_recv(vi);
198
199         /* Out of packets? */
200         if (received < budget) {
201                 netif_rx_complete(vi->dev, napi);
202                 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
203                     && netif_rx_reschedule(vi->dev, napi))
204                         goto again;
205         }
206
207         return received;
208 }
209
210 static void free_old_xmit_skbs(struct virtnet_info *vi)
211 {
212         struct sk_buff *skb;
213         unsigned int len;
214
215         while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
216                 pr_debug("Sent skb %p\n", skb);
217                 __skb_unlink(skb, &vi->send);
218                 vi->dev->stats.tx_bytes += len;
219                 vi->dev->stats.tx_packets++;
220                 kfree_skb(skb);
221         }
222 }
223
224 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
225 {
226         struct virtnet_info *vi = netdev_priv(dev);
227         int num, err;
228         struct scatterlist sg[1+MAX_SKB_FRAGS];
229         struct virtio_net_hdr *hdr;
230         const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
231         DECLARE_MAC_BUF(mac);
232
233         sg_init_table(sg, 1+MAX_SKB_FRAGS);
234
235         pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
236
237         /* Encode metadata header at front. */
238         hdr = skb_vnet_hdr(skb);
239         if (skb->ip_summed == CHECKSUM_PARTIAL) {
240                 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
241                 hdr->csum_start = skb->csum_start - skb_headroom(skb);
242                 hdr->csum_offset = skb->csum_offset;
243         } else {
244                 hdr->flags = 0;
245                 hdr->csum_offset = hdr->csum_start = 0;
246         }
247
248         if (skb_is_gso(skb)) {
249                 hdr->hdr_len = skb_transport_header(skb) - skb->data;
250                 hdr->gso_size = skb_shinfo(skb)->gso_size;
251                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
252                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
253                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
254                         hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
255                 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
256                         hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
257                 else
258                         BUG();
259                 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
260                         hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
261         } else {
262                 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
263                 hdr->gso_size = hdr->hdr_len = 0;
264         }
265
266         vnet_hdr_to_sg(sg, skb);
267         num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
268         __skb_queue_head(&vi->send, skb);
269
270 again:
271         /* Free up any pending old buffers before queueing new ones. */
272         free_old_xmit_skbs(vi);
273         err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
274         if (err) {
275                 pr_debug("%s: virtio not prepared to send\n", dev->name);
276                 netif_stop_queue(dev);
277
278                 /* Activate callback for using skbs: if this fails it
279                  * means some were used in the meantime. */
280                 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
281                         printk("Unlikely: restart svq failed\n");
282                         netif_start_queue(dev);
283                         goto again;
284                 }
285                 __skb_unlink(skb, &vi->send);
286
287                 return NETDEV_TX_BUSY;
288         }
289         vi->svq->vq_ops->kick(vi->svq);
290
291         return 0;
292 }
293
294 static int virtnet_open(struct net_device *dev)
295 {
296         struct virtnet_info *vi = netdev_priv(dev);
297
298         napi_enable(&vi->napi);
299
300         /* If all buffers were filled by other side before we napi_enabled, we
301          * won't get another interrupt, so process any outstanding packets
302          * now.  virtnet_poll wants re-enable the queue, so we disable here. */
303         vi->rvq->vq_ops->disable_cb(vi->rvq);
304         netif_rx_schedule(vi->dev, &vi->napi);
305
306         return 0;
307 }
308
309 static int virtnet_close(struct net_device *dev)
310 {
311         struct virtnet_info *vi = netdev_priv(dev);
312
313         napi_disable(&vi->napi);
314
315         return 0;
316 }
317
318 static int virtnet_probe(struct virtio_device *vdev)
319 {
320         int err;
321         struct net_device *dev;
322         struct virtnet_info *vi;
323
324         /* Allocate ourselves a network device with room for our info */
325         dev = alloc_etherdev(sizeof(struct virtnet_info));
326         if (!dev)
327                 return -ENOMEM;
328
329         /* Set up network device as normal. */
330         dev->open = virtnet_open;
331         dev->stop = virtnet_close;
332         dev->hard_start_xmit = start_xmit;
333         dev->features = NETIF_F_HIGHDMA;
334         SET_NETDEV_DEV(dev, &vdev->dev);
335
336         /* Do we support "hardware" checksums? */
337         if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
338                 /* This opens up the world of extra features. */
339                 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
340                 if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) {
341                         dev->features |= NETIF_F_TSO | NETIF_F_UFO
342                                 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
343                 }
344         }
345
346         /* Configuration may specify what MAC to use.  Otherwise random. */
347         if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
348                 vdev->config->get(vdev,
349                                   offsetof(struct virtio_net_config, mac),
350                                   dev->dev_addr, dev->addr_len);
351         } else
352                 random_ether_addr(dev->dev_addr);
353
354         /* Set up our device-specific information */
355         vi = netdev_priv(dev);
356         netif_napi_add(dev, &vi->napi, virtnet_poll, 16);
357         vi->dev = dev;
358         vi->vdev = vdev;
359
360         /* We expect two virtqueues, receive then send. */
361         vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
362         if (IS_ERR(vi->rvq)) {
363                 err = PTR_ERR(vi->rvq);
364                 goto free;
365         }
366
367         vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
368         if (IS_ERR(vi->svq)) {
369                 err = PTR_ERR(vi->svq);
370                 goto free_recv;
371         }
372
373         /* Initialize our empty receive and send queues. */
374         skb_queue_head_init(&vi->recv);
375         skb_queue_head_init(&vi->send);
376
377         err = register_netdev(dev);
378         if (err) {
379                 pr_debug("virtio_net: registering device failed\n");
380                 goto free_send;
381         }
382
383         /* Last of all, set up some receive buffers. */
384         try_fill_recv(vi);
385
386         /* If we didn't even get one input buffer, we're useless. */
387         if (vi->num == 0) {
388                 err = -ENOMEM;
389                 goto unregister;
390         }
391
392         pr_debug("virtnet: registered device %s\n", dev->name);
393         vdev->priv = vi;
394         return 0;
395
396 unregister:
397         unregister_netdev(dev);
398 free_send:
399         vdev->config->del_vq(vi->svq);
400 free_recv:
401         vdev->config->del_vq(vi->rvq);
402 free:
403         free_netdev(dev);
404         return err;
405 }
406
407 static void virtnet_remove(struct virtio_device *vdev)
408 {
409         struct virtnet_info *vi = vdev->priv;
410         struct sk_buff *skb;
411
412         /* Stop all the virtqueues. */
413         vdev->config->reset(vdev);
414
415         /* Free our skbs in send and recv queues, if any. */
416         while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
417                 kfree_skb(skb);
418                 vi->num--;
419         }
420         while ((skb = __skb_dequeue(&vi->send)) != NULL)
421                 kfree_skb(skb);
422
423         BUG_ON(vi->num != 0);
424
425         vdev->config->del_vq(vi->svq);
426         vdev->config->del_vq(vi->rvq);
427         unregister_netdev(vi->dev);
428         free_netdev(vi->dev);
429 }
430
431 static struct virtio_device_id id_table[] = {
432         { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
433         { 0 },
434 };
435
436 static struct virtio_driver virtio_net = {
437         .driver.name =  KBUILD_MODNAME,
438         .driver.owner = THIS_MODULE,
439         .id_table =     id_table,
440         .probe =        virtnet_probe,
441         .remove =       __devexit_p(virtnet_remove),
442 };
443
444 static int __init init(void)
445 {
446         return register_virtio_driver(&virtio_net);
447 }
448
449 static void __exit fini(void)
450 {
451         unregister_virtio_driver(&virtio_net);
452 }
453 module_init(init);
454 module_exit(fini);
455
456 MODULE_DEVICE_TABLE(virtio, id_table);
457 MODULE_DESCRIPTION("Virtio network driver");
458 MODULE_LICENSE("GPL");