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