blob: 438d0c09b7e6019b513f8e996f5549bb327463d2 [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;
Annie Li5bd07672014-03-10 22:58:34 +0800243 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
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. */
Annie Li5bd07672014-03-10 22:58:34 +0800302 if (skb_is_gso(skb)) {
303 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
304 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
305 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
306 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
307 }
Paul Durrant82cada22013-10-16 17:50:32 +0100308
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;
Ian Campbellf942dc22011-03-15 00:06:18 +0000341
342 old_meta_prod = npo->meta_prod;
343
Annie Li5bd07672014-03-10 22:58:34 +0800344 gso_type = XEN_NETIF_GSO_TYPE_NONE;
345 if (skb_is_gso(skb)) {
346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
349 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100350 }
351
Ian Campbellf942dc22011-03-15 00:06:18 +0000352 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000353 if ((1 << gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000354 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
355 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100356 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800357 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000358 meta->size = 0;
359 meta->id = req->id;
360 }
361
362 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
363 meta = npo->meta + npo->meta_prod++;
364
Paul Durrant82cada22013-10-16 17:50:32 +0100365 if ((1 << gso_type) & vif->gso_mask) {
366 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800367 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100368 } else {
369 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000370 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100371 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000372
373 meta->size = 0;
374 meta->id = req->id;
375 npo->copy_off = 0;
376 npo->copy_gref = req->gref;
377
378 data = skb->data;
379 while (data < skb_tail_pointer(skb)) {
380 unsigned int offset = offset_in_page(data);
381 unsigned int len = PAGE_SIZE - offset;
382
383 if (data + len > skb_tail_pointer(skb))
384 len = skb_tail_pointer(skb) - data;
385
Wei Liu73764192013-08-26 12:59:39 +0100386 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100387 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000388 data += len;
389 }
390
391 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100392 xenvif_gop_frag_copy(vif, skb, npo,
393 skb_frag_page(&skb_shinfo(skb)->frags[i]),
394 skb_frag_size(&skb_shinfo(skb)->frags[i]),
395 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100396 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000397 }
398
399 return npo->meta_prod - old_meta_prod;
400}
401
402/*
Wei Liu73764192013-08-26 12:59:39 +0100403 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000404 * used to set up the operations on the top of
405 * netrx_pending_operations, which have since been done. Check that
406 * they didn't give any errors and advance over them.
407 */
Wei Liu73764192013-08-26 12:59:39 +0100408static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
409 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000410{
411 struct gnttab_copy *copy_op;
412 int status = XEN_NETIF_RSP_OKAY;
413 int i;
414
415 for (i = 0; i < nr_meta_slots; i++) {
416 copy_op = npo->copy + npo->copy_cons++;
417 if (copy_op->status != GNTST_okay) {
418 netdev_dbg(vif->dev,
419 "Bad status %d from copy to DOM%d.\n",
420 copy_op->status, vif->domid);
421 status = XEN_NETIF_RSP_ERROR;
422 }
423 }
424
425 return status;
426}
427
Wei Liu73764192013-08-26 12:59:39 +0100428static void xenvif_add_frag_responses(struct xenvif *vif, int status,
429 struct xenvif_rx_meta *meta,
430 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000431{
432 int i;
433 unsigned long offset;
434
435 /* No fragments used */
436 if (nr_meta_slots <= 1)
437 return;
438
439 nr_meta_slots--;
440
441 for (i = 0; i < nr_meta_slots; i++) {
442 int flags;
443 if (i == nr_meta_slots - 1)
444 flags = 0;
445 else
446 flags = XEN_NETRXF_more_data;
447
448 offset = 0;
449 make_rx_response(vif, meta[i].id, status, offset,
450 meta[i].size, flags);
451 }
452}
453
Wei Liu33bc8012013-10-08 10:54:21 +0100454struct skb_cb_overlay {
455 int meta_slots_used;
456};
457
Paul Durrantca2f09f2013-12-06 16:36:07 +0000458void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000459{
Wei Liub3f980b2013-08-26 12:59:38 +0100460 wake_up(&vif->wq);
461}
462
Paul Durrantca2f09f2013-12-06 16:36:07 +0000463static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100464{
Ian Campbellf942dc22011-03-15 00:06:18 +0000465 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000466 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000467 struct xen_netif_rx_response *resp;
468 struct sk_buff_head rxq;
469 struct sk_buff *skb;
470 LIST_HEAD(notify);
471 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000472 unsigned long offset;
473 struct skb_cb_overlay *sco;
Paul Durrant11b57f92014-01-08 12:41:58 +0000474 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000475
476 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100477 .copy = vif->grant_copy_op,
478 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000479 };
480
481 skb_queue_head_init(&rxq);
482
Wei Liub3f980b2013-08-26 12:59:38 +0100483 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000484 RING_IDX max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000485 int i;
486
487 /* We need a cheap worse case estimate for the number of
488 * slots we'll use.
489 */
490
491 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
492 skb_headlen(skb),
493 PAGE_SIZE);
494 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
495 unsigned int size;
496 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
497 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
498 }
Annie Li5bd07672014-03-10 22:58:34 +0800499 if (skb_is_gso(skb) &&
500 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
501 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000502 max_slots_needed++;
503
504 /* If the skb may not fit then bail out now */
505 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
506 skb_queue_head(&vif->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000507 need_to_notify = true;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000508 vif->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000509 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000510 } else
511 vif->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000512
513 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100514 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000515 BUG_ON(sco->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000516
517 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000518 }
519
Wei Liub3f980b2013-08-26 12:59:38 +0100520 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000521
522 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000523 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000524
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000525 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liub3f980b2013-08-26 12:59:38 +0100526 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000527
528 while ((skb = __skb_dequeue(&rxq)) != NULL) {
529 sco = (struct skb_cb_overlay *)skb->cb;
530
Paul Durrant82cada22013-10-16 17:50:32 +0100531 if ((1 << vif->meta[npo.meta_cons].gso_type) &
532 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000533 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100534 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000535
536 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
537
Wei Liub3f980b2013-08-26 12:59:38 +0100538 resp->offset = vif->meta[npo.meta_cons].gso_size;
539 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000540 resp->status = sco->meta_slots_used;
541
542 npo.meta_cons++;
543 sco->meta_slots_used--;
544 }
545
546
547 vif->dev->stats.tx_bytes += skb->len;
548 vif->dev->stats.tx_packets++;
549
Wei Liu73764192013-08-26 12:59:39 +0100550 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000551
552 if (sco->meta_slots_used == 1)
553 flags = 0;
554 else
555 flags = XEN_NETRXF_more_data;
556
557 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
558 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
559 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
560 /* remote but checksummed. */
561 flags |= XEN_NETRXF_data_validated;
562
563 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100564 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000565 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100566 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000567 flags);
568
Paul Durrant82cada22013-10-16 17:50:32 +0100569 if ((1 << vif->meta[npo.meta_cons].gso_type) &
570 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 struct xen_netif_extra_info *gso =
572 (struct xen_netif_extra_info *)
573 RING_GET_RESPONSE(&vif->rx,
574 vif->rx.rsp_prod_pvt++);
575
576 resp->flags |= XEN_NETRXF_extra_info;
577
Paul Durrant82cada22013-10-16 17:50:32 +0100578 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100579 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000580 gso->u.gso.pad = 0;
581 gso->u.gso.features = 0;
582
583 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
584 gso->flags = 0;
585 }
586
Wei Liu73764192013-08-26 12:59:39 +0100587 xenvif_add_frag_responses(vif, status,
588 vif->meta + npo.meta_cons + 1,
589 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000590
591 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000592
Paul Durrant11b57f92014-01-08 12:41:58 +0000593 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100594
Ian Campbellf942dc22011-03-15 00:06:18 +0000595 npo.meta_cons += sco->meta_slots_used;
596 dev_kfree_skb(skb);
597 }
598
Paul Durrantca2f09f2013-12-06 16:36:07 +0000599done:
Wei Liub3f980b2013-08-26 12:59:38 +0100600 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000601 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000602}
603
Wei Liu73764192013-08-26 12:59:39 +0100604void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000605{
606 int more_to_do;
607
608 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
609
610 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100611 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000612}
613
614static void tx_add_credit(struct xenvif *vif)
615{
616 unsigned long max_burst, max_credit;
617
618 /*
619 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
620 * Otherwise the interface can seize up due to insufficient credit.
621 */
622 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
623 max_burst = min(max_burst, 131072UL);
624 max_burst = max(max_burst, vif->credit_bytes);
625
626 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
627 max_credit = vif->remaining_credit + vif->credit_bytes;
628 if (max_credit < vif->remaining_credit)
629 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
630
631 vif->remaining_credit = min(max_credit, max_burst);
632}
633
634static void tx_credit_callback(unsigned long data)
635{
636 struct xenvif *vif = (struct xenvif *)data;
637 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100638 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000639}
640
Wei Liu73764192013-08-26 12:59:39 +0100641static void xenvif_tx_err(struct xenvif *vif,
642 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000643{
644 RING_IDX cons = vif->tx.req_cons;
645
646 do {
647 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000648 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000649 break;
650 txp = RING_GET_REQUEST(&vif->tx, cons++);
651 } while (1);
652 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000653}
654
Wei Liu73764192013-08-26 12:59:39 +0100655static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000656{
657 netdev_err(vif->dev, "fatal error; disabling device\n");
658 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000659}
660
Wei Liu73764192013-08-26 12:59:39 +0100661static int xenvif_count_requests(struct xenvif *vif,
662 struct xen_netif_tx_request *first,
663 struct xen_netif_tx_request *txp,
664 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000665{
666 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000667 int slots = 0;
668 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000669 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000670
671 if (!(first->flags & XEN_NETTXF_more_data))
672 return 0;
673
674 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000675 struct xen_netif_tx_request dropped_tx = { 0 };
676
Wei Liu2810e5b2013-04-22 02:20:42 +0000677 if (slots >= work_to_do) {
678 netdev_err(vif->dev,
679 "Asked for %d slots but exceeds this limit\n",
680 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100681 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000682 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000683 }
684
Wei Liu2810e5b2013-04-22 02:20:42 +0000685 /* This guest is really using too many slots and
686 * considered malicious.
687 */
Wei Liu37641492013-05-02 00:43:59 +0000688 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000689 netdev_err(vif->dev,
690 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000691 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100692 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000693 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000694 }
695
Wei Liu2810e5b2013-04-22 02:20:42 +0000696 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000697 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
698 * the historical MAX_SKB_FRAGS value 18 to honor the
699 * same behavior as before. Any packet using more than
700 * 18 slots but less than fatal_skb_slots slots is
701 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000702 */
Wei Liu37641492013-05-02 00:43:59 +0000703 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000704 if (net_ratelimit())
705 netdev_dbg(vif->dev,
706 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000707 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000708 drop_err = -E2BIG;
709 }
710
Wei Liu59ccb4e2013-05-02 00:43:58 +0000711 if (drop_err)
712 txp = &dropped_tx;
713
Wei Liu2810e5b2013-04-22 02:20:42 +0000714 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000715 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000716
717 /* If the guest submitted a frame >= 64 KiB then
718 * first->size overflowed and following slots will
719 * appear to be larger than the frame.
720 *
721 * This cannot be fatal error as there are buggy
722 * frontends that do this.
723 *
724 * Consume all slots and drop the packet.
725 */
726 if (!drop_err && txp->size > first->size) {
727 if (net_ratelimit())
728 netdev_dbg(vif->dev,
729 "Invalid tx request, slot size %u > remaining size %u\n",
730 txp->size, first->size);
731 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000732 }
733
734 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000735 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000736
737 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000738 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000739 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100740 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000741 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000742 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000743
744 more_data = txp->flags & XEN_NETTXF_more_data;
745
746 if (!drop_err)
747 txp++;
748
749 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000750
751 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100752 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000753 return drop_err;
754 }
755
756 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000757}
758
Wei Liu73764192013-08-26 12:59:39 +0100759static struct page *xenvif_alloc_page(struct xenvif *vif,
760 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000761{
762 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100763
764 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000765 if (!page)
766 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100767 vif->mmap_pages[pending_idx] = page;
768
Ian Campbellf942dc22011-03-15 00:06:18 +0000769 return page;
770}
771
Wei Liu73764192013-08-26 12:59:39 +0100772static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
773 struct sk_buff *skb,
774 struct xen_netif_tx_request *txp,
775 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000776{
777 struct skb_shared_info *shinfo = skb_shinfo(skb);
778 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000779 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000780 u16 head_idx = 0;
781 int slot, start;
782 struct page *page;
783 pending_ring_idx_t index, start_idx = 0;
784 uint16_t dst_offset;
785 unsigned int nr_slots;
786 struct pending_tx_info *first = NULL;
787
788 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000789 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000790 */
791 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000792
793 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000794 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000795
Wei Liu2810e5b2013-04-22 02:20:42 +0000796 /* Coalesce tx requests, at this point the packet passed in
797 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100798 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000799 */
800 for (shinfo->nr_frags = slot = start; slot < nr_slots;
801 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000802 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100803 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000804
Wei Liub3f980b2013-08-26 12:59:38 +0100805 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000806 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000807 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000808
Wei Liu2810e5b2013-04-22 02:20:42 +0000809 dst_offset = 0;
810 first = NULL;
811 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
812 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000813
Wei Liu2810e5b2013-04-22 02:20:42 +0000814 gop->source.u.ref = txp->gref;
815 gop->source.domid = vif->domid;
816 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000817
Wei Liu2810e5b2013-04-22 02:20:42 +0000818 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000819
Wei Liu2810e5b2013-04-22 02:20:42 +0000820 gop->dest.offset = dst_offset;
821 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000822
Wei Liu2810e5b2013-04-22 02:20:42 +0000823 if (dst_offset + txp->size > PAGE_SIZE) {
824 /* This page can only merge a portion
825 * of tx request. Do not increment any
826 * pointer / counter here. The txp
827 * will be dealt with in future
828 * rounds, eventually hitting the
829 * `else` branch.
830 */
831 gop->len = PAGE_SIZE - dst_offset;
832 txp->offset += gop->len;
833 txp->size -= gop->len;
834 dst_offset += gop->len; /* quit loop */
835 } else {
836 /* This tx request can be merged in the page */
837 gop->len = txp->size;
838 dst_offset += gop->len;
839
Wei Liub3f980b2013-08-26 12:59:38 +0100840 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000841
Wei Liub3f980b2013-08-26 12:59:38 +0100842 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000843
844 memcpy(&pending_tx_info[pending_idx].req, txp,
845 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000846
847 /* Poison these fields, corresponding
848 * fields for head tx req will be set
849 * to correct values after the loop.
850 */
Wei Liub3f980b2013-08-26 12:59:38 +0100851 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000852 pending_tx_info[pending_idx].head =
853 INVALID_PENDING_RING_IDX;
854
855 if (!first) {
856 first = &pending_tx_info[pending_idx];
857 start_idx = index;
858 head_idx = pending_idx;
859 }
860
861 txp++;
862 slot++;
863 }
864
865 gop++;
866 }
867
868 first->req.offset = 0;
869 first->req.size = dst_offset;
870 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100871 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000872 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000873 }
874
Wei Liu2810e5b2013-04-22 02:20:42 +0000875 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
876
Ian Campbellf942dc22011-03-15 00:06:18 +0000877 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000878err:
879 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000880 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100881 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000882 frag_get_pending_idx(&frags[shinfo->nr_frags]),
883 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000884 }
885 /* The head too, if necessary. */
886 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100887 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000888
889 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000890}
891
Wei Liu73764192013-08-26 12:59:39 +0100892static int xenvif_tx_check_gop(struct xenvif *vif,
893 struct sk_buff *skb,
894 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000895{
896 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000897 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000898 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000899 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000900 int nr_frags = shinfo->nr_frags;
901 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000902 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000903
904 /* Check status of header. */
905 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000906 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100907 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000908
909 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000910 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000911
912 for (i = start; i < nr_frags; i++) {
913 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000914 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000915
Ian Campbellea066ad2011-10-05 00:28:46 +0000916 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100917 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000918 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000919
920 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000921 do {
922 newerr = (++gop)->status;
923 if (newerr)
924 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100925 peek = vif->pending_ring[pending_index(++head)];
926 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000927
Ian Campbellf942dc22011-03-15 00:06:18 +0000928 if (likely(!newerr)) {
929 /* Had a previous error? Invalidate this fragment. */
930 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100931 xenvif_idx_release(vif, pending_idx,
932 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000933 continue;
934 }
935
936 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100937 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000938
939 /* Not the first error? Preceding frags already invalidated. */
940 if (err)
941 continue;
942
943 /* First error: invalidate header and preceding fragments. */
944 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +0100945 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000946 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000947 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +0100948 xenvif_idx_release(vif, pending_idx,
949 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000950 }
951
952 /* Remember the error: invalidate all subsequent fragments. */
953 err = newerr;
954 }
955
956 *gopp = gop + 1;
957 return err;
958}
959
Wei Liu73764192013-08-26 12:59:39 +0100960static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000961{
962 struct skb_shared_info *shinfo = skb_shinfo(skb);
963 int nr_frags = shinfo->nr_frags;
964 int i;
965
966 for (i = 0; i < nr_frags; i++) {
967 skb_frag_t *frag = shinfo->frags + i;
968 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000969 struct page *page;
970 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000971
Ian Campbellea066ad2011-10-05 00:28:46 +0000972 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +0000973
Wei Liub3f980b2013-08-26 12:59:38 +0100974 txp = &vif->pending_tx_info[pending_idx].req;
975 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +0000976 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +0000977 skb->len += txp->size;
978 skb->data_len += txp->size;
979 skb->truesize += txp->size;
980
Wei Liu73764192013-08-26 12:59:39 +0100981 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +0100982 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +0100983 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000984 }
985}
986
Wei Liu73764192013-08-26 12:59:39 +0100987static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +0000988 struct xen_netif_extra_info *extras,
989 int work_to_do)
990{
991 struct xen_netif_extra_info extra;
992 RING_IDX cons = vif->tx.req_cons;
993
994 do {
995 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +0000996 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +0100997 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000998 return -EBADR;
999 }
1000
1001 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1002 sizeof(extra));
1003 if (unlikely(!extra.type ||
1004 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1005 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001006 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001007 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001008 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001009 return -EINVAL;
1010 }
1011
1012 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1013 vif->tx.req_cons = ++cons;
1014 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1015
1016 return work_to_do;
1017}
1018
Wei Liu73764192013-08-26 12:59:39 +01001019static int xenvif_set_skb_gso(struct xenvif *vif,
1020 struct sk_buff *skb,
1021 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001022{
1023 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001024 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001025 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001026 return -EINVAL;
1027 }
1028
Paul Durranta9468582013-10-16 17:50:31 +01001029 switch (gso->u.gso.type) {
1030 case XEN_NETIF_GSO_TYPE_TCPV4:
1031 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1032 break;
1033 case XEN_NETIF_GSO_TYPE_TCPV6:
1034 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1035 break;
1036 default:
Ian Campbell488562862013-02-06 23:41:35 +00001037 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001038 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001039 return -EINVAL;
1040 }
1041
1042 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001043 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001044
1045 return 0;
1046}
1047
1048static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1049{
Paul Durrant27216372014-01-09 10:02:47 +00001050 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001051
Paul Durrant2eba61d2013-10-16 17:50:29 +01001052 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001053 * peers can fail to set NETRXF_csum_blank when sending a GSO
1054 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1055 * recalculate the partial checksum.
1056 */
1057 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1058 vif->rx_gso_checksum_fixup++;
1059 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001060 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001061 }
1062
1063 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1064 if (skb->ip_summed != CHECKSUM_PARTIAL)
1065 return 0;
1066
Paul Durrant27216372014-01-09 10:02:47 +00001067 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001068}
1069
1070static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1071{
Wei Liu059dfa62013-10-28 12:07:57 +00001072 u64 now = get_jiffies_64();
1073 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001074 msecs_to_jiffies(vif->credit_usec / 1000);
1075
1076 /* Timer could already be pending in rare cases. */
1077 if (timer_pending(&vif->credit_timeout))
1078 return true;
1079
1080 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001081 if (time_after_eq64(now, next_credit)) {
1082 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001083 tx_add_credit(vif);
1084 }
1085
1086 /* Still too big to send right now? Set a callback. */
1087 if (size > vif->remaining_credit) {
1088 vif->credit_timeout.data =
1089 (unsigned long)vif;
1090 vif->credit_timeout.function =
1091 tx_credit_callback;
1092 mod_timer(&vif->credit_timeout,
1093 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001094 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001095
1096 return true;
1097 }
1098
1099 return false;
1100}
1101
Paul Durrant10574052013-12-11 10:57:15 +00001102static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001103{
Wei Liub3f980b2013-08-26 12:59:38 +01001104 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001105 struct sk_buff *skb;
1106 int ret;
1107
Wei Liub3f980b2013-08-26 12:59:38 +01001108 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
Paul Durrant10574052013-12-11 10:57:15 +00001109 < MAX_PENDING_REQS) &&
1110 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001111 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001112 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001113 struct page *page;
1114 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1115 u16 pending_idx;
1116 RING_IDX idx;
1117 int work_to_do;
1118 unsigned int data_len;
1119 pending_ring_idx_t index;
1120
Ian Campbell488562862013-02-06 23:41:35 +00001121 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1122 XEN_NETIF_TX_RING_SIZE) {
1123 netdev_err(vif->dev,
1124 "Impossible number of requests. "
1125 "req_prod %d, req_cons %d, size %ld\n",
1126 vif->tx.sring->req_prod, vif->tx.req_cons,
1127 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001128 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001129 continue;
1130 }
1131
Paul Durrantd9601a32013-12-11 10:57:16 +00001132 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001133 if (!work_to_do)
1134 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001135
1136 idx = vif->tx.req_cons;
1137 rmb(); /* Ensure that we see the request before we copy it. */
1138 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1139
1140 /* Credit-based scheduling. */
1141 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001142 tx_credit_exceeded(vif, txreq.size))
1143 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001144
1145 vif->remaining_credit -= txreq.size;
1146
1147 work_to_do--;
1148 vif->tx.req_cons = ++idx;
1149
1150 memset(extras, 0, sizeof(extras));
1151 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001152 work_to_do = xenvif_get_extras(vif, extras,
1153 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001154 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001155 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001156 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001157 }
1158
Wei Liu73764192013-08-26 12:59:39 +01001159 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001160 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001161 break;
Ian Campbell488562862013-02-06 23:41:35 +00001162
Ian Campbellf942dc22011-03-15 00:06:18 +00001163 idx += ret;
1164
1165 if (unlikely(txreq.size < ETH_HLEN)) {
1166 netdev_dbg(vif->dev,
1167 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001168 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001169 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001170 }
1171
1172 /* No crossing a page as the payload mustn't fragment. */
1173 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001174 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001175 "txreq.offset: %x, size: %u, end: %lu\n",
1176 txreq.offset, txreq.size,
1177 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001178 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001179 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001180 }
1181
Wei Liub3f980b2013-08-26 12:59:38 +01001182 index = pending_index(vif->pending_cons);
1183 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001184
1185 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001186 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001187 PKT_PROT_LEN : txreq.size;
1188
1189 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1190 GFP_ATOMIC | __GFP_NOWARN);
1191 if (unlikely(skb == NULL)) {
1192 netdev_dbg(vif->dev,
1193 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001194 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001195 break;
1196 }
1197
1198 /* Packets passed to netif_rx() must have some headroom. */
1199 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1200
1201 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1202 struct xen_netif_extra_info *gso;
1203 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1204
Wei Liu73764192013-08-26 12:59:39 +01001205 if (xenvif_set_skb_gso(vif, skb, gso)) {
1206 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001207 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001208 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001209 }
1210 }
1211
1212 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001213 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001214 if (!page) {
1215 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001216 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001217 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001218 }
1219
Ian Campbellf942dc22011-03-15 00:06:18 +00001220 gop->source.u.ref = txreq.gref;
1221 gop->source.domid = vif->domid;
1222 gop->source.offset = txreq.offset;
1223
1224 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1225 gop->dest.domid = DOMID_SELF;
1226 gop->dest.offset = txreq.offset;
1227
1228 gop->len = txreq.size;
1229 gop->flags = GNTCOPY_source_gref;
1230
1231 gop++;
1232
Wei Liub3f980b2013-08-26 12:59:38 +01001233 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001234 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001235 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001236 *((u16 *)skb->data) = pending_idx;
1237
1238 __skb_put(skb, data_len);
1239
1240 skb_shinfo(skb)->nr_frags = ret;
1241 if (data_len < txreq.size) {
1242 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001243 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1244 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001245 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001246 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1247 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001248 }
1249
Wei Liub3f980b2013-08-26 12:59:38 +01001250 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001251
Wei Liu73764192013-08-26 12:59:39 +01001252 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001253 if (request_gop == NULL) {
1254 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001255 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001256 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001257 }
1258 gop = request_gop;
1259
Wei Liub3f980b2013-08-26 12:59:38 +01001260 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001261
Ian Campbellf942dc22011-03-15 00:06:18 +00001262 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001263
Wei Liub3f980b2013-08-26 12:59:38 +01001264 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001265 break;
1266 }
1267
Wei Liub3f980b2013-08-26 12:59:38 +01001268 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001269}
1270
Ian Campbellf942dc22011-03-15 00:06:18 +00001271
Paul Durrant10574052013-12-11 10:57:15 +00001272static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001273{
1274 struct gnttab_copy *gop = vif->tx_copy_ops;
1275 struct sk_buff *skb;
1276 int work_done = 0;
1277
Paul Durrant10574052013-12-11 10:57:15 +00001278 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001279 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001280 u16 pending_idx;
1281 unsigned data_len;
1282
1283 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001284 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001285
1286 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001287 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001288 netdev_dbg(vif->dev, "netback grant failed.\n");
1289 skb_shinfo(skb)->nr_frags = 0;
1290 kfree_skb(skb);
1291 continue;
1292 }
1293
1294 data_len = skb->len;
1295 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001296 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001297 data_len);
1298 if (data_len < txp->size) {
1299 /* Append the packet payload as a fragment. */
1300 txp->offset += data_len;
1301 txp->size -= data_len;
1302 } else {
1303 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001304 xenvif_idx_release(vif, pending_idx,
1305 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001306 }
1307
1308 if (txp->flags & XEN_NETTXF_csum_blank)
1309 skb->ip_summed = CHECKSUM_PARTIAL;
1310 else if (txp->flags & XEN_NETTXF_data_validated)
1311 skb->ip_summed = CHECKSUM_UNNECESSARY;
1312
Wei Liu73764192013-08-26 12:59:39 +01001313 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001314
Paul Durrant2eba61d2013-10-16 17:50:29 +01001315 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001316 int target = min_t(int, skb->len, PKT_PROT_LEN);
1317 __pskb_pull_tail(skb, target - skb_headlen(skb));
1318 }
1319
1320 skb->dev = vif->dev;
1321 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001322 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001323
1324 if (checksum_setup(vif, skb)) {
1325 netdev_dbg(vif->dev,
1326 "Can't setup checksum in net_tx_action\n");
1327 kfree_skb(skb);
1328 continue;
1329 }
1330
Jason Wang40893fd2013-03-26 23:11:22 +00001331 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001332
Paul Durrantb89587a2013-12-17 11:44:35 +00001333 /* If the packet is GSO then we will have just set up the
1334 * transport header offset in checksum_setup so it's now
1335 * straightforward to calculate gso_segs.
1336 */
1337 if (skb_is_gso(skb)) {
1338 int mss = skb_shinfo(skb)->gso_size;
1339 int hdrlen = skb_transport_header(skb) -
1340 skb_mac_header(skb) +
1341 tcp_hdrlen(skb);
1342
1343 skb_shinfo(skb)->gso_segs =
1344 DIV_ROUND_UP(skb->len - hdrlen, mss);
1345 }
1346
Ian Campbellf942dc22011-03-15 00:06:18 +00001347 vif->dev->stats.rx_bytes += skb->len;
1348 vif->dev->stats.rx_packets++;
1349
Wei Liub3f980b2013-08-26 12:59:38 +01001350 work_done++;
1351
1352 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001353 }
Wei Liub3f980b2013-08-26 12:59:38 +01001354
1355 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001356}
1357
1358/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001359int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001360{
1361 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001362 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001363
Wei Liub3f980b2013-08-26 12:59:38 +01001364 if (unlikely(!tx_work_todo(vif)))
1365 return 0;
1366
Paul Durrant10574052013-12-11 10:57:15 +00001367 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001368
1369 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001370 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001371
Wei Liub3f980b2013-08-26 12:59:38 +01001372 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001373
Paul Durrant10574052013-12-11 10:57:15 +00001374 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001375
1376 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001377}
1378
Wei Liu73764192013-08-26 12:59:39 +01001379static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1380 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001381{
Ian Campbellf942dc22011-03-15 00:06:18 +00001382 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001383 pending_ring_idx_t head;
1384 u16 peek; /* peek into next tx request */
1385
Wei Liub3f980b2013-08-26 12:59:38 +01001386 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001387
1388 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001389 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001390 return;
1391
Wei Liub3f980b2013-08-26 12:59:38 +01001392 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001393
Wei Liu2810e5b2013-04-22 02:20:42 +00001394 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001395
Wei Liub3f980b2013-08-26 12:59:38 +01001396 BUG_ON(!pending_tx_is_head(vif, head));
1397 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001398
Wei Liu2810e5b2013-04-22 02:20:42 +00001399 do {
1400 pending_ring_idx_t index;
1401 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001402 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001403
Wei Liub3f980b2013-08-26 12:59:38 +01001404 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001405 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001406
Wei Liu2810e5b2013-04-22 02:20:42 +00001407 /* Setting any number other than
1408 * INVALID_PENDING_RING_IDX indicates this slot is
1409 * starting a new packet / ending a previous packet.
1410 */
1411 pending_tx_info->head = 0;
1412
Wei Liub3f980b2013-08-26 12:59:38 +01001413 index = pending_index(vif->pending_prod++);
1414 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001415
Wei Liub3f980b2013-08-26 12:59:38 +01001416 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001417
Wei Liub3f980b2013-08-26 12:59:38 +01001418 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001419
Wei Liub3f980b2013-08-26 12:59:38 +01001420 put_page(vif->mmap_pages[pending_idx]);
1421 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001422}
1423
Wei Liu2810e5b2013-04-22 02:20:42 +00001424
Ian Campbellf942dc22011-03-15 00:06:18 +00001425static void make_tx_response(struct xenvif *vif,
1426 struct xen_netif_tx_request *txp,
1427 s8 st)
1428{
1429 RING_IDX i = vif->tx.rsp_prod_pvt;
1430 struct xen_netif_tx_response *resp;
1431 int notify;
1432
1433 resp = RING_GET_RESPONSE(&vif->tx, i);
1434 resp->id = txp->id;
1435 resp->status = st;
1436
1437 if (txp->flags & XEN_NETTXF_extra_info)
1438 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1439
1440 vif->tx.rsp_prod_pvt = ++i;
1441 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1442 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001443 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001444}
1445
1446static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1447 u16 id,
1448 s8 st,
1449 u16 offset,
1450 u16 size,
1451 u16 flags)
1452{
1453 RING_IDX i = vif->rx.rsp_prod_pvt;
1454 struct xen_netif_rx_response *resp;
1455
1456 resp = RING_GET_RESPONSE(&vif->rx, i);
1457 resp->offset = offset;
1458 resp->flags = flags;
1459 resp->id = id;
1460 resp->status = (s16)size;
1461 if (st < 0)
1462 resp->status = (s16)st;
1463
1464 vif->rx.rsp_prod_pvt = ++i;
1465
1466 return resp;
1467}
1468
Wei Liub3f980b2013-08-26 12:59:38 +01001469static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001470{
Zoltan Kiss9ab98312014-02-04 19:54:37 +00001471 return !skb_queue_empty(&vif->rx_queue) &&
1472 xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
Ian Campbellf942dc22011-03-15 00:06:18 +00001473}
1474
Wei Liub3f980b2013-08-26 12:59:38 +01001475static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001476{
1477
Wei Liub3f980b2013-08-26 12:59:38 +01001478 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1479 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1480 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001481 return 1;
1482
1483 return 0;
1484}
1485
Wei Liu73764192013-08-26 12:59:39 +01001486void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001487{
David Vrabelc9d63692011-09-29 16:53:31 +01001488 if (vif->tx.sring)
1489 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1490 vif->tx.sring);
1491 if (vif->rx.sring)
1492 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1493 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001494}
1495
Wei Liu73764192013-08-26 12:59:39 +01001496int xenvif_map_frontend_rings(struct xenvif *vif,
1497 grant_ref_t tx_ring_ref,
1498 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001499{
David Vrabelc9d63692011-09-29 16:53:31 +01001500 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001501 struct xen_netif_tx_sring *txs;
1502 struct xen_netif_rx_sring *rxs;
1503
1504 int err = -ENOMEM;
1505
David Vrabelc9d63692011-09-29 16:53:31 +01001506 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1507 tx_ring_ref, &addr);
1508 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001509 goto err;
1510
David Vrabelc9d63692011-09-29 16:53:31 +01001511 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001512 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1513
David Vrabelc9d63692011-09-29 16:53:31 +01001514 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1515 rx_ring_ref, &addr);
1516 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001517 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001518
David Vrabelc9d63692011-09-29 16:53:31 +01001519 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001520 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1521
1522 return 0;
1523
1524err:
Wei Liu73764192013-08-26 12:59:39 +01001525 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001526 return err;
1527}
1528
Paul Durrantca2f09f2013-12-06 16:36:07 +00001529void xenvif_stop_queue(struct xenvif *vif)
1530{
1531 if (!vif->can_queue)
1532 return;
1533
1534 netif_stop_queue(vif->dev);
1535}
1536
1537static void xenvif_start_queue(struct xenvif *vif)
1538{
1539 if (xenvif_schedulable(vif))
1540 netif_wake_queue(vif->dev);
1541}
1542
Wei Liu73764192013-08-26 12:59:39 +01001543int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001544{
1545 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001546 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001547
1548 while (!kthread_should_stop()) {
1549 wait_event_interruptible(vif->wq,
1550 rx_work_todo(vif) ||
1551 kthread_should_stop());
1552 if (kthread_should_stop())
1553 break;
1554
Paul Durrantca2f09f2013-12-06 16:36:07 +00001555 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001556 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001557
Paul Durrantca2f09f2013-12-06 16:36:07 +00001558 if (skb_queue_empty(&vif->rx_queue) &&
1559 netif_queue_stopped(vif->dev))
1560 xenvif_start_queue(vif);
1561
Wei Liub3f980b2013-08-26 12:59:38 +01001562 cond_resched();
1563 }
1564
Paul Durrantca2f09f2013-12-06 16:36:07 +00001565 /* Bin any remaining skbs */
1566 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1567 dev_kfree_skb(skb);
1568
Wei Liub3f980b2013-08-26 12:59:38 +01001569 return 0;
1570}
1571
Ian Campbellf942dc22011-03-15 00:06:18 +00001572static int __init netback_init(void)
1573{
Ian Campbellf942dc22011-03-15 00:06:18 +00001574 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001575
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001576 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001577 return -ENODEV;
1578
Wei Liu37641492013-05-02 00:43:59 +00001579 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001580 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1581 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001582 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001583 }
1584
Ian Campbellf942dc22011-03-15 00:06:18 +00001585 rc = xenvif_xenbus_init();
1586 if (rc)
1587 goto failed_init;
1588
1589 return 0;
1590
1591failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001592 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001593}
1594
1595module_init(netback_init);
1596
Wei Liub103f352013-05-16 23:26:11 +00001597static void __exit netback_fini(void)
1598{
Wei Liub103f352013-05-16 23:26:11 +00001599 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001600}
1601module_exit(netback_fini);
1602
Ian Campbellf942dc22011-03-15 00:06:18 +00001603MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001604MODULE_ALIAS("xen-backend:vif");