blob: 611aebee4583679f4377658b65691a4e8fd0e29e [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
7 *
8 * Copyright (c) 2002-2005, K A Fraser
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include "common.h"
36
37#include <linux/kthread.h>
38#include <linux/if_vlan.h>
39#include <linux/udp.h>
40
41#include <net/tcp.h>
Andy Whitcroftae5e8122013-11-25 16:52:34 +000042#include <net/ip6_checksum.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000043
Stefano Stabellinica981632012-08-08 17:21:23 +000044#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000045#include <xen/events.h>
46#include <xen/interface/memory.h>
47
48#include <asm/xen/hypercall.h>
49#include <asm/xen/page.h>
50
Wei Liue1f00a692013-05-22 06:34:45 +000051/* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
53 * enabled by default.
54 */
55bool separate_tx_rx_irq = 1;
56module_param(separate_tx_rx_irq, bool, 0644);
57
Wei Liu2810e5b2013-04-22 02:20:42 +000058/*
59 * This is the maximum slots a skb can have. If a guest sends a skb
60 * which exceeds this limit it is considered malicious.
61 */
Wei Liu37641492013-05-02 00:43:59 +000062#define FATAL_SKB_SLOTS_DEFAULT 20
63static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
64module_param(fatal_skb_slots, uint, 0444);
65
66/*
67 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
68 * the maximum slots a valid packet can use. Now this value is defined
69 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
70 * all backend.
71 */
72#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
Wei Liu2810e5b2013-04-22 02:20:42 +000073
Wei Liu2810e5b2013-04-22 02:20:42 +000074/*
75 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
76 * one or more merged tx requests, otherwise it is the continuation of
77 * previous tx request.
78 */
Wei Liub3f980b2013-08-26 12:59:38 +010079static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
Wei Liu2810e5b2013-04-22 02:20:42 +000080{
Wei Liub3f980b2013-08-26 12:59:38 +010081 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
Wei Liu2810e5b2013-04-22 02:20:42 +000082}
83
Wei Liu73764192013-08-26 12:59:39 +010084static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
85 u8 status);
86
Ian Campbellf942dc22011-03-15 00:06:18 +000087static void make_tx_response(struct xenvif *vif,
88 struct xen_netif_tx_request *txp,
89 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010090
91static inline int tx_work_todo(struct xenvif *vif);
92static inline int rx_work_todo(struct xenvif *vif);
93
Ian Campbellf942dc22011-03-15 00:06:18 +000094static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
95 u16 id,
96 s8 st,
97 u16 offset,
98 u16 size,
99 u16 flags);
100
Wei Liub3f980b2013-08-26 12:59:38 +0100101static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000102 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000103{
Wei Liub3f980b2013-08-26 12:59:38 +0100104 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000105}
106
Wei Liub3f980b2013-08-26 12:59:38 +0100107static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000108 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000109{
Wei Liub3f980b2013-08-26 12:59:38 +0100110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000111}
112
Paul Durrant2eba61d2013-10-16 17:50:29 +0100113/* This is a miniumum size for the linear area to avoid lots of
114 * calls to __pskb_pull_tail() as we set up checksum offsets. The
115 * value 128 was chosen as it covers all IPv4 and most likely
116 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000117 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100118#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000119
Ian Campbellea066ad2011-10-05 00:28:46 +0000120static u16 frag_get_pending_idx(skb_frag_t *frag)
121{
122 return (u16)frag->page_offset;
123}
124
125static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
126{
127 frag->page_offset = pending_idx;
128}
129
Ian Campbellf942dc22011-03-15 00:06:18 +0000130static inline pending_ring_idx_t pending_index(unsigned i)
131{
132 return i & (MAX_PENDING_REQS-1);
133}
134
Wei Liub3f980b2013-08-26 12:59:38 +0100135static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000136{
137 return MAX_PENDING_REQS -
Wei Liub3f980b2013-08-26 12:59:38 +0100138 vif->pending_prod + vif->pending_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000139}
140
Paul Durrantca2f09f2013-12-06 16:36:07 +0000141bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000142{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000143 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000144
Paul Durrantca2f09f2013-12-06 16:36:07 +0000145 do {
146 prod = vif->rx.sring->req_prod;
147 cons = vif->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000148
Paul Durrantca2f09f2013-12-06 16:36:07 +0000149 if (prod - cons >= needed)
150 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000151
Paul Durrantca2f09f2013-12-06 16:36:07 +0000152 vif->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000153
Paul Durrantca2f09f2013-12-06 16:36:07 +0000154 /* Make sure event is visible before we check prod
155 * again.
156 */
157 mb();
158 } while (vif->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000159
Paul Durrantca2f09f2013-12-06 16:36:07 +0000160 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000161}
162
163/*
164 * Returns true if we should start a new receive buffer instead of
165 * adding 'size' bytes to a buffer which currently contains 'offset'
166 * bytes.
167 */
168static bool start_new_rx_buffer(int offset, unsigned long size, int head)
169{
170 /* simple case: we have completely filled the current buffer. */
171 if (offset == MAX_BUFFER_OFFSET)
172 return true;
173
174 /*
175 * complex case: start a fresh buffer if the current frag
176 * would overflow the current buffer but only if:
177 * (i) this frag would fit completely in the next buffer
178 * and (ii) there is already some data in the current buffer
179 * and (iii) this is not the head buffer.
180 *
181 * Where:
182 * - (i) stops us splitting a frag into two copies
183 * unless the frag is too large for a single buffer.
184 * - (ii) stops us from leaving a buffer pointlessly empty.
185 * - (iii) stops us leaving the first buffer
186 * empty. Strictly speaking this is already covered
187 * by (ii) but is explicitly checked because
188 * netfront relies on the first buffer being
189 * non-empty and can crash otherwise.
190 *
191 * This means we will effectively linearise small
192 * frags but do not needlessly split large buffers
193 * into multiple copies tend to give large frags their
194 * own buffers as before.
195 */
196 if ((offset + size > MAX_BUFFER_OFFSET) &&
197 (size <= MAX_BUFFER_OFFSET) && offset && !head)
198 return true;
199
200 return false;
201}
202
Ian Campbellf942dc22011-03-15 00:06:18 +0000203struct netrx_pending_operations {
204 unsigned copy_prod, copy_cons;
205 unsigned meta_prod, meta_cons;
206 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100207 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000208 int copy_off;
209 grant_ref_t copy_gref;
210};
211
Wei Liub3f980b2013-08-26 12:59:38 +0100212static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
213 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000214{
Wei Liub3f980b2013-08-26 12:59:38 +0100215 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000216 struct xen_netif_rx_request *req;
217
218 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
219
220 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100221 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000222 meta->gso_size = 0;
223 meta->size = 0;
224 meta->id = req->id;
225
226 npo->copy_off = 0;
227 npo->copy_gref = req->gref;
228
229 return meta;
230}
231
Wei Liu33bc8012013-10-08 10:54:21 +0100232/*
233 * Set up the grant operations for this fragment. If it's a flipping
234 * interface, we also set up the unmap request from here.
235 */
Wei Liu73764192013-08-26 12:59:39 +0100236static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
237 struct netrx_pending_operations *npo,
238 struct page *page, unsigned long size,
Wei Liu33bc8012013-10-08 10:54:21 +0100239 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000240{
241 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100242 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000243 unsigned long bytes;
Paul Durrant82cada22013-10-16 17:50:32 +0100244 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000245
246 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000247 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000248
249 meta = npo->meta + npo->meta_prod - 1;
250
Ian Campbell6a8ed462012-10-10 03:48:42 +0000251 /* Skip unused frames from start of page */
252 page += offset >> PAGE_SHIFT;
253 offset &= ~PAGE_MASK;
254
Ian Campbellf942dc22011-03-15 00:06:18 +0000255 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000256 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000257 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
258
Ian Campbell6a8ed462012-10-10 03:48:42 +0000259 bytes = PAGE_SIZE - offset;
260
261 if (bytes > size)
262 bytes = size;
263
Wei Liu33bc8012013-10-08 10:54:21 +0100264 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000265 /*
266 * Netfront requires there to be some data in the head
267 * buffer.
268 */
Wei Liu33bc8012013-10-08 10:54:21 +0100269 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000270
271 meta = get_next_rx_buffer(vif, npo);
272 }
273
Ian Campbellf942dc22011-03-15 00:06:18 +0000274 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
275 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
276
277 copy_gop = npo->copy + npo->copy_prod++;
278 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100279 copy_gop->len = bytes;
280
Wei Liu43e9d192013-08-26 12:59:37 +0100281 copy_gop->source.domid = DOMID_SELF;
282 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000283 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000284
Wei Liub3f980b2013-08-26 12:59:38 +0100285 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000286 copy_gop->dest.offset = npo->copy_off;
287 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000288
289 npo->copy_off += bytes;
290 meta->size += bytes;
291
292 offset += bytes;
293 size -= bytes;
294
Ian Campbell6a8ed462012-10-10 03:48:42 +0000295 /* Next frame */
296 if (offset == PAGE_SIZE && size) {
297 BUG_ON(!PageCompound(page));
298 page++;
299 offset = 0;
300 }
301
Ian Campbellf942dc22011-03-15 00:06:18 +0000302 /* Leave a gap for the GSO descriptor. */
Paul Durrant82cada22013-10-16 17:50:32 +0100303 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
304 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
305 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
306 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
307 else
308 gso_type = XEN_NETIF_GSO_TYPE_NONE;
309
310 if (*head && ((1 << gso_type) & vif->gso_mask))
Ian Campbellf942dc22011-03-15 00:06:18 +0000311 vif->rx.req_cons++;
312
Wei Liu33bc8012013-10-08 10:54:21 +0100313 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000314
315 }
316}
317
318/*
319 * Prepare an SKB to be transmitted to the frontend.
320 *
321 * This function is responsible for allocating grant operations, meta
322 * structures, etc.
323 *
324 * It returns the number of meta structures consumed. The number of
325 * ring slots used is always equal to the number of meta slots used
326 * plus the number of GSO descriptors used. Currently, we use either
327 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
328 * frontend-side LRO).
329 */
Wei Liu73764192013-08-26 12:59:39 +0100330static int xenvif_gop_skb(struct sk_buff *skb,
331 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000332{
333 struct xenvif *vif = netdev_priv(skb->dev);
334 int nr_frags = skb_shinfo(skb)->nr_frags;
335 int i;
336 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100337 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000338 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100339 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000340 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100341 int gso_type;
342 int gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000343
344 old_meta_prod = npo->meta_prod;
345
Paul Durrant82cada22013-10-16 17:50:32 +0100346 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
347 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
348 gso_size = skb_shinfo(skb)->gso_size;
349 } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
350 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
351 gso_size = skb_shinfo(skb)->gso_size;
352 } else {
353 gso_type = XEN_NETIF_GSO_TYPE_NONE;
354 gso_size = 0;
355 }
356
Ian Campbellf942dc22011-03-15 00:06:18 +0000357 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000358 if ((1 << gso_type) & vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
360 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100361 meta->gso_type = gso_type;
362 meta->gso_size = gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000363 meta->size = 0;
364 meta->id = req->id;
365 }
366
367 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
368 meta = npo->meta + npo->meta_prod++;
369
Paul Durrant82cada22013-10-16 17:50:32 +0100370 if ((1 << gso_type) & vif->gso_mask) {
371 meta->gso_type = gso_type;
372 meta->gso_size = gso_size;
373 } else {
374 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100376 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000377
378 meta->size = 0;
379 meta->id = req->id;
380 npo->copy_off = 0;
381 npo->copy_gref = req->gref;
382
383 data = skb->data;
384 while (data < skb_tail_pointer(skb)) {
385 unsigned int offset = offset_in_page(data);
386 unsigned int len = PAGE_SIZE - offset;
387
388 if (data + len > skb_tail_pointer(skb))
389 len = skb_tail_pointer(skb) - data;
390
Wei Liu73764192013-08-26 12:59:39 +0100391 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100392 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000393 data += len;
394 }
395
396 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100397 xenvif_gop_frag_copy(vif, skb, npo,
398 skb_frag_page(&skb_shinfo(skb)->frags[i]),
399 skb_frag_size(&skb_shinfo(skb)->frags[i]),
400 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100401 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000402 }
403
404 return npo->meta_prod - old_meta_prod;
405}
406
407/*
Wei Liu73764192013-08-26 12:59:39 +0100408 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000409 * used to set up the operations on the top of
410 * netrx_pending_operations, which have since been done. Check that
411 * they didn't give any errors and advance over them.
412 */
Wei Liu73764192013-08-26 12:59:39 +0100413static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
414 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000415{
416 struct gnttab_copy *copy_op;
417 int status = XEN_NETIF_RSP_OKAY;
418 int i;
419
420 for (i = 0; i < nr_meta_slots; i++) {
421 copy_op = npo->copy + npo->copy_cons++;
422 if (copy_op->status != GNTST_okay) {
423 netdev_dbg(vif->dev,
424 "Bad status %d from copy to DOM%d.\n",
425 copy_op->status, vif->domid);
426 status = XEN_NETIF_RSP_ERROR;
427 }
428 }
429
430 return status;
431}
432
Wei Liu73764192013-08-26 12:59:39 +0100433static void xenvif_add_frag_responses(struct xenvif *vif, int status,
434 struct xenvif_rx_meta *meta,
435 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000436{
437 int i;
438 unsigned long offset;
439
440 /* No fragments used */
441 if (nr_meta_slots <= 1)
442 return;
443
444 nr_meta_slots--;
445
446 for (i = 0; i < nr_meta_slots; i++) {
447 int flags;
448 if (i == nr_meta_slots - 1)
449 flags = 0;
450 else
451 flags = XEN_NETRXF_more_data;
452
453 offset = 0;
454 make_rx_response(vif, meta[i].id, status, offset,
455 meta[i].size, flags);
456 }
457}
458
Wei Liu33bc8012013-10-08 10:54:21 +0100459struct skb_cb_overlay {
460 int meta_slots_used;
461};
462
Paul Durrantca2f09f2013-12-06 16:36:07 +0000463void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000464{
Wei Liub3f980b2013-08-26 12:59:38 +0100465 wake_up(&vif->wq);
466}
467
Paul Durrantca2f09f2013-12-06 16:36:07 +0000468static void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100469{
Ian Campbellf942dc22011-03-15 00:06:18 +0000470 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000471 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000472 struct xen_netif_rx_response *resp;
473 struct sk_buff_head rxq;
474 struct sk_buff *skb;
475 LIST_HEAD(notify);
476 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000477 unsigned long offset;
478 struct skb_cb_overlay *sco;
Wei Liub3f980b2013-08-26 12:59:38 +0100479 int need_to_notify = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000480
481 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100482 .copy = vif->grant_copy_op,
483 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000484 };
485
486 skb_queue_head_init(&rxq);
487
Wei Liub3f980b2013-08-26 12:59:38 +0100488 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Paul Durrantca2f09f2013-12-06 16:36:07 +0000489 int max_slots_needed;
490 int i;
491
492 /* We need a cheap worse case estimate for the number of
493 * slots we'll use.
494 */
495
496 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
497 skb_headlen(skb),
498 PAGE_SIZE);
499 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
500 unsigned int size;
501 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
502 max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
503 }
504 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
505 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
506 max_slots_needed++;
507
508 /* If the skb may not fit then bail out now */
509 if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
510 skb_queue_head(&vif->rx_queue, skb);
511 need_to_notify = 1;
512 break;
513 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000514
515 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100516 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000517 BUG_ON(sco->meta_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000518
519 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000520 }
521
Wei Liub3f980b2013-08-26 12:59:38 +0100522 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000523
524 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000525 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000526
Wei Liub3f980b2013-08-26 12:59:38 +0100527 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
528 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000529
530 while ((skb = __skb_dequeue(&rxq)) != NULL) {
531 sco = (struct skb_cb_overlay *)skb->cb;
532
Paul Durrant82cada22013-10-16 17:50:32 +0100533 if ((1 << vif->meta[npo.meta_cons].gso_type) &
534 vif->gso_prefix_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000535 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100536 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000537
538 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
539
Wei Liub3f980b2013-08-26 12:59:38 +0100540 resp->offset = vif->meta[npo.meta_cons].gso_size;
541 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000542 resp->status = sco->meta_slots_used;
543
544 npo.meta_cons++;
545 sco->meta_slots_used--;
546 }
547
548
549 vif->dev->stats.tx_bytes += skb->len;
550 vif->dev->stats.tx_packets++;
551
Wei Liu73764192013-08-26 12:59:39 +0100552 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000553
554 if (sco->meta_slots_used == 1)
555 flags = 0;
556 else
557 flags = XEN_NETRXF_more_data;
558
559 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
560 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
561 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
562 /* remote but checksummed. */
563 flags |= XEN_NETRXF_data_validated;
564
565 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100566 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000567 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100568 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000569 flags);
570
Paul Durrant82cada22013-10-16 17:50:32 +0100571 if ((1 << vif->meta[npo.meta_cons].gso_type) &
572 vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000573 struct xen_netif_extra_info *gso =
574 (struct xen_netif_extra_info *)
575 RING_GET_RESPONSE(&vif->rx,
576 vif->rx.rsp_prod_pvt++);
577
578 resp->flags |= XEN_NETRXF_extra_info;
579
Paul Durrant82cada22013-10-16 17:50:32 +0100580 gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
Wei Liub3f980b2013-08-26 12:59:38 +0100581 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000582 gso->u.gso.pad = 0;
583 gso->u.gso.features = 0;
584
585 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
586 gso->flags = 0;
587 }
588
Wei Liu73764192013-08-26 12:59:39 +0100589 xenvif_add_frag_responses(vif, status,
590 vif->meta + npo.meta_cons + 1,
591 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000592
593 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000594
Wei Liub3f980b2013-08-26 12:59:38 +0100595 if (ret)
596 need_to_notify = 1;
597
Ian Campbellf942dc22011-03-15 00:06:18 +0000598 npo.meta_cons += sco->meta_slots_used;
599 dev_kfree_skb(skb);
600 }
601
Paul Durrantca2f09f2013-12-06 16:36:07 +0000602done:
Wei Liub3f980b2013-08-26 12:59:38 +0100603 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000604 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000605}
606
Wei Liu73764192013-08-26 12:59:39 +0100607void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000608{
609 int more_to_do;
610
611 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
612
613 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100614 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000615}
616
617static void tx_add_credit(struct xenvif *vif)
618{
619 unsigned long max_burst, max_credit;
620
621 /*
622 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
623 * Otherwise the interface can seize up due to insufficient credit.
624 */
625 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
626 max_burst = min(max_burst, 131072UL);
627 max_burst = max(max_burst, vif->credit_bytes);
628
629 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
630 max_credit = vif->remaining_credit + vif->credit_bytes;
631 if (max_credit < vif->remaining_credit)
632 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
633
634 vif->remaining_credit = min(max_credit, max_burst);
635}
636
637static void tx_credit_callback(unsigned long data)
638{
639 struct xenvif *vif = (struct xenvif *)data;
640 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100641 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000642}
643
Wei Liu73764192013-08-26 12:59:39 +0100644static void xenvif_tx_err(struct xenvif *vif,
645 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000646{
647 RING_IDX cons = vif->tx.req_cons;
648
649 do {
650 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000651 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000652 break;
653 txp = RING_GET_REQUEST(&vif->tx, cons++);
654 } while (1);
655 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000656}
657
Wei Liu73764192013-08-26 12:59:39 +0100658static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000659{
660 netdev_err(vif->dev, "fatal error; disabling device\n");
661 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000662}
663
Wei Liu73764192013-08-26 12:59:39 +0100664static int xenvif_count_requests(struct xenvif *vif,
665 struct xen_netif_tx_request *first,
666 struct xen_netif_tx_request *txp,
667 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000668{
669 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000670 int slots = 0;
671 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000672 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000673
674 if (!(first->flags & XEN_NETTXF_more_data))
675 return 0;
676
677 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000678 struct xen_netif_tx_request dropped_tx = { 0 };
679
Wei Liu2810e5b2013-04-22 02:20:42 +0000680 if (slots >= work_to_do) {
681 netdev_err(vif->dev,
682 "Asked for %d slots but exceeds this limit\n",
683 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100684 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000685 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000686 }
687
Wei Liu2810e5b2013-04-22 02:20:42 +0000688 /* This guest is really using too many slots and
689 * considered malicious.
690 */
Wei Liu37641492013-05-02 00:43:59 +0000691 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000692 netdev_err(vif->dev,
693 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000694 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100695 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000696 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000697 }
698
Wei Liu2810e5b2013-04-22 02:20:42 +0000699 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000700 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
701 * the historical MAX_SKB_FRAGS value 18 to honor the
702 * same behavior as before. Any packet using more than
703 * 18 slots but less than fatal_skb_slots slots is
704 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000705 */
Wei Liu37641492013-05-02 00:43:59 +0000706 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000707 if (net_ratelimit())
708 netdev_dbg(vif->dev,
709 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000710 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000711 drop_err = -E2BIG;
712 }
713
Wei Liu59ccb4e2013-05-02 00:43:58 +0000714 if (drop_err)
715 txp = &dropped_tx;
716
Wei Liu2810e5b2013-04-22 02:20:42 +0000717 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000718 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000719
720 /* If the guest submitted a frame >= 64 KiB then
721 * first->size overflowed and following slots will
722 * appear to be larger than the frame.
723 *
724 * This cannot be fatal error as there are buggy
725 * frontends that do this.
726 *
727 * Consume all slots and drop the packet.
728 */
729 if (!drop_err && txp->size > first->size) {
730 if (net_ratelimit())
731 netdev_dbg(vif->dev,
732 "Invalid tx request, slot size %u > remaining size %u\n",
733 txp->size, first->size);
734 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000735 }
736
737 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000738 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000739
740 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000741 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000742 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100743 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000744 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000745 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000746
747 more_data = txp->flags & XEN_NETTXF_more_data;
748
749 if (!drop_err)
750 txp++;
751
752 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000753
754 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100755 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000756 return drop_err;
757 }
758
759 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000760}
761
Wei Liu73764192013-08-26 12:59:39 +0100762static struct page *xenvif_alloc_page(struct xenvif *vif,
763 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000764{
765 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100766
767 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000768 if (!page)
769 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100770 vif->mmap_pages[pending_idx] = page;
771
Ian Campbellf942dc22011-03-15 00:06:18 +0000772 return page;
773}
774
Wei Liu73764192013-08-26 12:59:39 +0100775static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
776 struct sk_buff *skb,
777 struct xen_netif_tx_request *txp,
778 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000779{
780 struct skb_shared_info *shinfo = skb_shinfo(skb);
781 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000782 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000783 u16 head_idx = 0;
784 int slot, start;
785 struct page *page;
786 pending_ring_idx_t index, start_idx = 0;
787 uint16_t dst_offset;
788 unsigned int nr_slots;
789 struct pending_tx_info *first = NULL;
790
791 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000792 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000793 */
794 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000795
796 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000797 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000798
Wei Liu2810e5b2013-04-22 02:20:42 +0000799 /* Coalesce tx requests, at this point the packet passed in
800 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100801 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000802 */
803 for (shinfo->nr_frags = slot = start; slot < nr_slots;
804 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000805 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100806 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000807
Wei Liub3f980b2013-08-26 12:59:38 +0100808 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000809 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000810 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000811
Wei Liu2810e5b2013-04-22 02:20:42 +0000812 dst_offset = 0;
813 first = NULL;
814 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
815 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000816
Wei Liu2810e5b2013-04-22 02:20:42 +0000817 gop->source.u.ref = txp->gref;
818 gop->source.domid = vif->domid;
819 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000820
Wei Liu2810e5b2013-04-22 02:20:42 +0000821 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000822
Wei Liu2810e5b2013-04-22 02:20:42 +0000823 gop->dest.offset = dst_offset;
824 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000825
Wei Liu2810e5b2013-04-22 02:20:42 +0000826 if (dst_offset + txp->size > PAGE_SIZE) {
827 /* This page can only merge a portion
828 * of tx request. Do not increment any
829 * pointer / counter here. The txp
830 * will be dealt with in future
831 * rounds, eventually hitting the
832 * `else` branch.
833 */
834 gop->len = PAGE_SIZE - dst_offset;
835 txp->offset += gop->len;
836 txp->size -= gop->len;
837 dst_offset += gop->len; /* quit loop */
838 } else {
839 /* This tx request can be merged in the page */
840 gop->len = txp->size;
841 dst_offset += gop->len;
842
Wei Liub3f980b2013-08-26 12:59:38 +0100843 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000844
Wei Liub3f980b2013-08-26 12:59:38 +0100845 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000846
847 memcpy(&pending_tx_info[pending_idx].req, txp,
848 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000849
850 /* Poison these fields, corresponding
851 * fields for head tx req will be set
852 * to correct values after the loop.
853 */
Wei Liub3f980b2013-08-26 12:59:38 +0100854 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000855 pending_tx_info[pending_idx].head =
856 INVALID_PENDING_RING_IDX;
857
858 if (!first) {
859 first = &pending_tx_info[pending_idx];
860 start_idx = index;
861 head_idx = pending_idx;
862 }
863
864 txp++;
865 slot++;
866 }
867
868 gop++;
869 }
870
871 first->req.offset = 0;
872 first->req.size = dst_offset;
873 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100874 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000875 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000876 }
877
Wei Liu2810e5b2013-04-22 02:20:42 +0000878 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
879
Ian Campbellf942dc22011-03-15 00:06:18 +0000880 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000881err:
882 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000883 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100884 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000885 frag_get_pending_idx(&frags[shinfo->nr_frags]),
886 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000887 }
888 /* The head too, if necessary. */
889 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100890 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000891
892 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000893}
894
Wei Liu73764192013-08-26 12:59:39 +0100895static int xenvif_tx_check_gop(struct xenvif *vif,
896 struct sk_buff *skb,
897 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000898{
899 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000900 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000901 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000902 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000903 int nr_frags = shinfo->nr_frags;
904 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000905 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000906
907 /* Check status of header. */
908 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000909 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100910 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000911
912 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000913 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000914
915 for (i = start; i < nr_frags; i++) {
916 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000917 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000918
Ian Campbellea066ad2011-10-05 00:28:46 +0000919 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100920 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000921 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000922
923 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000924 do {
925 newerr = (++gop)->status;
926 if (newerr)
927 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100928 peek = vif->pending_ring[pending_index(++head)];
929 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000930
Ian Campbellf942dc22011-03-15 00:06:18 +0000931 if (likely(!newerr)) {
932 /* Had a previous error? Invalidate this fragment. */
933 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100934 xenvif_idx_release(vif, pending_idx,
935 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000936 continue;
937 }
938
939 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +0100940 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000941
942 /* Not the first error? Preceding frags already invalidated. */
943 if (err)
944 continue;
945
946 /* First error: invalidate header and preceding fragments. */
947 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +0100948 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000949 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000950 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +0100951 xenvif_idx_release(vif, pending_idx,
952 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000953 }
954
955 /* Remember the error: invalidate all subsequent fragments. */
956 err = newerr;
957 }
958
959 *gopp = gop + 1;
960 return err;
961}
962
Wei Liu73764192013-08-26 12:59:39 +0100963static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000964{
965 struct skb_shared_info *shinfo = skb_shinfo(skb);
966 int nr_frags = shinfo->nr_frags;
967 int i;
968
969 for (i = 0; i < nr_frags; i++) {
970 skb_frag_t *frag = shinfo->frags + i;
971 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000972 struct page *page;
973 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +0000974
Ian Campbellea066ad2011-10-05 00:28:46 +0000975 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +0000976
Wei Liub3f980b2013-08-26 12:59:38 +0100977 txp = &vif->pending_tx_info[pending_idx].req;
978 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +0000979 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +0000980 skb->len += txp->size;
981 skb->data_len += txp->size;
982 skb->truesize += txp->size;
983
Wei Liu73764192013-08-26 12:59:39 +0100984 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +0100985 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +0100986 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000987 }
988}
989
Wei Liu73764192013-08-26 12:59:39 +0100990static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +0000991 struct xen_netif_extra_info *extras,
992 int work_to_do)
993{
994 struct xen_netif_extra_info extra;
995 RING_IDX cons = vif->tx.req_cons;
996
997 do {
998 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +0000999 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001000 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001001 return -EBADR;
1002 }
1003
1004 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1005 sizeof(extra));
1006 if (unlikely(!extra.type ||
1007 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1008 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001009 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001010 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001011 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001012 return -EINVAL;
1013 }
1014
1015 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1016 vif->tx.req_cons = ++cons;
1017 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1018
1019 return work_to_do;
1020}
1021
Wei Liu73764192013-08-26 12:59:39 +01001022static int xenvif_set_skb_gso(struct xenvif *vif,
1023 struct sk_buff *skb,
1024 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001025{
1026 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001027 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001028 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001029 return -EINVAL;
1030 }
1031
Paul Durranta9468582013-10-16 17:50:31 +01001032 switch (gso->u.gso.type) {
1033 case XEN_NETIF_GSO_TYPE_TCPV4:
1034 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1035 break;
1036 case XEN_NETIF_GSO_TYPE_TCPV6:
1037 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1038 break;
1039 default:
Ian Campbell488562862013-02-06 23:41:35 +00001040 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001041 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001042 return -EINVAL;
1043 }
1044
1045 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001046 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001047
1048 return 0;
1049}
1050
Paul Durrant1431fb32013-12-03 17:39:29 +00001051static inline int maybe_pull_tail(struct sk_buff *skb, unsigned int len,
1052 unsigned int max)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001053{
Paul Durrant1431fb32013-12-03 17:39:29 +00001054 if (skb_headlen(skb) >= len)
1055 return 0;
1056
1057 /* If we need to pullup then pullup to the max, so we
1058 * won't need to do it again.
1059 */
1060 if (max > skb->len)
1061 max = skb->len;
1062
1063 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
1064 return -ENOMEM;
1065
1066 if (skb_headlen(skb) < len)
1067 return -EPROTO;
1068
1069 return 0;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001070}
1071
Paul Durrant1431fb32013-12-03 17:39:29 +00001072/* This value should be large enough to cover a tagged ethernet header plus
1073 * maximally sized IP and TCP or UDP headers.
1074 */
1075#define MAX_IP_HDR_LEN 128
1076
Paul Durrant2eba61d2013-10-16 17:50:29 +01001077static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1078 int recalculate_partial_csum)
1079{
Paul Durrant2eba61d2013-10-16 17:50:29 +01001080 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001081 bool fragment;
1082 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001083
Paul Durrant1431fb32013-12-03 17:39:29 +00001084 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001085
Paul Durrant1431fb32013-12-03 17:39:29 +00001086 err = maybe_pull_tail(skb,
1087 sizeof(struct iphdr),
1088 MAX_IP_HDR_LEN);
1089 if (err < 0)
1090 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001091
Paul Durrant1431fb32013-12-03 17:39:29 +00001092 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
1093 fragment = true;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001094
Paul Durrant1431fb32013-12-03 17:39:29 +00001095 off = ip_hdrlen(skb);
1096
1097 err = -EPROTO;
1098
Wei Yongjun7022ef82013-12-16 10:45:05 +08001099 if (fragment)
1100 goto out;
1101
Paul Durrant1431fb32013-12-03 17:39:29 +00001102 switch (ip_hdr(skb)->protocol) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001103 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001104 err = maybe_pull_tail(skb,
1105 off + sizeof(struct tcphdr),
1106 MAX_IP_HDR_LEN);
1107 if (err < 0)
1108 goto out;
1109
Paul Durrant2eba61d2013-10-16 17:50:29 +01001110 if (!skb_partial_csum_set(skb, off,
1111 offsetof(struct tcphdr, check)))
1112 goto out;
1113
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001114 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001115 tcp_hdr(skb)->check =
1116 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1117 ip_hdr(skb)->daddr,
1118 skb->len - off,
1119 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001120 break;
1121 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001122 err = maybe_pull_tail(skb,
1123 off + sizeof(struct udphdr),
1124 MAX_IP_HDR_LEN);
1125 if (err < 0)
1126 goto out;
1127
Paul Durrant2eba61d2013-10-16 17:50:29 +01001128 if (!skb_partial_csum_set(skb, off,
1129 offsetof(struct udphdr, check)))
1130 goto out;
1131
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001132 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001133 udp_hdr(skb)->check =
1134 ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
1135 ip_hdr(skb)->daddr,
1136 skb->len - off,
1137 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001138 break;
1139 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001140 goto out;
1141 }
1142
1143 err = 0;
1144
1145out:
1146 return err;
1147}
1148
Paul Durrant1431fb32013-12-03 17:39:29 +00001149/* This value should be large enough to cover a tagged ethernet header plus
1150 * an IPv6 header, all options, and a maximal TCP or UDP header.
1151 */
1152#define MAX_IPV6_HDR_LEN 256
1153
1154#define OPT_HDR(type, skb, off) \
1155 (type *)(skb_network_header(skb) + (off))
1156
Paul Durrant2eba61d2013-10-16 17:50:29 +01001157static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1158 int recalculate_partial_csum)
1159{
Paul Durrant1431fb32013-12-03 17:39:29 +00001160 int err;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001161 u8 nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001162 unsigned int off;
Paul Durrant1431fb32013-12-03 17:39:29 +00001163 unsigned int len;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001164 bool fragment;
1165 bool done;
1166
Paul Durrant1431fb32013-12-03 17:39:29 +00001167 fragment = false;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001168 done = false;
1169
1170 off = sizeof(struct ipv6hdr);
1171
Paul Durrant1431fb32013-12-03 17:39:29 +00001172 err = maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
1173 if (err < 0)
1174 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001175
Paul Durrant1431fb32013-12-03 17:39:29 +00001176 nexthdr = ipv6_hdr(skb)->nexthdr;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001177
Paul Durrant1431fb32013-12-03 17:39:29 +00001178 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
1179 while (off <= len && !done) {
Paul Durrant2eba61d2013-10-16 17:50:29 +01001180 switch (nexthdr) {
1181 case IPPROTO_DSTOPTS:
1182 case IPPROTO_HOPOPTS:
1183 case IPPROTO_ROUTING: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001184 struct ipv6_opt_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001185
Paul Durrant1431fb32013-12-03 17:39:29 +00001186 err = maybe_pull_tail(skb,
1187 off +
1188 sizeof(struct ipv6_opt_hdr),
1189 MAX_IPV6_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 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001194 nexthdr = hp->nexthdr;
1195 off += ipv6_optlen(hp);
1196 break;
1197 }
1198 case IPPROTO_AH: {
Paul Durrant1431fb32013-12-03 17:39:29 +00001199 struct ip_auth_hdr *hp;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001200
Paul Durrant1431fb32013-12-03 17:39:29 +00001201 err = maybe_pull_tail(skb,
1202 off +
1203 sizeof(struct ip_auth_hdr),
1204 MAX_IPV6_HDR_LEN);
1205 if (err < 0)
1206 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001207
Paul Durrant1431fb32013-12-03 17:39:29 +00001208 hp = OPT_HDR(struct ip_auth_hdr, skb, off);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001209 nexthdr = hp->nexthdr;
Paul Durrant1431fb32013-12-03 17:39:29 +00001210 off += ipv6_authlen(hp);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001211 break;
1212 }
Paul Durrant1431fb32013-12-03 17:39:29 +00001213 case IPPROTO_FRAGMENT: {
1214 struct frag_hdr *hp;
1215
1216 err = maybe_pull_tail(skb,
1217 off +
1218 sizeof(struct frag_hdr),
1219 MAX_IPV6_HDR_LEN);
1220 if (err < 0)
1221 goto out;
1222
1223 hp = OPT_HDR(struct frag_hdr, skb, off);
1224
1225 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
1226 fragment = true;
1227
1228 nexthdr = hp->nexthdr;
1229 off += sizeof(struct frag_hdr);
1230 break;
1231 }
Paul Durrant2eba61d2013-10-16 17:50:29 +01001232 default:
1233 done = true;
1234 break;
1235 }
1236 }
1237
Paul Durrant1431fb32013-12-03 17:39:29 +00001238 err = -EPROTO;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001239
Paul Durrant1431fb32013-12-03 17:39:29 +00001240 if (!done || fragment)
Paul Durrant2eba61d2013-10-16 17:50:29 +01001241 goto out;
Paul Durrant2eba61d2013-10-16 17:50:29 +01001242
1243 switch (nexthdr) {
1244 case IPPROTO_TCP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001245 err = maybe_pull_tail(skb,
1246 off + sizeof(struct tcphdr),
1247 MAX_IPV6_HDR_LEN);
1248 if (err < 0)
1249 goto out;
1250
Paul Durrant2eba61d2013-10-16 17:50:29 +01001251 if (!skb_partial_csum_set(skb, off,
1252 offsetof(struct tcphdr, check)))
1253 goto out;
1254
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001255 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001256 tcp_hdr(skb)->check =
1257 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1258 &ipv6_hdr(skb)->daddr,
1259 skb->len - off,
1260 IPPROTO_TCP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001261 break;
1262 case IPPROTO_UDP:
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001263 err = maybe_pull_tail(skb,
1264 off + sizeof(struct udphdr),
1265 MAX_IPV6_HDR_LEN);
1266 if (err < 0)
1267 goto out;
1268
Paul Durrant2eba61d2013-10-16 17:50:29 +01001269 if (!skb_partial_csum_set(skb, off,
1270 offsetof(struct udphdr, check)))
1271 goto out;
1272
Paul Durrantd52eb0d2013-12-11 16:37:40 +00001273 if (recalculate_partial_csum)
Paul Durrant1431fb32013-12-03 17:39:29 +00001274 udp_hdr(skb)->check =
1275 ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
1276 &ipv6_hdr(skb)->daddr,
1277 skb->len - off,
1278 IPPROTO_UDP, 0);
Paul Durrant2eba61d2013-10-16 17:50:29 +01001279 break;
1280 default:
Paul Durrant2eba61d2013-10-16 17:50:29 +01001281 goto out;
1282 }
1283
1284 err = 0;
1285
1286out:
1287 return err;
1288}
1289
Ian Campbellf942dc22011-03-15 00:06:18 +00001290static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1291{
Ian Campbellf942dc22011-03-15 00:06:18 +00001292 int err = -EPROTO;
1293 int recalculate_partial_csum = 0;
1294
Paul Durrant2eba61d2013-10-16 17:50:29 +01001295 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001296 * peers can fail to set NETRXF_csum_blank when sending a GSO
1297 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1298 * recalculate the partial checksum.
1299 */
1300 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1301 vif->rx_gso_checksum_fixup++;
1302 skb->ip_summed = CHECKSUM_PARTIAL;
1303 recalculate_partial_csum = 1;
1304 }
1305
1306 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1307 if (skb->ip_summed != CHECKSUM_PARTIAL)
1308 return 0;
1309
Paul Durrant2eba61d2013-10-16 17:50:29 +01001310 if (skb->protocol == htons(ETH_P_IP))
1311 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1312 else if (skb->protocol == htons(ETH_P_IPV6))
1313 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001314
Ian Campbellf942dc22011-03-15 00:06:18 +00001315 return err;
1316}
1317
1318static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1319{
Wei Liu059dfa62013-10-28 12:07:57 +00001320 u64 now = get_jiffies_64();
1321 u64 next_credit = vif->credit_window_start +
Ian Campbellf942dc22011-03-15 00:06:18 +00001322 msecs_to_jiffies(vif->credit_usec / 1000);
1323
1324 /* Timer could already be pending in rare cases. */
1325 if (timer_pending(&vif->credit_timeout))
1326 return true;
1327
1328 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001329 if (time_after_eq64(now, next_credit)) {
1330 vif->credit_window_start = now;
Ian Campbellf942dc22011-03-15 00:06:18 +00001331 tx_add_credit(vif);
1332 }
1333
1334 /* Still too big to send right now? Set a callback. */
1335 if (size > vif->remaining_credit) {
1336 vif->credit_timeout.data =
1337 (unsigned long)vif;
1338 vif->credit_timeout.function =
1339 tx_credit_callback;
1340 mod_timer(&vif->credit_timeout,
1341 next_credit);
Wei Liu059dfa62013-10-28 12:07:57 +00001342 vif->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001343
1344 return true;
1345 }
1346
1347 return false;
1348}
1349
Paul Durrant10574052013-12-11 10:57:15 +00001350static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001351{
Wei Liub3f980b2013-08-26 12:59:38 +01001352 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001353 struct sk_buff *skb;
1354 int ret;
1355
Wei Liub3f980b2013-08-26 12:59:38 +01001356 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
Paul Durrant10574052013-12-11 10:57:15 +00001357 < MAX_PENDING_REQS) &&
1358 (skb_queue_len(&vif->tx_queue) < budget)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001359 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001360 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001361 struct page *page;
1362 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1363 u16 pending_idx;
1364 RING_IDX idx;
1365 int work_to_do;
1366 unsigned int data_len;
1367 pending_ring_idx_t index;
1368
Ian Campbell488562862013-02-06 23:41:35 +00001369 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1370 XEN_NETIF_TX_RING_SIZE) {
1371 netdev_err(vif->dev,
1372 "Impossible number of requests. "
1373 "req_prod %d, req_cons %d, size %ld\n",
1374 vif->tx.sring->req_prod, vif->tx.req_cons,
1375 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001376 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001377 continue;
1378 }
1379
Paul Durrantd9601a32013-12-11 10:57:16 +00001380 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001381 if (!work_to_do)
1382 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001383
1384 idx = vif->tx.req_cons;
1385 rmb(); /* Ensure that we see the request before we copy it. */
1386 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1387
1388 /* Credit-based scheduling. */
1389 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001390 tx_credit_exceeded(vif, txreq.size))
1391 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001392
1393 vif->remaining_credit -= txreq.size;
1394
1395 work_to_do--;
1396 vif->tx.req_cons = ++idx;
1397
1398 memset(extras, 0, sizeof(extras));
1399 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001400 work_to_do = xenvif_get_extras(vif, extras,
1401 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001402 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001403 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001404 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001405 }
1406
Wei Liu73764192013-08-26 12:59:39 +01001407 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001408 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001409 break;
Ian Campbell488562862013-02-06 23:41:35 +00001410
Ian Campbellf942dc22011-03-15 00:06:18 +00001411 idx += ret;
1412
1413 if (unlikely(txreq.size < ETH_HLEN)) {
1414 netdev_dbg(vif->dev,
1415 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001416 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001417 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001418 }
1419
1420 /* No crossing a page as the payload mustn't fragment. */
1421 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001422 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001423 "txreq.offset: %x, size: %u, end: %lu\n",
1424 txreq.offset, txreq.size,
1425 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001426 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001427 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001428 }
1429
Wei Liub3f980b2013-08-26 12:59:38 +01001430 index = pending_index(vif->pending_cons);
1431 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001432
1433 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001434 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001435 PKT_PROT_LEN : txreq.size;
1436
1437 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1438 GFP_ATOMIC | __GFP_NOWARN);
1439 if (unlikely(skb == NULL)) {
1440 netdev_dbg(vif->dev,
1441 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001442 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001443 break;
1444 }
1445
1446 /* Packets passed to netif_rx() must have some headroom. */
1447 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1448
1449 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1450 struct xen_netif_extra_info *gso;
1451 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1452
Wei Liu73764192013-08-26 12:59:39 +01001453 if (xenvif_set_skb_gso(vif, skb, gso)) {
1454 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001455 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001456 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001457 }
1458 }
1459
1460 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001461 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001462 if (!page) {
1463 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001464 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001465 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001466 }
1467
Ian Campbellf942dc22011-03-15 00:06:18 +00001468 gop->source.u.ref = txreq.gref;
1469 gop->source.domid = vif->domid;
1470 gop->source.offset = txreq.offset;
1471
1472 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1473 gop->dest.domid = DOMID_SELF;
1474 gop->dest.offset = txreq.offset;
1475
1476 gop->len = txreq.size;
1477 gop->flags = GNTCOPY_source_gref;
1478
1479 gop++;
1480
Wei Liub3f980b2013-08-26 12:59:38 +01001481 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001482 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001483 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001484 *((u16 *)skb->data) = pending_idx;
1485
1486 __skb_put(skb, data_len);
1487
1488 skb_shinfo(skb)->nr_frags = ret;
1489 if (data_len < txreq.size) {
1490 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001491 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1492 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001493 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001494 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1495 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001496 }
1497
Wei Liub3f980b2013-08-26 12:59:38 +01001498 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001499
Wei Liu73764192013-08-26 12:59:39 +01001500 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001501 if (request_gop == NULL) {
1502 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001503 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001504 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001505 }
1506 gop = request_gop;
1507
Wei Liub3f980b2013-08-26 12:59:38 +01001508 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001509
Ian Campbellf942dc22011-03-15 00:06:18 +00001510 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001511
Wei Liub3f980b2013-08-26 12:59:38 +01001512 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001513 break;
1514 }
1515
Wei Liub3f980b2013-08-26 12:59:38 +01001516 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001517}
1518
Ian Campbellf942dc22011-03-15 00:06:18 +00001519
Paul Durrant10574052013-12-11 10:57:15 +00001520static int xenvif_tx_submit(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +01001521{
1522 struct gnttab_copy *gop = vif->tx_copy_ops;
1523 struct sk_buff *skb;
1524 int work_done = 0;
1525
Paul Durrant10574052013-12-11 10:57:15 +00001526 while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001527 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001528 u16 pending_idx;
1529 unsigned data_len;
1530
1531 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001532 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001533
1534 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001535 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001536 netdev_dbg(vif->dev, "netback grant failed.\n");
1537 skb_shinfo(skb)->nr_frags = 0;
1538 kfree_skb(skb);
1539 continue;
1540 }
1541
1542 data_len = skb->len;
1543 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001544 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001545 data_len);
1546 if (data_len < txp->size) {
1547 /* Append the packet payload as a fragment. */
1548 txp->offset += data_len;
1549 txp->size -= data_len;
1550 } else {
1551 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001552 xenvif_idx_release(vif, pending_idx,
1553 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001554 }
1555
1556 if (txp->flags & XEN_NETTXF_csum_blank)
1557 skb->ip_summed = CHECKSUM_PARTIAL;
1558 else if (txp->flags & XEN_NETTXF_data_validated)
1559 skb->ip_summed = CHECKSUM_UNNECESSARY;
1560
Wei Liu73764192013-08-26 12:59:39 +01001561 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001562
Paul Durrant2eba61d2013-10-16 17:50:29 +01001563 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001564 int target = min_t(int, skb->len, PKT_PROT_LEN);
1565 __pskb_pull_tail(skb, target - skb_headlen(skb));
1566 }
1567
1568 skb->dev = vif->dev;
1569 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001570 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001571
1572 if (checksum_setup(vif, skb)) {
1573 netdev_dbg(vif->dev,
1574 "Can't setup checksum in net_tx_action\n");
1575 kfree_skb(skb);
1576 continue;
1577 }
1578
Jason Wang40893fd2013-03-26 23:11:22 +00001579 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001580
Paul Durrantb89587a2013-12-17 11:44:35 +00001581 /* If the packet is GSO then we will have just set up the
1582 * transport header offset in checksum_setup so it's now
1583 * straightforward to calculate gso_segs.
1584 */
1585 if (skb_is_gso(skb)) {
1586 int mss = skb_shinfo(skb)->gso_size;
1587 int hdrlen = skb_transport_header(skb) -
1588 skb_mac_header(skb) +
1589 tcp_hdrlen(skb);
1590
1591 skb_shinfo(skb)->gso_segs =
1592 DIV_ROUND_UP(skb->len - hdrlen, mss);
1593 }
1594
Ian Campbellf942dc22011-03-15 00:06:18 +00001595 vif->dev->stats.rx_bytes += skb->len;
1596 vif->dev->stats.rx_packets++;
1597
Wei Liub3f980b2013-08-26 12:59:38 +01001598 work_done++;
1599
1600 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001601 }
Wei Liub3f980b2013-08-26 12:59:38 +01001602
1603 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001604}
1605
1606/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001607int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001608{
1609 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001610 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001611
Wei Liub3f980b2013-08-26 12:59:38 +01001612 if (unlikely(!tx_work_todo(vif)))
1613 return 0;
1614
Paul Durrant10574052013-12-11 10:57:15 +00001615 nr_gops = xenvif_tx_build_gops(vif, budget);
Ian Campbellf942dc22011-03-15 00:06:18 +00001616
1617 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001618 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001619
Wei Liub3f980b2013-08-26 12:59:38 +01001620 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001621
Paul Durrant10574052013-12-11 10:57:15 +00001622 work_done = xenvif_tx_submit(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001623
1624 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001625}
1626
Wei Liu73764192013-08-26 12:59:39 +01001627static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1628 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001629{
Ian Campbellf942dc22011-03-15 00:06:18 +00001630 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001631 pending_ring_idx_t head;
1632 u16 peek; /* peek into next tx request */
1633
Wei Liub3f980b2013-08-26 12:59:38 +01001634 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001635
1636 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001637 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001638 return;
1639
Wei Liub3f980b2013-08-26 12:59:38 +01001640 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001641
Wei Liu2810e5b2013-04-22 02:20:42 +00001642 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001643
Wei Liub3f980b2013-08-26 12:59:38 +01001644 BUG_ON(!pending_tx_is_head(vif, head));
1645 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001646
Wei Liu2810e5b2013-04-22 02:20:42 +00001647 do {
1648 pending_ring_idx_t index;
1649 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001650 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001651
Wei Liub3f980b2013-08-26 12:59:38 +01001652 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001653 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001654
Wei Liu2810e5b2013-04-22 02:20:42 +00001655 /* Setting any number other than
1656 * INVALID_PENDING_RING_IDX indicates this slot is
1657 * starting a new packet / ending a previous packet.
1658 */
1659 pending_tx_info->head = 0;
1660
Wei Liub3f980b2013-08-26 12:59:38 +01001661 index = pending_index(vif->pending_prod++);
1662 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001663
Wei Liub3f980b2013-08-26 12:59:38 +01001664 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001665
Wei Liub3f980b2013-08-26 12:59:38 +01001666 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001667
Wei Liub3f980b2013-08-26 12:59:38 +01001668 put_page(vif->mmap_pages[pending_idx]);
1669 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001670}
1671
Wei Liu2810e5b2013-04-22 02:20:42 +00001672
Ian Campbellf942dc22011-03-15 00:06:18 +00001673static void make_tx_response(struct xenvif *vif,
1674 struct xen_netif_tx_request *txp,
1675 s8 st)
1676{
1677 RING_IDX i = vif->tx.rsp_prod_pvt;
1678 struct xen_netif_tx_response *resp;
1679 int notify;
1680
1681 resp = RING_GET_RESPONSE(&vif->tx, i);
1682 resp->id = txp->id;
1683 resp->status = st;
1684
1685 if (txp->flags & XEN_NETTXF_extra_info)
1686 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1687
1688 vif->tx.rsp_prod_pvt = ++i;
1689 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1690 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001691 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001692}
1693
1694static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1695 u16 id,
1696 s8 st,
1697 u16 offset,
1698 u16 size,
1699 u16 flags)
1700{
1701 RING_IDX i = vif->rx.rsp_prod_pvt;
1702 struct xen_netif_rx_response *resp;
1703
1704 resp = RING_GET_RESPONSE(&vif->rx, i);
1705 resp->offset = offset;
1706 resp->flags = flags;
1707 resp->id = id;
1708 resp->status = (s16)size;
1709 if (st < 0)
1710 resp->status = (s16)st;
1711
1712 vif->rx.rsp_prod_pvt = ++i;
1713
1714 return resp;
1715}
1716
Wei Liub3f980b2013-08-26 12:59:38 +01001717static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001718{
Paul Durrantca2f09f2013-12-06 16:36:07 +00001719 return !skb_queue_empty(&vif->rx_queue) || vif->rx_event;
Ian Campbellf942dc22011-03-15 00:06:18 +00001720}
1721
Wei Liub3f980b2013-08-26 12:59:38 +01001722static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001723{
1724
Wei Liub3f980b2013-08-26 12:59:38 +01001725 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1726 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1727 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001728 return 1;
1729
1730 return 0;
1731}
1732
Wei Liu73764192013-08-26 12:59:39 +01001733void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001734{
David Vrabelc9d63692011-09-29 16:53:31 +01001735 if (vif->tx.sring)
1736 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1737 vif->tx.sring);
1738 if (vif->rx.sring)
1739 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1740 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001741}
1742
Wei Liu73764192013-08-26 12:59:39 +01001743int xenvif_map_frontend_rings(struct xenvif *vif,
1744 grant_ref_t tx_ring_ref,
1745 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001746{
David Vrabelc9d63692011-09-29 16:53:31 +01001747 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001748 struct xen_netif_tx_sring *txs;
1749 struct xen_netif_rx_sring *rxs;
1750
1751 int err = -ENOMEM;
1752
David Vrabelc9d63692011-09-29 16:53:31 +01001753 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1754 tx_ring_ref, &addr);
1755 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001756 goto err;
1757
David Vrabelc9d63692011-09-29 16:53:31 +01001758 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001759 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1760
David Vrabelc9d63692011-09-29 16:53:31 +01001761 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1762 rx_ring_ref, &addr);
1763 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001764 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001765
David Vrabelc9d63692011-09-29 16:53:31 +01001766 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001767 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1768
1769 return 0;
1770
1771err:
Wei Liu73764192013-08-26 12:59:39 +01001772 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001773 return err;
1774}
1775
Paul Durrantca2f09f2013-12-06 16:36:07 +00001776void xenvif_stop_queue(struct xenvif *vif)
1777{
1778 if (!vif->can_queue)
1779 return;
1780
1781 netif_stop_queue(vif->dev);
1782}
1783
1784static void xenvif_start_queue(struct xenvif *vif)
1785{
1786 if (xenvif_schedulable(vif))
1787 netif_wake_queue(vif->dev);
1788}
1789
Wei Liu73764192013-08-26 12:59:39 +01001790int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001791{
1792 struct xenvif *vif = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001793 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001794
1795 while (!kthread_should_stop()) {
1796 wait_event_interruptible(vif->wq,
1797 rx_work_todo(vif) ||
1798 kthread_should_stop());
1799 if (kthread_should_stop())
1800 break;
1801
Paul Durrantca2f09f2013-12-06 16:36:07 +00001802 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +01001803 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001804
Paul Durrantca2f09f2013-12-06 16:36:07 +00001805 vif->rx_event = false;
1806
1807 if (skb_queue_empty(&vif->rx_queue) &&
1808 netif_queue_stopped(vif->dev))
1809 xenvif_start_queue(vif);
1810
Wei Liub3f980b2013-08-26 12:59:38 +01001811 cond_resched();
1812 }
1813
Paul Durrantca2f09f2013-12-06 16:36:07 +00001814 /* Bin any remaining skbs */
1815 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
1816 dev_kfree_skb(skb);
1817
Wei Liub3f980b2013-08-26 12:59:38 +01001818 return 0;
1819}
1820
Ian Campbellf942dc22011-03-15 00:06:18 +00001821static int __init netback_init(void)
1822{
Ian Campbellf942dc22011-03-15 00:06:18 +00001823 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001824
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001825 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001826 return -ENODEV;
1827
Wei Liu37641492013-05-02 00:43:59 +00001828 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001829 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1830 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001831 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001832 }
1833
Ian Campbellf942dc22011-03-15 00:06:18 +00001834 rc = xenvif_xenbus_init();
1835 if (rc)
1836 goto failed_init;
1837
1838 return 0;
1839
1840failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001841 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001842}
1843
1844module_init(netback_init);
1845
Wei Liub103f352013-05-16 23:26:11 +00001846static void __exit netback_fini(void)
1847{
Wei Liub103f352013-05-16 23:26:11 +00001848 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001849}
1850module_exit(netback_fini);
1851
Ian Campbellf942dc22011-03-15 00:06:18 +00001852MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001853MODULE_ALIAS("xen-backend:vif");