1296f96fcSRusty Russell /* A simple network driver using virtio. 2296f96fcSRusty Russell * 3296f96fcSRusty Russell * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation 4296f96fcSRusty Russell * 5296f96fcSRusty Russell * This program is free software; you can redistribute it and/or modify 6296f96fcSRusty Russell * it under the terms of the GNU General Public License as published by 7296f96fcSRusty Russell * the Free Software Foundation; either version 2 of the License, or 8296f96fcSRusty Russell * (at your option) any later version. 9296f96fcSRusty Russell * 10296f96fcSRusty Russell * This program is distributed in the hope that it will be useful, 11296f96fcSRusty Russell * but WITHOUT ANY WARRANTY; without even the implied warranty of 12296f96fcSRusty Russell * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13296f96fcSRusty Russell * GNU General Public License for more details. 14296f96fcSRusty Russell * 15296f96fcSRusty Russell * You should have received a copy of the GNU General Public License 16296f96fcSRusty Russell * along with this program; if not, write to the Free Software 17296f96fcSRusty Russell * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18296f96fcSRusty Russell */ 19296f96fcSRusty Russell //#define DEBUG 20296f96fcSRusty Russell #include <linux/netdevice.h> 21296f96fcSRusty Russell #include <linux/etherdevice.h> 22296f96fcSRusty Russell #include <linux/module.h> 23296f96fcSRusty Russell #include <linux/virtio.h> 24296f96fcSRusty Russell #include <linux/virtio_net.h> 25296f96fcSRusty Russell #include <linux/scatterlist.h> 26296f96fcSRusty Russell 27296f96fcSRusty Russell /* FIXME: MTU in config. */ 28296f96fcSRusty Russell #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) 29296f96fcSRusty Russell 30296f96fcSRusty Russell struct virtnet_info 31296f96fcSRusty Russell { 32296f96fcSRusty Russell struct virtio_device *vdev; 33296f96fcSRusty Russell struct virtqueue *rvq, *svq; 34296f96fcSRusty Russell struct net_device *dev; 35296f96fcSRusty Russell struct napi_struct napi; 36296f96fcSRusty Russell 37296f96fcSRusty Russell /* Number of input buffers, and max we've ever had. */ 38296f96fcSRusty Russell unsigned int num, max; 39296f96fcSRusty Russell 40296f96fcSRusty Russell /* Receive & send queues. */ 41296f96fcSRusty Russell struct sk_buff_head recv; 42296f96fcSRusty Russell struct sk_buff_head send; 43296f96fcSRusty Russell }; 44296f96fcSRusty Russell 45296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) 46296f96fcSRusty Russell { 47296f96fcSRusty Russell return (struct virtio_net_hdr *)skb->cb; 48296f96fcSRusty Russell } 49296f96fcSRusty Russell 50296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) 51296f96fcSRusty Russell { 52296f96fcSRusty Russell sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); 53296f96fcSRusty Russell } 54296f96fcSRusty Russell 55296f96fcSRusty Russell static bool skb_xmit_done(struct virtqueue *rvq) 56296f96fcSRusty Russell { 57296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 58296f96fcSRusty Russell 59296f96fcSRusty Russell /* In case we were waiting for output buffers. */ 60296f96fcSRusty Russell netif_wake_queue(vi->dev); 61296f96fcSRusty Russell return true; 62296f96fcSRusty Russell } 63296f96fcSRusty Russell 64296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb, 65296f96fcSRusty Russell unsigned len) 66296f96fcSRusty Russell { 67296f96fcSRusty Russell struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 68296f96fcSRusty Russell 69296f96fcSRusty Russell if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 70296f96fcSRusty Russell pr_debug("%s: short packet %i\n", dev->name, len); 71296f96fcSRusty Russell dev->stats.rx_length_errors++; 72296f96fcSRusty Russell goto drop; 73296f96fcSRusty Russell } 74296f96fcSRusty Russell len -= sizeof(struct virtio_net_hdr); 75296f96fcSRusty Russell BUG_ON(len > MAX_PACKET_LEN); 76296f96fcSRusty Russell 77296f96fcSRusty Russell skb_trim(skb, len); 78296f96fcSRusty Russell skb->protocol = eth_type_trans(skb, dev); 79296f96fcSRusty Russell pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 80296f96fcSRusty Russell ntohs(skb->protocol), skb->len, skb->pkt_type); 81296f96fcSRusty Russell dev->stats.rx_bytes += skb->len; 82296f96fcSRusty Russell dev->stats.rx_packets++; 83296f96fcSRusty Russell 84296f96fcSRusty Russell if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 85296f96fcSRusty Russell pr_debug("Needs csum!\n"); 86*f35d9d8aSRusty Russell if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) 87296f96fcSRusty Russell goto frame_err; 88296f96fcSRusty Russell } 89296f96fcSRusty Russell 90296f96fcSRusty Russell if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { 91296f96fcSRusty Russell pr_debug("GSO!\n"); 92296f96fcSRusty Russell switch (hdr->gso_type) { 93296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 94296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 95296f96fcSRusty Russell break; 96296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4_ECN: 97296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCP_ECN; 98296f96fcSRusty Russell break; 99296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 100296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 101296f96fcSRusty Russell break; 102296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 103296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 104296f96fcSRusty Russell break; 105296f96fcSRusty Russell default: 106296f96fcSRusty Russell if (net_ratelimit()) 107296f96fcSRusty Russell printk(KERN_WARNING "%s: bad gso type %u.\n", 108296f96fcSRusty Russell dev->name, hdr->gso_type); 109296f96fcSRusty Russell goto frame_err; 110296f96fcSRusty Russell } 111296f96fcSRusty Russell 112296f96fcSRusty Russell skb_shinfo(skb)->gso_size = hdr->gso_size; 113296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 114296f96fcSRusty Russell if (net_ratelimit()) 115296f96fcSRusty Russell printk(KERN_WARNING "%s: zero gso size.\n", 116296f96fcSRusty Russell dev->name); 117296f96fcSRusty Russell goto frame_err; 118296f96fcSRusty Russell } 119296f96fcSRusty Russell 120296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 121296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 122296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 123296f96fcSRusty Russell } 124296f96fcSRusty Russell 125296f96fcSRusty Russell netif_receive_skb(skb); 126296f96fcSRusty Russell return; 127296f96fcSRusty Russell 128296f96fcSRusty Russell frame_err: 129296f96fcSRusty Russell dev->stats.rx_frame_errors++; 130296f96fcSRusty Russell drop: 131296f96fcSRusty Russell dev_kfree_skb(skb); 132296f96fcSRusty Russell } 133296f96fcSRusty Russell 134296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi) 135296f96fcSRusty Russell { 136296f96fcSRusty Russell struct sk_buff *skb; 137296f96fcSRusty Russell struct scatterlist sg[1+MAX_SKB_FRAGS]; 138296f96fcSRusty Russell int num, err; 139296f96fcSRusty Russell 1404d125de3SRusty Russell sg_init_table(sg, 1+MAX_SKB_FRAGS); 141296f96fcSRusty Russell for (;;) { 142296f96fcSRusty Russell skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); 143296f96fcSRusty Russell if (unlikely(!skb)) 144296f96fcSRusty Russell break; 145296f96fcSRusty Russell 146296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 147296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 148296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 149296f96fcSRusty Russell skb_queue_head(&vi->recv, skb); 150296f96fcSRusty Russell 151296f96fcSRusty Russell err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); 152296f96fcSRusty Russell if (err) { 153296f96fcSRusty Russell skb_unlink(skb, &vi->recv); 154296f96fcSRusty Russell kfree_skb(skb); 155296f96fcSRusty Russell break; 156296f96fcSRusty Russell } 157296f96fcSRusty Russell vi->num++; 158296f96fcSRusty Russell } 159296f96fcSRusty Russell if (unlikely(vi->num > vi->max)) 160296f96fcSRusty Russell vi->max = vi->num; 161296f96fcSRusty Russell vi->rvq->vq_ops->kick(vi->rvq); 162296f96fcSRusty Russell } 163296f96fcSRusty Russell 164296f96fcSRusty Russell static bool skb_recv_done(struct virtqueue *rvq) 165296f96fcSRusty Russell { 166296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 167296f96fcSRusty Russell netif_rx_schedule(vi->dev, &vi->napi); 168296f96fcSRusty Russell /* Suppress further interrupts. */ 169296f96fcSRusty Russell return false; 170296f96fcSRusty Russell } 171296f96fcSRusty Russell 172296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 173296f96fcSRusty Russell { 174296f96fcSRusty Russell struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); 175296f96fcSRusty Russell struct sk_buff *skb = NULL; 176296f96fcSRusty Russell unsigned int len, received = 0; 177296f96fcSRusty Russell 178296f96fcSRusty Russell again: 179296f96fcSRusty Russell while (received < budget && 180296f96fcSRusty Russell (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { 181296f96fcSRusty Russell __skb_unlink(skb, &vi->recv); 182296f96fcSRusty Russell receive_skb(vi->dev, skb, len); 183296f96fcSRusty Russell vi->num--; 184296f96fcSRusty Russell received++; 185296f96fcSRusty Russell } 186296f96fcSRusty Russell 187296f96fcSRusty Russell /* FIXME: If we oom and completely run out of inbufs, we need 188296f96fcSRusty Russell * to start a timer trying to fill more. */ 189296f96fcSRusty Russell if (vi->num < vi->max / 2) 190296f96fcSRusty Russell try_fill_recv(vi); 191296f96fcSRusty Russell 1928329d98eSRusty Russell /* Out of packets? */ 1938329d98eSRusty Russell if (received < budget) { 194296f96fcSRusty Russell netif_rx_complete(vi->dev, napi); 195296f96fcSRusty Russell if (unlikely(!vi->rvq->vq_ops->restart(vi->rvq)) 196296f96fcSRusty Russell && netif_rx_reschedule(vi->dev, napi)) 197296f96fcSRusty Russell goto again; 198296f96fcSRusty Russell } 199296f96fcSRusty Russell 200296f96fcSRusty Russell return received; 201296f96fcSRusty Russell } 202296f96fcSRusty Russell 203296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi) 204296f96fcSRusty Russell { 205296f96fcSRusty Russell struct sk_buff *skb; 206296f96fcSRusty Russell unsigned int len; 207296f96fcSRusty Russell 208296f96fcSRusty Russell while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { 209296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 210296f96fcSRusty Russell __skb_unlink(skb, &vi->send); 211296f96fcSRusty Russell vi->dev->stats.tx_bytes += len; 212296f96fcSRusty Russell vi->dev->stats.tx_packets++; 213296f96fcSRusty Russell kfree_skb(skb); 214296f96fcSRusty Russell } 215296f96fcSRusty Russell } 216296f96fcSRusty Russell 217296f96fcSRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev) 218296f96fcSRusty Russell { 219296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 220296f96fcSRusty Russell int num, err; 221296f96fcSRusty Russell struct scatterlist sg[1+MAX_SKB_FRAGS]; 222296f96fcSRusty Russell struct virtio_net_hdr *hdr; 223296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 224296f96fcSRusty Russell DECLARE_MAC_BUF(mac); 225296f96fcSRusty Russell 2264d125de3SRusty Russell sg_init_table(sg, 1+MAX_SKB_FRAGS); 2274d125de3SRusty Russell 228296f96fcSRusty Russell pr_debug("%s: xmit %p %s\n", dev->name, skb, print_mac(mac, dest)); 229296f96fcSRusty Russell 230296f96fcSRusty Russell free_old_xmit_skbs(vi); 231296f96fcSRusty Russell 232296f96fcSRusty Russell /* Encode metadata header at front. */ 233296f96fcSRusty Russell hdr = skb_vnet_hdr(skb); 234296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 235296f96fcSRusty Russell hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 236296f96fcSRusty Russell hdr->csum_start = skb->csum_start - skb_headroom(skb); 237296f96fcSRusty Russell hdr->csum_offset = skb->csum_offset; 238296f96fcSRusty Russell } else { 239296f96fcSRusty Russell hdr->flags = 0; 240296f96fcSRusty Russell hdr->csum_offset = hdr->csum_start = 0; 241296f96fcSRusty Russell } 242296f96fcSRusty Russell 243296f96fcSRusty Russell if (skb_is_gso(skb)) { 244296f96fcSRusty Russell hdr->gso_size = skb_shinfo(skb)->gso_size; 245296f96fcSRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 246296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4_ECN; 247296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 248296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 249296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 250296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 251296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 252296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; 253296f96fcSRusty Russell else 254296f96fcSRusty Russell BUG(); 255296f96fcSRusty Russell } else { 256296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; 257296f96fcSRusty Russell hdr->gso_size = 0; 258296f96fcSRusty Russell } 259296f96fcSRusty Russell 260296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 261296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 262296f96fcSRusty Russell __skb_queue_head(&vi->send, skb); 263296f96fcSRusty Russell err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); 264296f96fcSRusty Russell if (err) { 265296f96fcSRusty Russell pr_debug("%s: virtio not prepared to send\n", dev->name); 266296f96fcSRusty Russell skb_unlink(skb, &vi->send); 267296f96fcSRusty Russell netif_stop_queue(dev); 268296f96fcSRusty Russell return NETDEV_TX_BUSY; 269296f96fcSRusty Russell } 270296f96fcSRusty Russell vi->svq->vq_ops->kick(vi->svq); 271296f96fcSRusty Russell 272296f96fcSRusty Russell return 0; 273296f96fcSRusty Russell } 274296f96fcSRusty Russell 275296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 276296f96fcSRusty Russell { 277296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 278296f96fcSRusty Russell 279296f96fcSRusty Russell try_fill_recv(vi); 280296f96fcSRusty Russell 281296f96fcSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 282296f96fcSRusty Russell if (vi->num == 0) 283296f96fcSRusty Russell return -ENOMEM; 284296f96fcSRusty Russell 285296f96fcSRusty Russell napi_enable(&vi->napi); 286296f96fcSRusty Russell return 0; 287296f96fcSRusty Russell } 288296f96fcSRusty Russell 289296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 290296f96fcSRusty Russell { 291296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 292296f96fcSRusty Russell struct sk_buff *skb; 293296f96fcSRusty Russell 294296f96fcSRusty Russell napi_disable(&vi->napi); 295296f96fcSRusty Russell 296296f96fcSRusty Russell /* networking core has neutered skb_xmit_done/skb_recv_done, so don't 297296f96fcSRusty Russell * worry about races vs. get(). */ 298296f96fcSRusty Russell vi->rvq->vq_ops->shutdown(vi->rvq); 299296f96fcSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 300296f96fcSRusty Russell kfree_skb(skb); 301296f96fcSRusty Russell vi->num--; 302296f96fcSRusty Russell } 303296f96fcSRusty Russell vi->svq->vq_ops->shutdown(vi->svq); 304296f96fcSRusty Russell while ((skb = __skb_dequeue(&vi->send)) != NULL) 305296f96fcSRusty Russell kfree_skb(skb); 306296f96fcSRusty Russell 307296f96fcSRusty Russell BUG_ON(vi->num != 0); 308296f96fcSRusty Russell return 0; 309296f96fcSRusty Russell } 310296f96fcSRusty Russell 311296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 312296f96fcSRusty Russell { 313296f96fcSRusty Russell int err; 314296f96fcSRusty Russell unsigned int len; 315296f96fcSRusty Russell struct net_device *dev; 316296f96fcSRusty Russell struct virtnet_info *vi; 317296f96fcSRusty Russell void *token; 318296f96fcSRusty Russell 319296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 320296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 321296f96fcSRusty Russell if (!dev) 322296f96fcSRusty Russell return -ENOMEM; 323296f96fcSRusty Russell 324296f96fcSRusty Russell /* Set up network device as normal. */ 325296f96fcSRusty Russell ether_setup(dev); 326296f96fcSRusty Russell dev->open = virtnet_open; 327296f96fcSRusty Russell dev->stop = virtnet_close; 328296f96fcSRusty Russell dev->hard_start_xmit = start_xmit; 329296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 330296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 331296f96fcSRusty Russell 332296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 333296f96fcSRusty Russell token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_F, &len); 334296f96fcSRusty Russell if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_NO_CSUM)) { 335296f96fcSRusty Russell /* This opens up the world of extra features. */ 336296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 337296f96fcSRusty Russell if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4)) 338296f96fcSRusty Russell dev->features |= NETIF_F_TSO; 339296f96fcSRusty Russell if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_UFO)) 340296f96fcSRusty Russell dev->features |= NETIF_F_UFO; 341296f96fcSRusty Russell if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO4_ECN)) 342296f96fcSRusty Russell dev->features |= NETIF_F_TSO_ECN; 343296f96fcSRusty Russell if (virtio_use_bit(vdev, token, len, VIRTIO_NET_F_TSO6)) 344296f96fcSRusty Russell dev->features |= NETIF_F_TSO6; 345296f96fcSRusty Russell } 346296f96fcSRusty Russell 347296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 348296f96fcSRusty Russell token = vdev->config->find(vdev, VIRTIO_CONFIG_NET_MAC_F, &len); 349296f96fcSRusty Russell if (token) { 350296f96fcSRusty Russell dev->addr_len = len; 351296f96fcSRusty Russell vdev->config->get(vdev, token, dev->dev_addr, len); 352296f96fcSRusty Russell } else 353296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 354296f96fcSRusty Russell 355296f96fcSRusty Russell /* Set up our device-specific information */ 356296f96fcSRusty Russell vi = netdev_priv(dev); 357296f96fcSRusty Russell netif_napi_add(dev, &vi->napi, virtnet_poll, 16); 358296f96fcSRusty Russell vi->dev = dev; 359296f96fcSRusty Russell vi->vdev = vdev; 360296f96fcSRusty Russell 361296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 362296f96fcSRusty Russell vi->rvq = vdev->config->find_vq(vdev, skb_recv_done); 363296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 364296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 365296f96fcSRusty Russell goto free; 366296f96fcSRusty Russell } 367296f96fcSRusty Russell 368296f96fcSRusty Russell vi->svq = vdev->config->find_vq(vdev, skb_xmit_done); 369296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 370296f96fcSRusty Russell err = PTR_ERR(vi->svq); 371296f96fcSRusty Russell goto free_recv; 372296f96fcSRusty Russell } 373296f96fcSRusty Russell 374296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 375296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 376296f96fcSRusty Russell skb_queue_head_init(&vi->send); 377296f96fcSRusty Russell 378296f96fcSRusty Russell err = register_netdev(dev); 379296f96fcSRusty Russell if (err) { 380296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 381296f96fcSRusty Russell goto free_send; 382296f96fcSRusty Russell } 383296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 384296f96fcSRusty Russell vdev->priv = vi; 385296f96fcSRusty Russell return 0; 386296f96fcSRusty Russell 387296f96fcSRusty Russell free_send: 388296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 389296f96fcSRusty Russell free_recv: 390296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 391296f96fcSRusty Russell free: 392296f96fcSRusty Russell free_netdev(dev); 393296f96fcSRusty Russell return err; 394296f96fcSRusty Russell } 395296f96fcSRusty Russell 396296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 397296f96fcSRusty Russell { 39874b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 39974b2553fSRusty Russell 40074b2553fSRusty Russell vdev->config->del_vq(vi->svq); 40174b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 40274b2553fSRusty Russell unregister_netdev(vi->dev); 40374b2553fSRusty Russell free_netdev(vi->dev); 404296f96fcSRusty Russell } 405296f96fcSRusty Russell 406296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 407296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 408296f96fcSRusty Russell { 0 }, 409296f96fcSRusty Russell }; 410296f96fcSRusty Russell 411296f96fcSRusty Russell static struct virtio_driver virtio_net = { 412296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 413296f96fcSRusty Russell .driver.owner = THIS_MODULE, 414296f96fcSRusty Russell .id_table = id_table, 415296f96fcSRusty Russell .probe = virtnet_probe, 416296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 417296f96fcSRusty Russell }; 418296f96fcSRusty Russell 419296f96fcSRusty Russell static int __init init(void) 420296f96fcSRusty Russell { 421296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 422296f96fcSRusty Russell } 423296f96fcSRusty Russell 424296f96fcSRusty Russell static void __exit fini(void) 425296f96fcSRusty Russell { 426296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 427296f96fcSRusty Russell } 428296f96fcSRusty Russell module_init(init); 429296f96fcSRusty Russell module_exit(fini); 430296f96fcSRusty Russell 431296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 432296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 433296f96fcSRusty Russell MODULE_LICENSE("GPL"); 434