blob: 57aa3b507d3206b81654eb568d53efd7a9c70c74 [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
David Vrabelf48da8b2014-10-22 14:08:54 +010058/* The time that packets can stay on the guest Rx internal queue
59 * before they are dropped.
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);
Wei Liub3f980b2013-08-26 12:59:38 +010086
Wei Liue9ce7cb2014-06-04 10:30:42 +010087static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000088 u16 id,
89 s8 st,
90 u16 offset,
91 u16 size,
92 u16 flags);
93
Wei Liue9ce7cb2014-06-04 10:30:42 +010094static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +000095 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000096{
Wei Liue9ce7cb2014-06-04 10:30:42 +010097 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +000098}
99
Wei Liue9ce7cb2014-06-04 10:30:42 +0100100static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000101 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000102{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100103 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000104}
105
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000106#define callback_param(vif, pending_idx) \
107 (vif->pending_tx_info[pending_idx].callback_struct)
108
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000109/* Find the containing VIF's structure from a pointer in pending_tx_info array
110 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100111static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000112{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000113 u16 pending_idx = ubuf->desc;
114 struct pending_tx_info *temp =
115 container_of(ubuf, struct pending_tx_info, callback_struct);
116 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100117 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000118 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000119}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000120
Paul Durrant2eba61d2013-10-16 17:50:29 +0100121/* This is a miniumum size for the linear area to avoid lots of
122 * calls to __pskb_pull_tail() as we set up checksum offsets. The
123 * value 128 was chosen as it covers all IPv4 and most likely
124 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000125 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100126#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000127
Ian Campbellea066ad2011-10-05 00:28:46 +0000128static u16 frag_get_pending_idx(skb_frag_t *frag)
129{
130 return (u16)frag->page_offset;
131}
132
133static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
134{
135 frag->page_offset = pending_idx;
136}
137
Ian Campbellf942dc22011-03-15 00:06:18 +0000138static inline pending_ring_idx_t pending_index(unsigned i)
139{
140 return i & (MAX_PENDING_REQS-1);
141}
142
Wei Liue9ce7cb2014-06-04 10:30:42 +0100143bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000144{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000145 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000146
Paul Durrantca2f09f2013-12-06 16:36:07 +0000147 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100148 prod = queue->rx.sring->req_prod;
149 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000150
Paul Durrantca2f09f2013-12-06 16:36:07 +0000151 if (prod - cons >= needed)
152 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000153
Wei Liue9ce7cb2014-06-04 10:30:42 +0100154 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000155
Paul Durrantca2f09f2013-12-06 16:36:07 +0000156 /* Make sure event is visible before we check prod
157 * again.
158 */
159 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100160 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000161
Paul Durrantca2f09f2013-12-06 16:36:07 +0000162 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000163}
164
David Vrabelf48da8b2014-10-22 14:08:54 +0100165void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
166{
167 unsigned long flags;
168
169 spin_lock_irqsave(&queue->rx_queue.lock, flags);
170
171 __skb_queue_tail(&queue->rx_queue, skb);
172
173 queue->rx_queue_len += skb->len;
174 if (queue->rx_queue_len > queue->rx_queue_max)
175 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
176
177 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
178}
179
180static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
181{
182 struct sk_buff *skb;
183
184 spin_lock_irq(&queue->rx_queue.lock);
185
186 skb = __skb_dequeue(&queue->rx_queue);
187 if (skb)
188 queue->rx_queue_len -= skb->len;
189
190 spin_unlock_irq(&queue->rx_queue.lock);
191
192 return skb;
193}
194
195static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
196{
197 spin_lock_irq(&queue->rx_queue.lock);
198
199 if (queue->rx_queue_len < queue->rx_queue_max)
200 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
201
202 spin_unlock_irq(&queue->rx_queue.lock);
203}
204
205
206static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
207{
208 struct sk_buff *skb;
209 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
210 kfree_skb(skb);
211}
212
213static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
214{
215 struct sk_buff *skb;
216
217 for(;;) {
218 skb = skb_peek(&queue->rx_queue);
219 if (!skb)
220 break;
221 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
222 break;
223 xenvif_rx_dequeue(queue);
224 kfree_skb(skb);
225 }
226}
227
Ian Campbellf942dc22011-03-15 00:06:18 +0000228/*
229 * Returns true if we should start a new receive buffer instead of
230 * adding 'size' bytes to a buffer which currently contains 'offset'
231 * bytes.
232 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100233static bool start_new_rx_buffer(int offset, unsigned long size, int head,
234 bool full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000235{
236 /* simple case: we have completely filled the current buffer. */
237 if (offset == MAX_BUFFER_OFFSET)
238 return true;
239
240 /*
241 * complex case: start a fresh buffer if the current frag
242 * would overflow the current buffer but only if:
243 * (i) this frag would fit completely in the next buffer
244 * and (ii) there is already some data in the current buffer
245 * and (iii) this is not the head buffer.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100246 * and (iv) there is no need to fully utilize the buffers
Ian Campbellf942dc22011-03-15 00:06:18 +0000247 *
248 * Where:
249 * - (i) stops us splitting a frag into two copies
250 * unless the frag is too large for a single buffer.
251 * - (ii) stops us from leaving a buffer pointlessly empty.
252 * - (iii) stops us leaving the first buffer
253 * empty. Strictly speaking this is already covered
254 * by (ii) but is explicitly checked because
255 * netfront relies on the first buffer being
256 * non-empty and can crash otherwise.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100257 * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS
258 * slot
Ian Campbellf942dc22011-03-15 00:06:18 +0000259 *
260 * This means we will effectively linearise small
261 * frags but do not needlessly split large buffers
262 * into multiple copies tend to give large frags their
263 * own buffers as before.
264 */
Paul Durrant0576edd2014-03-28 11:39:05 +0000265 BUG_ON(size > MAX_BUFFER_OFFSET);
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100266 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head &&
267 !full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000268 return true;
269
270 return false;
271}
272
Ian Campbellf942dc22011-03-15 00:06:18 +0000273struct netrx_pending_operations {
274 unsigned copy_prod, copy_cons;
275 unsigned meta_prod, meta_cons;
276 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100277 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000278 int copy_off;
279 grant_ref_t copy_gref;
280};
281
Wei Liue9ce7cb2014-06-04 10:30:42 +0100282static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100283 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000284{
Wei Liub3f980b2013-08-26 12:59:38 +0100285 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000286 struct xen_netif_rx_request *req;
287
Wei Liue9ce7cb2014-06-04 10:30:42 +0100288 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000289
290 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100291 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000292 meta->gso_size = 0;
293 meta->size = 0;
294 meta->id = req->id;
295
296 npo->copy_off = 0;
297 npo->copy_gref = req->gref;
298
299 return meta;
300}
301
Wei Liu33bc8012013-10-08 10:54:21 +0100302/*
303 * Set up the grant operations for this fragment. If it's a flipping
304 * interface, we also set up the unmap request from here.
305 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100306static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100307 struct netrx_pending_operations *npo,
308 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000309 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100310 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000311 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000312{
313 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100314 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000315 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800316 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000317
318 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000319 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000320
321 meta = npo->meta + npo->meta_prod - 1;
322
Ian Campbell6a8ed462012-10-10 03:48:42 +0000323 /* Skip unused frames from start of page */
324 page += offset >> PAGE_SHIFT;
325 offset &= ~PAGE_MASK;
326
Ian Campbellf942dc22011-03-15 00:06:18 +0000327 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000328 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000329 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
330
Ian Campbell6a8ed462012-10-10 03:48:42 +0000331 bytes = PAGE_SIZE - offset;
332
333 if (bytes > size)
334 bytes = size;
335
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100336 if (start_new_rx_buffer(npo->copy_off,
337 bytes,
338 *head,
339 XENVIF_RX_CB(skb)->full_coalesce)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000340 /*
341 * Netfront requires there to be some data in the head
342 * buffer.
343 */
Wei Liu33bc8012013-10-08 10:54:21 +0100344 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000345
Wei Liue9ce7cb2014-06-04 10:30:42 +0100346 meta = get_next_rx_buffer(queue, npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000347 }
348
Ian Campbellf942dc22011-03-15 00:06:18 +0000349 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
350 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
351
352 copy_gop = npo->copy + npo->copy_prod++;
353 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100354 copy_gop->len = bytes;
355
Wei Liue9ce7cb2014-06-04 10:30:42 +0100356 if (foreign_queue) {
357 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000358 copy_gop->source.u.ref = foreign_gref;
359 copy_gop->flags |= GNTCOPY_source_gref;
360 } else {
361 copy_gop->source.domid = DOMID_SELF;
362 copy_gop->source.u.gmfn =
363 virt_to_mfn(page_address(page));
364 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000365 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000366
Wei Liue9ce7cb2014-06-04 10:30:42 +0100367 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000368 copy_gop->dest.offset = npo->copy_off;
369 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000370
371 npo->copy_off += bytes;
372 meta->size += bytes;
373
374 offset += bytes;
375 size -= bytes;
376
Ian Campbell6a8ed462012-10-10 03:48:42 +0000377 /* Next frame */
378 if (offset == PAGE_SIZE && size) {
379 BUG_ON(!PageCompound(page));
380 page++;
381 offset = 0;
382 }
383
Ian Campbellf942dc22011-03-15 00:06:18 +0000384 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800385 if (skb_is_gso(skb)) {
386 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
387 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
388 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
389 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
390 }
Paul Durrant82cada22013-10-16 17:50:32 +0100391
Wei Liue9ce7cb2014-06-04 10:30:42 +0100392 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
393 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000394
Wei Liu33bc8012013-10-08 10:54:21 +0100395 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000396
397 }
398}
399
400/*
Zoltan Kiss583757442014-05-15 11:08:34 +0100401 * Find the grant ref for a given frag in a chain of struct ubuf_info's
402 * skb: the skb itself
403 * i: the frag's number
404 * ubuf: a pointer to an element in the chain. It should not be NULL
405 *
406 * Returns a pointer to the element in the chain where the page were found. If
407 * not found, returns NULL.
408 * See the definition of callback_struct in common.h for more details about
409 * the chain.
410 */
411static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
412 const int i,
413 const struct ubuf_info *ubuf)
414{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100415 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss583757442014-05-15 11:08:34 +0100416
417 do {
418 u16 pending_idx = ubuf->desc;
419
420 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100421 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss583757442014-05-15 11:08:34 +0100422 break;
423 ubuf = (struct ubuf_info *) ubuf->ctx;
424 } while (ubuf);
425
426 return ubuf;
427}
428
429/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000430 * Prepare an SKB to be transmitted to the frontend.
431 *
432 * This function is responsible for allocating grant operations, meta
433 * structures, etc.
434 *
435 * It returns the number of meta structures consumed. The number of
436 * ring slots used is always equal to the number of meta slots used
437 * plus the number of GSO descriptors used. Currently, we use either
438 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
439 * frontend-side LRO).
440 */
Wei Liu73764192013-08-26 12:59:39 +0100441static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100442 struct netrx_pending_operations *npo,
443 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000444{
445 struct xenvif *vif = netdev_priv(skb->dev);
446 int nr_frags = skb_shinfo(skb)->nr_frags;
447 int i;
448 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100449 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000450 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100451 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000452 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100453 int gso_type;
Zoltan Kiss583757442014-05-15 11:08:34 +0100454 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
455 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000456
457 old_meta_prod = npo->meta_prod;
458
Annie Li5bd07672014-03-10 22:58:34 +0800459 gso_type = XEN_NETIF_GSO_TYPE_NONE;
460 if (skb_is_gso(skb)) {
461 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
462 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
463 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
464 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100465 }
466
Ian Campbellf942dc22011-03-15 00:06:18 +0000467 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000468 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100469 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000470 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100471 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800472 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000473 meta->size = 0;
474 meta->id = req->id;
475 }
476
Wei Liue9ce7cb2014-06-04 10:30:42 +0100477 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000478 meta = npo->meta + npo->meta_prod++;
479
Paul Durrant82cada22013-10-16 17:50:32 +0100480 if ((1 << gso_type) & vif->gso_mask) {
481 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800482 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100483 } else {
484 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000485 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100486 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000487
488 meta->size = 0;
489 meta->id = req->id;
490 npo->copy_off = 0;
491 npo->copy_gref = req->gref;
492
493 data = skb->data;
494 while (data < skb_tail_pointer(skb)) {
495 unsigned int offset = offset_in_page(data);
496 unsigned int len = PAGE_SIZE - offset;
497
498 if (data + len > skb_tail_pointer(skb))
499 len = skb_tail_pointer(skb) - data;
500
Wei Liue9ce7cb2014-06-04 10:30:42 +0100501 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000502 virt_to_page(data), len, offset, &head,
503 NULL,
504 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000505 data += len;
506 }
507
508 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss583757442014-05-15 11:08:34 +0100509 /* This variable also signals whether foreign_gref has a real
510 * value or not.
511 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100512 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss583757442014-05-15 11:08:34 +0100513 grant_ref_t foreign_gref;
514
515 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
516 (ubuf->callback == &xenvif_zerocopy_callback)) {
517 const struct ubuf_info *const startpoint = ubuf;
518
519 /* Ideally ubuf points to the chain element which
520 * belongs to this frag. Or if frags were removed from
521 * the beginning, then shortly before it.
522 */
523 ubuf = xenvif_find_gref(skb, i, ubuf);
524
525 /* Try again from the beginning of the list, if we
526 * haven't tried from there. This only makes sense in
527 * the unlikely event of reordering the original frags.
528 * For injected local pages it's an unnecessary second
529 * run.
530 */
531 if (unlikely(!ubuf) && startpoint != head_ubuf)
532 ubuf = xenvif_find_gref(skb, i, head_ubuf);
533
534 if (likely(ubuf)) {
535 u16 pending_idx = ubuf->desc;
536
Wei Liue9ce7cb2014-06-04 10:30:42 +0100537 foreign_queue = ubuf_to_queue(ubuf);
538 foreign_gref =
539 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss583757442014-05-15 11:08:34 +0100540 /* Just a safety measure. If this was the last
541 * element on the list, the for loop will
542 * iterate again if a local page were added to
543 * the end. Using head_ubuf here prevents the
544 * second search on the chain. Or the original
545 * frags changed order, but that's less likely.
546 * In any way, ubuf shouldn't be NULL.
547 */
548 ubuf = ubuf->ctx ?
549 (struct ubuf_info *) ubuf->ctx :
550 head_ubuf;
551 } else
552 /* This frag was a local page, added to the
553 * array after the skb left netback.
554 */
555 ubuf = head_ubuf;
556 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100557 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100558 skb_frag_page(&skb_shinfo(skb)->frags[i]),
559 skb_frag_size(&skb_shinfo(skb)->frags[i]),
560 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000561 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100562 foreign_queue,
563 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000564 }
565
566 return npo->meta_prod - old_meta_prod;
567}
568
569/*
Wei Liu73764192013-08-26 12:59:39 +0100570 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 * used to set up the operations on the top of
572 * netrx_pending_operations, which have since been done. Check that
573 * they didn't give any errors and advance over them.
574 */
Wei Liu73764192013-08-26 12:59:39 +0100575static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
576 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000577{
578 struct gnttab_copy *copy_op;
579 int status = XEN_NETIF_RSP_OKAY;
580 int i;
581
582 for (i = 0; i < nr_meta_slots; i++) {
583 copy_op = npo->copy + npo->copy_cons++;
584 if (copy_op->status != GNTST_okay) {
585 netdev_dbg(vif->dev,
586 "Bad status %d from copy to DOM%d.\n",
587 copy_op->status, vif->domid);
588 status = XEN_NETIF_RSP_ERROR;
589 }
590 }
591
592 return status;
593}
594
Wei Liue9ce7cb2014-06-04 10:30:42 +0100595static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100596 struct xenvif_rx_meta *meta,
597 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000598{
599 int i;
600 unsigned long offset;
601
602 /* No fragments used */
603 if (nr_meta_slots <= 1)
604 return;
605
606 nr_meta_slots--;
607
608 for (i = 0; i < nr_meta_slots; i++) {
609 int flags;
610 if (i == nr_meta_slots - 1)
611 flags = 0;
612 else
613 flags = XEN_NETRXF_more_data;
614
615 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100616 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000617 meta[i].size, flags);
618 }
619}
620
Wei Liue9ce7cb2014-06-04 10:30:42 +0100621void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000622{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100623 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100624}
625
Wei Liue9ce7cb2014-06-04 10:30:42 +0100626static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100627{
Ian Campbellf942dc22011-03-15 00:06:18 +0000628 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000629 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000630 struct xen_netif_rx_response *resp;
631 struct sk_buff_head rxq;
632 struct sk_buff *skb;
633 LIST_HEAD(notify);
634 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000635 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000636 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000637
638 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100639 .copy = queue->grant_copy_op,
640 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000641 };
642
643 skb_queue_head_init(&rxq);
644
David Vrabelf48da8b2014-10-22 14:08:54 +0100645 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
646 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000647 RING_IDX max_slots_needed;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000648 RING_IDX old_req_cons;
649 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000650 int i;
651
652 /* We need a cheap worse case estimate for the number of
653 * slots we'll use.
654 */
655
656 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
657 skb_headlen(skb),
658 PAGE_SIZE);
659 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
660 unsigned int size;
Paul Durranta02eb472014-03-28 11:39:06 +0000661 unsigned int offset;
662
Paul Durrantca2f09f2013-12-06 16:36:07 +0000663 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Paul Durranta02eb472014-03-28 11:39:06 +0000664 offset = skb_shinfo(skb)->frags[i].page_offset;
665
666 /* For a worse-case estimate we need to factor in
667 * the fragment page offset as this will affect the
668 * number of times xenvif_gop_frag_copy() will
669 * call start_new_rx_buffer().
670 */
671 max_slots_needed += DIV_ROUND_UP(offset + size,
672 PAGE_SIZE);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000673 }
Paul Durranta02eb472014-03-28 11:39:06 +0000674
675 /* To avoid the estimate becoming too pessimal for some
676 * frontends that limit posted rx requests, cap the estimate
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100677 * at MAX_SKB_FRAGS. In this case netback will fully coalesce
678 * the skb into the provided slots.
Paul Durranta02eb472014-03-28 11:39:06 +0000679 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100680 if (max_slots_needed > MAX_SKB_FRAGS) {
Paul Durranta02eb472014-03-28 11:39:06 +0000681 max_slots_needed = MAX_SKB_FRAGS;
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100682 XENVIF_RX_CB(skb)->full_coalesce = true;
683 } else {
684 XENVIF_RX_CB(skb)->full_coalesce = false;
685 }
Paul Durranta02eb472014-03-28 11:39:06 +0000686
687 /* We may need one more slot for GSO metadata */
Annie Li5bd07672014-03-10 22:58:34 +0800688 if (skb_is_gso(skb) &&
689 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
690 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000691 max_slots_needed++;
692
Wei Liue9ce7cb2014-06-04 10:30:42 +0100693 old_req_cons = queue->rx.req_cons;
694 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
695 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000696
697 BUG_ON(ring_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000698
699 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000700 }
701
Wei Liue9ce7cb2014-06-04 10:30:42 +0100702 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000703
704 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000705 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000706
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000707 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100708 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000709
710 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000711
Wei Liue9ce7cb2014-06-04 10:30:42 +0100712 if ((1 << queue->meta[npo.meta_cons].gso_type) &
713 queue->vif->gso_prefix_mask) {
714 resp = RING_GET_RESPONSE(&queue->rx,
715 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000716
717 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
718
Wei Liue9ce7cb2014-06-04 10:30:42 +0100719 resp->offset = queue->meta[npo.meta_cons].gso_size;
720 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000721 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000722
723 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000724 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000725 }
726
727
Wei Liue9ce7cb2014-06-04 10:30:42 +0100728 queue->stats.tx_bytes += skb->len;
729 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000730
Wei Liue9ce7cb2014-06-04 10:30:42 +0100731 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000732 XENVIF_RX_CB(skb)->meta_slots_used,
733 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000734
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000735 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000736 flags = 0;
737 else
738 flags = XEN_NETRXF_more_data;
739
740 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
741 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
742 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
743 /* remote but checksummed. */
744 flags |= XEN_NETRXF_data_validated;
745
746 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100747 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000748 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100749 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000750 flags);
751
Wei Liue9ce7cb2014-06-04 10:30:42 +0100752 if ((1 << queue->meta[npo.meta_cons].gso_type) &
753 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000754 struct xen_netif_extra_info *gso =
755 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100756 RING_GET_RESPONSE(&queue->rx,
757 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000758
759 resp->flags |= XEN_NETRXF_extra_info;
760
Wei Liue9ce7cb2014-06-04 10:30:42 +0100761 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
762 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000763 gso->u.gso.pad = 0;
764 gso->u.gso.features = 0;
765
766 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
767 gso->flags = 0;
768 }
769
Wei Liue9ce7cb2014-06-04 10:30:42 +0100770 xenvif_add_frag_responses(queue, status,
771 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000772 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000773
Wei Liue9ce7cb2014-06-04 10:30:42 +0100774 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000775
Paul Durrant11b57f92014-01-08 12:41:58 +0000776 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100777
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000778 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000779 dev_kfree_skb(skb);
780 }
781
Paul Durrantca2f09f2013-12-06 16:36:07 +0000782done:
Wei Liub3f980b2013-08-26 12:59:38 +0100783 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100784 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000785}
786
Wei Liue9ce7cb2014-06-04 10:30:42 +0100787void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000788{
789 int more_to_do;
790
Wei Liue9ce7cb2014-06-04 10:30:42 +0100791 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000792
793 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100794 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000795}
796
Wei Liue9ce7cb2014-06-04 10:30:42 +0100797static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000798{
799 unsigned long max_burst, max_credit;
800
801 /*
802 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
803 * Otherwise the interface can seize up due to insufficient credit.
804 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100805 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000806 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100807 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000808
809 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100810 max_credit = queue->remaining_credit + queue->credit_bytes;
811 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000812 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
813
Wei Liue9ce7cb2014-06-04 10:30:42 +0100814 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000815}
816
817static void tx_credit_callback(unsigned long data)
818{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100819 struct xenvif_queue *queue = (struct xenvif_queue *)data;
820 tx_add_credit(queue);
821 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000822}
823
Wei Liue9ce7cb2014-06-04 10:30:42 +0100824static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100825 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000826{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100827 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000828 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000829
830 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100831 spin_lock_irqsave(&queue->response_lock, flags);
832 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
833 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000834 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000835 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100836 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000837 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100838 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000839}
840
Wei Liu73764192013-08-26 12:59:39 +0100841static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000842{
843 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100844 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100845 /* Disable the vif from queue 0's kthread */
846 if (vif->queues)
847 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000848}
849
Wei Liue9ce7cb2014-06-04 10:30:42 +0100850static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100851 struct xen_netif_tx_request *first,
852 struct xen_netif_tx_request *txp,
853 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000854{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100855 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000856 int slots = 0;
857 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000858 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000859
860 if (!(first->flags & XEN_NETTXF_more_data))
861 return 0;
862
863 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000864 struct xen_netif_tx_request dropped_tx = { 0 };
865
Wei Liu2810e5b2013-04-22 02:20:42 +0000866 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100867 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000868 "Asked for %d slots but exceeds this limit\n",
869 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100870 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000871 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000872 }
873
Wei Liu2810e5b2013-04-22 02:20:42 +0000874 /* This guest is really using too many slots and
875 * considered malicious.
876 */
Wei Liu37641492013-05-02 00:43:59 +0000877 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100878 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000879 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000880 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100881 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000882 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000883 }
884
Wei Liu2810e5b2013-04-22 02:20:42 +0000885 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000886 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
887 * the historical MAX_SKB_FRAGS value 18 to honor the
888 * same behavior as before. Any packet using more than
889 * 18 slots but less than fatal_skb_slots slots is
890 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000891 */
Wei Liu37641492013-05-02 00:43:59 +0000892 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000893 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100894 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000895 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000896 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000897 drop_err = -E2BIG;
898 }
899
Wei Liu59ccb4e2013-05-02 00:43:58 +0000900 if (drop_err)
901 txp = &dropped_tx;
902
Wei Liue9ce7cb2014-06-04 10:30:42 +0100903 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000904 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000905
906 /* If the guest submitted a frame >= 64 KiB then
907 * first->size overflowed and following slots will
908 * appear to be larger than the frame.
909 *
910 * This cannot be fatal error as there are buggy
911 * frontends that do this.
912 *
913 * Consume all slots and drop the packet.
914 */
915 if (!drop_err && txp->size > first->size) {
916 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100917 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000918 "Invalid tx request, slot size %u > remaining size %u\n",
919 txp->size, first->size);
920 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000921 }
922
923 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000924 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000925
926 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100927 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000928 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100929 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000930 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000931 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000932
933 more_data = txp->flags & XEN_NETTXF_more_data;
934
935 if (!drop_err)
936 txp++;
937
938 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000939
940 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100941 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000942 return drop_err;
943 }
944
945 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000946}
947
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000948
949struct xenvif_tx_cb {
950 u16 pending_idx;
951};
952
953#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
954
Wei Liue9ce7cb2014-06-04 10:30:42 +0100955static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100956 u16 pending_idx,
957 struct xen_netif_tx_request *txp,
958 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000959{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100960 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
961 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000962 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100963 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000964
Wei Liue9ce7cb2014-06-04 10:30:42 +0100965 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000966 sizeof(*txp));
967}
968
Zoltan Kisse3377f32014-03-06 21:48:29 +0000969static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
970{
971 struct sk_buff *skb =
972 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
973 GFP_ATOMIC | __GFP_NOWARN);
974 if (unlikely(skb == NULL))
975 return NULL;
976
977 /* Packets passed to netif_rx() must have some headroom. */
978 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
979
980 /* Initialize it here to avoid later surprises */
981 skb_shinfo(skb)->destructor_arg = NULL;
982
983 return skb;
984}
985
Wei Liue9ce7cb2014-06-04 10:30:42 +0100986static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000987 struct sk_buff *skb,
988 struct xen_netif_tx_request *txp,
989 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000990{
991 struct skb_shared_info *shinfo = skb_shinfo(skb);
992 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000993 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000994 int start;
995 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000996 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000997
998 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000999 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +00001000 */
Zoltan Kisse3377f32014-03-06 21:48:29 +00001001 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
1002 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
1003 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1004 shinfo->nr_frags = MAX_SKB_FRAGS;
1005 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001006 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +00001007
1008 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001009 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001010
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001011 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
1012 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001013 index = pending_index(queue->pending_cons++);
1014 pending_idx = queue->pending_ring[index];
1015 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001016 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001017 }
1018
Zoltan Kisse3377f32014-03-06 21:48:29 +00001019 if (frag_overflow) {
1020 struct sk_buff *nskb = xenvif_alloc_skb(0);
1021 if (unlikely(nskb == NULL)) {
1022 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001023 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001024 "Can't allocate the frag_list skb.\n");
1025 return NULL;
1026 }
1027
1028 shinfo = skb_shinfo(nskb);
1029 frags = shinfo->frags;
1030
1031 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
1032 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001033 index = pending_index(queue->pending_cons++);
1034 pending_idx = queue->pending_ring[index];
1035 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001036 frag_set_pending_idx(&frags[shinfo->nr_frags],
1037 pending_idx);
1038 }
1039
1040 skb_shinfo(skb)->frag_list = nskb;
1041 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001042
Ian Campbellf942dc22011-03-15 00:06:18 +00001043 return gop;
1044}
1045
Wei Liue9ce7cb2014-06-04 10:30:42 +01001046static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001047 u16 pending_idx,
1048 grant_handle_t handle)
1049{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001050 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001051 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001052 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001053 "Trying to overwrite active handle! pending_idx: %x\n",
1054 pending_idx);
1055 BUG();
1056 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001057 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001058}
1059
Wei Liue9ce7cb2014-06-04 10:30:42 +01001060static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001061 u16 pending_idx)
1062{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001063 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001064 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001065 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001066 "Trying to unmap invalid handle! pending_idx: %x\n",
1067 pending_idx);
1068 BUG();
1069 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001070 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001071}
1072
Wei Liue9ce7cb2014-06-04 10:30:42 +01001073static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001074 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001075 struct gnttab_map_grant_ref **gopp_map,
1076 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +00001077{
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001078 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001079 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001080 /* This always points to the shinfo of the skb being checked, which
1081 * could be either the first or the one on the frag_list
1082 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001083 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001084 /* If this is non-NULL, we are currently checking the frag_list skb, and
1085 * this points to the shinfo of the first one
1086 */
1087 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001088 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001089 const bool sharedslot = nr_frags &&
1090 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001091 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001092
1093 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001094 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001095 if (unlikely(err)) {
1096 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001097 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001098 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001099 (*gopp_copy)->status,
1100 pending_idx,
1101 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001102 /* The first frag might still have this slot mapped */
1103 if (!sharedslot)
1104 xenvif_idx_release(queue, pending_idx,
1105 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001106 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +01001107 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001108
Zoltan Kisse3377f32014-03-06 21:48:29 +00001109check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001110 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001111 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001112
Ian Campbellea066ad2011-10-05 00:28:46 +00001113 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001114
1115 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001116 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001117
Ian Campbellf942dc22011-03-15 00:06:18 +00001118 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001119 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001120 pending_idx,
1121 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001122 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001123 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001124 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001125 /* If the mapping of the first frag was OK, but
1126 * the header's copy failed, and they are
1127 * sharing a slot, send an error
1128 */
1129 if (i == 0 && sharedslot)
1130 xenvif_idx_release(queue, pending_idx,
1131 XEN_NETIF_RSP_ERROR);
1132 else
1133 xenvif_idx_release(queue, pending_idx,
1134 XEN_NETIF_RSP_OKAY);
1135 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001136 continue;
1137 }
1138
1139 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001140 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001141 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001142 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001143 i,
1144 gop_map->status,
1145 pending_idx,
1146 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001147
Wei Liue9ce7cb2014-06-04 10:30:42 +01001148 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001149
1150 /* Not the first error? Preceding frags already invalidated. */
1151 if (err)
1152 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001153
1154 /* First error: if the header haven't shared a slot with the
1155 * first frag, release it as well.
1156 */
1157 if (!sharedslot)
1158 xenvif_idx_release(queue,
1159 XENVIF_TX_CB(skb)->pending_idx,
1160 XEN_NETIF_RSP_OKAY);
1161
1162 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001163 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001164 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001165 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001166 xenvif_idx_release(queue, pending_idx,
1167 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001168 }
1169
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001170 /* And if we found the error while checking the frag_list, unmap
1171 * the first skb's frags
1172 */
1173 if (first_shinfo) {
1174 for (j = 0; j < first_shinfo->nr_frags; j++) {
1175 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1176 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001177 xenvif_idx_release(queue, pending_idx,
1178 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001179 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001180 }
1181
1182 /* Remember the error: invalidate all subsequent fragments. */
1183 err = newerr;
1184 }
1185
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001186 if (skb_has_frag_list(skb) && !first_shinfo) {
1187 first_shinfo = skb_shinfo(skb);
1188 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001189 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001190
1191 goto check_frags;
1192 }
1193
Zoltan Kissbdab8272014-04-02 18:04:58 +01001194 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001195 return err;
1196}
1197
Wei Liue9ce7cb2014-06-04 10:30:42 +01001198static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001199{
1200 struct skb_shared_info *shinfo = skb_shinfo(skb);
1201 int nr_frags = shinfo->nr_frags;
1202 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001203 u16 prev_pending_idx = INVALID_PENDING_IDX;
1204
Ian Campbellf942dc22011-03-15 00:06:18 +00001205 for (i = 0; i < nr_frags; i++) {
1206 skb_frag_t *frag = shinfo->frags + i;
1207 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001208 struct page *page;
1209 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001210
Ian Campbellea066ad2011-10-05 00:28:46 +00001211 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001212
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001213 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001214 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001215 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001216 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001217 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001218 callback_param(queue, prev_pending_idx).ctx =
1219 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001220
Wei Liue9ce7cb2014-06-04 10:30:42 +01001221 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001222 prev_pending_idx = pending_idx;
1223
Wei Liue9ce7cb2014-06-04 10:30:42 +01001224 txp = &queue->pending_tx_info[pending_idx].req;
1225 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001226 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001227 skb->len += txp->size;
1228 skb->data_len += txp->size;
1229 skb->truesize += txp->size;
1230
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001231 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001232 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001233 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001234 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1235 * overlaps with "index", and "mapping" is not set. I think mapping
1236 * should be set. If delivered to local stack, it would drop this
1237 * skb in sk_filter unless the socket has the right to use it.
1238 */
1239 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001240}
1241
Wei Liue9ce7cb2014-06-04 10:30:42 +01001242static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001243 struct xen_netif_extra_info *extras,
1244 int work_to_do)
1245{
1246 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001247 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001248
1249 do {
1250 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001251 netdev_err(queue->vif->dev, "Missing extra info\n");
1252 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001253 return -EBADR;
1254 }
1255
Wei Liue9ce7cb2014-06-04 10:30:42 +01001256 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001257 sizeof(extra));
1258 if (unlikely(!extra.type ||
1259 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001260 queue->tx.req_cons = ++cons;
1261 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001262 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001263 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001264 return -EINVAL;
1265 }
1266
1267 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001268 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001269 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1270
1271 return work_to_do;
1272}
1273
Wei Liu73764192013-08-26 12:59:39 +01001274static int xenvif_set_skb_gso(struct xenvif *vif,
1275 struct sk_buff *skb,
1276 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001277{
1278 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001279 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001280 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001281 return -EINVAL;
1282 }
1283
Paul Durranta9468582013-10-16 17:50:31 +01001284 switch (gso->u.gso.type) {
1285 case XEN_NETIF_GSO_TYPE_TCPV4:
1286 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1287 break;
1288 case XEN_NETIF_GSO_TYPE_TCPV6:
1289 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1290 break;
1291 default:
Ian Campbell488562862013-02-06 23:41:35 +00001292 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001293 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001294 return -EINVAL;
1295 }
1296
1297 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001298 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001299
1300 return 0;
1301}
1302
Wei Liue9ce7cb2014-06-04 10:30:42 +01001303static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001304{
Paul Durrant27216372014-01-09 10:02:47 +00001305 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001306
Paul Durrant2eba61d2013-10-16 17:50:29 +01001307 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001308 * peers can fail to set NETRXF_csum_blank when sending a GSO
1309 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1310 * recalculate the partial checksum.
1311 */
1312 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001313 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001314 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001315 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001316 }
1317
1318 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1319 if (skb->ip_summed != CHECKSUM_PARTIAL)
1320 return 0;
1321
Paul Durrant27216372014-01-09 10:02:47 +00001322 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001323}
1324
Wei Liue9ce7cb2014-06-04 10:30:42 +01001325static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001326{
Wei Liu059dfa62013-10-28 12:07:57 +00001327 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001328 u64 next_credit = queue->credit_window_start +
1329 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001330
1331 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001332 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001333 return true;
1334
1335 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001336 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001337 queue->credit_window_start = now;
1338 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001339 }
1340
1341 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001342 if (size > queue->remaining_credit) {
1343 queue->credit_timeout.data =
1344 (unsigned long)queue;
1345 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001346 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001347 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001348 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001349 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001350
1351 return true;
1352 }
1353
1354 return false;
1355}
1356
Wei Liue9ce7cb2014-06-04 10:30:42 +01001357static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001358 int budget,
1359 unsigned *copy_ops,
1360 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001361{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001362 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001363 struct sk_buff *skb;
1364 int ret;
1365
Wei Liue9ce7cb2014-06-04 10:30:42 +01001366 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001367 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001368 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001369 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1370 u16 pending_idx;
1371 RING_IDX idx;
1372 int work_to_do;
1373 unsigned int data_len;
1374 pending_ring_idx_t index;
1375
Wei Liue9ce7cb2014-06-04 10:30:42 +01001376 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001377 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001378 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001379 "Impossible number of requests. "
1380 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001381 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001382 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001383 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001384 break;
Ian Campbell488562862013-02-06 23:41:35 +00001385 }
1386
Wei Liue9ce7cb2014-06-04 10:30:42 +01001387 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001388 if (!work_to_do)
1389 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001390
Wei Liue9ce7cb2014-06-04 10:30:42 +01001391 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001392 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001393 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001394
1395 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001396 if (txreq.size > queue->remaining_credit &&
1397 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001398 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001399
Wei Liue9ce7cb2014-06-04 10:30:42 +01001400 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001401
1402 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001403 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001404
1405 memset(extras, 0, sizeof(extras));
1406 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001407 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001408 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001410 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001411 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001412 }
1413
Wei Liue9ce7cb2014-06-04 10:30:42 +01001414 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001415 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001416 break;
Ian Campbell488562862013-02-06 23:41:35 +00001417
Ian Campbellf942dc22011-03-15 00:06:18 +00001418 idx += ret;
1419
1420 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001421 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001422 "Bad packet size: %d\n", txreq.size);
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
1427 /* No crossing a page as the payload mustn't fragment. */
1428 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001429 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001430 "txreq.offset: %x, size: %u, end: %lu\n",
1431 txreq.offset, txreq.size,
1432 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001433 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001434 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001435 }
1436
Wei Liue9ce7cb2014-06-04 10:30:42 +01001437 index = pending_index(queue->pending_cons);
1438 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001439
1440 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001441 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001442 PKT_PROT_LEN : txreq.size;
1443
Zoltan Kisse3377f32014-03-06 21:48:29 +00001444 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001445 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001446 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001447 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001448 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001449 break;
1450 }
1451
Ian Campbellf942dc22011-03-15 00:06:18 +00001452 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1453 struct xen_netif_extra_info *gso;
1454 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1455
Wei Liue9ce7cb2014-06-04 10:30:42 +01001456 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001457 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001458 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001459 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001460 }
1461 }
1462
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001463 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001464
1465 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001466 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1467 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1468 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001469
Wei Liue9ce7cb2014-06-04 10:30:42 +01001470 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001471 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001472 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1473 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001474 offset_in_page(skb->data);
1475
Wei Liue9ce7cb2014-06-04 10:30:42 +01001476 queue->tx_copy_ops[*copy_ops].len = data_len;
1477 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001478
1479 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001480
1481 skb_shinfo(skb)->nr_frags = ret;
1482 if (data_len < txreq.size) {
1483 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001484 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1485 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001486 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001487 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001488 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001489 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1490 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001491 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001492 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001493 }
1494
Wei Liue9ce7cb2014-06-04 10:30:42 +01001495 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001496
Wei Liue9ce7cb2014-06-04 10:30:42 +01001497 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001498 if (request_gop == NULL) {
1499 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001500 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001501 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001502 }
1503 gop = request_gop;
1504
Wei Liue9ce7cb2014-06-04 10:30:42 +01001505 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001506
Wei Liue9ce7cb2014-06-04 10:30:42 +01001507 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001508
Wei Liue9ce7cb2014-06-04 10:30:42 +01001509 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1510 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001511 break;
1512 }
1513
Wei Liue9ce7cb2014-06-04 10:30:42 +01001514 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001515 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001516}
1517
Zoltan Kisse3377f32014-03-06 21:48:29 +00001518/* Consolidate skb with a frag_list into a brand new one with local pages on
1519 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1520 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001521static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001522{
1523 unsigned int offset = skb_headlen(skb);
1524 skb_frag_t frags[MAX_SKB_FRAGS];
1525 int i;
1526 struct ubuf_info *uarg;
1527 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1528
Wei Liue9ce7cb2014-06-04 10:30:42 +01001529 queue->stats.tx_zerocopy_sent += 2;
1530 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001531
Wei Liue9ce7cb2014-06-04 10:30:42 +01001532 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001533 /* Subtract frags size, we will correct it later */
1534 skb->truesize -= skb->data_len;
1535 skb->len += nskb->len;
1536 skb->data_len += nskb->len;
1537
1538 /* create a brand new frags array and coalesce there */
1539 for (i = 0; offset < skb->len; i++) {
1540 struct page *page;
1541 unsigned int len;
1542
1543 BUG_ON(i >= MAX_SKB_FRAGS);
1544 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1545 if (!page) {
1546 int j;
1547 skb->truesize += skb->data_len;
1548 for (j = 0; j < i; j++)
1549 put_page(frags[j].page.p);
1550 return -ENOMEM;
1551 }
1552
1553 if (offset + PAGE_SIZE < skb->len)
1554 len = PAGE_SIZE;
1555 else
1556 len = skb->len - offset;
1557 if (skb_copy_bits(skb, offset, page_address(page), len))
1558 BUG();
1559
1560 offset += len;
1561 frags[i].page.p = page;
1562 frags[i].page_offset = 0;
1563 skb_frag_size_set(&frags[i], len);
1564 }
1565 /* swap out with old one */
1566 memcpy(skb_shinfo(skb)->frags,
1567 frags,
1568 i * sizeof(skb_frag_t));
1569 skb_shinfo(skb)->nr_frags = i;
1570 skb->truesize += i * PAGE_SIZE;
1571
1572 /* remove traces of mapped pages and frag_list */
1573 skb_frag_list_init(skb);
1574 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001575 /* increase inflight counter to offset decrement in callback */
1576 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001577 uarg->callback(uarg, true);
1578 skb_shinfo(skb)->destructor_arg = NULL;
1579
Wei Liua64bd932014-08-12 11:48:07 +01001580 xenvif_skb_zerocopy_prepare(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001581 kfree_skb(nskb);
1582
1583 return 0;
1584}
Ian Campbellf942dc22011-03-15 00:06:18 +00001585
Wei Liue9ce7cb2014-06-04 10:30:42 +01001586static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001587{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001588 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1589 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001590 struct sk_buff *skb;
1591 int work_done = 0;
1592
Wei Liue9ce7cb2014-06-04 10:30:42 +01001593 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001594 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001595 u16 pending_idx;
1596 unsigned data_len;
1597
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001598 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001599 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001600
1601 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001602 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001603 /* If there was an error, xenvif_tx_check_gop is
1604 * expected to release all the frags which were mapped,
1605 * so kfree_skb shouldn't do it again
1606 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001607 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001608 if (skb_has_frag_list(skb)) {
1609 struct sk_buff *nskb =
1610 skb_shinfo(skb)->frag_list;
1611 skb_shinfo(nskb)->nr_frags = 0;
1612 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001613 kfree_skb(skb);
1614 continue;
1615 }
1616
1617 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001618 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001619 if (data_len < txp->size) {
1620 /* Append the packet payload as a fragment. */
1621 txp->offset += data_len;
1622 txp->size -= data_len;
1623 } else {
1624 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001625 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001626 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001627 }
1628
1629 if (txp->flags & XEN_NETTXF_csum_blank)
1630 skb->ip_summed = CHECKSUM_PARTIAL;
1631 else if (txp->flags & XEN_NETTXF_data_validated)
1632 skb->ip_summed = CHECKSUM_UNNECESSARY;
1633
Wei Liue9ce7cb2014-06-04 10:30:42 +01001634 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001635
Zoltan Kisse3377f32014-03-06 21:48:29 +00001636 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001637 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001638 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001639 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001640 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001641 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001642 kfree_skb(skb);
1643 continue;
1644 }
1645 }
1646
Paul Durrant2eba61d2013-10-16 17:50:29 +01001647 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001648 int target = min_t(int, skb->len, PKT_PROT_LEN);
1649 __pskb_pull_tail(skb, target - skb_headlen(skb));
1650 }
1651
Wei Liue9ce7cb2014-06-04 10:30:42 +01001652 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001653 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001654 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001655
Wei Liue9ce7cb2014-06-04 10:30:42 +01001656 if (checksum_setup(queue, skb)) {
1657 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001658 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001659 /* We have to set this flag to trigger the callback */
1660 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001661 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001662 kfree_skb(skb);
1663 continue;
1664 }
1665
Jason Wang40893fd2013-03-26 23:11:22 +00001666 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001667
Paul Durrantb89587a2013-12-17 11:44:35 +00001668 /* If the packet is GSO then we will have just set up the
1669 * transport header offset in checksum_setup so it's now
1670 * straightforward to calculate gso_segs.
1671 */
1672 if (skb_is_gso(skb)) {
1673 int mss = skb_shinfo(skb)->gso_size;
1674 int hdrlen = skb_transport_header(skb) -
1675 skb_mac_header(skb) +
1676 tcp_hdrlen(skb);
1677
1678 skb_shinfo(skb)->gso_segs =
1679 DIV_ROUND_UP(skb->len - hdrlen, mss);
1680 }
1681
Wei Liue9ce7cb2014-06-04 10:30:42 +01001682 queue->stats.rx_bytes += skb->len;
1683 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001684
Wei Liub3f980b2013-08-26 12:59:38 +01001685 work_done++;
1686
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001687 /* Set this flag right before netif_receive_skb, otherwise
1688 * someone might think this packet already left netback, and
1689 * do a skb_copy_ubufs while we are still in control of the
1690 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1691 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001692 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001693 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001694 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001695 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001696
Wei Liub3f980b2013-08-26 12:59:38 +01001697 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001698 }
Wei Liub3f980b2013-08-26 12:59:38 +01001699
1700 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001701}
1702
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001703void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1704{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001705 unsigned long flags;
1706 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001707 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001708
1709 /* This is the only place where we grab this lock, to protect callbacks
1710 * from each other.
1711 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001712 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001713 do {
1714 u16 pending_idx = ubuf->desc;
1715 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001716 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001717 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001718 index = pending_index(queue->dealloc_prod);
1719 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001720 /* Sync with xenvif_tx_dealloc_action:
1721 * insert idx then incr producer.
1722 */
1723 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001724 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001725 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001726 wake_up(&queue->dealloc_wq);
1727 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001728
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001729 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001730 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001731 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001732 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001733 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001734}
1735
Wei Liue9ce7cb2014-06-04 10:30:42 +01001736static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001737{
1738 struct gnttab_unmap_grant_ref *gop;
1739 pending_ring_idx_t dc, dp;
1740 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1741 unsigned int i = 0;
1742
Wei Liue9ce7cb2014-06-04 10:30:42 +01001743 dc = queue->dealloc_cons;
1744 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001745
1746 /* Free up any grants we have finished using */
1747 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001748 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001749
1750 /* Ensure we see all indices enqueued by all
1751 * xenvif_zerocopy_callback().
1752 */
1753 smp_rmb();
1754
1755 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001756 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001757 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001758 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001759
Wei Liue9ce7cb2014-06-04 10:30:42 +01001760 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001761 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001762 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1763 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001764 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001765 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001766 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001767 queue->grant_tx_handle[pending_idx]);
1768 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001769 ++gop;
1770 }
1771
Wei Liue9ce7cb2014-06-04 10:30:42 +01001772 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001773
Wei Liue9ce7cb2014-06-04 10:30:42 +01001774 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001775
Wei Liue9ce7cb2014-06-04 10:30:42 +01001776 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001777 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001778 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001779 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001780 queue->pages_to_unmap,
1781 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001782 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001783 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1784 gop - queue->tx_unmap_ops, ret);
1785 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001786 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001787 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001788 " host_addr: %llx handle: %x status: %d\n",
1789 gop[i].host_addr,
1790 gop[i].handle,
1791 gop[i].status);
1792 }
1793 BUG();
1794 }
1795 }
1796
Wei Liue9ce7cb2014-06-04 10:30:42 +01001797 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1798 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001799 XEN_NETIF_RSP_OKAY);
1800}
1801
1802
Ian Campbellf942dc22011-03-15 00:06:18 +00001803/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001804int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001805{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001806 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001807 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001808
Wei Liue9ce7cb2014-06-04 10:30:42 +01001809 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001810 return 0;
1811
Wei Liue9ce7cb2014-06-04 10:30:42 +01001812 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001813
Zoltan Kissbdab8272014-04-02 18:04:58 +01001814 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001815 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001816
Wei Liue9ce7cb2014-06-04 10:30:42 +01001817 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001818 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001819 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001820 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001821 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001822 nr_mops);
1823 BUG_ON(ret);
1824 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001825
Wei Liue9ce7cb2014-06-04 10:30:42 +01001826 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001827
1828 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001829}
1830
Wei Liue9ce7cb2014-06-04 10:30:42 +01001831static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001832 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001833{
Ian Campbellf942dc22011-03-15 00:06:18 +00001834 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001835 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001836 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001837
Wei Liue9ce7cb2014-06-04 10:30:42 +01001838 pending_tx_info = &queue->pending_tx_info[pending_idx];
1839 spin_lock_irqsave(&queue->response_lock, flags);
1840 make_tx_response(queue, &pending_tx_info->req, status);
1841 index = pending_index(queue->pending_prod);
1842 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001843 /* TX shouldn't use the index before we give it back here */
1844 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001845 queue->pending_prod++;
1846 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001847}
1848
Wei Liu2810e5b2013-04-22 02:20:42 +00001849
Wei Liue9ce7cb2014-06-04 10:30:42 +01001850static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001851 struct xen_netif_tx_request *txp,
1852 s8 st)
1853{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001854 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001855 struct xen_netif_tx_response *resp;
1856 int notify;
1857
Wei Liue9ce7cb2014-06-04 10:30:42 +01001858 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001859 resp->id = txp->id;
1860 resp->status = st;
1861
1862 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001863 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001864
Wei Liue9ce7cb2014-06-04 10:30:42 +01001865 queue->tx.rsp_prod_pvt = ++i;
1866 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001867 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001868 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001869}
1870
Wei Liue9ce7cb2014-06-04 10:30:42 +01001871static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001872 u16 id,
1873 s8 st,
1874 u16 offset,
1875 u16 size,
1876 u16 flags)
1877{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001878 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001879 struct xen_netif_rx_response *resp;
1880
Wei Liue9ce7cb2014-06-04 10:30:42 +01001881 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001882 resp->offset = offset;
1883 resp->flags = flags;
1884 resp->id = id;
1885 resp->status = (s16)size;
1886 if (st < 0)
1887 resp->status = (s16)st;
1888
Wei Liue9ce7cb2014-06-04 10:30:42 +01001889 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001890
1891 return resp;
1892}
1893
Wei Liue9ce7cb2014-06-04 10:30:42 +01001894void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001895{
1896 int ret;
1897 struct gnttab_unmap_grant_ref tx_unmap_op;
1898
1899 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001900 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001901 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001902 queue->grant_tx_handle[pending_idx]);
1903 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001904
1905 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001906 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001907 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001908 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001909 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1910 ret,
1911 pending_idx,
1912 tx_unmap_op.host_addr,
1913 tx_unmap_op.handle,
1914 tx_unmap_op.status);
1915 BUG();
1916 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001917}
1918
Wei Liue9ce7cb2014-06-04 10:30:42 +01001919static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001920{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001921 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001922 return 1;
1923
1924 return 0;
1925}
1926
Wei Liue9ce7cb2014-06-04 10:30:42 +01001927static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001928{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001929 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001930}
1931
Wei Liue9ce7cb2014-06-04 10:30:42 +01001932void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001933{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001934 if (queue->tx.sring)
1935 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1936 queue->tx.sring);
1937 if (queue->rx.sring)
1938 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1939 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001940}
1941
Wei Liue9ce7cb2014-06-04 10:30:42 +01001942int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001943 grant_ref_t tx_ring_ref,
1944 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001945{
David Vrabelc9d63692011-09-29 16:53:31 +01001946 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001947 struct xen_netif_tx_sring *txs;
1948 struct xen_netif_rx_sring *rxs;
1949
1950 int err = -ENOMEM;
1951
Wei Liue9ce7cb2014-06-04 10:30:42 +01001952 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001953 tx_ring_ref, &addr);
1954 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001955 goto err;
1956
David Vrabelc9d63692011-09-29 16:53:31 +01001957 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001958 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001959
Wei Liue9ce7cb2014-06-04 10:30:42 +01001960 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001961 rx_ring_ref, &addr);
1962 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001963 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001964
David Vrabelc9d63692011-09-29 16:53:31 +01001965 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001966 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001967
1968 return 0;
1969
1970err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001971 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001972 return err;
1973}
1974
David Vrabelf48da8b2014-10-22 14:08:54 +01001975static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001976{
David Vrabelf48da8b2014-10-22 14:08:54 +01001977 return (!skb_queue_empty(&queue->rx_queue)
1978 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
1979 || kthread_should_stop()
1980 || queue->vif->disabled;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001981}
1982
David Vrabelf48da8b2014-10-22 14:08:54 +01001983static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001984{
David Vrabelf48da8b2014-10-22 14:08:54 +01001985 struct sk_buff *skb;
1986 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001987
David Vrabelf48da8b2014-10-22 14:08:54 +01001988 skb = skb_peek(&queue->rx_queue);
1989 if (!skb)
1990 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001991
David Vrabelf48da8b2014-10-22 14:08:54 +01001992 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1993 return timeout < 0 ? 0 : timeout;
1994}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001995
David Vrabelf48da8b2014-10-22 14:08:54 +01001996/* Wait until the guest Rx thread has work.
1997 *
1998 * The timeout needs to be adjusted based on the current head of the
1999 * queue (and not just the head at the beginning). In particular, if
2000 * the queue is initially empty an infinite timeout is used and this
2001 * needs to be reduced when a skb is queued.
2002 *
2003 * This cannot be done with wait_event_timeout() because it only
2004 * calculates the timeout once.
2005 */
2006static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
2007{
2008 DEFINE_WAIT(wait);
2009
2010 if (xenvif_have_rx_work(queue))
2011 return;
2012
2013 for (;;) {
2014 long ret;
2015
2016 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
2017 if (xenvif_have_rx_work(queue))
2018 break;
2019 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
2020 if (!ret)
2021 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002022 }
David Vrabelf48da8b2014-10-22 14:08:54 +01002023 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002024}
2025
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00002026int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01002027{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002028 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01002029 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01002030
David Vrabelf48da8b2014-10-22 14:08:54 +01002031 for (;;) {
2032 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01002033
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002034 if (kthread_should_stop())
2035 break;
2036
Wei Liue9d8b2c2014-04-01 12:46:12 +01002037 /* This frontend is found to be rogue, disable it in
2038 * kthread context. Currently this is only set when
2039 * netback finds out frontend sends malformed packet,
2040 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01002041 * context so we defer it here, if this thread is
2042 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01002043 */
David Vrabelf48da8b2014-10-22 14:08:54 +01002044 if (unlikely(vif->disabled && queue->id == 0)) {
2045 xenvif_carrier_off(vif);
2046 xenvif_rx_queue_purge(queue);
2047 continue;
Zoltan Kiss09350782014-03-06 21:48:30 +00002048 }
2049
Wei Liue9ce7cb2014-06-04 10:30:42 +01002050 if (!skb_queue_empty(&queue->rx_queue))
2051 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01002052
David Vrabelf48da8b2014-10-22 14:08:54 +01002053 /* Queued packets may have foreign pages from other
2054 * domains. These cannot be queued indefinitely as
2055 * this would starve guests of grant refs and transmit
2056 * slots.
2057 */
2058 xenvif_rx_queue_drop_expired(queue);
2059
2060 xenvif_rx_queue_maybe_wake(queue);
2061
Wei Liub3f980b2013-08-26 12:59:38 +01002062 cond_resched();
2063 }
2064
Paul Durrantca2f09f2013-12-06 16:36:07 +00002065 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01002066 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00002067
Wei Liub3f980b2013-08-26 12:59:38 +01002068 return 0;
2069}
2070
Wei Liua64bd932014-08-12 11:48:07 +01002071static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2072{
2073 /* Dealloc thread must remain running until all inflight
2074 * packets complete.
2075 */
2076 return kthread_should_stop() &&
2077 !atomic_read(&queue->inflight_packets);
2078}
2079
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002080int xenvif_dealloc_kthread(void *data)
2081{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002082 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002083
Wei Liua64bd932014-08-12 11:48:07 +01002084 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01002085 wait_event_interruptible(queue->dealloc_wq,
2086 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01002087 xenvif_dealloc_kthread_should_stop(queue));
2088 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002089 break;
2090
Wei Liue9ce7cb2014-06-04 10:30:42 +01002091 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002092 cond_resched();
2093 }
2094
2095 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01002096 if (tx_dealloc_work_todo(queue))
2097 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002098
2099 return 0;
2100}
2101
Ian Campbellf942dc22011-03-15 00:06:18 +00002102static int __init netback_init(void)
2103{
Ian Campbellf942dc22011-03-15 00:06:18 +00002104 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00002105
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05002106 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00002107 return -ENODEV;
2108
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01002109 /* Allow as many queues as there are CPUs, by default */
2110 xenvif_max_queues = num_online_cpus();
2111
Wei Liu37641492013-05-02 00:43:59 +00002112 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002113 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2114 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002115 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002116 }
2117
Ian Campbellf942dc22011-03-15 00:06:18 +00002118 rc = xenvif_xenbus_init();
2119 if (rc)
2120 goto failed_init;
2121
Zoltan Kiss09350782014-03-06 21:48:30 +00002122 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
2123
Zoltan Kissf51de242014-07-08 19:49:14 +01002124#ifdef CONFIG_DEBUG_FS
2125 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2126 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2127 pr_warn("Init of debugfs returned %ld!\n",
2128 PTR_ERR(xen_netback_dbg_root));
2129#endif /* CONFIG_DEBUG_FS */
2130
Ian Campbellf942dc22011-03-15 00:06:18 +00002131 return 0;
2132
2133failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002134 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002135}
2136
2137module_init(netback_init);
2138
Wei Liub103f352013-05-16 23:26:11 +00002139static void __exit netback_fini(void)
2140{
Zoltan Kissf51de242014-07-08 19:49:14 +01002141#ifdef CONFIG_DEBUG_FS
2142 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2143 debugfs_remove_recursive(xen_netback_dbg_root);
2144#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002145 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002146}
2147module_exit(netback_fini);
2148
Ian Campbellf942dc22011-03-15 00:06:18 +00002149MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002150MODULE_ALIAS("xen-backend:vif");