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 568*9c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p) 569*9c46f6d4SAlex Williamson { 570*9c46f6d4SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 571*9c46f6d4SAlex Williamson struct virtio_device *vdev = vi->vdev; 572*9c46f6d4SAlex Williamson int ret; 573*9c46f6d4SAlex Williamson 574*9c46f6d4SAlex Williamson ret = eth_mac_addr(dev, p); 575*9c46f6d4SAlex Williamson if (ret) 576*9c46f6d4SAlex Williamson return ret; 577*9c46f6d4SAlex Williamson 578*9c46f6d4SAlex Williamson vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), 579*9c46f6d4SAlex Williamson dev->dev_addr, dev->addr_len); 580*9c46f6d4SAlex Williamson 581*9c46f6d4SAlex Williamson return 0; 582*9c46f6d4SAlex Williamson } 583*9c46f6d4SAlex Williamson 584da74e89dSAmit Shah #ifdef CONFIG_NET_POLL_CONTROLLER 585da74e89dSAmit Shah static void virtnet_netpoll(struct net_device *dev) 586da74e89dSAmit Shah { 587da74e89dSAmit Shah struct virtnet_info *vi = netdev_priv(dev); 588da74e89dSAmit Shah 589da74e89dSAmit Shah napi_schedule(&vi->napi); 590da74e89dSAmit Shah } 591da74e89dSAmit Shah #endif 592da74e89dSAmit Shah 593296f96fcSRusty Russell static int virtnet_open(struct net_device *dev) 594296f96fcSRusty Russell { 595296f96fcSRusty Russell struct virtnet_info *vi = netdev_priv(dev); 596296f96fcSRusty Russell 597296f96fcSRusty Russell napi_enable(&vi->napi); 598a48bd8f6SRusty Russell 599a48bd8f6SRusty Russell /* If all buffers were filled by other side before we napi_enabled, we 600a48bd8f6SRusty Russell * won't get another interrupt, so process any outstanding packets 601370076d9SChristian Borntraeger * now. virtnet_poll wants re-enable the queue, so we disable here. 602370076d9SChristian Borntraeger * We synchronize against interrupts via NAPI_STATE_SCHED */ 603288379f0SBen Hutchings if (napi_schedule_prep(&vi->napi)) { 604a48bd8f6SRusty Russell vi->rvq->vq_ops->disable_cb(vi->rvq); 605288379f0SBen Hutchings __napi_schedule(&vi->napi); 606370076d9SChristian Borntraeger } 607296f96fcSRusty Russell return 0; 608296f96fcSRusty Russell } 609296f96fcSRusty Russell 6102a41f71dSAlex Williamson /* 6112a41f71dSAlex Williamson * Send command via the control virtqueue and check status. Commands 6122a41f71dSAlex Williamson * supported by the hypervisor, as indicated by feature bits, should 6132a41f71dSAlex Williamson * never fail unless improperly formated. 6142a41f71dSAlex Williamson */ 6152a41f71dSAlex Williamson static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, 6162a41f71dSAlex Williamson struct scatterlist *data, int out, int in) 6172a41f71dSAlex Williamson { 6182a41f71dSAlex Williamson struct scatterlist sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; 6192a41f71dSAlex Williamson struct virtio_net_ctrl_hdr ctrl; 6202a41f71dSAlex Williamson virtio_net_ctrl_ack status = ~0; 6212a41f71dSAlex Williamson unsigned int tmp; 6222a41f71dSAlex Williamson 6232a41f71dSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 6242a41f71dSAlex Williamson BUG(); /* Caller should know better */ 6252a41f71dSAlex Williamson return false; 6262a41f71dSAlex Williamson } 6272a41f71dSAlex Williamson 6282a41f71dSAlex Williamson BUG_ON(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)); 6392a41f71dSAlex Williamson memcpy(&sg[1], data, sizeof(struct scatterlist) * (out + in - 2)); 6402a41f71dSAlex Williamson sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); 6412a41f71dSAlex Williamson 6422a41f71dSAlex Williamson if (vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) != 0) 6432a41f71dSAlex Williamson BUG(); 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; 684f565a7c2SAlex Williamson void *buf; 685f565a7c2SAlex Williamson int i; 6862af7698eSAlex Williamson 6872af7698eSAlex Williamson /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ 6882af7698eSAlex Williamson if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 6892af7698eSAlex Williamson return; 6902af7698eSAlex Williamson 691f565a7c2SAlex Williamson promisc = ((dev->flags & IFF_PROMISC) != 0); 692f565a7c2SAlex Williamson allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 6932af7698eSAlex Williamson 694f565a7c2SAlex Williamson sg_set_buf(sg, &promisc, sizeof(promisc)); 6952af7698eSAlex Williamson 6962af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 6972af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_PROMISC, 698f565a7c2SAlex Williamson sg, 1, 0)) 6992af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 7002af7698eSAlex Williamson promisc ? "en" : "dis"); 7012af7698eSAlex Williamson 702f565a7c2SAlex Williamson sg_set_buf(sg, &allmulti, sizeof(allmulti)); 7032af7698eSAlex Williamson 7042af7698eSAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 7052af7698eSAlex Williamson VIRTIO_NET_CTRL_RX_ALLMULTI, 706f565a7c2SAlex Williamson sg, 1, 0)) 7072af7698eSAlex Williamson dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 7082af7698eSAlex Williamson allmulti ? "en" : "dis"); 709f565a7c2SAlex Williamson 710f565a7c2SAlex Williamson /* MAC filter - use one buffer for both lists */ 711f565a7c2SAlex Williamson mac_data = buf = kzalloc(((dev->uc_count + dev->mc_count) * ETH_ALEN) + 712f565a7c2SAlex Williamson (2 * sizeof(mac_data->entries)), GFP_ATOMIC); 713f565a7c2SAlex Williamson if (!buf) { 714f565a7c2SAlex Williamson dev_warn(&dev->dev, "No memory for MAC address buffer\n"); 715f565a7c2SAlex Williamson return; 716f565a7c2SAlex Williamson } 717f565a7c2SAlex Williamson 718f565a7c2SAlex Williamson /* Store the unicast list and count in the front of the buffer */ 719f565a7c2SAlex Williamson mac_data->entries = dev->uc_count; 720f565a7c2SAlex Williamson addr = dev->uc_list; 721f565a7c2SAlex Williamson for (i = 0; i < dev->uc_count; i++, addr = addr->next) 722f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 723f565a7c2SAlex Williamson 724f565a7c2SAlex Williamson sg_set_buf(&sg[0], mac_data, 725f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->uc_count * ETH_ALEN)); 726f565a7c2SAlex Williamson 727f565a7c2SAlex Williamson /* multicast list and count fill the end */ 728f565a7c2SAlex Williamson mac_data = (void *)&mac_data->macs[dev->uc_count][0]; 729f565a7c2SAlex Williamson 730f565a7c2SAlex Williamson mac_data->entries = dev->mc_count; 731f565a7c2SAlex Williamson addr = dev->mc_list; 732f565a7c2SAlex Williamson for (i = 0; i < dev->mc_count; i++, addr = addr->next) 733f565a7c2SAlex Williamson memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); 734f565a7c2SAlex Williamson 735f565a7c2SAlex Williamson sg_set_buf(&sg[1], mac_data, 736f565a7c2SAlex Williamson sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); 737f565a7c2SAlex Williamson 738f565a7c2SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, 739f565a7c2SAlex Williamson VIRTIO_NET_CTRL_MAC_TABLE_SET, 740f565a7c2SAlex Williamson sg, 2, 0)) 741f565a7c2SAlex Williamson dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); 742f565a7c2SAlex Williamson 743f565a7c2SAlex Williamson kfree(buf); 7442af7698eSAlex Williamson } 7452af7698eSAlex Williamson 7460bde9569SAlex Williamson static void virnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) 7470bde9569SAlex Williamson { 7480bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7490bde9569SAlex Williamson struct scatterlist sg; 7500bde9569SAlex Williamson 7510bde9569SAlex Williamson sg_set_buf(&sg, &vid, sizeof(vid)); 7520bde9569SAlex Williamson 7530bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7540bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) 7550bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); 7560bde9569SAlex Williamson } 7570bde9569SAlex Williamson 7580bde9569SAlex Williamson static void virnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) 7590bde9569SAlex Williamson { 7600bde9569SAlex Williamson struct virtnet_info *vi = netdev_priv(dev); 7610bde9569SAlex Williamson struct scatterlist sg; 7620bde9569SAlex Williamson 7630bde9569SAlex Williamson sg_set_buf(&sg, &vid, sizeof(vid)); 7640bde9569SAlex Williamson 7650bde9569SAlex Williamson if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 7660bde9569SAlex Williamson VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) 7670bde9569SAlex Williamson dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); 7680bde9569SAlex Williamson } 7690bde9569SAlex Williamson 770a9ea3fc6SHerbert Xu static struct ethtool_ops virtnet_ethtool_ops = { 771a9ea3fc6SHerbert Xu .set_tx_csum = virtnet_set_tx_csum, 772a9ea3fc6SHerbert Xu .set_sg = ethtool_op_set_sg, 7730276b497SMark McLoughlin .set_tso = ethtool_op_set_tso, 7749f4d26d0SMark McLoughlin .get_link = ethtool_op_get_link, 775a9ea3fc6SHerbert Xu }; 776a9ea3fc6SHerbert Xu 77739da5814SMark McLoughlin #define MIN_MTU 68 77839da5814SMark McLoughlin #define MAX_MTU 65535 77939da5814SMark McLoughlin 78039da5814SMark McLoughlin static int virtnet_change_mtu(struct net_device *dev, int new_mtu) 78139da5814SMark McLoughlin { 78239da5814SMark McLoughlin if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) 78339da5814SMark McLoughlin return -EINVAL; 78439da5814SMark McLoughlin dev->mtu = new_mtu; 78539da5814SMark McLoughlin return 0; 78639da5814SMark McLoughlin } 78739da5814SMark McLoughlin 78876288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = { 78976288b4eSStephen Hemminger .ndo_open = virtnet_open, 79076288b4eSStephen Hemminger .ndo_stop = virtnet_close, 79176288b4eSStephen Hemminger .ndo_start_xmit = start_xmit, 79276288b4eSStephen Hemminger .ndo_validate_addr = eth_validate_addr, 793*9c46f6d4SAlex Williamson .ndo_set_mac_address = virtnet_set_mac_address, 7942af7698eSAlex Williamson .ndo_set_rx_mode = virtnet_set_rx_mode, 79576288b4eSStephen Hemminger .ndo_change_mtu = virtnet_change_mtu, 7960bde9569SAlex Williamson .ndo_vlan_rx_add_vid = virnet_vlan_rx_add_vid, 7970bde9569SAlex Williamson .ndo_vlan_rx_kill_vid = virnet_vlan_rx_kill_vid, 79876288b4eSStephen Hemminger #ifdef CONFIG_NET_POLL_CONTROLLER 79976288b4eSStephen Hemminger .ndo_poll_controller = virtnet_netpoll, 80076288b4eSStephen Hemminger #endif 80176288b4eSStephen Hemminger }; 80276288b4eSStephen Hemminger 8039f4d26d0SMark McLoughlin static void virtnet_update_status(struct virtnet_info *vi) 8049f4d26d0SMark McLoughlin { 8059f4d26d0SMark McLoughlin u16 v; 8069f4d26d0SMark McLoughlin 8079f4d26d0SMark McLoughlin if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) 8089f4d26d0SMark McLoughlin return; 8099f4d26d0SMark McLoughlin 8109f4d26d0SMark McLoughlin vi->vdev->config->get(vi->vdev, 8119f4d26d0SMark McLoughlin offsetof(struct virtio_net_config, status), 8129f4d26d0SMark McLoughlin &v, sizeof(v)); 8139f4d26d0SMark McLoughlin 8149f4d26d0SMark McLoughlin /* Ignore unknown (future) status bits */ 8159f4d26d0SMark McLoughlin v &= VIRTIO_NET_S_LINK_UP; 8169f4d26d0SMark McLoughlin 8179f4d26d0SMark McLoughlin if (vi->status == v) 8189f4d26d0SMark McLoughlin return; 8199f4d26d0SMark McLoughlin 8209f4d26d0SMark McLoughlin vi->status = v; 8219f4d26d0SMark McLoughlin 8229f4d26d0SMark McLoughlin if (vi->status & VIRTIO_NET_S_LINK_UP) { 8239f4d26d0SMark McLoughlin netif_carrier_on(vi->dev); 8249f4d26d0SMark McLoughlin netif_wake_queue(vi->dev); 8259f4d26d0SMark McLoughlin } else { 8269f4d26d0SMark McLoughlin netif_carrier_off(vi->dev); 8279f4d26d0SMark McLoughlin netif_stop_queue(vi->dev); 8289f4d26d0SMark McLoughlin } 8299f4d26d0SMark McLoughlin } 8309f4d26d0SMark McLoughlin 8319f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev) 8329f4d26d0SMark McLoughlin { 8339f4d26d0SMark McLoughlin struct virtnet_info *vi = vdev->priv; 8349f4d26d0SMark McLoughlin 8359f4d26d0SMark McLoughlin virtnet_update_status(vi); 8369f4d26d0SMark McLoughlin } 8379f4d26d0SMark McLoughlin 838296f96fcSRusty Russell static int virtnet_probe(struct virtio_device *vdev) 839296f96fcSRusty Russell { 840296f96fcSRusty Russell int err; 841296f96fcSRusty Russell struct net_device *dev; 842296f96fcSRusty Russell struct virtnet_info *vi; 843296f96fcSRusty Russell 844296f96fcSRusty Russell /* Allocate ourselves a network device with room for our info */ 845296f96fcSRusty Russell dev = alloc_etherdev(sizeof(struct virtnet_info)); 846296f96fcSRusty Russell if (!dev) 847296f96fcSRusty Russell return -ENOMEM; 848296f96fcSRusty Russell 849296f96fcSRusty Russell /* Set up network device as normal. */ 85076288b4eSStephen Hemminger dev->netdev_ops = &virtnet_netdev; 851296f96fcSRusty Russell dev->features = NETIF_F_HIGHDMA; 852a9ea3fc6SHerbert Xu SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); 853296f96fcSRusty Russell SET_NETDEV_DEV(dev, &vdev->dev); 854296f96fcSRusty Russell 855296f96fcSRusty Russell /* Do we support "hardware" checksums? */ 856c45a6816SRusty Russell if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { 857296f96fcSRusty Russell /* This opens up the world of extra features. */ 858296f96fcSRusty Russell dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; 859c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { 86034a48579SRusty Russell dev->features |= NETIF_F_TSO | NETIF_F_UFO 86134a48579SRusty Russell | NETIF_F_TSO_ECN | NETIF_F_TSO6; 86234a48579SRusty Russell } 8635539ae96SRusty Russell /* Individual feature bits: what can host handle? */ 864c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) 8655539ae96SRusty Russell dev->features |= NETIF_F_TSO; 866c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) 8675539ae96SRusty Russell dev->features |= NETIF_F_TSO6; 868c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) 8695539ae96SRusty Russell dev->features |= NETIF_F_TSO_ECN; 870c45a6816SRusty Russell if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) 8715539ae96SRusty Russell dev->features |= NETIF_F_UFO; 872296f96fcSRusty Russell } 873296f96fcSRusty Russell 874296f96fcSRusty Russell /* Configuration may specify what MAC to use. Otherwise random. */ 875c45a6816SRusty Russell if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { 876a586d4f6SRusty Russell vdev->config->get(vdev, 877a586d4f6SRusty Russell offsetof(struct virtio_net_config, mac), 878a586d4f6SRusty Russell dev->dev_addr, dev->addr_len); 879*9c46f6d4SAlex Williamson } else { 880296f96fcSRusty Russell random_ether_addr(dev->dev_addr); 881*9c46f6d4SAlex Williamson vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), 882*9c46f6d4SAlex Williamson dev->dev_addr, dev->addr_len); 883*9c46f6d4SAlex Williamson } 884296f96fcSRusty Russell 885296f96fcSRusty Russell /* Set up our device-specific information */ 886296f96fcSRusty Russell vi = netdev_priv(dev); 8876c0cd7c0SDor Laor netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); 888296f96fcSRusty Russell vi->dev = dev; 889296f96fcSRusty Russell vi->vdev = vdev; 890d9d5dcc8SChristian Borntraeger vdev->priv = vi; 891fb6813f4SRusty Russell vi->pages = NULL; 892296f96fcSRusty Russell 893363f1514SRusty Russell /* If they give us a callback when all buffers are done, we don't need 894363f1514SRusty Russell * the timer. */ 895363f1514SRusty Russell vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); 896363f1514SRusty Russell 89797402b96SHerbert Xu /* If we can receive ANY GSO packets, we must allocate large ones. */ 89897402b96SHerbert Xu if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) 89997402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) 90097402b96SHerbert Xu || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) 90197402b96SHerbert Xu vi->big_packets = true; 90297402b96SHerbert Xu 9033f2c31d9SMark McLoughlin if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) 9043f2c31d9SMark McLoughlin vi->mergeable_rx_bufs = true; 9053f2c31d9SMark McLoughlin 906296f96fcSRusty Russell /* We expect two virtqueues, receive then send. */ 907a586d4f6SRusty Russell vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); 908296f96fcSRusty Russell if (IS_ERR(vi->rvq)) { 909296f96fcSRusty Russell err = PTR_ERR(vi->rvq); 910296f96fcSRusty Russell goto free; 911296f96fcSRusty Russell } 912296f96fcSRusty Russell 913a586d4f6SRusty Russell vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); 914296f96fcSRusty Russell if (IS_ERR(vi->svq)) { 915296f96fcSRusty Russell err = PTR_ERR(vi->svq); 916296f96fcSRusty Russell goto free_recv; 917296f96fcSRusty Russell } 918296f96fcSRusty Russell 9192a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { 9202a41f71dSAlex Williamson vi->cvq = vdev->config->find_vq(vdev, 2, NULL); 9212a41f71dSAlex Williamson if (IS_ERR(vi->cvq)) { 9222a41f71dSAlex Williamson err = PTR_ERR(vi->svq); 9232a41f71dSAlex Williamson goto free_send; 9242a41f71dSAlex Williamson } 9250bde9569SAlex Williamson 9260bde9569SAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) 9270bde9569SAlex Williamson dev->features |= NETIF_F_HW_VLAN_FILTER; 9282a41f71dSAlex Williamson } 9292a41f71dSAlex Williamson 930296f96fcSRusty Russell /* Initialize our empty receive and send queues. */ 931296f96fcSRusty Russell skb_queue_head_init(&vi->recv); 932296f96fcSRusty Russell skb_queue_head_init(&vi->send); 933296f96fcSRusty Russell 93411a3a154SRusty Russell tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); 93511a3a154SRusty Russell 936363f1514SRusty Russell if (!vi->free_in_tasklet) 93714c998f0SMark McLoughlin setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); 93814c998f0SMark McLoughlin 939296f96fcSRusty Russell err = register_netdev(dev); 940296f96fcSRusty Russell if (err) { 941296f96fcSRusty Russell pr_debug("virtio_net: registering device failed\n"); 9422a41f71dSAlex Williamson goto free_ctrl; 943296f96fcSRusty Russell } 944b3369c1fSRusty Russell 945b3369c1fSRusty Russell /* Last of all, set up some receive buffers. */ 946b3369c1fSRusty Russell try_fill_recv(vi); 947b3369c1fSRusty Russell 948b3369c1fSRusty Russell /* If we didn't even get one input buffer, we're useless. */ 949b3369c1fSRusty Russell if (vi->num == 0) { 950b3369c1fSRusty Russell err = -ENOMEM; 951b3369c1fSRusty Russell goto unregister; 952b3369c1fSRusty Russell } 953b3369c1fSRusty Russell 9549f4d26d0SMark McLoughlin vi->status = VIRTIO_NET_S_LINK_UP; 9559f4d26d0SMark McLoughlin virtnet_update_status(vi); 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); 9622a41f71dSAlex Williamson free_ctrl: 9632a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9642a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 965296f96fcSRusty Russell free_send: 966296f96fcSRusty Russell vdev->config->del_vq(vi->svq); 967296f96fcSRusty Russell free_recv: 968296f96fcSRusty Russell vdev->config->del_vq(vi->rvq); 969296f96fcSRusty Russell free: 970296f96fcSRusty Russell free_netdev(dev); 971296f96fcSRusty Russell return err; 972296f96fcSRusty Russell } 973296f96fcSRusty Russell 974296f96fcSRusty Russell static void virtnet_remove(struct virtio_device *vdev) 975296f96fcSRusty Russell { 97674b2553fSRusty Russell struct virtnet_info *vi = vdev->priv; 977b3369c1fSRusty Russell struct sk_buff *skb; 978b3369c1fSRusty Russell 9796e5aa7efSRusty Russell /* Stop all the virtqueues. */ 9806e5aa7efSRusty Russell vdev->config->reset(vdev); 9816e5aa7efSRusty Russell 982363f1514SRusty Russell if (!vi->free_in_tasklet) 98314c998f0SMark McLoughlin del_timer_sync(&vi->xmit_free_timer); 98414c998f0SMark McLoughlin 985b3369c1fSRusty Russell /* Free our skbs in send and recv queues, if any. */ 986b3369c1fSRusty Russell while ((skb = __skb_dequeue(&vi->recv)) != NULL) { 987b3369c1fSRusty Russell kfree_skb(skb); 988b3369c1fSRusty Russell vi->num--; 989b3369c1fSRusty Russell } 990288369ccSWang Chen __skb_queue_purge(&vi->send); 991b3369c1fSRusty Russell 992b3369c1fSRusty Russell BUG_ON(vi->num != 0); 99374b2553fSRusty Russell 99474b2553fSRusty Russell vdev->config->del_vq(vi->svq); 99574b2553fSRusty Russell vdev->config->del_vq(vi->rvq); 9962a41f71dSAlex Williamson if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) 9972a41f71dSAlex Williamson vdev->config->del_vq(vi->cvq); 99874b2553fSRusty Russell unregister_netdev(vi->dev); 999fb6813f4SRusty Russell 1000fb6813f4SRusty Russell while (vi->pages) 1001fb6813f4SRusty Russell __free_pages(get_a_page(vi, GFP_KERNEL), 0); 1002fb6813f4SRusty Russell 100374b2553fSRusty Russell free_netdev(vi->dev); 1004296f96fcSRusty Russell } 1005296f96fcSRusty Russell 1006296f96fcSRusty Russell static struct virtio_device_id id_table[] = { 1007296f96fcSRusty Russell { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, 1008296f96fcSRusty Russell { 0 }, 1009296f96fcSRusty Russell }; 1010296f96fcSRusty Russell 1011c45a6816SRusty Russell static unsigned int features[] = { 10125e4fe5c4SMark McLoughlin VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, 10135e4fe5c4SMark McLoughlin VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, 1014c45a6816SRusty Russell VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, 101597402b96SHerbert Xu VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, 101697402b96SHerbert Xu VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */ 10172a41f71dSAlex Williamson VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, 10180bde9569SAlex Williamson VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, 101997402b96SHerbert Xu VIRTIO_F_NOTIFY_ON_EMPTY, 1020c45a6816SRusty Russell }; 1021c45a6816SRusty Russell 1022296f96fcSRusty Russell static struct virtio_driver virtio_net = { 1023c45a6816SRusty Russell .feature_table = features, 1024c45a6816SRusty Russell .feature_table_size = ARRAY_SIZE(features), 1025296f96fcSRusty Russell .driver.name = KBUILD_MODNAME, 1026296f96fcSRusty Russell .driver.owner = THIS_MODULE, 1027296f96fcSRusty Russell .id_table = id_table, 1028296f96fcSRusty Russell .probe = virtnet_probe, 1029296f96fcSRusty Russell .remove = __devexit_p(virtnet_remove), 10309f4d26d0SMark McLoughlin .config_changed = virtnet_config_changed, 1031296f96fcSRusty Russell }; 1032296f96fcSRusty Russell 1033296f96fcSRusty Russell static int __init init(void) 1034296f96fcSRusty Russell { 1035296f96fcSRusty Russell return register_virtio_driver(&virtio_net); 1036296f96fcSRusty Russell } 1037296f96fcSRusty Russell 1038296f96fcSRusty Russell static void __exit fini(void) 1039296f96fcSRusty Russell { 1040296f96fcSRusty Russell unregister_virtio_driver(&virtio_net); 1041296f96fcSRusty Russell } 1042296f96fcSRusty Russell module_init(init); 1043296f96fcSRusty Russell module_exit(fini); 1044296f96fcSRusty Russell 1045296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table); 1046296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver"); 1047296f96fcSRusty Russell MODULE_LICENSE("GPL"); 1048