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> 22a9ea3fc6SHerbert Xu #include <linux/ethtool.h> 23296f96fcSRusty Russell #include <linux/module.h> 24296f96fcSRusty Russell #include <linux/virtio.h> 25296f96fcSRusty Russell #include <linux/virtio_net.h> 26296f96fcSRusty Russell #include <linux/scatterlist.h> 27e918085aSAlex Williamson #include <linux/if_vlan.h> 28296f96fcSRusty Russell 296c0cd7c0SDor Laor static int napi_weight = 128; 306c0cd7c0SDor Laor module_param(napi_weight, int, 0444); 316c0cd7c0SDor Laor 3234a48579SRusty Russell static int csum = 1, gso = 1; 3334a48579SRusty Russell module_param(csum, bool, 0444); 3434a48579SRusty Russell module_param(gso, bool, 0444); 3534a48579SRusty Russell 36296f96fcSRusty Russell /* FIXME: MTU in config. */ 37e918085aSAlex Williamson #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) 383f2c31d9SMark McLoughlin #define GOOD_COPY_LEN 128 39296f96fcSRusty Russell 40*2af7698eSAlex Williamson #define VIRTNET_SEND_COMMAND_SG_MAX 1 412a41f71dSAlex Williamson 42296f96fcSRusty Russell struct virtnet_info 43296f96fcSRusty Russell { 44296f96fcSRusty Russell struct virtio_device *vdev; 452a41f71dSAlex Williamson struct virtqueue *rvq, *svq, *cvq; 46296f96fcSRusty Russell struct net_device *dev; 47296f96fcSRusty Russell struct napi_struct napi; 489f4d26d0SMark McLoughlin unsigned int status; 49296f96fcSRusty Russell 5099ffc696SRusty Russell /* The skb we couldn't send because buffers were full. */ 5199ffc696SRusty Russell struct sk_buff *last_xmit_skb; 5299ffc696SRusty Russell 53363f1514SRusty Russell /* If we need to free in a timer, this is it. */ 5414c998f0SMark McLoughlin struct timer_list xmit_free_timer; 5514c998f0SMark McLoughlin 56296f96fcSRusty Russell /* Number of input buffers, and max we've ever had. */ 57296f96fcSRusty Russell unsigned int num, max; 58296f96fcSRusty Russell 5911a3a154SRusty Russell /* For cleaning up after transmission. */ 6011a3a154SRusty Russell struct tasklet_struct tasklet; 61363f1514SRusty Russell bool free_in_tasklet; 6211a3a154SRusty Russell 6397402b96SHerbert Xu /* I like... big packets and I cannot lie! */ 6497402b96SHerbert Xu bool big_packets; 6597402b96SHerbert Xu 663f2c31d9SMark McLoughlin /* Host will merge rx buffers for big packets (shake it! shake it!) */ 673f2c31d9SMark McLoughlin bool mergeable_rx_bufs; 683f2c31d9SMark McLoughlin 69296f96fcSRusty Russell /* Receive & send queues. */ 70296f96fcSRusty Russell struct sk_buff_head recv; 71296f96fcSRusty Russell struct sk_buff_head send; 72fb6813f4SRusty Russell 73fb6813f4SRusty Russell /* Chain pages by the private ptr. */ 74fb6813f4SRusty Russell struct page *pages; 75296f96fcSRusty Russell }; 76296f96fcSRusty Russell 773f2c31d9SMark McLoughlin static inline void *skb_vnet_hdr(struct sk_buff *skb) 78296f96fcSRusty Russell { 79296f96fcSRusty Russell return (struct virtio_net_hdr *)skb->cb; 80296f96fcSRusty Russell } 81296f96fcSRusty Russell 82fb6813f4SRusty Russell static void give_a_page(struct virtnet_info *vi, struct page *page) 83fb6813f4SRusty Russell { 84fb6813f4SRusty Russell page->private = (unsigned long)vi->pages; 85fb6813f4SRusty Russell vi->pages = page; 86fb6813f4SRusty Russell } 87fb6813f4SRusty Russell 880a888fd1SMark McLoughlin static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb) 890a888fd1SMark McLoughlin { 900a888fd1SMark McLoughlin unsigned int i; 910a888fd1SMark McLoughlin 920a888fd1SMark McLoughlin for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 930a888fd1SMark McLoughlin give_a_page(vi, skb_shinfo(skb)->frags[i].page); 940a888fd1SMark McLoughlin skb_shinfo(skb)->nr_frags = 0; 950a888fd1SMark McLoughlin skb->data_len = 0; 960a888fd1SMark McLoughlin } 970a888fd1SMark McLoughlin 98fb6813f4SRusty Russell static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask) 99fb6813f4SRusty Russell { 100fb6813f4SRusty Russell struct page *p = vi->pages; 101fb6813f4SRusty Russell 102fb6813f4SRusty Russell if (p) 103fb6813f4SRusty Russell vi->pages = (struct page *)p->private; 104fb6813f4SRusty Russell else 105fb6813f4SRusty Russell p = alloc_page(gfp_mask); 106fb6813f4SRusty Russell return p; 107fb6813f4SRusty Russell } 108fb6813f4SRusty Russell 1092cb9c6baSRusty Russell static void skb_xmit_done(struct virtqueue *svq) 110296f96fcSRusty Russell { 1112cb9c6baSRusty Russell struct virtnet_info *vi = svq->vdev->priv; 112296f96fcSRusty Russell 1132cb9c6baSRusty Russell /* Suppress further interrupts. */ 1142cb9c6baSRusty Russell svq->vq_ops->disable_cb(svq); 11511a3a154SRusty Russell 116363f1514SRusty Russell /* We were probably waiting for more output buffers. */ 117296f96fcSRusty Russell netif_wake_queue(vi->dev); 11811a3a154SRusty Russell 11911a3a154SRusty Russell /* Make sure we re-xmit last_xmit_skb: if there are no more packets 12011a3a154SRusty Russell * queued, start_xmit won't be called. */ 12111a3a154SRusty Russell tasklet_schedule(&vi->tasklet); 122296f96fcSRusty Russell } 123296f96fcSRusty Russell 124296f96fcSRusty Russell static void receive_skb(struct net_device *dev, struct sk_buff *skb, 125296f96fcSRusty Russell unsigned len) 126296f96fcSRusty Russell { 1273f2c31d9SMark McLoughlin struct virtnet_info *vi = netdev_priv(dev); 128296f96fcSRusty Russell struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 12997402b96SHerbert Xu int err; 1303f2c31d9SMark McLoughlin int i; 131296f96fcSRusty Russell 132296f96fcSRusty Russell if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { 133296f96fcSRusty Russell pr_debug("%s: short packet %i\n", dev->name, len); 134296f96fcSRusty Russell dev->stats.rx_length_errors++; 135296f96fcSRusty Russell goto drop; 136296f96fcSRusty Russell } 1373f2c31d9SMark McLoughlin 1383f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) { 1393f2c31d9SMark McLoughlin struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); 1403f2c31d9SMark McLoughlin unsigned int copy; 1413f2c31d9SMark McLoughlin char *p = page_address(skb_shinfo(skb)->frags[0].page); 1423f2c31d9SMark McLoughlin 1433f2c31d9SMark McLoughlin if (len > PAGE_SIZE) 1443f2c31d9SMark McLoughlin len = PAGE_SIZE; 1453f2c31d9SMark McLoughlin len -= sizeof(struct virtio_net_hdr_mrg_rxbuf); 1463f2c31d9SMark McLoughlin 1473f2c31d9SMark McLoughlin memcpy(hdr, p, sizeof(*mhdr)); 1483f2c31d9SMark McLoughlin p += sizeof(*mhdr); 1493f2c31d9SMark McLoughlin 1503f2c31d9SMark McLoughlin copy = len; 1513f2c31d9SMark McLoughlin if (copy > skb_tailroom(skb)) 1523f2c31d9SMark McLoughlin copy = skb_tailroom(skb); 1533f2c31d9SMark McLoughlin 1543f2c31d9SMark McLoughlin memcpy(skb_put(skb, copy), p, copy); 1553f2c31d9SMark McLoughlin 1563f2c31d9SMark McLoughlin len -= copy; 1573f2c31d9SMark McLoughlin 1583f2c31d9SMark McLoughlin if (!len) { 1593f2c31d9SMark McLoughlin give_a_page(vi, skb_shinfo(skb)->frags[0].page); 1603f2c31d9SMark McLoughlin skb_shinfo(skb)->nr_frags--; 1613f2c31d9SMark McLoughlin } else { 1623f2c31d9SMark McLoughlin skb_shinfo(skb)->frags[0].page_offset += 1633f2c31d9SMark McLoughlin sizeof(*mhdr) + copy; 1643f2c31d9SMark McLoughlin skb_shinfo(skb)->frags[0].size = len; 1653f2c31d9SMark McLoughlin skb->data_len += len; 1663f2c31d9SMark McLoughlin skb->len += len; 1673f2c31d9SMark McLoughlin } 1683f2c31d9SMark McLoughlin 1693f2c31d9SMark McLoughlin while (--mhdr->num_buffers) { 1703f2c31d9SMark McLoughlin struct sk_buff *nskb; 1713f2c31d9SMark McLoughlin 1723f2c31d9SMark McLoughlin i = skb_shinfo(skb)->nr_frags; 1733f2c31d9SMark McLoughlin if (i >= MAX_SKB_FRAGS) { 1743f2c31d9SMark McLoughlin pr_debug("%s: packet too long %d\n", dev->name, 1753f2c31d9SMark McLoughlin len); 1763f2c31d9SMark McLoughlin dev->stats.rx_length_errors++; 1773f2c31d9SMark McLoughlin goto drop; 1783f2c31d9SMark McLoughlin } 1793f2c31d9SMark McLoughlin 1803f2c31d9SMark McLoughlin nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len); 1813f2c31d9SMark McLoughlin if (!nskb) { 1823f2c31d9SMark McLoughlin pr_debug("%s: rx error: %d buffers missing\n", 1833f2c31d9SMark McLoughlin dev->name, mhdr->num_buffers); 1843f2c31d9SMark McLoughlin dev->stats.rx_length_errors++; 1853f2c31d9SMark McLoughlin goto drop; 1863f2c31d9SMark McLoughlin } 1873f2c31d9SMark McLoughlin 1883f2c31d9SMark McLoughlin __skb_unlink(nskb, &vi->recv); 1893f2c31d9SMark McLoughlin vi->num--; 1903f2c31d9SMark McLoughlin 1913f2c31d9SMark McLoughlin skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0]; 1923f2c31d9SMark McLoughlin skb_shinfo(nskb)->nr_frags = 0; 1933f2c31d9SMark McLoughlin kfree_skb(nskb); 1943f2c31d9SMark McLoughlin 1953f2c31d9SMark McLoughlin if (len > PAGE_SIZE) 1963f2c31d9SMark McLoughlin len = PAGE_SIZE; 1973f2c31d9SMark McLoughlin 1983f2c31d9SMark McLoughlin skb_shinfo(skb)->frags[i].size = len; 1993f2c31d9SMark McLoughlin skb_shinfo(skb)->nr_frags++; 2003f2c31d9SMark McLoughlin skb->data_len += len; 2013f2c31d9SMark McLoughlin skb->len += len; 2023f2c31d9SMark McLoughlin } 2033f2c31d9SMark McLoughlin } else { 204296f96fcSRusty Russell len -= sizeof(struct virtio_net_hdr); 205296f96fcSRusty Russell 2060a888fd1SMark McLoughlin if (len <= MAX_PACKET_LEN) 2073f2c31d9SMark McLoughlin trim_pages(vi, skb); 208fb6813f4SRusty Russell 20997402b96SHerbert Xu err = pskb_trim(skb, len); 21097402b96SHerbert Xu if (err) { 2113f2c31d9SMark McLoughlin pr_debug("%s: pskb_trim failed %i %d\n", dev->name, 2123f2c31d9SMark McLoughlin len, err); 21397402b96SHerbert Xu dev->stats.rx_dropped++; 21497402b96SHerbert Xu goto drop; 21597402b96SHerbert Xu } 2163f2c31d9SMark McLoughlin } 2173f2c31d9SMark McLoughlin 21897402b96SHerbert Xu skb->truesize += skb->data_len; 219296f96fcSRusty Russell dev->stats.rx_bytes += skb->len; 220296f96fcSRusty Russell dev->stats.rx_packets++; 221296f96fcSRusty Russell 222296f96fcSRusty Russell if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 223296f96fcSRusty Russell pr_debug("Needs csum!\n"); 224f35d9d8aSRusty Russell if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) 225296f96fcSRusty Russell goto frame_err; 226296f96fcSRusty Russell } 227296f96fcSRusty Russell 22823cde76dSMark McLoughlin skb->protocol = eth_type_trans(skb, dev); 22923cde76dSMark McLoughlin pr_debug("Receiving skb proto 0x%04x len %i type %i\n", 23023cde76dSMark McLoughlin ntohs(skb->protocol), skb->len, skb->pkt_type); 23123cde76dSMark McLoughlin 232296f96fcSRusty Russell if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { 233296f96fcSRusty Russell pr_debug("GSO!\n"); 23434a48579SRusty Russell switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { 235296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV4: 236296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; 237296f96fcSRusty Russell break; 238296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_UDP: 239296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 240296f96fcSRusty Russell break; 241296f96fcSRusty Russell case VIRTIO_NET_HDR_GSO_TCPV6: 242296f96fcSRusty Russell skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; 243296f96fcSRusty Russell break; 244296f96fcSRusty Russell default: 245296f96fcSRusty Russell if (net_ratelimit()) 246296f96fcSRusty Russell printk(KERN_WARNING "%s: bad gso type %u.\n", 247296f96fcSRusty Russell dev->name, hdr->gso_type); 248296f96fcSRusty Russell goto frame_err; 249296f96fcSRusty Russell } 250296f96fcSRusty Russell 25134a48579SRusty Russell if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) 25234a48579SRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 25334a48579SRusty Russell 254296f96fcSRusty Russell skb_shinfo(skb)->gso_size = hdr->gso_size; 255296f96fcSRusty Russell if (skb_shinfo(skb)->gso_size == 0) { 256296f96fcSRusty Russell if (net_ratelimit()) 257296f96fcSRusty Russell printk(KERN_WARNING "%s: zero gso size.\n", 258296f96fcSRusty Russell dev->name); 259296f96fcSRusty Russell goto frame_err; 260296f96fcSRusty Russell } 261296f96fcSRusty Russell 262296f96fcSRusty Russell /* Header must be checked, and gso_segs computed. */ 263296f96fcSRusty Russell skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 264296f96fcSRusty Russell skb_shinfo(skb)->gso_segs = 0; 265296f96fcSRusty Russell } 266296f96fcSRusty Russell 267296f96fcSRusty Russell netif_receive_skb(skb); 268296f96fcSRusty Russell return; 269296f96fcSRusty Russell 270296f96fcSRusty Russell frame_err: 271296f96fcSRusty Russell dev->stats.rx_frame_errors++; 272296f96fcSRusty Russell drop: 273296f96fcSRusty Russell dev_kfree_skb(skb); 274296f96fcSRusty Russell } 275296f96fcSRusty Russell 2763f2c31d9SMark McLoughlin static void try_fill_recv_maxbufs(struct virtnet_info *vi) 277296f96fcSRusty Russell { 278296f96fcSRusty Russell struct sk_buff *skb; 27905271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 28097402b96SHerbert Xu int num, err, i; 281296f96fcSRusty Russell 28205271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 283296f96fcSRusty Russell for (;;) { 2843f2c31d9SMark McLoughlin struct virtio_net_hdr *hdr; 2853f2c31d9SMark McLoughlin 286296f96fcSRusty Russell skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); 287296f96fcSRusty Russell if (unlikely(!skb)) 288296f96fcSRusty Russell break; 289296f96fcSRusty Russell 290296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 2913f2c31d9SMark McLoughlin 2923f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 2938527bec5SIra W. Snyder sg_set_buf(sg, hdr, sizeof(*hdr)); 29497402b96SHerbert Xu 29597402b96SHerbert Xu if (vi->big_packets) { 29697402b96SHerbert Xu for (i = 0; i < MAX_SKB_FRAGS; i++) { 29797402b96SHerbert Xu skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 298fb6813f4SRusty Russell f->page = get_a_page(vi, GFP_ATOMIC); 29997402b96SHerbert Xu if (!f->page) 30097402b96SHerbert Xu break; 30197402b96SHerbert Xu 30297402b96SHerbert Xu f->page_offset = 0; 30397402b96SHerbert Xu f->size = PAGE_SIZE; 30497402b96SHerbert Xu 30597402b96SHerbert Xu skb->data_len += PAGE_SIZE; 30697402b96SHerbert Xu skb->len += PAGE_SIZE; 30797402b96SHerbert Xu 30897402b96SHerbert Xu skb_shinfo(skb)->nr_frags++; 30997402b96SHerbert Xu } 31097402b96SHerbert Xu } 31197402b96SHerbert Xu 312296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 313296f96fcSRusty Russell skb_queue_head(&vi->recv, skb); 314296f96fcSRusty Russell 315296f96fcSRusty Russell err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); 316296f96fcSRusty Russell if (err) { 317296f96fcSRusty Russell skb_unlink(skb, &vi->recv); 3180a888fd1SMark McLoughlin trim_pages(vi, skb); 319296f96fcSRusty Russell kfree_skb(skb); 320296f96fcSRusty Russell break; 321296f96fcSRusty Russell } 322296f96fcSRusty Russell vi->num++; 323296f96fcSRusty Russell } 324296f96fcSRusty Russell if (unlikely(vi->num > vi->max)) 325296f96fcSRusty Russell vi->max = vi->num; 326296f96fcSRusty Russell vi->rvq->vq_ops->kick(vi->rvq); 327296f96fcSRusty Russell } 328296f96fcSRusty Russell 3293f2c31d9SMark McLoughlin static void try_fill_recv(struct virtnet_info *vi) 3303f2c31d9SMark McLoughlin { 3313f2c31d9SMark McLoughlin struct sk_buff *skb; 3323f2c31d9SMark McLoughlin struct scatterlist sg[1]; 3333f2c31d9SMark McLoughlin int err; 3343f2c31d9SMark McLoughlin 3353f2c31d9SMark McLoughlin if (!vi->mergeable_rx_bufs) { 3363f2c31d9SMark McLoughlin try_fill_recv_maxbufs(vi); 3373f2c31d9SMark McLoughlin return; 3383f2c31d9SMark McLoughlin } 3393f2c31d9SMark McLoughlin 3403f2c31d9SMark McLoughlin for (;;) { 3413f2c31d9SMark McLoughlin skb_frag_t *f; 3423f2c31d9SMark McLoughlin 3433f2c31d9SMark McLoughlin skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); 3443f2c31d9SMark McLoughlin if (unlikely(!skb)) 3453f2c31d9SMark McLoughlin break; 3463f2c31d9SMark McLoughlin 3473f2c31d9SMark McLoughlin skb_reserve(skb, NET_IP_ALIGN); 3483f2c31d9SMark McLoughlin 3493f2c31d9SMark McLoughlin f = &skb_shinfo(skb)->frags[0]; 3503f2c31d9SMark McLoughlin f->page = get_a_page(vi, GFP_ATOMIC); 3513f2c31d9SMark McLoughlin if (!f->page) { 3523f2c31d9SMark McLoughlin kfree_skb(skb); 3533f2c31d9SMark McLoughlin break; 3543f2c31d9SMark McLoughlin } 3553f2c31d9SMark McLoughlin 3563f2c31d9SMark McLoughlin f->page_offset = 0; 3573f2c31d9SMark McLoughlin f->size = PAGE_SIZE; 3583f2c31d9SMark McLoughlin 3593f2c31d9SMark McLoughlin skb_shinfo(skb)->nr_frags++; 3603f2c31d9SMark McLoughlin 3613f2c31d9SMark McLoughlin sg_init_one(sg, page_address(f->page), PAGE_SIZE); 3623f2c31d9SMark McLoughlin skb_queue_head(&vi->recv, skb); 3633f2c31d9SMark McLoughlin 3643f2c31d9SMark McLoughlin err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); 3653f2c31d9SMark McLoughlin if (err) { 3663f2c31d9SMark McLoughlin skb_unlink(skb, &vi->recv); 3673f2c31d9SMark McLoughlin kfree_skb(skb); 3683f2c31d9SMark McLoughlin break; 3693f2c31d9SMark McLoughlin } 3703f2c31d9SMark McLoughlin vi->num++; 3713f2c31d9SMark McLoughlin } 3723f2c31d9SMark McLoughlin if (unlikely(vi->num > vi->max)) 3733f2c31d9SMark McLoughlin vi->max = vi->num; 3743f2c31d9SMark McLoughlin vi->rvq->vq_ops->kick(vi->rvq); 3753f2c31d9SMark McLoughlin } 3763f2c31d9SMark McLoughlin 37718445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 378296f96fcSRusty Russell { 379296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 38018445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 381288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 38218445c4dSRusty Russell rvq->vq_ops->disable_cb(rvq); 383288379f0SBen Hutchings __napi_schedule(&vi->napi); 38418445c4dSRusty Russell } 385296f96fcSRusty Russell } 386296f96fcSRusty Russell 387296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 388296f96fcSRusty Russell { 389296f96fcSRusty Russell struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); 390296f96fcSRusty Russell struct sk_buff *skb = NULL; 391296f96fcSRusty Russell unsigned int len, received = 0; 392296f96fcSRusty Russell 393296f96fcSRusty Russell again: 394296f96fcSRusty Russell while (received < budget && 395296f96fcSRusty Russell (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { 396296f96fcSRusty Russell __skb_unlink(skb, &vi->recv); 397296f96fcSRusty Russell receive_skb(vi->dev, skb, len); 398296f96fcSRusty Russell vi->num--; 399296f96fcSRusty Russell received++; 400296f96fcSRusty Russell } 401296f96fcSRusty Russell 402296f96fcSRusty Russell /* FIXME: If we oom and completely run out of inbufs, we need 403296f96fcSRusty Russell * to start a timer trying to fill more. */ 404296f96fcSRusty Russell if (vi->num < vi->max / 2) 405296f96fcSRusty Russell try_fill_recv(vi); 406296f96fcSRusty Russell 4078329d98eSRusty Russell /* Out of packets? */ 4088329d98eSRusty Russell if (received < budget) { 409288379f0SBen Hutchings napi_complete(napi); 41018445c4dSRusty Russell if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) 4114265f161SChristian Borntraeger && napi_schedule_prep(napi)) { 4124265f161SChristian Borntraeger vi->rvq->vq_ops->disable_cb(vi->rvq); 413288379f0SBen Hutchings __napi_schedule(napi); 414296f96fcSRusty Russell goto again; 415296f96fcSRusty Russell } 4164265f161SChristian Borntraeger } 417296f96fcSRusty Russell 418296f96fcSRusty Russell return received; 419296f96fcSRusty Russell } 420296f96fcSRusty Russell 421296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi) 422296f96fcSRusty Russell { 423296f96fcSRusty Russell struct sk_buff *skb; 424296f96fcSRusty Russell unsigned int len; 425296f96fcSRusty Russell 426296f96fcSRusty Russell while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { 427296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 428296f96fcSRusty Russell __skb_unlink(skb, &vi->send); 429655aa31fSRusty Russell vi->dev->stats.tx_bytes += skb->len; 430296f96fcSRusty Russell vi->dev->stats.tx_packets++; 431296f96fcSRusty Russell kfree_skb(skb); 432296f96fcSRusty Russell } 433296f96fcSRusty Russell } 434296f96fcSRusty Russell 435363f1514SRusty Russell /* If the virtio transport doesn't always notify us when all in-flight packets 436363f1514SRusty Russell * are consumed, we fall back to using this function on a timer to free them. */ 43714c998f0SMark McLoughlin static void xmit_free(unsigned long data) 43814c998f0SMark McLoughlin { 43914c998f0SMark McLoughlin struct virtnet_info *vi = (void *)data; 44014c998f0SMark McLoughlin 44114c998f0SMark McLoughlin netif_tx_lock(vi->dev); 44214c998f0SMark McLoughlin 44314c998f0SMark McLoughlin free_old_xmit_skbs(vi); 44414c998f0SMark McLoughlin 44514c998f0SMark McLoughlin if (!skb_queue_empty(&vi->send)) 44614c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 44714c998f0SMark McLoughlin 44814c998f0SMark McLoughlin netif_tx_unlock(vi->dev); 44914c998f0SMark McLoughlin } 45014c998f0SMark McLoughlin 45199ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) 452296f96fcSRusty Russell { 45314c998f0SMark McLoughlin int num, err; 45405271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 4553f2c31d9SMark McLoughlin struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); 4563f2c31d9SMark McLoughlin struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 457296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 458296f96fcSRusty Russell 45905271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 4604d125de3SRusty Russell 461e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 462296f96fcSRusty Russell 463296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 464296f96fcSRusty Russell hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 465296f96fcSRusty Russell hdr->csum_start = skb->csum_start - skb_headroom(skb); 466296f96fcSRusty Russell hdr->csum_offset = skb->csum_offset; 467296f96fcSRusty Russell } else { 468296f96fcSRusty Russell hdr->flags = 0; 469296f96fcSRusty Russell hdr->csum_offset = hdr->csum_start = 0; 470296f96fcSRusty Russell } 471296f96fcSRusty Russell 472296f96fcSRusty Russell if (skb_is_gso(skb)) { 47350c8ea80SRusty Russell hdr->hdr_len = skb_transport_header(skb) - skb->data; 474296f96fcSRusty Russell hdr->gso_size = skb_shinfo(skb)->gso_size; 47534a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 476296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 477296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 478296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 479296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 480296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; 481296f96fcSRusty Russell else 482296f96fcSRusty Russell BUG(); 48334a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 48434a48579SRusty Russell hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 485296f96fcSRusty Russell } else { 486296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; 48750c8ea80SRusty Russell hdr->gso_size = hdr->hdr_len = 0; 488296f96fcSRusty Russell } 489296f96fcSRusty Russell 4903f2c31d9SMark McLoughlin mhdr->num_buffers = 0; 4913f2c31d9SMark McLoughlin 4923f2c31d9SMark McLoughlin /* Encode metadata header at front. */ 4933f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) 4948527bec5SIra W. Snyder sg_set_buf(sg, mhdr, sizeof(*mhdr)); 4953f2c31d9SMark McLoughlin else 4968527bec5SIra W. Snyder sg_set_buf(sg, hdr, sizeof(*hdr)); 4973f2c31d9SMark McLoughlin 498296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 49999ffc696SRusty Russell 50014c998f0SMark McLoughlin err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); 501363f1514SRusty Russell if (!err && !vi->free_in_tasklet) 50214c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 50314c998f0SMark McLoughlin 50414c998f0SMark McLoughlin return err; 50599ffc696SRusty Russell } 50699ffc696SRusty Russell 50711a3a154SRusty Russell static void xmit_tasklet(unsigned long data) 50811a3a154SRusty Russell { 50911a3a154SRusty Russell struct virtnet_info *vi = (void *)data; 51011a3a154SRusty Russell 51111a3a154SRusty Russell netif_tx_lock_bh(vi->dev); 51211a3a154SRusty Russell if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { 51311a3a154SRusty Russell vi->svq->vq_ops->kick(vi->svq); 51411a3a154SRusty Russell vi->last_xmit_skb = NULL; 51511a3a154SRusty Russell } 516363f1514SRusty Russell if (vi->free_in_tasklet) 517363f1514SRusty Russell free_old_xmit_skbs(vi); 51811a3a154SRusty Russell netif_tx_unlock_bh(vi->dev); 51911a3a154SRusty Russell } 52011a3a154SRusty Russell 52199ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev) 52299ffc696SRusty Russell { 52399ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 5242cb9c6baSRusty Russell 5252cb9c6baSRusty Russell again: 5262cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 5272cb9c6baSRusty Russell free_old_xmit_skbs(vi); 52899ffc696SRusty Russell 52999ffc696SRusty Russell /* If we has a buffer left over from last time, send it now. */ 5309953ca6cSMark McLoughlin if (unlikely(vi->last_xmit_skb) && 5319953ca6cSMark McLoughlin xmit_skb(vi, vi->last_xmit_skb) != 0) 53299ffc696SRusty Russell goto stop_queue; 5339953ca6cSMark McLoughlin 53499ffc696SRusty Russell vi->last_xmit_skb = NULL; 53599ffc696SRusty Russell 53699ffc696SRusty Russell /* Put new one in send queue and do transmit */ 5377eb2e251SRusty Russell if (likely(skb)) { 53899ffc696SRusty Russell __skb_queue_head(&vi->send, skb); 53999ffc696SRusty Russell if (xmit_skb(vi, skb) != 0) { 54099ffc696SRusty Russell vi->last_xmit_skb = skb; 5417eb2e251SRusty Russell skb = NULL; 54299ffc696SRusty Russell goto stop_queue; 54399ffc696SRusty Russell } 5447eb2e251SRusty Russell } 54599ffc696SRusty Russell done: 54699ffc696SRusty Russell vi->svq->vq_ops->kick(vi->svq); 54799ffc696SRusty Russell return NETDEV_TX_OK; 54899ffc696SRusty Russell 54999ffc696SRusty Russell stop_queue: 550296f96fcSRusty Russell pr_debug("%s: virtio not prepared to send\n", dev->name); 551296f96fcSRusty Russell netif_stop_queue(dev); 5522cb9c6baSRusty Russell 5534265f161SChristian Borntraeger /* Activate callback for using skbs: if this returns false it 5542cb9c6baSRusty Russell * means some were used in the meantime. */ 5552cb9c6baSRusty Russell if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { 5564265f161SChristian Borntraeger vi->svq->vq_ops->disable_cb(vi->svq); 5572cb9c6baSRusty Russell netif_start_queue(dev); 5582cb9c6baSRusty Russell goto again; 5592cb9c6baSRusty Russell } 5609953ca6cSMark McLoughlin if (skb) { 5619953ca6cSMark McLoughlin /* Drop this skb: we only queue one. */ 5629953ca6cSMark McLoughlin vi->dev->stats.tx_dropped++; 5639953ca6cSMark McLoughlin kfree_skb(skb); 5649953ca6cSMark McLoughlin } 56599ffc696SRusty Russell goto done; 566296f96fcSRusty Russell } 567296f96fcSRusty Russell 568da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 569da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 570da74e89dSAmit Shah { 571da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 572da74e89dSAmit Shah 573da74e89dSAmit Shah napi_schedule(&vi->napi); 574da74e89dSAmit Shah } 575da74e89dSAmit Shah #endif 576da74e89dSAmit Shah 577296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 578296f96fcSRusty Russell { 579296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 580296f96fcSRusty Russell 581296f96fcSRusty Russell napi_enable(&vi->napi); 582a48bd8f6SRusty Russell 583a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 584a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 585370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 586370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 587288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 588a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 589288379f0SBen Hutchings __napi_schedule(&vi->napi); 590370076d9SChristian Borntraeger } 591296f96fcSRusty Russell return 0; 592296f96fcSRusty Russell } 593296f96fcSRusty Russell 5942a41f71dSAlex Williamson /* 5952a41f71dSAlex Williamson * Send command via the control virtqueue and check status. Commands 5962a41f71dSAlex Williamson * supported by the hypervisor, as indicated by feature bits, should 5972a41f71dSAlex Williamson * never fail unless improperly formated. 5982a41f71dSAlex Williamson */ 5992a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 6002a41f71dSAlex Williamson struct scatterlist *data, int out, int in) 6012a41f71dSAlex Williamson { 6022a41f71dSAlex Williamson struct scatterlist sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; 6032a41f71dSAlex Williamson struct virtio_net_ctrl_hdr ctrl; 6042a41f71dSAlex Williamson virtio_net_ctrl_ack status = ~0; 6052a41f71dSAlex Williamson unsigned int tmp; 6062a41f71dSAlex Williamson 6072a41f71dSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 6082a41f71dSAlex Williamson BUG(); /* Caller should know better */ 6092a41f71dSAlex Williamson return false; 6102a41f71dSAlex Williamson } 6112a41f71dSAlex Williamson 6122a41f71dSAlex Williamson BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX); 6132a41f71dSAlex Williamson 6142a41f71dSAlex Williamson out++; /* Add header */ 6152a41f71dSAlex Williamson in++; /* Add return status */ 6162a41f71dSAlex Williamson 6172a41f71dSAlex Williamson ctrl.class = class; 6182a41f71dSAlex Williamson ctrl.cmd = cmd; 6192a41f71dSAlex Williamson 6202a41f71dSAlex Williamson sg_init_table(sg, out + in); 6212a41f71dSAlex Williamson 6222a41f71dSAlex Williamson sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); 6232a41f71dSAlex Williamson memcpy(&sg[1], data, sizeof(struct scatterlist) * (out + in - 2)); 6242a41f71dSAlex Williamson sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); 6252a41f71dSAlex Williamson 6262a41f71dSAlex Williamson if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0) 6272a41f71dSAlex Williamson BUG(); 6282a41f71dSAlex Williamson 6292a41f71dSAlex Williamson vi->cvq->vq_ops->kick(vi->cvq); 6302a41f71dSAlex Williamson 6312a41f71dSAlex Williamson /* 6322a41f71dSAlex Williamson * Spin for a response, the kick causes an ioport write, trapping 6332a41f71dSAlex Williamson * into the hypervisor, so the request should be handled immediately. 6342a41f71dSAlex Williamson */ 6352a41f71dSAlex Williamson while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) 6362a41f71dSAlex Williamson cpu_relax(); 6372a41f71dSAlex Williamson 6382a41f71dSAlex Williamson return status == VIRTIO_NET_OK; 6392a41f71dSAlex Williamson } 6402a41f71dSAlex Williamson 641296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 642296f96fcSRusty Russell { 643296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 644296f96fcSRusty Russell 645296f96fcSRusty Russell napi_disable(&vi->napi); 646296f96fcSRusty Russell 647296f96fcSRusty Russell return 0; 648296f96fcSRusty Russell } 649296f96fcSRusty Russell 650a9ea3fc6SHerbert Xu static int virtnet_set_tx_csum(struct net_device *dev, u32 data) 651a9ea3fc6SHerbert Xu { 652a9ea3fc6SHerbert Xu struct virtnet_info *vi = netdev_priv(dev); 653a9ea3fc6SHerbert Xu struct virtio_device *vdev = vi->vdev; 654a9ea3fc6SHerbert Xu 655a9ea3fc6SHerbert Xu if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) 656a9ea3fc6SHerbert Xu return -ENOSYS; 657a9ea3fc6SHerbert Xu 658a9ea3fc6SHerbert Xu return ethtool_op_set_tx_hw_csum(dev, data); 659a9ea3fc6SHerbert Xu } 660a9ea3fc6SHerbert Xu 661*2af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 662*2af7698eSAlex Williamson { 663*2af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 664*2af7698eSAlex Williamson struct scatterlist sg; 665*2af7698eSAlex Williamson u8 promisc, allmulti; 666*2af7698eSAlex Williamson 667*2af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 668*2af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 669*2af7698eSAlex Williamson return; 670*2af7698eSAlex Williamson 671*2af7698eSAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0 || dev->uc_count > 0); 672*2af7698eSAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0 || dev->mc_count > 0); 673*2af7698eSAlex Williamson 674*2af7698eSAlex Williamson sg_set_buf(&sg, &promisc, sizeof(promisc)); 675*2af7698eSAlex Williamson 676*2af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 677*2af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 678*2af7698eSAlex Williamson &sg, 1, 0)) 679*2af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 680*2af7698eSAlex Williamson promisc ? "en" : "dis"); 681*2af7698eSAlex Williamson 682*2af7698eSAlex Williamson sg_set_buf(&sg, &allmulti, sizeof(allmulti)); 683*2af7698eSAlex Williamson 684*2af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 685*2af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 686*2af7698eSAlex Williamson &sg, 1, 0)) 687*2af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 688*2af7698eSAlex Williamson allmulti ? "en" : "dis"); 689*2af7698eSAlex Williamson } 690*2af7698eSAlex Williamson 691a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = { 692a9ea3fc6SHerbert Xu .set_tx_csum = virtnet_set_tx_csum, 693a9ea3fc6SHerbert Xu .set_sg = ethtool_op_set_sg, 6940276b497SMark McLoughlin .set_tso = ethtool_op_set_tso, 6959f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 696a9ea3fc6SHerbert Xu }; 697a9ea3fc6SHerbert Xu 69839da5814SMark McLoughlin #define MIN_MTU 68 69939da5814SMark McLoughlin #define MAX_MTU 65535 70039da5814SMark McLoughlin 70139da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 70239da5814SMark McLoughlin { 70339da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 70439da5814SMark McLoughlin return -EINVAL; 70539da5814SMark McLoughlin dev->mtu = new_mtu; 70639da5814SMark McLoughlin return 0; 70739da5814SMark McLoughlin } 70839da5814SMark McLoughlin 70976288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 71076288b4eSStephen Hemminger .ndo_open = virtnet_open, 71176288b4eSStephen Hemminger .ndo_stop = virtnet_close, 71276288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 71376288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 71476288b4eSStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 715*2af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 71676288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 71776288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 71876288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 71976288b4eSStephen Hemminger #endif 72076288b4eSStephen Hemminger }; 72176288b4eSStephen Hemminger 7229f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi) 7239f4d26d0SMark McLoughlin { 7249f4d26d0SMark McLoughlin u16 v; 7259f4d26d0SMark McLoughlin 7269f4d26d0SMark McLoughlin if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) 7279f4d26d0SMark McLoughlin return; 7289f4d26d0SMark McLoughlin 7299f4d26d0SMark McLoughlin vi->vdev->config->get(vi->vdev, 7309f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 7319f4d26d0SMark McLoughlin &v, sizeof(v)); 7329f4d26d0SMark McLoughlin 7339f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 7349f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 7359f4d26d0SMark McLoughlin 7369f4d26d0SMark McLoughlin if (vi->status == v) 7379f4d26d0SMark McLoughlin return; 7389f4d26d0SMark McLoughlin 7399f4d26d0SMark McLoughlin vi->status = v; 7409f4d26d0SMark McLoughlin 7419f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 7429f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 7439f4d26d0SMark McLoughlin netif_wake_queue(vi->dev); 7449f4d26d0SMark McLoughlin } else { 7459f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 7469f4d26d0SMark McLoughlin netif_stop_queue(vi->dev); 7479f4d26d0SMark McLoughlin } 7489f4d26d0SMark McLoughlin } 7499f4d26d0SMark McLoughlin 7509f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 7519f4d26d0SMark McLoughlin { 7529f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 7539f4d26d0SMark McLoughlin 7549f4d26d0SMark McLoughlin virtnet_update_status(vi); 7559f4d26d0SMark McLoughlin } 7569f4d26d0SMark McLoughlin 757296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 758296f96fcSRusty Russell { 759296f96fcSRusty Russell int err; 760296f96fcSRusty Russell struct net_device *dev; 761296f96fcSRusty Russell struct virtnet_info *vi; 762296f96fcSRusty Russell 763296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 764296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 765296f96fcSRusty Russell if (!dev) 766296f96fcSRusty Russell return -ENOMEM; 767296f96fcSRusty Russell 768296f96fcSRusty Russell /* Set up network device as normal. */ 76976288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 770296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 771a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 772296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 773296f96fcSRusty Russell 774296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 775c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 776296f96fcSRusty Russell /* This opens up the world of extra features. */ 777296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 778c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 77934a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 78034a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 78134a48579SRusty Russell } 7825539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 783c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 7845539ae96SRusty Russell dev->features |= NETIF_F_TSO; 785c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 7865539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 787c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 7885539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 789c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 7905539ae96SRusty Russell dev->features |= NETIF_F_UFO; 791296f96fcSRusty Russell } 792296f96fcSRusty Russell 793296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 794c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 795a586d4f6SRusty Russell vdev->config->get(vdev, 796a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 797a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 798296f96fcSRusty Russell } else 799296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 800296f96fcSRusty Russell 801296f96fcSRusty Russell /* Set up our device-specific information */ 802296f96fcSRusty Russell vi = netdev_priv(dev); 8036c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 804296f96fcSRusty Russell vi->dev = dev; 805296f96fcSRusty Russell vi->vdev = vdev; 806d9d5dcc8SChristian Borntraeger vdev->priv = vi; 807fb6813f4SRusty Russell vi->pages = NULL; 808296f96fcSRusty Russell 809363f1514SRusty Russell /* If they give us a callback when all buffers are done, we don't need 810363f1514SRusty Russell * the timer. */ 811363f1514SRusty Russell vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); 812363f1514SRusty Russell 81397402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 81497402b96SHerbert Xu if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) 81597402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) 81697402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 81797402b96SHerbert Xu vi->big_packets = true; 81897402b96SHerbert Xu 8193f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 8203f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 8213f2c31d9SMark McLoughlin 822296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 823a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 824296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 825296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 826296f96fcSRusty Russell goto free; 827296f96fcSRusty Russell } 828296f96fcSRusty Russell 829a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 830296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 831296f96fcSRusty Russell err = PTR_ERR(vi->svq); 832296f96fcSRusty Russell goto free_recv; 833296f96fcSRusty Russell } 834296f96fcSRusty Russell 8352a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 8362a41f71dSAlex Williamson vi->cvq = vdev->config->find_vq(vdev, 2, NULL); 8372a41f71dSAlex Williamson if (IS_ERR(vi->cvq)) { 8382a41f71dSAlex Williamson err = PTR_ERR(vi->svq); 8392a41f71dSAlex Williamson goto free_send; 8402a41f71dSAlex Williamson } 8412a41f71dSAlex Williamson } 8422a41f71dSAlex Williamson 843296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 844296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 845296f96fcSRusty Russell skb_queue_head_init(&vi->send); 846296f96fcSRusty Russell 84711a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 84811a3a154SRusty Russell 849363f1514SRusty Russell if (!vi->free_in_tasklet) 85014c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 85114c998f0SMark McLoughlin 852296f96fcSRusty Russell err = register_netdev(dev); 853296f96fcSRusty Russell if (err) { 854296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 8552a41f71dSAlex Williamson goto free_ctrl; 856296f96fcSRusty Russell } 857b3369c1fSRusty Russell 858b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 859b3369c1fSRusty Russell try_fill_recv(vi); 860b3369c1fSRusty Russell 861b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 862b3369c1fSRusty Russell if (vi->num == 0) { 863b3369c1fSRusty Russell err = -ENOMEM; 864b3369c1fSRusty Russell goto unregister; 865b3369c1fSRusty Russell } 866b3369c1fSRusty Russell 8679f4d26d0SMark McLoughlin vi->status = VIRTIO_NET_S_LINK_UP; 8689f4d26d0SMark McLoughlin virtnet_update_status(vi); 8699f4d26d0SMark McLoughlin 870296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 871296f96fcSRusty Russell return 0; 872296f96fcSRusty Russell 873b3369c1fSRusty Russell unregister: 874b3369c1fSRusty Russell unregister_netdev(dev); 8752a41f71dSAlex Williamson free_ctrl: 8762a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 8772a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 878296f96fcSRusty Russell free_send: 879296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 880296f96fcSRusty Russell free_recv: 881296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 882296f96fcSRusty Russell free: 883296f96fcSRusty Russell free_netdev(dev); 884296f96fcSRusty Russell return err; 885296f96fcSRusty Russell } 886296f96fcSRusty Russell 887296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 888296f96fcSRusty Russell { 88974b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 890b3369c1fSRusty Russell struct sk_buff *skb; 891b3369c1fSRusty Russell 8926e5aa7efSRusty Russell /* Stop all the virtqueues. */ 8936e5aa7efSRusty Russell vdev->config->reset(vdev); 8946e5aa7efSRusty Russell 895363f1514SRusty Russell if (!vi->free_in_tasklet) 89614c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 89714c998f0SMark McLoughlin 898b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 899b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 900b3369c1fSRusty Russell kfree_skb(skb); 901b3369c1fSRusty Russell vi->num--; 902b3369c1fSRusty Russell } 903288369ccSWang Chen __skb_queue_purge(&vi->send); 904b3369c1fSRusty Russell 905b3369c1fSRusty Russell BUG_ON(vi->num != 0); 90674b2553fSRusty Russell 90774b2553fSRusty Russell vdev->config->del_vq(vi->svq); 90874b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 9092a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9102a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 91174b2553fSRusty Russell unregister_netdev(vi->dev); 912fb6813f4SRusty Russell 913fb6813f4SRusty Russell while (vi->pages) 914fb6813f4SRusty Russell __free_pages(get_a_page(vi, GFP_KERNEL), 0); 915fb6813f4SRusty Russell 91674b2553fSRusty Russell free_netdev(vi->dev); 917296f96fcSRusty Russell } 918296f96fcSRusty Russell 919296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 920296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 921296f96fcSRusty Russell { 0 }, 922296f96fcSRusty Russell }; 923296f96fcSRusty Russell 924c45a6816SRusty Russell static unsigned int features[] = { 9255e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 9265e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 927c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 92897402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 92997402b96SHerbert Xu VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ 9302a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 931*2af7698eSAlex Williamson VIRTIO_NET_F_CTRL_RX, 93297402b96SHerbert Xu VIRTIO_F_NOTIFY_ON_EMPTY, 933c45a6816SRusty Russell }; 934c45a6816SRusty Russell 935296f96fcSRusty Russell static struct virtio_driver virtio_net = { 936c45a6816SRusty Russell .feature_table = features, 937c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 938296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 939296f96fcSRusty Russell .driver.owner = THIS_MODULE, 940296f96fcSRusty Russell .id_table = id_table, 941296f96fcSRusty Russell .probe = virtnet_probe, 942296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 9439f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 944296f96fcSRusty Russell }; 945296f96fcSRusty Russell 946296f96fcSRusty Russell static int __init init(void) 947296f96fcSRusty Russell { 948296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 949296f96fcSRusty Russell } 950296f96fcSRusty Russell 951296f96fcSRusty Russell static void __exit fini(void) 952296f96fcSRusty Russell { 953296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 954296f96fcSRusty Russell } 955296f96fcSRusty Russell module_init(init); 956296f96fcSRusty Russell module_exit(fini); 957296f96fcSRusty Russell 958296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 959296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 960296f96fcSRusty Russell MODULE_LICENSE("GPL"); 961