blob: 33b8aa612d13632f91dcfe2d6548558eec8e55b8 [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
141static int max_required_rx_slots(struct xenvif *vif)
142{
143 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
144
Wei Liu2810e5b2013-04-22 02:20:42 +0000145 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
Paul Durrant82cada22013-10-16 17:50:32 +0100146 if (vif->can_sg || vif->gso_mask || vif->gso_prefix_mask)
Ian Campbellf942dc22011-03-15 00:06:18 +0000147 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
148
149 return max;
150}
151
Wei Liu73764192013-08-26 12:59:39 +0100152int xenvif_rx_ring_full(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000153{
154 RING_IDX peek = vif->rx_req_cons_peek;
155 RING_IDX needed = max_required_rx_slots(vif);
156
157 return ((vif->rx.sring->req_prod - peek) < needed) ||
158 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
159}
160
Wei Liu73764192013-08-26 12:59:39 +0100161int xenvif_must_stop_queue(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000162{
Wei Liu73764192013-08-26 12:59:39 +0100163 if (!xenvif_rx_ring_full(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +0000164 return 0;
165
166 vif->rx.sring->req_event = vif->rx_req_cons_peek +
167 max_required_rx_slots(vif);
168 mb(); /* request notification /then/ check the queue */
169
Wei Liu73764192013-08-26 12:59:39 +0100170 return xenvif_rx_ring_full(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000171}
172
173/*
174 * Returns true if we should start a new receive buffer instead of
175 * adding 'size' bytes to a buffer which currently contains 'offset'
176 * bytes.
177 */
178static bool start_new_rx_buffer(int offset, unsigned long size, int head)
179{
180 /* simple case: we have completely filled the current buffer. */
181 if (offset == MAX_BUFFER_OFFSET)
182 return true;
183
184 /*
185 * complex case: start a fresh buffer if the current frag
186 * would overflow the current buffer but only if:
187 * (i) this frag would fit completely in the next buffer
188 * and (ii) there is already some data in the current buffer
189 * and (iii) this is not the head buffer.
190 *
191 * Where:
192 * - (i) stops us splitting a frag into two copies
193 * unless the frag is too large for a single buffer.
194 * - (ii) stops us from leaving a buffer pointlessly empty.
195 * - (iii) stops us leaving the first buffer
196 * empty. Strictly speaking this is already covered
197 * by (ii) but is explicitly checked because
198 * netfront relies on the first buffer being
199 * non-empty and can crash otherwise.
200 *
201 * This means we will effectively linearise small
202 * frags but do not needlessly split large buffers
203 * into multiple copies tend to give large frags their
204 * own buffers as before.
205 */
206 if ((offset + size > MAX_BUFFER_OFFSET) &&
207 (size <= MAX_BUFFER_OFFSET) && offset && !head)
208 return true;
209
210 return false;
211}
212
Wei Liu33bc8012013-10-08 10:54:21 +0100213struct xenvif_count_slot_state {
214 unsigned long copy_off;
215 bool head;
216};
217
218unsigned int xenvif_count_frag_slots(struct xenvif *vif,
219 unsigned long offset, unsigned long size,
220 struct xenvif_count_slot_state *state)
221{
222 unsigned count = 0;
223
224 offset &= ~PAGE_MASK;
225
226 while (size > 0) {
227 unsigned long bytes;
228
229 bytes = PAGE_SIZE - offset;
230
231 if (bytes > size)
232 bytes = size;
233
234 if (start_new_rx_buffer(state->copy_off, bytes, state->head)) {
235 count++;
236 state->copy_off = 0;
237 }
238
239 if (state->copy_off + bytes > MAX_BUFFER_OFFSET)
240 bytes = MAX_BUFFER_OFFSET - state->copy_off;
241
242 state->copy_off += bytes;
243
244 offset += bytes;
245 size -= bytes;
246
247 if (offset == PAGE_SIZE)
248 offset = 0;
249
250 state->head = false;
251 }
252
253 return count;
254}
255
Ian Campbellf942dc22011-03-15 00:06:18 +0000256/*
257 * Figure out how many ring slots we're going to need to send @skb to
258 * the guest. This function is essentially a dry run of
Wei Liu73764192013-08-26 12:59:39 +0100259 * xenvif_gop_frag_copy.
Ian Campbellf942dc22011-03-15 00:06:18 +0000260 */
Wei Liu73764192013-08-26 12:59:39 +0100261unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000262{
Wei Liu33bc8012013-10-08 10:54:21 +0100263 struct xenvif_count_slot_state state;
Ian Campbellf942dc22011-03-15 00:06:18 +0000264 unsigned int count;
Wei Liu33bc8012013-10-08 10:54:21 +0100265 unsigned char *data;
266 unsigned i;
Ian Campbellf942dc22011-03-15 00:06:18 +0000267
Wei Liu33bc8012013-10-08 10:54:21 +0100268 state.head = true;
269 state.copy_off = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000270
Wei Liu33bc8012013-10-08 10:54:21 +0100271 /* Slot for the first (partial) page of data. */
272 count = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000273
Wei Liu33bc8012013-10-08 10:54:21 +0100274 /* Need a slot for the GSO prefix for GSO extra data? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000275 if (skb_shinfo(skb)->gso_size)
276 count++;
277
Wei Liu33bc8012013-10-08 10:54:21 +0100278 data = skb->data;
279 while (data < skb_tail_pointer(skb)) {
280 unsigned long offset = offset_in_page(data);
281 unsigned long size = PAGE_SIZE - offset;
282
283 if (data + size > skb_tail_pointer(skb))
284 size = skb_tail_pointer(skb) - data;
285
286 count += xenvif_count_frag_slots(vif, offset, size, &state);
287
288 data += size;
289 }
290
Ian Campbellf942dc22011-03-15 00:06:18 +0000291 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000292 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Ian Campbell6a8ed462012-10-10 03:48:42 +0000293 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
Ian Campbell6a8ed462012-10-10 03:48:42 +0000294
Wei Liu33bc8012013-10-08 10:54:21 +0100295 count += xenvif_count_frag_slots(vif, offset, size, &state);
Ian Campbellf942dc22011-03-15 00:06:18 +0000296 }
297 return count;
298}
299
300struct netrx_pending_operations {
301 unsigned copy_prod, copy_cons;
302 unsigned meta_prod, meta_cons;
303 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100304 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000305 int copy_off;
306 grant_ref_t copy_gref;
307};
308
Wei Liub3f980b2013-08-26 12:59:38 +0100309static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
310 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000311{
Wei Liub3f980b2013-08-26 12:59:38 +0100312 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000313 struct xen_netif_rx_request *req;
314
315 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
316
317 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100318 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000319 meta->gso_size = 0;
320 meta->size = 0;
321 meta->id = req->id;
322
323 npo->copy_off = 0;
324 npo->copy_gref = req->gref;
325
326 return meta;
327}
328
Wei Liu33bc8012013-10-08 10:54:21 +0100329/*
330 * Set up the grant operations for this fragment. If it's a flipping
331 * interface, we also set up the unmap request from here.
332 */
Wei Liu73764192013-08-26 12:59:39 +0100333static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
334 struct netrx_pending_operations *npo,
335 struct page *page, unsigned long size,
Wei Liu33bc8012013-10-08 10:54:21 +0100336 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000337{
338 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100339 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000340 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100341 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000342
343 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000344 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000345
346 meta = npo->meta + npo->meta_prod - 1;
347
Ian Campbell6a8ed462012-10-10 03:48:42 +0000348 /* Skip unused frames from start of page */
349 page += offset >> PAGE_SHIFT;
350 offset &= ~PAGE_MASK;
351
Ian Campbellf942dc22011-03-15 00:06:18 +0000352 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000353 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000354 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
355
Ian Campbell6a8ed462012-10-10 03:48:42 +0000356 bytes = PAGE_SIZE - offset;
357
358 if (bytes > size)
359 bytes = size;
360
Wei Liu33bc8012013-10-08 10:54:21 +0100361 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000362 /*
363 * Netfront requires there to be some data in the head
364 * buffer.
365 */
Wei Liu33bc8012013-10-08 10:54:21 +0100366 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000367
368 meta = get_next_rx_buffer(vif, npo);
369 }
370
Ian Campbellf942dc22011-03-15 00:06:18 +0000371 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
372 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
373
374 copy_gop = npo->copy + npo->copy_prod++;
375 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100376 copy_gop->len = bytes;
377
Wei Liu43e9d192013-08-26 12:59:37 +0100378 copy_gop->source.domid = DOMID_SELF;
379 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000380 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000381
Wei Liub3f980b2013-08-26 12:59:38 +0100382 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000383 copy_gop->dest.offset = npo->copy_off;
384 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000385
386 npo->copy_off += bytes;
387 meta->size += bytes;
388
389 offset += bytes;
390 size -= bytes;
391
Ian Campbell6a8ed462012-10-10 03:48:42 +0000392 /* Next frame */
393 if (offset == PAGE_SIZE && size) {
394 BUG_ON(!PageCompound(page));
395 page++;
396 offset = 0;
397 }
398
Ian Campbellf942dc22011-03-15 00:06:18 +0000399 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100400 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
401 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
402 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
403 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
404 else
405 gso_type = XEN_NETIF_GSO_TYPE_NONE;
406
407 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000408 vif->rx.req_cons++;
409
Wei Liu33bc8012013-10-08 10:54:21 +0100410 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000411
412 }
413}
414
415/*
416 * Prepare an SKB to be transmitted to the frontend.
417 *
418 * This function is responsible for allocating grant operations, meta
419 * structures, etc.
420 *
421 * It returns the number of meta structures consumed. The number of
422 * ring slots used is always equal to the number of meta slots used
423 * plus the number of GSO descriptors used. Currently, we use either
424 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
425 * frontend-side LRO).
426 */
Wei Liu73764192013-08-26 12:59:39 +0100427static int xenvif_gop_skb(struct sk_buff *skb,
428 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000429{
430 struct xenvif *vif = netdev_priv(skb->dev);
431 int nr_frags = skb_shinfo(skb)->nr_frags;
432 int i;
433 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100434 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000435 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100436 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000437 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100438 int gso_type;
439 int gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000440
441 old_meta_prod = npo->meta_prod;
442
Paul Durrant82cada22013-10-16 17:50:32 +0100443 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
444 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
445 gso_size = skb_shinfo(skb)->gso_size;
446 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
447 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
448 gso_size = skb_shinfo(skb)->gso_size;
449 } else {
450 gso_type = XEN_NETIF_GSO_TYPE_NONE;
451 gso_size = 0;
452 }
453
Ian Campbellf942dc22011-03-15 00:06:18 +0000454 /* Set up a GSO prefix descriptor, if necessary */
Paul Durrant82cada22013-10-16 17:50:32 +0100455 if ((1 << skb_shinfo(skb)->gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000456 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
457 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100458 meta->gso_type = gso_type;
459 meta->gso_size = gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000460 meta->size = 0;
461 meta->id = req->id;
462 }
463
464 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
465 meta = npo->meta + npo->meta_prod++;
466
Paul Durrant82cada22013-10-16 17:50:32 +0100467 if ((1 << gso_type) & vif->gso_mask) {
468 meta->gso_type = gso_type;
469 meta->gso_size = gso_size;
470 } else {
471 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000472 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100473 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000474
475 meta->size = 0;
476 meta->id = req->id;
477 npo->copy_off = 0;
478 npo->copy_gref = req->gref;
479
480 data = skb->data;
481 while (data < skb_tail_pointer(skb)) {
482 unsigned int offset = offset_in_page(data);
483 unsigned int len = PAGE_SIZE - offset;
484
485 if (data + len > skb_tail_pointer(skb))
486 len = skb_tail_pointer(skb) - data;
487
Wei Liu73764192013-08-26 12:59:39 +0100488 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100489 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000490 data += len;
491 }
492
493 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100494 xenvif_gop_frag_copy(vif, skb, npo,
495 skb_frag_page(&skb_shinfo(skb)->frags[i]),
496 skb_frag_size(&skb_shinfo(skb)->frags[i]),
497 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100498 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000499 }
500
501 return npo->meta_prod - old_meta_prod;
502}
503
504/*
Wei Liu73764192013-08-26 12:59:39 +0100505 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000506 * used to set up the operations on the top of
507 * netrx_pending_operations, which have since been done. Check that
508 * they didn't give any errors and advance over them.
509 */
Wei Liu73764192013-08-26 12:59:39 +0100510static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
511 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000512{
513 struct gnttab_copy *copy_op;
514 int status = XEN_NETIF_RSP_OKAY;
515 int i;
516
517 for (i = 0; i < nr_meta_slots; i++) {
518 copy_op = npo->copy + npo->copy_cons++;
519 if (copy_op->status != GNTST_okay) {
520 netdev_dbg(vif->dev,
521 "Bad status %d from copy to DOM%d.\n",
522 copy_op->status, vif->domid);
523 status = XEN_NETIF_RSP_ERROR;
524 }
525 }
526
527 return status;
528}
529
Wei Liu73764192013-08-26 12:59:39 +0100530static void xenvif_add_frag_responses(struct xenvif *vif, int status,
531 struct xenvif_rx_meta *meta,
532 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000533{
534 int i;
535 unsigned long offset;
536
537 /* No fragments used */
538 if (nr_meta_slots <= 1)
539 return;
540
541 nr_meta_slots--;
542
543 for (i = 0; i < nr_meta_slots; i++) {
544 int flags;
545 if (i == nr_meta_slots - 1)
546 flags = 0;
547 else
548 flags = XEN_NETRXF_more_data;
549
550 offset = 0;
551 make_rx_response(vif, meta[i].id, status, offset,
552 meta[i].size, flags);
553 }
554}
555
Wei Liu33bc8012013-10-08 10:54:21 +0100556struct skb_cb_overlay {
557 int meta_slots_used;
558};
559
Wei Liu73764192013-08-26 12:59:39 +0100560static void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000561{
Wei Liub3f980b2013-08-26 12:59:38 +0100562 wake_up(&vif->wq);
563}
564
Wei Liu73764192013-08-26 12:59:39 +0100565void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100566{
Ian Campbellf942dc22011-03-15 00:06:18 +0000567 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000568 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000569 struct xen_netif_rx_response *resp;
570 struct sk_buff_head rxq;
571 struct sk_buff *skb;
572 LIST_HEAD(notify);
573 int ret;
574 int nr_frags;
575 int count;
576 unsigned long offset;
577 struct skb_cb_overlay *sco;
Wei Liub3f980b2013-08-26 12:59:38 +0100578 int need_to_notify = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000579
580 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100581 .copy = vif->grant_copy_op,
582 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000583 };
584
585 skb_queue_head_init(&rxq);
586
587 count = 0;
588
Wei Liub3f980b2013-08-26 12:59:38 +0100589 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000590 vif = netdev_priv(skb->dev);
591 nr_frags = skb_shinfo(skb)->nr_frags;
592
593 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100594 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000595
Wei Liu33bc8012013-10-08 10:54:21 +0100596 count += nr_frags + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000597
598 __skb_queue_tail(&rxq, skb);
599
600 /* Filled the batch queue? */
Wei Liu33bc8012013-10-08 10:54:21 +0100601 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
602 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
Ian Campbellf942dc22011-03-15 00:06:18 +0000603 break;
604 }
605
Wei Liub3f980b2013-08-26 12:59:38 +0100606 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000607
608 if (!npo.copy_prod)
609 return;
610
Wei Liub3f980b2013-08-26 12:59:38 +0100611 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
612 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000613
614 while ((skb = __skb_dequeue(&rxq)) != NULL) {
615 sco = (struct skb_cb_overlay *)skb->cb;
616
617 vif = netdev_priv(skb->dev);
618
Paul Durrant82cada22013-10-16 17:50:32 +0100619 if ((1 << vif->meta[npo.meta_cons].gso_type) &
620 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000621 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100622 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000623
624 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
625
Wei Liub3f980b2013-08-26 12:59:38 +0100626 resp->offset = vif->meta[npo.meta_cons].gso_size;
627 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000628 resp->status = sco->meta_slots_used;
629
630 npo.meta_cons++;
631 sco->meta_slots_used--;
632 }
633
634
635 vif->dev->stats.tx_bytes += skb->len;
636 vif->dev->stats.tx_packets++;
637
Wei Liu73764192013-08-26 12:59:39 +0100638 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000639
640 if (sco->meta_slots_used == 1)
641 flags = 0;
642 else
643 flags = XEN_NETRXF_more_data;
644
645 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
646 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
647 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
648 /* remote but checksummed. */
649 flags |= XEN_NETRXF_data_validated;
650
651 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100652 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000653 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100654 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000655 flags);
656
Paul Durrant82cada22013-10-16 17:50:32 +0100657 if ((1 << vif->meta[npo.meta_cons].gso_type) &
658 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000659 struct xen_netif_extra_info *gso =
660 (struct xen_netif_extra_info *)
661 RING_GET_RESPONSE(&vif->rx,
662 vif->rx.rsp_prod_pvt++);
663
664 resp->flags |= XEN_NETRXF_extra_info;
665
Paul Durrant82cada22013-10-16 17:50:32 +0100666 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100667 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000668 gso->u.gso.pad = 0;
669 gso->u.gso.features = 0;
670
671 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
672 gso->flags = 0;
673 }
674
Wei Liu73764192013-08-26 12:59:39 +0100675 xenvif_add_frag_responses(vif, status,
676 vif->meta + npo.meta_cons + 1,
677 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000678
679 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000680
Wei Liub3f980b2013-08-26 12:59:38 +0100681 if (ret)
682 need_to_notify = 1;
683
Ian Campbellf942dc22011-03-15 00:06:18 +0000684 xenvif_notify_tx_completion(vif);
685
Ian Campbellf942dc22011-03-15 00:06:18 +0000686 npo.meta_cons += sco->meta_slots_used;
687 dev_kfree_skb(skb);
688 }
689
Wei Liub3f980b2013-08-26 12:59:38 +0100690 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000691 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000692
693 /* More work to do? */
Wei Liub3f980b2013-08-26 12:59:38 +0100694 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +0100695 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000696}
697
Wei Liu73764192013-08-26 12:59:39 +0100698void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000699{
Wei Liub3f980b2013-08-26 12:59:38 +0100700 skb_queue_tail(&vif->rx_queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000701
Wei Liu73764192013-08-26 12:59:39 +0100702 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000703}
704
Wei Liu73764192013-08-26 12:59:39 +0100705void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000706{
707 int more_to_do;
708
709 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
710
711 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100712 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000713}
714
715static void tx_add_credit(struct xenvif *vif)
716{
717 unsigned long max_burst, max_credit;
718
719 /*
720 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
721 * Otherwise the interface can seize up due to insufficient credit.
722 */
723 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
724 max_burst = min(max_burst, 131072UL);
725 max_burst = max(max_burst, vif->credit_bytes);
726
727 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
728 max_credit = vif->remaining_credit + vif->credit_bytes;
729 if (max_credit < vif->remaining_credit)
730 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
731
732 vif->remaining_credit = min(max_credit, max_burst);
733}
734
735static void tx_credit_callback(unsigned long data)
736{
737 struct xenvif *vif = (struct xenvif *)data;
738 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100739 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000740}
741
Wei Liu73764192013-08-26 12:59:39 +0100742static void xenvif_tx_err(struct xenvif *vif,
743 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000744{
745 RING_IDX cons = vif->tx.req_cons;
746
747 do {
748 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000749 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000750 break;
751 txp = RING_GET_REQUEST(&vif->tx, cons++);
752 } while (1);
753 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000754}
755
Wei Liu73764192013-08-26 12:59:39 +0100756static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000757{
758 netdev_err(vif->dev, "fatal error; disabling device\n");
759 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000760}
761
Wei Liu73764192013-08-26 12:59:39 +0100762static int xenvif_count_requests(struct xenvif *vif,
763 struct xen_netif_tx_request *first,
764 struct xen_netif_tx_request *txp,
765 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000766{
767 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000768 int slots = 0;
769 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000770 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000771
772 if (!(first->flags & XEN_NETTXF_more_data))
773 return 0;
774
775 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000776 struct xen_netif_tx_request dropped_tx = { 0 };
777
Wei Liu2810e5b2013-04-22 02:20:42 +0000778 if (slots >= work_to_do) {
779 netdev_err(vif->dev,
780 "Asked for %d slots but exceeds this limit\n",
781 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100782 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000783 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000784 }
785
Wei Liu2810e5b2013-04-22 02:20:42 +0000786 /* This guest is really using too many slots and
787 * considered malicious.
788 */
Wei Liu37641492013-05-02 00:43:59 +0000789 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000790 netdev_err(vif->dev,
791 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000792 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100793 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000794 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000795 }
796
Wei Liu2810e5b2013-04-22 02:20:42 +0000797 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000798 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
799 * the historical MAX_SKB_FRAGS value 18 to honor the
800 * same behavior as before. Any packet using more than
801 * 18 slots but less than fatal_skb_slots slots is
802 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000803 */
Wei Liu37641492013-05-02 00:43:59 +0000804 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000805 if (net_ratelimit())
806 netdev_dbg(vif->dev,
807 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000808 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000809 drop_err = -E2BIG;
810 }
811
Wei Liu59ccb4e2013-05-02 00:43:58 +0000812 if (drop_err)
813 txp = &dropped_tx;
814
Wei Liu2810e5b2013-04-22 02:20:42 +0000815 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000816 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000817
818 /* If the guest submitted a frame >= 64 KiB then
819 * first->size overflowed and following slots will
820 * appear to be larger than the frame.
821 *
822 * This cannot be fatal error as there are buggy
823 * frontends that do this.
824 *
825 * Consume all slots and drop the packet.
826 */
827 if (!drop_err && txp->size > first->size) {
828 if (net_ratelimit())
829 netdev_dbg(vif->dev,
830 "Invalid tx request, slot size %u > remaining size %u\n",
831 txp->size, first->size);
832 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000833 }
834
835 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000836 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000837
838 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000839 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000840 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100841 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000842 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000843 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000844
845 more_data = txp->flags & XEN_NETTXF_more_data;
846
847 if (!drop_err)
848 txp++;
849
850 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000851
852 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100853 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000854 return drop_err;
855 }
856
857 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000858}
859
Wei Liu73764192013-08-26 12:59:39 +0100860static struct page *xenvif_alloc_page(struct xenvif *vif,
861 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000862{
863 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100864
865 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000866 if (!page)
867 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100868 vif->mmap_pages[pending_idx] = page;
869
Ian Campbellf942dc22011-03-15 00:06:18 +0000870 return page;
871}
872
Wei Liu73764192013-08-26 12:59:39 +0100873static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
874 struct sk_buff *skb,
875 struct xen_netif_tx_request *txp,
876 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000877{
878 struct skb_shared_info *shinfo = skb_shinfo(skb);
879 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000880 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000881 u16 head_idx = 0;
882 int slot, start;
883 struct page *page;
884 pending_ring_idx_t index, start_idx = 0;
885 uint16_t dst_offset;
886 unsigned int nr_slots;
887 struct pending_tx_info *first = NULL;
888
889 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000890 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000891 */
892 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000893
894 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000895 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000896
Wei Liu2810e5b2013-04-22 02:20:42 +0000897 /* Coalesce tx requests, at this point the packet passed in
898 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100899 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000900 */
901 for (shinfo->nr_frags = slot = start; slot < nr_slots;
902 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000903 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100904 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000905
Wei Liub3f980b2013-08-26 12:59:38 +0100906 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000907 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000908 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000909
Wei Liu2810e5b2013-04-22 02:20:42 +0000910 dst_offset = 0;
911 first = NULL;
912 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
913 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000914
Wei Liu2810e5b2013-04-22 02:20:42 +0000915 gop->source.u.ref = txp->gref;
916 gop->source.domid = vif->domid;
917 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000918
Wei Liu2810e5b2013-04-22 02:20:42 +0000919 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000920
Wei Liu2810e5b2013-04-22 02:20:42 +0000921 gop->dest.offset = dst_offset;
922 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000923
Wei Liu2810e5b2013-04-22 02:20:42 +0000924 if (dst_offset + txp->size > PAGE_SIZE) {
925 /* This page can only merge a portion
926 * of tx request. Do not increment any
927 * pointer / counter here. The txp
928 * will be dealt with in future
929 * rounds, eventually hitting the
930 * `else` branch.
931 */
932 gop->len = PAGE_SIZE - dst_offset;
933 txp->offset += gop->len;
934 txp->size -= gop->len;
935 dst_offset += gop->len; /* quit loop */
936 } else {
937 /* This tx request can be merged in the page */
938 gop->len = txp->size;
939 dst_offset += gop->len;
940
Wei Liub3f980b2013-08-26 12:59:38 +0100941 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000942
Wei Liub3f980b2013-08-26 12:59:38 +0100943 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000944
945 memcpy(&pending_tx_info[pending_idx].req, txp,
946 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000947
948 /* Poison these fields, corresponding
949 * fields for head tx req will be set
950 * to correct values after the loop.
951 */
Wei Liub3f980b2013-08-26 12:59:38 +0100952 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000953 pending_tx_info[pending_idx].head =
954 INVALID_PENDING_RING_IDX;
955
956 if (!first) {
957 first = &pending_tx_info[pending_idx];
958 start_idx = index;
959 head_idx = pending_idx;
960 }
961
962 txp++;
963 slot++;
964 }
965
966 gop++;
967 }
968
969 first->req.offset = 0;
970 first->req.size = dst_offset;
971 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100972 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000973 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000974 }
975
Wei Liu2810e5b2013-04-22 02:20:42 +0000976 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
977
Ian Campbellf942dc22011-03-15 00:06:18 +0000978 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000979err:
980 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000981 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100982 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000983 frag_get_pending_idx(&frags[shinfo->nr_frags]),
984 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000985 }
986 /* The head too, if necessary. */
987 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100988 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000989
990 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000991}
992
Wei Liu73764192013-08-26 12:59:39 +0100993static int xenvif_tx_check_gop(struct xenvif *vif,
994 struct sk_buff *skb,
995 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000996{
997 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000998 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000999 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +00001000 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +00001001 int nr_frags = shinfo->nr_frags;
1002 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +00001003 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +00001004
1005 /* Check status of header. */
1006 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +00001007 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +01001008 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001009
1010 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001011 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001012
1013 for (i = start; i < nr_frags; i++) {
1014 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +00001015 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001016
Ian Campbellea066ad2011-10-05 00:28:46 +00001017 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +01001018 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001019 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001020
1021 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +00001022 do {
1023 newerr = (++gop)->status;
1024 if (newerr)
1025 break;
Wei Liub3f980b2013-08-26 12:59:38 +01001026 peek = vif->pending_ring[pending_index(++head)];
1027 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001028
Ian Campbellf942dc22011-03-15 00:06:18 +00001029 if (likely(!newerr)) {
1030 /* Had a previous error? Invalidate this fragment. */
1031 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +01001032 xenvif_idx_release(vif, pending_idx,
1033 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001034 continue;
1035 }
1036
1037 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +01001038 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001039
1040 /* Not the first error? Preceding frags already invalidated. */
1041 if (err)
1042 continue;
1043
1044 /* First error: invalidate header and preceding fragments. */
1045 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +01001046 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001047 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001048 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +01001049 xenvif_idx_release(vif, pending_idx,
1050 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001051 }
1052
1053 /* Remember the error: invalidate all subsequent fragments. */
1054 err = newerr;
1055 }
1056
1057 *gopp = gop + 1;
1058 return err;
1059}
1060
Wei Liu73764192013-08-26 12:59:39 +01001061static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001062{
1063 struct skb_shared_info *shinfo = skb_shinfo(skb);
1064 int nr_frags = shinfo->nr_frags;
1065 int i;
1066
1067 for (i = 0; i < nr_frags; i++) {
1068 skb_frag_t *frag = shinfo->frags + i;
1069 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001070 struct page *page;
1071 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001072
Ian Campbellea066ad2011-10-05 00:28:46 +00001073 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001074
Wei Liub3f980b2013-08-26 12:59:38 +01001075 txp = &vif->pending_tx_info[pending_idx].req;
1076 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001077 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001078 skb->len += txp->size;
1079 skb->data_len += txp->size;
1080 skb->truesize += txp->size;
1081
Wei Liu73764192013-08-26 12:59:39 +01001082 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +01001083 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +01001084 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001085 }
1086}
1087
Wei Liu73764192013-08-26 12:59:39 +01001088static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +00001089 struct xen_netif_extra_info *extras,
1090 int work_to_do)
1091{
1092 struct xen_netif_extra_info extra;
1093 RING_IDX cons = vif->tx.req_cons;
1094
1095 do {
1096 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001097 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001098 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001099 return -EBADR;
1100 }
1101
1102 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1103 sizeof(extra));
1104 if (unlikely(!extra.type ||
1105 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1106 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001107 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001108 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001109 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001110 return -EINVAL;
1111 }
1112
1113 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1114 vif->tx.req_cons = ++cons;
1115 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1116
1117 return work_to_do;
1118}
1119
Wei Liu73764192013-08-26 12:59:39 +01001120static int xenvif_set_skb_gso(struct xenvif *vif,
1121 struct sk_buff *skb,
1122 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001123{
1124 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001125 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001126 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001127 return -EINVAL;
1128 }
1129
Paul Durranta9468582013-10-16 17:50:31 +01001130 switch (gso->u.gso.type) {
1131 case XEN_NETIF_GSO_TYPE_TCPV4:
1132 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1133 break;
1134 case XEN_NETIF_GSO_TYPE_TCPV6:
1135 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1136 break;
1137 default:
Ian Campbell488562862013-02-06 23:41:35 +00001138 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001139 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001140 return -EINVAL;
1141 }
1142
1143 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001144
1145 /* Header must be checked, and gso_segs computed. */
1146 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1147 skb_shinfo(skb)->gso_segs = 0;
1148
1149 return 0;
1150}
1151
Paul Durrant1431fb32013-12-03 17:39:29 +00001152static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
1153 unsigned int max)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001154{
Paul Durrant1431fb32013-12-03 17:39:29 +00001155 if (skb_headlen(skb) >= len)
1156 return 0;
1157
1158 /* If we need to pullup then pullup to the max, so we
1159 * won't need to do it again.
1160 */
1161 if (max > skb->len)
1162 max = skb->len;
1163
1164 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
1165 return -ENOMEM;
1166
1167 if (skb_headlen(skb) < len)
1168 return -EPROTO;
1169
1170 return 0;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001171}
1172
Paul Durrant1431fb32013-12-03 17:39:29 +00001173/* This value should be large enough to cover a tagged ethernet header plus
1174 * maximally sized IP and TCP or UDP headers.
1175 */
1176#define MAX_IP_HDR_LEN 128
1177
Paul Durrant2eba61d2013-10-16 17:50:29 +01001178static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1179 int recalculate_partial_csum)
1180{
Paul Durrant2eba61d2013-10-16 17:50:29 +01001181 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001182 bool fragment;
1183 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001184
Paul Durrant1431fb32013-12-03 17:39:29 +00001185 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001186
Paul Durrant1431fb32013-12-03 17:39:29 +00001187 err = maybe_pull_tail(skb,
1188 sizeof(struct iphdr),
1189 MAX_IP_HDR_LEN);
1190 if (err < 0)
1191 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001192
Paul Durrant1431fb32013-12-03 17:39:29 +00001193 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
1194 fragment = true;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001195
Paul Durrant1431fb32013-12-03 17:39:29 +00001196 off = ip_hdrlen(skb);
1197
1198 err = -EPROTO;
1199
1200 switch (ip_hdr(skb)->protocol) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001201 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001202 err = maybe_pull_tail(skb,
1203 off + sizeof(struct tcphdr),
1204 MAX_IP_HDR_LEN);
1205 if (err < 0)
1206 goto out;
1207
Paul Durrant2eba61d2013-10-16 17:50:29 +01001208 if (!skb_partial_csum_set(skb, off,
1209 offsetof(struct tcphdr, check)))
1210 goto out;
1211
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001212 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001213 tcp_hdr(skb)->check =
1214 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1215 ip_hdr(skb)->daddr,
1216 skb->len - off,
1217 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001218 break;
1219 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001220 err = maybe_pull_tail(skb,
1221 off + sizeof(struct udphdr),
1222 MAX_IP_HDR_LEN);
1223 if (err < 0)
1224 goto out;
1225
Paul Durrant2eba61d2013-10-16 17:50:29 +01001226 if (!skb_partial_csum_set(skb, off,
1227 offsetof(struct udphdr, check)))
1228 goto out;
1229
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001230 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001231 udp_hdr(skb)->check =
1232 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1233 ip_hdr(skb)->daddr,
1234 skb->len - off,
1235 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001236 break;
1237 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001238 goto out;
1239 }
1240
1241 err = 0;
1242
1243out:
1244 return err;
1245}
1246
Paul Durrant1431fb32013-12-03 17:39:29 +00001247/* This value should be large enough to cover a tagged ethernet header plus
1248 * an IPv6 header, all options, and a maximal TCP or UDP header.
1249 */
1250#define MAX_IPV6_HDR_LEN 256
1251
1252#define OPT_HDR(type, skb, off) \
1253 (type *)(skb_network_header(skb) + (off))
1254
Paul Durrant2eba61d2013-10-16 17:50:29 +01001255static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1256 int recalculate_partial_csum)
1257{
Paul Durrant1431fb32013-12-03 17:39:29 +00001258 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001259 u8 nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001260 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001261 unsigned int len;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001262 bool fragment;
1263 bool done;
1264
Paul Durrant1431fb32013-12-03 17:39:29 +00001265 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001266 done = false;
1267
1268 off = sizeof(struct ipv6hdr);
1269
Paul Durrant1431fb32013-12-03 17:39:29 +00001270 err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
1271 if (err < 0)
1272 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001273
Paul Durrant1431fb32013-12-03 17:39:29 +00001274 nexthdr = ipv6_hdr(skb)->nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001275
Paul Durrant1431fb32013-12-03 17:39:29 +00001276 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
1277 while (off <= len && !done) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001278 switch (nexthdr) {
1279 case IPPROTO_DSTOPTS:
1280 case IPPROTO_HOPOPTS:
1281 case IPPROTO_ROUTING: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001282 struct ipv6_opt_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001283
Paul Durrant1431fb32013-12-03 17:39:29 +00001284 err = maybe_pull_tail(skb,
1285 off +
1286 sizeof(struct ipv6_opt_hdr),
1287 MAX_IPV6_HDR_LEN);
1288 if (err < 0)
1289 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001290
Paul Durrant1431fb32013-12-03 17:39:29 +00001291 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001292 nexthdr = hp->nexthdr;
1293 off += ipv6_optlen(hp);
1294 break;
1295 }
1296 case IPPROTO_AH: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001297 struct ip_auth_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001298
Paul Durrant1431fb32013-12-03 17:39:29 +00001299 err = maybe_pull_tail(skb,
1300 off +
1301 sizeof(struct ip_auth_hdr),
1302 MAX_IPV6_HDR_LEN);
1303 if (err < 0)
1304 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001305
Paul Durrant1431fb32013-12-03 17:39:29 +00001306 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001307 nexthdr = hp->nexthdr;
Paul Durrant1431fb32013-12-03 17:39:29 +00001308 off += ipv6_authlen(hp);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001309 break;
1310 }
Paul Durrant1431fb32013-12-03 17:39:29 +00001311 case IPPROTO_FRAGMENT: {
1312 struct frag_hdr *hp;
1313
1314 err = maybe_pull_tail(skb,
1315 off +
1316 sizeof(struct frag_hdr),
1317 MAX_IPV6_HDR_LEN);
1318 if (err < 0)
1319 goto out;
1320
1321 hp = OPT_HDR(struct frag_hdr, skb, off);
1322
1323 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
1324 fragment = true;
1325
1326 nexthdr = hp->nexthdr;
1327 off += sizeof(struct frag_hdr);
1328 break;
1329 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001330 default:
1331 done = true;
1332 break;
1333 }
1334 }
1335
Paul Durrant1431fb32013-12-03 17:39:29 +00001336 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001337
Paul Durrant1431fb32013-12-03 17:39:29 +00001338 if (!done || fragment)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001339 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001340
1341 switch (nexthdr) {
1342 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001343 err = maybe_pull_tail(skb,
1344 off + sizeof(struct tcphdr),
1345 MAX_IPV6_HDR_LEN);
1346 if (err < 0)
1347 goto out;
1348
Paul Durrant2eba61d2013-10-16 17:50:29 +01001349 if (!skb_partial_csum_set(skb, off,
1350 offsetof(struct tcphdr, check)))
1351 goto out;
1352
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001353 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001354 tcp_hdr(skb)->check =
1355 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1356 &ipv6_hdr(skb)->daddr,
1357 skb->len - off,
1358 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001359 break;
1360 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001361 err = maybe_pull_tail(skb,
1362 off + sizeof(struct udphdr),
1363 MAX_IPV6_HDR_LEN);
1364 if (err < 0)
1365 goto out;
1366
Paul Durrant2eba61d2013-10-16 17:50:29 +01001367 if (!skb_partial_csum_set(skb, off,
1368 offsetof(struct udphdr, check)))
1369 goto out;
1370
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001371 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001372 udp_hdr(skb)->check =
1373 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1374 &ipv6_hdr(skb)->daddr,
1375 skb->len - off,
1376 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001377 break;
1378 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001379 goto out;
1380 }
1381
1382 err = 0;
1383
1384out:
1385 return err;
1386}
1387
Ian Campbellf942dc22011-03-15 00:06:18 +00001388static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1389{
Ian Campbellf942dc22011-03-15 00:06:18 +00001390 int err = -EPROTO;
1391 int recalculate_partial_csum = 0;
1392
Paul Durrant2eba61d2013-10-16 17:50:29 +01001393 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001394 * peers can fail to set NETRXF_csum_blank when sending a GSO
1395 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1396 * recalculate the partial checksum.
1397 */
1398 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1399 vif->rx_gso_checksum_fixup++;
1400 skb->ip_summed = CHECKSUM_PARTIAL;
1401 recalculate_partial_csum = 1;
1402 }
1403
1404 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1405 if (skb->ip_summed != CHECKSUM_PARTIAL)
1406 return 0;
1407
Paul Durrant2eba61d2013-10-16 17:50:29 +01001408 if (skb->protocol == htons(ETH_P_IP))
1409 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1410 else if (skb->protocol == htons(ETH_P_IPV6))
1411 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001412
Ian Campbellf942dc22011-03-15 00:06:18 +00001413 return err;
1414}
1415
1416static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1417{
Wei Liu059dfa62013-10-28 12:07:57 +00001418 u64 now = get_jiffies_64();
1419 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001420 msecs_to_jiffies(vif->credit_usec / 1000);
1421
1422 /* Timer could already be pending in rare cases. */
1423 if (timer_pending(&vif->credit_timeout))
1424 return true;
1425
1426 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001427 if (time_after_eq64(now, next_credit)) {
1428 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001429 tx_add_credit(vif);
1430 }
1431
1432 /* Still too big to send right now? Set a callback. */
1433 if (size > vif->remaining_credit) {
1434 vif->credit_timeout.data =
1435 (unsigned long)vif;
1436 vif->credit_timeout.function =
1437 tx_credit_callback;
1438 mod_timer(&vif->credit_timeout,
1439 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001440 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001441
1442 return true;
1443 }
1444
1445 return false;
1446}
1447
Paul Durrant10574052013-12-11 10:57:15 +00001448static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001449{
Wei Liub3f980b2013-08-26 12:59:38 +01001450 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001451 struct sk_buff *skb;
1452 int ret;
1453
Wei Liub3f980b2013-08-26 12:59:38 +01001454 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
Paul Durrant10574052013-12-11 10:57:15 +00001455 < MAX_PENDING_REQS) &&
1456 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001457 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001458 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001459 struct page *page;
1460 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1461 u16 pending_idx;
1462 RING_IDX idx;
1463 int work_to_do;
1464 unsigned int data_len;
1465 pending_ring_idx_t index;
1466
Ian Campbell488562862013-02-06 23:41:35 +00001467 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1468 XEN_NETIF_TX_RING_SIZE) {
1469 netdev_err(vif->dev,
1470 "Impossible number of requests. "
1471 "req_prod %d, req_cons %d, size %ld\n",
1472 vif->tx.sring->req_prod, vif->tx.req_cons,
1473 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001474 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001475 continue;
1476 }
1477
Paul Durrantd9601a32013-12-11 10:57:16 +00001478 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001479 if (!work_to_do)
1480 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001481
1482 idx = vif->tx.req_cons;
1483 rmb(); /* Ensure that we see the request before we copy it. */
1484 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1485
1486 /* Credit-based scheduling. */
1487 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001488 tx_credit_exceeded(vif, txreq.size))
1489 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001490
1491 vif->remaining_credit -= txreq.size;
1492
1493 work_to_do--;
1494 vif->tx.req_cons = ++idx;
1495
1496 memset(extras, 0, sizeof(extras));
1497 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001498 work_to_do = xenvif_get_extras(vif, extras,
1499 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001500 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001501 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001502 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001503 }
1504
Wei Liu73764192013-08-26 12:59:39 +01001505 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001506 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001507 break;
Ian Campbell488562862013-02-06 23:41:35 +00001508
Ian Campbellf942dc22011-03-15 00:06:18 +00001509 idx += ret;
1510
1511 if (unlikely(txreq.size < ETH_HLEN)) {
1512 netdev_dbg(vif->dev,
1513 "Bad packet size: %d\n", txreq.size);
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
1518 /* No crossing a page as the payload mustn't fragment. */
1519 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001520 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001521 "txreq.offset: %x, size: %u, end: %lu\n",
1522 txreq.offset, txreq.size,
1523 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001524 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001525 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001526 }
1527
Wei Liub3f980b2013-08-26 12:59:38 +01001528 index = pending_index(vif->pending_cons);
1529 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001530
1531 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001532 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001533 PKT_PROT_LEN : txreq.size;
1534
1535 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1536 GFP_ATOMIC | __GFP_NOWARN);
1537 if (unlikely(skb == NULL)) {
1538 netdev_dbg(vif->dev,
1539 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001540 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001541 break;
1542 }
1543
1544 /* Packets passed to netif_rx() must have some headroom. */
1545 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1546
1547 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1548 struct xen_netif_extra_info *gso;
1549 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1550
Wei Liu73764192013-08-26 12:59:39 +01001551 if (xenvif_set_skb_gso(vif, skb, gso)) {
1552 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001553 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001554 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001555 }
1556 }
1557
1558 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001559 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001560 if (!page) {
1561 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001562 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001563 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001564 }
1565
Ian Campbellf942dc22011-03-15 00:06:18 +00001566 gop->source.u.ref = txreq.gref;
1567 gop->source.domid = vif->domid;
1568 gop->source.offset = txreq.offset;
1569
1570 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1571 gop->dest.domid = DOMID_SELF;
1572 gop->dest.offset = txreq.offset;
1573
1574 gop->len = txreq.size;
1575 gop->flags = GNTCOPY_source_gref;
1576
1577 gop++;
1578
Wei Liub3f980b2013-08-26 12:59:38 +01001579 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001580 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001581 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001582 *((u16 *)skb->data) = pending_idx;
1583
1584 __skb_put(skb, data_len);
1585
1586 skb_shinfo(skb)->nr_frags = ret;
1587 if (data_len < txreq.size) {
1588 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001589 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1590 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001591 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001592 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1593 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001594 }
1595
Wei Liub3f980b2013-08-26 12:59:38 +01001596 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001597
Wei Liu73764192013-08-26 12:59:39 +01001598 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001599 if (request_gop == NULL) {
1600 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001601 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001602 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001603 }
1604 gop = request_gop;
1605
Wei Liub3f980b2013-08-26 12:59:38 +01001606 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001607
Ian Campbellf942dc22011-03-15 00:06:18 +00001608 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001609
Wei Liub3f980b2013-08-26 12:59:38 +01001610 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001611 break;
1612 }
1613
Wei Liub3f980b2013-08-26 12:59:38 +01001614 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001615}
1616
Ian Campbellf942dc22011-03-15 00:06:18 +00001617
Paul Durrant10574052013-12-11 10:57:15 +00001618static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001619{
1620 struct gnttab_copy *gop = vif->tx_copy_ops;
1621 struct sk_buff *skb;
1622 int work_done = 0;
1623
Paul Durrant10574052013-12-11 10:57:15 +00001624 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001625 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001626 u16 pending_idx;
1627 unsigned data_len;
1628
1629 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001630 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001631
1632 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001633 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001634 netdev_dbg(vif->dev, "netback grant failed.\n");
1635 skb_shinfo(skb)->nr_frags = 0;
1636 kfree_skb(skb);
1637 continue;
1638 }
1639
1640 data_len = skb->len;
1641 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001642 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001643 data_len);
1644 if (data_len < txp->size) {
1645 /* Append the packet payload as a fragment. */
1646 txp->offset += data_len;
1647 txp->size -= data_len;
1648 } else {
1649 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001650 xenvif_idx_release(vif, pending_idx,
1651 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001652 }
1653
1654 if (txp->flags & XEN_NETTXF_csum_blank)
1655 skb->ip_summed = CHECKSUM_PARTIAL;
1656 else if (txp->flags & XEN_NETTXF_data_validated)
1657 skb->ip_summed = CHECKSUM_UNNECESSARY;
1658
Wei Liu73764192013-08-26 12:59:39 +01001659 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001660
Paul Durrant2eba61d2013-10-16 17:50:29 +01001661 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001662 int target = min_t(int, skb->len, PKT_PROT_LEN);
1663 __pskb_pull_tail(skb, target - skb_headlen(skb));
1664 }
1665
1666 skb->dev = vif->dev;
1667 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001668 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001669
1670 if (checksum_setup(vif, skb)) {
1671 netdev_dbg(vif->dev,
1672 "Can't setup checksum in net_tx_action\n");
1673 kfree_skb(skb);
1674 continue;
1675 }
1676
Jason Wang40893fd2013-03-26 23:11:22 +00001677 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001678
Ian Campbellf942dc22011-03-15 00:06:18 +00001679 vif->dev->stats.rx_bytes += skb->len;
1680 vif->dev->stats.rx_packets++;
1681
Wei Liub3f980b2013-08-26 12:59:38 +01001682 work_done++;
1683
1684 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001685 }
Wei Liub3f980b2013-08-26 12:59:38 +01001686
1687 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001688}
1689
1690/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001691int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001692{
1693 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001694 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001695
Wei Liub3f980b2013-08-26 12:59:38 +01001696 if (unlikely(!tx_work_todo(vif)))
1697 return 0;
1698
Paul Durrant10574052013-12-11 10:57:15 +00001699 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001700
1701 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001702 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001703
Wei Liub3f980b2013-08-26 12:59:38 +01001704 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001705
Paul Durrant10574052013-12-11 10:57:15 +00001706 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001707
1708 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001709}
1710
Wei Liu73764192013-08-26 12:59:39 +01001711static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1712 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001713{
Ian Campbellf942dc22011-03-15 00:06:18 +00001714 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001715 pending_ring_idx_t head;
1716 u16 peek; /* peek into next tx request */
1717
Wei Liub3f980b2013-08-26 12:59:38 +01001718 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001719
1720 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001721 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001722 return;
1723
Wei Liub3f980b2013-08-26 12:59:38 +01001724 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001725
Wei Liu2810e5b2013-04-22 02:20:42 +00001726 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001727
Wei Liub3f980b2013-08-26 12:59:38 +01001728 BUG_ON(!pending_tx_is_head(vif, head));
1729 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001730
Wei Liu2810e5b2013-04-22 02:20:42 +00001731 do {
1732 pending_ring_idx_t index;
1733 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001734 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001735
Wei Liub3f980b2013-08-26 12:59:38 +01001736 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001737 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001738
Wei Liu2810e5b2013-04-22 02:20:42 +00001739 /* Setting any number other than
1740 * INVALID_PENDING_RING_IDX indicates this slot is
1741 * starting a new packet / ending a previous packet.
1742 */
1743 pending_tx_info->head = 0;
1744
Wei Liub3f980b2013-08-26 12:59:38 +01001745 index = pending_index(vif->pending_prod++);
1746 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001747
Wei Liub3f980b2013-08-26 12:59:38 +01001748 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001749
Wei Liub3f980b2013-08-26 12:59:38 +01001750 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001751
Wei Liub3f980b2013-08-26 12:59:38 +01001752 put_page(vif->mmap_pages[pending_idx]);
1753 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001754}
1755
Wei Liu2810e5b2013-04-22 02:20:42 +00001756
Ian Campbellf942dc22011-03-15 00:06:18 +00001757static void make_tx_response(struct xenvif *vif,
1758 struct xen_netif_tx_request *txp,
1759 s8 st)
1760{
1761 RING_IDX i = vif->tx.rsp_prod_pvt;
1762 struct xen_netif_tx_response *resp;
1763 int notify;
1764
1765 resp = RING_GET_RESPONSE(&vif->tx, i);
1766 resp->id = txp->id;
1767 resp->status = st;
1768
1769 if (txp->flags & XEN_NETTXF_extra_info)
1770 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1771
1772 vif->tx.rsp_prod_pvt = ++i;
1773 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1774 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001775 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001776}
1777
1778static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1779 u16 id,
1780 s8 st,
1781 u16 offset,
1782 u16 size,
1783 u16 flags)
1784{
1785 RING_IDX i = vif->rx.rsp_prod_pvt;
1786 struct xen_netif_rx_response *resp;
1787
1788 resp = RING_GET_RESPONSE(&vif->rx, i);
1789 resp->offset = offset;
1790 resp->flags = flags;
1791 resp->id = id;
1792 resp->status = (s16)size;
1793 if (st < 0)
1794 resp->status = (s16)st;
1795
1796 vif->rx.rsp_prod_pvt = ++i;
1797
1798 return resp;
1799}
1800
Wei Liub3f980b2013-08-26 12:59:38 +01001801static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001802{
Wei Liub3f980b2013-08-26 12:59:38 +01001803 return !skb_queue_empty(&vif->rx_queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001804}
1805
Wei Liub3f980b2013-08-26 12:59:38 +01001806static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001807{
1808
Wei Liub3f980b2013-08-26 12:59:38 +01001809 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1810 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1811 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001812 return 1;
1813
1814 return 0;
1815}
1816
Wei Liu73764192013-08-26 12:59:39 +01001817void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001818{
David Vrabelc9d63692011-09-29 16:53:31 +01001819 if (vif->tx.sring)
1820 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1821 vif->tx.sring);
1822 if (vif->rx.sring)
1823 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1824 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001825}
1826
Wei Liu73764192013-08-26 12:59:39 +01001827int xenvif_map_frontend_rings(struct xenvif *vif,
1828 grant_ref_t tx_ring_ref,
1829 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001830{
David Vrabelc9d63692011-09-29 16:53:31 +01001831 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001832 struct xen_netif_tx_sring *txs;
1833 struct xen_netif_rx_sring *rxs;
1834
1835 int err = -ENOMEM;
1836
David Vrabelc9d63692011-09-29 16:53:31 +01001837 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1838 tx_ring_ref, &addr);
1839 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001840 goto err;
1841
David Vrabelc9d63692011-09-29 16:53:31 +01001842 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001843 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1844
David Vrabelc9d63692011-09-29 16:53:31 +01001845 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1846 rx_ring_ref, &addr);
1847 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001848 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001849
David Vrabelc9d63692011-09-29 16:53:31 +01001850 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001851 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1852
David Vrabelc9d63692011-09-29 16:53:31 +01001853 vif->rx_req_cons_peek = 0;
1854
Ian Campbellf942dc22011-03-15 00:06:18 +00001855 return 0;
1856
1857err:
Wei Liu73764192013-08-26 12:59:39 +01001858 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001859 return err;
1860}
1861
Wei Liu73764192013-08-26 12:59:39 +01001862int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001863{
1864 struct xenvif *vif = data;
1865
1866 while (!kthread_should_stop()) {
1867 wait_event_interruptible(vif->wq,
1868 rx_work_todo(vif) ||
1869 kthread_should_stop());
1870 if (kthread_should_stop())
1871 break;
1872
1873 if (rx_work_todo(vif))
Wei Liu73764192013-08-26 12:59:39 +01001874 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001875
1876 cond_resched();
1877 }
1878
1879 return 0;
1880}
1881
Ian Campbellf942dc22011-03-15 00:06:18 +00001882static int __init netback_init(void)
1883{
Ian Campbellf942dc22011-03-15 00:06:18 +00001884 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001885
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001886 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001887 return -ENODEV;
1888
Wei Liu37641492013-05-02 00:43:59 +00001889 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001890 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1891 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001892 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001893 }
1894
Ian Campbellf942dc22011-03-15 00:06:18 +00001895 rc = xenvif_xenbus_init();
1896 if (rc)
1897 goto failed_init;
1898
1899 return 0;
1900
1901failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001902 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001903}
1904
1905module_init(netback_init);
1906
Wei Liub103f352013-05-16 23:26:11 +00001907static void __exit netback_fini(void)
1908{
Wei Liub103f352013-05-16 23:26:11 +00001909 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001910}
1911module_exit(netback_fini);
1912
Ian Campbellf942dc22011-03-15 00:06:18 +00001913MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001914MODULE_ALIAS("xen-backend:vif");