blob: b9e002fccbca22e20541136a3779c9f56bc2220c [file] [log] [blame]
Rusty Russell48925e32009-09-24 09:59:20 -06001/* A network driver using virtio.
Rusty Russell296f96f2007-10-22 11:03:37 +10002 *
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 Xua9ea3fc2008-04-18 11:21:42 +080022#include <linux/ethtool.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100023#include <linux/module.h>
24#include <linux/virtio.h>
25#include <linux/virtio_net.h>
26#include <linux/scatterlist.h>
Alex Williamsone918085a2009-01-25 18:06:26 -080027#include <linux/if_vlan.h>
Rusty Russell296f96f2007-10-22 11:03:37 +100028
Dor Laor6c0cd7c2007-12-16 15:19:43 +020029static int napi_weight = 128;
30module_param(napi_weight, int, 0444);
31
Rusty Russell34a48572008-02-04 23:50:02 -050032static int csum = 1, gso = 1;
33module_param(csum, bool, 0444);
34module_param(gso, bool, 0444);
35
Rusty Russell296f96f2007-10-22 11:03:37 +100036/* FIXME: MTU in config. */
Alex Williamsone918085a2009-01-25 18:06:26 -080037#define MAX_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080038#define GOOD_COPY_LEN 128
Rusty Russell296f96f2007-10-22 11:03:37 +100039
Alex Williamsonf565a7c2009-02-04 09:02:45 +000040#define VIRTNET_SEND_COMMAND_SG_MAX 2
Alex Williamson2a41f712009-02-04 09:02:34 +000041
Rusty Russell296f96f2007-10-22 11:03:37 +100042struct virtnet_info
43{
44 struct virtio_device *vdev;
Alex Williamson2a41f712009-02-04 09:02:34 +000045 struct virtqueue *rvq, *svq, *cvq;
Rusty Russell296f96f2007-10-22 11:03:37 +100046 struct net_device *dev;
47 struct napi_struct napi;
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -080048 unsigned int status;
Rusty Russell296f96f2007-10-22 11:03:37 +100049
50 /* Number of input buffers, and max we've ever had. */
51 unsigned int num, max;
52
Herbert Xu97402b92008-04-18 11:24:27 +080053 /* I like... big packets and I cannot lie! */
54 bool big_packets;
55
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -080056 /* Host will merge rx buffers for big packets (shake it! shake it!) */
57 bool mergeable_rx_bufs;
58
Rusty Russell296f96f2007-10-22 11:03:37 +100059 /* Receive & send queues. */
60 struct sk_buff_head recv;
61 struct sk_buff_head send;
Rusty Russellfb6813f2008-07-25 12:06:01 -050062
Rusty Russell3161e452009-08-26 12:22:32 -070063 /* Work struct for refilling if we run low on memory. */
64 struct delayed_work refill;
65
Rusty Russellfb6813f2008-07-25 12:06:01 -050066 /* Chain pages by the private ptr. */
67 struct page *pages;
Rusty Russell296f96f2007-10-22 11:03:37 +100068};
69
Rusty Russellb3f24692009-09-24 09:59:19 -060070struct skb_vnet_hdr {
71 union {
72 struct virtio_net_hdr hdr;
73 struct virtio_net_hdr_mrg_rxbuf mhdr;
74 };
Rusty Russell48925e32009-09-24 09:59:20 -060075 unsigned int num_sg;
Rusty Russellb3f24692009-09-24 09:59:19 -060076};
77
78static inline struct skb_vnet_hdr *skb_vnet_hdr(struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +100079{
Rusty Russellb3f24692009-09-24 09:59:19 -060080 return (struct skb_vnet_hdr *)skb->cb;
Rusty Russell296f96f2007-10-22 11:03:37 +100081}
82
Rusty Russellfb6813f2008-07-25 12:06:01 -050083static void give_a_page(struct virtnet_info *vi, struct page *page)
84{
85 page->private = (unsigned long)vi->pages;
86 vi->pages = page;
87}
88
Mark McLoughlin0a888fd2008-11-16 22:39:18 -080089static void trim_pages(struct virtnet_info *vi, struct sk_buff *skb)
90{
91 unsigned int i;
92
93 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
94 give_a_page(vi, skb_shinfo(skb)->frags[i].page);
95 skb_shinfo(skb)->nr_frags = 0;
96 skb->data_len = 0;
97}
98
Rusty Russellfb6813f2008-07-25 12:06:01 -050099static struct page *get_a_page(struct virtnet_info *vi, gfp_t gfp_mask)
100{
101 struct page *p = vi->pages;
102
103 if (p)
104 vi->pages = (struct page *)p->private;
105 else
106 p = alloc_page(gfp_mask);
107 return p;
108}
109
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500110static void skb_xmit_done(struct virtqueue *svq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000111{
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500112 struct virtnet_info *vi = svq->vdev->priv;
Rusty Russell296f96f2007-10-22 11:03:37 +1000113
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500114 /* Suppress further interrupts. */
115 svq->vq_ops->disable_cb(svq);
Rusty Russell11a3a152008-05-26 17:48:13 +1000116
Rusty Russell363f1512008-06-08 20:51:55 +1000117 /* We were probably waiting for more output buffers. */
Rusty Russell296f96f2007-10-22 11:03:37 +1000118 netif_wake_queue(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000119}
120
121static void receive_skb(struct net_device *dev, struct sk_buff *skb,
122 unsigned len)
123{
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800124 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russellb3f24692009-09-24 09:59:19 -0600125 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
Herbert Xu97402b92008-04-18 11:24:27 +0800126 int err;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800127 int i;
Rusty Russell296f96f2007-10-22 11:03:37 +1000128
129 if (unlikely(len < sizeof(struct virtio_net_hdr) + ETH_HLEN)) {
130 pr_debug("%s: short packet %i\n", dev->name, len);
131 dev->stats.rx_length_errors++;
132 goto drop;
133 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000134
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800135 if (vi->mergeable_rx_bufs) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800136 unsigned int copy;
137 char *p = page_address(skb_shinfo(skb)->frags[0].page);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500138
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800139 if (len > PAGE_SIZE)
140 len = PAGE_SIZE;
141 len -= sizeof(struct virtio_net_hdr_mrg_rxbuf);
142
Rusty Russellb3f24692009-09-24 09:59:19 -0600143 memcpy(&hdr->mhdr, p, sizeof(hdr->mhdr));
144 p += sizeof(hdr->mhdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800145
146 copy = len;
147 if (copy > skb_tailroom(skb))
148 copy = skb_tailroom(skb);
149
150 memcpy(skb_put(skb, copy), p, copy);
151
152 len -= copy;
153
154 if (!len) {
155 give_a_page(vi, skb_shinfo(skb)->frags[0].page);
156 skb_shinfo(skb)->nr_frags--;
157 } else {
158 skb_shinfo(skb)->frags[0].page_offset +=
Rusty Russellb3f24692009-09-24 09:59:19 -0600159 sizeof(hdr->mhdr) + copy;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800160 skb_shinfo(skb)->frags[0].size = len;
161 skb->data_len += len;
162 skb->len += len;
163 }
164
Rusty Russellb3f24692009-09-24 09:59:19 -0600165 while (--hdr->mhdr.num_buffers) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800166 struct sk_buff *nskb;
167
168 i = skb_shinfo(skb)->nr_frags;
169 if (i >= MAX_SKB_FRAGS) {
170 pr_debug("%s: packet too long %d\n", dev->name,
171 len);
172 dev->stats.rx_length_errors++;
173 goto drop;
174 }
175
176 nskb = vi->rvq->vq_ops->get_buf(vi->rvq, &len);
177 if (!nskb) {
178 pr_debug("%s: rx error: %d buffers missing\n",
Rusty Russellb3f24692009-09-24 09:59:19 -0600179 dev->name, hdr->mhdr.num_buffers);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800180 dev->stats.rx_length_errors++;
181 goto drop;
182 }
183
184 __skb_unlink(nskb, &vi->recv);
185 vi->num--;
186
187 skb_shinfo(skb)->frags[i] = skb_shinfo(nskb)->frags[0];
188 skb_shinfo(nskb)->nr_frags = 0;
189 kfree_skb(nskb);
190
191 if (len > PAGE_SIZE)
192 len = PAGE_SIZE;
193
194 skb_shinfo(skb)->frags[i].size = len;
195 skb_shinfo(skb)->nr_frags++;
196 skb->data_len += len;
197 skb->len += len;
198 }
199 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600200 len -= sizeof(hdr->hdr);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800201
202 if (len <= MAX_PACKET_LEN)
203 trim_pages(vi, skb);
204
205 err = pskb_trim(skb, len);
206 if (err) {
207 pr_debug("%s: pskb_trim failed %i %d\n", dev->name,
208 len, err);
209 dev->stats.rx_dropped++;
210 goto drop;
211 }
Herbert Xu97402b92008-04-18 11:24:27 +0800212 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800213
Herbert Xu97402b92008-04-18 11:24:27 +0800214 skb->truesize += skb->data_len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000215 dev->stats.rx_bytes += skb->len;
216 dev->stats.rx_packets++;
217
Rusty Russellb3f24692009-09-24 09:59:19 -0600218 if (hdr->hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000219 pr_debug("Needs csum!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600220 if (!skb_partial_csum_set(skb,
221 hdr->hdr.csum_start,
222 hdr->hdr.csum_offset))
Rusty Russell296f96f2007-10-22 11:03:37 +1000223 goto frame_err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000224 }
225
Mark McLoughlin23cde762008-06-08 20:49:00 +1000226 skb->protocol = eth_type_trans(skb, dev);
227 pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
228 ntohs(skb->protocol), skb->len, skb->pkt_type);
229
Rusty Russellb3f24692009-09-24 09:59:19 -0600230 if (hdr->hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000231 pr_debug("GSO!\n");
Rusty Russellb3f24692009-09-24 09:59:19 -0600232 switch (hdr->hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000233 case VIRTIO_NET_HDR_GSO_TCPV4:
234 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
235 break;
Rusty Russell296f96f2007-10-22 11:03:37 +1000236 case VIRTIO_NET_HDR_GSO_UDP:
237 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
238 break;
239 case VIRTIO_NET_HDR_GSO_TCPV6:
240 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
241 break;
242 default:
243 if (net_ratelimit())
244 printk(KERN_WARNING "%s: bad gso type %u.\n",
Rusty Russellb3f24692009-09-24 09:59:19 -0600245 dev->name, hdr->hdr.gso_type);
Rusty Russell296f96f2007-10-22 11:03:37 +1000246 goto frame_err;
247 }
248
Rusty Russellb3f24692009-09-24 09:59:19 -0600249 if (hdr->hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
Rusty Russell34a48572008-02-04 23:50:02 -0500250 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
251
Rusty Russellb3f24692009-09-24 09:59:19 -0600252 skb_shinfo(skb)->gso_size = hdr->hdr.gso_size;
Rusty Russell296f96f2007-10-22 11:03:37 +1000253 if (skb_shinfo(skb)->gso_size == 0) {
254 if (net_ratelimit())
255 printk(KERN_WARNING "%s: zero gso size.\n",
256 dev->name);
257 goto frame_err;
258 }
259
260 /* Header must be checked, and gso_segs computed. */
261 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
262 skb_shinfo(skb)->gso_segs = 0;
263 }
264
265 netif_receive_skb(skb);
266 return;
267
268frame_err:
269 dev->stats.rx_frame_errors++;
270drop:
271 dev_kfree_skb(skb);
272}
273
Rusty Russell3161e452009-08-26 12:22:32 -0700274static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp)
Rusty Russell296f96f2007-10-22 11:03:37 +1000275{
276 struct sk_buff *skb;
Rusty Russell05271682008-05-02 21:50:45 -0500277 struct scatterlist sg[2+MAX_SKB_FRAGS];
Herbert Xu97402b92008-04-18 11:24:27 +0800278 int num, err, i;
Rusty Russell3161e452009-08-26 12:22:32 -0700279 bool oom = false;
Rusty Russell296f96f2007-10-22 11:03:37 +1000280
Rusty Russell05271682008-05-02 21:50:45 -0500281 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Amit Shah0aea51c2009-08-26 14:58:28 +0530282 do {
Rusty Russellb3f24692009-09-24 09:59:19 -0600283 struct skb_vnet_hdr *hdr;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800284
Herbert Xu8981f012009-06-11 20:55:17 -0700285 skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700286 if (unlikely(!skb)) {
287 oom = true;
Rusty Russell296f96f2007-10-22 11:03:37 +1000288 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700289 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000290
Herbert Xu8981f012009-06-11 20:55:17 -0700291 skb_reserve(skb, NET_IP_ALIGN);
Rusty Russell296f96f2007-10-22 11:03:37 +1000292 skb_put(skb, MAX_PACKET_LEN);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800293
294 hdr = skb_vnet_hdr(skb);
Rusty Russellb3f24692009-09-24 09:59:19 -0600295 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
Herbert Xu97402b92008-04-18 11:24:27 +0800296
297 if (vi->big_packets) {
298 for (i = 0; i < MAX_SKB_FRAGS; i++) {
299 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Rusty Russell3161e452009-08-26 12:22:32 -0700300 f->page = get_a_page(vi, gfp);
Herbert Xu97402b92008-04-18 11:24:27 +0800301 if (!f->page)
302 break;
303
304 f->page_offset = 0;
305 f->size = PAGE_SIZE;
306
307 skb->data_len += PAGE_SIZE;
308 skb->len += PAGE_SIZE;
309
310 skb_shinfo(skb)->nr_frags++;
311 }
312 }
313
Rusty Russell296f96f2007-10-22 11:03:37 +1000314 num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
315 skb_queue_head(&vi->recv, skb);
316
317 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600318 if (err < 0) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000319 skb_unlink(skb, &vi->recv);
Mark McLoughlin0a888fd2008-11-16 22:39:18 -0800320 trim_pages(vi, skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000321 kfree_skb(skb);
322 break;
323 }
324 vi->num++;
Amit Shah0aea51c2009-08-26 14:58:28 +0530325 } while (err >= num);
Rusty Russell296f96f2007-10-22 11:03:37 +1000326 if (unlikely(vi->num > vi->max))
327 vi->max = vi->num;
328 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700329 return !oom;
Rusty Russell296f96f2007-10-22 11:03:37 +1000330}
331
Rusty Russell3161e452009-08-26 12:22:32 -0700332/* Returns false if we couldn't fill entirely (OOM). */
333static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800334{
335 struct sk_buff *skb;
336 struct scatterlist sg[1];
337 int err;
Rusty Russell3161e452009-08-26 12:22:32 -0700338 bool oom = false;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800339
Rusty Russell3161e452009-08-26 12:22:32 -0700340 if (!vi->mergeable_rx_bufs)
341 return try_fill_recv_maxbufs(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800342
Amit Shah0aea51c2009-08-26 14:58:28 +0530343 do {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800344 skb_frag_t *f;
345
346 skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
Rusty Russell3161e452009-08-26 12:22:32 -0700347 if (unlikely(!skb)) {
348 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800349 break;
Rusty Russell3161e452009-08-26 12:22:32 -0700350 }
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800351
352 skb_reserve(skb, NET_IP_ALIGN);
353
354 f = &skb_shinfo(skb)->frags[0];
Rusty Russell3161e452009-08-26 12:22:32 -0700355 f->page = get_a_page(vi, gfp);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800356 if (!f->page) {
Rusty Russell3161e452009-08-26 12:22:32 -0700357 oom = true;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800358 kfree_skb(skb);
359 break;
360 }
361
362 f->page_offset = 0;
363 f->size = PAGE_SIZE;
364
365 skb_shinfo(skb)->nr_frags++;
366
367 sg_init_one(sg, page_address(f->page), PAGE_SIZE);
368 skb_queue_head(&vi->recv, skb);
369
370 err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb);
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600371 if (err < 0) {
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800372 skb_unlink(skb, &vi->recv);
373 kfree_skb(skb);
374 break;
375 }
376 vi->num++;
Amit Shah0aea51c2009-08-26 14:58:28 +0530377 } while (err > 0);
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800378 if (unlikely(vi->num > vi->max))
379 vi->max = vi->num;
380 vi->rvq->vq_ops->kick(vi->rvq);
Rusty Russell3161e452009-08-26 12:22:32 -0700381 return !oom;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800382}
383
Rusty Russell18445c42008-02-04 23:49:57 -0500384static void skb_recv_done(struct virtqueue *rvq)
Rusty Russell296f96f2007-10-22 11:03:37 +1000385{
386 struct virtnet_info *vi = rvq->vdev->priv;
Rusty Russell18445c42008-02-04 23:49:57 -0500387 /* Schedule NAPI, Suppress further interrupts if successful. */
Ben Hutchings288379f2009-01-19 16:43:59 -0800388 if (napi_schedule_prep(&vi->napi)) {
Rusty Russell18445c42008-02-04 23:49:57 -0500389 rvq->vq_ops->disable_cb(rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800390 __napi_schedule(&vi->napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500391 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000392}
393
Rusty Russell3161e452009-08-26 12:22:32 -0700394static void refill_work(struct work_struct *work)
395{
396 struct virtnet_info *vi;
397 bool still_empty;
398
399 vi = container_of(work, struct virtnet_info, refill.work);
400 napi_disable(&vi->napi);
401 try_fill_recv(vi, GFP_KERNEL);
402 still_empty = (vi->num == 0);
403 napi_enable(&vi->napi);
404
405 /* In theory, this can happen: if we don't get any buffers in
406 * we will *never* try to fill again. */
407 if (still_empty)
408 schedule_delayed_work(&vi->refill, HZ/2);
409}
410
Rusty Russell296f96f2007-10-22 11:03:37 +1000411static int virtnet_poll(struct napi_struct *napi, int budget)
412{
413 struct virtnet_info *vi = container_of(napi, struct virtnet_info, napi);
414 struct sk_buff *skb = NULL;
415 unsigned int len, received = 0;
416
417again:
418 while (received < budget &&
419 (skb = vi->rvq->vq_ops->get_buf(vi->rvq, &len)) != NULL) {
420 __skb_unlink(skb, &vi->recv);
421 receive_skb(vi->dev, skb, len);
422 vi->num--;
423 received++;
424 }
425
Rusty Russell3161e452009-08-26 12:22:32 -0700426 if (vi->num < vi->max / 2) {
427 if (!try_fill_recv(vi, GFP_ATOMIC))
428 schedule_delayed_work(&vi->refill, 0);
429 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000430
Rusty Russell8329d982007-11-19 11:20:43 -0500431 /* Out of packets? */
432 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800433 napi_complete(napi);
Rusty Russell18445c42008-02-04 23:49:57 -0500434 if (unlikely(!vi->rvq->vq_ops->enable_cb(vi->rvq))
Christian Borntraeger4265f162008-03-14 14:17:05 +0100435 && napi_schedule_prep(napi)) {
436 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800437 __napi_schedule(napi);
Rusty Russell296f96f2007-10-22 11:03:37 +1000438 goto again;
Christian Borntraeger4265f162008-03-14 14:17:05 +0100439 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000440 }
441
442 return received;
443}
444
Rusty Russell48925e32009-09-24 09:59:20 -0600445static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
Rusty Russell296f96f2007-10-22 11:03:37 +1000446{
447 struct sk_buff *skb;
Rusty Russell48925e32009-09-24 09:59:20 -0600448 unsigned int len, tot_sgs = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000449
450 while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
451 pr_debug("Sent skb %p\n", skb);
452 __skb_unlink(skb, &vi->send);
Rusty Russell655aa312008-05-02 21:50:43 -0500453 vi->dev->stats.tx_bytes += skb->len;
Rusty Russell296f96f2007-10-22 11:03:37 +1000454 vi->dev->stats.tx_packets++;
Rusty Russell48925e32009-09-24 09:59:20 -0600455 tot_sgs += skb_vnet_hdr(skb)->num_sg;
Eric Dumazeted79bab2009-10-14 14:36:43 +0000456 dev_kfree_skb_any(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000457 }
Rusty Russell48925e32009-09-24 09:59:20 -0600458 return tot_sgs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000459}
460
Rusty Russell99ffc692008-05-02 21:50:46 -0500461static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
Rusty Russell296f96f2007-10-22 11:03:37 +1000462{
Rusty Russell05271682008-05-02 21:50:45 -0500463 struct scatterlist sg[2+MAX_SKB_FRAGS];
Rusty Russellb3f24692009-09-24 09:59:19 -0600464 struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
Rusty Russell296f96f2007-10-22 11:03:37 +1000465 const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
Rusty Russell296f96f2007-10-22 11:03:37 +1000466
Rusty Russell05271682008-05-02 21:50:45 -0500467 sg_init_table(sg, 2+MAX_SKB_FRAGS);
Rusty Russell4d125de2007-11-07 16:34:49 +1100468
Johannes Berge1749612008-10-27 15:59:26 -0700469 pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
Rusty Russell296f96f2007-10-22 11:03:37 +1000470
Rusty Russell296f96f2007-10-22 11:03:37 +1000471 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600472 hdr->hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
473 hdr->hdr.csum_start = skb->csum_start - skb_headroom(skb);
474 hdr->hdr.csum_offset = skb->csum_offset;
Rusty Russell296f96f2007-10-22 11:03:37 +1000475 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600476 hdr->hdr.flags = 0;
477 hdr->hdr.csum_offset = hdr->hdr.csum_start = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000478 }
479
480 if (skb_is_gso(skb)) {
Rusty Russellb3f24692009-09-24 09:59:19 -0600481 hdr->hdr.hdr_len = skb_headlen(skb);
482 hdr->hdr.gso_size = skb_shinfo(skb)->gso_size;
Rusty Russell34a48572008-02-04 23:50:02 -0500483 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
Rusty Russellb3f24692009-09-24 09:59:19 -0600484 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
Rusty Russell296f96f2007-10-22 11:03:37 +1000485 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
Rusty Russellb3f24692009-09-24 09:59:19 -0600486 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
Rusty Russell296f96f2007-10-22 11:03:37 +1000487 else if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
Rusty Russellb3f24692009-09-24 09:59:19 -0600488 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
Rusty Russell296f96f2007-10-22 11:03:37 +1000489 else
490 BUG();
Rusty Russell34a48572008-02-04 23:50:02 -0500491 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCP_ECN)
Rusty Russellb3f24692009-09-24 09:59:19 -0600492 hdr->hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
Rusty Russell296f96f2007-10-22 11:03:37 +1000493 } else {
Rusty Russellb3f24692009-09-24 09:59:19 -0600494 hdr->hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
495 hdr->hdr.gso_size = hdr->hdr.hdr_len = 0;
Rusty Russell296f96f2007-10-22 11:03:37 +1000496 }
497
Rusty Russellb3f24692009-09-24 09:59:19 -0600498 hdr->mhdr.num_buffers = 0;
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800499
500 /* Encode metadata header at front. */
501 if (vi->mergeable_rx_bufs)
Rusty Russellb3f24692009-09-24 09:59:19 -0600502 sg_set_buf(sg, &hdr->mhdr, sizeof(hdr->mhdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800503 else
Rusty Russellb3f24692009-09-24 09:59:19 -0600504 sg_set_buf(sg, &hdr->hdr, sizeof(hdr->hdr));
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800505
Rusty Russell48925e32009-09-24 09:59:20 -0600506 hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
507 return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb);
Rusty Russell11a3a152008-05-26 17:48:13 +1000508}
509
Stephen Hemminger424efe92009-08-31 19:50:51 +0000510static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
Rusty Russell99ffc692008-05-02 21:50:46 -0500511{
512 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell48925e32009-09-24 09:59:20 -0600513 int capacity;
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500514
515again:
516 /* Free up any pending old buffers before queueing new ones. */
517 free_old_xmit_skbs(vi);
Rusty Russell2cb9c6b2008-02-04 23:50:07 -0500518
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700519 /* Try to transmit */
Rusty Russell48925e32009-09-24 09:59:20 -0600520 capacity = xmit_skb(vi, skb);
521
522 /* This can happen with OOM and indirect buffers. */
523 if (unlikely(capacity < 0)) {
524 netif_stop_queue(dev);
525 dev_warn(&dev->dev, "Unexpected full queue\n");
526 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
527 vi->svq->vq_ops->disable_cb(vi->svq);
528 netif_start_queue(dev);
529 goto again;
530 }
531 return NETDEV_TX_BUSY;
Rusty Russell99ffc692008-05-02 21:50:46 -0500532 }
Rusty Russell48925e32009-09-24 09:59:20 -0600533 vi->svq->vq_ops->kick(vi->svq);
Michael S. Tsirkin03f191b2009-10-28 04:03:38 -0700534
535 /*
536 * Put new one in send queue. You'd expect we'd need this before
537 * xmit_skb calls add_buf(), since the callback can be triggered
538 * immediately after that. But since the callback just triggers
539 * another call back here, normal network xmit locking prevents the
540 * race.
541 */
542 __skb_queue_head(&vi->send, skb);
543
Rusty Russell48925e32009-09-24 09:59:20 -0600544 /* Don't wait up for transmitted skbs to be freed. */
545 skb_orphan(skb);
546 nf_reset(skb);
Rusty Russell99ffc692008-05-02 21:50:46 -0500547
Rusty Russell48925e32009-09-24 09:59:20 -0600548 /* Apparently nice girls don't return TX_BUSY; stop the queue
549 * before it gets out of hand. Naturally, this wastes entries. */
550 if (capacity < 2+MAX_SKB_FRAGS) {
551 netif_stop_queue(dev);
552 if (unlikely(!vi->svq->vq_ops->enable_cb(vi->svq))) {
553 /* More just got used, free them then recheck. */
554 capacity += free_old_xmit_skbs(vi);
555 if (capacity >= 2+MAX_SKB_FRAGS) {
556 netif_start_queue(dev);
557 vi->svq->vq_ops->disable_cb(vi->svq);
558 }
559 }
Rusty Russell99ffc692008-05-02 21:50:46 -0500560 }
Rusty Russell48925e32009-09-24 09:59:20 -0600561
562 return NETDEV_TX_OK;
Rusty Russell296f96f2007-10-22 11:03:37 +1000563}
564
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800565static int virtnet_set_mac_address(struct net_device *dev, void *p)
566{
567 struct virtnet_info *vi = netdev_priv(dev);
568 struct virtio_device *vdev = vi->vdev;
569 int ret;
570
571 ret = eth_mac_addr(dev, p);
572 if (ret)
573 return ret;
574
Alex Williamson62994b22009-04-04 16:40:19 -0700575 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC))
576 vdev->config->set(vdev, offsetof(struct virtio_net_config, mac),
577 dev->dev_addr, dev->addr_len);
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800578
579 return 0;
580}
581
Amit Shahda74e892008-02-29 16:24:50 +0530582#ifdef CONFIG_NET_POLL_CONTROLLER
583static void virtnet_netpoll(struct net_device *dev)
584{
585 struct virtnet_info *vi = netdev_priv(dev);
586
587 napi_schedule(&vi->napi);
588}
589#endif
590
Rusty Russell296f96f2007-10-22 11:03:37 +1000591static int virtnet_open(struct net_device *dev)
592{
593 struct virtnet_info *vi = netdev_priv(dev);
594
Rusty Russell296f96f2007-10-22 11:03:37 +1000595 napi_enable(&vi->napi);
Rusty Russella48bd8f2008-02-04 23:50:07 -0500596
597 /* If all buffers were filled by other side before we napi_enabled, we
598 * won't get another interrupt, so process any outstanding packets
Christian Borntraeger370076d2008-02-06 08:50:11 +0100599 * now. virtnet_poll wants re-enable the queue, so we disable here.
600 * We synchronize against interrupts via NAPI_STATE_SCHED */
Ben Hutchings288379f2009-01-19 16:43:59 -0800601 if (napi_schedule_prep(&vi->napi)) {
Christian Borntraeger370076d2008-02-06 08:50:11 +0100602 vi->rvq->vq_ops->disable_cb(vi->rvq);
Ben Hutchings288379f2009-01-19 16:43:59 -0800603 __napi_schedule(&vi->napi);
Christian Borntraeger370076d2008-02-06 08:50:11 +0100604 }
Rusty Russell296f96f2007-10-22 11:03:37 +1000605 return 0;
606}
607
Alex Williamson2a41f712009-02-04 09:02:34 +0000608/*
609 * Send command via the control virtqueue and check status. Commands
610 * supported by the hypervisor, as indicated by feature bits, should
611 * never fail unless improperly formated.
612 */
613static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
614 struct scatterlist *data, int out, int in)
615{
Alex Williamson23e258e2009-05-01 17:27:56 +0000616 struct scatterlist *s, sg[VIRTNET_SEND_COMMAND_SG_MAX + 2];
Alex Williamson2a41f712009-02-04 09:02:34 +0000617 struct virtio_net_ctrl_hdr ctrl;
618 virtio_net_ctrl_ack status = ~0;
619 unsigned int tmp;
Alex Williamson23e258e2009-05-01 17:27:56 +0000620 int i;
Alex Williamson2a41f712009-02-04 09:02:34 +0000621
Alexander Beregalov0ee904c2009-04-11 14:50:23 +0000622 /* Caller should know better */
623 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ||
624 (out + in > VIRTNET_SEND_COMMAND_SG_MAX));
Alex Williamson2a41f712009-02-04 09:02:34 +0000625
626 out++; /* Add header */
627 in++; /* Add return status */
628
629 ctrl.class = class;
630 ctrl.cmd = cmd;
631
632 sg_init_table(sg, out + in);
633
634 sg_set_buf(&sg[0], &ctrl, sizeof(ctrl));
Alex Williamson23e258e2009-05-01 17:27:56 +0000635 for_each_sg(data, s, out + in - 2, i)
636 sg_set_buf(&sg[i + 1], sg_virt(s), s->length);
Alex Williamson2a41f712009-02-04 09:02:34 +0000637 sg_set_buf(&sg[out + in - 1], &status, sizeof(status));
638
Rusty Russell3c1b27d2009-09-23 22:26:31 -0600639 BUG_ON(vi->cvq->vq_ops->add_buf(vi->cvq, sg, out, in, vi) < 0);
Alex Williamson2a41f712009-02-04 09:02:34 +0000640
641 vi->cvq->vq_ops->kick(vi->cvq);
642
643 /*
644 * Spin for a response, the kick causes an ioport write, trapping
645 * into the hypervisor, so the request should be handled immediately.
646 */
647 while (!vi->cvq->vq_ops->get_buf(vi->cvq, &tmp))
648 cpu_relax();
649
650 return status == VIRTIO_NET_OK;
651}
652
Rusty Russell296f96f2007-10-22 11:03:37 +1000653static int virtnet_close(struct net_device *dev)
654{
655 struct virtnet_info *vi = netdev_priv(dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000656
657 napi_disable(&vi->napi);
658
Rusty Russell296f96f2007-10-22 11:03:37 +1000659 return 0;
660}
661
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800662static int virtnet_set_tx_csum(struct net_device *dev, u32 data)
663{
664 struct virtnet_info *vi = netdev_priv(dev);
665 struct virtio_device *vdev = vi->vdev;
666
667 if (data && !virtio_has_feature(vdev, VIRTIO_NET_F_CSUM))
668 return -ENOSYS;
669
670 return ethtool_op_set_tx_hw_csum(dev, data);
671}
672
Alex Williamson2af76982009-02-04 09:02:40 +0000673static void virtnet_set_rx_mode(struct net_device *dev)
674{
675 struct virtnet_info *vi = netdev_priv(dev);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000676 struct scatterlist sg[2];
Alex Williamson2af76982009-02-04 09:02:40 +0000677 u8 promisc, allmulti;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000678 struct virtio_net_ctrl_mac *mac_data;
679 struct dev_addr_list *addr;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000680 struct netdev_hw_addr *ha;
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000681 void *buf;
682 int i;
Alex Williamson2af76982009-02-04 09:02:40 +0000683
684 /* We can't dynamicaly set ndo_set_rx_mode, so return gracefully */
685 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
686 return;
687
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000688 promisc = ((dev->flags & IFF_PROMISC) != 0);
689 allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
Alex Williamson2af76982009-02-04 09:02:40 +0000690
Alex Williamson23e258e2009-05-01 17:27:56 +0000691 sg_init_one(sg, &promisc, sizeof(promisc));
Alex Williamson2af76982009-02-04 09:02:40 +0000692
693 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
694 VIRTIO_NET_CTRL_RX_PROMISC,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000695 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000696 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
697 promisc ? "en" : "dis");
698
Alex Williamson23e258e2009-05-01 17:27:56 +0000699 sg_init_one(sg, &allmulti, sizeof(allmulti));
Alex Williamson2af76982009-02-04 09:02:40 +0000700
701 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
702 VIRTIO_NET_CTRL_RX_ALLMULTI,
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000703 sg, 1, 0))
Alex Williamson2af76982009-02-04 09:02:40 +0000704 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
705 allmulti ? "en" : "dis");
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000706
707 /* MAC filter - use one buffer for both lists */
Jiri Pirko31278e72009-06-17 01:12:19 +0000708 mac_data = buf = kzalloc(((dev->uc.count + dev->mc_count) * ETH_ALEN) +
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000709 (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
710 if (!buf) {
711 dev_warn(&dev->dev, "No memory for MAC address buffer\n");
712 return;
713 }
714
Alex Williamson23e258e2009-05-01 17:27:56 +0000715 sg_init_table(sg, 2);
716
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000717 /* Store the unicast list and count in the front of the buffer */
Jiri Pirko31278e72009-06-17 01:12:19 +0000718 mac_data->entries = dev->uc.count;
Jiri Pirkoccffad252009-05-22 23:22:17 +0000719 i = 0;
Jiri Pirko31278e72009-06-17 01:12:19 +0000720 list_for_each_entry(ha, &dev->uc.list, list)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000721 memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000722
723 sg_set_buf(&sg[0], mac_data,
Jiri Pirko31278e72009-06-17 01:12:19 +0000724 sizeof(mac_data->entries) + (dev->uc.count * ETH_ALEN));
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000725
726 /* multicast list and count fill the end */
Jiri Pirko31278e72009-06-17 01:12:19 +0000727 mac_data = (void *)&mac_data->macs[dev->uc.count][0];
Alex Williamsonf565a7c2009-02-04 09:02:45 +0000728
729 mac_data->entries = dev->mc_count;
730 addr = dev->mc_list;
731 for (i = 0; i < dev->mc_count; i++, addr = addr->next)
732 memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
733
734 sg_set_buf(&sg[1], mac_data,
735 sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
736
737 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
738 VIRTIO_NET_CTRL_MAC_TABLE_SET,
739 sg, 2, 0))
740 dev_warn(&dev->dev, "Failed to set MAC fitler table.\n");
741
742 kfree(buf);
Alex Williamson2af76982009-02-04 09:02:40 +0000743}
744
Alex Williamson1824a982009-05-01 17:31:10 +0000745static void virtnet_vlan_rx_add_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000746{
747 struct virtnet_info *vi = netdev_priv(dev);
748 struct scatterlist sg;
749
Alex Williamson23e258e2009-05-01 17:27:56 +0000750 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000751
752 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
753 VIRTIO_NET_CTRL_VLAN_ADD, &sg, 1, 0))
754 dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
755}
756
Alex Williamson1824a982009-05-01 17:31:10 +0000757static void virtnet_vlan_rx_kill_vid(struct net_device *dev, u16 vid)
Alex Williamson0bde95692009-02-04 09:02:50 +0000758{
759 struct virtnet_info *vi = netdev_priv(dev);
760 struct scatterlist sg;
761
Alex Williamson23e258e2009-05-01 17:27:56 +0000762 sg_init_one(&sg, &vid, sizeof(vid));
Alex Williamson0bde95692009-02-04 09:02:50 +0000763
764 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
765 VIRTIO_NET_CTRL_VLAN_DEL, &sg, 1, 0))
766 dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
767}
768
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700769static const struct ethtool_ops virtnet_ethtool_ops = {
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800770 .set_tx_csum = virtnet_set_tx_csum,
771 .set_sg = ethtool_op_set_sg,
Mark McLoughlin0276b492008-11-16 22:40:36 -0800772 .set_tso = ethtool_op_set_tso,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000773 .set_ufo = ethtool_op_set_ufo,
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800774 .get_link = ethtool_op_get_link,
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800775};
776
Mark McLoughlin39da5812008-11-26 13:58:11 +0000777#define MIN_MTU 68
778#define MAX_MTU 65535
779
780static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
781{
782 if (new_mtu < MIN_MTU || new_mtu > MAX_MTU)
783 return -EINVAL;
784 dev->mtu = new_mtu;
785 return 0;
786}
787
Stephen Hemminger76288b42009-01-06 10:44:22 -0800788static const struct net_device_ops virtnet_netdev = {
789 .ndo_open = virtnet_open,
790 .ndo_stop = virtnet_close,
791 .ndo_start_xmit = start_xmit,
792 .ndo_validate_addr = eth_validate_addr,
Alex Williamson9c46f6d2009-02-04 16:36:34 -0800793 .ndo_set_mac_address = virtnet_set_mac_address,
Alex Williamson2af76982009-02-04 09:02:40 +0000794 .ndo_set_rx_mode = virtnet_set_rx_mode,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800795 .ndo_change_mtu = virtnet_change_mtu,
Alex Williamson1824a982009-05-01 17:31:10 +0000796 .ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
797 .ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
Stephen Hemminger76288b42009-01-06 10:44:22 -0800798#ifdef CONFIG_NET_POLL_CONTROLLER
799 .ndo_poll_controller = virtnet_netpoll,
800#endif
801};
802
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800803static void virtnet_update_status(struct virtnet_info *vi)
804{
805 u16 v;
806
807 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
808 return;
809
810 vi->vdev->config->get(vi->vdev,
811 offsetof(struct virtio_net_config, status),
812 &v, sizeof(v));
813
814 /* Ignore unknown (future) status bits */
815 v &= VIRTIO_NET_S_LINK_UP;
816
817 if (vi->status == v)
818 return;
819
820 vi->status = v;
821
822 if (vi->status & VIRTIO_NET_S_LINK_UP) {
823 netif_carrier_on(vi->dev);
824 netif_wake_queue(vi->dev);
825 } else {
826 netif_carrier_off(vi->dev);
827 netif_stop_queue(vi->dev);
828 }
829}
830
831static void virtnet_config_changed(struct virtio_device *vdev)
832{
833 struct virtnet_info *vi = vdev->priv;
834
835 virtnet_update_status(vi);
836}
837
Rusty Russell296f96f2007-10-22 11:03:37 +1000838static int virtnet_probe(struct virtio_device *vdev)
839{
840 int err;
Rusty Russell296f96f2007-10-22 11:03:37 +1000841 struct net_device *dev;
842 struct virtnet_info *vi;
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600843 struct virtqueue *vqs[3];
844 vq_callback_t *callbacks[] = { skb_recv_done, skb_xmit_done, NULL};
845 const char *names[] = { "input", "output", "control" };
846 int nvqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000847
848 /* Allocate ourselves a network device with room for our info */
849 dev = alloc_etherdev(sizeof(struct virtnet_info));
850 if (!dev)
851 return -ENOMEM;
852
853 /* Set up network device as normal. */
Stephen Hemminger76288b42009-01-06 10:44:22 -0800854 dev->netdev_ops = &virtnet_netdev;
Rusty Russell296f96f2007-10-22 11:03:37 +1000855 dev->features = NETIF_F_HIGHDMA;
Herbert Xua9ea3fc2008-04-18 11:21:42 +0800856 SET_ETHTOOL_OPS(dev, &virtnet_ethtool_ops);
Rusty Russell296f96f2007-10-22 11:03:37 +1000857 SET_NETDEV_DEV(dev, &vdev->dev);
858
859 /* Do we support "hardware" checksums? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500860 if (csum && virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
Rusty Russell296f96f2007-10-22 11:03:37 +1000861 /* This opens up the world of extra features. */
862 dev->features |= NETIF_F_HW_CSUM|NETIF_F_SG|NETIF_F_FRAGLIST;
Rusty Russellc45a6812008-05-02 21:50:50 -0500863 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
Rusty Russell34a48572008-02-04 23:50:02 -0500864 dev->features |= NETIF_F_TSO | NETIF_F_UFO
865 | NETIF_F_TSO_ECN | NETIF_F_TSO6;
866 }
Rusty Russell5539ae92008-05-02 21:50:46 -0500867 /* Individual feature bits: what can host handle? */
Rusty Russellc45a6812008-05-02 21:50:50 -0500868 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
Rusty Russell5539ae92008-05-02 21:50:46 -0500869 dev->features |= NETIF_F_TSO;
Rusty Russellc45a6812008-05-02 21:50:50 -0500870 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
Rusty Russell5539ae92008-05-02 21:50:46 -0500871 dev->features |= NETIF_F_TSO6;
Rusty Russellc45a6812008-05-02 21:50:50 -0500872 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
Rusty Russell5539ae92008-05-02 21:50:46 -0500873 dev->features |= NETIF_F_TSO_ECN;
Rusty Russellc45a6812008-05-02 21:50:50 -0500874 if (gso && virtio_has_feature(vdev, VIRTIO_NET_F_HOST_UFO))
Rusty Russell5539ae92008-05-02 21:50:46 -0500875 dev->features |= NETIF_F_UFO;
Rusty Russell296f96f2007-10-22 11:03:37 +1000876 }
877
878 /* Configuration may specify what MAC to use. Otherwise random. */
Rusty Russellc45a6812008-05-02 21:50:50 -0500879 if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
Rusty Russella586d4f2008-02-04 23:49:56 -0500880 vdev->config->get(vdev,
881 offsetof(struct virtio_net_config, mac),
882 dev->dev_addr, dev->addr_len);
Alex Williamson62994b22009-04-04 16:40:19 -0700883 } else
Rusty Russell296f96f2007-10-22 11:03:37 +1000884 random_ether_addr(dev->dev_addr);
885
886 /* Set up our device-specific information */
887 vi = netdev_priv(dev);
Dor Laor6c0cd7c2007-12-16 15:19:43 +0200888 netif_napi_add(dev, &vi->napi, virtnet_poll, napi_weight);
Rusty Russell296f96f2007-10-22 11:03:37 +1000889 vi->dev = dev;
890 vi->vdev = vdev;
Christian Borntraegerd9d5dcc2008-02-18 10:02:51 +0100891 vdev->priv = vi;
Rusty Russellfb6813f2008-07-25 12:06:01 -0500892 vi->pages = NULL;
Rusty Russell3161e452009-08-26 12:22:32 -0700893 INIT_DELAYED_WORK(&vi->refill, refill_work);
Rusty Russell296f96f2007-10-22 11:03:37 +1000894
Herbert Xu97402b92008-04-18 11:24:27 +0800895 /* If we can receive ANY GSO packets, we must allocate large ones. */
896 if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4)
897 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6)
898 || virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_ECN))
899 vi->big_packets = true;
900
Mark McLoughlin3f2c31d2008-11-16 22:41:34 -0800901 if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
902 vi->mergeable_rx_bufs = true;
903
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600904 /* We expect two virtqueues, receive then send,
905 * and optionally control. */
906 nvqs = virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ) ? 3 : 2;
Rusty Russell296f96f2007-10-22 11:03:37 +1000907
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600908 err = vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names);
909 if (err)
910 goto free;
911
912 vi->rvq = vqs[0];
913 vi->svq = vqs[1];
Rusty Russell296f96f2007-10-22 11:03:37 +1000914
Alex Williamson2a41f712009-02-04 09:02:34 +0000915 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)) {
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600916 vi->cvq = vqs[2];
Alex Williamson0bde95692009-02-04 09:02:50 +0000917
918 if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
919 dev->features |= NETIF_F_HW_VLAN_FILTER;
Alex Williamson2a41f712009-02-04 09:02:34 +0000920 }
921
Rusty Russell296f96f2007-10-22 11:03:37 +1000922 /* Initialize our empty receive and send queues. */
923 skb_queue_head_init(&vi->recv);
924 skb_queue_head_init(&vi->send);
925
926 err = register_netdev(dev);
927 if (err) {
928 pr_debug("virtio_net: registering device failed\n");
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600929 goto free_vqs;
Rusty Russell296f96f2007-10-22 11:03:37 +1000930 }
Rusty Russellb3369c12008-02-04 23:50:02 -0500931
932 /* Last of all, set up some receive buffers. */
Rusty Russell3161e452009-08-26 12:22:32 -0700933 try_fill_recv(vi, GFP_KERNEL);
Rusty Russellb3369c12008-02-04 23:50:02 -0500934
935 /* If we didn't even get one input buffer, we're useless. */
936 if (vi->num == 0) {
937 err = -ENOMEM;
938 goto unregister;
939 }
940
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800941 vi->status = VIRTIO_NET_S_LINK_UP;
942 virtnet_update_status(vi);
Pantelis Koukousoulas47832562009-03-18 18:40:02 -0700943 netif_carrier_on(dev);
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -0800944
Rusty Russell296f96f2007-10-22 11:03:37 +1000945 pr_debug("virtnet: registered device %s\n", dev->name);
Rusty Russell296f96f2007-10-22 11:03:37 +1000946 return 0;
947
Rusty Russellb3369c12008-02-04 23:50:02 -0500948unregister:
949 unregister_netdev(dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700950 cancel_delayed_work_sync(&vi->refill);
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600951free_vqs:
952 vdev->config->del_vqs(vdev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000953free:
954 free_netdev(dev);
955 return err;
956}
957
Uwe Kleine-König3d1285b2009-09-30 22:28:34 +0000958static void __devexit virtnet_remove(struct virtio_device *vdev)
Rusty Russell296f96f2007-10-22 11:03:37 +1000959{
Rusty Russell74b25532007-11-19 11:20:42 -0500960 struct virtnet_info *vi = vdev->priv;
Rusty Russellb3369c12008-02-04 23:50:02 -0500961 struct sk_buff *skb;
962
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500963 /* Stop all the virtqueues. */
964 vdev->config->reset(vdev);
965
Rusty Russellb3369c12008-02-04 23:50:02 -0500966 /* Free our skbs in send and recv queues, if any. */
Rusty Russellb3369c12008-02-04 23:50:02 -0500967 while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
968 kfree_skb(skb);
969 vi->num--;
970 }
Wang Chen288369c2008-05-22 18:07:43 +0800971 __skb_queue_purge(&vi->send);
Rusty Russellb3369c12008-02-04 23:50:02 -0500972
973 BUG_ON(vi->num != 0);
Rusty Russell74b25532007-11-19 11:20:42 -0500974
Rusty Russell74b25532007-11-19 11:20:42 -0500975 unregister_netdev(vi->dev);
Rusty Russell3161e452009-08-26 12:22:32 -0700976 cancel_delayed_work_sync(&vi->refill);
Rusty Russellfb6813f2008-07-25 12:06:01 -0500977
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600978 vdev->config->del_vqs(vi->vdev);
979
Rusty Russellfb6813f2008-07-25 12:06:01 -0500980 while (vi->pages)
981 __free_pages(get_a_page(vi, GFP_KERNEL), 0);
982
Rusty Russell74b25532007-11-19 11:20:42 -0500983 free_netdev(vi->dev);
Rusty Russell296f96f2007-10-22 11:03:37 +1000984}
985
986static struct virtio_device_id id_table[] = {
987 { VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
988 { 0 },
989};
990
Rusty Russellc45a6812008-05-02 21:50:50 -0500991static unsigned int features[] = {
Mark McLoughlin5e4fe5c2008-07-08 17:10:42 +1000992 VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM,
993 VIRTIO_NET_F_GSO, VIRTIO_NET_F_MAC,
Rusty Russellc45a6812008-05-02 21:50:50 -0500994 VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
Herbert Xu97402b92008-04-18 11:24:27 +0800995 VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
Sridhar Samudrala5c516752009-07-14 14:21:02 +0000996 VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO,
Alex Williamson2a41f712009-02-04 09:02:34 +0000997 VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ,
Alex Williamson0bde95692009-02-04 09:02:50 +0000998 VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN,
Rusty Russellc45a6812008-05-02 21:50:50 -0500999};
1000
Uwe Kleine-König22402522009-11-05 01:32:44 -08001001static struct virtio_driver virtio_net_driver = {
Rusty Russellc45a6812008-05-02 21:50:50 -05001002 .feature_table = features,
1003 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell296f96f2007-10-22 11:03:37 +10001004 .driver.name = KBUILD_MODNAME,
1005 .driver.owner = THIS_MODULE,
1006 .id_table = id_table,
1007 .probe = virtnet_probe,
1008 .remove = __devexit_p(virtnet_remove),
Mark McLoughlin9f4d26d2009-01-19 17:09:49 -08001009 .config_changed = virtnet_config_changed,
Rusty Russell296f96f2007-10-22 11:03:37 +10001010};
1011
1012static int __init init(void)
1013{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001014 return register_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001015}
1016
1017static void __exit fini(void)
1018{
Uwe Kleine-König22402522009-11-05 01:32:44 -08001019 unregister_virtio_driver(&virtio_net_driver);
Rusty Russell296f96f2007-10-22 11:03:37 +10001020}
1021module_init(init);
1022module_exit(fini);
1023
1024MODULE_DEVICE_TABLE(virtio, id_table);
1025MODULE_DESCRIPTION("Virtio network driver");
1026MODULE_LICENSE("GPL");