blob: f7a31d2cb3f1819fdf7ebdeb40e0f6bf44aabe0a [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);
Zoltan Kiss09350782014-03-06 21:48:30 +000063
David Vrabelecf08d22014-10-22 14:08:55 +010064/* The length of time before the frontend is considered unresponsive
65 * because it isn't providing Rx slots.
66 */
David Vrabel26c0e102014-12-18 11:13:06 +000067unsigned int rx_stall_timeout_msecs = 60000;
David Vrabelecf08d22014-10-22 14:08:55 +010068module_param(rx_stall_timeout_msecs, uint, 0444);
David Vrabelecf08d22014-10-22 14:08:55 +010069
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +010070unsigned int xenvif_max_queues;
71module_param_named(max_queues, xenvif_max_queues, uint, 0644);
72MODULE_PARM_DESC(max_queues,
73 "Maximum number of queues per virtual interface");
74
Wei Liu2810e5b2013-04-22 02:20:42 +000075/*
76 * This is the maximum slots a skb can have. If a guest sends a skb
77 * which exceeds this limit it is considered malicious.
78 */
Wei Liu37641492013-05-02 00:43:59 +000079#define FATAL_SKB_SLOTS_DEFAULT 20
80static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
81module_param(fatal_skb_slots, uint, 0444);
82
Malcolm Crossley7e5d7752014-11-05 10:50:22 +000083/* The amount to copy out of the first guest Tx slot into the skb's
84 * linear area. If the first slot has more data, it will be mapped
85 * and put into the first frag.
86 *
87 * This is sized to avoid pulling headers from the frags for most
88 * TCP/IP packets.
89 */
90#define XEN_NETBACK_TX_COPY_LEN 128
91
92
Wei Liue9ce7cb2014-06-04 10:30:42 +010093static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +010094 u8 status);
95
Wei Liue9ce7cb2014-06-04 10:30:42 +010096static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000097 struct xen_netif_tx_request *txp,
98 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010099
Wei Liue9ce7cb2014-06-04 10:30:42 +0100100static inline int tx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +0100101
Wei Liue9ce7cb2014-06-04 10:30:42 +0100102static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +0000103 u16 id,
104 s8 st,
105 u16 offset,
106 u16 size,
107 u16 flags);
108
Wei Liue9ce7cb2014-06-04 10:30:42 +0100109static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000110 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000111{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100112 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000113}
114
Wei Liue9ce7cb2014-06-04 10:30:42 +0100115static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000116 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000117{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100118 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000119}
120
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000121#define callback_param(vif, pending_idx) \
122 (vif->pending_tx_info[pending_idx].callback_struct)
123
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000124/* Find the containing VIF's structure from a pointer in pending_tx_info array
125 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100126static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000127{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000128 u16 pending_idx = ubuf->desc;
129 struct pending_tx_info *temp =
130 container_of(ubuf, struct pending_tx_info, callback_struct);
131 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100132 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000133 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000134}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000135
Ian Campbellea066ad2011-10-05 00:28:46 +0000136static u16 frag_get_pending_idx(skb_frag_t *frag)
137{
138 return (u16)frag->page_offset;
139}
140
141static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
142{
143 frag->page_offset = pending_idx;
144}
145
Ian Campbellf942dc22011-03-15 00:06:18 +0000146static inline pending_ring_idx_t pending_index(unsigned i)
147{
148 return i & (MAX_PENDING_REQS-1);
149}
150
Wei Liue9ce7cb2014-06-04 10:30:42 +0100151bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000152{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000153 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000154
Paul Durrantca2f09f2013-12-06 16:36:07 +0000155 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100156 prod = queue->rx.sring->req_prod;
157 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000158
Paul Durrantca2f09f2013-12-06 16:36:07 +0000159 if (prod - cons >= needed)
160 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000161
Wei Liue9ce7cb2014-06-04 10:30:42 +0100162 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000163
Paul Durrantca2f09f2013-12-06 16:36:07 +0000164 /* Make sure event is visible before we check prod
165 * again.
166 */
167 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100168 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000169
Paul Durrantca2f09f2013-12-06 16:36:07 +0000170 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000171}
172
David Vrabelf48da8b2014-10-22 14:08:54 +0100173void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
174{
175 unsigned long flags;
176
177 spin_lock_irqsave(&queue->rx_queue.lock, flags);
178
179 __skb_queue_tail(&queue->rx_queue, skb);
180
181 queue->rx_queue_len += skb->len;
182 if (queue->rx_queue_len > queue->rx_queue_max)
183 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
184
185 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
186}
187
188static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
189{
190 struct sk_buff *skb;
191
192 spin_lock_irq(&queue->rx_queue.lock);
193
194 skb = __skb_dequeue(&queue->rx_queue);
195 if (skb)
196 queue->rx_queue_len -= skb->len;
197
198 spin_unlock_irq(&queue->rx_queue.lock);
199
200 return skb;
201}
202
203static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
204{
205 spin_lock_irq(&queue->rx_queue.lock);
206
207 if (queue->rx_queue_len < queue->rx_queue_max)
208 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
209
210 spin_unlock_irq(&queue->rx_queue.lock);
211}
212
213
214static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
215{
216 struct sk_buff *skb;
217 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
218 kfree_skb(skb);
219}
220
221static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
222{
223 struct sk_buff *skb;
224
225 for(;;) {
226 skb = skb_peek(&queue->rx_queue);
227 if (!skb)
228 break;
229 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
230 break;
231 xenvif_rx_dequeue(queue);
232 kfree_skb(skb);
233 }
234}
235
Ian Campbellf942dc22011-03-15 00:06:18 +0000236struct netrx_pending_operations {
237 unsigned copy_prod, copy_cons;
238 unsigned meta_prod, meta_cons;
239 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100240 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000241 int copy_off;
242 grant_ref_t copy_gref;
243};
244
Wei Liue9ce7cb2014-06-04 10:30:42 +0100245static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100246 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000247{
Wei Liub3f980b2013-08-26 12:59:38 +0100248 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000249 struct xen_netif_rx_request *req;
250
Wei Liue9ce7cb2014-06-04 10:30:42 +0100251 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000252
253 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100254 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000255 meta->gso_size = 0;
256 meta->size = 0;
257 meta->id = req->id;
258
259 npo->copy_off = 0;
260 npo->copy_gref = req->gref;
261
262 return meta;
263}
264
Wei Liu33bc8012013-10-08 10:54:21 +0100265/*
266 * Set up the grant operations for this fragment. If it's a flipping
267 * interface, we also set up the unmap request from here.
268 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100269static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100270 struct netrx_pending_operations *npo,
271 struct page *page, unsigned long size,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000272 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000273{
274 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100275 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000276 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800277 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000278
279 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000280 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000281
282 meta = npo->meta + npo->meta_prod - 1;
283
Ian Campbell6a8ed462012-10-10 03:48:42 +0000284 /* Skip unused frames from start of page */
285 page += offset >> PAGE_SHIFT;
286 offset &= ~PAGE_MASK;
287
Ian Campbellf942dc22011-03-15 00:06:18 +0000288 while (size > 0) {
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000289 struct xen_page_foreign *foreign;
290
Ian Campbell6a8ed462012-10-10 03:48:42 +0000291 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000292 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
293
David Vrabel1650d542015-01-20 14:49:52 +0000294 if (npo->copy_off == MAX_BUFFER_OFFSET)
295 meta = get_next_rx_buffer(queue, npo);
Ian Campbell6a8ed462012-10-10 03:48:42 +0000296
David Vrabel1650d542015-01-20 14:49:52 +0000297 bytes = PAGE_SIZE - offset;
Ian Campbell6a8ed462012-10-10 03:48:42 +0000298 if (bytes > size)
299 bytes = size;
300
Ian Campbellf942dc22011-03-15 00:06:18 +0000301 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
302 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
303
304 copy_gop = npo->copy + npo->copy_prod++;
305 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100306 copy_gop->len = bytes;
307
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000308 foreign = xen_page_foreign(page);
309 if (foreign) {
310 copy_gop->source.domid = foreign->domid;
311 copy_gop->source.u.ref = foreign->gref;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000312 copy_gop->flags |= GNTCOPY_source_gref;
313 } else {
314 copy_gop->source.domid = DOMID_SELF;
315 copy_gop->source.u.gmfn =
316 virt_to_mfn(page_address(page));
317 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000318 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000319
Wei Liue9ce7cb2014-06-04 10:30:42 +0100320 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000321 copy_gop->dest.offset = npo->copy_off;
322 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000323
324 npo->copy_off += bytes;
325 meta->size += bytes;
326
327 offset += bytes;
328 size -= bytes;
329
Ian Campbell6a8ed462012-10-10 03:48:42 +0000330 /* Next frame */
331 if (offset == PAGE_SIZE && size) {
332 BUG_ON(!PageCompound(page));
333 page++;
334 offset = 0;
335 }
336
Ian Campbellf942dc22011-03-15 00:06:18 +0000337 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800338 if (skb_is_gso(skb)) {
339 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
340 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
341 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
342 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
343 }
Paul Durrant82cada22013-10-16 17:50:32 +0100344
Wei Liue9ce7cb2014-06-04 10:30:42 +0100345 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
346 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000347
Wei Liu33bc8012013-10-08 10:54:21 +0100348 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000349
350 }
351}
352
353/*
354 * Prepare an SKB to be transmitted to the frontend.
355 *
356 * This function is responsible for allocating grant operations, meta
357 * structures, etc.
358 *
359 * It returns the number of meta structures consumed. The number of
360 * ring slots used is always equal to the number of meta slots used
361 * plus the number of GSO descriptors used. Currently, we use either
362 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
363 * frontend-side LRO).
364 */
Wei Liu73764192013-08-26 12:59:39 +0100365static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100366 struct netrx_pending_operations *npo,
367 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000368{
369 struct xenvif *vif = netdev_priv(skb->dev);
370 int nr_frags = skb_shinfo(skb)->nr_frags;
371 int i;
372 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100373 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000374 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100375 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000376 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100377 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000378
379 old_meta_prod = npo->meta_prod;
380
Annie Li5bd07672014-03-10 22:58:34 +0800381 gso_type = XEN_NETIF_GSO_TYPE_NONE;
382 if (skb_is_gso(skb)) {
383 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
384 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
385 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
386 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100387 }
388
Ian Campbellf942dc22011-03-15 00:06:18 +0000389 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000390 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100391 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000392 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100393 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800394 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000395 meta->size = 0;
396 meta->id = req->id;
397 }
398
Wei Liue9ce7cb2014-06-04 10:30:42 +0100399 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000400 meta = npo->meta + npo->meta_prod++;
401
Paul Durrant82cada22013-10-16 17:50:32 +0100402 if ((1 << gso_type) & vif->gso_mask) {
403 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800404 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100405 } else {
406 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000407 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100408 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000409
410 meta->size = 0;
411 meta->id = req->id;
412 npo->copy_off = 0;
413 npo->copy_gref = req->gref;
414
415 data = skb->data;
416 while (data < skb_tail_pointer(skb)) {
417 unsigned int offset = offset_in_page(data);
418 unsigned int len = PAGE_SIZE - offset;
419
420 if (data + len > skb_tail_pointer(skb))
421 len = skb_tail_pointer(skb) - data;
422
Wei Liue9ce7cb2014-06-04 10:30:42 +0100423 xenvif_gop_frag_copy(queue, skb, npo,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000424 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000425 data += len;
426 }
427
428 for (i = 0; i < nr_frags; i++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100429 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100430 skb_frag_page(&skb_shinfo(skb)->frags[i]),
431 skb_frag_size(&skb_shinfo(skb)->frags[i]),
432 skb_shinfo(skb)->frags[i].page_offset,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000433 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000434 }
435
436 return npo->meta_prod - old_meta_prod;
437}
438
439/*
Wei Liu73764192013-08-26 12:59:39 +0100440 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000441 * used to set up the operations on the top of
442 * netrx_pending_operations, which have since been done. Check that
443 * they didn't give any errors and advance over them.
444 */
Wei Liu73764192013-08-26 12:59:39 +0100445static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
446 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000447{
448 struct gnttab_copy *copy_op;
449 int status = XEN_NETIF_RSP_OKAY;
450 int i;
451
452 for (i = 0; i < nr_meta_slots; i++) {
453 copy_op = npo->copy + npo->copy_cons++;
454 if (copy_op->status != GNTST_okay) {
455 netdev_dbg(vif->dev,
456 "Bad status %d from copy to DOM%d.\n",
457 copy_op->status, vif->domid);
458 status = XEN_NETIF_RSP_ERROR;
459 }
460 }
461
462 return status;
463}
464
Wei Liue9ce7cb2014-06-04 10:30:42 +0100465static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100466 struct xenvif_rx_meta *meta,
467 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000468{
469 int i;
470 unsigned long offset;
471
472 /* No fragments used */
473 if (nr_meta_slots <= 1)
474 return;
475
476 nr_meta_slots--;
477
478 for (i = 0; i < nr_meta_slots; i++) {
479 int flags;
480 if (i == nr_meta_slots - 1)
481 flags = 0;
482 else
483 flags = XEN_NETRXF_more_data;
484
485 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100486 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000487 meta[i].size, flags);
488 }
489}
490
Wei Liue9ce7cb2014-06-04 10:30:42 +0100491void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000492{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100493 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100494}
495
Wei Liue9ce7cb2014-06-04 10:30:42 +0100496static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100497{
Ian Campbellf942dc22011-03-15 00:06:18 +0000498 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000499 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000500 struct xen_netif_rx_response *resp;
501 struct sk_buff_head rxq;
502 struct sk_buff *skb;
503 LIST_HEAD(notify);
504 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000505 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000506 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000507
508 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100509 .copy = queue->grant_copy_op,
510 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000511 };
512
513 skb_queue_head_init(&rxq);
514
David Vrabelf48da8b2014-10-22 14:08:54 +0100515 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
516 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
Paul Durrant1425c7a2014-03-28 11:39:07 +0000517 RING_IDX old_req_cons;
518 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000519
David Vrabelecf08d22014-10-22 14:08:55 +0100520 queue->last_rx_time = jiffies;
521
Wei Liue9ce7cb2014-06-04 10:30:42 +0100522 old_req_cons = queue->rx.req_cons;
523 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
524 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000525
Ian Campbellf942dc22011-03-15 00:06:18 +0000526 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000527 }
528
Wei Liue9ce7cb2014-06-04 10:30:42 +0100529 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000530
531 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000532 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000533
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000534 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100535 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000536
537 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000538
Wei Liue9ce7cb2014-06-04 10:30:42 +0100539 if ((1 << queue->meta[npo.meta_cons].gso_type) &
540 queue->vif->gso_prefix_mask) {
541 resp = RING_GET_RESPONSE(&queue->rx,
542 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000543
544 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
545
Wei Liue9ce7cb2014-06-04 10:30:42 +0100546 resp->offset = queue->meta[npo.meta_cons].gso_size;
547 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000548 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000549
550 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000551 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000552 }
553
554
Wei Liue9ce7cb2014-06-04 10:30:42 +0100555 queue->stats.tx_bytes += skb->len;
556 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000557
Wei Liue9ce7cb2014-06-04 10:30:42 +0100558 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000559 XENVIF_RX_CB(skb)->meta_slots_used,
560 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000561
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000562 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000563 flags = 0;
564 else
565 flags = XEN_NETRXF_more_data;
566
567 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
568 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
569 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
570 /* remote but checksummed. */
571 flags |= XEN_NETRXF_data_validated;
572
573 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100574 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000575 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100576 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000577 flags);
578
Wei Liue9ce7cb2014-06-04 10:30:42 +0100579 if ((1 << queue->meta[npo.meta_cons].gso_type) &
580 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000581 struct xen_netif_extra_info *gso =
582 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100583 RING_GET_RESPONSE(&queue->rx,
584 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000585
586 resp->flags |= XEN_NETRXF_extra_info;
587
Wei Liue9ce7cb2014-06-04 10:30:42 +0100588 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
589 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000590 gso->u.gso.pad = 0;
591 gso->u.gso.features = 0;
592
593 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
594 gso->flags = 0;
595 }
596
Wei Liue9ce7cb2014-06-04 10:30:42 +0100597 xenvif_add_frag_responses(queue, status,
598 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000599 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000600
Wei Liue9ce7cb2014-06-04 10:30:42 +0100601 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000602
Paul Durrant11b57f92014-01-08 12:41:58 +0000603 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100604
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000605 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000606 dev_kfree_skb(skb);
607 }
608
Paul Durrantca2f09f2013-12-06 16:36:07 +0000609done:
Wei Liub3f980b2013-08-26 12:59:38 +0100610 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100611 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000612}
613
Wei Liue9ce7cb2014-06-04 10:30:42 +0100614void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000615{
616 int more_to_do;
617
Wei Liue9ce7cb2014-06-04 10:30:42 +0100618 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000619
620 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100621 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000622}
623
Wei Liue9ce7cb2014-06-04 10:30:42 +0100624static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000625{
626 unsigned long max_burst, max_credit;
627
628 /*
629 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
630 * Otherwise the interface can seize up due to insufficient credit.
631 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100632 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000633 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100634 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000635
636 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100637 max_credit = queue->remaining_credit + queue->credit_bytes;
638 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000639 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
640
Wei Liue9ce7cb2014-06-04 10:30:42 +0100641 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000642}
643
644static void tx_credit_callback(unsigned long data)
645{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100646 struct xenvif_queue *queue = (struct xenvif_queue *)data;
647 tx_add_credit(queue);
648 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000649}
650
Wei Liue9ce7cb2014-06-04 10:30:42 +0100651static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100652 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000653{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100654 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000655 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000656
657 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100658 spin_lock_irqsave(&queue->response_lock, flags);
659 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
660 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000661 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000662 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100663 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000664 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100665 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000666}
667
Wei Liu73764192013-08-26 12:59:39 +0100668static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000669{
670 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100671 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100672 /* Disable the vif from queue 0's kthread */
673 if (vif->queues)
674 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000675}
676
Wei Liue9ce7cb2014-06-04 10:30:42 +0100677static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100678 struct xen_netif_tx_request *first,
679 struct xen_netif_tx_request *txp,
680 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000681{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100682 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000683 int slots = 0;
684 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000685 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000686
687 if (!(first->flags & XEN_NETTXF_more_data))
688 return 0;
689
690 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000691 struct xen_netif_tx_request dropped_tx = { 0 };
692
Wei Liu2810e5b2013-04-22 02:20:42 +0000693 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100694 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000695 "Asked for %d slots but exceeds this limit\n",
696 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100697 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000698 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000699 }
700
Wei Liu2810e5b2013-04-22 02:20:42 +0000701 /* This guest is really using too many slots and
702 * considered malicious.
703 */
Wei Liu37641492013-05-02 00:43:59 +0000704 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100705 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000706 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000707 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100708 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000709 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000710 }
711
Wei Liu2810e5b2013-04-22 02:20:42 +0000712 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000713 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
714 * the historical MAX_SKB_FRAGS value 18 to honor the
715 * same behavior as before. Any packet using more than
716 * 18 slots but less than fatal_skb_slots slots is
717 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000718 */
Wei Liu37641492013-05-02 00:43:59 +0000719 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000720 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100721 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000722 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000723 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000724 drop_err = -E2BIG;
725 }
726
Wei Liu59ccb4e2013-05-02 00:43:58 +0000727 if (drop_err)
728 txp = &dropped_tx;
729
Wei Liue9ce7cb2014-06-04 10:30:42 +0100730 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000731 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000732
733 /* If the guest submitted a frame >= 64 KiB then
734 * first->size overflowed and following slots will
735 * appear to be larger than the frame.
736 *
737 * This cannot be fatal error as there are buggy
738 * frontends that do this.
739 *
740 * Consume all slots and drop the packet.
741 */
742 if (!drop_err && txp->size > first->size) {
743 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100744 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000745 "Invalid tx request, slot size %u > remaining size %u\n",
746 txp->size, first->size);
747 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000748 }
749
750 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000751 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000752
753 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100754 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000755 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100756 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000757 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000758 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000759
760 more_data = txp->flags & XEN_NETTXF_more_data;
761
762 if (!drop_err)
763 txp++;
764
765 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000766
767 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100768 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000769 return drop_err;
770 }
771
772 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000773}
774
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000775
776struct xenvif_tx_cb {
777 u16 pending_idx;
778};
779
780#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
781
Wei Liue9ce7cb2014-06-04 10:30:42 +0100782static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100783 u16 pending_idx,
784 struct xen_netif_tx_request *txp,
785 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000786{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100787 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
788 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000789 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100790 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000791
Wei Liue9ce7cb2014-06-04 10:30:42 +0100792 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000793 sizeof(*txp));
794}
795
Zoltan Kisse3377f32014-03-06 21:48:29 +0000796static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
797{
798 struct sk_buff *skb =
799 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
800 GFP_ATOMIC | __GFP_NOWARN);
801 if (unlikely(skb == NULL))
802 return NULL;
803
804 /* Packets passed to netif_rx() must have some headroom. */
805 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
806
807 /* Initialize it here to avoid later surprises */
808 skb_shinfo(skb)->destructor_arg = NULL;
809
810 return skb;
811}
812
Wei Liue9ce7cb2014-06-04 10:30:42 +0100813static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000814 struct sk_buff *skb,
815 struct xen_netif_tx_request *txp,
816 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000817{
818 struct skb_shared_info *shinfo = skb_shinfo(skb);
819 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000820 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000821 int start;
822 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000823 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000824
825 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000826 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000827 */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000828 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
829 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
830 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
831 shinfo->nr_frags = MAX_SKB_FRAGS;
832 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000833 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000834
835 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000836 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000837
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000838 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
839 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100840 index = pending_index(queue->pending_cons++);
841 pending_idx = queue->pending_ring[index];
842 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000843 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000844 }
845
Zoltan Kisse3377f32014-03-06 21:48:29 +0000846 if (frag_overflow) {
847 struct sk_buff *nskb = xenvif_alloc_skb(0);
848 if (unlikely(nskb == NULL)) {
849 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100850 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +0000851 "Can't allocate the frag_list skb.\n");
852 return NULL;
853 }
854
855 shinfo = skb_shinfo(nskb);
856 frags = shinfo->frags;
857
858 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
859 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100860 index = pending_index(queue->pending_cons++);
861 pending_idx = queue->pending_ring[index];
862 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +0000863 frag_set_pending_idx(&frags[shinfo->nr_frags],
864 pending_idx);
865 }
866
867 skb_shinfo(skb)->frag_list = nskb;
868 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000869
Ian Campbellf942dc22011-03-15 00:06:18 +0000870 return gop;
871}
872
Wei Liue9ce7cb2014-06-04 10:30:42 +0100873static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000874 u16 pending_idx,
875 grant_handle_t handle)
876{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100877 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000878 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100879 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000880 "Trying to overwrite active handle! pending_idx: %x\n",
881 pending_idx);
882 BUG();
883 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100884 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000885}
886
Wei Liue9ce7cb2014-06-04 10:30:42 +0100887static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000888 u16 pending_idx)
889{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100890 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000891 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100892 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000893 "Trying to unmap invalid handle! pending_idx: %x\n",
894 pending_idx);
895 BUG();
896 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100897 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000898}
899
Wei Liue9ce7cb2014-06-04 10:30:42 +0100900static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100901 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +0100902 struct gnttab_map_grant_ref **gopp_map,
903 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +0000904{
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100905 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000906 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100907 /* This always points to the shinfo of the skb being checked, which
908 * could be either the first or the one on the frag_list
909 */
Ian Campbellf942dc22011-03-15 00:06:18 +0000910 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100911 /* If this is non-NULL, we are currently checking the frag_list skb, and
912 * this points to the shinfo of the first one
913 */
914 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000915 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100916 const bool sharedslot = nr_frags &&
917 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +0100918 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000919
920 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100921 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +0100922 if (unlikely(err)) {
923 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100924 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +0100925 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +0100926 (*gopp_copy)->status,
927 pending_idx,
928 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100929 /* The first frag might still have this slot mapped */
930 if (!sharedslot)
931 xenvif_idx_release(queue, pending_idx,
932 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +0100933 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +0100934 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000935
Zoltan Kisse3377f32014-03-06 21:48:29 +0000936check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +0100937 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000938 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +0000939
Ian Campbellea066ad2011-10-05 00:28:46 +0000940 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000941
942 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100943 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +0000944
Ian Campbellf942dc22011-03-15 00:06:18 +0000945 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100946 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100947 pending_idx,
948 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000949 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100950 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100951 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100952 /* If the mapping of the first frag was OK, but
953 * the header's copy failed, and they are
954 * sharing a slot, send an error
955 */
956 if (i == 0 && sharedslot)
957 xenvif_idx_release(queue, pending_idx,
958 XEN_NETIF_RSP_ERROR);
959 else
960 xenvif_idx_release(queue, pending_idx,
961 XEN_NETIF_RSP_OKAY);
962 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000963 continue;
964 }
965
966 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100967 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100968 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +0100969 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +0100970 i,
971 gop_map->status,
972 pending_idx,
973 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100974
Wei Liue9ce7cb2014-06-04 10:30:42 +0100975 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000976
977 /* Not the first error? Preceding frags already invalidated. */
978 if (err)
979 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100980
981 /* First error: if the header haven't shared a slot with the
982 * first frag, release it as well.
983 */
984 if (!sharedslot)
985 xenvif_idx_release(queue,
986 XENVIF_TX_CB(skb)->pending_idx,
987 XEN_NETIF_RSP_OKAY);
988
989 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100990 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000991 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100992 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100993 xenvif_idx_release(queue, pending_idx,
994 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000995 }
996
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100997 /* And if we found the error while checking the frag_list, unmap
998 * the first skb's frags
999 */
1000 if (first_shinfo) {
1001 for (j = 0; j < first_shinfo->nr_frags; j++) {
1002 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1003 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001004 xenvif_idx_release(queue, pending_idx,
1005 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001006 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001007 }
1008
1009 /* Remember the error: invalidate all subsequent fragments. */
1010 err = newerr;
1011 }
1012
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001013 if (skb_has_frag_list(skb) && !first_shinfo) {
1014 first_shinfo = skb_shinfo(skb);
1015 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001016 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001017
1018 goto check_frags;
1019 }
1020
Zoltan Kissbdab8272014-04-02 18:04:58 +01001021 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001022 return err;
1023}
1024
Wei Liue9ce7cb2014-06-04 10:30:42 +01001025static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001026{
1027 struct skb_shared_info *shinfo = skb_shinfo(skb);
1028 int nr_frags = shinfo->nr_frags;
1029 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001030 u16 prev_pending_idx = INVALID_PENDING_IDX;
1031
Ian Campbellf942dc22011-03-15 00:06:18 +00001032 for (i = 0; i < nr_frags; i++) {
1033 skb_frag_t *frag = shinfo->frags + i;
1034 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001035 struct page *page;
1036 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001037
Ian Campbellea066ad2011-10-05 00:28:46 +00001038 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001039
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001040 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001041 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001042 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001043 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001044 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001045 callback_param(queue, prev_pending_idx).ctx =
1046 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001047
Wei Liue9ce7cb2014-06-04 10:30:42 +01001048 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001049 prev_pending_idx = pending_idx;
1050
Wei Liue9ce7cb2014-06-04 10:30:42 +01001051 txp = &queue->pending_tx_info[pending_idx].req;
1052 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001053 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001054 skb->len += txp->size;
1055 skb->data_len += txp->size;
1056 skb->truesize += txp->size;
1057
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001058 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001059 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001060 }
1061}
1062
Wei Liue9ce7cb2014-06-04 10:30:42 +01001063static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001064 struct xen_netif_extra_info *extras,
1065 int work_to_do)
1066{
1067 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001068 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001069
1070 do {
1071 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001072 netdev_err(queue->vif->dev, "Missing extra info\n");
1073 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001074 return -EBADR;
1075 }
1076
Wei Liue9ce7cb2014-06-04 10:30:42 +01001077 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001078 sizeof(extra));
1079 if (unlikely(!extra.type ||
1080 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001081 queue->tx.req_cons = ++cons;
1082 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001083 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001084 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001085 return -EINVAL;
1086 }
1087
1088 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001089 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001090 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1091
1092 return work_to_do;
1093}
1094
Wei Liu73764192013-08-26 12:59:39 +01001095static int xenvif_set_skb_gso(struct xenvif *vif,
1096 struct sk_buff *skb,
1097 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001098{
1099 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001100 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001101 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001102 return -EINVAL;
1103 }
1104
Paul Durranta9468582013-10-16 17:50:31 +01001105 switch (gso->u.gso.type) {
1106 case XEN_NETIF_GSO_TYPE_TCPV4:
1107 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1108 break;
1109 case XEN_NETIF_GSO_TYPE_TCPV6:
1110 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1111 break;
1112 default:
Ian Campbell488562862013-02-06 23:41:35 +00001113 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001114 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001115 return -EINVAL;
1116 }
1117
1118 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001119 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001120
1121 return 0;
1122}
1123
Wei Liue9ce7cb2014-06-04 10:30:42 +01001124static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001125{
Paul Durrant27216372014-01-09 10:02:47 +00001126 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001127
Paul Durrant2eba61d2013-10-16 17:50:29 +01001128 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001129 * peers can fail to set NETRXF_csum_blank when sending a GSO
1130 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1131 * recalculate the partial checksum.
1132 */
1133 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001134 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001135 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001136 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001137 }
1138
1139 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1140 if (skb->ip_summed != CHECKSUM_PARTIAL)
1141 return 0;
1142
Paul Durrant27216372014-01-09 10:02:47 +00001143 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001144}
1145
Wei Liue9ce7cb2014-06-04 10:30:42 +01001146static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001147{
Wei Liu059dfa62013-10-28 12:07:57 +00001148 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001149 u64 next_credit = queue->credit_window_start +
1150 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001151
1152 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001153 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001154 return true;
1155
1156 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001157 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001158 queue->credit_window_start = now;
1159 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001160 }
1161
1162 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001163 if (size > queue->remaining_credit) {
1164 queue->credit_timeout.data =
1165 (unsigned long)queue;
1166 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001167 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001168 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001169 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001170 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001171
1172 return true;
1173 }
1174
1175 return false;
1176}
1177
Wei Liue9ce7cb2014-06-04 10:30:42 +01001178static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001179 int budget,
1180 unsigned *copy_ops,
1181 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001182{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001183 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001184 struct sk_buff *skb;
1185 int ret;
1186
Wei Liue9ce7cb2014-06-04 10:30:42 +01001187 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001188 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001189 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001190 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1191 u16 pending_idx;
1192 RING_IDX idx;
1193 int work_to_do;
1194 unsigned int data_len;
1195 pending_ring_idx_t index;
1196
Wei Liue9ce7cb2014-06-04 10:30:42 +01001197 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001198 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001199 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001200 "Impossible number of requests. "
1201 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001202 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001203 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001204 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001205 break;
Ian Campbell488562862013-02-06 23:41:35 +00001206 }
1207
Wei Liue9ce7cb2014-06-04 10:30:42 +01001208 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001209 if (!work_to_do)
1210 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001211
Wei Liue9ce7cb2014-06-04 10:30:42 +01001212 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001213 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001214 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001215
1216 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001217 if (txreq.size > queue->remaining_credit &&
1218 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001219 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001220
Wei Liue9ce7cb2014-06-04 10:30:42 +01001221 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001222
1223 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001224 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001225
1226 memset(extras, 0, sizeof(extras));
1227 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001228 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001229 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001230 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001231 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001232 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001233 }
1234
Wei Liue9ce7cb2014-06-04 10:30:42 +01001235 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001236 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001237 break;
Ian Campbell488562862013-02-06 23:41:35 +00001238
Ian Campbellf942dc22011-03-15 00:06:18 +00001239 idx += ret;
1240
1241 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001242 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001243 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001244 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001245 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001246 }
1247
1248 /* No crossing a page as the payload mustn't fragment. */
1249 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001250 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001251 "txreq.offset: %x, size: %u, end: %lu\n",
1252 txreq.offset, txreq.size,
1253 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001254 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001255 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001256 }
1257
Wei Liue9ce7cb2014-06-04 10:30:42 +01001258 index = pending_index(queue->pending_cons);
1259 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001260
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001261 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001262 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001263 XEN_NETBACK_TX_COPY_LEN : txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001264
Zoltan Kisse3377f32014-03-06 21:48:29 +00001265 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001266 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001267 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001268 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001269 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001270 break;
1271 }
1272
Ian Campbellf942dc22011-03-15 00:06:18 +00001273 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1274 struct xen_netif_extra_info *gso;
1275 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1276
Wei Liue9ce7cb2014-06-04 10:30:42 +01001277 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001278 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001279 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001280 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001281 }
1282 }
1283
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001284 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001285
1286 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001287 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1288 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1289 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001290
Wei Liue9ce7cb2014-06-04 10:30:42 +01001291 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001292 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001293 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1294 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001295 offset_in_page(skb->data);
1296
Wei Liue9ce7cb2014-06-04 10:30:42 +01001297 queue->tx_copy_ops[*copy_ops].len = data_len;
1298 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001299
1300 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001301
1302 skb_shinfo(skb)->nr_frags = ret;
1303 if (data_len < txreq.size) {
1304 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001305 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1306 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001307 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001308 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001309 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001310 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1311 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001312 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001313 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001314 }
1315
Wei Liue9ce7cb2014-06-04 10:30:42 +01001316 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001317
Wei Liue9ce7cb2014-06-04 10:30:42 +01001318 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001319 if (request_gop == NULL) {
1320 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001321 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001322 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001323 }
1324 gop = request_gop;
1325
Wei Liue9ce7cb2014-06-04 10:30:42 +01001326 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001327
Wei Liue9ce7cb2014-06-04 10:30:42 +01001328 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001329
Wei Liue9ce7cb2014-06-04 10:30:42 +01001330 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1331 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001332 break;
1333 }
1334
Wei Liue9ce7cb2014-06-04 10:30:42 +01001335 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001336 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001337}
1338
Zoltan Kisse3377f32014-03-06 21:48:29 +00001339/* Consolidate skb with a frag_list into a brand new one with local pages on
1340 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1341 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001342static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001343{
1344 unsigned int offset = skb_headlen(skb);
1345 skb_frag_t frags[MAX_SKB_FRAGS];
1346 int i;
1347 struct ubuf_info *uarg;
1348 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1349
Wei Liue9ce7cb2014-06-04 10:30:42 +01001350 queue->stats.tx_zerocopy_sent += 2;
1351 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001352
Wei Liue9ce7cb2014-06-04 10:30:42 +01001353 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001354 /* Subtract frags size, we will correct it later */
1355 skb->truesize -= skb->data_len;
1356 skb->len += nskb->len;
1357 skb->data_len += nskb->len;
1358
1359 /* create a brand new frags array and coalesce there */
1360 for (i = 0; offset < skb->len; i++) {
1361 struct page *page;
1362 unsigned int len;
1363
1364 BUG_ON(i >= MAX_SKB_FRAGS);
Zoltan Kiss44cc8ed2014-10-28 15:29:31 +00001365 page = alloc_page(GFP_ATOMIC);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001366 if (!page) {
1367 int j;
1368 skb->truesize += skb->data_len;
1369 for (j = 0; j < i; j++)
1370 put_page(frags[j].page.p);
1371 return -ENOMEM;
1372 }
1373
1374 if (offset + PAGE_SIZE < skb->len)
1375 len = PAGE_SIZE;
1376 else
1377 len = skb->len - offset;
1378 if (skb_copy_bits(skb, offset, page_address(page), len))
1379 BUG();
1380
1381 offset += len;
1382 frags[i].page.p = page;
1383 frags[i].page_offset = 0;
1384 skb_frag_size_set(&frags[i], len);
1385 }
1386 /* swap out with old one */
1387 memcpy(skb_shinfo(skb)->frags,
1388 frags,
1389 i * sizeof(skb_frag_t));
1390 skb_shinfo(skb)->nr_frags = i;
1391 skb->truesize += i * PAGE_SIZE;
1392
1393 /* remove traces of mapped pages and frag_list */
1394 skb_frag_list_init(skb);
1395 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001396 /* increase inflight counter to offset decrement in callback */
1397 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001398 uarg->callback(uarg, true);
1399 skb_shinfo(skb)->destructor_arg = NULL;
1400
Wei Liua64bd932014-08-12 11:48:07 +01001401 xenvif_skb_zerocopy_prepare(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001402 kfree_skb(nskb);
1403
1404 return 0;
1405}
Ian Campbellf942dc22011-03-15 00:06:18 +00001406
Wei Liue9ce7cb2014-06-04 10:30:42 +01001407static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001408{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1410 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001411 struct sk_buff *skb;
1412 int work_done = 0;
1413
Wei Liue9ce7cb2014-06-04 10:30:42 +01001414 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001415 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001416 u16 pending_idx;
1417 unsigned data_len;
1418
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001419 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001420 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001421
1422 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001423 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001424 /* If there was an error, xenvif_tx_check_gop is
1425 * expected to release all the frags which were mapped,
1426 * so kfree_skb shouldn't do it again
1427 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001428 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001429 if (skb_has_frag_list(skb)) {
1430 struct sk_buff *nskb =
1431 skb_shinfo(skb)->frag_list;
1432 skb_shinfo(nskb)->nr_frags = 0;
1433 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001434 kfree_skb(skb);
1435 continue;
1436 }
1437
1438 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001439 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001440 if (data_len < txp->size) {
1441 /* Append the packet payload as a fragment. */
1442 txp->offset += data_len;
1443 txp->size -= data_len;
1444 } else {
1445 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001446 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001447 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001448 }
1449
1450 if (txp->flags & XEN_NETTXF_csum_blank)
1451 skb->ip_summed = CHECKSUM_PARTIAL;
1452 else if (txp->flags & XEN_NETTXF_data_validated)
1453 skb->ip_summed = CHECKSUM_UNNECESSARY;
1454
Wei Liue9ce7cb2014-06-04 10:30:42 +01001455 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001456
Zoltan Kisse3377f32014-03-06 21:48:29 +00001457 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001458 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001459 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001460 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001461 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001462 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001463 kfree_skb(skb);
1464 continue;
1465 }
1466 }
1467
Wei Liue9ce7cb2014-06-04 10:30:42 +01001468 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001469 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001470 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001471
Wei Liue9ce7cb2014-06-04 10:30:42 +01001472 if (checksum_setup(queue, skb)) {
1473 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001474 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001475 /* We have to set this flag to trigger the callback */
1476 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001477 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001478 kfree_skb(skb);
1479 continue;
1480 }
1481
Jason Wang40893fd2013-03-26 23:11:22 +00001482 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001483
Paul Durrantb89587a2013-12-17 11:44:35 +00001484 /* If the packet is GSO then we will have just set up the
1485 * transport header offset in checksum_setup so it's now
1486 * straightforward to calculate gso_segs.
1487 */
1488 if (skb_is_gso(skb)) {
1489 int mss = skb_shinfo(skb)->gso_size;
1490 int hdrlen = skb_transport_header(skb) -
1491 skb_mac_header(skb) +
1492 tcp_hdrlen(skb);
1493
1494 skb_shinfo(skb)->gso_segs =
1495 DIV_ROUND_UP(skb->len - hdrlen, mss);
1496 }
1497
Wei Liue9ce7cb2014-06-04 10:30:42 +01001498 queue->stats.rx_bytes += skb->len;
1499 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001500
Wei Liub3f980b2013-08-26 12:59:38 +01001501 work_done++;
1502
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001503 /* Set this flag right before netif_receive_skb, otherwise
1504 * someone might think this packet already left netback, and
1505 * do a skb_copy_ubufs while we are still in control of the
1506 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1507 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001508 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001509 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001510 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001511 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001512
Wei Liub3f980b2013-08-26 12:59:38 +01001513 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001514 }
Wei Liub3f980b2013-08-26 12:59:38 +01001515
1516 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001517}
1518
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001519void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1520{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001521 unsigned long flags;
1522 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001523 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001524
1525 /* This is the only place where we grab this lock, to protect callbacks
1526 * from each other.
1527 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001528 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001529 do {
1530 u16 pending_idx = ubuf->desc;
1531 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001532 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001533 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001534 index = pending_index(queue->dealloc_prod);
1535 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001536 /* Sync with xenvif_tx_dealloc_action:
1537 * insert idx then incr producer.
1538 */
1539 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001540 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001541 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001542 wake_up(&queue->dealloc_wq);
1543 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001544
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001545 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001546 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001547 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001548 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001549 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001550}
1551
Wei Liue9ce7cb2014-06-04 10:30:42 +01001552static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001553{
1554 struct gnttab_unmap_grant_ref *gop;
1555 pending_ring_idx_t dc, dp;
1556 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1557 unsigned int i = 0;
1558
Wei Liue9ce7cb2014-06-04 10:30:42 +01001559 dc = queue->dealloc_cons;
1560 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001561
1562 /* Free up any grants we have finished using */
1563 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001564 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001565
1566 /* Ensure we see all indices enqueued by all
1567 * xenvif_zerocopy_callback().
1568 */
1569 smp_rmb();
1570
1571 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001572 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001573 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001574 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001575
Wei Liue9ce7cb2014-06-04 10:30:42 +01001576 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001577 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001578 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1579 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001580 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001581 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001582 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001583 queue->grant_tx_handle[pending_idx]);
1584 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001585 ++gop;
1586 }
1587
Wei Liue9ce7cb2014-06-04 10:30:42 +01001588 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001589
Wei Liue9ce7cb2014-06-04 10:30:42 +01001590 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001591
Wei Liue9ce7cb2014-06-04 10:30:42 +01001592 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001593 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001594 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001595 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001596 queue->pages_to_unmap,
1597 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001598 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001599 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1600 gop - queue->tx_unmap_ops, ret);
1601 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001602 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001603 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001604 " host_addr: %llx handle: %x status: %d\n",
1605 gop[i].host_addr,
1606 gop[i].handle,
1607 gop[i].status);
1608 }
1609 BUG();
1610 }
1611 }
1612
Wei Liue9ce7cb2014-06-04 10:30:42 +01001613 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1614 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001615 XEN_NETIF_RSP_OKAY);
1616}
1617
1618
Ian Campbellf942dc22011-03-15 00:06:18 +00001619/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001620int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001621{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001622 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001623 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001624
Wei Liue9ce7cb2014-06-04 10:30:42 +01001625 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001626 return 0;
1627
Wei Liue9ce7cb2014-06-04 10:30:42 +01001628 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001629
Zoltan Kissbdab8272014-04-02 18:04:58 +01001630 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001631 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001632
Wei Liue9ce7cb2014-06-04 10:30:42 +01001633 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001634 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001635 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001636 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001637 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001638 nr_mops);
1639 BUG_ON(ret);
1640 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001641
Wei Liue9ce7cb2014-06-04 10:30:42 +01001642 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001643
1644 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001645}
1646
Wei Liue9ce7cb2014-06-04 10:30:42 +01001647static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001648 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001649{
Ian Campbellf942dc22011-03-15 00:06:18 +00001650 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001651 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001652 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001653
Wei Liue9ce7cb2014-06-04 10:30:42 +01001654 pending_tx_info = &queue->pending_tx_info[pending_idx];
1655 spin_lock_irqsave(&queue->response_lock, flags);
1656 make_tx_response(queue, &pending_tx_info->req, status);
1657 index = pending_index(queue->pending_prod);
1658 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001659 /* TX shouldn't use the index before we give it back here */
1660 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001661 queue->pending_prod++;
1662 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001663}
1664
Wei Liu2810e5b2013-04-22 02:20:42 +00001665
Wei Liue9ce7cb2014-06-04 10:30:42 +01001666static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001667 struct xen_netif_tx_request *txp,
1668 s8 st)
1669{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001670 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001671 struct xen_netif_tx_response *resp;
1672 int notify;
1673
Wei Liue9ce7cb2014-06-04 10:30:42 +01001674 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001675 resp->id = txp->id;
1676 resp->status = st;
1677
1678 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001679 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001680
Wei Liue9ce7cb2014-06-04 10:30:42 +01001681 queue->tx.rsp_prod_pvt = ++i;
1682 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001683 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001684 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001685}
1686
Wei Liue9ce7cb2014-06-04 10:30:42 +01001687static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001688 u16 id,
1689 s8 st,
1690 u16 offset,
1691 u16 size,
1692 u16 flags)
1693{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001694 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001695 struct xen_netif_rx_response *resp;
1696
Wei Liue9ce7cb2014-06-04 10:30:42 +01001697 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001698 resp->offset = offset;
1699 resp->flags = flags;
1700 resp->id = id;
1701 resp->status = (s16)size;
1702 if (st < 0)
1703 resp->status = (s16)st;
1704
Wei Liue9ce7cb2014-06-04 10:30:42 +01001705 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001706
1707 return resp;
1708}
1709
Wei Liue9ce7cb2014-06-04 10:30:42 +01001710void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001711{
1712 int ret;
1713 struct gnttab_unmap_grant_ref tx_unmap_op;
1714
1715 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001716 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001717 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001718 queue->grant_tx_handle[pending_idx]);
1719 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001720
1721 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001722 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001723 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001724 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001725 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1726 ret,
1727 pending_idx,
1728 tx_unmap_op.host_addr,
1729 tx_unmap_op.handle,
1730 tx_unmap_op.status);
1731 BUG();
1732 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001733}
1734
Wei Liue9ce7cb2014-06-04 10:30:42 +01001735static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001736{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001737 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001738 return 1;
1739
1740 return 0;
1741}
1742
Wei Liue9ce7cb2014-06-04 10:30:42 +01001743static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001744{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001745 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001746}
1747
Wei Liue9ce7cb2014-06-04 10:30:42 +01001748void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001749{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001750 if (queue->tx.sring)
1751 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1752 queue->tx.sring);
1753 if (queue->rx.sring)
1754 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1755 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001756}
1757
Wei Liue9ce7cb2014-06-04 10:30:42 +01001758int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001759 grant_ref_t tx_ring_ref,
1760 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001761{
David Vrabelc9d63692011-09-29 16:53:31 +01001762 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001763 struct xen_netif_tx_sring *txs;
1764 struct xen_netif_rx_sring *rxs;
1765
1766 int err = -ENOMEM;
1767
Wei Liue9ce7cb2014-06-04 10:30:42 +01001768 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001769 tx_ring_ref, &addr);
1770 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001771 goto err;
1772
David Vrabelc9d63692011-09-29 16:53:31 +01001773 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001774 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001775
Wei Liue9ce7cb2014-06-04 10:30:42 +01001776 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001777 rx_ring_ref, &addr);
1778 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001779 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001780
David Vrabelc9d63692011-09-29 16:53:31 +01001781 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001782 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001783
1784 return 0;
1785
1786err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001787 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001788 return err;
1789}
1790
David Vrabelecf08d22014-10-22 14:08:55 +01001791static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001792{
David Vrabelecf08d22014-10-22 14:08:55 +01001793 struct xenvif *vif = queue->vif;
1794
1795 queue->stalled = true;
1796
1797 /* At least one queue has stalled? Disable the carrier. */
1798 spin_lock(&vif->lock);
1799 if (vif->stalled_queues++ == 0) {
1800 netdev_info(vif->dev, "Guest Rx stalled");
1801 netif_carrier_off(vif->dev);
1802 }
1803 spin_unlock(&vif->lock);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001804}
1805
David Vrabelecf08d22014-10-22 14:08:55 +01001806static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001807{
David Vrabelecf08d22014-10-22 14:08:55 +01001808 struct xenvif *vif = queue->vif;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001809
David Vrabelecf08d22014-10-22 14:08:55 +01001810 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
1811 queue->stalled = false;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001812
David Vrabelecf08d22014-10-22 14:08:55 +01001813 /* All queues are ready? Enable the carrier. */
1814 spin_lock(&vif->lock);
1815 if (--vif->stalled_queues == 0) {
1816 netdev_info(vif->dev, "Guest Rx ready");
1817 netif_carrier_on(vif->dev);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001818 }
David Vrabelecf08d22014-10-22 14:08:55 +01001819 spin_unlock(&vif->lock);
1820}
1821
1822static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
1823{
1824 RING_IDX prod, cons;
1825
1826 prod = queue->rx.sring->req_prod;
1827 cons = queue->rx.req_cons;
1828
1829 return !queue->stalled
1830 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
1831 && time_after(jiffies,
David Vrabel26c0e102014-12-18 11:13:06 +00001832 queue->last_rx_time + queue->vif->stall_timeout);
David Vrabelecf08d22014-10-22 14:08:55 +01001833}
1834
1835static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
1836{
1837 RING_IDX prod, cons;
1838
1839 prod = queue->rx.sring->req_prod;
1840 cons = queue->rx.req_cons;
1841
1842 return queue->stalled
1843 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
1844}
1845
David Vrabelf48da8b2014-10-22 14:08:54 +01001846static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001847{
David Vrabelf48da8b2014-10-22 14:08:54 +01001848 return (!skb_queue_empty(&queue->rx_queue)
1849 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
David Vrabel26c0e102014-12-18 11:13:06 +00001850 || (queue->vif->stall_timeout &&
1851 (xenvif_rx_queue_stalled(queue)
1852 || xenvif_rx_queue_ready(queue)))
David Vrabelf48da8b2014-10-22 14:08:54 +01001853 || kthread_should_stop()
1854 || queue->vif->disabled;
Ian Campbellf942dc22011-03-15 00:06:18 +00001855}
1856
David Vrabelf48da8b2014-10-22 14:08:54 +01001857static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001858{
David Vrabelf48da8b2014-10-22 14:08:54 +01001859 struct sk_buff *skb;
1860 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001861
David Vrabelf48da8b2014-10-22 14:08:54 +01001862 skb = skb_peek(&queue->rx_queue);
1863 if (!skb)
1864 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001865
David Vrabelf48da8b2014-10-22 14:08:54 +01001866 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1867 return timeout < 0 ? 0 : timeout;
1868}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001869
David Vrabelf48da8b2014-10-22 14:08:54 +01001870/* Wait until the guest Rx thread has work.
1871 *
1872 * The timeout needs to be adjusted based on the current head of the
1873 * queue (and not just the head at the beginning). In particular, if
1874 * the queue is initially empty an infinite timeout is used and this
1875 * needs to be reduced when a skb is queued.
1876 *
1877 * This cannot be done with wait_event_timeout() because it only
1878 * calculates the timeout once.
1879 */
1880static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
1881{
1882 DEFINE_WAIT(wait);
1883
1884 if (xenvif_have_rx_work(queue))
1885 return;
1886
1887 for (;;) {
1888 long ret;
1889
1890 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
1891 if (xenvif_have_rx_work(queue))
1892 break;
1893 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
1894 if (!ret)
1895 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001896 }
David Vrabelf48da8b2014-10-22 14:08:54 +01001897 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001898}
1899
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001900int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001901{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001902 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01001903 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01001904
David Vrabel26c0e102014-12-18 11:13:06 +00001905 if (!vif->stall_timeout)
1906 xenvif_queue_carrier_on(queue);
1907
David Vrabelf48da8b2014-10-22 14:08:54 +01001908 for (;;) {
1909 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001910
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001911 if (kthread_should_stop())
1912 break;
1913
Wei Liue9d8b2c2014-04-01 12:46:12 +01001914 /* This frontend is found to be rogue, disable it in
1915 * kthread context. Currently this is only set when
1916 * netback finds out frontend sends malformed packet,
1917 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01001918 * context so we defer it here, if this thread is
1919 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01001920 */
David Vrabelf48da8b2014-10-22 14:08:54 +01001921 if (unlikely(vif->disabled && queue->id == 0)) {
1922 xenvif_carrier_off(vif);
David Vrabel42b52122015-02-02 16:57:51 +00001923 break;
Zoltan Kiss09350782014-03-06 21:48:30 +00001924 }
1925
Wei Liue9ce7cb2014-06-04 10:30:42 +01001926 if (!skb_queue_empty(&queue->rx_queue))
1927 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001928
David Vrabelecf08d22014-10-22 14:08:55 +01001929 /* If the guest hasn't provided any Rx slots for a
1930 * while it's probably not responsive, drop the
1931 * carrier so packets are dropped earlier.
1932 */
David Vrabel26c0e102014-12-18 11:13:06 +00001933 if (vif->stall_timeout) {
1934 if (xenvif_rx_queue_stalled(queue))
1935 xenvif_queue_carrier_off(queue);
1936 else if (xenvif_rx_queue_ready(queue))
1937 xenvif_queue_carrier_on(queue);
1938 }
David Vrabelecf08d22014-10-22 14:08:55 +01001939
David Vrabelf48da8b2014-10-22 14:08:54 +01001940 /* Queued packets may have foreign pages from other
1941 * domains. These cannot be queued indefinitely as
1942 * this would starve guests of grant refs and transmit
1943 * slots.
1944 */
1945 xenvif_rx_queue_drop_expired(queue);
1946
1947 xenvif_rx_queue_maybe_wake(queue);
1948
Wei Liub3f980b2013-08-26 12:59:38 +01001949 cond_resched();
1950 }
1951
Paul Durrantca2f09f2013-12-06 16:36:07 +00001952 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01001953 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001954
Wei Liub3f980b2013-08-26 12:59:38 +01001955 return 0;
1956}
1957
Wei Liua64bd932014-08-12 11:48:07 +01001958static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1959{
1960 /* Dealloc thread must remain running until all inflight
1961 * packets complete.
1962 */
1963 return kthread_should_stop() &&
1964 !atomic_read(&queue->inflight_packets);
1965}
1966
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001967int xenvif_dealloc_kthread(void *data)
1968{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001969 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001970
Wei Liua64bd932014-08-12 11:48:07 +01001971 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001972 wait_event_interruptible(queue->dealloc_wq,
1973 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01001974 xenvif_dealloc_kthread_should_stop(queue));
1975 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001976 break;
1977
Wei Liue9ce7cb2014-06-04 10:30:42 +01001978 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001979 cond_resched();
1980 }
1981
1982 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01001983 if (tx_dealloc_work_todo(queue))
1984 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001985
1986 return 0;
1987}
1988
Ian Campbellf942dc22011-03-15 00:06:18 +00001989static int __init netback_init(void)
1990{
Ian Campbellf942dc22011-03-15 00:06:18 +00001991 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001992
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001993 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001994 return -ENODEV;
1995
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01001996 /* Allow as many queues as there are CPUs, by default */
1997 xenvif_max_queues = num_online_cpus();
1998
Wei Liu37641492013-05-02 00:43:59 +00001999 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002000 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2001 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002002 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002003 }
2004
Ian Campbellf942dc22011-03-15 00:06:18 +00002005 rc = xenvif_xenbus_init();
2006 if (rc)
2007 goto failed_init;
2008
Zoltan Kissf51de242014-07-08 19:49:14 +01002009#ifdef CONFIG_DEBUG_FS
2010 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2011 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2012 pr_warn("Init of debugfs returned %ld!\n",
2013 PTR_ERR(xen_netback_dbg_root));
2014#endif /* CONFIG_DEBUG_FS */
2015
Ian Campbellf942dc22011-03-15 00:06:18 +00002016 return 0;
2017
2018failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002019 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002020}
2021
2022module_init(netback_init);
2023
Wei Liub103f352013-05-16 23:26:11 +00002024static void __exit netback_fini(void)
2025{
Zoltan Kissf51de242014-07-08 19:49:14 +01002026#ifdef CONFIG_DEBUG_FS
2027 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2028 debugfs_remove_recursive(xen_netback_dbg_root);
2029#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002030 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002031}
2032module_exit(netback_fini);
2033
Ian Campbellf942dc22011-03-15 00:06:18 +00002034MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002035MODULE_ALIAS("xen-backend:vif");