blob: 6563f0713fc0f5e324a18e2d87ba1f4614e1a008 [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
David Vrabelecf08d22014-10-22 14:08:55 +010065/* The length of time before the frontend is considered unresponsive
66 * because it isn't providing Rx slots.
67 */
68static unsigned int rx_stall_timeout_msecs = 60000;
69module_param(rx_stall_timeout_msecs, uint, 0444);
70static unsigned int rx_stall_timeout_jiffies;
71
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +010072unsigned int xenvif_max_queues;
73module_param_named(max_queues, xenvif_max_queues, uint, 0644);
74MODULE_PARM_DESC(max_queues,
75 "Maximum number of queues per virtual interface");
76
Wei Liu2810e5b2013-04-22 02:20:42 +000077/*
78 * This is the maximum slots a skb can have. If a guest sends a skb
79 * which exceeds this limit it is considered malicious.
80 */
Wei Liu37641492013-05-02 00:43:59 +000081#define FATAL_SKB_SLOTS_DEFAULT 20
82static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
83module_param(fatal_skb_slots, uint, 0444);
84
Wei Liue9ce7cb2014-06-04 10:30:42 +010085static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +010086 u8 status);
87
Wei Liue9ce7cb2014-06-04 10:30:42 +010088static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000089 struct xen_netif_tx_request *txp,
90 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010091
Wei Liue9ce7cb2014-06-04 10:30:42 +010092static inline int tx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +010093
Wei Liue9ce7cb2014-06-04 10:30:42 +010094static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000095 u16 id,
96 s8 st,
97 u16 offset,
98 u16 size,
99 u16 flags);
100
Wei Liue9ce7cb2014-06-04 10:30:42 +0100101static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000102 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000103{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100104 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000105}
106
Wei Liue9ce7cb2014-06-04 10:30:42 +0100107static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000108 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000109{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100110 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000111}
112
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000113#define callback_param(vif, pending_idx) \
114 (vif->pending_tx_info[pending_idx].callback_struct)
115
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000116/* Find the containing VIF's structure from a pointer in pending_tx_info array
117 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100118static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000119{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000120 u16 pending_idx = ubuf->desc;
121 struct pending_tx_info *temp =
122 container_of(ubuf, struct pending_tx_info, callback_struct);
123 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100124 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000125 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000126}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000127
Paul Durrant2eba61d2013-10-16 17:50:29 +0100128/* This is a miniumum size for the linear area to avoid lots of
129 * calls to __pskb_pull_tail() as we set up checksum offsets. The
130 * value 128 was chosen as it covers all IPv4 and most likely
131 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000132 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100133#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000134
Ian Campbellea066ad2011-10-05 00:28:46 +0000135static u16 frag_get_pending_idx(skb_frag_t *frag)
136{
137 return (u16)frag->page_offset;
138}
139
140static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
141{
142 frag->page_offset = pending_idx;
143}
144
Ian Campbellf942dc22011-03-15 00:06:18 +0000145static inline pending_ring_idx_t pending_index(unsigned i)
146{
147 return i & (MAX_PENDING_REQS-1);
148}
149
Wei Liue9ce7cb2014-06-04 10:30:42 +0100150bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000151{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000152 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000153
Paul Durrantca2f09f2013-12-06 16:36:07 +0000154 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100155 prod = queue->rx.sring->req_prod;
156 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000157
Paul Durrantca2f09f2013-12-06 16:36:07 +0000158 if (prod - cons >= needed)
159 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000160
Wei Liue9ce7cb2014-06-04 10:30:42 +0100161 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000162
Paul Durrantca2f09f2013-12-06 16:36:07 +0000163 /* Make sure event is visible before we check prod
164 * again.
165 */
166 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100167 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000168
Paul Durrantca2f09f2013-12-06 16:36:07 +0000169 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000170}
171
David Vrabelf48da8b2014-10-22 14:08:54 +0100172void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
173{
174 unsigned long flags;
175
176 spin_lock_irqsave(&queue->rx_queue.lock, flags);
177
178 __skb_queue_tail(&queue->rx_queue, skb);
179
180 queue->rx_queue_len += skb->len;
181 if (queue->rx_queue_len > queue->rx_queue_max)
182 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
183
184 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
185}
186
187static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
188{
189 struct sk_buff *skb;
190
191 spin_lock_irq(&queue->rx_queue.lock);
192
193 skb = __skb_dequeue(&queue->rx_queue);
194 if (skb)
195 queue->rx_queue_len -= skb->len;
196
197 spin_unlock_irq(&queue->rx_queue.lock);
198
199 return skb;
200}
201
202static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
203{
204 spin_lock_irq(&queue->rx_queue.lock);
205
206 if (queue->rx_queue_len < queue->rx_queue_max)
207 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
208
209 spin_unlock_irq(&queue->rx_queue.lock);
210}
211
212
213static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
214{
215 struct sk_buff *skb;
216 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
217 kfree_skb(skb);
218}
219
220static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
221{
222 struct sk_buff *skb;
223
224 for(;;) {
225 skb = skb_peek(&queue->rx_queue);
226 if (!skb)
227 break;
228 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
229 break;
230 xenvif_rx_dequeue(queue);
231 kfree_skb(skb);
232 }
233}
234
Ian Campbellf942dc22011-03-15 00:06:18 +0000235/*
236 * Returns true if we should start a new receive buffer instead of
237 * adding 'size' bytes to a buffer which currently contains 'offset'
238 * bytes.
239 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100240static bool start_new_rx_buffer(int offset, unsigned long size, int head,
241 bool full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000242{
243 /* simple case: we have completely filled the current buffer. */
244 if (offset == MAX_BUFFER_OFFSET)
245 return true;
246
247 /*
248 * complex case: start a fresh buffer if the current frag
249 * would overflow the current buffer but only if:
250 * (i) this frag would fit completely in the next buffer
251 * and (ii) there is already some data in the current buffer
252 * and (iii) this is not the head buffer.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100253 * and (iv) there is no need to fully utilize the buffers
Ian Campbellf942dc22011-03-15 00:06:18 +0000254 *
255 * Where:
256 * - (i) stops us splitting a frag into two copies
257 * unless the frag is too large for a single buffer.
258 * - (ii) stops us from leaving a buffer pointlessly empty.
259 * - (iii) stops us leaving the first buffer
260 * empty. Strictly speaking this is already covered
261 * by (ii) but is explicitly checked because
262 * netfront relies on the first buffer being
263 * non-empty and can crash otherwise.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100264 * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS
265 * slot
Ian Campbellf942dc22011-03-15 00:06:18 +0000266 *
267 * This means we will effectively linearise small
268 * frags but do not needlessly split large buffers
269 * into multiple copies tend to give large frags their
270 * own buffers as before.
271 */
Paul Durrant0576edd2014-03-28 11:39:05 +0000272 BUG_ON(size > MAX_BUFFER_OFFSET);
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100273 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head &&
274 !full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000275 return true;
276
277 return false;
278}
279
Ian Campbellf942dc22011-03-15 00:06:18 +0000280struct netrx_pending_operations {
281 unsigned copy_prod, copy_cons;
282 unsigned meta_prod, meta_cons;
283 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100284 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000285 int copy_off;
286 grant_ref_t copy_gref;
287};
288
Wei Liue9ce7cb2014-06-04 10:30:42 +0100289static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100290 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000291{
Wei Liub3f980b2013-08-26 12:59:38 +0100292 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000293 struct xen_netif_rx_request *req;
294
Wei Liue9ce7cb2014-06-04 10:30:42 +0100295 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000296
297 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100298 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000299 meta->gso_size = 0;
300 meta->size = 0;
301 meta->id = req->id;
302
303 npo->copy_off = 0;
304 npo->copy_gref = req->gref;
305
306 return meta;
307}
308
Wei Liu33bc8012013-10-08 10:54:21 +0100309/*
310 * Set up the grant operations for this fragment. If it's a flipping
311 * interface, we also set up the unmap request from here.
312 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100313static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100314 struct netrx_pending_operations *npo,
315 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000316 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100317 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000318 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000319{
320 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100321 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000322 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800323 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000324
325 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000326 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000327
328 meta = npo->meta + npo->meta_prod - 1;
329
Ian Campbell6a8ed462012-10-10 03:48:42 +0000330 /* Skip unused frames from start of page */
331 page += offset >> PAGE_SHIFT;
332 offset &= ~PAGE_MASK;
333
Ian Campbellf942dc22011-03-15 00:06:18 +0000334 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000335 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000336 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
337
Ian Campbell6a8ed462012-10-10 03:48:42 +0000338 bytes = PAGE_SIZE - offset;
339
340 if (bytes > size)
341 bytes = size;
342
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100343 if (start_new_rx_buffer(npo->copy_off,
344 bytes,
345 *head,
346 XENVIF_RX_CB(skb)->full_coalesce)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000347 /*
348 * Netfront requires there to be some data in the head
349 * buffer.
350 */
Wei Liu33bc8012013-10-08 10:54:21 +0100351 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000352
Wei Liue9ce7cb2014-06-04 10:30:42 +0100353 meta = get_next_rx_buffer(queue, npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000354 }
355
Ian Campbellf942dc22011-03-15 00:06:18 +0000356 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
357 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
358
359 copy_gop = npo->copy + npo->copy_prod++;
360 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100361 copy_gop->len = bytes;
362
Wei Liue9ce7cb2014-06-04 10:30:42 +0100363 if (foreign_queue) {
364 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000365 copy_gop->source.u.ref = foreign_gref;
366 copy_gop->flags |= GNTCOPY_source_gref;
367 } else {
368 copy_gop->source.domid = DOMID_SELF;
369 copy_gop->source.u.gmfn =
370 virt_to_mfn(page_address(page));
371 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000372 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000373
Wei Liue9ce7cb2014-06-04 10:30:42 +0100374 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 copy_gop->dest.offset = npo->copy_off;
376 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000377
378 npo->copy_off += bytes;
379 meta->size += bytes;
380
381 offset += bytes;
382 size -= bytes;
383
Ian Campbell6a8ed462012-10-10 03:48:42 +0000384 /* Next frame */
385 if (offset == PAGE_SIZE && size) {
386 BUG_ON(!PageCompound(page));
387 page++;
388 offset = 0;
389 }
390
Ian Campbellf942dc22011-03-15 00:06:18 +0000391 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800392 if (skb_is_gso(skb)) {
393 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
394 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
395 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
396 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
397 }
Paul Durrant82cada22013-10-16 17:50:32 +0100398
Wei Liue9ce7cb2014-06-04 10:30:42 +0100399 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
400 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000401
Wei Liu33bc8012013-10-08 10:54:21 +0100402 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000403
404 }
405}
406
407/*
Zoltan Kiss583757442014-05-15 11:08:34 +0100408 * Find the grant ref for a given frag in a chain of struct ubuf_info's
409 * skb: the skb itself
410 * i: the frag's number
411 * ubuf: a pointer to an element in the chain. It should not be NULL
412 *
413 * Returns a pointer to the element in the chain where the page were found. If
414 * not found, returns NULL.
415 * See the definition of callback_struct in common.h for more details about
416 * the chain.
417 */
418static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
419 const int i,
420 const struct ubuf_info *ubuf)
421{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100422 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss583757442014-05-15 11:08:34 +0100423
424 do {
425 u16 pending_idx = ubuf->desc;
426
427 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100428 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss583757442014-05-15 11:08:34 +0100429 break;
430 ubuf = (struct ubuf_info *) ubuf->ctx;
431 } while (ubuf);
432
433 return ubuf;
434}
435
436/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000437 * Prepare an SKB to be transmitted to the frontend.
438 *
439 * This function is responsible for allocating grant operations, meta
440 * structures, etc.
441 *
442 * It returns the number of meta structures consumed. The number of
443 * ring slots used is always equal to the number of meta slots used
444 * plus the number of GSO descriptors used. Currently, we use either
445 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
446 * frontend-side LRO).
447 */
Wei Liu73764192013-08-26 12:59:39 +0100448static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100449 struct netrx_pending_operations *npo,
450 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000451{
452 struct xenvif *vif = netdev_priv(skb->dev);
453 int nr_frags = skb_shinfo(skb)->nr_frags;
454 int i;
455 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100456 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000457 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100458 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000459 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100460 int gso_type;
Zoltan Kiss583757442014-05-15 11:08:34 +0100461 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
462 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000463
464 old_meta_prod = npo->meta_prod;
465
Annie Li5bd07672014-03-10 22:58:34 +0800466 gso_type = XEN_NETIF_GSO_TYPE_NONE;
467 if (skb_is_gso(skb)) {
468 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
469 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
470 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
471 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100472 }
473
Ian Campbellf942dc22011-03-15 00:06:18 +0000474 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000475 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100476 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000477 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100478 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800479 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000480 meta->size = 0;
481 meta->id = req->id;
482 }
483
Wei Liue9ce7cb2014-06-04 10:30:42 +0100484 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000485 meta = npo->meta + npo->meta_prod++;
486
Paul Durrant82cada22013-10-16 17:50:32 +0100487 if ((1 << gso_type) & vif->gso_mask) {
488 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800489 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100490 } else {
491 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000492 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100493 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000494
495 meta->size = 0;
496 meta->id = req->id;
497 npo->copy_off = 0;
498 npo->copy_gref = req->gref;
499
500 data = skb->data;
501 while (data < skb_tail_pointer(skb)) {
502 unsigned int offset = offset_in_page(data);
503 unsigned int len = PAGE_SIZE - offset;
504
505 if (data + len > skb_tail_pointer(skb))
506 len = skb_tail_pointer(skb) - data;
507
Wei Liue9ce7cb2014-06-04 10:30:42 +0100508 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000509 virt_to_page(data), len, offset, &head,
510 NULL,
511 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000512 data += len;
513 }
514
515 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss583757442014-05-15 11:08:34 +0100516 /* This variable also signals whether foreign_gref has a real
517 * value or not.
518 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100519 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss583757442014-05-15 11:08:34 +0100520 grant_ref_t foreign_gref;
521
522 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
523 (ubuf->callback == &xenvif_zerocopy_callback)) {
524 const struct ubuf_info *const startpoint = ubuf;
525
526 /* Ideally ubuf points to the chain element which
527 * belongs to this frag. Or if frags were removed from
528 * the beginning, then shortly before it.
529 */
530 ubuf = xenvif_find_gref(skb, i, ubuf);
531
532 /* Try again from the beginning of the list, if we
533 * haven't tried from there. This only makes sense in
534 * the unlikely event of reordering the original frags.
535 * For injected local pages it's an unnecessary second
536 * run.
537 */
538 if (unlikely(!ubuf) && startpoint != head_ubuf)
539 ubuf = xenvif_find_gref(skb, i, head_ubuf);
540
541 if (likely(ubuf)) {
542 u16 pending_idx = ubuf->desc;
543
Wei Liue9ce7cb2014-06-04 10:30:42 +0100544 foreign_queue = ubuf_to_queue(ubuf);
545 foreign_gref =
546 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss583757442014-05-15 11:08:34 +0100547 /* Just a safety measure. If this was the last
548 * element on the list, the for loop will
549 * iterate again if a local page were added to
550 * the end. Using head_ubuf here prevents the
551 * second search on the chain. Or the original
552 * frags changed order, but that's less likely.
553 * In any way, ubuf shouldn't be NULL.
554 */
555 ubuf = ubuf->ctx ?
556 (struct ubuf_info *) ubuf->ctx :
557 head_ubuf;
558 } else
559 /* This frag was a local page, added to the
560 * array after the skb left netback.
561 */
562 ubuf = head_ubuf;
563 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100564 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100565 skb_frag_page(&skb_shinfo(skb)->frags[i]),
566 skb_frag_size(&skb_shinfo(skb)->frags[i]),
567 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000568 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100569 foreign_queue,
570 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 }
572
573 return npo->meta_prod - old_meta_prod;
574}
575
576/*
Wei Liu73764192013-08-26 12:59:39 +0100577 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000578 * used to set up the operations on the top of
579 * netrx_pending_operations, which have since been done. Check that
580 * they didn't give any errors and advance over them.
581 */
Wei Liu73764192013-08-26 12:59:39 +0100582static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
583 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000584{
585 struct gnttab_copy *copy_op;
586 int status = XEN_NETIF_RSP_OKAY;
587 int i;
588
589 for (i = 0; i < nr_meta_slots; i++) {
590 copy_op = npo->copy + npo->copy_cons++;
591 if (copy_op->status != GNTST_okay) {
592 netdev_dbg(vif->dev,
593 "Bad status %d from copy to DOM%d.\n",
594 copy_op->status, vif->domid);
595 status = XEN_NETIF_RSP_ERROR;
596 }
597 }
598
599 return status;
600}
601
Wei Liue9ce7cb2014-06-04 10:30:42 +0100602static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100603 struct xenvif_rx_meta *meta,
604 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000605{
606 int i;
607 unsigned long offset;
608
609 /* No fragments used */
610 if (nr_meta_slots <= 1)
611 return;
612
613 nr_meta_slots--;
614
615 for (i = 0; i < nr_meta_slots; i++) {
616 int flags;
617 if (i == nr_meta_slots - 1)
618 flags = 0;
619 else
620 flags = XEN_NETRXF_more_data;
621
622 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100623 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000624 meta[i].size, flags);
625 }
626}
627
Wei Liue9ce7cb2014-06-04 10:30:42 +0100628void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000629{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100630 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100631}
632
Wei Liue9ce7cb2014-06-04 10:30:42 +0100633static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100634{
Ian Campbellf942dc22011-03-15 00:06:18 +0000635 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000636 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000637 struct xen_netif_rx_response *resp;
638 struct sk_buff_head rxq;
639 struct sk_buff *skb;
640 LIST_HEAD(notify);
641 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000642 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000643 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000644
645 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100646 .copy = queue->grant_copy_op,
647 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000648 };
649
650 skb_queue_head_init(&rxq);
651
David Vrabelf48da8b2014-10-22 14:08:54 +0100652 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
653 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000654 RING_IDX max_slots_needed;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000655 RING_IDX old_req_cons;
656 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000657 int i;
658
David Vrabelecf08d22014-10-22 14:08:55 +0100659 queue->last_rx_time = jiffies;
660
Paul Durrantca2f09f2013-12-06 16:36:07 +0000661 /* We need a cheap worse case estimate for the number of
662 * slots we'll use.
663 */
664
665 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
666 skb_headlen(skb),
667 PAGE_SIZE);
668 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
669 unsigned int size;
Paul Durranta02eb472014-03-28 11:39:06 +0000670 unsigned int offset;
671
Paul Durrantca2f09f2013-12-06 16:36:07 +0000672 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Paul Durranta02eb472014-03-28 11:39:06 +0000673 offset = skb_shinfo(skb)->frags[i].page_offset;
674
675 /* For a worse-case estimate we need to factor in
676 * the fragment page offset as this will affect the
677 * number of times xenvif_gop_frag_copy() will
678 * call start_new_rx_buffer().
679 */
680 max_slots_needed += DIV_ROUND_UP(offset + size,
681 PAGE_SIZE);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000682 }
Paul Durranta02eb472014-03-28 11:39:06 +0000683
684 /* To avoid the estimate becoming too pessimal for some
685 * frontends that limit posted rx requests, cap the estimate
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100686 * at MAX_SKB_FRAGS. In this case netback will fully coalesce
687 * the skb into the provided slots.
Paul Durranta02eb472014-03-28 11:39:06 +0000688 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100689 if (max_slots_needed > MAX_SKB_FRAGS) {
Paul Durranta02eb472014-03-28 11:39:06 +0000690 max_slots_needed = MAX_SKB_FRAGS;
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100691 XENVIF_RX_CB(skb)->full_coalesce = true;
692 } else {
693 XENVIF_RX_CB(skb)->full_coalesce = false;
694 }
Paul Durranta02eb472014-03-28 11:39:06 +0000695
696 /* We may need one more slot for GSO metadata */
Annie Li5bd07672014-03-10 22:58:34 +0800697 if (skb_is_gso(skb) &&
698 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
699 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000700 max_slots_needed++;
701
Wei Liue9ce7cb2014-06-04 10:30:42 +0100702 old_req_cons = queue->rx.req_cons;
703 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
704 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000705
706 BUG_ON(ring_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000707
708 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000709 }
710
Wei Liue9ce7cb2014-06-04 10:30:42 +0100711 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000712
713 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000714 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000715
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000716 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100717 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000718
719 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000720
Wei Liue9ce7cb2014-06-04 10:30:42 +0100721 if ((1 << queue->meta[npo.meta_cons].gso_type) &
722 queue->vif->gso_prefix_mask) {
723 resp = RING_GET_RESPONSE(&queue->rx,
724 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000725
726 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
727
Wei Liue9ce7cb2014-06-04 10:30:42 +0100728 resp->offset = queue->meta[npo.meta_cons].gso_size;
729 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000730 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000731
732 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000733 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000734 }
735
736
Wei Liue9ce7cb2014-06-04 10:30:42 +0100737 queue->stats.tx_bytes += skb->len;
738 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000739
Wei Liue9ce7cb2014-06-04 10:30:42 +0100740 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000741 XENVIF_RX_CB(skb)->meta_slots_used,
742 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000743
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000744 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000745 flags = 0;
746 else
747 flags = XEN_NETRXF_more_data;
748
749 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
750 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
751 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
752 /* remote but checksummed. */
753 flags |= XEN_NETRXF_data_validated;
754
755 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100756 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000757 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100758 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000759 flags);
760
Wei Liue9ce7cb2014-06-04 10:30:42 +0100761 if ((1 << queue->meta[npo.meta_cons].gso_type) &
762 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000763 struct xen_netif_extra_info *gso =
764 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100765 RING_GET_RESPONSE(&queue->rx,
766 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000767
768 resp->flags |= XEN_NETRXF_extra_info;
769
Wei Liue9ce7cb2014-06-04 10:30:42 +0100770 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
771 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000772 gso->u.gso.pad = 0;
773 gso->u.gso.features = 0;
774
775 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
776 gso->flags = 0;
777 }
778
Wei Liue9ce7cb2014-06-04 10:30:42 +0100779 xenvif_add_frag_responses(queue, status,
780 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000781 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000782
Wei Liue9ce7cb2014-06-04 10:30:42 +0100783 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000784
Paul Durrant11b57f92014-01-08 12:41:58 +0000785 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100786
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000787 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000788 dev_kfree_skb(skb);
789 }
790
Paul Durrantca2f09f2013-12-06 16:36:07 +0000791done:
Wei Liub3f980b2013-08-26 12:59:38 +0100792 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100793 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000794}
795
Wei Liue9ce7cb2014-06-04 10:30:42 +0100796void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000797{
798 int more_to_do;
799
Wei Liue9ce7cb2014-06-04 10:30:42 +0100800 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000801
802 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100803 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000804}
805
Wei Liue9ce7cb2014-06-04 10:30:42 +0100806static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000807{
808 unsigned long max_burst, max_credit;
809
810 /*
811 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
812 * Otherwise the interface can seize up due to insufficient credit.
813 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100814 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000815 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100816 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000817
818 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100819 max_credit = queue->remaining_credit + queue->credit_bytes;
820 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000821 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
822
Wei Liue9ce7cb2014-06-04 10:30:42 +0100823 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000824}
825
826static void tx_credit_callback(unsigned long data)
827{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100828 struct xenvif_queue *queue = (struct xenvif_queue *)data;
829 tx_add_credit(queue);
830 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000831}
832
Wei Liue9ce7cb2014-06-04 10:30:42 +0100833static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100834 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000835{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100836 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000837 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000838
839 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100840 spin_lock_irqsave(&queue->response_lock, flags);
841 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
842 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000843 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000844 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100845 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000846 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100847 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000848}
849
Wei Liu73764192013-08-26 12:59:39 +0100850static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000851{
852 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100853 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100854 /* Disable the vif from queue 0's kthread */
855 if (vif->queues)
856 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000857}
858
Wei Liue9ce7cb2014-06-04 10:30:42 +0100859static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100860 struct xen_netif_tx_request *first,
861 struct xen_netif_tx_request *txp,
862 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000863{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100864 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000865 int slots = 0;
866 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000867 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000868
869 if (!(first->flags & XEN_NETTXF_more_data))
870 return 0;
871
872 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000873 struct xen_netif_tx_request dropped_tx = { 0 };
874
Wei Liu2810e5b2013-04-22 02:20:42 +0000875 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100876 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000877 "Asked for %d slots but exceeds this limit\n",
878 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100879 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000880 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000881 }
882
Wei Liu2810e5b2013-04-22 02:20:42 +0000883 /* This guest is really using too many slots and
884 * considered malicious.
885 */
Wei Liu37641492013-05-02 00:43:59 +0000886 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100887 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000888 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000889 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100890 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000891 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000892 }
893
Wei Liu2810e5b2013-04-22 02:20:42 +0000894 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000895 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
896 * the historical MAX_SKB_FRAGS value 18 to honor the
897 * same behavior as before. Any packet using more than
898 * 18 slots but less than fatal_skb_slots slots is
899 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000900 */
Wei Liu37641492013-05-02 00:43:59 +0000901 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000902 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100903 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000904 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000905 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000906 drop_err = -E2BIG;
907 }
908
Wei Liu59ccb4e2013-05-02 00:43:58 +0000909 if (drop_err)
910 txp = &dropped_tx;
911
Wei Liue9ce7cb2014-06-04 10:30:42 +0100912 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000913 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000914
915 /* If the guest submitted a frame >= 64 KiB then
916 * first->size overflowed and following slots will
917 * appear to be larger than the frame.
918 *
919 * This cannot be fatal error as there are buggy
920 * frontends that do this.
921 *
922 * Consume all slots and drop the packet.
923 */
924 if (!drop_err && txp->size > first->size) {
925 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100926 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000927 "Invalid tx request, slot size %u > remaining size %u\n",
928 txp->size, first->size);
929 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000930 }
931
932 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000933 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000934
935 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100936 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000937 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100938 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000939 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000940 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000941
942 more_data = txp->flags & XEN_NETTXF_more_data;
943
944 if (!drop_err)
945 txp++;
946
947 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000948
949 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100950 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000951 return drop_err;
952 }
953
954 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000955}
956
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000957
958struct xenvif_tx_cb {
959 u16 pending_idx;
960};
961
962#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
963
Wei Liue9ce7cb2014-06-04 10:30:42 +0100964static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100965 u16 pending_idx,
966 struct xen_netif_tx_request *txp,
967 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000968{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100969 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
970 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000971 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100972 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000973
Wei Liue9ce7cb2014-06-04 10:30:42 +0100974 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000975 sizeof(*txp));
976}
977
Zoltan Kisse3377f32014-03-06 21:48:29 +0000978static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
979{
980 struct sk_buff *skb =
981 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
982 GFP_ATOMIC | __GFP_NOWARN);
983 if (unlikely(skb == NULL))
984 return NULL;
985
986 /* Packets passed to netif_rx() must have some headroom. */
987 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
988
989 /* Initialize it here to avoid later surprises */
990 skb_shinfo(skb)->destructor_arg = NULL;
991
992 return skb;
993}
994
Wei Liue9ce7cb2014-06-04 10:30:42 +0100995static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000996 struct sk_buff *skb,
997 struct xen_netif_tx_request *txp,
998 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000999{
1000 struct skb_shared_info *shinfo = skb_shinfo(skb);
1001 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001002 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001003 int start;
1004 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001005 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +00001006
1007 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +00001008 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +00001009 */
Zoltan Kisse3377f32014-03-06 21:48:29 +00001010 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
1011 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
1012 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1013 shinfo->nr_frags = MAX_SKB_FRAGS;
1014 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001015 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +00001016
1017 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001018 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001019
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001020 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
1021 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001022 index = pending_index(queue->pending_cons++);
1023 pending_idx = queue->pending_ring[index];
1024 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001025 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001026 }
1027
Zoltan Kisse3377f32014-03-06 21:48:29 +00001028 if (frag_overflow) {
1029 struct sk_buff *nskb = xenvif_alloc_skb(0);
1030 if (unlikely(nskb == NULL)) {
1031 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001032 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001033 "Can't allocate the frag_list skb.\n");
1034 return NULL;
1035 }
1036
1037 shinfo = skb_shinfo(nskb);
1038 frags = shinfo->frags;
1039
1040 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
1041 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001042 index = pending_index(queue->pending_cons++);
1043 pending_idx = queue->pending_ring[index];
1044 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001045 frag_set_pending_idx(&frags[shinfo->nr_frags],
1046 pending_idx);
1047 }
1048
1049 skb_shinfo(skb)->frag_list = nskb;
1050 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001051
Ian Campbellf942dc22011-03-15 00:06:18 +00001052 return gop;
1053}
1054
Wei Liue9ce7cb2014-06-04 10:30:42 +01001055static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001056 u16 pending_idx,
1057 grant_handle_t handle)
1058{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001059 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001060 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001061 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001062 "Trying to overwrite active handle! pending_idx: %x\n",
1063 pending_idx);
1064 BUG();
1065 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001066 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001067}
1068
Wei Liue9ce7cb2014-06-04 10:30:42 +01001069static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001070 u16 pending_idx)
1071{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001072 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001073 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001074 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001075 "Trying to unmap invalid handle! pending_idx: %x\n",
1076 pending_idx);
1077 BUG();
1078 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001079 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001080}
1081
Wei Liue9ce7cb2014-06-04 10:30:42 +01001082static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001083 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001084 struct gnttab_map_grant_ref **gopp_map,
1085 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +00001086{
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001087 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001088 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001089 /* This always points to the shinfo of the skb being checked, which
1090 * could be either the first or the one on the frag_list
1091 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001092 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001093 /* If this is non-NULL, we are currently checking the frag_list skb, and
1094 * this points to the shinfo of the first one
1095 */
1096 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001097 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001098 const bool sharedslot = nr_frags &&
1099 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001100 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001101
1102 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001103 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001104 if (unlikely(err)) {
1105 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001106 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001107 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001108 (*gopp_copy)->status,
1109 pending_idx,
1110 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001111 /* The first frag might still have this slot mapped */
1112 if (!sharedslot)
1113 xenvif_idx_release(queue, pending_idx,
1114 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001115 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +01001116 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001117
Zoltan Kisse3377f32014-03-06 21:48:29 +00001118check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001119 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001120 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001121
Ian Campbellea066ad2011-10-05 00:28:46 +00001122 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001123
1124 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001125 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001126
Ian Campbellf942dc22011-03-15 00:06:18 +00001127 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001128 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001129 pending_idx,
1130 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001131 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001132 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001133 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001134 /* If the mapping of the first frag was OK, but
1135 * the header's copy failed, and they are
1136 * sharing a slot, send an error
1137 */
1138 if (i == 0 && sharedslot)
1139 xenvif_idx_release(queue, pending_idx,
1140 XEN_NETIF_RSP_ERROR);
1141 else
1142 xenvif_idx_release(queue, pending_idx,
1143 XEN_NETIF_RSP_OKAY);
1144 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001145 continue;
1146 }
1147
1148 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001149 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001150 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001151 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001152 i,
1153 gop_map->status,
1154 pending_idx,
1155 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001156
Wei Liue9ce7cb2014-06-04 10:30:42 +01001157 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001158
1159 /* Not the first error? Preceding frags already invalidated. */
1160 if (err)
1161 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001162
1163 /* First error: if the header haven't shared a slot with the
1164 * first frag, release it as well.
1165 */
1166 if (!sharedslot)
1167 xenvif_idx_release(queue,
1168 XENVIF_TX_CB(skb)->pending_idx,
1169 XEN_NETIF_RSP_OKAY);
1170
1171 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001172 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001173 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001174 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001175 xenvif_idx_release(queue, pending_idx,
1176 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001177 }
1178
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001179 /* And if we found the error while checking the frag_list, unmap
1180 * the first skb's frags
1181 */
1182 if (first_shinfo) {
1183 for (j = 0; j < first_shinfo->nr_frags; j++) {
1184 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1185 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001186 xenvif_idx_release(queue, pending_idx,
1187 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001188 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001189 }
1190
1191 /* Remember the error: invalidate all subsequent fragments. */
1192 err = newerr;
1193 }
1194
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001195 if (skb_has_frag_list(skb) && !first_shinfo) {
1196 first_shinfo = skb_shinfo(skb);
1197 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001198 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001199
1200 goto check_frags;
1201 }
1202
Zoltan Kissbdab8272014-04-02 18:04:58 +01001203 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001204 return err;
1205}
1206
Wei Liue9ce7cb2014-06-04 10:30:42 +01001207static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001208{
1209 struct skb_shared_info *shinfo = skb_shinfo(skb);
1210 int nr_frags = shinfo->nr_frags;
1211 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001212 u16 prev_pending_idx = INVALID_PENDING_IDX;
1213
Ian Campbellf942dc22011-03-15 00:06:18 +00001214 for (i = 0; i < nr_frags; i++) {
1215 skb_frag_t *frag = shinfo->frags + i;
1216 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001217 struct page *page;
1218 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001219
Ian Campbellea066ad2011-10-05 00:28:46 +00001220 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001221
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001222 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001223 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001224 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001225 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001226 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001227 callback_param(queue, prev_pending_idx).ctx =
1228 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001229
Wei Liue9ce7cb2014-06-04 10:30:42 +01001230 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001231 prev_pending_idx = pending_idx;
1232
Wei Liue9ce7cb2014-06-04 10:30:42 +01001233 txp = &queue->pending_tx_info[pending_idx].req;
1234 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001235 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001236 skb->len += txp->size;
1237 skb->data_len += txp->size;
1238 skb->truesize += txp->size;
1239
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001240 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001241 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001242 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001243 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1244 * overlaps with "index", and "mapping" is not set. I think mapping
1245 * should be set. If delivered to local stack, it would drop this
1246 * skb in sk_filter unless the socket has the right to use it.
1247 */
1248 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001249}
1250
Wei Liue9ce7cb2014-06-04 10:30:42 +01001251static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001252 struct xen_netif_extra_info *extras,
1253 int work_to_do)
1254{
1255 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001256 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001257
1258 do {
1259 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001260 netdev_err(queue->vif->dev, "Missing extra info\n");
1261 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001262 return -EBADR;
1263 }
1264
Wei Liue9ce7cb2014-06-04 10:30:42 +01001265 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001266 sizeof(extra));
1267 if (unlikely(!extra.type ||
1268 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001269 queue->tx.req_cons = ++cons;
1270 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001271 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001272 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001273 return -EINVAL;
1274 }
1275
1276 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001277 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001278 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1279
1280 return work_to_do;
1281}
1282
Wei Liu73764192013-08-26 12:59:39 +01001283static int xenvif_set_skb_gso(struct xenvif *vif,
1284 struct sk_buff *skb,
1285 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001286{
1287 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001288 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001289 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001290 return -EINVAL;
1291 }
1292
Paul Durranta9468582013-10-16 17:50:31 +01001293 switch (gso->u.gso.type) {
1294 case XEN_NETIF_GSO_TYPE_TCPV4:
1295 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1296 break;
1297 case XEN_NETIF_GSO_TYPE_TCPV6:
1298 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1299 break;
1300 default:
Ian Campbell488562862013-02-06 23:41:35 +00001301 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001302 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001303 return -EINVAL;
1304 }
1305
1306 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001307 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001308
1309 return 0;
1310}
1311
Wei Liue9ce7cb2014-06-04 10:30:42 +01001312static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001313{
Paul Durrant27216372014-01-09 10:02:47 +00001314 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001315
Paul Durrant2eba61d2013-10-16 17:50:29 +01001316 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001317 * peers can fail to set NETRXF_csum_blank when sending a GSO
1318 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1319 * recalculate the partial checksum.
1320 */
1321 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001322 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001323 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001324 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001325 }
1326
1327 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1328 if (skb->ip_summed != CHECKSUM_PARTIAL)
1329 return 0;
1330
Paul Durrant27216372014-01-09 10:02:47 +00001331 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001332}
1333
Wei Liue9ce7cb2014-06-04 10:30:42 +01001334static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001335{
Wei Liu059dfa62013-10-28 12:07:57 +00001336 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001337 u64 next_credit = queue->credit_window_start +
1338 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001339
1340 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001341 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001342 return true;
1343
1344 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001345 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001346 queue->credit_window_start = now;
1347 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001348 }
1349
1350 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001351 if (size > queue->remaining_credit) {
1352 queue->credit_timeout.data =
1353 (unsigned long)queue;
1354 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001355 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001356 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001357 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001358 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001359
1360 return true;
1361 }
1362
1363 return false;
1364}
1365
Wei Liue9ce7cb2014-06-04 10:30:42 +01001366static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001367 int budget,
1368 unsigned *copy_ops,
1369 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001370{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001371 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001372 struct sk_buff *skb;
1373 int ret;
1374
Wei Liue9ce7cb2014-06-04 10:30:42 +01001375 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001376 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001377 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001378 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1379 u16 pending_idx;
1380 RING_IDX idx;
1381 int work_to_do;
1382 unsigned int data_len;
1383 pending_ring_idx_t index;
1384
Wei Liue9ce7cb2014-06-04 10:30:42 +01001385 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001386 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001387 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001388 "Impossible number of requests. "
1389 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001390 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001391 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001392 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001393 break;
Ian Campbell488562862013-02-06 23:41:35 +00001394 }
1395
Wei Liue9ce7cb2014-06-04 10:30:42 +01001396 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001397 if (!work_to_do)
1398 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001399
Wei Liue9ce7cb2014-06-04 10:30:42 +01001400 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001401 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001402 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001403
1404 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001405 if (txreq.size > queue->remaining_credit &&
1406 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001407 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001408
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001410
1411 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001412 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001413
1414 memset(extras, 0, sizeof(extras));
1415 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001416 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001417 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001418 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001419 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001420 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001421 }
1422
Wei Liue9ce7cb2014-06-04 10:30:42 +01001423 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001424 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001425 break;
Ian Campbell488562862013-02-06 23:41:35 +00001426
Ian Campbellf942dc22011-03-15 00:06:18 +00001427 idx += ret;
1428
1429 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001430 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001431 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001432 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001433 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001434 }
1435
1436 /* No crossing a page as the payload mustn't fragment. */
1437 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001438 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001439 "txreq.offset: %x, size: %u, end: %lu\n",
1440 txreq.offset, txreq.size,
1441 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001442 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001443 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001444 }
1445
Wei Liue9ce7cb2014-06-04 10:30:42 +01001446 index = pending_index(queue->pending_cons);
1447 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001448
1449 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001450 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001451 PKT_PROT_LEN : txreq.size;
1452
Zoltan Kisse3377f32014-03-06 21:48:29 +00001453 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001454 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001455 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001456 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001457 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001458 break;
1459 }
1460
Ian Campbellf942dc22011-03-15 00:06:18 +00001461 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1462 struct xen_netif_extra_info *gso;
1463 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1464
Wei Liue9ce7cb2014-06-04 10:30:42 +01001465 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001466 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001467 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001468 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001469 }
1470 }
1471
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001472 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001473
1474 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001475 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1476 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1477 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001478
Wei Liue9ce7cb2014-06-04 10:30:42 +01001479 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001480 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001481 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1482 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001483 offset_in_page(skb->data);
1484
Wei Liue9ce7cb2014-06-04 10:30:42 +01001485 queue->tx_copy_ops[*copy_ops].len = data_len;
1486 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001487
1488 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001489
1490 skb_shinfo(skb)->nr_frags = ret;
1491 if (data_len < txreq.size) {
1492 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001493 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1494 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001495 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001496 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001497 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001498 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1499 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001500 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001501 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001502 }
1503
Wei Liue9ce7cb2014-06-04 10:30:42 +01001504 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001505
Wei Liue9ce7cb2014-06-04 10:30:42 +01001506 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001507 if (request_gop == NULL) {
1508 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001509 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001510 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001511 }
1512 gop = request_gop;
1513
Wei Liue9ce7cb2014-06-04 10:30:42 +01001514 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001515
Wei Liue9ce7cb2014-06-04 10:30:42 +01001516 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001517
Wei Liue9ce7cb2014-06-04 10:30:42 +01001518 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1519 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001520 break;
1521 }
1522
Wei Liue9ce7cb2014-06-04 10:30:42 +01001523 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001524 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001525}
1526
Zoltan Kisse3377f32014-03-06 21:48:29 +00001527/* Consolidate skb with a frag_list into a brand new one with local pages on
1528 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1529 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001530static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001531{
1532 unsigned int offset = skb_headlen(skb);
1533 skb_frag_t frags[MAX_SKB_FRAGS];
1534 int i;
1535 struct ubuf_info *uarg;
1536 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1537
Wei Liue9ce7cb2014-06-04 10:30:42 +01001538 queue->stats.tx_zerocopy_sent += 2;
1539 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001540
Wei Liue9ce7cb2014-06-04 10:30:42 +01001541 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001542 /* Subtract frags size, we will correct it later */
1543 skb->truesize -= skb->data_len;
1544 skb->len += nskb->len;
1545 skb->data_len += nskb->len;
1546
1547 /* create a brand new frags array and coalesce there */
1548 for (i = 0; offset < skb->len; i++) {
1549 struct page *page;
1550 unsigned int len;
1551
1552 BUG_ON(i >= MAX_SKB_FRAGS);
1553 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1554 if (!page) {
1555 int j;
1556 skb->truesize += skb->data_len;
1557 for (j = 0; j < i; j++)
1558 put_page(frags[j].page.p);
1559 return -ENOMEM;
1560 }
1561
1562 if (offset + PAGE_SIZE < skb->len)
1563 len = PAGE_SIZE;
1564 else
1565 len = skb->len - offset;
1566 if (skb_copy_bits(skb, offset, page_address(page), len))
1567 BUG();
1568
1569 offset += len;
1570 frags[i].page.p = page;
1571 frags[i].page_offset = 0;
1572 skb_frag_size_set(&frags[i], len);
1573 }
1574 /* swap out with old one */
1575 memcpy(skb_shinfo(skb)->frags,
1576 frags,
1577 i * sizeof(skb_frag_t));
1578 skb_shinfo(skb)->nr_frags = i;
1579 skb->truesize += i * PAGE_SIZE;
1580
1581 /* remove traces of mapped pages and frag_list */
1582 skb_frag_list_init(skb);
1583 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001584 /* increase inflight counter to offset decrement in callback */
1585 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001586 uarg->callback(uarg, true);
1587 skb_shinfo(skb)->destructor_arg = NULL;
1588
Wei Liua64bd932014-08-12 11:48:07 +01001589 xenvif_skb_zerocopy_prepare(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001590 kfree_skb(nskb);
1591
1592 return 0;
1593}
Ian Campbellf942dc22011-03-15 00:06:18 +00001594
Wei Liue9ce7cb2014-06-04 10:30:42 +01001595static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001596{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001597 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1598 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001599 struct sk_buff *skb;
1600 int work_done = 0;
1601
Wei Liue9ce7cb2014-06-04 10:30:42 +01001602 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001603 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001604 u16 pending_idx;
1605 unsigned data_len;
1606
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001607 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001608 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001609
1610 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001611 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001612 /* If there was an error, xenvif_tx_check_gop is
1613 * expected to release all the frags which were mapped,
1614 * so kfree_skb shouldn't do it again
1615 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001616 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001617 if (skb_has_frag_list(skb)) {
1618 struct sk_buff *nskb =
1619 skb_shinfo(skb)->frag_list;
1620 skb_shinfo(nskb)->nr_frags = 0;
1621 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001622 kfree_skb(skb);
1623 continue;
1624 }
1625
1626 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001627 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001628 if (data_len < txp->size) {
1629 /* Append the packet payload as a fragment. */
1630 txp->offset += data_len;
1631 txp->size -= data_len;
1632 } else {
1633 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001634 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001635 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001636 }
1637
1638 if (txp->flags & XEN_NETTXF_csum_blank)
1639 skb->ip_summed = CHECKSUM_PARTIAL;
1640 else if (txp->flags & XEN_NETTXF_data_validated)
1641 skb->ip_summed = CHECKSUM_UNNECESSARY;
1642
Wei Liue9ce7cb2014-06-04 10:30:42 +01001643 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001644
Zoltan Kisse3377f32014-03-06 21:48:29 +00001645 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001646 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001647 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001648 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001649 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001650 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001651 kfree_skb(skb);
1652 continue;
1653 }
1654 }
1655
Paul Durrant2eba61d2013-10-16 17:50:29 +01001656 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001657 int target = min_t(int, skb->len, PKT_PROT_LEN);
1658 __pskb_pull_tail(skb, target - skb_headlen(skb));
1659 }
1660
Wei Liue9ce7cb2014-06-04 10:30:42 +01001661 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001662 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001663 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001664
Wei Liue9ce7cb2014-06-04 10:30:42 +01001665 if (checksum_setup(queue, skb)) {
1666 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001667 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001668 /* We have to set this flag to trigger the callback */
1669 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001670 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001671 kfree_skb(skb);
1672 continue;
1673 }
1674
Jason Wang40893fd2013-03-26 23:11:22 +00001675 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001676
Paul Durrantb89587a2013-12-17 11:44:35 +00001677 /* If the packet is GSO then we will have just set up the
1678 * transport header offset in checksum_setup so it's now
1679 * straightforward to calculate gso_segs.
1680 */
1681 if (skb_is_gso(skb)) {
1682 int mss = skb_shinfo(skb)->gso_size;
1683 int hdrlen = skb_transport_header(skb) -
1684 skb_mac_header(skb) +
1685 tcp_hdrlen(skb);
1686
1687 skb_shinfo(skb)->gso_segs =
1688 DIV_ROUND_UP(skb->len - hdrlen, mss);
1689 }
1690
Wei Liue9ce7cb2014-06-04 10:30:42 +01001691 queue->stats.rx_bytes += skb->len;
1692 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001693
Wei Liub3f980b2013-08-26 12:59:38 +01001694 work_done++;
1695
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001696 /* Set this flag right before netif_receive_skb, otherwise
1697 * someone might think this packet already left netback, and
1698 * do a skb_copy_ubufs while we are still in control of the
1699 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1700 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001701 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001702 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001703 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001704 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001705
Wei Liub3f980b2013-08-26 12:59:38 +01001706 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001707 }
Wei Liub3f980b2013-08-26 12:59:38 +01001708
1709 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001710}
1711
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001712void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1713{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001714 unsigned long flags;
1715 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001716 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001717
1718 /* This is the only place where we grab this lock, to protect callbacks
1719 * from each other.
1720 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001721 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001722 do {
1723 u16 pending_idx = ubuf->desc;
1724 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001725 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001726 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001727 index = pending_index(queue->dealloc_prod);
1728 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001729 /* Sync with xenvif_tx_dealloc_action:
1730 * insert idx then incr producer.
1731 */
1732 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001733 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001734 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001735 wake_up(&queue->dealloc_wq);
1736 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001737
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001738 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001739 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001740 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001741 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001742 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001743}
1744
Wei Liue9ce7cb2014-06-04 10:30:42 +01001745static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001746{
1747 struct gnttab_unmap_grant_ref *gop;
1748 pending_ring_idx_t dc, dp;
1749 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1750 unsigned int i = 0;
1751
Wei Liue9ce7cb2014-06-04 10:30:42 +01001752 dc = queue->dealloc_cons;
1753 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001754
1755 /* Free up any grants we have finished using */
1756 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001757 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001758
1759 /* Ensure we see all indices enqueued by all
1760 * xenvif_zerocopy_callback().
1761 */
1762 smp_rmb();
1763
1764 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001765 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001766 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001767 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001768
Wei Liue9ce7cb2014-06-04 10:30:42 +01001769 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001770 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001771 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1772 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001773 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001774 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001775 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001776 queue->grant_tx_handle[pending_idx]);
1777 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001778 ++gop;
1779 }
1780
Wei Liue9ce7cb2014-06-04 10:30:42 +01001781 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001782
Wei Liue9ce7cb2014-06-04 10:30:42 +01001783 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001784
Wei Liue9ce7cb2014-06-04 10:30:42 +01001785 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001786 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001787 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001788 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001789 queue->pages_to_unmap,
1790 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001791 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001792 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1793 gop - queue->tx_unmap_ops, ret);
1794 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001795 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001796 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001797 " host_addr: %llx handle: %x status: %d\n",
1798 gop[i].host_addr,
1799 gop[i].handle,
1800 gop[i].status);
1801 }
1802 BUG();
1803 }
1804 }
1805
Wei Liue9ce7cb2014-06-04 10:30:42 +01001806 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1807 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001808 XEN_NETIF_RSP_OKAY);
1809}
1810
1811
Ian Campbellf942dc22011-03-15 00:06:18 +00001812/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001813int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001814{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001815 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001816 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001817
Wei Liue9ce7cb2014-06-04 10:30:42 +01001818 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001819 return 0;
1820
Wei Liue9ce7cb2014-06-04 10:30:42 +01001821 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001822
Zoltan Kissbdab8272014-04-02 18:04:58 +01001823 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001824 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001825
Wei Liue9ce7cb2014-06-04 10:30:42 +01001826 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001827 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001828 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001829 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001830 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001831 nr_mops);
1832 BUG_ON(ret);
1833 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001834
Wei Liue9ce7cb2014-06-04 10:30:42 +01001835 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001836
1837 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001838}
1839
Wei Liue9ce7cb2014-06-04 10:30:42 +01001840static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001841 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001842{
Ian Campbellf942dc22011-03-15 00:06:18 +00001843 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001844 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001845 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001846
Wei Liue9ce7cb2014-06-04 10:30:42 +01001847 pending_tx_info = &queue->pending_tx_info[pending_idx];
1848 spin_lock_irqsave(&queue->response_lock, flags);
1849 make_tx_response(queue, &pending_tx_info->req, status);
1850 index = pending_index(queue->pending_prod);
1851 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001852 /* TX shouldn't use the index before we give it back here */
1853 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001854 queue->pending_prod++;
1855 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001856}
1857
Wei Liu2810e5b2013-04-22 02:20:42 +00001858
Wei Liue9ce7cb2014-06-04 10:30:42 +01001859static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001860 struct xen_netif_tx_request *txp,
1861 s8 st)
1862{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001863 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001864 struct xen_netif_tx_response *resp;
1865 int notify;
1866
Wei Liue9ce7cb2014-06-04 10:30:42 +01001867 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001868 resp->id = txp->id;
1869 resp->status = st;
1870
1871 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001872 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001873
Wei Liue9ce7cb2014-06-04 10:30:42 +01001874 queue->tx.rsp_prod_pvt = ++i;
1875 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001876 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001877 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001878}
1879
Wei Liue9ce7cb2014-06-04 10:30:42 +01001880static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001881 u16 id,
1882 s8 st,
1883 u16 offset,
1884 u16 size,
1885 u16 flags)
1886{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001887 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001888 struct xen_netif_rx_response *resp;
1889
Wei Liue9ce7cb2014-06-04 10:30:42 +01001890 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001891 resp->offset = offset;
1892 resp->flags = flags;
1893 resp->id = id;
1894 resp->status = (s16)size;
1895 if (st < 0)
1896 resp->status = (s16)st;
1897
Wei Liue9ce7cb2014-06-04 10:30:42 +01001898 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001899
1900 return resp;
1901}
1902
Wei Liue9ce7cb2014-06-04 10:30:42 +01001903void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001904{
1905 int ret;
1906 struct gnttab_unmap_grant_ref tx_unmap_op;
1907
1908 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001909 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001910 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001911 queue->grant_tx_handle[pending_idx]);
1912 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001913
1914 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001915 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001916 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001917 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001918 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1919 ret,
1920 pending_idx,
1921 tx_unmap_op.host_addr,
1922 tx_unmap_op.handle,
1923 tx_unmap_op.status);
1924 BUG();
1925 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001926}
1927
Wei Liue9ce7cb2014-06-04 10:30:42 +01001928static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001929{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001930 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001931 return 1;
1932
1933 return 0;
1934}
1935
Wei Liue9ce7cb2014-06-04 10:30:42 +01001936static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001937{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001938 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001939}
1940
Wei Liue9ce7cb2014-06-04 10:30:42 +01001941void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001942{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001943 if (queue->tx.sring)
1944 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1945 queue->tx.sring);
1946 if (queue->rx.sring)
1947 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1948 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001949}
1950
Wei Liue9ce7cb2014-06-04 10:30:42 +01001951int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001952 grant_ref_t tx_ring_ref,
1953 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001954{
David Vrabelc9d63692011-09-29 16:53:31 +01001955 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001956 struct xen_netif_tx_sring *txs;
1957 struct xen_netif_rx_sring *rxs;
1958
1959 int err = -ENOMEM;
1960
Wei Liue9ce7cb2014-06-04 10:30:42 +01001961 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001962 tx_ring_ref, &addr);
1963 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001964 goto err;
1965
David Vrabelc9d63692011-09-29 16:53:31 +01001966 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001967 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001968
Wei Liue9ce7cb2014-06-04 10:30:42 +01001969 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001970 rx_ring_ref, &addr);
1971 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001972 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001973
David Vrabelc9d63692011-09-29 16:53:31 +01001974 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001975 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001976
1977 return 0;
1978
1979err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001980 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001981 return err;
1982}
1983
David Vrabelecf08d22014-10-22 14:08:55 +01001984static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
1985{
1986 struct xenvif *vif = queue->vif;
1987
1988 queue->stalled = true;
1989
1990 /* At least one queue has stalled? Disable the carrier. */
1991 spin_lock(&vif->lock);
1992 if (vif->stalled_queues++ == 0) {
1993 netdev_info(vif->dev, "Guest Rx stalled");
1994 netif_carrier_off(vif->dev);
1995 }
1996 spin_unlock(&vif->lock);
1997}
1998
1999static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
2000{
2001 struct xenvif *vif = queue->vif;
2002
2003 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
2004 queue->stalled = false;
2005
2006 /* All queues are ready? Enable the carrier. */
2007 spin_lock(&vif->lock);
2008 if (--vif->stalled_queues == 0) {
2009 netdev_info(vif->dev, "Guest Rx ready");
2010 netif_carrier_on(vif->dev);
2011 }
2012 spin_unlock(&vif->lock);
2013}
2014
2015static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
2016{
2017 RING_IDX prod, cons;
2018
2019 prod = queue->rx.sring->req_prod;
2020 cons = queue->rx.req_cons;
2021
2022 return !queue->stalled
2023 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
2024 && time_after(jiffies,
2025 queue->last_rx_time + rx_stall_timeout_jiffies);
2026}
2027
2028static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
2029{
2030 RING_IDX prod, cons;
2031
2032 prod = queue->rx.sring->req_prod;
2033 cons = queue->rx.req_cons;
2034
2035 return queue->stalled
2036 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
2037}
2038
David Vrabelf48da8b2014-10-22 14:08:54 +01002039static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00002040{
David Vrabelf48da8b2014-10-22 14:08:54 +01002041 return (!skb_queue_empty(&queue->rx_queue)
2042 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
David Vrabelecf08d22014-10-22 14:08:55 +01002043 || xenvif_rx_queue_stalled(queue)
2044 || xenvif_rx_queue_ready(queue)
David Vrabelf48da8b2014-10-22 14:08:54 +01002045 || kthread_should_stop()
2046 || queue->vif->disabled;
Paul Durrantca2f09f2013-12-06 16:36:07 +00002047}
2048
David Vrabelf48da8b2014-10-22 14:08:54 +01002049static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002050{
David Vrabelf48da8b2014-10-22 14:08:54 +01002051 struct sk_buff *skb;
2052 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002053
David Vrabelf48da8b2014-10-22 14:08:54 +01002054 skb = skb_peek(&queue->rx_queue);
2055 if (!skb)
2056 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002057
David Vrabelf48da8b2014-10-22 14:08:54 +01002058 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
2059 return timeout < 0 ? 0 : timeout;
2060}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002061
David Vrabelf48da8b2014-10-22 14:08:54 +01002062/* Wait until the guest Rx thread has work.
2063 *
2064 * The timeout needs to be adjusted based on the current head of the
2065 * queue (and not just the head at the beginning). In particular, if
2066 * the queue is initially empty an infinite timeout is used and this
2067 * needs to be reduced when a skb is queued.
2068 *
2069 * This cannot be done with wait_event_timeout() because it only
2070 * calculates the timeout once.
2071 */
2072static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
2073{
2074 DEFINE_WAIT(wait);
2075
2076 if (xenvif_have_rx_work(queue))
2077 return;
2078
2079 for (;;) {
2080 long ret;
2081
2082 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
2083 if (xenvif_have_rx_work(queue))
2084 break;
2085 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
2086 if (!ret)
2087 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002088 }
David Vrabelf48da8b2014-10-22 14:08:54 +01002089 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002090}
2091
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00002092int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01002093{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002094 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01002095 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01002096
David Vrabelf48da8b2014-10-22 14:08:54 +01002097 for (;;) {
2098 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01002099
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002100 if (kthread_should_stop())
2101 break;
2102
Wei Liue9d8b2c2014-04-01 12:46:12 +01002103 /* This frontend is found to be rogue, disable it in
2104 * kthread context. Currently this is only set when
2105 * netback finds out frontend sends malformed packet,
2106 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01002107 * context so we defer it here, if this thread is
2108 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01002109 */
David Vrabelf48da8b2014-10-22 14:08:54 +01002110 if (unlikely(vif->disabled && queue->id == 0)) {
2111 xenvif_carrier_off(vif);
2112 xenvif_rx_queue_purge(queue);
2113 continue;
Zoltan Kiss09350782014-03-06 21:48:30 +00002114 }
2115
Wei Liue9ce7cb2014-06-04 10:30:42 +01002116 if (!skb_queue_empty(&queue->rx_queue))
2117 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01002118
David Vrabelecf08d22014-10-22 14:08:55 +01002119 /* If the guest hasn't provided any Rx slots for a
2120 * while it's probably not responsive, drop the
2121 * carrier so packets are dropped earlier.
2122 */
2123 if (xenvif_rx_queue_stalled(queue))
2124 xenvif_queue_carrier_off(queue);
2125 else if (xenvif_rx_queue_ready(queue))
2126 xenvif_queue_carrier_on(queue);
2127
David Vrabelf48da8b2014-10-22 14:08:54 +01002128 /* Queued packets may have foreign pages from other
2129 * domains. These cannot be queued indefinitely as
2130 * this would starve guests of grant refs and transmit
2131 * slots.
2132 */
2133 xenvif_rx_queue_drop_expired(queue);
2134
2135 xenvif_rx_queue_maybe_wake(queue);
2136
Wei Liub3f980b2013-08-26 12:59:38 +01002137 cond_resched();
2138 }
2139
Paul Durrantca2f09f2013-12-06 16:36:07 +00002140 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01002141 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00002142
Wei Liub3f980b2013-08-26 12:59:38 +01002143 return 0;
2144}
2145
Wei Liua64bd932014-08-12 11:48:07 +01002146static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2147{
2148 /* Dealloc thread must remain running until all inflight
2149 * packets complete.
2150 */
2151 return kthread_should_stop() &&
2152 !atomic_read(&queue->inflight_packets);
2153}
2154
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002155int xenvif_dealloc_kthread(void *data)
2156{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002157 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002158
Wei Liua64bd932014-08-12 11:48:07 +01002159 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01002160 wait_event_interruptible(queue->dealloc_wq,
2161 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01002162 xenvif_dealloc_kthread_should_stop(queue));
2163 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002164 break;
2165
Wei Liue9ce7cb2014-06-04 10:30:42 +01002166 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002167 cond_resched();
2168 }
2169
2170 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01002171 if (tx_dealloc_work_todo(queue))
2172 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002173
2174 return 0;
2175}
2176
Ian Campbellf942dc22011-03-15 00:06:18 +00002177static int __init netback_init(void)
2178{
Ian Campbellf942dc22011-03-15 00:06:18 +00002179 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00002180
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05002181 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00002182 return -ENODEV;
2183
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01002184 /* Allow as many queues as there are CPUs, by default */
2185 xenvif_max_queues = num_online_cpus();
2186
Wei Liu37641492013-05-02 00:43:59 +00002187 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002188 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2189 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002190 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002191 }
2192
Ian Campbellf942dc22011-03-15 00:06:18 +00002193 rc = xenvif_xenbus_init();
2194 if (rc)
2195 goto failed_init;
2196
Zoltan Kiss09350782014-03-06 21:48:30 +00002197 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
David Vrabelecf08d22014-10-22 14:08:55 +01002198 rx_stall_timeout_jiffies = msecs_to_jiffies(rx_stall_timeout_msecs);
Zoltan Kiss09350782014-03-06 21:48:30 +00002199
Zoltan Kissf51de242014-07-08 19:49:14 +01002200#ifdef CONFIG_DEBUG_FS
2201 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2202 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2203 pr_warn("Init of debugfs returned %ld!\n",
2204 PTR_ERR(xen_netback_dbg_root));
2205#endif /* CONFIG_DEBUG_FS */
2206
Ian Campbellf942dc22011-03-15 00:06:18 +00002207 return 0;
2208
2209failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002210 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002211}
2212
2213module_init(netback_init);
2214
Wei Liub103f352013-05-16 23:26:11 +00002215static void __exit netback_fini(void)
2216{
Zoltan Kissf51de242014-07-08 19:49:14 +01002217#ifdef CONFIG_DEBUG_FS
2218 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2219 debugfs_remove_recursive(xen_netback_dbg_root);
2220#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002221 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002222}
2223module_exit(netback_fini);
2224
Ian Campbellf942dc22011-03-15 00:06:18 +00002225MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002226MODULE_ALIAS("xen-backend:vif");