blob: 0e327d46a139e452cfabf9f9deaadfc70406c5c8 [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>
40
41#include <net/tcp.h>
42
Stefano Stabellinica981632012-08-08 17:21:23 +000043#include <xen/xen.h>
Ian Campbellf942dc22011-03-15 00:06:18 +000044#include <xen/events.h>
45#include <xen/interface/memory.h>
46
47#include <asm/xen/hypercall.h>
48#include <asm/xen/page.h>
49
Wei Liue1f00a692013-05-22 06:34:45 +000050/* Provide an option to disable split event channels at load time as
51 * event channels are limited resource. Split event channels are
52 * enabled by default.
53 */
54bool separate_tx_rx_irq = 1;
55module_param(separate_tx_rx_irq, bool, 0644);
56
Wei Liu2810e5b2013-04-22 02:20:42 +000057/*
58 * This is the maximum slots a skb can have. If a guest sends a skb
59 * which exceeds this limit it is considered malicious.
60 */
Wei Liu37641492013-05-02 00:43:59 +000061#define FATAL_SKB_SLOTS_DEFAULT 20
62static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
63module_param(fatal_skb_slots, uint, 0444);
64
65/*
66 * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating
67 * the maximum slots a valid packet can use. Now this value is defined
68 * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by
69 * all backend.
70 */
71#define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN
Wei Liu2810e5b2013-04-22 02:20:42 +000072
Wei Liu2810e5b2013-04-22 02:20:42 +000073/*
74 * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
75 * one or more merged tx requests, otherwise it is the continuation of
76 * previous tx request.
77 */
Wei Liub3f980b2013-08-26 12:59:38 +010078static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
Wei Liu2810e5b2013-04-22 02:20:42 +000079{
Wei Liub3f980b2013-08-26 12:59:38 +010080 return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
Wei Liu2810e5b2013-04-22 02:20:42 +000081}
82
Wei Liu73764192013-08-26 12:59:39 +010083static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
84 u8 status);
85
Ian Campbellf942dc22011-03-15 00:06:18 +000086static void make_tx_response(struct xenvif *vif,
87 struct xen_netif_tx_request *txp,
88 s8 st);
Wei Liub3f980b2013-08-26 12:59:38 +010089
90static inline int tx_work_todo(struct xenvif *vif);
91static inline int rx_work_todo(struct xenvif *vif);
92
Ian Campbellf942dc22011-03-15 00:06:18 +000093static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
94 u16 id,
95 s8 st,
96 u16 offset,
97 u16 size,
98 u16 flags);
99
Wei Liub3f980b2013-08-26 12:59:38 +0100100static inline unsigned long idx_to_pfn(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000101 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000102{
Wei Liub3f980b2013-08-26 12:59:38 +0100103 return page_to_pfn(vif->mmap_pages[idx]);
Ian Campbellf942dc22011-03-15 00:06:18 +0000104}
105
Wei Liub3f980b2013-08-26 12:59:38 +0100106static inline unsigned long idx_to_kaddr(struct xenvif *vif,
Ian Campbellea066ad2011-10-05 00:28:46 +0000107 u16 idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000108{
Wei Liub3f980b2013-08-26 12:59:38 +0100109 return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
Ian Campbellf942dc22011-03-15 00:06:18 +0000110}
111
Paul Durrant2eba61d2013-10-16 17:50:29 +0100112/* This is a miniumum size for the linear area to avoid lots of
113 * calls to __pskb_pull_tail() as we set up checksum offsets. The
114 * value 128 was chosen as it covers all IPv4 and most likely
115 * IPv6 headers.
Ian Campbellf942dc22011-03-15 00:06:18 +0000116 */
Paul Durrant2eba61d2013-10-16 17:50:29 +0100117#define PKT_PROT_LEN 128
Ian Campbellf942dc22011-03-15 00:06:18 +0000118
Ian Campbellea066ad2011-10-05 00:28:46 +0000119static u16 frag_get_pending_idx(skb_frag_t *frag)
120{
121 return (u16)frag->page_offset;
122}
123
124static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
125{
126 frag->page_offset = pending_idx;
127}
128
Ian Campbellf942dc22011-03-15 00:06:18 +0000129static inline pending_ring_idx_t pending_index(unsigned i)
130{
131 return i & (MAX_PENDING_REQS-1);
132}
133
Wei Liub3f980b2013-08-26 12:59:38 +0100134static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000135{
136 return MAX_PENDING_REQS -
Wei Liub3f980b2013-08-26 12:59:38 +0100137 vif->pending_prod + vif->pending_cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000138}
139
140static int max_required_rx_slots(struct xenvif *vif)
141{
142 int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE);
143
Wei Liu2810e5b2013-04-22 02:20:42 +0000144 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
Ian Campbellf942dc22011-03-15 00:06:18 +0000145 if (vif->can_sg || vif->gso || vif->gso_prefix)
146 max += MAX_SKB_FRAGS + 1; /* extra_info + frags */
147
148 return max;
149}
150
Wei Liu73764192013-08-26 12:59:39 +0100151int xenvif_rx_ring_full(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000152{
153 RING_IDX peek = vif->rx_req_cons_peek;
154 RING_IDX needed = max_required_rx_slots(vif);
155
156 return ((vif->rx.sring->req_prod - peek) < needed) ||
157 ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed);
158}
159
Wei Liu73764192013-08-26 12:59:39 +0100160int xenvif_must_stop_queue(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000161{
Wei Liu73764192013-08-26 12:59:39 +0100162 if (!xenvif_rx_ring_full(vif))
Ian Campbellf942dc22011-03-15 00:06:18 +0000163 return 0;
164
165 vif->rx.sring->req_event = vif->rx_req_cons_peek +
166 max_required_rx_slots(vif);
167 mb(); /* request notification /then/ check the queue */
168
Wei Liu73764192013-08-26 12:59:39 +0100169 return xenvif_rx_ring_full(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000170}
171
172/*
173 * Returns true if we should start a new receive buffer instead of
174 * adding 'size' bytes to a buffer which currently contains 'offset'
175 * bytes.
176 */
177static bool start_new_rx_buffer(int offset, unsigned long size, int head)
178{
179 /* simple case: we have completely filled the current buffer. */
180 if (offset == MAX_BUFFER_OFFSET)
181 return true;
182
183 /*
184 * complex case: start a fresh buffer if the current frag
185 * would overflow the current buffer but only if:
186 * (i) this frag would fit completely in the next buffer
187 * and (ii) there is already some data in the current buffer
188 * and (iii) this is not the head buffer.
189 *
190 * Where:
191 * - (i) stops us splitting a frag into two copies
192 * unless the frag is too large for a single buffer.
193 * - (ii) stops us from leaving a buffer pointlessly empty.
194 * - (iii) stops us leaving the first buffer
195 * empty. Strictly speaking this is already covered
196 * by (ii) but is explicitly checked because
197 * netfront relies on the first buffer being
198 * non-empty and can crash otherwise.
199 *
200 * This means we will effectively linearise small
201 * frags but do not needlessly split large buffers
202 * into multiple copies tend to give large frags their
203 * own buffers as before.
204 */
205 if ((offset + size > MAX_BUFFER_OFFSET) &&
206 (size <= MAX_BUFFER_OFFSET) && offset && !head)
207 return true;
208
209 return false;
210}
211
Wei Liu33bc8012013-10-08 10:54:21 +0100212struct xenvif_count_slot_state {
213 unsigned long copy_off;
214 bool head;
215};
216
217unsigned int xenvif_count_frag_slots(struct xenvif *vif,
218 unsigned long offset, unsigned long size,
219 struct xenvif_count_slot_state *state)
220{
221 unsigned count = 0;
222
223 offset &= ~PAGE_MASK;
224
225 while (size > 0) {
226 unsigned long bytes;
227
228 bytes = PAGE_SIZE - offset;
229
230 if (bytes > size)
231 bytes = size;
232
233 if (start_new_rx_buffer(state->copy_off, bytes, state->head)) {
234 count++;
235 state->copy_off = 0;
236 }
237
238 if (state->copy_off + bytes > MAX_BUFFER_OFFSET)
239 bytes = MAX_BUFFER_OFFSET - state->copy_off;
240
241 state->copy_off += bytes;
242
243 offset += bytes;
244 size -= bytes;
245
246 if (offset == PAGE_SIZE)
247 offset = 0;
248
249 state->head = false;
250 }
251
252 return count;
253}
254
Ian Campbellf942dc22011-03-15 00:06:18 +0000255/*
256 * Figure out how many ring slots we're going to need to send @skb to
257 * the guest. This function is essentially a dry run of
Wei Liu73764192013-08-26 12:59:39 +0100258 * xenvif_gop_frag_copy.
Ian Campbellf942dc22011-03-15 00:06:18 +0000259 */
Wei Liu73764192013-08-26 12:59:39 +0100260unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000261{
Wei Liu33bc8012013-10-08 10:54:21 +0100262 struct xenvif_count_slot_state state;
Ian Campbellf942dc22011-03-15 00:06:18 +0000263 unsigned int count;
Wei Liu33bc8012013-10-08 10:54:21 +0100264 unsigned char *data;
265 unsigned i;
Ian Campbellf942dc22011-03-15 00:06:18 +0000266
Wei Liu33bc8012013-10-08 10:54:21 +0100267 state.head = true;
268 state.copy_off = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000269
Wei Liu33bc8012013-10-08 10:54:21 +0100270 /* Slot for the first (partial) page of data. */
271 count = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000272
Wei Liu33bc8012013-10-08 10:54:21 +0100273 /* Need a slot for the GSO prefix for GSO extra data? */
Ian Campbellf942dc22011-03-15 00:06:18 +0000274 if (skb_shinfo(skb)->gso_size)
275 count++;
276
Wei Liu33bc8012013-10-08 10:54:21 +0100277 data = skb->data;
278 while (data < skb_tail_pointer(skb)) {
279 unsigned long offset = offset_in_page(data);
280 unsigned long size = PAGE_SIZE - offset;
281
282 if (data + size > skb_tail_pointer(skb))
283 size = skb_tail_pointer(skb) - data;
284
285 count += xenvif_count_frag_slots(vif, offset, size, &state);
286
287 data += size;
288 }
289
Ian Campbellf942dc22011-03-15 00:06:18 +0000290 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000291 unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
Ian Campbell6a8ed462012-10-10 03:48:42 +0000292 unsigned long offset = skb_shinfo(skb)->frags[i].page_offset;
Ian Campbell6a8ed462012-10-10 03:48:42 +0000293
Wei Liu33bc8012013-10-08 10:54:21 +0100294 count += xenvif_count_frag_slots(vif, offset, size, &state);
Ian Campbellf942dc22011-03-15 00:06:18 +0000295 }
296 return count;
297}
298
299struct netrx_pending_operations {
300 unsigned copy_prod, copy_cons;
301 unsigned meta_prod, meta_cons;
302 struct gnttab_copy *copy;
Wei Liub3f980b2013-08-26 12:59:38 +0100303 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000304 int copy_off;
305 grant_ref_t copy_gref;
306};
307
Wei Liub3f980b2013-08-26 12:59:38 +0100308static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
309 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000310{
Wei Liub3f980b2013-08-26 12:59:38 +0100311 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000312 struct xen_netif_rx_request *req;
313
314 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
315
316 meta = npo->meta + npo->meta_prod++;
317 meta->gso_size = 0;
318 meta->size = 0;
319 meta->id = req->id;
320
321 npo->copy_off = 0;
322 npo->copy_gref = req->gref;
323
324 return meta;
325}
326
Wei Liu33bc8012013-10-08 10:54:21 +0100327/*
328 * Set up the grant operations for this fragment. If it's a flipping
329 * interface, we also set up the unmap request from here.
330 */
Wei Liu73764192013-08-26 12:59:39 +0100331static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
332 struct netrx_pending_operations *npo,
333 struct page *page, unsigned long size,
Wei Liu33bc8012013-10-08 10:54:21 +0100334 unsigned long offset, int *head)
Ian Campbellf942dc22011-03-15 00:06:18 +0000335{
336 struct gnttab_copy *copy_gop;
Wei Liub3f980b2013-08-26 12:59:38 +0100337 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000338 unsigned long bytes;
339
340 /* Data must not cross a page boundary. */
Ian Campbell6a8ed462012-10-10 03:48:42 +0000341 BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000342
343 meta = npo->meta + npo->meta_prod - 1;
344
Ian Campbell6a8ed462012-10-10 03:48:42 +0000345 /* Skip unused frames from start of page */
346 page += offset >> PAGE_SHIFT;
347 offset &= ~PAGE_MASK;
348
Ian Campbellf942dc22011-03-15 00:06:18 +0000349 while (size > 0) {
Ian Campbell6a8ed462012-10-10 03:48:42 +0000350 BUG_ON(offset >= PAGE_SIZE);
Ian Campbellf942dc22011-03-15 00:06:18 +0000351 BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
352
Ian Campbell6a8ed462012-10-10 03:48:42 +0000353 bytes = PAGE_SIZE - offset;
354
355 if (bytes > size)
356 bytes = size;
357
Wei Liu33bc8012013-10-08 10:54:21 +0100358 if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000359 /*
360 * Netfront requires there to be some data in the head
361 * buffer.
362 */
Wei Liu33bc8012013-10-08 10:54:21 +0100363 BUG_ON(*head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000364
365 meta = get_next_rx_buffer(vif, npo);
366 }
367
Ian Campbellf942dc22011-03-15 00:06:18 +0000368 if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
369 bytes = MAX_BUFFER_OFFSET - npo->copy_off;
370
371 copy_gop = npo->copy + npo->copy_prod++;
372 copy_gop->flags = GNTCOPY_dest_gref;
Wei Liub3f980b2013-08-26 12:59:38 +0100373 copy_gop->len = bytes;
374
Wei Liu43e9d192013-08-26 12:59:37 +0100375 copy_gop->source.domid = DOMID_SELF;
376 copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000377 copy_gop->source.offset = offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000378
Wei Liub3f980b2013-08-26 12:59:38 +0100379 copy_gop->dest.domid = vif->domid;
Ian Campbellf942dc22011-03-15 00:06:18 +0000380 copy_gop->dest.offset = npo->copy_off;
381 copy_gop->dest.u.ref = npo->copy_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000382
383 npo->copy_off += bytes;
384 meta->size += bytes;
385
386 offset += bytes;
387 size -= bytes;
388
Ian Campbell6a8ed462012-10-10 03:48:42 +0000389 /* Next frame */
390 if (offset == PAGE_SIZE && size) {
391 BUG_ON(!PageCompound(page));
392 page++;
393 offset = 0;
394 }
395
Ian Campbellf942dc22011-03-15 00:06:18 +0000396 /* Leave a gap for the GSO descriptor. */
Wei Liu33bc8012013-10-08 10:54:21 +0100397 if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix)
Ian Campbellf942dc22011-03-15 00:06:18 +0000398 vif->rx.req_cons++;
399
Wei Liu33bc8012013-10-08 10:54:21 +0100400 *head = 0; /* There must be something in this buffer now. */
Ian Campbellf942dc22011-03-15 00:06:18 +0000401
402 }
403}
404
405/*
406 * Prepare an SKB to be transmitted to the frontend.
407 *
408 * This function is responsible for allocating grant operations, meta
409 * structures, etc.
410 *
411 * It returns the number of meta structures consumed. The number of
412 * ring slots used is always equal to the number of meta slots used
413 * plus the number of GSO descriptors used. Currently, we use either
414 * zero GSO descriptors (for non-GSO packets) or one descriptor (for
415 * frontend-side LRO).
416 */
Wei Liu73764192013-08-26 12:59:39 +0100417static int xenvif_gop_skb(struct sk_buff *skb,
418 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000419{
420 struct xenvif *vif = netdev_priv(skb->dev);
421 int nr_frags = skb_shinfo(skb)->nr_frags;
422 int i;
423 struct xen_netif_rx_request *req;
Wei Liub3f980b2013-08-26 12:59:38 +0100424 struct xenvif_rx_meta *meta;
Ian Campbellf942dc22011-03-15 00:06:18 +0000425 unsigned char *data;
Wei Liu33bc8012013-10-08 10:54:21 +0100426 int head = 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000427 int old_meta_prod;
428
429 old_meta_prod = npo->meta_prod;
430
431 /* Set up a GSO prefix descriptor, if necessary */
432 if (skb_shinfo(skb)->gso_size && vif->gso_prefix) {
433 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
434 meta = npo->meta + npo->meta_prod++;
435 meta->gso_size = skb_shinfo(skb)->gso_size;
436 meta->size = 0;
437 meta->id = req->id;
438 }
439
440 req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
441 meta = npo->meta + npo->meta_prod++;
442
443 if (!vif->gso_prefix)
444 meta->gso_size = skb_shinfo(skb)->gso_size;
445 else
446 meta->gso_size = 0;
447
448 meta->size = 0;
449 meta->id = req->id;
450 npo->copy_off = 0;
451 npo->copy_gref = req->gref;
452
453 data = skb->data;
454 while (data < skb_tail_pointer(skb)) {
455 unsigned int offset = offset_in_page(data);
456 unsigned int len = PAGE_SIZE - offset;
457
458 if (data + len > skb_tail_pointer(skb))
459 len = skb_tail_pointer(skb) - data;
460
Wei Liu73764192013-08-26 12:59:39 +0100461 xenvif_gop_frag_copy(vif, skb, npo,
Wei Liu33bc8012013-10-08 10:54:21 +0100462 virt_to_page(data), len, offset, &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000463 data += len;
464 }
465
466 for (i = 0; i < nr_frags; i++) {
Wei Liu73764192013-08-26 12:59:39 +0100467 xenvif_gop_frag_copy(vif, skb, npo,
468 skb_frag_page(&skb_shinfo(skb)->frags[i]),
469 skb_frag_size(&skb_shinfo(skb)->frags[i]),
470 skb_shinfo(skb)->frags[i].page_offset,
Wei Liu33bc8012013-10-08 10:54:21 +0100471 &head);
Ian Campbellf942dc22011-03-15 00:06:18 +0000472 }
473
474 return npo->meta_prod - old_meta_prod;
475}
476
477/*
Wei Liu73764192013-08-26 12:59:39 +0100478 * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
Ian Campbellf942dc22011-03-15 00:06:18 +0000479 * used to set up the operations on the top of
480 * netrx_pending_operations, which have since been done. Check that
481 * they didn't give any errors and advance over them.
482 */
Wei Liu73764192013-08-26 12:59:39 +0100483static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
484 struct netrx_pending_operations *npo)
Ian Campbellf942dc22011-03-15 00:06:18 +0000485{
486 struct gnttab_copy *copy_op;
487 int status = XEN_NETIF_RSP_OKAY;
488 int i;
489
490 for (i = 0; i < nr_meta_slots; i++) {
491 copy_op = npo->copy + npo->copy_cons++;
492 if (copy_op->status != GNTST_okay) {
493 netdev_dbg(vif->dev,
494 "Bad status %d from copy to DOM%d.\n",
495 copy_op->status, vif->domid);
496 status = XEN_NETIF_RSP_ERROR;
497 }
498 }
499
500 return status;
501}
502
Wei Liu73764192013-08-26 12:59:39 +0100503static void xenvif_add_frag_responses(struct xenvif *vif, int status,
504 struct xenvif_rx_meta *meta,
505 int nr_meta_slots)
Ian Campbellf942dc22011-03-15 00:06:18 +0000506{
507 int i;
508 unsigned long offset;
509
510 /* No fragments used */
511 if (nr_meta_slots <= 1)
512 return;
513
514 nr_meta_slots--;
515
516 for (i = 0; i < nr_meta_slots; i++) {
517 int flags;
518 if (i == nr_meta_slots - 1)
519 flags = 0;
520 else
521 flags = XEN_NETRXF_more_data;
522
523 offset = 0;
524 make_rx_response(vif, meta[i].id, status, offset,
525 meta[i].size, flags);
526 }
527}
528
Wei Liu33bc8012013-10-08 10:54:21 +0100529struct skb_cb_overlay {
530 int meta_slots_used;
531};
532
Wei Liu73764192013-08-26 12:59:39 +0100533static void xenvif_kick_thread(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000534{
Wei Liub3f980b2013-08-26 12:59:38 +0100535 wake_up(&vif->wq);
536}
537
Wei Liu73764192013-08-26 12:59:39 +0100538void xenvif_rx_action(struct xenvif *vif)
Wei Liub3f980b2013-08-26 12:59:38 +0100539{
Ian Campbellf942dc22011-03-15 00:06:18 +0000540 s8 status;
Wei Liue1f00a692013-05-22 06:34:45 +0000541 u16 flags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000542 struct xen_netif_rx_response *resp;
543 struct sk_buff_head rxq;
544 struct sk_buff *skb;
545 LIST_HEAD(notify);
546 int ret;
547 int nr_frags;
548 int count;
549 unsigned long offset;
550 struct skb_cb_overlay *sco;
Wei Liub3f980b2013-08-26 12:59:38 +0100551 int need_to_notify = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +0000552
553 struct netrx_pending_operations npo = {
Wei Liub3f980b2013-08-26 12:59:38 +0100554 .copy = vif->grant_copy_op,
555 .meta = vif->meta,
Ian Campbellf942dc22011-03-15 00:06:18 +0000556 };
557
558 skb_queue_head_init(&rxq);
559
560 count = 0;
561
Wei Liub3f980b2013-08-26 12:59:38 +0100562 while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000563 vif = netdev_priv(skb->dev);
564 nr_frags = skb_shinfo(skb)->nr_frags;
565
566 sco = (struct skb_cb_overlay *)skb->cb;
Wei Liu73764192013-08-26 12:59:39 +0100567 sco->meta_slots_used = xenvif_gop_skb(skb, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000568
Wei Liu33bc8012013-10-08 10:54:21 +0100569 count += nr_frags + 1;
Ian Campbellf942dc22011-03-15 00:06:18 +0000570
571 __skb_queue_tail(&rxq, skb);
572
573 /* Filled the batch queue? */
Wei Liu33bc8012013-10-08 10:54:21 +0100574 /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */
575 if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE)
Ian Campbellf942dc22011-03-15 00:06:18 +0000576 break;
577 }
578
Wei Liub3f980b2013-08-26 12:59:38 +0100579 BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
Ian Campbellf942dc22011-03-15 00:06:18 +0000580
581 if (!npo.copy_prod)
582 return;
583
Wei Liub3f980b2013-08-26 12:59:38 +0100584 BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op));
585 gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
Ian Campbellf942dc22011-03-15 00:06:18 +0000586
587 while ((skb = __skb_dequeue(&rxq)) != NULL) {
588 sco = (struct skb_cb_overlay *)skb->cb;
589
590 vif = netdev_priv(skb->dev);
591
Wei Liub3f980b2013-08-26 12:59:38 +0100592 if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000593 resp = RING_GET_RESPONSE(&vif->rx,
Wei Liub3f980b2013-08-26 12:59:38 +0100594 vif->rx.rsp_prod_pvt++);
Ian Campbellf942dc22011-03-15 00:06:18 +0000595
596 resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
597
Wei Liub3f980b2013-08-26 12:59:38 +0100598 resp->offset = vif->meta[npo.meta_cons].gso_size;
599 resp->id = vif->meta[npo.meta_cons].id;
Ian Campbellf942dc22011-03-15 00:06:18 +0000600 resp->status = sco->meta_slots_used;
601
602 npo.meta_cons++;
603 sco->meta_slots_used--;
604 }
605
606
607 vif->dev->stats.tx_bytes += skb->len;
608 vif->dev->stats.tx_packets++;
609
Wei Liu73764192013-08-26 12:59:39 +0100610 status = xenvif_check_gop(vif, sco->meta_slots_used, &npo);
Ian Campbellf942dc22011-03-15 00:06:18 +0000611
612 if (sco->meta_slots_used == 1)
613 flags = 0;
614 else
615 flags = XEN_NETRXF_more_data;
616
617 if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
618 flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
619 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
620 /* remote but checksummed. */
621 flags |= XEN_NETRXF_data_validated;
622
623 offset = 0;
Wei Liub3f980b2013-08-26 12:59:38 +0100624 resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
Ian Campbellf942dc22011-03-15 00:06:18 +0000625 status, offset,
Wei Liub3f980b2013-08-26 12:59:38 +0100626 vif->meta[npo.meta_cons].size,
Ian Campbellf942dc22011-03-15 00:06:18 +0000627 flags);
628
Wei Liub3f980b2013-08-26 12:59:38 +0100629 if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000630 struct xen_netif_extra_info *gso =
631 (struct xen_netif_extra_info *)
632 RING_GET_RESPONSE(&vif->rx,
633 vif->rx.rsp_prod_pvt++);
634
635 resp->flags |= XEN_NETRXF_extra_info;
636
Wei Liub3f980b2013-08-26 12:59:38 +0100637 gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
Ian Campbellf942dc22011-03-15 00:06:18 +0000638 gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
639 gso->u.gso.pad = 0;
640 gso->u.gso.features = 0;
641
642 gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
643 gso->flags = 0;
644 }
645
Wei Liu73764192013-08-26 12:59:39 +0100646 xenvif_add_frag_responses(vif, status,
647 vif->meta + npo.meta_cons + 1,
648 sco->meta_slots_used);
Ian Campbellf942dc22011-03-15 00:06:18 +0000649
650 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
Ian Campbellf942dc22011-03-15 00:06:18 +0000651
Wei Liub3f980b2013-08-26 12:59:38 +0100652 if (ret)
653 need_to_notify = 1;
654
Ian Campbellf942dc22011-03-15 00:06:18 +0000655 xenvif_notify_tx_completion(vif);
656
Ian Campbellf942dc22011-03-15 00:06:18 +0000657 npo.meta_cons += sco->meta_slots_used;
658 dev_kfree_skb(skb);
659 }
660
Wei Liub3f980b2013-08-26 12:59:38 +0100661 if (need_to_notify)
Wei Liue1f00a692013-05-22 06:34:45 +0000662 notify_remote_via_irq(vif->rx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +0000663
664 /* More work to do? */
Wei Liub3f980b2013-08-26 12:59:38 +0100665 if (!skb_queue_empty(&vif->rx_queue))
Wei Liu73764192013-08-26 12:59:39 +0100666 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000667}
668
Wei Liu73764192013-08-26 12:59:39 +0100669void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +0000670{
Wei Liub3f980b2013-08-26 12:59:38 +0100671 skb_queue_tail(&vif->rx_queue, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +0000672
Wei Liu73764192013-08-26 12:59:39 +0100673 xenvif_kick_thread(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000674}
675
Wei Liu73764192013-08-26 12:59:39 +0100676void xenvif_check_rx_xenvif(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +0000677{
678 int more_to_do;
679
680 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
681
682 if (more_to_do)
Wei Liub3f980b2013-08-26 12:59:38 +0100683 napi_schedule(&vif->napi);
Ian Campbellf942dc22011-03-15 00:06:18 +0000684}
685
686static void tx_add_credit(struct xenvif *vif)
687{
688 unsigned long max_burst, max_credit;
689
690 /*
691 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
692 * Otherwise the interface can seize up due to insufficient credit.
693 */
694 max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
695 max_burst = min(max_burst, 131072UL);
696 max_burst = max(max_burst, vif->credit_bytes);
697
698 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
699 max_credit = vif->remaining_credit + vif->credit_bytes;
700 if (max_credit < vif->remaining_credit)
701 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
702
703 vif->remaining_credit = min(max_credit, max_burst);
704}
705
706static void tx_credit_callback(unsigned long data)
707{
708 struct xenvif *vif = (struct xenvif *)data;
709 tx_add_credit(vif);
Wei Liu73764192013-08-26 12:59:39 +0100710 xenvif_check_rx_xenvif(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +0000711}
712
Wei Liu73764192013-08-26 12:59:39 +0100713static void xenvif_tx_err(struct xenvif *vif,
714 struct xen_netif_tx_request *txp, RING_IDX end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000715{
716 RING_IDX cons = vif->tx.req_cons;
717
718 do {
719 make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
Ian Campbellb9149722013-02-06 23:41:38 +0000720 if (cons == end)
Ian Campbellf942dc22011-03-15 00:06:18 +0000721 break;
722 txp = RING_GET_REQUEST(&vif->tx, cons++);
723 } while (1);
724 vif->tx.req_cons = cons;
Ian Campbellf942dc22011-03-15 00:06:18 +0000725}
726
Wei Liu73764192013-08-26 12:59:39 +0100727static void xenvif_fatal_tx_err(struct xenvif *vif)
Ian Campbell488562862013-02-06 23:41:35 +0000728{
729 netdev_err(vif->dev, "fatal error; disabling device\n");
730 xenvif_carrier_off(vif);
Ian Campbell488562862013-02-06 23:41:35 +0000731}
732
Wei Liu73764192013-08-26 12:59:39 +0100733static int xenvif_count_requests(struct xenvif *vif,
734 struct xen_netif_tx_request *first,
735 struct xen_netif_tx_request *txp,
736 int work_to_do)
Ian Campbellf942dc22011-03-15 00:06:18 +0000737{
738 RING_IDX cons = vif->tx.req_cons;
Wei Liu2810e5b2013-04-22 02:20:42 +0000739 int slots = 0;
740 int drop_err = 0;
Wei Liu59ccb4e2013-05-02 00:43:58 +0000741 int more_data;
Ian Campbellf942dc22011-03-15 00:06:18 +0000742
743 if (!(first->flags & XEN_NETTXF_more_data))
744 return 0;
745
746 do {
Wei Liu59ccb4e2013-05-02 00:43:58 +0000747 struct xen_netif_tx_request dropped_tx = { 0 };
748
Wei Liu2810e5b2013-04-22 02:20:42 +0000749 if (slots >= work_to_do) {
750 netdev_err(vif->dev,
751 "Asked for %d slots but exceeds this limit\n",
752 work_to_do);
Wei Liu73764192013-08-26 12:59:39 +0100753 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000754 return -ENODATA;
Ian Campbellf942dc22011-03-15 00:06:18 +0000755 }
756
Wei Liu2810e5b2013-04-22 02:20:42 +0000757 /* This guest is really using too many slots and
758 * considered malicious.
759 */
Wei Liu37641492013-05-02 00:43:59 +0000760 if (unlikely(slots >= fatal_skb_slots)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000761 netdev_err(vif->dev,
762 "Malicious frontend using %d slots, threshold %u\n",
Wei Liu37641492013-05-02 00:43:59 +0000763 slots, fatal_skb_slots);
Wei Liu73764192013-08-26 12:59:39 +0100764 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000765 return -E2BIG;
Ian Campbellf942dc22011-03-15 00:06:18 +0000766 }
767
Wei Liu2810e5b2013-04-22 02:20:42 +0000768 /* Xen network protocol had implicit dependency on
Wei Liu37641492013-05-02 00:43:59 +0000769 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
770 * the historical MAX_SKB_FRAGS value 18 to honor the
771 * same behavior as before. Any packet using more than
772 * 18 slots but less than fatal_skb_slots slots is
773 * dropped
Wei Liu2810e5b2013-04-22 02:20:42 +0000774 */
Wei Liu37641492013-05-02 00:43:59 +0000775 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000776 if (net_ratelimit())
777 netdev_dbg(vif->dev,
778 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
Wei Liu37641492013-05-02 00:43:59 +0000779 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu2810e5b2013-04-22 02:20:42 +0000780 drop_err = -E2BIG;
781 }
782
Wei Liu59ccb4e2013-05-02 00:43:58 +0000783 if (drop_err)
784 txp = &dropped_tx;
785
Wei Liu2810e5b2013-04-22 02:20:42 +0000786 memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
Ian Campbellf942dc22011-03-15 00:06:18 +0000787 sizeof(*txp));
Wei Liu03393fd52013-04-22 02:20:43 +0000788
789 /* If the guest submitted a frame >= 64 KiB then
790 * first->size overflowed and following slots will
791 * appear to be larger than the frame.
792 *
793 * This cannot be fatal error as there are buggy
794 * frontends that do this.
795 *
796 * Consume all slots and drop the packet.
797 */
798 if (!drop_err && txp->size > first->size) {
799 if (net_ratelimit())
800 netdev_dbg(vif->dev,
801 "Invalid tx request, slot size %u > remaining size %u\n",
802 txp->size, first->size);
803 drop_err = -EIO;
Ian Campbellf942dc22011-03-15 00:06:18 +0000804 }
805
806 first->size -= txp->size;
Wei Liu2810e5b2013-04-22 02:20:42 +0000807 slots++;
Ian Campbellf942dc22011-03-15 00:06:18 +0000808
809 if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
Wei Liu2810e5b2013-04-22 02:20:42 +0000810 netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
Ian Campbellf942dc22011-03-15 00:06:18 +0000811 txp->offset, txp->size);
Wei Liu73764192013-08-26 12:59:39 +0100812 xenvif_fatal_tx_err(vif);
David Vrabel35876b52013-02-14 03:18:57 +0000813 return -EINVAL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000814 }
Wei Liu59ccb4e2013-05-02 00:43:58 +0000815
816 more_data = txp->flags & XEN_NETTXF_more_data;
817
818 if (!drop_err)
819 txp++;
820
821 } while (more_data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000822
823 if (drop_err) {
Wei Liu73764192013-08-26 12:59:39 +0100824 xenvif_tx_err(vif, first, cons + slots);
Wei Liu2810e5b2013-04-22 02:20:42 +0000825 return drop_err;
826 }
827
828 return slots;
Ian Campbellf942dc22011-03-15 00:06:18 +0000829}
830
Wei Liu73764192013-08-26 12:59:39 +0100831static struct page *xenvif_alloc_page(struct xenvif *vif,
832 u16 pending_idx)
Ian Campbellf942dc22011-03-15 00:06:18 +0000833{
834 struct page *page;
Wei Liub3f980b2013-08-26 12:59:38 +0100835
836 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000837 if (!page)
838 return NULL;
Wei Liub3f980b2013-08-26 12:59:38 +0100839 vif->mmap_pages[pending_idx] = page;
840
Ian Campbellf942dc22011-03-15 00:06:18 +0000841 return page;
842}
843
Wei Liu73764192013-08-26 12:59:39 +0100844static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
845 struct sk_buff *skb,
846 struct xen_netif_tx_request *txp,
847 struct gnttab_copy *gop)
Ian Campbellf942dc22011-03-15 00:06:18 +0000848{
849 struct skb_shared_info *shinfo = skb_shinfo(skb);
850 skb_frag_t *frags = shinfo->frags;
Ian Campbellea066ad2011-10-05 00:28:46 +0000851 u16 pending_idx = *((u16 *)skb->data);
Wei Liu2810e5b2013-04-22 02:20:42 +0000852 u16 head_idx = 0;
853 int slot, start;
854 struct page *page;
855 pending_ring_idx_t index, start_idx = 0;
856 uint16_t dst_offset;
857 unsigned int nr_slots;
858 struct pending_tx_info *first = NULL;
859
860 /* At this point shinfo->nr_frags is in fact the number of
Wei Liu37641492013-05-02 00:43:59 +0000861 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
Wei Liu2810e5b2013-04-22 02:20:42 +0000862 */
863 nr_slots = shinfo->nr_frags;
Ian Campbellf942dc22011-03-15 00:06:18 +0000864
865 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000866 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000867
Wei Liu2810e5b2013-04-22 02:20:42 +0000868 /* Coalesce tx requests, at this point the packet passed in
869 * should be <= 64K. Any packets larger than 64K have been
Wei Liu73764192013-08-26 12:59:39 +0100870 * handled in xenvif_count_requests().
Wei Liu2810e5b2013-04-22 02:20:42 +0000871 */
872 for (shinfo->nr_frags = slot = start; slot < nr_slots;
873 shinfo->nr_frags++) {
Ian Campbellf942dc22011-03-15 00:06:18 +0000874 struct pending_tx_info *pending_tx_info =
Wei Liub3f980b2013-08-26 12:59:38 +0100875 vif->pending_tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000876
Wei Liub3f980b2013-08-26 12:59:38 +0100877 page = alloc_page(GFP_ATOMIC|__GFP_COLD);
Ian Campbellf942dc22011-03-15 00:06:18 +0000878 if (!page)
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000879 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +0000880
Wei Liu2810e5b2013-04-22 02:20:42 +0000881 dst_offset = 0;
882 first = NULL;
883 while (dst_offset < PAGE_SIZE && slot < nr_slots) {
884 gop->flags = GNTCOPY_source_gref;
Ian Campbellf942dc22011-03-15 00:06:18 +0000885
Wei Liu2810e5b2013-04-22 02:20:42 +0000886 gop->source.u.ref = txp->gref;
887 gop->source.domid = vif->domid;
888 gop->source.offset = txp->offset;
Ian Campbellf942dc22011-03-15 00:06:18 +0000889
Wei Liu2810e5b2013-04-22 02:20:42 +0000890 gop->dest.domid = DOMID_SELF;
Ian Campbellf942dc22011-03-15 00:06:18 +0000891
Wei Liu2810e5b2013-04-22 02:20:42 +0000892 gop->dest.offset = dst_offset;
893 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
Ian Campbellf942dc22011-03-15 00:06:18 +0000894
Wei Liu2810e5b2013-04-22 02:20:42 +0000895 if (dst_offset + txp->size > PAGE_SIZE) {
896 /* This page can only merge a portion
897 * of tx request. Do not increment any
898 * pointer / counter here. The txp
899 * will be dealt with in future
900 * rounds, eventually hitting the
901 * `else` branch.
902 */
903 gop->len = PAGE_SIZE - dst_offset;
904 txp->offset += gop->len;
905 txp->size -= gop->len;
906 dst_offset += gop->len; /* quit loop */
907 } else {
908 /* This tx request can be merged in the page */
909 gop->len = txp->size;
910 dst_offset += gop->len;
911
Wei Liub3f980b2013-08-26 12:59:38 +0100912 index = pending_index(vif->pending_cons++);
Wei Liu2810e5b2013-04-22 02:20:42 +0000913
Wei Liub3f980b2013-08-26 12:59:38 +0100914 pending_idx = vif->pending_ring[index];
Wei Liu2810e5b2013-04-22 02:20:42 +0000915
916 memcpy(&pending_tx_info[pending_idx].req, txp,
917 sizeof(*txp));
Wei Liu2810e5b2013-04-22 02:20:42 +0000918
919 /* Poison these fields, corresponding
920 * fields for head tx req will be set
921 * to correct values after the loop.
922 */
Wei Liub3f980b2013-08-26 12:59:38 +0100923 vif->mmap_pages[pending_idx] = (void *)(~0UL);
Wei Liu2810e5b2013-04-22 02:20:42 +0000924 pending_tx_info[pending_idx].head =
925 INVALID_PENDING_RING_IDX;
926
927 if (!first) {
928 first = &pending_tx_info[pending_idx];
929 start_idx = index;
930 head_idx = pending_idx;
931 }
932
933 txp++;
934 slot++;
935 }
936
937 gop++;
938 }
939
940 first->req.offset = 0;
941 first->req.size = dst_offset;
942 first->head = start_idx;
Wei Liub3f980b2013-08-26 12:59:38 +0100943 vif->mmap_pages[head_idx] = page;
Wei Liu2810e5b2013-04-22 02:20:42 +0000944 frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000945 }
946
Wei Liu2810e5b2013-04-22 02:20:42 +0000947 BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
948
Ian Campbellf942dc22011-03-15 00:06:18 +0000949 return gop;
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000950err:
951 /* Unwind, freeing all pages and sending error responses. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000952 while (shinfo->nr_frags-- > start) {
Wei Liu73764192013-08-26 12:59:39 +0100953 xenvif_idx_release(vif,
Wei Liu2810e5b2013-04-22 02:20:42 +0000954 frag_get_pending_idx(&frags[shinfo->nr_frags]),
955 XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000956 }
957 /* The head too, if necessary. */
958 if (start)
Wei Liu73764192013-08-26 12:59:39 +0100959 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbell4cc7c1c2013-02-06 23:41:37 +0000960
961 return NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +0000962}
963
Wei Liu73764192013-08-26 12:59:39 +0100964static int xenvif_tx_check_gop(struct xenvif *vif,
965 struct sk_buff *skb,
966 struct gnttab_copy **gopp)
Ian Campbellf942dc22011-03-15 00:06:18 +0000967{
968 struct gnttab_copy *gop = *gopp;
Ian Campbellea066ad2011-10-05 00:28:46 +0000969 u16 pending_idx = *((u16 *)skb->data);
Ian Campbellf942dc22011-03-15 00:06:18 +0000970 struct skb_shared_info *shinfo = skb_shinfo(skb);
Wei Liu2810e5b2013-04-22 02:20:42 +0000971 struct pending_tx_info *tx_info;
Ian Campbellf942dc22011-03-15 00:06:18 +0000972 int nr_frags = shinfo->nr_frags;
973 int i, err, start;
Wei Liu2810e5b2013-04-22 02:20:42 +0000974 u16 peek; /* peek into next tx request */
Ian Campbellf942dc22011-03-15 00:06:18 +0000975
976 /* Check status of header. */
977 err = gop->status;
Matthew Daley7d5145d2013-02-06 23:41:36 +0000978 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +0100979 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +0000980
981 /* Skip first skb fragment if it is on same page as header fragment. */
Ian Campbellea066ad2011-10-05 00:28:46 +0000982 start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +0000983
984 for (i = start; i < nr_frags; i++) {
985 int j, newerr;
Wei Liu2810e5b2013-04-22 02:20:42 +0000986 pending_ring_idx_t head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000987
Ian Campbellea066ad2011-10-05 00:28:46 +0000988 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
Wei Liub3f980b2013-08-26 12:59:38 +0100989 tx_info = &vif->pending_tx_info[pending_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +0000990 head = tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +0000991
992 /* Check error status: if okay then remember grant handle. */
Wei Liu2810e5b2013-04-22 02:20:42 +0000993 do {
994 newerr = (++gop)->status;
995 if (newerr)
996 break;
Wei Liub3f980b2013-08-26 12:59:38 +0100997 peek = vif->pending_ring[pending_index(++head)];
998 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +0000999
Ian Campbellf942dc22011-03-15 00:06:18 +00001000 if (likely(!newerr)) {
1001 /* Had a previous error? Invalidate this fragment. */
1002 if (unlikely(err))
Wei Liu73764192013-08-26 12:59:39 +01001003 xenvif_idx_release(vif, pending_idx,
1004 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001005 continue;
1006 }
1007
1008 /* Error on this fragment: respond to client with an error. */
Wei Liu73764192013-08-26 12:59:39 +01001009 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
Ian Campbellf942dc22011-03-15 00:06:18 +00001010
1011 /* Not the first error? Preceding frags already invalidated. */
1012 if (err)
1013 continue;
1014
1015 /* First error: invalidate header and preceding fragments. */
1016 pending_idx = *((u16 *)skb->data);
Wei Liu73764192013-08-26 12:59:39 +01001017 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001018 for (j = start; j < i; j++) {
Jan Beulich5ccb3ea2011-11-18 05:42:05 +00001019 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
Wei Liu73764192013-08-26 12:59:39 +01001020 xenvif_idx_release(vif, pending_idx,
1021 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001022 }
1023
1024 /* Remember the error: invalidate all subsequent fragments. */
1025 err = newerr;
1026 }
1027
1028 *gopp = gop + 1;
1029 return err;
1030}
1031
Wei Liu73764192013-08-26 12:59:39 +01001032static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
Ian Campbellf942dc22011-03-15 00:06:18 +00001033{
1034 struct skb_shared_info *shinfo = skb_shinfo(skb);
1035 int nr_frags = shinfo->nr_frags;
1036 int i;
1037
1038 for (i = 0; i < nr_frags; i++) {
1039 skb_frag_t *frag = shinfo->frags + i;
1040 struct xen_netif_tx_request *txp;
Ian Campbellea066ad2011-10-05 00:28:46 +00001041 struct page *page;
1042 u16 pending_idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001043
Ian Campbellea066ad2011-10-05 00:28:46 +00001044 pending_idx = frag_get_pending_idx(frag);
Ian Campbellf942dc22011-03-15 00:06:18 +00001045
Wei Liub3f980b2013-08-26 12:59:38 +01001046 txp = &vif->pending_tx_info[pending_idx].req;
1047 page = virt_to_page(idx_to_kaddr(vif, pending_idx));
Ian Campbellea066ad2011-10-05 00:28:46 +00001048 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
Ian Campbellf942dc22011-03-15 00:06:18 +00001049 skb->len += txp->size;
1050 skb->data_len += txp->size;
1051 skb->truesize += txp->size;
1052
Wei Liu73764192013-08-26 12:59:39 +01001053 /* Take an extra reference to offset xenvif_idx_release */
Wei Liub3f980b2013-08-26 12:59:38 +01001054 get_page(vif->mmap_pages[pending_idx]);
Wei Liu73764192013-08-26 12:59:39 +01001055 xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001056 }
1057}
1058
Wei Liu73764192013-08-26 12:59:39 +01001059static int xenvif_get_extras(struct xenvif *vif,
Ian Campbellf942dc22011-03-15 00:06:18 +00001060 struct xen_netif_extra_info *extras,
1061 int work_to_do)
1062{
1063 struct xen_netif_extra_info extra;
1064 RING_IDX cons = vif->tx.req_cons;
1065
1066 do {
1067 if (unlikely(work_to_do-- <= 0)) {
Ian Campbell488562862013-02-06 23:41:35 +00001068 netdev_err(vif->dev, "Missing extra info\n");
Wei Liu73764192013-08-26 12:59:39 +01001069 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001070 return -EBADR;
1071 }
1072
1073 memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
1074 sizeof(extra));
1075 if (unlikely(!extra.type ||
1076 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1077 vif->tx.req_cons = ++cons;
Ian Campbell488562862013-02-06 23:41:35 +00001078 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001079 "Invalid extra type: %d\n", extra.type);
Wei Liu73764192013-08-26 12:59:39 +01001080 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001081 return -EINVAL;
1082 }
1083
1084 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
1085 vif->tx.req_cons = ++cons;
1086 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
1087
1088 return work_to_do;
1089}
1090
Wei Liu73764192013-08-26 12:59:39 +01001091static int xenvif_set_skb_gso(struct xenvif *vif,
1092 struct sk_buff *skb,
1093 struct xen_netif_extra_info *gso)
Ian Campbellf942dc22011-03-15 00:06:18 +00001094{
1095 if (!gso->u.gso.size) {
Ian Campbell488562862013-02-06 23:41:35 +00001096 netdev_err(vif->dev, "GSO size must not be zero.\n");
Wei Liu73764192013-08-26 12:59:39 +01001097 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001098 return -EINVAL;
1099 }
1100
Paul Durranta9468582013-10-16 17:50:31 +01001101 switch (gso->u.gso.type) {
1102 case XEN_NETIF_GSO_TYPE_TCPV4:
1103 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
1104 break;
1105 case XEN_NETIF_GSO_TYPE_TCPV6:
1106 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
1107 break;
1108 default:
Ian Campbell488562862013-02-06 23:41:35 +00001109 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
Wei Liu73764192013-08-26 12:59:39 +01001110 xenvif_fatal_tx_err(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001111 return -EINVAL;
1112 }
1113
1114 skb_shinfo(skb)->gso_size = gso->u.gso.size;
Ian Campbellf942dc22011-03-15 00:06:18 +00001115
1116 /* Header must be checked, and gso_segs computed. */
1117 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
1118 skb_shinfo(skb)->gso_segs = 0;
1119
1120 return 0;
1121}
1122
Paul Durrant2eba61d2013-10-16 17:50:29 +01001123static inline void maybe_pull_tail(struct sk_buff *skb, unsigned int len)
1124{
1125 if (skb_is_nonlinear(skb) && skb_headlen(skb) < len) {
1126 /* If we need to pullup then pullup to the max, so we
1127 * won't need to do it again.
1128 */
1129 int target = min_t(int, skb->len, MAX_TCP_HEADER);
1130 __pskb_pull_tail(skb, target - skb_headlen(skb));
1131 }
1132}
1133
1134static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
1135 int recalculate_partial_csum)
1136{
1137 struct iphdr *iph = (void *)skb->data;
1138 unsigned int header_size;
1139 unsigned int off;
1140 int err = -EPROTO;
1141
1142 off = sizeof(struct iphdr);
1143
1144 header_size = skb->network_header + off + MAX_IPOPTLEN;
1145 maybe_pull_tail(skb, header_size);
1146
1147 off = iph->ihl * 4;
1148
1149 switch (iph->protocol) {
1150 case IPPROTO_TCP:
1151 if (!skb_partial_csum_set(skb, off,
1152 offsetof(struct tcphdr, check)))
1153 goto out;
1154
1155 if (recalculate_partial_csum) {
1156 struct tcphdr *tcph = tcp_hdr(skb);
1157
1158 header_size = skb->network_header +
1159 off +
1160 sizeof(struct tcphdr);
1161 maybe_pull_tail(skb, header_size);
1162
1163 tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1164 skb->len - off,
1165 IPPROTO_TCP, 0);
1166 }
1167 break;
1168 case IPPROTO_UDP:
1169 if (!skb_partial_csum_set(skb, off,
1170 offsetof(struct udphdr, check)))
1171 goto out;
1172
1173 if (recalculate_partial_csum) {
1174 struct udphdr *udph = udp_hdr(skb);
1175
1176 header_size = skb->network_header +
1177 off +
1178 sizeof(struct udphdr);
1179 maybe_pull_tail(skb, header_size);
1180
1181 udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
1182 skb->len - off,
1183 IPPROTO_UDP, 0);
1184 }
1185 break;
1186 default:
1187 if (net_ratelimit())
1188 netdev_err(vif->dev,
1189 "Attempting to checksum a non-TCP/UDP packet, "
1190 "dropping a protocol %d packet\n",
1191 iph->protocol);
1192 goto out;
1193 }
1194
1195 err = 0;
1196
1197out:
1198 return err;
1199}
1200
1201static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
1202 int recalculate_partial_csum)
1203{
1204 int err = -EPROTO;
1205 struct ipv6hdr *ipv6h = (void *)skb->data;
1206 u8 nexthdr;
1207 unsigned int header_size;
1208 unsigned int off;
1209 bool fragment;
1210 bool done;
1211
1212 done = false;
1213
1214 off = sizeof(struct ipv6hdr);
1215
1216 header_size = skb->network_header + off;
1217 maybe_pull_tail(skb, header_size);
1218
1219 nexthdr = ipv6h->nexthdr;
1220
1221 while ((off <= sizeof(struct ipv6hdr) + ntohs(ipv6h->payload_len)) &&
1222 !done) {
1223 switch (nexthdr) {
1224 case IPPROTO_DSTOPTS:
1225 case IPPROTO_HOPOPTS:
1226 case IPPROTO_ROUTING: {
1227 struct ipv6_opt_hdr *hp = (void *)(skb->data + off);
1228
1229 header_size = skb->network_header +
1230 off +
1231 sizeof(struct ipv6_opt_hdr);
1232 maybe_pull_tail(skb, header_size);
1233
1234 nexthdr = hp->nexthdr;
1235 off += ipv6_optlen(hp);
1236 break;
1237 }
1238 case IPPROTO_AH: {
1239 struct ip_auth_hdr *hp = (void *)(skb->data + off);
1240
1241 header_size = skb->network_header +
1242 off +
1243 sizeof(struct ip_auth_hdr);
1244 maybe_pull_tail(skb, header_size);
1245
1246 nexthdr = hp->nexthdr;
1247 off += (hp->hdrlen+2)<<2;
1248 break;
1249 }
1250 case IPPROTO_FRAGMENT:
1251 fragment = true;
1252 /* fall through */
1253 default:
1254 done = true;
1255 break;
1256 }
1257 }
1258
1259 if (!done) {
1260 if (net_ratelimit())
1261 netdev_err(vif->dev, "Failed to parse packet header\n");
1262 goto out;
1263 }
1264
1265 if (fragment) {
1266 if (net_ratelimit())
1267 netdev_err(vif->dev, "Packet is a fragment!\n");
1268 goto out;
1269 }
1270
1271 switch (nexthdr) {
1272 case IPPROTO_TCP:
1273 if (!skb_partial_csum_set(skb, off,
1274 offsetof(struct tcphdr, check)))
1275 goto out;
1276
1277 if (recalculate_partial_csum) {
1278 struct tcphdr *tcph = tcp_hdr(skb);
1279
1280 header_size = skb->network_header +
1281 off +
1282 sizeof(struct tcphdr);
1283 maybe_pull_tail(skb, header_size);
1284
1285 tcph->check = ~csum_ipv6_magic(&ipv6h->saddr,
1286 &ipv6h->daddr,
1287 skb->len - off,
1288 IPPROTO_TCP, 0);
1289 }
1290 break;
1291 case IPPROTO_UDP:
1292 if (!skb_partial_csum_set(skb, off,
1293 offsetof(struct udphdr, check)))
1294 goto out;
1295
1296 if (recalculate_partial_csum) {
1297 struct udphdr *udph = udp_hdr(skb);
1298
1299 header_size = skb->network_header +
1300 off +
1301 sizeof(struct udphdr);
1302 maybe_pull_tail(skb, header_size);
1303
1304 udph->check = ~csum_ipv6_magic(&ipv6h->saddr,
1305 &ipv6h->daddr,
1306 skb->len - off,
1307 IPPROTO_UDP, 0);
1308 }
1309 break;
1310 default:
1311 if (net_ratelimit())
1312 netdev_err(vif->dev,
1313 "Attempting to checksum a non-TCP/UDP packet, "
1314 "dropping a protocol %d packet\n",
1315 nexthdr);
1316 goto out;
1317 }
1318
1319 err = 0;
1320
1321out:
1322 return err;
1323}
1324
Ian Campbellf942dc22011-03-15 00:06:18 +00001325static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
1326{
Ian Campbellf942dc22011-03-15 00:06:18 +00001327 int err = -EPROTO;
1328 int recalculate_partial_csum = 0;
1329
Paul Durrant2eba61d2013-10-16 17:50:29 +01001330 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
Ian Campbellf942dc22011-03-15 00:06:18 +00001331 * peers can fail to set NETRXF_csum_blank when sending a GSO
1332 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
1333 * recalculate the partial checksum.
1334 */
1335 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
1336 vif->rx_gso_checksum_fixup++;
1337 skb->ip_summed = CHECKSUM_PARTIAL;
1338 recalculate_partial_csum = 1;
1339 }
1340
1341 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
1342 if (skb->ip_summed != CHECKSUM_PARTIAL)
1343 return 0;
1344
Paul Durrant2eba61d2013-10-16 17:50:29 +01001345 if (skb->protocol == htons(ETH_P_IP))
1346 err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
1347 else if (skb->protocol == htons(ETH_P_IPV6))
1348 err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
Ian Campbellf942dc22011-03-15 00:06:18 +00001349
Ian Campbellf942dc22011-03-15 00:06:18 +00001350 return err;
1351}
1352
1353static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
1354{
1355 unsigned long now = jiffies;
1356 unsigned long next_credit =
1357 vif->credit_timeout.expires +
1358 msecs_to_jiffies(vif->credit_usec / 1000);
1359
1360 /* Timer could already be pending in rare cases. */
1361 if (timer_pending(&vif->credit_timeout))
1362 return true;
1363
1364 /* Passed the point where we can replenish credit? */
1365 if (time_after_eq(now, next_credit)) {
1366 vif->credit_timeout.expires = now;
1367 tx_add_credit(vif);
1368 }
1369
1370 /* Still too big to send right now? Set a callback. */
1371 if (size > vif->remaining_credit) {
1372 vif->credit_timeout.data =
1373 (unsigned long)vif;
1374 vif->credit_timeout.function =
1375 tx_credit_callback;
1376 mod_timer(&vif->credit_timeout,
1377 next_credit);
1378
1379 return true;
1380 }
1381
1382 return false;
1383}
1384
Wei Liu73764192013-08-26 12:59:39 +01001385static unsigned xenvif_tx_build_gops(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001386{
Wei Liub3f980b2013-08-26 12:59:38 +01001387 struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
Ian Campbellf942dc22011-03-15 00:06:18 +00001388 struct sk_buff *skb;
1389 int ret;
1390
Wei Liub3f980b2013-08-26 12:59:38 +01001391 while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1392 < MAX_PENDING_REQS)) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001393 struct xen_netif_tx_request txreq;
Wei Liu37641492013-05-02 00:43:59 +00001394 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
Ian Campbellf942dc22011-03-15 00:06:18 +00001395 struct page *page;
1396 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
1397 u16 pending_idx;
1398 RING_IDX idx;
1399 int work_to_do;
1400 unsigned int data_len;
1401 pending_ring_idx_t index;
1402
Ian Campbell488562862013-02-06 23:41:35 +00001403 if (vif->tx.sring->req_prod - vif->tx.req_cons >
1404 XEN_NETIF_TX_RING_SIZE) {
1405 netdev_err(vif->dev,
1406 "Impossible number of requests. "
1407 "req_prod %d, req_cons %d, size %ld\n",
1408 vif->tx.sring->req_prod, vif->tx.req_cons,
1409 XEN_NETIF_TX_RING_SIZE);
Wei Liu73764192013-08-26 12:59:39 +01001410 xenvif_fatal_tx_err(vif);
Ian Campbell488562862013-02-06 23:41:35 +00001411 continue;
1412 }
1413
Ian Campbellf942dc22011-03-15 00:06:18 +00001414 RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
Wei Liub3f980b2013-08-26 12:59:38 +01001415 if (!work_to_do)
1416 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001417
1418 idx = vif->tx.req_cons;
1419 rmb(); /* Ensure that we see the request before we copy it. */
1420 memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
1421
1422 /* Credit-based scheduling. */
1423 if (txreq.size > vif->remaining_credit &&
Wei Liub3f980b2013-08-26 12:59:38 +01001424 tx_credit_exceeded(vif, txreq.size))
1425 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001426
1427 vif->remaining_credit -= txreq.size;
1428
1429 work_to_do--;
1430 vif->tx.req_cons = ++idx;
1431
1432 memset(extras, 0, sizeof(extras));
1433 if (txreq.flags & XEN_NETTXF_extra_info) {
Wei Liu73764192013-08-26 12:59:39 +01001434 work_to_do = xenvif_get_extras(vif, extras,
1435 work_to_do);
Ian Campbellf942dc22011-03-15 00:06:18 +00001436 idx = vif->tx.req_cons;
Ian Campbell488562862013-02-06 23:41:35 +00001437 if (unlikely(work_to_do < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001438 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001439 }
1440
Wei Liu73764192013-08-26 12:59:39 +01001441 ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
Ian Campbell488562862013-02-06 23:41:35 +00001442 if (unlikely(ret < 0))
Wei Liub3f980b2013-08-26 12:59:38 +01001443 break;
Ian Campbell488562862013-02-06 23:41:35 +00001444
Ian Campbellf942dc22011-03-15 00:06:18 +00001445 idx += ret;
1446
1447 if (unlikely(txreq.size < ETH_HLEN)) {
1448 netdev_dbg(vif->dev,
1449 "Bad packet size: %d\n", txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001450 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001451 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001452 }
1453
1454 /* No crossing a page as the payload mustn't fragment. */
1455 if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
Ian Campbell488562862013-02-06 23:41:35 +00001456 netdev_err(vif->dev,
Ian Campbellf942dc22011-03-15 00:06:18 +00001457 "txreq.offset: %x, size: %u, end: %lu\n",
1458 txreq.offset, txreq.size,
1459 (txreq.offset&~PAGE_MASK) + txreq.size);
Wei Liu73764192013-08-26 12:59:39 +01001460 xenvif_fatal_tx_err(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001461 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001462 }
1463
Wei Liub3f980b2013-08-26 12:59:38 +01001464 index = pending_index(vif->pending_cons);
1465 pending_idx = vif->pending_ring[index];
Ian Campbellf942dc22011-03-15 00:06:18 +00001466
1467 data_len = (txreq.size > PKT_PROT_LEN &&
Wei Liu37641492013-05-02 00:43:59 +00001468 ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
Ian Campbellf942dc22011-03-15 00:06:18 +00001469 PKT_PROT_LEN : txreq.size;
1470
1471 skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
1472 GFP_ATOMIC | __GFP_NOWARN);
1473 if (unlikely(skb == NULL)) {
1474 netdev_dbg(vif->dev,
1475 "Can't allocate a skb in start_xmit.\n");
Wei Liu73764192013-08-26 12:59:39 +01001476 xenvif_tx_err(vif, &txreq, idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001477 break;
1478 }
1479
1480 /* Packets passed to netif_rx() must have some headroom. */
1481 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
1482
1483 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1484 struct xen_netif_extra_info *gso;
1485 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1486
Wei Liu73764192013-08-26 12:59:39 +01001487 if (xenvif_set_skb_gso(vif, skb, gso)) {
1488 /* Failure in xenvif_set_skb_gso is fatal. */
Ian Campbellf942dc22011-03-15 00:06:18 +00001489 kfree_skb(skb);
Wei Liub3f980b2013-08-26 12:59:38 +01001490 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001491 }
1492 }
1493
1494 /* XXX could copy straight to head */
Wei Liu73764192013-08-26 12:59:39 +01001495 page = xenvif_alloc_page(vif, pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001496 if (!page) {
1497 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001498 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001499 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001500 }
1501
Ian Campbellf942dc22011-03-15 00:06:18 +00001502 gop->source.u.ref = txreq.gref;
1503 gop->source.domid = vif->domid;
1504 gop->source.offset = txreq.offset;
1505
1506 gop->dest.u.gmfn = virt_to_mfn(page_address(page));
1507 gop->dest.domid = DOMID_SELF;
1508 gop->dest.offset = txreq.offset;
1509
1510 gop->len = txreq.size;
1511 gop->flags = GNTCOPY_source_gref;
1512
1513 gop++;
1514
Wei Liub3f980b2013-08-26 12:59:38 +01001515 memcpy(&vif->pending_tx_info[pending_idx].req,
Ian Campbellf942dc22011-03-15 00:06:18 +00001516 &txreq, sizeof(txreq));
Wei Liub3f980b2013-08-26 12:59:38 +01001517 vif->pending_tx_info[pending_idx].head = index;
Ian Campbellf942dc22011-03-15 00:06:18 +00001518 *((u16 *)skb->data) = pending_idx;
1519
1520 __skb_put(skb, data_len);
1521
1522 skb_shinfo(skb)->nr_frags = ret;
1523 if (data_len < txreq.size) {
1524 skb_shinfo(skb)->nr_frags++;
Ian Campbellea066ad2011-10-05 00:28:46 +00001525 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1526 pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001527 } else {
Ian Campbellea066ad2011-10-05 00:28:46 +00001528 frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
1529 INVALID_PENDING_IDX);
Ian Campbellf942dc22011-03-15 00:06:18 +00001530 }
1531
Wei Liub3f980b2013-08-26 12:59:38 +01001532 vif->pending_cons++;
Ian Campbellf942dc22011-03-15 00:06:18 +00001533
Wei Liu73764192013-08-26 12:59:39 +01001534 request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
Ian Campbellf942dc22011-03-15 00:06:18 +00001535 if (request_gop == NULL) {
1536 kfree_skb(skb);
Wei Liu73764192013-08-26 12:59:39 +01001537 xenvif_tx_err(vif, &txreq, idx);
Wei Liub3f980b2013-08-26 12:59:38 +01001538 break;
Ian Campbellf942dc22011-03-15 00:06:18 +00001539 }
1540 gop = request_gop;
1541
Wei Liub3f980b2013-08-26 12:59:38 +01001542 __skb_queue_tail(&vif->tx_queue, skb);
Annie Li1e0b6ea2012-06-27 00:46:58 +00001543
Ian Campbellf942dc22011-03-15 00:06:18 +00001544 vif->tx.req_cons = idx;
Ian Campbellf942dc22011-03-15 00:06:18 +00001545
Wei Liub3f980b2013-08-26 12:59:38 +01001546 if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
Ian Campbellf942dc22011-03-15 00:06:18 +00001547 break;
1548 }
1549
Wei Liub3f980b2013-08-26 12:59:38 +01001550 return gop - vif->tx_copy_ops;
Ian Campbellf942dc22011-03-15 00:06:18 +00001551}
1552
Ian Campbellf942dc22011-03-15 00:06:18 +00001553
Wei Liu73764192013-08-26 12:59:39 +01001554static int xenvif_tx_submit(struct xenvif *vif, int budget)
Wei Liub3f980b2013-08-26 12:59:38 +01001555{
1556 struct gnttab_copy *gop = vif->tx_copy_ops;
1557 struct sk_buff *skb;
1558 int work_done = 0;
1559
1560 while (work_done < budget &&
1561 (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001562 struct xen_netif_tx_request *txp;
Ian Campbellf942dc22011-03-15 00:06:18 +00001563 u16 pending_idx;
1564 unsigned data_len;
1565
1566 pending_idx = *((u16 *)skb->data);
Wei Liub3f980b2013-08-26 12:59:38 +01001567 txp = &vif->pending_tx_info[pending_idx].req;
Ian Campbellf942dc22011-03-15 00:06:18 +00001568
1569 /* Check the remap error code. */
Wei Liu73764192013-08-26 12:59:39 +01001570 if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001571 netdev_dbg(vif->dev, "netback grant failed.\n");
1572 skb_shinfo(skb)->nr_frags = 0;
1573 kfree_skb(skb);
1574 continue;
1575 }
1576
1577 data_len = skb->len;
1578 memcpy(skb->data,
Wei Liub3f980b2013-08-26 12:59:38 +01001579 (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
Ian Campbellf942dc22011-03-15 00:06:18 +00001580 data_len);
1581 if (data_len < txp->size) {
1582 /* Append the packet payload as a fragment. */
1583 txp->offset += data_len;
1584 txp->size -= data_len;
1585 } else {
1586 /* Schedule a response immediately. */
Wei Liu73764192013-08-26 12:59:39 +01001587 xenvif_idx_release(vif, pending_idx,
1588 XEN_NETIF_RSP_OKAY);
Ian Campbellf942dc22011-03-15 00:06:18 +00001589 }
1590
1591 if (txp->flags & XEN_NETTXF_csum_blank)
1592 skb->ip_summed = CHECKSUM_PARTIAL;
1593 else if (txp->flags & XEN_NETTXF_data_validated)
1594 skb->ip_summed = CHECKSUM_UNNECESSARY;
1595
Wei Liu73764192013-08-26 12:59:39 +01001596 xenvif_fill_frags(vif, skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001597
Paul Durrant2eba61d2013-10-16 17:50:29 +01001598 if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
Ian Campbellf942dc22011-03-15 00:06:18 +00001599 int target = min_t(int, skb->len, PKT_PROT_LEN);
1600 __pskb_pull_tail(skb, target - skb_headlen(skb));
1601 }
1602
1603 skb->dev = vif->dev;
1604 skb->protocol = eth_type_trans(skb, skb->dev);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001605 skb_reset_network_header(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001606
1607 if (checksum_setup(vif, skb)) {
1608 netdev_dbg(vif->dev,
1609 "Can't setup checksum in net_tx_action\n");
1610 kfree_skb(skb);
1611 continue;
1612 }
1613
Jason Wang40893fd2013-03-26 23:11:22 +00001614 skb_probe_transport_header(skb, 0);
Jason Wangf9ca8f72013-03-25 20:19:58 +00001615
Ian Campbellf942dc22011-03-15 00:06:18 +00001616 vif->dev->stats.rx_bytes += skb->len;
1617 vif->dev->stats.rx_packets++;
1618
Wei Liub3f980b2013-08-26 12:59:38 +01001619 work_done++;
1620
1621 netif_receive_skb(skb);
Ian Campbellf942dc22011-03-15 00:06:18 +00001622 }
Wei Liub3f980b2013-08-26 12:59:38 +01001623
1624 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001625}
1626
1627/* Called after netfront has transmitted */
Wei Liu73764192013-08-26 12:59:39 +01001628int xenvif_tx_action(struct xenvif *vif, int budget)
Ian Campbellf942dc22011-03-15 00:06:18 +00001629{
1630 unsigned nr_gops;
Wei Liub3f980b2013-08-26 12:59:38 +01001631 int work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001632
Wei Liub3f980b2013-08-26 12:59:38 +01001633 if (unlikely(!tx_work_todo(vif)))
1634 return 0;
1635
Wei Liu73764192013-08-26 12:59:39 +01001636 nr_gops = xenvif_tx_build_gops(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001637
1638 if (nr_gops == 0)
Wei Liub3f980b2013-08-26 12:59:38 +01001639 return 0;
Andres Lagar-Cavillac5718982012-09-14 14:26:59 +00001640
Wei Liub3f980b2013-08-26 12:59:38 +01001641 gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
Ian Campbellf942dc22011-03-15 00:06:18 +00001642
Wei Liu73764192013-08-26 12:59:39 +01001643 work_done = xenvif_tx_submit(vif, nr_gops);
Wei Liub3f980b2013-08-26 12:59:38 +01001644
1645 return work_done;
Ian Campbellf942dc22011-03-15 00:06:18 +00001646}
1647
Wei Liu73764192013-08-26 12:59:39 +01001648static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
1649 u8 status)
Ian Campbellf942dc22011-03-15 00:06:18 +00001650{
Ian Campbellf942dc22011-03-15 00:06:18 +00001651 struct pending_tx_info *pending_tx_info;
Wei Liu2810e5b2013-04-22 02:20:42 +00001652 pending_ring_idx_t head;
1653 u16 peek; /* peek into next tx request */
1654
Wei Liub3f980b2013-08-26 12:59:38 +01001655 BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
Ian Campbellf942dc22011-03-15 00:06:18 +00001656
1657 /* Already complete? */
Wei Liub3f980b2013-08-26 12:59:38 +01001658 if (vif->mmap_pages[pending_idx] == NULL)
Ian Campbellf942dc22011-03-15 00:06:18 +00001659 return;
1660
Wei Liub3f980b2013-08-26 12:59:38 +01001661 pending_tx_info = &vif->pending_tx_info[pending_idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001662
Wei Liu2810e5b2013-04-22 02:20:42 +00001663 head = pending_tx_info->head;
Ian Campbellf942dc22011-03-15 00:06:18 +00001664
Wei Liub3f980b2013-08-26 12:59:38 +01001665 BUG_ON(!pending_tx_is_head(vif, head));
1666 BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
Ian Campbellf942dc22011-03-15 00:06:18 +00001667
Wei Liu2810e5b2013-04-22 02:20:42 +00001668 do {
1669 pending_ring_idx_t index;
1670 pending_ring_idx_t idx = pending_index(head);
Wei Liub3f980b2013-08-26 12:59:38 +01001671 u16 info_idx = vif->pending_ring[idx];
Ian Campbellf942dc22011-03-15 00:06:18 +00001672
Wei Liub3f980b2013-08-26 12:59:38 +01001673 pending_tx_info = &vif->pending_tx_info[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001674 make_tx_response(vif, &pending_tx_info->req, status);
Ian Campbellf942dc22011-03-15 00:06:18 +00001675
Wei Liu2810e5b2013-04-22 02:20:42 +00001676 /* Setting any number other than
1677 * INVALID_PENDING_RING_IDX indicates this slot is
1678 * starting a new packet / ending a previous packet.
1679 */
1680 pending_tx_info->head = 0;
1681
Wei Liub3f980b2013-08-26 12:59:38 +01001682 index = pending_index(vif->pending_prod++);
1683 vif->pending_ring[index] = vif->pending_ring[info_idx];
Wei Liu2810e5b2013-04-22 02:20:42 +00001684
Wei Liub3f980b2013-08-26 12:59:38 +01001685 peek = vif->pending_ring[pending_index(++head)];
Wei Liu2810e5b2013-04-22 02:20:42 +00001686
Wei Liub3f980b2013-08-26 12:59:38 +01001687 } while (!pending_tx_is_head(vif, peek));
Wei Liu2810e5b2013-04-22 02:20:42 +00001688
Wei Liub3f980b2013-08-26 12:59:38 +01001689 put_page(vif->mmap_pages[pending_idx]);
1690 vif->mmap_pages[pending_idx] = NULL;
Ian Campbellf942dc22011-03-15 00:06:18 +00001691}
1692
Wei Liu2810e5b2013-04-22 02:20:42 +00001693
Ian Campbellf942dc22011-03-15 00:06:18 +00001694static void make_tx_response(struct xenvif *vif,
1695 struct xen_netif_tx_request *txp,
1696 s8 st)
1697{
1698 RING_IDX i = vif->tx.rsp_prod_pvt;
1699 struct xen_netif_tx_response *resp;
1700 int notify;
1701
1702 resp = RING_GET_RESPONSE(&vif->tx, i);
1703 resp->id = txp->id;
1704 resp->status = st;
1705
1706 if (txp->flags & XEN_NETTXF_extra_info)
1707 RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1708
1709 vif->tx.rsp_prod_pvt = ++i;
1710 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
1711 if (notify)
Wei Liue1f00a692013-05-22 06:34:45 +00001712 notify_remote_via_irq(vif->tx_irq);
Ian Campbellf942dc22011-03-15 00:06:18 +00001713}
1714
1715static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
1716 u16 id,
1717 s8 st,
1718 u16 offset,
1719 u16 size,
1720 u16 flags)
1721{
1722 RING_IDX i = vif->rx.rsp_prod_pvt;
1723 struct xen_netif_rx_response *resp;
1724
1725 resp = RING_GET_RESPONSE(&vif->rx, i);
1726 resp->offset = offset;
1727 resp->flags = flags;
1728 resp->id = id;
1729 resp->status = (s16)size;
1730 if (st < 0)
1731 resp->status = (s16)st;
1732
1733 vif->rx.rsp_prod_pvt = ++i;
1734
1735 return resp;
1736}
1737
Wei Liub3f980b2013-08-26 12:59:38 +01001738static inline int rx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001739{
Wei Liub3f980b2013-08-26 12:59:38 +01001740 return !skb_queue_empty(&vif->rx_queue);
Ian Campbellf942dc22011-03-15 00:06:18 +00001741}
1742
Wei Liub3f980b2013-08-26 12:59:38 +01001743static inline int tx_work_todo(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001744{
1745
Wei Liub3f980b2013-08-26 12:59:38 +01001746 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
1747 (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
1748 < MAX_PENDING_REQS))
Ian Campbellf942dc22011-03-15 00:06:18 +00001749 return 1;
1750
1751 return 0;
1752}
1753
Wei Liu73764192013-08-26 12:59:39 +01001754void xenvif_unmap_frontend_rings(struct xenvif *vif)
Ian Campbellf942dc22011-03-15 00:06:18 +00001755{
David Vrabelc9d63692011-09-29 16:53:31 +01001756 if (vif->tx.sring)
1757 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1758 vif->tx.sring);
1759 if (vif->rx.sring)
1760 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
1761 vif->rx.sring);
Ian Campbellf942dc22011-03-15 00:06:18 +00001762}
1763
Wei Liu73764192013-08-26 12:59:39 +01001764int xenvif_map_frontend_rings(struct xenvif *vif,
1765 grant_ref_t tx_ring_ref,
1766 grant_ref_t rx_ring_ref)
Ian Campbellf942dc22011-03-15 00:06:18 +00001767{
David Vrabelc9d63692011-09-29 16:53:31 +01001768 void *addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001769 struct xen_netif_tx_sring *txs;
1770 struct xen_netif_rx_sring *rxs;
1771
1772 int err = -ENOMEM;
1773
David Vrabelc9d63692011-09-29 16:53:31 +01001774 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1775 tx_ring_ref, &addr);
1776 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001777 goto err;
1778
David Vrabelc9d63692011-09-29 16:53:31 +01001779 txs = (struct xen_netif_tx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001780 BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
1781
David Vrabelc9d63692011-09-29 16:53:31 +01001782 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
1783 rx_ring_ref, &addr);
1784 if (err)
Ian Campbellf942dc22011-03-15 00:06:18 +00001785 goto err;
Ian Campbellf942dc22011-03-15 00:06:18 +00001786
David Vrabelc9d63692011-09-29 16:53:31 +01001787 rxs = (struct xen_netif_rx_sring *)addr;
Ian Campbellf942dc22011-03-15 00:06:18 +00001788 BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
1789
David Vrabelc9d63692011-09-29 16:53:31 +01001790 vif->rx_req_cons_peek = 0;
1791
Ian Campbellf942dc22011-03-15 00:06:18 +00001792 return 0;
1793
1794err:
Wei Liu73764192013-08-26 12:59:39 +01001795 xenvif_unmap_frontend_rings(vif);
Ian Campbellf942dc22011-03-15 00:06:18 +00001796 return err;
1797}
1798
Wei Liu73764192013-08-26 12:59:39 +01001799int xenvif_kthread(void *data)
Wei Liub3f980b2013-08-26 12:59:38 +01001800{
1801 struct xenvif *vif = data;
1802
1803 while (!kthread_should_stop()) {
1804 wait_event_interruptible(vif->wq,
1805 rx_work_todo(vif) ||
1806 kthread_should_stop());
1807 if (kthread_should_stop())
1808 break;
1809
1810 if (rx_work_todo(vif))
Wei Liu73764192013-08-26 12:59:39 +01001811 xenvif_rx_action(vif);
Wei Liub3f980b2013-08-26 12:59:38 +01001812
1813 cond_resched();
1814 }
1815
1816 return 0;
1817}
1818
Ian Campbellf942dc22011-03-15 00:06:18 +00001819static int __init netback_init(void)
1820{
Ian Campbellf942dc22011-03-15 00:06:18 +00001821 int rc = 0;
Ian Campbellf942dc22011-03-15 00:06:18 +00001822
Daniel De Graaf2a14b2442011-12-14 15:12:13 -05001823 if (!xen_domain())
Ian Campbellf942dc22011-03-15 00:06:18 +00001824 return -ENODEV;
1825
Wei Liu37641492013-05-02 00:43:59 +00001826 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
Joe Perches383eda32013-06-27 21:57:49 -07001827 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1828 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
Wei Liu37641492013-05-02 00:43:59 +00001829 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
Wei Liu2810e5b2013-04-22 02:20:42 +00001830 }
1831
Ian Campbellf942dc22011-03-15 00:06:18 +00001832 rc = xenvif_xenbus_init();
1833 if (rc)
1834 goto failed_init;
1835
1836 return 0;
1837
1838failed_init:
Ian Campbellf942dc22011-03-15 00:06:18 +00001839 return rc;
Ian Campbellf942dc22011-03-15 00:06:18 +00001840}
1841
1842module_init(netback_init);
1843
Wei Liub103f352013-05-16 23:26:11 +00001844static void __exit netback_fini(void)
1845{
Wei Liub103f352013-05-16 23:26:11 +00001846 xenvif_xenbus_fini();
Wei Liub103f352013-05-16 23:26:11 +00001847}
1848module_exit(netback_fini);
1849
Ian Campbellf942dc22011-03-15 00:06:18 +00001850MODULE_LICENSE("Dual BSD/GPL");
Bastian Blankf984cec2011-06-30 11:19:09 -07001851MODULE_ALIAS("xen-backend:vif");