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 void 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);
63 static void receive_skb(struct net_device *dev, struct sk_buff *skb,
66 struct virtio_net_hdr *hdr = skb_vnet_hdr(skb);
68 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
69 pr_debug("%s: short packet %i\n", dev->name, len);
70 dev->stats.rx_length_errors++;
73 len -= sizeof(struct virtio_net_hdr);
74 BUG_ON(len > MAX_PACKET_LEN);
77 skb->protocol = eth_type_trans(skb, dev);
78 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
79 ntohs(skb->protocol), skb->len, skb->pkt_type);
80 dev->stats.rx_bytes += skb->len;
81 dev->stats.rx_packets++;
83 if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
84 pr_debug("Needs csum!\n");
85 if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset))
89 if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
91 switch (hdr->gso_type) {
92 case VIRTIO_NET_HDR_GSO_TCPV4:
93 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
95 case VIRTIO_NET_HDR_GSO_TCPV4_ECN:
96 skb_shinfo(skb)->gso_type = SKB_GSO_TCP_ECN;
98 case VIRTIO_NET_HDR_GSO_UDP:
99 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
101 case VIRTIO_NET_HDR_GSO_TCPV6:
102 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
106 printk(KERN_WARNING "%s: bad gso type %u.\n",
107 dev->name, hdr->gso_type);
111 skb_shinfo(skb)->gso_size = hdr->gso_size;
112 if (skb_shinfo(skb)->gso_size == 0) {
114 printk(KERN_WARNING "%s: zero gso size.\n",
119 /* Header must be checked, and gso_segs computed. */
120 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
121 skb_shinfo(skb)->gso_segs = 0;
124 netif_receive_skb(skb);
128 dev->stats.rx_frame_errors++;
133 static void try_fill_recv(struct virtnet_info *vi)
136 struct scatterlist sg[1+MAX_SKB_FRAGS];
139 sg_init_table(sg, 1+MAX_SKB_FRAGS);
141 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN);
145 skb_put(skb, MAX_PACKET_LEN);
146 vnet_hdr_to_sg(sg, skb);
147 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
148 skb_queue_head(&vi->recv, skb);
150 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
152 skb_unlink(skb, &vi->recv);
158 if (unlikely(vi->num > vi->max))
160 vi->rvq->vq_ops->kick(vi->rvq);
163 static void skb_recv_done(struct virtqueue *rvq)
165 struct virtnet_info *vi = rvq->vdev->priv;
166 /* Schedule NAPI, Suppress further interrupts if successful. */
167 if (netif_rx_schedule_prep(vi->dev, &vi->napi)) {
168 rvq->vq_ops->disable_cb(rvq);
169 __netif_rx_schedule(vi->dev, &vi->napi);
173 static int virtnet_poll(struct napi_struct *napi, int budget)
175 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
176 struct sk_buff *skb = NULL;
177 unsigned int len, received = 0;
180 while (received < budget &&
181 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
182 __skb_unlink(skb, &vi->recv);
183 receive_skb(vi->dev, skb, len);
188 /* FIXME: If we oom and completely run out of inbufs, we need
189 * to start a timer trying to fill more. */
190 if (vi->num < vi->max / 2)
193 /* Out of packets? */
194 if (received < budget) {
195 netif_rx_complete(vi->dev, napi);
196 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
197 && netif_rx_reschedule(vi->dev, napi))
204 static void free_old_xmit_skbs(struct virtnet_info *vi)
209 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
210 pr_debug("Sent skb %p\n", skb);
211 __skb_unlink(skb, &vi->send);
212 vi->dev->stats.tx_bytes += len;
213 vi->dev->stats.tx_packets++;
218 static int start_xmit(struct sk_buff *skb, struct net_device *dev)
220 struct virtnet_info *vi = netdev_priv(dev);
222 struct scatterlist sg[1+MAX_SKB_FRAGS];
223 struct virtio_net_hdr *hdr;
224 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
225 DECLARE_MAC_BUF(mac);
227 sg_init_table(sg, 1+MAX_SKB_FRAGS);
229 pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest));
231 free_old_xmit_skbs(vi);
233 /* Encode metadata header at front. */
234 hdr = skb_vnet_hdr(skb);
235 if (skb->ip_summed == CHECKSUM_PARTIAL) {
236 hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
237 hdr->csum_start = skb->csum_start - skb_headroom(skb);
238 hdr->csum_offset = skb->csum_offset;
241 hdr->csum_offset = hdr->csum_start = 0;
244 if (skb_is_gso(skb)) {
245 hdr->hdr_len = skb_transport_header(skb) - skb->data;
246 hdr->gso_size = skb_shinfo(skb)->gso_size;
247 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
248 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4_ECN;
249 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
250 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
251 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
252 hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
253 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
254 hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
258 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
259 hdr->gso_size = hdr->hdr_len = 0;
262 vnet_hdr_to_sg(sg, skb);
263 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
264 __skb_queue_head(&vi->send, skb);
265 err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb);
267 pr_debug("%s: virtio not prepared to send\n", dev->name);
268 skb_unlink(skb, &vi->send);
269 netif_stop_queue(dev);
270 return NETDEV_TX_BUSY;
272 vi->svq->vq_ops->kick(vi->svq);
277 static int virtnet_open(struct net_device *dev)
279 struct virtnet_info *vi = netdev_priv(dev);
283 /* If we didn't even get one input buffer, we're useless. */
287 napi_enable(&vi->napi);
291 static int virtnet_close(struct net_device *dev)
293 struct virtnet_info *vi = netdev_priv(dev);
296 napi_disable(&vi->napi);
298 /* networking core has neutered skb_xmit_done/skb_recv_done, so don't
299 * worry about races vs. get(). */
300 vi->rvq->vq_ops->shutdown(vi->rvq);
301 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
305 vi->svq->vq_ops->shutdown(vi->svq);
306 while ((skb = __skb_dequeue(&vi->send)) != NULL)
309 BUG_ON(vi->num != 0);
313 static int virtnet_probe(struct virtio_device *vdev)
316 struct net_device *dev;
317 struct virtnet_info *vi;
319 /* Allocate ourselves a network device with room for our info */
320 dev = alloc_etherdev(sizeof(struct virtnet_info));
324 /* Set up network device as normal. */
326 dev->open = virtnet_open;
327 dev->stop = virtnet_close;
328 dev->hard_start_xmit = start_xmit;
329 dev->features = NETIF_F_HIGHDMA;
330 SET_NETDEV_DEV(dev, &vdev->dev);
332 /* Do we support "hardware" checksums? */
333 if (vdev->config->feature(vdev, VIRTIO_NET_F_NO_CSUM)) {
334 /* This opens up the world of extra features. */
335 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
336 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4))
337 dev->features |= NETIF_F_TSO;
338 if (vdev->config->feature(vdev, VIRTIO_NET_F_UFO))
339 dev->features |= NETIF_F_UFO;
340 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO4_ECN))
341 dev->features |= NETIF_F_TSO_ECN;
342 if (vdev->config->feature(vdev, VIRTIO_NET_F_TSO6))
343 dev->features |= 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");
382 pr_debug("virtnet: registered device %s\n", dev->name);
387 vdev->config->del_vq(vi->svq);
389 vdev->config->del_vq(vi->rvq);
395 static void virtnet_remove(struct virtio_device *vdev)
397 struct virtnet_info *vi = vdev->priv;
399 vdev->config->del_vq(vi->svq);
400 vdev->config->del_vq(vi->rvq);
401 unregister_netdev(vi->dev);
402 free_netdev(vi->dev);
405 static struct virtio_device_id id_table[] = {
406 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
410 static 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),
418 static int __init init(void)
420 return register_virtio_driver(&virtio_net);
423 static void __exit fini(void)
425 unregister_virtio_driver(&virtio_net);
430 MODULE_DEVICE_TABLE(virtio, id_table);
431 MODULE_DESCRIPTION("Virtio network driver");
432 MODULE_LICENSE("GPL");