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