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