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 /* FIXME: MTU in config. */
28 #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN)
32 struct virtio_device *vdev;
33 struct virtqueue *rvq, *svq;
34 struct net_device *dev;
35 struct napi_struct napi;
37 /* Number of input buffers, and max we've ever had. */
38 unsigned int num, max;
40 /* Receive & send queues. */
41 struct sk_buff_head recv;
42 struct sk_buff_head send;
45 static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb)
47 return (struct virtio_net_hdr *)skb->cb;
50 static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb)
52 sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr));
55 static bool skb_xmit_done(struct virtqueue *rvq)
57 struct virtnet_info *vi = rvq->vdev->priv;
59 /* In case we were waiting for output buffers. */
60 netif_wake_queue(vi->dev);
64 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
67 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
69 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
70 pr_debug("%s: short packet %i\n", dev->name, len);
71 dev->stats.rx_length_errors++;
74 len -= sizeof(struct virtio_net_hdr);
75 BUG_ON(len > MAX_PACKET_LEN);
78 skb->protocol = eth_type_trans(skb, dev);
79 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
80 ntohs(skb->protocol), skb->len, skb->pkt_type);
81 dev->stats.rx_bytes += skb->len;
82 dev->stats.rx_packets++;
84 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
85 pr_debug("Needs csum!\n");
86 skb->ip_summed = CHECKSUM_PARTIAL;
87 skb->csum_start = hdr->csum_start;
88 skb->csum_offset = hdr->csum_offset;
89 if (skb->csum_start > skb->len - 2
90 || skb->csum_offset > skb->len - 2) {
92 printk(KERN_WARNING "%s: csum=%u/%u len=%u\n",
93 dev->name, skb->csum_start,
94 skb->csum_offset, skb->len);
99 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
101 switch (hdr->gso_type) {
102 case VIRTIO_NET_HDR_GSO_TCPV4:
103 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
105 case VIRTIO_NET_HDR_GSO_TCPV4_ECN:
106 skb_shinfo(skb)->gso_type = SKB_GSO_TCP_ECN;
108 case VIRTIO_NET_HDR_GSO_UDP:
109 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
111 case VIRTIO_NET_HDR_GSO_TCPV6:
112 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
116 printk(KERN_WARNING "%s: bad gso type %u.\n",
117 dev->name, hdr->gso_type);
121 skb_shinfo(skb)->gso_size = hdr->gso_size;
122 if (skb_shinfo(skb)->gso_size == 0) {
124 printk(KERN_WARNING "%s: zero gso size.\n",
129 /* Header must be checked, and gso_segs computed. */
130 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
131 skb_shinfo(skb)->gso_segs = 0;
134 netif_receive_skb(skb);
138 dev->stats.rx_frame_errors++;
143 static void try_fill_recv(struct virtnet_info *vi)
146 struct scatterlist sg[1+MAX_SKB_FRAGS];
149 sg_init_table(sg, 1+MAX_SKB_FRAGS);
151 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
155 skb_put(skb, MAX_PACKET_LEN);
156 vnet_hdr_to_sg(sg, skb);
157 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
158 skb_queue_head(&vi->recv, skb);
160 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
162 skb_unlink(skb, &vi->recv);
168 if (unlikely(vi->num > vi->max))
170 vi->rvq->vq_ops->kick(vi->rvq);
173 static bool skb_recv_done(struct virtqueue *rvq)
175 struct virtnet_info *vi = rvq->vdev->priv;
176 netif_rx_schedule(vi->dev, &vi->napi);
177 /* Suppress further interrupts. */
181 static int virtnet_poll(struct napi_struct *napi, int budget)
183 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
184 struct sk_buff *skb = NULL;
185 unsigned int len, received = 0;
188 while (received < budget &&
189 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
190 __skb_unlink(skb, &vi->recv);
191 receive_skb(vi->dev, skb, len);
196 /* FIXME: If we oom and completely run out of inbufs, we need
197 * to start a timer trying to fill more. */
198 if (vi->num < vi->max / 2)
201 /* Out of packets? */
202 if (received < budget) {
203 netif_rx_complete(vi->dev, napi);
204 if (unlikely(!vi->rvq->vq_ops->restart(vi->rvq))
205 && netif_rx_reschedule(vi->dev, napi))
212 static void free_old_xmit_skbs(struct virtnet_info *vi)
217 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
218 pr_debug("Sent skb %p\n", skb);
219 __skb_unlink(skb, &vi->send);
220 vi->dev->stats.tx_bytes += len;
221 vi->dev->stats.tx_packets++;
226 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
228 struct virtnet_info *vi = netdev_priv(dev);
230 struct scatterlist sg[1+MAX_SKB_FRAGS];
231 struct virtio_net_hdr *hdr;
232 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
233 DECLARE_MAC_BUF(mac);
235 sg_init_table(sg, 1+MAX_SKB_FRAGS);
237 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
239 free_old_xmit_skbs(vi);
241 /* Encode metadata header at front. */
242 hdr = skb_vnet_hdr(skb);
243 if (skb->ip_summed == CHECKSUM_PARTIAL) {
244 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
245 hdr->csum_start = skb->csum_start - skb_headroom(skb);
246 hdr->csum_offset = skb->csum_offset;
249 hdr->csum_offset = hdr->csum_start = 0;
252 if (skb_is_gso(skb)) {
253 hdr->gso_size = skb_shinfo(skb)->gso_size;
254 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
255 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4_ECN;
256 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
257 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
258 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
259 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
260 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
261 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
265 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
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);
272 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
274 pr_debug("%s: virtio not prepared to send\n", dev->name);
275 skb_unlink(skb, &vi->send);
276 netif_stop_queue(dev);
277 return NETDEV_TX_BUSY;
279 vi->svq->vq_ops->kick(vi->svq);
284 static int virtnet_open(struct net_device *dev)
286 struct virtnet_info *vi = netdev_priv(dev);
290 /* If we didn't even get one input buffer, we're useless. */
294 napi_enable(&vi->napi);
298 static int virtnet_close(struct net_device *dev)
300 struct virtnet_info *vi = netdev_priv(dev);
303 napi_disable(&vi->napi);
305 /* networking core has neutered skb_xmit_done/skb_recv_done, so don't
306 * worry about races vs. get(). */
307 vi->rvq->vq_ops->shutdown(vi->rvq);
308 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
312 vi->svq->vq_ops->shutdown(vi->svq);
313 while ((skb = __skb_dequeue(&vi->send)) != NULL)
316 BUG_ON(vi->num != 0);
320 static int virtnet_probe(struct virtio_device *vdev)
324 struct net_device *dev;
325 struct virtnet_info *vi;
328 /* Allocate ourselves a network device with room for our info */
329 dev = alloc_etherdev(sizeof(struct virtnet_info));
333 /* Set up network device as normal. */
335 dev->open = virtnet_open;
336 dev->stop = virtnet_close;
337 dev->hard_start_xmit = start_xmit;
338 dev->features = NETIF_F_HIGHDMA;
339 SET_NETDEV_DEV(dev, &vdev->dev);
341 /* Do we support "hardware" checksums? */
342 token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_F, &len);
343 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_NO_CSUM)) {
344 /* This opens up the world of extra features. */
345 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
346 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4))
347 dev->features |= NETIF_F_TSO;
348 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_UFO))
349 dev->features |= NETIF_F_UFO;
350 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4_ECN))
351 dev->features |= NETIF_F_TSO_ECN;
352 if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO6))
353 dev->features |= NETIF_F_TSO6;
356 /* Configuration may specify what MAC to use. Otherwise random. */
357 token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_MAC_F, &len);
360 vdev->config->get(vdev, token, dev->dev_addr, len);
362 random_ether_addr(dev->dev_addr);
364 /* Set up our device-specific information */
365 vi = netdev_priv(dev);
366 netif_napi_add(dev, &vi->napi, virtnet_poll, 16);
370 /* We expect two virtqueues, receive then send. */
371 vi->rvq = vdev->config->find_vq(vdev, skb_recv_done);
372 if (IS_ERR(vi->rvq)) {
373 err = PTR_ERR(vi->rvq);
377 vi->svq = vdev->config->find_vq(vdev, skb_xmit_done);
378 if (IS_ERR(vi->svq)) {
379 err = PTR_ERR(vi->svq);
383 /* Initialize our empty receive and send queues. */
384 skb_queue_head_init(&vi->recv);
385 skb_queue_head_init(&vi->send);
387 err = register_netdev(dev);
389 pr_debug("virtio_net: registering device failed\n");
392 pr_debug("virtnet: registered device %s\n", dev->name);
397 vdev->config->del_vq(vi->svq);
399 vdev->config->del_vq(vi->rvq);
405 static void virtnet_remove(struct virtio_device *vdev)
407 struct virtnet_info *vi = vdev->priv;
409 vdev->config->del_vq(vi->svq);
410 vdev->config->del_vq(vi->rvq);
411 unregister_netdev(vi->dev);
412 free_netdev(vi->dev);
415 static struct virtio_device_id id_table[] = {
416 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
420 static struct virtio_driver virtio_net = {
421 .driver.name = KBUILD_MODNAME,
422 .driver.owner = THIS_MODULE,
423 .id_table = id_table,
424 .probe = virtnet_probe,
425 .remove = __devexit_p(virtnet_remove),
428 static int __init init(void)
430 return register_virtio_driver(&virtio_net);
433 static void __exit fini(void)
435 unregister_virtio_driver(&virtio_net);
440 MODULE_DEVICE_TABLE(virtio, id_table);
441 MODULE_DESCRIPTION("Virtio network driver");
442 MODULE_LICENSE("GPL");