blob: 58effc49f526b31fa914c704e547a800743f8c8f [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
7 *
8 * Copyright (c) 2002-2005, K A Fraser
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include "common.h"
36
37#include <linux/kthread.h>
38#include <linux/if_vlan.h>
39#include <linux/udp.h>
Zoltan Kisse3377f32014-03-06 21:48:29 +000040#include <linux/highmem.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000041
42#include <net/tcp.h>
43
Stefano Stabellinica981632012-08-08 17:21:23 +000044#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000045#include <xen/events.h>
46#include <xen/interface/memory.h>
47
48#include <asm/xen/hypercall.h>
49#include <asm/xen/page.h>
50
Wei Liue1f00a692013-05-22 06:34:45 +000051/* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
53 * enabled by default.
54 */
55bool separate_tx_rx_irq = 1;
56module_param(separate_tx_rx_irq, bool, 0644);
57
Wei Liu2810e5b2013-04-22 02:20:42 +000058/*
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
61 */
Wei Liu37641492013-05-02 00:43:59 +000062#define FATAL_SKB_SLOTS_DEFAULT 20
63static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64module_param(fatal_skb_slots, uint, 0444);
65
Wei Liu73764192013-08-26 12:59:39 +010066static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
67 u8 status);
68
Ian Campbellf942dc22011-03-15 00:06:18 +000069static void make_tx_response(struct xenvif *vif,
70 struct xen_netif_tx_request *txp,
71 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010072
73static inline int tx_work_todo(struct xenvif *vif);
74static inline int rx_work_todo(struct xenvif *vif);
75
Ian Campbellf942dc22011-03-15 00:06:18 +000076static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
77 u16 id,
78 s8 st,
79 u16 offset,
80 u16 size,
81 u16 flags);
82
Wei Liub3f980b2013-08-26 12:59:38 +010083static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +000084 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000085{
Wei Liub3f980b2013-08-26 12:59:38 +010086 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +000087}
88
Wei Liub3f980b2013-08-26 12:59:38 +010089static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +000090 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000091{
Wei Liub3f980b2013-08-26 12:59:38 +010092 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +000093}
94
Zoltan Kissf53c3fe2014-03-06 21:48:26 +000095/* Find the containing VIF's structure from a pointer in pending_tx_info array
96 */
Zoltan Kiss3e2234b2014-03-06 21:48:25 +000097static inline struct xenvif* ubuf_to_vif(struct ubuf_info *ubuf)
98{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +000099 u16 pending_idx = ubuf->desc;
100 struct pending_tx_info *temp =
101 container_of(ubuf, struct pending_tx_info, callback_struct);
102 return container_of(temp - pending_idx,
103 struct xenvif,
104 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000105}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000106
Paul Durrant2eba61d2013-10-16 17:50:29 +0100107/* This is a miniumum size for the linear area to avoid lots of
108 * calls to __pskb_pull_tail() as we set up checksum offsets. The
109 * value 128 was chosen as it covers all IPv4 and most likely
110 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000111 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100112#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000113
Ian Campbellea066ad2011-10-05 00:28:46 +0000114static u16 frag_get_pending_idx(skb_frag_t *frag)
115{
116 return (u16)frag->page_offset;
117}
118
119static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
120{
121 frag->page_offset = pending_idx;
122}
123
Ian Campbellf942dc22011-03-15 00:06:18 +0000124static inline pending_ring_idx_t pending_index(unsigned i)
125{
126 return i & (MAX_PENDING_REQS-1);
127}
128
Paul Durrantca2f09f2013-12-06 16:36:07 +0000129bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000130{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000131 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000132
Paul Durrantca2f09f2013-12-06 16:36:07 +0000133 do {
134 prod = vif->rx.sring->req_prod;
135 cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000136
Paul Durrantca2f09f2013-12-06 16:36:07 +0000137 if (prod - cons >= needed)
138 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000139
Paul Durrantca2f09f2013-12-06 16:36:07 +0000140 vif->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000141
Paul Durrantca2f09f2013-12-06 16:36:07 +0000142 /* Make sure event is visible before we check prod
143 * again.
144 */
145 mb();
146 } while (vif->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000147
Paul Durrantca2f09f2013-12-06 16:36:07 +0000148 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000149}
150
151/*
152 * Returns true if we should start a new receive buffer instead of
153 * adding 'size' bytes to a buffer which currently contains 'offset'
154 * bytes.
155 */
156static bool start_new_rx_buffer(int offset, unsigned long size, int head)
157{
158 /* simple case: we have completely filled the current buffer. */
159 if (offset == MAX_BUFFER_OFFSET)
160 return true;
161
162 /*
163 * complex case: start a fresh buffer if the current frag
164 * would overflow the current buffer but only if:
165 * (i) this frag would fit completely in the next buffer
166 * and (ii) there is already some data in the current buffer
167 * and (iii) this is not the head buffer.
168 *
169 * Where:
170 * - (i) stops us splitting a frag into two copies
171 * unless the frag is too large for a single buffer.
172 * - (ii) stops us from leaving a buffer pointlessly empty.
173 * - (iii) stops us leaving the first buffer
174 * empty. Strictly speaking this is already covered
175 * by (ii) but is explicitly checked because
176 * netfront relies on the first buffer being
177 * non-empty and can crash otherwise.
178 *
179 * This means we will effectively linearise small
180 * frags but do not needlessly split large buffers
181 * into multiple copies tend to give large frags their
182 * own buffers as before.
183 */
184 if ((offset + size > MAX_BUFFER_OFFSET) &&
185 (size <= MAX_BUFFER_OFFSET) && offset && !head)
186 return true;
187
188 return false;
189}
190
Ian Campbellf942dc22011-03-15 00:06:18 +0000191struct netrx_pending_operations {
192 unsigned copy_prod, copy_cons;
193 unsigned meta_prod, meta_cons;
194 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100195 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000196 int copy_off;
197 grant_ref_t copy_gref;
198};
199
Wei Liub3f980b2013-08-26 12:59:38 +0100200static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
201 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000202{
Wei Liub3f980b2013-08-26 12:59:38 +0100203 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000204 struct xen_netif_rx_request *req;
205
206 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
207
208 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100209 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000210 meta->gso_size = 0;
211 meta->size = 0;
212 meta->id = req->id;
213
214 npo->copy_off = 0;
215 npo->copy_gref = req->gref;
216
217 return meta;
218}
219
Wei Liu33bc8012013-10-08 10:54:21 +0100220/*
221 * Set up the grant operations for this fragment. If it's a flipping
222 * interface, we also set up the unmap request from here.
223 */
Wei Liu73764192013-08-26 12:59:39 +0100224static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
225 struct netrx_pending_operations *npo,
226 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000227 unsigned long offset, int *head,
228 struct xenvif *foreign_vif,
229 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000230{
231 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100232 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000233 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100234 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000235
236 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000237 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000238
239 meta = npo->meta + npo->meta_prod - 1;
240
Ian Campbell6a8ed462012-10-10 03:48:42 +0000241 /* Skip unused frames from start of page */
242 page += offset >> PAGE_SHIFT;
243 offset &= ~PAGE_MASK;
244
Ian Campbellf942dc22011-03-15 00:06:18 +0000245 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000246 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000247 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
248
Ian Campbell6a8ed462012-10-10 03:48:42 +0000249 bytes = PAGE_SIZE - offset;
250
251 if (bytes > size)
252 bytes = size;
253
Wei Liu33bc8012013-10-08 10:54:21 +0100254 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000255 /*
256 * Netfront requires there to be some data in the head
257 * buffer.
258 */
Wei Liu33bc8012013-10-08 10:54:21 +0100259 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000260
261 meta = get_next_rx_buffer(vif, npo);
262 }
263
Ian Campbellf942dc22011-03-15 00:06:18 +0000264 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
265 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
266
267 copy_gop = npo->copy + npo->copy_prod++;
268 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100269 copy_gop->len = bytes;
270
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000271 if (foreign_vif) {
272 copy_gop->source.domid = foreign_vif->domid;
273 copy_gop->source.u.ref = foreign_gref;
274 copy_gop->flags |= GNTCOPY_source_gref;
275 } else {
276 copy_gop->source.domid = DOMID_SELF;
277 copy_gop->source.u.gmfn =
278 virt_to_mfn(page_address(page));
279 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000280 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000281
Wei Liub3f980b2013-08-26 12:59:38 +0100282 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000283 copy_gop->dest.offset = npo->copy_off;
284 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000285
286 npo->copy_off += bytes;
287 meta->size += bytes;
288
289 offset += bytes;
290 size -= bytes;
291
Ian Campbell6a8ed462012-10-10 03:48:42 +0000292 /* Next frame */
293 if (offset == PAGE_SIZE && size) {
294 BUG_ON(!PageCompound(page));
295 page++;
296 offset = 0;
297 }
298
Ian Campbellf942dc22011-03-15 00:06:18 +0000299 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100300 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
301 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
302 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
303 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
304 else
305 gso_type = XEN_NETIF_GSO_TYPE_NONE;
306
307 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000308 vif->rx.req_cons++;
309
Wei Liu33bc8012013-10-08 10:54:21 +0100310 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000311
312 }
313}
314
315/*
316 * Prepare an SKB to be transmitted to the frontend.
317 *
318 * This function is responsible for allocating grant operations, meta
319 * structures, etc.
320 *
321 * It returns the number of meta structures consumed. The number of
322 * ring slots used is always equal to the number of meta slots used
323 * plus the number of GSO descriptors used. Currently, we use either
324 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
325 * frontend-side LRO).
326 */
Wei Liu73764192013-08-26 12:59:39 +0100327static int xenvif_gop_skb(struct sk_buff *skb,
328 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000329{
330 struct xenvif *vif = netdev_priv(skb->dev);
331 int nr_frags = skb_shinfo(skb)->nr_frags;
332 int i;
333 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100334 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000335 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100336 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000337 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100338 int gso_type;
339 int gso_size;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000340 struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
341 grant_ref_t foreign_grefs[MAX_SKB_FRAGS];
342 struct xenvif *foreign_vif = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000343
344 old_meta_prod = npo->meta_prod;
345
Paul Durrant82cada22013-10-16 17:50:32 +0100346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 gso_size = skb_shinfo(skb)->gso_size;
349 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
350 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
351 gso_size = skb_shinfo(skb)->gso_size;
352 } else {
353 gso_type = XEN_NETIF_GSO_TYPE_NONE;
354 gso_size = 0;
355 }
356
Ian Campbellf942dc22011-03-15 00:06:18 +0000357 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000358 if ((1 << gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
360 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100361 meta->gso_type = gso_type;
362 meta->gso_size = gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000363 meta->size = 0;
364 meta->id = req->id;
365 }
366
367 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
368 meta = npo->meta + npo->meta_prod++;
369
Paul Durrant82cada22013-10-16 17:50:32 +0100370 if ((1 << gso_type) & vif->gso_mask) {
371 meta->gso_type = gso_type;
372 meta->gso_size = gso_size;
373 } else {
374 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100376 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000377
378 meta->size = 0;
379 meta->id = req->id;
380 npo->copy_off = 0;
381 npo->copy_gref = req->gref;
382
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000383 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
384 (ubuf->callback == &xenvif_zerocopy_callback)) {
385 int i = 0;
386 foreign_vif = ubuf_to_vif(ubuf);
387
388 do {
389 u16 pending_idx = ubuf->desc;
390 foreign_grefs[i++] =
391 foreign_vif->pending_tx_info[pending_idx].req.gref;
392 ubuf = (struct ubuf_info *) ubuf->ctx;
393 } while (ubuf);
394 }
395
Ian Campbellf942dc22011-03-15 00:06:18 +0000396 data = skb->data;
397 while (data < skb_tail_pointer(skb)) {
398 unsigned int offset = offset_in_page(data);
399 unsigned int len = PAGE_SIZE - offset;
400
401 if (data + len > skb_tail_pointer(skb))
402 len = skb_tail_pointer(skb) - data;
403
Wei Liu73764192013-08-26 12:59:39 +0100404 xenvif_gop_frag_copy(vif, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000405 virt_to_page(data), len, offset, &head,
406 NULL,
407 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000408 data += len;
409 }
410
411 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100412 xenvif_gop_frag_copy(vif, skb, npo,
413 skb_frag_page(&skb_shinfo(skb)->frags[i]),
414 skb_frag_size(&skb_shinfo(skb)->frags[i]),
415 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000416 &head,
417 foreign_vif,
418 foreign_grefs[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000419 }
420
421 return npo->meta_prod - old_meta_prod;
422}
423
424/*
Wei Liu73764192013-08-26 12:59:39 +0100425 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000426 * used to set up the operations on the top of
427 * netrx_pending_operations, which have since been done. Check that
428 * they didn't give any errors and advance over them.
429 */
Wei Liu73764192013-08-26 12:59:39 +0100430static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
431 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000432{
433 struct gnttab_copy *copy_op;
434 int status = XEN_NETIF_RSP_OKAY;
435 int i;
436
437 for (i = 0; i < nr_meta_slots; i++) {
438 copy_op = npo->copy + npo->copy_cons++;
439 if (copy_op->status != GNTST_okay) {
440 netdev_dbg(vif->dev,
441 "Bad status %d from copy to DOM%d.\n",
442 copy_op->status, vif->domid);
443 status = XEN_NETIF_RSP_ERROR;
444 }
445 }
446
447 return status;
448}
449
Wei Liu73764192013-08-26 12:59:39 +0100450static void xenvif_add_frag_responses(struct xenvif *vif, int status,
451 struct xenvif_rx_meta *meta,
452 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000453{
454 int i;
455 unsigned long offset;
456
457 /* No fragments used */
458 if (nr_meta_slots <= 1)
459 return;
460
461 nr_meta_slots--;
462
463 for (i = 0; i < nr_meta_slots; i++) {
464 int flags;
465 if (i == nr_meta_slots - 1)
466 flags = 0;
467 else
468 flags = XEN_NETRXF_more_data;
469
470 offset = 0;
471 make_rx_response(vif, meta[i].id, status, offset,
472 meta[i].size, flags);
473 }
474}
475
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000476struct xenvif_rx_cb {
Wei Liu33bc8012013-10-08 10:54:21 +0100477 int meta_slots_used;
478};
479
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000480#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
481
Paul Durrantca2f09f2013-12-06 16:36:07 +0000482void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000483{
Wei Liub3f980b2013-08-26 12:59:38 +0100484 wake_up(&vif->wq);
485}
486
Paul Durrantca2f09f2013-12-06 16:36:07 +0000487static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100488{
Ian Campbellf942dc22011-03-15 00:06:18 +0000489 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000490 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000491 struct xen_netif_rx_response *resp;
492 struct sk_buff_head rxq;
493 struct sk_buff *skb;
494 LIST_HEAD(notify);
495 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000496 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000497 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000498
499 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100500 .copy = vif->grant_copy_op,
501 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000502 };
503
504 skb_queue_head_init(&rxq);
505
Wei Liub3f980b2013-08-26 12:59:38 +0100506 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000507 RING_IDX max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000508 int i;
509
510 /* We need a cheap worse case estimate for the number of
511 * slots we'll use.
512 */
513
514 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
515 skb_headlen(skb),
516 PAGE_SIZE);
517 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
518 unsigned int size;
519 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
520 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
521 }
522 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
523 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
524 max_slots_needed++;
525
526 /* If the skb may not fit then bail out now */
527 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
528 skb_queue_head(&vif->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000529 need_to_notify = true;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000530 vif->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000531 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000532 } else
533 vif->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000534
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000535 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
536 BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000537
538 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000539 }
540
Wei Liub3f980b2013-08-26 12:59:38 +0100541 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000542
543 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000544 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000545
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000546 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liub3f980b2013-08-26 12:59:38 +0100547 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000548
549 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000550
Paul Durrant82cada22013-10-16 17:50:32 +0100551 if ((1 << vif->meta[npo.meta_cons].gso_type) &
552 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000553 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100554 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000555
556 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
557
Wei Liub3f980b2013-08-26 12:59:38 +0100558 resp->offset = vif->meta[npo.meta_cons].gso_size;
559 resp->id = vif->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000560 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000561
562 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000563 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000564 }
565
566
567 vif->dev->stats.tx_bytes += skb->len;
568 vif->dev->stats.tx_packets++;
569
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000570 status = xenvif_check_gop(vif,
571 XENVIF_RX_CB(skb)->meta_slots_used,
572 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000573
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000574 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000575 flags = 0;
576 else
577 flags = XEN_NETRXF_more_data;
578
579 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
580 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
581 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
582 /* remote but checksummed. */
583 flags |= XEN_NETRXF_data_validated;
584
585 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100586 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000587 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100588 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000589 flags);
590
Paul Durrant82cada22013-10-16 17:50:32 +0100591 if ((1 << vif->meta[npo.meta_cons].gso_type) &
592 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000593 struct xen_netif_extra_info *gso =
594 (struct xen_netif_extra_info *)
595 RING_GET_RESPONSE(&vif->rx,
596 vif->rx.rsp_prod_pvt++);
597
598 resp->flags |= XEN_NETRXF_extra_info;
599
Paul Durrant82cada22013-10-16 17:50:32 +0100600 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100601 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000602 gso->u.gso.pad = 0;
603 gso->u.gso.features = 0;
604
605 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
606 gso->flags = 0;
607 }
608
Wei Liu73764192013-08-26 12:59:39 +0100609 xenvif_add_frag_responses(vif, status,
610 vif->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000611 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000612
613 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000614
Paul Durrant11b57f92014-01-08 12:41:58 +0000615 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100616
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000617 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000618 dev_kfree_skb(skb);
619 }
620
Paul Durrantca2f09f2013-12-06 16:36:07 +0000621done:
Wei Liub3f980b2013-08-26 12:59:38 +0100622 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000623 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000624}
625
Wei Liu73764192013-08-26 12:59:39 +0100626void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000627{
628 int more_to_do;
629
630 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
631
632 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100633 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000634}
635
636static void tx_add_credit(struct xenvif *vif)
637{
638 unsigned long max_burst, max_credit;
639
640 /*
641 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
642 * Otherwise the interface can seize up due to insufficient credit.
643 */
644 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
645 max_burst = min(max_burst, 131072UL);
646 max_burst = max(max_burst, vif->credit_bytes);
647
648 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
649 max_credit = vif->remaining_credit + vif->credit_bytes;
650 if (max_credit < vif->remaining_credit)
651 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
652
653 vif->remaining_credit = min(max_credit, max_burst);
654}
655
656static void tx_credit_callback(unsigned long data)
657{
658 struct xenvif *vif = (struct xenvif *)data;
659 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100660 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000661}
662
Wei Liu73764192013-08-26 12:59:39 +0100663static void xenvif_tx_err(struct xenvif *vif,
664 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000665{
666 RING_IDX cons = vif->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000667 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000668
669 do {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000670 spin_lock_irqsave(&vif->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +0000671 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000672 spin_unlock_irqrestore(&vif->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000673 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000674 break;
675 txp = RING_GET_REQUEST(&vif->tx, cons++);
676 } while (1);
677 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000678}
679
Wei Liu73764192013-08-26 12:59:39 +0100680static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000681{
682 netdev_err(vif->dev, "fatal error; disabling device\n");
683 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000684}
685
Wei Liu73764192013-08-26 12:59:39 +0100686static int xenvif_count_requests(struct xenvif *vif,
687 struct xen_netif_tx_request *first,
688 struct xen_netif_tx_request *txp,
689 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000690{
691 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000692 int slots = 0;
693 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000694 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000695
696 if (!(first->flags & XEN_NETTXF_more_data))
697 return 0;
698
699 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000700 struct xen_netif_tx_request dropped_tx = { 0 };
701
Wei Liu2810e5b2013-04-22 02:20:42 +0000702 if (slots >= work_to_do) {
703 netdev_err(vif->dev,
704 "Asked for %d slots but exceeds this limit\n",
705 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100706 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000707 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000708 }
709
Wei Liu2810e5b2013-04-22 02:20:42 +0000710 /* This guest is really using too many slots and
711 * considered malicious.
712 */
Wei Liu37641492013-05-02 00:43:59 +0000713 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000714 netdev_err(vif->dev,
715 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000716 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100717 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000718 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000719 }
720
Wei Liu2810e5b2013-04-22 02:20:42 +0000721 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000722 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
723 * the historical MAX_SKB_FRAGS value 18 to honor the
724 * same behavior as before. Any packet using more than
725 * 18 slots but less than fatal_skb_slots slots is
726 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000727 */
Wei Liu37641492013-05-02 00:43:59 +0000728 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000729 if (net_ratelimit())
730 netdev_dbg(vif->dev,
731 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000732 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000733 drop_err = -E2BIG;
734 }
735
Wei Liu59ccb4e2013-05-02 00:43:58 +0000736 if (drop_err)
737 txp = &dropped_tx;
738
Wei Liu2810e5b2013-04-22 02:20:42 +0000739 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000740 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000741
742 /* If the guest submitted a frame >= 64 KiB then
743 * first->size overflowed and following slots will
744 * appear to be larger than the frame.
745 *
746 * This cannot be fatal error as there are buggy
747 * frontends that do this.
748 *
749 * Consume all slots and drop the packet.
750 */
751 if (!drop_err && txp->size > first->size) {
752 if (net_ratelimit())
753 netdev_dbg(vif->dev,
754 "Invalid tx request, slot size %u > remaining size %u\n",
755 txp->size, first->size);
756 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000757 }
758
759 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000760 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000761
762 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000763 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000764 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100765 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000766 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000767 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000768
769 more_data = txp->flags & XEN_NETTXF_more_data;
770
771 if (!drop_err)
772 txp++;
773
774 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000775
776 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100777 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000778 return drop_err;
779 }
780
781 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000782}
783
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000784
785struct xenvif_tx_cb {
786 u16 pending_idx;
787};
788
789#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
790
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000791static inline void xenvif_tx_create_gop(struct xenvif *vif,
792 u16 pending_idx,
793 struct xen_netif_tx_request *txp,
794 struct gnttab_map_grant_ref *gop)
795{
796 vif->pages_to_map[gop-vif->tx_map_ops] = vif->mmap_pages[pending_idx];
797 gnttab_set_map_op(gop, idx_to_kaddr(vif, pending_idx),
798 GNTMAP_host_map | GNTMAP_readonly,
799 txp->gref, vif->domid);
800
801 memcpy(&vif->pending_tx_info[pending_idx].req, txp,
802 sizeof(*txp));
803}
804
Zoltan Kisse3377f32014-03-06 21:48:29 +0000805static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
806{
807 struct sk_buff *skb =
808 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
809 GFP_ATOMIC | __GFP_NOWARN);
810 if (unlikely(skb == NULL))
811 return NULL;
812
813 /* Packets passed to netif_rx() must have some headroom. */
814 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
815
816 /* Initialize it here to avoid later surprises */
817 skb_shinfo(skb)->destructor_arg = NULL;
818
819 return skb;
820}
821
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000822static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif *vif,
823 struct sk_buff *skb,
824 struct xen_netif_tx_request *txp,
825 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000826{
827 struct skb_shared_info *shinfo = skb_shinfo(skb);
828 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000829 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000830 int start;
831 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000832 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000833
834 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000835 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000836 */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000837 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
838 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
839 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
840 shinfo->nr_frags = MAX_SKB_FRAGS;
841 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000842 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000843
844 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000845 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000846
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000847 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
848 shinfo->nr_frags++, txp++, gop++) {
Zoltan Kiss62bad312014-03-06 21:48:27 +0000849 index = pending_index(vif->pending_cons++);
850 pending_idx = vif->pending_ring[index];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000851 xenvif_tx_create_gop(vif, pending_idx, txp, gop);
852 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000853 }
854
Zoltan Kisse3377f32014-03-06 21:48:29 +0000855 if (frag_overflow) {
856 struct sk_buff *nskb = xenvif_alloc_skb(0);
857 if (unlikely(nskb == NULL)) {
858 if (net_ratelimit())
859 netdev_err(vif->dev,
860 "Can't allocate the frag_list skb.\n");
861 return NULL;
862 }
863
864 shinfo = skb_shinfo(nskb);
865 frags = shinfo->frags;
866
867 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
868 shinfo->nr_frags++, txp++, gop++) {
869 index = pending_index(vif->pending_cons++);
870 pending_idx = vif->pending_ring[index];
871 xenvif_tx_create_gop(vif, pending_idx, txp, gop);
872 frag_set_pending_idx(&frags[shinfo->nr_frags],
873 pending_idx);
874 }
875
876 skb_shinfo(skb)->frag_list = nskb;
877 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000878
Ian Campbellf942dc22011-03-15 00:06:18 +0000879 return gop;
880}
881
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000882static inline void xenvif_grant_handle_set(struct xenvif *vif,
883 u16 pending_idx,
884 grant_handle_t handle)
885{
886 if (unlikely(vif->grant_tx_handle[pending_idx] !=
887 NETBACK_INVALID_HANDLE)) {
888 netdev_err(vif->dev,
889 "Trying to overwrite active handle! pending_idx: %x\n",
890 pending_idx);
891 BUG();
892 }
893 vif->grant_tx_handle[pending_idx] = handle;
894}
895
896static inline void xenvif_grant_handle_reset(struct xenvif *vif,
897 u16 pending_idx)
898{
899 if (unlikely(vif->grant_tx_handle[pending_idx] ==
900 NETBACK_INVALID_HANDLE)) {
901 netdev_err(vif->dev,
902 "Trying to unmap invalid handle! pending_idx: %x\n",
903 pending_idx);
904 BUG();
905 }
906 vif->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
907}
908
Wei Liu73764192013-08-26 12:59:39 +0100909static int xenvif_tx_check_gop(struct xenvif *vif,
910 struct sk_buff *skb,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000911 struct gnttab_map_grant_ref **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000912{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000913 struct gnttab_map_grant_ref *gop = *gopp;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000914 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000915 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000916 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000917 int nr_frags = shinfo->nr_frags;
918 int i, err, start;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000919 struct sk_buff *first_skb = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000920
921 /* Check status of header. */
922 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000923 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100924 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000925 else
926 xenvif_grant_handle_set(vif, pending_idx , gop->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000927
928 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000929 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000930
Zoltan Kisse3377f32014-03-06 21:48:29 +0000931check_frags:
Ian Campbellf942dc22011-03-15 00:06:18 +0000932 for (i = start; i < nr_frags; i++) {
933 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +0000934
Ian Campbellea066ad2011-10-05 00:28:46 +0000935 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100936 tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +0000937
938 /* Check error status: if okay then remember grant handle. */
Zoltan Kiss62bad312014-03-06 21:48:27 +0000939 newerr = (++gop)->status;
Wei Liu2810e5b2013-04-22 02:20:42 +0000940
Ian Campbellf942dc22011-03-15 00:06:18 +0000941 if (likely(!newerr)) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000942 xenvif_grant_handle_set(vif, pending_idx , gop->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000943 /* Had a previous error? Invalidate this fragment. */
944 if (unlikely(err))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000945 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000946 continue;
947 }
948
949 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100950 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000951
952 /* Not the first error? Preceding frags already invalidated. */
953 if (err)
954 continue;
Ian Campbellf942dc22011-03-15 00:06:18 +0000955 /* First error: invalidate header and preceding fragments. */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000956 if (!first_skb)
957 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
958 else
959 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000960 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000961 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000962 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000963 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000964 }
965
966 /* Remember the error: invalidate all subsequent fragments. */
967 err = newerr;
968 }
969
Zoltan Kisse3377f32014-03-06 21:48:29 +0000970 if (skb_has_frag_list(skb)) {
971 first_skb = skb;
972 skb = shinfo->frag_list;
973 shinfo = skb_shinfo(skb);
974 nr_frags = shinfo->nr_frags;
975 start = 0;
976
977 goto check_frags;
978 }
979
980 /* There was a mapping error in the frag_list skb. We have to unmap
981 * the first skb's frags
982 */
983 if (first_skb && err) {
984 int j;
985 shinfo = skb_shinfo(first_skb);
986 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
987 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
988 for (j = start; j < shinfo->nr_frags; j++) {
989 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
990 xenvif_idx_unmap(vif, pending_idx);
991 }
992 }
993
Ian Campbellf942dc22011-03-15 00:06:18 +0000994 *gopp = gop + 1;
995 return err;
996}
997
Wei Liu73764192013-08-26 12:59:39 +0100998static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000999{
1000 struct skb_shared_info *shinfo = skb_shinfo(skb);
1001 int nr_frags = shinfo->nr_frags;
1002 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001003 u16 prev_pending_idx = INVALID_PENDING_IDX;
1004
1005 if (skb_shinfo(skb)->destructor_arg)
1006 prev_pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001007
1008 for (i = 0; i < nr_frags; i++) {
1009 skb_frag_t *frag = shinfo->frags + i;
1010 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001011 struct page *page;
1012 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001013
Ian Campbellea066ad2011-10-05 00:28:46 +00001014 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001015
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001016 /* If this is not the first frag, chain it to the previous*/
1017 if (unlikely(prev_pending_idx == INVALID_PENDING_IDX))
1018 skb_shinfo(skb)->destructor_arg =
1019 &vif->pending_tx_info[pending_idx].callback_struct;
1020 else if (likely(pending_idx != prev_pending_idx))
1021 vif->pending_tx_info[prev_pending_idx].callback_struct.ctx =
1022 &(vif->pending_tx_info[pending_idx].callback_struct);
1023
1024 vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
1025 prev_pending_idx = pending_idx;
1026
Wei Liub3f980b2013-08-26 12:59:38 +01001027 txp = &vif->pending_tx_info[pending_idx].req;
1028 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001029 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001030 skb->len += txp->size;
1031 skb->data_len += txp->size;
1032 skb->truesize += txp->size;
1033
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001034 /* Take an extra reference to offset network stack's put_page */
Wei Liub3f980b2013-08-26 12:59:38 +01001035 get_page(vif->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001036 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001037 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1038 * overlaps with "index", and "mapping" is not set. I think mapping
1039 * should be set. If delivered to local stack, it would drop this
1040 * skb in sk_filter unless the socket has the right to use it.
1041 */
1042 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001043}
1044
Wei Liu73764192013-08-26 12:59:39 +01001045static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +00001046 struct xen_netif_extra_info *extras,
1047 int work_to_do)
1048{
1049 struct xen_netif_extra_info extra;
1050 RING_IDX cons = vif->tx.req_cons;
1051
1052 do {
1053 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001054 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001055 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001056 return -EBADR;
1057 }
1058
1059 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1060 sizeof(extra));
1061 if (unlikely(!extra.type ||
1062 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1063 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001064 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001065 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001066 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001067 return -EINVAL;
1068 }
1069
1070 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1071 vif->tx.req_cons = ++cons;
1072 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1073
1074 return work_to_do;
1075}
1076
Wei Liu73764192013-08-26 12:59:39 +01001077static int xenvif_set_skb_gso(struct xenvif *vif,
1078 struct sk_buff *skb,
1079 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001080{
1081 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001082 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001083 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001084 return -EINVAL;
1085 }
1086
Paul Durranta9468582013-10-16 17:50:31 +01001087 switch (gso->u.gso.type) {
1088 case XEN_NETIF_GSO_TYPE_TCPV4:
1089 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1090 break;
1091 case XEN_NETIF_GSO_TYPE_TCPV6:
1092 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1093 break;
1094 default:
Ian Campbell488562862013-02-06 23:41:35 +00001095 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001096 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001097 return -EINVAL;
1098 }
1099
1100 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001101 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001102
1103 return 0;
1104}
1105
1106static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1107{
Paul Durrant27216372014-01-09 10:02:47 +00001108 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001109
Paul Durrant2eba61d2013-10-16 17:50:29 +01001110 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001111 * peers can fail to set NETRXF_csum_blank when sending a GSO
1112 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1113 * recalculate the partial checksum.
1114 */
1115 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1116 vif->rx_gso_checksum_fixup++;
1117 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001118 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001119 }
1120
1121 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1122 if (skb->ip_summed != CHECKSUM_PARTIAL)
1123 return 0;
1124
Paul Durrant27216372014-01-09 10:02:47 +00001125 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001126}
1127
1128static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1129{
Wei Liu059dfa62013-10-28 12:07:57 +00001130 u64 now = get_jiffies_64();
1131 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001132 msecs_to_jiffies(vif->credit_usec / 1000);
1133
1134 /* Timer could already be pending in rare cases. */
1135 if (timer_pending(&vif->credit_timeout))
1136 return true;
1137
1138 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001139 if (time_after_eq64(now, next_credit)) {
1140 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001141 tx_add_credit(vif);
1142 }
1143
1144 /* Still too big to send right now? Set a callback. */
1145 if (size > vif->remaining_credit) {
1146 vif->credit_timeout.data =
1147 (unsigned long)vif;
1148 vif->credit_timeout.function =
1149 tx_credit_callback;
1150 mod_timer(&vif->credit_timeout,
1151 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001152 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001153
1154 return true;
1155 }
1156
1157 return false;
1158}
1159
Paul Durrant10574052013-12-11 10:57:15 +00001160static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001161{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001162 struct gnttab_map_grant_ref *gop = vif->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001163 struct sk_buff *skb;
1164 int ret;
1165
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001166 while (xenvif_tx_pending_slots_available(vif) &&
Paul Durrant10574052013-12-11 10:57:15 +00001167 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001168 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001169 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001170 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1171 u16 pending_idx;
1172 RING_IDX idx;
1173 int work_to_do;
1174 unsigned int data_len;
1175 pending_ring_idx_t index;
1176
Ian Campbell488562862013-02-06 23:41:35 +00001177 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1178 XEN_NETIF_TX_RING_SIZE) {
1179 netdev_err(vif->dev,
1180 "Impossible number of requests. "
1181 "req_prod %d, req_cons %d, size %ld\n",
1182 vif->tx.sring->req_prod, vif->tx.req_cons,
1183 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001184 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001185 continue;
1186 }
1187
Paul Durrantd9601a32013-12-11 10:57:16 +00001188 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001189 if (!work_to_do)
1190 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001191
1192 idx = vif->tx.req_cons;
1193 rmb(); /* Ensure that we see the request before we copy it. */
1194 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1195
1196 /* Credit-based scheduling. */
1197 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001198 tx_credit_exceeded(vif, txreq.size))
1199 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001200
1201 vif->remaining_credit -= txreq.size;
1202
1203 work_to_do--;
1204 vif->tx.req_cons = ++idx;
1205
1206 memset(extras, 0, sizeof(extras));
1207 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001208 work_to_do = xenvif_get_extras(vif, extras,
1209 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001210 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001211 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001212 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001213 }
1214
Wei Liu73764192013-08-26 12:59:39 +01001215 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001216 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001217 break;
Ian Campbell488562862013-02-06 23:41:35 +00001218
Ian Campbellf942dc22011-03-15 00:06:18 +00001219 idx += ret;
1220
1221 if (unlikely(txreq.size < ETH_HLEN)) {
1222 netdev_dbg(vif->dev,
1223 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001224 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001225 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001226 }
1227
1228 /* No crossing a page as the payload mustn't fragment. */
1229 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001230 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001231 "txreq.offset: %x, size: %u, end: %lu\n",
1232 txreq.offset, txreq.size,
1233 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001234 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001235 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001236 }
1237
Wei Liub3f980b2013-08-26 12:59:38 +01001238 index = pending_index(vif->pending_cons);
1239 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001240
1241 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001242 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001243 PKT_PROT_LEN : txreq.size;
1244
Zoltan Kisse3377f32014-03-06 21:48:29 +00001245 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001246 if (unlikely(skb == NULL)) {
1247 netdev_dbg(vif->dev,
1248 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001249 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001250 break;
1251 }
1252
Ian Campbellf942dc22011-03-15 00:06:18 +00001253 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1254 struct xen_netif_extra_info *gso;
1255 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1256
Wei Liu73764192013-08-26 12:59:39 +01001257 if (xenvif_set_skb_gso(vif, skb, gso)) {
1258 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001259 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001260 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001261 }
1262 }
1263
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001264 xenvif_tx_create_gop(vif, pending_idx, &txreq, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001265
1266 gop++;
1267
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001268 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001269
1270 __skb_put(skb, data_len);
1271
1272 skb_shinfo(skb)->nr_frags = ret;
1273 if (data_len < txreq.size) {
1274 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001275 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1276 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001277 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001278 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1279 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001280 }
1281
Wei Liub3f980b2013-08-26 12:59:38 +01001282 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001283
Wei Liu73764192013-08-26 12:59:39 +01001284 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001285 if (request_gop == NULL) {
1286 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001287 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001288 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001289 }
1290 gop = request_gop;
1291
Wei Liub3f980b2013-08-26 12:59:38 +01001292 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001293
Ian Campbellf942dc22011-03-15 00:06:18 +00001294 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001295
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001296 if ((gop-vif->tx_map_ops) >= ARRAY_SIZE(vif->tx_map_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001297 break;
1298 }
1299
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001300 return gop - vif->tx_map_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001301}
1302
Zoltan Kisse3377f32014-03-06 21:48:29 +00001303/* Consolidate skb with a frag_list into a brand new one with local pages on
1304 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1305 */
1306static int xenvif_handle_frag_list(struct xenvif *vif, struct sk_buff *skb)
1307{
1308 unsigned int offset = skb_headlen(skb);
1309 skb_frag_t frags[MAX_SKB_FRAGS];
1310 int i;
1311 struct ubuf_info *uarg;
1312 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1313
1314 vif->tx_zerocopy_sent += 2;
1315 vif->tx_frag_overflow++;
1316
1317 xenvif_fill_frags(vif, nskb);
1318 /* Subtract frags size, we will correct it later */
1319 skb->truesize -= skb->data_len;
1320 skb->len += nskb->len;
1321 skb->data_len += nskb->len;
1322
1323 /* create a brand new frags array and coalesce there */
1324 for (i = 0; offset < skb->len; i++) {
1325 struct page *page;
1326 unsigned int len;
1327
1328 BUG_ON(i >= MAX_SKB_FRAGS);
1329 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1330 if (!page) {
1331 int j;
1332 skb->truesize += skb->data_len;
1333 for (j = 0; j < i; j++)
1334 put_page(frags[j].page.p);
1335 return -ENOMEM;
1336 }
1337
1338 if (offset + PAGE_SIZE < skb->len)
1339 len = PAGE_SIZE;
1340 else
1341 len = skb->len - offset;
1342 if (skb_copy_bits(skb, offset, page_address(page), len))
1343 BUG();
1344
1345 offset += len;
1346 frags[i].page.p = page;
1347 frags[i].page_offset = 0;
1348 skb_frag_size_set(&frags[i], len);
1349 }
1350 /* swap out with old one */
1351 memcpy(skb_shinfo(skb)->frags,
1352 frags,
1353 i * sizeof(skb_frag_t));
1354 skb_shinfo(skb)->nr_frags = i;
1355 skb->truesize += i * PAGE_SIZE;
1356
1357 /* remove traces of mapped pages and frag_list */
1358 skb_frag_list_init(skb);
1359 uarg = skb_shinfo(skb)->destructor_arg;
1360 uarg->callback(uarg, true);
1361 skb_shinfo(skb)->destructor_arg = NULL;
1362
1363 skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1364 kfree_skb(nskb);
1365
1366 return 0;
1367}
Ian Campbellf942dc22011-03-15 00:06:18 +00001368
Paul Durrant10574052013-12-11 10:57:15 +00001369static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001370{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001371 struct gnttab_map_grant_ref *gop = vif->tx_map_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001372 struct sk_buff *skb;
1373 int work_done = 0;
1374
Paul Durrant10574052013-12-11 10:57:15 +00001375 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001376 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001377 u16 pending_idx;
1378 unsigned data_len;
1379
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001380 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liub3f980b2013-08-26 12:59:38 +01001381 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001382
1383 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001384 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001385 netdev_dbg(vif->dev, "netback grant failed.\n");
1386 skb_shinfo(skb)->nr_frags = 0;
1387 kfree_skb(skb);
1388 continue;
1389 }
1390
1391 data_len = skb->len;
1392 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001393 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001394 data_len);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001395 vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001396 if (data_len < txp->size) {
1397 /* Append the packet payload as a fragment. */
1398 txp->offset += data_len;
1399 txp->size -= data_len;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001400 skb_shinfo(skb)->destructor_arg =
1401 &vif->pending_tx_info[pending_idx].callback_struct;
Ian Campbellf942dc22011-03-15 00:06:18 +00001402 } else {
1403 /* Schedule a response immediately. */
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001404 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001405 }
1406
1407 if (txp->flags & XEN_NETTXF_csum_blank)
1408 skb->ip_summed = CHECKSUM_PARTIAL;
1409 else if (txp->flags & XEN_NETTXF_data_validated)
1410 skb->ip_summed = CHECKSUM_UNNECESSARY;
1411
Wei Liu73764192013-08-26 12:59:39 +01001412 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001413
Zoltan Kisse3377f32014-03-06 21:48:29 +00001414 if (unlikely(skb_has_frag_list(skb))) {
1415 if (xenvif_handle_frag_list(vif, skb)) {
1416 if (net_ratelimit())
1417 netdev_err(vif->dev,
1418 "Not enough memory to consolidate frag_list!\n");
1419 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1420 kfree_skb(skb);
1421 continue;
1422 }
1423 }
1424
Paul Durrant2eba61d2013-10-16 17:50:29 +01001425 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001426 int target = min_t(int, skb->len, PKT_PROT_LEN);
1427 __pskb_pull_tail(skb, target - skb_headlen(skb));
1428 }
1429
1430 skb->dev = vif->dev;
1431 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001432 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001433
1434 if (checksum_setup(vif, skb)) {
1435 netdev_dbg(vif->dev,
1436 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001437 /* We have to set this flag to trigger the callback */
1438 if (skb_shinfo(skb)->destructor_arg)
1439 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Ian Campbellf942dc22011-03-15 00:06:18 +00001440 kfree_skb(skb);
1441 continue;
1442 }
1443
Jason Wang40893fd2013-03-26 23:11:22 +00001444 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001445
Paul Durrantb89587a2013-12-17 11:44:35 +00001446 /* If the packet is GSO then we will have just set up the
1447 * transport header offset in checksum_setup so it's now
1448 * straightforward to calculate gso_segs.
1449 */
1450 if (skb_is_gso(skb)) {
1451 int mss = skb_shinfo(skb)->gso_size;
1452 int hdrlen = skb_transport_header(skb) -
1453 skb_mac_header(skb) +
1454 tcp_hdrlen(skb);
1455
1456 skb_shinfo(skb)->gso_segs =
1457 DIV_ROUND_UP(skb->len - hdrlen, mss);
1458 }
1459
Ian Campbellf942dc22011-03-15 00:06:18 +00001460 vif->dev->stats.rx_bytes += skb->len;
1461 vif->dev->stats.rx_packets++;
1462
Wei Liub3f980b2013-08-26 12:59:38 +01001463 work_done++;
1464
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001465 /* Set this flag right before netif_receive_skb, otherwise
1466 * someone might think this packet already left netback, and
1467 * do a skb_copy_ubufs while we are still in control of the
1468 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1469 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001470 if (skb_shinfo(skb)->destructor_arg) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001471 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001472 vif->tx_zerocopy_sent++;
1473 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001474
Wei Liub3f980b2013-08-26 12:59:38 +01001475 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001476 }
Wei Liub3f980b2013-08-26 12:59:38 +01001477
1478 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001479}
1480
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001481void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1482{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001483 unsigned long flags;
1484 pending_ring_idx_t index;
1485 struct xenvif *vif = ubuf_to_vif(ubuf);
1486
1487 /* This is the only place where we grab this lock, to protect callbacks
1488 * from each other.
1489 */
1490 spin_lock_irqsave(&vif->callback_lock, flags);
1491 do {
1492 u16 pending_idx = ubuf->desc;
1493 ubuf = (struct ubuf_info *) ubuf->ctx;
1494 BUG_ON(vif->dealloc_prod - vif->dealloc_cons >=
1495 MAX_PENDING_REQS);
1496 index = pending_index(vif->dealloc_prod);
1497 vif->dealloc_ring[index] = pending_idx;
1498 /* Sync with xenvif_tx_dealloc_action:
1499 * insert idx then incr producer.
1500 */
1501 smp_wmb();
1502 vif->dealloc_prod++;
1503 } while (ubuf);
1504 wake_up(&vif->dealloc_wq);
1505 spin_unlock_irqrestore(&vif->callback_lock, flags);
1506
1507 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx) &&
1508 xenvif_tx_pending_slots_available(vif)) {
1509 local_bh_disable();
1510 napi_schedule(&vif->napi);
1511 local_bh_enable();
1512 }
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001513
1514 if (likely(zerocopy_success))
1515 vif->tx_zerocopy_success++;
1516 else
1517 vif->tx_zerocopy_fail++;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001518}
1519
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001520static inline void xenvif_tx_dealloc_action(struct xenvif *vif)
1521{
1522 struct gnttab_unmap_grant_ref *gop;
1523 pending_ring_idx_t dc, dp;
1524 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1525 unsigned int i = 0;
1526
1527 dc = vif->dealloc_cons;
1528 gop = vif->tx_unmap_ops;
1529
1530 /* Free up any grants we have finished using */
1531 do {
1532 dp = vif->dealloc_prod;
1533
1534 /* Ensure we see all indices enqueued by all
1535 * xenvif_zerocopy_callback().
1536 */
1537 smp_rmb();
1538
1539 while (dc != dp) {
1540 BUG_ON(gop - vif->tx_unmap_ops > MAX_PENDING_REQS);
1541 pending_idx =
1542 vif->dealloc_ring[pending_index(dc++)];
1543
1544 pending_idx_release[gop-vif->tx_unmap_ops] =
1545 pending_idx;
1546 vif->pages_to_unmap[gop-vif->tx_unmap_ops] =
1547 vif->mmap_pages[pending_idx];
1548 gnttab_set_unmap_op(gop,
1549 idx_to_kaddr(vif, pending_idx),
1550 GNTMAP_host_map,
1551 vif->grant_tx_handle[pending_idx]);
1552 /* Btw. already unmapped? */
1553 xenvif_grant_handle_reset(vif, pending_idx);
1554 ++gop;
1555 }
1556
1557 } while (dp != vif->dealloc_prod);
1558
1559 vif->dealloc_cons = dc;
1560
1561 if (gop - vif->tx_unmap_ops > 0) {
1562 int ret;
1563 ret = gnttab_unmap_refs(vif->tx_unmap_ops,
1564 NULL,
1565 vif->pages_to_unmap,
1566 gop - vif->tx_unmap_ops);
1567 if (ret) {
1568 netdev_err(vif->dev, "Unmap fail: nr_ops %x ret %d\n",
1569 gop - vif->tx_unmap_ops, ret);
1570 for (i = 0; i < gop - vif->tx_unmap_ops; ++i) {
1571 if (gop[i].status != GNTST_okay)
1572 netdev_err(vif->dev,
1573 " host_addr: %llx handle: %x status: %d\n",
1574 gop[i].host_addr,
1575 gop[i].handle,
1576 gop[i].status);
1577 }
1578 BUG();
1579 }
1580 }
1581
1582 for (i = 0; i < gop - vif->tx_unmap_ops; ++i)
1583 xenvif_idx_release(vif, pending_idx_release[i],
1584 XEN_NETIF_RSP_OKAY);
1585}
1586
1587
Ian Campbellf942dc22011-03-15 00:06:18 +00001588/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001589int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001590{
1591 unsigned nr_gops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001592 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001593
Wei Liub3f980b2013-08-26 12:59:38 +01001594 if (unlikely(!tx_work_todo(vif)))
1595 return 0;
1596
Paul Durrant10574052013-12-11 10:57:15 +00001597 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001598
1599 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001600 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001601
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001602 ret = gnttab_map_refs(vif->tx_map_ops,
1603 NULL,
1604 vif->pages_to_map,
1605 nr_gops);
1606 BUG_ON(ret);
Ian Campbellf942dc22011-03-15 00:06:18 +00001607
Paul Durrant10574052013-12-11 10:57:15 +00001608 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001609
1610 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001611}
1612
Wei Liu73764192013-08-26 12:59:39 +01001613static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1614 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001615{
Ian Campbellf942dc22011-03-15 00:06:18 +00001616 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001617 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001618 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001619
Zoltan Kiss62bad312014-03-06 21:48:27 +00001620 pending_tx_info = &vif->pending_tx_info[pending_idx];
1621 spin_lock_irqsave(&vif->response_lock, flags);
1622 make_tx_response(vif, &pending_tx_info->req, status);
1623 index = pending_index(vif->pending_prod);
1624 vif->pending_ring[index] = pending_idx;
1625 /* TX shouldn't use the index before we give it back here */
1626 mb();
1627 vif->pending_prod++;
1628 spin_unlock_irqrestore(&vif->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001629}
1630
Wei Liu2810e5b2013-04-22 02:20:42 +00001631
Ian Campbellf942dc22011-03-15 00:06:18 +00001632static void make_tx_response(struct xenvif *vif,
1633 struct xen_netif_tx_request *txp,
1634 s8 st)
1635{
1636 RING_IDX i = vif->tx.rsp_prod_pvt;
1637 struct xen_netif_tx_response *resp;
1638 int notify;
1639
1640 resp = RING_GET_RESPONSE(&vif->tx, i);
1641 resp->id = txp->id;
1642 resp->status = st;
1643
1644 if (txp->flags & XEN_NETTXF_extra_info)
1645 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1646
1647 vif->tx.rsp_prod_pvt = ++i;
1648 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1649 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001650 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001651}
1652
1653static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1654 u16 id,
1655 s8 st,
1656 u16 offset,
1657 u16 size,
1658 u16 flags)
1659{
1660 RING_IDX i = vif->rx.rsp_prod_pvt;
1661 struct xen_netif_rx_response *resp;
1662
1663 resp = RING_GET_RESPONSE(&vif->rx, i);
1664 resp->offset = offset;
1665 resp->flags = flags;
1666 resp->id = id;
1667 resp->status = (s16)size;
1668 if (st < 0)
1669 resp->status = (s16)st;
1670
1671 vif->rx.rsp_prod_pvt = ++i;
1672
1673 return resp;
1674}
1675
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001676void xenvif_idx_unmap(struct xenvif *vif, u16 pending_idx)
1677{
1678 int ret;
1679 struct gnttab_unmap_grant_ref tx_unmap_op;
1680
1681 gnttab_set_unmap_op(&tx_unmap_op,
1682 idx_to_kaddr(vif, pending_idx),
1683 GNTMAP_host_map,
1684 vif->grant_tx_handle[pending_idx]);
1685 /* Btw. already unmapped? */
1686 xenvif_grant_handle_reset(vif, pending_idx);
1687
1688 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1689 &vif->mmap_pages[pending_idx], 1);
1690 BUG_ON(ret);
1691
1692 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1693}
1694
Wei Liub3f980b2013-08-26 12:59:38 +01001695static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001696{
Zoltan Kiss9ab98312014-02-04 19:54:37 +00001697 return !skb_queue_empty(&vif->rx_queue) &&
1698 xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
Ian Campbellf942dc22011-03-15 00:06:18 +00001699}
1700
Wei Liub3f980b2013-08-26 12:59:38 +01001701static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001702{
1703
Wei Liub3f980b2013-08-26 12:59:38 +01001704 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001705 xenvif_tx_pending_slots_available(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +00001706 return 1;
1707
1708 return 0;
1709}
1710
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001711static inline bool tx_dealloc_work_todo(struct xenvif *vif)
1712{
1713 return vif->dealloc_cons != vif->dealloc_prod;
1714}
1715
Wei Liu73764192013-08-26 12:59:39 +01001716void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001717{
David Vrabelc9d63692011-09-29 16:53:31 +01001718 if (vif->tx.sring)
1719 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1720 vif->tx.sring);
1721 if (vif->rx.sring)
1722 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1723 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001724}
1725
Wei Liu73764192013-08-26 12:59:39 +01001726int xenvif_map_frontend_rings(struct xenvif *vif,
1727 grant_ref_t tx_ring_ref,
1728 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001729{
David Vrabelc9d63692011-09-29 16:53:31 +01001730 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001731 struct xen_netif_tx_sring *txs;
1732 struct xen_netif_rx_sring *rxs;
1733
1734 int err = -ENOMEM;
1735
David Vrabelc9d63692011-09-29 16:53:31 +01001736 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1737 tx_ring_ref, &addr);
1738 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001739 goto err;
1740
David Vrabelc9d63692011-09-29 16:53:31 +01001741 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001742 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1743
David Vrabelc9d63692011-09-29 16:53:31 +01001744 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1745 rx_ring_ref, &addr);
1746 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001747 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001748
David Vrabelc9d63692011-09-29 16:53:31 +01001749 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001750 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1751
1752 return 0;
1753
1754err:
Wei Liu73764192013-08-26 12:59:39 +01001755 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001756 return err;
1757}
1758
Paul Durrantca2f09f2013-12-06 16:36:07 +00001759void xenvif_stop_queue(struct xenvif *vif)
1760{
1761 if (!vif->can_queue)
1762 return;
1763
1764 netif_stop_queue(vif->dev);
1765}
1766
1767static void xenvif_start_queue(struct xenvif *vif)
1768{
1769 if (xenvif_schedulable(vif))
1770 netif_wake_queue(vif->dev);
1771}
1772
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001773int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001774{
1775 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001776 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001777
1778 while (!kthread_should_stop()) {
1779 wait_event_interruptible(vif->wq,
1780 rx_work_todo(vif) ||
1781 kthread_should_stop());
1782 if (kthread_should_stop())
1783 break;
1784
Paul Durrantca2f09f2013-12-06 16:36:07 +00001785 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001786 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001787
Paul Durrantca2f09f2013-12-06 16:36:07 +00001788 if (skb_queue_empty(&vif->rx_queue) &&
1789 netif_queue_stopped(vif->dev))
1790 xenvif_start_queue(vif);
1791
Wei Liub3f980b2013-08-26 12:59:38 +01001792 cond_resched();
1793 }
1794
Paul Durrantca2f09f2013-12-06 16:36:07 +00001795 /* Bin any remaining skbs */
1796 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1797 dev_kfree_skb(skb);
1798
Wei Liub3f980b2013-08-26 12:59:38 +01001799 return 0;
1800}
1801
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001802int xenvif_dealloc_kthread(void *data)
1803{
1804 struct xenvif *vif = data;
1805
1806 while (!kthread_should_stop()) {
1807 wait_event_interruptible(vif->dealloc_wq,
1808 tx_dealloc_work_todo(vif) ||
1809 kthread_should_stop());
1810 if (kthread_should_stop())
1811 break;
1812
1813 xenvif_tx_dealloc_action(vif);
1814 cond_resched();
1815 }
1816
1817 /* Unmap anything remaining*/
1818 if (tx_dealloc_work_todo(vif))
1819 xenvif_tx_dealloc_action(vif);
1820
1821 return 0;
1822}
1823
Ian Campbellf942dc22011-03-15 00:06:18 +00001824static int __init netback_init(void)
1825{
Ian Campbellf942dc22011-03-15 00:06:18 +00001826 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001827
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001828 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001829 return -ENODEV;
1830
Wei Liu37641492013-05-02 00:43:59 +00001831 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001832 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1833 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001834 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001835 }
1836
Ian Campbellf942dc22011-03-15 00:06:18 +00001837 rc = xenvif_xenbus_init();
1838 if (rc)
1839 goto failed_init;
1840
1841 return 0;
1842
1843failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001844 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001845}
1846
1847module_init(netback_init);
1848
Wei Liub103f352013-05-16 23:26:11 +00001849static void __exit netback_fini(void)
1850{
Wei Liub103f352013-05-16 23:26:11 +00001851 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001852}
1853module_exit(netback_fini);
1854
Ian Campbellf942dc22011-03-15 00:06:18 +00001855MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001856MODULE_ALIAS("xen-backend:vif");