blob: 3cb586357df716999a91995aece35fbf0dfe19ab [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>
40
41#include <net/tcp.h>
42
Stefano Stabellinica981632012-08-08 17:21:23 +000043#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000044#include <xen/events.h>
45#include <xen/interface/memory.h>
46
47#include <asm/xen/hypercall.h>
48#include <asm/xen/page.h>
49
Wei Liue1f00a692013-05-22 06:34:45 +000050/* Provide an option to disable split event channels at load time as
51 * event channels are limited resource. Split event channels are
52 * enabled by default.
53 */
54bool separate_tx_rx_irq = 1;
55module_param(separate_tx_rx_irq, bool, 0644);
56
Wei Liu2810e5b2013-04-22 02:20:42 +000057/*
58 * This is the maximum slots a skb can have. If a guest sends a skb
59 * which exceeds this limit it is considered malicious.
60 */
Wei Liu37641492013-05-02 00:43:59 +000061#define FATAL_SKB_SLOTS_DEFAULT 20
62static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
63module_param(fatal_skb_slots, uint, 0444);
64
Wei Liu73764192013-08-26 12:59:39 +010065static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
66 u8 status);
67
Ian Campbellf942dc22011-03-15 00:06:18 +000068static void make_tx_response(struct xenvif *vif,
69 struct xen_netif_tx_request *txp,
70 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010071
72static inline int tx_work_todo(struct xenvif *vif);
73static inline int rx_work_todo(struct xenvif *vif);
74
Ian Campbellf942dc22011-03-15 00:06:18 +000075static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
76 u16 id,
77 s8 st,
78 u16 offset,
79 u16 size,
80 u16 flags);
81
Wei Liub3f980b2013-08-26 12:59:38 +010082static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +000083 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000084{
Wei Liub3f980b2013-08-26 12:59:38 +010085 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +000086}
87
Wei Liub3f980b2013-08-26 12:59:38 +010088static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +000089 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000090{
Wei Liub3f980b2013-08-26 12:59:38 +010091 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +000092}
93
Zoltan Kissf53c3fe2014-03-06 21:48:26 +000094/* Find the containing VIF's structure from a pointer in pending_tx_info array
95 */
Zoltan Kiss3e2234b2014-03-06 21:48:25 +000096static inline struct xenvif* ubuf_to_vif(struct ubuf_info *ubuf)
97{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +000098 u16 pending_idx = ubuf->desc;
99 struct pending_tx_info *temp =
100 container_of(ubuf, struct pending_tx_info, callback_struct);
101 return container_of(temp - pending_idx,
102 struct xenvif,
103 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000104}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000105
Paul Durrant2eba61d2013-10-16 17:50:29 +0100106/* This is a miniumum size for the linear area to avoid lots of
107 * calls to __pskb_pull_tail() as we set up checksum offsets. The
108 * value 128 was chosen as it covers all IPv4 and most likely
109 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000110 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100111#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000112
Ian Campbellea066ad2011-10-05 00:28:46 +0000113static u16 frag_get_pending_idx(skb_frag_t *frag)
114{
115 return (u16)frag->page_offset;
116}
117
118static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
119{
120 frag->page_offset = pending_idx;
121}
122
Ian Campbellf942dc22011-03-15 00:06:18 +0000123static inline pending_ring_idx_t pending_index(unsigned i)
124{
125 return i & (MAX_PENDING_REQS-1);
126}
127
Paul Durrantca2f09f2013-12-06 16:36:07 +0000128bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000129{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000130 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000131
Paul Durrantca2f09f2013-12-06 16:36:07 +0000132 do {
133 prod = vif->rx.sring->req_prod;
134 cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000135
Paul Durrantca2f09f2013-12-06 16:36:07 +0000136 if (prod - cons >= needed)
137 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000138
Paul Durrantca2f09f2013-12-06 16:36:07 +0000139 vif->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000140
Paul Durrantca2f09f2013-12-06 16:36:07 +0000141 /* Make sure event is visible before we check prod
142 * again.
143 */
144 mb();
145 } while (vif->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000146
Paul Durrantca2f09f2013-12-06 16:36:07 +0000147 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000148}
149
150/*
151 * Returns true if we should start a new receive buffer instead of
152 * adding 'size' bytes to a buffer which currently contains 'offset'
153 * bytes.
154 */
155static bool start_new_rx_buffer(int offset, unsigned long size, int head)
156{
157 /* simple case: we have completely filled the current buffer. */
158 if (offset == MAX_BUFFER_OFFSET)
159 return true;
160
161 /*
162 * complex case: start a fresh buffer if the current frag
163 * would overflow the current buffer but only if:
164 * (i) this frag would fit completely in the next buffer
165 * and (ii) there is already some data in the current buffer
166 * and (iii) this is not the head buffer.
167 *
168 * Where:
169 * - (i) stops us splitting a frag into two copies
170 * unless the frag is too large for a single buffer.
171 * - (ii) stops us from leaving a buffer pointlessly empty.
172 * - (iii) stops us leaving the first buffer
173 * empty. Strictly speaking this is already covered
174 * by (ii) but is explicitly checked because
175 * netfront relies on the first buffer being
176 * non-empty and can crash otherwise.
177 *
178 * This means we will effectively linearise small
179 * frags but do not needlessly split large buffers
180 * into multiple copies tend to give large frags their
181 * own buffers as before.
182 */
183 if ((offset + size > MAX_BUFFER_OFFSET) &&
184 (size <= MAX_BUFFER_OFFSET) && offset && !head)
185 return true;
186
187 return false;
188}
189
Ian Campbellf942dc22011-03-15 00:06:18 +0000190struct netrx_pending_operations {
191 unsigned copy_prod, copy_cons;
192 unsigned meta_prod, meta_cons;
193 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100194 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000195 int copy_off;
196 grant_ref_t copy_gref;
197};
198
Wei Liub3f980b2013-08-26 12:59:38 +0100199static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
200 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000201{
Wei Liub3f980b2013-08-26 12:59:38 +0100202 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000203 struct xen_netif_rx_request *req;
204
205 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
206
207 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100208 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000209 meta->gso_size = 0;
210 meta->size = 0;
211 meta->id = req->id;
212
213 npo->copy_off = 0;
214 npo->copy_gref = req->gref;
215
216 return meta;
217}
218
Wei Liu33bc8012013-10-08 10:54:21 +0100219/*
220 * Set up the grant operations for this fragment. If it's a flipping
221 * interface, we also set up the unmap request from here.
222 */
Wei Liu73764192013-08-26 12:59:39 +0100223static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
224 struct netrx_pending_operations *npo,
225 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000226 unsigned long offset, int *head,
227 struct xenvif *foreign_vif,
228 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000229{
230 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100231 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000232 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100233 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000234
235 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000236 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000237
238 meta = npo->meta + npo->meta_prod - 1;
239
Ian Campbell6a8ed462012-10-10 03:48:42 +0000240 /* Skip unused frames from start of page */
241 page += offset >> PAGE_SHIFT;
242 offset &= ~PAGE_MASK;
243
Ian Campbellf942dc22011-03-15 00:06:18 +0000244 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000245 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000246 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
247
Ian Campbell6a8ed462012-10-10 03:48:42 +0000248 bytes = PAGE_SIZE - offset;
249
250 if (bytes > size)
251 bytes = size;
252
Wei Liu33bc8012013-10-08 10:54:21 +0100253 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000254 /*
255 * Netfront requires there to be some data in the head
256 * buffer.
257 */
Wei Liu33bc8012013-10-08 10:54:21 +0100258 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000259
260 meta = get_next_rx_buffer(vif, npo);
261 }
262
Ian Campbellf942dc22011-03-15 00:06:18 +0000263 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
264 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
265
266 copy_gop = npo->copy + npo->copy_prod++;
267 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100268 copy_gop->len = bytes;
269
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000270 if (foreign_vif) {
271 copy_gop->source.domid = foreign_vif->domid;
272 copy_gop->source.u.ref = foreign_gref;
273 copy_gop->flags |= GNTCOPY_source_gref;
274 } else {
275 copy_gop->source.domid = DOMID_SELF;
276 copy_gop->source.u.gmfn =
277 virt_to_mfn(page_address(page));
278 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000279 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000280
Wei Liub3f980b2013-08-26 12:59:38 +0100281 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000282 copy_gop->dest.offset = npo->copy_off;
283 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000284
285 npo->copy_off += bytes;
286 meta->size += bytes;
287
288 offset += bytes;
289 size -= bytes;
290
Ian Campbell6a8ed462012-10-10 03:48:42 +0000291 /* Next frame */
292 if (offset == PAGE_SIZE && size) {
293 BUG_ON(!PageCompound(page));
294 page++;
295 offset = 0;
296 }
297
Ian Campbellf942dc22011-03-15 00:06:18 +0000298 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100299 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
300 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
301 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
302 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
303 else
304 gso_type = XEN_NETIF_GSO_TYPE_NONE;
305
306 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000307 vif->rx.req_cons++;
308
Wei Liu33bc8012013-10-08 10:54:21 +0100309 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000310
311 }
312}
313
314/*
315 * Prepare an SKB to be transmitted to the frontend.
316 *
317 * This function is responsible for allocating grant operations, meta
318 * structures, etc.
319 *
320 * It returns the number of meta structures consumed. The number of
321 * ring slots used is always equal to the number of meta slots used
322 * plus the number of GSO descriptors used. Currently, we use either
323 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
324 * frontend-side LRO).
325 */
Wei Liu73764192013-08-26 12:59:39 +0100326static int xenvif_gop_skb(struct sk_buff *skb,
327 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000328{
329 struct xenvif *vif = netdev_priv(skb->dev);
330 int nr_frags = skb_shinfo(skb)->nr_frags;
331 int i;
332 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100333 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000334 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100335 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000336 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100337 int gso_type;
338 int gso_size;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000339 struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
340 grant_ref_t foreign_grefs[MAX_SKB_FRAGS];
341 struct xenvif *foreign_vif = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000342
343 old_meta_prod = npo->meta_prod;
344
Paul Durrant82cada22013-10-16 17:50:32 +0100345 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
346 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
347 gso_size = skb_shinfo(skb)->gso_size;
348 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
349 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
350 gso_size = skb_shinfo(skb)->gso_size;
351 } else {
352 gso_type = XEN_NETIF_GSO_TYPE_NONE;
353 gso_size = 0;
354 }
355
Ian Campbellf942dc22011-03-15 00:06:18 +0000356 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000357 if ((1 << gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000358 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
359 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100360 meta->gso_type = gso_type;
361 meta->gso_size = gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000362 meta->size = 0;
363 meta->id = req->id;
364 }
365
366 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
367 meta = npo->meta + npo->meta_prod++;
368
Paul Durrant82cada22013-10-16 17:50:32 +0100369 if ((1 << gso_type) & vif->gso_mask) {
370 meta->gso_type = gso_type;
371 meta->gso_size = gso_size;
372 } else {
373 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000374 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100375 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000376
377 meta->size = 0;
378 meta->id = req->id;
379 npo->copy_off = 0;
380 npo->copy_gref = req->gref;
381
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000382 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
383 (ubuf->callback == &xenvif_zerocopy_callback)) {
384 int i = 0;
385 foreign_vif = ubuf_to_vif(ubuf);
386
387 do {
388 u16 pending_idx = ubuf->desc;
389 foreign_grefs[i++] =
390 foreign_vif->pending_tx_info[pending_idx].req.gref;
391 ubuf = (struct ubuf_info *) ubuf->ctx;
392 } while (ubuf);
393 }
394
Ian Campbellf942dc22011-03-15 00:06:18 +0000395 data = skb->data;
396 while (data < skb_tail_pointer(skb)) {
397 unsigned int offset = offset_in_page(data);
398 unsigned int len = PAGE_SIZE - offset;
399
400 if (data + len > skb_tail_pointer(skb))
401 len = skb_tail_pointer(skb) - data;
402
Wei Liu73764192013-08-26 12:59:39 +0100403 xenvif_gop_frag_copy(vif, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000404 virt_to_page(data), len, offset, &head,
405 NULL,
406 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000407 data += len;
408 }
409
410 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100411 xenvif_gop_frag_copy(vif, skb, npo,
412 skb_frag_page(&skb_shinfo(skb)->frags[i]),
413 skb_frag_size(&skb_shinfo(skb)->frags[i]),
414 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000415 &head,
416 foreign_vif,
417 foreign_grefs[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000418 }
419
420 return npo->meta_prod - old_meta_prod;
421}
422
423/*
Wei Liu73764192013-08-26 12:59:39 +0100424 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000425 * used to set up the operations on the top of
426 * netrx_pending_operations, which have since been done. Check that
427 * they didn't give any errors and advance over them.
428 */
Wei Liu73764192013-08-26 12:59:39 +0100429static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
430 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000431{
432 struct gnttab_copy *copy_op;
433 int status = XEN_NETIF_RSP_OKAY;
434 int i;
435
436 for (i = 0; i < nr_meta_slots; i++) {
437 copy_op = npo->copy + npo->copy_cons++;
438 if (copy_op->status != GNTST_okay) {
439 netdev_dbg(vif->dev,
440 "Bad status %d from copy to DOM%d.\n",
441 copy_op->status, vif->domid);
442 status = XEN_NETIF_RSP_ERROR;
443 }
444 }
445
446 return status;
447}
448
Wei Liu73764192013-08-26 12:59:39 +0100449static void xenvif_add_frag_responses(struct xenvif *vif, int status,
450 struct xenvif_rx_meta *meta,
451 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000452{
453 int i;
454 unsigned long offset;
455
456 /* No fragments used */
457 if (nr_meta_slots <= 1)
458 return;
459
460 nr_meta_slots--;
461
462 for (i = 0; i < nr_meta_slots; i++) {
463 int flags;
464 if (i == nr_meta_slots - 1)
465 flags = 0;
466 else
467 flags = XEN_NETRXF_more_data;
468
469 offset = 0;
470 make_rx_response(vif, meta[i].id, status, offset,
471 meta[i].size, flags);
472 }
473}
474
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000475struct xenvif_rx_cb {
Wei Liu33bc8012013-10-08 10:54:21 +0100476 int meta_slots_used;
477};
478
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000479#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
480
Paul Durrantca2f09f2013-12-06 16:36:07 +0000481void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000482{
Wei Liub3f980b2013-08-26 12:59:38 +0100483 wake_up(&vif->wq);
484}
485
Paul Durrantca2f09f2013-12-06 16:36:07 +0000486static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100487{
Ian Campbellf942dc22011-03-15 00:06:18 +0000488 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000489 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000490 struct xen_netif_rx_response *resp;
491 struct sk_buff_head rxq;
492 struct sk_buff *skb;
493 LIST_HEAD(notify);
494 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000495 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000496 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000497
498 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100499 .copy = vif->grant_copy_op,
500 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000501 };
502
503 skb_queue_head_init(&rxq);
504
Wei Liub3f980b2013-08-26 12:59:38 +0100505 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000506 RING_IDX max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000507 int i;
508
509 /* We need a cheap worse case estimate for the number of
510 * slots we'll use.
511 */
512
513 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
514 skb_headlen(skb),
515 PAGE_SIZE);
516 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
517 unsigned int size;
518 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
519 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
520 }
521 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
522 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
523 max_slots_needed++;
524
525 /* If the skb may not fit then bail out now */
526 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
527 skb_queue_head(&vif->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000528 need_to_notify = true;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000529 vif->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000530 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000531 } else
532 vif->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000533
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000534 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
535 BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000536
537 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000538 }
539
Wei Liub3f980b2013-08-26 12:59:38 +0100540 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000541
542 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000543 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000544
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000545 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liub3f980b2013-08-26 12:59:38 +0100546 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000547
548 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000549
Paul Durrant82cada22013-10-16 17:50:32 +0100550 if ((1 << vif->meta[npo.meta_cons].gso_type) &
551 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000552 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100553 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000554
555 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
556
Wei Liub3f980b2013-08-26 12:59:38 +0100557 resp->offset = vif->meta[npo.meta_cons].gso_size;
558 resp->id = vif->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000559 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000560
561 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000562 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000563 }
564
565
566 vif->dev->stats.tx_bytes += skb->len;
567 vif->dev->stats.tx_packets++;
568
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000569 status = xenvif_check_gop(vif,
570 XENVIF_RX_CB(skb)->meta_slots_used,
571 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000572
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000573 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000574 flags = 0;
575 else
576 flags = XEN_NETRXF_more_data;
577
578 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
579 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
580 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
581 /* remote but checksummed. */
582 flags |= XEN_NETRXF_data_validated;
583
584 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100585 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000586 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100587 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000588 flags);
589
Paul Durrant82cada22013-10-16 17:50:32 +0100590 if ((1 << vif->meta[npo.meta_cons].gso_type) &
591 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000592 struct xen_netif_extra_info *gso =
593 (struct xen_netif_extra_info *)
594 RING_GET_RESPONSE(&vif->rx,
595 vif->rx.rsp_prod_pvt++);
596
597 resp->flags |= XEN_NETRXF_extra_info;
598
Paul Durrant82cada22013-10-16 17:50:32 +0100599 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100600 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000601 gso->u.gso.pad = 0;
602 gso->u.gso.features = 0;
603
604 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
605 gso->flags = 0;
606 }
607
Wei Liu73764192013-08-26 12:59:39 +0100608 xenvif_add_frag_responses(vif, status,
609 vif->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000610 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000611
612 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000613
Paul Durrant11b57f92014-01-08 12:41:58 +0000614 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100615
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000616 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000617 dev_kfree_skb(skb);
618 }
619
Paul Durrantca2f09f2013-12-06 16:36:07 +0000620done:
Wei Liub3f980b2013-08-26 12:59:38 +0100621 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000622 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000623}
624
Wei Liu73764192013-08-26 12:59:39 +0100625void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000626{
627 int more_to_do;
628
629 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
630
631 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100632 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000633}
634
635static void tx_add_credit(struct xenvif *vif)
636{
637 unsigned long max_burst, max_credit;
638
639 /*
640 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
641 * Otherwise the interface can seize up due to insufficient credit.
642 */
643 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
644 max_burst = min(max_burst, 131072UL);
645 max_burst = max(max_burst, vif->credit_bytes);
646
647 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
648 max_credit = vif->remaining_credit + vif->credit_bytes;
649 if (max_credit < vif->remaining_credit)
650 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
651
652 vif->remaining_credit = min(max_credit, max_burst);
653}
654
655static void tx_credit_callback(unsigned long data)
656{
657 struct xenvif *vif = (struct xenvif *)data;
658 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100659 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000660}
661
Wei Liu73764192013-08-26 12:59:39 +0100662static void xenvif_tx_err(struct xenvif *vif,
663 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000664{
665 RING_IDX cons = vif->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000666 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000667
668 do {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000669 spin_lock_irqsave(&vif->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +0000670 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000671 spin_unlock_irqrestore(&vif->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000672 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000673 break;
674 txp = RING_GET_REQUEST(&vif->tx, cons++);
675 } while (1);
676 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000677}
678
Wei Liu73764192013-08-26 12:59:39 +0100679static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000680{
681 netdev_err(vif->dev, "fatal error; disabling device\n");
682 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000683}
684
Wei Liu73764192013-08-26 12:59:39 +0100685static int xenvif_count_requests(struct xenvif *vif,
686 struct xen_netif_tx_request *first,
687 struct xen_netif_tx_request *txp,
688 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000689{
690 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000691 int slots = 0;
692 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000693 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000694
695 if (!(first->flags & XEN_NETTXF_more_data))
696 return 0;
697
698 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000699 struct xen_netif_tx_request dropped_tx = { 0 };
700
Wei Liu2810e5b2013-04-22 02:20:42 +0000701 if (slots >= work_to_do) {
702 netdev_err(vif->dev,
703 "Asked for %d slots but exceeds this limit\n",
704 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100705 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000706 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000707 }
708
Wei Liu2810e5b2013-04-22 02:20:42 +0000709 /* This guest is really using too many slots and
710 * considered malicious.
711 */
Wei Liu37641492013-05-02 00:43:59 +0000712 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000713 netdev_err(vif->dev,
714 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000715 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100716 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000717 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000718 }
719
Wei Liu2810e5b2013-04-22 02:20:42 +0000720 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000721 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
722 * the historical MAX_SKB_FRAGS value 18 to honor the
723 * same behavior as before. Any packet using more than
724 * 18 slots but less than fatal_skb_slots slots is
725 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000726 */
Wei Liu37641492013-05-02 00:43:59 +0000727 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000728 if (net_ratelimit())
729 netdev_dbg(vif->dev,
730 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000731 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000732 drop_err = -E2BIG;
733 }
734
Wei Liu59ccb4e2013-05-02 00:43:58 +0000735 if (drop_err)
736 txp = &dropped_tx;
737
Wei Liu2810e5b2013-04-22 02:20:42 +0000738 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000739 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000740
741 /* If the guest submitted a frame >= 64 KiB then
742 * first->size overflowed and following slots will
743 * appear to be larger than the frame.
744 *
745 * This cannot be fatal error as there are buggy
746 * frontends that do this.
747 *
748 * Consume all slots and drop the packet.
749 */
750 if (!drop_err && txp->size > first->size) {
751 if (net_ratelimit())
752 netdev_dbg(vif->dev,
753 "Invalid tx request, slot size %u > remaining size %u\n",
754 txp->size, first->size);
755 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000756 }
757
758 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000759 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000760
761 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000762 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000763 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100764 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000765 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000766 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000767
768 more_data = txp->flags & XEN_NETTXF_more_data;
769
770 if (!drop_err)
771 txp++;
772
773 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000774
775 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100776 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000777 return drop_err;
778 }
779
780 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000781}
782
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000783
784struct xenvif_tx_cb {
785 u16 pending_idx;
786};
787
788#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
789
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000790static inline void xenvif_tx_create_gop(struct xenvif *vif,
791 u16 pending_idx,
792 struct xen_netif_tx_request *txp,
793 struct gnttab_map_grant_ref *gop)
794{
795 vif->pages_to_map[gop-vif->tx_map_ops] = vif->mmap_pages[pending_idx];
796 gnttab_set_map_op(gop, idx_to_kaddr(vif, pending_idx),
797 GNTMAP_host_map | GNTMAP_readonly,
798 txp->gref, vif->domid);
799
800 memcpy(&vif->pending_tx_info[pending_idx].req, txp,
801 sizeof(*txp));
802}
803
804static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif *vif,
805 struct sk_buff *skb,
806 struct xen_netif_tx_request *txp,
807 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000808{
809 struct skb_shared_info *shinfo = skb_shinfo(skb);
810 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000811 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000812 int start;
813 pending_ring_idx_t index;
Wei Liu2810e5b2013-04-22 02:20:42 +0000814 unsigned int nr_slots;
Wei Liu2810e5b2013-04-22 02:20:42 +0000815
816 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000817 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000818 */
819 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000820
821 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000822 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000823
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000824 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
825 shinfo->nr_frags++, txp++, gop++) {
Zoltan Kiss62bad312014-03-06 21:48:27 +0000826 index = pending_index(vif->pending_cons++);
827 pending_idx = vif->pending_ring[index];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000828 xenvif_tx_create_gop(vif, pending_idx, txp, gop);
829 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000830 }
831
Wei Liu2810e5b2013-04-22 02:20:42 +0000832 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
833
Ian Campbellf942dc22011-03-15 00:06:18 +0000834 return gop;
835}
836
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000837static inline void xenvif_grant_handle_set(struct xenvif *vif,
838 u16 pending_idx,
839 grant_handle_t handle)
840{
841 if (unlikely(vif->grant_tx_handle[pending_idx] !=
842 NETBACK_INVALID_HANDLE)) {
843 netdev_err(vif->dev,
844 "Trying to overwrite active handle! pending_idx: %x\n",
845 pending_idx);
846 BUG();
847 }
848 vif->grant_tx_handle[pending_idx] = handle;
849}
850
851static inline void xenvif_grant_handle_reset(struct xenvif *vif,
852 u16 pending_idx)
853{
854 if (unlikely(vif->grant_tx_handle[pending_idx] ==
855 NETBACK_INVALID_HANDLE)) {
856 netdev_err(vif->dev,
857 "Trying to unmap invalid handle! pending_idx: %x\n",
858 pending_idx);
859 BUG();
860 }
861 vif->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
862}
863
Wei Liu73764192013-08-26 12:59:39 +0100864static int xenvif_tx_check_gop(struct xenvif *vif,
865 struct sk_buff *skb,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000866 struct gnttab_map_grant_ref **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000867{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000868 struct gnttab_map_grant_ref *gop = *gopp;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000869 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000870 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000871 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000872 int nr_frags = shinfo->nr_frags;
873 int i, err, start;
874
875 /* Check status of header. */
876 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000877 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100878 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000879 else
880 xenvif_grant_handle_set(vif, pending_idx , gop->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000881
882 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000883 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000884
885 for (i = start; i < nr_frags; i++) {
886 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +0000887
Ian Campbellea066ad2011-10-05 00:28:46 +0000888 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100889 tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +0000890
891 /* Check error status: if okay then remember grant handle. */
Zoltan Kiss62bad312014-03-06 21:48:27 +0000892 newerr = (++gop)->status;
Wei Liu2810e5b2013-04-22 02:20:42 +0000893
Ian Campbellf942dc22011-03-15 00:06:18 +0000894 if (likely(!newerr)) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000895 xenvif_grant_handle_set(vif, pending_idx , gop->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000896 /* Had a previous error? Invalidate this fragment. */
897 if (unlikely(err))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000898 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000899 continue;
900 }
901
902 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100903 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000904
905 /* Not the first error? Preceding frags already invalidated. */
906 if (err)
907 continue;
908
909 /* First error: invalidate header and preceding fragments. */
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000910 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000911 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000912 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000913 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000914 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000915 }
916
917 /* Remember the error: invalidate all subsequent fragments. */
918 err = newerr;
919 }
920
921 *gopp = gop + 1;
922 return err;
923}
924
Wei Liu73764192013-08-26 12:59:39 +0100925static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000926{
927 struct skb_shared_info *shinfo = skb_shinfo(skb);
928 int nr_frags = shinfo->nr_frags;
929 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000930 u16 prev_pending_idx = INVALID_PENDING_IDX;
931
932 if (skb_shinfo(skb)->destructor_arg)
933 prev_pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000934
935 for (i = 0; i < nr_frags; i++) {
936 skb_frag_t *frag = shinfo->frags + i;
937 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000938 struct page *page;
939 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000940
Ian Campbellea066ad2011-10-05 00:28:46 +0000941 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +0000942
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000943 /* If this is not the first frag, chain it to the previous*/
944 if (unlikely(prev_pending_idx == INVALID_PENDING_IDX))
945 skb_shinfo(skb)->destructor_arg =
946 &vif->pending_tx_info[pending_idx].callback_struct;
947 else if (likely(pending_idx != prev_pending_idx))
948 vif->pending_tx_info[prev_pending_idx].callback_struct.ctx =
949 &(vif->pending_tx_info[pending_idx].callback_struct);
950
951 vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
952 prev_pending_idx = pending_idx;
953
Wei Liub3f980b2013-08-26 12:59:38 +0100954 txp = &vif->pending_tx_info[pending_idx].req;
955 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +0000956 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +0000957 skb->len += txp->size;
958 skb->data_len += txp->size;
959 skb->truesize += txp->size;
960
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000961 /* Take an extra reference to offset network stack's put_page */
Wei Liub3f980b2013-08-26 12:59:38 +0100962 get_page(vif->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000963 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000964 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
965 * overlaps with "index", and "mapping" is not set. I think mapping
966 * should be set. If delivered to local stack, it would drop this
967 * skb in sk_filter unless the socket has the right to use it.
968 */
969 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000970}
971
Wei Liu73764192013-08-26 12:59:39 +0100972static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +0000973 struct xen_netif_extra_info *extras,
974 int work_to_do)
975{
976 struct xen_netif_extra_info extra;
977 RING_IDX cons = vif->tx.req_cons;
978
979 do {
980 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +0000981 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +0100982 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000983 return -EBADR;
984 }
985
986 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
987 sizeof(extra));
988 if (unlikely(!extra.type ||
989 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
990 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +0000991 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +0000992 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +0100993 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000994 return -EINVAL;
995 }
996
997 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
998 vif->tx.req_cons = ++cons;
999 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1000
1001 return work_to_do;
1002}
1003
Wei Liu73764192013-08-26 12:59:39 +01001004static int xenvif_set_skb_gso(struct xenvif *vif,
1005 struct sk_buff *skb,
1006 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001007{
1008 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001009 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001010 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001011 return -EINVAL;
1012 }
1013
Paul Durranta9468582013-10-16 17:50:31 +01001014 switch (gso->u.gso.type) {
1015 case XEN_NETIF_GSO_TYPE_TCPV4:
1016 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1017 break;
1018 case XEN_NETIF_GSO_TYPE_TCPV6:
1019 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1020 break;
1021 default:
Ian Campbell488562862013-02-06 23:41:35 +00001022 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001023 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001024 return -EINVAL;
1025 }
1026
1027 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001028 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001029
1030 return 0;
1031}
1032
1033static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1034{
Paul Durrant27216372014-01-09 10:02:47 +00001035 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001036
Paul Durrant2eba61d2013-10-16 17:50:29 +01001037 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001038 * peers can fail to set NETRXF_csum_blank when sending a GSO
1039 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1040 * recalculate the partial checksum.
1041 */
1042 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1043 vif->rx_gso_checksum_fixup++;
1044 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001045 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001046 }
1047
1048 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1049 if (skb->ip_summed != CHECKSUM_PARTIAL)
1050 return 0;
1051
Paul Durrant27216372014-01-09 10:02:47 +00001052 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001053}
1054
1055static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1056{
Wei Liu059dfa62013-10-28 12:07:57 +00001057 u64 now = get_jiffies_64();
1058 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001059 msecs_to_jiffies(vif->credit_usec / 1000);
1060
1061 /* Timer could already be pending in rare cases. */
1062 if (timer_pending(&vif->credit_timeout))
1063 return true;
1064
1065 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001066 if (time_after_eq64(now, next_credit)) {
1067 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001068 tx_add_credit(vif);
1069 }
1070
1071 /* Still too big to send right now? Set a callback. */
1072 if (size > vif->remaining_credit) {
1073 vif->credit_timeout.data =
1074 (unsigned long)vif;
1075 vif->credit_timeout.function =
1076 tx_credit_callback;
1077 mod_timer(&vif->credit_timeout,
1078 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001079 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001080
1081 return true;
1082 }
1083
1084 return false;
1085}
1086
Paul Durrant10574052013-12-11 10:57:15 +00001087static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001088{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001089 struct gnttab_map_grant_ref *gop = vif->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001090 struct sk_buff *skb;
1091 int ret;
1092
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001093 while (xenvif_tx_pending_slots_available(vif) &&
Paul Durrant10574052013-12-11 10:57:15 +00001094 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001095 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001096 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001097 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1098 u16 pending_idx;
1099 RING_IDX idx;
1100 int work_to_do;
1101 unsigned int data_len;
1102 pending_ring_idx_t index;
1103
Ian Campbell488562862013-02-06 23:41:35 +00001104 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1105 XEN_NETIF_TX_RING_SIZE) {
1106 netdev_err(vif->dev,
1107 "Impossible number of requests. "
1108 "req_prod %d, req_cons %d, size %ld\n",
1109 vif->tx.sring->req_prod, vif->tx.req_cons,
1110 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001111 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001112 continue;
1113 }
1114
Paul Durrantd9601a32013-12-11 10:57:16 +00001115 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001116 if (!work_to_do)
1117 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001118
1119 idx = vif->tx.req_cons;
1120 rmb(); /* Ensure that we see the request before we copy it. */
1121 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1122
1123 /* Credit-based scheduling. */
1124 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001125 tx_credit_exceeded(vif, txreq.size))
1126 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001127
1128 vif->remaining_credit -= txreq.size;
1129
1130 work_to_do--;
1131 vif->tx.req_cons = ++idx;
1132
1133 memset(extras, 0, sizeof(extras));
1134 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001135 work_to_do = xenvif_get_extras(vif, extras,
1136 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001137 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001138 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001139 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001140 }
1141
Wei Liu73764192013-08-26 12:59:39 +01001142 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001143 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001144 break;
Ian Campbell488562862013-02-06 23:41:35 +00001145
Ian Campbellf942dc22011-03-15 00:06:18 +00001146 idx += ret;
1147
1148 if (unlikely(txreq.size < ETH_HLEN)) {
1149 netdev_dbg(vif->dev,
1150 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001151 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001152 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001153 }
1154
1155 /* No crossing a page as the payload mustn't fragment. */
1156 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001157 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001158 "txreq.offset: %x, size: %u, end: %lu\n",
1159 txreq.offset, txreq.size,
1160 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001161 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001162 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001163 }
1164
Wei Liub3f980b2013-08-26 12:59:38 +01001165 index = pending_index(vif->pending_cons);
1166 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001167
1168 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001169 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001170 PKT_PROT_LEN : txreq.size;
1171
1172 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1173 GFP_ATOMIC | __GFP_NOWARN);
1174 if (unlikely(skb == NULL)) {
1175 netdev_dbg(vif->dev,
1176 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001177 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001178 break;
1179 }
1180
1181 /* Packets passed to netif_rx() must have some headroom. */
1182 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1183
1184 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1185 struct xen_netif_extra_info *gso;
1186 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1187
Wei Liu73764192013-08-26 12:59:39 +01001188 if (xenvif_set_skb_gso(vif, skb, gso)) {
1189 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001190 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001191 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001192 }
1193 }
1194
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001195 xenvif_tx_create_gop(vif, pending_idx, &txreq, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001196
1197 gop++;
1198
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001199 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001200
1201 __skb_put(skb, data_len);
1202
1203 skb_shinfo(skb)->nr_frags = ret;
1204 if (data_len < txreq.size) {
1205 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001206 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1207 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001208 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001209 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1210 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001211 }
1212
Wei Liub3f980b2013-08-26 12:59:38 +01001213 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001214
Wei Liu73764192013-08-26 12:59:39 +01001215 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001216 if (request_gop == NULL) {
1217 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001218 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001219 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001220 }
1221 gop = request_gop;
1222
Wei Liub3f980b2013-08-26 12:59:38 +01001223 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001224
Ian Campbellf942dc22011-03-15 00:06:18 +00001225 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001226
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001227 if ((gop-vif->tx_map_ops) >= ARRAY_SIZE(vif->tx_map_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001228 break;
1229 }
1230
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001231 return gop - vif->tx_map_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001232}
1233
Ian Campbellf942dc22011-03-15 00:06:18 +00001234
Paul Durrant10574052013-12-11 10:57:15 +00001235static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001236{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001237 struct gnttab_map_grant_ref *gop = vif->tx_map_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001238 struct sk_buff *skb;
1239 int work_done = 0;
1240
Paul Durrant10574052013-12-11 10:57:15 +00001241 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001242 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001243 u16 pending_idx;
1244 unsigned data_len;
1245
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001246 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liub3f980b2013-08-26 12:59:38 +01001247 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001248
1249 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001250 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001251 netdev_dbg(vif->dev, "netback grant failed.\n");
1252 skb_shinfo(skb)->nr_frags = 0;
1253 kfree_skb(skb);
1254 continue;
1255 }
1256
1257 data_len = skb->len;
1258 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001259 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001260 data_len);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001261 vif->pending_tx_info[pending_idx].callback_struct.ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001262 if (data_len < txp->size) {
1263 /* Append the packet payload as a fragment. */
1264 txp->offset += data_len;
1265 txp->size -= data_len;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001266 skb_shinfo(skb)->destructor_arg =
1267 &vif->pending_tx_info[pending_idx].callback_struct;
Ian Campbellf942dc22011-03-15 00:06:18 +00001268 } else {
1269 /* Schedule a response immediately. */
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001270 skb_shinfo(skb)->destructor_arg = NULL;
1271 xenvif_idx_unmap(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001272 }
1273
1274 if (txp->flags & XEN_NETTXF_csum_blank)
1275 skb->ip_summed = CHECKSUM_PARTIAL;
1276 else if (txp->flags & XEN_NETTXF_data_validated)
1277 skb->ip_summed = CHECKSUM_UNNECESSARY;
1278
Wei Liu73764192013-08-26 12:59:39 +01001279 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001280
Paul Durrant2eba61d2013-10-16 17:50:29 +01001281 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001282 int target = min_t(int, skb->len, PKT_PROT_LEN);
1283 __pskb_pull_tail(skb, target - skb_headlen(skb));
1284 }
1285
1286 skb->dev = vif->dev;
1287 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001288 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001289
1290 if (checksum_setup(vif, skb)) {
1291 netdev_dbg(vif->dev,
1292 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001293 /* We have to set this flag to trigger the callback */
1294 if (skb_shinfo(skb)->destructor_arg)
1295 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Ian Campbellf942dc22011-03-15 00:06:18 +00001296 kfree_skb(skb);
1297 continue;
1298 }
1299
Jason Wang40893fd2013-03-26 23:11:22 +00001300 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001301
Paul Durrantb89587a2013-12-17 11:44:35 +00001302 /* If the packet is GSO then we will have just set up the
1303 * transport header offset in checksum_setup so it's now
1304 * straightforward to calculate gso_segs.
1305 */
1306 if (skb_is_gso(skb)) {
1307 int mss = skb_shinfo(skb)->gso_size;
1308 int hdrlen = skb_transport_header(skb) -
1309 skb_mac_header(skb) +
1310 tcp_hdrlen(skb);
1311
1312 skb_shinfo(skb)->gso_segs =
1313 DIV_ROUND_UP(skb->len - hdrlen, mss);
1314 }
1315
Ian Campbellf942dc22011-03-15 00:06:18 +00001316 vif->dev->stats.rx_bytes += skb->len;
1317 vif->dev->stats.rx_packets++;
1318
Wei Liub3f980b2013-08-26 12:59:38 +01001319 work_done++;
1320
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001321 /* Set this flag right before netif_receive_skb, otherwise
1322 * someone might think this packet already left netback, and
1323 * do a skb_copy_ubufs while we are still in control of the
1324 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1325 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001326 if (skb_shinfo(skb)->destructor_arg) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001327 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001328 vif->tx_zerocopy_sent++;
1329 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001330
Wei Liub3f980b2013-08-26 12:59:38 +01001331 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001332 }
Wei Liub3f980b2013-08-26 12:59:38 +01001333
1334 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001335}
1336
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001337void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1338{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001339 unsigned long flags;
1340 pending_ring_idx_t index;
1341 struct xenvif *vif = ubuf_to_vif(ubuf);
1342
1343 /* This is the only place where we grab this lock, to protect callbacks
1344 * from each other.
1345 */
1346 spin_lock_irqsave(&vif->callback_lock, flags);
1347 do {
1348 u16 pending_idx = ubuf->desc;
1349 ubuf = (struct ubuf_info *) ubuf->ctx;
1350 BUG_ON(vif->dealloc_prod - vif->dealloc_cons >=
1351 MAX_PENDING_REQS);
1352 index = pending_index(vif->dealloc_prod);
1353 vif->dealloc_ring[index] = pending_idx;
1354 /* Sync with xenvif_tx_dealloc_action:
1355 * insert idx then incr producer.
1356 */
1357 smp_wmb();
1358 vif->dealloc_prod++;
1359 } while (ubuf);
1360 wake_up(&vif->dealloc_wq);
1361 spin_unlock_irqrestore(&vif->callback_lock, flags);
1362
1363 if (RING_HAS_UNCONSUMED_REQUESTS(&vif->tx) &&
1364 xenvif_tx_pending_slots_available(vif)) {
1365 local_bh_disable();
1366 napi_schedule(&vif->napi);
1367 local_bh_enable();
1368 }
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001369
1370 if (likely(zerocopy_success))
1371 vif->tx_zerocopy_success++;
1372 else
1373 vif->tx_zerocopy_fail++;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001374}
1375
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001376static inline void xenvif_tx_dealloc_action(struct xenvif *vif)
1377{
1378 struct gnttab_unmap_grant_ref *gop;
1379 pending_ring_idx_t dc, dp;
1380 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1381 unsigned int i = 0;
1382
1383 dc = vif->dealloc_cons;
1384 gop = vif->tx_unmap_ops;
1385
1386 /* Free up any grants we have finished using */
1387 do {
1388 dp = vif->dealloc_prod;
1389
1390 /* Ensure we see all indices enqueued by all
1391 * xenvif_zerocopy_callback().
1392 */
1393 smp_rmb();
1394
1395 while (dc != dp) {
1396 BUG_ON(gop - vif->tx_unmap_ops > MAX_PENDING_REQS);
1397 pending_idx =
1398 vif->dealloc_ring[pending_index(dc++)];
1399
1400 pending_idx_release[gop-vif->tx_unmap_ops] =
1401 pending_idx;
1402 vif->pages_to_unmap[gop-vif->tx_unmap_ops] =
1403 vif->mmap_pages[pending_idx];
1404 gnttab_set_unmap_op(gop,
1405 idx_to_kaddr(vif, pending_idx),
1406 GNTMAP_host_map,
1407 vif->grant_tx_handle[pending_idx]);
1408 /* Btw. already unmapped? */
1409 xenvif_grant_handle_reset(vif, pending_idx);
1410 ++gop;
1411 }
1412
1413 } while (dp != vif->dealloc_prod);
1414
1415 vif->dealloc_cons = dc;
1416
1417 if (gop - vif->tx_unmap_ops > 0) {
1418 int ret;
1419 ret = gnttab_unmap_refs(vif->tx_unmap_ops,
1420 NULL,
1421 vif->pages_to_unmap,
1422 gop - vif->tx_unmap_ops);
1423 if (ret) {
1424 netdev_err(vif->dev, "Unmap fail: nr_ops %x ret %d\n",
1425 gop - vif->tx_unmap_ops, ret);
1426 for (i = 0; i < gop - vif->tx_unmap_ops; ++i) {
1427 if (gop[i].status != GNTST_okay)
1428 netdev_err(vif->dev,
1429 " host_addr: %llx handle: %x status: %d\n",
1430 gop[i].host_addr,
1431 gop[i].handle,
1432 gop[i].status);
1433 }
1434 BUG();
1435 }
1436 }
1437
1438 for (i = 0; i < gop - vif->tx_unmap_ops; ++i)
1439 xenvif_idx_release(vif, pending_idx_release[i],
1440 XEN_NETIF_RSP_OKAY);
1441}
1442
1443
Ian Campbellf942dc22011-03-15 00:06:18 +00001444/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001445int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001446{
1447 unsigned nr_gops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001448 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001449
Wei Liub3f980b2013-08-26 12:59:38 +01001450 if (unlikely(!tx_work_todo(vif)))
1451 return 0;
1452
Paul Durrant10574052013-12-11 10:57:15 +00001453 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001454
1455 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001456 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001457
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001458 ret = gnttab_map_refs(vif->tx_map_ops,
1459 NULL,
1460 vif->pages_to_map,
1461 nr_gops);
1462 BUG_ON(ret);
Ian Campbellf942dc22011-03-15 00:06:18 +00001463
Paul Durrant10574052013-12-11 10:57:15 +00001464 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001465
1466 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001467}
1468
Wei Liu73764192013-08-26 12:59:39 +01001469static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1470 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001471{
Ian Campbellf942dc22011-03-15 00:06:18 +00001472 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001473 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001474 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001475
Zoltan Kiss62bad312014-03-06 21:48:27 +00001476 pending_tx_info = &vif->pending_tx_info[pending_idx];
1477 spin_lock_irqsave(&vif->response_lock, flags);
1478 make_tx_response(vif, &pending_tx_info->req, status);
1479 index = pending_index(vif->pending_prod);
1480 vif->pending_ring[index] = pending_idx;
1481 /* TX shouldn't use the index before we give it back here */
1482 mb();
1483 vif->pending_prod++;
1484 spin_unlock_irqrestore(&vif->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001485}
1486
Wei Liu2810e5b2013-04-22 02:20:42 +00001487
Ian Campbellf942dc22011-03-15 00:06:18 +00001488static void make_tx_response(struct xenvif *vif,
1489 struct xen_netif_tx_request *txp,
1490 s8 st)
1491{
1492 RING_IDX i = vif->tx.rsp_prod_pvt;
1493 struct xen_netif_tx_response *resp;
1494 int notify;
1495
1496 resp = RING_GET_RESPONSE(&vif->tx, i);
1497 resp->id = txp->id;
1498 resp->status = st;
1499
1500 if (txp->flags & XEN_NETTXF_extra_info)
1501 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1502
1503 vif->tx.rsp_prod_pvt = ++i;
1504 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1505 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001506 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001507}
1508
1509static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1510 u16 id,
1511 s8 st,
1512 u16 offset,
1513 u16 size,
1514 u16 flags)
1515{
1516 RING_IDX i = vif->rx.rsp_prod_pvt;
1517 struct xen_netif_rx_response *resp;
1518
1519 resp = RING_GET_RESPONSE(&vif->rx, i);
1520 resp->offset = offset;
1521 resp->flags = flags;
1522 resp->id = id;
1523 resp->status = (s16)size;
1524 if (st < 0)
1525 resp->status = (s16)st;
1526
1527 vif->rx.rsp_prod_pvt = ++i;
1528
1529 return resp;
1530}
1531
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001532void xenvif_idx_unmap(struct xenvif *vif, u16 pending_idx)
1533{
1534 int ret;
1535 struct gnttab_unmap_grant_ref tx_unmap_op;
1536
1537 gnttab_set_unmap_op(&tx_unmap_op,
1538 idx_to_kaddr(vif, pending_idx),
1539 GNTMAP_host_map,
1540 vif->grant_tx_handle[pending_idx]);
1541 /* Btw. already unmapped? */
1542 xenvif_grant_handle_reset(vif, pending_idx);
1543
1544 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1545 &vif->mmap_pages[pending_idx], 1);
1546 BUG_ON(ret);
1547
1548 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
1549}
1550
Wei Liub3f980b2013-08-26 12:59:38 +01001551static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001552{
Zoltan Kiss9ab98312014-02-04 19:54:37 +00001553 return !skb_queue_empty(&vif->rx_queue) &&
1554 xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
Ian Campbellf942dc22011-03-15 00:06:18 +00001555}
1556
Wei Liub3f980b2013-08-26 12:59:38 +01001557static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001558{
1559
Wei Liub3f980b2013-08-26 12:59:38 +01001560 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001561 xenvif_tx_pending_slots_available(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +00001562 return 1;
1563
1564 return 0;
1565}
1566
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001567static inline bool tx_dealloc_work_todo(struct xenvif *vif)
1568{
1569 return vif->dealloc_cons != vif->dealloc_prod;
1570}
1571
Wei Liu73764192013-08-26 12:59:39 +01001572void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001573{
David Vrabelc9d63692011-09-29 16:53:31 +01001574 if (vif->tx.sring)
1575 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1576 vif->tx.sring);
1577 if (vif->rx.sring)
1578 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1579 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001580}
1581
Wei Liu73764192013-08-26 12:59:39 +01001582int xenvif_map_frontend_rings(struct xenvif *vif,
1583 grant_ref_t tx_ring_ref,
1584 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001585{
David Vrabelc9d63692011-09-29 16:53:31 +01001586 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001587 struct xen_netif_tx_sring *txs;
1588 struct xen_netif_rx_sring *rxs;
1589
1590 int err = -ENOMEM;
1591
David Vrabelc9d63692011-09-29 16:53:31 +01001592 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1593 tx_ring_ref, &addr);
1594 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001595 goto err;
1596
David Vrabelc9d63692011-09-29 16:53:31 +01001597 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001598 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1599
David Vrabelc9d63692011-09-29 16:53:31 +01001600 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1601 rx_ring_ref, &addr);
1602 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001603 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001604
David Vrabelc9d63692011-09-29 16:53:31 +01001605 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001606 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1607
1608 return 0;
1609
1610err:
Wei Liu73764192013-08-26 12:59:39 +01001611 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001612 return err;
1613}
1614
Paul Durrantca2f09f2013-12-06 16:36:07 +00001615void xenvif_stop_queue(struct xenvif *vif)
1616{
1617 if (!vif->can_queue)
1618 return;
1619
1620 netif_stop_queue(vif->dev);
1621}
1622
1623static void xenvif_start_queue(struct xenvif *vif)
1624{
1625 if (xenvif_schedulable(vif))
1626 netif_wake_queue(vif->dev);
1627}
1628
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001629int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001630{
1631 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001632 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001633
1634 while (!kthread_should_stop()) {
1635 wait_event_interruptible(vif->wq,
1636 rx_work_todo(vif) ||
1637 kthread_should_stop());
1638 if (kthread_should_stop())
1639 break;
1640
Paul Durrantca2f09f2013-12-06 16:36:07 +00001641 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001642 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001643
Paul Durrantca2f09f2013-12-06 16:36:07 +00001644 if (skb_queue_empty(&vif->rx_queue) &&
1645 netif_queue_stopped(vif->dev))
1646 xenvif_start_queue(vif);
1647
Wei Liub3f980b2013-08-26 12:59:38 +01001648 cond_resched();
1649 }
1650
Paul Durrantca2f09f2013-12-06 16:36:07 +00001651 /* Bin any remaining skbs */
1652 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1653 dev_kfree_skb(skb);
1654
Wei Liub3f980b2013-08-26 12:59:38 +01001655 return 0;
1656}
1657
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001658int xenvif_dealloc_kthread(void *data)
1659{
1660 struct xenvif *vif = data;
1661
1662 while (!kthread_should_stop()) {
1663 wait_event_interruptible(vif->dealloc_wq,
1664 tx_dealloc_work_todo(vif) ||
1665 kthread_should_stop());
1666 if (kthread_should_stop())
1667 break;
1668
1669 xenvif_tx_dealloc_action(vif);
1670 cond_resched();
1671 }
1672
1673 /* Unmap anything remaining*/
1674 if (tx_dealloc_work_todo(vif))
1675 xenvif_tx_dealloc_action(vif);
1676
1677 return 0;
1678}
1679
Ian Campbellf942dc22011-03-15 00:06:18 +00001680static int __init netback_init(void)
1681{
Ian Campbellf942dc22011-03-15 00:06:18 +00001682 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001683
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001684 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001685 return -ENODEV;
1686
Wei Liu37641492013-05-02 00:43:59 +00001687 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001688 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1689 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001690 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001691 }
1692
Ian Campbellf942dc22011-03-15 00:06:18 +00001693 rc = xenvif_xenbus_init();
1694 if (rc)
1695 goto failed_init;
1696
1697 return 0;
1698
1699failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001700 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001701}
1702
1703module_init(netback_init);
1704
Wei Liub103f352013-05-16 23:26:11 +00001705static void __exit netback_fini(void)
1706{
Wei Liub103f352013-05-16 23:26:11 +00001707 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001708}
1709module_exit(netback_fini);
1710
Ian Campbellf942dc22011-03-15 00:06:18 +00001711MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001712MODULE_ALIAS("xen-backend:vif");