blob: 4a509f715fe8be2f8ff70f7dbf87dce93631caa9 [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
Malcolm Crossley7e5d7752014-11-05 10:50:22 +000085/* The amount to copy out of the first guest Tx slot into the skb's
86 * linear area. If the first slot has more data, it will be mapped
87 * and put into the first frag.
88 *
89 * This is sized to avoid pulling headers from the frags for most
90 * TCP/IP packets.
91 */
92#define XEN_NETBACK_TX_COPY_LEN 128
93
94
Wei Liue9ce7cb2014-06-04 10:30:42 +010095static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +010096 u8 status);
97
Wei Liue9ce7cb2014-06-04 10:30:42 +010098static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000099 struct xen_netif_tx_request *txp,
100 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +0100101
Wei Liue9ce7cb2014-06-04 10:30:42 +0100102static inline int tx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +0100103
Wei Liue9ce7cb2014-06-04 10:30:42 +0100104static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +0000105 u16 id,
106 s8 st,
107 u16 offset,
108 u16 size,
109 u16 flags);
110
Wei Liue9ce7cb2014-06-04 10:30:42 +0100111static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000112 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000113{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100114 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000115}
116
Wei Liue9ce7cb2014-06-04 10:30:42 +0100117static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000118 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000119{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100120 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000121}
122
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000123#define callback_param(vif, pending_idx) \
124 (vif->pending_tx_info[pending_idx].callback_struct)
125
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000126/* Find the containing VIF's structure from a pointer in pending_tx_info array
127 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100128static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000129{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000130 u16 pending_idx = ubuf->desc;
131 struct pending_tx_info *temp =
132 container_of(ubuf, struct pending_tx_info, callback_struct);
133 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100134 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000135 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000136}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000137
Ian Campbellea066ad2011-10-05 00:28:46 +0000138static u16 frag_get_pending_idx(skb_frag_t *frag)
139{
140 return (u16)frag->page_offset;
141}
142
143static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
144{
145 frag->page_offset = pending_idx;
146}
147
Ian Campbellf942dc22011-03-15 00:06:18 +0000148static inline pending_ring_idx_t pending_index(unsigned i)
149{
150 return i & (MAX_PENDING_REQS-1);
151}
152
Wei Liue9ce7cb2014-06-04 10:30:42 +0100153bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000154{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000155 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000156
Paul Durrantca2f09f2013-12-06 16:36:07 +0000157 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100158 prod = queue->rx.sring->req_prod;
159 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000160
Paul Durrantca2f09f2013-12-06 16:36:07 +0000161 if (prod - cons >= needed)
162 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000163
Wei Liue9ce7cb2014-06-04 10:30:42 +0100164 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000165
Paul Durrantca2f09f2013-12-06 16:36:07 +0000166 /* Make sure event is visible before we check prod
167 * again.
168 */
169 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100170 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000171
Paul Durrantca2f09f2013-12-06 16:36:07 +0000172 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000173}
174
David Vrabelf48da8b2014-10-22 14:08:54 +0100175void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
176{
177 unsigned long flags;
178
179 spin_lock_irqsave(&queue->rx_queue.lock, flags);
180
181 __skb_queue_tail(&queue->rx_queue, skb);
182
183 queue->rx_queue_len += skb->len;
184 if (queue->rx_queue_len > queue->rx_queue_max)
185 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
186
187 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
188}
189
190static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
191{
192 struct sk_buff *skb;
193
194 spin_lock_irq(&queue->rx_queue.lock);
195
196 skb = __skb_dequeue(&queue->rx_queue);
197 if (skb)
198 queue->rx_queue_len -= skb->len;
199
200 spin_unlock_irq(&queue->rx_queue.lock);
201
202 return skb;
203}
204
205static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
206{
207 spin_lock_irq(&queue->rx_queue.lock);
208
209 if (queue->rx_queue_len < queue->rx_queue_max)
210 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
211
212 spin_unlock_irq(&queue->rx_queue.lock);
213}
214
215
216static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
217{
218 struct sk_buff *skb;
219 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
220 kfree_skb(skb);
221}
222
223static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
224{
225 struct sk_buff *skb;
226
227 for(;;) {
228 skb = skb_peek(&queue->rx_queue);
229 if (!skb)
230 break;
231 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
232 break;
233 xenvif_rx_dequeue(queue);
234 kfree_skb(skb);
235 }
236}
237
Ian Campbellf942dc22011-03-15 00:06:18 +0000238/*
239 * Returns true if we should start a new receive buffer instead of
240 * adding 'size' bytes to a buffer which currently contains 'offset'
241 * bytes.
242 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100243static bool start_new_rx_buffer(int offset, unsigned long size, int head,
244 bool full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000245{
246 /* simple case: we have completely filled the current buffer. */
247 if (offset == MAX_BUFFER_OFFSET)
248 return true;
249
250 /*
251 * complex case: start a fresh buffer if the current frag
252 * would overflow the current buffer but only if:
253 * (i) this frag would fit completely in the next buffer
254 * and (ii) there is already some data in the current buffer
255 * and (iii) this is not the head buffer.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100256 * and (iv) there is no need to fully utilize the buffers
Ian Campbellf942dc22011-03-15 00:06:18 +0000257 *
258 * Where:
259 * - (i) stops us splitting a frag into two copies
260 * unless the frag is too large for a single buffer.
261 * - (ii) stops us from leaving a buffer pointlessly empty.
262 * - (iii) stops us leaving the first buffer
263 * empty. Strictly speaking this is already covered
264 * by (ii) but is explicitly checked because
265 * netfront relies on the first buffer being
266 * non-empty and can crash otherwise.
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100267 * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS
268 * slot
Ian Campbellf942dc22011-03-15 00:06:18 +0000269 *
270 * This means we will effectively linearise small
271 * frags but do not needlessly split large buffers
272 * into multiple copies tend to give large frags their
273 * own buffers as before.
274 */
Paul Durrant0576edd2014-03-28 11:39:05 +0000275 BUG_ON(size > MAX_BUFFER_OFFSET);
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100276 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head &&
277 !full_coalesce)
Ian Campbellf942dc22011-03-15 00:06:18 +0000278 return true;
279
280 return false;
281}
282
Ian Campbellf942dc22011-03-15 00:06:18 +0000283struct netrx_pending_operations {
284 unsigned copy_prod, copy_cons;
285 unsigned meta_prod, meta_cons;
286 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100287 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000288 int copy_off;
289 grant_ref_t copy_gref;
290};
291
Wei Liue9ce7cb2014-06-04 10:30:42 +0100292static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100293 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000294{
Wei Liub3f980b2013-08-26 12:59:38 +0100295 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000296 struct xen_netif_rx_request *req;
297
Wei Liue9ce7cb2014-06-04 10:30:42 +0100298 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000299
300 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100301 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000302 meta->gso_size = 0;
303 meta->size = 0;
304 meta->id = req->id;
305
306 npo->copy_off = 0;
307 npo->copy_gref = req->gref;
308
309 return meta;
310}
311
Wei Liu33bc8012013-10-08 10:54:21 +0100312/*
313 * Set up the grant operations for this fragment. If it's a flipping
314 * interface, we also set up the unmap request from here.
315 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100316static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100317 struct netrx_pending_operations *npo,
318 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000319 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100320 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000321 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000322{
323 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100324 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000325 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800326 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000327
328 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000329 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000330
331 meta = npo->meta + npo->meta_prod - 1;
332
Ian Campbell6a8ed462012-10-10 03:48:42 +0000333 /* Skip unused frames from start of page */
334 page += offset >> PAGE_SHIFT;
335 offset &= ~PAGE_MASK;
336
Ian Campbellf942dc22011-03-15 00:06:18 +0000337 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000338 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000339 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
340
Ian Campbell6a8ed462012-10-10 03:48:42 +0000341 bytes = PAGE_SIZE - offset;
342
343 if (bytes > size)
344 bytes = size;
345
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100346 if (start_new_rx_buffer(npo->copy_off,
347 bytes,
348 *head,
349 XENVIF_RX_CB(skb)->full_coalesce)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000350 /*
351 * Netfront requires there to be some data in the head
352 * buffer.
353 */
Wei Liu33bc8012013-10-08 10:54:21 +0100354 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000355
Wei Liue9ce7cb2014-06-04 10:30:42 +0100356 meta = get_next_rx_buffer(queue, npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000357 }
358
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
360 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
361
362 copy_gop = npo->copy + npo->copy_prod++;
363 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100364 copy_gop->len = bytes;
365
Wei Liue9ce7cb2014-06-04 10:30:42 +0100366 if (foreign_queue) {
367 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000368 copy_gop->source.u.ref = foreign_gref;
369 copy_gop->flags |= GNTCOPY_source_gref;
370 } else {
371 copy_gop->source.domid = DOMID_SELF;
372 copy_gop->source.u.gmfn =
373 virt_to_mfn(page_address(page));
374 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000376
Wei Liue9ce7cb2014-06-04 10:30:42 +0100377 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000378 copy_gop->dest.offset = npo->copy_off;
379 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000380
381 npo->copy_off += bytes;
382 meta->size += bytes;
383
384 offset += bytes;
385 size -= bytes;
386
Ian Campbell6a8ed462012-10-10 03:48:42 +0000387 /* Next frame */
388 if (offset == PAGE_SIZE && size) {
389 BUG_ON(!PageCompound(page));
390 page++;
391 offset = 0;
392 }
393
Ian Campbellf942dc22011-03-15 00:06:18 +0000394 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800395 if (skb_is_gso(skb)) {
396 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
397 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
398 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
399 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
400 }
Paul Durrant82cada22013-10-16 17:50:32 +0100401
Wei Liue9ce7cb2014-06-04 10:30:42 +0100402 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
403 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000404
Wei Liu33bc8012013-10-08 10:54:21 +0100405 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000406
407 }
408}
409
410/*
Zoltan Kiss58375742014-05-15 11:08:34 +0100411 * Find the grant ref for a given frag in a chain of struct ubuf_info's
412 * skb: the skb itself
413 * i: the frag's number
414 * ubuf: a pointer to an element in the chain. It should not be NULL
415 *
416 * Returns a pointer to the element in the chain where the page were found. If
417 * not found, returns NULL.
418 * See the definition of callback_struct in common.h for more details about
419 * the chain.
420 */
421static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
422 const int i,
423 const struct ubuf_info *ubuf)
424{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100425 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss58375742014-05-15 11:08:34 +0100426
427 do {
428 u16 pending_idx = ubuf->desc;
429
430 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100431 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss58375742014-05-15 11:08:34 +0100432 break;
433 ubuf = (struct ubuf_info *) ubuf->ctx;
434 } while (ubuf);
435
436 return ubuf;
437}
438
439/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000440 * Prepare an SKB to be transmitted to the frontend.
441 *
442 * This function is responsible for allocating grant operations, meta
443 * structures, etc.
444 *
445 * It returns the number of meta structures consumed. The number of
446 * ring slots used is always equal to the number of meta slots used
447 * plus the number of GSO descriptors used. Currently, we use either
448 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
449 * frontend-side LRO).
450 */
Wei Liu73764192013-08-26 12:59:39 +0100451static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100452 struct netrx_pending_operations *npo,
453 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000454{
455 struct xenvif *vif = netdev_priv(skb->dev);
456 int nr_frags = skb_shinfo(skb)->nr_frags;
457 int i;
458 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100459 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000460 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100461 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000462 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100463 int gso_type;
Zoltan Kiss58375742014-05-15 11:08:34 +0100464 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
465 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000466
467 old_meta_prod = npo->meta_prod;
468
Annie Li5bd07672014-03-10 22:58:34 +0800469 gso_type = XEN_NETIF_GSO_TYPE_NONE;
470 if (skb_is_gso(skb)) {
471 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
472 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
473 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
474 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100475 }
476
Ian Campbellf942dc22011-03-15 00:06:18 +0000477 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000478 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100479 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000480 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100481 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800482 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000483 meta->size = 0;
484 meta->id = req->id;
485 }
486
Wei Liue9ce7cb2014-06-04 10:30:42 +0100487 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000488 meta = npo->meta + npo->meta_prod++;
489
Paul Durrant82cada22013-10-16 17:50:32 +0100490 if ((1 << gso_type) & vif->gso_mask) {
491 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800492 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100493 } else {
494 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000495 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100496 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000497
498 meta->size = 0;
499 meta->id = req->id;
500 npo->copy_off = 0;
501 npo->copy_gref = req->gref;
502
503 data = skb->data;
504 while (data < skb_tail_pointer(skb)) {
505 unsigned int offset = offset_in_page(data);
506 unsigned int len = PAGE_SIZE - offset;
507
508 if (data + len > skb_tail_pointer(skb))
509 len = skb_tail_pointer(skb) - data;
510
Wei Liue9ce7cb2014-06-04 10:30:42 +0100511 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000512 virt_to_page(data), len, offset, &head,
513 NULL,
514 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000515 data += len;
516 }
517
518 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss58375742014-05-15 11:08:34 +0100519 /* This variable also signals whether foreign_gref has a real
520 * value or not.
521 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100522 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss58375742014-05-15 11:08:34 +0100523 grant_ref_t foreign_gref;
524
525 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
526 (ubuf->callback == &xenvif_zerocopy_callback)) {
527 const struct ubuf_info *const startpoint = ubuf;
528
529 /* Ideally ubuf points to the chain element which
530 * belongs to this frag. Or if frags were removed from
531 * the beginning, then shortly before it.
532 */
533 ubuf = xenvif_find_gref(skb, i, ubuf);
534
535 /* Try again from the beginning of the list, if we
536 * haven't tried from there. This only makes sense in
537 * the unlikely event of reordering the original frags.
538 * For injected local pages it's an unnecessary second
539 * run.
540 */
541 if (unlikely(!ubuf) && startpoint != head_ubuf)
542 ubuf = xenvif_find_gref(skb, i, head_ubuf);
543
544 if (likely(ubuf)) {
545 u16 pending_idx = ubuf->desc;
546
Wei Liue9ce7cb2014-06-04 10:30:42 +0100547 foreign_queue = ubuf_to_queue(ubuf);
548 foreign_gref =
549 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss58375742014-05-15 11:08:34 +0100550 /* Just a safety measure. If this was the last
551 * element on the list, the for loop will
552 * iterate again if a local page were added to
553 * the end. Using head_ubuf here prevents the
554 * second search on the chain. Or the original
555 * frags changed order, but that's less likely.
556 * In any way, ubuf shouldn't be NULL.
557 */
558 ubuf = ubuf->ctx ?
559 (struct ubuf_info *) ubuf->ctx :
560 head_ubuf;
561 } else
562 /* This frag was a local page, added to the
563 * array after the skb left netback.
564 */
565 ubuf = head_ubuf;
566 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100567 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100568 skb_frag_page(&skb_shinfo(skb)->frags[i]),
569 skb_frag_size(&skb_shinfo(skb)->frags[i]),
570 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000571 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100572 foreign_queue,
573 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000574 }
575
576 return npo->meta_prod - old_meta_prod;
577}
578
579/*
Wei Liu73764192013-08-26 12:59:39 +0100580 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000581 * used to set up the operations on the top of
582 * netrx_pending_operations, which have since been done. Check that
583 * they didn't give any errors and advance over them.
584 */
Wei Liu73764192013-08-26 12:59:39 +0100585static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
586 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000587{
588 struct gnttab_copy *copy_op;
589 int status = XEN_NETIF_RSP_OKAY;
590 int i;
591
592 for (i = 0; i < nr_meta_slots; i++) {
593 copy_op = npo->copy + npo->copy_cons++;
594 if (copy_op->status != GNTST_okay) {
595 netdev_dbg(vif->dev,
596 "Bad status %d from copy to DOM%d.\n",
597 copy_op->status, vif->domid);
598 status = XEN_NETIF_RSP_ERROR;
599 }
600 }
601
602 return status;
603}
604
Wei Liue9ce7cb2014-06-04 10:30:42 +0100605static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100606 struct xenvif_rx_meta *meta,
607 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000608{
609 int i;
610 unsigned long offset;
611
612 /* No fragments used */
613 if (nr_meta_slots <= 1)
614 return;
615
616 nr_meta_slots--;
617
618 for (i = 0; i < nr_meta_slots; i++) {
619 int flags;
620 if (i == nr_meta_slots - 1)
621 flags = 0;
622 else
623 flags = XEN_NETRXF_more_data;
624
625 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100626 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000627 meta[i].size, flags);
628 }
629}
630
Wei Liue9ce7cb2014-06-04 10:30:42 +0100631void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000632{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100633 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100634}
635
Wei Liue9ce7cb2014-06-04 10:30:42 +0100636static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100637{
Ian Campbellf942dc22011-03-15 00:06:18 +0000638 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000639 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000640 struct xen_netif_rx_response *resp;
641 struct sk_buff_head rxq;
642 struct sk_buff *skb;
643 LIST_HEAD(notify);
644 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000645 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000646 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000647
648 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100649 .copy = queue->grant_copy_op,
650 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000651 };
652
653 skb_queue_head_init(&rxq);
654
David Vrabelf48da8b2014-10-22 14:08:54 +0100655 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
656 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000657 RING_IDX max_slots_needed;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000658 RING_IDX old_req_cons;
659 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000660 int i;
661
David Vrabelecf08d22014-10-22 14:08:55 +0100662 queue->last_rx_time = jiffies;
663
Paul Durrantca2f09f2013-12-06 16:36:07 +0000664 /* We need a cheap worse case estimate for the number of
665 * slots we'll use.
666 */
667
668 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
669 skb_headlen(skb),
670 PAGE_SIZE);
671 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
672 unsigned int size;
Paul Durranta02eb472014-03-28 11:39:06 +0000673 unsigned int offset;
674
Paul Durrantca2f09f2013-12-06 16:36:07 +0000675 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Paul Durranta02eb472014-03-28 11:39:06 +0000676 offset = skb_shinfo(skb)->frags[i].page_offset;
677
678 /* For a worse-case estimate we need to factor in
679 * the fragment page offset as this will affect the
680 * number of times xenvif_gop_frag_copy() will
681 * call start_new_rx_buffer().
682 */
683 max_slots_needed += DIV_ROUND_UP(offset + size,
684 PAGE_SIZE);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000685 }
Paul Durranta02eb472014-03-28 11:39:06 +0000686
687 /* To avoid the estimate becoming too pessimal for some
688 * frontends that limit posted rx requests, cap the estimate
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100689 * at MAX_SKB_FRAGS. In this case netback will fully coalesce
690 * the skb into the provided slots.
Paul Durranta02eb472014-03-28 11:39:06 +0000691 */
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100692 if (max_slots_needed > MAX_SKB_FRAGS) {
Paul Durranta02eb472014-03-28 11:39:06 +0000693 max_slots_needed = MAX_SKB_FRAGS;
Zoltan Kiss59ae9fc2014-06-04 19:58:51 +0100694 XENVIF_RX_CB(skb)->full_coalesce = true;
695 } else {
696 XENVIF_RX_CB(skb)->full_coalesce = false;
697 }
Paul Durranta02eb472014-03-28 11:39:06 +0000698
699 /* We may need one more slot for GSO metadata */
Annie Li5bd07672014-03-10 22:58:34 +0800700 if (skb_is_gso(skb) &&
701 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
702 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000703 max_slots_needed++;
704
Wei Liue9ce7cb2014-06-04 10:30:42 +0100705 old_req_cons = queue->rx.req_cons;
706 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
707 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000708
709 BUG_ON(ring_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000710
711 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000712 }
713
Wei Liue9ce7cb2014-06-04 10:30:42 +0100714 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000715
716 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000717 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000718
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000719 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100720 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000721
722 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000723
Wei Liue9ce7cb2014-06-04 10:30:42 +0100724 if ((1 << queue->meta[npo.meta_cons].gso_type) &
725 queue->vif->gso_prefix_mask) {
726 resp = RING_GET_RESPONSE(&queue->rx,
727 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000728
729 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
730
Wei Liue9ce7cb2014-06-04 10:30:42 +0100731 resp->offset = queue->meta[npo.meta_cons].gso_size;
732 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000733 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000734
735 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000736 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000737 }
738
739
Wei Liue9ce7cb2014-06-04 10:30:42 +0100740 queue->stats.tx_bytes += skb->len;
741 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000742
Wei Liue9ce7cb2014-06-04 10:30:42 +0100743 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000744 XENVIF_RX_CB(skb)->meta_slots_used,
745 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000746
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000747 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000748 flags = 0;
749 else
750 flags = XEN_NETRXF_more_data;
751
752 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
753 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
754 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
755 /* remote but checksummed. */
756 flags |= XEN_NETRXF_data_validated;
757
758 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100759 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000760 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100761 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000762 flags);
763
Wei Liue9ce7cb2014-06-04 10:30:42 +0100764 if ((1 << queue->meta[npo.meta_cons].gso_type) &
765 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000766 struct xen_netif_extra_info *gso =
767 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100768 RING_GET_RESPONSE(&queue->rx,
769 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000770
771 resp->flags |= XEN_NETRXF_extra_info;
772
Wei Liue9ce7cb2014-06-04 10:30:42 +0100773 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
774 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000775 gso->u.gso.pad = 0;
776 gso->u.gso.features = 0;
777
778 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
779 gso->flags = 0;
780 }
781
Wei Liue9ce7cb2014-06-04 10:30:42 +0100782 xenvif_add_frag_responses(queue, status,
783 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000784 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000785
Wei Liue9ce7cb2014-06-04 10:30:42 +0100786 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000787
Paul Durrant11b57f92014-01-08 12:41:58 +0000788 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100789
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000790 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000791 dev_kfree_skb(skb);
792 }
793
Paul Durrantca2f09f2013-12-06 16:36:07 +0000794done:
Wei Liub3f980b2013-08-26 12:59:38 +0100795 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100796 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000797}
798
Wei Liue9ce7cb2014-06-04 10:30:42 +0100799void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000800{
801 int more_to_do;
802
Wei Liue9ce7cb2014-06-04 10:30:42 +0100803 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000804
805 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100806 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000807}
808
Wei Liue9ce7cb2014-06-04 10:30:42 +0100809static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000810{
811 unsigned long max_burst, max_credit;
812
813 /*
814 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
815 * Otherwise the interface can seize up due to insufficient credit.
816 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100817 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000818 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100819 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000820
821 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100822 max_credit = queue->remaining_credit + queue->credit_bytes;
823 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000824 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
825
Wei Liue9ce7cb2014-06-04 10:30:42 +0100826 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000827}
828
829static void tx_credit_callback(unsigned long data)
830{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100831 struct xenvif_queue *queue = (struct xenvif_queue *)data;
832 tx_add_credit(queue);
833 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000834}
835
Wei Liue9ce7cb2014-06-04 10:30:42 +0100836static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100837 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000838{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100839 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000840 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000841
842 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100843 spin_lock_irqsave(&queue->response_lock, flags);
844 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
845 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000846 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000847 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100848 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000849 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100850 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000851}
852
Wei Liu73764192013-08-26 12:59:39 +0100853static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000854{
855 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100856 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100857 /* Disable the vif from queue 0's kthread */
858 if (vif->queues)
859 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000860}
861
Wei Liue9ce7cb2014-06-04 10:30:42 +0100862static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100863 struct xen_netif_tx_request *first,
864 struct xen_netif_tx_request *txp,
865 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000866{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100867 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000868 int slots = 0;
869 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000870 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000871
872 if (!(first->flags & XEN_NETTXF_more_data))
873 return 0;
874
875 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000876 struct xen_netif_tx_request dropped_tx = { 0 };
877
Wei Liu2810e5b2013-04-22 02:20:42 +0000878 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100879 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000880 "Asked for %d slots but exceeds this limit\n",
881 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100882 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000883 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000884 }
885
Wei Liu2810e5b2013-04-22 02:20:42 +0000886 /* This guest is really using too many slots and
887 * considered malicious.
888 */
Wei Liu37641492013-05-02 00:43:59 +0000889 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100890 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000891 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000892 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100893 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000894 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000895 }
896
Wei Liu2810e5b2013-04-22 02:20:42 +0000897 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000898 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
899 * the historical MAX_SKB_FRAGS value 18 to honor the
900 * same behavior as before. Any packet using more than
901 * 18 slots but less than fatal_skb_slots slots is
902 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000903 */
Wei Liu37641492013-05-02 00:43:59 +0000904 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000905 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100906 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000907 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000908 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000909 drop_err = -E2BIG;
910 }
911
Wei Liu59ccb4e2013-05-02 00:43:58 +0000912 if (drop_err)
913 txp = &dropped_tx;
914
Wei Liue9ce7cb2014-06-04 10:30:42 +0100915 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000916 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000917
918 /* If the guest submitted a frame >= 64 KiB then
919 * first->size overflowed and following slots will
920 * appear to be larger than the frame.
921 *
922 * This cannot be fatal error as there are buggy
923 * frontends that do this.
924 *
925 * Consume all slots and drop the packet.
926 */
927 if (!drop_err && txp->size > first->size) {
928 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100929 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000930 "Invalid tx request, slot size %u > remaining size %u\n",
931 txp->size, first->size);
932 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000933 }
934
935 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000936 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000937
938 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100939 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000940 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100941 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000942 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000943 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000944
945 more_data = txp->flags & XEN_NETTXF_more_data;
946
947 if (!drop_err)
948 txp++;
949
950 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000951
952 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100953 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000954 return drop_err;
955 }
956
957 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000958}
959
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000960
961struct xenvif_tx_cb {
962 u16 pending_idx;
963};
964
965#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
966
Wei Liue9ce7cb2014-06-04 10:30:42 +0100967static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100968 u16 pending_idx,
969 struct xen_netif_tx_request *txp,
970 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000971{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100972 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
973 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000974 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100975 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000976
Wei Liue9ce7cb2014-06-04 10:30:42 +0100977 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000978 sizeof(*txp));
979}
980
Zoltan Kisse3377f32014-03-06 21:48:29 +0000981static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
982{
983 struct sk_buff *skb =
984 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
985 GFP_ATOMIC | __GFP_NOWARN);
986 if (unlikely(skb == NULL))
987 return NULL;
988
989 /* Packets passed to netif_rx() must have some headroom. */
990 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
991
992 /* Initialize it here to avoid later surprises */
993 skb_shinfo(skb)->destructor_arg = NULL;
994
995 return skb;
996}
997
Wei Liue9ce7cb2014-06-04 10:30:42 +0100998static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000999 struct sk_buff *skb,
1000 struct xen_netif_tx_request *txp,
1001 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +00001002{
1003 struct skb_shared_info *shinfo = skb_shinfo(skb);
1004 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001005 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001006 int start;
1007 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001008 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +00001009
1010 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +00001011 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +00001012 */
Zoltan Kisse3377f32014-03-06 21:48:29 +00001013 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
1014 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
1015 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1016 shinfo->nr_frags = MAX_SKB_FRAGS;
1017 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001018 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +00001019
1020 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +00001021 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001022
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001023 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
1024 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001025 index = pending_index(queue->pending_cons++);
1026 pending_idx = queue->pending_ring[index];
1027 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001028 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001029 }
1030
Zoltan Kisse3377f32014-03-06 21:48:29 +00001031 if (frag_overflow) {
1032 struct sk_buff *nskb = xenvif_alloc_skb(0);
1033 if (unlikely(nskb == NULL)) {
1034 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001035 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001036 "Can't allocate the frag_list skb.\n");
1037 return NULL;
1038 }
1039
1040 shinfo = skb_shinfo(nskb);
1041 frags = shinfo->frags;
1042
1043 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
1044 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001045 index = pending_index(queue->pending_cons++);
1046 pending_idx = queue->pending_ring[index];
1047 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001048 frag_set_pending_idx(&frags[shinfo->nr_frags],
1049 pending_idx);
1050 }
1051
1052 skb_shinfo(skb)->frag_list = nskb;
1053 }
Wei Liu2810e5b2013-04-22 02:20:42 +00001054
Ian Campbellf942dc22011-03-15 00:06:18 +00001055 return gop;
1056}
1057
Wei Liue9ce7cb2014-06-04 10:30:42 +01001058static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001059 u16 pending_idx,
1060 grant_handle_t handle)
1061{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001062 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001063 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001064 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001065 "Trying to overwrite active handle! pending_idx: %x\n",
1066 pending_idx);
1067 BUG();
1068 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001069 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001070}
1071
Wei Liue9ce7cb2014-06-04 10:30:42 +01001072static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001073 u16 pending_idx)
1074{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001075 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001076 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001077 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001078 "Trying to unmap invalid handle! pending_idx: %x\n",
1079 pending_idx);
1080 BUG();
1081 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001082 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001083}
1084
Wei Liue9ce7cb2014-06-04 10:30:42 +01001085static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001086 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001087 struct gnttab_map_grant_ref **gopp_map,
1088 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +00001089{
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001090 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001091 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001092 /* This always points to the shinfo of the skb being checked, which
1093 * could be either the first or the one on the frag_list
1094 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001095 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001096 /* If this is non-NULL, we are currently checking the frag_list skb, and
1097 * this points to the shinfo of the first one
1098 */
1099 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001100 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001101 const bool sharedslot = nr_frags &&
1102 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001103 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001104
1105 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001106 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001107 if (unlikely(err)) {
1108 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001109 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001110 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001111 (*gopp_copy)->status,
1112 pending_idx,
1113 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001114 /* The first frag might still have this slot mapped */
1115 if (!sharedslot)
1116 xenvif_idx_release(queue, pending_idx,
1117 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001118 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +01001119 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001120
Zoltan Kisse3377f32014-03-06 21:48:29 +00001121check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001122 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001123 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001124
Ian Campbellea066ad2011-10-05 00:28:46 +00001125 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001126
1127 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001128 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001129
Ian Campbellf942dc22011-03-15 00:06:18 +00001130 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001131 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001132 pending_idx,
1133 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001134 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001135 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001136 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001137 /* If the mapping of the first frag was OK, but
1138 * the header's copy failed, and they are
1139 * sharing a slot, send an error
1140 */
1141 if (i == 0 && sharedslot)
1142 xenvif_idx_release(queue, pending_idx,
1143 XEN_NETIF_RSP_ERROR);
1144 else
1145 xenvif_idx_release(queue, pending_idx,
1146 XEN_NETIF_RSP_OKAY);
1147 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001148 continue;
1149 }
1150
1151 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001152 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001153 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001154 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001155 i,
1156 gop_map->status,
1157 pending_idx,
1158 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001159
Wei Liue9ce7cb2014-06-04 10:30:42 +01001160 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001161
1162 /* Not the first error? Preceding frags already invalidated. */
1163 if (err)
1164 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001165
1166 /* First error: if the header haven't shared a slot with the
1167 * first frag, release it as well.
1168 */
1169 if (!sharedslot)
1170 xenvif_idx_release(queue,
1171 XENVIF_TX_CB(skb)->pending_idx,
1172 XEN_NETIF_RSP_OKAY);
1173
1174 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001175 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001176 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001177 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001178 xenvif_idx_release(queue, pending_idx,
1179 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001180 }
1181
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001182 /* And if we found the error while checking the frag_list, unmap
1183 * the first skb's frags
1184 */
1185 if (first_shinfo) {
1186 for (j = 0; j < first_shinfo->nr_frags; j++) {
1187 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1188 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001189 xenvif_idx_release(queue, pending_idx,
1190 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001191 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001192 }
1193
1194 /* Remember the error: invalidate all subsequent fragments. */
1195 err = newerr;
1196 }
1197
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001198 if (skb_has_frag_list(skb) && !first_shinfo) {
1199 first_shinfo = skb_shinfo(skb);
1200 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001201 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001202
1203 goto check_frags;
1204 }
1205
Zoltan Kissbdab8272014-04-02 18:04:58 +01001206 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001207 return err;
1208}
1209
Wei Liue9ce7cb2014-06-04 10:30:42 +01001210static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001211{
1212 struct skb_shared_info *shinfo = skb_shinfo(skb);
1213 int nr_frags = shinfo->nr_frags;
1214 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001215 u16 prev_pending_idx = INVALID_PENDING_IDX;
1216
Ian Campbellf942dc22011-03-15 00:06:18 +00001217 for (i = 0; i < nr_frags; i++) {
1218 skb_frag_t *frag = shinfo->frags + i;
1219 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001220 struct page *page;
1221 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001222
Ian Campbellea066ad2011-10-05 00:28:46 +00001223 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001224
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001225 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001226 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001227 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001228 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001229 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001230 callback_param(queue, prev_pending_idx).ctx =
1231 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001232
Wei Liue9ce7cb2014-06-04 10:30:42 +01001233 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001234 prev_pending_idx = pending_idx;
1235
Wei Liue9ce7cb2014-06-04 10:30:42 +01001236 txp = &queue->pending_tx_info[pending_idx].req;
1237 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001238 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001239 skb->len += txp->size;
1240 skb->data_len += txp->size;
1241 skb->truesize += txp->size;
1242
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001243 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001244 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001245 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001246 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1247 * overlaps with "index", and "mapping" is not set. I think mapping
1248 * should be set. If delivered to local stack, it would drop this
1249 * skb in sk_filter unless the socket has the right to use it.
1250 */
1251 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001252}
1253
Wei Liue9ce7cb2014-06-04 10:30:42 +01001254static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001255 struct xen_netif_extra_info *extras,
1256 int work_to_do)
1257{
1258 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001259 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001260
1261 do {
1262 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001263 netdev_err(queue->vif->dev, "Missing extra info\n");
1264 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001265 return -EBADR;
1266 }
1267
Wei Liue9ce7cb2014-06-04 10:30:42 +01001268 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001269 sizeof(extra));
1270 if (unlikely(!extra.type ||
1271 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001272 queue->tx.req_cons = ++cons;
1273 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001274 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001275 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001276 return -EINVAL;
1277 }
1278
1279 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001280 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001281 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1282
1283 return work_to_do;
1284}
1285
Wei Liu73764192013-08-26 12:59:39 +01001286static int xenvif_set_skb_gso(struct xenvif *vif,
1287 struct sk_buff *skb,
1288 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001289{
1290 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001291 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001292 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001293 return -EINVAL;
1294 }
1295
Paul Durranta9468582013-10-16 17:50:31 +01001296 switch (gso->u.gso.type) {
1297 case XEN_NETIF_GSO_TYPE_TCPV4:
1298 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1299 break;
1300 case XEN_NETIF_GSO_TYPE_TCPV6:
1301 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1302 break;
1303 default:
Ian Campbell488562862013-02-06 23:41:35 +00001304 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001305 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001306 return -EINVAL;
1307 }
1308
1309 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001310 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001311
1312 return 0;
1313}
1314
Wei Liue9ce7cb2014-06-04 10:30:42 +01001315static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001316{
Paul Durrant27216372014-01-09 10:02:47 +00001317 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001318
Paul Durrant2eba61d2013-10-16 17:50:29 +01001319 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001320 * peers can fail to set NETRXF_csum_blank when sending a GSO
1321 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1322 * recalculate the partial checksum.
1323 */
1324 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001325 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001326 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001327 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001328 }
1329
1330 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1331 if (skb->ip_summed != CHECKSUM_PARTIAL)
1332 return 0;
1333
Paul Durrant27216372014-01-09 10:02:47 +00001334 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001335}
1336
Wei Liue9ce7cb2014-06-04 10:30:42 +01001337static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001338{
Wei Liu059dfa62013-10-28 12:07:57 +00001339 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001340 u64 next_credit = queue->credit_window_start +
1341 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001342
1343 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001344 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001345 return true;
1346
1347 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001348 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001349 queue->credit_window_start = now;
1350 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001351 }
1352
1353 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001354 if (size > queue->remaining_credit) {
1355 queue->credit_timeout.data =
1356 (unsigned long)queue;
1357 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001359 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001360 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001361 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001362
1363 return true;
1364 }
1365
1366 return false;
1367}
1368
Wei Liue9ce7cb2014-06-04 10:30:42 +01001369static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001370 int budget,
1371 unsigned *copy_ops,
1372 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001373{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001374 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001375 struct sk_buff *skb;
1376 int ret;
1377
Wei Liue9ce7cb2014-06-04 10:30:42 +01001378 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001379 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001380 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001381 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1382 u16 pending_idx;
1383 RING_IDX idx;
1384 int work_to_do;
1385 unsigned int data_len;
1386 pending_ring_idx_t index;
1387
Wei Liue9ce7cb2014-06-04 10:30:42 +01001388 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001389 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001390 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001391 "Impossible number of requests. "
1392 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001393 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001394 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001395 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001396 break;
Ian Campbell488562862013-02-06 23:41:35 +00001397 }
1398
Wei Liue9ce7cb2014-06-04 10:30:42 +01001399 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001400 if (!work_to_do)
1401 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001402
Wei Liue9ce7cb2014-06-04 10:30:42 +01001403 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001404 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001405 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001406
1407 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001408 if (txreq.size > queue->remaining_credit &&
1409 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001410 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001411
Wei Liue9ce7cb2014-06-04 10:30:42 +01001412 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001413
1414 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001415 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001416
1417 memset(extras, 0, sizeof(extras));
1418 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001419 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001420 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001421 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001422 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001423 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001424 }
1425
Wei Liue9ce7cb2014-06-04 10:30:42 +01001426 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001427 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001428 break;
Ian Campbell488562862013-02-06 23:41:35 +00001429
Ian Campbellf942dc22011-03-15 00:06:18 +00001430 idx += ret;
1431
1432 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001433 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001434 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001435 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001436 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001437 }
1438
1439 /* No crossing a page as the payload mustn't fragment. */
1440 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001441 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001442 "txreq.offset: %x, size: %u, end: %lu\n",
1443 txreq.offset, txreq.size,
1444 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001445 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001446 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001447 }
1448
Wei Liue9ce7cb2014-06-04 10:30:42 +01001449 index = pending_index(queue->pending_cons);
1450 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001451
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001452 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001453 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001454 XEN_NETBACK_TX_COPY_LEN : txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001455
Zoltan Kisse3377f32014-03-06 21:48:29 +00001456 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001457 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001458 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001459 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001460 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001461 break;
1462 }
1463
Ian Campbellf942dc22011-03-15 00:06:18 +00001464 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1465 struct xen_netif_extra_info *gso;
1466 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1467
Wei Liue9ce7cb2014-06-04 10:30:42 +01001468 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001469 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001470 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001471 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001472 }
1473 }
1474
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001475 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001476
1477 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001478 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1479 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1480 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001481
Wei Liue9ce7cb2014-06-04 10:30:42 +01001482 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001483 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001484 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1485 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001486 offset_in_page(skb->data);
1487
Wei Liue9ce7cb2014-06-04 10:30:42 +01001488 queue->tx_copy_ops[*copy_ops].len = data_len;
1489 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001490
1491 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001492
1493 skb_shinfo(skb)->nr_frags = ret;
1494 if (data_len < txreq.size) {
1495 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001496 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1497 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001498 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001499 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001500 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001501 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1502 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001503 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001504 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001505 }
1506
Wei Liue9ce7cb2014-06-04 10:30:42 +01001507 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001508
Wei Liue9ce7cb2014-06-04 10:30:42 +01001509 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001510 if (request_gop == NULL) {
1511 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001512 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001513 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001514 }
1515 gop = request_gop;
1516
Wei Liue9ce7cb2014-06-04 10:30:42 +01001517 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001518
Wei Liue9ce7cb2014-06-04 10:30:42 +01001519 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001520
Wei Liue9ce7cb2014-06-04 10:30:42 +01001521 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1522 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001523 break;
1524 }
1525
Wei Liue9ce7cb2014-06-04 10:30:42 +01001526 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001527 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001528}
1529
Zoltan Kisse3377f32014-03-06 21:48:29 +00001530/* Consolidate skb with a frag_list into a brand new one with local pages on
1531 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1532 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001533static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001534{
1535 unsigned int offset = skb_headlen(skb);
1536 skb_frag_t frags[MAX_SKB_FRAGS];
1537 int i;
1538 struct ubuf_info *uarg;
1539 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1540
Wei Liue9ce7cb2014-06-04 10:30:42 +01001541 queue->stats.tx_zerocopy_sent += 2;
1542 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001543
Wei Liue9ce7cb2014-06-04 10:30:42 +01001544 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001545 /* Subtract frags size, we will correct it later */
1546 skb->truesize -= skb->data_len;
1547 skb->len += nskb->len;
1548 skb->data_len += nskb->len;
1549
1550 /* create a brand new frags array and coalesce there */
1551 for (i = 0; offset < skb->len; i++) {
1552 struct page *page;
1553 unsigned int len;
1554
1555 BUG_ON(i >= MAX_SKB_FRAGS);
Zoltan Kiss44cc8ed2014-10-28 15:29:31 +00001556 page = alloc_page(GFP_ATOMIC);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001557 if (!page) {
1558 int j;
1559 skb->truesize += skb->data_len;
1560 for (j = 0; j < i; j++)
1561 put_page(frags[j].page.p);
1562 return -ENOMEM;
1563 }
1564
1565 if (offset + PAGE_SIZE < skb->len)
1566 len = PAGE_SIZE;
1567 else
1568 len = skb->len - offset;
1569 if (skb_copy_bits(skb, offset, page_address(page), len))
1570 BUG();
1571
1572 offset += len;
1573 frags[i].page.p = page;
1574 frags[i].page_offset = 0;
1575 skb_frag_size_set(&frags[i], len);
1576 }
1577 /* swap out with old one */
1578 memcpy(skb_shinfo(skb)->frags,
1579 frags,
1580 i * sizeof(skb_frag_t));
1581 skb_shinfo(skb)->nr_frags = i;
1582 skb->truesize += i * PAGE_SIZE;
1583
1584 /* remove traces of mapped pages and frag_list */
1585 skb_frag_list_init(skb);
1586 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001587 /* increase inflight counter to offset decrement in callback */
1588 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001589 uarg->callback(uarg, true);
1590 skb_shinfo(skb)->destructor_arg = NULL;
1591
Wei Liua64bd932014-08-12 11:48:07 +01001592 xenvif_skb_zerocopy_prepare(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001593 kfree_skb(nskb);
1594
1595 return 0;
1596}
Ian Campbellf942dc22011-03-15 00:06:18 +00001597
Wei Liue9ce7cb2014-06-04 10:30:42 +01001598static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001599{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001600 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1601 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001602 struct sk_buff *skb;
1603 int work_done = 0;
1604
Wei Liue9ce7cb2014-06-04 10:30:42 +01001605 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001606 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001607 u16 pending_idx;
1608 unsigned data_len;
1609
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001610 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001611 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001612
1613 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001614 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001615 /* If there was an error, xenvif_tx_check_gop is
1616 * expected to release all the frags which were mapped,
1617 * so kfree_skb shouldn't do it again
1618 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001619 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001620 if (skb_has_frag_list(skb)) {
1621 struct sk_buff *nskb =
1622 skb_shinfo(skb)->frag_list;
1623 skb_shinfo(nskb)->nr_frags = 0;
1624 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001625 kfree_skb(skb);
1626 continue;
1627 }
1628
1629 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001630 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001631 if (data_len < txp->size) {
1632 /* Append the packet payload as a fragment. */
1633 txp->offset += data_len;
1634 txp->size -= data_len;
1635 } else {
1636 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001637 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001638 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001639 }
1640
1641 if (txp->flags & XEN_NETTXF_csum_blank)
1642 skb->ip_summed = CHECKSUM_PARTIAL;
1643 else if (txp->flags & XEN_NETTXF_data_validated)
1644 skb->ip_summed = CHECKSUM_UNNECESSARY;
1645
Wei Liue9ce7cb2014-06-04 10:30:42 +01001646 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001647
Zoltan Kisse3377f32014-03-06 21:48:29 +00001648 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001649 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001650 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001651 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001652 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001653 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001654 kfree_skb(skb);
1655 continue;
1656 }
1657 }
1658
Wei Liue9ce7cb2014-06-04 10:30:42 +01001659 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001660 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001661 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001662
Wei Liue9ce7cb2014-06-04 10:30:42 +01001663 if (checksum_setup(queue, skb)) {
1664 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001665 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001666 /* We have to set this flag to trigger the callback */
1667 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001668 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001669 kfree_skb(skb);
1670 continue;
1671 }
1672
Jason Wang40893fd2013-03-26 23:11:22 +00001673 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001674
Paul Durrantb89587a2013-12-17 11:44:35 +00001675 /* If the packet is GSO then we will have just set up the
1676 * transport header offset in checksum_setup so it's now
1677 * straightforward to calculate gso_segs.
1678 */
1679 if (skb_is_gso(skb)) {
1680 int mss = skb_shinfo(skb)->gso_size;
1681 int hdrlen = skb_transport_header(skb) -
1682 skb_mac_header(skb) +
1683 tcp_hdrlen(skb);
1684
1685 skb_shinfo(skb)->gso_segs =
1686 DIV_ROUND_UP(skb->len - hdrlen, mss);
1687 }
1688
Wei Liue9ce7cb2014-06-04 10:30:42 +01001689 queue->stats.rx_bytes += skb->len;
1690 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001691
Wei Liub3f980b2013-08-26 12:59:38 +01001692 work_done++;
1693
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001694 /* Set this flag right before netif_receive_skb, otherwise
1695 * someone might think this packet already left netback, and
1696 * do a skb_copy_ubufs while we are still in control of the
1697 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1698 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001699 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001700 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001701 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001702 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001703
Wei Liub3f980b2013-08-26 12:59:38 +01001704 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001705 }
Wei Liub3f980b2013-08-26 12:59:38 +01001706
1707 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001708}
1709
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001710void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1711{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001712 unsigned long flags;
1713 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001714 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001715
1716 /* This is the only place where we grab this lock, to protect callbacks
1717 * from each other.
1718 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001719 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001720 do {
1721 u16 pending_idx = ubuf->desc;
1722 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001723 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001724 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001725 index = pending_index(queue->dealloc_prod);
1726 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001727 /* Sync with xenvif_tx_dealloc_action:
1728 * insert idx then incr producer.
1729 */
1730 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001731 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001732 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001733 wake_up(&queue->dealloc_wq);
1734 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001735
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001736 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001737 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001738 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001739 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001740 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001741}
1742
Wei Liue9ce7cb2014-06-04 10:30:42 +01001743static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001744{
1745 struct gnttab_unmap_grant_ref *gop;
1746 pending_ring_idx_t dc, dp;
1747 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1748 unsigned int i = 0;
1749
Wei Liue9ce7cb2014-06-04 10:30:42 +01001750 dc = queue->dealloc_cons;
1751 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001752
1753 /* Free up any grants we have finished using */
1754 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001755 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001756
1757 /* Ensure we see all indices enqueued by all
1758 * xenvif_zerocopy_callback().
1759 */
1760 smp_rmb();
1761
1762 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001763 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001764 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001765 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001766
Wei Liue9ce7cb2014-06-04 10:30:42 +01001767 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001768 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001769 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1770 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001771 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001772 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001773 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001774 queue->grant_tx_handle[pending_idx]);
1775 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001776 ++gop;
1777 }
1778
Wei Liue9ce7cb2014-06-04 10:30:42 +01001779 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001780
Wei Liue9ce7cb2014-06-04 10:30:42 +01001781 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001782
Wei Liue9ce7cb2014-06-04 10:30:42 +01001783 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001784 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001785 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001786 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001787 queue->pages_to_unmap,
1788 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001789 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001790 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1791 gop - queue->tx_unmap_ops, ret);
1792 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001793 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001794 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001795 " host_addr: %llx handle: %x status: %d\n",
1796 gop[i].host_addr,
1797 gop[i].handle,
1798 gop[i].status);
1799 }
1800 BUG();
1801 }
1802 }
1803
Wei Liue9ce7cb2014-06-04 10:30:42 +01001804 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1805 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001806 XEN_NETIF_RSP_OKAY);
1807}
1808
1809
Ian Campbellf942dc22011-03-15 00:06:18 +00001810/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001811int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001812{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001813 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001814 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001815
Wei Liue9ce7cb2014-06-04 10:30:42 +01001816 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001817 return 0;
1818
Wei Liue9ce7cb2014-06-04 10:30:42 +01001819 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001820
Zoltan Kissbdab8272014-04-02 18:04:58 +01001821 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001822 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001823
Wei Liue9ce7cb2014-06-04 10:30:42 +01001824 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001825 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001826 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001827 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001828 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001829 nr_mops);
1830 BUG_ON(ret);
1831 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001832
Wei Liue9ce7cb2014-06-04 10:30:42 +01001833 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001834
1835 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001836}
1837
Wei Liue9ce7cb2014-06-04 10:30:42 +01001838static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001839 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001840{
Ian Campbellf942dc22011-03-15 00:06:18 +00001841 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001842 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001843 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001844
Wei Liue9ce7cb2014-06-04 10:30:42 +01001845 pending_tx_info = &queue->pending_tx_info[pending_idx];
1846 spin_lock_irqsave(&queue->response_lock, flags);
1847 make_tx_response(queue, &pending_tx_info->req, status);
1848 index = pending_index(queue->pending_prod);
1849 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001850 /* TX shouldn't use the index before we give it back here */
1851 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001852 queue->pending_prod++;
1853 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001854}
1855
Wei Liu2810e5b2013-04-22 02:20:42 +00001856
Wei Liue9ce7cb2014-06-04 10:30:42 +01001857static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001858 struct xen_netif_tx_request *txp,
1859 s8 st)
1860{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001861 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001862 struct xen_netif_tx_response *resp;
1863 int notify;
1864
Wei Liue9ce7cb2014-06-04 10:30:42 +01001865 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001866 resp->id = txp->id;
1867 resp->status = st;
1868
1869 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001870 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001871
Wei Liue9ce7cb2014-06-04 10:30:42 +01001872 queue->tx.rsp_prod_pvt = ++i;
1873 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001874 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001875 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001876}
1877
Wei Liue9ce7cb2014-06-04 10:30:42 +01001878static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001879 u16 id,
1880 s8 st,
1881 u16 offset,
1882 u16 size,
1883 u16 flags)
1884{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001885 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001886 struct xen_netif_rx_response *resp;
1887
Wei Liue9ce7cb2014-06-04 10:30:42 +01001888 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001889 resp->offset = offset;
1890 resp->flags = flags;
1891 resp->id = id;
1892 resp->status = (s16)size;
1893 if (st < 0)
1894 resp->status = (s16)st;
1895
Wei Liue9ce7cb2014-06-04 10:30:42 +01001896 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001897
1898 return resp;
1899}
1900
Wei Liue9ce7cb2014-06-04 10:30:42 +01001901void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001902{
1903 int ret;
1904 struct gnttab_unmap_grant_ref tx_unmap_op;
1905
1906 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001907 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001908 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001909 queue->grant_tx_handle[pending_idx]);
1910 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001911
1912 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001913 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001914 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001915 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001916 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1917 ret,
1918 pending_idx,
1919 tx_unmap_op.host_addr,
1920 tx_unmap_op.handle,
1921 tx_unmap_op.status);
1922 BUG();
1923 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001924}
1925
Wei Liue9ce7cb2014-06-04 10:30:42 +01001926static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001927{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001928 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001929 return 1;
1930
1931 return 0;
1932}
1933
Wei Liue9ce7cb2014-06-04 10:30:42 +01001934static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001935{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001936 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001937}
1938
Wei Liue9ce7cb2014-06-04 10:30:42 +01001939void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001940{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001941 if (queue->tx.sring)
1942 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1943 queue->tx.sring);
1944 if (queue->rx.sring)
1945 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1946 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001947}
1948
Wei Liue9ce7cb2014-06-04 10:30:42 +01001949int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001950 grant_ref_t tx_ring_ref,
1951 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001952{
David Vrabelc9d63692011-09-29 16:53:31 +01001953 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001954 struct xen_netif_tx_sring *txs;
1955 struct xen_netif_rx_sring *rxs;
1956
1957 int err = -ENOMEM;
1958
Wei Liue9ce7cb2014-06-04 10:30:42 +01001959 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001960 tx_ring_ref, &addr);
1961 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001962 goto err;
1963
David Vrabelc9d63692011-09-29 16:53:31 +01001964 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001965 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001966
Wei Liue9ce7cb2014-06-04 10:30:42 +01001967 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001968 rx_ring_ref, &addr);
1969 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001970 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001971
David Vrabelc9d63692011-09-29 16:53:31 +01001972 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001973 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001974
1975 return 0;
1976
1977err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001978 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001979 return err;
1980}
1981
David Vrabelecf08d22014-10-22 14:08:55 +01001982static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001983{
David Vrabelecf08d22014-10-22 14:08:55 +01001984 struct xenvif *vif = queue->vif;
1985
1986 queue->stalled = true;
1987
1988 /* At least one queue has stalled? Disable the carrier. */
1989 spin_lock(&vif->lock);
1990 if (vif->stalled_queues++ == 0) {
1991 netdev_info(vif->dev, "Guest Rx stalled");
1992 netif_carrier_off(vif->dev);
1993 }
1994 spin_unlock(&vif->lock);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001995}
1996
David Vrabelecf08d22014-10-22 14:08:55 +01001997static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001998{
David Vrabelecf08d22014-10-22 14:08:55 +01001999 struct xenvif *vif = queue->vif;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002000
David Vrabelecf08d22014-10-22 14:08:55 +01002001 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
2002 queue->stalled = false;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002003
David Vrabelecf08d22014-10-22 14:08:55 +01002004 /* All queues are ready? Enable the carrier. */
2005 spin_lock(&vif->lock);
2006 if (--vif->stalled_queues == 0) {
2007 netdev_info(vif->dev, "Guest Rx ready");
2008 netif_carrier_on(vif->dev);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002009 }
David Vrabelecf08d22014-10-22 14:08:55 +01002010 spin_unlock(&vif->lock);
2011}
2012
2013static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
2014{
2015 RING_IDX prod, cons;
2016
2017 prod = queue->rx.sring->req_prod;
2018 cons = queue->rx.req_cons;
2019
2020 return !queue->stalled
2021 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
2022 && time_after(jiffies,
2023 queue->last_rx_time + rx_stall_timeout_jiffies);
2024}
2025
2026static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
2027{
2028 RING_IDX prod, cons;
2029
2030 prod = queue->rx.sring->req_prod;
2031 cons = queue->rx.req_cons;
2032
2033 return queue->stalled
2034 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
2035}
2036
David Vrabelf48da8b2014-10-22 14:08:54 +01002037static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00002038{
David Vrabelf48da8b2014-10-22 14:08:54 +01002039 return (!skb_queue_empty(&queue->rx_queue)
2040 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
David Vrabelecf08d22014-10-22 14:08:55 +01002041 || xenvif_rx_queue_stalled(queue)
2042 || xenvif_rx_queue_ready(queue)
David Vrabelf48da8b2014-10-22 14:08:54 +01002043 || kthread_should_stop()
2044 || queue->vif->disabled;
Ian Campbellf942dc22011-03-15 00:06:18 +00002045}
2046
David Vrabelf48da8b2014-10-22 14:08:54 +01002047static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002048{
David Vrabelf48da8b2014-10-22 14:08:54 +01002049 struct sk_buff *skb;
2050 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002051
David Vrabelf48da8b2014-10-22 14:08:54 +01002052 skb = skb_peek(&queue->rx_queue);
2053 if (!skb)
2054 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002055
David Vrabelf48da8b2014-10-22 14:08:54 +01002056 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
2057 return timeout < 0 ? 0 : timeout;
2058}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002059
David Vrabelf48da8b2014-10-22 14:08:54 +01002060/* Wait until the guest Rx thread has work.
2061 *
2062 * The timeout needs to be adjusted based on the current head of the
2063 * queue (and not just the head at the beginning). In particular, if
2064 * the queue is initially empty an infinite timeout is used and this
2065 * needs to be reduced when a skb is queued.
2066 *
2067 * This cannot be done with wait_event_timeout() because it only
2068 * calculates the timeout once.
2069 */
2070static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
2071{
2072 DEFINE_WAIT(wait);
2073
2074 if (xenvif_have_rx_work(queue))
2075 return;
2076
2077 for (;;) {
2078 long ret;
2079
2080 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
2081 if (xenvif_have_rx_work(queue))
2082 break;
2083 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
2084 if (!ret)
2085 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002086 }
David Vrabelf48da8b2014-10-22 14:08:54 +01002087 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002088}
2089
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00002090int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01002091{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002092 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01002093 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01002094
David Vrabelf48da8b2014-10-22 14:08:54 +01002095 for (;;) {
2096 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01002097
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01002098 if (kthread_should_stop())
2099 break;
2100
Wei Liue9d8b2c2014-04-01 12:46:12 +01002101 /* This frontend is found to be rogue, disable it in
2102 * kthread context. Currently this is only set when
2103 * netback finds out frontend sends malformed packet,
2104 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01002105 * context so we defer it here, if this thread is
2106 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01002107 */
David Vrabelf48da8b2014-10-22 14:08:54 +01002108 if (unlikely(vif->disabled && queue->id == 0)) {
2109 xenvif_carrier_off(vif);
2110 xenvif_rx_queue_purge(queue);
2111 continue;
Zoltan Kiss09350782014-03-06 21:48:30 +00002112 }
2113
Wei Liue9ce7cb2014-06-04 10:30:42 +01002114 if (!skb_queue_empty(&queue->rx_queue))
2115 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01002116
David Vrabelecf08d22014-10-22 14:08:55 +01002117 /* If the guest hasn't provided any Rx slots for a
2118 * while it's probably not responsive, drop the
2119 * carrier so packets are dropped earlier.
2120 */
2121 if (xenvif_rx_queue_stalled(queue))
2122 xenvif_queue_carrier_off(queue);
2123 else if (xenvif_rx_queue_ready(queue))
2124 xenvif_queue_carrier_on(queue);
2125
David Vrabelf48da8b2014-10-22 14:08:54 +01002126 /* Queued packets may have foreign pages from other
2127 * domains. These cannot be queued indefinitely as
2128 * this would starve guests of grant refs and transmit
2129 * slots.
2130 */
2131 xenvif_rx_queue_drop_expired(queue);
2132
2133 xenvif_rx_queue_maybe_wake(queue);
2134
Wei Liub3f980b2013-08-26 12:59:38 +01002135 cond_resched();
2136 }
2137
Paul Durrantca2f09f2013-12-06 16:36:07 +00002138 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01002139 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00002140
Wei Liub3f980b2013-08-26 12:59:38 +01002141 return 0;
2142}
2143
Wei Liua64bd932014-08-12 11:48:07 +01002144static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2145{
2146 /* Dealloc thread must remain running until all inflight
2147 * packets complete.
2148 */
2149 return kthread_should_stop() &&
2150 !atomic_read(&queue->inflight_packets);
2151}
2152
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002153int xenvif_dealloc_kthread(void *data)
2154{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002155 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002156
Wei Liua64bd932014-08-12 11:48:07 +01002157 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01002158 wait_event_interruptible(queue->dealloc_wq,
2159 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01002160 xenvif_dealloc_kthread_should_stop(queue));
2161 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002162 break;
2163
Wei Liue9ce7cb2014-06-04 10:30:42 +01002164 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002165 cond_resched();
2166 }
2167
2168 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01002169 if (tx_dealloc_work_todo(queue))
2170 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002171
2172 return 0;
2173}
2174
Ian Campbellf942dc22011-03-15 00:06:18 +00002175static int __init netback_init(void)
2176{
Ian Campbellf942dc22011-03-15 00:06:18 +00002177 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00002178
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05002179 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00002180 return -ENODEV;
2181
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01002182 /* Allow as many queues as there are CPUs, by default */
2183 xenvif_max_queues = num_online_cpus();
2184
Wei Liu37641492013-05-02 00:43:59 +00002185 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002186 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2187 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002188 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002189 }
2190
Ian Campbellf942dc22011-03-15 00:06:18 +00002191 rc = xenvif_xenbus_init();
2192 if (rc)
2193 goto failed_init;
2194
Zoltan Kiss09350782014-03-06 21:48:30 +00002195 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
David Vrabelecf08d22014-10-22 14:08:55 +01002196 rx_stall_timeout_jiffies = msecs_to_jiffies(rx_stall_timeout_msecs);
Zoltan Kiss09350782014-03-06 21:48:30 +00002197
Zoltan Kissf51de242014-07-08 19:49:14 +01002198#ifdef CONFIG_DEBUG_FS
2199 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2200 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2201 pr_warn("Init of debugfs returned %ld!\n",
2202 PTR_ERR(xen_netback_dbg_root));
2203#endif /* CONFIG_DEBUG_FS */
2204
Ian Campbellf942dc22011-03-15 00:06:18 +00002205 return 0;
2206
2207failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002208 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002209}
2210
2211module_init(netback_init);
2212
Wei Liub103f352013-05-16 23:26:11 +00002213static void __exit netback_fini(void)
2214{
Zoltan Kissf51de242014-07-08 19:49:14 +01002215#ifdef CONFIG_DEBUG_FS
2216 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2217 debugfs_remove_recursive(xen_netback_dbg_root);
2218#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002219 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002220}
2221module_exit(netback_fini);
2222
Ian Campbellf942dc22011-03-15 00:06:18 +00002223MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002224MODULE_ALIAS("xen-backend:vif");