blob: bd4d26a36eadcb1a95bac0ba5226cf3254b400d2 [file] [log] [blame]
Rusty Russell296f96f2007-10-22 11:03:37 +10001/* 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
Rusty Russell34a48572008-02-04 23:50:02 -050027static int csum = 1, gso = 1;
28module_param(csum, bool, 0444);
29module_param(gso, bool, 0444);
30
Rusty Russell296f96f2007-10-22 11:03:37 +100031/* FIXME: MTU in config. */
32#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
33
34struct 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
49static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
50{
51 return (struct virtio_net_hdr *)skb->cb;
52}
53
54static 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
Rusty Russell18445c42008-02-04 23:49:57 -050059static void skb_xmit_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +100060{
61 struct virtnet_info *vi = rvq->vdev->priv;
62
63 /* In case we were waiting for output buffers. */
64 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +100065}
66
67static void receive_skb(struct net_device *dev, struct sk_buff *skb,
68 unsigned len)
69{
70 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
71
72 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
73 pr_debug("%s: short packet %i\n", dev->name, len);
74 dev->stats.rx_length_errors++;
75 goto drop;
76 }
77 len -= sizeof(struct virtio_net_hdr);
78 BUG_ON(len > MAX_PACKET_LEN);
79
80 skb_trim(skb, len);
81 skb->protocol = eth_type_trans(skb, dev);
82 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
83 ntohs(skb->protocol), skb->len, skb->pkt_type);
84 dev->stats.rx_bytes += skb->len;
85 dev->stats.rx_packets++;
86
87 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
88 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -050089 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +100090 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +100091 }
92
93 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
94 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -050095 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +100096 case VIRTIO_NET_HDR_GSO_TCPV4:
97 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
98 break;
Rusty Russell296f96f2007-10-22 11:03:37 +100099 case VIRTIO_NET_HDR_GSO_UDP:
100 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
101 break;
102 case VIRTIO_NET_HDR_GSO_TCPV6:
103 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
104 break;
105 default:
106 if (net_ratelimit())
107 printk(KERN_WARNING "%s: bad gso type %u.\n",
108 dev->name, hdr->gso_type);
109 goto frame_err;
110 }
111
Rusty Russell34a48572008-02-04 23:50:02 -0500112 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
113 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
114
Rusty Russell296f96f2007-10-22 11:03:37 +1000115 skb_shinfo(skb)->gso_size = hdr->gso_size;
116 if (skb_shinfo(skb)->gso_size == 0) {
117 if (net_ratelimit())
118 printk(KERN_WARNING "%s: zero gso size.\n",
119 dev->name);
120 goto frame_err;
121 }
122
123 /* Header must be checked, and gso_segs computed. */
124 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
125 skb_shinfo(skb)->gso_segs = 0;
126 }
127
128 netif_receive_skb(skb);
129 return;
130
131frame_err:
132 dev->stats.rx_frame_errors++;
133drop:
134 dev_kfree_skb(skb);
135}
136
137static void try_fill_recv(struct virtnet_info *vi)
138{
139 struct sk_buff *skb;
140 struct scatterlist sg[1+MAX_SKB_FRAGS];
141 int num, err;
142
Rusty Russell4d125de2007-11-07 16:34:49 +1100143 sg_init_table(sg, 1+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000144 for (;;) {
145 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
146 if (unlikely(!skb))
147 break;
148
149 skb_put(skb, MAX_PACKET_LEN);
150 vnet_hdr_to_sg(sg, skb);
151 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
152 skb_queue_head(&vi->recv, skb);
153
154 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
155 if (err) {
156 skb_unlink(skb, &vi->recv);
157 kfree_skb(skb);
158 break;
159 }
160 vi->num++;
161 }
162 if (unlikely(vi->num > vi->max))
163 vi->max = vi->num;
164 vi->rvq->vq_ops->kick(vi->rvq);
165}
166
Rusty Russell18445c42008-02-04 23:49:57 -0500167static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000168{
169 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500170 /* Schedule NAPI, Suppress further interrupts if successful. */
171 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
172 rvq->vq_ops->disable_cb(rvq);
173 __netif_rx_schedule(vi->dev, &vi->napi);
174 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000175}
176
177static int virtnet_poll(struct napi_struct *napi, int budget)
178{
179 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
180 struct sk_buff *skb = NULL;
181 unsigned int len, received = 0;
182
183again:
184 while (received < budget &&
185 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
186 __skb_unlink(skb, &vi->recv);
187 receive_skb(vi->dev, skb, len);
188 vi->num--;
189 received++;
190 }
191
192 /* FIXME: If we oom and completely run out of inbufs, we need
193 * to start a timer trying to fill more. */
194 if (vi->num < vi->max / 2)
195 try_fill_recv(vi);
196
Rusty Russell8329d982007-11-19 11:20:43 -0500197 /* Out of packets? */
198 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000199 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500200 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Rusty Russell296f96f2007-10-22 11:03:37 +1000201 && netif_rx_reschedule(vi->dev, napi))
202 goto again;
203 }
204
205 return received;
206}
207
208static void free_old_xmit_skbs(struct virtnet_info *vi)
209{
210 struct sk_buff *skb;
211 unsigned int len;
212
213 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
214 pr_debug("Sent skb %p\n", skb);
215 __skb_unlink(skb, &vi->send);
216 vi->dev->stats.tx_bytes += len;
217 vi->dev->stats.tx_packets++;
218 kfree_skb(skb);
219 }
220}
221
222static int start_xmit(struct sk_buff *skb, struct net_device *dev)
223{
224 struct virtnet_info *vi = netdev_priv(dev);
225 int num, err;
226 struct scatterlist sg[1+MAX_SKB_FRAGS];
227 struct virtio_net_hdr *hdr;
228 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
229 DECLARE_MAC_BUF(mac);
230
Rusty Russell4d125de2007-11-07 16:34:49 +1100231 sg_init_table(sg, 1+MAX_SKB_FRAGS);
232
Rusty Russell296f96f2007-10-22 11:03:37 +1000233 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
234
235 free_old_xmit_skbs(vi);
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)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500249 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000250 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500251 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000252 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();
Rusty Russell34a48572008-02-04 23:50:02 -0500259 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
260 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000261 } else {
262 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500263 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000264 }
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 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
270 if (err) {
271 pr_debug("%s: virtio not prepared to send\n", dev->name);
272 skb_unlink(skb, &vi->send);
273 netif_stop_queue(dev);
274 return NETDEV_TX_BUSY;
275 }
276 vi->svq->vq_ops->kick(vi->svq);
277
278 return 0;
279}
280
281static int virtnet_open(struct net_device *dev)
282{
283 struct virtnet_info *vi = netdev_priv(dev);
284
Rusty Russell296f96f2007-10-22 11:03:37 +1000285 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500286
287 /* If all buffers were filled by other side before we napi_enabled, we
288 * won't get another interrupt, so process any outstanding packets
289 * now. virtnet_poll wants re-enable the queue, so we disable here. */
290 vi->rvq->vq_ops->disable_cb(vi->rvq);
291 netif_rx_schedule(vi->dev, &vi->napi);
292
Rusty Russell296f96f2007-10-22 11:03:37 +1000293 return 0;
294}
295
296static int virtnet_close(struct net_device *dev)
297{
298 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000299
300 napi_disable(&vi->napi);
301
Rusty Russell296f96f2007-10-22 11:03:37 +1000302 return 0;
303}
304
305static int virtnet_probe(struct virtio_device *vdev)
306{
307 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000308 struct net_device *dev;
309 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000310
311 /* Allocate ourselves a network device with room for our info */
312 dev = alloc_etherdev(sizeof(struct virtnet_info));
313 if (!dev)
314 return -ENOMEM;
315
316 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000317 dev->open = virtnet_open;
318 dev->stop = virtnet_close;
319 dev->hard_start_xmit = start_xmit;
320 dev->features = NETIF_F_HIGHDMA;
321 SET_NETDEV_DEV(dev, &vdev->dev);
322
323 /* Do we support "hardware" checksums? */
Rusty Russell34a48572008-02-04 23:50:02 -0500324 if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000325 /* This opens up the world of extra features. */
326 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russell34a48572008-02-04 23:50:02 -0500327 if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) {
328 dev->features |= NETIF_F_TSO | NETIF_F_UFO
329 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
330 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000331 }
332
333 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500334 if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
335 vdev->config->get(vdev,
336 offsetof(struct virtio_net_config, mac),
337 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000338 } else
339 random_ether_addr(dev->dev_addr);
340
341 /* Set up our device-specific information */
342 vi = netdev_priv(dev);
343 netif_napi_add(dev, &vi->napi, virtnet_poll, 16);
344 vi->dev = dev;
345 vi->vdev = vdev;
346
347 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500348 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000349 if (IS_ERR(vi->rvq)) {
350 err = PTR_ERR(vi->rvq);
351 goto free;
352 }
353
Rusty Russella586d4f2008-02-04 23:49:56 -0500354 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000355 if (IS_ERR(vi->svq)) {
356 err = PTR_ERR(vi->svq);
357 goto free_recv;
358 }
359
360 /* Initialize our empty receive and send queues. */
361 skb_queue_head_init(&vi->recv);
362 skb_queue_head_init(&vi->send);
363
364 err = register_netdev(dev);
365 if (err) {
366 pr_debug("virtio_net: registering device failed\n");
367 goto free_send;
368 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500369
370 /* Last of all, set up some receive buffers. */
371 try_fill_recv(vi);
372
373 /* If we didn't even get one input buffer, we're useless. */
374 if (vi->num == 0) {
375 err = -ENOMEM;
376 goto unregister;
377 }
378
Rusty Russell296f96f2007-10-22 11:03:37 +1000379 pr_debug("virtnet: registered device %s\n", dev->name);
380 vdev->priv = vi;
381 return 0;
382
Rusty Russellb3369c12008-02-04 23:50:02 -0500383unregister:
384 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000385free_send:
386 vdev->config->del_vq(vi->svq);
387free_recv:
388 vdev->config->del_vq(vi->rvq);
389free:
390 free_netdev(dev);
391 return err;
392}
393
394static void virtnet_remove(struct virtio_device *vdev)
395{
Rusty Russell74b25532007-11-19 11:20:42 -0500396 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500397 struct sk_buff *skb;
398
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500399 /* Stop all the virtqueues. */
400 vdev->config->reset(vdev);
401
Rusty Russellb3369c12008-02-04 23:50:02 -0500402 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500403 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
404 kfree_skb(skb);
405 vi->num--;
406 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500407 while ((skb = __skb_dequeue(&vi->send)) != NULL)
408 kfree_skb(skb);
409
410 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500411
412 vdev->config->del_vq(vi->svq);
413 vdev->config->del_vq(vi->rvq);
414 unregister_netdev(vi->dev);
415 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000416}
417
418static struct virtio_device_id id_table[] = {
419 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
420 { 0 },
421};
422
423static struct virtio_driver virtio_net = {
424 .driver.name = KBUILD_MODNAME,
425 .driver.owner = THIS_MODULE,
426 .id_table = id_table,
427 .probe = virtnet_probe,
428 .remove = __devexit_p(virtnet_remove),
429};
430
431static int __init init(void)
432{
433 return register_virtio_driver(&virtio_net);
434}
435
436static void __exit fini(void)
437{
438 unregister_virtio_driver(&virtio_net);
439}
440module_init(init);
441module_exit(fini);
442
443MODULE_DEVICE_TABLE(virtio, id_table);
444MODULE_DESCRIPTION("Virtio network driver");
445MODULE_LICENSE("GPL");