Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 1 | /* A simple network driver using virtio. |
| 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> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/virtio.h> |
| 24 | #include <linux/virtio_net.h> |
| 25 | #include <linux/scatterlist.h> |
| 26 | |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 27 | static int napi_weight = 128; |
| 28 | module_param(napi_weight, int, 0444); |
| 29 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 30 | static int csum = 1, gso = 1; |
| 31 | module_param(csum, bool, 0444); |
| 32 | module_param(gso, bool, 0444); |
| 33 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 34 | /* FIXME: MTU in config. */ |
| 35 | #define MAX_PACKET_LEN (ETH_HLEN+ETH_DATA_LEN) |
| 36 | |
| 37 | struct virtnet_info |
| 38 | { |
| 39 | struct virtio_device *vdev; |
| 40 | struct virtqueue *rvq, *svq; |
| 41 | struct net_device *dev; |
| 42 | struct napi_struct napi; |
| 43 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 44 | /* The skb we couldn't send because buffers were full. */ |
| 45 | struct sk_buff *last_xmit_skb; |
| 46 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 47 | /* If we need to free in a timer, this is it. */ |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 48 | struct timer_list xmit_free_timer; |
| 49 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 50 | /* Number of input buffers, and max we've ever had. */ |
| 51 | unsigned int num, max; |
| 52 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 53 | /* For cleaning up after transmission. */ |
| 54 | struct tasklet_struct tasklet; |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 55 | bool free_in_tasklet; |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 56 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 57 | /* Receive & send queues. */ |
| 58 | struct sk_buff_head recv; |
| 59 | struct sk_buff_head send; |
| 60 | }; |
| 61 | |
| 62 | static inline struct virtio_net_hdr *skb_vnet_hdr(struct sk_buff *skb) |
| 63 | { |
| 64 | return (struct virtio_net_hdr *)skb->cb; |
| 65 | } |
| 66 | |
| 67 | static inline void vnet_hdr_to_sg(struct scatterlist *sg, struct sk_buff *skb) |
| 68 | { |
| 69 | sg_init_one(sg, skb_vnet_hdr(skb), sizeof(struct virtio_net_hdr)); |
| 70 | } |
| 71 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 72 | static void skb_xmit_done(struct virtqueue *svq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 73 | { |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 74 | struct virtnet_info *vi = svq->vdev->priv; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 75 | |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 76 | /* Suppress further interrupts. */ |
| 77 | svq->vq_ops->disable_cb(svq); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 78 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 79 | /* We were probably waiting for more output buffers. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 80 | netif_wake_queue(vi->dev); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 81 | |
| 82 | /* Make sure we re-xmit last_xmit_skb: if there are no more packets |
| 83 | * queued, start_xmit won't be called. */ |
| 84 | tasklet_schedule(&vi->tasklet); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void receive_skb(struct net_device *dev, struct sk_buff *skb, |
| 88 | unsigned len) |
| 89 | { |
| 90 | struct virtio_net_hdr *hdr = skb_vnet_hdr(skb); |
| 91 | |
| 92 | if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) { |
| 93 | pr_debug("%s: short packet %i\n", dev->name, len); |
| 94 | dev->stats.rx_length_errors++; |
| 95 | goto drop; |
| 96 | } |
| 97 | len -= sizeof(struct virtio_net_hdr); |
| 98 | BUG_ON(len > MAX_PACKET_LEN); |
| 99 | |
| 100 | skb_trim(skb, len); |
Mark McLoughlin | 23cde76 | 2008-06-08 20:49:00 +1000 | [diff] [blame] | 101 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 102 | dev->stats.rx_bytes += skb->len; |
| 103 | dev->stats.rx_packets++; |
| 104 | |
| 105 | if (hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { |
| 106 | pr_debug("Needs csum!\n"); |
Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 107 | if (!skb_partial_csum_set(skb,hdr->csum_start,hdr->csum_offset)) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 108 | goto frame_err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 109 | } |
| 110 | |
Mark McLoughlin | 23cde76 | 2008-06-08 20:49:00 +1000 | [diff] [blame] | 111 | skb->protocol = eth_type_trans(skb, dev); |
| 112 | pr_debug("Receiving skb proto 0x%04x len %i type %i\n", |
| 113 | ntohs(skb->protocol), skb->len, skb->pkt_type); |
| 114 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 115 | if (hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) { |
| 116 | pr_debug("GSO!\n"); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 117 | switch (hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 118 | case VIRTIO_NET_HDR_GSO_TCPV4: |
| 119 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 120 | break; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 121 | case VIRTIO_NET_HDR_GSO_UDP: |
| 122 | skb_shinfo(skb)->gso_type = SKB_GSO_UDP; |
| 123 | break; |
| 124 | case VIRTIO_NET_HDR_GSO_TCPV6: |
| 125 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 126 | break; |
| 127 | default: |
| 128 | if (net_ratelimit()) |
| 129 | printk(KERN_WARNING "%s: bad gso type %u.\n", |
| 130 | dev->name, hdr->gso_type); |
| 131 | goto frame_err; |
| 132 | } |
| 133 | |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 134 | if (hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN) |
| 135 | skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; |
| 136 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 137 | skb_shinfo(skb)->gso_size = hdr->gso_size; |
| 138 | if (skb_shinfo(skb)->gso_size == 0) { |
| 139 | if (net_ratelimit()) |
| 140 | printk(KERN_WARNING "%s: zero gso size.\n", |
| 141 | dev->name); |
| 142 | goto frame_err; |
| 143 | } |
| 144 | |
| 145 | /* Header must be checked, and gso_segs computed. */ |
| 146 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 147 | skb_shinfo(skb)->gso_segs = 0; |
| 148 | } |
| 149 | |
| 150 | netif_receive_skb(skb); |
| 151 | return; |
| 152 | |
| 153 | frame_err: |
| 154 | dev->stats.rx_frame_errors++; |
| 155 | drop: |
| 156 | dev_kfree_skb(skb); |
| 157 | } |
| 158 | |
| 159 | static void try_fill_recv(struct virtnet_info *vi) |
| 160 | { |
| 161 | struct sk_buff *skb; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 162 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 163 | int num, err; |
| 164 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 165 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 166 | for (;;) { |
| 167 | skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN); |
| 168 | if (unlikely(!skb)) |
| 169 | break; |
| 170 | |
| 171 | skb_put(skb, MAX_PACKET_LEN); |
| 172 | vnet_hdr_to_sg(sg, skb); |
| 173 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
| 174 | skb_queue_head(&vi->recv, skb); |
| 175 | |
| 176 | err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); |
| 177 | if (err) { |
| 178 | skb_unlink(skb, &vi->recv); |
| 179 | kfree_skb(skb); |
| 180 | break; |
| 181 | } |
| 182 | vi->num++; |
| 183 | } |
| 184 | if (unlikely(vi->num > vi->max)) |
| 185 | vi->max = vi->num; |
| 186 | vi->rvq->vq_ops->kick(vi->rvq); |
| 187 | } |
| 188 | |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 189 | static void skb_recv_done(struct virtqueue *rvq) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 190 | { |
| 191 | struct virtnet_info *vi = rvq->vdev->priv; |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 192 | /* Schedule NAPI, Suppress further interrupts if successful. */ |
| 193 | if (netif_rx_schedule_prep(vi->dev, &vi->napi)) { |
| 194 | rvq->vq_ops->disable_cb(rvq); |
| 195 | __netif_rx_schedule(vi->dev, &vi->napi); |
| 196 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int virtnet_poll(struct napi_struct *napi, int budget) |
| 200 | { |
| 201 | struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi); |
| 202 | struct sk_buff *skb = NULL; |
| 203 | unsigned int len, received = 0; |
| 204 | |
| 205 | again: |
| 206 | while (received < budget && |
| 207 | (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) { |
| 208 | __skb_unlink(skb, &vi->recv); |
| 209 | receive_skb(vi->dev, skb, len); |
| 210 | vi->num--; |
| 211 | received++; |
| 212 | } |
| 213 | |
| 214 | /* FIXME: If we oom and completely run out of inbufs, we need |
| 215 | * to start a timer trying to fill more. */ |
| 216 | if (vi->num < vi->max / 2) |
| 217 | try_fill_recv(vi); |
| 218 | |
Rusty Russell | 8329d98 | 2007-11-19 11:20:43 -0500 | [diff] [blame] | 219 | /* Out of packets? */ |
| 220 | if (received < budget) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 221 | netif_rx_complete(vi->dev, napi); |
Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 222 | if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq)) |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 223 | && napi_schedule_prep(napi)) { |
| 224 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 225 | __netif_rx_schedule(vi->dev, napi); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 226 | goto again; |
Christian Borntraeger | 4265f16 | 2008-03-14 14:17:05 +0100 | [diff] [blame] | 227 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return received; |
| 231 | } |
| 232 | |
| 233 | static void free_old_xmit_skbs(struct virtnet_info *vi) |
| 234 | { |
| 235 | struct sk_buff *skb; |
| 236 | unsigned int len; |
| 237 | |
| 238 | while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) { |
| 239 | pr_debug("Sent skb %p\n", skb); |
| 240 | __skb_unlink(skb, &vi->send); |
Rusty Russell | 655aa31 | 2008-05-02 21:50:43 -0500 | [diff] [blame] | 241 | vi->dev->stats.tx_bytes += skb->len; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 242 | vi->dev->stats.tx_packets++; |
| 243 | kfree_skb(skb); |
| 244 | } |
| 245 | } |
| 246 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 247 | /* If the virtio transport doesn't always notify us when all in-flight packets |
| 248 | * are consumed, we fall back to using this function on a timer to free them. */ |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 249 | static void xmit_free(unsigned long data) |
| 250 | { |
| 251 | struct virtnet_info *vi = (void *)data; |
| 252 | |
| 253 | netif_tx_lock(vi->dev); |
| 254 | |
| 255 | free_old_xmit_skbs(vi); |
| 256 | |
| 257 | if (!skb_queue_empty(&vi->send)) |
| 258 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
| 259 | |
| 260 | netif_tx_unlock(vi->dev); |
| 261 | } |
| 262 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 263 | static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 264 | { |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 265 | int num, err; |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 266 | struct scatterlist sg[2+MAX_SKB_FRAGS]; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 267 | struct virtio_net_hdr *hdr; |
| 268 | const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 269 | |
Rusty Russell | 0527168 | 2008-05-02 21:50:45 -0500 | [diff] [blame] | 270 | sg_init_table(sg, 2+MAX_SKB_FRAGS); |
Rusty Russell | 4d125de | 2007-11-07 16:34:49 +1100 | [diff] [blame] | 271 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 272 | pr_debug("%s: xmit %p " MAC_FMT "\n", vi->dev->name, skb, |
David S. Miller | 21f644f | 2008-04-08 16:50:44 -0700 | [diff] [blame] | 273 | dest[0], dest[1], dest[2], |
| 274 | dest[3], dest[4], dest[5]); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 275 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 276 | /* Encode metadata header at front. */ |
| 277 | hdr = skb_vnet_hdr(skb); |
| 278 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 279 | hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM; |
| 280 | hdr->csum_start = skb->csum_start - skb_headroom(skb); |
| 281 | hdr->csum_offset = skb->csum_offset; |
| 282 | } else { |
| 283 | hdr->flags = 0; |
| 284 | hdr->csum_offset = hdr->csum_start = 0; |
| 285 | } |
| 286 | |
| 287 | if (skb_is_gso(skb)) { |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 288 | hdr->hdr_len = skb_transport_header(skb) - skb->data; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 289 | hdr->gso_size = skb_shinfo(skb)->gso_size; |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 290 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 291 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; |
| 292 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 293 | hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; |
| 294 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 295 | hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP; |
| 296 | else |
| 297 | BUG(); |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 298 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN) |
| 299 | hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 300 | } else { |
| 301 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; |
Rusty Russell | 50c8ea8 | 2008-02-04 23:50:01 -0500 | [diff] [blame] | 302 | hdr->gso_size = hdr->hdr_len = 0; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | vnet_hdr_to_sg(sg, skb); |
| 306 | num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 307 | |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 308 | err = vi->svq->vq_ops->add_buf(vi->svq, sg, num, 0, skb); |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 309 | if (!err && !vi->free_in_tasklet) |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 310 | mod_timer(&vi->xmit_free_timer, jiffies + (HZ/10)); |
| 311 | |
| 312 | return err; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 315 | static void xmit_tasklet(unsigned long data) |
| 316 | { |
| 317 | struct virtnet_info *vi = (void *)data; |
| 318 | |
| 319 | netif_tx_lock_bh(vi->dev); |
| 320 | if (vi->last_xmit_skb && xmit_skb(vi, vi->last_xmit_skb) == 0) { |
| 321 | vi->svq->vq_ops->kick(vi->svq); |
| 322 | vi->last_xmit_skb = NULL; |
| 323 | } |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 324 | if (vi->free_in_tasklet) |
| 325 | free_old_xmit_skbs(vi); |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 326 | netif_tx_unlock_bh(vi->dev); |
| 327 | } |
| 328 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 329 | static int start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 330 | { |
| 331 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 332 | |
| 333 | again: |
| 334 | /* Free up any pending old buffers before queueing new ones. */ |
| 335 | free_old_xmit_skbs(vi); |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 336 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 337 | /* If we has a buffer left over from last time, send it now. */ |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 338 | if (unlikely(vi->last_xmit_skb)) { |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 339 | if (xmit_skb(vi, vi->last_xmit_skb) != 0) { |
| 340 | /* Drop this skb: we only queue one. */ |
| 341 | vi->dev->stats.tx_dropped++; |
| 342 | kfree_skb(skb); |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 343 | skb = NULL; |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 344 | goto stop_queue; |
Rusty Russell | 2cb9c6b | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 345 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 346 | vi->last_xmit_skb = NULL; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 347 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 348 | |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 349 | /* Put new one in send queue and do transmit */ |
Rusty Russell | 7eb2e25 | 2008-05-26 17:42:42 +1000 | [diff] [blame] | 350 | if (likely(skb)) { |
| 351 | __skb_queue_head(&vi->send, skb); |
| 352 | if (xmit_skb(vi, skb) != 0) { |
| 353 | vi->last_xmit_skb = skb; |
| 354 | skb = NULL; |
| 355 | goto stop_queue; |
| 356 | } |
Rusty Russell | 99ffc69 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 357 | } |
| 358 | done: |
| 359 | vi->svq->vq_ops->kick(vi->svq); |
| 360 | return NETDEV_TX_OK; |
| 361 | |
| 362 | stop_queue: |
| 363 | pr_debug("%s: virtio not prepared to send\n", dev->name); |
| 364 | netif_stop_queue(dev); |
| 365 | |
| 366 | /* Activate callback for using skbs: if this returns false it |
| 367 | * means some were used in the meantime. */ |
| 368 | if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) { |
| 369 | vi->svq->vq_ops->disable_cb(vi->svq); |
| 370 | netif_start_queue(dev); |
| 371 | goto again; |
| 372 | } |
| 373 | goto done; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 374 | } |
| 375 | |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 376 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 377 | static void virtnet_netpoll(struct net_device *dev) |
| 378 | { |
| 379 | struct virtnet_info *vi = netdev_priv(dev); |
| 380 | |
| 381 | napi_schedule(&vi->napi); |
| 382 | } |
| 383 | #endif |
| 384 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 385 | static int virtnet_open(struct net_device *dev) |
| 386 | { |
| 387 | struct virtnet_info *vi = netdev_priv(dev); |
| 388 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 389 | napi_enable(&vi->napi); |
Rusty Russell | a48bd8f | 2008-02-04 23:50:07 -0500 | [diff] [blame] | 390 | |
| 391 | /* If all buffers were filled by other side before we napi_enabled, we |
| 392 | * won't get another interrupt, so process any outstanding packets |
Christian Borntraeger | 370076d | 2008-02-06 08:50:11 +0100 | [diff] [blame] | 393 | * now. virtnet_poll wants re-enable the queue, so we disable here. |
| 394 | * We synchronize against interrupts via NAPI_STATE_SCHED */ |
| 395 | if (netif_rx_schedule_prep(dev, &vi->napi)) { |
| 396 | vi->rvq->vq_ops->disable_cb(vi->rvq); |
| 397 | __netif_rx_schedule(dev, &vi->napi); |
| 398 | } |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | static int virtnet_close(struct net_device *dev) |
| 403 | { |
| 404 | struct virtnet_info *vi = netdev_priv(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 405 | |
| 406 | napi_disable(&vi->napi); |
| 407 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int virtnet_probe(struct virtio_device *vdev) |
| 412 | { |
| 413 | int err; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 414 | struct net_device *dev; |
| 415 | struct virtnet_info *vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 416 | |
| 417 | /* Allocate ourselves a network device with room for our info */ |
| 418 | dev = alloc_etherdev(sizeof(struct virtnet_info)); |
| 419 | if (!dev) |
| 420 | return -ENOMEM; |
| 421 | |
| 422 | /* Set up network device as normal. */ |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 423 | dev->open = virtnet_open; |
| 424 | dev->stop = virtnet_close; |
| 425 | dev->hard_start_xmit = start_xmit; |
| 426 | dev->features = NETIF_F_HIGHDMA; |
Amit Shah | da74e89 | 2008-02-29 16:24:50 +0530 | [diff] [blame] | 427 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 428 | dev->poll_controller = virtnet_netpoll; |
| 429 | #endif |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 430 | SET_NETDEV_DEV(dev, &vdev->dev); |
| 431 | |
| 432 | /* Do we support "hardware" checksums? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 433 | if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) { |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 434 | /* This opens up the world of extra features. */ |
| 435 | dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 436 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) { |
Rusty Russell | 34a4857 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 437 | dev->features |= NETIF_F_TSO | NETIF_F_UFO |
| 438 | | NETIF_F_TSO_ECN | NETIF_F_TSO6; |
| 439 | } |
Rusty Russell | 5539ae96 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 440 | /* Individual feature bits: what can host handle? */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 441 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4)) |
Rusty Russell | 5539ae96 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 442 | dev->features |= NETIF_F_TSO; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 443 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6)) |
Rusty Russell | 5539ae96 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 444 | dev->features |= NETIF_F_TSO6; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 445 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN)) |
Rusty Russell | 5539ae96 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 446 | dev->features |= NETIF_F_TSO_ECN; |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 447 | if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO)) |
Rusty Russell | 5539ae96 | 2008-05-02 21:50:46 -0500 | [diff] [blame] | 448 | dev->features |= NETIF_F_UFO; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Configuration may specify what MAC to use. Otherwise random. */ |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 452 | if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) { |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 453 | vdev->config->get(vdev, |
| 454 | offsetof(struct virtio_net_config, mac), |
| 455 | dev->dev_addr, dev->addr_len); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 456 | } else |
| 457 | random_ether_addr(dev->dev_addr); |
| 458 | |
| 459 | /* Set up our device-specific information */ |
| 460 | vi = netdev_priv(dev); |
Dor Laor | 6c0cd7c | 2007-12-16 15:19:43 +0200 | [diff] [blame] | 461 | netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 462 | vi->dev = dev; |
| 463 | vi->vdev = vdev; |
Christian Borntraeger | d9d5dcc | 2008-02-18 10:02:51 +0100 | [diff] [blame] | 464 | vdev->priv = vi; |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 465 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 466 | /* If they give us a callback when all buffers are done, we don't need |
| 467 | * the timer. */ |
| 468 | vi->free_in_tasklet = virtio_has_feature(vdev,VIRTIO_F_NOTIFY_ON_EMPTY); |
| 469 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 470 | /* We expect two virtqueues, receive then send. */ |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 471 | vi->rvq = vdev->config->find_vq(vdev, 0, skb_recv_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 472 | if (IS_ERR(vi->rvq)) { |
| 473 | err = PTR_ERR(vi->rvq); |
| 474 | goto free; |
| 475 | } |
| 476 | |
Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 477 | vi->svq = vdev->config->find_vq(vdev, 1, skb_xmit_done); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 478 | if (IS_ERR(vi->svq)) { |
| 479 | err = PTR_ERR(vi->svq); |
| 480 | goto free_recv; |
| 481 | } |
| 482 | |
| 483 | /* Initialize our empty receive and send queues. */ |
| 484 | skb_queue_head_init(&vi->recv); |
| 485 | skb_queue_head_init(&vi->send); |
| 486 | |
Rusty Russell | 11a3a15 | 2008-05-26 17:48:13 +1000 | [diff] [blame] | 487 | tasklet_init(&vi->tasklet, xmit_tasklet, (unsigned long)vi); |
| 488 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 489 | if (!vi->free_in_tasklet) |
| 490 | setup_timer(&vi->xmit_free_timer, xmit_free, (unsigned long)vi); |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 491 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 492 | err = register_netdev(dev); |
| 493 | if (err) { |
| 494 | pr_debug("virtio_net: registering device failed\n"); |
| 495 | goto free_send; |
| 496 | } |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 497 | |
| 498 | /* Last of all, set up some receive buffers. */ |
| 499 | try_fill_recv(vi); |
| 500 | |
| 501 | /* If we didn't even get one input buffer, we're useless. */ |
| 502 | if (vi->num == 0) { |
| 503 | err = -ENOMEM; |
| 504 | goto unregister; |
| 505 | } |
| 506 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 507 | pr_debug("virtnet: registered device %s\n", dev->name); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 508 | return 0; |
| 509 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 510 | unregister: |
| 511 | unregister_netdev(dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 512 | free_send: |
| 513 | vdev->config->del_vq(vi->svq); |
| 514 | free_recv: |
| 515 | vdev->config->del_vq(vi->rvq); |
| 516 | free: |
| 517 | free_netdev(dev); |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | static void virtnet_remove(struct virtio_device *vdev) |
| 522 | { |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 523 | struct virtnet_info *vi = vdev->priv; |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 524 | struct sk_buff *skb; |
| 525 | |
Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 526 | /* Stop all the virtqueues. */ |
| 527 | vdev->config->reset(vdev); |
| 528 | |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 529 | if (!vi->free_in_tasklet) |
| 530 | del_timer_sync(&vi->xmit_free_timer); |
Mark McLoughlin | 14c998f | 2008-06-08 20:50:56 +1000 | [diff] [blame] | 531 | |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 532 | /* Free our skbs in send and recv queues, if any. */ |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 533 | while ((skb = __skb_dequeue(&vi->recv)) != NULL) { |
| 534 | kfree_skb(skb); |
| 535 | vi->num--; |
| 536 | } |
Wang Chen | 288369c | 2008-05-22 18:07:43 +0800 | [diff] [blame] | 537 | __skb_queue_purge(&vi->send); |
Rusty Russell | b3369c1 | 2008-02-04 23:50:02 -0500 | [diff] [blame] | 538 | |
| 539 | BUG_ON(vi->num != 0); |
Rusty Russell | 74b2553 | 2007-11-19 11:20:42 -0500 | [diff] [blame] | 540 | |
| 541 | vdev->config->del_vq(vi->svq); |
| 542 | vdev->config->del_vq(vi->rvq); |
| 543 | unregister_netdev(vi->dev); |
| 544 | free_netdev(vi->dev); |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | static struct virtio_device_id id_table[] = { |
| 548 | { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID }, |
| 549 | { 0 }, |
| 550 | }; |
| 551 | |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 552 | static unsigned int features[] = { |
Mark McLoughlin | 5e4fe5c | 2008-07-08 17:10:42 +1000 | [diff] [blame^] | 553 | VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, |
| 554 | VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 555 | VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, |
Rusty Russell | 363f151 | 2008-06-08 20:51:55 +1000 | [diff] [blame] | 556 | VIRTIO_NET_F_HOST_ECN, VIRTIO_F_NOTIFY_ON_EMPTY, |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 557 | }; |
| 558 | |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 559 | static struct virtio_driver virtio_net = { |
Rusty Russell | c45a681 | 2008-05-02 21:50:50 -0500 | [diff] [blame] | 560 | .feature_table = features, |
| 561 | .feature_table_size = ARRAY_SIZE(features), |
Rusty Russell | 296f96f | 2007-10-22 11:03:37 +1000 | [diff] [blame] | 562 | .driver.name = KBUILD_MODNAME, |
| 563 | .driver.owner = THIS_MODULE, |
| 564 | .id_table = id_table, |
| 565 | .probe = virtnet_probe, |
| 566 | .remove = __devexit_p(virtnet_remove), |
| 567 | }; |
| 568 | |
| 569 | static int __init init(void) |
| 570 | { |
| 571 | return register_virtio_driver(&virtio_net); |
| 572 | } |
| 573 | |
| 574 | static void __exit fini(void) |
| 575 | { |
| 576 | unregister_virtio_driver(&virtio_net); |
| 577 | } |
| 578 | module_init(init); |
| 579 | module_exit(fini); |
| 580 | |
| 581 | MODULE_DEVICE_TABLE(virtio, id_table); |
| 582 | MODULE_DESCRIPTION("Virtio network driver"); |
| 583 | MODULE_LICENSE("GPL"); |