blob: e575df83e5c21f23ff68028437ae79887119d540 [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
Dor Laor6c0cd7c2007-12-16 15:19:43 +020027static int napi_weight = 128;
28module_param(napi_weight, int, 0444);
29
Rusty Russell34a48572008-02-04 23:50:02 -050030static int csum = 1, gso = 1;
31module_param(csum, bool, 0444);
32module_param(gso, bool, 0444);
33
Rusty Russell296f96f2007-10-22 11:03:37 +100034/* FIXME: MTU in config. */
35#define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
36
37struct virtnet_info
38{
39 struct virtio_device *vdev;
40 struct virtqueue *rvq, *svq;
41 struct net_device *dev;
42 struct napi_struct napi;
43
44 /* Number of input buffers, and max we've ever had. */
45 unsigned int num, max;
46
47 /* Receive & send queues. */
48 struct sk_buff_head recv;
49 struct sk_buff_head send;
50};
51
52static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
53{
54 return (struct virtio_net_hdr *)skb->cb;
55}
56
57static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
58{
59 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
60}
61
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050062static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +100063{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050064 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +100065
Rusty Russell2cb9c6b2008-02-04 23:50:07 -050066 /* Suppress further interrupts. */
67 svq->vq_ops->disable_cb(svq);
68 /* We were waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +100069 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +100070}
71
72static void receive_skb(struct net_device *dev, struct sk_buff *skb,
73 unsigned len)
74{
75 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
76
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;
81 }
82 len -= sizeof(struct virtio_net_hdr);
83 BUG_ON(len > MAX_PACKET_LEN);
84
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++;
91
92 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
93 pr_debug("Needs csum!\n");
Rusty Russellf35d9d82008-02-04 23:49:54 -050094 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +100095 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +100096 }
97
98 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
99 pr_debug("GSO!\n");
Rusty Russell34a48572008-02-04 23:50:02 -0500100 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000101 case VIRTIO_NET_HDR_GSO_TCPV4:
102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
103 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000104 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;
115 }
116
Rusty Russell34a48572008-02-04 23:50:02 -0500117 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
118 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
119
Rusty Russell296f96f2007-10-22 11:03:37 +1000120 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;
126 }
127
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;
131 }
132
133 netif_receive_skb(skb);
134 return;
135
136frame_err:
137 dev->stats.rx_frame_errors++;
138drop:
139 dev_kfree_skb(skb);
140}
141
142static void try_fill_recv(struct virtnet_info *vi)
143{
144 struct sk_buff *skb;
145 struct scatterlist sg[1+MAX_SKB_FRAGS];
146 int num, err;
147
Rusty Russell4d125de2007-11-07 16:34:49 +1100148 sg_init_table(sg, 1+MAX_SKB_FRAGS);
Rusty Russell296f96f2007-10-22 11:03:37 +1000149 for (;;) {
150 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
151 if (unlikely(!skb))
152 break;
153
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);
158
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;
164 }
165 vi->num++;
166 }
167 if (unlikely(vi->num > vi->max))
168 vi->max = vi->num;
169 vi->rvq->vq_ops->kick(vi->rvq);
170}
171
Rusty Russell18445c42008-02-04 23:49:57 -0500172static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000173{
174 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500175 /* 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);
179 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000180}
181
182static int virtnet_poll(struct napi_struct *napi, int budget)
183{
184 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
185 struct sk_buff *skb = NULL;
186 unsigned int len, received = 0;
187
188again:
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++;
195 }
196
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);
201
Rusty Russell8329d982007-11-19 11:20:43 -0500202 /* Out of packets? */
203 if (received < budget) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000204 netif_rx_complete(vi->dev, napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500205 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Rusty Russell296f96f2007-10-22 11:03:37 +1000206 && netif_rx_reschedule(vi->dev, napi))
207 goto again;
208 }
209
210 return received;
211}
212
213static void free_old_xmit_skbs(struct virtnet_info *vi)
214{
215 struct sk_buff *skb;
216 unsigned int len;
217
218 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
219 pr_debug("Sent skb %p\n", skb);
220 __skb_unlink(skb, &vi->send);
221 vi->dev->stats.tx_bytes += len;
222 vi->dev->stats.tx_packets++;
223 kfree_skb(skb);
224 }
225}
226
227static int start_xmit(struct sk_buff *skb, struct net_device *dev)
228{
229 struct virtnet_info *vi = netdev_priv(dev);
230 int num, err;
231 struct scatterlist sg[1+MAX_SKB_FRAGS];
232 struct virtio_net_hdr *hdr;
233 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
234 DECLARE_MAC_BUF(mac);
235
Rusty Russell4d125de2007-11-07 16:34:49 +1100236 sg_init_table(sg, 1+MAX_SKB_FRAGS);
237
Rusty Russell296f96f2007-10-22 11:03:37 +1000238 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
239
Rusty Russell296f96f2007-10-22 11:03:37 +1000240 /* Encode metadata header at front. */
241 hdr = skb_vnet_hdr(skb);
242 if (skb->ip_summed == CHECKSUM_PARTIAL) {
243 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
244 hdr->csum_start = skb->csum_start - skb_headroom(skb);
245 hdr->csum_offset = skb->csum_offset;
246 } else {
247 hdr->flags = 0;
248 hdr->csum_offset = hdr->csum_start = 0;
249 }
250
251 if (skb_is_gso(skb)) {
Rusty Russell50c8ea82008-02-04 23:50:01 -0500252 hdr->hdr_len = skb_transport_header(skb) - skb->data;
Rusty Russell296f96f2007-10-22 11:03:37 +1000253 hdr->gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500254 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russell296f96f2007-10-22 11:03:37 +1000255 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
256 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
257 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
258 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
259 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
260 else
261 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500262 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
263 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000264 } else {
265 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
Rusty Russell50c8ea82008-02-04 23:50:01 -0500266 hdr->gso_size = hdr->hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000267 }
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);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500272
273again:
274 /* Free up any pending old buffers before queueing new ones. */
275 free_old_xmit_skbs(vi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000276 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
277 if (err) {
278 pr_debug("%s: virtio not prepared to send\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000279 netif_stop_queue(dev);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500280
281 /* Activate callback for using skbs: if this fails it
282 * means some were used in the meantime. */
283 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
284 printk("Unlikely: restart svq failed\n");
285 netif_start_queue(dev);
286 goto again;
287 }
288 __skb_unlink(skb, &vi->send);
289
Rusty Russell296f96f2007-10-22 11:03:37 +1000290 return NETDEV_TX_BUSY;
291 }
292 vi->svq->vq_ops->kick(vi->svq);
293
294 return 0;
295}
296
Amit Shahda74e892008-02-29 16:24:50 +0530297#ifdef CONFIG_NET_POLL_CONTROLLER
298static void virtnet_netpoll(struct net_device *dev)
299{
300 struct virtnet_info *vi = netdev_priv(dev);
301
302 napi_schedule(&vi->napi);
303}
304#endif
305
Rusty Russell296f96f2007-10-22 11:03:37 +1000306static int virtnet_open(struct net_device *dev)
307{
308 struct virtnet_info *vi = netdev_priv(dev);
309
Rusty Russell296f96f2007-10-22 11:03:37 +1000310 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500311
312 /* If all buffers were filled by other side before we napi_enabled, we
313 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100314 * now. virtnet_poll wants re-enable the queue, so we disable here.
315 * We synchronize against interrupts via NAPI_STATE_SCHED */
316 if (netif_rx_schedule_prep(dev, &vi->napi)) {
317 vi->rvq->vq_ops->disable_cb(vi->rvq);
318 __netif_rx_schedule(dev, &vi->napi);
319 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000320 return 0;
321}
322
323static int virtnet_close(struct net_device *dev)
324{
325 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000326
327 napi_disable(&vi->napi);
328
Rusty Russell296f96f2007-10-22 11:03:37 +1000329 return 0;
330}
331
332static int virtnet_probe(struct virtio_device *vdev)
333{
334 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000335 struct net_device *dev;
336 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000337
338 /* Allocate ourselves a network device with room for our info */
339 dev = alloc_etherdev(sizeof(struct virtnet_info));
340 if (!dev)
341 return -ENOMEM;
342
343 /* Set up network device as normal. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000344 dev->open = virtnet_open;
345 dev->stop = virtnet_close;
346 dev->hard_start_xmit = start_xmit;
347 dev->features = NETIF_F_HIGHDMA;
Amit Shahda74e892008-02-29 16:24:50 +0530348#ifdef CONFIG_NET_POLL_CONTROLLER
349 dev->poll_controller = virtnet_netpoll;
350#endif
Rusty Russell296f96f2007-10-22 11:03:37 +1000351 SET_NETDEV_DEV(dev, &vdev->dev);
352
353 /* Do we support "hardware" checksums? */
Rusty Russell34a48572008-02-04 23:50:02 -0500354 if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000355 /* This opens up the world of extra features. */
356 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russell34a48572008-02-04 23:50:02 -0500357 if (gso && vdev->config->feature(vdev, VIRTIO_NET_F_GSO)) {
358 dev->features |= NETIF_F_TSO | NETIF_F_UFO
359 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
360 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000361 }
362
363 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500364 if (vdev->config->feature(vdev, VIRTIO_NET_F_MAC)) {
365 vdev->config->get(vdev,
366 offsetof(struct virtio_net_config, mac),
367 dev->dev_addr, dev->addr_len);
Rusty Russell296f96f2007-10-22 11:03:37 +1000368 } else
369 random_ether_addr(dev->dev_addr);
370
371 /* Set up our device-specific information */
372 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200373 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000374 vi->dev = dev;
375 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100376 vdev->priv = vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000377
378 /* We expect two virtqueues, receive then send. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500379 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000380 if (IS_ERR(vi->rvq)) {
381 err = PTR_ERR(vi->rvq);
382 goto free;
383 }
384
Rusty Russella586d4f2008-02-04 23:49:56 -0500385 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000386 if (IS_ERR(vi->svq)) {
387 err = PTR_ERR(vi->svq);
388 goto free_recv;
389 }
390
391 /* Initialize our empty receive and send queues. */
392 skb_queue_head_init(&vi->recv);
393 skb_queue_head_init(&vi->send);
394
395 err = register_netdev(dev);
396 if (err) {
397 pr_debug("virtio_net: registering device failed\n");
398 goto free_send;
399 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500400
401 /* Last of all, set up some receive buffers. */
402 try_fill_recv(vi);
403
404 /* If we didn't even get one input buffer, we're useless. */
405 if (vi->num == 0) {
406 err = -ENOMEM;
407 goto unregister;
408 }
409
Rusty Russell296f96f2007-10-22 11:03:37 +1000410 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000411 return 0;
412
Rusty Russellb3369c12008-02-04 23:50:02 -0500413unregister:
414 unregister_netdev(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000415free_send:
416 vdev->config->del_vq(vi->svq);
417free_recv:
418 vdev->config->del_vq(vi->rvq);
419free:
420 free_netdev(dev);
421 return err;
422}
423
424static void virtnet_remove(struct virtio_device *vdev)
425{
Rusty Russell74b25532007-11-19 11:20:42 -0500426 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500427 struct sk_buff *skb;
428
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500429 /* Stop all the virtqueues. */
430 vdev->config->reset(vdev);
431
Rusty Russellb3369c12008-02-04 23:50:02 -0500432 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500433 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
434 kfree_skb(skb);
435 vi->num--;
436 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500437 while ((skb = __skb_dequeue(&vi->send)) != NULL)
438 kfree_skb(skb);
439
440 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500441
442 vdev->config->del_vq(vi->svq);
443 vdev->config->del_vq(vi->rvq);
444 unregister_netdev(vi->dev);
445 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000446}
447
448static struct virtio_device_id id_table[] = {
449 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
450 { 0 },
451};
452
453static struct virtio_driver virtio_net = {
454 .driver.name = KBUILD_MODNAME,
455 .driver.owner = THIS_MODULE,
456 .id_table = id_table,
457 .probe = virtnet_probe,
458 .remove = __devexit_p(virtnet_remove),
459};
460
461static int __init init(void)
462{
463 return register_virtio_driver(&virtio_net);
464}
465
466static void __exit fini(void)
467{
468 unregister_virtio_driver(&virtio_net);
469}
470module_init(init);
471module_exit(fini);
472
473MODULE_DEVICE_TABLE(virtio, id_table);
474MODULE_DESCRIPTION("Virtio network driver");
475MODULE_LICENSE("GPL");