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 5689c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 5699c46f6d4SAlex Williamson { 5709c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 5719c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 5729c46f6d4SAlex Williamson int ret; 5739c46f6d4SAlex Williamson 5749c46f6d4SAlex Williamson ret = eth_mac_addr(dev, p); 5759c46f6d4SAlex Williamson if (ret) 5769c46f6d4SAlex Williamson return ret; 5779c46f6d4SAlex Williamson 57862994b2dSAlex Williamson if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) 5799c46f6d4SAlex Williamson vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), 5809c46f6d4SAlex Williamson dev->dev_addr, dev->addr_len); 5819c46f6d4SAlex Williamson 5829c46f6d4SAlex Williamson return 0; 5839c46f6d4SAlex Williamson } 5849c46f6d4SAlex Williamson 585da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 586da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 587da74e89dSAmit Shah { 588da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 589da74e89dSAmit Shah 590da74e89dSAmit Shah napi_schedule(&vi->napi); 591da74e89dSAmit Shah } 592da74e89dSAmit Shah #endif 593da74e89dSAmit Shah 594296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 595296f96fcSRusty Russell { 596296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 597296f96fcSRusty Russell 598296f96fcSRusty Russell napi_enable(&vi->napi); 599a48bd8f6SRusty Russell 600a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 601a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 602370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 603370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 604288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 605a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 606288379f0SBen Hutchings __napi_schedule(&vi->napi); 607370076d9SChristian Borntraeger } 608296f96fcSRusty Russell return 0; 609296f96fcSRusty Russell } 610296f96fcSRusty Russell 6112a41f71dSAlex Williamson /* 6122a41f71dSAlex Williamson * Send command via the control virtqueue and check status. Commands 6132a41f71dSAlex Williamson * supported by the hypervisor, as indicated by feature bits, should 6142a41f71dSAlex Williamson * never fail unless improperly formated. 6152a41f71dSAlex Williamson */ 6162a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 6172a41f71dSAlex Williamson struct scatterlist *data, int out, int in) 6182a41f71dSAlex Williamson { 61923e258e1SAlex Williamson struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; 6202a41f71dSAlex Williamson struct virtio_net_ctrl_hdr ctrl; 6212a41f71dSAlex Williamson virtio_net_ctrl_ack status = ~0; 6222a41f71dSAlex Williamson unsigned int tmp; 62323e258e1SAlex Williamson int i; 6242a41f71dSAlex Williamson 6252a41f71dSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 6262a41f71dSAlex Williamson BUG(); /* Caller should know better */ 6272a41f71dSAlex Williamson return false; 6282a41f71dSAlex Williamson } 6292a41f71dSAlex Williamson 6302a41f71dSAlex Williamson BUG_ON(out + in > VIRTNET_SEND_COMMAND_SG_MAX); 6312a41f71dSAlex Williamson 6322a41f71dSAlex Williamson out++; /* Add header */ 6332a41f71dSAlex Williamson in++; /* Add return status */ 6342a41f71dSAlex Williamson 6352a41f71dSAlex Williamson ctrl.class = class; 6362a41f71dSAlex Williamson ctrl.cmd = cmd; 6372a41f71dSAlex Williamson 6382a41f71dSAlex Williamson sg_init_table(sg, out + in); 6392a41f71dSAlex Williamson 6402a41f71dSAlex Williamson sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); 64123e258e1SAlex Williamson for_each_sg(data, s, out + in - 2, i) 64223e258e1SAlex Williamson sg_set_buf(&sg[i + 1], sg_virt(s), s->length); 6432a41f71dSAlex Williamson sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); 6442a41f71dSAlex Williamson 6452a41f71dSAlex Williamson if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0) 6462a41f71dSAlex Williamson BUG(); 6472a41f71dSAlex Williamson 6482a41f71dSAlex Williamson vi->cvq->vq_ops->kick(vi->cvq); 6492a41f71dSAlex Williamson 6502a41f71dSAlex Williamson /* 6512a41f71dSAlex Williamson * Spin for a response, the kick causes an ioport write, trapping 6522a41f71dSAlex Williamson * into the hypervisor, so the request should be handled immediately. 6532a41f71dSAlex Williamson */ 6542a41f71dSAlex Williamson while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) 6552a41f71dSAlex Williamson cpu_relax(); 6562a41f71dSAlex Williamson 6572a41f71dSAlex Williamson return status == VIRTIO_NET_OK; 6582a41f71dSAlex Williamson } 6592a41f71dSAlex Williamson 660296f96fcSRusty Russell static int virtnet_close(struct net_device *dev) 661296f96fcSRusty Russell { 662296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 663296f96fcSRusty Russell 664296f96fcSRusty Russell napi_disable(&vi->napi); 665296f96fcSRusty Russell 666296f96fcSRusty Russell return 0; 667296f96fcSRusty Russell } 668296f96fcSRusty Russell 669a9ea3fc6SHerbert Xu static int virtnet_set_tx_csum(struct net_device *dev, u32 data) 670a9ea3fc6SHerbert Xu { 671a9ea3fc6SHerbert Xu struct virtnet_info *vi = netdev_priv(dev); 672a9ea3fc6SHerbert Xu struct virtio_device *vdev = vi->vdev; 673a9ea3fc6SHerbert Xu 674a9ea3fc6SHerbert Xu if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) 675a9ea3fc6SHerbert Xu return -ENOSYS; 676a9ea3fc6SHerbert Xu 677a9ea3fc6SHerbert Xu return ethtool_op_set_tx_hw_csum(dev, data); 678a9ea3fc6SHerbert Xu } 679a9ea3fc6SHerbert Xu 6802af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev) 6812af7698eSAlex Williamson { 6822af7698eSAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 683f565a7c2SAlex Williamson struct scatterlist sg[2]; 6842af7698eSAlex Williamson u8 promisc, allmulti; 685f565a7c2SAlex Williamson struct virtio_net_ctrl_mac *mac_data; 686f565a7c2SAlex Williamson struct dev_addr_list *addr; 687f565a7c2SAlex Williamson void *buf; 688f565a7c2SAlex Williamson int i; 6892af7698eSAlex Williamson 6902af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 6912af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 6922af7698eSAlex Williamson return; 6932af7698eSAlex Williamson 694f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 695f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 6962af7698eSAlex Williamson 69723e258e1SAlex Williamson sg_init_one(sg, &promisc, sizeof(promisc)); 6982af7698eSAlex Williamson 6992af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 7002af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 701f565a7c2SAlex Williamson sg, 1, 0)) 7022af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 7032af7698eSAlex Williamson promisc ? "en" : "dis"); 7042af7698eSAlex Williamson 70523e258e1SAlex Williamson sg_init_one(sg, &allmulti, sizeof(allmulti)); 7062af7698eSAlex Williamson 7072af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 7082af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 709f565a7c2SAlex Williamson sg, 1, 0)) 7102af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 7112af7698eSAlex Williamson allmulti ? "en" : "dis"); 712f565a7c2SAlex Williamson 713f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 714f565a7c2SAlex Williamson mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) + 715f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 716f565a7c2SAlex Williamson if (!buf) { 717f565a7c2SAlex Williamson dev_warn(&dev->dev, "No memory for MAC address buffer\n"); 718f565a7c2SAlex Williamson return; 719f565a7c2SAlex Williamson } 720f565a7c2SAlex Williamson 72123e258e1SAlex Williamson sg_init_table(sg, 2); 72223e258e1SAlex Williamson 723f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 724f565a7c2SAlex Williamson mac_data->entries = dev->uc_count; 725f565a7c2SAlex Williamson addr = dev->uc_list; 726f565a7c2SAlex Williamson for (i = 0; i < dev->uc_count; i++, addr = addr->next) 727f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 728f565a7c2SAlex Williamson 729f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 730f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN)); 731f565a7c2SAlex Williamson 732f565a7c2SAlex Williamson /* multicast list and count fill the end */ 733f565a7c2SAlex Williamson mac_data = (void *)&mac_data->macs[dev->uc_count][0]; 734f565a7c2SAlex Williamson 735f565a7c2SAlex Williamson mac_data->entries = dev->mc_count; 736f565a7c2SAlex Williamson addr = dev->mc_list; 737f565a7c2SAlex Williamson for (i = 0; i < dev->mc_count; i++, addr = addr->next) 738f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 739f565a7c2SAlex Williamson 740f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 741f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); 742f565a7c2SAlex Williamson 743f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 744f565a7c2SAlex Williamson VIRTIO_NET_CTRL_MAC_TABLE_SET, 745f565a7c2SAlex Williamson sg, 2, 0)) 746f565a7c2SAlex Williamson dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); 747f565a7c2SAlex Williamson 748f565a7c2SAlex Williamson kfree(buf); 7492af7698eSAlex Williamson } 7502af7698eSAlex Williamson 751*1824a989SAlex Williamson static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) 7520bde9569SAlex Williamson { 7530bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7540bde9569SAlex Williamson struct scatterlist sg; 7550bde9569SAlex Williamson 75623e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 7570bde9569SAlex Williamson 7580bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7590bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) 7600bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 7610bde9569SAlex Williamson } 7620bde9569SAlex Williamson 763*1824a989SAlex Williamson static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) 7640bde9569SAlex Williamson { 7650bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7660bde9569SAlex Williamson struct scatterlist sg; 7670bde9569SAlex Williamson 76823e258e1SAlex Williamson sg_init_one(&sg, &vid, sizeof(vid)); 7690bde9569SAlex Williamson 7700bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7710bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) 7720bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 7730bde9569SAlex Williamson } 7740bde9569SAlex Williamson 775a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = { 776a9ea3fc6SHerbert Xu .set_tx_csum = virtnet_set_tx_csum, 777a9ea3fc6SHerbert Xu .set_sg = ethtool_op_set_sg, 7780276b497SMark McLoughlin .set_tso = ethtool_op_set_tso, 7799f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 780a9ea3fc6SHerbert Xu }; 781a9ea3fc6SHerbert Xu 78239da5814SMark McLoughlin #define MIN_MTU 68 78339da5814SMark McLoughlin #define MAX_MTU 65535 78439da5814SMark McLoughlin 78539da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 78639da5814SMark McLoughlin { 78739da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 78839da5814SMark McLoughlin return -EINVAL; 78939da5814SMark McLoughlin dev->mtu = new_mtu; 79039da5814SMark McLoughlin return 0; 79139da5814SMark McLoughlin } 79239da5814SMark McLoughlin 79376288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 79476288b4eSStephen Hemminger .ndo_open = virtnet_open, 79576288b4eSStephen Hemminger .ndo_stop = virtnet_close, 79676288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 79776288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 7989c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 7992af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 80076288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 801*1824a989SAlex Williamson .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, 802*1824a989SAlex Williamson .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, 80376288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 80476288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 80576288b4eSStephen Hemminger #endif 80676288b4eSStephen Hemminger }; 80776288b4eSStephen Hemminger 8089f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi) 8099f4d26d0SMark McLoughlin { 8109f4d26d0SMark McLoughlin u16 v; 8119f4d26d0SMark McLoughlin 8129f4d26d0SMark McLoughlin if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) 8139f4d26d0SMark McLoughlin return; 8149f4d26d0SMark McLoughlin 8159f4d26d0SMark McLoughlin vi->vdev->config->get(vi->vdev, 8169f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 8179f4d26d0SMark McLoughlin &v, sizeof(v)); 8189f4d26d0SMark McLoughlin 8199f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 8209f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 8219f4d26d0SMark McLoughlin 8229f4d26d0SMark McLoughlin if (vi->status == v) 8239f4d26d0SMark McLoughlin return; 8249f4d26d0SMark McLoughlin 8259f4d26d0SMark McLoughlin vi->status = v; 8269f4d26d0SMark McLoughlin 8279f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 8289f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 8299f4d26d0SMark McLoughlin netif_wake_queue(vi->dev); 8309f4d26d0SMark McLoughlin } else { 8319f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 8329f4d26d0SMark McLoughlin netif_stop_queue(vi->dev); 8339f4d26d0SMark McLoughlin } 8349f4d26d0SMark McLoughlin } 8359f4d26d0SMark McLoughlin 8369f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 8379f4d26d0SMark McLoughlin { 8389f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 8399f4d26d0SMark McLoughlin 8409f4d26d0SMark McLoughlin virtnet_update_status(vi); 8419f4d26d0SMark McLoughlin } 8429f4d26d0SMark McLoughlin 843296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 844296f96fcSRusty Russell { 845296f96fcSRusty Russell int err; 846296f96fcSRusty Russell struct net_device *dev; 847296f96fcSRusty Russell struct virtnet_info *vi; 848296f96fcSRusty Russell 849296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 850296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 851296f96fcSRusty Russell if (!dev) 852296f96fcSRusty Russell return -ENOMEM; 853296f96fcSRusty Russell 854296f96fcSRusty Russell /* Set up network device as normal. */ 85576288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 856296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 857a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 858296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 859296f96fcSRusty Russell 860296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 861c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 862296f96fcSRusty Russell /* This opens up the world of extra features. */ 863296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 864c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 86534a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 86634a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 86734a48579SRusty Russell } 8685539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 869c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 8705539ae96SRusty Russell dev->features |= NETIF_F_TSO; 871c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 8725539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 873c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 8745539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 875c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 8765539ae96SRusty Russell dev->features |= NETIF_F_UFO; 877296f96fcSRusty Russell } 878296f96fcSRusty Russell 879296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 880c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 881a586d4f6SRusty Russell vdev->config->get(vdev, 882a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 883a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 88462994b2dSAlex Williamson } else 885296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 886296f96fcSRusty Russell 887296f96fcSRusty Russell /* Set up our device-specific information */ 888296f96fcSRusty Russell vi = netdev_priv(dev); 8896c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 890296f96fcSRusty Russell vi->dev = dev; 891296f96fcSRusty Russell vi->vdev = vdev; 892d9d5dcc8SChristian Borntraeger vdev->priv = vi; 893fb6813f4SRusty Russell vi->pages = NULL; 894296f96fcSRusty Russell 895363f1514SRusty Russell /* If they give us a callback when all buffers are done, we don't need 896363f1514SRusty Russell * the timer. */ 897363f1514SRusty Russell vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); 898363f1514SRusty Russell 89997402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 90097402b96SHerbert Xu if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) 90197402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) 90297402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 90397402b96SHerbert Xu vi->big_packets = true; 90497402b96SHerbert Xu 9053f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 9063f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 9073f2c31d9SMark McLoughlin 908296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 909a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 910296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 911296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 912296f96fcSRusty Russell goto free; 913296f96fcSRusty Russell } 914296f96fcSRusty Russell 915a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 916296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 917296f96fcSRusty Russell err = PTR_ERR(vi->svq); 918296f96fcSRusty Russell goto free_recv; 919296f96fcSRusty Russell } 920296f96fcSRusty Russell 9212a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 9222a41f71dSAlex Williamson vi->cvq = vdev->config->find_vq(vdev, 2, NULL); 9232a41f71dSAlex Williamson if (IS_ERR(vi->cvq)) { 9242a41f71dSAlex Williamson err = PTR_ERR(vi->svq); 9252a41f71dSAlex Williamson goto free_send; 9262a41f71dSAlex Williamson } 9270bde9569SAlex Williamson 9280bde9569SAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 9290bde9569SAlex Williamson dev->features |= NETIF_F_HW_VLAN_FILTER; 9302a41f71dSAlex Williamson } 9312a41f71dSAlex Williamson 932296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 933296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 934296f96fcSRusty Russell skb_queue_head_init(&vi->send); 935296f96fcSRusty Russell 93611a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 93711a3a154SRusty Russell 938363f1514SRusty Russell if (!vi->free_in_tasklet) 93914c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 94014c998f0SMark McLoughlin 941296f96fcSRusty Russell err = register_netdev(dev); 942296f96fcSRusty Russell if (err) { 943296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 9442a41f71dSAlex Williamson goto free_ctrl; 945296f96fcSRusty Russell } 946b3369c1fSRusty Russell 947b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 948b3369c1fSRusty Russell try_fill_recv(vi); 949b3369c1fSRusty Russell 950b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 951b3369c1fSRusty Russell if (vi->num == 0) { 952b3369c1fSRusty Russell err = -ENOMEM; 953b3369c1fSRusty Russell goto unregister; 954b3369c1fSRusty Russell } 955b3369c1fSRusty Russell 9569f4d26d0SMark McLoughlin vi->status = VIRTIO_NET_S_LINK_UP; 9579f4d26d0SMark McLoughlin virtnet_update_status(vi); 9584783256eSPantelis Koukousoulas netif_carrier_on(dev); 9599f4d26d0SMark McLoughlin 960296f96fcSRusty Russell pr_debug("virtnet: registered device %s\n", dev->name); 961296f96fcSRusty Russell return 0; 962296f96fcSRusty Russell 963b3369c1fSRusty Russell unregister: 964b3369c1fSRusty Russell unregister_netdev(dev); 9652a41f71dSAlex Williamson free_ctrl: 9662a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9672a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 968296f96fcSRusty Russell free_send: 969296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 970296f96fcSRusty Russell free_recv: 971296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 972296f96fcSRusty Russell free: 973296f96fcSRusty Russell free_netdev(dev); 974296f96fcSRusty Russell return err; 975296f96fcSRusty Russell } 976296f96fcSRusty Russell 977296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 978296f96fcSRusty Russell { 97974b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 980b3369c1fSRusty Russell struct sk_buff *skb; 981b3369c1fSRusty Russell 9826e5aa7efSRusty Russell /* Stop all the virtqueues. */ 9836e5aa7efSRusty Russell vdev->config->reset(vdev); 9846e5aa7efSRusty Russell 985363f1514SRusty Russell if (!vi->free_in_tasklet) 98614c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 98714c998f0SMark McLoughlin 988b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 989b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 990b3369c1fSRusty Russell kfree_skb(skb); 991b3369c1fSRusty Russell vi->num--; 992b3369c1fSRusty Russell } 993288369ccSWang Chen __skb_queue_purge(&vi->send); 994b3369c1fSRusty Russell 995b3369c1fSRusty Russell BUG_ON(vi->num != 0); 99674b2553fSRusty Russell 99774b2553fSRusty Russell vdev->config->del_vq(vi->svq); 99874b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 9992a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 10002a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 100174b2553fSRusty Russell unregister_netdev(vi->dev); 1002fb6813f4SRusty Russell 1003fb6813f4SRusty Russell while (vi->pages) 1004fb6813f4SRusty Russell __free_pages(get_a_page(vi, GFP_KERNEL), 0); 1005fb6813f4SRusty Russell 100674b2553fSRusty Russell free_netdev(vi->dev); 1007296f96fcSRusty Russell } 1008296f96fcSRusty Russell 1009296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1010296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1011296f96fcSRusty Russell { 0 }, 1012296f96fcSRusty Russell }; 1013296f96fcSRusty Russell 1014c45a6816SRusty Russell static unsigned int features[] = { 10155e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 10165e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1017c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 101897402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 101997402b96SHerbert Xu VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ 10202a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 10210bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 102297402b96SHerbert Xu VIRTIO_F_NOTIFY_ON_EMPTY, 1023c45a6816SRusty Russell }; 1024c45a6816SRusty Russell 1025296f96fcSRusty Russell static struct virtio_driver virtio_net = { 1026c45a6816SRusty Russell .feature_table = features, 1027c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1028296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1029296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1030296f96fcSRusty Russell .id_table = id_table, 1031296f96fcSRusty Russell .probe = virtnet_probe, 1032296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 10339f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 1034296f96fcSRusty Russell }; 1035296f96fcSRusty Russell 1036296f96fcSRusty Russell static int __init init(void) 1037296f96fcSRusty Russell { 1038296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 1039296f96fcSRusty Russell } 1040296f96fcSRusty Russell 1041296f96fcSRusty Russell static void __exit fini(void) 1042296f96fcSRusty Russell { 1043296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 1044296f96fcSRusty Russell } 1045296f96fcSRusty Russell module_init(init); 1046296f96fcSRusty Russell module_exit(fini); 1047296f96fcSRusty Russell 1048296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1049296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1050296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1051