blob: 27385639b6e5609cf7b30625adaa0e4c31733cd8 [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>
Andy Whitcroftae5e8122013-11-25 16:52:34 +000042#include <net/ip6_checksum.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000043
Stefano Stabellinica981632012-08-08 17:21:23 +000044#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000045#include <xen/events.h>
46#include <xen/interface/memory.h>
47
48#include <asm/xen/hypercall.h>
49#include <asm/xen/page.h>
50
Wei Liue1f00a692013-05-22 06:34:45 +000051/* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
53 * enabled by default.
54 */
55bool separate_tx_rx_irq = 1;
56module_param(separate_tx_rx_irq, bool, 0644);
57
Wei Liu2810e5b2013-04-22 02:20:42 +000058/*
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
61 */
Wei Liu37641492013-05-02 00:43:59 +000062#define FATAL_SKB_SLOTS_DEFAULT 20
63static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64module_param(fatal_skb_slots, uint, 0444);
65
66/*
67 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
68 * the maximum slots a valid packet can use. Now this value is defined
69 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
70 * all backend.
71 */
72#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
Wei Liu2810e5b2013-04-22 02:20:42 +000073
Wei Liu2810e5b2013-04-22 02:20:42 +000074/*
75 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
76 * one or more merged tx requests, otherwise it is the continuation of
77 * previous tx request.
78 */
Wei Liub3f980b2013-08-26 12:59:38 +010079static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
Wei Liu2810e5b2013-04-22 02:20:42 +000080{
Wei Liub3f980b2013-08-26 12:59:38 +010081 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
Wei Liu2810e5b2013-04-22 02:20:42 +000082}
83
Wei Liu73764192013-08-26 12:59:39 +010084static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
85 u8 status);
86
Ian Campbellf942dc22011-03-15 00:06:18 +000087static void make_tx_response(struct xenvif *vif,
88 struct xen_netif_tx_request *txp,
89 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010090
91static inline int tx_work_todo(struct xenvif *vif);
92static inline int rx_work_todo(struct xenvif *vif);
93
Ian Campbellf942dc22011-03-15 00:06:18 +000094static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
95 u16 id,
96 s8 st,
97 u16 offset,
98 u16 size,
99 u16 flags);
100
Wei Liub3f980b2013-08-26 12:59:38 +0100101static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000102 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000103{
Wei Liub3f980b2013-08-26 12:59:38 +0100104 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000105}
106
Wei Liub3f980b2013-08-26 12:59:38 +0100107static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000108 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000109{
Wei Liub3f980b2013-08-26 12:59:38 +0100110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000111}
112
Paul Durrant2eba61d2013-10-16 17:50:29 +0100113/* This is a miniumum size for the linear area to avoid lots of
114 * calls to __pskb_pull_tail() as we set up checksum offsets. The
115 * value 128 was chosen as it covers all IPv4 and most likely
116 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000117 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100118#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000119
Ian Campbellea066ad2011-10-05 00:28:46 +0000120static u16 frag_get_pending_idx(skb_frag_t *frag)
121{
122 return (u16)frag->page_offset;
123}
124
125static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
126{
127 frag->page_offset = pending_idx;
128}
129
Ian Campbellf942dc22011-03-15 00:06:18 +0000130static inline pending_ring_idx_t pending_index(unsigned i)
131{
132 return i & (MAX_PENDING_REQS-1);
133}
134
Wei Liub3f980b2013-08-26 12:59:38 +0100135static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000136{
137 return MAX_PENDING_REQS -
Wei Liub3f980b2013-08-26 12:59:38 +0100138 vif->pending_prod + vif->pending_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000139}
140
Paul Durrantca2f09f2013-12-06 16:36:07 +0000141bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000142{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000143 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000144
Paul Durrantca2f09f2013-12-06 16:36:07 +0000145 do {
146 prod = vif->rx.sring->req_prod;
147 cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000148
Paul Durrantca2f09f2013-12-06 16:36:07 +0000149 if (prod - cons >= needed)
150 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000151
Paul Durrantca2f09f2013-12-06 16:36:07 +0000152 vif->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000153
Paul Durrantca2f09f2013-12-06 16:36:07 +0000154 /* Make sure event is visible before we check prod
155 * again.
156 */
157 mb();
158 } while (vif->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000159
Paul Durrantca2f09f2013-12-06 16:36:07 +0000160 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000161}
162
163/*
164 * Returns true if we should start a new receive buffer instead of
165 * adding 'size' bytes to a buffer which currently contains 'offset'
166 * bytes.
167 */
168static bool start_new_rx_buffer(int offset, unsigned long size, int head)
169{
170 /* simple case: we have completely filled the current buffer. */
171 if (offset == MAX_BUFFER_OFFSET)
172 return true;
173
174 /*
175 * complex case: start a fresh buffer if the current frag
176 * would overflow the current buffer but only if:
177 * (i) this frag would fit completely in the next buffer
178 * and (ii) there is already some data in the current buffer
179 * and (iii) this is not the head buffer.
180 *
181 * Where:
182 * - (i) stops us splitting a frag into two copies
183 * unless the frag is too large for a single buffer.
184 * - (ii) stops us from leaving a buffer pointlessly empty.
185 * - (iii) stops us leaving the first buffer
186 * empty. Strictly speaking this is already covered
187 * by (ii) but is explicitly checked because
188 * netfront relies on the first buffer being
189 * non-empty and can crash otherwise.
190 *
191 * This means we will effectively linearise small
192 * frags but do not needlessly split large buffers
193 * into multiple copies tend to give large frags their
194 * own buffers as before.
195 */
196 if ((offset + size > MAX_BUFFER_OFFSET) &&
197 (size <= MAX_BUFFER_OFFSET) && offset && !head)
198 return true;
199
200 return false;
201}
202
Ian Campbellf942dc22011-03-15 00:06:18 +0000203struct netrx_pending_operations {
204 unsigned copy_prod, copy_cons;
205 unsigned meta_prod, meta_cons;
206 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100207 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000208 int copy_off;
209 grant_ref_t copy_gref;
210};
211
Wei Liub3f980b2013-08-26 12:59:38 +0100212static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
213 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000214{
Wei Liub3f980b2013-08-26 12:59:38 +0100215 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000216 struct xen_netif_rx_request *req;
217
218 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
219
220 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100221 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000222 meta->gso_size = 0;
223 meta->size = 0;
224 meta->id = req->id;
225
226 npo->copy_off = 0;
227 npo->copy_gref = req->gref;
228
229 return meta;
230}
231
Wei Liu33bc8012013-10-08 10:54:21 +0100232/*
233 * Set up the grant operations for this fragment. If it's a flipping
234 * interface, we also set up the unmap request from here.
235 */
Wei Liu73764192013-08-26 12:59:39 +0100236static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
237 struct netrx_pending_operations *npo,
238 struct page *page, unsigned long size,
Wei Liu33bc8012013-10-08 10:54:21 +0100239 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000240{
241 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100242 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000243 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100244 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000245
246 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000247 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000248
249 meta = npo->meta + npo->meta_prod - 1;
250
Ian Campbell6a8ed462012-10-10 03:48:42 +0000251 /* Skip unused frames from start of page */
252 page += offset >> PAGE_SHIFT;
253 offset &= ~PAGE_MASK;
254
Ian Campbellf942dc22011-03-15 00:06:18 +0000255 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000256 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000257 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
258
Ian Campbell6a8ed462012-10-10 03:48:42 +0000259 bytes = PAGE_SIZE - offset;
260
261 if (bytes > size)
262 bytes = size;
263
Wei Liu33bc8012013-10-08 10:54:21 +0100264 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000265 /*
266 * Netfront requires there to be some data in the head
267 * buffer.
268 */
Wei Liu33bc8012013-10-08 10:54:21 +0100269 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000270
271 meta = get_next_rx_buffer(vif, npo);
272 }
273
Ian Campbellf942dc22011-03-15 00:06:18 +0000274 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
275 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
276
277 copy_gop = npo->copy + npo->copy_prod++;
278 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100279 copy_gop->len = bytes;
280
Wei Liu43e9d192013-08-26 12:59:37 +0100281 copy_gop->source.domid = DOMID_SELF;
282 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000283 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000284
Wei Liub3f980b2013-08-26 12:59:38 +0100285 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000286 copy_gop->dest.offset = npo->copy_off;
287 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000288
289 npo->copy_off += bytes;
290 meta->size += bytes;
291
292 offset += bytes;
293 size -= bytes;
294
Ian Campbell6a8ed462012-10-10 03:48:42 +0000295 /* Next frame */
296 if (offset == PAGE_SIZE && size) {
297 BUG_ON(!PageCompound(page));
298 page++;
299 offset = 0;
300 }
301
Ian Campbellf942dc22011-03-15 00:06:18 +0000302 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100303 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 else
308 gso_type = XEN_NETIF_GSO_TYPE_NONE;
309
310 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000311 vif->rx.req_cons++;
312
Wei Liu33bc8012013-10-08 10:54:21 +0100313 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000314
315 }
316}
317
318/*
319 * Prepare an SKB to be transmitted to the frontend.
320 *
321 * This function is responsible for allocating grant operations, meta
322 * structures, etc.
323 *
324 * It returns the number of meta structures consumed. The number of
325 * ring slots used is always equal to the number of meta slots used
326 * plus the number of GSO descriptors used. Currently, we use either
327 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
328 * frontend-side LRO).
329 */
Wei Liu73764192013-08-26 12:59:39 +0100330static int xenvif_gop_skb(struct sk_buff *skb,
331 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000332{
333 struct xenvif *vif = netdev_priv(skb->dev);
334 int nr_frags = skb_shinfo(skb)->nr_frags;
335 int i;
336 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100337 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000338 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100339 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000340 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100341 int gso_type;
342 int gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000343
344 old_meta_prod = npo->meta_prod;
345
Paul Durrant82cada22013-10-16 17:50:32 +0100346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 gso_size = skb_shinfo(skb)->gso_size;
349 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
350 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
351 gso_size = skb_shinfo(skb)->gso_size;
352 } else {
353 gso_type = XEN_NETIF_GSO_TYPE_NONE;
354 gso_size = 0;
355 }
356
Ian Campbellf942dc22011-03-15 00:06:18 +0000357 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000358 if ((1 << gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
360 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100361 meta->gso_type = gso_type;
362 meta->gso_size = gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000363 meta->size = 0;
364 meta->id = req->id;
365 }
366
367 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
368 meta = npo->meta + npo->meta_prod++;
369
Paul Durrant82cada22013-10-16 17:50:32 +0100370 if ((1 << gso_type) & vif->gso_mask) {
371 meta->gso_type = gso_type;
372 meta->gso_size = gso_size;
373 } else {
374 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100376 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000377
378 meta->size = 0;
379 meta->id = req->id;
380 npo->copy_off = 0;
381 npo->copy_gref = req->gref;
382
383 data = skb->data;
384 while (data < skb_tail_pointer(skb)) {
385 unsigned int offset = offset_in_page(data);
386 unsigned int len = PAGE_SIZE - offset;
387
388 if (data + len > skb_tail_pointer(skb))
389 len = skb_tail_pointer(skb) - data;
390
Wei Liu73764192013-08-26 12:59:39 +0100391 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100392 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000393 data += len;
394 }
395
396 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100397 xenvif_gop_frag_copy(vif, skb, npo,
398 skb_frag_page(&skb_shinfo(skb)->frags[i]),
399 skb_frag_size(&skb_shinfo(skb)->frags[i]),
400 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100401 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000402 }
403
404 return npo->meta_prod - old_meta_prod;
405}
406
407/*
Wei Liu73764192013-08-26 12:59:39 +0100408 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000409 * used to set up the operations on the top of
410 * netrx_pending_operations, which have since been done. Check that
411 * they didn't give any errors and advance over them.
412 */
Wei Liu73764192013-08-26 12:59:39 +0100413static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
414 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000415{
416 struct gnttab_copy *copy_op;
417 int status = XEN_NETIF_RSP_OKAY;
418 int i;
419
420 for (i = 0; i < nr_meta_slots; i++) {
421 copy_op = npo->copy + npo->copy_cons++;
422 if (copy_op->status != GNTST_okay) {
423 netdev_dbg(vif->dev,
424 "Bad status %d from copy to DOM%d.\n",
425 copy_op->status, vif->domid);
426 status = XEN_NETIF_RSP_ERROR;
427 }
428 }
429
430 return status;
431}
432
Wei Liu73764192013-08-26 12:59:39 +0100433static void xenvif_add_frag_responses(struct xenvif *vif, int status,
434 struct xenvif_rx_meta *meta,
435 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000436{
437 int i;
438 unsigned long offset;
439
440 /* No fragments used */
441 if (nr_meta_slots <= 1)
442 return;
443
444 nr_meta_slots--;
445
446 for (i = 0; i < nr_meta_slots; i++) {
447 int flags;
448 if (i == nr_meta_slots - 1)
449 flags = 0;
450 else
451 flags = XEN_NETRXF_more_data;
452
453 offset = 0;
454 make_rx_response(vif, meta[i].id, status, offset,
455 meta[i].size, flags);
456 }
457}
458
Wei Liu33bc8012013-10-08 10:54:21 +0100459struct skb_cb_overlay {
460 int meta_slots_used;
461};
462
Paul Durrantca2f09f2013-12-06 16:36:07 +0000463void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000464{
Wei Liub3f980b2013-08-26 12:59:38 +0100465 wake_up(&vif->wq);
466}
467
Paul Durrantca2f09f2013-12-06 16:36:07 +0000468static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100469{
Ian Campbellf942dc22011-03-15 00:06:18 +0000470 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000471 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000472 struct xen_netif_rx_response *resp;
473 struct sk_buff_head rxq;
474 struct sk_buff *skb;
475 LIST_HEAD(notify);
476 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000477 unsigned long offset;
478 struct skb_cb_overlay *sco;
Paul Durrant11b57f92014-01-08 12:41:58 +0000479 bool need_to_notify = false;
480 bool ring_full = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000481
482 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100483 .copy = vif->grant_copy_op,
484 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000485 };
486
487 skb_queue_head_init(&rxq);
488
Wei Liub3f980b2013-08-26 12:59:38 +0100489 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Paul Durrantca2f09f2013-12-06 16:36:07 +0000490 int max_slots_needed;
491 int i;
492
493 /* We need a cheap worse case estimate for the number of
494 * slots we'll use.
495 */
496
497 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
498 skb_headlen(skb),
499 PAGE_SIZE);
500 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
501 unsigned int size;
502 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
503 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
504 }
505 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
506 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
507 max_slots_needed++;
508
509 /* If the skb may not fit then bail out now */
510 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
511 skb_queue_head(&vif->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000512 need_to_notify = true;
513 ring_full = true;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000514 break;
515 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000516
517 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100518 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000519 BUG_ON(sco->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000520
521 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000522 }
523
Wei Liub3f980b2013-08-26 12:59:38 +0100524 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000525
Paul Durrant11b57f92014-01-08 12:41:58 +0000526 vif->rx_queue_stopped = !npo.copy_prod && ring_full;
527
Ian Campbellf942dc22011-03-15 00:06:18 +0000528 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000529 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000530
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000531 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liub3f980b2013-08-26 12:59:38 +0100532 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000533
534 while ((skb = __skb_dequeue(&rxq)) != NULL) {
535 sco = (struct skb_cb_overlay *)skb->cb;
536
Paul Durrant82cada22013-10-16 17:50:32 +0100537 if ((1 << vif->meta[npo.meta_cons].gso_type) &
538 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000539 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100540 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000541
542 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
543
Wei Liub3f980b2013-08-26 12:59:38 +0100544 resp->offset = vif->meta[npo.meta_cons].gso_size;
545 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000546 resp->status = sco->meta_slots_used;
547
548 npo.meta_cons++;
549 sco->meta_slots_used--;
550 }
551
552
553 vif->dev->stats.tx_bytes += skb->len;
554 vif->dev->stats.tx_packets++;
555
Wei Liu73764192013-08-26 12:59:39 +0100556 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000557
558 if (sco->meta_slots_used == 1)
559 flags = 0;
560 else
561 flags = XEN_NETRXF_more_data;
562
563 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
564 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
565 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
566 /* remote but checksummed. */
567 flags |= XEN_NETRXF_data_validated;
568
569 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100570 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100572 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000573 flags);
574
Paul Durrant82cada22013-10-16 17:50:32 +0100575 if ((1 << vif->meta[npo.meta_cons].gso_type) &
576 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000577 struct xen_netif_extra_info *gso =
578 (struct xen_netif_extra_info *)
579 RING_GET_RESPONSE(&vif->rx,
580 vif->rx.rsp_prod_pvt++);
581
582 resp->flags |= XEN_NETRXF_extra_info;
583
Paul Durrant82cada22013-10-16 17:50:32 +0100584 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100585 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000586 gso->u.gso.pad = 0;
587 gso->u.gso.features = 0;
588
589 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
590 gso->flags = 0;
591 }
592
Wei Liu73764192013-08-26 12:59:39 +0100593 xenvif_add_frag_responses(vif, status,
594 vif->meta + npo.meta_cons + 1,
595 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000596
597 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000598
Paul Durrant11b57f92014-01-08 12:41:58 +0000599 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100600
Ian Campbellf942dc22011-03-15 00:06:18 +0000601 npo.meta_cons += sco->meta_slots_used;
602 dev_kfree_skb(skb);
603 }
604
Paul Durrantca2f09f2013-12-06 16:36:07 +0000605done:
Wei Liub3f980b2013-08-26 12:59:38 +0100606 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000607 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000608}
609
Wei Liu73764192013-08-26 12:59:39 +0100610void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000611{
612 int more_to_do;
613
614 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
615
616 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100617 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000618}
619
620static void tx_add_credit(struct xenvif *vif)
621{
622 unsigned long max_burst, max_credit;
623
624 /*
625 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
626 * Otherwise the interface can seize up due to insufficient credit.
627 */
628 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
629 max_burst = min(max_burst, 131072UL);
630 max_burst = max(max_burst, vif->credit_bytes);
631
632 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
633 max_credit = vif->remaining_credit + vif->credit_bytes;
634 if (max_credit < vif->remaining_credit)
635 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
636
637 vif->remaining_credit = min(max_credit, max_burst);
638}
639
640static void tx_credit_callback(unsigned long data)
641{
642 struct xenvif *vif = (struct xenvif *)data;
643 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100644 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000645}
646
Wei Liu73764192013-08-26 12:59:39 +0100647static void xenvif_tx_err(struct xenvif *vif,
648 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000649{
650 RING_IDX cons = vif->tx.req_cons;
651
652 do {
653 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000654 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000655 break;
656 txp = RING_GET_REQUEST(&vif->tx, cons++);
657 } while (1);
658 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000659}
660
Wei Liu73764192013-08-26 12:59:39 +0100661static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000662{
663 netdev_err(vif->dev, "fatal error; disabling device\n");
664 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000665}
666
Wei Liu73764192013-08-26 12:59:39 +0100667static int xenvif_count_requests(struct xenvif *vif,
668 struct xen_netif_tx_request *first,
669 struct xen_netif_tx_request *txp,
670 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000671{
672 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000673 int slots = 0;
674 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000675 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000676
677 if (!(first->flags & XEN_NETTXF_more_data))
678 return 0;
679
680 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000681 struct xen_netif_tx_request dropped_tx = { 0 };
682
Wei Liu2810e5b2013-04-22 02:20:42 +0000683 if (slots >= work_to_do) {
684 netdev_err(vif->dev,
685 "Asked for %d slots but exceeds this limit\n",
686 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100687 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000688 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000689 }
690
Wei Liu2810e5b2013-04-22 02:20:42 +0000691 /* This guest is really using too many slots and
692 * considered malicious.
693 */
Wei Liu37641492013-05-02 00:43:59 +0000694 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000695 netdev_err(vif->dev,
696 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000697 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100698 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000699 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000700 }
701
Wei Liu2810e5b2013-04-22 02:20:42 +0000702 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000703 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
704 * the historical MAX_SKB_FRAGS value 18 to honor the
705 * same behavior as before. Any packet using more than
706 * 18 slots but less than fatal_skb_slots slots is
707 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000708 */
Wei Liu37641492013-05-02 00:43:59 +0000709 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000710 if (net_ratelimit())
711 netdev_dbg(vif->dev,
712 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000713 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000714 drop_err = -E2BIG;
715 }
716
Wei Liu59ccb4e2013-05-02 00:43:58 +0000717 if (drop_err)
718 txp = &dropped_tx;
719
Wei Liu2810e5b2013-04-22 02:20:42 +0000720 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000721 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000722
723 /* If the guest submitted a frame >= 64 KiB then
724 * first->size overflowed and following slots will
725 * appear to be larger than the frame.
726 *
727 * This cannot be fatal error as there are buggy
728 * frontends that do this.
729 *
730 * Consume all slots and drop the packet.
731 */
732 if (!drop_err && txp->size > first->size) {
733 if (net_ratelimit())
734 netdev_dbg(vif->dev,
735 "Invalid tx request, slot size %u > remaining size %u\n",
736 txp->size, first->size);
737 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000738 }
739
740 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000741 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000742
743 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000744 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000745 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100746 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000747 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000748 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000749
750 more_data = txp->flags & XEN_NETTXF_more_data;
751
752 if (!drop_err)
753 txp++;
754
755 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000756
757 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100758 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000759 return drop_err;
760 }
761
762 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000763}
764
Wei Liu73764192013-08-26 12:59:39 +0100765static struct page *xenvif_alloc_page(struct xenvif *vif,
766 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000767{
768 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100769
770 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000771 if (!page)
772 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100773 vif->mmap_pages[pending_idx] = page;
774
Ian Campbellf942dc22011-03-15 00:06:18 +0000775 return page;
776}
777
Wei Liu73764192013-08-26 12:59:39 +0100778static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
779 struct sk_buff *skb,
780 struct xen_netif_tx_request *txp,
781 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000782{
783 struct skb_shared_info *shinfo = skb_shinfo(skb);
784 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000785 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000786 u16 head_idx = 0;
787 int slot, start;
788 struct page *page;
789 pending_ring_idx_t index, start_idx = 0;
790 uint16_t dst_offset;
791 unsigned int nr_slots;
792 struct pending_tx_info *first = NULL;
793
794 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000795 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000796 */
797 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000798
799 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000800 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000801
Wei Liu2810e5b2013-04-22 02:20:42 +0000802 /* Coalesce tx requests, at this point the packet passed in
803 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100804 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000805 */
806 for (shinfo->nr_frags = slot = start; slot < nr_slots;
807 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000808 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100809 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000810
Wei Liub3f980b2013-08-26 12:59:38 +0100811 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000812 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000813 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000814
Wei Liu2810e5b2013-04-22 02:20:42 +0000815 dst_offset = 0;
816 first = NULL;
817 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
818 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000819
Wei Liu2810e5b2013-04-22 02:20:42 +0000820 gop->source.u.ref = txp->gref;
821 gop->source.domid = vif->domid;
822 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000823
Wei Liu2810e5b2013-04-22 02:20:42 +0000824 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000825
Wei Liu2810e5b2013-04-22 02:20:42 +0000826 gop->dest.offset = dst_offset;
827 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000828
Wei Liu2810e5b2013-04-22 02:20:42 +0000829 if (dst_offset + txp->size > PAGE_SIZE) {
830 /* This page can only merge a portion
831 * of tx request. Do not increment any
832 * pointer / counter here. The txp
833 * will be dealt with in future
834 * rounds, eventually hitting the
835 * `else` branch.
836 */
837 gop->len = PAGE_SIZE - dst_offset;
838 txp->offset += gop->len;
839 txp->size -= gop->len;
840 dst_offset += gop->len; /* quit loop */
841 } else {
842 /* This tx request can be merged in the page */
843 gop->len = txp->size;
844 dst_offset += gop->len;
845
Wei Liub3f980b2013-08-26 12:59:38 +0100846 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000847
Wei Liub3f980b2013-08-26 12:59:38 +0100848 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000849
850 memcpy(&pending_tx_info[pending_idx].req, txp,
851 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000852
853 /* Poison these fields, corresponding
854 * fields for head tx req will be set
855 * to correct values after the loop.
856 */
Wei Liub3f980b2013-08-26 12:59:38 +0100857 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000858 pending_tx_info[pending_idx].head =
859 INVALID_PENDING_RING_IDX;
860
861 if (!first) {
862 first = &pending_tx_info[pending_idx];
863 start_idx = index;
864 head_idx = pending_idx;
865 }
866
867 txp++;
868 slot++;
869 }
870
871 gop++;
872 }
873
874 first->req.offset = 0;
875 first->req.size = dst_offset;
876 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100877 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000878 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000879 }
880
Wei Liu2810e5b2013-04-22 02:20:42 +0000881 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
882
Ian Campbellf942dc22011-03-15 00:06:18 +0000883 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000884err:
885 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000886 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100887 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000888 frag_get_pending_idx(&frags[shinfo->nr_frags]),
889 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000890 }
891 /* The head too, if necessary. */
892 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100893 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000894
895 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000896}
897
Wei Liu73764192013-08-26 12:59:39 +0100898static int xenvif_tx_check_gop(struct xenvif *vif,
899 struct sk_buff *skb,
900 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000901{
902 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000903 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000904 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000905 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000906 int nr_frags = shinfo->nr_frags;
907 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000908 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000909
910 /* Check status of header. */
911 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000912 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100913 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000914
915 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000916 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000917
918 for (i = start; i < nr_frags; i++) {
919 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000920 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000921
Ian Campbellea066ad2011-10-05 00:28:46 +0000922 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100923 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000924 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000925
926 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000927 do {
928 newerr = (++gop)->status;
929 if (newerr)
930 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100931 peek = vif->pending_ring[pending_index(++head)];
932 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000933
Ian Campbellf942dc22011-03-15 00:06:18 +0000934 if (likely(!newerr)) {
935 /* Had a previous error? Invalidate this fragment. */
936 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100937 xenvif_idx_release(vif, pending_idx,
938 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000939 continue;
940 }
941
942 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100943 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000944
945 /* Not the first error? Preceding frags already invalidated. */
946 if (err)
947 continue;
948
949 /* First error: invalidate header and preceding fragments. */
950 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +0100951 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000952 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000953 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +0100954 xenvif_idx_release(vif, pending_idx,
955 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000956 }
957
958 /* Remember the error: invalidate all subsequent fragments. */
959 err = newerr;
960 }
961
962 *gopp = gop + 1;
963 return err;
964}
965
Wei Liu73764192013-08-26 12:59:39 +0100966static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000967{
968 struct skb_shared_info *shinfo = skb_shinfo(skb);
969 int nr_frags = shinfo->nr_frags;
970 int i;
971
972 for (i = 0; i < nr_frags; i++) {
973 skb_frag_t *frag = shinfo->frags + i;
974 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000975 struct page *page;
976 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000977
Ian Campbellea066ad2011-10-05 00:28:46 +0000978 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +0000979
Wei Liub3f980b2013-08-26 12:59:38 +0100980 txp = &vif->pending_tx_info[pending_idx].req;
981 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +0000982 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +0000983 skb->len += txp->size;
984 skb->data_len += txp->size;
985 skb->truesize += txp->size;
986
Wei Liu73764192013-08-26 12:59:39 +0100987 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +0100988 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +0100989 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000990 }
991}
992
Wei Liu73764192013-08-26 12:59:39 +0100993static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +0000994 struct xen_netif_extra_info *extras,
995 int work_to_do)
996{
997 struct xen_netif_extra_info extra;
998 RING_IDX cons = vif->tx.req_cons;
999
1000 do {
1001 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001002 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001003 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001004 return -EBADR;
1005 }
1006
1007 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1008 sizeof(extra));
1009 if (unlikely(!extra.type ||
1010 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1011 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001012 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001013 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001014 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001015 return -EINVAL;
1016 }
1017
1018 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1019 vif->tx.req_cons = ++cons;
1020 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1021
1022 return work_to_do;
1023}
1024
Wei Liu73764192013-08-26 12:59:39 +01001025static int xenvif_set_skb_gso(struct xenvif *vif,
1026 struct sk_buff *skb,
1027 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001028{
1029 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001030 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001031 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001032 return -EINVAL;
1033 }
1034
Paul Durranta9468582013-10-16 17:50:31 +01001035 switch (gso->u.gso.type) {
1036 case XEN_NETIF_GSO_TYPE_TCPV4:
1037 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1038 break;
1039 case XEN_NETIF_GSO_TYPE_TCPV6:
1040 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1041 break;
1042 default:
Ian Campbell488562862013-02-06 23:41:35 +00001043 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001044 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001045 return -EINVAL;
1046 }
1047
1048 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001049 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001050
1051 return 0;
1052}
1053
Paul Durrant1431fb32013-12-03 17:39:29 +00001054static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
1055 unsigned int max)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001056{
Paul Durrant1431fb32013-12-03 17:39:29 +00001057 if (skb_headlen(skb) >= len)
1058 return 0;
1059
1060 /* If we need to pullup then pullup to the max, so we
1061 * won't need to do it again.
1062 */
1063 if (max > skb->len)
1064 max = skb->len;
1065
1066 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
1067 return -ENOMEM;
1068
1069 if (skb_headlen(skb) < len)
1070 return -EPROTO;
1071
1072 return 0;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001073}
1074
Paul Durrant1431fb32013-12-03 17:39:29 +00001075/* This value should be large enough to cover a tagged ethernet header plus
1076 * maximally sized IP and TCP or UDP headers.
1077 */
1078#define MAX_IP_HDR_LEN 128
1079
Paul Durrant2eba61d2013-10-16 17:50:29 +01001080static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1081 int recalculate_partial_csum)
1082{
Paul Durrant2eba61d2013-10-16 17:50:29 +01001083 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001084 bool fragment;
1085 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001086
Paul Durrant1431fb32013-12-03 17:39:29 +00001087 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001088
Paul Durrant1431fb32013-12-03 17:39:29 +00001089 err = maybe_pull_tail(skb,
1090 sizeof(struct iphdr),
1091 MAX_IP_HDR_LEN);
1092 if (err < 0)
1093 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001094
Paul Durrant1431fb32013-12-03 17:39:29 +00001095 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
1096 fragment = true;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001097
Paul Durrant1431fb32013-12-03 17:39:29 +00001098 off = ip_hdrlen(skb);
1099
1100 err = -EPROTO;
1101
Wei Yongjun7022ef82013-12-16 10:45:05 +08001102 if (fragment)
1103 goto out;
1104
Paul Durrant1431fb32013-12-03 17:39:29 +00001105 switch (ip_hdr(skb)->protocol) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001106 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001107 err = maybe_pull_tail(skb,
1108 off + sizeof(struct tcphdr),
1109 MAX_IP_HDR_LEN);
1110 if (err < 0)
1111 goto out;
1112
Paul Durrant2eba61d2013-10-16 17:50:29 +01001113 if (!skb_partial_csum_set(skb, off,
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001114 offsetof(struct tcphdr, check))) {
1115 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001116 goto out;
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001117 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001118
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001119 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001120 tcp_hdr(skb)->check =
1121 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1122 ip_hdr(skb)->daddr,
1123 skb->len - off,
1124 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001125 break;
1126 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001127 err = maybe_pull_tail(skb,
1128 off + sizeof(struct udphdr),
1129 MAX_IP_HDR_LEN);
1130 if (err < 0)
1131 goto out;
1132
Paul Durrant2eba61d2013-10-16 17:50:29 +01001133 if (!skb_partial_csum_set(skb, off,
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001134 offsetof(struct udphdr, check))) {
1135 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001136 goto out;
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001137 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001138
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001139 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001140 udp_hdr(skb)->check =
1141 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1142 ip_hdr(skb)->daddr,
1143 skb->len - off,
1144 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001145 break;
1146 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001147 goto out;
1148 }
1149
1150 err = 0;
1151
1152out:
1153 return err;
1154}
1155
Paul Durrant1431fb32013-12-03 17:39:29 +00001156/* This value should be large enough to cover a tagged ethernet header plus
1157 * an IPv6 header, all options, and a maximal TCP or UDP header.
1158 */
1159#define MAX_IPV6_HDR_LEN 256
1160
1161#define OPT_HDR(type, skb, off) \
1162 (type *)(skb_network_header(skb) + (off))
1163
Paul Durrant2eba61d2013-10-16 17:50:29 +01001164static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1165 int recalculate_partial_csum)
1166{
Paul Durrant1431fb32013-12-03 17:39:29 +00001167 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001168 u8 nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001169 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001170 unsigned int len;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001171 bool fragment;
1172 bool done;
1173
Paul Durrant1431fb32013-12-03 17:39:29 +00001174 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001175 done = false;
1176
1177 off = sizeof(struct ipv6hdr);
1178
Paul Durrant1431fb32013-12-03 17:39:29 +00001179 err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
1180 if (err < 0)
1181 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001182
Paul Durrant1431fb32013-12-03 17:39:29 +00001183 nexthdr = ipv6_hdr(skb)->nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001184
Paul Durrant1431fb32013-12-03 17:39:29 +00001185 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
1186 while (off <= len && !done) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001187 switch (nexthdr) {
1188 case IPPROTO_DSTOPTS:
1189 case IPPROTO_HOPOPTS:
1190 case IPPROTO_ROUTING: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001191 struct ipv6_opt_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001192
Paul Durrant1431fb32013-12-03 17:39:29 +00001193 err = maybe_pull_tail(skb,
1194 off +
1195 sizeof(struct ipv6_opt_hdr),
1196 MAX_IPV6_HDR_LEN);
1197 if (err < 0)
1198 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001199
Paul Durrant1431fb32013-12-03 17:39:29 +00001200 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001201 nexthdr = hp->nexthdr;
1202 off += ipv6_optlen(hp);
1203 break;
1204 }
1205 case IPPROTO_AH: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001206 struct ip_auth_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001207
Paul Durrant1431fb32013-12-03 17:39:29 +00001208 err = maybe_pull_tail(skb,
1209 off +
1210 sizeof(struct ip_auth_hdr),
1211 MAX_IPV6_HDR_LEN);
1212 if (err < 0)
1213 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001214
Paul Durrant1431fb32013-12-03 17:39:29 +00001215 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001216 nexthdr = hp->nexthdr;
Paul Durrant1431fb32013-12-03 17:39:29 +00001217 off += ipv6_authlen(hp);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001218 break;
1219 }
Paul Durrant1431fb32013-12-03 17:39:29 +00001220 case IPPROTO_FRAGMENT: {
1221 struct frag_hdr *hp;
1222
1223 err = maybe_pull_tail(skb,
1224 off +
1225 sizeof(struct frag_hdr),
1226 MAX_IPV6_HDR_LEN);
1227 if (err < 0)
1228 goto out;
1229
1230 hp = OPT_HDR(struct frag_hdr, skb, off);
1231
1232 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
1233 fragment = true;
1234
1235 nexthdr = hp->nexthdr;
1236 off += sizeof(struct frag_hdr);
1237 break;
1238 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001239 default:
1240 done = true;
1241 break;
1242 }
1243 }
1244
Paul Durrant1431fb32013-12-03 17:39:29 +00001245 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001246
Paul Durrant1431fb32013-12-03 17:39:29 +00001247 if (!done || fragment)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001248 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001249
1250 switch (nexthdr) {
1251 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001252 err = maybe_pull_tail(skb,
1253 off + sizeof(struct tcphdr),
1254 MAX_IPV6_HDR_LEN);
1255 if (err < 0)
1256 goto out;
1257
Paul Durrant2eba61d2013-10-16 17:50:29 +01001258 if (!skb_partial_csum_set(skb, off,
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001259 offsetof(struct tcphdr, check))) {
1260 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001261 goto out;
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001262 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001263
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001264 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001265 tcp_hdr(skb)->check =
1266 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1267 &ipv6_hdr(skb)->daddr,
1268 skb->len - off,
1269 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001270 break;
1271 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001272 err = maybe_pull_tail(skb,
1273 off + sizeof(struct udphdr),
1274 MAX_IPV6_HDR_LEN);
1275 if (err < 0)
1276 goto out;
1277
Paul Durrant2eba61d2013-10-16 17:50:29 +01001278 if (!skb_partial_csum_set(skb, off,
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001279 offsetof(struct udphdr, check))) {
1280 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001281 goto out;
Wei Yongjun0c8d0872013-12-17 10:42:09 +08001282 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001283
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001284 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001285 udp_hdr(skb)->check =
1286 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1287 &ipv6_hdr(skb)->daddr,
1288 skb->len - off,
1289 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001290 break;
1291 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001292 goto out;
1293 }
1294
1295 err = 0;
1296
1297out:
1298 return err;
1299}
1300
Ian Campbellf942dc22011-03-15 00:06:18 +00001301static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1302{
Ian Campbellf942dc22011-03-15 00:06:18 +00001303 int err = -EPROTO;
1304 int recalculate_partial_csum = 0;
1305
Paul Durrant2eba61d2013-10-16 17:50:29 +01001306 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001307 * peers can fail to set NETRXF_csum_blank when sending a GSO
1308 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1309 * recalculate the partial checksum.
1310 */
1311 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1312 vif->rx_gso_checksum_fixup++;
1313 skb->ip_summed = CHECKSUM_PARTIAL;
1314 recalculate_partial_csum = 1;
1315 }
1316
1317 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1318 if (skb->ip_summed != CHECKSUM_PARTIAL)
1319 return 0;
1320
Paul Durrant2eba61d2013-10-16 17:50:29 +01001321 if (skb->protocol == htons(ETH_P_IP))
1322 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1323 else if (skb->protocol == htons(ETH_P_IPV6))
1324 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001325
Ian Campbellf942dc22011-03-15 00:06:18 +00001326 return err;
1327}
1328
1329static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1330{
Wei Liu059dfa62013-10-28 12:07:57 +00001331 u64 now = get_jiffies_64();
1332 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001333 msecs_to_jiffies(vif->credit_usec / 1000);
1334
1335 /* Timer could already be pending in rare cases. */
1336 if (timer_pending(&vif->credit_timeout))
1337 return true;
1338
1339 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001340 if (time_after_eq64(now, next_credit)) {
1341 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001342 tx_add_credit(vif);
1343 }
1344
1345 /* Still too big to send right now? Set a callback. */
1346 if (size > vif->remaining_credit) {
1347 vif->credit_timeout.data =
1348 (unsigned long)vif;
1349 vif->credit_timeout.function =
1350 tx_credit_callback;
1351 mod_timer(&vif->credit_timeout,
1352 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001353 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001354
1355 return true;
1356 }
1357
1358 return false;
1359}
1360
Paul Durrant10574052013-12-11 10:57:15 +00001361static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001362{
Wei Liub3f980b2013-08-26 12:59:38 +01001363 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001364 struct sk_buff *skb;
1365 int ret;
1366
Wei Liub3f980b2013-08-26 12:59:38 +01001367 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
Paul Durrant10574052013-12-11 10:57:15 +00001368 < MAX_PENDING_REQS) &&
1369 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001370 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001371 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001372 struct page *page;
1373 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1374 u16 pending_idx;
1375 RING_IDX idx;
1376 int work_to_do;
1377 unsigned int data_len;
1378 pending_ring_idx_t index;
1379
Ian Campbell488562862013-02-06 23:41:35 +00001380 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1381 XEN_NETIF_TX_RING_SIZE) {
1382 netdev_err(vif->dev,
1383 "Impossible number of requests. "
1384 "req_prod %d, req_cons %d, size %ld\n",
1385 vif->tx.sring->req_prod, vif->tx.req_cons,
1386 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001387 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001388 continue;
1389 }
1390
Paul Durrantd9601a32013-12-11 10:57:16 +00001391 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001392 if (!work_to_do)
1393 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001394
1395 idx = vif->tx.req_cons;
1396 rmb(); /* Ensure that we see the request before we copy it. */
1397 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1398
1399 /* Credit-based scheduling. */
1400 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001401 tx_credit_exceeded(vif, txreq.size))
1402 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001403
1404 vif->remaining_credit -= txreq.size;
1405
1406 work_to_do--;
1407 vif->tx.req_cons = ++idx;
1408
1409 memset(extras, 0, sizeof(extras));
1410 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001411 work_to_do = xenvif_get_extras(vif, extras,
1412 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001413 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001414 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001415 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001416 }
1417
Wei Liu73764192013-08-26 12:59:39 +01001418 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001419 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001420 break;
Ian Campbell488562862013-02-06 23:41:35 +00001421
Ian Campbellf942dc22011-03-15 00:06:18 +00001422 idx += ret;
1423
1424 if (unlikely(txreq.size < ETH_HLEN)) {
1425 netdev_dbg(vif->dev,
1426 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001427 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001428 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001429 }
1430
1431 /* No crossing a page as the payload mustn't fragment. */
1432 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001433 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001434 "txreq.offset: %x, size: %u, end: %lu\n",
1435 txreq.offset, txreq.size,
1436 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001437 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001438 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001439 }
1440
Wei Liub3f980b2013-08-26 12:59:38 +01001441 index = pending_index(vif->pending_cons);
1442 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001443
1444 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001445 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001446 PKT_PROT_LEN : txreq.size;
1447
1448 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1449 GFP_ATOMIC | __GFP_NOWARN);
1450 if (unlikely(skb == NULL)) {
1451 netdev_dbg(vif->dev,
1452 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001453 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001454 break;
1455 }
1456
1457 /* Packets passed to netif_rx() must have some headroom. */
1458 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1459
1460 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1461 struct xen_netif_extra_info *gso;
1462 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1463
Wei Liu73764192013-08-26 12:59:39 +01001464 if (xenvif_set_skb_gso(vif, skb, gso)) {
1465 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001466 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001467 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001468 }
1469 }
1470
1471 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001472 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001473 if (!page) {
1474 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001475 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001476 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001477 }
1478
Ian Campbellf942dc22011-03-15 00:06:18 +00001479 gop->source.u.ref = txreq.gref;
1480 gop->source.domid = vif->domid;
1481 gop->source.offset = txreq.offset;
1482
1483 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1484 gop->dest.domid = DOMID_SELF;
1485 gop->dest.offset = txreq.offset;
1486
1487 gop->len = txreq.size;
1488 gop->flags = GNTCOPY_source_gref;
1489
1490 gop++;
1491
Wei Liub3f980b2013-08-26 12:59:38 +01001492 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001493 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001494 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001495 *((u16 *)skb->data) = pending_idx;
1496
1497 __skb_put(skb, data_len);
1498
1499 skb_shinfo(skb)->nr_frags = ret;
1500 if (data_len < txreq.size) {
1501 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001502 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1503 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001504 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001505 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1506 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001507 }
1508
Wei Liub3f980b2013-08-26 12:59:38 +01001509 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001510
Wei Liu73764192013-08-26 12:59:39 +01001511 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001512 if (request_gop == NULL) {
1513 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001514 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001515 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001516 }
1517 gop = request_gop;
1518
Wei Liub3f980b2013-08-26 12:59:38 +01001519 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001520
Ian Campbellf942dc22011-03-15 00:06:18 +00001521 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001522
Wei Liub3f980b2013-08-26 12:59:38 +01001523 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001524 break;
1525 }
1526
Wei Liub3f980b2013-08-26 12:59:38 +01001527 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001528}
1529
Ian Campbellf942dc22011-03-15 00:06:18 +00001530
Paul Durrant10574052013-12-11 10:57:15 +00001531static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001532{
1533 struct gnttab_copy *gop = vif->tx_copy_ops;
1534 struct sk_buff *skb;
1535 int work_done = 0;
1536
Paul Durrant10574052013-12-11 10:57:15 +00001537 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001538 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001539 u16 pending_idx;
1540 unsigned data_len;
1541
1542 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001543 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001544
1545 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001546 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001547 netdev_dbg(vif->dev, "netback grant failed.\n");
1548 skb_shinfo(skb)->nr_frags = 0;
1549 kfree_skb(skb);
1550 continue;
1551 }
1552
1553 data_len = skb->len;
1554 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001555 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001556 data_len);
1557 if (data_len < txp->size) {
1558 /* Append the packet payload as a fragment. */
1559 txp->offset += data_len;
1560 txp->size -= data_len;
1561 } else {
1562 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001563 xenvif_idx_release(vif, pending_idx,
1564 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001565 }
1566
1567 if (txp->flags & XEN_NETTXF_csum_blank)
1568 skb->ip_summed = CHECKSUM_PARTIAL;
1569 else if (txp->flags & XEN_NETTXF_data_validated)
1570 skb->ip_summed = CHECKSUM_UNNECESSARY;
1571
Wei Liu73764192013-08-26 12:59:39 +01001572 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001573
Paul Durrant2eba61d2013-10-16 17:50:29 +01001574 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001575 int target = min_t(int, skb->len, PKT_PROT_LEN);
1576 __pskb_pull_tail(skb, target - skb_headlen(skb));
1577 }
1578
1579 skb->dev = vif->dev;
1580 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001581 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001582
1583 if (checksum_setup(vif, skb)) {
1584 netdev_dbg(vif->dev,
1585 "Can't setup checksum in net_tx_action\n");
1586 kfree_skb(skb);
1587 continue;
1588 }
1589
Jason Wang40893fd2013-03-26 23:11:22 +00001590 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001591
Paul Durrantb89587a2013-12-17 11:44:35 +00001592 /* If the packet is GSO then we will have just set up the
1593 * transport header offset in checksum_setup so it's now
1594 * straightforward to calculate gso_segs.
1595 */
1596 if (skb_is_gso(skb)) {
1597 int mss = skb_shinfo(skb)->gso_size;
1598 int hdrlen = skb_transport_header(skb) -
1599 skb_mac_header(skb) +
1600 tcp_hdrlen(skb);
1601
1602 skb_shinfo(skb)->gso_segs =
1603 DIV_ROUND_UP(skb->len - hdrlen, mss);
1604 }
1605
Ian Campbellf942dc22011-03-15 00:06:18 +00001606 vif->dev->stats.rx_bytes += skb->len;
1607 vif->dev->stats.rx_packets++;
1608
Wei Liub3f980b2013-08-26 12:59:38 +01001609 work_done++;
1610
1611 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001612 }
Wei Liub3f980b2013-08-26 12:59:38 +01001613
1614 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001615}
1616
1617/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001618int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001619{
1620 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001621 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001622
Wei Liub3f980b2013-08-26 12:59:38 +01001623 if (unlikely(!tx_work_todo(vif)))
1624 return 0;
1625
Paul Durrant10574052013-12-11 10:57:15 +00001626 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001627
1628 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001629 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001630
Wei Liub3f980b2013-08-26 12:59:38 +01001631 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001632
Paul Durrant10574052013-12-11 10:57:15 +00001633 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001634
1635 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001636}
1637
Wei Liu73764192013-08-26 12:59:39 +01001638static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1639 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001640{
Ian Campbellf942dc22011-03-15 00:06:18 +00001641 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001642 pending_ring_idx_t head;
1643 u16 peek; /* peek into next tx request */
1644
Wei Liub3f980b2013-08-26 12:59:38 +01001645 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001646
1647 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001648 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001649 return;
1650
Wei Liub3f980b2013-08-26 12:59:38 +01001651 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001652
Wei Liu2810e5b2013-04-22 02:20:42 +00001653 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001654
Wei Liub3f980b2013-08-26 12:59:38 +01001655 BUG_ON(!pending_tx_is_head(vif, head));
1656 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001657
Wei Liu2810e5b2013-04-22 02:20:42 +00001658 do {
1659 pending_ring_idx_t index;
1660 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001661 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001662
Wei Liub3f980b2013-08-26 12:59:38 +01001663 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001664 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001665
Wei Liu2810e5b2013-04-22 02:20:42 +00001666 /* Setting any number other than
1667 * INVALID_PENDING_RING_IDX indicates this slot is
1668 * starting a new packet / ending a previous packet.
1669 */
1670 pending_tx_info->head = 0;
1671
Wei Liub3f980b2013-08-26 12:59:38 +01001672 index = pending_index(vif->pending_prod++);
1673 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001674
Wei Liub3f980b2013-08-26 12:59:38 +01001675 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001676
Wei Liub3f980b2013-08-26 12:59:38 +01001677 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001678
Wei Liub3f980b2013-08-26 12:59:38 +01001679 put_page(vif->mmap_pages[pending_idx]);
1680 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001681}
1682
Wei Liu2810e5b2013-04-22 02:20:42 +00001683
Ian Campbellf942dc22011-03-15 00:06:18 +00001684static void make_tx_response(struct xenvif *vif,
1685 struct xen_netif_tx_request *txp,
1686 s8 st)
1687{
1688 RING_IDX i = vif->tx.rsp_prod_pvt;
1689 struct xen_netif_tx_response *resp;
1690 int notify;
1691
1692 resp = RING_GET_RESPONSE(&vif->tx, i);
1693 resp->id = txp->id;
1694 resp->status = st;
1695
1696 if (txp->flags & XEN_NETTXF_extra_info)
1697 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1698
1699 vif->tx.rsp_prod_pvt = ++i;
1700 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1701 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001702 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001703}
1704
1705static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1706 u16 id,
1707 s8 st,
1708 u16 offset,
1709 u16 size,
1710 u16 flags)
1711{
1712 RING_IDX i = vif->rx.rsp_prod_pvt;
1713 struct xen_netif_rx_response *resp;
1714
1715 resp = RING_GET_RESPONSE(&vif->rx, i);
1716 resp->offset = offset;
1717 resp->flags = flags;
1718 resp->id = id;
1719 resp->status = (s16)size;
1720 if (st < 0)
1721 resp->status = (s16)st;
1722
1723 vif->rx.rsp_prod_pvt = ++i;
1724
1725 return resp;
1726}
1727
Wei Liub3f980b2013-08-26 12:59:38 +01001728static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001729{
Paul Durrant11b57f92014-01-08 12:41:58 +00001730 return (!skb_queue_empty(&vif->rx_queue) && !vif->rx_queue_stopped) ||
1731 vif->rx_event;
Ian Campbellf942dc22011-03-15 00:06:18 +00001732}
1733
Wei Liub3f980b2013-08-26 12:59:38 +01001734static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001735{
1736
Wei Liub3f980b2013-08-26 12:59:38 +01001737 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1738 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1739 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001740 return 1;
1741
1742 return 0;
1743}
1744
Wei Liu73764192013-08-26 12:59:39 +01001745void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001746{
David Vrabelc9d63692011-09-29 16:53:31 +01001747 if (vif->tx.sring)
1748 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1749 vif->tx.sring);
1750 if (vif->rx.sring)
1751 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1752 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001753}
1754
Wei Liu73764192013-08-26 12:59:39 +01001755int xenvif_map_frontend_rings(struct xenvif *vif,
1756 grant_ref_t tx_ring_ref,
1757 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001758{
David Vrabelc9d63692011-09-29 16:53:31 +01001759 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001760 struct xen_netif_tx_sring *txs;
1761 struct xen_netif_rx_sring *rxs;
1762
1763 int err = -ENOMEM;
1764
David Vrabelc9d63692011-09-29 16:53:31 +01001765 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1766 tx_ring_ref, &addr);
1767 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001768 goto err;
1769
David Vrabelc9d63692011-09-29 16:53:31 +01001770 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001771 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1772
David Vrabelc9d63692011-09-29 16:53:31 +01001773 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1774 rx_ring_ref, &addr);
1775 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001776 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001777
David Vrabelc9d63692011-09-29 16:53:31 +01001778 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001779 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1780
1781 return 0;
1782
1783err:
Wei Liu73764192013-08-26 12:59:39 +01001784 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001785 return err;
1786}
1787
Paul Durrantca2f09f2013-12-06 16:36:07 +00001788void xenvif_stop_queue(struct xenvif *vif)
1789{
1790 if (!vif->can_queue)
1791 return;
1792
1793 netif_stop_queue(vif->dev);
1794}
1795
1796static void xenvif_start_queue(struct xenvif *vif)
1797{
1798 if (xenvif_schedulable(vif))
1799 netif_wake_queue(vif->dev);
1800}
1801
Wei Liu73764192013-08-26 12:59:39 +01001802int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001803{
1804 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001805 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001806
1807 while (!kthread_should_stop()) {
1808 wait_event_interruptible(vif->wq,
1809 rx_work_todo(vif) ||
1810 kthread_should_stop());
1811 if (kthread_should_stop())
1812 break;
1813
Paul Durrantca2f09f2013-12-06 16:36:07 +00001814 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001815 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001816
Paul Durrantca2f09f2013-12-06 16:36:07 +00001817 vif->rx_event = false;
1818
1819 if (skb_queue_empty(&vif->rx_queue) &&
1820 netif_queue_stopped(vif->dev))
1821 xenvif_start_queue(vif);
1822
Wei Liub3f980b2013-08-26 12:59:38 +01001823 cond_resched();
1824 }
1825
Paul Durrantca2f09f2013-12-06 16:36:07 +00001826 /* Bin any remaining skbs */
1827 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1828 dev_kfree_skb(skb);
1829
Wei Liub3f980b2013-08-26 12:59:38 +01001830 return 0;
1831}
1832
Ian Campbellf942dc22011-03-15 00:06:18 +00001833static int __init netback_init(void)
1834{
Ian Campbellf942dc22011-03-15 00:06:18 +00001835 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001836
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001837 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001838 return -ENODEV;
1839
Wei Liu37641492013-05-02 00:43:59 +00001840 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001841 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1842 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001843 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001844 }
1845
Ian Campbellf942dc22011-03-15 00:06:18 +00001846 rc = xenvif_xenbus_init();
1847 if (rc)
1848 goto failed_init;
1849
1850 return 0;
1851
1852failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001853 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001854}
1855
1856module_init(netback_init);
1857
Wei Liub103f352013-05-16 23:26:11 +00001858static void __exit netback_fini(void)
1859{
Wei Liub103f352013-05-16 23:26:11 +00001860 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001861}
1862module_exit(netback_fini);
1863
Ian Campbellf942dc22011-03-15 00:06:18 +00001864MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001865MODULE_ALIAS("xen-backend:vif");