blob: 3f44b522b8311a2c64eba48e6a9b7217ea0cb3a7 [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>
Julien Gralla9fd60e2015-06-17 15:28:02 +010047#include <xen/page.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000048
49#include <asm/xen/hypercall.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000050
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 */
Shailendra Vermac489dbb2015-05-25 23:19:31 +053055bool separate_tx_rx_irq = true;
Wei Liue1f00a692013-05-22 06:34:45 +000056module_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);
David Vrabelc8a4d292015-03-11 15:27:59 +000099static void push_tx_responses(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +0100100
Wei Liue9ce7cb2014-06-04 10:30:42 +0100101static inline int tx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +0100102
Wei Liue9ce7cb2014-06-04 10:30:42 +0100103static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +0000104 u16 id,
105 s8 st,
106 u16 offset,
107 u16 size,
108 u16 flags);
109
Wei Liue9ce7cb2014-06-04 10:30:42 +0100110static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000111 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000112{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100113 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000114}
115
Wei Liue9ce7cb2014-06-04 10:30:42 +0100116static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000117 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000118{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100119 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000120}
121
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000122#define callback_param(vif, pending_idx) \
123 (vif->pending_tx_info[pending_idx].callback_struct)
124
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000125/* Find the containing VIF's structure from a pointer in pending_tx_info array
126 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100127static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000128{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000129 u16 pending_idx = ubuf->desc;
130 struct pending_tx_info *temp =
131 container_of(ubuf, struct pending_tx_info, callback_struct);
132 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100133 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000134 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000135}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000136
Ian Campbellea066ad2011-10-05 00:28:46 +0000137static u16 frag_get_pending_idx(skb_frag_t *frag)
138{
139 return (u16)frag->page_offset;
140}
141
142static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
143{
144 frag->page_offset = pending_idx;
145}
146
Ian Campbellf942dc22011-03-15 00:06:18 +0000147static inline pending_ring_idx_t pending_index(unsigned i)
148{
149 return i & (MAX_PENDING_REQS-1);
150}
151
Wei Liue9ce7cb2014-06-04 10:30:42 +0100152bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000153{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000154 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000155
Paul Durrantca2f09f2013-12-06 16:36:07 +0000156 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100157 prod = queue->rx.sring->req_prod;
158 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000159
Paul Durrantca2f09f2013-12-06 16:36:07 +0000160 if (prod - cons >= needed)
161 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000162
Wei Liue9ce7cb2014-06-04 10:30:42 +0100163 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000164
Paul Durrantca2f09f2013-12-06 16:36:07 +0000165 /* Make sure event is visible before we check prod
166 * again.
167 */
168 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100169 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000170
Paul Durrantca2f09f2013-12-06 16:36:07 +0000171 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000172}
173
David Vrabelf48da8b2014-10-22 14:08:54 +0100174void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
175{
176 unsigned long flags;
177
178 spin_lock_irqsave(&queue->rx_queue.lock, flags);
179
180 __skb_queue_tail(&queue->rx_queue, skb);
181
182 queue->rx_queue_len += skb->len;
183 if (queue->rx_queue_len > queue->rx_queue_max)
184 netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
185
186 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
187}
188
189static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
190{
191 struct sk_buff *skb;
192
193 spin_lock_irq(&queue->rx_queue.lock);
194
195 skb = __skb_dequeue(&queue->rx_queue);
196 if (skb)
197 queue->rx_queue_len -= skb->len;
198
199 spin_unlock_irq(&queue->rx_queue.lock);
200
201 return skb;
202}
203
204static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
205{
206 spin_lock_irq(&queue->rx_queue.lock);
207
208 if (queue->rx_queue_len < queue->rx_queue_max)
209 netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
210
211 spin_unlock_irq(&queue->rx_queue.lock);
212}
213
214
215static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
216{
217 struct sk_buff *skb;
218 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
219 kfree_skb(skb);
220}
221
222static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
223{
224 struct sk_buff *skb;
225
226 for(;;) {
227 skb = skb_peek(&queue->rx_queue);
228 if (!skb)
229 break;
230 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
231 break;
232 xenvif_rx_dequeue(queue);
233 kfree_skb(skb);
234 }
235}
236
Ian Campbellf942dc22011-03-15 00:06:18 +0000237struct netrx_pending_operations {
238 unsigned copy_prod, copy_cons;
239 unsigned meta_prod, meta_cons;
240 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100241 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000242 int copy_off;
243 grant_ref_t copy_gref;
244};
245
Wei Liue9ce7cb2014-06-04 10:30:42 +0100246static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100247 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000248{
Wei Liub3f980b2013-08-26 12:59:38 +0100249 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000250 struct xen_netif_rx_request *req;
251
Wei Liue9ce7cb2014-06-04 10:30:42 +0100252 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000253
254 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100255 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000256 meta->gso_size = 0;
257 meta->size = 0;
258 meta->id = req->id;
259
260 npo->copy_off = 0;
261 npo->copy_gref = req->gref;
262
263 return meta;
264}
265
Wei Liu33bc8012013-10-08 10:54:21 +0100266/*
267 * Set up the grant operations for this fragment. If it's a flipping
268 * interface, we also set up the unmap request from here.
269 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100270static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100271 struct netrx_pending_operations *npo,
272 struct page *page, unsigned long size,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000273 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000274{
275 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100276 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000277 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800278 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000279
280 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000281 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000282
283 meta = npo->meta + npo->meta_prod - 1;
284
Ian Campbell6a8ed462012-10-10 03:48:42 +0000285 /* Skip unused frames from start of page */
286 page += offset >> PAGE_SHIFT;
287 offset &= ~PAGE_MASK;
288
Ian Campbellf942dc22011-03-15 00:06:18 +0000289 while (size > 0) {
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000290 struct xen_page_foreign *foreign;
291
Ian Campbell6a8ed462012-10-10 03:48:42 +0000292 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000293 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
294
David Vrabel1650d542015-01-20 14:49:52 +0000295 if (npo->copy_off == MAX_BUFFER_OFFSET)
296 meta = get_next_rx_buffer(queue, npo);
Ian Campbell6a8ed462012-10-10 03:48:42 +0000297
David Vrabel1650d542015-01-20 14:49:52 +0000298 bytes = PAGE_SIZE - offset;
Ian Campbell6a8ed462012-10-10 03:48:42 +0000299 if (bytes > size)
300 bytes = size;
301
Ian Campbellf942dc22011-03-15 00:06:18 +0000302 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
303 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
304
305 copy_gop = npo->copy + npo->copy_prod++;
306 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100307 copy_gop->len = bytes;
308
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000309 foreign = xen_page_foreign(page);
310 if (foreign) {
311 copy_gop->source.domid = foreign->domid;
312 copy_gop->source.u.ref = foreign->gref;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000313 copy_gop->flags |= GNTCOPY_source_gref;
314 } else {
315 copy_gop->source.domid = DOMID_SELF;
316 copy_gop->source.u.gmfn =
317 virt_to_mfn(page_address(page));
318 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000319 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000320
Wei Liue9ce7cb2014-06-04 10:30:42 +0100321 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000322 copy_gop->dest.offset = npo->copy_off;
323 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000324
325 npo->copy_off += bytes;
326 meta->size += bytes;
327
328 offset += bytes;
329 size -= bytes;
330
Ian Campbell6a8ed462012-10-10 03:48:42 +0000331 /* Next frame */
332 if (offset == PAGE_SIZE && size) {
333 BUG_ON(!PageCompound(page));
334 page++;
335 offset = 0;
336 }
337
Ian Campbellf942dc22011-03-15 00:06:18 +0000338 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800339 if (skb_is_gso(skb)) {
340 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
341 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
342 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
343 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
344 }
Paul Durrant82cada22013-10-16 17:50:32 +0100345
Wei Liue9ce7cb2014-06-04 10:30:42 +0100346 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
347 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000348
Wei Liu33bc8012013-10-08 10:54:21 +0100349 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000350
351 }
352}
353
354/*
355 * Prepare an SKB to be transmitted to the frontend.
356 *
357 * This function is responsible for allocating grant operations, meta
358 * structures, etc.
359 *
360 * It returns the number of meta structures consumed. The number of
361 * ring slots used is always equal to the number of meta slots used
362 * plus the number of GSO descriptors used. Currently, we use either
363 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
364 * frontend-side LRO).
365 */
Wei Liu73764192013-08-26 12:59:39 +0100366static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100367 struct netrx_pending_operations *npo,
368 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000369{
370 struct xenvif *vif = netdev_priv(skb->dev);
371 int nr_frags = skb_shinfo(skb)->nr_frags;
372 int i;
373 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100374 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000375 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100376 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000377 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100378 int gso_type;
Ian Campbellf942dc22011-03-15 00:06:18 +0000379
380 old_meta_prod = npo->meta_prod;
381
Annie Li5bd07672014-03-10 22:58:34 +0800382 gso_type = XEN_NETIF_GSO_TYPE_NONE;
383 if (skb_is_gso(skb)) {
384 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
385 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
386 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
387 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100388 }
389
Ian Campbellf942dc22011-03-15 00:06:18 +0000390 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000391 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100392 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000393 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100394 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800395 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000396 meta->size = 0;
397 meta->id = req->id;
398 }
399
Wei Liue9ce7cb2014-06-04 10:30:42 +0100400 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000401 meta = npo->meta + npo->meta_prod++;
402
Paul Durrant82cada22013-10-16 17:50:32 +0100403 if ((1 << gso_type) & vif->gso_mask) {
404 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800405 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100406 } else {
407 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000408 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100409 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000410
411 meta->size = 0;
412 meta->id = req->id;
413 npo->copy_off = 0;
414 npo->copy_gref = req->gref;
415
416 data = skb->data;
417 while (data < skb_tail_pointer(skb)) {
418 unsigned int offset = offset_in_page(data);
419 unsigned int len = PAGE_SIZE - offset;
420
421 if (data + len > skb_tail_pointer(skb))
422 len = skb_tail_pointer(skb) - data;
423
Wei Liue9ce7cb2014-06-04 10:30:42 +0100424 xenvif_gop_frag_copy(queue, skb, npo,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000425 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000426 data += len;
427 }
428
429 for (i = 0; i < nr_frags; i++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100430 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100431 skb_frag_page(&skb_shinfo(skb)->frags[i]),
432 skb_frag_size(&skb_shinfo(skb)->frags[i]),
433 skb_shinfo(skb)->frags[i].page_offset,
Jennifer Herbertc2677a62015-01-05 14:45:10 +0000434 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000435 }
436
437 return npo->meta_prod - old_meta_prod;
438}
439
440/*
Wei Liu73764192013-08-26 12:59:39 +0100441 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000442 * used to set up the operations on the top of
443 * netrx_pending_operations, which have since been done. Check that
444 * they didn't give any errors and advance over them.
445 */
Wei Liu73764192013-08-26 12:59:39 +0100446static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
447 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000448{
449 struct gnttab_copy *copy_op;
450 int status = XEN_NETIF_RSP_OKAY;
451 int i;
452
453 for (i = 0; i < nr_meta_slots; i++) {
454 copy_op = npo->copy + npo->copy_cons++;
455 if (copy_op->status != GNTST_okay) {
456 netdev_dbg(vif->dev,
457 "Bad status %d from copy to DOM%d.\n",
458 copy_op->status, vif->domid);
459 status = XEN_NETIF_RSP_ERROR;
460 }
461 }
462
463 return status;
464}
465
Wei Liue9ce7cb2014-06-04 10:30:42 +0100466static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100467 struct xenvif_rx_meta *meta,
468 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000469{
470 int i;
471 unsigned long offset;
472
473 /* No fragments used */
474 if (nr_meta_slots <= 1)
475 return;
476
477 nr_meta_slots--;
478
479 for (i = 0; i < nr_meta_slots; i++) {
480 int flags;
481 if (i == nr_meta_slots - 1)
482 flags = 0;
483 else
484 flags = XEN_NETRXF_more_data;
485
486 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100487 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000488 meta[i].size, flags);
489 }
490}
491
Wei Liue9ce7cb2014-06-04 10:30:42 +0100492void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000493{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100494 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100495}
496
Wei Liue9ce7cb2014-06-04 10:30:42 +0100497static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100498{
Ian Campbellf942dc22011-03-15 00:06:18 +0000499 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000500 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000501 struct xen_netif_rx_response *resp;
502 struct sk_buff_head rxq;
503 struct sk_buff *skb;
504 LIST_HEAD(notify);
505 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000506 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000507 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000508
509 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100510 .copy = queue->grant_copy_op,
511 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000512 };
513
514 skb_queue_head_init(&rxq);
515
David Vrabelf48da8b2014-10-22 14:08:54 +0100516 while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
517 && (skb = xenvif_rx_dequeue(queue)) != NULL) {
David Vrabelecf08d22014-10-22 14:08:55 +0100518 queue->last_rx_time = jiffies;
519
Wei Liue9ce7cb2014-06-04 10:30:42 +0100520 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
Paul Durrant1425c7a2014-03-28 11:39:07 +0000521
Ian Campbellf942dc22011-03-15 00:06:18 +0000522 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000523 }
524
Wei Liue9ce7cb2014-06-04 10:30:42 +0100525 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000526
527 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000528 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000529
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000530 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100531 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000532
533 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000534
Wei Liue9ce7cb2014-06-04 10:30:42 +0100535 if ((1 << queue->meta[npo.meta_cons].gso_type) &
536 queue->vif->gso_prefix_mask) {
537 resp = RING_GET_RESPONSE(&queue->rx,
538 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000539
540 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
541
Wei Liue9ce7cb2014-06-04 10:30:42 +0100542 resp->offset = queue->meta[npo.meta_cons].gso_size;
543 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000544 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000545
546 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000547 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000548 }
549
550
Wei Liue9ce7cb2014-06-04 10:30:42 +0100551 queue->stats.tx_bytes += skb->len;
552 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000553
Wei Liue9ce7cb2014-06-04 10:30:42 +0100554 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000555 XENVIF_RX_CB(skb)->meta_slots_used,
556 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000557
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000558 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000559 flags = 0;
560 else
561 flags = XEN_NETRXF_more_data;
562
563 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
564 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
565 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
566 /* remote but checksummed. */
567 flags |= XEN_NETRXF_data_validated;
568
569 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100570 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100572 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000573 flags);
574
Wei Liue9ce7cb2014-06-04 10:30:42 +0100575 if ((1 << queue->meta[npo.meta_cons].gso_type) &
576 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000577 struct xen_netif_extra_info *gso =
578 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100579 RING_GET_RESPONSE(&queue->rx,
580 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000581
582 resp->flags |= XEN_NETRXF_extra_info;
583
Wei Liue9ce7cb2014-06-04 10:30:42 +0100584 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
585 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000586 gso->u.gso.pad = 0;
587 gso->u.gso.features = 0;
588
589 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
590 gso->flags = 0;
591 }
592
Wei Liue9ce7cb2014-06-04 10:30:42 +0100593 xenvif_add_frag_responses(queue, status,
594 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000595 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000596
Wei Liue9ce7cb2014-06-04 10:30:42 +0100597 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000598
Paul Durrant11b57f92014-01-08 12:41:58 +0000599 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100600
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000601 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000602 dev_kfree_skb(skb);
603 }
604
Paul Durrantca2f09f2013-12-06 16:36:07 +0000605done:
Wei Liub3f980b2013-08-26 12:59:38 +0100606 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100607 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000608}
609
Wei Liue9ce7cb2014-06-04 10:30:42 +0100610void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000611{
612 int more_to_do;
613
Wei Liue9ce7cb2014-06-04 10:30:42 +0100614 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000615
616 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100617 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000618}
619
Wei Liue9ce7cb2014-06-04 10:30:42 +0100620static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000621{
622 unsigned long max_burst, max_credit;
623
624 /*
625 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
626 * Otherwise the interface can seize up due to insufficient credit.
627 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100628 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000629 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100630 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000631
632 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100633 max_credit = queue->remaining_credit + queue->credit_bytes;
634 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000635 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
636
Wei Liue9ce7cb2014-06-04 10:30:42 +0100637 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000638}
639
Palik, Imreedafc132015-03-19 11:05:42 +0100640void xenvif_tx_credit_callback(unsigned long data)
Ian Campbellf942dc22011-03-15 00:06:18 +0000641{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100642 struct xenvif_queue *queue = (struct xenvif_queue *)data;
643 tx_add_credit(queue);
644 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000645}
646
Wei Liue9ce7cb2014-06-04 10:30:42 +0100647static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100648 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000649{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100650 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000651 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000652
653 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100654 spin_lock_irqsave(&queue->response_lock, flags);
655 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
David Vrabelc8a4d292015-03-11 15:27:59 +0000656 push_tx_responses(queue);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100657 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000658 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000659 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100660 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000661 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100662 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000663}
664
Wei Liu73764192013-08-26 12:59:39 +0100665static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000666{
667 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100668 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100669 /* Disable the vif from queue 0's kthread */
670 if (vif->queues)
671 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000672}
673
Wei Liue9ce7cb2014-06-04 10:30:42 +0100674static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100675 struct xen_netif_tx_request *first,
676 struct xen_netif_tx_request *txp,
677 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000678{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100679 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000680 int slots = 0;
681 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000682 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000683
684 if (!(first->flags & XEN_NETTXF_more_data))
685 return 0;
686
687 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000688 struct xen_netif_tx_request dropped_tx = { 0 };
689
Wei Liu2810e5b2013-04-22 02:20:42 +0000690 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100691 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000692 "Asked for %d slots but exceeds this limit\n",
693 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100694 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000695 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000696 }
697
Wei Liu2810e5b2013-04-22 02:20:42 +0000698 /* This guest is really using too many slots and
699 * considered malicious.
700 */
Wei Liu37641492013-05-02 00:43:59 +0000701 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100702 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000703 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000704 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100705 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000706 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000707 }
708
Wei Liu2810e5b2013-04-22 02:20:42 +0000709 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000710 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
711 * the historical MAX_SKB_FRAGS value 18 to honor the
712 * same behavior as before. Any packet using more than
713 * 18 slots but less than fatal_skb_slots slots is
714 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000715 */
Wei Liu37641492013-05-02 00:43:59 +0000716 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000717 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100718 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000719 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000720 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000721 drop_err = -E2BIG;
722 }
723
Wei Liu59ccb4e2013-05-02 00:43:58 +0000724 if (drop_err)
725 txp = &dropped_tx;
726
Wei Liue9ce7cb2014-06-04 10:30:42 +0100727 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000728 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000729
730 /* If the guest submitted a frame >= 64 KiB then
731 * first->size overflowed and following slots will
732 * appear to be larger than the frame.
733 *
734 * This cannot be fatal error as there are buggy
735 * frontends that do this.
736 *
737 * Consume all slots and drop the packet.
738 */
739 if (!drop_err && txp->size > first->size) {
740 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100741 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000742 "Invalid tx request, slot size %u > remaining size %u\n",
743 txp->size, first->size);
744 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000745 }
746
747 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000748 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000749
750 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Julien Grall68946152015-06-16 20:10:48 +0100751 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000752 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100753 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000754 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000755 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000756
757 more_data = txp->flags & XEN_NETTXF_more_data;
758
759 if (!drop_err)
760 txp++;
761
762 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000763
764 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100765 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000766 return drop_err;
767 }
768
769 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000770}
771
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000772
773struct xenvif_tx_cb {
774 u16 pending_idx;
775};
776
777#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
778
Wei Liue9ce7cb2014-06-04 10:30:42 +0100779static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100780 u16 pending_idx,
781 struct xen_netif_tx_request *txp,
782 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000783{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100784 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
785 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000786 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100787 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000788
Wei Liue9ce7cb2014-06-04 10:30:42 +0100789 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000790 sizeof(*txp));
791}
792
Zoltan Kisse3377f32014-03-06 21:48:29 +0000793static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
794{
795 struct sk_buff *skb =
796 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
797 GFP_ATOMIC | __GFP_NOWARN);
798 if (unlikely(skb == NULL))
799 return NULL;
800
801 /* Packets passed to netif_rx() must have some headroom. */
802 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
803
804 /* Initialize it here to avoid later surprises */
805 skb_shinfo(skb)->destructor_arg = NULL;
806
807 return skb;
808}
809
Wei Liue9ce7cb2014-06-04 10:30:42 +0100810static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000811 struct sk_buff *skb,
812 struct xen_netif_tx_request *txp,
Ross Lagerwall2475b222015-08-03 15:38:03 +0100813 struct gnttab_map_grant_ref *gop,
814 unsigned int frag_overflow,
815 struct sk_buff *nskb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000816{
817 struct skb_shared_info *shinfo = skb_shinfo(skb);
818 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000819 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000820 int start;
821 pending_ring_idx_t index;
Ross Lagerwall2475b222015-08-03 15:38:03 +0100822 unsigned int nr_slots;
Wei Liu2810e5b2013-04-22 02:20:42 +0000823
Wei Liu2810e5b2013-04-22 02:20:42 +0000824 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000825
826 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000827 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000828
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000829 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
830 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100831 index = pending_index(queue->pending_cons++);
832 pending_idx = queue->pending_ring[index];
833 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000834 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000835 }
836
Zoltan Kisse3377f32014-03-06 21:48:29 +0000837 if (frag_overflow) {
Zoltan Kisse3377f32014-03-06 21:48:29 +0000838
839 shinfo = skb_shinfo(nskb);
840 frags = shinfo->frags;
841
842 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
843 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100844 index = pending_index(queue->pending_cons++);
845 pending_idx = queue->pending_ring[index];
846 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +0000847 frag_set_pending_idx(&frags[shinfo->nr_frags],
848 pending_idx);
849 }
850
851 skb_shinfo(skb)->frag_list = nskb;
852 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000853
Ian Campbellf942dc22011-03-15 00:06:18 +0000854 return gop;
855}
856
Wei Liue9ce7cb2014-06-04 10:30:42 +0100857static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000858 u16 pending_idx,
859 grant_handle_t handle)
860{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100861 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000862 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100863 netdev_err(queue->vif->dev,
Julien Grall68946152015-06-16 20:10:48 +0100864 "Trying to overwrite active handle! pending_idx: 0x%x\n",
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000865 pending_idx);
866 BUG();
867 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100868 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000869}
870
Wei Liue9ce7cb2014-06-04 10:30:42 +0100871static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000872 u16 pending_idx)
873{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100874 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000875 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100876 netdev_err(queue->vif->dev,
Julien Grall68946152015-06-16 20:10:48 +0100877 "Trying to unmap invalid handle! pending_idx: 0x%x\n",
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000878 pending_idx);
879 BUG();
880 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100881 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000882}
883
Wei Liue9ce7cb2014-06-04 10:30:42 +0100884static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100885 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +0100886 struct gnttab_map_grant_ref **gopp_map,
887 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +0000888{
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100889 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000890 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100891 /* This always points to the shinfo of the skb being checked, which
892 * could be either the first or the one on the frag_list
893 */
Ian Campbellf942dc22011-03-15 00:06:18 +0000894 struct skb_shared_info *shinfo = skb_shinfo(skb);
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100895 /* If this is non-NULL, we are currently checking the frag_list skb, and
896 * this points to the shinfo of the first one
897 */
898 struct skb_shared_info *first_shinfo = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000899 int nr_frags = shinfo->nr_frags;
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100900 const bool sharedslot = nr_frags &&
901 frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
Zoltan Kissbdab8272014-04-02 18:04:58 +0100902 int i, err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000903
904 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100905 err = (*gopp_copy)->status;
Zoltan Kissbdab8272014-04-02 18:04:58 +0100906 if (unlikely(err)) {
907 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100908 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +0100909 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +0100910 (*gopp_copy)->status,
911 pending_idx,
912 (*gopp_copy)->source.u.ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100913 /* The first frag might still have this slot mapped */
914 if (!sharedslot)
915 xenvif_idx_release(queue, pending_idx,
916 XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +0100917 }
Zoltan Kissd8cfbfc2014-07-18 19:08:05 +0100918 (*gopp_copy)++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000919
Zoltan Kisse3377f32014-03-06 21:48:29 +0000920check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +0100921 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000922 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +0000923
Ian Campbellea066ad2011-10-05 00:28:46 +0000924 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000925
926 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100927 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +0000928
Ian Campbellf942dc22011-03-15 00:06:18 +0000929 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100930 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100931 pending_idx,
932 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +0000933 /* Had a previous error? Invalidate this fragment. */
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100934 if (unlikely(err)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100935 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100936 /* If the mapping of the first frag was OK, but
937 * the header's copy failed, and they are
938 * sharing a slot, send an error
939 */
940 if (i == 0 && sharedslot)
941 xenvif_idx_release(queue, pending_idx,
942 XEN_NETIF_RSP_ERROR);
943 else
944 xenvif_idx_release(queue, pending_idx,
945 XEN_NETIF_RSP_OKAY);
946 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000947 continue;
948 }
949
950 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100951 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100952 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +0100953 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +0100954 i,
955 gop_map->status,
956 pending_idx,
957 gop_map->ref);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100958
Wei Liue9ce7cb2014-06-04 10:30:42 +0100959 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000960
961 /* Not the first error? Preceding frags already invalidated. */
962 if (err)
963 continue;
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100964
965 /* First error: if the header haven't shared a slot with the
966 * first frag, release it as well.
967 */
968 if (!sharedslot)
969 xenvif_idx_release(queue,
970 XENVIF_TX_CB(skb)->pending_idx,
971 XEN_NETIF_RSP_OKAY);
972
973 /* Invalidate preceding fragments of this skb. */
Zoltan Kissbdab8272014-04-02 18:04:58 +0100974 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +0000975 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100976 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100977 xenvif_idx_release(queue, pending_idx,
978 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +0000979 }
980
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100981 /* And if we found the error while checking the frag_list, unmap
982 * the first skb's frags
983 */
984 if (first_shinfo) {
985 for (j = 0; j < first_shinfo->nr_frags; j++) {
986 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
987 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kiss1b860da2014-07-18 19:08:04 +0100988 xenvif_idx_release(queue, pending_idx,
989 XEN_NETIF_RSP_OKAY);
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100990 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000991 }
992
993 /* Remember the error: invalidate all subsequent fragments. */
994 err = newerr;
995 }
996
Zoltan Kiss1a998d32014-07-18 19:08:02 +0100997 if (skb_has_frag_list(skb) && !first_shinfo) {
998 first_shinfo = skb_shinfo(skb);
999 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001000 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001001
1002 goto check_frags;
1003 }
1004
Zoltan Kissbdab8272014-04-02 18:04:58 +01001005 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001006 return err;
1007}
1008
Wei Liue9ce7cb2014-06-04 10:30:42 +01001009static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001010{
1011 struct skb_shared_info *shinfo = skb_shinfo(skb);
1012 int nr_frags = shinfo->nr_frags;
1013 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001014 u16 prev_pending_idx = INVALID_PENDING_IDX;
1015
Ian Campbellf942dc22011-03-15 00:06:18 +00001016 for (i = 0; i < nr_frags; i++) {
1017 skb_frag_t *frag = shinfo->frags + i;
1018 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001019 struct page *page;
1020 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001021
Ian Campbellea066ad2011-10-05 00:28:46 +00001022 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001023
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001024 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001025 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001026 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001027 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001028 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001029 callback_param(queue, prev_pending_idx).ctx =
1030 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001031
Wei Liue9ce7cb2014-06-04 10:30:42 +01001032 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001033 prev_pending_idx = pending_idx;
1034
Wei Liue9ce7cb2014-06-04 10:30:42 +01001035 txp = &queue->pending_tx_info[pending_idx].req;
1036 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001037 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001038 skb->len += txp->size;
1039 skb->data_len += txp->size;
1040 skb->truesize += txp->size;
1041
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001042 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001043 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001044 }
1045}
1046
Wei Liue9ce7cb2014-06-04 10:30:42 +01001047static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001048 struct xen_netif_extra_info *extras,
1049 int work_to_do)
1050{
1051 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001052 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001053
1054 do {
1055 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001056 netdev_err(queue->vif->dev, "Missing extra info\n");
1057 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001058 return -EBADR;
1059 }
1060
Wei Liue9ce7cb2014-06-04 10:30:42 +01001061 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001062 sizeof(extra));
1063 if (unlikely(!extra.type ||
1064 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001065 queue->tx.req_cons = ++cons;
1066 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001067 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001068 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001069 return -EINVAL;
1070 }
1071
1072 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001073 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001074 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1075
1076 return work_to_do;
1077}
1078
Wei Liu73764192013-08-26 12:59:39 +01001079static int xenvif_set_skb_gso(struct xenvif *vif,
1080 struct sk_buff *skb,
1081 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001082{
1083 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001084 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001085 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001086 return -EINVAL;
1087 }
1088
Paul Durranta9468582013-10-16 17:50:31 +01001089 switch (gso->u.gso.type) {
1090 case XEN_NETIF_GSO_TYPE_TCPV4:
1091 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1092 break;
1093 case XEN_NETIF_GSO_TYPE_TCPV6:
1094 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1095 break;
1096 default:
Ian Campbell488562862013-02-06 23:41:35 +00001097 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001098 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001099 return -EINVAL;
1100 }
1101
1102 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001103 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001104
1105 return 0;
1106}
1107
Wei Liue9ce7cb2014-06-04 10:30:42 +01001108static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001109{
Paul Durrant27216372014-01-09 10:02:47 +00001110 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001111
Paul Durrant2eba61d2013-10-16 17:50:29 +01001112 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001113 * peers can fail to set NETRXF_csum_blank when sending a GSO
1114 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1115 * recalculate the partial checksum.
1116 */
1117 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001118 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001119 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001120 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001121 }
1122
1123 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1124 if (skb->ip_summed != CHECKSUM_PARTIAL)
1125 return 0;
1126
Paul Durrant27216372014-01-09 10:02:47 +00001127 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001128}
1129
Wei Liue9ce7cb2014-06-04 10:30:42 +01001130static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001131{
Wei Liu059dfa62013-10-28 12:07:57 +00001132 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001133 u64 next_credit = queue->credit_window_start +
1134 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001135
1136 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001137 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001138 return true;
1139
1140 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001141 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001142 queue->credit_window_start = now;
1143 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001144 }
1145
1146 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001147 if (size > queue->remaining_credit) {
1148 queue->credit_timeout.data =
1149 (unsigned long)queue;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001150 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001151 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001152 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001153
1154 return true;
1155 }
1156
1157 return false;
1158}
1159
Wei Liue9ce7cb2014-06-04 10:30:42 +01001160static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001161 int budget,
1162 unsigned *copy_ops,
1163 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001164{
Ross Lagerwall2475b222015-08-03 15:38:03 +01001165 struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
1166 struct sk_buff *skb, *nskb;
Ian Campbellf942dc22011-03-15 00:06:18 +00001167 int ret;
Ross Lagerwall2475b222015-08-03 15:38:03 +01001168 unsigned int frag_overflow;
Ian Campbellf942dc22011-03-15 00:06:18 +00001169
Wei Liue9ce7cb2014-06-04 10:30:42 +01001170 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001171 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001172 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001173 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1174 u16 pending_idx;
1175 RING_IDX idx;
1176 int work_to_do;
1177 unsigned int data_len;
1178 pending_ring_idx_t index;
1179
Wei Liue9ce7cb2014-06-04 10:30:42 +01001180 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001181 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001182 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001183 "Impossible number of requests. "
1184 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001185 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001186 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001187 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001188 break;
Ian Campbell488562862013-02-06 23:41:35 +00001189 }
1190
Wei Liue9ce7cb2014-06-04 10:30:42 +01001191 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001192 if (!work_to_do)
1193 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001194
Wei Liue9ce7cb2014-06-04 10:30:42 +01001195 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001196 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001197 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001198
1199 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001200 if (txreq.size > queue->remaining_credit &&
1201 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001202 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001203
Wei Liue9ce7cb2014-06-04 10:30:42 +01001204 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001205
1206 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001207 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001208
1209 memset(extras, 0, sizeof(extras));
1210 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001211 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001212 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001213 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001214 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001215 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001216 }
1217
Wei Liue9ce7cb2014-06-04 10:30:42 +01001218 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001219 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001220 break;
Ian Campbell488562862013-02-06 23:41:35 +00001221
Ian Campbellf942dc22011-03-15 00:06:18 +00001222 idx += ret;
1223
1224 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001225 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001226 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001227 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001228 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001229 }
1230
1231 /* No crossing a page as the payload mustn't fragment. */
1232 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001233 netdev_err(queue->vif->dev,
Julien Grall68946152015-06-16 20:10:48 +01001234 "txreq.offset: %u, size: %u, end: %lu\n",
Ian Campbellf942dc22011-03-15 00:06:18 +00001235 txreq.offset, txreq.size,
Ian Campbelldc5e7a82015-06-01 11:30:04 +01001236 (unsigned long)(txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001237 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001238 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001239 }
1240
Wei Liue9ce7cb2014-06-04 10:30:42 +01001241 index = pending_index(queue->pending_cons);
1242 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001243
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001244 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001245 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Malcolm Crossley7e5d7752014-11-05 10:50:22 +00001246 XEN_NETBACK_TX_COPY_LEN : txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001247
Zoltan Kisse3377f32014-03-06 21:48:29 +00001248 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001249 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001250 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001251 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001252 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001253 break;
1254 }
1255
Ross Lagerwall2475b222015-08-03 15:38:03 +01001256 skb_shinfo(skb)->nr_frags = ret;
1257 if (data_len < txreq.size)
1258 skb_shinfo(skb)->nr_frags++;
1259 /* At this point shinfo->nr_frags is in fact the number of
1260 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1261 */
1262 frag_overflow = 0;
1263 nskb = NULL;
1264 if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
1265 frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
1266 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1267 skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
1268 nskb = xenvif_alloc_skb(0);
1269 if (unlikely(nskb == NULL)) {
1270 kfree_skb(skb);
1271 xenvif_tx_err(queue, &txreq, idx);
1272 if (net_ratelimit())
1273 netdev_err(queue->vif->dev,
1274 "Can't allocate the frag_list skb.\n");
1275 break;
1276 }
1277 }
1278
Ian Campbellf942dc22011-03-15 00:06:18 +00001279 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1280 struct xen_netif_extra_info *gso;
1281 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1282
Wei Liue9ce7cb2014-06-04 10:30:42 +01001283 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001284 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001285 kfree_skb(skb);
Ross Lagerwall2475b222015-08-03 15:38:03 +01001286 kfree_skb(nskb);
Wei Liub3f980b2013-08-26 12:59:38 +01001287 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001288 }
1289 }
1290
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001291 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001292
1293 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001294 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1295 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1296 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001297
Wei Liue9ce7cb2014-06-04 10:30:42 +01001298 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001299 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001300 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1301 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001302 offset_in_page(skb->data);
1303
Wei Liue9ce7cb2014-06-04 10:30:42 +01001304 queue->tx_copy_ops[*copy_ops].len = data_len;
1305 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001306
1307 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001308
Ian Campbellf942dc22011-03-15 00:06:18 +00001309 if (data_len < txreq.size) {
Ian Campbellea066ad2011-10-05 00:28:46 +00001310 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1311 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001312 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001313 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001314 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001315 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1316 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001317 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001318 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001319 }
1320
Wei Liue9ce7cb2014-06-04 10:30:42 +01001321 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001322
Ross Lagerwall2475b222015-08-03 15:38:03 +01001323 gop = xenvif_get_requests(queue, skb, txfrags, gop,
1324 frag_overflow, nskb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001325
Wei Liue9ce7cb2014-06-04 10:30:42 +01001326 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001327
Wei Liue9ce7cb2014-06-04 10:30:42 +01001328 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001329
Wei Liue9ce7cb2014-06-04 10:30:42 +01001330 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1331 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001332 break;
1333 }
1334
Wei Liue9ce7cb2014-06-04 10:30:42 +01001335 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001336 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001337}
1338
Zoltan Kisse3377f32014-03-06 21:48:29 +00001339/* Consolidate skb with a frag_list into a brand new one with local pages on
1340 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1341 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001342static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001343{
1344 unsigned int offset = skb_headlen(skb);
1345 skb_frag_t frags[MAX_SKB_FRAGS];
David Vrabel49d99912015-03-04 11:14:47 +00001346 int i, f;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001347 struct ubuf_info *uarg;
1348 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1349
Wei Liue9ce7cb2014-06-04 10:30:42 +01001350 queue->stats.tx_zerocopy_sent += 2;
1351 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001352
Wei Liue9ce7cb2014-06-04 10:30:42 +01001353 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001354 /* Subtract frags size, we will correct it later */
1355 skb->truesize -= skb->data_len;
1356 skb->len += nskb->len;
1357 skb->data_len += nskb->len;
1358
1359 /* create a brand new frags array and coalesce there */
1360 for (i = 0; offset < skb->len; i++) {
1361 struct page *page;
1362 unsigned int len;
1363
1364 BUG_ON(i >= MAX_SKB_FRAGS);
Zoltan Kiss44cc8ed2014-10-28 15:29:31 +00001365 page = alloc_page(GFP_ATOMIC);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001366 if (!page) {
1367 int j;
1368 skb->truesize += skb->data_len;
1369 for (j = 0; j < i; j++)
1370 put_page(frags[j].page.p);
1371 return -ENOMEM;
1372 }
1373
1374 if (offset + PAGE_SIZE < skb->len)
1375 len = PAGE_SIZE;
1376 else
1377 len = skb->len - offset;
1378 if (skb_copy_bits(skb, offset, page_address(page), len))
1379 BUG();
1380
1381 offset += len;
1382 frags[i].page.p = page;
1383 frags[i].page_offset = 0;
1384 skb_frag_size_set(&frags[i], len);
1385 }
David Vrabel49d99912015-03-04 11:14:47 +00001386
David Vrabelb0c21ba2015-03-04 11:14:48 +00001387 /* Copied all the bits from the frag list -- free it. */
1388 skb_frag_list_init(skb);
1389 xenvif_skb_zerocopy_prepare(queue, nskb);
1390 kfree_skb(nskb);
1391
David Vrabel49d99912015-03-04 11:14:47 +00001392 /* Release all the original (foreign) frags. */
1393 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1394 skb_frag_unref(skb, f);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001395 uarg = skb_shinfo(skb)->destructor_arg;
Wei Liua64bd932014-08-12 11:48:07 +01001396 /* increase inflight counter to offset decrement in callback */
1397 atomic_inc(&queue->inflight_packets);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001398 uarg->callback(uarg, true);
1399 skb_shinfo(skb)->destructor_arg = NULL;
1400
David Vrabelb0c21ba2015-03-04 11:14:48 +00001401 /* Fill the skb with the new (local) frags. */
1402 memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1403 skb_shinfo(skb)->nr_frags = i;
1404 skb->truesize += i * PAGE_SIZE;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001405
1406 return 0;
1407}
Ian Campbellf942dc22011-03-15 00:06:18 +00001408
Wei Liue9ce7cb2014-06-04 10:30:42 +01001409static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001410{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001411 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1412 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001413 struct sk_buff *skb;
1414 int work_done = 0;
1415
Wei Liue9ce7cb2014-06-04 10:30:42 +01001416 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001417 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001418 u16 pending_idx;
1419 unsigned data_len;
1420
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001421 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001422 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001423
1424 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001425 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001426 /* If there was an error, xenvif_tx_check_gop is
1427 * expected to release all the frags which were mapped,
1428 * so kfree_skb shouldn't do it again
1429 */
Ian Campbellf942dc22011-03-15 00:06:18 +00001430 skb_shinfo(skb)->nr_frags = 0;
Zoltan Kissb42cc6e2014-07-18 19:08:03 +01001431 if (skb_has_frag_list(skb)) {
1432 struct sk_buff *nskb =
1433 skb_shinfo(skb)->frag_list;
1434 skb_shinfo(nskb)->nr_frags = 0;
1435 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001436 kfree_skb(skb);
1437 continue;
1438 }
1439
1440 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001441 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001442 if (data_len < txp->size) {
1443 /* Append the packet payload as a fragment. */
1444 txp->offset += data_len;
1445 txp->size -= data_len;
1446 } else {
1447 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001448 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001449 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001450 }
1451
1452 if (txp->flags & XEN_NETTXF_csum_blank)
1453 skb->ip_summed = CHECKSUM_PARTIAL;
1454 else if (txp->flags & XEN_NETTXF_data_validated)
1455 skb->ip_summed = CHECKSUM_UNNECESSARY;
1456
Wei Liue9ce7cb2014-06-04 10:30:42 +01001457 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001458
Zoltan Kisse3377f32014-03-06 21:48:29 +00001459 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001460 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001461 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001462 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001463 "Not enough memory to consolidate frag_list!\n");
Wei Liua64bd932014-08-12 11:48:07 +01001464 xenvif_skb_zerocopy_prepare(queue, skb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001465 kfree_skb(skb);
1466 continue;
1467 }
1468 }
1469
Wei Liue9ce7cb2014-06-04 10:30:42 +01001470 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001471 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001472 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001473
Wei Liue9ce7cb2014-06-04 10:30:42 +01001474 if (checksum_setup(queue, skb)) {
1475 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001476 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001477 /* We have to set this flag to trigger the callback */
1478 if (skb_shinfo(skb)->destructor_arg)
Wei Liua64bd932014-08-12 11:48:07 +01001479 xenvif_skb_zerocopy_prepare(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001480 kfree_skb(skb);
1481 continue;
1482 }
1483
Jason Wang40893fd2013-03-26 23:11:22 +00001484 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001485
Paul Durrantb89587a2013-12-17 11:44:35 +00001486 /* If the packet is GSO then we will have just set up the
1487 * transport header offset in checksum_setup so it's now
1488 * straightforward to calculate gso_segs.
1489 */
1490 if (skb_is_gso(skb)) {
1491 int mss = skb_shinfo(skb)->gso_size;
1492 int hdrlen = skb_transport_header(skb) -
1493 skb_mac_header(skb) +
1494 tcp_hdrlen(skb);
1495
1496 skb_shinfo(skb)->gso_segs =
1497 DIV_ROUND_UP(skb->len - hdrlen, mss);
1498 }
1499
Wei Liue9ce7cb2014-06-04 10:30:42 +01001500 queue->stats.rx_bytes += skb->len;
1501 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001502
Wei Liub3f980b2013-08-26 12:59:38 +01001503 work_done++;
1504
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001505 /* Set this flag right before netif_receive_skb, otherwise
1506 * someone might think this packet already left netback, and
1507 * do a skb_copy_ubufs while we are still in control of the
1508 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1509 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001510 if (skb_shinfo(skb)->destructor_arg) {
Wei Liua64bd932014-08-12 11:48:07 +01001511 xenvif_skb_zerocopy_prepare(queue, skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001512 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001513 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001514
Wei Liub3f980b2013-08-26 12:59:38 +01001515 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001516 }
Wei Liub3f980b2013-08-26 12:59:38 +01001517
1518 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001519}
1520
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001521void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1522{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001523 unsigned long flags;
1524 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001525 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001526
1527 /* This is the only place where we grab this lock, to protect callbacks
1528 * from each other.
1529 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001530 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001531 do {
1532 u16 pending_idx = ubuf->desc;
1533 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001534 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001535 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001536 index = pending_index(queue->dealloc_prod);
1537 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001538 /* Sync with xenvif_tx_dealloc_action:
1539 * insert idx then incr producer.
1540 */
1541 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001542 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001543 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001544 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001545
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001546 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001547 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001548 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001549 queue->stats.tx_zerocopy_fail++;
Wei Liua64bd932014-08-12 11:48:07 +01001550 xenvif_skb_zerocopy_complete(queue);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001551}
1552
Wei Liue9ce7cb2014-06-04 10:30:42 +01001553static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001554{
1555 struct gnttab_unmap_grant_ref *gop;
1556 pending_ring_idx_t dc, dp;
1557 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1558 unsigned int i = 0;
1559
Wei Liue9ce7cb2014-06-04 10:30:42 +01001560 dc = queue->dealloc_cons;
1561 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001562
1563 /* Free up any grants we have finished using */
1564 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001565 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001566
1567 /* Ensure we see all indices enqueued by all
1568 * xenvif_zerocopy_callback().
1569 */
1570 smp_rmb();
1571
1572 while (dc != dp) {
Dan Carpenter50c2e4d2015-07-12 01:20:55 +03001573 BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001574 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001575 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001576
Dan Carpenter50c2e4d2015-07-12 01:20:55 +03001577 pending_idx_release[gop - queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001578 pending_idx;
Dan Carpenter50c2e4d2015-07-12 01:20:55 +03001579 queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001580 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001581 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001582 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001583 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001584 queue->grant_tx_handle[pending_idx]);
1585 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001586 ++gop;
1587 }
1588
Wei Liue9ce7cb2014-06-04 10:30:42 +01001589 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001590
Wei Liue9ce7cb2014-06-04 10:30:42 +01001591 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001592
Wei Liue9ce7cb2014-06-04 10:30:42 +01001593 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001594 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001595 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001596 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001597 queue->pages_to_unmap,
1598 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001599 if (ret) {
Julien Grall68946152015-06-16 20:10:48 +01001600 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001601 gop - queue->tx_unmap_ops, ret);
1602 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001603 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001604 netdev_err(queue->vif->dev,
Julien Grall68946152015-06-16 20:10:48 +01001605 " host_addr: 0x%llx handle: 0x%x status: %d\n",
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001606 gop[i].host_addr,
1607 gop[i].handle,
1608 gop[i].status);
1609 }
1610 BUG();
1611 }
1612 }
1613
Wei Liue9ce7cb2014-06-04 10:30:42 +01001614 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1615 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001616 XEN_NETIF_RSP_OKAY);
1617}
1618
1619
Ian Campbellf942dc22011-03-15 00:06:18 +00001620/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001621int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001622{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001623 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001624 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001625
Wei Liue9ce7cb2014-06-04 10:30:42 +01001626 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001627 return 0;
1628
Wei Liue9ce7cb2014-06-04 10:30:42 +01001629 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001630
Zoltan Kissbdab8272014-04-02 18:04:58 +01001631 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001632 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001633
Wei Liue9ce7cb2014-06-04 10:30:42 +01001634 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001635 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001636 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001637 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001638 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001639 nr_mops);
1640 BUG_ON(ret);
1641 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001642
Wei Liue9ce7cb2014-06-04 10:30:42 +01001643 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001644
1645 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001646}
1647
Wei Liue9ce7cb2014-06-04 10:30:42 +01001648static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001649 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001650{
Ian Campbellf942dc22011-03-15 00:06:18 +00001651 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001652 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001653 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001654
Wei Liue9ce7cb2014-06-04 10:30:42 +01001655 pending_tx_info = &queue->pending_tx_info[pending_idx];
David Vrabel7fbb9d82015-02-24 11:17:59 +00001656
Wei Liue9ce7cb2014-06-04 10:30:42 +01001657 spin_lock_irqsave(&queue->response_lock, flags);
David Vrabel7fbb9d82015-02-24 11:17:59 +00001658
Wei Liue9ce7cb2014-06-04 10:30:42 +01001659 make_tx_response(queue, &pending_tx_info->req, status);
David Vrabel7fbb9d82015-02-24 11:17:59 +00001660
1661 /* Release the pending index before pusing the Tx response so
1662 * its available before a new Tx request is pushed by the
1663 * frontend.
1664 */
1665 index = pending_index(queue->pending_prod++);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001666 queue->pending_ring[index] = pending_idx;
David Vrabel7fbb9d82015-02-24 11:17:59 +00001667
David Vrabelc8a4d292015-03-11 15:27:59 +00001668 push_tx_responses(queue);
David Vrabel7fbb9d82015-02-24 11:17:59 +00001669
Wei Liue9ce7cb2014-06-04 10:30:42 +01001670 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001671}
1672
Wei Liu2810e5b2013-04-22 02:20:42 +00001673
Wei Liue9ce7cb2014-06-04 10:30:42 +01001674static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001675 struct xen_netif_tx_request *txp,
1676 s8 st)
1677{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001678 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001679 struct xen_netif_tx_response *resp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001680
Wei Liue9ce7cb2014-06-04 10:30:42 +01001681 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001682 resp->id = txp->id;
1683 resp->status = st;
1684
1685 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001686 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001687
Wei Liue9ce7cb2014-06-04 10:30:42 +01001688 queue->tx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001689}
1690
David Vrabelc8a4d292015-03-11 15:27:59 +00001691static void push_tx_responses(struct xenvif_queue *queue)
1692{
1693 int notify;
1694
1695 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1696 if (notify)
1697 notify_remote_via_irq(queue->tx_irq);
1698}
1699
Wei Liue9ce7cb2014-06-04 10:30:42 +01001700static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001701 u16 id,
1702 s8 st,
1703 u16 offset,
1704 u16 size,
1705 u16 flags)
1706{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001707 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001708 struct xen_netif_rx_response *resp;
1709
Wei Liue9ce7cb2014-06-04 10:30:42 +01001710 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001711 resp->offset = offset;
1712 resp->flags = flags;
1713 resp->id = id;
1714 resp->status = (s16)size;
1715 if (st < 0)
1716 resp->status = (s16)st;
1717
Wei Liue9ce7cb2014-06-04 10:30:42 +01001718 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001719
1720 return resp;
1721}
1722
Wei Liue9ce7cb2014-06-04 10:30:42 +01001723void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001724{
1725 int ret;
1726 struct gnttab_unmap_grant_ref tx_unmap_op;
1727
1728 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001729 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001730 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001731 queue->grant_tx_handle[pending_idx]);
1732 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001733
1734 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001735 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001736 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001737 netdev_err(queue->vif->dev,
Julien Grall68946152015-06-16 20:10:48 +01001738 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001739 ret,
1740 pending_idx,
1741 tx_unmap_op.host_addr,
1742 tx_unmap_op.handle,
1743 tx_unmap_op.status);
1744 BUG();
1745 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001746}
1747
Wei Liue9ce7cb2014-06-04 10:30:42 +01001748static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001749{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001750 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001751 return 1;
1752
1753 return 0;
1754}
1755
Wei Liue9ce7cb2014-06-04 10:30:42 +01001756static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001757{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001758 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001759}
1760
Wei Liue9ce7cb2014-06-04 10:30:42 +01001761void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001762{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001763 if (queue->tx.sring)
1764 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1765 queue->tx.sring);
1766 if (queue->rx.sring)
1767 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1768 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001769}
1770
Wei Liue9ce7cb2014-06-04 10:30:42 +01001771int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001772 grant_ref_t tx_ring_ref,
1773 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001774{
David Vrabelc9d63692011-09-29 16:53:31 +01001775 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001776 struct xen_netif_tx_sring *txs;
1777 struct xen_netif_rx_sring *rxs;
1778
1779 int err = -ENOMEM;
1780
Wei Liue9ce7cb2014-06-04 10:30:42 +01001781 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
Wei Liuccc9d902015-04-03 14:44:59 +08001782 &tx_ring_ref, 1, &addr);
David Vrabelc9d63692011-09-29 16:53:31 +01001783 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001784 goto err;
1785
David Vrabelc9d63692011-09-29 16:53:31 +01001786 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001787 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001788
Wei Liue9ce7cb2014-06-04 10:30:42 +01001789 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
Wei Liuccc9d902015-04-03 14:44:59 +08001790 &rx_ring_ref, 1, &addr);
David Vrabelc9d63692011-09-29 16:53:31 +01001791 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001792 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001793
David Vrabelc9d63692011-09-29 16:53:31 +01001794 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001795 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001796
1797 return 0;
1798
1799err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001800 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001801 return err;
1802}
1803
David Vrabelecf08d22014-10-22 14:08:55 +01001804static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001805{
David Vrabelecf08d22014-10-22 14:08:55 +01001806 struct xenvif *vif = queue->vif;
1807
1808 queue->stalled = true;
1809
1810 /* At least one queue has stalled? Disable the carrier. */
1811 spin_lock(&vif->lock);
1812 if (vif->stalled_queues++ == 0) {
1813 netdev_info(vif->dev, "Guest Rx stalled");
1814 netif_carrier_off(vif->dev);
1815 }
1816 spin_unlock(&vif->lock);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001817}
1818
David Vrabelecf08d22014-10-22 14:08:55 +01001819static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001820{
David Vrabelecf08d22014-10-22 14:08:55 +01001821 struct xenvif *vif = queue->vif;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001822
David Vrabelecf08d22014-10-22 14:08:55 +01001823 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
1824 queue->stalled = false;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001825
David Vrabelecf08d22014-10-22 14:08:55 +01001826 /* All queues are ready? Enable the carrier. */
1827 spin_lock(&vif->lock);
1828 if (--vif->stalled_queues == 0) {
1829 netdev_info(vif->dev, "Guest Rx ready");
1830 netif_carrier_on(vif->dev);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001831 }
David Vrabelecf08d22014-10-22 14:08:55 +01001832 spin_unlock(&vif->lock);
1833}
1834
1835static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
1836{
1837 RING_IDX prod, cons;
1838
1839 prod = queue->rx.sring->req_prod;
1840 cons = queue->rx.req_cons;
1841
1842 return !queue->stalled
1843 && prod - cons < XEN_NETBK_RX_SLOTS_MAX
1844 && time_after(jiffies,
David Vrabel26c0e102014-12-18 11:13:06 +00001845 queue->last_rx_time + queue->vif->stall_timeout);
David Vrabelecf08d22014-10-22 14:08:55 +01001846}
1847
1848static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
1849{
1850 RING_IDX prod, cons;
1851
1852 prod = queue->rx.sring->req_prod;
1853 cons = queue->rx.req_cons;
1854
1855 return queue->stalled
1856 && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
1857}
1858
David Vrabelf48da8b2014-10-22 14:08:54 +01001859static bool xenvif_have_rx_work(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001860{
David Vrabelf48da8b2014-10-22 14:08:54 +01001861 return (!skb_queue_empty(&queue->rx_queue)
1862 && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
David Vrabel26c0e102014-12-18 11:13:06 +00001863 || (queue->vif->stall_timeout &&
1864 (xenvif_rx_queue_stalled(queue)
1865 || xenvif_rx_queue_ready(queue)))
David Vrabelf48da8b2014-10-22 14:08:54 +01001866 || kthread_should_stop()
1867 || queue->vif->disabled;
Ian Campbellf942dc22011-03-15 00:06:18 +00001868}
1869
David Vrabelf48da8b2014-10-22 14:08:54 +01001870static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001871{
David Vrabelf48da8b2014-10-22 14:08:54 +01001872 struct sk_buff *skb;
1873 long timeout;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001874
David Vrabelf48da8b2014-10-22 14:08:54 +01001875 skb = skb_peek(&queue->rx_queue);
1876 if (!skb)
1877 return MAX_SCHEDULE_TIMEOUT;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001878
David Vrabelf48da8b2014-10-22 14:08:54 +01001879 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
1880 return timeout < 0 ? 0 : timeout;
1881}
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001882
David Vrabelf48da8b2014-10-22 14:08:54 +01001883/* Wait until the guest Rx thread has work.
1884 *
1885 * The timeout needs to be adjusted based on the current head of the
1886 * queue (and not just the head at the beginning). In particular, if
1887 * the queue is initially empty an infinite timeout is used and this
1888 * needs to be reduced when a skb is queued.
1889 *
1890 * This cannot be done with wait_event_timeout() because it only
1891 * calculates the timeout once.
1892 */
1893static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
1894{
1895 DEFINE_WAIT(wait);
1896
1897 if (xenvif_have_rx_work(queue))
1898 return;
1899
1900 for (;;) {
1901 long ret;
1902
1903 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
1904 if (xenvif_have_rx_work(queue))
1905 break;
1906 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
1907 if (!ret)
1908 break;
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001909 }
David Vrabelf48da8b2014-10-22 14:08:54 +01001910 finish_wait(&queue->wq, &wait);
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001911}
1912
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001913int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001914{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001915 struct xenvif_queue *queue = data;
David Vrabelf48da8b2014-10-22 14:08:54 +01001916 struct xenvif *vif = queue->vif;
Wei Liub3f980b2013-08-26 12:59:38 +01001917
David Vrabel26c0e102014-12-18 11:13:06 +00001918 if (!vif->stall_timeout)
1919 xenvif_queue_carrier_on(queue);
1920
David Vrabelf48da8b2014-10-22 14:08:54 +01001921 for (;;) {
1922 xenvif_wait_for_rx_work(queue);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001923
Zoltan Kissf34a4cf2014-08-04 16:20:58 +01001924 if (kthread_should_stop())
1925 break;
1926
Wei Liue9d8b2c2014-04-01 12:46:12 +01001927 /* This frontend is found to be rogue, disable it in
1928 * kthread context. Currently this is only set when
1929 * netback finds out frontend sends malformed packet,
1930 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01001931 * context so we defer it here, if this thread is
1932 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01001933 */
David Vrabelf48da8b2014-10-22 14:08:54 +01001934 if (unlikely(vif->disabled && queue->id == 0)) {
1935 xenvif_carrier_off(vif);
David Vrabel42b52122015-02-02 16:57:51 +00001936 break;
Zoltan Kiss09350782014-03-06 21:48:30 +00001937 }
1938
Wei Liue9ce7cb2014-06-04 10:30:42 +01001939 if (!skb_queue_empty(&queue->rx_queue))
1940 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001941
David Vrabelecf08d22014-10-22 14:08:55 +01001942 /* If the guest hasn't provided any Rx slots for a
1943 * while it's probably not responsive, drop the
1944 * carrier so packets are dropped earlier.
1945 */
David Vrabel26c0e102014-12-18 11:13:06 +00001946 if (vif->stall_timeout) {
1947 if (xenvif_rx_queue_stalled(queue))
1948 xenvif_queue_carrier_off(queue);
1949 else if (xenvif_rx_queue_ready(queue))
1950 xenvif_queue_carrier_on(queue);
1951 }
David Vrabelecf08d22014-10-22 14:08:55 +01001952
David Vrabelf48da8b2014-10-22 14:08:54 +01001953 /* Queued packets may have foreign pages from other
1954 * domains. These cannot be queued indefinitely as
1955 * this would starve guests of grant refs and transmit
1956 * slots.
1957 */
1958 xenvif_rx_queue_drop_expired(queue);
1959
1960 xenvif_rx_queue_maybe_wake(queue);
1961
Wei Liub3f980b2013-08-26 12:59:38 +01001962 cond_resched();
1963 }
1964
Paul Durrantca2f09f2013-12-06 16:36:07 +00001965 /* Bin any remaining skbs */
David Vrabelf48da8b2014-10-22 14:08:54 +01001966 xenvif_rx_queue_purge(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001967
Wei Liub3f980b2013-08-26 12:59:38 +01001968 return 0;
1969}
1970
Wei Liua64bd932014-08-12 11:48:07 +01001971static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1972{
1973 /* Dealloc thread must remain running until all inflight
1974 * packets complete.
1975 */
1976 return kthread_should_stop() &&
1977 !atomic_read(&queue->inflight_packets);
1978}
1979
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001980int xenvif_dealloc_kthread(void *data)
1981{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001982 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001983
Wei Liua64bd932014-08-12 11:48:07 +01001984 for (;;) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001985 wait_event_interruptible(queue->dealloc_wq,
1986 tx_dealloc_work_todo(queue) ||
Wei Liua64bd932014-08-12 11:48:07 +01001987 xenvif_dealloc_kthread_should_stop(queue));
1988 if (xenvif_dealloc_kthread_should_stop(queue))
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001989 break;
1990
Wei Liue9ce7cb2014-06-04 10:30:42 +01001991 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001992 cond_resched();
1993 }
1994
1995 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01001996 if (tx_dealloc_work_todo(queue))
1997 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001998
1999 return 0;
2000}
2001
Ian Campbellf942dc22011-03-15 00:06:18 +00002002static int __init netback_init(void)
2003{
Ian Campbellf942dc22011-03-15 00:06:18 +00002004 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00002005
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05002006 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00002007 return -ENODEV;
2008
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01002009 /* Allow as many queues as there are CPUs, by default */
2010 xenvif_max_queues = num_online_cpus();
2011
Wei Liu37641492013-05-02 00:43:59 +00002012 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07002013 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
2014 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00002015 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00002016 }
2017
Ian Campbellf942dc22011-03-15 00:06:18 +00002018 rc = xenvif_xenbus_init();
2019 if (rc)
2020 goto failed_init;
2021
Zoltan Kissf51de242014-07-08 19:49:14 +01002022#ifdef CONFIG_DEBUG_FS
2023 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
2024 if (IS_ERR_OR_NULL(xen_netback_dbg_root))
2025 pr_warn("Init of debugfs returned %ld!\n",
2026 PTR_ERR(xen_netback_dbg_root));
2027#endif /* CONFIG_DEBUG_FS */
2028
Ian Campbellf942dc22011-03-15 00:06:18 +00002029 return 0;
2030
2031failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00002032 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00002033}
2034
2035module_init(netback_init);
2036
Wei Liub103f352013-05-16 23:26:11 +00002037static void __exit netback_fini(void)
2038{
Zoltan Kissf51de242014-07-08 19:49:14 +01002039#ifdef CONFIG_DEBUG_FS
2040 if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
2041 debugfs_remove_recursive(xen_netback_dbg_root);
2042#endif /* CONFIG_DEBUG_FS */
Wei Liub103f352013-05-16 23:26:11 +00002043 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00002044}
2045module_exit(netback_fini);
2046
Ian Campbellf942dc22011-03-15 00:06:18 +00002047MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07002048MODULE_ALIAS("xen-backend:vif");