blob: a773f2016bad9cd431b9ab1123097280324e6b38 [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>
Zoltan Kisse3377f32014-03-06 21:48:29 +000040#include <linux/highmem.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000041
42#include <net/tcp.h>
43
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
Zoltan Kiss09350782014-03-06 21:48:30 +000058/* When guest ring is filled up, qdisc queues the packets for us, but we have
Zoltan Kiss0e59a4a2014-03-24 23:59:50 +000059 * to timeout them, otherwise other guests' packets can get stuck there
Zoltan Kiss09350782014-03-06 21:48:30 +000060 */
61unsigned int rx_drain_timeout_msecs = 10000;
62module_param(rx_drain_timeout_msecs, uint, 0444);
63unsigned int rx_drain_timeout_jiffies;
64
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +010065unsigned int xenvif_max_queues;
66module_param_named(max_queues, xenvif_max_queues, uint, 0644);
67MODULE_PARM_DESC(max_queues,
68 "Maximum number of queues per virtual interface");
69
Wei Liu2810e5b2013-04-22 02:20:42 +000070/*
71 * This is the maximum slots a skb can have. If a guest sends a skb
72 * which exceeds this limit it is considered malicious.
73 */
Wei Liu37641492013-05-02 00:43:59 +000074#define FATAL_SKB_SLOTS_DEFAULT 20
75static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
76module_param(fatal_skb_slots, uint, 0444);
77
Wei Liue9ce7cb2014-06-04 10:30:42 +010078static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +010079 u8 status);
80
Wei Liue9ce7cb2014-06-04 10:30:42 +010081static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000082 struct xen_netif_tx_request *txp,
83 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010084
Wei Liue9ce7cb2014-06-04 10:30:42 +010085static inline int tx_work_todo(struct xenvif_queue *queue);
86static inline int rx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +010087
Wei Liue9ce7cb2014-06-04 10:30:42 +010088static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000089 u16 id,
90 s8 st,
91 u16 offset,
92 u16 size,
93 u16 flags);
94
Wei Liue9ce7cb2014-06-04 10:30:42 +010095static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +000096 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000097{
Wei Liue9ce7cb2014-06-04 10:30:42 +010098 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +000099}
100
Wei Liue9ce7cb2014-06-04 10:30:42 +0100101static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000102 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000103{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100104 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000105}
106
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000107#define callback_param(vif, pending_idx) \
108 (vif->pending_tx_info[pending_idx].callback_struct)
109
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000110/* Find the containing VIF's structure from a pointer in pending_tx_info array
111 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100112static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000113{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000114 u16 pending_idx = ubuf->desc;
115 struct pending_tx_info *temp =
116 container_of(ubuf, struct pending_tx_info, callback_struct);
117 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100118 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000119 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000120}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000121
Paul Durrant2eba61d2013-10-16 17:50:29 +0100122/* This is a miniumum size for the linear area to avoid lots of
123 * calls to __pskb_pull_tail() as we set up checksum offsets. The
124 * value 128 was chosen as it covers all IPv4 and most likely
125 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000126 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100127#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000128
Ian Campbellea066ad2011-10-05 00:28:46 +0000129static u16 frag_get_pending_idx(skb_frag_t *frag)
130{
131 return (u16)frag->page_offset;
132}
133
134static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
135{
136 frag->page_offset = pending_idx;
137}
138
Ian Campbellf942dc22011-03-15 00:06:18 +0000139static inline pending_ring_idx_t pending_index(unsigned i)
140{
141 return i & (MAX_PENDING_REQS-1);
142}
143
Wei Liue9ce7cb2014-06-04 10:30:42 +0100144bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000145{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000146 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000147
Paul Durrantca2f09f2013-12-06 16:36:07 +0000148 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100149 prod = queue->rx.sring->req_prod;
150 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000151
Paul Durrantca2f09f2013-12-06 16:36:07 +0000152 if (prod - cons >= needed)
153 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000154
Wei Liue9ce7cb2014-06-04 10:30:42 +0100155 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000156
Paul Durrantca2f09f2013-12-06 16:36:07 +0000157 /* Make sure event is visible before we check prod
158 * again.
159 */
160 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100161 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000162
Paul Durrantca2f09f2013-12-06 16:36:07 +0000163 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000164}
165
166/*
167 * Returns true if we should start a new receive buffer instead of
168 * adding 'size' bytes to a buffer which currently contains 'offset'
169 * bytes.
170 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100171static bool start_new_rx_buffer(int offset, unsigned long size, int head,
172 bool full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000173{
174 /* simple case: we have completely filled the current buffer. */
175 if (offset == MAX_BUFFER_OFFSET)
176 return true;
177
178 /*
179 * complex case: start a fresh buffer if the current frag
180 * would overflow the current buffer but only if:
181 * (i) this frag would fit completely in the next buffer
182 * and (ii) there is already some data in the current buffer
183 * and (iii) this is not the head buffer.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100184 * and (iv) there is no need to fully utilize the buffers
Ian Campbellf942dc22011-03-15 00:06:18 +0000185 *
186 * Where:
187 * - (i) stops us splitting a frag into two copies
188 * unless the frag is too large for a single buffer.
189 * - (ii) stops us from leaving a buffer pointlessly empty.
190 * - (iii) stops us leaving the first buffer
191 * empty. Strictly speaking this is already covered
192 * by (ii) but is explicitly checked because
193 * netfront relies on the first buffer being
194 * non-empty and can crash otherwise.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100195 * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS
196 * slot
Ian Campbellf942dc22011-03-15 00:06:18 +0000197 *
198 * This means we will effectively linearise small
199 * frags but do not needlessly split large buffers
200 * into multiple copies tend to give large frags their
201 * own buffers as before.
202 */
Paul Durrant0576edd2014-03-28 11:39:05 +0000203 BUG_ON(size > MAX_BUFFER_OFFSET);
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100204 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head &&
205 !full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000206 return true;
207
208 return false;
209}
210
Ian Campbellf942dc22011-03-15 00:06:18 +0000211struct netrx_pending_operations {
212 unsigned copy_prod, copy_cons;
213 unsigned meta_prod, meta_cons;
214 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100215 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000216 int copy_off;
217 grant_ref_t copy_gref;
218};
219
Wei Liue9ce7cb2014-06-04 10:30:42 +0100220static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100221 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000222{
Wei Liub3f980b2013-08-26 12:59:38 +0100223 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000224 struct xen_netif_rx_request *req;
225
Wei Liue9ce7cb2014-06-04 10:30:42 +0100226 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000227
228 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100229 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000230 meta->gso_size = 0;
231 meta->size = 0;
232 meta->id = req->id;
233
234 npo->copy_off = 0;
235 npo->copy_gref = req->gref;
236
237 return meta;
238}
239
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100240struct xenvif_rx_cb {
241 int meta_slots_used;
242 bool full_coalesce;
243};
244
245#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
246
Wei Liu33bc8012013-10-08 10:54:21 +0100247/*
248 * Set up the grant operations for this fragment. If it's a flipping
249 * interface, we also set up the unmap request from here.
250 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100251static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100252 struct netrx_pending_operations *npo,
253 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000254 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100255 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000256 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000257{
258 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100259 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000260 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800261 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000262
263 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000264 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000265
266 meta = npo->meta + npo->meta_prod - 1;
267
Ian Campbell6a8ed462012-10-10 03:48:42 +0000268 /* Skip unused frames from start of page */
269 page += offset >> PAGE_SHIFT;
270 offset &= ~PAGE_MASK;
271
Ian Campbellf942dc22011-03-15 00:06:18 +0000272 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000273 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000274 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
275
Ian Campbell6a8ed462012-10-10 03:48:42 +0000276 bytes = PAGE_SIZE - offset;
277
278 if (bytes > size)
279 bytes = size;
280
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100281 if (start_new_rx_buffer(npo->copy_off,
282 bytes,
283 *head,
284 XENVIF_RX_CB(skb)->full_coalesce)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000285 /*
286 * Netfront requires there to be some data in the head
287 * buffer.
288 */
Wei Liu33bc8012013-10-08 10:54:21 +0100289 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000290
Wei Liue9ce7cb2014-06-04 10:30:42 +0100291 meta = get_next_rx_buffer(queue, npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000292 }
293
Ian Campbellf942dc22011-03-15 00:06:18 +0000294 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
295 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
296
297 copy_gop = npo->copy + npo->copy_prod++;
298 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100299 copy_gop->len = bytes;
300
Wei Liue9ce7cb2014-06-04 10:30:42 +0100301 if (foreign_queue) {
302 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000303 copy_gop->source.u.ref = foreign_gref;
304 copy_gop->flags |= GNTCOPY_source_gref;
305 } else {
306 copy_gop->source.domid = DOMID_SELF;
307 copy_gop->source.u.gmfn =
308 virt_to_mfn(page_address(page));
309 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000310 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000311
Wei Liue9ce7cb2014-06-04 10:30:42 +0100312 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000313 copy_gop->dest.offset = npo->copy_off;
314 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000315
316 npo->copy_off += bytes;
317 meta->size += bytes;
318
319 offset += bytes;
320 size -= bytes;
321
Ian Campbell6a8ed462012-10-10 03:48:42 +0000322 /* Next frame */
323 if (offset == PAGE_SIZE && size) {
324 BUG_ON(!PageCompound(page));
325 page++;
326 offset = 0;
327 }
328
Ian Campbellf942dc22011-03-15 00:06:18 +0000329 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800330 if (skb_is_gso(skb)) {
331 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
332 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
333 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
334 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
335 }
Paul Durrant82cada22013-10-16 17:50:32 +0100336
Wei Liue9ce7cb2014-06-04 10:30:42 +0100337 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
338 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000339
Wei Liu33bc8012013-10-08 10:54:21 +0100340 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000341
342 }
343}
344
345/*
Zoltan Kiss58375742014-05-15 11:08:34 +0100346 * Find the grant ref for a given frag in a chain of struct ubuf_info's
347 * skb: the skb itself
348 * i: the frag's number
349 * ubuf: a pointer to an element in the chain. It should not be NULL
350 *
351 * Returns a pointer to the element in the chain where the page were found. If
352 * not found, returns NULL.
353 * See the definition of callback_struct in common.h for more details about
354 * the chain.
355 */
356static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
357 const int i,
358 const struct ubuf_info *ubuf)
359{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100360 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss58375742014-05-15 11:08:34 +0100361
362 do {
363 u16 pending_idx = ubuf->desc;
364
365 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100366 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss58375742014-05-15 11:08:34 +0100367 break;
368 ubuf = (struct ubuf_info *) ubuf->ctx;
369 } while (ubuf);
370
371 return ubuf;
372}
373
374/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 * Prepare an SKB to be transmitted to the frontend.
376 *
377 * This function is responsible for allocating grant operations, meta
378 * structures, etc.
379 *
380 * It returns the number of meta structures consumed. The number of
381 * ring slots used is always equal to the number of meta slots used
382 * plus the number of GSO descriptors used. Currently, we use either
383 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
384 * frontend-side LRO).
385 */
Wei Liu73764192013-08-26 12:59:39 +0100386static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100387 struct netrx_pending_operations *npo,
388 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000389{
390 struct xenvif *vif = netdev_priv(skb->dev);
391 int nr_frags = skb_shinfo(skb)->nr_frags;
392 int i;
393 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100394 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000395 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100396 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000397 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100398 int gso_type;
Zoltan Kiss58375742014-05-15 11:08:34 +0100399 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
400 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000401
402 old_meta_prod = npo->meta_prod;
403
Annie Li5bd07672014-03-10 22:58:34 +0800404 gso_type = XEN_NETIF_GSO_TYPE_NONE;
405 if (skb_is_gso(skb)) {
406 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
407 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
408 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
409 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100410 }
411
Ian Campbellf942dc22011-03-15 00:06:18 +0000412 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000413 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100414 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000415 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100416 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800417 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000418 meta->size = 0;
419 meta->id = req->id;
420 }
421
Wei Liue9ce7cb2014-06-04 10:30:42 +0100422 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000423 meta = npo->meta + npo->meta_prod++;
424
Paul Durrant82cada22013-10-16 17:50:32 +0100425 if ((1 << gso_type) & vif->gso_mask) {
426 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800427 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100428 } else {
429 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000430 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100431 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000432
433 meta->size = 0;
434 meta->id = req->id;
435 npo->copy_off = 0;
436 npo->copy_gref = req->gref;
437
438 data = skb->data;
439 while (data < skb_tail_pointer(skb)) {
440 unsigned int offset = offset_in_page(data);
441 unsigned int len = PAGE_SIZE - offset;
442
443 if (data + len > skb_tail_pointer(skb))
444 len = skb_tail_pointer(skb) - data;
445
Wei Liue9ce7cb2014-06-04 10:30:42 +0100446 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000447 virt_to_page(data), len, offset, &head,
448 NULL,
449 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000450 data += len;
451 }
452
453 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss58375742014-05-15 11:08:34 +0100454 /* This variable also signals whether foreign_gref has a real
455 * value or not.
456 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100457 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss58375742014-05-15 11:08:34 +0100458 grant_ref_t foreign_gref;
459
460 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
461 (ubuf->callback == &xenvif_zerocopy_callback)) {
462 const struct ubuf_info *const startpoint = ubuf;
463
464 /* Ideally ubuf points to the chain element which
465 * belongs to this frag. Or if frags were removed from
466 * the beginning, then shortly before it.
467 */
468 ubuf = xenvif_find_gref(skb, i, ubuf);
469
470 /* Try again from the beginning of the list, if we
471 * haven't tried from there. This only makes sense in
472 * the unlikely event of reordering the original frags.
473 * For injected local pages it's an unnecessary second
474 * run.
475 */
476 if (unlikely(!ubuf) && startpoint != head_ubuf)
477 ubuf = xenvif_find_gref(skb, i, head_ubuf);
478
479 if (likely(ubuf)) {
480 u16 pending_idx = ubuf->desc;
481
Wei Liue9ce7cb2014-06-04 10:30:42 +0100482 foreign_queue = ubuf_to_queue(ubuf);
483 foreign_gref =
484 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss58375742014-05-15 11:08:34 +0100485 /* Just a safety measure. If this was the last
486 * element on the list, the for loop will
487 * iterate again if a local page were added to
488 * the end. Using head_ubuf here prevents the
489 * second search on the chain. Or the original
490 * frags changed order, but that's less likely.
491 * In any way, ubuf shouldn't be NULL.
492 */
493 ubuf = ubuf->ctx ?
494 (struct ubuf_info *) ubuf->ctx :
495 head_ubuf;
496 } else
497 /* This frag was a local page, added to the
498 * array after the skb left netback.
499 */
500 ubuf = head_ubuf;
501 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100502 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100503 skb_frag_page(&skb_shinfo(skb)->frags[i]),
504 skb_frag_size(&skb_shinfo(skb)->frags[i]),
505 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000506 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100507 foreign_queue,
508 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000509 }
510
511 return npo->meta_prod - old_meta_prod;
512}
513
514/*
Wei Liu73764192013-08-26 12:59:39 +0100515 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000516 * used to set up the operations on the top of
517 * netrx_pending_operations, which have since been done. Check that
518 * they didn't give any errors and advance over them.
519 */
Wei Liu73764192013-08-26 12:59:39 +0100520static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
521 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000522{
523 struct gnttab_copy *copy_op;
524 int status = XEN_NETIF_RSP_OKAY;
525 int i;
526
527 for (i = 0; i < nr_meta_slots; i++) {
528 copy_op = npo->copy + npo->copy_cons++;
529 if (copy_op->status != GNTST_okay) {
530 netdev_dbg(vif->dev,
531 "Bad status %d from copy to DOM%d.\n",
532 copy_op->status, vif->domid);
533 status = XEN_NETIF_RSP_ERROR;
534 }
535 }
536
537 return status;
538}
539
Wei Liue9ce7cb2014-06-04 10:30:42 +0100540static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100541 struct xenvif_rx_meta *meta,
542 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000543{
544 int i;
545 unsigned long offset;
546
547 /* No fragments used */
548 if (nr_meta_slots <= 1)
549 return;
550
551 nr_meta_slots--;
552
553 for (i = 0; i < nr_meta_slots; i++) {
554 int flags;
555 if (i == nr_meta_slots - 1)
556 flags = 0;
557 else
558 flags = XEN_NETRXF_more_data;
559
560 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100561 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000562 meta[i].size, flags);
563 }
564}
565
Wei Liue9ce7cb2014-06-04 10:30:42 +0100566void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000567{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100568 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100569}
570
Wei Liue9ce7cb2014-06-04 10:30:42 +0100571static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100572{
Ian Campbellf942dc22011-03-15 00:06:18 +0000573 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000574 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000575 struct xen_netif_rx_response *resp;
576 struct sk_buff_head rxq;
577 struct sk_buff *skb;
578 LIST_HEAD(notify);
579 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000580 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000581 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000582
583 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100584 .copy = queue->grant_copy_op,
585 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000586 };
587
588 skb_queue_head_init(&rxq);
589
Wei Liue9ce7cb2014-06-04 10:30:42 +0100590 while ((skb = skb_dequeue(&queue->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000591 RING_IDX max_slots_needed;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000592 RING_IDX old_req_cons;
593 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000594 int i;
595
596 /* We need a cheap worse case estimate for the number of
597 * slots we'll use.
598 */
599
600 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
601 skb_headlen(skb),
602 PAGE_SIZE);
603 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
604 unsigned int size;
Paul Durranta02eb472014-03-28 11:39:06 +0000605 unsigned int offset;
606
Paul Durrantca2f09f2013-12-06 16:36:07 +0000607 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Paul Durranta02eb472014-03-28 11:39:06 +0000608 offset = skb_shinfo(skb)->frags[i].page_offset;
609
610 /* For a worse-case estimate we need to factor in
611 * the fragment page offset as this will affect the
612 * number of times xenvif_gop_frag_copy() will
613 * call start_new_rx_buffer().
614 */
615 max_slots_needed += DIV_ROUND_UP(offset + size,
616 PAGE_SIZE);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000617 }
Paul Durranta02eb472014-03-28 11:39:06 +0000618
619 /* To avoid the estimate becoming too pessimal for some
620 * frontends that limit posted rx requests, cap the estimate
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100621 * at MAX_SKB_FRAGS. In this case netback will fully coalesce
622 * the skb into the provided slots.
Paul Durranta02eb472014-03-28 11:39:06 +0000623 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100624 if (max_slots_needed > MAX_SKB_FRAGS) {
Paul Durranta02eb472014-03-28 11:39:06 +0000625 max_slots_needed = MAX_SKB_FRAGS;
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100626 XENVIF_RX_CB(skb)->full_coalesce = true;
627 } else {
628 XENVIF_RX_CB(skb)->full_coalesce = false;
629 }
Paul Durranta02eb472014-03-28 11:39:06 +0000630
631 /* We may need one more slot for GSO metadata */
Annie Li5bd07672014-03-10 22:58:34 +0800632 if (skb_is_gso(skb) &&
633 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
634 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000635 max_slots_needed++;
636
637 /* If the skb may not fit then bail out now */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100638 if (!xenvif_rx_ring_slots_available(queue, max_slots_needed)) {
639 skb_queue_head(&queue->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000640 need_to_notify = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100641 queue->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000642 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000643 } else
Wei Liue9ce7cb2014-06-04 10:30:42 +0100644 queue->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000645
Wei Liue9ce7cb2014-06-04 10:30:42 +0100646 old_req_cons = queue->rx.req_cons;
647 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
648 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000649
650 BUG_ON(ring_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000651
652 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000653 }
654
Wei Liue9ce7cb2014-06-04 10:30:42 +0100655 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000656
657 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000658 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000659
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000660 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100661 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000662
663 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000664
Wei Liue9ce7cb2014-06-04 10:30:42 +0100665 if ((1 << queue->meta[npo.meta_cons].gso_type) &
666 queue->vif->gso_prefix_mask) {
667 resp = RING_GET_RESPONSE(&queue->rx,
668 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000669
670 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
671
Wei Liue9ce7cb2014-06-04 10:30:42 +0100672 resp->offset = queue->meta[npo.meta_cons].gso_size;
673 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000674 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000675
676 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000677 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000678 }
679
680
Wei Liue9ce7cb2014-06-04 10:30:42 +0100681 queue->stats.tx_bytes += skb->len;
682 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000683
Wei Liue9ce7cb2014-06-04 10:30:42 +0100684 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000685 XENVIF_RX_CB(skb)->meta_slots_used,
686 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000687
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000688 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000689 flags = 0;
690 else
691 flags = XEN_NETRXF_more_data;
692
693 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
694 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
695 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
696 /* remote but checksummed. */
697 flags |= XEN_NETRXF_data_validated;
698
699 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100700 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000701 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100702 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000703 flags);
704
Wei Liue9ce7cb2014-06-04 10:30:42 +0100705 if ((1 << queue->meta[npo.meta_cons].gso_type) &
706 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000707 struct xen_netif_extra_info *gso =
708 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100709 RING_GET_RESPONSE(&queue->rx,
710 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000711
712 resp->flags |= XEN_NETRXF_extra_info;
713
Wei Liue9ce7cb2014-06-04 10:30:42 +0100714 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
715 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000716 gso->u.gso.pad = 0;
717 gso->u.gso.features = 0;
718
719 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
720 gso->flags = 0;
721 }
722
Wei Liue9ce7cb2014-06-04 10:30:42 +0100723 xenvif_add_frag_responses(queue, status,
724 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000725 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000726
Wei Liue9ce7cb2014-06-04 10:30:42 +0100727 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000728
Paul Durrant11b57f92014-01-08 12:41:58 +0000729 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100730
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000731 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000732 dev_kfree_skb(skb);
733 }
734
Paul Durrantca2f09f2013-12-06 16:36:07 +0000735done:
Wei Liub3f980b2013-08-26 12:59:38 +0100736 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100737 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000738}
739
Wei Liue9ce7cb2014-06-04 10:30:42 +0100740void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000741{
742 int more_to_do;
743
Wei Liue9ce7cb2014-06-04 10:30:42 +0100744 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000745
746 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100747 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000748}
749
Wei Liue9ce7cb2014-06-04 10:30:42 +0100750static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000751{
752 unsigned long max_burst, max_credit;
753
754 /*
755 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
756 * Otherwise the interface can seize up due to insufficient credit.
757 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100758 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000759 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100760 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000761
762 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100763 max_credit = queue->remaining_credit + queue->credit_bytes;
764 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000765 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
766
Wei Liue9ce7cb2014-06-04 10:30:42 +0100767 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000768}
769
770static void tx_credit_callback(unsigned long data)
771{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100772 struct xenvif_queue *queue = (struct xenvif_queue *)data;
773 tx_add_credit(queue);
774 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000775}
776
Wei Liue9ce7cb2014-06-04 10:30:42 +0100777static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100778 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000779{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100780 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000781 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000782
783 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100784 spin_lock_irqsave(&queue->response_lock, flags);
785 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
786 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000787 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000788 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100789 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000790 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100791 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000792}
793
Wei Liu73764192013-08-26 12:59:39 +0100794static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000795{
796 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100797 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100798 /* Disable the vif from queue 0's kthread */
799 if (vif->queues)
800 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000801}
802
Wei Liue9ce7cb2014-06-04 10:30:42 +0100803static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100804 struct xen_netif_tx_request *first,
805 struct xen_netif_tx_request *txp,
806 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000807{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100808 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000809 int slots = 0;
810 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000811 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000812
813 if (!(first->flags & XEN_NETTXF_more_data))
814 return 0;
815
816 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000817 struct xen_netif_tx_request dropped_tx = { 0 };
818
Wei Liu2810e5b2013-04-22 02:20:42 +0000819 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100820 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000821 "Asked for %d slots but exceeds this limit\n",
822 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100823 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000824 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000825 }
826
Wei Liu2810e5b2013-04-22 02:20:42 +0000827 /* This guest is really using too many slots and
828 * considered malicious.
829 */
Wei Liu37641492013-05-02 00:43:59 +0000830 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100831 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000832 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000833 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100834 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000835 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000836 }
837
Wei Liu2810e5b2013-04-22 02:20:42 +0000838 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000839 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
840 * the historical MAX_SKB_FRAGS value 18 to honor the
841 * same behavior as before. Any packet using more than
842 * 18 slots but less than fatal_skb_slots slots is
843 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000844 */
Wei Liu37641492013-05-02 00:43:59 +0000845 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000846 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100847 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000848 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000849 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000850 drop_err = -E2BIG;
851 }
852
Wei Liu59ccb4e2013-05-02 00:43:58 +0000853 if (drop_err)
854 txp = &dropped_tx;
855
Wei Liue9ce7cb2014-06-04 10:30:42 +0100856 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000857 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000858
859 /* If the guest submitted a frame >= 64 KiB then
860 * first->size overflowed and following slots will
861 * appear to be larger than the frame.
862 *
863 * This cannot be fatal error as there are buggy
864 * frontends that do this.
865 *
866 * Consume all slots and drop the packet.
867 */
868 if (!drop_err && txp->size > first->size) {
869 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100870 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000871 "Invalid tx request, slot size %u > remaining size %u\n",
872 txp->size, first->size);
873 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000874 }
875
876 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000877 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000878
879 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100880 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000881 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100882 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000883 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000884 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000885
886 more_data = txp->flags & XEN_NETTXF_more_data;
887
888 if (!drop_err)
889 txp++;
890
891 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000892
893 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100894 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000895 return drop_err;
896 }
897
898 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000899}
900
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000901
902struct xenvif_tx_cb {
903 u16 pending_idx;
904};
905
906#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
907
Wei Liue9ce7cb2014-06-04 10:30:42 +0100908static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100909 u16 pending_idx,
910 struct xen_netif_tx_request *txp,
911 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000912{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100913 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
914 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000915 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100916 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000917
Wei Liue9ce7cb2014-06-04 10:30:42 +0100918 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000919 sizeof(*txp));
920}
921
Zoltan Kisse3377f32014-03-06 21:48:29 +0000922static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
923{
924 struct sk_buff *skb =
925 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
926 GFP_ATOMIC | __GFP_NOWARN);
927 if (unlikely(skb == NULL))
928 return NULL;
929
930 /* Packets passed to netif_rx() must have some headroom. */
931 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
932
933 /* Initialize it here to avoid later surprises */
934 skb_shinfo(skb)->destructor_arg = NULL;
935
936 return skb;
937}
938
Wei Liue9ce7cb2014-06-04 10:30:42 +0100939static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000940 struct sk_buff *skb,
941 struct xen_netif_tx_request *txp,
942 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000943{
944 struct skb_shared_info *shinfo = skb_shinfo(skb);
945 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000946 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000947 int start;
948 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000949 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000950
951 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000952 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000953 */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000954 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
955 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
956 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
957 shinfo->nr_frags = MAX_SKB_FRAGS;
958 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000959 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000960
961 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000962 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000963
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000964 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
965 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100966 index = pending_index(queue->pending_cons++);
967 pending_idx = queue->pending_ring[index];
968 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000969 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000970 }
971
Zoltan Kisse3377f32014-03-06 21:48:29 +0000972 if (frag_overflow) {
973 struct sk_buff *nskb = xenvif_alloc_skb(0);
974 if (unlikely(nskb == NULL)) {
975 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100976 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +0000977 "Can't allocate the frag_list skb.\n");
978 return NULL;
979 }
980
981 shinfo = skb_shinfo(nskb);
982 frags = shinfo->frags;
983
984 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
985 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100986 index = pending_index(queue->pending_cons++);
987 pending_idx = queue->pending_ring[index];
988 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +0000989 frag_set_pending_idx(&frags[shinfo->nr_frags],
990 pending_idx);
991 }
992
993 skb_shinfo(skb)->frag_list = nskb;
994 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000995
Ian Campbellf942dc22011-03-15 00:06:18 +0000996 return gop;
997}
998
Wei Liue9ce7cb2014-06-04 10:30:42 +0100999static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001000 u16 pending_idx,
1001 grant_handle_t handle)
1002{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001003 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001004 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001005 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001006 "Trying to overwrite active handle! pending_idx: %x\n",
1007 pending_idx);
1008 BUG();
1009 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001010 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001011}
1012
Wei Liue9ce7cb2014-06-04 10:30:42 +01001013static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001014 u16 pending_idx)
1015{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001016 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001017 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001018 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001019 "Trying to unmap invalid handle! pending_idx: %x\n",
1020 pending_idx);
1021 BUG();
1022 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001023 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001024}
1025
Wei Liue9ce7cb2014-06-04 10:30:42 +01001026static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001027 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001028 struct gnttab_map_grant_ref **gopp_map,
1029 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +00001030{
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001031 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001032 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001033 /* This always points to the shinfo of the skb being checked, which
1034 * could be either the first or the one on the frag_list
1035 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001036 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001037 /* If this is non-NULL, we are currently checking the frag_list skb, and
1038 * this points to the shinfo of the first one
1039 */
1040 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001041 int nr_frags = shinfo->nr_frags;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001042 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001043
1044 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001045 err = (*gopp_copy)->status;
1046 (*gopp_copy)++;
1047 if (unlikely(err)) {
1048 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001049 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001050 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001051 (*gopp_copy)->status,
1052 pending_idx,
1053 (*gopp_copy)->source.u.ref);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001054 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001055 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001056
Zoltan Kisse3377f32014-03-06 21:48:29 +00001057check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001058 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001059 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001060
Ian Campbellea066ad2011-10-05 00:28:46 +00001061 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001062
1063 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001064 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001065
Ian Campbellf942dc22011-03-15 00:06:18 +00001066 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001067 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001068 pending_idx,
1069 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001070 /* Had a previous error? Invalidate this fragment. */
1071 if (unlikely(err))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001072 xenvif_idx_unmap(queue, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001073 continue;
1074 }
1075
1076 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001077 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001078 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001079 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001080 i,
1081 gop_map->status,
1082 pending_idx,
1083 gop_map->ref);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001084 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001085
1086 /* Not the first error? Preceding frags already invalidated. */
1087 if (err)
1088 continue;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001089 /* First error: invalidate preceding fragments. */
1090 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001091 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001092 xenvif_idx_unmap(queue, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001093 }
1094
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001095 /* And if we found the error while checking the frag_list, unmap
1096 * the first skb's frags
1097 */
1098 if (first_shinfo) {
1099 for (j = 0; j < first_shinfo->nr_frags; j++) {
1100 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1101 xenvif_idx_unmap(queue, pending_idx);
1102 }
1103 }
1104
Ian Campbellf942dc22011-03-15 00:06:18 +00001105 /* Remember the error: invalidate all subsequent fragments. */
1106 err = newerr;
1107 }
1108
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001109 if (skb_has_frag_list(skb) && !first_shinfo) {
1110 first_shinfo = skb_shinfo(skb);
1111 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001112 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001113
1114 goto check_frags;
1115 }
1116
Zoltan Kissbdab8272014-04-02 18:04:58 +01001117 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001118 return err;
1119}
1120
Wei Liue9ce7cb2014-06-04 10:30:42 +01001121static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001122{
1123 struct skb_shared_info *shinfo = skb_shinfo(skb);
1124 int nr_frags = shinfo->nr_frags;
1125 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001126 u16 prev_pending_idx = INVALID_PENDING_IDX;
1127
Ian Campbellf942dc22011-03-15 00:06:18 +00001128 for (i = 0; i < nr_frags; i++) {
1129 skb_frag_t *frag = shinfo->frags + i;
1130 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001131 struct page *page;
1132 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001133
Ian Campbellea066ad2011-10-05 00:28:46 +00001134 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001135
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001136 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001137 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001138 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001139 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001140 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001141 callback_param(queue, prev_pending_idx).ctx =
1142 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001143
Wei Liue9ce7cb2014-06-04 10:30:42 +01001144 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001145 prev_pending_idx = pending_idx;
1146
Wei Liue9ce7cb2014-06-04 10:30:42 +01001147 txp = &queue->pending_tx_info[pending_idx].req;
1148 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001149 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001150 skb->len += txp->size;
1151 skb->data_len += txp->size;
1152 skb->truesize += txp->size;
1153
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001154 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001155 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001156 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001157 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1158 * overlaps with "index", and "mapping" is not set. I think mapping
1159 * should be set. If delivered to local stack, it would drop this
1160 * skb in sk_filter unless the socket has the right to use it.
1161 */
1162 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001163}
1164
Wei Liue9ce7cb2014-06-04 10:30:42 +01001165static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001166 struct xen_netif_extra_info *extras,
1167 int work_to_do)
1168{
1169 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001170 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001171
1172 do {
1173 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001174 netdev_err(queue->vif->dev, "Missing extra info\n");
1175 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001176 return -EBADR;
1177 }
1178
Wei Liue9ce7cb2014-06-04 10:30:42 +01001179 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001180 sizeof(extra));
1181 if (unlikely(!extra.type ||
1182 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001183 queue->tx.req_cons = ++cons;
1184 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001185 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001186 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001187 return -EINVAL;
1188 }
1189
1190 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001191 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001192 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1193
1194 return work_to_do;
1195}
1196
Wei Liu73764192013-08-26 12:59:39 +01001197static int xenvif_set_skb_gso(struct xenvif *vif,
1198 struct sk_buff *skb,
1199 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001200{
1201 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001202 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001203 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001204 return -EINVAL;
1205 }
1206
Paul Durranta9468582013-10-16 17:50:31 +01001207 switch (gso->u.gso.type) {
1208 case XEN_NETIF_GSO_TYPE_TCPV4:
1209 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1210 break;
1211 case XEN_NETIF_GSO_TYPE_TCPV6:
1212 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1213 break;
1214 default:
Ian Campbell488562862013-02-06 23:41:35 +00001215 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001216 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001217 return -EINVAL;
1218 }
1219
1220 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001221 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001222
1223 return 0;
1224}
1225
Wei Liue9ce7cb2014-06-04 10:30:42 +01001226static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001227{
Paul Durrant27216372014-01-09 10:02:47 +00001228 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001229
Paul Durrant2eba61d2013-10-16 17:50:29 +01001230 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001231 * peers can fail to set NETRXF_csum_blank when sending a GSO
1232 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1233 * recalculate the partial checksum.
1234 */
1235 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001236 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001237 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001238 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001239 }
1240
1241 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1242 if (skb->ip_summed != CHECKSUM_PARTIAL)
1243 return 0;
1244
Paul Durrant27216372014-01-09 10:02:47 +00001245 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001246}
1247
Wei Liue9ce7cb2014-06-04 10:30:42 +01001248static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001249{
Wei Liu059dfa62013-10-28 12:07:57 +00001250 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001251 u64 next_credit = queue->credit_window_start +
1252 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001253
1254 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001255 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001256 return true;
1257
1258 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001259 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001260 queue->credit_window_start = now;
1261 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001262 }
1263
1264 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001265 if (size > queue->remaining_credit) {
1266 queue->credit_timeout.data =
1267 (unsigned long)queue;
1268 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001269 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001270 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001271 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001272 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001273
1274 return true;
1275 }
1276
1277 return false;
1278}
1279
Wei Liue9ce7cb2014-06-04 10:30:42 +01001280static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001281 int budget,
1282 unsigned *copy_ops,
1283 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001284{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001285 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001286 struct sk_buff *skb;
1287 int ret;
1288
Wei Liue9ce7cb2014-06-04 10:30:42 +01001289 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001290 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001291 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001292 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1293 u16 pending_idx;
1294 RING_IDX idx;
1295 int work_to_do;
1296 unsigned int data_len;
1297 pending_ring_idx_t index;
1298
Wei Liue9ce7cb2014-06-04 10:30:42 +01001299 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001300 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001301 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001302 "Impossible number of requests. "
1303 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001304 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001305 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001306 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001307 break;
Ian Campbell488562862013-02-06 23:41:35 +00001308 }
1309
Wei Liue9ce7cb2014-06-04 10:30:42 +01001310 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001311 if (!work_to_do)
1312 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001313
Wei Liue9ce7cb2014-06-04 10:30:42 +01001314 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001315 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001316 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001317
1318 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001319 if (txreq.size > queue->remaining_credit &&
1320 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001321 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001322
Wei Liue9ce7cb2014-06-04 10:30:42 +01001323 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001324
1325 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001326 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001327
1328 memset(extras, 0, sizeof(extras));
1329 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001330 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001331 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001332 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001333 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001334 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001335 }
1336
Wei Liue9ce7cb2014-06-04 10:30:42 +01001337 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001338 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001339 break;
Ian Campbell488562862013-02-06 23:41:35 +00001340
Ian Campbellf942dc22011-03-15 00:06:18 +00001341 idx += ret;
1342
1343 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001344 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001345 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001346 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001347 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001348 }
1349
1350 /* No crossing a page as the payload mustn't fragment. */
1351 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001352 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001353 "txreq.offset: %x, size: %u, end: %lu\n",
1354 txreq.offset, txreq.size,
1355 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001356 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001357 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 }
1359
Wei Liue9ce7cb2014-06-04 10:30:42 +01001360 index = pending_index(queue->pending_cons);
1361 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001362
1363 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001364 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001365 PKT_PROT_LEN : txreq.size;
1366
Zoltan Kisse3377f32014-03-06 21:48:29 +00001367 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001368 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001369 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001370 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001371 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001372 break;
1373 }
1374
Ian Campbellf942dc22011-03-15 00:06:18 +00001375 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1376 struct xen_netif_extra_info *gso;
1377 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1378
Wei Liue9ce7cb2014-06-04 10:30:42 +01001379 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001380 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001381 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001382 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001383 }
1384 }
1385
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001386 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001387
1388 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001389 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1390 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1391 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001392
Wei Liue9ce7cb2014-06-04 10:30:42 +01001393 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001394 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001395 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1396 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001397 offset_in_page(skb->data);
1398
Wei Liue9ce7cb2014-06-04 10:30:42 +01001399 queue->tx_copy_ops[*copy_ops].len = data_len;
1400 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001401
1402 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001403
1404 skb_shinfo(skb)->nr_frags = ret;
1405 if (data_len < txreq.size) {
1406 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001407 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1408 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001410 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001411 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001412 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1413 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001414 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001415 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001416 }
1417
Wei Liue9ce7cb2014-06-04 10:30:42 +01001418 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001419
Wei Liue9ce7cb2014-06-04 10:30:42 +01001420 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001421 if (request_gop == NULL) {
1422 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001423 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001424 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001425 }
1426 gop = request_gop;
1427
Wei Liue9ce7cb2014-06-04 10:30:42 +01001428 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001429
Wei Liue9ce7cb2014-06-04 10:30:42 +01001430 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001431
Wei Liue9ce7cb2014-06-04 10:30:42 +01001432 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1433 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001434 break;
1435 }
1436
Wei Liue9ce7cb2014-06-04 10:30:42 +01001437 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001438 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001439}
1440
Zoltan Kisse3377f32014-03-06 21:48:29 +00001441/* Consolidate skb with a frag_list into a brand new one with local pages on
1442 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1443 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001444static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001445{
1446 unsigned int offset = skb_headlen(skb);
1447 skb_frag_t frags[MAX_SKB_FRAGS];
1448 int i;
1449 struct ubuf_info *uarg;
1450 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1451
Wei Liue9ce7cb2014-06-04 10:30:42 +01001452 queue->stats.tx_zerocopy_sent += 2;
1453 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001454
Wei Liue9ce7cb2014-06-04 10:30:42 +01001455 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001456 /* Subtract frags size, we will correct it later */
1457 skb->truesize -= skb->data_len;
1458 skb->len += nskb->len;
1459 skb->data_len += nskb->len;
1460
1461 /* create a brand new frags array and coalesce there */
1462 for (i = 0; offset < skb->len; i++) {
1463 struct page *page;
1464 unsigned int len;
1465
1466 BUG_ON(i >= MAX_SKB_FRAGS);
1467 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1468 if (!page) {
1469 int j;
1470 skb->truesize += skb->data_len;
1471 for (j = 0; j < i; j++)
1472 put_page(frags[j].page.p);
1473 return -ENOMEM;
1474 }
1475
1476 if (offset + PAGE_SIZE < skb->len)
1477 len = PAGE_SIZE;
1478 else
1479 len = skb->len - offset;
1480 if (skb_copy_bits(skb, offset, page_address(page), len))
1481 BUG();
1482
1483 offset += len;
1484 frags[i].page.p = page;
1485 frags[i].page_offset = 0;
1486 skb_frag_size_set(&frags[i], len);
1487 }
1488 /* swap out with old one */
1489 memcpy(skb_shinfo(skb)->frags,
1490 frags,
1491 i * sizeof(skb_frag_t));
1492 skb_shinfo(skb)->nr_frags = i;
1493 skb->truesize += i * PAGE_SIZE;
1494
1495 /* remove traces of mapped pages and frag_list */
1496 skb_frag_list_init(skb);
1497 uarg = skb_shinfo(skb)->destructor_arg;
1498 uarg->callback(uarg, true);
1499 skb_shinfo(skb)->destructor_arg = NULL;
1500
1501 skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1502 kfree_skb(nskb);
1503
1504 return 0;
1505}
Ian Campbellf942dc22011-03-15 00:06:18 +00001506
Wei Liue9ce7cb2014-06-04 10:30:42 +01001507static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001508{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001509 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1510 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001511 struct sk_buff *skb;
1512 int work_done = 0;
1513
Wei Liue9ce7cb2014-06-04 10:30:42 +01001514 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001515 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001516 u16 pending_idx;
1517 unsigned data_len;
1518
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001519 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001520 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001521
1522 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001523 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001524 skb_shinfo(skb)->nr_frags = 0;
1525 kfree_skb(skb);
1526 continue;
1527 }
1528
1529 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001530 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001531 if (data_len < txp->size) {
1532 /* Append the packet payload as a fragment. */
1533 txp->offset += data_len;
1534 txp->size -= data_len;
1535 } else {
1536 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001537 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001538 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001539 }
1540
1541 if (txp->flags & XEN_NETTXF_csum_blank)
1542 skb->ip_summed = CHECKSUM_PARTIAL;
1543 else if (txp->flags & XEN_NETTXF_data_validated)
1544 skb->ip_summed = CHECKSUM_UNNECESSARY;
1545
Wei Liue9ce7cb2014-06-04 10:30:42 +01001546 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001547
Zoltan Kisse3377f32014-03-06 21:48:29 +00001548 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001549 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001550 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001551 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001552 "Not enough memory to consolidate frag_list!\n");
1553 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1554 kfree_skb(skb);
1555 continue;
1556 }
1557 }
1558
Paul Durrant2eba61d2013-10-16 17:50:29 +01001559 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001560 int target = min_t(int, skb->len, PKT_PROT_LEN);
1561 __pskb_pull_tail(skb, target - skb_headlen(skb));
1562 }
1563
Wei Liue9ce7cb2014-06-04 10:30:42 +01001564 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001565 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001566 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001567
Wei Liue9ce7cb2014-06-04 10:30:42 +01001568 if (checksum_setup(queue, skb)) {
1569 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001570 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001571 /* We have to set this flag to trigger the callback */
1572 if (skb_shinfo(skb)->destructor_arg)
1573 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Ian Campbellf942dc22011-03-15 00:06:18 +00001574 kfree_skb(skb);
1575 continue;
1576 }
1577
Jason Wang40893fd2013-03-26 23:11:22 +00001578 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001579
Paul Durrantb89587a2013-12-17 11:44:35 +00001580 /* If the packet is GSO then we will have just set up the
1581 * transport header offset in checksum_setup so it's now
1582 * straightforward to calculate gso_segs.
1583 */
1584 if (skb_is_gso(skb)) {
1585 int mss = skb_shinfo(skb)->gso_size;
1586 int hdrlen = skb_transport_header(skb) -
1587 skb_mac_header(skb) +
1588 tcp_hdrlen(skb);
1589
1590 skb_shinfo(skb)->gso_segs =
1591 DIV_ROUND_UP(skb->len - hdrlen, mss);
1592 }
1593
Wei Liue9ce7cb2014-06-04 10:30:42 +01001594 queue->stats.rx_bytes += skb->len;
1595 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001596
Wei Liub3f980b2013-08-26 12:59:38 +01001597 work_done++;
1598
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001599 /* Set this flag right before netif_receive_skb, otherwise
1600 * someone might think this packet already left netback, and
1601 * do a skb_copy_ubufs while we are still in control of the
1602 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1603 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001604 if (skb_shinfo(skb)->destructor_arg) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001605 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001606 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001607 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001608
Wei Liub3f980b2013-08-26 12:59:38 +01001609 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001610 }
Wei Liub3f980b2013-08-26 12:59:38 +01001611
1612 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001613}
1614
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001615void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1616{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001617 unsigned long flags;
1618 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001619 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001620
1621 /* This is the only place where we grab this lock, to protect callbacks
1622 * from each other.
1623 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001624 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001625 do {
1626 u16 pending_idx = ubuf->desc;
1627 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001628 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001629 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001630 index = pending_index(queue->dealloc_prod);
1631 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001632 /* Sync with xenvif_tx_dealloc_action:
1633 * insert idx then incr producer.
1634 */
1635 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001636 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001637 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001638 wake_up(&queue->dealloc_wq);
1639 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001640
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001641 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001642 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001643 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001644 queue->stats.tx_zerocopy_fail++;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001645}
1646
Wei Liue9ce7cb2014-06-04 10:30:42 +01001647static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001648{
1649 struct gnttab_unmap_grant_ref *gop;
1650 pending_ring_idx_t dc, dp;
1651 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1652 unsigned int i = 0;
1653
Wei Liue9ce7cb2014-06-04 10:30:42 +01001654 dc = queue->dealloc_cons;
1655 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001656
1657 /* Free up any grants we have finished using */
1658 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001659 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001660
1661 /* Ensure we see all indices enqueued by all
1662 * xenvif_zerocopy_callback().
1663 */
1664 smp_rmb();
1665
1666 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001667 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001668 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001669 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001670
Wei Liue9ce7cb2014-06-04 10:30:42 +01001671 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001672 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001673 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1674 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001675 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001676 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001677 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001678 queue->grant_tx_handle[pending_idx]);
1679 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001680 ++gop;
1681 }
1682
Wei Liue9ce7cb2014-06-04 10:30:42 +01001683 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001684
Wei Liue9ce7cb2014-06-04 10:30:42 +01001685 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001686
Wei Liue9ce7cb2014-06-04 10:30:42 +01001687 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001688 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001689 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001690 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001691 queue->pages_to_unmap,
1692 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001693 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001694 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1695 gop - queue->tx_unmap_ops, ret);
1696 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001697 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001698 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001699 " host_addr: %llx handle: %x status: %d\n",
1700 gop[i].host_addr,
1701 gop[i].handle,
1702 gop[i].status);
1703 }
1704 BUG();
1705 }
1706 }
1707
Wei Liue9ce7cb2014-06-04 10:30:42 +01001708 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1709 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001710 XEN_NETIF_RSP_OKAY);
1711}
1712
1713
Ian Campbellf942dc22011-03-15 00:06:18 +00001714/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001715int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001716{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001717 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001718 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001719
Wei Liue9ce7cb2014-06-04 10:30:42 +01001720 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001721 return 0;
1722
Wei Liue9ce7cb2014-06-04 10:30:42 +01001723 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001724
Zoltan Kissbdab8272014-04-02 18:04:58 +01001725 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001726 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001727
Wei Liue9ce7cb2014-06-04 10:30:42 +01001728 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001729 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001730 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001731 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001732 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001733 nr_mops);
1734 BUG_ON(ret);
1735 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001736
Wei Liue9ce7cb2014-06-04 10:30:42 +01001737 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001738
1739 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001740}
1741
Wei Liue9ce7cb2014-06-04 10:30:42 +01001742static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001743 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001744{
Ian Campbellf942dc22011-03-15 00:06:18 +00001745 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001746 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001747 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001748
Wei Liue9ce7cb2014-06-04 10:30:42 +01001749 pending_tx_info = &queue->pending_tx_info[pending_idx];
1750 spin_lock_irqsave(&queue->response_lock, flags);
1751 make_tx_response(queue, &pending_tx_info->req, status);
1752 index = pending_index(queue->pending_prod);
1753 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001754 /* TX shouldn't use the index before we give it back here */
1755 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001756 queue->pending_prod++;
1757 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001758}
1759
Wei Liu2810e5b2013-04-22 02:20:42 +00001760
Wei Liue9ce7cb2014-06-04 10:30:42 +01001761static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001762 struct xen_netif_tx_request *txp,
1763 s8 st)
1764{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001765 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001766 struct xen_netif_tx_response *resp;
1767 int notify;
1768
Wei Liue9ce7cb2014-06-04 10:30:42 +01001769 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001770 resp->id = txp->id;
1771 resp->status = st;
1772
1773 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001774 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001775
Wei Liue9ce7cb2014-06-04 10:30:42 +01001776 queue->tx.rsp_prod_pvt = ++i;
1777 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001778 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001779 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001780}
1781
Wei Liue9ce7cb2014-06-04 10:30:42 +01001782static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001783 u16 id,
1784 s8 st,
1785 u16 offset,
1786 u16 size,
1787 u16 flags)
1788{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001789 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001790 struct xen_netif_rx_response *resp;
1791
Wei Liue9ce7cb2014-06-04 10:30:42 +01001792 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001793 resp->offset = offset;
1794 resp->flags = flags;
1795 resp->id = id;
1796 resp->status = (s16)size;
1797 if (st < 0)
1798 resp->status = (s16)st;
1799
Wei Liue9ce7cb2014-06-04 10:30:42 +01001800 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001801
1802 return resp;
1803}
1804
Wei Liue9ce7cb2014-06-04 10:30:42 +01001805void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001806{
1807 int ret;
1808 struct gnttab_unmap_grant_ref tx_unmap_op;
1809
1810 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001811 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001812 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001813 queue->grant_tx_handle[pending_idx]);
1814 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001815
1816 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001817 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001818 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001819 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001820 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1821 ret,
1822 pending_idx,
1823 tx_unmap_op.host_addr,
1824 tx_unmap_op.handle,
1825 tx_unmap_op.status);
1826 BUG();
1827 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001828
Wei Liue9ce7cb2014-06-04 10:30:42 +01001829 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_OKAY);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001830}
1831
Wei Liue9ce7cb2014-06-04 10:30:42 +01001832static inline int rx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001833{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001834 return (!skb_queue_empty(&queue->rx_queue) &&
1835 xenvif_rx_ring_slots_available(queue, queue->rx_last_skb_slots)) ||
1836 queue->rx_queue_purge;
Ian Campbellf942dc22011-03-15 00:06:18 +00001837}
1838
Wei Liue9ce7cb2014-06-04 10:30:42 +01001839static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001840{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001841 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001842 return 1;
1843
1844 return 0;
1845}
1846
Wei Liue9ce7cb2014-06-04 10:30:42 +01001847static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001848{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001849 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001850}
1851
Wei Liue9ce7cb2014-06-04 10:30:42 +01001852void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001853{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001854 if (queue->tx.sring)
1855 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1856 queue->tx.sring);
1857 if (queue->rx.sring)
1858 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1859 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001860}
1861
Wei Liue9ce7cb2014-06-04 10:30:42 +01001862int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001863 grant_ref_t tx_ring_ref,
1864 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001865{
David Vrabelc9d63692011-09-29 16:53:31 +01001866 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001867 struct xen_netif_tx_sring *txs;
1868 struct xen_netif_rx_sring *rxs;
1869
1870 int err = -ENOMEM;
1871
Wei Liue9ce7cb2014-06-04 10:30:42 +01001872 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001873 tx_ring_ref, &addr);
1874 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001875 goto err;
1876
David Vrabelc9d63692011-09-29 16:53:31 +01001877 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001878 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001879
Wei Liue9ce7cb2014-06-04 10:30:42 +01001880 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001881 rx_ring_ref, &addr);
1882 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001883 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001884
David Vrabelc9d63692011-09-29 16:53:31 +01001885 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001886 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001887
1888 return 0;
1889
1890err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001891 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001892 return err;
1893}
1894
Wei Liue9ce7cb2014-06-04 10:30:42 +01001895static void xenvif_start_queue(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001896{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001897 if (xenvif_schedulable(queue->vif))
1898 xenvif_wake_queue(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001899}
1900
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001901int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001902{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001903 struct xenvif_queue *queue = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001904 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001905
1906 while (!kthread_should_stop()) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001907 wait_event_interruptible(queue->wq,
1908 rx_work_todo(queue) ||
1909 queue->vif->disabled ||
Wei Liub3f980b2013-08-26 12:59:38 +01001910 kthread_should_stop());
Wei Liue9d8b2c2014-04-01 12:46:12 +01001911
1912 /* This frontend is found to be rogue, disable it in
1913 * kthread context. Currently this is only set when
1914 * netback finds out frontend sends malformed packet,
1915 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01001916 * context so we defer it here, if this thread is
1917 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01001918 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001919 if (unlikely(queue->vif->disabled && netif_carrier_ok(queue->vif->dev) && queue->id == 0))
1920 xenvif_carrier_off(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001921
Wei Liub3f980b2013-08-26 12:59:38 +01001922 if (kthread_should_stop())
1923 break;
1924
Wei Liue9ce7cb2014-06-04 10:30:42 +01001925 if (queue->rx_queue_purge) {
1926 skb_queue_purge(&queue->rx_queue);
1927 queue->rx_queue_purge = false;
Zoltan Kiss09350782014-03-06 21:48:30 +00001928 }
1929
Wei Liue9ce7cb2014-06-04 10:30:42 +01001930 if (!skb_queue_empty(&queue->rx_queue))
1931 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001932
Wei Liue9ce7cb2014-06-04 10:30:42 +01001933 if (skb_queue_empty(&queue->rx_queue) &&
1934 xenvif_queue_stopped(queue)) {
1935 del_timer_sync(&queue->wake_queue);
1936 xenvif_start_queue(queue);
Zoltan Kiss09350782014-03-06 21:48:30 +00001937 }
Paul Durrantca2f09f2013-12-06 16:36:07 +00001938
Wei Liub3f980b2013-08-26 12:59:38 +01001939 cond_resched();
1940 }
1941
Paul Durrantca2f09f2013-12-06 16:36:07 +00001942 /* Bin any remaining skbs */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001943 while ((skb = skb_dequeue(&queue->rx_queue)) != NULL)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001944 dev_kfree_skb(skb);
1945
Wei Liub3f980b2013-08-26 12:59:38 +01001946 return 0;
1947}
1948
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001949int xenvif_dealloc_kthread(void *data)
1950{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001951 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001952
1953 while (!kthread_should_stop()) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001954 wait_event_interruptible(queue->dealloc_wq,
1955 tx_dealloc_work_todo(queue) ||
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001956 kthread_should_stop());
1957 if (kthread_should_stop())
1958 break;
1959
Wei Liue9ce7cb2014-06-04 10:30:42 +01001960 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001961 cond_resched();
1962 }
1963
1964 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01001965 if (tx_dealloc_work_todo(queue))
1966 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001967
1968 return 0;
1969}
1970
Ian Campbellf942dc22011-03-15 00:06:18 +00001971static int __init netback_init(void)
1972{
Ian Campbellf942dc22011-03-15 00:06:18 +00001973 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001974
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001975 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001976 return -ENODEV;
1977
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01001978 /* Allow as many queues as there are CPUs, by default */
1979 xenvif_max_queues = num_online_cpus();
1980
Wei Liu37641492013-05-02 00:43:59 +00001981 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001982 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1983 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001984 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001985 }
1986
Ian Campbellf942dc22011-03-15 00:06:18 +00001987 rc = xenvif_xenbus_init();
1988 if (rc)
1989 goto failed_init;
1990
Zoltan Kiss09350782014-03-06 21:48:30 +00001991 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
1992
Ian Campbellf942dc22011-03-15 00:06:18 +00001993 return 0;
1994
1995failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001996 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001997}
1998
1999module_init(netback_init);
2000
Wei Liub103f352013-05-16 23:26:11 +00002001static void __exit netback_fini(void)
2002{
Wei Liub103f352013-05-16 23:26:11 +00002003 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002004}
2005module_exit(netback_fini);
2006
Ian Campbellf942dc22011-03-15 00:06:18 +00002007MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002008MODULE_ALIAS("xen-backend:vif");