1 /*
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 #include <linux/highmem.h>
41
42 #include <net/tcp.h>
43
44 #include <xen/xen.h>
45 #include <xen/events.h>
46 #include <xen/interface/memory.h>
47 #include <xen/page.h>
48
49 #include <asm/xen/hypercall.h>
50
51 /* 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 */
55 bool separate_tx_rx_irq = true;
56 module_param(separate_tx_rx_irq, bool, 0644);
57
58 /* The time that packets can stay on the guest Rx internal queue
59 * before they are dropped.
60 */
61 unsigned int rx_drain_timeout_msecs = 10000;
62 module_param(rx_drain_timeout_msecs, uint, 0444);
63
64 /* The length of time before the frontend is considered unresponsive
65 * because it isn't providing Rx slots.
66 */
67 unsigned int rx_stall_timeout_msecs = 60000;
68 module_param(rx_stall_timeout_msecs, uint, 0444);
69
70 #define MAX_QUEUES_DEFAULT 8
71 unsigned int xenvif_max_queues;
72 module_param_named(max_queues, xenvif_max_queues, uint, 0644);
73 MODULE_PARM_DESC(max_queues,
74 "Maximum number of queues per virtual interface");
75
76 /*
77 * This is the maximum slots a skb can have. If a guest sends a skb
78 * which exceeds this limit it is considered malicious.
79 */
80 #define FATAL_SKB_SLOTS_DEFAULT 20
81 static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
82 module_param(fatal_skb_slots, uint, 0444);
83
84 /* The amount to copy out of the first guest Tx slot into the skb's
85 * linear area. If the first slot has more data, it will be mapped
86 * and put into the first frag.
87 *
88 * This is sized to avoid pulling headers from the frags for most
89 * TCP/IP packets.
90 */
91 #define XEN_NETBACK_TX_COPY_LEN 128
92
93 /* This is the maximum number of flows in the hash cache. */
94 #define XENVIF_HASH_CACHE_SIZE_DEFAULT 64
95 unsigned int xenvif_hash_cache_size = XENVIF_HASH_CACHE_SIZE_DEFAULT;
96 module_param_named(hash_cache_size, xenvif_hash_cache_size, uint, 0644);
97 MODULE_PARM_DESC(hash_cache_size, "Number of flows in the hash cache");
98
99 /* The module parameter tells that we have to put data
100 * for xen-netfront with the XDP_PACKET_HEADROOM offset
101 * needed for XDP processing
102 */
103 bool provides_xdp_headroom = true;
104 module_param(provides_xdp_headroom, bool, 0644);
105
106 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
107 u8 status);
108
109 static void make_tx_response(struct xenvif_queue *queue,
110 struct xen_netif_tx_request *txp,
111 unsigned int extra_count,
112 s8 st);
113 static void push_tx_responses(struct xenvif_queue *queue);
114
115 static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx);
116
117 static inline int tx_work_todo(struct xenvif_queue *queue);
118
idx_to_pfn(struct xenvif_queue * queue,u16 idx)119 static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
120 u16 idx)
121 {
122 return page_to_pfn(queue->mmap_pages[idx]);
123 }
124
idx_to_kaddr(struct xenvif_queue * queue,u16 idx)125 static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
126 u16 idx)
127 {
128 return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
129 }
130
131 #define callback_param(vif, pending_idx) \
132 (vif->pending_tx_info[pending_idx].callback_struct)
133
134 /* Find the containing VIF's structure from a pointer in pending_tx_info array
135 */
ubuf_to_queue(const struct ubuf_info * ubuf)136 static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
137 {
138 u16 pending_idx = ubuf->desc;
139 struct pending_tx_info *temp =
140 container_of(ubuf, struct pending_tx_info, callback_struct);
141 return container_of(temp - pending_idx,
142 struct xenvif_queue,
143 pending_tx_info[0]);
144 }
145
frag_get_pending_idx(skb_frag_t * frag)146 static u16 frag_get_pending_idx(skb_frag_t *frag)
147 {
148 return (u16)skb_frag_off(frag);
149 }
150
frag_set_pending_idx(skb_frag_t * frag,u16 pending_idx)151 static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
152 {
153 skb_frag_off_set(frag, pending_idx);
154 }
155
pending_index(unsigned i)156 static inline pending_ring_idx_t pending_index(unsigned i)
157 {
158 return i & (MAX_PENDING_REQS-1);
159 }
160
xenvif_kick_thread(struct xenvif_queue * queue)161 void xenvif_kick_thread(struct xenvif_queue *queue)
162 {
163 wake_up(&queue->wq);
164 }
165
xenvif_napi_schedule_or_enable_events(struct xenvif_queue * queue)166 void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
167 {
168 int more_to_do;
169
170 RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
171
172 if (more_to_do)
173 napi_schedule(&queue->napi);
174 else if (atomic_fetch_andnot(NETBK_TX_EOI | NETBK_COMMON_EOI,
175 &queue->eoi_pending) &
176 (NETBK_TX_EOI | NETBK_COMMON_EOI))
177 xen_irq_lateeoi(queue->tx_irq, 0);
178 }
179
tx_add_credit(struct xenvif_queue * queue)180 static void tx_add_credit(struct xenvif_queue *queue)
181 {
182 unsigned long max_burst, max_credit;
183
184 /*
185 * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
186 * Otherwise the interface can seize up due to insufficient credit.
187 */
188 max_burst = max(131072UL, queue->credit_bytes);
189
190 /* Take care that adding a new chunk of credit doesn't wrap to zero. */
191 max_credit = queue->remaining_credit + queue->credit_bytes;
192 if (max_credit < queue->remaining_credit)
193 max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
194
195 queue->remaining_credit = min(max_credit, max_burst);
196 queue->rate_limited = false;
197 }
198
xenvif_tx_credit_callback(struct timer_list * t)199 void xenvif_tx_credit_callback(struct timer_list *t)
200 {
201 struct xenvif_queue *queue = from_timer(queue, t, credit_timeout);
202 tx_add_credit(queue);
203 xenvif_napi_schedule_or_enable_events(queue);
204 }
205
xenvif_tx_err(struct xenvif_queue * queue,struct xen_netif_tx_request * txp,unsigned int extra_count,RING_IDX end)206 static void xenvif_tx_err(struct xenvif_queue *queue,
207 struct xen_netif_tx_request *txp,
208 unsigned int extra_count, RING_IDX end)
209 {
210 RING_IDX cons = queue->tx.req_cons;
211 unsigned long flags;
212
213 do {
214 spin_lock_irqsave(&queue->response_lock, flags);
215 make_tx_response(queue, txp, extra_count, XEN_NETIF_RSP_ERROR);
216 push_tx_responses(queue);
217 spin_unlock_irqrestore(&queue->response_lock, flags);
218 if (cons == end)
219 break;
220 RING_COPY_REQUEST(&queue->tx, cons++, txp);
221 extra_count = 0; /* only the first frag can have extras */
222 } while (1);
223 queue->tx.req_cons = cons;
224 }
225
xenvif_fatal_tx_err(struct xenvif * vif)226 static void xenvif_fatal_tx_err(struct xenvif *vif)
227 {
228 netdev_err(vif->dev, "fatal error; disabling device\n");
229 vif->disabled = true;
230 /* Disable the vif from queue 0's kthread */
231 if (vif->num_queues)
232 xenvif_kick_thread(&vif->queues[0]);
233 }
234
xenvif_count_requests(struct xenvif_queue * queue,struct xen_netif_tx_request * first,unsigned int extra_count,struct xen_netif_tx_request * txp,int work_to_do)235 static int xenvif_count_requests(struct xenvif_queue *queue,
236 struct xen_netif_tx_request *first,
237 unsigned int extra_count,
238 struct xen_netif_tx_request *txp,
239 int work_to_do)
240 {
241 RING_IDX cons = queue->tx.req_cons;
242 int slots = 0;
243 int drop_err = 0;
244 int more_data;
245
246 if (!(first->flags & XEN_NETTXF_more_data))
247 return 0;
248
249 do {
250 struct xen_netif_tx_request dropped_tx = { 0 };
251
252 if (slots >= work_to_do) {
253 netdev_err(queue->vif->dev,
254 "Asked for %d slots but exceeds this limit\n",
255 work_to_do);
256 xenvif_fatal_tx_err(queue->vif);
257 return -ENODATA;
258 }
259
260 /* This guest is really using too many slots and
261 * considered malicious.
262 */
263 if (unlikely(slots >= fatal_skb_slots)) {
264 netdev_err(queue->vif->dev,
265 "Malicious frontend using %d slots, threshold %u\n",
266 slots, fatal_skb_slots);
267 xenvif_fatal_tx_err(queue->vif);
268 return -E2BIG;
269 }
270
271 /* Xen network protocol had implicit dependency on
272 * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
273 * the historical MAX_SKB_FRAGS value 18 to honor the
274 * same behavior as before. Any packet using more than
275 * 18 slots but less than fatal_skb_slots slots is
276 * dropped
277 */
278 if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
279 if (net_ratelimit())
280 netdev_dbg(queue->vif->dev,
281 "Too many slots (%d) exceeding limit (%d), dropping packet\n",
282 slots, XEN_NETBK_LEGACY_SLOTS_MAX);
283 drop_err = -E2BIG;
284 }
285
286 if (drop_err)
287 txp = &dropped_tx;
288
289 RING_COPY_REQUEST(&queue->tx, cons + slots, txp);
290
291 /* If the guest submitted a frame >= 64 KiB then
292 * first->size overflowed and following slots will
293 * appear to be larger than the frame.
294 *
295 * This cannot be fatal error as there are buggy
296 * frontends that do this.
297 *
298 * Consume all slots and drop the packet.
299 */
300 if (!drop_err && txp->size > first->size) {
301 if (net_ratelimit())
302 netdev_dbg(queue->vif->dev,
303 "Invalid tx request, slot size %u > remaining size %u\n",
304 txp->size, first->size);
305 drop_err = -EIO;
306 }
307
308 first->size -= txp->size;
309 slots++;
310
311 if (unlikely((txp->offset + txp->size) > XEN_PAGE_SIZE)) {
312 netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
313 txp->offset, txp->size);
314 xenvif_fatal_tx_err(queue->vif);
315 return -EINVAL;
316 }
317
318 more_data = txp->flags & XEN_NETTXF_more_data;
319
320 if (!drop_err)
321 txp++;
322
323 } while (more_data);
324
325 if (drop_err) {
326 xenvif_tx_err(queue, first, extra_count, cons + slots);
327 return drop_err;
328 }
329
330 return slots;
331 }
332
333
334 struct xenvif_tx_cb {
335 u16 copy_pending_idx[XEN_NETBK_LEGACY_SLOTS_MAX + 1];
336 u8 copy_count;
337 u32 split_mask;
338 };
339
340 #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
341 #define copy_pending_idx(skb, i) (XENVIF_TX_CB(skb)->copy_pending_idx[i])
342 #define copy_count(skb) (XENVIF_TX_CB(skb)->copy_count)
343
xenvif_tx_create_map_op(struct xenvif_queue * queue,u16 pending_idx,struct xen_netif_tx_request * txp,unsigned int extra_count,struct gnttab_map_grant_ref * mop)344 static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
345 u16 pending_idx,
346 struct xen_netif_tx_request *txp,
347 unsigned int extra_count,
348 struct gnttab_map_grant_ref *mop)
349 {
350 queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
351 gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
352 GNTMAP_host_map | GNTMAP_readonly,
353 txp->gref, queue->vif->domid);
354
355 memcpy(&queue->pending_tx_info[pending_idx].req, txp,
356 sizeof(*txp));
357 queue->pending_tx_info[pending_idx].extra_count = extra_count;
358 }
359
xenvif_alloc_skb(unsigned int size)360 static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
361 {
362 struct sk_buff *skb =
363 alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
364 GFP_ATOMIC | __GFP_NOWARN);
365
366 BUILD_BUG_ON(sizeof(*XENVIF_TX_CB(skb)) > sizeof(skb->cb));
367 if (unlikely(skb == NULL))
368 return NULL;
369
370 /* Packets passed to netif_rx() must have some headroom. */
371 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
372
373 /* Initialize it here to avoid later surprises */
374 skb_shinfo(skb)->destructor_arg = NULL;
375
376 return skb;
377 }
378
xenvif_get_requests(struct xenvif_queue * queue,struct sk_buff * skb,struct xen_netif_tx_request * first,struct xen_netif_tx_request * txfrags,unsigned * copy_ops,unsigned * map_ops,unsigned int frag_overflow,struct sk_buff * nskb,unsigned int extra_count,unsigned int data_len)379 static void xenvif_get_requests(struct xenvif_queue *queue,
380 struct sk_buff *skb,
381 struct xen_netif_tx_request *first,
382 struct xen_netif_tx_request *txfrags,
383 unsigned *copy_ops,
384 unsigned *map_ops,
385 unsigned int frag_overflow,
386 struct sk_buff *nskb,
387 unsigned int extra_count,
388 unsigned int data_len)
389 {
390 struct skb_shared_info *shinfo = skb_shinfo(skb);
391 skb_frag_t *frags = shinfo->frags;
392 u16 pending_idx;
393 pending_ring_idx_t index;
394 unsigned int nr_slots;
395 struct gnttab_copy *cop = queue->tx_copy_ops + *copy_ops;
396 struct gnttab_map_grant_ref *gop = queue->tx_map_ops + *map_ops;
397 struct xen_netif_tx_request *txp = first;
398
399 nr_slots = shinfo->nr_frags + frag_overflow + 1;
400
401 copy_count(skb) = 0;
402 XENVIF_TX_CB(skb)->split_mask = 0;
403
404 /* Create copy ops for exactly data_len bytes into the skb head. */
405 __skb_put(skb, data_len);
406 while (data_len > 0) {
407 int amount = data_len > txp->size ? txp->size : data_len;
408 bool split = false;
409
410 cop->source.u.ref = txp->gref;
411 cop->source.domid = queue->vif->domid;
412 cop->source.offset = txp->offset;
413
414 cop->dest.domid = DOMID_SELF;
415 cop->dest.offset = (offset_in_page(skb->data +
416 skb_headlen(skb) -
417 data_len)) & ~XEN_PAGE_MASK;
418 cop->dest.u.gmfn = virt_to_gfn(skb->data + skb_headlen(skb)
419 - data_len);
420
421 /* Don't cross local page boundary! */
422 if (cop->dest.offset + amount > XEN_PAGE_SIZE) {
423 amount = XEN_PAGE_SIZE - cop->dest.offset;
424 XENVIF_TX_CB(skb)->split_mask |= 1U << copy_count(skb);
425 split = true;
426 }
427
428 cop->len = amount;
429 cop->flags = GNTCOPY_source_gref;
430
431 index = pending_index(queue->pending_cons);
432 pending_idx = queue->pending_ring[index];
433 callback_param(queue, pending_idx).ctx = NULL;
434 copy_pending_idx(skb, copy_count(skb)) = pending_idx;
435 if (!split)
436 copy_count(skb)++;
437
438 cop++;
439 data_len -= amount;
440
441 if (amount == txp->size) {
442 /* The copy op covered the full tx_request */
443
444 memcpy(&queue->pending_tx_info[pending_idx].req,
445 txp, sizeof(*txp));
446 queue->pending_tx_info[pending_idx].extra_count =
447 (txp == first) ? extra_count : 0;
448
449 if (txp == first)
450 txp = txfrags;
451 else
452 txp++;
453 queue->pending_cons++;
454 nr_slots--;
455 } else {
456 /* The copy op partially covered the tx_request.
457 * The remainder will be mapped or copied in the next
458 * iteration.
459 */
460 txp->offset += amount;
461 txp->size -= amount;
462 }
463 }
464
465 for (shinfo->nr_frags = 0; nr_slots > 0 && shinfo->nr_frags < MAX_SKB_FRAGS;
466 nr_slots--) {
467 if (unlikely(!txp->size)) {
468 unsigned long flags;
469
470 spin_lock_irqsave(&queue->response_lock, flags);
471 make_tx_response(queue, txp, 0, XEN_NETIF_RSP_OKAY);
472 push_tx_responses(queue);
473 spin_unlock_irqrestore(&queue->response_lock, flags);
474 ++txp;
475 continue;
476 }
477
478 index = pending_index(queue->pending_cons++);
479 pending_idx = queue->pending_ring[index];
480 xenvif_tx_create_map_op(queue, pending_idx, txp,
481 txp == first ? extra_count : 0, gop);
482 frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
483 ++shinfo->nr_frags;
484 ++gop;
485
486 if (txp == first)
487 txp = txfrags;
488 else
489 txp++;
490 }
491
492 if (nr_slots > 0) {
493
494 shinfo = skb_shinfo(nskb);
495 frags = shinfo->frags;
496
497 for (shinfo->nr_frags = 0; shinfo->nr_frags < nr_slots; ++txp) {
498 if (unlikely(!txp->size)) {
499 unsigned long flags;
500
501 spin_lock_irqsave(&queue->response_lock, flags);
502 make_tx_response(queue, txp, 0,
503 XEN_NETIF_RSP_OKAY);
504 push_tx_responses(queue);
505 spin_unlock_irqrestore(&queue->response_lock,
506 flags);
507 continue;
508 }
509
510 index = pending_index(queue->pending_cons++);
511 pending_idx = queue->pending_ring[index];
512 xenvif_tx_create_map_op(queue, pending_idx, txp, 0,
513 gop);
514 frag_set_pending_idx(&frags[shinfo->nr_frags],
515 pending_idx);
516 ++shinfo->nr_frags;
517 ++gop;
518 }
519
520 if (shinfo->nr_frags) {
521 skb_shinfo(skb)->frag_list = nskb;
522 nskb = NULL;
523 }
524 }
525
526 if (nskb) {
527 /* A frag_list skb was allocated but it is no longer needed
528 * because enough slots were converted to copy ops above or some
529 * were empty.
530 */
531 kfree_skb(nskb);
532 }
533
534 (*copy_ops) = cop - queue->tx_copy_ops;
535 (*map_ops) = gop - queue->tx_map_ops;
536 }
537
xenvif_grant_handle_set(struct xenvif_queue * queue,u16 pending_idx,grant_handle_t handle)538 static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
539 u16 pending_idx,
540 grant_handle_t handle)
541 {
542 if (unlikely(queue->grant_tx_handle[pending_idx] !=
543 NETBACK_INVALID_HANDLE)) {
544 netdev_err(queue->vif->dev,
545 "Trying to overwrite active handle! pending_idx: 0x%x\n",
546 pending_idx);
547 BUG();
548 }
549 queue->grant_tx_handle[pending_idx] = handle;
550 }
551
xenvif_grant_handle_reset(struct xenvif_queue * queue,u16 pending_idx)552 static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
553 u16 pending_idx)
554 {
555 if (unlikely(queue->grant_tx_handle[pending_idx] ==
556 NETBACK_INVALID_HANDLE)) {
557 netdev_err(queue->vif->dev,
558 "Trying to unmap invalid handle! pending_idx: 0x%x\n",
559 pending_idx);
560 BUG();
561 }
562 queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
563 }
564
xenvif_tx_check_gop(struct xenvif_queue * queue,struct sk_buff * skb,struct gnttab_map_grant_ref ** gopp_map,struct gnttab_copy ** gopp_copy)565 static int xenvif_tx_check_gop(struct xenvif_queue *queue,
566 struct sk_buff *skb,
567 struct gnttab_map_grant_ref **gopp_map,
568 struct gnttab_copy **gopp_copy)
569 {
570 struct gnttab_map_grant_ref *gop_map = *gopp_map;
571 u16 pending_idx;
572 /* This always points to the shinfo of the skb being checked, which
573 * could be either the first or the one on the frag_list
574 */
575 struct skb_shared_info *shinfo = skb_shinfo(skb);
576 /* If this is non-NULL, we are currently checking the frag_list skb, and
577 * this points to the shinfo of the first one
578 */
579 struct skb_shared_info *first_shinfo = NULL;
580 int nr_frags = shinfo->nr_frags;
581 const bool sharedslot = nr_frags &&
582 frag_get_pending_idx(&shinfo->frags[0]) ==
583 copy_pending_idx(skb, copy_count(skb) - 1);
584 int i, err = 0;
585
586 for (i = 0; i < copy_count(skb); i++) {
587 int newerr;
588
589 /* Check status of header. */
590 pending_idx = copy_pending_idx(skb, i);
591
592 newerr = (*gopp_copy)->status;
593
594 /* Split copies need to be handled together. */
595 if (XENVIF_TX_CB(skb)->split_mask & (1U << i)) {
596 (*gopp_copy)++;
597 if (!newerr)
598 newerr = (*gopp_copy)->status;
599 }
600 if (likely(!newerr)) {
601 /* The first frag might still have this slot mapped */
602 if (i < copy_count(skb) - 1 || !sharedslot)
603 xenvif_idx_release(queue, pending_idx,
604 XEN_NETIF_RSP_OKAY);
605 } else {
606 err = newerr;
607 if (net_ratelimit())
608 netdev_dbg(queue->vif->dev,
609 "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
610 (*gopp_copy)->status,
611 pending_idx,
612 (*gopp_copy)->source.u.ref);
613 /* The first frag might still have this slot mapped */
614 if (i < copy_count(skb) - 1 || !sharedslot)
615 xenvif_idx_release(queue, pending_idx,
616 XEN_NETIF_RSP_ERROR);
617 }
618 (*gopp_copy)++;
619 }
620
621 check_frags:
622 for (i = 0; i < nr_frags; i++, gop_map++) {
623 int j, newerr;
624
625 pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
626
627 /* Check error status: if okay then remember grant handle. */
628 newerr = gop_map->status;
629
630 if (likely(!newerr)) {
631 xenvif_grant_handle_set(queue,
632 pending_idx,
633 gop_map->handle);
634 /* Had a previous error? Invalidate this fragment. */
635 if (unlikely(err)) {
636 xenvif_idx_unmap(queue, pending_idx);
637 /* If the mapping of the first frag was OK, but
638 * the header's copy failed, and they are
639 * sharing a slot, send an error
640 */
641 if (i == 0 && !first_shinfo && sharedslot)
642 xenvif_idx_release(queue, pending_idx,
643 XEN_NETIF_RSP_ERROR);
644 else
645 xenvif_idx_release(queue, pending_idx,
646 XEN_NETIF_RSP_OKAY);
647 }
648 continue;
649 }
650
651 /* Error on this fragment: respond to client with an error. */
652 if (net_ratelimit())
653 netdev_dbg(queue->vif->dev,
654 "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
655 i,
656 gop_map->status,
657 pending_idx,
658 gop_map->ref);
659
660 xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
661
662 /* Not the first error? Preceding frags already invalidated. */
663 if (err)
664 continue;
665
666 /* Invalidate preceding fragments of this skb. */
667 for (j = 0; j < i; j++) {
668 pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
669 xenvif_idx_unmap(queue, pending_idx);
670 xenvif_idx_release(queue, pending_idx,
671 XEN_NETIF_RSP_OKAY);
672 }
673
674 /* And if we found the error while checking the frag_list, unmap
675 * the first skb's frags
676 */
677 if (first_shinfo) {
678 for (j = 0; j < first_shinfo->nr_frags; j++) {
679 pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
680 xenvif_idx_unmap(queue, pending_idx);
681 xenvif_idx_release(queue, pending_idx,
682 XEN_NETIF_RSP_OKAY);
683 }
684 }
685
686 /* Remember the error: invalidate all subsequent fragments. */
687 err = newerr;
688 }
689
690 if (skb_has_frag_list(skb) && !first_shinfo) {
691 first_shinfo = skb_shinfo(skb);
692 shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
693 nr_frags = shinfo->nr_frags;
694
695 goto check_frags;
696 }
697
698 *gopp_map = gop_map;
699 return err;
700 }
701
xenvif_fill_frags(struct xenvif_queue * queue,struct sk_buff * skb)702 static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
703 {
704 struct skb_shared_info *shinfo = skb_shinfo(skb);
705 int nr_frags = shinfo->nr_frags;
706 int i;
707 u16 prev_pending_idx = INVALID_PENDING_IDX;
708
709 for (i = 0; i < nr_frags; i++) {
710 skb_frag_t *frag = shinfo->frags + i;
711 struct xen_netif_tx_request *txp;
712 struct page *page;
713 u16 pending_idx;
714
715 pending_idx = frag_get_pending_idx(frag);
716
717 /* If this is not the first frag, chain it to the previous*/
718 if (prev_pending_idx == INVALID_PENDING_IDX)
719 skb_shinfo(skb)->destructor_arg =
720 &callback_param(queue, pending_idx);
721 else
722 callback_param(queue, prev_pending_idx).ctx =
723 &callback_param(queue, pending_idx);
724
725 callback_param(queue, pending_idx).ctx = NULL;
726 prev_pending_idx = pending_idx;
727
728 txp = &queue->pending_tx_info[pending_idx].req;
729 page = virt_to_page(idx_to_kaddr(queue, pending_idx));
730 __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
731 skb->len += txp->size;
732 skb->data_len += txp->size;
733 skb->truesize += txp->size;
734
735 /* Take an extra reference to offset network stack's put_page */
736 get_page(queue->mmap_pages[pending_idx]);
737 }
738 }
739
xenvif_get_extras(struct xenvif_queue * queue,struct xen_netif_extra_info * extras,unsigned int * extra_count,int work_to_do)740 static int xenvif_get_extras(struct xenvif_queue *queue,
741 struct xen_netif_extra_info *extras,
742 unsigned int *extra_count,
743 int work_to_do)
744 {
745 struct xen_netif_extra_info extra;
746 RING_IDX cons = queue->tx.req_cons;
747
748 do {
749 if (unlikely(work_to_do-- <= 0)) {
750 netdev_err(queue->vif->dev, "Missing extra info\n");
751 xenvif_fatal_tx_err(queue->vif);
752 return -EBADR;
753 }
754
755 RING_COPY_REQUEST(&queue->tx, cons, &extra);
756
757 queue->tx.req_cons = ++cons;
758 (*extra_count)++;
759
760 if (unlikely(!extra.type ||
761 extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
762 netdev_err(queue->vif->dev,
763 "Invalid extra type: %d\n", extra.type);
764 xenvif_fatal_tx_err(queue->vif);
765 return -EINVAL;
766 }
767
768 memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
769 } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
770
771 return work_to_do;
772 }
773
xenvif_set_skb_gso(struct xenvif * vif,struct sk_buff * skb,struct xen_netif_extra_info * gso)774 static int xenvif_set_skb_gso(struct xenvif *vif,
775 struct sk_buff *skb,
776 struct xen_netif_extra_info *gso)
777 {
778 if (!gso->u.gso.size) {
779 netdev_err(vif->dev, "GSO size must not be zero.\n");
780 xenvif_fatal_tx_err(vif);
781 return -EINVAL;
782 }
783
784 switch (gso->u.gso.type) {
785 case XEN_NETIF_GSO_TYPE_TCPV4:
786 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
787 break;
788 case XEN_NETIF_GSO_TYPE_TCPV6:
789 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
790 break;
791 default:
792 netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
793 xenvif_fatal_tx_err(vif);
794 return -EINVAL;
795 }
796
797 skb_shinfo(skb)->gso_size = gso->u.gso.size;
798 /* gso_segs will be calculated later */
799
800 return 0;
801 }
802
checksum_setup(struct xenvif_queue * queue,struct sk_buff * skb)803 static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
804 {
805 bool recalculate_partial_csum = false;
806
807 /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
808 * peers can fail to set NETRXF_csum_blank when sending a GSO
809 * frame. In this case force the SKB to CHECKSUM_PARTIAL and
810 * recalculate the partial checksum.
811 */
812 if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
813 queue->stats.rx_gso_checksum_fixup++;
814 skb->ip_summed = CHECKSUM_PARTIAL;
815 recalculate_partial_csum = true;
816 }
817
818 /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
819 if (skb->ip_summed != CHECKSUM_PARTIAL)
820 return 0;
821
822 return skb_checksum_setup(skb, recalculate_partial_csum);
823 }
824
tx_credit_exceeded(struct xenvif_queue * queue,unsigned size)825 static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
826 {
827 u64 now = get_jiffies_64();
828 u64 next_credit = queue->credit_window_start +
829 msecs_to_jiffies(queue->credit_usec / 1000);
830
831 /* Timer could already be pending in rare cases. */
832 if (timer_pending(&queue->credit_timeout)) {
833 queue->rate_limited = true;
834 return true;
835 }
836
837 /* Passed the point where we can replenish credit? */
838 if (time_after_eq64(now, next_credit)) {
839 queue->credit_window_start = now;
840 tx_add_credit(queue);
841 }
842
843 /* Still too big to send right now? Set a callback. */
844 if (size > queue->remaining_credit) {
845 mod_timer(&queue->credit_timeout,
846 next_credit);
847 queue->credit_window_start = next_credit;
848 queue->rate_limited = true;
849
850 return true;
851 }
852
853 return false;
854 }
855
856 /* No locking is required in xenvif_mcast_add/del() as they are
857 * only ever invoked from NAPI poll. An RCU list is used because
858 * xenvif_mcast_match() is called asynchronously, during start_xmit.
859 */
860
xenvif_mcast_add(struct xenvif * vif,const u8 * addr)861 static int xenvif_mcast_add(struct xenvif *vif, const u8 *addr)
862 {
863 struct xenvif_mcast_addr *mcast;
864
865 if (vif->fe_mcast_count == XEN_NETBK_MCAST_MAX) {
866 if (net_ratelimit())
867 netdev_err(vif->dev,
868 "Too many multicast addresses\n");
869 return -ENOSPC;
870 }
871
872 mcast = kzalloc(sizeof(*mcast), GFP_ATOMIC);
873 if (!mcast)
874 return -ENOMEM;
875
876 ether_addr_copy(mcast->addr, addr);
877 list_add_tail_rcu(&mcast->entry, &vif->fe_mcast_addr);
878 vif->fe_mcast_count++;
879
880 return 0;
881 }
882
xenvif_mcast_del(struct xenvif * vif,const u8 * addr)883 static void xenvif_mcast_del(struct xenvif *vif, const u8 *addr)
884 {
885 struct xenvif_mcast_addr *mcast;
886
887 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
888 if (ether_addr_equal(addr, mcast->addr)) {
889 --vif->fe_mcast_count;
890 list_del_rcu(&mcast->entry);
891 kfree_rcu(mcast, rcu);
892 break;
893 }
894 }
895 }
896
xenvif_mcast_match(struct xenvif * vif,const u8 * addr)897 bool xenvif_mcast_match(struct xenvif *vif, const u8 *addr)
898 {
899 struct xenvif_mcast_addr *mcast;
900
901 rcu_read_lock();
902 list_for_each_entry_rcu(mcast, &vif->fe_mcast_addr, entry) {
903 if (ether_addr_equal(addr, mcast->addr)) {
904 rcu_read_unlock();
905 return true;
906 }
907 }
908 rcu_read_unlock();
909
910 return false;
911 }
912
xenvif_mcast_addr_list_free(struct xenvif * vif)913 void xenvif_mcast_addr_list_free(struct xenvif *vif)
914 {
915 /* No need for locking or RCU here. NAPI poll and TX queue
916 * are stopped.
917 */
918 while (!list_empty(&vif->fe_mcast_addr)) {
919 struct xenvif_mcast_addr *mcast;
920
921 mcast = list_first_entry(&vif->fe_mcast_addr,
922 struct xenvif_mcast_addr,
923 entry);
924 --vif->fe_mcast_count;
925 list_del(&mcast->entry);
926 kfree(mcast);
927 }
928 }
929
xenvif_tx_build_gops(struct xenvif_queue * queue,int budget,unsigned * copy_ops,unsigned * map_ops)930 static void xenvif_tx_build_gops(struct xenvif_queue *queue,
931 int budget,
932 unsigned *copy_ops,
933 unsigned *map_ops)
934 {
935 struct sk_buff *skb, *nskb;
936 int ret;
937 unsigned int frag_overflow;
938
939 while (skb_queue_len(&queue->tx_queue) < budget) {
940 struct xen_netif_tx_request txreq;
941 struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
942 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
943 unsigned int extra_count;
944 u16 pending_idx;
945 RING_IDX idx;
946 int work_to_do;
947 unsigned int data_len;
948 pending_ring_idx_t index;
949
950 if (queue->tx.sring->req_prod - queue->tx.req_cons >
951 XEN_NETIF_TX_RING_SIZE) {
952 netdev_err(queue->vif->dev,
953 "Impossible number of requests. "
954 "req_prod %d, req_cons %d, size %ld\n",
955 queue->tx.sring->req_prod, queue->tx.req_cons,
956 XEN_NETIF_TX_RING_SIZE);
957 xenvif_fatal_tx_err(queue->vif);
958 break;
959 }
960
961 work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
962 if (!work_to_do)
963 break;
964
965 idx = queue->tx.req_cons;
966 rmb(); /* Ensure that we see the request before we copy it. */
967 RING_COPY_REQUEST(&queue->tx, idx, &txreq);
968
969 /* Credit-based scheduling. */
970 if (txreq.size > queue->remaining_credit &&
971 tx_credit_exceeded(queue, txreq.size))
972 break;
973
974 queue->remaining_credit -= txreq.size;
975
976 work_to_do--;
977 queue->tx.req_cons = ++idx;
978
979 memset(extras, 0, sizeof(extras));
980 extra_count = 0;
981 if (txreq.flags & XEN_NETTXF_extra_info) {
982 work_to_do = xenvif_get_extras(queue, extras,
983 &extra_count,
984 work_to_do);
985 idx = queue->tx.req_cons;
986 if (unlikely(work_to_do < 0))
987 break;
988 }
989
990 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1].type) {
991 struct xen_netif_extra_info *extra;
992
993 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_ADD - 1];
994 ret = xenvif_mcast_add(queue->vif, extra->u.mcast.addr);
995
996 make_tx_response(queue, &txreq, extra_count,
997 (ret == 0) ?
998 XEN_NETIF_RSP_OKAY :
999 XEN_NETIF_RSP_ERROR);
1000 push_tx_responses(queue);
1001 continue;
1002 }
1003
1004 if (extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1].type) {
1005 struct xen_netif_extra_info *extra;
1006
1007 extra = &extras[XEN_NETIF_EXTRA_TYPE_MCAST_DEL - 1];
1008 xenvif_mcast_del(queue->vif, extra->u.mcast.addr);
1009
1010 make_tx_response(queue, &txreq, extra_count,
1011 XEN_NETIF_RSP_OKAY);
1012 push_tx_responses(queue);
1013 continue;
1014 }
1015
1016 data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN) ?
1017 XEN_NETBACK_TX_COPY_LEN : txreq.size;
1018
1019 ret = xenvif_count_requests(queue, &txreq, extra_count,
1020 txfrags, work_to_do);
1021
1022 if (unlikely(ret < 0))
1023 break;
1024
1025 idx += ret;
1026
1027 if (unlikely(txreq.size < ETH_HLEN)) {
1028 netdev_dbg(queue->vif->dev,
1029 "Bad packet size: %d\n", txreq.size);
1030 xenvif_tx_err(queue, &txreq, extra_count, idx);
1031 break;
1032 }
1033
1034 /* No crossing a page as the payload mustn't fragment. */
1035 if (unlikely((txreq.offset + txreq.size) > XEN_PAGE_SIZE)) {
1036 netdev_err(queue->vif->dev, "Cross page boundary, txreq.offset: %u, size: %u\n",
1037 txreq.offset, txreq.size);
1038 xenvif_fatal_tx_err(queue->vif);
1039 break;
1040 }
1041
1042 index = pending_index(queue->pending_cons);
1043 pending_idx = queue->pending_ring[index];
1044
1045 if (ret >= XEN_NETBK_LEGACY_SLOTS_MAX - 1 && data_len < txreq.size)
1046 data_len = txreq.size;
1047
1048 skb = xenvif_alloc_skb(data_len);
1049 if (unlikely(skb == NULL)) {
1050 netdev_dbg(queue->vif->dev,
1051 "Can't allocate a skb in start_xmit.\n");
1052 xenvif_tx_err(queue, &txreq, extra_count, idx);
1053 break;
1054 }
1055
1056 skb_shinfo(skb)->nr_frags = ret;
1057 /* At this point shinfo->nr_frags is in fact the number of
1058 * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
1059 */
1060 frag_overflow = 0;
1061 nskb = NULL;
1062 if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
1063 frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
1064 BUG_ON(frag_overflow > MAX_SKB_FRAGS);
1065 skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
1066 nskb = xenvif_alloc_skb(0);
1067 if (unlikely(nskb == NULL)) {
1068 skb_shinfo(skb)->nr_frags = 0;
1069 kfree_skb(skb);
1070 xenvif_tx_err(queue, &txreq, extra_count, idx);
1071 if (net_ratelimit())
1072 netdev_err(queue->vif->dev,
1073 "Can't allocate the frag_list skb.\n");
1074 break;
1075 }
1076 }
1077
1078 if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
1079 struct xen_netif_extra_info *gso;
1080 gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
1081
1082 if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
1083 /* Failure in xenvif_set_skb_gso is fatal. */
1084 skb_shinfo(skb)->nr_frags = 0;
1085 kfree_skb(skb);
1086 kfree_skb(nskb);
1087 break;
1088 }
1089 }
1090
1091 if (extras[XEN_NETIF_EXTRA_TYPE_HASH - 1].type) {
1092 struct xen_netif_extra_info *extra;
1093 enum pkt_hash_types type = PKT_HASH_TYPE_NONE;
1094
1095 extra = &extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
1096
1097 switch (extra->u.hash.type) {
1098 case _XEN_NETIF_CTRL_HASH_TYPE_IPV4:
1099 case _XEN_NETIF_CTRL_HASH_TYPE_IPV6:
1100 type = PKT_HASH_TYPE_L3;
1101 break;
1102
1103 case _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP:
1104 case _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP:
1105 type = PKT_HASH_TYPE_L4;
1106 break;
1107
1108 default:
1109 break;
1110 }
1111
1112 if (type != PKT_HASH_TYPE_NONE)
1113 skb_set_hash(skb,
1114 *(u32 *)extra->u.hash.value,
1115 type);
1116 }
1117
1118 xenvif_get_requests(queue, skb, &txreq, txfrags, copy_ops,
1119 map_ops, frag_overflow, nskb, extra_count,
1120 data_len);
1121
1122 __skb_queue_tail(&queue->tx_queue, skb);
1123
1124 queue->tx.req_cons = idx;
1125
1126 if ((*map_ops >= ARRAY_SIZE(queue->tx_map_ops)) ||
1127 (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
1128 break;
1129 }
1130
1131 return;
1132 }
1133
1134 /* Consolidate skb with a frag_list into a brand new one with local pages on
1135 * frags. Returns 0 or -ENOMEM if can't allocate new pages.
1136 */
xenvif_handle_frag_list(struct xenvif_queue * queue,struct sk_buff * skb)1137 static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
1138 {
1139 unsigned int offset = skb_headlen(skb);
1140 skb_frag_t frags[MAX_SKB_FRAGS];
1141 int i, f;
1142 struct ubuf_info *uarg;
1143 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1144
1145 queue->stats.tx_zerocopy_sent += 2;
1146 queue->stats.tx_frag_overflow++;
1147
1148 xenvif_fill_frags(queue, nskb);
1149 /* Subtract frags size, we will correct it later */
1150 skb->truesize -= skb->data_len;
1151 skb->len += nskb->len;
1152 skb->data_len += nskb->len;
1153
1154 /* create a brand new frags array and coalesce there */
1155 for (i = 0; offset < skb->len; i++) {
1156 struct page *page;
1157 unsigned int len;
1158
1159 BUG_ON(i >= MAX_SKB_FRAGS);
1160 page = alloc_page(GFP_ATOMIC);
1161 if (!page) {
1162 int j;
1163 skb->truesize += skb->data_len;
1164 for (j = 0; j < i; j++)
1165 put_page(skb_frag_page(&frags[j]));
1166 return -ENOMEM;
1167 }
1168
1169 if (offset + PAGE_SIZE < skb->len)
1170 len = PAGE_SIZE;
1171 else
1172 len = skb->len - offset;
1173 if (skb_copy_bits(skb, offset, page_address(page), len))
1174 BUG();
1175
1176 offset += len;
1177 __skb_frag_set_page(&frags[i], page);
1178 skb_frag_off_set(&frags[i], 0);
1179 skb_frag_size_set(&frags[i], len);
1180 }
1181
1182 /* Release all the original (foreign) frags. */
1183 for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
1184 skb_frag_unref(skb, f);
1185 uarg = skb_shinfo(skb)->destructor_arg;
1186 /* increase inflight counter to offset decrement in callback */
1187 atomic_inc(&queue->inflight_packets);
1188 uarg->callback(uarg, true);
1189 skb_shinfo(skb)->destructor_arg = NULL;
1190
1191 /* Fill the skb with the new (local) frags. */
1192 memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
1193 skb_shinfo(skb)->nr_frags = i;
1194 skb->truesize += i * PAGE_SIZE;
1195
1196 return 0;
1197 }
1198
xenvif_tx_submit(struct xenvif_queue * queue)1199 static int xenvif_tx_submit(struct xenvif_queue *queue)
1200 {
1201 struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
1202 struct gnttab_copy *gop_copy = queue->tx_copy_ops;
1203 struct sk_buff *skb;
1204 int work_done = 0;
1205
1206 while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
1207 struct xen_netif_tx_request *txp;
1208 u16 pending_idx;
1209
1210 pending_idx = copy_pending_idx(skb, 0);
1211 txp = &queue->pending_tx_info[pending_idx].req;
1212
1213 /* Check the remap error code. */
1214 if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
1215 /* If there was an error, xenvif_tx_check_gop is
1216 * expected to release all the frags which were mapped,
1217 * so kfree_skb shouldn't do it again
1218 */
1219 skb_shinfo(skb)->nr_frags = 0;
1220 if (skb_has_frag_list(skb)) {
1221 struct sk_buff *nskb =
1222 skb_shinfo(skb)->frag_list;
1223 skb_shinfo(nskb)->nr_frags = 0;
1224 }
1225 kfree_skb(skb);
1226 continue;
1227 }
1228
1229 if (txp->flags & XEN_NETTXF_csum_blank)
1230 skb->ip_summed = CHECKSUM_PARTIAL;
1231 else if (txp->flags & XEN_NETTXF_data_validated)
1232 skb->ip_summed = CHECKSUM_UNNECESSARY;
1233
1234 xenvif_fill_frags(queue, skb);
1235
1236 if (unlikely(skb_has_frag_list(skb))) {
1237 struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
1238 xenvif_skb_zerocopy_prepare(queue, nskb);
1239 if (xenvif_handle_frag_list(queue, skb)) {
1240 if (net_ratelimit())
1241 netdev_err(queue->vif->dev,
1242 "Not enough memory to consolidate frag_list!\n");
1243 xenvif_skb_zerocopy_prepare(queue, skb);
1244 kfree_skb(skb);
1245 continue;
1246 }
1247 /* Copied all the bits from the frag list -- free it. */
1248 skb_frag_list_init(skb);
1249 kfree_skb(nskb);
1250 }
1251
1252 skb->dev = queue->vif->dev;
1253 skb->protocol = eth_type_trans(skb, skb->dev);
1254 skb_reset_network_header(skb);
1255
1256 if (checksum_setup(queue, skb)) {
1257 netdev_dbg(queue->vif->dev,
1258 "Can't setup checksum in net_tx_action\n");
1259 /* We have to set this flag to trigger the callback */
1260 if (skb_shinfo(skb)->destructor_arg)
1261 xenvif_skb_zerocopy_prepare(queue, skb);
1262 kfree_skb(skb);
1263 continue;
1264 }
1265
1266 skb_probe_transport_header(skb);
1267
1268 /* If the packet is GSO then we will have just set up the
1269 * transport header offset in checksum_setup so it's now
1270 * straightforward to calculate gso_segs.
1271 */
1272 if (skb_is_gso(skb)) {
1273 int mss, hdrlen;
1274
1275 /* GSO implies having the L4 header. */
1276 WARN_ON_ONCE(!skb_transport_header_was_set(skb));
1277 if (unlikely(!skb_transport_header_was_set(skb))) {
1278 kfree_skb(skb);
1279 continue;
1280 }
1281
1282 mss = skb_shinfo(skb)->gso_size;
1283 hdrlen = skb_transport_header(skb) -
1284 skb_mac_header(skb) +
1285 tcp_hdrlen(skb);
1286
1287 skb_shinfo(skb)->gso_segs =
1288 DIV_ROUND_UP(skb->len - hdrlen, mss);
1289 }
1290
1291 queue->stats.rx_bytes += skb->len;
1292 queue->stats.rx_packets++;
1293
1294 work_done++;
1295
1296 /* Set this flag right before netif_receive_skb, otherwise
1297 * someone might think this packet already left netback, and
1298 * do a skb_copy_ubufs while we are still in control of the
1299 * skb. E.g. the __pskb_pull_tail earlier can do such thing.
1300 */
1301 if (skb_shinfo(skb)->destructor_arg) {
1302 xenvif_skb_zerocopy_prepare(queue, skb);
1303 queue->stats.tx_zerocopy_sent++;
1304 }
1305
1306 netif_receive_skb(skb);
1307 }
1308
1309 return work_done;
1310 }
1311
xenvif_zerocopy_callback(struct ubuf_info * ubuf,bool zerocopy_success)1312 void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
1313 {
1314 unsigned long flags;
1315 pending_ring_idx_t index;
1316 struct xenvif_queue *queue = ubuf_to_queue(ubuf);
1317
1318 /* This is the only place where we grab this lock, to protect callbacks
1319 * from each other.
1320 */
1321 spin_lock_irqsave(&queue->callback_lock, flags);
1322 do {
1323 u16 pending_idx = ubuf->desc;
1324 ubuf = (struct ubuf_info *) ubuf->ctx;
1325 BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
1326 MAX_PENDING_REQS);
1327 index = pending_index(queue->dealloc_prod);
1328 queue->dealloc_ring[index] = pending_idx;
1329 /* Sync with xenvif_tx_dealloc_action:
1330 * insert idx then incr producer.
1331 */
1332 smp_wmb();
1333 queue->dealloc_prod++;
1334 } while (ubuf);
1335 spin_unlock_irqrestore(&queue->callback_lock, flags);
1336
1337 if (likely(zerocopy_success))
1338 queue->stats.tx_zerocopy_success++;
1339 else
1340 queue->stats.tx_zerocopy_fail++;
1341 xenvif_skb_zerocopy_complete(queue);
1342 }
1343
xenvif_tx_dealloc_action(struct xenvif_queue * queue)1344 static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
1345 {
1346 struct gnttab_unmap_grant_ref *gop;
1347 pending_ring_idx_t dc, dp;
1348 u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
1349 unsigned int i = 0;
1350
1351 dc = queue->dealloc_cons;
1352 gop = queue->tx_unmap_ops;
1353
1354 /* Free up any grants we have finished using */
1355 do {
1356 dp = queue->dealloc_prod;
1357
1358 /* Ensure we see all indices enqueued by all
1359 * xenvif_zerocopy_callback().
1360 */
1361 smp_rmb();
1362
1363 while (dc != dp) {
1364 BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
1365 pending_idx =
1366 queue->dealloc_ring[pending_index(dc++)];
1367
1368 pending_idx_release[gop - queue->tx_unmap_ops] =
1369 pending_idx;
1370 queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
1371 queue->mmap_pages[pending_idx];
1372 gnttab_set_unmap_op(gop,
1373 idx_to_kaddr(queue, pending_idx),
1374 GNTMAP_host_map,
1375 queue->grant_tx_handle[pending_idx]);
1376 xenvif_grant_handle_reset(queue, pending_idx);
1377 ++gop;
1378 }
1379
1380 } while (dp != queue->dealloc_prod);
1381
1382 queue->dealloc_cons = dc;
1383
1384 if (gop - queue->tx_unmap_ops > 0) {
1385 int ret;
1386 ret = gnttab_unmap_refs(queue->tx_unmap_ops,
1387 NULL,
1388 queue->pages_to_unmap,
1389 gop - queue->tx_unmap_ops);
1390 if (ret) {
1391 netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
1392 gop - queue->tx_unmap_ops, ret);
1393 for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
1394 if (gop[i].status != GNTST_okay)
1395 netdev_err(queue->vif->dev,
1396 " host_addr: 0x%llx handle: 0x%x status: %d\n",
1397 gop[i].host_addr,
1398 gop[i].handle,
1399 gop[i].status);
1400 }
1401 BUG();
1402 }
1403 }
1404
1405 for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
1406 xenvif_idx_release(queue, pending_idx_release[i],
1407 XEN_NETIF_RSP_OKAY);
1408 }
1409
1410
1411 /* Called after netfront has transmitted */
xenvif_tx_action(struct xenvif_queue * queue,int budget)1412 int xenvif_tx_action(struct xenvif_queue *queue, int budget)
1413 {
1414 unsigned nr_mops = 0, nr_cops = 0;
1415 int work_done, ret;
1416
1417 if (unlikely(!tx_work_todo(queue)))
1418 return 0;
1419
1420 xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
1421
1422 if (nr_cops == 0)
1423 return 0;
1424
1425 gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
1426 if (nr_mops != 0) {
1427 ret = gnttab_map_refs(queue->tx_map_ops,
1428 NULL,
1429 queue->pages_to_map,
1430 nr_mops);
1431 if (ret) {
1432 unsigned int i;
1433
1434 netdev_err(queue->vif->dev, "Map fail: nr %u ret %d\n",
1435 nr_mops, ret);
1436 for (i = 0; i < nr_mops; ++i)
1437 WARN_ON_ONCE(queue->tx_map_ops[i].status ==
1438 GNTST_okay);
1439 }
1440 }
1441
1442 work_done = xenvif_tx_submit(queue);
1443
1444 return work_done;
1445 }
1446
xenvif_idx_release(struct xenvif_queue * queue,u16 pending_idx,u8 status)1447 static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
1448 u8 status)
1449 {
1450 struct pending_tx_info *pending_tx_info;
1451 pending_ring_idx_t index;
1452 unsigned long flags;
1453
1454 pending_tx_info = &queue->pending_tx_info[pending_idx];
1455
1456 spin_lock_irqsave(&queue->response_lock, flags);
1457
1458 make_tx_response(queue, &pending_tx_info->req,
1459 pending_tx_info->extra_count, status);
1460
1461 /* Release the pending index before pusing the Tx response so
1462 * its available before a new Tx request is pushed by the
1463 * frontend.
1464 */
1465 index = pending_index(queue->pending_prod++);
1466 queue->pending_ring[index] = pending_idx;
1467
1468 push_tx_responses(queue);
1469
1470 spin_unlock_irqrestore(&queue->response_lock, flags);
1471 }
1472
1473
make_tx_response(struct xenvif_queue * queue,struct xen_netif_tx_request * txp,unsigned int extra_count,s8 st)1474 static void make_tx_response(struct xenvif_queue *queue,
1475 struct xen_netif_tx_request *txp,
1476 unsigned int extra_count,
1477 s8 st)
1478 {
1479 RING_IDX i = queue->tx.rsp_prod_pvt;
1480 struct xen_netif_tx_response *resp;
1481
1482 resp = RING_GET_RESPONSE(&queue->tx, i);
1483 resp->id = txp->id;
1484 resp->status = st;
1485
1486 while (extra_count-- != 0)
1487 RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
1488
1489 queue->tx.rsp_prod_pvt = ++i;
1490 }
1491
push_tx_responses(struct xenvif_queue * queue)1492 static void push_tx_responses(struct xenvif_queue *queue)
1493 {
1494 int notify;
1495
1496 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
1497 if (notify)
1498 notify_remote_via_irq(queue->tx_irq);
1499 }
1500
xenvif_idx_unmap(struct xenvif_queue * queue,u16 pending_idx)1501 static void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
1502 {
1503 int ret;
1504 struct gnttab_unmap_grant_ref tx_unmap_op;
1505
1506 gnttab_set_unmap_op(&tx_unmap_op,
1507 idx_to_kaddr(queue, pending_idx),
1508 GNTMAP_host_map,
1509 queue->grant_tx_handle[pending_idx]);
1510 xenvif_grant_handle_reset(queue, pending_idx);
1511
1512 ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
1513 &queue->mmap_pages[pending_idx], 1);
1514 if (ret) {
1515 netdev_err(queue->vif->dev,
1516 "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
1517 ret,
1518 pending_idx,
1519 tx_unmap_op.host_addr,
1520 tx_unmap_op.handle,
1521 tx_unmap_op.status);
1522 BUG();
1523 }
1524 }
1525
tx_work_todo(struct xenvif_queue * queue)1526 static inline int tx_work_todo(struct xenvif_queue *queue)
1527 {
1528 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
1529 return 1;
1530
1531 return 0;
1532 }
1533
tx_dealloc_work_todo(struct xenvif_queue * queue)1534 static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
1535 {
1536 return queue->dealloc_cons != queue->dealloc_prod;
1537 }
1538
xenvif_unmap_frontend_data_rings(struct xenvif_queue * queue)1539 void xenvif_unmap_frontend_data_rings(struct xenvif_queue *queue)
1540 {
1541 if (queue->tx.sring)
1542 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1543 queue->tx.sring);
1544 if (queue->rx.sring)
1545 xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
1546 queue->rx.sring);
1547 }
1548
xenvif_map_frontend_data_rings(struct xenvif_queue * queue,grant_ref_t tx_ring_ref,grant_ref_t rx_ring_ref)1549 int xenvif_map_frontend_data_rings(struct xenvif_queue *queue,
1550 grant_ref_t tx_ring_ref,
1551 grant_ref_t rx_ring_ref)
1552 {
1553 void *addr;
1554 struct xen_netif_tx_sring *txs;
1555 struct xen_netif_rx_sring *rxs;
1556 RING_IDX rsp_prod, req_prod;
1557 int err = -ENOMEM;
1558
1559 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1560 &tx_ring_ref, 1, &addr);
1561 if (err)
1562 goto err;
1563
1564 txs = (struct xen_netif_tx_sring *)addr;
1565 rsp_prod = READ_ONCE(txs->rsp_prod);
1566 req_prod = READ_ONCE(txs->req_prod);
1567
1568 BACK_RING_ATTACH(&queue->tx, txs, rsp_prod, XEN_PAGE_SIZE);
1569
1570 err = -EIO;
1571 if (req_prod - rsp_prod > RING_SIZE(&queue->tx))
1572 goto err;
1573
1574 err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
1575 &rx_ring_ref, 1, &addr);
1576 if (err)
1577 goto err;
1578
1579 rxs = (struct xen_netif_rx_sring *)addr;
1580 rsp_prod = READ_ONCE(rxs->rsp_prod);
1581 req_prod = READ_ONCE(rxs->req_prod);
1582
1583 BACK_RING_ATTACH(&queue->rx, rxs, rsp_prod, XEN_PAGE_SIZE);
1584
1585 err = -EIO;
1586 if (req_prod - rsp_prod > RING_SIZE(&queue->rx))
1587 goto err;
1588
1589 return 0;
1590
1591 err:
1592 xenvif_unmap_frontend_data_rings(queue);
1593 return err;
1594 }
1595
xenvif_dealloc_kthread_should_stop(struct xenvif_queue * queue)1596 static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
1597 {
1598 /* Dealloc thread must remain running until all inflight
1599 * packets complete.
1600 */
1601 return kthread_should_stop() &&
1602 !atomic_read(&queue->inflight_packets);
1603 }
1604
xenvif_dealloc_kthread(void * data)1605 int xenvif_dealloc_kthread(void *data)
1606 {
1607 struct xenvif_queue *queue = data;
1608
1609 for (;;) {
1610 wait_event_interruptible(queue->dealloc_wq,
1611 tx_dealloc_work_todo(queue) ||
1612 xenvif_dealloc_kthread_should_stop(queue));
1613 if (xenvif_dealloc_kthread_should_stop(queue))
1614 break;
1615
1616 xenvif_tx_dealloc_action(queue);
1617 cond_resched();
1618 }
1619
1620 /* Unmap anything remaining*/
1621 if (tx_dealloc_work_todo(queue))
1622 xenvif_tx_dealloc_action(queue);
1623
1624 return 0;
1625 }
1626
make_ctrl_response(struct xenvif * vif,const struct xen_netif_ctrl_request * req,u32 status,u32 data)1627 static void make_ctrl_response(struct xenvif *vif,
1628 const struct xen_netif_ctrl_request *req,
1629 u32 status, u32 data)
1630 {
1631 RING_IDX idx = vif->ctrl.rsp_prod_pvt;
1632 struct xen_netif_ctrl_response rsp = {
1633 .id = req->id,
1634 .type = req->type,
1635 .status = status,
1636 .data = data,
1637 };
1638
1639 *RING_GET_RESPONSE(&vif->ctrl, idx) = rsp;
1640 vif->ctrl.rsp_prod_pvt = ++idx;
1641 }
1642
push_ctrl_response(struct xenvif * vif)1643 static void push_ctrl_response(struct xenvif *vif)
1644 {
1645 int notify;
1646
1647 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->ctrl, notify);
1648 if (notify)
1649 notify_remote_via_irq(vif->ctrl_irq);
1650 }
1651
process_ctrl_request(struct xenvif * vif,const struct xen_netif_ctrl_request * req)1652 static void process_ctrl_request(struct xenvif *vif,
1653 const struct xen_netif_ctrl_request *req)
1654 {
1655 u32 status = XEN_NETIF_CTRL_STATUS_NOT_SUPPORTED;
1656 u32 data = 0;
1657
1658 switch (req->type) {
1659 case XEN_NETIF_CTRL_TYPE_SET_HASH_ALGORITHM:
1660 status = xenvif_set_hash_alg(vif, req->data[0]);
1661 break;
1662
1663 case XEN_NETIF_CTRL_TYPE_GET_HASH_FLAGS:
1664 status = xenvif_get_hash_flags(vif, &data);
1665 break;
1666
1667 case XEN_NETIF_CTRL_TYPE_SET_HASH_FLAGS:
1668 status = xenvif_set_hash_flags(vif, req->data[0]);
1669 break;
1670
1671 case XEN_NETIF_CTRL_TYPE_SET_HASH_KEY:
1672 status = xenvif_set_hash_key(vif, req->data[0],
1673 req->data[1]);
1674 break;
1675
1676 case XEN_NETIF_CTRL_TYPE_GET_HASH_MAPPING_SIZE:
1677 status = XEN_NETIF_CTRL_STATUS_SUCCESS;
1678 data = XEN_NETBK_MAX_HASH_MAPPING_SIZE;
1679 break;
1680
1681 case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING_SIZE:
1682 status = xenvif_set_hash_mapping_size(vif,
1683 req->data[0]);
1684 break;
1685
1686 case XEN_NETIF_CTRL_TYPE_SET_HASH_MAPPING:
1687 status = xenvif_set_hash_mapping(vif, req->data[0],
1688 req->data[1],
1689 req->data[2]);
1690 break;
1691
1692 default:
1693 break;
1694 }
1695
1696 make_ctrl_response(vif, req, status, data);
1697 push_ctrl_response(vif);
1698 }
1699
xenvif_ctrl_action(struct xenvif * vif)1700 static void xenvif_ctrl_action(struct xenvif *vif)
1701 {
1702 for (;;) {
1703 RING_IDX req_prod, req_cons;
1704
1705 req_prod = vif->ctrl.sring->req_prod;
1706 req_cons = vif->ctrl.req_cons;
1707
1708 /* Make sure we can see requests before we process them. */
1709 rmb();
1710
1711 if (req_cons == req_prod)
1712 break;
1713
1714 while (req_cons != req_prod) {
1715 struct xen_netif_ctrl_request req;
1716
1717 RING_COPY_REQUEST(&vif->ctrl, req_cons, &req);
1718 req_cons++;
1719
1720 process_ctrl_request(vif, &req);
1721 }
1722
1723 vif->ctrl.req_cons = req_cons;
1724 vif->ctrl.sring->req_event = req_cons + 1;
1725 }
1726 }
1727
xenvif_ctrl_work_todo(struct xenvif * vif)1728 static bool xenvif_ctrl_work_todo(struct xenvif *vif)
1729 {
1730 if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->ctrl)))
1731 return true;
1732
1733 return false;
1734 }
1735
xenvif_ctrl_irq_fn(int irq,void * data)1736 irqreturn_t xenvif_ctrl_irq_fn(int irq, void *data)
1737 {
1738 struct xenvif *vif = data;
1739 unsigned int eoi_flag = XEN_EOI_FLAG_SPURIOUS;
1740
1741 while (xenvif_ctrl_work_todo(vif)) {
1742 xenvif_ctrl_action(vif);
1743 eoi_flag = 0;
1744 }
1745
1746 xen_irq_lateeoi(irq, eoi_flag);
1747
1748 return IRQ_HANDLED;
1749 }
1750
netback_init(void)1751 static int __init netback_init(void)
1752 {
1753 int rc = 0;
1754
1755 if (!xen_domain())
1756 return -ENODEV;
1757
1758 /* Allow as many queues as there are CPUs but max. 8 if user has not
1759 * specified a value.
1760 */
1761 if (xenvif_max_queues == 0)
1762 xenvif_max_queues = min_t(unsigned int, MAX_QUEUES_DEFAULT,
1763 num_online_cpus());
1764
1765 if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
1766 pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
1767 fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
1768 fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
1769 }
1770
1771 rc = xenvif_xenbus_init();
1772 if (rc)
1773 goto failed_init;
1774
1775 #ifdef CONFIG_DEBUG_FS
1776 xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
1777 #endif /* CONFIG_DEBUG_FS */
1778
1779 return 0;
1780
1781 failed_init:
1782 return rc;
1783 }
1784
1785 module_init(netback_init);
1786
netback_fini(void)1787 static void __exit netback_fini(void)
1788 {
1789 #ifdef CONFIG_DEBUG_FS
1790 debugfs_remove_recursive(xen_netback_dbg_root);
1791 #endif /* CONFIG_DEBUG_FS */
1792 xenvif_xenbus_fini();
1793 }
1794 module_exit(netback_fini);
1795
1796 MODULE_LICENSE("Dual BSD/GPL");
1797 MODULE_ALIAS("xen-backend:vif");
1798