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 2868981f010SHerbert Xu skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN); 287296f96fcSRusty Russell if (unlikely(!skb)) 288296f96fcSRusty Russell break; 289296f96fcSRusty Russell 2908981f010SHerbert Xu skb_reserve(skb, NET_IP_ALIGN); 291296f96fcSRusty Russell skb_put(skb, MAX_PACKET_LEN); 2923f2c31d9SMark McLoughlin 2933f2c31d9SMark McLoughlin hdr = skb_vnet_hdr(skb); 2948527bec5SIra W. Snyder sg_set_buf(sg, hdr, sizeof(*hdr)); 29597402b96SHerbert Xu 29697402b96SHerbert Xu if (vi->big_packets) { 29797402b96SHerbert Xu for (i = 0; i < MAX_SKB_FRAGS; i++) { 29897402b96SHerbert Xu skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 299fb6813f4SRusty Russell f->page = get_a_page(vi, GFP_ATOMIC); 30097402b96SHerbert Xu if (!f->page) 30197402b96SHerbert Xu break; 30297402b96SHerbert Xu 30397402b96SHerbert Xu f->page_offset = 0; 30497402b96SHerbert Xu f->size = PAGE_SIZE; 30597402b96SHerbert Xu 30697402b96SHerbert Xu skb->data_len += PAGE_SIZE; 30797402b96SHerbert Xu skb->len += PAGE_SIZE; 30897402b96SHerbert Xu 30997402b96SHerbert Xu skb_shinfo(skb)->nr_frags++; 31097402b96SHerbert Xu } 31197402b96SHerbert Xu } 31297402b96SHerbert Xu 313296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 314296f96fcSRusty Russell skb_queue_head(&vi->recv, skb); 315296f96fcSRusty Russell 316296f96fcSRusty Russell err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); 317296f96fcSRusty Russell if (err) { 318296f96fcSRusty Russell skb_unlink(skb, &vi->recv); 3190a888fd1SMark McLoughlin trim_pages(vi, skb); 320296f96fcSRusty Russell kfree_skb(skb); 321296f96fcSRusty Russell break; 322296f96fcSRusty Russell } 323296f96fcSRusty Russell vi->num++; 324296f96fcSRusty Russell } 325296f96fcSRusty Russell if (unlikely(vi->num > vi->max)) 326296f96fcSRusty Russell vi->max = vi->num; 327296f96fcSRusty Russell vi->rvq->vq_ops->kick(vi->rvq); 328296f96fcSRusty Russell } 329296f96fcSRusty Russell 3303f2c31d9SMark McLoughlin static void try_fill_recv(struct virtnet_info *vi) 3313f2c31d9SMark McLoughlin { 3323f2c31d9SMark McLoughlin struct sk_buff *skb; 3333f2c31d9SMark McLoughlin struct scatterlist sg[1]; 3343f2c31d9SMark McLoughlin int err; 3353f2c31d9SMark McLoughlin 3363f2c31d9SMark McLoughlin if (!vi->mergeable_rx_bufs) { 3373f2c31d9SMark McLoughlin try_fill_recv_maxbufs(vi); 3383f2c31d9SMark McLoughlin return; 3393f2c31d9SMark McLoughlin } 3403f2c31d9SMark McLoughlin 3413f2c31d9SMark McLoughlin for (;;) { 3423f2c31d9SMark McLoughlin skb_frag_t *f; 3433f2c31d9SMark McLoughlin 3443f2c31d9SMark McLoughlin skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); 3453f2c31d9SMark McLoughlin if (unlikely(!skb)) 3463f2c31d9SMark McLoughlin break; 3473f2c31d9SMark McLoughlin 3483f2c31d9SMark McLoughlin skb_reserve(skb, NET_IP_ALIGN); 3493f2c31d9SMark McLoughlin 3503f2c31d9SMark McLoughlin f = &skb_shinfo(skb)->frags[0]; 3513f2c31d9SMark McLoughlin f->page = get_a_page(vi, GFP_ATOMIC); 3523f2c31d9SMark McLoughlin if (!f->page) { 3533f2c31d9SMark McLoughlin kfree_skb(skb); 3543f2c31d9SMark McLoughlin break; 3553f2c31d9SMark McLoughlin } 3563f2c31d9SMark McLoughlin 3573f2c31d9SMark McLoughlin f->page_offset = 0; 3583f2c31d9SMark McLoughlin f->size = PAGE_SIZE; 3593f2c31d9SMark McLoughlin 3603f2c31d9SMark McLoughlin skb_shinfo(skb)->nr_frags++; 3613f2c31d9SMark McLoughlin 3623f2c31d9SMark McLoughlin sg_init_one(sg, page_address(f->page), PAGE_SIZE); 3633f2c31d9SMark McLoughlin skb_queue_head(&vi->recv, skb); 3643f2c31d9SMark McLoughlin 3653f2c31d9SMark McLoughlin err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); 3663f2c31d9SMark McLoughlin if (err) { 3673f2c31d9SMark McLoughlin skb_unlink(skb, &vi->recv); 3683f2c31d9SMark McLoughlin kfree_skb(skb); 3693f2c31d9SMark McLoughlin break; 3703f2c31d9SMark McLoughlin } 3713f2c31d9SMark McLoughlin vi->num++; 3723f2c31d9SMark McLoughlin } 3733f2c31d9SMark McLoughlin if (unlikely(vi->num > vi->max)) 3743f2c31d9SMark McLoughlin vi->max = vi->num; 3753f2c31d9SMark McLoughlin vi->rvq->vq_ops->kick(vi->rvq); 3763f2c31d9SMark McLoughlin } 3773f2c31d9SMark McLoughlin 37818445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq) 379296f96fcSRusty Russell { 380296f96fcSRusty Russell struct virtnet_info *vi = rvq->vdev->priv; 38118445c4dSRusty Russell /* Schedule NAPI, Suppress further interrupts if successful. */ 382288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 38318445c4dSRusty Russell rvq->vq_ops->disable_cb(rvq); 384288379f0SBen Hutchings __napi_schedule(&vi->napi); 38518445c4dSRusty Russell } 386296f96fcSRusty Russell } 387296f96fcSRusty Russell 388296f96fcSRusty Russell static int virtnet_poll(struct napi_struct *napi, int budget) 389296f96fcSRusty Russell { 390296f96fcSRusty Russell struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); 391296f96fcSRusty Russell struct sk_buff *skb = NULL; 392296f96fcSRusty Russell unsigned int len, received = 0; 393296f96fcSRusty Russell 394296f96fcSRusty Russell again: 395296f96fcSRusty Russell while (received < budget && 396296f96fcSRusty Russell (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { 397296f96fcSRusty Russell __skb_unlink(skb, &vi->recv); 398296f96fcSRusty Russell receive_skb(vi->dev, skb, len); 399296f96fcSRusty Russell vi->num--; 400296f96fcSRusty Russell received++; 401296f96fcSRusty Russell } 402296f96fcSRusty Russell 403296f96fcSRusty Russell /* FIXME: If we oom and completely run out of inbufs, we need 404296f96fcSRusty Russell * to start a timer trying to fill more. */ 405296f96fcSRusty Russell if (vi->num < vi->max / 2) 406296f96fcSRusty Russell try_fill_recv(vi); 407296f96fcSRusty Russell 4088329d98eSRusty Russell /* Out of packets? */ 4098329d98eSRusty Russell if (received < budget) { 410288379f0SBen Hutchings napi_complete(napi); 41118445c4dSRusty Russell if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) 4124265f161SChristian Borntraeger && napi_schedule_prep(napi)) { 4134265f161SChristian Borntraeger vi->rvq->vq_ops->disable_cb(vi->rvq); 414288379f0SBen Hutchings __napi_schedule(napi); 415296f96fcSRusty Russell goto again; 416296f96fcSRusty Russell } 4174265f161SChristian Borntraeger } 418296f96fcSRusty Russell 419296f96fcSRusty Russell return received; 420296f96fcSRusty Russell } 421296f96fcSRusty Russell 422296f96fcSRusty Russell static void free_old_xmit_skbs(struct virtnet_info *vi) 423296f96fcSRusty Russell { 424296f96fcSRusty Russell struct sk_buff *skb; 425296f96fcSRusty Russell unsigned int len; 426296f96fcSRusty Russell 427296f96fcSRusty Russell while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { 428296f96fcSRusty Russell pr_debug("Sent skb %p\n", skb); 429296f96fcSRusty Russell __skb_unlink(skb, &vi->send); 430655aa31fSRusty Russell vi->dev->stats.tx_bytes += skb->len; 431296f96fcSRusty Russell vi->dev->stats.tx_packets++; 432296f96fcSRusty Russell kfree_skb(skb); 433296f96fcSRusty Russell } 434296f96fcSRusty Russell } 435296f96fcSRusty Russell 436363f1514SRusty Russell /* If the virtio transport doesn't always notify us when all in-flight packets 437363f1514SRusty Russell * are consumed, we fall back to using this function on a timer to free them. */ 43814c998f0SMark McLoughlin static void xmit_free(unsigned long data) 43914c998f0SMark McLoughlin { 44014c998f0SMark McLoughlin struct virtnet_info *vi = (void *)data; 44114c998f0SMark McLoughlin 44214c998f0SMark McLoughlin netif_tx_lock(vi->dev); 44314c998f0SMark McLoughlin 44414c998f0SMark McLoughlin free_old_xmit_skbs(vi); 44514c998f0SMark McLoughlin 44614c998f0SMark McLoughlin if (!skb_queue_empty(&vi->send)) 44714c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 44814c998f0SMark McLoughlin 44914c998f0SMark McLoughlin netif_tx_unlock(vi->dev); 45014c998f0SMark McLoughlin } 45114c998f0SMark McLoughlin 45299ffc696SRusty Russell static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) 453296f96fcSRusty Russell { 45414c998f0SMark McLoughlin int num, err; 45505271685SRusty Russell struct scatterlist sg[2+MAX_SKB_FRAGS]; 4563f2c31d9SMark McLoughlin struct virtio_net_hdr_mrg_rxbuf *mhdr = skb_vnet_hdr(skb); 4573f2c31d9SMark McLoughlin struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); 458296f96fcSRusty Russell const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; 459296f96fcSRusty Russell 46005271685SRusty Russell sg_init_table(sg, 2+MAX_SKB_FRAGS); 4614d125de3SRusty Russell 462e174961cSJohannes Berg pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); 463296f96fcSRusty Russell 464296f96fcSRusty Russell if (skb->ip_summed == CHECKSUM_PARTIAL) { 465296f96fcSRusty Russell hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; 466296f96fcSRusty Russell hdr->csum_start = skb->csum_start - skb_headroom(skb); 467296f96fcSRusty Russell hdr->csum_offset = skb->csum_offset; 468296f96fcSRusty Russell } else { 469296f96fcSRusty Russell hdr->flags = 0; 470296f96fcSRusty Russell hdr->csum_offset = hdr->csum_start = 0; 471296f96fcSRusty Russell } 472296f96fcSRusty Russell 473296f96fcSRusty Russell if (skb_is_gso(skb)) { 474b82f08eaSHerbert Xu hdr->hdr_len = skb_headlen(skb); 475296f96fcSRusty Russell hdr->gso_size = skb_shinfo(skb)->gso_size; 47634a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) 477296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; 478296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) 479296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; 480296f96fcSRusty Russell else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) 481296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; 482296f96fcSRusty Russell else 483296f96fcSRusty Russell BUG(); 48434a48579SRusty Russell if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) 48534a48579SRusty Russell hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; 486296f96fcSRusty Russell } else { 487296f96fcSRusty Russell hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; 48850c8ea80SRusty Russell hdr->gso_size = hdr->hdr_len = 0; 489296f96fcSRusty Russell } 490296f96fcSRusty Russell 4913f2c31d9SMark McLoughlin mhdr->num_buffers = 0; 4923f2c31d9SMark McLoughlin 4933f2c31d9SMark McLoughlin /* Encode metadata header at front. */ 4943f2c31d9SMark McLoughlin if (vi->mergeable_rx_bufs) 4958527bec5SIra W. Snyder sg_set_buf(sg, mhdr, sizeof(*mhdr)); 4963f2c31d9SMark McLoughlin else 4978527bec5SIra W. Snyder sg_set_buf(sg, hdr, sizeof(*hdr)); 4983f2c31d9SMark McLoughlin 499296f96fcSRusty Russell num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; 50099ffc696SRusty Russell 50114c998f0SMark McLoughlin err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); 502363f1514SRusty Russell if (!err && !vi->free_in_tasklet) 50314c998f0SMark McLoughlin mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); 50414c998f0SMark McLoughlin 50514c998f0SMark McLoughlin return err; 50699ffc696SRusty Russell } 50799ffc696SRusty Russell 50811a3a154SRusty Russell static void xmit_tasklet(unsigned long data) 50911a3a154SRusty Russell { 51011a3a154SRusty Russell struct virtnet_info *vi = (void *)data; 51111a3a154SRusty Russell 51211a3a154SRusty Russell netif_tx_lock_bh(vi->dev); 51311a3a154SRusty Russell if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { 51411a3a154SRusty Russell vi->svq->vq_ops->kick(vi->svq); 51511a3a154SRusty Russell vi->last_xmit_skb = NULL; 51611a3a154SRusty Russell } 517363f1514SRusty Russell if (vi->free_in_tasklet) 518363f1514SRusty Russell free_old_xmit_skbs(vi); 51911a3a154SRusty Russell netif_tx_unlock_bh(vi->dev); 52011a3a154SRusty Russell } 52111a3a154SRusty Russell 52299ffc696SRusty Russell static int start_xmit(struct sk_buff *skb, struct net_device *dev) 52399ffc696SRusty Russell { 52499ffc696SRusty Russell struct virtnet_info *vi = netdev_priv(dev); 5252cb9c6baSRusty Russell 5262cb9c6baSRusty Russell again: 5272cb9c6baSRusty Russell /* Free up any pending old buffers before queueing new ones. */ 5282cb9c6baSRusty Russell free_old_xmit_skbs(vi); 52999ffc696SRusty Russell 53099ffc696SRusty Russell /* If we has a buffer left over from last time, send it now. */ 5319953ca6cSMark McLoughlin if (unlikely(vi->last_xmit_skb) && 5329953ca6cSMark McLoughlin xmit_skb(vi, vi->last_xmit_skb) != 0) 53399ffc696SRusty Russell goto stop_queue; 5349953ca6cSMark McLoughlin 53599ffc696SRusty Russell vi->last_xmit_skb = NULL; 53699ffc696SRusty Russell 53799ffc696SRusty Russell /* Put new one in send queue and do transmit */ 5387eb2e251SRusty Russell if (likely(skb)) { 53999ffc696SRusty Russell __skb_queue_head(&vi->send, skb); 54099ffc696SRusty Russell if (xmit_skb(vi, skb) != 0) { 54199ffc696SRusty Russell vi->last_xmit_skb = skb; 5427eb2e251SRusty Russell skb = NULL; 54399ffc696SRusty Russell goto stop_queue; 54499ffc696SRusty Russell } 5457eb2e251SRusty Russell } 54699ffc696SRusty Russell done: 54799ffc696SRusty Russell vi->svq->vq_ops->kick(vi->svq); 54899ffc696SRusty Russell return NETDEV_TX_OK; 54999ffc696SRusty Russell 55099ffc696SRusty Russell stop_queue: 551296f96fcSRusty Russell pr_debug("%s: virtio not prepared to send\n", dev->name); 552296f96fcSRusty Russell netif_stop_queue(dev); 5532cb9c6baSRusty Russell 5544265f161SChristian Borntraeger /* Activate callback for using skbs: if this returns false it 5552cb9c6baSRusty Russell * means some were used in the meantime. */ 5562cb9c6baSRusty Russell if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { 5574265f161SChristian Borntraeger vi->svq->vq_ops->disable_cb(vi->svq); 5582cb9c6baSRusty Russell netif_start_queue(dev); 5592cb9c6baSRusty Russell goto again; 5602cb9c6baSRusty Russell } 5619953ca6cSMark McLoughlin if (skb) { 5629953ca6cSMark McLoughlin /* Drop this skb: we only queue one. */ 5639953ca6cSMark McLoughlin vi->dev->stats.tx_dropped++; 5649953ca6cSMark McLoughlin kfree_skb(skb); 5659953ca6cSMark McLoughlin } 56699ffc696SRusty Russell goto done; 567296f96fcSRusty Russell } 568296f96fcSRusty Russell 5699c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 5709c46f6d4SAlex Williamson { 5719c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 5729c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 5739c46f6d4SAlex Williamson int ret; 5749c46f6d4SAlex Williamson 5759c46f6d4SAlex Williamson ret = eth_mac_addr(dev, p); 5769c46f6d4SAlex Williamson if (ret) 5779c46f6d4SAlex Williamson return ret; 5789c46f6d4SAlex Williamson 57962994b2dSAlex Williamson if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 5809c46f6d4SAlex Williamson vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), 5819c46f6d4SAlex Williamson dev->dev_addr, dev->addr_len); 5829c46f6d4SAlex Williamson 5839c46f6d4SAlex Williamson return 0; 5849c46f6d4SAlex Williamson } 5859c46f6d4SAlex Williamson 586da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 587da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 588da74e89dSAmit Shah { 589da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 590da74e89dSAmit Shah 591da74e89dSAmit Shah napi_schedule(&vi->napi); 592da74e89dSAmit Shah } 593da74e89dSAmit Shah #endif 594da74e89dSAmit Shah 595296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 596296f96fcSRusty Russell { 597296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 598296f96fcSRusty Russell 599296f96fcSRusty Russell napi_enable(&vi->napi); 600a48bd8f6SRusty Russell 601a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 602a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 603370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 604370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 605288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 606a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 607288379f0SBen Hutchings __napi_schedule(&vi->napi); 608370076d9SChristian Borntraeger } 609296f96fcSRusty Russell return 0; 610296f96fcSRusty Russell } 611296f96fcSRusty Russell 6122a41f71dSAlex Williamson /* 6132a41f71dSAlex Williamson * Send command via the control virtqueue and check status. Commands 6142a41f71dSAlex Williamson * supported by the hypervisor, as indicated by feature bits, should 6152a41f71dSAlex Williamson * never fail unless improperly formated. 6162a41f71dSAlex Williamson */ 6172a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 6182a41f71dSAlex Williamson struct scatterlist *data, int out, int in) 6192a41f71dSAlex Williamson { 62023e258e1SAlex Williamson struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; 6212a41f71dSAlex Williamson struct virtio_net_ctrl_hdr ctrl; 6222a41f71dSAlex Williamson virtio_net_ctrl_ack status = ~0; 6232a41f71dSAlex Williamson unsigned int tmp; 62423e258e1SAlex Williamson int i; 6252a41f71dSAlex Williamson 6260ee904c3SAlexander Beregalov /* Caller should know better */ 6270ee904c3SAlexander Beregalov BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || 6280ee904c3SAlexander Beregalov (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); 6292a41f71dSAlex Williamson 6302a41f71dSAlex Williamson out++; /* Add header */ 6312a41f71dSAlex Williamson in++; /* Add return status */ 6322a41f71dSAlex Williamson 6332a41f71dSAlex Williamson ctrl.class = class; 6342a41f71dSAlex Williamson ctrl.cmd = cmd; 6352a41f71dSAlex Williamson 6362a41f71dSAlex Williamson sg_init_table(sg, out + in); 6372a41f71dSAlex Williamson 6382a41f71dSAlex Williamson sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); 63923e258e1SAlex Williamson for_each_sg(data, s, out + in - 2, i) 64023e258e1SAlex Williamson sg_set_buf(&sg[i + 1], sg_virt(s), s->length); 6412a41f71dSAlex Williamson sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); 6422a41f71dSAlex Williamson 6430ee904c3SAlexander Beregalov BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi)); 6442a41f71dSAlex Williamson 6452a41f71dSAlex Williamson vi->cvq->vq_ops->kick(vi->cvq); 6462a41f71dSAlex Williamson 6472a41f71dSAlex Williamson /* 6482a41f71dSAlex Williamson * Spin for a response, the kick causes an ioport write, trapping 6492a41f71dSAlex Williamson * into the hypervisor, so the request should be handled immediately. 6502a41f71dSAlex Williamson */ 6512a41f71dSAlex Williamson while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) 6522a41f71dSAlex Williamson cpu_relax(); 6532a41f71dSAlex Williamson 6542a41f71dSAlex Williamson return status == VIRTIO_NET_OK; 6552a41f71dSAlex Williamson } 6562a41f71dSAlex Williamson 657296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 658296f96fcSRusty Russell { 659296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 660296f96fcSRusty Russell 661296f96fcSRusty Russell napi_disable(&vi->napi); 662296f96fcSRusty Russell 663296f96fcSRusty Russell return 0; 664296f96fcSRusty Russell } 665296f96fcSRusty Russell 666a9ea3fc6SHerbert Xu static int virtnet_set_tx_csum(struct net_device *dev, u32 data) 667a9ea3fc6SHerbert Xu { 668a9ea3fc6SHerbert Xu struct virtnet_info *vi = netdev_priv(dev); 669a9ea3fc6SHerbert Xu struct virtio_device *vdev = vi->vdev; 670a9ea3fc6SHerbert Xu 671a9ea3fc6SHerbert Xu if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) 672a9ea3fc6SHerbert Xu return -ENOSYS; 673a9ea3fc6SHerbert Xu 674a9ea3fc6SHerbert Xu return ethtool_op_set_tx_hw_csum(dev, data); 675a9ea3fc6SHerbert Xu } 676a9ea3fc6SHerbert Xu 6772af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 6782af7698eSAlex Williamson { 6792af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 680f565a7c2SAlex Williamson struct scatterlist sg[2]; 6812af7698eSAlex Williamson u8 promisc, allmulti; 682f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 683f565a7c2SAlex Williamson struct dev_addr_list *addr; 684ccffad25SJiri Pirko struct netdev_hw_addr *ha; 685f565a7c2SAlex Williamson void *buf; 686f565a7c2SAlex Williamson int i; 6872af7698eSAlex Williamson 6882af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 6892af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 6902af7698eSAlex Williamson return; 6912af7698eSAlex Williamson 692f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 693f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 6942af7698eSAlex Williamson 69523e258e1SAlex Williamson sg_init_one(sg, &promisc, sizeof(promisc)); 6962af7698eSAlex Williamson 6972af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 6982af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 699f565a7c2SAlex Williamson sg, 1, 0)) 7002af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 7012af7698eSAlex Williamson promisc ? "en" : "dis"); 7022af7698eSAlex Williamson 70323e258e1SAlex Williamson sg_init_one(sg, &allmulti, sizeof(allmulti)); 7042af7698eSAlex Williamson 7052af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 7062af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 707f565a7c2SAlex Williamson sg, 1, 0)) 7082af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 7092af7698eSAlex Williamson allmulti ? "en" : "dis"); 710f565a7c2SAlex Williamson 711f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 71231278e71SJiri Pirko mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) + 713f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 714f565a7c2SAlex Williamson if (!buf) { 715f565a7c2SAlex Williamson dev_warn(&dev->dev, "No memory for MAC address buffer\n"); 716f565a7c2SAlex Williamson return; 717f565a7c2SAlex Williamson } 718f565a7c2SAlex Williamson 71923e258e1SAlex Williamson sg_init_table(sg, 2); 72023e258e1SAlex Williamson 721f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 72231278e71SJiri Pirko mac_data->entries = dev->uc.count; 723ccffad25SJiri Pirko i = 0; 72431278e71SJiri Pirko list_for_each_entry(ha, &dev->uc.list, list) 725ccffad25SJiri Pirko memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); 726f565a7c2SAlex Williamson 727f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 72831278e71SJiri Pirko sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN)); 729f565a7c2SAlex Williamson 730f565a7c2SAlex Williamson /* multicast list and count fill the end */ 73131278e71SJiri Pirko mac_data = (void *)&mac_data->macs[dev->uc.count][0]; 732f565a7c2SAlex Williamson 733f565a7c2SAlex Williamson mac_data->entries = dev->mc_count; 734f565a7c2SAlex Williamson addr = dev->mc_list; 735f565a7c2SAlex Williamson for (i = 0; i < dev->mc_count; i++, addr = addr->next) 736f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 737f565a7c2SAlex Williamson 738f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 739f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); 740f565a7c2SAlex Williamson 741f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 742f565a7c2SAlex Williamson VIRTIO_NET_CTRL_MAC_TABLE_SET, 743f565a7c2SAlex Williamson sg, 2, 0)) 744f565a7c2SAlex Williamson dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); 745f565a7c2SAlex Williamson 746f565a7c2SAlex Williamson kfree(buf); 7472af7698eSAlex Williamson } 7482af7698eSAlex Williamson 7491824a989SAlex Williamson static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) 7500bde9569SAlex Williamson { 7510bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7520bde9569SAlex Williamson struct scatterlist sg; 7530bde9569SAlex Williamson 75423e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 7550bde9569SAlex Williamson 7560bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7570bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) 7580bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 7590bde9569SAlex Williamson } 7600bde9569SAlex Williamson 7611824a989SAlex Williamson static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) 7620bde9569SAlex Williamson { 7630bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7640bde9569SAlex Williamson struct scatterlist sg; 7650bde9569SAlex Williamson 76623e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 7670bde9569SAlex Williamson 7680bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7690bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) 7700bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 7710bde9569SAlex Williamson } 7720bde9569SAlex Williamson 773a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = { 774a9ea3fc6SHerbert Xu .set_tx_csum = virtnet_set_tx_csum, 775a9ea3fc6SHerbert Xu .set_sg = ethtool_op_set_sg, 7760276b497SMark McLoughlin .set_tso = ethtool_op_set_tso, 777*5c516751SSridhar Samudrala .set_ufo = ethtool_op_set_ufo, 7789f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 779a9ea3fc6SHerbert Xu }; 780a9ea3fc6SHerbert Xu 78139da5814SMark McLoughlin #define MIN_MTU 68 78239da5814SMark McLoughlin #define MAX_MTU 65535 78339da5814SMark McLoughlin 78439da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 78539da5814SMark McLoughlin { 78639da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 78739da5814SMark McLoughlin return -EINVAL; 78839da5814SMark McLoughlin dev->mtu = new_mtu; 78939da5814SMark McLoughlin return 0; 79039da5814SMark McLoughlin } 79139da5814SMark McLoughlin 79276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 79376288b4eSStephen Hemminger .ndo_open = virtnet_open, 79476288b4eSStephen Hemminger .ndo_stop = virtnet_close, 79576288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 79676288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 7979c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 7982af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 79976288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 8001824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 8011824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 80276288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 80376288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 80476288b4eSStephen Hemminger #endif 80576288b4eSStephen Hemminger }; 80676288b4eSStephen Hemminger 8079f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi) 8089f4d26d0SMark McLoughlin { 8099f4d26d0SMark McLoughlin u16 v; 8109f4d26d0SMark McLoughlin 8119f4d26d0SMark McLoughlin if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) 8129f4d26d0SMark McLoughlin return; 8139f4d26d0SMark McLoughlin 8149f4d26d0SMark McLoughlin vi->vdev->config->get(vi->vdev, 8159f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 8169f4d26d0SMark McLoughlin &v, sizeof(v)); 8179f4d26d0SMark McLoughlin 8189f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 8199f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 8209f4d26d0SMark McLoughlin 8219f4d26d0SMark McLoughlin if (vi->status == v) 8229f4d26d0SMark McLoughlin return; 8239f4d26d0SMark McLoughlin 8249f4d26d0SMark McLoughlin vi->status = v; 8259f4d26d0SMark McLoughlin 8269f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 8279f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 8289f4d26d0SMark McLoughlin netif_wake_queue(vi->dev); 8299f4d26d0SMark McLoughlin } else { 8309f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 8319f4d26d0SMark McLoughlin netif_stop_queue(vi->dev); 8329f4d26d0SMark McLoughlin } 8339f4d26d0SMark McLoughlin } 8349f4d26d0SMark McLoughlin 8359f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 8369f4d26d0SMark McLoughlin { 8379f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 8389f4d26d0SMark McLoughlin 8399f4d26d0SMark McLoughlin virtnet_update_status(vi); 8409f4d26d0SMark McLoughlin } 8419f4d26d0SMark McLoughlin 842296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 843296f96fcSRusty Russell { 844296f96fcSRusty Russell int err; 845296f96fcSRusty Russell struct net_device *dev; 846296f96fcSRusty Russell struct virtnet_info *vi; 847d2a7dddaSMichael S. Tsirkin struct virtqueue *vqs[3]; 848d2a7dddaSMichael S. Tsirkin vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL}; 849d2a7dddaSMichael S. Tsirkin const char *names[] = { "input", "output", "control" }; 850d2a7dddaSMichael S. Tsirkin int nvqs; 851296f96fcSRusty Russell 852296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 853296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 854296f96fcSRusty Russell if (!dev) 855296f96fcSRusty Russell return -ENOMEM; 856296f96fcSRusty Russell 857296f96fcSRusty Russell /* Set up network device as normal. */ 85876288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 859296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 860a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 861296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 862296f96fcSRusty Russell 863296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 864c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 865296f96fcSRusty Russell /* This opens up the world of extra features. */ 866296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 867c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 86834a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 86934a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 87034a48579SRusty Russell } 8715539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 872c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 8735539ae96SRusty Russell dev->features |= NETIF_F_TSO; 874c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 8755539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 876c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 8775539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 878c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 8795539ae96SRusty Russell dev->features |= NETIF_F_UFO; 880296f96fcSRusty Russell } 881296f96fcSRusty Russell 882296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 883c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 884a586d4f6SRusty Russell vdev->config->get(vdev, 885a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 886a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 88762994b2dSAlex Williamson } else 888296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 889296f96fcSRusty Russell 890296f96fcSRusty Russell /* Set up our device-specific information */ 891296f96fcSRusty Russell vi = netdev_priv(dev); 8926c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 893296f96fcSRusty Russell vi->dev = dev; 894296f96fcSRusty Russell vi->vdev = vdev; 895d9d5dcc8SChristian Borntraeger vdev->priv = vi; 896fb6813f4SRusty Russell vi->pages = NULL; 897296f96fcSRusty Russell 898363f1514SRusty Russell /* If they give us a callback when all buffers are done, we don't need 899363f1514SRusty Russell * the timer. */ 900363f1514SRusty Russell vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); 901363f1514SRusty Russell 90297402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 90397402b96SHerbert Xu if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) 90497402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) 90597402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 90697402b96SHerbert Xu vi->big_packets = true; 90797402b96SHerbert Xu 9083f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 9093f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 9103f2c31d9SMark McLoughlin 911d2a7dddaSMichael S. Tsirkin /* We expect two virtqueues, receive then send, 912d2a7dddaSMichael S. Tsirkin * and optionally control. */ 913d2a7dddaSMichael S. Tsirkin nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2; 914296f96fcSRusty Russell 915d2a7dddaSMichael S. Tsirkin err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names); 916d2a7dddaSMichael S. Tsirkin if (err) 917d2a7dddaSMichael S. Tsirkin goto free; 918d2a7dddaSMichael S. Tsirkin 919d2a7dddaSMichael S. Tsirkin vi->rvq = vqs[0]; 920d2a7dddaSMichael S. Tsirkin vi->svq = vqs[1]; 921296f96fcSRusty Russell 9222a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 923d2a7dddaSMichael S. Tsirkin vi->cvq = vqs[2]; 9240bde9569SAlex Williamson 9250bde9569SAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 9260bde9569SAlex Williamson dev->features |= NETIF_F_HW_VLAN_FILTER; 9272a41f71dSAlex Williamson } 9282a41f71dSAlex Williamson 929296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 930296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 931296f96fcSRusty Russell skb_queue_head_init(&vi->send); 932296f96fcSRusty Russell 93311a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 93411a3a154SRusty Russell 935363f1514SRusty Russell if (!vi->free_in_tasklet) 93614c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 93714c998f0SMark McLoughlin 938296f96fcSRusty Russell err = register_netdev(dev); 939296f96fcSRusty Russell if (err) { 940296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 941d2a7dddaSMichael S. Tsirkin goto free_vqs; 942296f96fcSRusty Russell } 943b3369c1fSRusty Russell 944b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 945b3369c1fSRusty Russell try_fill_recv(vi); 946b3369c1fSRusty Russell 947b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 948b3369c1fSRusty Russell if (vi->num == 0) { 949b3369c1fSRusty Russell err = -ENOMEM; 950b3369c1fSRusty Russell goto unregister; 951b3369c1fSRusty Russell } 952b3369c1fSRusty Russell 9539f4d26d0SMark McLoughlin vi->status = VIRTIO_NET_S_LINK_UP; 9549f4d26d0SMark McLoughlin virtnet_update_status(vi); 9554783256eSPantelis Koukousoulas netif_carrier_on(dev); 9569f4d26d0SMark McLoughlin 957296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 958296f96fcSRusty Russell return 0; 959296f96fcSRusty Russell 960b3369c1fSRusty Russell unregister: 961b3369c1fSRusty Russell unregister_netdev(dev); 962d2a7dddaSMichael S. Tsirkin free_vqs: 963d2a7dddaSMichael S. Tsirkin vdev->config->del_vqs(vdev); 964296f96fcSRusty Russell free: 965296f96fcSRusty Russell free_netdev(dev); 966296f96fcSRusty Russell return err; 967296f96fcSRusty Russell } 968296f96fcSRusty Russell 969296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 970296f96fcSRusty Russell { 97174b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 972b3369c1fSRusty Russell struct sk_buff *skb; 973b3369c1fSRusty Russell 9746e5aa7efSRusty Russell /* Stop all the virtqueues. */ 9756e5aa7efSRusty Russell vdev->config->reset(vdev); 9766e5aa7efSRusty Russell 977363f1514SRusty Russell if (!vi->free_in_tasklet) 97814c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 97914c998f0SMark McLoughlin 980b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 981b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 982b3369c1fSRusty Russell kfree_skb(skb); 983b3369c1fSRusty Russell vi->num--; 984b3369c1fSRusty Russell } 985288369ccSWang Chen __skb_queue_purge(&vi->send); 986b3369c1fSRusty Russell 987b3369c1fSRusty Russell BUG_ON(vi->num != 0); 98874b2553fSRusty Russell 98974b2553fSRusty Russell unregister_netdev(vi->dev); 990fb6813f4SRusty Russell 991d2a7dddaSMichael S. Tsirkin vdev->config->del_vqs(vi->vdev); 992d2a7dddaSMichael S. Tsirkin 993fb6813f4SRusty Russell while (vi->pages) 994fb6813f4SRusty Russell __free_pages(get_a_page(vi, GFP_KERNEL), 0); 995fb6813f4SRusty Russell 99674b2553fSRusty Russell free_netdev(vi->dev); 997296f96fcSRusty Russell } 998296f96fcSRusty Russell 999296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1000296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1001296f96fcSRusty Russell { 0 }, 1002296f96fcSRusty Russell }; 1003296f96fcSRusty Russell 1004c45a6816SRusty Russell static unsigned int features[] = { 10055e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 10065e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1007c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 100897402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 1009*5c516751SSridhar Samudrala VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, 10102a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 10110bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 101297402b96SHerbert Xu VIRTIO_F_NOTIFY_ON_EMPTY, 1013c45a6816SRusty Russell }; 1014c45a6816SRusty Russell 1015296f96fcSRusty Russell static struct virtio_driver virtio_net = { 1016c45a6816SRusty Russell .feature_table = features, 1017c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1018296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1019296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1020296f96fcSRusty Russell .id_table = id_table, 1021296f96fcSRusty Russell .probe = virtnet_probe, 1022296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 10239f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 1024296f96fcSRusty Russell }; 1025296f96fcSRusty Russell 1026296f96fcSRusty Russell static int __init init(void) 1027296f96fcSRusty Russell { 1028296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 1029296f96fcSRusty Russell } 1030296f96fcSRusty Russell 1031296f96fcSRusty Russell static void __exit fini(void) 1032296f96fcSRusty Russell { 1033296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 1034296f96fcSRusty Russell } 1035296f96fcSRusty Russell module_init(init); 1036296f96fcSRusty Russell module_exit(fini); 1037296f96fcSRusty Russell 1038296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1039296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1040296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1041