Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 1 | /* A network driver using virtio. |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | //#define DEBUG |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/etherdevice.h> |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 22 | #include <linux/ethtool.h> |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/virtio.h> |
| 25 | #include <linux/virtio_net.h> |
| 26 | #include <linux/scatterlist.h> |
Alex Williamson | e918085a | 2009-01-25 18:06:26 -0800 | [diff] [blame] | 27 | #include <linux/if_vlan.h> |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 28 | |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 29 | static int napi_weight = 128; |
| 30 | module_param(napi_weight, int, 0444); |
| 31 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 32 | static int csum = 1, gso = 1; |
| 33 | module_param(csum, bool, 0444); |
| 34 | module_param(gso, bool, 0444); |
| 35 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 36 | /* FIXME: MTU in config. */ |
Alex Williamson | e918085a | 2009-01-25 18:06:26 -0800 | [diff] [blame] | 37 | #define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN) |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 38 | #define GOOD_COPY_LEN 128 |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 39 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 40 | #define VIRTNET_SEND_COMMAND_SG_MAX 2 |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 41 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 42 | struct virtnet_info |
| 43 | { |
| 44 | struct virtio_device *vdev; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 45 | struct virtqueue *rvq, *svq, *cvq; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 46 | struct net_device *dev; |
| 47 | struct napi_struct napi; |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 48 | unsigned int status; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 49 | |
| 50 | /* Number of input buffers, and max we've ever had. */ |
| 51 | unsigned int num, max; |
| 52 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 53 | /* I like... big packets and I cannot lie! */ |
| 54 | bool big_packets; |
| 55 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 56 | /* Host will merge rx buffers for big packets (shake it! shake it!) */ |
| 57 | bool mergeable_rx_bufs; |
| 58 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 59 | /* Receive & send queues. */ |
| 60 | struct sk_buff_head recv; |
| 61 | struct sk_buff_head send; |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 62 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 63 | /* Work struct for refilling if we run low on memory. */ |
| 64 | struct delayed_work refill; |
| 65 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 66 | /* Chain pages by the private ptr. */ |
| 67 | struct page *pages; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 68 | }; |
| 69 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 70 | struct skb_vnet_hdr { |
| 71 | union { |
| 72 | struct virtio_net_hdr hdr; |
| 73 | struct virtio_net_hdr_mrg_rxbuf mhdr; |
| 74 | }; |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 75 | unsigned int num_sg; |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 79 | { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 80 | return (struct skb_vnet_hdr *)skb->cb; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 81 | } |
| 82 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 83 | static void give_a_page(struct virtnet_info *vi, struct page *page) |
| 84 | { |
| 85 | page->private = (unsigned long)vi->pages; |
| 86 | vi->pages = page; |
| 87 | } |
| 88 | |
Mark McLoughlin | 0a888fd | 2008-11-16 22:39:18 -0800 | [diff] [blame] | 89 | static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb) |
| 90 | { |
| 91 | unsigned int i; |
| 92 | |
| 93 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 94 | give_a_page(vi, skb_shinfo(skb)->frags[i].page); |
| 95 | skb_shinfo(skb)->nr_frags = 0; |
| 96 | skb->data_len = 0; |
| 97 | } |
| 98 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 99 | static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask) |
| 100 | { |
| 101 | struct page *p = vi->pages; |
| 102 | |
| 103 | if (p) |
| 104 | vi->pages = (struct page *)p->private; |
| 105 | else |
| 106 | p = alloc_page(gfp_mask); |
| 107 | return p; |
| 108 | } |
| 109 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 110 | static void skb_xmit_done(struct virtqueue *svq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 111 | { |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 112 | struct virtnet_info *vi = svq->vdev->priv; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 113 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 114 | /* Suppress further interrupts. */ |
| 115 | svq->vq_ops->disable_cb(svq); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 116 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 117 | /* We were probably waiting for more output buffers. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 118 | netif_wake_queue(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, |
| 122 | unsigned len) |
| 123 | { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 124 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 125 | struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 126 | int err; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 127 | int i; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 128 | |
| 129 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { |
| 130 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 131 | dev->stats.rx_length_errors++; |
| 132 | goto drop; |
| 133 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 134 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 135 | if (vi->mergeable_rx_bufs) { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 136 | unsigned int copy; |
| 137 | char *p = page_address(skb_shinfo(skb)->frags[0].page); |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 138 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 139 | if (len > PAGE_SIZE) |
| 140 | len = PAGE_SIZE; |
| 141 | len -= sizeof(struct virtio_net_hdr_mrg_rxbuf); |
| 142 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 143 | memcpy(&hdr->mhdr, p, sizeof(hdr->mhdr)); |
| 144 | p += sizeof(hdr->mhdr); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 145 | |
| 146 | copy = len; |
| 147 | if (copy > skb_tailroom(skb)) |
| 148 | copy = skb_tailroom(skb); |
| 149 | |
| 150 | memcpy(skb_put(skb, copy), p, copy); |
| 151 | |
| 152 | len -= copy; |
| 153 | |
| 154 | if (!len) { |
| 155 | give_a_page(vi, skb_shinfo(skb)->frags[0].page); |
| 156 | skb_shinfo(skb)->nr_frags--; |
| 157 | } else { |
| 158 | skb_shinfo(skb)->frags[0].page_offset += |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 159 | sizeof(hdr->mhdr) + copy; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 160 | skb_shinfo(skb)->frags[0].size = len; |
| 161 | skb->data_len += len; |
| 162 | skb->len += len; |
| 163 | } |
| 164 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 165 | while (--hdr->mhdr.num_buffers) { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 166 | struct sk_buff *nskb; |
| 167 | |
| 168 | i = skb_shinfo(skb)->nr_frags; |
| 169 | if (i >= MAX_SKB_FRAGS) { |
| 170 | pr_debug("%s: packet too long %d\n", dev->name, |
| 171 | len); |
| 172 | dev->stats.rx_length_errors++; |
| 173 | goto drop; |
| 174 | } |
| 175 | |
| 176 | nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len); |
| 177 | if (!nskb) { |
| 178 | pr_debug("%s: rx error: %d buffers missing\n", |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 179 | dev->name, hdr->mhdr.num_buffers); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 180 | dev->stats.rx_length_errors++; |
| 181 | goto drop; |
| 182 | } |
| 183 | |
| 184 | __skb_unlink(nskb, &vi->recv); |
| 185 | vi->num--; |
| 186 | |
| 187 | skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0]; |
| 188 | skb_shinfo(nskb)->nr_frags = 0; |
| 189 | kfree_skb(nskb); |
| 190 | |
| 191 | if (len > PAGE_SIZE) |
| 192 | len = PAGE_SIZE; |
| 193 | |
| 194 | skb_shinfo(skb)->frags[i].size = len; |
| 195 | skb_shinfo(skb)->nr_frags++; |
| 196 | skb->data_len += len; |
| 197 | skb->len += len; |
| 198 | } |
| 199 | } else { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 200 | len -= sizeof(hdr->hdr); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 201 | |
| 202 | if (len <= MAX_PACKET_LEN) |
| 203 | trim_pages(vi, skb); |
| 204 | |
| 205 | err = pskb_trim(skb, len); |
| 206 | if (err) { |
| 207 | pr_debug("%s: pskb_trim failed %i %d\n", dev->name, |
| 208 | len, err); |
| 209 | dev->stats.rx_dropped++; |
| 210 | goto drop; |
| 211 | } |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 212 | } |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 213 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 214 | skb->truesize += skb->data_len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 215 | dev->stats.rx_bytes += skb->len; |
| 216 | dev->stats.rx_packets++; |
| 217 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 218 | if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 219 | pr_debug("Needs csum!\n"); |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 220 | if (!skb_partial_csum_set(skb, |
| 221 | hdr->hdr.csum_start, |
| 222 | hdr->hdr.csum_offset)) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 223 | goto frame_err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 224 | } |
| 225 | |
Mark McLoughlin | 23cde76 | 2008-06-08 20:49:00 +1000 | [diff] [blame] | 226 | skb->protocol = eth_type_trans(skb, dev); |
| 227 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 228 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 229 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 230 | if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 231 | pr_debug("GSO!\n"); |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 232 | switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 233 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 234 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 235 | break; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 236 | case VIRTIO_NET_HDR_GSO_UDP: |
| 237 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 238 | break; |
| 239 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 240 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 241 | break; |
| 242 | default: |
| 243 | if (net_ratelimit()) |
| 244 | printk(KERN_WARNING "%s: bad gso type %u.\n", |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 245 | dev->name, hdr->hdr.gso_type); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 246 | goto frame_err; |
| 247 | } |
| 248 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 249 | if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN) |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 250 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 251 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 252 | skb_shinfo(skb)->gso_size = hdr->hdr.gso_size; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 253 | if (skb_shinfo(skb)->gso_size == 0) { |
| 254 | if (net_ratelimit()) |
| 255 | printk(KERN_WARNING "%s: zero gso size.\n", |
| 256 | dev->name); |
| 257 | goto frame_err; |
| 258 | } |
| 259 | |
| 260 | /* Header must be checked, and gso_segs computed. */ |
| 261 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 262 | skb_shinfo(skb)->gso_segs = 0; |
| 263 | } |
| 264 | |
| 265 | netif_receive_skb(skb); |
| 266 | return; |
| 267 | |
| 268 | frame_err: |
| 269 | dev->stats.rx_frame_errors++; |
| 270 | drop: |
| 271 | dev_kfree_skb(skb); |
| 272 | } |
| 273 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 274 | static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 275 | { |
| 276 | struct sk_buff *skb; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 277 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 278 | int num, err, i; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 279 | bool oom = false; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 280 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 281 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Amit Shah | 0aea51c | 2009-08-26 14:58:28 +0530 | [diff] [blame] | 282 | do { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 283 | struct skb_vnet_hdr *hdr; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 284 | |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 285 | skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 286 | if (unlikely(!skb)) { |
| 287 | oom = true; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 288 | break; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 289 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 290 | |
| 291 | skb_put(skb, MAX_PACKET_LEN); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 292 | |
| 293 | hdr = skb_vnet_hdr(skb); |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 294 | sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr)); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 295 | |
| 296 | if (vi->big_packets) { |
| 297 | for (i = 0; i < MAX_SKB_FRAGS; i++) { |
| 298 | skb_frag_t *f = &skb_shinfo(skb)->frags[i]; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 299 | f->page = get_a_page(vi, gfp); |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 300 | if (!f->page) |
| 301 | break; |
| 302 | |
| 303 | f->page_offset = 0; |
| 304 | f->size = PAGE_SIZE; |
| 305 | |
| 306 | skb->data_len += PAGE_SIZE; |
| 307 | skb->len += PAGE_SIZE; |
| 308 | |
| 309 | skb_shinfo(skb)->nr_frags++; |
| 310 | } |
| 311 | } |
| 312 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 313 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 314 | skb_queue_head(&vi->recv, skb); |
| 315 | |
| 316 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 317 | if (err < 0) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 318 | skb_unlink(skb, &vi->recv); |
Mark McLoughlin | 0a888fd | 2008-11-16 22:39:18 -0800 | [diff] [blame] | 319 | trim_pages(vi, skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 320 | kfree_skb(skb); |
| 321 | break; |
| 322 | } |
| 323 | vi->num++; |
Amit Shah | 0aea51c | 2009-08-26 14:58:28 +0530 | [diff] [blame] | 324 | } while (err >= num); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 325 | if (unlikely(vi->num > vi->max)) |
| 326 | vi->max = vi->num; |
| 327 | vi->rvq->vq_ops->kick(vi->rvq); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 328 | return !oom; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 329 | } |
| 330 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 331 | /* Returns false if we couldn't fill entirely (OOM). */ |
| 332 | static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 333 | { |
| 334 | struct sk_buff *skb; |
| 335 | struct scatterlist sg[1]; |
| 336 | int err; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 337 | bool oom = false; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 338 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 339 | if (!vi->mergeable_rx_bufs) |
| 340 | return try_fill_recv_maxbufs(vi, gfp); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 341 | |
Amit Shah | 0aea51c | 2009-08-26 14:58:28 +0530 | [diff] [blame] | 342 | do { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 343 | skb_frag_t *f; |
| 344 | |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 345 | skb = netdev_alloc_skb_ip_align(vi->dev, GOOD_COPY_LEN); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 346 | if (unlikely(!skb)) { |
| 347 | oom = true; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 348 | break; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 349 | } |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 350 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 351 | f = &skb_shinfo(skb)->frags[0]; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 352 | f->page = get_a_page(vi, gfp); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 353 | if (!f->page) { |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 354 | oom = true; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 355 | kfree_skb(skb); |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | f->page_offset = 0; |
| 360 | f->size = PAGE_SIZE; |
| 361 | |
| 362 | skb_shinfo(skb)->nr_frags++; |
| 363 | |
| 364 | sg_init_one(sg, page_address(f->page), PAGE_SIZE); |
| 365 | skb_queue_head(&vi->recv, skb); |
| 366 | |
| 367 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 368 | if (err < 0) { |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 369 | skb_unlink(skb, &vi->recv); |
| 370 | kfree_skb(skb); |
| 371 | break; |
| 372 | } |
| 373 | vi->num++; |
Amit Shah | 0aea51c | 2009-08-26 14:58:28 +0530 | [diff] [blame] | 374 | } while (err > 0); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 375 | if (unlikely(vi->num > vi->max)) |
| 376 | vi->max = vi->num; |
| 377 | vi->rvq->vq_ops->kick(vi->rvq); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 378 | return !oom; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 381 | static void skb_recv_done(struct virtqueue *rvq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 382 | { |
| 383 | struct virtnet_info *vi = rvq->vdev->priv; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 384 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 385 | if (napi_schedule_prep(&vi->napi)) { |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 386 | rvq->vq_ops->disable_cb(rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 387 | __napi_schedule(&vi->napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 388 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 389 | } |
| 390 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 391 | static void refill_work(struct work_struct *work) |
| 392 | { |
| 393 | struct virtnet_info *vi; |
| 394 | bool still_empty; |
| 395 | |
| 396 | vi = container_of(work, struct virtnet_info, refill.work); |
| 397 | napi_disable(&vi->napi); |
| 398 | try_fill_recv(vi, GFP_KERNEL); |
| 399 | still_empty = (vi->num == 0); |
| 400 | napi_enable(&vi->napi); |
| 401 | |
| 402 | /* In theory, this can happen: if we don't get any buffers in |
| 403 | * we will *never* try to fill again. */ |
| 404 | if (still_empty) |
| 405 | schedule_delayed_work(&vi->refill, HZ/2); |
| 406 | } |
| 407 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 408 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 409 | { |
| 410 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); |
| 411 | struct sk_buff *skb = NULL; |
| 412 | unsigned int len, received = 0; |
| 413 | |
| 414 | again: |
| 415 | while (received < budget && |
| 416 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { |
| 417 | __skb_unlink(skb, &vi->recv); |
| 418 | receive_skb(vi->dev, skb, len); |
| 419 | vi->num--; |
| 420 | received++; |
| 421 | } |
| 422 | |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 423 | if (vi->num < vi->max / 2) { |
| 424 | if (!try_fill_recv(vi, GFP_ATOMIC)) |
| 425 | schedule_delayed_work(&vi->refill, 0); |
| 426 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 427 | |
Rusty Russell | 8329d98 | 2007-11-19 11:20:43 -0500 | [diff] [blame] | 428 | /* Out of packets? */ |
| 429 | if (received < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 430 | napi_complete(napi); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 431 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) && |
| 432 | napi_schedule_prep(napi)) { |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 433 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 434 | __napi_schedule(napi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 435 | goto again; |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 436 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | return received; |
| 440 | } |
| 441 | |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 442 | static unsigned int free_old_xmit_skbs(struct virtnet_info *vi) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 443 | { |
| 444 | struct sk_buff *skb; |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 445 | unsigned int len, tot_sgs = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 446 | |
| 447 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { |
| 448 | pr_debug("Sent skb %p\n", skb); |
| 449 | __skb_unlink(skb, &vi->send); |
Rusty Russell | 655aa31 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 450 | vi->dev->stats.tx_bytes += skb->len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 451 | vi->dev->stats.tx_packets++; |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 452 | tot_sgs += skb_vnet_hdr(skb)->num_sg; |
Eric Dumazet | ed79bab | 2009-10-14 14:36:43 +0000 | [diff] [blame] | 453 | dev_kfree_skb_any(skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 454 | } |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 455 | return tot_sgs; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 456 | } |
| 457 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 458 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 459 | { |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 460 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 461 | struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 462 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 463 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 464 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 465 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 466 | pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 467 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 468 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 469 | hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 470 | hdr->hdr.csum_start = skb->csum_start - skb_headroom(skb); |
| 471 | hdr->hdr.csum_offset = skb->csum_offset; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 472 | } else { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 473 | hdr->hdr.flags = 0; |
| 474 | hdr->hdr.csum_offset = hdr->hdr.csum_start = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if (skb_is_gso(skb)) { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 478 | hdr->hdr.hdr_len = skb_headlen(skb); |
| 479 | hdr->hdr.gso_size = skb_shinfo(skb)->gso_size; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 480 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 481 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 482 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 483 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 484 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 485 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 486 | else |
| 487 | BUG(); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 488 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 489 | hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 490 | } else { |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 491 | hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE; |
| 492 | hdr->hdr.gso_size = hdr->hdr.hdr_len = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 493 | } |
| 494 | |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 495 | hdr->mhdr.num_buffers = 0; |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 496 | |
| 497 | /* Encode metadata header at front. */ |
| 498 | if (vi->mergeable_rx_bufs) |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 499 | sg_set_buf(sg, &hdr->mhdr, sizeof(hdr->mhdr)); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 500 | else |
Rusty Russell | b3f2469 | 2009-09-24 09:59:19 -0600 | [diff] [blame] | 501 | sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr)); |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 502 | |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 503 | hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 504 | return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 505 | } |
| 506 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 507 | static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev) |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 508 | { |
| 509 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 510 | int capacity; |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 511 | |
| 512 | again: |
| 513 | /* Free up any pending old buffers before queueing new ones. */ |
| 514 | free_old_xmit_skbs(vi); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 515 | |
Michael S. Tsirkin | 03f191b | 2009-10-28 04:03:38 -0700 | [diff] [blame] | 516 | /* Try to transmit */ |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 517 | capacity = xmit_skb(vi, skb); |
| 518 | |
| 519 | /* This can happen with OOM and indirect buffers. */ |
| 520 | if (unlikely(capacity < 0)) { |
| 521 | netif_stop_queue(dev); |
| 522 | dev_warn(&dev->dev, "Unexpected full queue\n"); |
| 523 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
| 524 | vi->svq->vq_ops->disable_cb(vi->svq); |
| 525 | netif_start_queue(dev); |
| 526 | goto again; |
| 527 | } |
| 528 | return NETDEV_TX_BUSY; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 529 | } |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 530 | vi->svq->vq_ops->kick(vi->svq); |
Michael S. Tsirkin | 03f191b | 2009-10-28 04:03:38 -0700 | [diff] [blame] | 531 | |
| 532 | /* |
| 533 | * Put new one in send queue. You'd expect we'd need this before |
| 534 | * xmit_skb calls add_buf(), since the callback can be triggered |
| 535 | * immediately after that. But since the callback just triggers |
| 536 | * another call back here, normal network xmit locking prevents the |
| 537 | * race. |
| 538 | */ |
| 539 | __skb_queue_head(&vi->send, skb); |
| 540 | |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 541 | /* Don't wait up for transmitted skbs to be freed. */ |
| 542 | skb_orphan(skb); |
| 543 | nf_reset(skb); |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 544 | |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 545 | /* Apparently nice girls don't return TX_BUSY; stop the queue |
| 546 | * before it gets out of hand. Naturally, this wastes entries. */ |
| 547 | if (capacity < 2+MAX_SKB_FRAGS) { |
| 548 | netif_stop_queue(dev); |
| 549 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
| 550 | /* More just got used, free them then recheck. */ |
| 551 | capacity += free_old_xmit_skbs(vi); |
| 552 | if (capacity >= 2+MAX_SKB_FRAGS) { |
| 553 | netif_start_queue(dev); |
| 554 | vi->svq->vq_ops->disable_cb(vi->svq); |
| 555 | } |
| 556 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 557 | } |
Rusty Russell | 48925e3 | 2009-09-24 09:59:20 -0600 | [diff] [blame] | 558 | |
| 559 | return NETDEV_TX_OK; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 560 | } |
| 561 | |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 562 | static int virtnet_set_mac_address(struct net_device *dev, void *p) |
| 563 | { |
| 564 | struct virtnet_info *vi = netdev_priv(dev); |
| 565 | struct virtio_device *vdev = vi->vdev; |
| 566 | int ret; |
| 567 | |
| 568 | ret = eth_mac_addr(dev, p); |
| 569 | if (ret) |
| 570 | return ret; |
| 571 | |
Alex Williamson | 62994b2 | 2009-04-04 16:40:19 -0700 | [diff] [blame] | 572 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) |
| 573 | vdev->config->set(vdev, offsetof(struct virtio_net_config, mac), |
| 574 | dev->dev_addr, dev->addr_len); |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 579 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 580 | static void virtnet_netpoll(struct net_device *dev) |
| 581 | { |
| 582 | struct virtnet_info *vi = netdev_priv(dev); |
| 583 | |
| 584 | napi_schedule(&vi->napi); |
| 585 | } |
| 586 | #endif |
| 587 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 588 | static int virtnet_open(struct net_device *dev) |
| 589 | { |
| 590 | struct virtnet_info *vi = netdev_priv(dev); |
| 591 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 592 | napi_enable(&vi->napi); |
Rusty Russell | a48bd8f | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 593 | |
| 594 | /* If all buffers were filled by other side before we napi_enabled, we |
| 595 | * won't get another interrupt, so process any outstanding packets |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 596 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
| 597 | * We synchronize against interrupts via NAPI_STATE_SCHED */ |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 598 | if (napi_schedule_prep(&vi->napi)) { |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 599 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 600 | __napi_schedule(&vi->napi); |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 601 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 605 | /* |
| 606 | * Send command via the control virtqueue and check status. Commands |
| 607 | * supported by the hypervisor, as indicated by feature bits, should |
| 608 | * never fail unless improperly formated. |
| 609 | */ |
| 610 | static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, |
| 611 | struct scatterlist *data, int out, int in) |
| 612 | { |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 613 | struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2]; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 614 | struct virtio_net_ctrl_hdr ctrl; |
| 615 | virtio_net_ctrl_ack status = ~0; |
| 616 | unsigned int tmp; |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 617 | int i; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 618 | |
Alexander Beregalov | 0ee904c | 2009-04-11 14:50:23 +0000 | [diff] [blame] | 619 | /* Caller should know better */ |
| 620 | BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) || |
| 621 | (out + in > VIRTNET_SEND_COMMAND_SG_MAX)); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 622 | |
| 623 | out++; /* Add header */ |
| 624 | in++; /* Add return status */ |
| 625 | |
| 626 | ctrl.class = class; |
| 627 | ctrl.cmd = cmd; |
| 628 | |
| 629 | sg_init_table(sg, out + in); |
| 630 | |
| 631 | sg_set_buf(&sg[0], &ctrl, sizeof(ctrl)); |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 632 | for_each_sg(data, s, out + in - 2, i) |
| 633 | sg_set_buf(&sg[i + 1], sg_virt(s), s->length); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 634 | sg_set_buf(&sg[out + in - 1], &status, sizeof(status)); |
| 635 | |
Rusty Russell | 3c1b27d | 2009-09-23 22:26:31 -0600 | [diff] [blame] | 636 | BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0); |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 637 | |
| 638 | vi->cvq->vq_ops->kick(vi->cvq); |
| 639 | |
| 640 | /* |
| 641 | * Spin for a response, the kick causes an ioport write, trapping |
| 642 | * into the hypervisor, so the request should be handled immediately. |
| 643 | */ |
| 644 | while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp)) |
| 645 | cpu_relax(); |
| 646 | |
| 647 | return status == VIRTIO_NET_OK; |
| 648 | } |
| 649 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 650 | static int virtnet_close(struct net_device *dev) |
| 651 | { |
| 652 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 653 | |
| 654 | napi_disable(&vi->napi); |
| 655 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 659 | static int virtnet_set_tx_csum(struct net_device *dev, u32 data) |
| 660 | { |
| 661 | struct virtnet_info *vi = netdev_priv(dev); |
| 662 | struct virtio_device *vdev = vi->vdev; |
| 663 | |
| 664 | if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) |
| 665 | return -ENOSYS; |
| 666 | |
| 667 | return ethtool_op_set_tx_hw_csum(dev, data); |
| 668 | } |
| 669 | |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 670 | static void virtnet_set_rx_mode(struct net_device *dev) |
| 671 | { |
| 672 | struct virtnet_info *vi = netdev_priv(dev); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 673 | struct scatterlist sg[2]; |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 674 | u8 promisc, allmulti; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 675 | struct virtio_net_ctrl_mac *mac_data; |
| 676 | struct dev_addr_list *addr; |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 677 | struct netdev_hw_addr *ha; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 678 | void *buf; |
| 679 | int i; |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 680 | |
| 681 | /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */ |
| 682 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) |
| 683 | return; |
| 684 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 685 | promisc = ((dev->flags & IFF_PROMISC) != 0); |
| 686 | allmulti = ((dev->flags & IFF_ALLMULTI) != 0); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 687 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 688 | sg_init_one(sg, &promisc, sizeof(promisc)); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 689 | |
| 690 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 691 | VIRTIO_NET_CTRL_RX_PROMISC, |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 692 | sg, 1, 0)) |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 693 | dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", |
| 694 | promisc ? "en" : "dis"); |
| 695 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 696 | sg_init_one(sg, &allmulti, sizeof(allmulti)); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 697 | |
| 698 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, |
| 699 | VIRTIO_NET_CTRL_RX_ALLMULTI, |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 700 | sg, 1, 0)) |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 701 | dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", |
| 702 | allmulti ? "en" : "dis"); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 703 | |
| 704 | /* MAC filter - use one buffer for both lists */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 705 | mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) + |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 706 | (2 * sizeof(mac_data->entries)), GFP_ATOMIC); |
| 707 | if (!buf) { |
| 708 | dev_warn(&dev->dev, "No memory for MAC address buffer\n"); |
| 709 | return; |
| 710 | } |
| 711 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 712 | sg_init_table(sg, 2); |
| 713 | |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 714 | /* Store the unicast list and count in the front of the buffer */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 715 | mac_data->entries = dev->uc.count; |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 716 | i = 0; |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 717 | list_for_each_entry(ha, &dev->uc.list, list) |
Jiri Pirko | ccffad2 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 718 | memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 719 | |
| 720 | sg_set_buf(&sg[0], mac_data, |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 721 | sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN)); |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 722 | |
| 723 | /* multicast list and count fill the end */ |
Jiri Pirko | 31278e7 | 2009-06-17 01:12:19 +0000 | [diff] [blame] | 724 | mac_data = (void *)&mac_data->macs[dev->uc.count][0]; |
Alex Williamson | f565a7c | 2009-02-04 09:02:45 +0000 | [diff] [blame] | 725 | |
| 726 | mac_data->entries = dev->mc_count; |
| 727 | addr = dev->mc_list; |
| 728 | for (i = 0; i < dev->mc_count; i++, addr = addr->next) |
| 729 | memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN); |
| 730 | |
| 731 | sg_set_buf(&sg[1], mac_data, |
| 732 | sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN)); |
| 733 | |
| 734 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC, |
| 735 | VIRTIO_NET_CTRL_MAC_TABLE_SET, |
| 736 | sg, 2, 0)) |
| 737 | dev_warn(&dev->dev, "Failed to set MAC fitler table.\n"); |
| 738 | |
| 739 | kfree(buf); |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 742 | static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid) |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 743 | { |
| 744 | struct virtnet_info *vi = netdev_priv(dev); |
| 745 | struct scatterlist sg; |
| 746 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 747 | sg_init_one(&sg, &vid, sizeof(vid)); |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 748 | |
| 749 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 750 | VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0)) |
| 751 | dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid); |
| 752 | } |
| 753 | |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 754 | static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid) |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 755 | { |
| 756 | struct virtnet_info *vi = netdev_priv(dev); |
| 757 | struct scatterlist sg; |
| 758 | |
Alex Williamson | 23e258e | 2009-05-01 17:27:56 +0000 | [diff] [blame] | 759 | sg_init_one(&sg, &vid, sizeof(vid)); |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 760 | |
| 761 | if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, |
| 762 | VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0)) |
| 763 | dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid); |
| 764 | } |
| 765 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 766 | static const struct ethtool_ops virtnet_ethtool_ops = { |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 767 | .set_tx_csum = virtnet_set_tx_csum, |
| 768 | .set_sg = ethtool_op_set_sg, |
Mark McLoughlin | 0276b49 | 2008-11-16 22:40:36 -0800 | [diff] [blame] | 769 | .set_tso = ethtool_op_set_tso, |
Sridhar Samudrala | 5c51675 | 2009-07-14 14:21:02 +0000 | [diff] [blame] | 770 | .set_ufo = ethtool_op_set_ufo, |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 771 | .get_link = ethtool_op_get_link, |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 772 | }; |
| 773 | |
Mark McLoughlin | 39da581 | 2008-11-26 13:58:11 +0000 | [diff] [blame] | 774 | #define MIN_MTU 68 |
| 775 | #define MAX_MTU 65535 |
| 776 | |
| 777 | static int virtnet_change_mtu(struct net_device *dev, int new_mtu) |
| 778 | { |
| 779 | if (new_mtu < MIN_MTU || new_mtu > MAX_MTU) |
| 780 | return -EINVAL; |
| 781 | dev->mtu = new_mtu; |
| 782 | return 0; |
| 783 | } |
| 784 | |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 785 | static const struct net_device_ops virtnet_netdev = { |
| 786 | .ndo_open = virtnet_open, |
| 787 | .ndo_stop = virtnet_close, |
| 788 | .ndo_start_xmit = start_xmit, |
| 789 | .ndo_validate_addr = eth_validate_addr, |
Alex Williamson | 9c46f6d | 2009-02-04 16:36:34 -0800 | [diff] [blame] | 790 | .ndo_set_mac_address = virtnet_set_mac_address, |
Alex Williamson | 2af7698 | 2009-02-04 09:02:40 +0000 | [diff] [blame] | 791 | .ndo_set_rx_mode = virtnet_set_rx_mode, |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 792 | .ndo_change_mtu = virtnet_change_mtu, |
Alex Williamson | 1824a98 | 2009-05-01 17:31:10 +0000 | [diff] [blame] | 793 | .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid, |
| 794 | .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid, |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 795 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 796 | .ndo_poll_controller = virtnet_netpoll, |
| 797 | #endif |
| 798 | }; |
| 799 | |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 800 | static void virtnet_update_status(struct virtnet_info *vi) |
| 801 | { |
| 802 | u16 v; |
| 803 | |
| 804 | if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) |
| 805 | return; |
| 806 | |
| 807 | vi->vdev->config->get(vi->vdev, |
| 808 | offsetof(struct virtio_net_config, status), |
| 809 | &v, sizeof(v)); |
| 810 | |
| 811 | /* Ignore unknown (future) status bits */ |
| 812 | v &= VIRTIO_NET_S_LINK_UP; |
| 813 | |
| 814 | if (vi->status == v) |
| 815 | return; |
| 816 | |
| 817 | vi->status = v; |
| 818 | |
| 819 | if (vi->status & VIRTIO_NET_S_LINK_UP) { |
| 820 | netif_carrier_on(vi->dev); |
| 821 | netif_wake_queue(vi->dev); |
| 822 | } else { |
| 823 | netif_carrier_off(vi->dev); |
| 824 | netif_stop_queue(vi->dev); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | static void virtnet_config_changed(struct virtio_device *vdev) |
| 829 | { |
| 830 | struct virtnet_info *vi = vdev->priv; |
| 831 | |
| 832 | virtnet_update_status(vi); |
| 833 | } |
| 834 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 835 | static int virtnet_probe(struct virtio_device *vdev) |
| 836 | { |
| 837 | int err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 838 | struct net_device *dev; |
| 839 | struct virtnet_info *vi; |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 840 | struct virtqueue *vqs[3]; |
| 841 | vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL}; |
| 842 | const char *names[] = { "input", "output", "control" }; |
| 843 | int nvqs; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 844 | |
| 845 | /* Allocate ourselves a network device with room for our info */ |
| 846 | dev = alloc_etherdev(sizeof(struct virtnet_info)); |
| 847 | if (!dev) |
| 848 | return -ENOMEM; |
| 849 | |
| 850 | /* Set up network device as normal. */ |
Stephen Hemminger | 76288b4 | 2009-01-06 10:44:22 -0800 | [diff] [blame] | 851 | dev->netdev_ops = &virtnet_netdev; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 852 | dev->features = NETIF_F_HIGHDMA; |
Herbert Xu | a9ea3fc | 2008-04-18 11:21:42 +0800 | [diff] [blame] | 853 | SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 854 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 855 | |
| 856 | /* Do we support "hardware" checksums? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 857 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 858 | /* This opens up the world of extra features. */ |
| 859 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 860 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 861 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
| 862 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 863 | } |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 864 | /* Individual feature bits: what can host handle? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 865 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 866 | dev->features |= NETIF_F_TSO; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 867 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 868 | dev->features |= NETIF_F_TSO6; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 869 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 870 | dev->features |= NETIF_F_TSO_ECN; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 871 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
Rusty Russell | 5539ae9 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 872 | dev->features |= NETIF_F_UFO; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /* Configuration may specify what MAC to use. Otherwise random. */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 876 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 877 | vdev->config->get(vdev, |
| 878 | offsetof(struct virtio_net_config, mac), |
| 879 | dev->dev_addr, dev->addr_len); |
Alex Williamson | 62994b2 | 2009-04-04 16:40:19 -0700 | [diff] [blame] | 880 | } else |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 881 | random_ether_addr(dev->dev_addr); |
| 882 | |
| 883 | /* Set up our device-specific information */ |
| 884 | vi = netdev_priv(dev); |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 885 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 886 | vi->dev = dev; |
| 887 | vi->vdev = vdev; |
Christian Borntraeger | d9d5dcc | 2008-02-18 10:02:51 +0100 | [diff] [blame] | 888 | vdev->priv = vi; |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 889 | vi->pages = NULL; |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 890 | INIT_DELAYED_WORK(&vi->refill, refill_work); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 891 | |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 892 | /* If we can receive ANY GSO packets, we must allocate large ones. */ |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 893 | if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || |
| 894 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6) || |
| 895 | virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN)) |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 896 | vi->big_packets = true; |
| 897 | |
Mark McLoughlin | 3f2c31d | 2008-11-16 22:41:34 -0800 | [diff] [blame] | 898 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) |
| 899 | vi->mergeable_rx_bufs = true; |
| 900 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 901 | /* We expect two virtqueues, receive then send, |
| 902 | * and optionally control. */ |
| 903 | nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 904 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 905 | err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names); |
| 906 | if (err) |
| 907 | goto free; |
| 908 | |
| 909 | vi->rvq = vqs[0]; |
| 910 | vi->svq = vqs[1]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 911 | |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 912 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) { |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 913 | vi->cvq = vqs[2]; |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 914 | |
| 915 | if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN)) |
| 916 | dev->features |= NETIF_F_HW_VLAN_FILTER; |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 919 | /* Initialize our empty receive and send queues. */ |
| 920 | skb_queue_head_init(&vi->recv); |
| 921 | skb_queue_head_init(&vi->send); |
| 922 | |
| 923 | err = register_netdev(dev); |
| 924 | if (err) { |
| 925 | pr_debug("virtio_net: registering device failed\n"); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 926 | goto free_vqs; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 927 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 928 | |
| 929 | /* Last of all, set up some receive buffers. */ |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 930 | try_fill_recv(vi, GFP_KERNEL); |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 931 | |
| 932 | /* If we didn't even get one input buffer, we're useless. */ |
| 933 | if (vi->num == 0) { |
| 934 | err = -ENOMEM; |
| 935 | goto unregister; |
| 936 | } |
| 937 | |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 938 | vi->status = VIRTIO_NET_S_LINK_UP; |
| 939 | virtnet_update_status(vi); |
Pantelis Koukousoulas | 4783256 | 2009-03-18 18:40:02 -0700 | [diff] [blame] | 940 | netif_carrier_on(dev); |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 941 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 942 | pr_debug("virtnet: registered device %s\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 943 | return 0; |
| 944 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 945 | unregister: |
| 946 | unregister_netdev(dev); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 947 | cancel_delayed_work_sync(&vi->refill); |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 948 | free_vqs: |
| 949 | vdev->config->del_vqs(vdev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 950 | free: |
| 951 | free_netdev(dev); |
| 952 | return err; |
| 953 | } |
| 954 | |
Uwe Kleine-König | 3d1285b | 2009-09-30 22:28:34 +0000 | [diff] [blame] | 955 | static void __devexit virtnet_remove(struct virtio_device *vdev) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 956 | { |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 957 | struct virtnet_info *vi = vdev->priv; |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 958 | struct sk_buff *skb; |
| 959 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 960 | /* Stop all the virtqueues. */ |
| 961 | vdev->config->reset(vdev); |
| 962 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 963 | /* Free our skbs in send and recv queues, if any. */ |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 964 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
| 965 | kfree_skb(skb); |
| 966 | vi->num--; |
| 967 | } |
Wang Chen | 288369c | 2008-05-22 18:07:43 +0800 | [diff] [blame] | 968 | __skb_queue_purge(&vi->send); |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 969 | |
| 970 | BUG_ON(vi->num != 0); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 971 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 972 | unregister_netdev(vi->dev); |
Rusty Russell | 3161e45 | 2009-08-26 12:22:32 -0700 | [diff] [blame] | 973 | cancel_delayed_work_sync(&vi->refill); |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 974 | |
Michael S. Tsirkin | d2a7ddd | 2009-06-12 22:16:36 -0600 | [diff] [blame] | 975 | vdev->config->del_vqs(vi->vdev); |
| 976 | |
Rusty Russell | fb6813f | 2008-07-25 12:06:01 -0500 | [diff] [blame] | 977 | while (vi->pages) |
| 978 | __free_pages(get_a_page(vi, GFP_KERNEL), 0); |
| 979 | |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 980 | free_netdev(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static struct virtio_device_id id_table[] = { |
| 984 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 985 | { 0 }, |
| 986 | }; |
| 987 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 988 | static unsigned int features[] = { |
Mark McLoughlin | 5e4fe5c | 2008-07-08 17:10:42 +1000 | [diff] [blame] | 989 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
| 990 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 991 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
Herbert Xu | 97402b9 | 2008-04-18 11:24:27 +0800 | [diff] [blame] | 992 | VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, |
Sridhar Samudrala | 5c51675 | 2009-07-14 14:21:02 +0000 | [diff] [blame] | 993 | VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, |
Alex Williamson | 2a41f71 | 2009-02-04 09:02:34 +0000 | [diff] [blame] | 994 | VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, |
Alex Williamson | 0bde9569 | 2009-02-04 09:02:50 +0000 | [diff] [blame] | 995 | VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 996 | }; |
| 997 | |
Uwe Kleine-König | 2240252 | 2009-11-05 01:32:44 -0800 | [diff] [blame] | 998 | static struct virtio_driver virtio_net_driver = { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 999 | .feature_table = features, |
| 1000 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1001 | .driver.name = KBUILD_MODNAME, |
| 1002 | .driver.owner = THIS_MODULE, |
| 1003 | .id_table = id_table, |
| 1004 | .probe = virtnet_probe, |
| 1005 | .remove = __devexit_p(virtnet_remove), |
Mark McLoughlin | 9f4d26d | 2009-01-19 17:09:49 -0800 | [diff] [blame] | 1006 | .config_changed = virtnet_config_changed, |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1007 | }; |
| 1008 | |
| 1009 | static int __init init(void) |
| 1010 | { |
Uwe Kleine-König | 2240252 | 2009-11-05 01:32:44 -0800 | [diff] [blame] | 1011 | return register_virtio_driver(&virtio_net_driver); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | static void __exit fini(void) |
| 1015 | { |
Uwe Kleine-König | 2240252 | 2009-11-05 01:32:44 -0800 | [diff] [blame] | 1016 | unregister_virtio_driver(&virtio_net_driver); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1017 | } |
| 1018 | module_init(init); |
| 1019 | module_exit(fini); |
| 1020 | |
| 1021 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 1022 | MODULE_DESCRIPTION("Virtio network driver"); |
| 1023 | MODULE_LICENSE("GPL"); |