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