1 /* A simple network driver using virtio.
3 * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
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.
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.
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
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>
27 static int csum = 1, gso = 1;
28 module_param(csum, bool, 0444);
29 module_param(gso, bool, 0444);
31 /* FIXME: MTU in config. */
32 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
36 struct virtio_device *vdev;
37 struct virtqueue *rvq, *svq;
38 struct net_device *dev;
39 struct napi_struct napi;
41 /* Number of input buffers, and max we've ever had. */
42 unsigned int num, max;
44 /* Receive & send queues. */
45 struct sk_buff_head recv;
46 struct sk_buff_head send;
49 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
51 return (struct virtio_net_hdr *)skb->cb;
54 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
56 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
59 static void skb_xmit_done(struct virtqueue *svq)
61 struct virtnet_info *vi = svq->vdev->priv;
63 /* Suppress further interrupts. */
64 svq->vq_ops->disable_cb(svq);
65 /* We were waiting for more output buffers. */
66 netif_wake_queue(vi->dev);
69 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
72 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
74 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
75 pr_debug("%s: short packet %i\n", dev->name, len);
76 dev->stats.rx_length_errors++;
79 len -= sizeof(struct virtio_net_hdr);
80 BUG_ON(len > MAX_PACKET_LEN);
83 skb->protocol = eth_type_trans(skb, dev);
84 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
85 ntohs(skb->protocol), skb->len, skb->pkt_type);
86 dev->stats.rx_bytes += skb->len;
87 dev->stats.rx_packets++;
89 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
90 pr_debug("Needs csum!\n");
91 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
95 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
97 switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
98 case VIRTIO_NET_HDR_GSO_TCPV4:
99 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
101 case VIRTIO_NET_HDR_GSO_UDP:
102 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
104 case VIRTIO_NET_HDR_GSO_TCPV6:
105 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
109 printk(KERN_WARNING "%s: bad gso type %u.\n",
110 dev->name, hdr->gso_type);
114 if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
115 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
117 skb_shinfo(skb)->gso_size = hdr->gso_size;
118 if (skb_shinfo(skb)->gso_size == 0) {
120 printk(KERN_WARNING "%s: zero gso size.\n",
125 /* Header must be checked, and gso_segs computed. */
126 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
127 skb_shinfo(skb)->gso_segs = 0;
130 netif_receive_skb(skb);
134 dev->stats.rx_frame_errors++;
139 static void try_fill_recv(struct virtnet_info *vi)
142 struct scatterlist sg[1+MAX_SKB_FRAGS];
145 sg_init_table(sg, 1+MAX_SKB_FRAGS);
147 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
151 skb_put(skb, MAX_PACKET_LEN);
152 vnet_hdr_to_sg(sg, skb);
153 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
154 skb_queue_head(&vi->recv, skb);
156 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
158 skb_unlink(skb, &vi->recv);
164 if (unlikely(vi->num > vi->max))
166 vi->rvq->vq_ops->kick(vi->rvq);
169 static void skb_recv_done(struct virtqueue *rvq)
171 struct virtnet_info *vi = rvq->vdev->priv;
172 /* Schedule NAPI, Suppress further interrupts if successful. */
173 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
174 rvq->vq_ops->disable_cb(rvq);
175 __netif_rx_schedule(vi->dev, &vi->napi);
179 static int virtnet_poll(struct napi_struct *napi, int budget)
181 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
182 struct sk_buff *skb = NULL;
183 unsigned int len, received = 0;
186 while (received < budget &&
187 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
188 __skb_unlink(skb, &vi->recv);
189 receive_skb(vi->dev, skb, len);
194 /* FIXME: If we oom and completely run out of inbufs, we need
195 * to start a timer trying to fill more. */
196 if (vi->num < vi->max / 2)
199 /* Out of packets? */
200 if (received < budget) {
201 netif_rx_complete(vi->dev, napi);
202 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
203 && netif_rx_reschedule(vi->dev, napi))
210 static void free_old_xmit_skbs(struct virtnet_info *vi)
215 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
216 pr_debug("Sent skb %p\n", skb);
217 __skb_unlink(skb, &vi->send);
218 vi->dev->stats.tx_bytes += len;
219 vi->dev->stats.tx_packets++;
224 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
226 struct virtnet_info *vi = netdev_priv(dev);
228 struct scatterlist sg[1+MAX_SKB_FRAGS];
229 struct virtio_net_hdr *hdr;
230 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
231 DECLARE_MAC_BUF(mac);
233 sg_init_table(sg, 1+MAX_SKB_FRAGS);
235 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
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;
245 hdr->csum_offset = hdr->csum_start = 0;
248 if (skb_is_gso(skb)) {
249 hdr->hdr_len = skb_transport_header(skb) - skb->data;
250 hdr->gso_size = skb_shinfo(skb)->gso_size;
251 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
252 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;
259 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
260 hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
262 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
263 hdr->gso_size = hdr->hdr_len = 0;
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);
271 /* Free up any pending old buffers before queueing new ones. */
272 free_old_xmit_skbs(vi);
273 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
275 pr_debug("%s: virtio not prepared to send\n", dev->name);
276 netif_stop_queue(dev);
278 /* Activate callback for using skbs: if this fails it
279 * means some were used in the meantime. */
280 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
281 printk("Unlikely: restart svq failed\n");
282 netif_start_queue(dev);
285 __skb_unlink(skb, &vi->send);
287 return NETDEV_TX_BUSY;
289 vi->svq->vq_ops->kick(vi->svq);
294 static int virtnet_open(struct net_device *dev)
296 struct virtnet_info *vi = netdev_priv(dev);
298 napi_enable(&vi->napi);
300 /* If all buffers were filled by other side before we napi_enabled, we
301 * won't get another interrupt, so process any outstanding packets
302 * now. virtnet_poll wants re-enable the queue, so we disable here. */
303 vi->rvq->vq_ops->disable_cb(vi->rvq);
304 netif_rx_schedule(vi->dev, &vi->napi);
309 static int virtnet_close(struct net_device *dev)
311 struct virtnet_info *vi = netdev_priv(dev);
313 napi_disable(&vi->napi);
318 static int virtnet_probe(struct virtio_device *vdev)
321 struct net_device *dev;
322 struct virtnet_info *vi;
324 /* Allocate ourselves a network device with room for our info */
325 dev = alloc_etherdev(sizeof(struct virtnet_info));
329 /* Set up network device as normal. */
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);
336 /* Do we support "hardware" checksums? */
337 if (csum && vdev->config->feature(vdev, VIRTIO_NET_F_CSUM)) {
338 /* This opens up the world of extra features. */
339 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
340 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;
346 /* Configuration may specify what MAC to use. Otherwise random. */
347 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);
352 random_ether_addr(dev->dev_addr);
354 /* Set up our device-specific information */
355 vi = netdev_priv(dev);
356 netif_napi_add(dev, &vi->napi, virtnet_poll, 16);
360 /* We expect two virtqueues, receive then send. */
361 vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done);
362 if (IS_ERR(vi->rvq)) {
363 err = PTR_ERR(vi->rvq);
367 vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done);
368 if (IS_ERR(vi->svq)) {
369 err = PTR_ERR(vi->svq);
373 /* Initialize our empty receive and send queues. */
374 skb_queue_head_init(&vi->recv);
375 skb_queue_head_init(&vi->send);
377 err = register_netdev(dev);
379 pr_debug("virtio_net: registering device failed\n");
383 /* Last of all, set up some receive buffers. */
386 /* If we didn't even get one input buffer, we're useless. */
392 pr_debug("virtnet: registered device %s\n", dev->name);
397 unregister_netdev(dev);
399 vdev->config->del_vq(vi->svq);
401 vdev->config->del_vq(vi->rvq);
407 static void virtnet_remove(struct virtio_device *vdev)
409 struct virtnet_info *vi = vdev->priv;
412 /* Stop all the virtqueues. */
413 vdev->config->reset(vdev);
415 /* Free our skbs in send and recv queues, if any. */
416 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
420 while ((skb = __skb_dequeue(&vi->send)) != NULL)
423 BUG_ON(vi->num != 0);
425 vdev->config->del_vq(vi->svq);
426 vdev->config->del_vq(vi->rvq);
427 unregister_netdev(vi->dev);
428 free_netdev(vi->dev);
431 static struct virtio_device_id id_table[] = {
432 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
436 static struct virtio_driver virtio_net = {
437 .driver.name = KBUILD_MODNAME,
438 .driver.owner = THIS_MODULE,
439 .id_table = id_table,
440 .probe = virtnet_probe,
441 .remove = __devexit_p(virtnet_remove),
444 static int __init init(void)
446 return register_virtio_driver(&virtio_net);
449 static void __exit fini(void)
451 unregister_virtio_driver(&virtio_net);
456 MODULE_DEVICE_TABLE(virtio, id_table);
457 MODULE_DESCRIPTION("Virtio network driver");
458 MODULE_LICENSE("GPL");