blob: d0b0feb035fb82da90ae8f380e314f423cac94aa [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
7 *
8 * Copyright (c) 2002-2005, K A Fraser
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include "common.h"
36
37#include <linux/kthread.h>
38#include <linux/if_vlan.h>
39#include <linux/udp.h>
40
41#include <net/tcp.h>
42
Stefano Stabellinica981632012-08-08 17:21:23 +000043#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000044#include <xen/events.h>
45#include <xen/interface/memory.h>
46
47#include <asm/xen/hypercall.h>
48#include <asm/xen/page.h>
49
Wei Liu4f0581d2013-09-22 19:03:44 +010050/* SKB control block overlay is used to store useful information when
51 * doing guest RX.
52 */
53struct skb_cb_overlay {
54 int meta_slots_used;
55 int peek_slots_count;
56};
57
Wei Liue1f00a692013-05-22 06:34:45 +000058/* Provide an option to disable split event channels at load time as
59 * event channels are limited resource. Split event channels are
60 * enabled by default.
61 */
62bool separate_tx_rx_irq = 1;
63module_param(separate_tx_rx_irq, bool, 0644);
64
Wei Liu2810e5b2013-04-22 02:20:42 +000065/*
66 * This is the maximum slots a skb can have. If a guest sends a skb
67 * which exceeds this limit it is considered malicious.
68 */
Wei Liu37641492013-05-02 00:43:59 +000069#define FATAL_SKB_SLOTS_DEFAULT 20
70static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
71module_param(fatal_skb_slots, uint, 0444);
72
73/*
74 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
75 * the maximum slots a valid packet can use. Now this value is defined
76 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
77 * all backend.
78 */
79#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
Wei Liu2810e5b2013-04-22 02:20:42 +000080
Wei Liu2810e5b2013-04-22 02:20:42 +000081/*
82 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
83 * one or more merged tx requests, otherwise it is the continuation of
84 * previous tx request.
85 */
Wei Liub3f980b2013-08-26 12:59:38 +010086static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
Wei Liu2810e5b2013-04-22 02:20:42 +000087{
Wei Liub3f980b2013-08-26 12:59:38 +010088 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
Wei Liu2810e5b2013-04-22 02:20:42 +000089}
90
Wei Liu73764192013-08-26 12:59:39 +010091static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
92 u8 status);
93
Ian Campbellf942dc22011-03-15 00:06:18 +000094static void make_tx_response(struct xenvif *vif,
95 struct xen_netif_tx_request *txp,
96 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010097
98static inline int tx_work_todo(struct xenvif *vif);
99static inline int rx_work_todo(struct xenvif *vif);
100
Ian Campbellf942dc22011-03-15 00:06:18 +0000101static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
102 u16 id,
103 s8 st,
104 u16 offset,
105 u16 size,
106 u16 flags);
107
Wei Liub3f980b2013-08-26 12:59:38 +0100108static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000109 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000110{
Wei Liub3f980b2013-08-26 12:59:38 +0100111 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000112}
113
Wei Liub3f980b2013-08-26 12:59:38 +0100114static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000115 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000116{
Wei Liub3f980b2013-08-26 12:59:38 +0100117 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000118}
119
Ian Campbellf942dc22011-03-15 00:06:18 +0000120/*
121 * This is the amount of packet we copy rather than map, so that the
122 * guest can't fiddle with the contents of the headers while we do
123 * packet processing on them (netfilter, routing, etc).
124 */
125#define PKT_PROT_LEN (ETH_HLEN + \
126 VLAN_HLEN + \
127 sizeof(struct iphdr) + MAX_IPOPTLEN + \
128 sizeof(struct tcphdr) + MAX_TCP_OPTION_SPACE)
129
Ian Campbellea066ad2011-10-05 00:28:46 +0000130static u16 frag_get_pending_idx(skb_frag_t *frag)
131{
132 return (u16)frag->page_offset;
133}
134
135static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
136{
137 frag->page_offset = pending_idx;
138}
139
Ian Campbellf942dc22011-03-15 00:06:18 +0000140static inline pending_ring_idx_t pending_index(unsigned i)
141{
142 return i & (MAX_PENDING_REQS-1);
143}
144
Wei Liub3f980b2013-08-26 12:59:38 +0100145static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000146{
147 return MAX_PENDING_REQS -
Wei Liub3f980b2013-08-26 12:59:38 +0100148 vif->pending_prod + vif->pending_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000149}
150
151static int max_required_rx_slots(struct xenvif *vif)
152{
153 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
154
Wei Liu2810e5b2013-04-22 02:20:42 +0000155 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
Ian Campbellf942dc22011-03-15 00:06:18 +0000156 if (vif->can_sg || vif->gso || vif->gso_prefix)
157 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
158
159 return max;
160}
161
Wei Liu73764192013-08-26 12:59:39 +0100162int xenvif_rx_ring_full(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000163{
164 RING_IDX peek = vif->rx_req_cons_peek;
165 RING_IDX needed = max_required_rx_slots(vif);
166
167 return ((vif->rx.sring->req_prod - peek) < needed) ||
168 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
169}
170
Wei Liu73764192013-08-26 12:59:39 +0100171int xenvif_must_stop_queue(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000172{
Wei Liu73764192013-08-26 12:59:39 +0100173 if (!xenvif_rx_ring_full(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +0000174 return 0;
175
176 vif->rx.sring->req_event = vif->rx_req_cons_peek +
177 max_required_rx_slots(vif);
178 mb(); /* request notification /then/ check the queue */
179
Wei Liu73764192013-08-26 12:59:39 +0100180 return xenvif_rx_ring_full(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000181}
182
183/*
184 * Returns true if we should start a new receive buffer instead of
185 * adding 'size' bytes to a buffer which currently contains 'offset'
186 * bytes.
187 */
188static bool start_new_rx_buffer(int offset, unsigned long size, int head)
189{
190 /* simple case: we have completely filled the current buffer. */
191 if (offset == MAX_BUFFER_OFFSET)
192 return true;
193
194 /*
195 * complex case: start a fresh buffer if the current frag
196 * would overflow the current buffer but only if:
197 * (i) this frag would fit completely in the next buffer
198 * and (ii) there is already some data in the current buffer
199 * and (iii) this is not the head buffer.
200 *
201 * Where:
202 * - (i) stops us splitting a frag into two copies
203 * unless the frag is too large for a single buffer.
204 * - (ii) stops us from leaving a buffer pointlessly empty.
205 * - (iii) stops us leaving the first buffer
206 * empty. Strictly speaking this is already covered
207 * by (ii) but is explicitly checked because
208 * netfront relies on the first buffer being
209 * non-empty and can crash otherwise.
210 *
211 * This means we will effectively linearise small
212 * frags but do not needlessly split large buffers
213 * into multiple copies tend to give large frags their
214 * own buffers as before.
215 */
216 if ((offset + size > MAX_BUFFER_OFFSET) &&
217 (size <= MAX_BUFFER_OFFSET) && offset && !head)
218 return true;
219
220 return false;
221}
222
223/*
224 * Figure out how many ring slots we're going to need to send @skb to
225 * the guest. This function is essentially a dry run of
Wei Liu73764192013-08-26 12:59:39 +0100226 * xenvif_gop_frag_copy.
Ian Campbellf942dc22011-03-15 00:06:18 +0000227 */
Wei Liu73764192013-08-26 12:59:39 +0100228unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000229{
230 unsigned int count;
Wei Liu4f0581d2013-09-22 19:03:44 +0100231 int i, copy_off;
232 struct skb_cb_overlay *sco;
Ian Campbellf942dc22011-03-15 00:06:18 +0000233
Wei Liu4f0581d2013-09-22 19:03:44 +0100234 count = DIV_ROUND_UP(skb_headlen(skb), PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000235
Wei Liu4f0581d2013-09-22 19:03:44 +0100236 copy_off = skb_headlen(skb) % PAGE_SIZE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000237
238 if (skb_shinfo(skb)->gso_size)
239 count++;
240
241 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000242 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Ian Campbell6a8ed462012-10-10 03:48:42 +0000243 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
Wei Liu4f0581d2013-09-22 19:03:44 +0100244 unsigned long bytes;
Ian Campbell6a8ed462012-10-10 03:48:42 +0000245
Wei Liu4f0581d2013-09-22 19:03:44 +0100246 offset &= ~PAGE_MASK;
247
248 while (size > 0) {
249 BUG_ON(offset >= PAGE_SIZE);
250 BUG_ON(copy_off > MAX_BUFFER_OFFSET);
251
252 bytes = PAGE_SIZE - offset;
253
254 if (bytes > size)
255 bytes = size;
256
257 if (start_new_rx_buffer(copy_off, bytes, 0)) {
258 count++;
259 copy_off = 0;
260 }
261
262 if (copy_off + bytes > MAX_BUFFER_OFFSET)
263 bytes = MAX_BUFFER_OFFSET - copy_off;
264
265 copy_off += bytes;
266
267 offset += bytes;
268 size -= bytes;
269
270 if (offset == PAGE_SIZE)
271 offset = 0;
272 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000273 }
Wei Liu4f0581d2013-09-22 19:03:44 +0100274
275 sco = (struct skb_cb_overlay *)skb->cb;
276 sco->peek_slots_count = count;
Ian Campbellf942dc22011-03-15 00:06:18 +0000277 return count;
278}
279
280struct netrx_pending_operations {
281 unsigned copy_prod, copy_cons;
282 unsigned meta_prod, meta_cons;
283 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100284 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000285 int copy_off;
286 grant_ref_t copy_gref;
287};
288
Wei Liub3f980b2013-08-26 12:59:38 +0100289static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
290 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000291{
Wei Liub3f980b2013-08-26 12:59:38 +0100292 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000293 struct xen_netif_rx_request *req;
294
295 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
296
297 meta = npo->meta + npo->meta_prod++;
298 meta->gso_size = 0;
299 meta->size = 0;
300 meta->id = req->id;
301
302 npo->copy_off = 0;
303 npo->copy_gref = req->gref;
304
305 return meta;
306}
307
Wei Liu4f0581d2013-09-22 19:03:44 +0100308/* Set up the grant operations for this fragment. */
Wei Liu73764192013-08-26 12:59:39 +0100309static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
310 struct netrx_pending_operations *npo,
311 struct page *page, unsigned long size,
Wei Liu4f0581d2013-09-22 19:03:44 +0100312 unsigned long offset, int head, int *first)
Ian Campbellf942dc22011-03-15 00:06:18 +0000313{
314 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100315 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000316 unsigned long bytes;
317
318 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000319 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000320
321 meta = npo->meta + npo->meta_prod - 1;
322
Ian Campbell6a8ed462012-10-10 03:48:42 +0000323 /* Skip unused frames from start of page */
324 page += offset >> PAGE_SHIFT;
325 offset &= ~PAGE_MASK;
326
Ian Campbellf942dc22011-03-15 00:06:18 +0000327 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000328 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000329 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
330
Ian Campbell6a8ed462012-10-10 03:48:42 +0000331 bytes = PAGE_SIZE - offset;
332
333 if (bytes > size)
334 bytes = size;
335
Wei Liu4f0581d2013-09-22 19:03:44 +0100336 if (start_new_rx_buffer(npo->copy_off, bytes, head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000337 /*
338 * Netfront requires there to be some data in the head
339 * buffer.
340 */
Wei Liu4f0581d2013-09-22 19:03:44 +0100341 BUG_ON(*first);
Ian Campbellf942dc22011-03-15 00:06:18 +0000342
343 meta = get_next_rx_buffer(vif, npo);
344 }
345
Ian Campbellf942dc22011-03-15 00:06:18 +0000346 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
347 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
348
349 copy_gop = npo->copy + npo->copy_prod++;
350 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100351 copy_gop->len = bytes;
352
Wei Liu43e9d192013-08-26 12:59:37 +0100353 copy_gop->source.domid = DOMID_SELF;
354 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000355 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000356
Wei Liub3f980b2013-08-26 12:59:38 +0100357 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000358 copy_gop->dest.offset = npo->copy_off;
359 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000360
361 npo->copy_off += bytes;
362 meta->size += bytes;
363
364 offset += bytes;
365 size -= bytes;
366
Ian Campbell6a8ed462012-10-10 03:48:42 +0000367 /* Next frame */
368 if (offset == PAGE_SIZE && size) {
369 BUG_ON(!PageCompound(page));
370 page++;
371 offset = 0;
372 }
373
Ian Campbellf942dc22011-03-15 00:06:18 +0000374 /* Leave a gap for the GSO descriptor. */
Wei Liu4f0581d2013-09-22 19:03:44 +0100375 if (*first && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
Ian Campbellf942dc22011-03-15 00:06:18 +0000376 vif->rx.req_cons++;
377
Wei Liu4f0581d2013-09-22 19:03:44 +0100378 *first = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000379
380 }
381}
382
383/*
384 * Prepare an SKB to be transmitted to the frontend.
385 *
386 * This function is responsible for allocating grant operations, meta
387 * structures, etc.
388 *
389 * It returns the number of meta structures consumed. The number of
390 * ring slots used is always equal to the number of meta slots used
391 * plus the number of GSO descriptors used. Currently, we use either
392 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
393 * frontend-side LRO).
394 */
Wei Liu73764192013-08-26 12:59:39 +0100395static int xenvif_gop_skb(struct sk_buff *skb,
396 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000397{
398 struct xenvif *vif = netdev_priv(skb->dev);
399 int nr_frags = skb_shinfo(skb)->nr_frags;
400 int i;
401 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100402 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000403 unsigned char *data;
Wei Liu4f0581d2013-09-22 19:03:44 +0100404 int first = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000405 int old_meta_prod;
406
407 old_meta_prod = npo->meta_prod;
408
409 /* Set up a GSO prefix descriptor, if necessary */
410 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
411 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
412 meta = npo->meta + npo->meta_prod++;
413 meta->gso_size = skb_shinfo(skb)->gso_size;
414 meta->size = 0;
415 meta->id = req->id;
416 }
417
418 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
419 meta = npo->meta + npo->meta_prod++;
420
421 if (!vif->gso_prefix)
422 meta->gso_size = skb_shinfo(skb)->gso_size;
423 else
424 meta->gso_size = 0;
425
426 meta->size = 0;
427 meta->id = req->id;
428 npo->copy_off = 0;
429 npo->copy_gref = req->gref;
430
431 data = skb->data;
432 while (data < skb_tail_pointer(skb)) {
433 unsigned int offset = offset_in_page(data);
434 unsigned int len = PAGE_SIZE - offset;
435
436 if (data + len > skb_tail_pointer(skb))
437 len = skb_tail_pointer(skb) - data;
438
Wei Liu73764192013-08-26 12:59:39 +0100439 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu4f0581d2013-09-22 19:03:44 +0100440 virt_to_page(data), len, offset, 1, &first);
Ian Campbellf942dc22011-03-15 00:06:18 +0000441 data += len;
442 }
443
444 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100445 xenvif_gop_frag_copy(vif, skb, npo,
446 skb_frag_page(&skb_shinfo(skb)->frags[i]),
447 skb_frag_size(&skb_shinfo(skb)->frags[i]),
448 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu4f0581d2013-09-22 19:03:44 +0100449 0, &first);
Ian Campbellf942dc22011-03-15 00:06:18 +0000450 }
451
452 return npo->meta_prod - old_meta_prod;
453}
454
455/*
Wei Liu73764192013-08-26 12:59:39 +0100456 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000457 * used to set up the operations on the top of
458 * netrx_pending_operations, which have since been done. Check that
459 * they didn't give any errors and advance over them.
460 */
Wei Liu73764192013-08-26 12:59:39 +0100461static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
462 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000463{
464 struct gnttab_copy *copy_op;
465 int status = XEN_NETIF_RSP_OKAY;
466 int i;
467
468 for (i = 0; i < nr_meta_slots; i++) {
469 copy_op = npo->copy + npo->copy_cons++;
470 if (copy_op->status != GNTST_okay) {
471 netdev_dbg(vif->dev,
472 "Bad status %d from copy to DOM%d.\n",
473 copy_op->status, vif->domid);
474 status = XEN_NETIF_RSP_ERROR;
475 }
476 }
477
478 return status;
479}
480
Wei Liu73764192013-08-26 12:59:39 +0100481static void xenvif_add_frag_responses(struct xenvif *vif, int status,
482 struct xenvif_rx_meta *meta,
483 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000484{
485 int i;
486 unsigned long offset;
487
488 /* No fragments used */
489 if (nr_meta_slots <= 1)
490 return;
491
492 nr_meta_slots--;
493
494 for (i = 0; i < nr_meta_slots; i++) {
495 int flags;
496 if (i == nr_meta_slots - 1)
497 flags = 0;
498 else
499 flags = XEN_NETRXF_more_data;
500
501 offset = 0;
502 make_rx_response(vif, meta[i].id, status, offset,
503 meta[i].size, flags);
504 }
505}
506
Wei Liu73764192013-08-26 12:59:39 +0100507static void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000508{
Wei Liub3f980b2013-08-26 12:59:38 +0100509 wake_up(&vif->wq);
510}
511
Wei Liu73764192013-08-26 12:59:39 +0100512void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100513{
Ian Campbellf942dc22011-03-15 00:06:18 +0000514 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000515 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000516 struct xen_netif_rx_response *resp;
517 struct sk_buff_head rxq;
518 struct sk_buff *skb;
519 LIST_HEAD(notify);
520 int ret;
521 int nr_frags;
522 int count;
523 unsigned long offset;
524 struct skb_cb_overlay *sco;
Wei Liub3f980b2013-08-26 12:59:38 +0100525 int need_to_notify = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000526
527 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100528 .copy = vif->grant_copy_op,
529 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000530 };
531
532 skb_queue_head_init(&rxq);
533
534 count = 0;
535
Wei Liub3f980b2013-08-26 12:59:38 +0100536 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Wei Liu4f0581d2013-09-22 19:03:44 +0100537 RING_IDX old_rx_req_cons;
538
Ian Campbellf942dc22011-03-15 00:06:18 +0000539 vif = netdev_priv(skb->dev);
540 nr_frags = skb_shinfo(skb)->nr_frags;
541
Wei Liu4f0581d2013-09-22 19:03:44 +0100542 old_rx_req_cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000543 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100544 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000545
Wei Liu4f0581d2013-09-22 19:03:44 +0100546 count += vif->rx.req_cons - old_rx_req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000547
548 __skb_queue_tail(&rxq, skb);
549
Wei Liu4f0581d2013-09-22 19:03:44 +0100550 skb = skb_peek(&vif->rx_queue);
551 if (skb == NULL)
552 break;
553 sco = (struct skb_cb_overlay *)skb->cb;
554
Ian Campbellf942dc22011-03-15 00:06:18 +0000555 /* Filled the batch queue? */
Wei Liu4f0581d2013-09-22 19:03:44 +0100556 if (count + sco->peek_slots_count >= XEN_NETIF_RX_RING_SIZE)
Ian Campbellf942dc22011-03-15 00:06:18 +0000557 break;
558 }
559
Wei Liub3f980b2013-08-26 12:59:38 +0100560 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000561
562 if (!npo.copy_prod)
563 return;
564
Wei Liub3f980b2013-08-26 12:59:38 +0100565 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
566 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000567
568 while ((skb = __skb_dequeue(&rxq)) != NULL) {
569 sco = (struct skb_cb_overlay *)skb->cb;
570
571 vif = netdev_priv(skb->dev);
572
Wei Liub3f980b2013-08-26 12:59:38 +0100573 if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000574 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100575 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000576
577 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
578
Wei Liub3f980b2013-08-26 12:59:38 +0100579 resp->offset = vif->meta[npo.meta_cons].gso_size;
580 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000581 resp->status = sco->meta_slots_used;
582
583 npo.meta_cons++;
584 sco->meta_slots_used--;
585 }
586
587
588 vif->dev->stats.tx_bytes += skb->len;
589 vif->dev->stats.tx_packets++;
590
Wei Liu73764192013-08-26 12:59:39 +0100591 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000592
593 if (sco->meta_slots_used == 1)
594 flags = 0;
595 else
596 flags = XEN_NETRXF_more_data;
597
598 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
599 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
600 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
601 /* remote but checksummed. */
602 flags |= XEN_NETRXF_data_validated;
603
604 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100605 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000606 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100607 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000608 flags);
609
Wei Liub3f980b2013-08-26 12:59:38 +0100610 if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000611 struct xen_netif_extra_info *gso =
612 (struct xen_netif_extra_info *)
613 RING_GET_RESPONSE(&vif->rx,
614 vif->rx.rsp_prod_pvt++);
615
616 resp->flags |= XEN_NETRXF_extra_info;
617
Wei Liub3f980b2013-08-26 12:59:38 +0100618 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000619 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
620 gso->u.gso.pad = 0;
621 gso->u.gso.features = 0;
622
623 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
624 gso->flags = 0;
625 }
626
Wei Liu73764192013-08-26 12:59:39 +0100627 xenvif_add_frag_responses(vif, status,
628 vif->meta + npo.meta_cons + 1,
629 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000630
631 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000632
Wei Liub3f980b2013-08-26 12:59:38 +0100633 if (ret)
634 need_to_notify = 1;
635
Ian Campbellf942dc22011-03-15 00:06:18 +0000636 xenvif_notify_tx_completion(vif);
637
Ian Campbellf942dc22011-03-15 00:06:18 +0000638 npo.meta_cons += sco->meta_slots_used;
639 dev_kfree_skb(skb);
640 }
641
Wei Liub3f980b2013-08-26 12:59:38 +0100642 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000643 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000644
645 /* More work to do? */
Wei Liub3f980b2013-08-26 12:59:38 +0100646 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +0100647 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000648}
649
Wei Liu73764192013-08-26 12:59:39 +0100650void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000651{
Wei Liub3f980b2013-08-26 12:59:38 +0100652 skb_queue_tail(&vif->rx_queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000653
Wei Liu73764192013-08-26 12:59:39 +0100654 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000655}
656
Wei Liu73764192013-08-26 12:59:39 +0100657void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000658{
659 int more_to_do;
660
661 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
662
663 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100664 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000665}
666
667static void tx_add_credit(struct xenvif *vif)
668{
669 unsigned long max_burst, max_credit;
670
671 /*
672 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
673 * Otherwise the interface can seize up due to insufficient credit.
674 */
675 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
676 max_burst = min(max_burst, 131072UL);
677 max_burst = max(max_burst, vif->credit_bytes);
678
679 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
680 max_credit = vif->remaining_credit + vif->credit_bytes;
681 if (max_credit < vif->remaining_credit)
682 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
683
684 vif->remaining_credit = min(max_credit, max_burst);
685}
686
687static void tx_credit_callback(unsigned long data)
688{
689 struct xenvif *vif = (struct xenvif *)data;
690 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100691 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000692}
693
Wei Liu73764192013-08-26 12:59:39 +0100694static void xenvif_tx_err(struct xenvif *vif,
695 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000696{
697 RING_IDX cons = vif->tx.req_cons;
698
699 do {
700 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000701 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000702 break;
703 txp = RING_GET_REQUEST(&vif->tx, cons++);
704 } while (1);
705 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000706}
707
Wei Liu73764192013-08-26 12:59:39 +0100708static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000709{
710 netdev_err(vif->dev, "fatal error; disabling device\n");
711 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000712}
713
Wei Liu73764192013-08-26 12:59:39 +0100714static int xenvif_count_requests(struct xenvif *vif,
715 struct xen_netif_tx_request *first,
716 struct xen_netif_tx_request *txp,
717 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000718{
719 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000720 int slots = 0;
721 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000722 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000723
724 if (!(first->flags & XEN_NETTXF_more_data))
725 return 0;
726
727 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000728 struct xen_netif_tx_request dropped_tx = { 0 };
729
Wei Liu2810e5b2013-04-22 02:20:42 +0000730 if (slots >= work_to_do) {
731 netdev_err(vif->dev,
732 "Asked for %d slots but exceeds this limit\n",
733 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100734 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000735 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000736 }
737
Wei Liu2810e5b2013-04-22 02:20:42 +0000738 /* This guest is really using too many slots and
739 * considered malicious.
740 */
Wei Liu37641492013-05-02 00:43:59 +0000741 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000742 netdev_err(vif->dev,
743 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000744 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100745 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000746 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000747 }
748
Wei Liu2810e5b2013-04-22 02:20:42 +0000749 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000750 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
751 * the historical MAX_SKB_FRAGS value 18 to honor the
752 * same behavior as before. Any packet using more than
753 * 18 slots but less than fatal_skb_slots slots is
754 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000755 */
Wei Liu37641492013-05-02 00:43:59 +0000756 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000757 if (net_ratelimit())
758 netdev_dbg(vif->dev,
759 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000760 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000761 drop_err = -E2BIG;
762 }
763
Wei Liu59ccb4e2013-05-02 00:43:58 +0000764 if (drop_err)
765 txp = &dropped_tx;
766
Wei Liu2810e5b2013-04-22 02:20:42 +0000767 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000768 sizeof(*txp));
Wei Liu03393fd2013-04-22 02:20:43 +0000769
770 /* If the guest submitted a frame >= 64 KiB then
771 * first->size overflowed and following slots will
772 * appear to be larger than the frame.
773 *
774 * This cannot be fatal error as there are buggy
775 * frontends that do this.
776 *
777 * Consume all slots and drop the packet.
778 */
779 if (!drop_err && txp->size > first->size) {
780 if (net_ratelimit())
781 netdev_dbg(vif->dev,
782 "Invalid tx request, slot size %u > remaining size %u\n",
783 txp->size, first->size);
784 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000785 }
786
787 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000788 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000789
790 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000791 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000792 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100793 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000794 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000795 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000796
797 more_data = txp->flags & XEN_NETTXF_more_data;
798
799 if (!drop_err)
800 txp++;
801
802 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000803
804 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100805 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000806 return drop_err;
807 }
808
809 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000810}
811
Wei Liu73764192013-08-26 12:59:39 +0100812static struct page *xenvif_alloc_page(struct xenvif *vif,
813 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000814{
815 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100816
817 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000818 if (!page)
819 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100820 vif->mmap_pages[pending_idx] = page;
821
Ian Campbellf942dc22011-03-15 00:06:18 +0000822 return page;
823}
824
Wei Liu73764192013-08-26 12:59:39 +0100825static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
826 struct sk_buff *skb,
827 struct xen_netif_tx_request *txp,
828 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000829{
830 struct skb_shared_info *shinfo = skb_shinfo(skb);
831 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000832 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000833 u16 head_idx = 0;
834 int slot, start;
835 struct page *page;
836 pending_ring_idx_t index, start_idx = 0;
837 uint16_t dst_offset;
838 unsigned int nr_slots;
839 struct pending_tx_info *first = NULL;
840
841 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000842 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000843 */
844 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000845
846 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000847 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000848
Wei Liu2810e5b2013-04-22 02:20:42 +0000849 /* Coalesce tx requests, at this point the packet passed in
850 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100851 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000852 */
853 for (shinfo->nr_frags = slot = start; slot < nr_slots;
854 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000855 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100856 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000857
Wei Liub3f980b2013-08-26 12:59:38 +0100858 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000859 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000860 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000861
Wei Liu2810e5b2013-04-22 02:20:42 +0000862 dst_offset = 0;
863 first = NULL;
864 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
865 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000866
Wei Liu2810e5b2013-04-22 02:20:42 +0000867 gop->source.u.ref = txp->gref;
868 gop->source.domid = vif->domid;
869 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000870
Wei Liu2810e5b2013-04-22 02:20:42 +0000871 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000872
Wei Liu2810e5b2013-04-22 02:20:42 +0000873 gop->dest.offset = dst_offset;
874 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000875
Wei Liu2810e5b2013-04-22 02:20:42 +0000876 if (dst_offset + txp->size > PAGE_SIZE) {
877 /* This page can only merge a portion
878 * of tx request. Do not increment any
879 * pointer / counter here. The txp
880 * will be dealt with in future
881 * rounds, eventually hitting the
882 * `else` branch.
883 */
884 gop->len = PAGE_SIZE - dst_offset;
885 txp->offset += gop->len;
886 txp->size -= gop->len;
887 dst_offset += gop->len; /* quit loop */
888 } else {
889 /* This tx request can be merged in the page */
890 gop->len = txp->size;
891 dst_offset += gop->len;
892
Wei Liub3f980b2013-08-26 12:59:38 +0100893 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000894
Wei Liub3f980b2013-08-26 12:59:38 +0100895 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000896
897 memcpy(&pending_tx_info[pending_idx].req, txp,
898 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000899
900 /* Poison these fields, corresponding
901 * fields for head tx req will be set
902 * to correct values after the loop.
903 */
Wei Liub3f980b2013-08-26 12:59:38 +0100904 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000905 pending_tx_info[pending_idx].head =
906 INVALID_PENDING_RING_IDX;
907
908 if (!first) {
909 first = &pending_tx_info[pending_idx];
910 start_idx = index;
911 head_idx = pending_idx;
912 }
913
914 txp++;
915 slot++;
916 }
917
918 gop++;
919 }
920
921 first->req.offset = 0;
922 first->req.size = dst_offset;
923 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100924 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000925 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000926 }
927
Wei Liu2810e5b2013-04-22 02:20:42 +0000928 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
929
Ian Campbellf942dc22011-03-15 00:06:18 +0000930 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000931err:
932 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000933 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100934 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000935 frag_get_pending_idx(&frags[shinfo->nr_frags]),
936 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000937 }
938 /* The head too, if necessary. */
939 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100940 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000941
942 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000943}
944
Wei Liu73764192013-08-26 12:59:39 +0100945static int xenvif_tx_check_gop(struct xenvif *vif,
946 struct sk_buff *skb,
947 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000948{
949 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000950 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000951 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000952 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000953 int nr_frags = shinfo->nr_frags;
954 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000955 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000956
957 /* Check status of header. */
958 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000959 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100960 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000961
962 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000963 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000964
965 for (i = start; i < nr_frags; i++) {
966 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000967 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000968
Ian Campbellea066ad2011-10-05 00:28:46 +0000969 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100970 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000971 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000972
973 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000974 do {
975 newerr = (++gop)->status;
976 if (newerr)
977 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100978 peek = vif->pending_ring[pending_index(++head)];
979 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000980
Ian Campbellf942dc22011-03-15 00:06:18 +0000981 if (likely(!newerr)) {
982 /* Had a previous error? Invalidate this fragment. */
983 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100984 xenvif_idx_release(vif, pending_idx,
985 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000986 continue;
987 }
988
989 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100990 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000991
992 /* Not the first error? Preceding frags already invalidated. */
993 if (err)
994 continue;
995
996 /* First error: invalidate header and preceding fragments. */
997 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +0100998 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000999 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001000 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +01001001 xenvif_idx_release(vif, pending_idx,
1002 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001003 }
1004
1005 /* Remember the error: invalidate all subsequent fragments. */
1006 err = newerr;
1007 }
1008
1009 *gopp = gop + 1;
1010 return err;
1011}
1012
Wei Liu73764192013-08-26 12:59:39 +01001013static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001014{
1015 struct skb_shared_info *shinfo = skb_shinfo(skb);
1016 int nr_frags = shinfo->nr_frags;
1017 int i;
1018
1019 for (i = 0; i < nr_frags; i++) {
1020 skb_frag_t *frag = shinfo->frags + i;
1021 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001022 struct page *page;
1023 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001024
Ian Campbellea066ad2011-10-05 00:28:46 +00001025 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001026
Wei Liub3f980b2013-08-26 12:59:38 +01001027 txp = &vif->pending_tx_info[pending_idx].req;
1028 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001029 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001030 skb->len += txp->size;
1031 skb->data_len += txp->size;
1032 skb->truesize += txp->size;
1033
Wei Liu73764192013-08-26 12:59:39 +01001034 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +01001035 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +01001036 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001037 }
1038}
1039
Wei Liu73764192013-08-26 12:59:39 +01001040static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +00001041 struct xen_netif_extra_info *extras,
1042 int work_to_do)
1043{
1044 struct xen_netif_extra_info extra;
1045 RING_IDX cons = vif->tx.req_cons;
1046
1047 do {
1048 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001049 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001050 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001051 return -EBADR;
1052 }
1053
1054 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1055 sizeof(extra));
1056 if (unlikely(!extra.type ||
1057 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1058 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001059 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001060 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001061 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001062 return -EINVAL;
1063 }
1064
1065 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1066 vif->tx.req_cons = ++cons;
1067 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1068
1069 return work_to_do;
1070}
1071
Wei Liu73764192013-08-26 12:59:39 +01001072static int xenvif_set_skb_gso(struct xenvif *vif,
1073 struct sk_buff *skb,
1074 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001075{
1076 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001077 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001078 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001079 return -EINVAL;
1080 }
1081
1082 /* Currently only TCPv4 S.O. is supported. */
1083 if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
Ian Campbell488562862013-02-06 23:41:35 +00001084 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001085 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001086 return -EINVAL;
1087 }
1088
1089 skb_shinfo(skb)->gso_size = gso->u.gso.size;
1090 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1091
1092 /* Header must be checked, and gso_segs computed. */
1093 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1094 skb_shinfo(skb)->gso_segs = 0;
1095
1096 return 0;
1097}
1098
1099static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1100{
1101 struct iphdr *iph;
Ian Campbellf942dc22011-03-15 00:06:18 +00001102 int err = -EPROTO;
1103 int recalculate_partial_csum = 0;
1104
1105 /*
1106 * A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
1107 * peers can fail to set NETRXF_csum_blank when sending a GSO
1108 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1109 * recalculate the partial checksum.
1110 */
1111 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1112 vif->rx_gso_checksum_fixup++;
1113 skb->ip_summed = CHECKSUM_PARTIAL;
1114 recalculate_partial_csum = 1;
1115 }
1116
1117 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1118 if (skb->ip_summed != CHECKSUM_PARTIAL)
1119 return 0;
1120
1121 if (skb->protocol != htons(ETH_P_IP))
1122 goto out;
1123
1124 iph = (void *)skb->data;
Ian Campbellf942dc22011-03-15 00:06:18 +00001125 switch (iph->protocol) {
1126 case IPPROTO_TCP:
Jason Wangbea89332013-04-10 20:35:29 +00001127 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1128 offsetof(struct tcphdr, check)))
1129 goto out;
Ian Campbellf942dc22011-03-15 00:06:18 +00001130
1131 if (recalculate_partial_csum) {
Jason Wangbea89332013-04-10 20:35:29 +00001132 struct tcphdr *tcph = tcp_hdr(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001133 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1134 skb->len - iph->ihl*4,
1135 IPPROTO_TCP, 0);
1136 }
1137 break;
1138 case IPPROTO_UDP:
Jason Wangbea89332013-04-10 20:35:29 +00001139 if (!skb_partial_csum_set(skb, 4 * iph->ihl,
1140 offsetof(struct udphdr, check)))
1141 goto out;
Ian Campbellf942dc22011-03-15 00:06:18 +00001142
1143 if (recalculate_partial_csum) {
Jason Wangbea89332013-04-10 20:35:29 +00001144 struct udphdr *udph = udp_hdr(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001145 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1146 skb->len - iph->ihl*4,
1147 IPPROTO_UDP, 0);
1148 }
1149 break;
1150 default:
1151 if (net_ratelimit())
1152 netdev_err(vif->dev,
1153 "Attempting to checksum a non-TCP/UDP packet, dropping a protocol %d packet\n",
1154 iph->protocol);
1155 goto out;
1156 }
1157
Ian Campbellf942dc22011-03-15 00:06:18 +00001158 err = 0;
1159
1160out:
1161 return err;
1162}
1163
1164static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1165{
1166 unsigned long now = jiffies;
1167 unsigned long next_credit =
1168 vif->credit_timeout.expires +
1169 msecs_to_jiffies(vif->credit_usec / 1000);
1170
1171 /* Timer could already be pending in rare cases. */
1172 if (timer_pending(&vif->credit_timeout))
1173 return true;
1174
1175 /* Passed the point where we can replenish credit? */
1176 if (time_after_eq(now, next_credit)) {
1177 vif->credit_timeout.expires = now;
1178 tx_add_credit(vif);
1179 }
1180
1181 /* Still too big to send right now? Set a callback. */
1182 if (size > vif->remaining_credit) {
1183 vif->credit_timeout.data =
1184 (unsigned long)vif;
1185 vif->credit_timeout.function =
1186 tx_credit_callback;
1187 mod_timer(&vif->credit_timeout,
1188 next_credit);
1189
1190 return true;
1191 }
1192
1193 return false;
1194}
1195
Wei Liu73764192013-08-26 12:59:39 +01001196static unsigned xenvif_tx_build_gops(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001197{
Wei Liub3f980b2013-08-26 12:59:38 +01001198 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001199 struct sk_buff *skb;
1200 int ret;
1201
Wei Liub3f980b2013-08-26 12:59:38 +01001202 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1203 < MAX_PENDING_REQS)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001204 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001205 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001206 struct page *page;
1207 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1208 u16 pending_idx;
1209 RING_IDX idx;
1210 int work_to_do;
1211 unsigned int data_len;
1212 pending_ring_idx_t index;
1213
Ian Campbell488562862013-02-06 23:41:35 +00001214 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1215 XEN_NETIF_TX_RING_SIZE) {
1216 netdev_err(vif->dev,
1217 "Impossible number of requests. "
1218 "req_prod %d, req_cons %d, size %ld\n",
1219 vif->tx.sring->req_prod, vif->tx.req_cons,
1220 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001221 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001222 continue;
1223 }
1224
Ian Campbellf942dc22011-03-15 00:06:18 +00001225 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
Wei Liub3f980b2013-08-26 12:59:38 +01001226 if (!work_to_do)
1227 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001228
1229 idx = vif->tx.req_cons;
1230 rmb(); /* Ensure that we see the request before we copy it. */
1231 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1232
1233 /* Credit-based scheduling. */
1234 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001235 tx_credit_exceeded(vif, txreq.size))
1236 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001237
1238 vif->remaining_credit -= txreq.size;
1239
1240 work_to_do--;
1241 vif->tx.req_cons = ++idx;
1242
1243 memset(extras, 0, sizeof(extras));
1244 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001245 work_to_do = xenvif_get_extras(vif, extras,
1246 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001247 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001248 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001249 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001250 }
1251
Wei Liu73764192013-08-26 12:59:39 +01001252 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001253 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001254 break;
Ian Campbell488562862013-02-06 23:41:35 +00001255
Ian Campbellf942dc22011-03-15 00:06:18 +00001256 idx += ret;
1257
1258 if (unlikely(txreq.size < ETH_HLEN)) {
1259 netdev_dbg(vif->dev,
1260 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001261 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001262 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001263 }
1264
1265 /* No crossing a page as the payload mustn't fragment. */
1266 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001267 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001268 "txreq.offset: %x, size: %u, end: %lu\n",
1269 txreq.offset, txreq.size,
1270 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001271 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001272 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001273 }
1274
Wei Liub3f980b2013-08-26 12:59:38 +01001275 index = pending_index(vif->pending_cons);
1276 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001277
1278 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001279 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001280 PKT_PROT_LEN : txreq.size;
1281
1282 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1283 GFP_ATOMIC | __GFP_NOWARN);
1284 if (unlikely(skb == NULL)) {
1285 netdev_dbg(vif->dev,
1286 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001287 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001288 break;
1289 }
1290
1291 /* Packets passed to netif_rx() must have some headroom. */
1292 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1293
1294 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1295 struct xen_netif_extra_info *gso;
1296 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1297
Wei Liu73764192013-08-26 12:59:39 +01001298 if (xenvif_set_skb_gso(vif, skb, gso)) {
1299 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001300 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001301 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001302 }
1303 }
1304
1305 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001306 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001307 if (!page) {
1308 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001309 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001310 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001311 }
1312
Ian Campbellf942dc22011-03-15 00:06:18 +00001313 gop->source.u.ref = txreq.gref;
1314 gop->source.domid = vif->domid;
1315 gop->source.offset = txreq.offset;
1316
1317 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1318 gop->dest.domid = DOMID_SELF;
1319 gop->dest.offset = txreq.offset;
1320
1321 gop->len = txreq.size;
1322 gop->flags = GNTCOPY_source_gref;
1323
1324 gop++;
1325
Wei Liub3f980b2013-08-26 12:59:38 +01001326 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001327 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001328 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001329 *((u16 *)skb->data) = pending_idx;
1330
1331 __skb_put(skb, data_len);
1332
1333 skb_shinfo(skb)->nr_frags = ret;
1334 if (data_len < txreq.size) {
1335 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001336 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1337 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001338 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001339 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1340 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001341 }
1342
Wei Liub3f980b2013-08-26 12:59:38 +01001343 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001344
Wei Liu73764192013-08-26 12:59:39 +01001345 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001346 if (request_gop == NULL) {
1347 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001348 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001349 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001350 }
1351 gop = request_gop;
1352
Wei Liub3f980b2013-08-26 12:59:38 +01001353 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001354
Ian Campbellf942dc22011-03-15 00:06:18 +00001355 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001356
Wei Liub3f980b2013-08-26 12:59:38 +01001357 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 break;
1359 }
1360
Wei Liub3f980b2013-08-26 12:59:38 +01001361 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001362}
1363
Ian Campbellf942dc22011-03-15 00:06:18 +00001364
Wei Liu73764192013-08-26 12:59:39 +01001365static int xenvif_tx_submit(struct xenvif *vif, int budget)
Wei Liub3f980b2013-08-26 12:59:38 +01001366{
1367 struct gnttab_copy *gop = vif->tx_copy_ops;
1368 struct sk_buff *skb;
1369 int work_done = 0;
1370
1371 while (work_done < budget &&
1372 (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001373 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001374 u16 pending_idx;
1375 unsigned data_len;
1376
1377 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001378 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001379
1380 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001381 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001382 netdev_dbg(vif->dev, "netback grant failed.\n");
1383 skb_shinfo(skb)->nr_frags = 0;
1384 kfree_skb(skb);
1385 continue;
1386 }
1387
1388 data_len = skb->len;
1389 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001390 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001391 data_len);
1392 if (data_len < txp->size) {
1393 /* Append the packet payload as a fragment. */
1394 txp->offset += data_len;
1395 txp->size -= data_len;
1396 } else {
1397 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001398 xenvif_idx_release(vif, pending_idx,
1399 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001400 }
1401
1402 if (txp->flags & XEN_NETTXF_csum_blank)
1403 skb->ip_summed = CHECKSUM_PARTIAL;
1404 else if (txp->flags & XEN_NETTXF_data_validated)
1405 skb->ip_summed = CHECKSUM_UNNECESSARY;
1406
Wei Liu73764192013-08-26 12:59:39 +01001407 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001408
1409 /*
1410 * If the initial fragment was < PKT_PROT_LEN then
1411 * pull through some bytes from the other fragments to
1412 * increase the linear region to PKT_PROT_LEN bytes.
1413 */
1414 if (skb_headlen(skb) < PKT_PROT_LEN && skb_is_nonlinear(skb)) {
1415 int target = min_t(int, skb->len, PKT_PROT_LEN);
1416 __pskb_pull_tail(skb, target - skb_headlen(skb));
1417 }
1418
1419 skb->dev = vif->dev;
1420 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001421 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001422
1423 if (checksum_setup(vif, skb)) {
1424 netdev_dbg(vif->dev,
1425 "Can't setup checksum in net_tx_action\n");
1426 kfree_skb(skb);
1427 continue;
1428 }
1429
Jason Wang40893fd2013-03-26 23:11:22 +00001430 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001431
Ian Campbellf942dc22011-03-15 00:06:18 +00001432 vif->dev->stats.rx_bytes += skb->len;
1433 vif->dev->stats.rx_packets++;
1434
Wei Liub3f980b2013-08-26 12:59:38 +01001435 work_done++;
1436
1437 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001438 }
Wei Liub3f980b2013-08-26 12:59:38 +01001439
1440 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001441}
1442
1443/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001444int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001445{
1446 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001447 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001448
Wei Liub3f980b2013-08-26 12:59:38 +01001449 if (unlikely(!tx_work_todo(vif)))
1450 return 0;
1451
Wei Liu73764192013-08-26 12:59:39 +01001452 nr_gops = xenvif_tx_build_gops(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001453
1454 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001455 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001456
Wei Liub3f980b2013-08-26 12:59:38 +01001457 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001458
Wei Liu73764192013-08-26 12:59:39 +01001459 work_done = xenvif_tx_submit(vif, nr_gops);
Wei Liub3f980b2013-08-26 12:59:38 +01001460
1461 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001462}
1463
Wei Liu73764192013-08-26 12:59:39 +01001464static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1465 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001466{
Ian Campbellf942dc22011-03-15 00:06:18 +00001467 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001468 pending_ring_idx_t head;
1469 u16 peek; /* peek into next tx request */
1470
Wei Liub3f980b2013-08-26 12:59:38 +01001471 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001472
1473 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001474 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001475 return;
1476
Wei Liub3f980b2013-08-26 12:59:38 +01001477 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001478
Wei Liu2810e5b2013-04-22 02:20:42 +00001479 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001480
Wei Liub3f980b2013-08-26 12:59:38 +01001481 BUG_ON(!pending_tx_is_head(vif, head));
1482 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001483
Wei Liu2810e5b2013-04-22 02:20:42 +00001484 do {
1485 pending_ring_idx_t index;
1486 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001487 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001488
Wei Liub3f980b2013-08-26 12:59:38 +01001489 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001490 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001491
Wei Liu2810e5b2013-04-22 02:20:42 +00001492 /* Setting any number other than
1493 * INVALID_PENDING_RING_IDX indicates this slot is
1494 * starting a new packet / ending a previous packet.
1495 */
1496 pending_tx_info->head = 0;
1497
Wei Liub3f980b2013-08-26 12:59:38 +01001498 index = pending_index(vif->pending_prod++);
1499 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001500
Wei Liub3f980b2013-08-26 12:59:38 +01001501 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001502
Wei Liub3f980b2013-08-26 12:59:38 +01001503 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001504
Wei Liub3f980b2013-08-26 12:59:38 +01001505 put_page(vif->mmap_pages[pending_idx]);
1506 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001507}
1508
Wei Liu2810e5b2013-04-22 02:20:42 +00001509
Ian Campbellf942dc22011-03-15 00:06:18 +00001510static void make_tx_response(struct xenvif *vif,
1511 struct xen_netif_tx_request *txp,
1512 s8 st)
1513{
1514 RING_IDX i = vif->tx.rsp_prod_pvt;
1515 struct xen_netif_tx_response *resp;
1516 int notify;
1517
1518 resp = RING_GET_RESPONSE(&vif->tx, i);
1519 resp->id = txp->id;
1520 resp->status = st;
1521
1522 if (txp->flags & XEN_NETTXF_extra_info)
1523 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1524
1525 vif->tx.rsp_prod_pvt = ++i;
1526 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1527 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001528 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001529}
1530
1531static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1532 u16 id,
1533 s8 st,
1534 u16 offset,
1535 u16 size,
1536 u16 flags)
1537{
1538 RING_IDX i = vif->rx.rsp_prod_pvt;
1539 struct xen_netif_rx_response *resp;
1540
1541 resp = RING_GET_RESPONSE(&vif->rx, i);
1542 resp->offset = offset;
1543 resp->flags = flags;
1544 resp->id = id;
1545 resp->status = (s16)size;
1546 if (st < 0)
1547 resp->status = (s16)st;
1548
1549 vif->rx.rsp_prod_pvt = ++i;
1550
1551 return resp;
1552}
1553
Wei Liub3f980b2013-08-26 12:59:38 +01001554static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001555{
Wei Liub3f980b2013-08-26 12:59:38 +01001556 return !skb_queue_empty(&vif->rx_queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001557}
1558
Wei Liub3f980b2013-08-26 12:59:38 +01001559static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001560{
1561
Wei Liub3f980b2013-08-26 12:59:38 +01001562 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1563 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1564 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001565 return 1;
1566
1567 return 0;
1568}
1569
Wei Liu73764192013-08-26 12:59:39 +01001570void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001571{
David Vrabelc9d63692011-09-29 16:53:31 +01001572 if (vif->tx.sring)
1573 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1574 vif->tx.sring);
1575 if (vif->rx.sring)
1576 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1577 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001578}
1579
Wei Liu73764192013-08-26 12:59:39 +01001580int xenvif_map_frontend_rings(struct xenvif *vif,
1581 grant_ref_t tx_ring_ref,
1582 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001583{
David Vrabelc9d63692011-09-29 16:53:31 +01001584 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001585 struct xen_netif_tx_sring *txs;
1586 struct xen_netif_rx_sring *rxs;
1587
1588 int err = -ENOMEM;
1589
David Vrabelc9d63692011-09-29 16:53:31 +01001590 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1591 tx_ring_ref, &addr);
1592 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001593 goto err;
1594
David Vrabelc9d63692011-09-29 16:53:31 +01001595 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001596 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1597
David Vrabelc9d63692011-09-29 16:53:31 +01001598 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1599 rx_ring_ref, &addr);
1600 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001601 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001602
David Vrabelc9d63692011-09-29 16:53:31 +01001603 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001604 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1605
David Vrabelc9d63692011-09-29 16:53:31 +01001606 vif->rx_req_cons_peek = 0;
1607
Ian Campbellf942dc22011-03-15 00:06:18 +00001608 return 0;
1609
1610err:
Wei Liu73764192013-08-26 12:59:39 +01001611 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001612 return err;
1613}
1614
Wei Liu73764192013-08-26 12:59:39 +01001615int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001616{
1617 struct xenvif *vif = data;
1618
1619 while (!kthread_should_stop()) {
1620 wait_event_interruptible(vif->wq,
1621 rx_work_todo(vif) ||
1622 kthread_should_stop());
1623 if (kthread_should_stop())
1624 break;
1625
1626 if (rx_work_todo(vif))
Wei Liu73764192013-08-26 12:59:39 +01001627 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001628
1629 cond_resched();
1630 }
1631
1632 return 0;
1633}
1634
Ian Campbellf942dc22011-03-15 00:06:18 +00001635static int __init netback_init(void)
1636{
Ian Campbellf942dc22011-03-15 00:06:18 +00001637 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001638
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001639 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001640 return -ENODEV;
1641
Wei Liu37641492013-05-02 00:43:59 +00001642 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001643 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1644 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001645 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001646 }
1647
Ian Campbellf942dc22011-03-15 00:06:18 +00001648 rc = xenvif_xenbus_init();
1649 if (rc)
1650 goto failed_init;
1651
1652 return 0;
1653
1654failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001655 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001656}
1657
1658module_init(netback_init);
1659
Wei Liub103f352013-05-16 23:26:11 +00001660static void __exit netback_fini(void)
1661{
Wei Liub103f352013-05-16 23:26:11 +00001662 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001663}
1664module_exit(netback_fini);
1665
Ian Campbellf942dc22011-03-15 00:06:18 +00001666MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001667MODULE_ALIAS("xen-backend:vif");