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