blob: 49efff9b99f49f6d34bbe72b0414b0d8ddec849f [file] [log] [blame]
Ian Campbellf942dc22011-03-15 00:06:18 +00001/*
2 * Back-end of the driver for virtual network devices. This portion of the
3 * driver exports a 'unified' network-device interface that can be accessed
4 * by any operating system that implements a compatible front end. A
5 * reference front-end implementation can be found in:
6 * drivers/net/xen-netfront.c
7 *
8 * Copyright (c) 2002-2005, K A Fraser
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation; or, when distributed
13 * separately from the Linux kernel or incorporated into other
14 * software packages, subject to the following license:
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a copy
17 * of this source file (the "Software"), to deal in the Software without
18 * restriction, including without limitation the rights to use, copy, modify,
19 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32 * IN THE SOFTWARE.
33 */
34
35#include "common.h"
36
37#include <linux/kthread.h>
38#include <linux/if_vlan.h>
39#include <linux/udp.h>
Zoltan Kisse3377f32014-03-06 21:48:29 +000040#include <linux/highmem.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000041
42#include <net/tcp.h>
43
Stefano Stabellinica981632012-08-08 17:21:23 +000044#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000045#include <xen/events.h>
46#include <xen/interface/memory.h>
47
48#include <asm/xen/hypercall.h>
49#include <asm/xen/page.h>
50
Wei Liue1f00a692013-05-22 06:34:45 +000051/* Provide an option to disable split event channels at load time as
52 * event channels are limited resource. Split event channels are
53 * enabled by default.
54 */
55bool separate_tx_rx_irq = 1;
56module_param(separate_tx_rx_irq, bool, 0644);
57
Zoltan Kiss09350782014-03-06 21:48:30 +000058/* When guest ring is filled up, qdisc queues the packets for us, but we have
Zoltan Kiss0e59a4a2014-03-24 23:59:50 +000059 * to timeout them, otherwise other guests' packets can get stuck there
Zoltan Kiss09350782014-03-06 21:48:30 +000060 */
61unsigned int rx_drain_timeout_msecs = 10000;
62module_param(rx_drain_timeout_msecs, uint, 0444);
63unsigned int rx_drain_timeout_jiffies;
64
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +010065unsigned int xenvif_max_queues;
66module_param_named(max_queues, xenvif_max_queues, uint, 0644);
67MODULE_PARM_DESC(max_queues,
68 "Maximum number of queues per virtual interface");
69
Wei Liu2810e5b2013-04-22 02:20:42 +000070/*
71 * This is the maximum slots a skb can have. If a guest sends a skb
72 * which exceeds this limit it is considered malicious.
73 */
Wei Liu37641492013-05-02 00:43:59 +000074#define FATAL_SKB_SLOTS_DEFAULT 20
75static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
76module_param(fatal_skb_slots, uint, 0444);
77
Wei Liue9ce7cb2014-06-04 10:30:42 +010078static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +010079 u8 status);
80
Wei Liue9ce7cb2014-06-04 10:30:42 +010081static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000082 struct xen_netif_tx_request *txp,
83 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010084
Wei Liue9ce7cb2014-06-04 10:30:42 +010085static inline int tx_work_todo(struct xenvif_queue *queue);
86static inline int rx_work_todo(struct xenvif_queue *queue);
Wei Liub3f980b2013-08-26 12:59:38 +010087
Wei Liue9ce7cb2014-06-04 10:30:42 +010088static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +000089 u16 id,
90 s8 st,
91 u16 offset,
92 u16 size,
93 u16 flags);
94
Wei Liue9ce7cb2014-06-04 10:30:42 +010095static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +000096 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +000097{
Wei Liue9ce7cb2014-06-04 10:30:42 +010098 return page_to_pfn(queue->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +000099}
100
Wei Liue9ce7cb2014-06-04 10:30:42 +0100101static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
Ian Campbellea066ad2011-10-05 00:28:46 +0000102 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000103{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100104 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000105}
106
Zoltan Kiss7aceb472014-03-24 23:59:51 +0000107#define callback_param(vif, pending_idx) \
108 (vif->pending_tx_info[pending_idx].callback_struct)
109
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000110/* Find the containing VIF's structure from a pointer in pending_tx_info array
111 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100112static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000113{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000114 u16 pending_idx = ubuf->desc;
115 struct pending_tx_info *temp =
116 container_of(ubuf, struct pending_tx_info, callback_struct);
117 return container_of(temp - pending_idx,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100118 struct xenvif_queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000119 pending_tx_info[0]);
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000120}
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000121
Paul Durrant2eba61d2013-10-16 17:50:29 +0100122/* This is a miniumum size for the linear area to avoid lots of
123 * calls to __pskb_pull_tail() as we set up checksum offsets. The
124 * value 128 was chosen as it covers all IPv4 and most likely
125 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000126 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100127#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000128
Ian Campbellea066ad2011-10-05 00:28:46 +0000129static u16 frag_get_pending_idx(skb_frag_t *frag)
130{
131 return (u16)frag->page_offset;
132}
133
134static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
135{
136 frag->page_offset = pending_idx;
137}
138
Ian Campbellf942dc22011-03-15 00:06:18 +0000139static inline pending_ring_idx_t pending_index(unsigned i)
140{
141 return i & (MAX_PENDING_REQS-1);
142}
143
Wei Liue9ce7cb2014-06-04 10:30:42 +0100144bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
Ian Campbellf942dc22011-03-15 00:06:18 +0000145{
Paul Durrantca2f09f2013-12-06 16:36:07 +0000146 RING_IDX prod, cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000147
Paul Durrantca2f09f2013-12-06 16:36:07 +0000148 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100149 prod = queue->rx.sring->req_prod;
150 cons = queue->rx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000151
Paul Durrantca2f09f2013-12-06 16:36:07 +0000152 if (prod - cons >= needed)
153 return true;
Ian Campbellf942dc22011-03-15 00:06:18 +0000154
Wei Liue9ce7cb2014-06-04 10:30:42 +0100155 queue->rx.sring->req_event = prod + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000156
Paul Durrantca2f09f2013-12-06 16:36:07 +0000157 /* Make sure event is visible before we check prod
158 * again.
159 */
160 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +0100161 } while (queue->rx.sring->req_prod != prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000162
Paul Durrantca2f09f2013-12-06 16:36:07 +0000163 return false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000164}
165
166/*
167 * Returns true if we should start a new receive buffer instead of
168 * adding 'size' bytes to a buffer which currently contains 'offset'
169 * bytes.
170 */
171static bool start_new_rx_buffer(int offset, unsigned long size, int head)
172{
173 /* simple case: we have completely filled the current buffer. */
174 if (offset == MAX_BUFFER_OFFSET)
175 return true;
176
177 /*
178 * complex case: start a fresh buffer if the current frag
179 * would overflow the current buffer but only if:
180 * (i) this frag would fit completely in the next buffer
181 * and (ii) there is already some data in the current buffer
182 * and (iii) this is not the head buffer.
183 *
184 * Where:
185 * - (i) stops us splitting a frag into two copies
186 * unless the frag is too large for a single buffer.
187 * - (ii) stops us from leaving a buffer pointlessly empty.
188 * - (iii) stops us leaving the first buffer
189 * empty. Strictly speaking this is already covered
190 * by (ii) but is explicitly checked because
191 * netfront relies on the first buffer being
192 * non-empty and can crash otherwise.
193 *
194 * This means we will effectively linearise small
195 * frags but do not needlessly split large buffers
196 * into multiple copies tend to give large frags their
197 * own buffers as before.
198 */
Paul Durrant0576edd2014-03-28 11:39:05 +0000199 BUG_ON(size > MAX_BUFFER_OFFSET);
200 if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000201 return true;
202
203 return false;
204}
205
Ian Campbellf942dc22011-03-15 00:06:18 +0000206struct netrx_pending_operations {
207 unsigned copy_prod, copy_cons;
208 unsigned meta_prod, meta_cons;
209 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100210 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000211 int copy_off;
212 grant_ref_t copy_gref;
213};
214
Wei Liue9ce7cb2014-06-04 10:30:42 +0100215static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
Wei Liub3f980b2013-08-26 12:59:38 +0100216 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000217{
Wei Liub3f980b2013-08-26 12:59:38 +0100218 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000219 struct xen_netif_rx_request *req;
220
Wei Liue9ce7cb2014-06-04 10:30:42 +0100221 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000222
223 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100224 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000225 meta->gso_size = 0;
226 meta->size = 0;
227 meta->id = req->id;
228
229 npo->copy_off = 0;
230 npo->copy_gref = req->gref;
231
232 return meta;
233}
234
Wei Liu33bc8012013-10-08 10:54:21 +0100235/*
236 * Set up the grant operations for this fragment. If it's a flipping
237 * interface, we also set up the unmap request from here.
238 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100239static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
Wei Liu73764192013-08-26 12:59:39 +0100240 struct netrx_pending_operations *npo,
241 struct page *page, unsigned long size,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000242 unsigned long offset, int *head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100243 struct xenvif_queue *foreign_queue,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000244 grant_ref_t foreign_gref)
Ian Campbellf942dc22011-03-15 00:06:18 +0000245{
246 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100247 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000248 unsigned long bytes;
Annie Li5bd07672014-03-10 22:58:34 +0800249 int gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000250
251 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000252 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000253
254 meta = npo->meta + npo->meta_prod - 1;
255
Ian Campbell6a8ed462012-10-10 03:48:42 +0000256 /* Skip unused frames from start of page */
257 page += offset >> PAGE_SHIFT;
258 offset &= ~PAGE_MASK;
259
Ian Campbellf942dc22011-03-15 00:06:18 +0000260 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000261 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000262 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
263
Ian Campbell6a8ed462012-10-10 03:48:42 +0000264 bytes = PAGE_SIZE - offset;
265
266 if (bytes > size)
267 bytes = size;
268
Wei Liu33bc8012013-10-08 10:54:21 +0100269 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000270 /*
271 * Netfront requires there to be some data in the head
272 * buffer.
273 */
Wei Liu33bc8012013-10-08 10:54:21 +0100274 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000275
Wei Liue9ce7cb2014-06-04 10:30:42 +0100276 meta = get_next_rx_buffer(queue, npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000277 }
278
Ian Campbellf942dc22011-03-15 00:06:18 +0000279 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
280 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
281
282 copy_gop = npo->copy + npo->copy_prod++;
283 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100284 copy_gop->len = bytes;
285
Wei Liue9ce7cb2014-06-04 10:30:42 +0100286 if (foreign_queue) {
287 copy_gop->source.domid = foreign_queue->vif->domid;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000288 copy_gop->source.u.ref = foreign_gref;
289 copy_gop->flags |= GNTCOPY_source_gref;
290 } else {
291 copy_gop->source.domid = DOMID_SELF;
292 copy_gop->source.u.gmfn =
293 virt_to_mfn(page_address(page));
294 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000295 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000296
Wei Liue9ce7cb2014-06-04 10:30:42 +0100297 copy_gop->dest.domid = queue->vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000298 copy_gop->dest.offset = npo->copy_off;
299 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000300
301 npo->copy_off += bytes;
302 meta->size += bytes;
303
304 offset += bytes;
305 size -= bytes;
306
Ian Campbell6a8ed462012-10-10 03:48:42 +0000307 /* Next frame */
308 if (offset == PAGE_SIZE && size) {
309 BUG_ON(!PageCompound(page));
310 page++;
311 offset = 0;
312 }
313
Ian Campbellf942dc22011-03-15 00:06:18 +0000314 /* Leave a gap for the GSO descriptor. */
Annie Li5bd07672014-03-10 22:58:34 +0800315 if (skb_is_gso(skb)) {
316 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
317 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
318 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
319 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
320 }
Paul Durrant82cada22013-10-16 17:50:32 +0100321
Wei Liue9ce7cb2014-06-04 10:30:42 +0100322 if (*head && ((1 << gso_type) & queue->vif->gso_mask))
323 queue->rx.req_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000324
Wei Liu33bc8012013-10-08 10:54:21 +0100325 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000326
327 }
328}
329
330/*
Zoltan Kiss583757442014-05-15 11:08:34 +0100331 * Find the grant ref for a given frag in a chain of struct ubuf_info's
332 * skb: the skb itself
333 * i: the frag's number
334 * ubuf: a pointer to an element in the chain. It should not be NULL
335 *
336 * Returns a pointer to the element in the chain where the page were found. If
337 * not found, returns NULL.
338 * See the definition of callback_struct in common.h for more details about
339 * the chain.
340 */
341static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb,
342 const int i,
343 const struct ubuf_info *ubuf)
344{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100345 struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf);
Zoltan Kiss583757442014-05-15 11:08:34 +0100346
347 do {
348 u16 pending_idx = ubuf->desc;
349
350 if (skb_shinfo(skb)->frags[i].page.p ==
Wei Liue9ce7cb2014-06-04 10:30:42 +0100351 foreign_queue->mmap_pages[pending_idx])
Zoltan Kiss583757442014-05-15 11:08:34 +0100352 break;
353 ubuf = (struct ubuf_info *) ubuf->ctx;
354 } while (ubuf);
355
356 return ubuf;
357}
358
359/*
Ian Campbellf942dc22011-03-15 00:06:18 +0000360 * Prepare an SKB to be transmitted to the frontend.
361 *
362 * This function is responsible for allocating grant operations, meta
363 * structures, etc.
364 *
365 * It returns the number of meta structures consumed. The number of
366 * ring slots used is always equal to the number of meta slots used
367 * plus the number of GSO descriptors used. Currently, we use either
368 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
369 * frontend-side LRO).
370 */
Wei Liu73764192013-08-26 12:59:39 +0100371static int xenvif_gop_skb(struct sk_buff *skb,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100372 struct netrx_pending_operations *npo,
373 struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000374{
375 struct xenvif *vif = netdev_priv(skb->dev);
376 int nr_frags = skb_shinfo(skb)->nr_frags;
377 int i;
378 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100379 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000380 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100381 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000382 int old_meta_prod;
Paul Durrant82cada22013-10-16 17:50:32 +0100383 int gso_type;
Zoltan Kiss583757442014-05-15 11:08:34 +0100384 const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg;
385 const struct ubuf_info *const head_ubuf = ubuf;
Ian Campbellf942dc22011-03-15 00:06:18 +0000386
387 old_meta_prod = npo->meta_prod;
388
Annie Li5bd07672014-03-10 22:58:34 +0800389 gso_type = XEN_NETIF_GSO_TYPE_NONE;
390 if (skb_is_gso(skb)) {
391 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
392 gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
393 else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
394 gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
Paul Durrant82cada22013-10-16 17:50:32 +0100395 }
396
Ian Campbellf942dc22011-03-15 00:06:18 +0000397 /* Set up a GSO prefix descriptor, if necessary */
Paul Durranta3314f32013-12-12 14:20:13 +0000398 if ((1 << gso_type) & vif->gso_prefix_mask) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100399 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000400 meta = npo->meta + npo->meta_prod++;
Paul Durrant82cada22013-10-16 17:50:32 +0100401 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800402 meta->gso_size = skb_shinfo(skb)->gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000403 meta->size = 0;
404 meta->id = req->id;
405 }
406
Wei Liue9ce7cb2014-06-04 10:30:42 +0100407 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000408 meta = npo->meta + npo->meta_prod++;
409
Paul Durrant82cada22013-10-16 17:50:32 +0100410 if ((1 << gso_type) & vif->gso_mask) {
411 meta->gso_type = gso_type;
Annie Li5bd07672014-03-10 22:58:34 +0800412 meta->gso_size = skb_shinfo(skb)->gso_size;
Paul Durrant82cada22013-10-16 17:50:32 +0100413 } else {
414 meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
Ian Campbellf942dc22011-03-15 00:06:18 +0000415 meta->gso_size = 0;
Paul Durrant82cada22013-10-16 17:50:32 +0100416 }
Ian Campbellf942dc22011-03-15 00:06:18 +0000417
418 meta->size = 0;
419 meta->id = req->id;
420 npo->copy_off = 0;
421 npo->copy_gref = req->gref;
422
423 data = skb->data;
424 while (data < skb_tail_pointer(skb)) {
425 unsigned int offset = offset_in_page(data);
426 unsigned int len = PAGE_SIZE - offset;
427
428 if (data + len > skb_tail_pointer(skb))
429 len = skb_tail_pointer(skb) - data;
430
Wei Liue9ce7cb2014-06-04 10:30:42 +0100431 xenvif_gop_frag_copy(queue, skb, npo,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000432 virt_to_page(data), len, offset, &head,
433 NULL,
434 0);
Ian Campbellf942dc22011-03-15 00:06:18 +0000435 data += len;
436 }
437
438 for (i = 0; i < nr_frags; i++) {
Zoltan Kiss583757442014-05-15 11:08:34 +0100439 /* This variable also signals whether foreign_gref has a real
440 * value or not.
441 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100442 struct xenvif_queue *foreign_queue = NULL;
Zoltan Kiss583757442014-05-15 11:08:34 +0100443 grant_ref_t foreign_gref;
444
445 if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) &&
446 (ubuf->callback == &xenvif_zerocopy_callback)) {
447 const struct ubuf_info *const startpoint = ubuf;
448
449 /* Ideally ubuf points to the chain element which
450 * belongs to this frag. Or if frags were removed from
451 * the beginning, then shortly before it.
452 */
453 ubuf = xenvif_find_gref(skb, i, ubuf);
454
455 /* Try again from the beginning of the list, if we
456 * haven't tried from there. This only makes sense in
457 * the unlikely event of reordering the original frags.
458 * For injected local pages it's an unnecessary second
459 * run.
460 */
461 if (unlikely(!ubuf) && startpoint != head_ubuf)
462 ubuf = xenvif_find_gref(skb, i, head_ubuf);
463
464 if (likely(ubuf)) {
465 u16 pending_idx = ubuf->desc;
466
Wei Liue9ce7cb2014-06-04 10:30:42 +0100467 foreign_queue = ubuf_to_queue(ubuf);
468 foreign_gref =
469 foreign_queue->pending_tx_info[pending_idx].req.gref;
Zoltan Kiss583757442014-05-15 11:08:34 +0100470 /* Just a safety measure. If this was the last
471 * element on the list, the for loop will
472 * iterate again if a local page were added to
473 * the end. Using head_ubuf here prevents the
474 * second search on the chain. Or the original
475 * frags changed order, but that's less likely.
476 * In any way, ubuf shouldn't be NULL.
477 */
478 ubuf = ubuf->ctx ?
479 (struct ubuf_info *) ubuf->ctx :
480 head_ubuf;
481 } else
482 /* This frag was a local page, added to the
483 * array after the skb left netback.
484 */
485 ubuf = head_ubuf;
486 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100487 xenvif_gop_frag_copy(queue, skb, npo,
Wei Liu73764192013-08-26 12:59:39 +0100488 skb_frag_page(&skb_shinfo(skb)->frags[i]),
489 skb_frag_size(&skb_shinfo(skb)->frags[i]),
490 skb_shinfo(skb)->frags[i].page_offset,
Zoltan Kiss3e2234b2014-03-06 21:48:25 +0000491 &head,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100492 foreign_queue,
493 foreign_queue ? foreign_gref : UINT_MAX);
Ian Campbellf942dc22011-03-15 00:06:18 +0000494 }
495
496 return npo->meta_prod - old_meta_prod;
497}
498
499/*
Wei Liu73764192013-08-26 12:59:39 +0100500 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000501 * used to set up the operations on the top of
502 * netrx_pending_operations, which have since been done. Check that
503 * they didn't give any errors and advance over them.
504 */
Wei Liu73764192013-08-26 12:59:39 +0100505static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
506 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000507{
508 struct gnttab_copy *copy_op;
509 int status = XEN_NETIF_RSP_OKAY;
510 int i;
511
512 for (i = 0; i < nr_meta_slots; i++) {
513 copy_op = npo->copy + npo->copy_cons++;
514 if (copy_op->status != GNTST_okay) {
515 netdev_dbg(vif->dev,
516 "Bad status %d from copy to DOM%d.\n",
517 copy_op->status, vif->domid);
518 status = XEN_NETIF_RSP_ERROR;
519 }
520 }
521
522 return status;
523}
524
Wei Liue9ce7cb2014-06-04 10:30:42 +0100525static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
Wei Liu73764192013-08-26 12:59:39 +0100526 struct xenvif_rx_meta *meta,
527 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000528{
529 int i;
530 unsigned long offset;
531
532 /* No fragments used */
533 if (nr_meta_slots <= 1)
534 return;
535
536 nr_meta_slots--;
537
538 for (i = 0; i < nr_meta_slots; i++) {
539 int flags;
540 if (i == nr_meta_slots - 1)
541 flags = 0;
542 else
543 flags = XEN_NETRXF_more_data;
544
545 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100546 make_rx_response(queue, meta[i].id, status, offset,
Ian Campbellf942dc22011-03-15 00:06:18 +0000547 meta[i].size, flags);
548 }
549}
550
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000551struct xenvif_rx_cb {
Wei Liu33bc8012013-10-08 10:54:21 +0100552 int meta_slots_used;
553};
554
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000555#define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
556
Wei Liue9ce7cb2014-06-04 10:30:42 +0100557void xenvif_kick_thread(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000558{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100559 wake_up(&queue->wq);
Wei Liub3f980b2013-08-26 12:59:38 +0100560}
561
Wei Liue9ce7cb2014-06-04 10:30:42 +0100562static void xenvif_rx_action(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +0100563{
Ian Campbellf942dc22011-03-15 00:06:18 +0000564 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000565 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000566 struct xen_netif_rx_response *resp;
567 struct sk_buff_head rxq;
568 struct sk_buff *skb;
569 LIST_HEAD(notify);
570 int ret;
Ian Campbellf942dc22011-03-15 00:06:18 +0000571 unsigned long offset;
Paul Durrant11b57f92014-01-08 12:41:58 +0000572 bool need_to_notify = false;
Ian Campbellf942dc22011-03-15 00:06:18 +0000573
574 struct netrx_pending_operations npo = {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100575 .copy = queue->grant_copy_op,
576 .meta = queue->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000577 };
578
579 skb_queue_head_init(&rxq);
580
Wei Liue9ce7cb2014-06-04 10:30:42 +0100581 while ((skb = skb_dequeue(&queue->rx_queue)) != NULL) {
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000582 RING_IDX max_slots_needed;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000583 RING_IDX old_req_cons;
584 RING_IDX ring_slots_used;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000585 int i;
586
587 /* We need a cheap worse case estimate for the number of
588 * slots we'll use.
589 */
590
591 max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
592 skb_headlen(skb),
593 PAGE_SIZE);
594 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
595 unsigned int size;
Paul Durranta02eb472014-03-28 11:39:06 +0000596 unsigned int offset;
597
Paul Durrantca2f09f2013-12-06 16:36:07 +0000598 size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Paul Durranta02eb472014-03-28 11:39:06 +0000599 offset = skb_shinfo(skb)->frags[i].page_offset;
600
601 /* For a worse-case estimate we need to factor in
602 * the fragment page offset as this will affect the
603 * number of times xenvif_gop_frag_copy() will
604 * call start_new_rx_buffer().
605 */
606 max_slots_needed += DIV_ROUND_UP(offset + size,
607 PAGE_SIZE);
Paul Durrantca2f09f2013-12-06 16:36:07 +0000608 }
Paul Durranta02eb472014-03-28 11:39:06 +0000609
610 /* To avoid the estimate becoming too pessimal for some
611 * frontends that limit posted rx requests, cap the estimate
612 * at MAX_SKB_FRAGS.
613 */
614 if (max_slots_needed > MAX_SKB_FRAGS)
615 max_slots_needed = MAX_SKB_FRAGS;
616
617 /* We may need one more slot for GSO metadata */
Annie Li5bd07672014-03-10 22:58:34 +0800618 if (skb_is_gso(skb) &&
619 (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
620 skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6))
Paul Durrantca2f09f2013-12-06 16:36:07 +0000621 max_slots_needed++;
622
623 /* If the skb may not fit then bail out now */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100624 if (!xenvif_rx_ring_slots_available(queue, max_slots_needed)) {
625 skb_queue_head(&queue->rx_queue, skb);
Paul Durrant11b57f92014-01-08 12:41:58 +0000626 need_to_notify = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100627 queue->rx_last_skb_slots = max_slots_needed;
Paul Durrantca2f09f2013-12-06 16:36:07 +0000628 break;
Zoltan Kiss9ab98312014-02-04 19:54:37 +0000629 } else
Wei Liue9ce7cb2014-06-04 10:30:42 +0100630 queue->rx_last_skb_slots = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000631
Wei Liue9ce7cb2014-06-04 10:30:42 +0100632 old_req_cons = queue->rx.req_cons;
633 XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
634 ring_slots_used = queue->rx.req_cons - old_req_cons;
Paul Durrant1425c7a2014-03-28 11:39:07 +0000635
636 BUG_ON(ring_slots_used > max_slots_needed);
Ian Campbellf942dc22011-03-15 00:06:18 +0000637
638 __skb_queue_tail(&rxq, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000639 }
640
Wei Liue9ce7cb2014-06-04 10:30:42 +0100641 BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000642
643 if (!npo.copy_prod)
Paul Durrantca2f09f2013-12-06 16:36:07 +0000644 goto done;
Ian Campbellf942dc22011-03-15 00:06:18 +0000645
Paul Durrantac3d5ac2013-12-23 09:27:17 +0000646 BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100647 gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000648
649 while ((skb = __skb_dequeue(&rxq)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000650
Wei Liue9ce7cb2014-06-04 10:30:42 +0100651 if ((1 << queue->meta[npo.meta_cons].gso_type) &
652 queue->vif->gso_prefix_mask) {
653 resp = RING_GET_RESPONSE(&queue->rx,
654 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000655
656 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
657
Wei Liue9ce7cb2014-06-04 10:30:42 +0100658 resp->offset = queue->meta[npo.meta_cons].gso_size;
659 resp->id = queue->meta[npo.meta_cons].id;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000660 resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000661
662 npo.meta_cons++;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000663 XENVIF_RX_CB(skb)->meta_slots_used--;
Ian Campbellf942dc22011-03-15 00:06:18 +0000664 }
665
666
Wei Liue9ce7cb2014-06-04 10:30:42 +0100667 queue->stats.tx_bytes += skb->len;
668 queue->stats.tx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000669
Wei Liue9ce7cb2014-06-04 10:30:42 +0100670 status = xenvif_check_gop(queue->vif,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000671 XENVIF_RX_CB(skb)->meta_slots_used,
672 &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000673
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000674 if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
Ian Campbellf942dc22011-03-15 00:06:18 +0000675 flags = 0;
676 else
677 flags = XEN_NETRXF_more_data;
678
679 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
680 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
681 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
682 /* remote but checksummed. */
683 flags |= XEN_NETRXF_data_validated;
684
685 offset = 0;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100686 resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000687 status, offset,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100688 queue->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000689 flags);
690
Wei Liue9ce7cb2014-06-04 10:30:42 +0100691 if ((1 << queue->meta[npo.meta_cons].gso_type) &
692 queue->vif->gso_mask) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000693 struct xen_netif_extra_info *gso =
694 (struct xen_netif_extra_info *)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100695 RING_GET_RESPONSE(&queue->rx,
696 queue->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000697
698 resp->flags |= XEN_NETRXF_extra_info;
699
Wei Liue9ce7cb2014-06-04 10:30:42 +0100700 gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
701 gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000702 gso->u.gso.pad = 0;
703 gso->u.gso.features = 0;
704
705 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
706 gso->flags = 0;
707 }
708
Wei Liue9ce7cb2014-06-04 10:30:42 +0100709 xenvif_add_frag_responses(queue, status,
710 queue->meta + npo.meta_cons + 1,
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000711 XENVIF_RX_CB(skb)->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000712
Wei Liue9ce7cb2014-06-04 10:30:42 +0100713 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000714
Paul Durrant11b57f92014-01-08 12:41:58 +0000715 need_to_notify |= !!ret;
Wei Liub3f980b2013-08-26 12:59:38 +0100716
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000717 npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
Ian Campbellf942dc22011-03-15 00:06:18 +0000718 dev_kfree_skb(skb);
719 }
720
Paul Durrantca2f09f2013-12-06 16:36:07 +0000721done:
Wei Liub3f980b2013-08-26 12:59:38 +0100722 if (need_to_notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100723 notify_remote_via_irq(queue->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000724}
725
Wei Liue9ce7cb2014-06-04 10:30:42 +0100726void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000727{
728 int more_to_do;
729
Wei Liue9ce7cb2014-06-04 10:30:42 +0100730 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +0000731
732 if (more_to_do)
Wei Liue9ce7cb2014-06-04 10:30:42 +0100733 napi_schedule(&queue->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000734}
735
Wei Liue9ce7cb2014-06-04 10:30:42 +0100736static void tx_add_credit(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +0000737{
738 unsigned long max_burst, max_credit;
739
740 /*
741 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
742 * Otherwise the interface can seize up due to insufficient credit.
743 */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100744 max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000745 max_burst = min(max_burst, 131072UL);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100746 max_burst = max(max_burst, queue->credit_bytes);
Ian Campbellf942dc22011-03-15 00:06:18 +0000747
748 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
Wei Liue9ce7cb2014-06-04 10:30:42 +0100749 max_credit = queue->remaining_credit + queue->credit_bytes;
750 if (max_credit < queue->remaining_credit)
Ian Campbellf942dc22011-03-15 00:06:18 +0000751 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
752
Wei Liue9ce7cb2014-06-04 10:30:42 +0100753 queue->remaining_credit = min(max_credit, max_burst);
Ian Campbellf942dc22011-03-15 00:06:18 +0000754}
755
756static void tx_credit_callback(unsigned long data)
757{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100758 struct xenvif_queue *queue = (struct xenvif_queue *)data;
759 tx_add_credit(queue);
760 xenvif_napi_schedule_or_enable_events(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +0000761}
762
Wei Liue9ce7cb2014-06-04 10:30:42 +0100763static void xenvif_tx_err(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100764 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000765{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100766 RING_IDX cons = queue->tx.req_cons;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000767 unsigned long flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000768
769 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100770 spin_lock_irqsave(&queue->response_lock, flags);
771 make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
772 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellb9149722013-02-06 23:41:38 +0000773 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000774 break;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100775 txp = RING_GET_REQUEST(&queue->tx, cons++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000776 } while (1);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100777 queue->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000778}
779
Wei Liu73764192013-08-26 12:59:39 +0100780static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000781{
782 netdev_err(vif->dev, "fatal error; disabling device\n");
Wei Liue9d8b2c2014-04-01 12:46:12 +0100783 vif->disabled = true;
Wei Liue9ce7cb2014-06-04 10:30:42 +0100784 /* Disable the vif from queue 0's kthread */
785 if (vif->queues)
786 xenvif_kick_thread(&vif->queues[0]);
Ian Campbell488562862013-02-06 23:41:35 +0000787}
788
Wei Liue9ce7cb2014-06-04 10:30:42 +0100789static int xenvif_count_requests(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +0100790 struct xen_netif_tx_request *first,
791 struct xen_netif_tx_request *txp,
792 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000793{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100794 RING_IDX cons = queue->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000795 int slots = 0;
796 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000797 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000798
799 if (!(first->flags & XEN_NETTXF_more_data))
800 return 0;
801
802 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000803 struct xen_netif_tx_request dropped_tx = { 0 };
804
Wei Liu2810e5b2013-04-22 02:20:42 +0000805 if (slots >= work_to_do) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100806 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000807 "Asked for %d slots but exceeds this limit\n",
808 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100809 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000810 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000811 }
812
Wei Liu2810e5b2013-04-22 02:20:42 +0000813 /* This guest is really using too many slots and
814 * considered malicious.
815 */
Wei Liu37641492013-05-02 00:43:59 +0000816 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100817 netdev_err(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000818 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000819 slots, fatal_skb_slots);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100820 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000821 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000822 }
823
Wei Liu2810e5b2013-04-22 02:20:42 +0000824 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000825 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
826 * the historical MAX_SKB_FRAGS value 18 to honor the
827 * same behavior as before. Any packet using more than
828 * 18 slots but less than fatal_skb_slots slots is
829 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000830 */
Wei Liu37641492013-05-02 00:43:59 +0000831 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000832 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100833 netdev_dbg(queue->vif->dev,
Wei Liu2810e5b2013-04-22 02:20:42 +0000834 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000835 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000836 drop_err = -E2BIG;
837 }
838
Wei Liu59ccb4e2013-05-02 00:43:58 +0000839 if (drop_err)
840 txp = &dropped_tx;
841
Wei Liue9ce7cb2014-06-04 10:30:42 +0100842 memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000843 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000844
845 /* If the guest submitted a frame >= 64 KiB then
846 * first->size overflowed and following slots will
847 * appear to be larger than the frame.
848 *
849 * This cannot be fatal error as there are buggy
850 * frontends that do this.
851 *
852 * Consume all slots and drop the packet.
853 */
854 if (!drop_err && txp->size > first->size) {
855 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100856 netdev_dbg(queue->vif->dev,
Wei Liu03393fd52013-04-22 02:20:43 +0000857 "Invalid tx request, slot size %u > remaining size %u\n",
858 txp->size, first->size);
859 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000860 }
861
862 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000863 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000864
865 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100866 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000867 txp->offset, txp->size);
Wei Liue9ce7cb2014-06-04 10:30:42 +0100868 xenvif_fatal_tx_err(queue->vif);
David Vrabel35876b52013-02-14 03:18:57 +0000869 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000870 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000871
872 more_data = txp->flags & XEN_NETTXF_more_data;
873
874 if (!drop_err)
875 txp++;
876
877 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000878
879 if (drop_err) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100880 xenvif_tx_err(queue, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000881 return drop_err;
882 }
883
884 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000885}
886
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000887
888struct xenvif_tx_cb {
889 u16 pending_idx;
890};
891
892#define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
893
Wei Liue9ce7cb2014-06-04 10:30:42 +0100894static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +0100895 u16 pending_idx,
896 struct xen_netif_tx_request *txp,
897 struct gnttab_map_grant_ref *mop)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000898{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100899 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
900 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000901 GNTMAP_host_map | GNTMAP_readonly,
Wei Liue9ce7cb2014-06-04 10:30:42 +0100902 txp->gref, queue->vif->domid);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000903
Wei Liue9ce7cb2014-06-04 10:30:42 +0100904 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000905 sizeof(*txp));
906}
907
Zoltan Kisse3377f32014-03-06 21:48:29 +0000908static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
909{
910 struct sk_buff *skb =
911 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
912 GFP_ATOMIC | __GFP_NOWARN);
913 if (unlikely(skb == NULL))
914 return NULL;
915
916 /* Packets passed to netif_rx() must have some headroom. */
917 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
918
919 /* Initialize it here to avoid later surprises */
920 skb_shinfo(skb)->destructor_arg = NULL;
921
922 return skb;
923}
924
Wei Liue9ce7cb2014-06-04 10:30:42 +0100925static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000926 struct sk_buff *skb,
927 struct xen_netif_tx_request *txp,
928 struct gnttab_map_grant_ref *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000929{
930 struct skb_shared_info *shinfo = skb_shinfo(skb);
931 skb_frag_t *frags = shinfo->frags;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +0000932 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +0000933 int start;
934 pending_ring_idx_t index;
Zoltan Kisse3377f32014-03-06 21:48:29 +0000935 unsigned int nr_slots, frag_overflow = 0;
Wei Liu2810e5b2013-04-22 02:20:42 +0000936
937 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000938 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000939 */
Zoltan Kisse3377f32014-03-06 21:48:29 +0000940 if (shinfo->nr_frags > MAX_SKB_FRAGS) {
941 frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
942 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
943 shinfo->nr_frags = MAX_SKB_FRAGS;
944 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000945 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000946
947 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000948 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000949
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000950 for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
951 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100952 index = pending_index(queue->pending_cons++);
953 pending_idx = queue->pending_ring[index];
954 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000955 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000956 }
957
Zoltan Kisse3377f32014-03-06 21:48:29 +0000958 if (frag_overflow) {
959 struct sk_buff *nskb = xenvif_alloc_skb(0);
960 if (unlikely(nskb == NULL)) {
961 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +0100962 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +0000963 "Can't allocate the frag_list skb.\n");
964 return NULL;
965 }
966
967 shinfo = skb_shinfo(nskb);
968 frags = shinfo->frags;
969
970 for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
971 shinfo->nr_frags++, txp++, gop++) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100972 index = pending_index(queue->pending_cons++);
973 pending_idx = queue->pending_ring[index];
974 xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
Zoltan Kisse3377f32014-03-06 21:48:29 +0000975 frag_set_pending_idx(&frags[shinfo->nr_frags],
976 pending_idx);
977 }
978
979 skb_shinfo(skb)->frag_list = nskb;
980 }
Wei Liu2810e5b2013-04-22 02:20:42 +0000981
Ian Campbellf942dc22011-03-15 00:06:18 +0000982 return gop;
983}
984
Wei Liue9ce7cb2014-06-04 10:30:42 +0100985static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000986 u16 pending_idx,
987 grant_handle_t handle)
988{
Wei Liue9ce7cb2014-06-04 10:30:42 +0100989 if (unlikely(queue->grant_tx_handle[pending_idx] !=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000990 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +0100991 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000992 "Trying to overwrite active handle! pending_idx: %x\n",
993 pending_idx);
994 BUG();
995 }
Wei Liue9ce7cb2014-06-04 10:30:42 +0100996 queue->grant_tx_handle[pending_idx] = handle;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +0000997}
998
Wei Liue9ce7cb2014-06-04 10:30:42 +0100999static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001000 u16 pending_idx)
1001{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001002 if (unlikely(queue->grant_tx_handle[pending_idx] ==
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001003 NETBACK_INVALID_HANDLE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001004 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001005 "Trying to unmap invalid handle! pending_idx: %x\n",
1006 pending_idx);
1007 BUG();
1008 }
Wei Liue9ce7cb2014-06-04 10:30:42 +01001009 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001010}
1011
Wei Liue9ce7cb2014-06-04 10:30:42 +01001012static int xenvif_tx_check_gop(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001013 struct sk_buff *skb,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001014 struct gnttab_map_grant_ref **gopp_map,
1015 struct gnttab_copy **gopp_copy)
Ian Campbellf942dc22011-03-15 00:06:18 +00001016{
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001017 struct gnttab_map_grant_ref *gop_map = *gopp_map;
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001018 u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001019 struct skb_shared_info *shinfo = skb_shinfo(skb);
1020 int nr_frags = shinfo->nr_frags;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001021 int i, err;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001022 struct sk_buff *first_skb = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001023
1024 /* Check status of header. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001025 err = (*gopp_copy)->status;
1026 (*gopp_copy)++;
1027 if (unlikely(err)) {
1028 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001029 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001030 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001031 (*gopp_copy)->status,
1032 pending_idx,
1033 (*gopp_copy)->source.u.ref);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001034 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001035 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001036
Zoltan Kisse3377f32014-03-06 21:48:29 +00001037check_frags:
Zoltan Kissbdab8272014-04-02 18:04:58 +01001038 for (i = 0; i < nr_frags; i++, gop_map++) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001039 int j, newerr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001040
Ian Campbellea066ad2011-10-05 00:28:46 +00001041 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001042
1043 /* Check error status: if okay then remember grant handle. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001044 newerr = gop_map->status;
Wei Liu2810e5b2013-04-22 02:20:42 +00001045
Ian Campbellf942dc22011-03-15 00:06:18 +00001046 if (likely(!newerr)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001047 xenvif_grant_handle_set(queue,
Zoltan Kiss9074ce22014-04-02 18:04:57 +01001048 pending_idx,
1049 gop_map->handle);
Ian Campbellf942dc22011-03-15 00:06:18 +00001050 /* Had a previous error? Invalidate this fragment. */
1051 if (unlikely(err))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001052 xenvif_idx_unmap(queue, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001053 continue;
1054 }
1055
1056 /* Error on this fragment: respond to client with an error. */
Zoltan Kissbdab8272014-04-02 18:04:58 +01001057 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001058 netdev_dbg(queue->vif->dev,
Zoltan Kiss00aefce2014-04-04 15:45:24 +01001059 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
Zoltan Kissbdab8272014-04-02 18:04:58 +01001060 i,
1061 gop_map->status,
1062 pending_idx,
1063 gop_map->ref);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001064 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001065
1066 /* Not the first error? Preceding frags already invalidated. */
1067 if (err)
1068 continue;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001069 /* First error: invalidate preceding fragments. */
1070 for (j = 0; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001071 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001072 xenvif_idx_unmap(queue, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001073 }
1074
1075 /* Remember the error: invalidate all subsequent fragments. */
1076 err = newerr;
1077 }
1078
Zoltan Kisse3377f32014-03-06 21:48:29 +00001079 if (skb_has_frag_list(skb)) {
1080 first_skb = skb;
1081 skb = shinfo->frag_list;
1082 shinfo = skb_shinfo(skb);
1083 nr_frags = shinfo->nr_frags;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001084
1085 goto check_frags;
1086 }
1087
1088 /* There was a mapping error in the frag_list skb. We have to unmap
1089 * the first skb's frags
1090 */
1091 if (first_skb && err) {
1092 int j;
1093 shinfo = skb_shinfo(first_skb);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001094 for (j = 0; j < shinfo->nr_frags; j++) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001095 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001096 xenvif_idx_unmap(queue, pending_idx);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001097 }
1098 }
1099
Zoltan Kissbdab8272014-04-02 18:04:58 +01001100 *gopp_map = gop_map;
Ian Campbellf942dc22011-03-15 00:06:18 +00001101 return err;
1102}
1103
Wei Liue9ce7cb2014-06-04 10:30:42 +01001104static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001105{
1106 struct skb_shared_info *shinfo = skb_shinfo(skb);
1107 int nr_frags = shinfo->nr_frags;
1108 int i;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001109 u16 prev_pending_idx = INVALID_PENDING_IDX;
1110
Ian Campbellf942dc22011-03-15 00:06:18 +00001111 for (i = 0; i < nr_frags; i++) {
1112 skb_frag_t *frag = shinfo->frags + i;
1113 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001114 struct page *page;
1115 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001116
Ian Campbellea066ad2011-10-05 00:28:46 +00001117 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001118
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001119 /* If this is not the first frag, chain it to the previous*/
Zoltan Kissbdab8272014-04-02 18:04:58 +01001120 if (prev_pending_idx == INVALID_PENDING_IDX)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001121 skb_shinfo(skb)->destructor_arg =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001122 &callback_param(queue, pending_idx);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001123 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001124 callback_param(queue, prev_pending_idx).ctx =
1125 &callback_param(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001126
Wei Liue9ce7cb2014-06-04 10:30:42 +01001127 callback_param(queue, pending_idx).ctx = NULL;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001128 prev_pending_idx = pending_idx;
1129
Wei Liue9ce7cb2014-06-04 10:30:42 +01001130 txp = &queue->pending_tx_info[pending_idx].req;
1131 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001132 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001133 skb->len += txp->size;
1134 skb->data_len += txp->size;
1135 skb->truesize += txp->size;
1136
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001137 /* Take an extra reference to offset network stack's put_page */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001138 get_page(queue->mmap_pages[pending_idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +00001139 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001140 /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
1141 * overlaps with "index", and "mapping" is not set. I think mapping
1142 * should be set. If delivered to local stack, it would drop this
1143 * skb in sk_filter unless the socket has the right to use it.
1144 */
1145 skb->pfmemalloc = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001146}
1147
Wei Liue9ce7cb2014-06-04 10:30:42 +01001148static int xenvif_get_extras(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001149 struct xen_netif_extra_info *extras,
1150 int work_to_do)
1151{
1152 struct xen_netif_extra_info extra;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001153 RING_IDX cons = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001154
1155 do {
1156 if (unlikely(work_to_do-- <= 0)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001157 netdev_err(queue->vif->dev, "Missing extra info\n");
1158 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001159 return -EBADR;
1160 }
1161
Wei Liue9ce7cb2014-06-04 10:30:42 +01001162 memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
Ian Campbellf942dc22011-03-15 00:06:18 +00001163 sizeof(extra));
1164 if (unlikely(!extra.type ||
1165 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001166 queue->tx.req_cons = ++cons;
1167 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001168 "Invalid extra type: %d\n", extra.type);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001169 xenvif_fatal_tx_err(queue->vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001170 return -EINVAL;
1171 }
1172
1173 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
Wei Liue9ce7cb2014-06-04 10:30:42 +01001174 queue->tx.req_cons = ++cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001175 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1176
1177 return work_to_do;
1178}
1179
Wei Liu73764192013-08-26 12:59:39 +01001180static int xenvif_set_skb_gso(struct xenvif *vif,
1181 struct sk_buff *skb,
1182 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001183{
1184 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001185 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001186 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001187 return -EINVAL;
1188 }
1189
Paul Durranta9468582013-10-16 17:50:31 +01001190 switch (gso->u.gso.type) {
1191 case XEN_NETIF_GSO_TYPE_TCPV4:
1192 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1193 break;
1194 case XEN_NETIF_GSO_TYPE_TCPV6:
1195 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1196 break;
1197 default:
Ian Campbell488562862013-02-06 23:41:35 +00001198 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001199 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001200 return -EINVAL;
1201 }
1202
1203 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Paul Durrantb89587a2013-12-17 11:44:35 +00001204 /* gso_segs will be calculated later */
Ian Campbellf942dc22011-03-15 00:06:18 +00001205
1206 return 0;
1207}
1208
Wei Liue9ce7cb2014-06-04 10:30:42 +01001209static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001210{
Paul Durrant27216372014-01-09 10:02:47 +00001211 bool recalculate_partial_csum = false;
Ian Campbellf942dc22011-03-15 00:06:18 +00001212
Paul Durrant2eba61d2013-10-16 17:50:29 +01001213 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001214 * peers can fail to set NETRXF_csum_blank when sending a GSO
1215 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1216 * recalculate the partial checksum.
1217 */
1218 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001219 queue->stats.rx_gso_checksum_fixup++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001220 skb->ip_summed = CHECKSUM_PARTIAL;
Paul Durrant27216372014-01-09 10:02:47 +00001221 recalculate_partial_csum = true;
Ian Campbellf942dc22011-03-15 00:06:18 +00001222 }
1223
1224 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1225 if (skb->ip_summed != CHECKSUM_PARTIAL)
1226 return 0;
1227
Paul Durrant27216372014-01-09 10:02:47 +00001228 return skb_checksum_setup(skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001229}
1230
Wei Liue9ce7cb2014-06-04 10:30:42 +01001231static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
Ian Campbellf942dc22011-03-15 00:06:18 +00001232{
Wei Liu059dfa62013-10-28 12:07:57 +00001233 u64 now = get_jiffies_64();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001234 u64 next_credit = queue->credit_window_start +
1235 msecs_to_jiffies(queue->credit_usec / 1000);
Ian Campbellf942dc22011-03-15 00:06:18 +00001236
1237 /* Timer could already be pending in rare cases. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001238 if (timer_pending(&queue->credit_timeout))
Ian Campbellf942dc22011-03-15 00:06:18 +00001239 return true;
1240
1241 /* Passed the point where we can replenish credit? */
Wei Liu059dfa62013-10-28 12:07:57 +00001242 if (time_after_eq64(now, next_credit)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001243 queue->credit_window_start = now;
1244 tx_add_credit(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001245 }
1246
1247 /* Still too big to send right now? Set a callback. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001248 if (size > queue->remaining_credit) {
1249 queue->credit_timeout.data =
1250 (unsigned long)queue;
1251 queue->credit_timeout.function =
Ian Campbellf942dc22011-03-15 00:06:18 +00001252 tx_credit_callback;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001253 mod_timer(&queue->credit_timeout,
Ian Campbellf942dc22011-03-15 00:06:18 +00001254 next_credit);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001255 queue->credit_window_start = next_credit;
Ian Campbellf942dc22011-03-15 00:06:18 +00001256
1257 return true;
1258 }
1259
1260 return false;
1261}
1262
Wei Liue9ce7cb2014-06-04 10:30:42 +01001263static void xenvif_tx_build_gops(struct xenvif_queue *queue,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001264 int budget,
1265 unsigned *copy_ops,
1266 unsigned *map_ops)
Ian Campbellf942dc22011-03-15 00:06:18 +00001267{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001268 struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001269 struct sk_buff *skb;
1270 int ret;
1271
Wei Liue9ce7cb2014-06-04 10:30:42 +01001272 while (skb_queue_len(&queue->tx_queue) < budget) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001273 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001274 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001275 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1276 u16 pending_idx;
1277 RING_IDX idx;
1278 int work_to_do;
1279 unsigned int data_len;
1280 pending_ring_idx_t index;
1281
Wei Liue9ce7cb2014-06-04 10:30:42 +01001282 if (queue->tx.sring->req_prod - queue->tx.req_cons >
Ian Campbell488562862013-02-06 23:41:35 +00001283 XEN_NETIF_TX_RING_SIZE) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001284 netdev_err(queue->vif->dev,
Ian Campbell488562862013-02-06 23:41:35 +00001285 "Impossible number of requests. "
1286 "req_prod %d, req_cons %d, size %ld\n",
Wei Liue9ce7cb2014-06-04 10:30:42 +01001287 queue->tx.sring->req_prod, queue->tx.req_cons,
Ian Campbell488562862013-02-06 23:41:35 +00001288 XEN_NETIF_TX_RING_SIZE);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001289 xenvif_fatal_tx_err(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001290 break;
Ian Campbell488562862013-02-06 23:41:35 +00001291 }
1292
Wei Liue9ce7cb2014-06-04 10:30:42 +01001293 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
Wei Liub3f980b2013-08-26 12:59:38 +01001294 if (!work_to_do)
1295 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001296
Wei Liue9ce7cb2014-06-04 10:30:42 +01001297 idx = queue->tx.req_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +00001298 rmb(); /* Ensure that we see the request before we copy it. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001299 memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001300
1301 /* Credit-based scheduling. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001302 if (txreq.size > queue->remaining_credit &&
1303 tx_credit_exceeded(queue, txreq.size))
Wei Liub3f980b2013-08-26 12:59:38 +01001304 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001305
Wei Liue9ce7cb2014-06-04 10:30:42 +01001306 queue->remaining_credit -= txreq.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001307
1308 work_to_do--;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001309 queue->tx.req_cons = ++idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001310
1311 memset(extras, 0, sizeof(extras));
1312 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001313 work_to_do = xenvif_get_extras(queue, extras,
Wei Liu73764192013-08-26 12:59:39 +01001314 work_to_do);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001315 idx = queue->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001316 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001317 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001318 }
1319
Wei Liue9ce7cb2014-06-04 10:30:42 +01001320 ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001321 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001322 break;
Ian Campbell488562862013-02-06 23:41:35 +00001323
Ian Campbellf942dc22011-03-15 00:06:18 +00001324 idx += ret;
1325
1326 if (unlikely(txreq.size < ETH_HLEN)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001327 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001328 "Bad packet size: %d\n", txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001329 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001330 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001331 }
1332
1333 /* No crossing a page as the payload mustn't fragment. */
1334 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001335 netdev_err(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001336 "txreq.offset: %x, size: %u, end: %lu\n",
1337 txreq.offset, txreq.size,
1338 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001339 xenvif_fatal_tx_err(queue->vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001340 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001341 }
1342
Wei Liue9ce7cb2014-06-04 10:30:42 +01001343 index = pending_index(queue->pending_cons);
1344 pending_idx = queue->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001345
1346 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001347 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001348 PKT_PROT_LEN : txreq.size;
1349
Zoltan Kisse3377f32014-03-06 21:48:29 +00001350 skb = xenvif_alloc_skb(data_len);
Ian Campbellf942dc22011-03-15 00:06:18 +00001351 if (unlikely(skb == NULL)) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001352 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001353 "Can't allocate a skb in start_xmit.\n");
Wei Liue9ce7cb2014-06-04 10:30:42 +01001354 xenvif_tx_err(queue, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001355 break;
1356 }
1357
Ian Campbellf942dc22011-03-15 00:06:18 +00001358 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1359 struct xen_netif_extra_info *gso;
1360 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1361
Wei Liue9ce7cb2014-06-04 10:30:42 +01001362 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
Wei Liu73764192013-08-26 12:59:39 +01001363 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001364 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001365 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001366 }
1367 }
1368
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001369 XENVIF_TX_CB(skb)->pending_idx = pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001370
1371 __skb_put(skb, data_len);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001372 queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
1373 queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
1374 queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001375
Wei Liue9ce7cb2014-06-04 10:30:42 +01001376 queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001377 virt_to_mfn(skb->data);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001378 queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
1379 queue->tx_copy_ops[*copy_ops].dest.offset =
Zoltan Kissbdab8272014-04-02 18:04:58 +01001380 offset_in_page(skb->data);
1381
Wei Liue9ce7cb2014-06-04 10:30:42 +01001382 queue->tx_copy_ops[*copy_ops].len = data_len;
1383 queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001384
1385 (*copy_ops)++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001386
1387 skb_shinfo(skb)->nr_frags = ret;
1388 if (data_len < txreq.size) {
1389 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001390 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1391 pending_idx);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001392 xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001393 gop++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001394 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001395 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1396 INVALID_PENDING_IDX);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001397 memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001398 sizeof(txreq));
Ian Campbellf942dc22011-03-15 00:06:18 +00001399 }
1400
Wei Liue9ce7cb2014-06-04 10:30:42 +01001401 queue->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001402
Wei Liue9ce7cb2014-06-04 10:30:42 +01001403 request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001404 if (request_gop == NULL) {
1405 kfree_skb(skb);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001406 xenvif_tx_err(queue, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001407 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001408 }
1409 gop = request_gop;
1410
Wei Liue9ce7cb2014-06-04 10:30:42 +01001411 __skb_queue_tail(&queue->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001412
Wei Liue9ce7cb2014-06-04 10:30:42 +01001413 queue->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001414
Wei Liue9ce7cb2014-06-04 10:30:42 +01001415 if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
1416 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001417 break;
1418 }
1419
Wei Liue9ce7cb2014-06-04 10:30:42 +01001420 (*map_ops) = gop - queue->tx_map_ops;
Zoltan Kissbdab8272014-04-02 18:04:58 +01001421 return;
Ian Campbellf942dc22011-03-15 00:06:18 +00001422}
1423
Zoltan Kisse3377f32014-03-06 21:48:29 +00001424/* Consolidate skb with a frag_list into a brand new one with local pages on
1425 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1426 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001427static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
Zoltan Kisse3377f32014-03-06 21:48:29 +00001428{
1429 unsigned int offset = skb_headlen(skb);
1430 skb_frag_t frags[MAX_SKB_FRAGS];
1431 int i;
1432 struct ubuf_info *uarg;
1433 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1434
Wei Liue9ce7cb2014-06-04 10:30:42 +01001435 queue->stats.tx_zerocopy_sent += 2;
1436 queue->stats.tx_frag_overflow++;
Zoltan Kisse3377f32014-03-06 21:48:29 +00001437
Wei Liue9ce7cb2014-06-04 10:30:42 +01001438 xenvif_fill_frags(queue, nskb);
Zoltan Kisse3377f32014-03-06 21:48:29 +00001439 /* Subtract frags size, we will correct it later */
1440 skb->truesize -= skb->data_len;
1441 skb->len += nskb->len;
1442 skb->data_len += nskb->len;
1443
1444 /* create a brand new frags array and coalesce there */
1445 for (i = 0; offset < skb->len; i++) {
1446 struct page *page;
1447 unsigned int len;
1448
1449 BUG_ON(i >= MAX_SKB_FRAGS);
1450 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
1451 if (!page) {
1452 int j;
1453 skb->truesize += skb->data_len;
1454 for (j = 0; j < i; j++)
1455 put_page(frags[j].page.p);
1456 return -ENOMEM;
1457 }
1458
1459 if (offset + PAGE_SIZE < skb->len)
1460 len = PAGE_SIZE;
1461 else
1462 len = skb->len - offset;
1463 if (skb_copy_bits(skb, offset, page_address(page), len))
1464 BUG();
1465
1466 offset += len;
1467 frags[i].page.p = page;
1468 frags[i].page_offset = 0;
1469 skb_frag_size_set(&frags[i], len);
1470 }
1471 /* swap out with old one */
1472 memcpy(skb_shinfo(skb)->frags,
1473 frags,
1474 i * sizeof(skb_frag_t));
1475 skb_shinfo(skb)->nr_frags = i;
1476 skb->truesize += i * PAGE_SIZE;
1477
1478 /* remove traces of mapped pages and frag_list */
1479 skb_frag_list_init(skb);
1480 uarg = skb_shinfo(skb)->destructor_arg;
1481 uarg->callback(uarg, true);
1482 skb_shinfo(skb)->destructor_arg = NULL;
1483
1484 skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1485 kfree_skb(nskb);
1486
1487 return 0;
1488}
Ian Campbellf942dc22011-03-15 00:06:18 +00001489
Wei Liue9ce7cb2014-06-04 10:30:42 +01001490static int xenvif_tx_submit(struct xenvif_queue *queue)
Wei Liub3f980b2013-08-26 12:59:38 +01001491{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001492 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1493 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
Wei Liub3f980b2013-08-26 12:59:38 +01001494 struct sk_buff *skb;
1495 int work_done = 0;
1496
Wei Liue9ce7cb2014-06-04 10:30:42 +01001497 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001498 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001499 u16 pending_idx;
1500 unsigned data_len;
1501
Zoltan Kiss8f13dd92014-03-06 21:48:23 +00001502 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001503 txp = &queue->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001504
1505 /* Check the remap error code. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001506 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001507 skb_shinfo(skb)->nr_frags = 0;
1508 kfree_skb(skb);
1509 continue;
1510 }
1511
1512 data_len = skb->len;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001513 callback_param(queue, pending_idx).ctx = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001514 if (data_len < txp->size) {
1515 /* Append the packet payload as a fragment. */
1516 txp->offset += data_len;
1517 txp->size -= data_len;
1518 } else {
1519 /* Schedule a response immediately. */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001520 xenvif_idx_release(queue, pending_idx,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001521 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001522 }
1523
1524 if (txp->flags & XEN_NETTXF_csum_blank)
1525 skb->ip_summed = CHECKSUM_PARTIAL;
1526 else if (txp->flags & XEN_NETTXF_data_validated)
1527 skb->ip_summed = CHECKSUM_UNNECESSARY;
1528
Wei Liue9ce7cb2014-06-04 10:30:42 +01001529 xenvif_fill_frags(queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001530
Zoltan Kisse3377f32014-03-06 21:48:29 +00001531 if (unlikely(skb_has_frag_list(skb))) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001532 if (xenvif_handle_frag_list(queue, skb)) {
Zoltan Kisse3377f32014-03-06 21:48:29 +00001533 if (net_ratelimit())
Wei Liue9ce7cb2014-06-04 10:30:42 +01001534 netdev_err(queue->vif->dev,
Zoltan Kisse3377f32014-03-06 21:48:29 +00001535 "Not enough memory to consolidate frag_list!\n");
1536 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
1537 kfree_skb(skb);
1538 continue;
1539 }
1540 }
1541
Paul Durrant2eba61d2013-10-16 17:50:29 +01001542 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001543 int target = min_t(int, skb->len, PKT_PROT_LEN);
1544 __pskb_pull_tail(skb, target - skb_headlen(skb));
1545 }
1546
Wei Liue9ce7cb2014-06-04 10:30:42 +01001547 skb->dev = queue->vif->dev;
Ian Campbellf942dc22011-03-15 00:06:18 +00001548 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001549 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001550
Wei Liue9ce7cb2014-06-04 10:30:42 +01001551 if (checksum_setup(queue, skb)) {
1552 netdev_dbg(queue->vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001553 "Can't setup checksum in net_tx_action\n");
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001554 /* We have to set this flag to trigger the callback */
1555 if (skb_shinfo(skb)->destructor_arg)
1556 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Ian Campbellf942dc22011-03-15 00:06:18 +00001557 kfree_skb(skb);
1558 continue;
1559 }
1560
Jason Wang40893fd2013-03-26 23:11:22 +00001561 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001562
Paul Durrantb89587a2013-12-17 11:44:35 +00001563 /* If the packet is GSO then we will have just set up the
1564 * transport header offset in checksum_setup so it's now
1565 * straightforward to calculate gso_segs.
1566 */
1567 if (skb_is_gso(skb)) {
1568 int mss = skb_shinfo(skb)->gso_size;
1569 int hdrlen = skb_transport_header(skb) -
1570 skb_mac_header(skb) +
1571 tcp_hdrlen(skb);
1572
1573 skb_shinfo(skb)->gso_segs =
1574 DIV_ROUND_UP(skb->len - hdrlen, mss);
1575 }
1576
Wei Liue9ce7cb2014-06-04 10:30:42 +01001577 queue->stats.rx_bytes += skb->len;
1578 queue->stats.rx_packets++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001579
Wei Liub3f980b2013-08-26 12:59:38 +01001580 work_done++;
1581
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001582 /* Set this flag right before netif_receive_skb, otherwise
1583 * someone might think this packet already left netback, and
1584 * do a skb_copy_ubufs while we are still in control of the
1585 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1586 */
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001587 if (skb_shinfo(skb)->destructor_arg) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001588 skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001589 queue->stats.tx_zerocopy_sent++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001590 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001591
Wei Liub3f980b2013-08-26 12:59:38 +01001592 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001593 }
Wei Liub3f980b2013-08-26 12:59:38 +01001594
1595 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001596}
1597
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001598void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1599{
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001600 unsigned long flags;
1601 pending_ring_idx_t index;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001602 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001603
1604 /* This is the only place where we grab this lock, to protect callbacks
1605 * from each other.
1606 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001607 spin_lock_irqsave(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001608 do {
1609 u16 pending_idx = ubuf->desc;
1610 ubuf = (struct ubuf_info *) ubuf->ctx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001611 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001612 MAX_PENDING_REQS);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001613 index = pending_index(queue->dealloc_prod);
1614 queue->dealloc_ring[index] = pending_idx;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001615 /* Sync with xenvif_tx_dealloc_action:
1616 * insert idx then incr producer.
1617 */
1618 smp_wmb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001619 queue->dealloc_prod++;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001620 } while (ubuf);
Wei Liue9ce7cb2014-06-04 10:30:42 +01001621 wake_up(&queue->dealloc_wq);
1622 spin_unlock_irqrestore(&queue->callback_lock, flags);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001623
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001624 if (likely(zerocopy_success))
Wei Liue9ce7cb2014-06-04 10:30:42 +01001625 queue->stats.tx_zerocopy_success++;
Zoltan Kiss1bb332a2014-03-06 21:48:28 +00001626 else
Wei Liue9ce7cb2014-06-04 10:30:42 +01001627 queue->stats.tx_zerocopy_fail++;
Zoltan Kiss3e2234b2014-03-06 21:48:25 +00001628}
1629
Wei Liue9ce7cb2014-06-04 10:30:42 +01001630static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001631{
1632 struct gnttab_unmap_grant_ref *gop;
1633 pending_ring_idx_t dc, dp;
1634 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1635 unsigned int i = 0;
1636
Wei Liue9ce7cb2014-06-04 10:30:42 +01001637 dc = queue->dealloc_cons;
1638 gop = queue->tx_unmap_ops;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001639
1640 /* Free up any grants we have finished using */
1641 do {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001642 dp = queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001643
1644 /* Ensure we see all indices enqueued by all
1645 * xenvif_zerocopy_callback().
1646 */
1647 smp_rmb();
1648
1649 while (dc != dp) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001650 BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001651 pending_idx =
Wei Liue9ce7cb2014-06-04 10:30:42 +01001652 queue->dealloc_ring[pending_index(dc++)];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001653
Wei Liue9ce7cb2014-06-04 10:30:42 +01001654 pending_idx_release[gop-queue->tx_unmap_ops] =
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001655 pending_idx;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001656 queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
1657 queue->mmap_pages[pending_idx];
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001658 gnttab_set_unmap_op(gop,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001659 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001660 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001661 queue->grant_tx_handle[pending_idx]);
1662 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001663 ++gop;
1664 }
1665
Wei Liue9ce7cb2014-06-04 10:30:42 +01001666 } while (dp != queue->dealloc_prod);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001667
Wei Liue9ce7cb2014-06-04 10:30:42 +01001668 queue->dealloc_cons = dc;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001669
Wei Liue9ce7cb2014-06-04 10:30:42 +01001670 if (gop - queue->tx_unmap_ops > 0) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001671 int ret;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001672 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001673 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001674 queue->pages_to_unmap,
1675 gop - queue->tx_unmap_ops);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001676 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001677 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
1678 gop - queue->tx_unmap_ops, ret);
1679 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001680 if (gop[i].status != GNTST_okay)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001681 netdev_err(queue->vif->dev,
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001682 " host_addr: %llx handle: %x status: %d\n",
1683 gop[i].host_addr,
1684 gop[i].handle,
1685 gop[i].status);
1686 }
1687 BUG();
1688 }
1689 }
1690
Wei Liue9ce7cb2014-06-04 10:30:42 +01001691 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1692 xenvif_idx_release(queue, pending_idx_release[i],
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001693 XEN_NETIF_RSP_OKAY);
1694}
1695
1696
Ian Campbellf942dc22011-03-15 00:06:18 +00001697/* Called after netfront has transmitted */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001698int xenvif_tx_action(struct xenvif_queue *queue, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001699{
Zoltan Kissbdab8272014-04-02 18:04:58 +01001700 unsigned nr_mops, nr_cops = 0;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001701 int work_done, ret;
Ian Campbellf942dc22011-03-15 00:06:18 +00001702
Wei Liue9ce7cb2014-06-04 10:30:42 +01001703 if (unlikely(!tx_work_todo(queue)))
Wei Liub3f980b2013-08-26 12:59:38 +01001704 return 0;
1705
Wei Liue9ce7cb2014-06-04 10:30:42 +01001706 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001707
Zoltan Kissbdab8272014-04-02 18:04:58 +01001708 if (nr_cops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001709 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001710
Wei Liue9ce7cb2014-06-04 10:30:42 +01001711 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
Zoltan Kissbdab8272014-04-02 18:04:58 +01001712 if (nr_mops != 0) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001713 ret = gnttab_map_refs(queue->tx_map_ops,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001714 NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001715 queue->pages_to_map,
Zoltan Kissbdab8272014-04-02 18:04:58 +01001716 nr_mops);
1717 BUG_ON(ret);
1718 }
Ian Campbellf942dc22011-03-15 00:06:18 +00001719
Wei Liue9ce7cb2014-06-04 10:30:42 +01001720 work_done = xenvif_tx_submit(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001721
1722 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001723}
1724
Wei Liue9ce7cb2014-06-04 10:30:42 +01001725static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
Wei Liu73764192013-08-26 12:59:39 +01001726 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001727{
Ian Campbellf942dc22011-03-15 00:06:18 +00001728 struct pending_tx_info *pending_tx_info;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001729 pending_ring_idx_t index;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001730 unsigned long flags;
Wei Liu2810e5b2013-04-22 02:20:42 +00001731
Wei Liue9ce7cb2014-06-04 10:30:42 +01001732 pending_tx_info = &queue->pending_tx_info[pending_idx];
1733 spin_lock_irqsave(&queue->response_lock, flags);
1734 make_tx_response(queue, &pending_tx_info->req, status);
1735 index = pending_index(queue->pending_prod);
1736 queue->pending_ring[index] = pending_idx;
Zoltan Kiss62bad312014-03-06 21:48:27 +00001737 /* TX shouldn't use the index before we give it back here */
1738 mb();
Wei Liue9ce7cb2014-06-04 10:30:42 +01001739 queue->pending_prod++;
1740 spin_unlock_irqrestore(&queue->response_lock, flags);
Ian Campbellf942dc22011-03-15 00:06:18 +00001741}
1742
Wei Liu2810e5b2013-04-22 02:20:42 +00001743
Wei Liue9ce7cb2014-06-04 10:30:42 +01001744static void make_tx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001745 struct xen_netif_tx_request *txp,
1746 s8 st)
1747{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001748 RING_IDX i = queue->tx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001749 struct xen_netif_tx_response *resp;
1750 int notify;
1751
Wei Liue9ce7cb2014-06-04 10:30:42 +01001752 resp = RING_GET_RESPONSE(&queue->tx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001753 resp->id = txp->id;
1754 resp->status = st;
1755
1756 if (txp->flags & XEN_NETTXF_extra_info)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001757 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001758
Wei Liue9ce7cb2014-06-04 10:30:42 +01001759 queue->tx.rsp_prod_pvt = ++i;
1760 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
Ian Campbellf942dc22011-03-15 00:06:18 +00001761 if (notify)
Wei Liue9ce7cb2014-06-04 10:30:42 +01001762 notify_remote_via_irq(queue->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001763}
1764
Wei Liue9ce7cb2014-06-04 10:30:42 +01001765static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
Ian Campbellf942dc22011-03-15 00:06:18 +00001766 u16 id,
1767 s8 st,
1768 u16 offset,
1769 u16 size,
1770 u16 flags)
1771{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001772 RING_IDX i = queue->rx.rsp_prod_pvt;
Ian Campbellf942dc22011-03-15 00:06:18 +00001773 struct xen_netif_rx_response *resp;
1774
Wei Liue9ce7cb2014-06-04 10:30:42 +01001775 resp = RING_GET_RESPONSE(&queue->rx, i);
Ian Campbellf942dc22011-03-15 00:06:18 +00001776 resp->offset = offset;
1777 resp->flags = flags;
1778 resp->id = id;
1779 resp->status = (s16)size;
1780 if (st < 0)
1781 resp->status = (s16)st;
1782
Wei Liue9ce7cb2014-06-04 10:30:42 +01001783 queue->rx.rsp_prod_pvt = ++i;
Ian Campbellf942dc22011-03-15 00:06:18 +00001784
1785 return resp;
1786}
1787
Wei Liue9ce7cb2014-06-04 10:30:42 +01001788void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001789{
1790 int ret;
1791 struct gnttab_unmap_grant_ref tx_unmap_op;
1792
1793 gnttab_set_unmap_op(&tx_unmap_op,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001794 idx_to_kaddr(queue, pending_idx),
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001795 GNTMAP_host_map,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001796 queue->grant_tx_handle[pending_idx]);
1797 xenvif_grant_handle_reset(queue, pending_idx);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001798
1799 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
Wei Liue9ce7cb2014-06-04 10:30:42 +01001800 &queue->mmap_pages[pending_idx], 1);
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001801 if (ret) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001802 netdev_err(queue->vif->dev,
Zoltan Kiss7aceb472014-03-24 23:59:51 +00001803 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
1804 ret,
1805 pending_idx,
1806 tx_unmap_op.host_addr,
1807 tx_unmap_op.handle,
1808 tx_unmap_op.status);
1809 BUG();
1810 }
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001811
Wei Liue9ce7cb2014-06-04 10:30:42 +01001812 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_OKAY);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001813}
1814
Wei Liue9ce7cb2014-06-04 10:30:42 +01001815static inline int rx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001816{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001817 return (!skb_queue_empty(&queue->rx_queue) &&
1818 xenvif_rx_ring_slots_available(queue, queue->rx_last_skb_slots)) ||
1819 queue->rx_queue_purge;
Ian Campbellf942dc22011-03-15 00:06:18 +00001820}
1821
Wei Liue9ce7cb2014-06-04 10:30:42 +01001822static inline int tx_work_todo(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001823{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001824 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
Ian Campbellf942dc22011-03-15 00:06:18 +00001825 return 1;
1826
1827 return 0;
1828}
1829
Wei Liue9ce7cb2014-06-04 10:30:42 +01001830static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001831{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001832 return queue->dealloc_cons != queue->dealloc_prod;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001833}
1834
Wei Liue9ce7cb2014-06-04 10:30:42 +01001835void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
Ian Campbellf942dc22011-03-15 00:06:18 +00001836{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001837 if (queue->tx.sring)
1838 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1839 queue->tx.sring);
1840 if (queue->rx.sring)
1841 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1842 queue->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001843}
1844
Wei Liue9ce7cb2014-06-04 10:30:42 +01001845int xenvif_map_frontend_rings(struct xenvif_queue *queue,
Wei Liu73764192013-08-26 12:59:39 +01001846 grant_ref_t tx_ring_ref,
1847 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001848{
David Vrabelc9d63692011-09-29 16:53:31 +01001849 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001850 struct xen_netif_tx_sring *txs;
1851 struct xen_netif_rx_sring *rxs;
1852
1853 int err = -ENOMEM;
1854
Wei Liue9ce7cb2014-06-04 10:30:42 +01001855 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001856 tx_ring_ref, &addr);
1857 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001858 goto err;
1859
David Vrabelc9d63692011-09-29 16:53:31 +01001860 txs = (struct xen_netif_tx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001861 BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001862
Wei Liue9ce7cb2014-06-04 10:30:42 +01001863 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
David Vrabelc9d63692011-09-29 16:53:31 +01001864 rx_ring_ref, &addr);
1865 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001866 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001867
David Vrabelc9d63692011-09-29 16:53:31 +01001868 rxs = (struct xen_netif_rx_sring *)addr;
Wei Liue9ce7cb2014-06-04 10:30:42 +01001869 BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +00001870
1871 return 0;
1872
1873err:
Wei Liue9ce7cb2014-06-04 10:30:42 +01001874 xenvif_unmap_frontend_rings(queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001875 return err;
1876}
1877
Wei Liue9ce7cb2014-06-04 10:30:42 +01001878static void xenvif_start_queue(struct xenvif_queue *queue)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001879{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001880 if (xenvif_schedulable(queue->vif))
1881 xenvif_wake_queue(queue);
Paul Durrantca2f09f2013-12-06 16:36:07 +00001882}
1883
Zoltan Kiss121fa4b2014-03-06 21:48:24 +00001884int xenvif_kthread_guest_rx(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001885{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001886 struct xenvif_queue *queue = data;
Paul Durrantca2f09f2013-12-06 16:36:07 +00001887 struct sk_buff *skb;
Wei Liub3f980b2013-08-26 12:59:38 +01001888
1889 while (!kthread_should_stop()) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001890 wait_event_interruptible(queue->wq,
1891 rx_work_todo(queue) ||
1892 queue->vif->disabled ||
Wei Liub3f980b2013-08-26 12:59:38 +01001893 kthread_should_stop());
Wei Liue9d8b2c2014-04-01 12:46:12 +01001894
1895 /* This frontend is found to be rogue, disable it in
1896 * kthread context. Currently this is only set when
1897 * netback finds out frontend sends malformed packet,
1898 * but we cannot disable the interface in softirq
Wei Liue9ce7cb2014-06-04 10:30:42 +01001899 * context so we defer it here, if this thread is
1900 * associated with queue 0.
Wei Liue9d8b2c2014-04-01 12:46:12 +01001901 */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001902 if (unlikely(queue->vif->disabled && netif_carrier_ok(queue->vif->dev) && queue->id == 0))
1903 xenvif_carrier_off(queue->vif);
Wei Liue9d8b2c2014-04-01 12:46:12 +01001904
Wei Liub3f980b2013-08-26 12:59:38 +01001905 if (kthread_should_stop())
1906 break;
1907
Wei Liue9ce7cb2014-06-04 10:30:42 +01001908 if (queue->rx_queue_purge) {
1909 skb_queue_purge(&queue->rx_queue);
1910 queue->rx_queue_purge = false;
Zoltan Kiss09350782014-03-06 21:48:30 +00001911 }
1912
Wei Liue9ce7cb2014-06-04 10:30:42 +01001913 if (!skb_queue_empty(&queue->rx_queue))
1914 xenvif_rx_action(queue);
Wei Liub3f980b2013-08-26 12:59:38 +01001915
Wei Liue9ce7cb2014-06-04 10:30:42 +01001916 if (skb_queue_empty(&queue->rx_queue) &&
1917 xenvif_queue_stopped(queue)) {
1918 del_timer_sync(&queue->wake_queue);
1919 xenvif_start_queue(queue);
Zoltan Kiss09350782014-03-06 21:48:30 +00001920 }
Paul Durrantca2f09f2013-12-06 16:36:07 +00001921
Wei Liub3f980b2013-08-26 12:59:38 +01001922 cond_resched();
1923 }
1924
Paul Durrantca2f09f2013-12-06 16:36:07 +00001925 /* Bin any remaining skbs */
Wei Liue9ce7cb2014-06-04 10:30:42 +01001926 while ((skb = skb_dequeue(&queue->rx_queue)) != NULL)
Paul Durrantca2f09f2013-12-06 16:36:07 +00001927 dev_kfree_skb(skb);
1928
Wei Liub3f980b2013-08-26 12:59:38 +01001929 return 0;
1930}
1931
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001932int xenvif_dealloc_kthread(void *data)
1933{
Wei Liue9ce7cb2014-06-04 10:30:42 +01001934 struct xenvif_queue *queue = data;
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001935
1936 while (!kthread_should_stop()) {
Wei Liue9ce7cb2014-06-04 10:30:42 +01001937 wait_event_interruptible(queue->dealloc_wq,
1938 tx_dealloc_work_todo(queue) ||
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001939 kthread_should_stop());
1940 if (kthread_should_stop())
1941 break;
1942
Wei Liue9ce7cb2014-06-04 10:30:42 +01001943 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001944 cond_resched();
1945 }
1946
1947 /* Unmap anything remaining*/
Wei Liue9ce7cb2014-06-04 10:30:42 +01001948 if (tx_dealloc_work_todo(queue))
1949 xenvif_tx_dealloc_action(queue);
Zoltan Kissf53c3fe2014-03-06 21:48:26 +00001950
1951 return 0;
1952}
1953
Ian Campbellf942dc22011-03-15 00:06:18 +00001954static int __init netback_init(void)
1955{
Ian Campbellf942dc22011-03-15 00:06:18 +00001956 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001957
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001958 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001959 return -ENODEV;
1960
Andrew J. Bennieston8d3d53b2014-06-04 10:30:43 +01001961 /* Allow as many queues as there are CPUs, by default */
1962 xenvif_max_queues = num_online_cpus();
1963
Wei Liu37641492013-05-02 00:43:59 +00001964 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001965 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1966 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001967 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001968 }
1969
Ian Campbellf942dc22011-03-15 00:06:18 +00001970 rc = xenvif_xenbus_init();
1971 if (rc)
1972 goto failed_init;
1973
Zoltan Kiss09350782014-03-06 21:48:30 +00001974 rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
1975
Ian Campbellf942dc22011-03-15 00:06:18 +00001976 return 0;
1977
1978failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001979 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001980}
1981
1982module_init(netback_init);
1983
Wei Liub103f352013-05-16 23:26:11 +00001984static void __exit netback_fini(void)
1985{
Wei Liub103f352013-05-16 23:26:11 +00001986 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001987}
1988module_exit(netback_fini);
1989
Ian Campbellf942dc22011-03-15 00:06:18 +00001990MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001991MODULE_ALIAS("xen-backend:vif");