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