blob: 49322b6c32dffe32f9636610bbebf525e653355e [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,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000272 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100273 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000274 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000275{
276 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100277 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000278 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800279 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000280
281 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000282 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000283
284 meta = npo->meta + npo->meta_prod - 1;
285
Ian Campbell6a8ed462012-10-10 03:48:42 +0000286 /* Skip unused frames from start of page */
287 page += offset >> PAGE_SHIFT;
288 offset &= ~PAGE_MASK;
289
Ian Campbellf942dc22011-03-15 00:06:18 +0000290 while (size > 0) {
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
Wei Liue9ce7cb2014-06-04 10:30:42 +0100308 if (foreign_queue) {
309 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000310 copy_gop->source.u.ref = foreign_gref;
311 copy_gop->flags |= GNTCOPY_source_gref;
312 } else {
313 copy_gop->source.domid = DOMID_SELF;
314 copy_gop->source.u.gmfn =
315 virt_to_mfn(page_address(page));
316 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000317 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000318
Wei Liue9ce7cb2014-06-04 10:30:42 +0100319 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000320 copy_gop->dest.offset = npo->copy_off;
321 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000322
323 npo->copy_off += bytes;
324 meta->size += bytes;
325
326 offset += bytes;
327 size -= bytes;
328
Ian Campbell6a8ed462012-10-10 03:48:42 +0000329 /* Next frame */
330 if (offset == PAGE_SIZE && size) {
331 BUG_ON(!PageCompound(page));
332 page++;
333 offset = 0;
334 }
335
Ian Campbellf942dc22011-03-15 00:06:18 +0000336 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800337 if (skb_is_gso(skb)) {
338 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
339 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
340 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
341 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
342 }
Paul Durrant82cada22013-10-16 17:50:32 +0100343
Wei Liue9ce7cb2014-06-04 10:30:42 +0100344 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
345 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000346
Wei Liu33bc8012013-10-08 10:54:21 +0100347 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000348
349 }
350}
351
352/*
Zoltan Kiss583757442014-05-15 11:08:34 +0100353 * Find the grant ref for a given frag in a chain of struct ubuf_info's
354 * skb: the skb itself
355 * i: the frag's number
356 * ubuf: a pointer to an element in the chain. It should not be NULL
357 *
358 * Returns a pointer to the element in the chain where the page were found. If
359 * not found, returns NULL.
360 * See the definition of callback_struct in common.h for more details about
361 * the chain.
362 */
363static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
364 const int i,
365 const struct ubuf_info *ubuf)
366{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100367 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss583757442014-05-15 11:08:34 +0100368
369 do {
370 u16 pending_idx = ubuf->desc;
371
372 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100373 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss583757442014-05-15 11:08:34 +0100374 break;
375 ubuf = (struct ubuf_info *) ubuf->ctx;
376 } while (ubuf);
377
378 return ubuf;
379}
380
381/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000382 * Prepare an SKB to be transmitted to the frontend.
383 *
384 * This function is responsible for allocating grant operations, meta
385 * structures, etc.
386 *
387 * It returns the number of meta structures consumed. The number of
388 * ring slots used is always equal to the number of meta slots used
389 * plus the number of GSO descriptors used. Currently, we use either
390 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
391 * frontend-side LRO).
392 */
Wei Liu73764192013-08-26 12:59:39 +0100393static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100394 struct netrx_pending_operations *npo,
395 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000396{
397 struct xenvif *vif = netdev_priv(skb->dev);
398 int nr_frags = skb_shinfo(skb)->nr_frags;
399 int i;
400 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100401 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000402 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100403 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000404 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100405 int gso_type;
Zoltan Kiss583757442014-05-15 11:08:34 +0100406 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
407 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000408
409 old_meta_prod = npo->meta_prod;
410
Annie Li5bd07672014-03-10 22:58:34 +0800411 gso_type = XEN_NETIF_GSO_TYPE_NONE;
412 if (skb_is_gso(skb)) {
413 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
414 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
415 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
416 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100417 }
418
Ian Campbellf942dc22011-03-15 00:06:18 +0000419 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000420 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100421 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000422 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100423 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800424 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000425 meta->size = 0;
426 meta->id = req->id;
427 }
428
Wei Liue9ce7cb2014-06-04 10:30:42 +0100429 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000430 meta = npo->meta + npo->meta_prod++;
431
Paul Durrant82cada22013-10-16 17:50:32 +0100432 if ((1 << gso_type) & vif->gso_mask) {
433 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800434 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100435 } else {
436 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000437 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100438 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000439
440 meta->size = 0;
441 meta->id = req->id;
442 npo->copy_off = 0;
443 npo->copy_gref = req->gref;
444
445 data = skb->data;
446 while (data < skb_tail_pointer(skb)) {
447 unsigned int offset = offset_in_page(data);
448 unsigned int len = PAGE_SIZE - offset;
449
450 if (data + len > skb_tail_pointer(skb))
451 len = skb_tail_pointer(skb) - data;
452
Wei Liue9ce7cb2014-06-04 10:30:42 +0100453 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000454 virt_to_page(data), len, offset, &head,
455 NULL,
456 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000457 data += len;
458 }
459
460 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss583757442014-05-15 11:08:34 +0100461 /* This variable also signals whether foreign_gref has a real
462 * value or not.
463 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100464 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss583757442014-05-15 11:08:34 +0100465 grant_ref_t foreign_gref;
466
467 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
468 (ubuf->callback == &xenvif_zerocopy_callback)) {
469 const struct ubuf_info *const startpoint = ubuf;
470
471 /* Ideally ubuf points to the chain element which
472 * belongs to this frag. Or if frags were removed from
473 * the beginning, then shortly before it.
474 */
475 ubuf = xenvif_find_gref(skb, i, ubuf);
476
477 /* Try again from the beginning of the list, if we
478 * haven't tried from there. This only makes sense in
479 * the unlikely event of reordering the original frags.
480 * For injected local pages it's an unnecessary second
481 * run.
482 */
483 if (unlikely(!ubuf) && startpoint != head_ubuf)
484 ubuf = xenvif_find_gref(skb, i, head_ubuf);
485
486 if (likely(ubuf)) {
487 u16 pending_idx = ubuf->desc;
488
Wei Liue9ce7cb2014-06-04 10:30:42 +0100489 foreign_queue = ubuf_to_queue(ubuf);
490 foreign_gref =
491 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss583757442014-05-15 11:08:34 +0100492 /* Just a safety measure. If this was the last
493 * element on the list, the for loop will
494 * iterate again if a local page were added to
495 * the end. Using head_ubuf here prevents the
496 * second search on the chain. Or the original
497 * frags changed order, but that's less likely.
498 * In any way, ubuf shouldn't be NULL.
499 */
500 ubuf = ubuf->ctx ?
501 (struct ubuf_info *) ubuf->ctx :
502 head_ubuf;
503 } else
504 /* This frag was a local page, added to the
505 * array after the skb left netback.
506 */
507 ubuf = head_ubuf;
508 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100509 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100510 skb_frag_page(&skb_shinfo(skb)->frags[i]),
511 skb_frag_size(&skb_shinfo(skb)->frags[i]),
512 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000513 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100514 foreign_queue,
515 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000516 }
517
518 return npo->meta_prod - old_meta_prod;
519}
520
521/*
Wei Liu73764192013-08-26 12:59:39 +0100522 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000523 * used to set up the operations on the top of
524 * netrx_pending_operations, which have since been done. Check that
525 * they didn't give any errors and advance over them.
526 */
Wei Liu73764192013-08-26 12:59:39 +0100527static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
528 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000529{
530 struct gnttab_copy *copy_op;
531 int status = XEN_NETIF_RSP_OKAY;
532 int i;
533
534 for (i = 0; i < nr_meta_slots; i++) {
535 copy_op = npo->copy + npo->copy_cons++;
536 if (copy_op->status != GNTST_okay) {
537 netdev_dbg(vif->dev,
538 "Bad status %d from copy to DOM%d.\n",
539 copy_op->status, vif->domid);
540 status = XEN_NETIF_RSP_ERROR;
541 }
542 }
543
544 return status;
545}
546
Wei Liue9ce7cb2014-06-04 10:30:42 +0100547static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100548 struct xenvif_rx_meta *meta,
549 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000550{
551 int i;
552 unsigned long offset;
553
554 /* No fragments used */
555 if (nr_meta_slots <= 1)
556 return;
557
558 nr_meta_slots--;
559
560 for (i = 0; i < nr_meta_slots; i++) {
561 int flags;
562 if (i == nr_meta_slots - 1)
563 flags = 0;
564 else
565 flags = XEN_NETRXF_more_data;
566
567 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100568 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000569 meta[i].size, flags);
570 }
571}
572
Wei Liue9ce7cb2014-06-04 10:30:42 +0100573void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000574{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100575 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100576}
577
Wei Liue9ce7cb2014-06-04 10:30:42 +0100578static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100579{
Ian Campbellf942dc22011-03-15 00:06:18 +0000580 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000581 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000582 struct xen_netif_rx_response *resp;
583 struct sk_buff_head rxq;
584 struct sk_buff *skb;
585 LIST_HEAD(notify);
586 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000587 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000588 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000589
590 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100591 .copy = queue->grant_copy_op,
592 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000593 };
594
595 skb_queue_head_init(&rxq);
596
David Vrabelf48da8b2014-10-22 14:08:54 +0100597 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
598 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
Paul Durrant1425c7a2014-03-28 11:39:07 +0000599 RING_IDX old_req_cons;
600 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000601
David Vrabelecf08d22014-10-22 14:08:55 +0100602 queue->last_rx_time = jiffies;
603
Wei Liue9ce7cb2014-06-04 10:30:42 +0100604 old_req_cons = queue->rx.req_cons;
605 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
606 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000607
Ian Campbellf942dc22011-03-15 00:06:18 +0000608 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000609 }
610
Wei Liue9ce7cb2014-06-04 10:30:42 +0100611 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000612
613 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000614 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000615
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000616 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100617 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000618
619 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000620
Wei Liue9ce7cb2014-06-04 10:30:42 +0100621 if ((1 << queue->meta[npo.meta_cons].gso_type) &
622 queue->vif->gso_prefix_mask) {
623 resp = RING_GET_RESPONSE(&queue->rx,
624 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000625
626 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
627
Wei Liue9ce7cb2014-06-04 10:30:42 +0100628 resp->offset = queue->meta[npo.meta_cons].gso_size;
629 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000630 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000631
632 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000633 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000634 }
635
636
Wei Liue9ce7cb2014-06-04 10:30:42 +0100637 queue->stats.tx_bytes += skb->len;
638 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000639
Wei Liue9ce7cb2014-06-04 10:30:42 +0100640 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000641 XENVIF_RX_CB(skb)->meta_slots_used,
642 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000643
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000644 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000645 flags = 0;
646 else
647 flags = XEN_NETRXF_more_data;
648
649 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
650 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
651 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
652 /* remote but checksummed. */
653 flags |= XEN_NETRXF_data_validated;
654
655 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100656 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000657 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100658 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000659 flags);
660
Wei Liue9ce7cb2014-06-04 10:30:42 +0100661 if ((1 << queue->meta[npo.meta_cons].gso_type) &
662 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000663 struct xen_netif_extra_info *gso =
664 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100665 RING_GET_RESPONSE(&queue->rx,
666 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000667
668 resp->flags |= XEN_NETRXF_extra_info;
669
Wei Liue9ce7cb2014-06-04 10:30:42 +0100670 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
671 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000672 gso->u.gso.pad = 0;
673 gso->u.gso.features = 0;
674
675 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
676 gso->flags = 0;
677 }
678
Wei Liue9ce7cb2014-06-04 10:30:42 +0100679 xenvif_add_frag_responses(queue, status,
680 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000681 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000682
Wei Liue9ce7cb2014-06-04 10:30:42 +0100683 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000684
Paul Durrant11b57f92014-01-08 12:41:58 +0000685 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100686
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000687 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000688 dev_kfree_skb(skb);
689 }
690
Paul Durrantca2f09f2013-12-06 16:36:07 +0000691done:
Wei Liub3f980b2013-08-26 12:59:38 +0100692 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100693 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000694}
695
Wei Liue9ce7cb2014-06-04 10:30:42 +0100696void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000697{
698 int more_to_do;
699
Wei Liue9ce7cb2014-06-04 10:30:42 +0100700 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000701
702 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100703 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000704}
705
Wei Liue9ce7cb2014-06-04 10:30:42 +0100706static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000707{
708 unsigned long max_burst, max_credit;
709
710 /*
711 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
712 * Otherwise the interface can seize up due to insufficient credit.
713 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100714 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000715 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100716 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000717
718 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100719 max_credit = queue->remaining_credit + queue->credit_bytes;
720 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000721 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
722
Wei Liue9ce7cb2014-06-04 10:30:42 +0100723 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000724}
725
726static void tx_credit_callback(unsigned long data)
727{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100728 struct xenvif_queue *queue = (struct xenvif_queue *)data;
729 tx_add_credit(queue);
730 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000731}
732
Wei Liue9ce7cb2014-06-04 10:30:42 +0100733static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100734 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000735{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100736 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000737 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000738
739 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100740 spin_lock_irqsave(&queue->response_lock, flags);
741 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
742 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000743 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000744 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100745 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000746 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100747 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000748}
749
Wei Liu73764192013-08-26 12:59:39 +0100750static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000751{
752 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100753 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100754 /* Disable the vif from queue 0's kthread */
755 if (vif->queues)
756 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000757}
758
Wei Liue9ce7cb2014-06-04 10:30:42 +0100759static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100760 struct xen_netif_tx_request *first,
761 struct xen_netif_tx_request *txp,
762 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000763{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100764 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000765 int slots = 0;
766 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000767 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000768
769 if (!(first->flags & XEN_NETTXF_more_data))
770 return 0;
771
772 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000773 struct xen_netif_tx_request dropped_tx = { 0 };
774
Wei Liu2810e5b2013-04-22 02:20:42 +0000775 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100776 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000777 "Asked for %d slots but exceeds this limit\n",
778 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100779 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000780 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000781 }
782
Wei Liu2810e5b2013-04-22 02:20:42 +0000783 /* This guest is really using too many slots and
784 * considered malicious.
785 */
Wei Liu37641492013-05-02 00:43:59 +0000786 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100787 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000788 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000789 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100790 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000791 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000792 }
793
Wei Liu2810e5b2013-04-22 02:20:42 +0000794 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000795 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
796 * the historical MAX_SKB_FRAGS value 18 to honor the
797 * same behavior as before. Any packet using more than
798 * 18 slots but less than fatal_skb_slots slots is
799 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000800 */
Wei Liu37641492013-05-02 00:43:59 +0000801 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000802 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100803 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000804 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000805 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000806 drop_err = -E2BIG;
807 }
808
Wei Liu59ccb4e2013-05-02 00:43:58 +0000809 if (drop_err)
810 txp = &dropped_tx;
811
Wei Liue9ce7cb2014-06-04 10:30:42 +0100812 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000813 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000814
815 /* If the guest submitted a frame >= 64 KiB then
816 * first->size overflowed and following slots will
817 * appear to be larger than the frame.
818 *
819 * This cannot be fatal error as there are buggy
820 * frontends that do this.
821 *
822 * Consume all slots and drop the packet.
823 */
824 if (!drop_err && txp->size > first->size) {
825 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100826 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000827 "Invalid tx request, slot size %u > remaining size %u\n",
828 txp->size, first->size);
829 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000830 }
831
832 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000833 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000834
835 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100836 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000837 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100838 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000839 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000840 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000841
842 more_data = txp->flags & XEN_NETTXF_more_data;
843
844 if (!drop_err)
845 txp++;
846
847 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000848
849 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100850 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000851 return drop_err;
852 }
853
854 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000855}
856
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000857
858struct xenvif_tx_cb {
859 u16 pending_idx;
860};
861
862#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
863
Wei Liue9ce7cb2014-06-04 10:30:42 +0100864static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100865 u16 pending_idx,
866 struct xen_netif_tx_request *txp,
867 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000868{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100869 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
870 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000871 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100872 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000873
Wei Liue9ce7cb2014-06-04 10:30:42 +0100874 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000875 sizeof(*txp));
876}
877
Zoltan Kisse3377f32014-03-06 21:48:29 +0000878static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
879{
880 struct sk_buff *skb =
881 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
882 GFP_ATOMIC | __GFP_NOWARN);
883 if (unlikely(skb == NULL))
884 return NULL;
885
886 /* Packets passed to netif_rx() must have some headroom. */
887 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
888
889 /* Initialize it here to avoid later surprises */
890 skb_shinfo(skb)->destructor_arg = NULL;
891
892 return skb;
893}
894
Wei Liue9ce7cb2014-06-04 10:30:42 +0100895static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000896 struct sk_buff *skb,
897 struct xen_netif_tx_request *txp,
898 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000899{
900 struct skb_shared_info *shinfo = skb_shinfo(skb);
901 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000902 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000903 int start;
904 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000905 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000906
907 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000908 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000909 */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000910 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
911 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
912 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
913 shinfo->nr_frags = MAX_SKB_FRAGS;
914 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000915 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000916
917 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000918 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000919
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000920 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
921 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100922 index = pending_index(queue->pending_cons++);
923 pending_idx = queue->pending_ring[index];
924 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000925 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000926 }
927
Zoltan Kisse3377f32014-03-06 21:48:29 +0000928 if (frag_overflow) {
929 struct sk_buff *nskb = xenvif_alloc_skb(0);
930 if (unlikely(nskb == NULL)) {
931 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100932 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +0000933 "Can't allocate the frag_list skb.\n");
934 return NULL;
935 }
936
937 shinfo = skb_shinfo(nskb);
938 frags = shinfo->frags;
939
940 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
941 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100942 index = pending_index(queue->pending_cons++);
943 pending_idx = queue->pending_ring[index];
944 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +0000945 frag_set_pending_idx(&frags[shinfo->nr_frags],
946 pending_idx);
947 }
948
949 skb_shinfo(skb)->frag_list = nskb;
950 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000951
Ian Campbellf942dc22011-03-15 00:06:18 +0000952 return gop;
953}
954
Wei Liue9ce7cb2014-06-04 10:30:42 +0100955static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000956 u16 pending_idx,
957 grant_handle_t handle)
958{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100959 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000960 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100961 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000962 "Trying to overwrite active handle! pending_idx: %x\n",
963 pending_idx);
964 BUG();
965 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100966 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000967}
968
Wei Liue9ce7cb2014-06-04 10:30:42 +0100969static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000970 u16 pending_idx)
971{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100972 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000973 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100974 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000975 "Trying to unmap invalid handle! pending_idx: %x\n",
976 pending_idx);
977 BUG();
978 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100979 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000980}
981
Wei Liue9ce7cb2014-06-04 10:30:42 +0100982static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100983 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +0100984 struct gnttab_map_grant_ref **gopp_map,
985 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +0000986{
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100987 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000988 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100989 /* This always points to the shinfo of the skb being checked, which
990 * could be either the first or the one on the frag_list
991 */
Ian Campbellf942dc22011-03-15 00:06:18 +0000992 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100993 /* If this is non-NULL, we are currently checking the frag_list skb, and
994 * this points to the shinfo of the first one
995 */
996 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000997 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100998 const bool sharedslot = nr_frags &&
999 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001000 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001001
1002 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001003 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001004 if (unlikely(err)) {
1005 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001006 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001007 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001008 (*gopp_copy)->status,
1009 pending_idx,
1010 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001011 /* The first frag might still have this slot mapped */
1012 if (!sharedslot)
1013 xenvif_idx_release(queue, pending_idx,
1014 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001015 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +01001016 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001017
Zoltan Kisse3377f32014-03-06 21:48:29 +00001018check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001019 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001020 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001021
Ian Campbellea066ad2011-10-05 00:28:46 +00001022 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001023
1024 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001025 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001026
Ian Campbellf942dc22011-03-15 00:06:18 +00001027 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001028 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001029 pending_idx,
1030 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001031 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001032 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001033 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001034 /* If the mapping of the first frag was OK, but
1035 * the header's copy failed, and they are
1036 * sharing a slot, send an error
1037 */
1038 if (i == 0 && sharedslot)
1039 xenvif_idx_release(queue, pending_idx,
1040 XEN_NETIF_RSP_ERROR);
1041 else
1042 xenvif_idx_release(queue, pending_idx,
1043 XEN_NETIF_RSP_OKAY);
1044 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001045 continue;
1046 }
1047
1048 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001049 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001050 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001051 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001052 i,
1053 gop_map->status,
1054 pending_idx,
1055 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001056
Wei Liue9ce7cb2014-06-04 10:30:42 +01001057 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001058
1059 /* Not the first error? Preceding frags already invalidated. */
1060 if (err)
1061 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001062
1063 /* First error: if the header haven't shared a slot with the
1064 * first frag, release it as well.
1065 */
1066 if (!sharedslot)
1067 xenvif_idx_release(queue,
1068 XENVIF_TX_CB(skb)->pending_idx,
1069 XEN_NETIF_RSP_OKAY);
1070
1071 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001072 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001073 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001074 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001075 xenvif_idx_release(queue, pending_idx,
1076 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001077 }
1078
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001079 /* And if we found the error while checking the frag_list, unmap
1080 * the first skb's frags
1081 */
1082 if (first_shinfo) {
1083 for (j = 0; j < first_shinfo->nr_frags; j++) {
1084 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
1085 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +01001086 xenvif_idx_release(queue, pending_idx,
1087 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001088 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001089 }
1090
1091 /* Remember the error: invalidate all subsequent fragments. */
1092 err = newerr;
1093 }
1094
Zoltan Kiss1a998d32014-07-18 19:08:02 +01001095 if (skb_has_frag_list(skb) && !first_shinfo) {
1096 first_shinfo = skb_shinfo(skb);
1097 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001098 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001099
1100 goto check_frags;
1101 }
1102
Zoltan Kissbdab8272014-04-02 18:04:58 +01001103 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001104 return err;
1105}
1106
Wei Liue9ce7cb2014-06-04 10:30:42 +01001107static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001108{
1109 struct skb_shared_info *shinfo = skb_shinfo(skb);
1110 int nr_frags = shinfo->nr_frags;
1111 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001112 u16 prev_pending_idx = INVALID_PENDING_IDX;
1113
Ian Campbellf942dc22011-03-15 00:06:18 +00001114 for (i = 0; i < nr_frags; i++) {
1115 skb_frag_t *frag = shinfo->frags + i;
1116 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001117 struct page *page;
1118 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001119
Ian Campbellea066ad2011-10-05 00:28:46 +00001120 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001121
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001122 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001123 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001124 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001125 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001126 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001127 callback_param(queue, prev_pending_idx).ctx =
1128 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001129
Wei Liue9ce7cb2014-06-04 10:30:42 +01001130 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001131 prev_pending_idx = pending_idx;
1132
Wei Liue9ce7cb2014-06-04 10:30:42 +01001133 txp = &queue->pending_tx_info[pending_idx].req;
1134 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001135 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001136 skb->len += txp->size;
1137 skb->data_len += txp->size;
1138 skb->truesize += txp->size;
1139
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001140 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001141 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001142 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001143 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1144 * overlaps with "index", and "mapping" is not set. I think mapping
1145 * should be set. If delivered to local stack, it would drop this
1146 * skb in sk_filter unless the socket has the right to use it.
1147 */
1148 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001149}
1150
Wei Liue9ce7cb2014-06-04 10:30:42 +01001151static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001152 struct xen_netif_extra_info *extras,
1153 int work_to_do)
1154{
1155 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001156 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001157
1158 do {
1159 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001160 netdev_err(queue->vif->dev, "Missing extra info\n");
1161 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001162 return -EBADR;
1163 }
1164
Wei Liue9ce7cb2014-06-04 10:30:42 +01001165 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001166 sizeof(extra));
1167 if (unlikely(!extra.type ||
1168 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001169 queue->tx.req_cons = ++cons;
1170 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001171 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001172 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001173 return -EINVAL;
1174 }
1175
1176 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001177 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001178 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1179
1180 return work_to_do;
1181}
1182
Wei Liu73764192013-08-26 12:59:39 +01001183static int xenvif_set_skb_gso(struct xenvif *vif,
1184 struct sk_buff *skb,
1185 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001186{
1187 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001188 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001189 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001190 return -EINVAL;
1191 }
1192
Paul Durranta9468582013-10-16 17:50:31 +01001193 switch (gso->u.gso.type) {
1194 case XEN_NETIF_GSO_TYPE_TCPV4:
1195 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1196 break;
1197 case XEN_NETIF_GSO_TYPE_TCPV6:
1198 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1199 break;
1200 default:
Ian Campbell488562862013-02-06 23:41:35 +00001201 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001202 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001203 return -EINVAL;
1204 }
1205
1206 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001207 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001208
1209 return 0;
1210}
1211
Wei Liue9ce7cb2014-06-04 10:30:42 +01001212static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001213{
Paul Durrant27216372014-01-09 10:02:47 +00001214 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001215
Paul Durrant2eba61d2013-10-16 17:50:29 +01001216 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001217 * peers can fail to set NETRXF_csum_blank when sending a GSO
1218 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1219 * recalculate the partial checksum.
1220 */
1221 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001222 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001223 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001224 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001225 }
1226
1227 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1228 if (skb->ip_summed != CHECKSUM_PARTIAL)
1229 return 0;
1230
Paul Durrant27216372014-01-09 10:02:47 +00001231 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001232}
1233
Wei Liue9ce7cb2014-06-04 10:30:42 +01001234static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001235{
Wei Liu059dfa62013-10-28 12:07:57 +00001236 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001237 u64 next_credit = queue->credit_window_start +
1238 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001239
1240 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001241 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001242 return true;
1243
1244 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001245 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001246 queue->credit_window_start = now;
1247 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001248 }
1249
1250 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001251 if (size > queue->remaining_credit) {
1252 queue->credit_timeout.data =
1253 (unsigned long)queue;
1254 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001255 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001256 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001257 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001258 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001259
1260 return true;
1261 }
1262
1263 return false;
1264}
1265
Wei Liue9ce7cb2014-06-04 10:30:42 +01001266static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001267 int budget,
1268 unsigned *copy_ops,
1269 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001270{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001271 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001272 struct sk_buff *skb;
1273 int ret;
1274
Wei Liue9ce7cb2014-06-04 10:30:42 +01001275 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001276 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001277 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001278 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1279 u16 pending_idx;
1280 RING_IDX idx;
1281 int work_to_do;
1282 unsigned int data_len;
1283 pending_ring_idx_t index;
1284
Wei Liue9ce7cb2014-06-04 10:30:42 +01001285 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001286 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001287 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001288 "Impossible number of requests. "
1289 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001290 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001291 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001292 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001293 break;
Ian Campbell488562862013-02-06 23:41:35 +00001294 }
1295
Wei Liue9ce7cb2014-06-04 10:30:42 +01001296 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001297 if (!work_to_do)
1298 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001299
Wei Liue9ce7cb2014-06-04 10:30:42 +01001300 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001301 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001302 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001303
1304 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001305 if (txreq.size > queue->remaining_credit &&
1306 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001307 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001308
Wei Liue9ce7cb2014-06-04 10:30:42 +01001309 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001310
1311 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001312 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001313
1314 memset(extras, 0, sizeof(extras));
1315 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001316 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001317 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001318 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001319 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001320 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001321 }
1322
Wei Liue9ce7cb2014-06-04 10:30:42 +01001323 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001324 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001325 break;
Ian Campbell488562862013-02-06 23:41:35 +00001326
Ian Campbellf942dc22011-03-15 00:06:18 +00001327 idx += ret;
1328
1329 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001330 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001331 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001332 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001333 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001334 }
1335
1336 /* No crossing a page as the payload mustn't fragment. */
1337 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001338 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001339 "txreq.offset: %x, size: %u, end: %lu\n",
1340 txreq.offset, txreq.size,
1341 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001342 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001343 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001344 }
1345
Wei Liue9ce7cb2014-06-04 10:30:42 +01001346 index = pending_index(queue->pending_cons);
1347 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001348
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001349 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001350 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001351 XEN_NETBACK_TX_COPY_LEN : txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001352
Zoltan Kisse3377f32014-03-06 21:48:29 +00001353 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001354 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001355 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001356 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001357 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 break;
1359 }
1360
Ian Campbellf942dc22011-03-15 00:06:18 +00001361 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1362 struct xen_netif_extra_info *gso;
1363 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1364
Wei Liue9ce7cb2014-06-04 10:30:42 +01001365 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001366 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001367 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001368 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001369 }
1370 }
1371
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001372 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001373
1374 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001375 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1376 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1377 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001378
Wei Liue9ce7cb2014-06-04 10:30:42 +01001379 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001380 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001381 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1382 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001383 offset_in_page(skb->data);
1384
Wei Liue9ce7cb2014-06-04 10:30:42 +01001385 queue->tx_copy_ops[*copy_ops].len = data_len;
1386 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001387
1388 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001389
1390 skb_shinfo(skb)->nr_frags = ret;
1391 if (data_len < txreq.size) {
1392 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001393 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1394 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001395 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001396 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001397 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001398 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1399 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001400 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001401 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001402 }
1403
Wei Liue9ce7cb2014-06-04 10:30:42 +01001404 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001405
Wei Liue9ce7cb2014-06-04 10:30:42 +01001406 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001407 if (request_gop == NULL) {
1408 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001410 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001411 }
1412 gop = request_gop;
1413
Wei Liue9ce7cb2014-06-04 10:30:42 +01001414 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001415
Wei Liue9ce7cb2014-06-04 10:30:42 +01001416 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001417
Wei Liue9ce7cb2014-06-04 10:30:42 +01001418 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1419 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001420 break;
1421 }
1422
Wei Liue9ce7cb2014-06-04 10:30:42 +01001423 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001424 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001425}
1426
Zoltan Kisse3377f32014-03-06 21:48:29 +00001427/* Consolidate skb with a frag_list into a brand new one with local pages on
1428 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1429 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001430static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001431{
1432 unsigned int offset = skb_headlen(skb);
1433 skb_frag_t frags[MAX_SKB_FRAGS];
1434 int i;
1435 struct ubuf_info *uarg;
1436 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1437
Wei Liue9ce7cb2014-06-04 10:30:42 +01001438 queue->stats.tx_zerocopy_sent += 2;
1439 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001440
Wei Liue9ce7cb2014-06-04 10:30:42 +01001441 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001442 /* Subtract frags size, we will correct it later */
1443 skb->truesize -= skb->data_len;
1444 skb->len += nskb->len;
1445 skb->data_len += nskb->len;
1446
1447 /* create a brand new frags array and coalesce there */
1448 for (i = 0; offset < skb->len; i++) {
1449 struct page *page;
1450 unsigned int len;
1451
1452 BUG_ON(i >= MAX_SKB_FRAGS);
Zoltan Kiss44cc8ed2014-10-28 15:29:31 +00001453 page = alloc_page(GFP_ATOMIC);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001454 if (!page) {
1455 int j;
1456 skb->truesize += skb->data_len;
1457 for (j = 0; j < i; j++)
1458 put_page(frags[j].page.p);
1459 return -ENOMEM;
1460 }
1461
1462 if (offset + PAGE_SIZE < skb->len)
1463 len = PAGE_SIZE;
1464 else
1465 len = skb->len - offset;
1466 if (skb_copy_bits(skb, offset, page_address(page), len))
1467 BUG();
1468
1469 offset += len;
1470 frags[i].page.p = page;
1471 frags[i].page_offset = 0;
1472 skb_frag_size_set(&frags[i], len);
1473 }
1474 /* swap out with old one */
1475 memcpy(skb_shinfo(skb)->frags,
1476 frags,
1477 i * sizeof(skb_frag_t));
1478 skb_shinfo(skb)->nr_frags = i;
1479 skb->truesize += i * PAGE_SIZE;
1480
1481 /* remove traces of mapped pages and frag_list */
1482 skb_frag_list_init(skb);
1483 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001484 /* increase inflight counter to offset decrement in callback */
1485 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001486 uarg->callback(uarg, true);
1487 skb_shinfo(skb)->destructor_arg = NULL;
1488
Wei Liua64bd932014-08-12 11:48:07 +01001489 xenvif_skb_zerocopy_prepare(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001490 kfree_skb(nskb);
1491
1492 return 0;
1493}
Ian Campbellf942dc22011-03-15 00:06:18 +00001494
Wei Liue9ce7cb2014-06-04 10:30:42 +01001495static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001496{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001497 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1498 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001499 struct sk_buff *skb;
1500 int work_done = 0;
1501
Wei Liue9ce7cb2014-06-04 10:30:42 +01001502 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001503 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001504 u16 pending_idx;
1505 unsigned data_len;
1506
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001507 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001508 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001509
1510 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001511 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001512 /* If there was an error, xenvif_tx_check_gop is
1513 * expected to release all the frags which were mapped,
1514 * so kfree_skb shouldn't do it again
1515 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001516 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001517 if (skb_has_frag_list(skb)) {
1518 struct sk_buff *nskb =
1519 skb_shinfo(skb)->frag_list;
1520 skb_shinfo(nskb)->nr_frags = 0;
1521 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001522 kfree_skb(skb);
1523 continue;
1524 }
1525
1526 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001527 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001528 if (data_len < txp->size) {
1529 /* Append the packet payload as a fragment. */
1530 txp->offset += data_len;
1531 txp->size -= data_len;
1532 } else {
1533 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001534 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001535 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001536 }
1537
1538 if (txp->flags & XEN_NETTXF_csum_blank)
1539 skb->ip_summed = CHECKSUM_PARTIAL;
1540 else if (txp->flags & XEN_NETTXF_data_validated)
1541 skb->ip_summed = CHECKSUM_UNNECESSARY;
1542
Wei Liue9ce7cb2014-06-04 10:30:42 +01001543 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001544
Zoltan Kisse3377f32014-03-06 21:48:29 +00001545 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001546 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001547 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001548 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001549 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001550 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001551 kfree_skb(skb);
1552 continue;
1553 }
1554 }
1555
Wei Liue9ce7cb2014-06-04 10:30:42 +01001556 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001557 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001558 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001559
Wei Liue9ce7cb2014-06-04 10:30:42 +01001560 if (checksum_setup(queue, skb)) {
1561 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001562 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001563 /* We have to set this flag to trigger the callback */
1564 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001565 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001566 kfree_skb(skb);
1567 continue;
1568 }
1569
Jason Wang40893fd2013-03-26 23:11:22 +00001570 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001571
Paul Durrantb89587a2013-12-17 11:44:35 +00001572 /* If the packet is GSO then we will have just set up the
1573 * transport header offset in checksum_setup so it's now
1574 * straightforward to calculate gso_segs.
1575 */
1576 if (skb_is_gso(skb)) {
1577 int mss = skb_shinfo(skb)->gso_size;
1578 int hdrlen = skb_transport_header(skb) -
1579 skb_mac_header(skb) +
1580 tcp_hdrlen(skb);
1581
1582 skb_shinfo(skb)->gso_segs =
1583 DIV_ROUND_UP(skb->len - hdrlen, mss);
1584 }
1585
Wei Liue9ce7cb2014-06-04 10:30:42 +01001586 queue->stats.rx_bytes += skb->len;
1587 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001588
Wei Liub3f980b2013-08-26 12:59:38 +01001589 work_done++;
1590
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001591 /* Set this flag right before netif_receive_skb, otherwise
1592 * someone might think this packet already left netback, and
1593 * do a skb_copy_ubufs while we are still in control of the
1594 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1595 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001596 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001597 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001598 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001599 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001600
Wei Liub3f980b2013-08-26 12:59:38 +01001601 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001602 }
Wei Liub3f980b2013-08-26 12:59:38 +01001603
1604 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001605}
1606
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001607void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1608{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001609 unsigned long flags;
1610 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001611 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001612
1613 /* This is the only place where we grab this lock, to protect callbacks
1614 * from each other.
1615 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001616 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001617 do {
1618 u16 pending_idx = ubuf->desc;
1619 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001620 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001621 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001622 index = pending_index(queue->dealloc_prod);
1623 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001624 /* Sync with xenvif_tx_dealloc_action:
1625 * insert idx then incr producer.
1626 */
1627 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001628 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001629 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001630 wake_up(&queue->dealloc_wq);
1631 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001632
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001633 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001634 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001635 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001636 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001637 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001638}
1639
Wei Liue9ce7cb2014-06-04 10:30:42 +01001640static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001641{
1642 struct gnttab_unmap_grant_ref *gop;
1643 pending_ring_idx_t dc, dp;
1644 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1645 unsigned int i = 0;
1646
Wei Liue9ce7cb2014-06-04 10:30:42 +01001647 dc = queue->dealloc_cons;
1648 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001649
1650 /* Free up any grants we have finished using */
1651 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001652 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001653
1654 /* Ensure we see all indices enqueued by all
1655 * xenvif_zerocopy_callback().
1656 */
1657 smp_rmb();
1658
1659 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001660 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001661 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001662 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001663
Wei Liue9ce7cb2014-06-04 10:30:42 +01001664 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001665 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001666 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1667 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001668 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001669 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001670 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001671 queue->grant_tx_handle[pending_idx]);
1672 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001673 ++gop;
1674 }
1675
Wei Liue9ce7cb2014-06-04 10:30:42 +01001676 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001677
Wei Liue9ce7cb2014-06-04 10:30:42 +01001678 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001679
Wei Liue9ce7cb2014-06-04 10:30:42 +01001680 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001681 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001682 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001683 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001684 queue->pages_to_unmap,
1685 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001686 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001687 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1688 gop - queue->tx_unmap_ops, ret);
1689 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001690 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001691 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001692 " host_addr: %llx handle: %x status: %d\n",
1693 gop[i].host_addr,
1694 gop[i].handle,
1695 gop[i].status);
1696 }
1697 BUG();
1698 }
1699 }
1700
Wei Liue9ce7cb2014-06-04 10:30:42 +01001701 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1702 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001703 XEN_NETIF_RSP_OKAY);
1704}
1705
1706
Ian Campbellf942dc22011-03-15 00:06:18 +00001707/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001708int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001709{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001710 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001711 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001712
Wei Liue9ce7cb2014-06-04 10:30:42 +01001713 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001714 return 0;
1715
Wei Liue9ce7cb2014-06-04 10:30:42 +01001716 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001717
Zoltan Kissbdab8272014-04-02 18:04:58 +01001718 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001719 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001720
Wei Liue9ce7cb2014-06-04 10:30:42 +01001721 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001722 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001723 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001724 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001725 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001726 nr_mops);
1727 BUG_ON(ret);
1728 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001729
Wei Liue9ce7cb2014-06-04 10:30:42 +01001730 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001731
1732 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001733}
1734
Wei Liue9ce7cb2014-06-04 10:30:42 +01001735static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001736 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001737{
Ian Campbellf942dc22011-03-15 00:06:18 +00001738 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001739 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001740 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001741
Wei Liue9ce7cb2014-06-04 10:30:42 +01001742 pending_tx_info = &queue->pending_tx_info[pending_idx];
1743 spin_lock_irqsave(&queue->response_lock, flags);
1744 make_tx_response(queue, &pending_tx_info->req, status);
1745 index = pending_index(queue->pending_prod);
1746 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001747 /* TX shouldn't use the index before we give it back here */
1748 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001749 queue->pending_prod++;
1750 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001751}
1752
Wei Liu2810e5b2013-04-22 02:20:42 +00001753
Wei Liue9ce7cb2014-06-04 10:30:42 +01001754static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001755 struct xen_netif_tx_request *txp,
1756 s8 st)
1757{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001758 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001759 struct xen_netif_tx_response *resp;
1760 int notify;
1761
Wei Liue9ce7cb2014-06-04 10:30:42 +01001762 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001763 resp->id = txp->id;
1764 resp->status = st;
1765
1766 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001767 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001768
Wei Liue9ce7cb2014-06-04 10:30:42 +01001769 queue->tx.rsp_prod_pvt = ++i;
1770 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001771 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001772 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001773}
1774
Wei Liue9ce7cb2014-06-04 10:30:42 +01001775static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001776 u16 id,
1777 s8 st,
1778 u16 offset,
1779 u16 size,
1780 u16 flags)
1781{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001782 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001783 struct xen_netif_rx_response *resp;
1784
Wei Liue9ce7cb2014-06-04 10:30:42 +01001785 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001786 resp->offset = offset;
1787 resp->flags = flags;
1788 resp->id = id;
1789 resp->status = (s16)size;
1790 if (st < 0)
1791 resp->status = (s16)st;
1792
Wei Liue9ce7cb2014-06-04 10:30:42 +01001793 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001794
1795 return resp;
1796}
1797
Wei Liue9ce7cb2014-06-04 10:30:42 +01001798void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001799{
1800 int ret;
1801 struct gnttab_unmap_grant_ref tx_unmap_op;
1802
1803 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001804 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001805 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001806 queue->grant_tx_handle[pending_idx]);
1807 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001808
1809 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001810 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001811 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001812 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001813 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1814 ret,
1815 pending_idx,
1816 tx_unmap_op.host_addr,
1817 tx_unmap_op.handle,
1818 tx_unmap_op.status);
1819 BUG();
1820 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001821}
1822
Wei Liue9ce7cb2014-06-04 10:30:42 +01001823static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001824{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001825 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001826 return 1;
1827
1828 return 0;
1829}
1830
Wei Liue9ce7cb2014-06-04 10:30:42 +01001831static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001832{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001833 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001834}
1835
Wei Liue9ce7cb2014-06-04 10:30:42 +01001836void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001837{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001838 if (queue->tx.sring)
1839 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1840 queue->tx.sring);
1841 if (queue->rx.sring)
1842 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1843 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001844}
1845
Wei Liue9ce7cb2014-06-04 10:30:42 +01001846int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001847 grant_ref_t tx_ring_ref,
1848 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001849{
David Vrabelc9d63692011-09-29 16:53:31 +01001850 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001851 struct xen_netif_tx_sring *txs;
1852 struct xen_netif_rx_sring *rxs;
1853
1854 int err = -ENOMEM;
1855
Wei Liue9ce7cb2014-06-04 10:30:42 +01001856 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001857 tx_ring_ref, &addr);
1858 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001859 goto err;
1860
David Vrabelc9d63692011-09-29 16:53:31 +01001861 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001862 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001863
Wei Liue9ce7cb2014-06-04 10:30:42 +01001864 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001865 rx_ring_ref, &addr);
1866 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001867 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001868
David Vrabelc9d63692011-09-29 16:53:31 +01001869 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001870 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001871
1872 return 0;
1873
1874err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001875 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001876 return err;
1877}
1878
David Vrabelecf08d22014-10-22 14:08:55 +01001879static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001880{
David Vrabelecf08d22014-10-22 14:08:55 +01001881 struct xenvif *vif = queue->vif;
1882
1883 queue->stalled = true;
1884
1885 /* At least one queue has stalled? Disable the carrier. */
1886 spin_lock(&vif->lock);
1887 if (vif->stalled_queues++ == 0) {
1888 netdev_info(vif->dev, "Guest Rx stalled");
1889 netif_carrier_off(vif->dev);
1890 }
1891 spin_unlock(&vif->lock);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001892}
1893
David Vrabelecf08d22014-10-22 14:08:55 +01001894static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001895{
David Vrabelecf08d22014-10-22 14:08:55 +01001896 struct xenvif *vif = queue->vif;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001897
David Vrabelecf08d22014-10-22 14:08:55 +01001898 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
1899 queue->stalled = false;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001900
David Vrabelecf08d22014-10-22 14:08:55 +01001901 /* All queues are ready? Enable the carrier. */
1902 spin_lock(&vif->lock);
1903 if (--vif->stalled_queues == 0) {
1904 netdev_info(vif->dev, "Guest Rx ready");
1905 netif_carrier_on(vif->dev);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001906 }
David Vrabelecf08d22014-10-22 14:08:55 +01001907 spin_unlock(&vif->lock);
1908}
1909
1910static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
1911{
1912 RING_IDX prod, cons;
1913
1914 prod = queue->rx.sring->req_prod;
1915 cons = queue->rx.req_cons;
1916
1917 return !queue->stalled
1918 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
1919 && time_after(jiffies,
David Vrabel26c0e102014-12-18 11:13:06 +00001920 queue->last_rx_time + queue->vif->stall_timeout);
David Vrabelecf08d22014-10-22 14:08:55 +01001921}
1922
1923static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
1924{
1925 RING_IDX prod, cons;
1926
1927 prod = queue->rx.sring->req_prod;
1928 cons = queue->rx.req_cons;
1929
1930 return queue->stalled
1931 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
1932}
1933
David Vrabelf48da8b2014-10-22 14:08:54 +01001934static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001935{
David Vrabelf48da8b2014-10-22 14:08:54 +01001936 return (!skb_queue_empty(&queue->rx_queue)
1937 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
David Vrabel26c0e102014-12-18 11:13:06 +00001938 || (queue->vif->stall_timeout &&
1939 (xenvif_rx_queue_stalled(queue)
1940 || xenvif_rx_queue_ready(queue)))
David Vrabelf48da8b2014-10-22 14:08:54 +01001941 || kthread_should_stop()
1942 || queue->vif->disabled;
Ian Campbellf942dc22011-03-15 00:06:18 +00001943}
1944
David Vrabelf48da8b2014-10-22 14:08:54 +01001945static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001946{
David Vrabelf48da8b2014-10-22 14:08:54 +01001947 struct sk_buff *skb;
1948 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001949
David Vrabelf48da8b2014-10-22 14:08:54 +01001950 skb = skb_peek(&queue->rx_queue);
1951 if (!skb)
1952 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001953
David Vrabelf48da8b2014-10-22 14:08:54 +01001954 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1955 return timeout < 0 ? 0 : timeout;
1956}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001957
David Vrabelf48da8b2014-10-22 14:08:54 +01001958/* Wait until the guest Rx thread has work.
1959 *
1960 * The timeout needs to be adjusted based on the current head of the
1961 * queue (and not just the head at the beginning). In particular, if
1962 * the queue is initially empty an infinite timeout is used and this
1963 * needs to be reduced when a skb is queued.
1964 *
1965 * This cannot be done with wait_event_timeout() because it only
1966 * calculates the timeout once.
1967 */
1968static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
1969{
1970 DEFINE_WAIT(wait);
1971
1972 if (xenvif_have_rx_work(queue))
1973 return;
1974
1975 for (;;) {
1976 long ret;
1977
1978 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
1979 if (xenvif_have_rx_work(queue))
1980 break;
1981 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
1982 if (!ret)
1983 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001984 }
David Vrabelf48da8b2014-10-22 14:08:54 +01001985 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001986}
1987
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001988int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001989{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001990 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01001991 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01001992
David Vrabel26c0e102014-12-18 11:13:06 +00001993 if (!vif->stall_timeout)
1994 xenvif_queue_carrier_on(queue);
1995
David Vrabelf48da8b2014-10-22 14:08:54 +01001996 for (;;) {
1997 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001998
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001999 if (kthread_should_stop())
2000 break;
2001
Wei Liue9d8b2c2014-04-01 12:46:12 +01002002 /* This frontend is found to be rogue, disable it in
2003 * kthread context. Currently this is only set when
2004 * netback finds out frontend sends malformed packet,
2005 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01002006 * context so we defer it here, if this thread is
2007 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01002008 */
David Vrabelf48da8b2014-10-22 14:08:54 +01002009 if (unlikely(vif->disabled && queue->id == 0)) {
2010 xenvif_carrier_off(vif);
2011 xenvif_rx_queue_purge(queue);
2012 continue;
Zoltan Kiss09350782014-03-06 21:48:30 +00002013 }
2014
Wei Liue9ce7cb2014-06-04 10:30:42 +01002015 if (!skb_queue_empty(&queue->rx_queue))
2016 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01002017
David Vrabelecf08d22014-10-22 14:08:55 +01002018 /* If the guest hasn't provided any Rx slots for a
2019 * while it's probably not responsive, drop the
2020 * carrier so packets are dropped earlier.
2021 */
David Vrabel26c0e102014-12-18 11:13:06 +00002022 if (vif->stall_timeout) {
2023 if (xenvif_rx_queue_stalled(queue))
2024 xenvif_queue_carrier_off(queue);
2025 else if (xenvif_rx_queue_ready(queue))
2026 xenvif_queue_carrier_on(queue);
2027 }
David Vrabelecf08d22014-10-22 14:08:55 +01002028
David Vrabelf48da8b2014-10-22 14:08:54 +01002029 /* Queued packets may have foreign pages from other
2030 * domains. These cannot be queued indefinitely as
2031 * this would starve guests of grant refs and transmit
2032 * slots.
2033 */
2034 xenvif_rx_queue_drop_expired(queue);
2035
2036 xenvif_rx_queue_maybe_wake(queue);
2037
Wei Liub3f980b2013-08-26 12:59:38 +01002038 cond_resched();
2039 }
2040
Paul Durrantca2f09f2013-12-06 16:36:07 +00002041 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01002042 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00002043
Wei Liub3f980b2013-08-26 12:59:38 +01002044 return 0;
2045}
2046
Wei Liua64bd932014-08-12 11:48:07 +01002047static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
2048{
2049 /* Dealloc thread must remain running until all inflight
2050 * packets complete.
2051 */
2052 return kthread_should_stop() &&
2053 !atomic_read(&queue->inflight_packets);
2054}
2055
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002056int xenvif_dealloc_kthread(void *data)
2057{
Wei Liue9ce7cb2014-06-04 10:30:42 +01002058 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002059
Wei Liua64bd932014-08-12 11:48:07 +01002060 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01002061 wait_event_interruptible(queue->dealloc_wq,
2062 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01002063 xenvif_dealloc_kthread_should_stop(queue));
2064 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002065 break;
2066
Wei Liue9ce7cb2014-06-04 10:30:42 +01002067 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002068 cond_resched();
2069 }
2070
2071 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01002072 if (tx_dealloc_work_todo(queue))
2073 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00002074
2075 return 0;
2076}
2077
Ian Campbellf942dc22011-03-15 00:06:18 +00002078static int __init netback_init(void)
2079{
Ian Campbellf942dc22011-03-15 00:06:18 +00002080 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00002081
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05002082 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00002083 return -ENODEV;
2084
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01002085 /* Allow as many queues as there are CPUs, by default */
2086 xenvif_max_queues = num_online_cpus();
2087
Wei Liu37641492013-05-02 00:43:59 +00002088 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002089 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2090 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002091 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002092 }
2093
Ian Campbellf942dc22011-03-15 00:06:18 +00002094 rc = xenvif_xenbus_init();
2095 if (rc)
2096 goto failed_init;
2097
Zoltan Kissf51de242014-07-08 19:49:14 +01002098#ifdef CONFIG_DEBUG_FS
2099 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2100 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2101 pr_warn("Init of debugfs returned %ld!\n",
2102 PTR_ERR(xen_netback_dbg_root));
2103#endif /* CONFIG_DEBUG_FS */
2104
Ian Campbellf942dc22011-03-15 00:06:18 +00002105 return 0;
2106
2107failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002108 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002109}
2110
2111module_init(netback_init);
2112
Wei Liub103f352013-05-16 23:26:11 +00002113static void __exit netback_fini(void)
2114{
Zoltan Kissf51de242014-07-08 19:49:14 +01002115#ifdef CONFIG_DEBUG_FS
2116 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2117 debugfs_remove_recursive(xen_netback_dbg_root);
2118#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002119 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002120}
2121module_exit(netback_fini);
2122
Ian Campbellf942dc22011-03-15 00:06:18 +00002123MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002124MODULE_ALIAS("xen-backend:vif");