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 276c0cd7c0SDor Laor static int napi_weight = 128; 286c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 296c0cd7c0SDor Laor 3034a48579SRusty Russell static int csum = 1, gso = 1; 3134a48579SRusty Russell module_param(csum, bool, 0444); 3234a48579SRusty Russell module_param(gso, bool, 0444); 3334a48579SRusty Russell 34296f96fcSRusty Russell /* FIXME: MTU in config. */ 35296f96fcSRusty Russell #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) 36296f96fcSRusty Russell 37296f96fcSRusty Russell struct virtnet_info 38296f96fcSRusty Russell { 39296f96fcSRusty Russell struct virtio_device *vdev; 40296f96fcSRusty Russell struct virtqueue *rvq, *svq; 41296f96fcSRusty Russell struct net_device *dev; 42296f96fcSRusty Russell struct napi_struct napi; 43296f96fcSRusty Russell 4499ffc696SRusty Russell /* The skb we couldn't send because buffers were full. */ 4599ffc696SRusty Russell struct sk_buff *last_xmit_skb; 4699ffc696SRusty Russell 47296f96fcSRusty Russell /* Number of input buffers, and max we've ever had. */ 48296f96fcSRusty Russell unsigned int num, max; 49296f96fcSRusty Russell 50296f96fcSRusty Russell /* Receive & send queues. */ 51296f96fcSRusty Russell struct sk_buff_head recv; 52296f96fcSRusty Russell struct sk_buff_head send; 53296f96fcSRusty Russell }; 54296f96fcSRusty Russell 55296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) 56296f96fcSRusty Russell { 57296f96fcSRusty Russell return (struct virtio_net_hdr *)skb->cb; 58296f96fcSRusty Russell } 59296f96fcSRusty Russell 60296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) 61296f96fcSRusty Russell { 62296f96fcSRusty Russell sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); 63296f96fcSRusty Russell } 64296f96fcSRusty Russell 652cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq) 66296f96fcSRusty Russell { 672cb9c6baSRusty Russell struct virtnet_info *vi = svq->vdev->priv; 68296f96fcSRusty Russell 692cb9c6baSRusty Russell /* Suppress further interrupts. */ 702cb9c6baSRusty Russell svq->vq_ops->disable_cb(svq); 712cb9c6baSRusty Russell /* We were waiting for more output buffers. */ 72296f96fcSRusty Russell netif_wake_queue(vi->dev); 73296f96fcSRusty Russell } 74296f96fcSRusty Russell 75296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb, 76296f96fcSRusty Russell unsigned len) 77296f96fcSRusty Russell { 78296f96fcSRusty Russell struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 79296f96fcSRusty Russell 80296f96fcSRusty Russell if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 81296f96fcSRusty Russell pr_debug("%s: short packet %i\n", dev->name, len); 82296f96fcSRusty Russell dev->stats.rx_length_errors++; 83296f96fcSRusty Russell goto drop; 84296f96fcSRusty Russell } 85296f96fcSRusty Russell len -= sizeof(struct virtio_net_hdr); 86296f96fcSRusty Russell BUG_ON(len > MAX_PACKET_LEN); 87296f96fcSRusty Russell 88296f96fcSRusty Russell skb_trim(skb, len); 89296f96fcSRusty Russell skb->protocol = eth_type_trans(skb, dev); 90296f96fcSRusty Russell pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 91296f96fcSRusty Russell ntohs(skb->protocol), skb->len, skb->pkt_type); 92296f96fcSRusty Russell dev->stats.rx_bytes += skb->len; 93296f96fcSRusty Russell dev->stats.rx_packets++; 94296f96fcSRusty Russell 95296f96fcSRusty Russell if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 96296f96fcSRusty Russell pr_debug("Needs csum!\n"); 97f35d9d8aSRusty Russell if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) 98296f96fcSRusty Russell goto frame_err; 99296f96fcSRusty Russell } 100296f96fcSRusty Russell 101296f96fcSRusty Russell if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { 102296f96fcSRusty Russell pr_debug("GSO!\n"); 10334a48579SRusty Russell switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 104296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 105296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 106296f96fcSRusty Russell break; 107296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 108296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 109296f96fcSRusty Russell break; 110296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 111296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 112296f96fcSRusty Russell break; 113296f96fcSRusty Russell default: 114296f96fcSRusty Russell if (net_ratelimit()) 115296f96fcSRusty Russell printk(KERN_WARNING "%s: bad gso type %u.\n", 116296f96fcSRusty Russell dev->name, hdr->gso_type); 117296f96fcSRusty Russell goto frame_err; 118296f96fcSRusty Russell } 119296f96fcSRusty Russell 12034a48579SRusty Russell if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) 12134a48579SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 12234a48579SRusty Russell 123296f96fcSRusty Russell skb_shinfo(skb)->gso_size = hdr->gso_size; 124296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 125296f96fcSRusty Russell if (net_ratelimit()) 126296f96fcSRusty Russell printk(KERN_WARNING "%s: zero gso size.\n", 127296f96fcSRusty Russell dev->name); 128296f96fcSRusty Russell goto frame_err; 129296f96fcSRusty Russell } 130296f96fcSRusty Russell 131296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 132296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 133296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 134296f96fcSRusty Russell } 135296f96fcSRusty Russell 136296f96fcSRusty Russell netif_receive_skb(skb); 137296f96fcSRusty Russell return; 138296f96fcSRusty Russell 139296f96fcSRusty Russell frame_err: 140296f96fcSRusty Russell dev->stats.rx_frame_errors++; 141296f96fcSRusty Russell drop: 142296f96fcSRusty Russell dev_kfree_skb(skb); 143296f96fcSRusty Russell } 144296f96fcSRusty Russell 145296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi) 146296f96fcSRusty Russell { 147296f96fcSRusty Russell struct sk_buff *skb; 14805271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 149296f96fcSRusty Russell int num, err; 150296f96fcSRusty Russell 15105271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 152296f96fcSRusty Russell for (;;) { 153296f96fcSRusty Russell skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); 154296f96fcSRusty Russell if (unlikely(!skb)) 155296f96fcSRusty Russell break; 156296f96fcSRusty Russell 157296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 158296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 159296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 160296f96fcSRusty Russell skb_queue_head(&vi->recv, skb); 161296f96fcSRusty Russell 162296f96fcSRusty Russell err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); 163296f96fcSRusty Russell if (err) { 164296f96fcSRusty Russell skb_unlink(skb, &vi->recv); 165296f96fcSRusty Russell kfree_skb(skb); 166296f96fcSRusty Russell break; 167296f96fcSRusty Russell } 168296f96fcSRusty Russell vi->num++; 169296f96fcSRusty Russell } 170296f96fcSRusty Russell if (unlikely(vi->num > vi->max)) 171296f96fcSRusty Russell vi->max = vi->num; 172296f96fcSRusty Russell vi->rvq->vq_ops->kick(vi->rvq); 173296f96fcSRusty Russell } 174296f96fcSRusty Russell 17518445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 176296f96fcSRusty Russell { 177296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 17818445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 17918445c4dSRusty Russell if (netif_rx_schedule_prep(vi->dev, &vi->napi)) { 18018445c4dSRusty Russell rvq->vq_ops->disable_cb(rvq); 18118445c4dSRusty Russell __netif_rx_schedule(vi->dev, &vi->napi); 18218445c4dSRusty Russell } 183296f96fcSRusty Russell } 184296f96fcSRusty Russell 185296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 186296f96fcSRusty Russell { 187296f96fcSRusty Russell struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); 188296f96fcSRusty Russell struct sk_buff *skb = NULL; 189296f96fcSRusty Russell unsigned int len, received = 0; 190296f96fcSRusty Russell 191296f96fcSRusty Russell again: 192296f96fcSRusty Russell while (received < budget && 193296f96fcSRusty Russell (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { 194296f96fcSRusty Russell __skb_unlink(skb, &vi->recv); 195296f96fcSRusty Russell receive_skb(vi->dev, skb, len); 196296f96fcSRusty Russell vi->num--; 197296f96fcSRusty Russell received++; 198296f96fcSRusty Russell } 199296f96fcSRusty Russell 200296f96fcSRusty Russell /* FIXME: If we oom and completely run out of inbufs, we need 201296f96fcSRusty Russell * to start a timer trying to fill more. */ 202296f96fcSRusty Russell if (vi->num < vi->max / 2) 203296f96fcSRusty Russell try_fill_recv(vi); 204296f96fcSRusty Russell 2058329d98eSRusty Russell /* Out of packets? */ 2068329d98eSRusty Russell if (received < budget) { 207296f96fcSRusty Russell netif_rx_complete(vi->dev, napi); 20818445c4dSRusty Russell if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) 2094265f161SChristian Borntraeger && napi_schedule_prep(napi)) { 2104265f161SChristian Borntraeger vi->rvq->vq_ops->disable_cb(vi->rvq); 2114265f161SChristian Borntraeger __netif_rx_schedule(vi->dev, napi); 212296f96fcSRusty Russell goto again; 213296f96fcSRusty Russell } 2144265f161SChristian Borntraeger } 215296f96fcSRusty Russell 216296f96fcSRusty Russell return received; 217296f96fcSRusty Russell } 218296f96fcSRusty Russell 219296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi) 220296f96fcSRusty Russell { 221296f96fcSRusty Russell struct sk_buff *skb; 222296f96fcSRusty Russell unsigned int len; 223296f96fcSRusty Russell 224296f96fcSRusty Russell while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { 225296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 226296f96fcSRusty Russell __skb_unlink(skb, &vi->send); 227655aa31fSRusty Russell vi->dev->stats.tx_bytes += skb->len; 228296f96fcSRusty Russell vi->dev->stats.tx_packets++; 229296f96fcSRusty Russell kfree_skb(skb); 230296f96fcSRusty Russell } 231296f96fcSRusty Russell } 232296f96fcSRusty Russell 23399ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) 234296f96fcSRusty Russell { 23599ffc696SRusty Russell int num; 23605271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 237296f96fcSRusty Russell struct virtio_net_hdr *hdr; 238296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 239296f96fcSRusty Russell 24005271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 2414d125de3SRusty Russell 24299ffc696SRusty Russell pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb, 24321f644f3SDavid S. Miller dest[0], dest[1], dest[2], 24421f644f3SDavid S. Miller dest[3], dest[4], dest[5]); 245296f96fcSRusty Russell 246296f96fcSRusty Russell /* Encode metadata header at front. */ 247296f96fcSRusty Russell hdr = skb_vnet_hdr(skb); 248296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 249296f96fcSRusty Russell hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 250296f96fcSRusty Russell hdr->csum_start = skb->csum_start - skb_headroom(skb); 251296f96fcSRusty Russell hdr->csum_offset = skb->csum_offset; 252296f96fcSRusty Russell } else { 253296f96fcSRusty Russell hdr->flags = 0; 254296f96fcSRusty Russell hdr->csum_offset = hdr->csum_start = 0; 255296f96fcSRusty Russell } 256296f96fcSRusty Russell 257296f96fcSRusty Russell if (skb_is_gso(skb)) { 25850c8ea80SRusty Russell hdr->hdr_len = skb_transport_header(skb) - skb->data; 259296f96fcSRusty Russell hdr->gso_size = skb_shinfo(skb)->gso_size; 26034a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 261296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 262296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 263296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 264296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 265296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; 266296f96fcSRusty Russell else 267296f96fcSRusty Russell BUG(); 26834a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 26934a48579SRusty Russell hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 270296f96fcSRusty Russell } else { 271296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; 27250c8ea80SRusty Russell hdr->gso_size = hdr->hdr_len = 0; 273296f96fcSRusty Russell } 274296f96fcSRusty Russell 275296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 276296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 27799ffc696SRusty Russell 27899ffc696SRusty Russell return vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); 27999ffc696SRusty Russell } 28099ffc696SRusty Russell 28199ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev) 28299ffc696SRusty Russell { 28399ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 2842cb9c6baSRusty Russell 2852cb9c6baSRusty Russell again: 2862cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 2872cb9c6baSRusty Russell free_old_xmit_skbs(vi); 28899ffc696SRusty Russell 28999ffc696SRusty Russell /* If we has a buffer left over from last time, send it now. */ 29099ffc696SRusty Russell if (vi->last_xmit_skb) { 29199ffc696SRusty Russell if (xmit_skb(vi, vi->last_xmit_skb) != 0) { 29299ffc696SRusty Russell /* Drop this skb: we only queue one. */ 29399ffc696SRusty Russell vi->dev->stats.tx_dropped++; 29499ffc696SRusty Russell kfree_skb(skb); 29599ffc696SRusty Russell goto stop_queue; 29699ffc696SRusty Russell } 29799ffc696SRusty Russell vi->last_xmit_skb = NULL; 29899ffc696SRusty Russell } 29999ffc696SRusty Russell 30099ffc696SRusty Russell /* Put new one in send queue and do transmit */ 30199ffc696SRusty Russell __skb_queue_head(&vi->send, skb); 30299ffc696SRusty Russell if (xmit_skb(vi, skb) != 0) { 30399ffc696SRusty Russell vi->last_xmit_skb = skb; 30499ffc696SRusty Russell goto stop_queue; 30599ffc696SRusty Russell } 30699ffc696SRusty Russell done: 30799ffc696SRusty Russell vi->svq->vq_ops->kick(vi->svq); 30899ffc696SRusty Russell return NETDEV_TX_OK; 30999ffc696SRusty Russell 31099ffc696SRusty Russell stop_queue: 311296f96fcSRusty Russell pr_debug("%s: virtio not prepared to send\n", dev->name); 312296f96fcSRusty Russell netif_stop_queue(dev); 3132cb9c6baSRusty Russell 3144265f161SChristian Borntraeger /* Activate callback for using skbs: if this returns false it 3152cb9c6baSRusty Russell * means some were used in the meantime. */ 3162cb9c6baSRusty Russell if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { 3174265f161SChristian Borntraeger vi->svq->vq_ops->disable_cb(vi->svq); 3182cb9c6baSRusty Russell netif_start_queue(dev); 3192cb9c6baSRusty Russell goto again; 3202cb9c6baSRusty Russell } 32199ffc696SRusty Russell goto done; 322296f96fcSRusty Russell } 323296f96fcSRusty Russell 324da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 325da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 326da74e89dSAmit Shah { 327da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 328da74e89dSAmit Shah 329da74e89dSAmit Shah napi_schedule(&vi->napi); 330da74e89dSAmit Shah } 331da74e89dSAmit Shah #endif 332da74e89dSAmit Shah 333296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 334296f96fcSRusty Russell { 335296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 336296f96fcSRusty Russell 337296f96fcSRusty Russell napi_enable(&vi->napi); 338a48bd8f6SRusty Russell 339a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 340a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 341370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 342370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 343370076d9SChristian Borntraeger if (netif_rx_schedule_prep(dev, &vi->napi)) { 344a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 345370076d9SChristian Borntraeger __netif_rx_schedule(dev, &vi->napi); 346370076d9SChristian Borntraeger } 347296f96fcSRusty Russell return 0; 348296f96fcSRusty Russell } 349296f96fcSRusty Russell 350296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 351296f96fcSRusty Russell { 352296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 353296f96fcSRusty Russell 354296f96fcSRusty Russell napi_disable(&vi->napi); 355296f96fcSRusty Russell 356296f96fcSRusty Russell return 0; 357296f96fcSRusty Russell } 358296f96fcSRusty Russell 359296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 360296f96fcSRusty Russell { 361296f96fcSRusty Russell int err; 362296f96fcSRusty Russell struct net_device *dev; 363296f96fcSRusty Russell struct virtnet_info *vi; 364296f96fcSRusty Russell 365296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 366296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 367296f96fcSRusty Russell if (!dev) 368296f96fcSRusty Russell return -ENOMEM; 369296f96fcSRusty Russell 370296f96fcSRusty Russell /* Set up network device as normal. */ 371296f96fcSRusty Russell dev->open = virtnet_open; 372296f96fcSRusty Russell dev->stop = virtnet_close; 373296f96fcSRusty Russell dev->hard_start_xmit = start_xmit; 374296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 375da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 376da74e89dSAmit Shah dev->poll_controller = virtnet_netpoll; 377da74e89dSAmit Shah #endif 378296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 379296f96fcSRusty Russell 380296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 381c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 382296f96fcSRusty Russell /* This opens up the world of extra features. */ 383296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 384c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 38534a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 38634a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 38734a48579SRusty Russell } 3885539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 389c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 3905539ae96SRusty Russell dev->features |= NETIF_F_TSO; 391c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 3925539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 393c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 3945539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 395c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 3965539ae96SRusty Russell dev->features |= NETIF_F_UFO; 397296f96fcSRusty Russell } 398296f96fcSRusty Russell 399296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 400c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 401a586d4f6SRusty Russell vdev->config->get(vdev, 402a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 403a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 404296f96fcSRusty Russell } else 405296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 406296f96fcSRusty Russell 407296f96fcSRusty Russell /* Set up our device-specific information */ 408296f96fcSRusty Russell vi = netdev_priv(dev); 4096c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 410296f96fcSRusty Russell vi->dev = dev; 411296f96fcSRusty Russell vi->vdev = vdev; 412d9d5dcc8SChristian Borntraeger vdev->priv = vi; 413296f96fcSRusty Russell 414296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 415a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 416296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 417296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 418296f96fcSRusty Russell goto free; 419296f96fcSRusty Russell } 420296f96fcSRusty Russell 421a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 422296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 423296f96fcSRusty Russell err = PTR_ERR(vi->svq); 424296f96fcSRusty Russell goto free_recv; 425296f96fcSRusty Russell } 426296f96fcSRusty Russell 427296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 428296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 429296f96fcSRusty Russell skb_queue_head_init(&vi->send); 430296f96fcSRusty Russell 431296f96fcSRusty Russell err = register_netdev(dev); 432296f96fcSRusty Russell if (err) { 433296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 434296f96fcSRusty Russell goto free_send; 435296f96fcSRusty Russell } 436b3369c1fSRusty Russell 437b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 438b3369c1fSRusty Russell try_fill_recv(vi); 439b3369c1fSRusty Russell 440b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 441b3369c1fSRusty Russell if (vi->num == 0) { 442b3369c1fSRusty Russell err = -ENOMEM; 443b3369c1fSRusty Russell goto unregister; 444b3369c1fSRusty Russell } 445b3369c1fSRusty Russell 446296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 447296f96fcSRusty Russell return 0; 448296f96fcSRusty Russell 449b3369c1fSRusty Russell unregister: 450b3369c1fSRusty Russell unregister_netdev(dev); 451296f96fcSRusty Russell free_send: 452296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 453296f96fcSRusty Russell free_recv: 454296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 455296f96fcSRusty Russell free: 456296f96fcSRusty Russell free_netdev(dev); 457296f96fcSRusty Russell return err; 458296f96fcSRusty Russell } 459296f96fcSRusty Russell 460296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 461296f96fcSRusty Russell { 46274b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 463b3369c1fSRusty Russell struct sk_buff *skb; 464b3369c1fSRusty Russell 4656e5aa7efSRusty Russell /* Stop all the virtqueues. */ 4666e5aa7efSRusty Russell vdev->config->reset(vdev); 4676e5aa7efSRusty Russell 468b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 469b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 470b3369c1fSRusty Russell kfree_skb(skb); 471b3369c1fSRusty Russell vi->num--; 472b3369c1fSRusty Russell } 473*288369ccSWang Chen __skb_queue_purge(&vi->send); 474b3369c1fSRusty Russell 475b3369c1fSRusty Russell BUG_ON(vi->num != 0); 47674b2553fSRusty Russell 47774b2553fSRusty Russell vdev->config->del_vq(vi->svq); 47874b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 47974b2553fSRusty Russell unregister_netdev(vi->dev); 48074b2553fSRusty Russell free_netdev(vi->dev); 481296f96fcSRusty Russell } 482296f96fcSRusty Russell 483296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 484296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 485296f96fcSRusty Russell { 0 }, 486296f96fcSRusty Russell }; 487296f96fcSRusty Russell 488c45a6816SRusty Russell static unsigned int features[] = { 489c45a6816SRusty Russell VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 490c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 491c45a6816SRusty Russell VIRTIO_NET_F_HOST_ECN, 492c45a6816SRusty Russell }; 493c45a6816SRusty Russell 494296f96fcSRusty Russell static struct virtio_driver virtio_net = { 495c45a6816SRusty Russell .feature_table = features, 496c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 497296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 498296f96fcSRusty Russell .driver.owner = THIS_MODULE, 499296f96fcSRusty Russell .id_table = id_table, 500296f96fcSRusty Russell .probe = virtnet_probe, 501296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 502296f96fcSRusty Russell }; 503296f96fcSRusty Russell 504296f96fcSRusty Russell static int __init init(void) 505296f96fcSRusty Russell { 506296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 507296f96fcSRusty Russell } 508296f96fcSRusty Russell 509296f96fcSRusty Russell static void __exit fini(void) 510296f96fcSRusty Russell { 511296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 512296f96fcSRusty Russell } 513296f96fcSRusty Russell module_init(init); 514296f96fcSRusty Russell module_exit(fini); 515296f96fcSRusty Russell 516296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 517296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 518296f96fcSRusty Russell MODULE_LICENSE("GPL"); 519