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 47*14c998f0SMark McLoughlin struct timer_list xmit_free_timer; 48*14c998f0SMark McLoughlin 49296f96fcSRusty Russell /* Number of input buffers, and max we've ever had. */ 50296f96fcSRusty Russell unsigned int num, max; 51296f96fcSRusty Russell 5211a3a154SRusty Russell /* For cleaning up after transmission. */ 5311a3a154SRusty Russell struct tasklet_struct tasklet; 5411a3a154SRusty Russell 55296f96fcSRusty Russell /* Receive & send queues. */ 56296f96fcSRusty Russell struct sk_buff_head recv; 57296f96fcSRusty Russell struct sk_buff_head send; 58296f96fcSRusty Russell }; 59296f96fcSRusty Russell 60296f96fcSRusty Russell static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) 61296f96fcSRusty Russell { 62296f96fcSRusty Russell return (struct virtio_net_hdr *)skb->cb; 63296f96fcSRusty Russell } 64296f96fcSRusty Russell 65296f96fcSRusty Russell static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) 66296f96fcSRusty Russell { 67296f96fcSRusty Russell sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); 68296f96fcSRusty Russell } 69296f96fcSRusty Russell 702cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq) 71296f96fcSRusty Russell { 722cb9c6baSRusty Russell struct virtnet_info *vi = svq->vdev->priv; 73296f96fcSRusty Russell 742cb9c6baSRusty Russell /* Suppress further interrupts. */ 752cb9c6baSRusty Russell svq->vq_ops->disable_cb(svq); 7611a3a154SRusty Russell 772cb9c6baSRusty Russell /* We were waiting for more output buffers. */ 78296f96fcSRusty Russell netif_wake_queue(vi->dev); 7911a3a154SRusty Russell 8011a3a154SRusty Russell /* Make sure we re-xmit last_xmit_skb: if there are no more packets 8111a3a154SRusty Russell * queued, start_xmit won't be called. */ 8211a3a154SRusty Russell tasklet_schedule(&vi->tasklet); 83296f96fcSRusty Russell } 84296f96fcSRusty Russell 85296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb, 86296f96fcSRusty Russell unsigned len) 87296f96fcSRusty Russell { 88296f96fcSRusty Russell struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 89296f96fcSRusty Russell 90296f96fcSRusty Russell if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 91296f96fcSRusty Russell pr_debug("%s: short packet %i\n", dev->name, len); 92296f96fcSRusty Russell dev->stats.rx_length_errors++; 93296f96fcSRusty Russell goto drop; 94296f96fcSRusty Russell } 95296f96fcSRusty Russell len -= sizeof(struct virtio_net_hdr); 96296f96fcSRusty Russell BUG_ON(len > MAX_PACKET_LEN); 97296f96fcSRusty Russell 98296f96fcSRusty Russell skb_trim(skb, len); 9923cde76dSMark McLoughlin 100296f96fcSRusty Russell dev->stats.rx_bytes += skb->len; 101296f96fcSRusty Russell dev->stats.rx_packets++; 102296f96fcSRusty Russell 103296f96fcSRusty Russell if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 104296f96fcSRusty Russell pr_debug("Needs csum!\n"); 105f35d9d8aSRusty Russell if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) 106296f96fcSRusty Russell goto frame_err; 107296f96fcSRusty Russell } 108296f96fcSRusty Russell 10923cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 11023cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 11123cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 11223cde76dSMark McLoughlin 113296f96fcSRusty Russell if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { 114296f96fcSRusty Russell pr_debug("GSO!\n"); 11534a48579SRusty Russell switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 116296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 117296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 118296f96fcSRusty Russell break; 119296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 120296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 121296f96fcSRusty Russell break; 122296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 123296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 124296f96fcSRusty Russell break; 125296f96fcSRusty Russell default: 126296f96fcSRusty Russell if (net_ratelimit()) 127296f96fcSRusty Russell printk(KERN_WARNING "%s: bad gso type %u.\n", 128296f96fcSRusty Russell dev->name, hdr->gso_type); 129296f96fcSRusty Russell goto frame_err; 130296f96fcSRusty Russell } 131296f96fcSRusty Russell 13234a48579SRusty Russell if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) 13334a48579SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 13434a48579SRusty Russell 135296f96fcSRusty Russell skb_shinfo(skb)->gso_size = hdr->gso_size; 136296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 137296f96fcSRusty Russell if (net_ratelimit()) 138296f96fcSRusty Russell printk(KERN_WARNING "%s: zero gso size.\n", 139296f96fcSRusty Russell dev->name); 140296f96fcSRusty Russell goto frame_err; 141296f96fcSRusty Russell } 142296f96fcSRusty Russell 143296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 144296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 145296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 146296f96fcSRusty Russell } 147296f96fcSRusty Russell 148296f96fcSRusty Russell netif_receive_skb(skb); 149296f96fcSRusty Russell return; 150296f96fcSRusty Russell 151296f96fcSRusty Russell frame_err: 152296f96fcSRusty Russell dev->stats.rx_frame_errors++; 153296f96fcSRusty Russell drop: 154296f96fcSRusty Russell dev_kfree_skb(skb); 155296f96fcSRusty Russell } 156296f96fcSRusty Russell 157296f96fcSRusty Russell static void try_fill_recv(struct virtnet_info *vi) 158296f96fcSRusty Russell { 159296f96fcSRusty Russell struct sk_buff *skb; 16005271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 161296f96fcSRusty Russell int num, err; 162296f96fcSRusty Russell 16305271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 164296f96fcSRusty Russell for (;;) { 165296f96fcSRusty Russell skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); 166296f96fcSRusty Russell if (unlikely(!skb)) 167296f96fcSRusty Russell break; 168296f96fcSRusty Russell 169296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 170296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 171296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 172296f96fcSRusty Russell skb_queue_head(&vi->recv, skb); 173296f96fcSRusty Russell 174296f96fcSRusty Russell err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); 175296f96fcSRusty Russell if (err) { 176296f96fcSRusty Russell skb_unlink(skb, &vi->recv); 177296f96fcSRusty Russell kfree_skb(skb); 178296f96fcSRusty Russell break; 179296f96fcSRusty Russell } 180296f96fcSRusty Russell vi->num++; 181296f96fcSRusty Russell } 182296f96fcSRusty Russell if (unlikely(vi->num > vi->max)) 183296f96fcSRusty Russell vi->max = vi->num; 184296f96fcSRusty Russell vi->rvq->vq_ops->kick(vi->rvq); 185296f96fcSRusty Russell } 186296f96fcSRusty Russell 18718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 188296f96fcSRusty Russell { 189296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 19018445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 19118445c4dSRusty Russell if (netif_rx_schedule_prep(vi->dev, &vi->napi)) { 19218445c4dSRusty Russell rvq->vq_ops->disable_cb(rvq); 19318445c4dSRusty Russell __netif_rx_schedule(vi->dev, &vi->napi); 19418445c4dSRusty Russell } 195296f96fcSRusty Russell } 196296f96fcSRusty Russell 197296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 198296f96fcSRusty Russell { 199296f96fcSRusty Russell struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); 200296f96fcSRusty Russell struct sk_buff *skb = NULL; 201296f96fcSRusty Russell unsigned int len, received = 0; 202296f96fcSRusty Russell 203296f96fcSRusty Russell again: 204296f96fcSRusty Russell while (received < budget && 205296f96fcSRusty Russell (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { 206296f96fcSRusty Russell __skb_unlink(skb, &vi->recv); 207296f96fcSRusty Russell receive_skb(vi->dev, skb, len); 208296f96fcSRusty Russell vi->num--; 209296f96fcSRusty Russell received++; 210296f96fcSRusty Russell } 211296f96fcSRusty Russell 212296f96fcSRusty Russell /* FIXME: If we oom and completely run out of inbufs, we need 213296f96fcSRusty Russell * to start a timer trying to fill more. */ 214296f96fcSRusty Russell if (vi->num < vi->max / 2) 215296f96fcSRusty Russell try_fill_recv(vi); 216296f96fcSRusty Russell 2178329d98eSRusty Russell /* Out of packets? */ 2188329d98eSRusty Russell if (received < budget) { 219296f96fcSRusty Russell netif_rx_complete(vi->dev, napi); 22018445c4dSRusty Russell if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) 2214265f161SChristian Borntraeger && napi_schedule_prep(napi)) { 2224265f161SChristian Borntraeger vi->rvq->vq_ops->disable_cb(vi->rvq); 2234265f161SChristian Borntraeger __netif_rx_schedule(vi->dev, napi); 224296f96fcSRusty Russell goto again; 225296f96fcSRusty Russell } 2264265f161SChristian Borntraeger } 227296f96fcSRusty Russell 228296f96fcSRusty Russell return received; 229296f96fcSRusty Russell } 230296f96fcSRusty Russell 231296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi) 232296f96fcSRusty Russell { 233296f96fcSRusty Russell struct sk_buff *skb; 234296f96fcSRusty Russell unsigned int len; 235296f96fcSRusty Russell 236296f96fcSRusty Russell while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { 237296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 238296f96fcSRusty Russell __skb_unlink(skb, &vi->send); 239655aa31fSRusty Russell vi->dev->stats.tx_bytes += skb->len; 240296f96fcSRusty Russell vi->dev->stats.tx_packets++; 241296f96fcSRusty Russell kfree_skb(skb); 242296f96fcSRusty Russell } 243296f96fcSRusty Russell } 244296f96fcSRusty Russell 245*14c998f0SMark McLoughlin static void xmit_free(unsigned long data) 246*14c998f0SMark McLoughlin { 247*14c998f0SMark McLoughlin struct virtnet_info *vi = (void *)data; 248*14c998f0SMark McLoughlin 249*14c998f0SMark McLoughlin netif_tx_lock(vi->dev); 250*14c998f0SMark McLoughlin 251*14c998f0SMark McLoughlin free_old_xmit_skbs(vi); 252*14c998f0SMark McLoughlin 253*14c998f0SMark McLoughlin if (!skb_queue_empty(&vi->send)) 254*14c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 255*14c998f0SMark McLoughlin 256*14c998f0SMark McLoughlin netif_tx_unlock(vi->dev); 257*14c998f0SMark McLoughlin } 258*14c998f0SMark McLoughlin 25999ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) 260296f96fcSRusty Russell { 261*14c998f0SMark McLoughlin int num, err; 26205271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 263296f96fcSRusty Russell struct virtio_net_hdr *hdr; 264296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 265296f96fcSRusty Russell 26605271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 2674d125de3SRusty Russell 26899ffc696SRusty Russell pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb, 26921f644f3SDavid S. Miller dest[0], dest[1], dest[2], 27021f644f3SDavid S. Miller dest[3], dest[4], dest[5]); 271296f96fcSRusty Russell 272296f96fcSRusty Russell /* Encode metadata header at front. */ 273296f96fcSRusty Russell hdr = skb_vnet_hdr(skb); 274296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 275296f96fcSRusty Russell hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 276296f96fcSRusty Russell hdr->csum_start = skb->csum_start - skb_headroom(skb); 277296f96fcSRusty Russell hdr->csum_offset = skb->csum_offset; 278296f96fcSRusty Russell } else { 279296f96fcSRusty Russell hdr->flags = 0; 280296f96fcSRusty Russell hdr->csum_offset = hdr->csum_start = 0; 281296f96fcSRusty Russell } 282296f96fcSRusty Russell 283296f96fcSRusty Russell if (skb_is_gso(skb)) { 28450c8ea80SRusty Russell hdr->hdr_len = skb_transport_header(skb) - skb->data; 285296f96fcSRusty Russell hdr->gso_size = skb_shinfo(skb)->gso_size; 28634a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 287296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 288296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 289296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 290296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 291296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; 292296f96fcSRusty Russell else 293296f96fcSRusty Russell BUG(); 29434a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 29534a48579SRusty Russell hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 296296f96fcSRusty Russell } else { 297296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; 29850c8ea80SRusty Russell hdr->gso_size = hdr->hdr_len = 0; 299296f96fcSRusty Russell } 300296f96fcSRusty Russell 301296f96fcSRusty Russell vnet_hdr_to_sg(sg, skb); 302296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 30399ffc696SRusty Russell 304*14c998f0SMark McLoughlin err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); 305*14c998f0SMark McLoughlin if (!err) 306*14c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 307*14c998f0SMark McLoughlin 308*14c998f0SMark McLoughlin return err; 30999ffc696SRusty Russell } 31099ffc696SRusty Russell 31111a3a154SRusty Russell static void xmit_tasklet(unsigned long data) 31211a3a154SRusty Russell { 31311a3a154SRusty Russell struct virtnet_info *vi = (void *)data; 31411a3a154SRusty Russell 31511a3a154SRusty Russell netif_tx_lock_bh(vi->dev); 31611a3a154SRusty Russell if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { 31711a3a154SRusty Russell vi->svq->vq_ops->kick(vi->svq); 31811a3a154SRusty Russell vi->last_xmit_skb = NULL; 31911a3a154SRusty Russell } 32011a3a154SRusty Russell netif_tx_unlock_bh(vi->dev); 32111a3a154SRusty Russell } 32211a3a154SRusty Russell 32399ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev) 32499ffc696SRusty Russell { 32599ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 3262cb9c6baSRusty Russell 3272cb9c6baSRusty Russell again: 3282cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 3292cb9c6baSRusty Russell free_old_xmit_skbs(vi); 33099ffc696SRusty Russell 33199ffc696SRusty Russell /* If we has a buffer left over from last time, send it now. */ 3327eb2e251SRusty Russell if (unlikely(vi->last_xmit_skb)) { 33399ffc696SRusty Russell if (xmit_skb(vi, vi->last_xmit_skb) != 0) { 33499ffc696SRusty Russell /* Drop this skb: we only queue one. */ 33599ffc696SRusty Russell vi->dev->stats.tx_dropped++; 33699ffc696SRusty Russell kfree_skb(skb); 3377eb2e251SRusty Russell skb = NULL; 33899ffc696SRusty Russell goto stop_queue; 33999ffc696SRusty Russell } 34099ffc696SRusty Russell vi->last_xmit_skb = NULL; 34199ffc696SRusty Russell } 34299ffc696SRusty Russell 34399ffc696SRusty Russell /* Put new one in send queue and do transmit */ 3447eb2e251SRusty Russell if (likely(skb)) { 34599ffc696SRusty Russell __skb_queue_head(&vi->send, skb); 34699ffc696SRusty Russell if (xmit_skb(vi, skb) != 0) { 34799ffc696SRusty Russell vi->last_xmit_skb = skb; 3487eb2e251SRusty Russell skb = NULL; 34999ffc696SRusty Russell goto stop_queue; 35099ffc696SRusty Russell } 3517eb2e251SRusty Russell } 35299ffc696SRusty Russell done: 35399ffc696SRusty Russell vi->svq->vq_ops->kick(vi->svq); 35499ffc696SRusty Russell return NETDEV_TX_OK; 35599ffc696SRusty Russell 35699ffc696SRusty Russell stop_queue: 357296f96fcSRusty Russell pr_debug("%s: virtio not prepared to send\n", dev->name); 358296f96fcSRusty Russell netif_stop_queue(dev); 3592cb9c6baSRusty Russell 3604265f161SChristian Borntraeger /* Activate callback for using skbs: if this returns false it 3612cb9c6baSRusty Russell * means some were used in the meantime. */ 3622cb9c6baSRusty Russell if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { 3634265f161SChristian Borntraeger vi->svq->vq_ops->disable_cb(vi->svq); 3642cb9c6baSRusty Russell netif_start_queue(dev); 3652cb9c6baSRusty Russell goto again; 3662cb9c6baSRusty Russell } 36799ffc696SRusty Russell goto done; 368296f96fcSRusty Russell } 369296f96fcSRusty Russell 370da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 371da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 372da74e89dSAmit Shah { 373da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 374da74e89dSAmit Shah 375da74e89dSAmit Shah napi_schedule(&vi->napi); 376da74e89dSAmit Shah } 377da74e89dSAmit Shah #endif 378da74e89dSAmit Shah 379296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 380296f96fcSRusty Russell { 381296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 382296f96fcSRusty Russell 383296f96fcSRusty Russell napi_enable(&vi->napi); 384a48bd8f6SRusty Russell 385a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 386a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 387370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 388370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 389370076d9SChristian Borntraeger if (netif_rx_schedule_prep(dev, &vi->napi)) { 390a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 391370076d9SChristian Borntraeger __netif_rx_schedule(dev, &vi->napi); 392370076d9SChristian Borntraeger } 393296f96fcSRusty Russell return 0; 394296f96fcSRusty Russell } 395296f96fcSRusty Russell 396296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 397296f96fcSRusty Russell { 398296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 399296f96fcSRusty Russell 400296f96fcSRusty Russell napi_disable(&vi->napi); 401296f96fcSRusty Russell 402296f96fcSRusty Russell return 0; 403296f96fcSRusty Russell } 404296f96fcSRusty Russell 405296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 406296f96fcSRusty Russell { 407296f96fcSRusty Russell int err; 408296f96fcSRusty Russell struct net_device *dev; 409296f96fcSRusty Russell struct virtnet_info *vi; 410296f96fcSRusty Russell 411296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 412296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 413296f96fcSRusty Russell if (!dev) 414296f96fcSRusty Russell return -ENOMEM; 415296f96fcSRusty Russell 416296f96fcSRusty Russell /* Set up network device as normal. */ 417296f96fcSRusty Russell dev->open = virtnet_open; 418296f96fcSRusty Russell dev->stop = virtnet_close; 419296f96fcSRusty Russell dev->hard_start_xmit = start_xmit; 420296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 421da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 422da74e89dSAmit Shah dev->poll_controller = virtnet_netpoll; 423da74e89dSAmit Shah #endif 424296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 425296f96fcSRusty Russell 426296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 427c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 428296f96fcSRusty Russell /* This opens up the world of extra features. */ 429296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 430c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 43134a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 43234a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 43334a48579SRusty Russell } 4345539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 435c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 4365539ae96SRusty Russell dev->features |= NETIF_F_TSO; 437c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 4385539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 439c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 4405539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 441c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 4425539ae96SRusty Russell dev->features |= NETIF_F_UFO; 443296f96fcSRusty Russell } 444296f96fcSRusty Russell 445296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 446c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 447a586d4f6SRusty Russell vdev->config->get(vdev, 448a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 449a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 450296f96fcSRusty Russell } else 451296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 452296f96fcSRusty Russell 453296f96fcSRusty Russell /* Set up our device-specific information */ 454296f96fcSRusty Russell vi = netdev_priv(dev); 4556c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 456296f96fcSRusty Russell vi->dev = dev; 457296f96fcSRusty Russell vi->vdev = vdev; 458d9d5dcc8SChristian Borntraeger vdev->priv = vi; 459296f96fcSRusty Russell 460296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 461a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 462296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 463296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 464296f96fcSRusty Russell goto free; 465296f96fcSRusty Russell } 466296f96fcSRusty Russell 467a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 468296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 469296f96fcSRusty Russell err = PTR_ERR(vi->svq); 470296f96fcSRusty Russell goto free_recv; 471296f96fcSRusty Russell } 472296f96fcSRusty Russell 473296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 474296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 475296f96fcSRusty Russell skb_queue_head_init(&vi->send); 476296f96fcSRusty Russell 47711a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 47811a3a154SRusty Russell 479*14c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 480*14c998f0SMark McLoughlin 481296f96fcSRusty Russell err = register_netdev(dev); 482296f96fcSRusty Russell if (err) { 483296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 484296f96fcSRusty Russell goto free_send; 485296f96fcSRusty Russell } 486b3369c1fSRusty Russell 487b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 488b3369c1fSRusty Russell try_fill_recv(vi); 489b3369c1fSRusty Russell 490b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 491b3369c1fSRusty Russell if (vi->num == 0) { 492b3369c1fSRusty Russell err = -ENOMEM; 493b3369c1fSRusty Russell goto unregister; 494b3369c1fSRusty Russell } 495b3369c1fSRusty Russell 496296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 497296f96fcSRusty Russell return 0; 498296f96fcSRusty Russell 499b3369c1fSRusty Russell unregister: 500b3369c1fSRusty Russell unregister_netdev(dev); 501296f96fcSRusty Russell free_send: 502296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 503296f96fcSRusty Russell free_recv: 504296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 505296f96fcSRusty Russell free: 506296f96fcSRusty Russell free_netdev(dev); 507296f96fcSRusty Russell return err; 508296f96fcSRusty Russell } 509296f96fcSRusty Russell 510296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 511296f96fcSRusty Russell { 51274b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 513b3369c1fSRusty Russell struct sk_buff *skb; 514b3369c1fSRusty Russell 5156e5aa7efSRusty Russell /* Stop all the virtqueues. */ 5166e5aa7efSRusty Russell vdev->config->reset(vdev); 5176e5aa7efSRusty Russell 518*14c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 519*14c998f0SMark McLoughlin 520b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 521b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 522b3369c1fSRusty Russell kfree_skb(skb); 523b3369c1fSRusty Russell vi->num--; 524b3369c1fSRusty Russell } 525288369ccSWang Chen __skb_queue_purge(&vi->send); 526b3369c1fSRusty Russell 527b3369c1fSRusty Russell BUG_ON(vi->num != 0); 52874b2553fSRusty Russell 52974b2553fSRusty Russell vdev->config->del_vq(vi->svq); 53074b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 53174b2553fSRusty Russell unregister_netdev(vi->dev); 53274b2553fSRusty Russell free_netdev(vi->dev); 533296f96fcSRusty Russell } 534296f96fcSRusty Russell 535296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 536296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 537296f96fcSRusty Russell { 0 }, 538296f96fcSRusty Russell }; 539296f96fcSRusty Russell 540c45a6816SRusty Russell static unsigned int features[] = { 541c45a6816SRusty Russell VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 542c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 543c45a6816SRusty Russell VIRTIO_NET_F_HOST_ECN, 544c45a6816SRusty Russell }; 545c45a6816SRusty Russell 546296f96fcSRusty Russell static struct virtio_driver virtio_net = { 547c45a6816SRusty Russell .feature_table = features, 548c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 549296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 550296f96fcSRusty Russell .driver.owner = THIS_MODULE, 551296f96fcSRusty Russell .id_table = id_table, 552296f96fcSRusty Russell .probe = virtnet_probe, 553296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 554296f96fcSRusty Russell }; 555296f96fcSRusty Russell 556296f96fcSRusty Russell static int __init init(void) 557296f96fcSRusty Russell { 558296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 559296f96fcSRusty Russell } 560296f96fcSRusty Russell 561296f96fcSRusty Russell static void __exit fini(void) 562296f96fcSRusty Russell { 563296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 564296f96fcSRusty Russell } 565296f96fcSRusty Russell module_init(init); 566296f96fcSRusty Russell module_exit(fini); 567296f96fcSRusty Russell 568296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 569296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 570296f96fcSRusty Russell MODULE_LICENSE("GPL"); 571