blob: 73f01db59ab9e4c351d0efd9a0c3a7b7e928838d [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
285 try_fill_recv(vi);
286
287 /* If we didn't even get one input buffer, we're useless. */
288 if (vi->num == 0)
289 return -ENOMEM;
290
291 napi_enable(&vi->napi);
292 return 0;
293}
294
295static int virtnet_close(struct net_device *dev)
296{
297 struct virtnet_info *vi = netdev_priv(dev);
298 struct sk_buff *skb;
299
300 napi_disable(&vi->napi);
301
302 /* networking core has neutered skb_xmit_done/skb_recv_done, so don't
303 * worry about races vs. get(). */
304 vi->rvq->vq_ops->shutdown(vi->rvq);
305 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
306 kfree_skb(skb);
307 vi->num--;
308 }
309 vi->svq->vq_ops->shutdown(vi->svq);
310 while ((skb = __skb_dequeue(&vi->send)) != NULL)
311 kfree_skb(skb);
312
313 BUG_ON(vi->num != 0);
314 return 0;
315}
316
317static int virtnet_probe(struct virtio_device *vdev)
318{
319 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000320 struct net_device *dev;
321 struct virtnet_info *vi;
Rusty Russell296f96f2007-10-22 11:03:37 +1000322
323 /* Allocate ourselves a network device with room for our info */
324 dev = alloc_etherdev(sizeof(struct virtnet_info));
325 if (!dev)
326 return -ENOMEM;
327
328 /* Set up network device as normal. */
329 ether_setup(dev);
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? */
Rusty Russell34a48572008-02-04 23:50:02 -0500337 if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000338 /* This opens up the world of extra features. */
339 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russell34a48572008-02-04 23:50:02 -0500340 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 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000344 }
345
346 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500347 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);
Rusty Russell296f96f2007-10-22 11:03:37 +1000351 } 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. */
Rusty Russella586d4f2008-02-04 23:49:56 -0500361 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000362 if (IS_ERR(vi->rvq)) {
363 err = PTR_ERR(vi->rvq);
364 goto free;
365 }
366
Rusty Russella586d4f2008-02-04 23:49:56 -0500367 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
Rusty Russell296f96f2007-10-22 11:03:37 +1000368 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 pr_debug("virtnet: registered device %s\n", dev->name);
383 vdev->priv = vi;
384 return 0;
385
386free_send:
387 vdev->config->del_vq(vi->svq);
388free_recv:
389 vdev->config->del_vq(vi->rvq);
390free:
391 free_netdev(dev);
392 return err;
393}
394
395static void virtnet_remove(struct virtio_device *vdev)
396{
Rusty Russell74b25532007-11-19 11:20:42 -0500397 struct virtnet_info *vi = vdev->priv;
398
399 vdev->config->del_vq(vi->svq);
400 vdev->config->del_vq(vi->rvq);
401 unregister_netdev(vi->dev);
402 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000403}
404
405static struct virtio_device_id id_table[] = {
406 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
407 { 0 },
408};
409
410static struct virtio_driver virtio_net = {
411 .driver.name = KBUILD_MODNAME,
412 .driver.owner = THIS_MODULE,
413 .id_table = id_table,
414 .probe = virtnet_probe,
415 .remove = __devexit_p(virtnet_remove),
416};
417
418static int __init init(void)
419{
420 return register_virtio_driver(&virtio_net);
421}
422
423static void __exit fini(void)
424{
425 unregister_virtio_driver(&virtio_net);
426}
427module_init(init);
428module_exit(fini);
429
430MODULE_DEVICE_TABLE(virtio, id_table);
431MODULE_DESCRIPTION("Virtio network driver");
432MODULE_LICENSE("GPL");