blob: 43ae4bad50c4903e9be6c4e08a65e95bad7c6c7e [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
65/*
66 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
67 * the maximum slots a valid packet can use. Now this value is defined
68 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
69 * all backend.
70 */
71#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
Wei Liu2810e5b2013-04-22 02:20:42 +000072
Wei Liu2810e5b2013-04-22 02:20:42 +000073/*
74 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
75 * one or more merged tx requests, otherwise it is the continuation of
76 * previous tx request.
77 */
Wei Liub3f980b2013-08-26 12:59:38 +010078static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
Wei Liu2810e5b2013-04-22 02:20:42 +000079{
Wei Liub3f980b2013-08-26 12:59:38 +010080 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
Wei Liu2810e5b2013-04-22 02:20:42 +000081}
82
Wei Liu73764192013-08-26 12:59:39 +010083static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
84 u8 status);
85
Ian Campbellf942dc22011-03-15 00:06:18 +000086static void make_tx_response(struct xenvif *vif,
87 struct xen_netif_tx_request *txp,
88 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010089
90static inline int tx_work_todo(struct xenvif *vif);
91static inline int rx_work_todo(struct xenvif *vif);
92
Ian Campbellf942dc22011-03-15 00:06:18 +000093static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
94 u16 id,
95 s8 st,
96 u16 offset,
97 u16 size,
98 u16 flags);
99
Wei Liub3f980b2013-08-26 12:59:38 +0100100static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000101 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000102{
Wei Liub3f980b2013-08-26 12:59:38 +0100103 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000104}
105
Wei Liub3f980b2013-08-26 12:59:38 +0100106static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000107 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000108{
Wei Liub3f980b2013-08-26 12:59:38 +0100109 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000110}
111
Paul Durrant2eba61d2013-10-16 17:50:29 +0100112/* This is a miniumum size for the linear area to avoid lots of
113 * calls to __pskb_pull_tail() as we set up checksum offsets. The
114 * value 128 was chosen as it covers all IPv4 and most likely
115 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000116 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100117#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000118
Ian Campbellea066ad2011-10-05 00:28:46 +0000119static u16 frag_get_pending_idx(skb_frag_t *frag)
120{
121 return (u16)frag->page_offset;
122}
123
124static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
125{
126 frag->page_offset = pending_idx;
127}
128
Ian Campbellf942dc22011-03-15 00:06:18 +0000129static inline pending_ring_idx_t pending_index(unsigned i)
130{
131 return i & (MAX_PENDING_REQS-1);
132}
133
Wei Liub3f980b2013-08-26 12:59:38 +0100134static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000135{
136 return MAX_PENDING_REQS -
Wei Liub3f980b2013-08-26 12:59:38 +0100137 vif->pending_prod + vif->pending_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000138}
139
Paul Durrantca2f09f2013-12-06 16:36:07 +0000140bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000141{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000142 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000143
Paul Durrantca2f09f2013-12-06 16:36:07 +0000144 do {
145 prod = vif->rx.sring->req_prod;
146 cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000147
Paul Durrantca2f09f2013-12-06 16:36:07 +0000148 if (prod - cons >= needed)
149 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000150
Paul Durrantca2f09f2013-12-06 16:36:07 +0000151 vif->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000152
Paul Durrantca2f09f2013-12-06 16:36:07 +0000153 /* Make sure event is visible before we check prod
154 * again.
155 */
156 mb();
157 } while (vif->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000158
Paul Durrantca2f09f2013-12-06 16:36:07 +0000159 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000160}
161
162/*
163 * Returns true if we should start a new receive buffer instead of
164 * adding 'size' bytes to a buffer which currently contains 'offset'
165 * bytes.
166 */
167static bool start_new_rx_buffer(int offset, unsigned long size, int head)
168{
169 /* simple case: we have completely filled the current buffer. */
170 if (offset == MAX_BUFFER_OFFSET)
171 return true;
172
173 /*
174 * complex case: start a fresh buffer if the current frag
175 * would overflow the current buffer but only if:
176 * (i) this frag would fit completely in the next buffer
177 * and (ii) there is already some data in the current buffer
178 * and (iii) this is not the head buffer.
179 *
180 * Where:
181 * - (i) stops us splitting a frag into two copies
182 * unless the frag is too large for a single buffer.
183 * - (ii) stops us from leaving a buffer pointlessly empty.
184 * - (iii) stops us leaving the first buffer
185 * empty. Strictly speaking this is already covered
186 * by (ii) but is explicitly checked because
187 * netfront relies on the first buffer being
188 * non-empty and can crash otherwise.
189 *
190 * This means we will effectively linearise small
191 * frags but do not needlessly split large buffers
192 * into multiple copies tend to give large frags their
193 * own buffers as before.
194 */
195 if ((offset + size > MAX_BUFFER_OFFSET) &&
196 (size <= MAX_BUFFER_OFFSET) && offset && !head)
197 return true;
198
199 return false;
200}
201
Ian Campbellf942dc22011-03-15 00:06:18 +0000202struct netrx_pending_operations {
203 unsigned copy_prod, copy_cons;
204 unsigned meta_prod, meta_cons;
205 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100206 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000207 int copy_off;
208 grant_ref_t copy_gref;
209};
210
Wei Liub3f980b2013-08-26 12:59:38 +0100211static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
212 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000213{
Wei Liub3f980b2013-08-26 12:59:38 +0100214 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000215 struct xen_netif_rx_request *req;
216
217 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
218
219 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100220 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000221 meta->gso_size = 0;
222 meta->size = 0;
223 meta->id = req->id;
224
225 npo->copy_off = 0;
226 npo->copy_gref = req->gref;
227
228 return meta;
229}
230
Wei Liu33bc8012013-10-08 10:54:21 +0100231/*
232 * Set up the grant operations for this fragment. If it's a flipping
233 * interface, we also set up the unmap request from here.
234 */
Wei Liu73764192013-08-26 12:59:39 +0100235static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
236 struct netrx_pending_operations *npo,
237 struct page *page, unsigned long size,
Wei Liu33bc8012013-10-08 10:54:21 +0100238 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000239{
240 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100241 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000242 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100243 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000244
245 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000246 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000247
248 meta = npo->meta + npo->meta_prod - 1;
249
Ian Campbell6a8ed462012-10-10 03:48:42 +0000250 /* Skip unused frames from start of page */
251 page += offset >> PAGE_SHIFT;
252 offset &= ~PAGE_MASK;
253
Ian Campbellf942dc22011-03-15 00:06:18 +0000254 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000255 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000256 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
257
Ian Campbell6a8ed462012-10-10 03:48:42 +0000258 bytes = PAGE_SIZE - offset;
259
260 if (bytes > size)
261 bytes = size;
262
Wei Liu33bc8012013-10-08 10:54:21 +0100263 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000264 /*
265 * Netfront requires there to be some data in the head
266 * buffer.
267 */
Wei Liu33bc8012013-10-08 10:54:21 +0100268 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000269
270 meta = get_next_rx_buffer(vif, npo);
271 }
272
Ian Campbellf942dc22011-03-15 00:06:18 +0000273 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
274 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
275
276 copy_gop = npo->copy + npo->copy_prod++;
277 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100278 copy_gop->len = bytes;
279
Wei Liu43e9d192013-08-26 12:59:37 +0100280 copy_gop->source.domid = DOMID_SELF;
281 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000282 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000283
Wei Liub3f980b2013-08-26 12:59:38 +0100284 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000285 copy_gop->dest.offset = npo->copy_off;
286 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000287
288 npo->copy_off += bytes;
289 meta->size += bytes;
290
291 offset += bytes;
292 size -= bytes;
293
Ian Campbell6a8ed462012-10-10 03:48:42 +0000294 /* Next frame */
295 if (offset == PAGE_SIZE && size) {
296 BUG_ON(!PageCompound(page));
297 page++;
298 offset = 0;
299 }
300
Ian Campbellf942dc22011-03-15 00:06:18 +0000301 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100302 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
303 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
304 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
305 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
306 else
307 gso_type = XEN_NETIF_GSO_TYPE_NONE;
308
309 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000310 vif->rx.req_cons++;
311
Wei Liu33bc8012013-10-08 10:54:21 +0100312 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000313
314 }
315}
316
317/*
318 * Prepare an SKB to be transmitted to the frontend.
319 *
320 * This function is responsible for allocating grant operations, meta
321 * structures, etc.
322 *
323 * It returns the number of meta structures consumed. The number of
324 * ring slots used is always equal to the number of meta slots used
325 * plus the number of GSO descriptors used. Currently, we use either
326 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
327 * frontend-side LRO).
328 */
Wei Liu73764192013-08-26 12:59:39 +0100329static int xenvif_gop_skb(struct sk_buff *skb,
330 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000331{
332 struct xenvif *vif = netdev_priv(skb->dev);
333 int nr_frags = skb_shinfo(skb)->nr_frags;
334 int i;
335 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100336 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000337 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100338 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000339 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100340 int gso_type;
341 int gso_size;
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
382 data = skb->data;
383 while (data < skb_tail_pointer(skb)) {
384 unsigned int offset = offset_in_page(data);
385 unsigned int len = PAGE_SIZE - offset;
386
387 if (data + len > skb_tail_pointer(skb))
388 len = skb_tail_pointer(skb) - data;
389
Wei Liu73764192013-08-26 12:59:39 +0100390 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100391 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000392 data += len;
393 }
394
395 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100396 xenvif_gop_frag_copy(vif, skb, npo,
397 skb_frag_page(&skb_shinfo(skb)->frags[i]),
398 skb_frag_size(&skb_shinfo(skb)->frags[i]),
399 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100400 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000401 }
402
403 return npo->meta_prod - old_meta_prod;
404}
405
406/*
Wei Liu73764192013-08-26 12:59:39 +0100407 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000408 * used to set up the operations on the top of
409 * netrx_pending_operations, which have since been done. Check that
410 * they didn't give any errors and advance over them.
411 */
Wei Liu73764192013-08-26 12:59:39 +0100412static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
413 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000414{
415 struct gnttab_copy *copy_op;
416 int status = XEN_NETIF_RSP_OKAY;
417 int i;
418
419 for (i = 0; i < nr_meta_slots; i++) {
420 copy_op = npo->copy + npo->copy_cons++;
421 if (copy_op->status != GNTST_okay) {
422 netdev_dbg(vif->dev,
423 "Bad status %d from copy to DOM%d.\n",
424 copy_op->status, vif->domid);
425 status = XEN_NETIF_RSP_ERROR;
426 }
427 }
428
429 return status;
430}
431
Wei Liu73764192013-08-26 12:59:39 +0100432static void xenvif_add_frag_responses(struct xenvif *vif, int status,
433 struct xenvif_rx_meta *meta,
434 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000435{
436 int i;
437 unsigned long offset;
438
439 /* No fragments used */
440 if (nr_meta_slots <= 1)
441 return;
442
443 nr_meta_slots--;
444
445 for (i = 0; i < nr_meta_slots; i++) {
446 int flags;
447 if (i == nr_meta_slots - 1)
448 flags = 0;
449 else
450 flags = XEN_NETRXF_more_data;
451
452 offset = 0;
453 make_rx_response(vif, meta[i].id, status, offset,
454 meta[i].size, flags);
455 }
456}
457
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000458struct xenvif_rx_cb {
Wei Liu33bc8012013-10-08 10:54:21 +0100459 int meta_slots_used;
460};
461
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000462#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
463
Paul Durrantca2f09f2013-12-06 16:36:07 +0000464void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000465{
Wei Liub3f980b2013-08-26 12:59:38 +0100466 wake_up(&vif->wq);
467}
468
Paul Durrantca2f09f2013-12-06 16:36:07 +0000469static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100470{
Ian Campbellf942dc22011-03-15 00:06:18 +0000471 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000472 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000473 struct xen_netif_rx_response *resp;
474 struct sk_buff_head rxq;
475 struct sk_buff *skb;
476 LIST_HEAD(notify);
477 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000478 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000479 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000480
481 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100482 .copy = vif->grant_copy_op,
483 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000484 };
485
486 skb_queue_head_init(&rxq);
487
Wei Liub3f980b2013-08-26 12:59:38 +0100488 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000489 RING_IDX max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000490 int i;
491
492 /* We need a cheap worse case estimate for the number of
493 * slots we'll use.
494 */
495
496 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
497 skb_headlen(skb),
498 PAGE_SIZE);
499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
500 unsigned int size;
501 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
502 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
503 }
504 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
505 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
506 max_slots_needed++;
507
508 /* If the skb may not fit then bail out now */
509 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
510 skb_queue_head(&vif->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000511 need_to_notify = true;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000512 vif->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000513 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000514 } else
515 vif->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000516
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000517 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
518 BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000519
520 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000521 }
522
Wei Liub3f980b2013-08-26 12:59:38 +0100523 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000524
525 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000526 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000527
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000528 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liub3f980b2013-08-26 12:59:38 +0100529 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000530
531 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000532
Paul Durrant82cada22013-10-16 17:50:32 +0100533 if ((1 << vif->meta[npo.meta_cons].gso_type) &
534 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000535 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100536 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000537
538 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
539
Wei Liub3f980b2013-08-26 12:59:38 +0100540 resp->offset = vif->meta[npo.meta_cons].gso_size;
541 resp->id = vif->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000542 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000543
544 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000545 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000546 }
547
548
549 vif->dev->stats.tx_bytes += skb->len;
550 vif->dev->stats.tx_packets++;
551
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000552 status = xenvif_check_gop(vif,
553 XENVIF_RX_CB(skb)->meta_slots_used,
554 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000555
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000556 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000557 flags = 0;
558 else
559 flags = XEN_NETRXF_more_data;
560
561 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
562 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
563 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
564 /* remote but checksummed. */
565 flags |= XEN_NETRXF_data_validated;
566
567 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100568 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000569 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100570 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 flags);
572
Paul Durrant82cada22013-10-16 17:50:32 +0100573 if ((1 << vif->meta[npo.meta_cons].gso_type) &
574 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000575 struct xen_netif_extra_info *gso =
576 (struct xen_netif_extra_info *)
577 RING_GET_RESPONSE(&vif->rx,
578 vif->rx.rsp_prod_pvt++);
579
580 resp->flags |= XEN_NETRXF_extra_info;
581
Paul Durrant82cada22013-10-16 17:50:32 +0100582 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100583 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000584 gso->u.gso.pad = 0;
585 gso->u.gso.features = 0;
586
587 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
588 gso->flags = 0;
589 }
590
Wei Liu73764192013-08-26 12:59:39 +0100591 xenvif_add_frag_responses(vif, status,
592 vif->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000593 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000594
595 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000596
Paul Durrant11b57f92014-01-08 12:41:58 +0000597 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100598
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000599 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000600 dev_kfree_skb(skb);
601 }
602
Paul Durrantca2f09f2013-12-06 16:36:07 +0000603done:
Wei Liub3f980b2013-08-26 12:59:38 +0100604 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000605 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000606}
607
Wei Liu73764192013-08-26 12:59:39 +0100608void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000609{
610 int more_to_do;
611
612 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
613
614 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100615 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000616}
617
618static void tx_add_credit(struct xenvif *vif)
619{
620 unsigned long max_burst, max_credit;
621
622 /*
623 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
624 * Otherwise the interface can seize up due to insufficient credit.
625 */
626 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
627 max_burst = min(max_burst, 131072UL);
628 max_burst = max(max_burst, vif->credit_bytes);
629
630 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
631 max_credit = vif->remaining_credit + vif->credit_bytes;
632 if (max_credit < vif->remaining_credit)
633 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
634
635 vif->remaining_credit = min(max_credit, max_burst);
636}
637
638static void tx_credit_callback(unsigned long data)
639{
640 struct xenvif *vif = (struct xenvif *)data;
641 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100642 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000643}
644
Wei Liu73764192013-08-26 12:59:39 +0100645static void xenvif_tx_err(struct xenvif *vif,
646 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000647{
648 RING_IDX cons = vif->tx.req_cons;
649
650 do {
651 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000652 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000653 break;
654 txp = RING_GET_REQUEST(&vif->tx, cons++);
655 } while (1);
656 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000657}
658
Wei Liu73764192013-08-26 12:59:39 +0100659static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000660{
661 netdev_err(vif->dev, "fatal error; disabling device\n");
662 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000663}
664
Wei Liu73764192013-08-26 12:59:39 +0100665static int xenvif_count_requests(struct xenvif *vif,
666 struct xen_netif_tx_request *first,
667 struct xen_netif_tx_request *txp,
668 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000669{
670 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000671 int slots = 0;
672 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000673 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000674
675 if (!(first->flags & XEN_NETTXF_more_data))
676 return 0;
677
678 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000679 struct xen_netif_tx_request dropped_tx = { 0 };
680
Wei Liu2810e5b2013-04-22 02:20:42 +0000681 if (slots >= work_to_do) {
682 netdev_err(vif->dev,
683 "Asked for %d slots but exceeds this limit\n",
684 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100685 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000686 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000687 }
688
Wei Liu2810e5b2013-04-22 02:20:42 +0000689 /* This guest is really using too many slots and
690 * considered malicious.
691 */
Wei Liu37641492013-05-02 00:43:59 +0000692 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000693 netdev_err(vif->dev,
694 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000695 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100696 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000697 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000698 }
699
Wei Liu2810e5b2013-04-22 02:20:42 +0000700 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000701 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
702 * the historical MAX_SKB_FRAGS value 18 to honor the
703 * same behavior as before. Any packet using more than
704 * 18 slots but less than fatal_skb_slots slots is
705 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000706 */
Wei Liu37641492013-05-02 00:43:59 +0000707 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000708 if (net_ratelimit())
709 netdev_dbg(vif->dev,
710 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000711 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000712 drop_err = -E2BIG;
713 }
714
Wei Liu59ccb4e2013-05-02 00:43:58 +0000715 if (drop_err)
716 txp = &dropped_tx;
717
Wei Liu2810e5b2013-04-22 02:20:42 +0000718 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000719 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000720
721 /* If the guest submitted a frame >= 64 KiB then
722 * first->size overflowed and following slots will
723 * appear to be larger than the frame.
724 *
725 * This cannot be fatal error as there are buggy
726 * frontends that do this.
727 *
728 * Consume all slots and drop the packet.
729 */
730 if (!drop_err && txp->size > first->size) {
731 if (net_ratelimit())
732 netdev_dbg(vif->dev,
733 "Invalid tx request, slot size %u > remaining size %u\n",
734 txp->size, first->size);
735 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000736 }
737
738 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000739 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000740
741 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000742 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000743 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100744 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000745 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000746 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000747
748 more_data = txp->flags & XEN_NETTXF_more_data;
749
750 if (!drop_err)
751 txp++;
752
753 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000754
755 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100756 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000757 return drop_err;
758 }
759
760 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000761}
762
Wei Liu73764192013-08-26 12:59:39 +0100763static struct page *xenvif_alloc_page(struct xenvif *vif,
764 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000765{
766 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100767
768 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000769 if (!page)
770 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100771 vif->mmap_pages[pending_idx] = page;
772
Ian Campbellf942dc22011-03-15 00:06:18 +0000773 return page;
774}
775
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000776
777struct xenvif_tx_cb {
778 u16 pending_idx;
779};
780
781#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
782
Wei Liu73764192013-08-26 12:59:39 +0100783static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
784 struct sk_buff *skb,
785 struct xen_netif_tx_request *txp,
786 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000787{
788 struct skb_shared_info *shinfo = skb_shinfo(skb);
789 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000790 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liu2810e5b2013-04-22 02:20:42 +0000791 u16 head_idx = 0;
792 int slot, start;
793 struct page *page;
794 pending_ring_idx_t index, start_idx = 0;
795 uint16_t dst_offset;
796 unsigned int nr_slots;
797 struct pending_tx_info *first = NULL;
798
799 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000800 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000801 */
802 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000803
804 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000805 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000806
Wei Liu2810e5b2013-04-22 02:20:42 +0000807 /* Coalesce tx requests, at this point the packet passed in
808 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100809 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000810 */
811 for (shinfo->nr_frags = slot = start; slot < nr_slots;
812 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000813 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100814 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000815
Wei Liub3f980b2013-08-26 12:59:38 +0100816 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000817 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000818 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000819
Wei Liu2810e5b2013-04-22 02:20:42 +0000820 dst_offset = 0;
821 first = NULL;
822 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
823 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000824
Wei Liu2810e5b2013-04-22 02:20:42 +0000825 gop->source.u.ref = txp->gref;
826 gop->source.domid = vif->domid;
827 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000828
Wei Liu2810e5b2013-04-22 02:20:42 +0000829 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000830
Wei Liu2810e5b2013-04-22 02:20:42 +0000831 gop->dest.offset = dst_offset;
832 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000833
Wei Liu2810e5b2013-04-22 02:20:42 +0000834 if (dst_offset + txp->size > PAGE_SIZE) {
835 /* This page can only merge a portion
836 * of tx request. Do not increment any
837 * pointer / counter here. The txp
838 * will be dealt with in future
839 * rounds, eventually hitting the
840 * `else` branch.
841 */
842 gop->len = PAGE_SIZE - dst_offset;
843 txp->offset += gop->len;
844 txp->size -= gop->len;
845 dst_offset += gop->len; /* quit loop */
846 } else {
847 /* This tx request can be merged in the page */
848 gop->len = txp->size;
849 dst_offset += gop->len;
850
Wei Liub3f980b2013-08-26 12:59:38 +0100851 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000852
Wei Liub3f980b2013-08-26 12:59:38 +0100853 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000854
855 memcpy(&pending_tx_info[pending_idx].req, txp,
856 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000857
858 /* Poison these fields, corresponding
859 * fields for head tx req will be set
860 * to correct values after the loop.
861 */
Wei Liub3f980b2013-08-26 12:59:38 +0100862 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000863 pending_tx_info[pending_idx].head =
864 INVALID_PENDING_RING_IDX;
865
866 if (!first) {
867 first = &pending_tx_info[pending_idx];
868 start_idx = index;
869 head_idx = pending_idx;
870 }
871
872 txp++;
873 slot++;
874 }
875
876 gop++;
877 }
878
879 first->req.offset = 0;
880 first->req.size = dst_offset;
881 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100882 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000883 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000884 }
885
Wei Liu2810e5b2013-04-22 02:20:42 +0000886 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
887
Ian Campbellf942dc22011-03-15 00:06:18 +0000888 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000889err:
890 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000891 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100892 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000893 frag_get_pending_idx(&frags[shinfo->nr_frags]),
894 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000895 }
896 /* The head too, if necessary. */
897 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100898 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000899
900 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000901}
902
Wei Liu73764192013-08-26 12:59:39 +0100903static int xenvif_tx_check_gop(struct xenvif *vif,
904 struct sk_buff *skb,
905 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000906{
907 struct gnttab_copy *gop = *gopp;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000908 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000909 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000910 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000911 int nr_frags = shinfo->nr_frags;
912 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000913 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000914
915 /* Check status of header. */
916 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000917 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100918 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000919
920 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000921 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000922
923 for (i = start; i < nr_frags; i++) {
924 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000925 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000926
Ian Campbellea066ad2011-10-05 00:28:46 +0000927 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100928 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000929 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000930
931 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000932 do {
933 newerr = (++gop)->status;
934 if (newerr)
935 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100936 peek = vif->pending_ring[pending_index(++head)];
937 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000938
Ian Campbellf942dc22011-03-15 00:06:18 +0000939 if (likely(!newerr)) {
940 /* Had a previous error? Invalidate this fragment. */
941 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100942 xenvif_idx_release(vif, pending_idx,
943 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000944 continue;
945 }
946
947 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100948 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000949
950 /* Not the first error? Preceding frags already invalidated. */
951 if (err)
952 continue;
953
954 /* First error: invalidate header and preceding fragments. */
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000955 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liu73764192013-08-26 12:59:39 +0100956 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000957 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000958 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +0100959 xenvif_idx_release(vif, pending_idx,
960 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000961 }
962
963 /* Remember the error: invalidate all subsequent fragments. */
964 err = newerr;
965 }
966
967 *gopp = gop + 1;
968 return err;
969}
970
Wei Liu73764192013-08-26 12:59:39 +0100971static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000972{
973 struct skb_shared_info *shinfo = skb_shinfo(skb);
974 int nr_frags = shinfo->nr_frags;
975 int i;
976
977 for (i = 0; i < nr_frags; i++) {
978 skb_frag_t *frag = shinfo->frags + i;
979 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000980 struct page *page;
981 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000982
Ian Campbellea066ad2011-10-05 00:28:46 +0000983 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +0000984
Wei Liub3f980b2013-08-26 12:59:38 +0100985 txp = &vif->pending_tx_info[pending_idx].req;
986 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +0000987 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +0000988 skb->len += txp->size;
989 skb->data_len += txp->size;
990 skb->truesize += txp->size;
991
Wei Liu73764192013-08-26 12:59:39 +0100992 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +0100993 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +0100994 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000995 }
996}
997
Wei Liu73764192013-08-26 12:59:39 +0100998static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +0000999 struct xen_netif_extra_info *extras,
1000 int work_to_do)
1001{
1002 struct xen_netif_extra_info extra;
1003 RING_IDX cons = vif->tx.req_cons;
1004
1005 do {
1006 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001007 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001008 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001009 return -EBADR;
1010 }
1011
1012 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1013 sizeof(extra));
1014 if (unlikely(!extra.type ||
1015 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1016 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001017 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001018 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001019 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001020 return -EINVAL;
1021 }
1022
1023 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1024 vif->tx.req_cons = ++cons;
1025 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1026
1027 return work_to_do;
1028}
1029
Wei Liu73764192013-08-26 12:59:39 +01001030static int xenvif_set_skb_gso(struct xenvif *vif,
1031 struct sk_buff *skb,
1032 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001033{
1034 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001035 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001036 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001037 return -EINVAL;
1038 }
1039
Paul Durranta9468582013-10-16 17:50:31 +01001040 switch (gso->u.gso.type) {
1041 case XEN_NETIF_GSO_TYPE_TCPV4:
1042 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1043 break;
1044 case XEN_NETIF_GSO_TYPE_TCPV6:
1045 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1046 break;
1047 default:
Ian Campbell488562862013-02-06 23:41:35 +00001048 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001049 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001050 return -EINVAL;
1051 }
1052
1053 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001054 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001055
1056 return 0;
1057}
1058
1059static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1060{
Paul Durrant27216372014-01-09 10:02:47 +00001061 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001062
Paul Durrant2eba61d2013-10-16 17:50:29 +01001063 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001064 * peers can fail to set NETRXF_csum_blank when sending a GSO
1065 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1066 * recalculate the partial checksum.
1067 */
1068 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1069 vif->rx_gso_checksum_fixup++;
1070 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001071 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001072 }
1073
1074 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1075 if (skb->ip_summed != CHECKSUM_PARTIAL)
1076 return 0;
1077
Paul Durrant27216372014-01-09 10:02:47 +00001078 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001079}
1080
1081static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1082{
Wei Liu059dfa62013-10-28 12:07:57 +00001083 u64 now = get_jiffies_64();
1084 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001085 msecs_to_jiffies(vif->credit_usec / 1000);
1086
1087 /* Timer could already be pending in rare cases. */
1088 if (timer_pending(&vif->credit_timeout))
1089 return true;
1090
1091 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001092 if (time_after_eq64(now, next_credit)) {
1093 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001094 tx_add_credit(vif);
1095 }
1096
1097 /* Still too big to send right now? Set a callback. */
1098 if (size > vif->remaining_credit) {
1099 vif->credit_timeout.data =
1100 (unsigned long)vif;
1101 vif->credit_timeout.function =
1102 tx_credit_callback;
1103 mod_timer(&vif->credit_timeout,
1104 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001105 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001106
1107 return true;
1108 }
1109
1110 return false;
1111}
1112
Paul Durrant10574052013-12-11 10:57:15 +00001113static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001114{
Wei Liub3f980b2013-08-26 12:59:38 +01001115 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001116 struct sk_buff *skb;
1117 int ret;
1118
Wei Liub3f980b2013-08-26 12:59:38 +01001119 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
Paul Durrant10574052013-12-11 10:57:15 +00001120 < MAX_PENDING_REQS) &&
1121 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001122 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001123 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001124 struct page *page;
1125 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1126 u16 pending_idx;
1127 RING_IDX idx;
1128 int work_to_do;
1129 unsigned int data_len;
1130 pending_ring_idx_t index;
1131
Ian Campbell488562862013-02-06 23:41:35 +00001132 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1133 XEN_NETIF_TX_RING_SIZE) {
1134 netdev_err(vif->dev,
1135 "Impossible number of requests. "
1136 "req_prod %d, req_cons %d, size %ld\n",
1137 vif->tx.sring->req_prod, vif->tx.req_cons,
1138 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001139 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001140 continue;
1141 }
1142
Paul Durrantd9601a32013-12-11 10:57:16 +00001143 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001144 if (!work_to_do)
1145 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001146
1147 idx = vif->tx.req_cons;
1148 rmb(); /* Ensure that we see the request before we copy it. */
1149 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1150
1151 /* Credit-based scheduling. */
1152 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001153 tx_credit_exceeded(vif, txreq.size))
1154 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001155
1156 vif->remaining_credit -= txreq.size;
1157
1158 work_to_do--;
1159 vif->tx.req_cons = ++idx;
1160
1161 memset(extras, 0, sizeof(extras));
1162 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001163 work_to_do = xenvif_get_extras(vif, extras,
1164 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001165 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001166 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001167 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001168 }
1169
Wei Liu73764192013-08-26 12:59:39 +01001170 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001171 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001172 break;
Ian Campbell488562862013-02-06 23:41:35 +00001173
Ian Campbellf942dc22011-03-15 00:06:18 +00001174 idx += ret;
1175
1176 if (unlikely(txreq.size < ETH_HLEN)) {
1177 netdev_dbg(vif->dev,
1178 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001179 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001180 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001181 }
1182
1183 /* No crossing a page as the payload mustn't fragment. */
1184 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001185 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001186 "txreq.offset: %x, size: %u, end: %lu\n",
1187 txreq.offset, txreq.size,
1188 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001189 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001190 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001191 }
1192
Wei Liub3f980b2013-08-26 12:59:38 +01001193 index = pending_index(vif->pending_cons);
1194 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001195
1196 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001197 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001198 PKT_PROT_LEN : txreq.size;
1199
1200 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1201 GFP_ATOMIC | __GFP_NOWARN);
1202 if (unlikely(skb == NULL)) {
1203 netdev_dbg(vif->dev,
1204 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001205 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001206 break;
1207 }
1208
1209 /* Packets passed to netif_rx() must have some headroom. */
1210 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1211
1212 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1213 struct xen_netif_extra_info *gso;
1214 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1215
Wei Liu73764192013-08-26 12:59:39 +01001216 if (xenvif_set_skb_gso(vif, skb, gso)) {
1217 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001218 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001219 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001220 }
1221 }
1222
1223 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001224 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001225 if (!page) {
1226 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001227 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001228 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001229 }
1230
Ian Campbellf942dc22011-03-15 00:06:18 +00001231 gop->source.u.ref = txreq.gref;
1232 gop->source.domid = vif->domid;
1233 gop->source.offset = txreq.offset;
1234
1235 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1236 gop->dest.domid = DOMID_SELF;
1237 gop->dest.offset = txreq.offset;
1238
1239 gop->len = txreq.size;
1240 gop->flags = GNTCOPY_source_gref;
1241
1242 gop++;
1243
Wei Liub3f980b2013-08-26 12:59:38 +01001244 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001245 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001246 vif->pending_tx_info[pending_idx].head = index;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001247 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001248
1249 __skb_put(skb, data_len);
1250
1251 skb_shinfo(skb)->nr_frags = ret;
1252 if (data_len < txreq.size) {
1253 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001254 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1255 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001256 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001257 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1258 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001259 }
1260
Wei Liub3f980b2013-08-26 12:59:38 +01001261 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001262
Wei Liu73764192013-08-26 12:59:39 +01001263 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001264 if (request_gop == NULL) {
1265 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001266 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001267 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001268 }
1269 gop = request_gop;
1270
Wei Liub3f980b2013-08-26 12:59:38 +01001271 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001272
Ian Campbellf942dc22011-03-15 00:06:18 +00001273 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001274
Wei Liub3f980b2013-08-26 12:59:38 +01001275 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001276 break;
1277 }
1278
Wei Liub3f980b2013-08-26 12:59:38 +01001279 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001280}
1281
Ian Campbellf942dc22011-03-15 00:06:18 +00001282
Paul Durrant10574052013-12-11 10:57:15 +00001283static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001284{
1285 struct gnttab_copy *gop = vif->tx_copy_ops;
1286 struct sk_buff *skb;
1287 int work_done = 0;
1288
Paul Durrant10574052013-12-11 10:57:15 +00001289 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001290 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001291 u16 pending_idx;
1292 unsigned data_len;
1293
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001294 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liub3f980b2013-08-26 12:59:38 +01001295 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001296
1297 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001298 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001299 netdev_dbg(vif->dev, "netback grant failed.\n");
1300 skb_shinfo(skb)->nr_frags = 0;
1301 kfree_skb(skb);
1302 continue;
1303 }
1304
1305 data_len = skb->len;
1306 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001307 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001308 data_len);
1309 if (data_len < txp->size) {
1310 /* Append the packet payload as a fragment. */
1311 txp->offset += data_len;
1312 txp->size -= data_len;
1313 } else {
1314 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001315 xenvif_idx_release(vif, pending_idx,
1316 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001317 }
1318
1319 if (txp->flags & XEN_NETTXF_csum_blank)
1320 skb->ip_summed = CHECKSUM_PARTIAL;
1321 else if (txp->flags & XEN_NETTXF_data_validated)
1322 skb->ip_summed = CHECKSUM_UNNECESSARY;
1323
Wei Liu73764192013-08-26 12:59:39 +01001324 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001325
Paul Durrant2eba61d2013-10-16 17:50:29 +01001326 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001327 int target = min_t(int, skb->len, PKT_PROT_LEN);
1328 __pskb_pull_tail(skb, target - skb_headlen(skb));
1329 }
1330
1331 skb->dev = vif->dev;
1332 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001333 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001334
1335 if (checksum_setup(vif, skb)) {
1336 netdev_dbg(vif->dev,
1337 "Can't setup checksum in net_tx_action\n");
1338 kfree_skb(skb);
1339 continue;
1340 }
1341
Jason Wang40893fd2013-03-26 23:11:22 +00001342 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001343
Paul Durrantb89587a2013-12-17 11:44:35 +00001344 /* If the packet is GSO then we will have just set up the
1345 * transport header offset in checksum_setup so it's now
1346 * straightforward to calculate gso_segs.
1347 */
1348 if (skb_is_gso(skb)) {
1349 int mss = skb_shinfo(skb)->gso_size;
1350 int hdrlen = skb_transport_header(skb) -
1351 skb_mac_header(skb) +
1352 tcp_hdrlen(skb);
1353
1354 skb_shinfo(skb)->gso_segs =
1355 DIV_ROUND_UP(skb->len - hdrlen, mss);
1356 }
1357
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 vif->dev->stats.rx_bytes += skb->len;
1359 vif->dev->stats.rx_packets++;
1360
Wei Liub3f980b2013-08-26 12:59:38 +01001361 work_done++;
1362
1363 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001364 }
Wei Liub3f980b2013-08-26 12:59:38 +01001365
1366 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001367}
1368
1369/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001370int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001371{
1372 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001373 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001374
Wei Liub3f980b2013-08-26 12:59:38 +01001375 if (unlikely(!tx_work_todo(vif)))
1376 return 0;
1377
Paul Durrant10574052013-12-11 10:57:15 +00001378 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001379
1380 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001381 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001382
Wei Liub3f980b2013-08-26 12:59:38 +01001383 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001384
Paul Durrant10574052013-12-11 10:57:15 +00001385 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001386
1387 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001388}
1389
Wei Liu73764192013-08-26 12:59:39 +01001390static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1391 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001392{
Ian Campbellf942dc22011-03-15 00:06:18 +00001393 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001394 pending_ring_idx_t head;
1395 u16 peek; /* peek into next tx request */
1396
Wei Liub3f980b2013-08-26 12:59:38 +01001397 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001398
1399 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001400 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001401 return;
1402
Wei Liub3f980b2013-08-26 12:59:38 +01001403 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001404
Wei Liu2810e5b2013-04-22 02:20:42 +00001405 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001406
Wei Liub3f980b2013-08-26 12:59:38 +01001407 BUG_ON(!pending_tx_is_head(vif, head));
1408 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001409
Wei Liu2810e5b2013-04-22 02:20:42 +00001410 do {
1411 pending_ring_idx_t index;
1412 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001413 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001414
Wei Liub3f980b2013-08-26 12:59:38 +01001415 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001416 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001417
Wei Liu2810e5b2013-04-22 02:20:42 +00001418 /* Setting any number other than
1419 * INVALID_PENDING_RING_IDX indicates this slot is
1420 * starting a new packet / ending a previous packet.
1421 */
1422 pending_tx_info->head = 0;
1423
Wei Liub3f980b2013-08-26 12:59:38 +01001424 index = pending_index(vif->pending_prod++);
1425 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001426
Wei Liub3f980b2013-08-26 12:59:38 +01001427 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001428
Wei Liub3f980b2013-08-26 12:59:38 +01001429 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001430
Wei Liub3f980b2013-08-26 12:59:38 +01001431 put_page(vif->mmap_pages[pending_idx]);
1432 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001433}
1434
Wei Liu2810e5b2013-04-22 02:20:42 +00001435
Ian Campbellf942dc22011-03-15 00:06:18 +00001436static void make_tx_response(struct xenvif *vif,
1437 struct xen_netif_tx_request *txp,
1438 s8 st)
1439{
1440 RING_IDX i = vif->tx.rsp_prod_pvt;
1441 struct xen_netif_tx_response *resp;
1442 int notify;
1443
1444 resp = RING_GET_RESPONSE(&vif->tx, i);
1445 resp->id = txp->id;
1446 resp->status = st;
1447
1448 if (txp->flags & XEN_NETTXF_extra_info)
1449 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1450
1451 vif->tx.rsp_prod_pvt = ++i;
1452 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1453 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001454 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001455}
1456
1457static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1458 u16 id,
1459 s8 st,
1460 u16 offset,
1461 u16 size,
1462 u16 flags)
1463{
1464 RING_IDX i = vif->rx.rsp_prod_pvt;
1465 struct xen_netif_rx_response *resp;
1466
1467 resp = RING_GET_RESPONSE(&vif->rx, i);
1468 resp->offset = offset;
1469 resp->flags = flags;
1470 resp->id = id;
1471 resp->status = (s16)size;
1472 if (st < 0)
1473 resp->status = (s16)st;
1474
1475 vif->rx.rsp_prod_pvt = ++i;
1476
1477 return resp;
1478}
1479
Wei Liub3f980b2013-08-26 12:59:38 +01001480static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001481{
Zoltan Kiss9ab98312014-02-04 19:54:37 +00001482 return !skb_queue_empty(&vif->rx_queue) &&
1483 xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
Ian Campbellf942dc22011-03-15 00:06:18 +00001484}
1485
Wei Liub3f980b2013-08-26 12:59:38 +01001486static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001487{
1488
Wei Liub3f980b2013-08-26 12:59:38 +01001489 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1490 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1491 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001492 return 1;
1493
1494 return 0;
1495}
1496
Wei Liu73764192013-08-26 12:59:39 +01001497void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001498{
David Vrabelc9d63692011-09-29 16:53:31 +01001499 if (vif->tx.sring)
1500 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1501 vif->tx.sring);
1502 if (vif->rx.sring)
1503 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1504 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001505}
1506
Wei Liu73764192013-08-26 12:59:39 +01001507int xenvif_map_frontend_rings(struct xenvif *vif,
1508 grant_ref_t tx_ring_ref,
1509 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001510{
David Vrabelc9d63692011-09-29 16:53:31 +01001511 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001512 struct xen_netif_tx_sring *txs;
1513 struct xen_netif_rx_sring *rxs;
1514
1515 int err = -ENOMEM;
1516
David Vrabelc9d63692011-09-29 16:53:31 +01001517 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1518 tx_ring_ref, &addr);
1519 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001520 goto err;
1521
David Vrabelc9d63692011-09-29 16:53:31 +01001522 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001523 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1524
David Vrabelc9d63692011-09-29 16:53:31 +01001525 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1526 rx_ring_ref, &addr);
1527 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001528 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001529
David Vrabelc9d63692011-09-29 16:53:31 +01001530 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001531 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1532
1533 return 0;
1534
1535err:
Wei Liu73764192013-08-26 12:59:39 +01001536 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001537 return err;
1538}
1539
Paul Durrantca2f09f2013-12-06 16:36:07 +00001540void xenvif_stop_queue(struct xenvif *vif)
1541{
1542 if (!vif->can_queue)
1543 return;
1544
1545 netif_stop_queue(vif->dev);
1546}
1547
1548static void xenvif_start_queue(struct xenvif *vif)
1549{
1550 if (xenvif_schedulable(vif))
1551 netif_wake_queue(vif->dev);
1552}
1553
Wei Liu73764192013-08-26 12:59:39 +01001554int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001555{
1556 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001557 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001558
1559 while (!kthread_should_stop()) {
1560 wait_event_interruptible(vif->wq,
1561 rx_work_todo(vif) ||
1562 kthread_should_stop());
1563 if (kthread_should_stop())
1564 break;
1565
Paul Durrantca2f09f2013-12-06 16:36:07 +00001566 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001567 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001568
Paul Durrantca2f09f2013-12-06 16:36:07 +00001569 if (skb_queue_empty(&vif->rx_queue) &&
1570 netif_queue_stopped(vif->dev))
1571 xenvif_start_queue(vif);
1572
Wei Liub3f980b2013-08-26 12:59:38 +01001573 cond_resched();
1574 }
1575
Paul Durrantca2f09f2013-12-06 16:36:07 +00001576 /* Bin any remaining skbs */
1577 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1578 dev_kfree_skb(skb);
1579
Wei Liub3f980b2013-08-26 12:59:38 +01001580 return 0;
1581}
1582
Ian Campbellf942dc22011-03-15 00:06:18 +00001583static int __init netback_init(void)
1584{
Ian Campbellf942dc22011-03-15 00:06:18 +00001585 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001586
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001587 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001588 return -ENODEV;
1589
Wei Liu37641492013-05-02 00:43:59 +00001590 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001591 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1592 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001593 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001594 }
1595
Ian Campbellf942dc22011-03-15 00:06:18 +00001596 rc = xenvif_xenbus_init();
1597 if (rc)
1598 goto failed_init;
1599
1600 return 0;
1601
1602failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001603 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001604}
1605
1606module_init(netback_init);
1607
Wei Liub103f352013-05-16 23:26:11 +00001608static void __exit netback_fini(void)
1609{
Wei Liub103f352013-05-16 23:26:11 +00001610 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001611}
1612module_exit(netback_fini);
1613
Ian Campbellf942dc22011-03-15 00:06:18 +00001614MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001615MODULE_ALIAS("xen-backend:vif");