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 40f565a7c2SAlex Williamson #define VIRTNET_SEND_COMMAND_SG_MAX 2 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 6612af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 6622af7698eSAlex Williamson { 6632af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 664f565a7c2SAlex Williamson struct scatterlist sg[2]; 6652af7698eSAlex Williamson u8 promisc, allmulti; 666f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 667f565a7c2SAlex Williamson struct dev_addr_list *addr; 668f565a7c2SAlex Williamson void *buf; 669f565a7c2SAlex Williamson int i; 6702af7698eSAlex Williamson 6712af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 6722af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 6732af7698eSAlex Williamson return; 6742af7698eSAlex Williamson 675f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 676f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 6772af7698eSAlex Williamson 678f565a7c2SAlex Williamson sg_set_buf(sg, &promisc, sizeof(promisc)); 6792af7698eSAlex Williamson 6802af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 6812af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 682f565a7c2SAlex Williamson sg, 1, 0)) 6832af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 6842af7698eSAlex Williamson promisc ? "en" : "dis"); 6852af7698eSAlex Williamson 686f565a7c2SAlex Williamson sg_set_buf(sg, &allmulti, sizeof(allmulti)); 6872af7698eSAlex Williamson 6882af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 6892af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 690f565a7c2SAlex Williamson sg, 1, 0)) 6912af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 6922af7698eSAlex Williamson allmulti ? "en" : "dis"); 693f565a7c2SAlex Williamson 694f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 695f565a7c2SAlex Williamson mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) + 696f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 697f565a7c2SAlex Williamson if (!buf) { 698f565a7c2SAlex Williamson dev_warn(&dev->dev, "No memory for MAC address buffer\n"); 699f565a7c2SAlex Williamson return; 700f565a7c2SAlex Williamson } 701f565a7c2SAlex Williamson 702f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 703f565a7c2SAlex Williamson mac_data->entries = dev->uc_count; 704f565a7c2SAlex Williamson addr = dev->uc_list; 705f565a7c2SAlex Williamson for (i = 0; i < dev->uc_count; i++, addr = addr->next) 706f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 707f565a7c2SAlex Williamson 708f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 709f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN)); 710f565a7c2SAlex Williamson 711f565a7c2SAlex Williamson /* multicast list and count fill the end */ 712f565a7c2SAlex Williamson mac_data = (void *)&mac_data->macs[dev->uc_count][0]; 713f565a7c2SAlex Williamson 714f565a7c2SAlex Williamson mac_data->entries = dev->mc_count; 715f565a7c2SAlex Williamson addr = dev->mc_list; 716f565a7c2SAlex Williamson for (i = 0; i < dev->mc_count; i++, addr = addr->next) 717f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 718f565a7c2SAlex Williamson 719f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 720f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); 721f565a7c2SAlex Williamson 722f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 723f565a7c2SAlex Williamson VIRTIO_NET_CTRL_MAC_TABLE_SET, 724f565a7c2SAlex Williamson sg, 2, 0)) 725f565a7c2SAlex Williamson dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); 726f565a7c2SAlex Williamson 727f565a7c2SAlex Williamson kfree(buf); 7282af7698eSAlex Williamson } 7292af7698eSAlex Williamson 730*0bde9569SAlex Williamson static void virnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) 731*0bde9569SAlex Williamson { 732*0bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 733*0bde9569SAlex Williamson struct scatterlist sg; 734*0bde9569SAlex Williamson 735*0bde9569SAlex Williamson sg_set_buf(&sg, &vid, sizeof(vid)); 736*0bde9569SAlex Williamson 737*0bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 738*0bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) 739*0bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 740*0bde9569SAlex Williamson } 741*0bde9569SAlex Williamson 742*0bde9569SAlex Williamson static void virnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) 743*0bde9569SAlex Williamson { 744*0bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 745*0bde9569SAlex Williamson struct scatterlist sg; 746*0bde9569SAlex Williamson 747*0bde9569SAlex Williamson sg_set_buf(&sg, &vid, sizeof(vid)); 748*0bde9569SAlex Williamson 749*0bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 750*0bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) 751*0bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 752*0bde9569SAlex Williamson } 753*0bde9569SAlex Williamson 754a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = { 755a9ea3fc6SHerbert Xu .set_tx_csum = virtnet_set_tx_csum, 756a9ea3fc6SHerbert Xu .set_sg = ethtool_op_set_sg, 7570276b497SMark McLoughlin .set_tso = ethtool_op_set_tso, 7589f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 759a9ea3fc6SHerbert Xu }; 760a9ea3fc6SHerbert Xu 76139da5814SMark McLoughlin #define MIN_MTU 68 76239da5814SMark McLoughlin #define MAX_MTU 65535 76339da5814SMark McLoughlin 76439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 76539da5814SMark McLoughlin { 76639da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 76739da5814SMark McLoughlin return -EINVAL; 76839da5814SMark McLoughlin dev->mtu = new_mtu; 76939da5814SMark McLoughlin return 0; 77039da5814SMark McLoughlin } 77139da5814SMark McLoughlin 77276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 77376288b4eSStephen Hemminger .ndo_open = virtnet_open, 77476288b4eSStephen Hemminger .ndo_stop = virtnet_close, 77576288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 77676288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 77776288b4eSStephen Hemminger .ndo_set_mac_address = eth_mac_addr, 7782af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 77976288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 780*0bde9569SAlex Williamson .ndo_vlan_rx_add_vid = virnet_vlan_rx_add_vid, 781*0bde9569SAlex Williamson .ndo_vlan_rx_kill_vid = virnet_vlan_rx_kill_vid, 78276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 78376288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 78476288b4eSStephen Hemminger #endif 78576288b4eSStephen Hemminger }; 78676288b4eSStephen Hemminger 7879f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi) 7889f4d26d0SMark McLoughlin { 7899f4d26d0SMark McLoughlin u16 v; 7909f4d26d0SMark McLoughlin 7919f4d26d0SMark McLoughlin if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) 7929f4d26d0SMark McLoughlin return; 7939f4d26d0SMark McLoughlin 7949f4d26d0SMark McLoughlin vi->vdev->config->get(vi->vdev, 7959f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 7969f4d26d0SMark McLoughlin &v, sizeof(v)); 7979f4d26d0SMark McLoughlin 7989f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 7999f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 8009f4d26d0SMark McLoughlin 8019f4d26d0SMark McLoughlin if (vi->status == v) 8029f4d26d0SMark McLoughlin return; 8039f4d26d0SMark McLoughlin 8049f4d26d0SMark McLoughlin vi->status = v; 8059f4d26d0SMark McLoughlin 8069f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 8079f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 8089f4d26d0SMark McLoughlin netif_wake_queue(vi->dev); 8099f4d26d0SMark McLoughlin } else { 8109f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 8119f4d26d0SMark McLoughlin netif_stop_queue(vi->dev); 8129f4d26d0SMark McLoughlin } 8139f4d26d0SMark McLoughlin } 8149f4d26d0SMark McLoughlin 8159f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 8169f4d26d0SMark McLoughlin { 8179f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 8189f4d26d0SMark McLoughlin 8199f4d26d0SMark McLoughlin virtnet_update_status(vi); 8209f4d26d0SMark McLoughlin } 8219f4d26d0SMark McLoughlin 822296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 823296f96fcSRusty Russell { 824296f96fcSRusty Russell int err; 825296f96fcSRusty Russell struct net_device *dev; 826296f96fcSRusty Russell struct virtnet_info *vi; 827296f96fcSRusty Russell 828296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 829296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 830296f96fcSRusty Russell if (!dev) 831296f96fcSRusty Russell return -ENOMEM; 832296f96fcSRusty Russell 833296f96fcSRusty Russell /* Set up network device as normal. */ 83476288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 835296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 836a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 837296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 838296f96fcSRusty Russell 839296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 840c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 841296f96fcSRusty Russell /* This opens up the world of extra features. */ 842296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 843c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 84434a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 84534a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 84634a48579SRusty Russell } 8475539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 848c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 8495539ae96SRusty Russell dev->features |= NETIF_F_TSO; 850c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 8515539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 852c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 8535539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 854c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 8555539ae96SRusty Russell dev->features |= NETIF_F_UFO; 856296f96fcSRusty Russell } 857296f96fcSRusty Russell 858296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 859c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 860a586d4f6SRusty Russell vdev->config->get(vdev, 861a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 862a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 863296f96fcSRusty Russell } else 864296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 865296f96fcSRusty Russell 866296f96fcSRusty Russell /* Set up our device-specific information */ 867296f96fcSRusty Russell vi = netdev_priv(dev); 8686c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 869296f96fcSRusty Russell vi->dev = dev; 870296f96fcSRusty Russell vi->vdev = vdev; 871d9d5dcc8SChristian Borntraeger vdev->priv = vi; 872fb6813f4SRusty Russell vi->pages = NULL; 873296f96fcSRusty Russell 874363f1514SRusty Russell /* If they give us a callback when all buffers are done, we don't need 875363f1514SRusty Russell * the timer. */ 876363f1514SRusty Russell vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); 877363f1514SRusty Russell 87897402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 87997402b96SHerbert Xu if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) 88097402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) 88197402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 88297402b96SHerbert Xu vi->big_packets = true; 88397402b96SHerbert Xu 8843f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 8853f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 8863f2c31d9SMark McLoughlin 887296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 888a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 889296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 890296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 891296f96fcSRusty Russell goto free; 892296f96fcSRusty Russell } 893296f96fcSRusty Russell 894a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 895296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 896296f96fcSRusty Russell err = PTR_ERR(vi->svq); 897296f96fcSRusty Russell goto free_recv; 898296f96fcSRusty Russell } 899296f96fcSRusty Russell 9002a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 9012a41f71dSAlex Williamson vi->cvq = vdev->config->find_vq(vdev, 2, NULL); 9022a41f71dSAlex Williamson if (IS_ERR(vi->cvq)) { 9032a41f71dSAlex Williamson err = PTR_ERR(vi->svq); 9042a41f71dSAlex Williamson goto free_send; 9052a41f71dSAlex Williamson } 906*0bde9569SAlex Williamson 907*0bde9569SAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 908*0bde9569SAlex Williamson dev->features |= NETIF_F_HW_VLAN_FILTER; 9092a41f71dSAlex Williamson } 9102a41f71dSAlex Williamson 911296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 912296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 913296f96fcSRusty Russell skb_queue_head_init(&vi->send); 914296f96fcSRusty Russell 91511a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 91611a3a154SRusty Russell 917363f1514SRusty Russell if (!vi->free_in_tasklet) 91814c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 91914c998f0SMark McLoughlin 920296f96fcSRusty Russell err = register_netdev(dev); 921296f96fcSRusty Russell if (err) { 922296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 9232a41f71dSAlex Williamson goto free_ctrl; 924296f96fcSRusty Russell } 925b3369c1fSRusty Russell 926b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 927b3369c1fSRusty Russell try_fill_recv(vi); 928b3369c1fSRusty Russell 929b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 930b3369c1fSRusty Russell if (vi->num == 0) { 931b3369c1fSRusty Russell err = -ENOMEM; 932b3369c1fSRusty Russell goto unregister; 933b3369c1fSRusty Russell } 934b3369c1fSRusty Russell 9359f4d26d0SMark McLoughlin vi->status = VIRTIO_NET_S_LINK_UP; 9369f4d26d0SMark McLoughlin virtnet_update_status(vi); 9379f4d26d0SMark McLoughlin 938296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 939296f96fcSRusty Russell return 0; 940296f96fcSRusty Russell 941b3369c1fSRusty Russell unregister: 942b3369c1fSRusty Russell unregister_netdev(dev); 9432a41f71dSAlex Williamson free_ctrl: 9442a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9452a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 946296f96fcSRusty Russell free_send: 947296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 948296f96fcSRusty Russell free_recv: 949296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 950296f96fcSRusty Russell free: 951296f96fcSRusty Russell free_netdev(dev); 952296f96fcSRusty Russell return err; 953296f96fcSRusty Russell } 954296f96fcSRusty Russell 955296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 956296f96fcSRusty Russell { 95774b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 958b3369c1fSRusty Russell struct sk_buff *skb; 959b3369c1fSRusty Russell 9606e5aa7efSRusty Russell /* Stop all the virtqueues. */ 9616e5aa7efSRusty Russell vdev->config->reset(vdev); 9626e5aa7efSRusty Russell 963363f1514SRusty Russell if (!vi->free_in_tasklet) 96414c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 96514c998f0SMark McLoughlin 966b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 967b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 968b3369c1fSRusty Russell kfree_skb(skb); 969b3369c1fSRusty Russell vi->num--; 970b3369c1fSRusty Russell } 971288369ccSWang Chen __skb_queue_purge(&vi->send); 972b3369c1fSRusty Russell 973b3369c1fSRusty Russell BUG_ON(vi->num != 0); 97474b2553fSRusty Russell 97574b2553fSRusty Russell vdev->config->del_vq(vi->svq); 97674b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 9772a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9782a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 97974b2553fSRusty Russell unregister_netdev(vi->dev); 980fb6813f4SRusty Russell 981fb6813f4SRusty Russell while (vi->pages) 982fb6813f4SRusty Russell __free_pages(get_a_page(vi, GFP_KERNEL), 0); 983fb6813f4SRusty Russell 98474b2553fSRusty Russell free_netdev(vi->dev); 985296f96fcSRusty Russell } 986296f96fcSRusty Russell 987296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 988296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 989296f96fcSRusty Russell { 0 }, 990296f96fcSRusty Russell }; 991296f96fcSRusty Russell 992c45a6816SRusty Russell static unsigned int features[] = { 9935e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 9945e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 995c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 99697402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 99797402b96SHerbert Xu VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ 9982a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 999*0bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 100097402b96SHerbert Xu VIRTIO_F_NOTIFY_ON_EMPTY, 1001c45a6816SRusty Russell }; 1002c45a6816SRusty Russell 1003296f96fcSRusty Russell static struct virtio_driver virtio_net = { 1004c45a6816SRusty Russell .feature_table = features, 1005c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1006296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1007296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1008296f96fcSRusty Russell .id_table = id_table, 1009296f96fcSRusty Russell .probe = virtnet_probe, 1010296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 10119f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 1012296f96fcSRusty Russell }; 1013296f96fcSRusty Russell 1014296f96fcSRusty Russell static int __init init(void) 1015296f96fcSRusty Russell { 1016296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 1017296f96fcSRusty Russell } 1018296f96fcSRusty Russell 1019296f96fcSRusty Russell static void __exit fini(void) 1020296f96fcSRusty Russell { 1021296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 1022296f96fcSRusty Russell } 1023296f96fcSRusty Russell module_init(init); 1024296f96fcSRusty Russell module_exit(fini); 1025296f96fcSRusty Russell 1026296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1027296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1028296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1029