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