Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 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> |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 40 | #include <linux/highmem.h> |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 41 | |
| 42 | #include <net/tcp.h> |
| 43 | |
Stefano Stabellini | ca98163 | 2012-08-08 17:21:23 +0000 | [diff] [blame] | 44 | #include <xen/xen.h> |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 45 | #include <xen/events.h> |
| 46 | #include <xen/interface/memory.h> |
| 47 | |
| 48 | #include <asm/xen/hypercall.h> |
| 49 | #include <asm/xen/page.h> |
| 50 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 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 = 1; |
| 56 | module_param(separate_tx_rx_irq, bool, 0644); |
| 57 | |
Zoltan Kiss | 0935078 | 2014-03-06 21:48:30 +0000 | [diff] [blame] | 58 | /* When guest ring is filled up, qdisc queues the packets for us, but we have |
Zoltan Kiss | 0e59a4a | 2014-03-24 23:59:50 +0000 | [diff] [blame] | 59 | * to timeout them, otherwise other guests' packets can get stuck there |
Zoltan Kiss | 0935078 | 2014-03-06 21:48:30 +0000 | [diff] [blame] | 60 | */ |
| 61 | unsigned int rx_drain_timeout_msecs = 10000; |
| 62 | module_param(rx_drain_timeout_msecs, uint, 0444); |
| 63 | unsigned int rx_drain_timeout_jiffies; |
| 64 | |
Andrew J. Bennieston | 8d3d53b | 2014-06-04 10:30:43 +0100 | [diff] [blame] | 65 | unsigned int xenvif_max_queues; |
| 66 | module_param_named(max_queues, xenvif_max_queues, uint, 0644); |
| 67 | MODULE_PARM_DESC(max_queues, |
| 68 | "Maximum number of queues per virtual interface"); |
| 69 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 70 | /* |
| 71 | * This is the maximum slots a skb can have. If a guest sends a skb |
| 72 | * which exceeds this limit it is considered malicious. |
| 73 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 74 | #define FATAL_SKB_SLOTS_DEFAULT 20 |
| 75 | static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT; |
| 76 | module_param(fatal_skb_slots, uint, 0444); |
| 77 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 78 | static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 79 | u8 status); |
| 80 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 81 | static void make_tx_response(struct xenvif_queue *queue, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 82 | struct xen_netif_tx_request *txp, |
| 83 | s8 st); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 84 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 85 | static inline int tx_work_todo(struct xenvif_queue *queue); |
| 86 | static inline int rx_work_todo(struct xenvif_queue *queue); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 87 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 88 | static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 89 | u16 id, |
| 90 | s8 st, |
| 91 | u16 offset, |
| 92 | u16 size, |
| 93 | u16 flags); |
| 94 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 95 | static inline unsigned long idx_to_pfn(struct xenvif_queue *queue, |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 96 | u16 idx) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 97 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 98 | return page_to_pfn(queue->mmap_pages[idx]); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 101 | static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue, |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 102 | u16 idx) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 103 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 104 | return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Zoltan Kiss | 7aceb47 | 2014-03-24 23:59:51 +0000 | [diff] [blame] | 107 | #define callback_param(vif, pending_idx) \ |
| 108 | (vif->pending_tx_info[pending_idx].callback_struct) |
| 109 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 110 | /* Find the containing VIF's structure from a pointer in pending_tx_info array |
| 111 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 112 | static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf) |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 113 | { |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 114 | u16 pending_idx = ubuf->desc; |
| 115 | struct pending_tx_info *temp = |
| 116 | container_of(ubuf, struct pending_tx_info, callback_struct); |
| 117 | return container_of(temp - pending_idx, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 118 | struct xenvif_queue, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 119 | pending_tx_info[0]); |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 120 | } |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 121 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame] | 122 | /* This is a miniumum size for the linear area to avoid lots of |
| 123 | * calls to __pskb_pull_tail() as we set up checksum offsets. The |
| 124 | * value 128 was chosen as it covers all IPv4 and most likely |
| 125 | * IPv6 headers. |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 126 | */ |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame] | 127 | #define PKT_PROT_LEN 128 |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 128 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 129 | static u16 frag_get_pending_idx(skb_frag_t *frag) |
| 130 | { |
| 131 | return (u16)frag->page_offset; |
| 132 | } |
| 133 | |
| 134 | static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx) |
| 135 | { |
| 136 | frag->page_offset = pending_idx; |
| 137 | } |
| 138 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 139 | static inline pending_ring_idx_t pending_index(unsigned i) |
| 140 | { |
| 141 | return i & (MAX_PENDING_REQS-1); |
| 142 | } |
| 143 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 144 | bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 145 | { |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 146 | RING_IDX prod, cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 147 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 148 | do { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 149 | prod = queue->rx.sring->req_prod; |
| 150 | cons = queue->rx.req_cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 151 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 152 | if (prod - cons >= needed) |
| 153 | return true; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 154 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 155 | queue->rx.sring->req_event = prod + 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 156 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 157 | /* Make sure event is visible before we check prod |
| 158 | * again. |
| 159 | */ |
| 160 | mb(); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 161 | } while (queue->rx.sring->req_prod != prod); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 162 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 163 | return false; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Returns true if we should start a new receive buffer instead of |
| 168 | * adding 'size' bytes to a buffer which currently contains 'offset' |
| 169 | * bytes. |
| 170 | */ |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 171 | static bool start_new_rx_buffer(int offset, unsigned long size, int head, |
| 172 | bool full_coalesce) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 173 | { |
| 174 | /* simple case: we have completely filled the current buffer. */ |
| 175 | if (offset == MAX_BUFFER_OFFSET) |
| 176 | return true; |
| 177 | |
| 178 | /* |
| 179 | * complex case: start a fresh buffer if the current frag |
| 180 | * would overflow the current buffer but only if: |
| 181 | * (i) this frag would fit completely in the next buffer |
| 182 | * and (ii) there is already some data in the current buffer |
| 183 | * and (iii) this is not the head buffer. |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 184 | * and (iv) there is no need to fully utilize the buffers |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 185 | * |
| 186 | * Where: |
| 187 | * - (i) stops us splitting a frag into two copies |
| 188 | * unless the frag is too large for a single buffer. |
| 189 | * - (ii) stops us from leaving a buffer pointlessly empty. |
| 190 | * - (iii) stops us leaving the first buffer |
| 191 | * empty. Strictly speaking this is already covered |
| 192 | * by (ii) but is explicitly checked because |
| 193 | * netfront relies on the first buffer being |
| 194 | * non-empty and can crash otherwise. |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 195 | * - (iv) is needed for skbs which can use up more than MAX_SKB_FRAGS |
| 196 | * slot |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 197 | * |
| 198 | * This means we will effectively linearise small |
| 199 | * frags but do not needlessly split large buffers |
| 200 | * into multiple copies tend to give large frags their |
| 201 | * own buffers as before. |
| 202 | */ |
Paul Durrant | 0576edd | 2014-03-28 11:39:05 +0000 | [diff] [blame] | 203 | BUG_ON(size > MAX_BUFFER_OFFSET); |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 204 | if ((offset + size > MAX_BUFFER_OFFSET) && offset && !head && |
| 205 | !full_coalesce) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 206 | return true; |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 211 | struct netrx_pending_operations { |
| 212 | unsigned copy_prod, copy_cons; |
| 213 | unsigned meta_prod, meta_cons; |
| 214 | struct gnttab_copy *copy; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 215 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 216 | int copy_off; |
| 217 | grant_ref_t copy_gref; |
| 218 | }; |
| 219 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 220 | static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue, |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 221 | struct netrx_pending_operations *npo) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 222 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 223 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 224 | struct xen_netif_rx_request *req; |
| 225 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 226 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 227 | |
| 228 | meta = npo->meta + npo->meta_prod++; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 229 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 230 | meta->gso_size = 0; |
| 231 | meta->size = 0; |
| 232 | meta->id = req->id; |
| 233 | |
| 234 | npo->copy_off = 0; |
| 235 | npo->copy_gref = req->gref; |
| 236 | |
| 237 | return meta; |
| 238 | } |
| 239 | |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 240 | struct xenvif_rx_cb { |
| 241 | int meta_slots_used; |
| 242 | bool full_coalesce; |
| 243 | }; |
| 244 | |
| 245 | #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb) |
| 246 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 247 | /* |
| 248 | * Set up the grant operations for this fragment. If it's a flipping |
| 249 | * interface, we also set up the unmap request from here. |
| 250 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 251 | static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 252 | struct netrx_pending_operations *npo, |
| 253 | struct page *page, unsigned long size, |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 254 | unsigned long offset, int *head, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 255 | struct xenvif_queue *foreign_queue, |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 256 | grant_ref_t foreign_gref) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 257 | { |
| 258 | struct gnttab_copy *copy_gop; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 259 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 260 | unsigned long bytes; |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 261 | int gso_type = XEN_NETIF_GSO_TYPE_NONE; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 262 | |
| 263 | /* Data must not cross a page boundary. */ |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 264 | BUG_ON(size + offset > PAGE_SIZE<<compound_order(page)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 265 | |
| 266 | meta = npo->meta + npo->meta_prod - 1; |
| 267 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 268 | /* Skip unused frames from start of page */ |
| 269 | page += offset >> PAGE_SHIFT; |
| 270 | offset &= ~PAGE_MASK; |
| 271 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 272 | while (size > 0) { |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 273 | BUG_ON(offset >= PAGE_SIZE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 274 | BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET); |
| 275 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 276 | bytes = PAGE_SIZE - offset; |
| 277 | |
| 278 | if (bytes > size) |
| 279 | bytes = size; |
| 280 | |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 281 | if (start_new_rx_buffer(npo->copy_off, |
| 282 | bytes, |
| 283 | *head, |
| 284 | XENVIF_RX_CB(skb)->full_coalesce)) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 285 | /* |
| 286 | * Netfront requires there to be some data in the head |
| 287 | * buffer. |
| 288 | */ |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 289 | BUG_ON(*head); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 290 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 291 | meta = get_next_rx_buffer(queue, npo); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 294 | if (npo->copy_off + bytes > MAX_BUFFER_OFFSET) |
| 295 | bytes = MAX_BUFFER_OFFSET - npo->copy_off; |
| 296 | |
| 297 | copy_gop = npo->copy + npo->copy_prod++; |
| 298 | copy_gop->flags = GNTCOPY_dest_gref; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 299 | copy_gop->len = bytes; |
| 300 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 301 | if (foreign_queue) { |
| 302 | copy_gop->source.domid = foreign_queue->vif->domid; |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 303 | copy_gop->source.u.ref = foreign_gref; |
| 304 | copy_gop->flags |= GNTCOPY_source_gref; |
| 305 | } else { |
| 306 | copy_gop->source.domid = DOMID_SELF; |
| 307 | copy_gop->source.u.gmfn = |
| 308 | virt_to_mfn(page_address(page)); |
| 309 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 310 | copy_gop->source.offset = offset; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 311 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 312 | copy_gop->dest.domid = queue->vif->domid; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 313 | copy_gop->dest.offset = npo->copy_off; |
| 314 | copy_gop->dest.u.ref = npo->copy_gref; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 315 | |
| 316 | npo->copy_off += bytes; |
| 317 | meta->size += bytes; |
| 318 | |
| 319 | offset += bytes; |
| 320 | size -= bytes; |
| 321 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 322 | /* Next frame */ |
| 323 | if (offset == PAGE_SIZE && size) { |
| 324 | BUG_ON(!PageCompound(page)); |
| 325 | page++; |
| 326 | offset = 0; |
| 327 | } |
| 328 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 329 | /* Leave a gap for the GSO descriptor. */ |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 330 | if (skb_is_gso(skb)) { |
| 331 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
| 332 | gso_type = XEN_NETIF_GSO_TYPE_TCPV4; |
| 333 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 334 | gso_type = XEN_NETIF_GSO_TYPE_TCPV6; |
| 335 | } |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 336 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 337 | if (*head && ((1 << gso_type) & queue->vif->gso_mask)) |
| 338 | queue->rx.req_cons++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 339 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 340 | *head = 0; /* There must be something in this buffer now. */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 341 | |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 346 | * Find the grant ref for a given frag in a chain of struct ubuf_info's |
| 347 | * skb: the skb itself |
| 348 | * i: the frag's number |
| 349 | * ubuf: a pointer to an element in the chain. It should not be NULL |
| 350 | * |
| 351 | * Returns a pointer to the element in the chain where the page were found. If |
| 352 | * not found, returns NULL. |
| 353 | * See the definition of callback_struct in common.h for more details about |
| 354 | * the chain. |
| 355 | */ |
| 356 | static const struct ubuf_info *xenvif_find_gref(const struct sk_buff *const skb, |
| 357 | const int i, |
| 358 | const struct ubuf_info *ubuf) |
| 359 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 360 | struct xenvif_queue *foreign_queue = ubuf_to_queue(ubuf); |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 361 | |
| 362 | do { |
| 363 | u16 pending_idx = ubuf->desc; |
| 364 | |
| 365 | if (skb_shinfo(skb)->frags[i].page.p == |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 366 | foreign_queue->mmap_pages[pending_idx]) |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 367 | break; |
| 368 | ubuf = (struct ubuf_info *) ubuf->ctx; |
| 369 | } while (ubuf); |
| 370 | |
| 371 | return ubuf; |
| 372 | } |
| 373 | |
| 374 | /* |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 375 | * Prepare an SKB to be transmitted to the frontend. |
| 376 | * |
| 377 | * This function is responsible for allocating grant operations, meta |
| 378 | * structures, etc. |
| 379 | * |
| 380 | * It returns the number of meta structures consumed. The number of |
| 381 | * ring slots used is always equal to the number of meta slots used |
| 382 | * plus the number of GSO descriptors used. Currently, we use either |
| 383 | * zero GSO descriptors (for non-GSO packets) or one descriptor (for |
| 384 | * frontend-side LRO). |
| 385 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 386 | static int xenvif_gop_skb(struct sk_buff *skb, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 387 | struct netrx_pending_operations *npo, |
| 388 | struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 389 | { |
| 390 | struct xenvif *vif = netdev_priv(skb->dev); |
| 391 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 392 | int i; |
| 393 | struct xen_netif_rx_request *req; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 394 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 395 | unsigned char *data; |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 396 | int head = 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 397 | int old_meta_prod; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 398 | int gso_type; |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 399 | const struct ubuf_info *ubuf = skb_shinfo(skb)->destructor_arg; |
| 400 | const struct ubuf_info *const head_ubuf = ubuf; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 401 | |
| 402 | old_meta_prod = npo->meta_prod; |
| 403 | |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 404 | gso_type = XEN_NETIF_GSO_TYPE_NONE; |
| 405 | if (skb_is_gso(skb)) { |
| 406 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
| 407 | gso_type = XEN_NETIF_GSO_TYPE_TCPV4; |
| 408 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 409 | gso_type = XEN_NETIF_GSO_TYPE_TCPV6; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 412 | /* Set up a GSO prefix descriptor, if necessary */ |
Paul Durrant | a3314f3 | 2013-12-12 14:20:13 +0000 | [diff] [blame] | 413 | if ((1 << gso_type) & vif->gso_prefix_mask) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 414 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 415 | meta = npo->meta + npo->meta_prod++; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 416 | meta->gso_type = gso_type; |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 417 | meta->gso_size = skb_shinfo(skb)->gso_size; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 418 | meta->size = 0; |
| 419 | meta->id = req->id; |
| 420 | } |
| 421 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 422 | req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 423 | meta = npo->meta + npo->meta_prod++; |
| 424 | |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 425 | if ((1 << gso_type) & vif->gso_mask) { |
| 426 | meta->gso_type = gso_type; |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 427 | meta->gso_size = skb_shinfo(skb)->gso_size; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 428 | } else { |
| 429 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 430 | meta->gso_size = 0; |
Paul Durrant | 82cada2 | 2013-10-16 17:50:32 +0100 | [diff] [blame] | 431 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 432 | |
| 433 | meta->size = 0; |
| 434 | meta->id = req->id; |
| 435 | npo->copy_off = 0; |
| 436 | npo->copy_gref = req->gref; |
| 437 | |
| 438 | data = skb->data; |
| 439 | while (data < skb_tail_pointer(skb)) { |
| 440 | unsigned int offset = offset_in_page(data); |
| 441 | unsigned int len = PAGE_SIZE - offset; |
| 442 | |
| 443 | if (data + len > skb_tail_pointer(skb)) |
| 444 | len = skb_tail_pointer(skb) - data; |
| 445 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 446 | xenvif_gop_frag_copy(queue, skb, npo, |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 447 | virt_to_page(data), len, offset, &head, |
| 448 | NULL, |
| 449 | 0); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 450 | data += len; |
| 451 | } |
| 452 | |
| 453 | for (i = 0; i < nr_frags; i++) { |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 454 | /* This variable also signals whether foreign_gref has a real |
| 455 | * value or not. |
| 456 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 457 | struct xenvif_queue *foreign_queue = NULL; |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 458 | grant_ref_t foreign_gref; |
| 459 | |
| 460 | if ((skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) && |
| 461 | (ubuf->callback == &xenvif_zerocopy_callback)) { |
| 462 | const struct ubuf_info *const startpoint = ubuf; |
| 463 | |
| 464 | /* Ideally ubuf points to the chain element which |
| 465 | * belongs to this frag. Or if frags were removed from |
| 466 | * the beginning, then shortly before it. |
| 467 | */ |
| 468 | ubuf = xenvif_find_gref(skb, i, ubuf); |
| 469 | |
| 470 | /* Try again from the beginning of the list, if we |
| 471 | * haven't tried from there. This only makes sense in |
| 472 | * the unlikely event of reordering the original frags. |
| 473 | * For injected local pages it's an unnecessary second |
| 474 | * run. |
| 475 | */ |
| 476 | if (unlikely(!ubuf) && startpoint != head_ubuf) |
| 477 | ubuf = xenvif_find_gref(skb, i, head_ubuf); |
| 478 | |
| 479 | if (likely(ubuf)) { |
| 480 | u16 pending_idx = ubuf->desc; |
| 481 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 482 | foreign_queue = ubuf_to_queue(ubuf); |
| 483 | foreign_gref = |
| 484 | foreign_queue->pending_tx_info[pending_idx].req.gref; |
Zoltan Kiss | 58375744 | 2014-05-15 11:08:34 +0100 | [diff] [blame] | 485 | /* Just a safety measure. If this was the last |
| 486 | * element on the list, the for loop will |
| 487 | * iterate again if a local page were added to |
| 488 | * the end. Using head_ubuf here prevents the |
| 489 | * second search on the chain. Or the original |
| 490 | * frags changed order, but that's less likely. |
| 491 | * In any way, ubuf shouldn't be NULL. |
| 492 | */ |
| 493 | ubuf = ubuf->ctx ? |
| 494 | (struct ubuf_info *) ubuf->ctx : |
| 495 | head_ubuf; |
| 496 | } else |
| 497 | /* This frag was a local page, added to the |
| 498 | * array after the skb left netback. |
| 499 | */ |
| 500 | ubuf = head_ubuf; |
| 501 | } |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 502 | xenvif_gop_frag_copy(queue, skb, npo, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 503 | skb_frag_page(&skb_shinfo(skb)->frags[i]), |
| 504 | skb_frag_size(&skb_shinfo(skb)->frags[i]), |
| 505 | skb_shinfo(skb)->frags[i].page_offset, |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 506 | &head, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 507 | foreign_queue, |
| 508 | foreign_queue ? foreign_gref : UINT_MAX); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return npo->meta_prod - old_meta_prod; |
| 512 | } |
| 513 | |
| 514 | /* |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 515 | * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 516 | * used to set up the operations on the top of |
| 517 | * netrx_pending_operations, which have since been done. Check that |
| 518 | * they didn't give any errors and advance over them. |
| 519 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 520 | static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots, |
| 521 | struct netrx_pending_operations *npo) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 522 | { |
| 523 | struct gnttab_copy *copy_op; |
| 524 | int status = XEN_NETIF_RSP_OKAY; |
| 525 | int i; |
| 526 | |
| 527 | for (i = 0; i < nr_meta_slots; i++) { |
| 528 | copy_op = npo->copy + npo->copy_cons++; |
| 529 | if (copy_op->status != GNTST_okay) { |
| 530 | netdev_dbg(vif->dev, |
| 531 | "Bad status %d from copy to DOM%d.\n", |
| 532 | copy_op->status, vif->domid); |
| 533 | status = XEN_NETIF_RSP_ERROR; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return status; |
| 538 | } |
| 539 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 540 | static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 541 | struct xenvif_rx_meta *meta, |
| 542 | int nr_meta_slots) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 543 | { |
| 544 | int i; |
| 545 | unsigned long offset; |
| 546 | |
| 547 | /* No fragments used */ |
| 548 | if (nr_meta_slots <= 1) |
| 549 | return; |
| 550 | |
| 551 | nr_meta_slots--; |
| 552 | |
| 553 | for (i = 0; i < nr_meta_slots; i++) { |
| 554 | int flags; |
| 555 | if (i == nr_meta_slots - 1) |
| 556 | flags = 0; |
| 557 | else |
| 558 | flags = XEN_NETRXF_more_data; |
| 559 | |
| 560 | offset = 0; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 561 | make_rx_response(queue, meta[i].id, status, offset, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 562 | meta[i].size, flags); |
| 563 | } |
| 564 | } |
| 565 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 566 | void xenvif_kick_thread(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 567 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 568 | wake_up(&queue->wq); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 571 | static void xenvif_rx_action(struct xenvif_queue *queue) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 572 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 573 | s8 status; |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 574 | u16 flags; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 575 | struct xen_netif_rx_response *resp; |
| 576 | struct sk_buff_head rxq; |
| 577 | struct sk_buff *skb; |
| 578 | LIST_HEAD(notify); |
| 579 | int ret; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 580 | unsigned long offset; |
Paul Durrant | 11b57f9 | 2014-01-08 12:41:58 +0000 | [diff] [blame] | 581 | bool need_to_notify = false; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 582 | |
| 583 | struct netrx_pending_operations npo = { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 584 | .copy = queue->grant_copy_op, |
| 585 | .meta = queue->meta, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 586 | }; |
| 587 | |
| 588 | skb_queue_head_init(&rxq); |
| 589 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 590 | while ((skb = skb_dequeue(&queue->rx_queue)) != NULL) { |
Zoltan Kiss | 9ab9831 | 2014-02-04 19:54:37 +0000 | [diff] [blame] | 591 | RING_IDX max_slots_needed; |
Paul Durrant | 1425c7a | 2014-03-28 11:39:07 +0000 | [diff] [blame] | 592 | RING_IDX old_req_cons; |
| 593 | RING_IDX ring_slots_used; |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 594 | int i; |
| 595 | |
| 596 | /* We need a cheap worse case estimate for the number of |
| 597 | * slots we'll use. |
| 598 | */ |
| 599 | |
| 600 | max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) + |
| 601 | skb_headlen(skb), |
| 602 | PAGE_SIZE); |
| 603 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 604 | unsigned int size; |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 605 | unsigned int offset; |
| 606 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 607 | size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 608 | offset = skb_shinfo(skb)->frags[i].page_offset; |
| 609 | |
| 610 | /* For a worse-case estimate we need to factor in |
| 611 | * the fragment page offset as this will affect the |
| 612 | * number of times xenvif_gop_frag_copy() will |
| 613 | * call start_new_rx_buffer(). |
| 614 | */ |
| 615 | max_slots_needed += DIV_ROUND_UP(offset + size, |
| 616 | PAGE_SIZE); |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 617 | } |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 618 | |
| 619 | /* To avoid the estimate becoming too pessimal for some |
| 620 | * frontends that limit posted rx requests, cap the estimate |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 621 | * at MAX_SKB_FRAGS. In this case netback will fully coalesce |
| 622 | * the skb into the provided slots. |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 623 | */ |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 624 | if (max_slots_needed > MAX_SKB_FRAGS) { |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 625 | max_slots_needed = MAX_SKB_FRAGS; |
Zoltan Kiss | 59ae9fc | 2014-06-04 19:58:51 +0100 | [diff] [blame] | 626 | XENVIF_RX_CB(skb)->full_coalesce = true; |
| 627 | } else { |
| 628 | XENVIF_RX_CB(skb)->full_coalesce = false; |
| 629 | } |
Paul Durrant | a02eb47 | 2014-03-28 11:39:06 +0000 | [diff] [blame] | 630 | |
| 631 | /* We may need one more slot for GSO metadata */ |
Annie Li | 5bd0767 | 2014-03-10 22:58:34 +0800 | [diff] [blame] | 632 | if (skb_is_gso(skb) && |
| 633 | (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 || |
| 634 | skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)) |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 635 | max_slots_needed++; |
| 636 | |
| 637 | /* If the skb may not fit then bail out now */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 638 | if (!xenvif_rx_ring_slots_available(queue, max_slots_needed)) { |
| 639 | skb_queue_head(&queue->rx_queue, skb); |
Paul Durrant | 11b57f9 | 2014-01-08 12:41:58 +0000 | [diff] [blame] | 640 | need_to_notify = true; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 641 | queue->rx_last_skb_slots = max_slots_needed; |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 642 | break; |
Zoltan Kiss | 9ab9831 | 2014-02-04 19:54:37 +0000 | [diff] [blame] | 643 | } else |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 644 | queue->rx_last_skb_slots = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 645 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 646 | old_req_cons = queue->rx.req_cons; |
| 647 | XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue); |
| 648 | ring_slots_used = queue->rx.req_cons - old_req_cons; |
Paul Durrant | 1425c7a | 2014-03-28 11:39:07 +0000 | [diff] [blame] | 649 | |
| 650 | BUG_ON(ring_slots_used > max_slots_needed); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 651 | |
| 652 | __skb_queue_tail(&rxq, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 655 | BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 656 | |
| 657 | if (!npo.copy_prod) |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 658 | goto done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 659 | |
Paul Durrant | ac3d5ac | 2013-12-23 09:27:17 +0000 | [diff] [blame] | 660 | BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 661 | gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 662 | |
| 663 | while ((skb = __skb_dequeue(&rxq)) != NULL) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 664 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 665 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
| 666 | queue->vif->gso_prefix_mask) { |
| 667 | resp = RING_GET_RESPONSE(&queue->rx, |
| 668 | queue->rx.rsp_prod_pvt++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 669 | |
| 670 | resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data; |
| 671 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 672 | resp->offset = queue->meta[npo.meta_cons].gso_size; |
| 673 | resp->id = queue->meta[npo.meta_cons].id; |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 674 | resp->status = XENVIF_RX_CB(skb)->meta_slots_used; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 675 | |
| 676 | npo.meta_cons++; |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 677 | XENVIF_RX_CB(skb)->meta_slots_used--; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 681 | queue->stats.tx_bytes += skb->len; |
| 682 | queue->stats.tx_packets++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 683 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 684 | status = xenvif_check_gop(queue->vif, |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 685 | XENVIF_RX_CB(skb)->meta_slots_used, |
| 686 | &npo); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 687 | |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 688 | if (XENVIF_RX_CB(skb)->meta_slots_used == 1) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 689 | flags = 0; |
| 690 | else |
| 691 | flags = XEN_NETRXF_more_data; |
| 692 | |
| 693 | if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */ |
| 694 | flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated; |
| 695 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
| 696 | /* remote but checksummed. */ |
| 697 | flags |= XEN_NETRXF_data_validated; |
| 698 | |
| 699 | offset = 0; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 700 | resp = make_rx_response(queue, queue->meta[npo.meta_cons].id, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 701 | status, offset, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 702 | queue->meta[npo.meta_cons].size, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 703 | flags); |
| 704 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 705 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
| 706 | queue->vif->gso_mask) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 707 | struct xen_netif_extra_info *gso = |
| 708 | (struct xen_netif_extra_info *) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 709 | RING_GET_RESPONSE(&queue->rx, |
| 710 | queue->rx.rsp_prod_pvt++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 711 | |
| 712 | resp->flags |= XEN_NETRXF_extra_info; |
| 713 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 714 | gso->u.gso.type = queue->meta[npo.meta_cons].gso_type; |
| 715 | gso->u.gso.size = queue->meta[npo.meta_cons].gso_size; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 716 | gso->u.gso.pad = 0; |
| 717 | gso->u.gso.features = 0; |
| 718 | |
| 719 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; |
| 720 | gso->flags = 0; |
| 721 | } |
| 722 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 723 | xenvif_add_frag_responses(queue, status, |
| 724 | queue->meta + npo.meta_cons + 1, |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 725 | XENVIF_RX_CB(skb)->meta_slots_used); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 726 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 727 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 728 | |
Paul Durrant | 11b57f9 | 2014-01-08 12:41:58 +0000 | [diff] [blame] | 729 | need_to_notify |= !!ret; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 730 | |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 731 | npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 732 | dev_kfree_skb(skb); |
| 733 | } |
| 734 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 735 | done: |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 736 | if (need_to_notify) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 737 | notify_remote_via_irq(queue->rx_irq); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 740 | void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 741 | { |
| 742 | int more_to_do; |
| 743 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 744 | RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 745 | |
| 746 | if (more_to_do) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 747 | napi_schedule(&queue->napi); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 750 | static void tx_add_credit(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 751 | { |
| 752 | unsigned long max_burst, max_credit; |
| 753 | |
| 754 | /* |
| 755 | * Allow a burst big enough to transmit a jumbo packet of up to 128kB. |
| 756 | * Otherwise the interface can seize up due to insufficient credit. |
| 757 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 758 | max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 759 | max_burst = min(max_burst, 131072UL); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 760 | max_burst = max(max_burst, queue->credit_bytes); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 761 | |
| 762 | /* Take care that adding a new chunk of credit doesn't wrap to zero. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 763 | max_credit = queue->remaining_credit + queue->credit_bytes; |
| 764 | if (max_credit < queue->remaining_credit) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 765 | max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */ |
| 766 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 767 | queue->remaining_credit = min(max_credit, max_burst); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | static void tx_credit_callback(unsigned long data) |
| 771 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 772 | struct xenvif_queue *queue = (struct xenvif_queue *)data; |
| 773 | tx_add_credit(queue); |
| 774 | xenvif_napi_schedule_or_enable_events(queue); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 777 | static void xenvif_tx_err(struct xenvif_queue *queue, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 778 | struct xen_netif_tx_request *txp, RING_IDX end) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 779 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 780 | RING_IDX cons = queue->tx.req_cons; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 781 | unsigned long flags; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 782 | |
| 783 | do { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 784 | spin_lock_irqsave(&queue->response_lock, flags); |
| 785 | make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR); |
| 786 | spin_unlock_irqrestore(&queue->response_lock, flags); |
Ian Campbell | b914972 | 2013-02-06 23:41:38 +0000 | [diff] [blame] | 787 | if (cons == end) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 788 | break; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 789 | txp = RING_GET_REQUEST(&queue->tx, cons++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 790 | } while (1); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 791 | queue->tx.req_cons = cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 794 | static void xenvif_fatal_tx_err(struct xenvif *vif) |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 795 | { |
| 796 | netdev_err(vif->dev, "fatal error; disabling device\n"); |
Wei Liu | e9d8b2c | 2014-04-01 12:46:12 +0100 | [diff] [blame] | 797 | vif->disabled = true; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 798 | /* Disable the vif from queue 0's kthread */ |
| 799 | if (vif->queues) |
| 800 | xenvif_kick_thread(&vif->queues[0]); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 803 | static int xenvif_count_requests(struct xenvif_queue *queue, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 804 | struct xen_netif_tx_request *first, |
| 805 | struct xen_netif_tx_request *txp, |
| 806 | int work_to_do) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 807 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 808 | RING_IDX cons = queue->tx.req_cons; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 809 | int slots = 0; |
| 810 | int drop_err = 0; |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 811 | int more_data; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 812 | |
| 813 | if (!(first->flags & XEN_NETTXF_more_data)) |
| 814 | return 0; |
| 815 | |
| 816 | do { |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 817 | struct xen_netif_tx_request dropped_tx = { 0 }; |
| 818 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 819 | if (slots >= work_to_do) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 820 | netdev_err(queue->vif->dev, |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 821 | "Asked for %d slots but exceeds this limit\n", |
| 822 | work_to_do); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 823 | xenvif_fatal_tx_err(queue->vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 824 | return -ENODATA; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 827 | /* This guest is really using too many slots and |
| 828 | * considered malicious. |
| 829 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 830 | if (unlikely(slots >= fatal_skb_slots)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 831 | netdev_err(queue->vif->dev, |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 832 | "Malicious frontend using %d slots, threshold %u\n", |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 833 | slots, fatal_skb_slots); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 834 | xenvif_fatal_tx_err(queue->vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 835 | return -E2BIG; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 838 | /* Xen network protocol had implicit dependency on |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 839 | * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to |
| 840 | * the historical MAX_SKB_FRAGS value 18 to honor the |
| 841 | * same behavior as before. Any packet using more than |
| 842 | * 18 slots but less than fatal_skb_slots slots is |
| 843 | * dropped |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 844 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 845 | if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) { |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 846 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 847 | netdev_dbg(queue->vif->dev, |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 848 | "Too many slots (%d) exceeding limit (%d), dropping packet\n", |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 849 | slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 850 | drop_err = -E2BIG; |
| 851 | } |
| 852 | |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 853 | if (drop_err) |
| 854 | txp = &dropped_tx; |
| 855 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 856 | memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots), |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 857 | sizeof(*txp)); |
Wei Liu | 03393fd5 | 2013-04-22 02:20:43 +0000 | [diff] [blame] | 858 | |
| 859 | /* If the guest submitted a frame >= 64 KiB then |
| 860 | * first->size overflowed and following slots will |
| 861 | * appear to be larger than the frame. |
| 862 | * |
| 863 | * This cannot be fatal error as there are buggy |
| 864 | * frontends that do this. |
| 865 | * |
| 866 | * Consume all slots and drop the packet. |
| 867 | */ |
| 868 | if (!drop_err && txp->size > first->size) { |
| 869 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 870 | netdev_dbg(queue->vif->dev, |
Wei Liu | 03393fd5 | 2013-04-22 02:20:43 +0000 | [diff] [blame] | 871 | "Invalid tx request, slot size %u > remaining size %u\n", |
| 872 | txp->size, first->size); |
| 873 | drop_err = -EIO; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | first->size -= txp->size; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 877 | slots++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 878 | |
| 879 | if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 880 | netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n", |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 881 | txp->offset, txp->size); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 882 | xenvif_fatal_tx_err(queue->vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 883 | return -EINVAL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 884 | } |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 885 | |
| 886 | more_data = txp->flags & XEN_NETTXF_more_data; |
| 887 | |
| 888 | if (!drop_err) |
| 889 | txp++; |
| 890 | |
| 891 | } while (more_data); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 892 | |
| 893 | if (drop_err) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 894 | xenvif_tx_err(queue, first, cons + slots); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 895 | return drop_err; |
| 896 | } |
| 897 | |
| 898 | return slots; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 901 | |
| 902 | struct xenvif_tx_cb { |
| 903 | u16 pending_idx; |
| 904 | }; |
| 905 | |
| 906 | #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb) |
| 907 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 908 | static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue, |
Zoltan Kiss | 9074ce2 | 2014-04-02 18:04:57 +0100 | [diff] [blame] | 909 | u16 pending_idx, |
| 910 | struct xen_netif_tx_request *txp, |
| 911 | struct gnttab_map_grant_ref *mop) |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 912 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 913 | queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx]; |
| 914 | gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx), |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 915 | GNTMAP_host_map | GNTMAP_readonly, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 916 | txp->gref, queue->vif->domid); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 917 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 918 | memcpy(&queue->pending_tx_info[pending_idx].req, txp, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 919 | sizeof(*txp)); |
| 920 | } |
| 921 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 922 | static inline struct sk_buff *xenvif_alloc_skb(unsigned int size) |
| 923 | { |
| 924 | struct sk_buff *skb = |
| 925 | alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN, |
| 926 | GFP_ATOMIC | __GFP_NOWARN); |
| 927 | if (unlikely(skb == NULL)) |
| 928 | return NULL; |
| 929 | |
| 930 | /* Packets passed to netif_rx() must have some headroom. */ |
| 931 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 932 | |
| 933 | /* Initialize it here to avoid later surprises */ |
| 934 | skb_shinfo(skb)->destructor_arg = NULL; |
| 935 | |
| 936 | return skb; |
| 937 | } |
| 938 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 939 | static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 940 | struct sk_buff *skb, |
| 941 | struct xen_netif_tx_request *txp, |
| 942 | struct gnttab_map_grant_ref *gop) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 943 | { |
| 944 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 945 | skb_frag_t *frags = shinfo->frags; |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 946 | u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
Zoltan Kiss | 62bad31 | 2014-03-06 21:48:27 +0000 | [diff] [blame] | 947 | int start; |
| 948 | pending_ring_idx_t index; |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 949 | unsigned int nr_slots, frag_overflow = 0; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 950 | |
| 951 | /* At this point shinfo->nr_frags is in fact the number of |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 952 | * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX. |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 953 | */ |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 954 | if (shinfo->nr_frags > MAX_SKB_FRAGS) { |
| 955 | frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS; |
| 956 | BUG_ON(frag_overflow > MAX_SKB_FRAGS); |
| 957 | shinfo->nr_frags = MAX_SKB_FRAGS; |
| 958 | } |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 959 | nr_slots = shinfo->nr_frags; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 960 | |
| 961 | /* Skip first skb fragment if it is on same page as header fragment. */ |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 962 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 963 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 964 | for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots; |
| 965 | shinfo->nr_frags++, txp++, gop++) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 966 | index = pending_index(queue->pending_cons++); |
| 967 | pending_idx = queue->pending_ring[index]; |
| 968 | xenvif_tx_create_map_op(queue, pending_idx, txp, gop); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 969 | frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 972 | if (frag_overflow) { |
| 973 | struct sk_buff *nskb = xenvif_alloc_skb(0); |
| 974 | if (unlikely(nskb == NULL)) { |
| 975 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 976 | netdev_err(queue->vif->dev, |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 977 | "Can't allocate the frag_list skb.\n"); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
| 981 | shinfo = skb_shinfo(nskb); |
| 982 | frags = shinfo->frags; |
| 983 | |
| 984 | for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow; |
| 985 | shinfo->nr_frags++, txp++, gop++) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 986 | index = pending_index(queue->pending_cons++); |
| 987 | pending_idx = queue->pending_ring[index]; |
| 988 | xenvif_tx_create_map_op(queue, pending_idx, txp, gop); |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 989 | frag_set_pending_idx(&frags[shinfo->nr_frags], |
| 990 | pending_idx); |
| 991 | } |
| 992 | |
| 993 | skb_shinfo(skb)->frag_list = nskb; |
| 994 | } |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 995 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 996 | return gop; |
| 997 | } |
| 998 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 999 | static inline void xenvif_grant_handle_set(struct xenvif_queue *queue, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1000 | u16 pending_idx, |
| 1001 | grant_handle_t handle) |
| 1002 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1003 | if (unlikely(queue->grant_tx_handle[pending_idx] != |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1004 | NETBACK_INVALID_HANDLE)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1005 | netdev_err(queue->vif->dev, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1006 | "Trying to overwrite active handle! pending_idx: %x\n", |
| 1007 | pending_idx); |
| 1008 | BUG(); |
| 1009 | } |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1010 | queue->grant_tx_handle[pending_idx] = handle; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1013 | static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1014 | u16 pending_idx) |
| 1015 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1016 | if (unlikely(queue->grant_tx_handle[pending_idx] == |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1017 | NETBACK_INVALID_HANDLE)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1018 | netdev_err(queue->vif->dev, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1019 | "Trying to unmap invalid handle! pending_idx: %x\n", |
| 1020 | pending_idx); |
| 1021 | BUG(); |
| 1022 | } |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1023 | queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1026 | static int xenvif_tx_check_gop(struct xenvif_queue *queue, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1027 | struct sk_buff *skb, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1028 | struct gnttab_map_grant_ref **gopp_map, |
| 1029 | struct gnttab_copy **gopp_copy) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1030 | { |
Zoltan Kiss | 9074ce2 | 2014-04-02 18:04:57 +0100 | [diff] [blame] | 1031 | struct gnttab_map_grant_ref *gop_map = *gopp_map; |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 1032 | u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
Zoltan Kiss | 1a998d3 | 2014-07-18 19:08:02 +0100 | [diff] [blame] | 1033 | /* This always points to the shinfo of the skb being checked, which |
| 1034 | * could be either the first or the one on the frag_list |
| 1035 | */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1036 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
Zoltan Kiss | 1a998d3 | 2014-07-18 19:08:02 +0100 | [diff] [blame] | 1037 | /* If this is non-NULL, we are currently checking the frag_list skb, and |
| 1038 | * this points to the shinfo of the first one |
| 1039 | */ |
| 1040 | struct skb_shared_info *first_shinfo = NULL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1041 | int nr_frags = shinfo->nr_frags; |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1042 | const bool sharedslot = nr_frags && |
| 1043 | frag_get_pending_idx(&shinfo->frags[0]) == pending_idx; |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1044 | int i, err; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1045 | |
| 1046 | /* Check status of header. */ |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1047 | err = (*gopp_copy)->status; |
| 1048 | (*gopp_copy)++; |
| 1049 | if (unlikely(err)) { |
| 1050 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1051 | netdev_dbg(queue->vif->dev, |
Zoltan Kiss | 00aefce | 2014-04-04 15:45:24 +0100 | [diff] [blame] | 1052 | "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n", |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1053 | (*gopp_copy)->status, |
| 1054 | pending_idx, |
| 1055 | (*gopp_copy)->source.u.ref); |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1056 | /* The first frag might still have this slot mapped */ |
| 1057 | if (!sharedslot) |
| 1058 | xenvif_idx_release(queue, pending_idx, |
| 1059 | XEN_NETIF_RSP_ERROR); |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1060 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1061 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1062 | check_frags: |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1063 | for (i = 0; i < nr_frags; i++, gop_map++) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1064 | int j, newerr; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1065 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1066 | pending_idx = frag_get_pending_idx(&shinfo->frags[i]); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* Check error status: if okay then remember grant handle. */ |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1069 | newerr = gop_map->status; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1070 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1071 | if (likely(!newerr)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1072 | xenvif_grant_handle_set(queue, |
Zoltan Kiss | 9074ce2 | 2014-04-02 18:04:57 +0100 | [diff] [blame] | 1073 | pending_idx, |
| 1074 | gop_map->handle); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1075 | /* Had a previous error? Invalidate this fragment. */ |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1076 | if (unlikely(err)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1077 | xenvif_idx_unmap(queue, pending_idx); |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1078 | /* If the mapping of the first frag was OK, but |
| 1079 | * the header's copy failed, and they are |
| 1080 | * sharing a slot, send an error |
| 1081 | */ |
| 1082 | if (i == 0 && sharedslot) |
| 1083 | xenvif_idx_release(queue, pending_idx, |
| 1084 | XEN_NETIF_RSP_ERROR); |
| 1085 | else |
| 1086 | xenvif_idx_release(queue, pending_idx, |
| 1087 | XEN_NETIF_RSP_OKAY); |
| 1088 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1089 | continue; |
| 1090 | } |
| 1091 | |
| 1092 | /* Error on this fragment: respond to client with an error. */ |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1093 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1094 | netdev_dbg(queue->vif->dev, |
Zoltan Kiss | 00aefce | 2014-04-04 15:45:24 +0100 | [diff] [blame] | 1095 | "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n", |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1096 | i, |
| 1097 | gop_map->status, |
| 1098 | pending_idx, |
| 1099 | gop_map->ref); |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1100 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1101 | xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* Not the first error? Preceding frags already invalidated. */ |
| 1104 | if (err) |
| 1105 | continue; |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1106 | |
| 1107 | /* First error: if the header haven't shared a slot with the |
| 1108 | * first frag, release it as well. |
| 1109 | */ |
| 1110 | if (!sharedslot) |
| 1111 | xenvif_idx_release(queue, |
| 1112 | XENVIF_TX_CB(skb)->pending_idx, |
| 1113 | XEN_NETIF_RSP_OKAY); |
| 1114 | |
| 1115 | /* Invalidate preceding fragments of this skb. */ |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1116 | for (j = 0; j < i; j++) { |
Jan Beulich | 5ccb3ea | 2011-11-18 05:42:05 +0000 | [diff] [blame] | 1117 | pending_idx = frag_get_pending_idx(&shinfo->frags[j]); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1118 | xenvif_idx_unmap(queue, pending_idx); |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1119 | xenvif_idx_release(queue, pending_idx, |
| 1120 | XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Zoltan Kiss | 1a998d3 | 2014-07-18 19:08:02 +0100 | [diff] [blame] | 1123 | /* And if we found the error while checking the frag_list, unmap |
| 1124 | * the first skb's frags |
| 1125 | */ |
| 1126 | if (first_shinfo) { |
| 1127 | for (j = 0; j < first_shinfo->nr_frags; j++) { |
| 1128 | pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]); |
| 1129 | xenvif_idx_unmap(queue, pending_idx); |
Zoltan Kiss | 1b860da | 2014-07-18 19:08:04 +0100 | [diff] [blame^] | 1130 | xenvif_idx_release(queue, pending_idx, |
| 1131 | XEN_NETIF_RSP_OKAY); |
Zoltan Kiss | 1a998d3 | 2014-07-18 19:08:02 +0100 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1135 | /* Remember the error: invalidate all subsequent fragments. */ |
| 1136 | err = newerr; |
| 1137 | } |
| 1138 | |
Zoltan Kiss | 1a998d3 | 2014-07-18 19:08:02 +0100 | [diff] [blame] | 1139 | if (skb_has_frag_list(skb) && !first_shinfo) { |
| 1140 | first_shinfo = skb_shinfo(skb); |
| 1141 | shinfo = skb_shinfo(skb_shinfo(skb)->frag_list); |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1142 | nr_frags = shinfo->nr_frags; |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1143 | |
| 1144 | goto check_frags; |
| 1145 | } |
| 1146 | |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1147 | *gopp_map = gop_map; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1148 | return err; |
| 1149 | } |
| 1150 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1151 | static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1152 | { |
| 1153 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 1154 | int nr_frags = shinfo->nr_frags; |
| 1155 | int i; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1156 | u16 prev_pending_idx = INVALID_PENDING_IDX; |
| 1157 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1158 | for (i = 0; i < nr_frags; i++) { |
| 1159 | skb_frag_t *frag = shinfo->frags + i; |
| 1160 | struct xen_netif_tx_request *txp; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1161 | struct page *page; |
| 1162 | u16 pending_idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1163 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1164 | pending_idx = frag_get_pending_idx(frag); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1165 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1166 | /* If this is not the first frag, chain it to the previous*/ |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1167 | if (prev_pending_idx == INVALID_PENDING_IDX) |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1168 | skb_shinfo(skb)->destructor_arg = |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1169 | &callback_param(queue, pending_idx); |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1170 | else |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1171 | callback_param(queue, prev_pending_idx).ctx = |
| 1172 | &callback_param(queue, pending_idx); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1173 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1174 | callback_param(queue, pending_idx).ctx = NULL; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1175 | prev_pending_idx = pending_idx; |
| 1176 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1177 | txp = &queue->pending_tx_info[pending_idx].req; |
| 1178 | page = virt_to_page(idx_to_kaddr(queue, pending_idx)); |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1179 | __skb_fill_page_desc(skb, i, page, txp->offset, txp->size); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1180 | skb->len += txp->size; |
| 1181 | skb->data_len += txp->size; |
| 1182 | skb->truesize += txp->size; |
| 1183 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1184 | /* Take an extra reference to offset network stack's put_page */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1185 | get_page(queue->mmap_pages[pending_idx]); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1186 | } |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1187 | /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc |
| 1188 | * overlaps with "index", and "mapping" is not set. I think mapping |
| 1189 | * should be set. If delivered to local stack, it would drop this |
| 1190 | * skb in sk_filter unless the socket has the right to use it. |
| 1191 | */ |
| 1192 | skb->pfmemalloc = false; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1195 | static int xenvif_get_extras(struct xenvif_queue *queue, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1196 | struct xen_netif_extra_info *extras, |
| 1197 | int work_to_do) |
| 1198 | { |
| 1199 | struct xen_netif_extra_info extra; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1200 | RING_IDX cons = queue->tx.req_cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1201 | |
| 1202 | do { |
| 1203 | if (unlikely(work_to_do-- <= 0)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1204 | netdev_err(queue->vif->dev, "Missing extra info\n"); |
| 1205 | xenvif_fatal_tx_err(queue->vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1206 | return -EBADR; |
| 1207 | } |
| 1208 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1209 | memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons), |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1210 | sizeof(extra)); |
| 1211 | if (unlikely(!extra.type || |
| 1212 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1213 | queue->tx.req_cons = ++cons; |
| 1214 | netdev_err(queue->vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1215 | "Invalid extra type: %d\n", extra.type); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1216 | xenvif_fatal_tx_err(queue->vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | |
| 1220 | memcpy(&extras[extra.type - 1], &extra, sizeof(extra)); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1221 | queue->tx.req_cons = ++cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1222 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
| 1223 | |
| 1224 | return work_to_do; |
| 1225 | } |
| 1226 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1227 | static int xenvif_set_skb_gso(struct xenvif *vif, |
| 1228 | struct sk_buff *skb, |
| 1229 | struct xen_netif_extra_info *gso) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1230 | { |
| 1231 | if (!gso->u.gso.size) { |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1232 | netdev_err(vif->dev, "GSO size must not be zero.\n"); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1233 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | |
Paul Durrant | a946858 | 2013-10-16 17:50:31 +0100 | [diff] [blame] | 1237 | switch (gso->u.gso.type) { |
| 1238 | case XEN_NETIF_GSO_TYPE_TCPV4: |
| 1239 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 1240 | break; |
| 1241 | case XEN_NETIF_GSO_TYPE_TCPV6: |
| 1242 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; |
| 1243 | break; |
| 1244 | default: |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1245 | netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1246 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | |
| 1250 | skb_shinfo(skb)->gso_size = gso->u.gso.size; |
Paul Durrant | b89587a | 2013-12-17 11:44:35 +0000 | [diff] [blame] | 1251 | /* gso_segs will be calculated later */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1256 | static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1257 | { |
Paul Durrant | 2721637 | 2014-01-09 10:02:47 +0000 | [diff] [blame] | 1258 | bool recalculate_partial_csum = false; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1259 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame] | 1260 | /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1261 | * peers can fail to set NETRXF_csum_blank when sending a GSO |
| 1262 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and |
| 1263 | * recalculate the partial checksum. |
| 1264 | */ |
| 1265 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1266 | queue->stats.rx_gso_checksum_fixup++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1267 | skb->ip_summed = CHECKSUM_PARTIAL; |
Paul Durrant | 2721637 | 2014-01-09 10:02:47 +0000 | [diff] [blame] | 1268 | recalculate_partial_csum = true; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ |
| 1272 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 1273 | return 0; |
| 1274 | |
Paul Durrant | 2721637 | 2014-01-09 10:02:47 +0000 | [diff] [blame] | 1275 | return skb_checksum_setup(skb, recalculate_partial_csum); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1278 | static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1279 | { |
Wei Liu | 059dfa6 | 2013-10-28 12:07:57 +0000 | [diff] [blame] | 1280 | u64 now = get_jiffies_64(); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1281 | u64 next_credit = queue->credit_window_start + |
| 1282 | msecs_to_jiffies(queue->credit_usec / 1000); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* Timer could already be pending in rare cases. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1285 | if (timer_pending(&queue->credit_timeout)) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1286 | return true; |
| 1287 | |
| 1288 | /* Passed the point where we can replenish credit? */ |
Wei Liu | 059dfa6 | 2013-10-28 12:07:57 +0000 | [diff] [blame] | 1289 | if (time_after_eq64(now, next_credit)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1290 | queue->credit_window_start = now; |
| 1291 | tx_add_credit(queue); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* Still too big to send right now? Set a callback. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1295 | if (size > queue->remaining_credit) { |
| 1296 | queue->credit_timeout.data = |
| 1297 | (unsigned long)queue; |
| 1298 | queue->credit_timeout.function = |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1299 | tx_credit_callback; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1300 | mod_timer(&queue->credit_timeout, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1301 | next_credit); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1302 | queue->credit_window_start = next_credit; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1303 | |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1310 | static void xenvif_tx_build_gops(struct xenvif_queue *queue, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1311 | int budget, |
| 1312 | unsigned *copy_ops, |
| 1313 | unsigned *map_ops) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1314 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1315 | struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1316 | struct sk_buff *skb; |
| 1317 | int ret; |
| 1318 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1319 | while (skb_queue_len(&queue->tx_queue) < budget) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1320 | struct xen_netif_tx_request txreq; |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1321 | struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1322 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1]; |
| 1323 | u16 pending_idx; |
| 1324 | RING_IDX idx; |
| 1325 | int work_to_do; |
| 1326 | unsigned int data_len; |
| 1327 | pending_ring_idx_t index; |
| 1328 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1329 | if (queue->tx.sring->req_prod - queue->tx.req_cons > |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1330 | XEN_NETIF_TX_RING_SIZE) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1331 | netdev_err(queue->vif->dev, |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1332 | "Impossible number of requests. " |
| 1333 | "req_prod %d, req_cons %d, size %ld\n", |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1334 | queue->tx.sring->req_prod, queue->tx.req_cons, |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1335 | XEN_NETIF_TX_RING_SIZE); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1336 | xenvif_fatal_tx_err(queue->vif); |
Wei Liu | e9d8b2c | 2014-04-01 12:46:12 +0100 | [diff] [blame] | 1337 | break; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1340 | work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1341 | if (!work_to_do) |
| 1342 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1343 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1344 | idx = queue->tx.req_cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1345 | rmb(); /* Ensure that we see the request before we copy it. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1346 | memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1347 | |
| 1348 | /* Credit-based scheduling. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1349 | if (txreq.size > queue->remaining_credit && |
| 1350 | tx_credit_exceeded(queue, txreq.size)) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1351 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1352 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1353 | queue->remaining_credit -= txreq.size; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1354 | |
| 1355 | work_to_do--; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1356 | queue->tx.req_cons = ++idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1357 | |
| 1358 | memset(extras, 0, sizeof(extras)); |
| 1359 | if (txreq.flags & XEN_NETTXF_extra_info) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1360 | work_to_do = xenvif_get_extras(queue, extras, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1361 | work_to_do); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1362 | idx = queue->tx.req_cons; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1363 | if (unlikely(work_to_do < 0)) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1364 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1367 | ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1368 | if (unlikely(ret < 0)) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1369 | break; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1370 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1371 | idx += ret; |
| 1372 | |
| 1373 | if (unlikely(txreq.size < ETH_HLEN)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1374 | netdev_dbg(queue->vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1375 | "Bad packet size: %d\n", txreq.size); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1376 | xenvif_tx_err(queue, &txreq, idx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1377 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* No crossing a page as the payload mustn't fragment. */ |
| 1381 | if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1382 | netdev_err(queue->vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1383 | "txreq.offset: %x, size: %u, end: %lu\n", |
| 1384 | txreq.offset, txreq.size, |
| 1385 | (txreq.offset&~PAGE_MASK) + txreq.size); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1386 | xenvif_fatal_tx_err(queue->vif); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1387 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1390 | index = pending_index(queue->pending_cons); |
| 1391 | pending_idx = queue->pending_ring[index]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1392 | |
| 1393 | data_len = (txreq.size > PKT_PROT_LEN && |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1394 | ret < XEN_NETBK_LEGACY_SLOTS_MAX) ? |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1395 | PKT_PROT_LEN : txreq.size; |
| 1396 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1397 | skb = xenvif_alloc_skb(data_len); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1398 | if (unlikely(skb == NULL)) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1399 | netdev_dbg(queue->vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1400 | "Can't allocate a skb in start_xmit.\n"); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1401 | xenvif_tx_err(queue, &txreq, idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1402 | break; |
| 1403 | } |
| 1404 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1405 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { |
| 1406 | struct xen_netif_extra_info *gso; |
| 1407 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; |
| 1408 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1409 | if (xenvif_set_skb_gso(queue->vif, skb, gso)) { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1410 | /* Failure in xenvif_set_skb_gso is fatal. */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1411 | kfree_skb(skb); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1412 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 1416 | XENVIF_TX_CB(skb)->pending_idx = pending_idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1417 | |
| 1418 | __skb_put(skb, data_len); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1419 | queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref; |
| 1420 | queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid; |
| 1421 | queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset; |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1422 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1423 | queue->tx_copy_ops[*copy_ops].dest.u.gmfn = |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1424 | virt_to_mfn(skb->data); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1425 | queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF; |
| 1426 | queue->tx_copy_ops[*copy_ops].dest.offset = |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1427 | offset_in_page(skb->data); |
| 1428 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1429 | queue->tx_copy_ops[*copy_ops].len = data_len; |
| 1430 | queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref; |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1431 | |
| 1432 | (*copy_ops)++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1433 | |
| 1434 | skb_shinfo(skb)->nr_frags = ret; |
| 1435 | if (data_len < txreq.size) { |
| 1436 | skb_shinfo(skb)->nr_frags++; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1437 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
| 1438 | pending_idx); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1439 | xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop); |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1440 | gop++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1441 | } else { |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1442 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
| 1443 | INVALID_PENDING_IDX); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1444 | memcpy(&queue->pending_tx_info[pending_idx].req, &txreq, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1445 | sizeof(txreq)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1448 | queue->pending_cons++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1449 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1450 | request_gop = xenvif_get_requests(queue, skb, txfrags, gop); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1451 | if (request_gop == NULL) { |
| 1452 | kfree_skb(skb); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1453 | xenvif_tx_err(queue, &txreq, idx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1454 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1455 | } |
| 1456 | gop = request_gop; |
| 1457 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1458 | __skb_queue_tail(&queue->tx_queue, skb); |
Annie Li | 1e0b6ea | 2012-06-27 00:46:58 +0000 | [diff] [blame] | 1459 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1460 | queue->tx.req_cons = idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1461 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1462 | if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) || |
| 1463 | (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops))) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | } |
| 1466 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1467 | (*map_ops) = gop - queue->tx_map_ops; |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1468 | return; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1471 | /* Consolidate skb with a frag_list into a brand new one with local pages on |
| 1472 | * frags. Returns 0 or -ENOMEM if can't allocate new pages. |
| 1473 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1474 | static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb) |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1475 | { |
| 1476 | unsigned int offset = skb_headlen(skb); |
| 1477 | skb_frag_t frags[MAX_SKB_FRAGS]; |
| 1478 | int i; |
| 1479 | struct ubuf_info *uarg; |
| 1480 | struct sk_buff *nskb = skb_shinfo(skb)->frag_list; |
| 1481 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1482 | queue->stats.tx_zerocopy_sent += 2; |
| 1483 | queue->stats.tx_frag_overflow++; |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1484 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1485 | xenvif_fill_frags(queue, nskb); |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1486 | /* Subtract frags size, we will correct it later */ |
| 1487 | skb->truesize -= skb->data_len; |
| 1488 | skb->len += nskb->len; |
| 1489 | skb->data_len += nskb->len; |
| 1490 | |
| 1491 | /* create a brand new frags array and coalesce there */ |
| 1492 | for (i = 0; offset < skb->len; i++) { |
| 1493 | struct page *page; |
| 1494 | unsigned int len; |
| 1495 | |
| 1496 | BUG_ON(i >= MAX_SKB_FRAGS); |
| 1497 | page = alloc_page(GFP_ATOMIC|__GFP_COLD); |
| 1498 | if (!page) { |
| 1499 | int j; |
| 1500 | skb->truesize += skb->data_len; |
| 1501 | for (j = 0; j < i; j++) |
| 1502 | put_page(frags[j].page.p); |
| 1503 | return -ENOMEM; |
| 1504 | } |
| 1505 | |
| 1506 | if (offset + PAGE_SIZE < skb->len) |
| 1507 | len = PAGE_SIZE; |
| 1508 | else |
| 1509 | len = skb->len - offset; |
| 1510 | if (skb_copy_bits(skb, offset, page_address(page), len)) |
| 1511 | BUG(); |
| 1512 | |
| 1513 | offset += len; |
| 1514 | frags[i].page.p = page; |
| 1515 | frags[i].page_offset = 0; |
| 1516 | skb_frag_size_set(&frags[i], len); |
| 1517 | } |
| 1518 | /* swap out with old one */ |
| 1519 | memcpy(skb_shinfo(skb)->frags, |
| 1520 | frags, |
| 1521 | i * sizeof(skb_frag_t)); |
| 1522 | skb_shinfo(skb)->nr_frags = i; |
| 1523 | skb->truesize += i * PAGE_SIZE; |
| 1524 | |
| 1525 | /* remove traces of mapped pages and frag_list */ |
| 1526 | skb_frag_list_init(skb); |
| 1527 | uarg = skb_shinfo(skb)->destructor_arg; |
| 1528 | uarg->callback(uarg, true); |
| 1529 | skb_shinfo(skb)->destructor_arg = NULL; |
| 1530 | |
| 1531 | skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
| 1532 | kfree_skb(nskb); |
| 1533 | |
| 1534 | return 0; |
| 1535 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1536 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1537 | static int xenvif_tx_submit(struct xenvif_queue *queue) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1538 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1539 | struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops; |
| 1540 | struct gnttab_copy *gop_copy = queue->tx_copy_ops; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1541 | struct sk_buff *skb; |
| 1542 | int work_done = 0; |
| 1543 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1544 | while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1545 | struct xen_netif_tx_request *txp; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1546 | u16 pending_idx; |
| 1547 | unsigned data_len; |
| 1548 | |
Zoltan Kiss | 8f13dd9 | 2014-03-06 21:48:23 +0000 | [diff] [blame] | 1549 | pending_idx = XENVIF_TX_CB(skb)->pending_idx; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1550 | txp = &queue->pending_tx_info[pending_idx].req; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* Check the remap error code. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1553 | if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) { |
Zoltan Kiss | b42cc6e | 2014-07-18 19:08:03 +0100 | [diff] [blame] | 1554 | /* If there was an error, xenvif_tx_check_gop is |
| 1555 | * expected to release all the frags which were mapped, |
| 1556 | * so kfree_skb shouldn't do it again |
| 1557 | */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1558 | skb_shinfo(skb)->nr_frags = 0; |
Zoltan Kiss | b42cc6e | 2014-07-18 19:08:03 +0100 | [diff] [blame] | 1559 | if (skb_has_frag_list(skb)) { |
| 1560 | struct sk_buff *nskb = |
| 1561 | skb_shinfo(skb)->frag_list; |
| 1562 | skb_shinfo(nskb)->nr_frags = 0; |
| 1563 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1564 | kfree_skb(skb); |
| 1565 | continue; |
| 1566 | } |
| 1567 | |
| 1568 | data_len = skb->len; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1569 | callback_param(queue, pending_idx).ctx = NULL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1570 | if (data_len < txp->size) { |
| 1571 | /* Append the packet payload as a fragment. */ |
| 1572 | txp->offset += data_len; |
| 1573 | txp->size -= data_len; |
| 1574 | } else { |
| 1575 | /* Schedule a response immediately. */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1576 | xenvif_idx_release(queue, pending_idx, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1577 | XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | if (txp->flags & XEN_NETTXF_csum_blank) |
| 1581 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 1582 | else if (txp->flags & XEN_NETTXF_data_validated) |
| 1583 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1584 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1585 | xenvif_fill_frags(queue, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1586 | |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1587 | if (unlikely(skb_has_frag_list(skb))) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1588 | if (xenvif_handle_frag_list(queue, skb)) { |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1589 | if (net_ratelimit()) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1590 | netdev_err(queue->vif->dev, |
Zoltan Kiss | e3377f3 | 2014-03-06 21:48:29 +0000 | [diff] [blame] | 1591 | "Not enough memory to consolidate frag_list!\n"); |
| 1592 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
| 1593 | kfree_skb(skb); |
| 1594 | continue; |
| 1595 | } |
| 1596 | } |
| 1597 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame] | 1598 | if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1599 | int target = min_t(int, skb->len, PKT_PROT_LEN); |
| 1600 | __pskb_pull_tail(skb, target - skb_headlen(skb)); |
| 1601 | } |
| 1602 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1603 | skb->dev = queue->vif->dev; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1604 | skb->protocol = eth_type_trans(skb, skb->dev); |
Jason Wang | f9ca8f7 | 2013-03-25 20:19:58 +0000 | [diff] [blame] | 1605 | skb_reset_network_header(skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1606 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1607 | if (checksum_setup(queue, skb)) { |
| 1608 | netdev_dbg(queue->vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1609 | "Can't setup checksum in net_tx_action\n"); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1610 | /* We have to set this flag to trigger the callback */ |
| 1611 | if (skb_shinfo(skb)->destructor_arg) |
| 1612 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1613 | kfree_skb(skb); |
| 1614 | continue; |
| 1615 | } |
| 1616 | |
Jason Wang | 40893fd | 2013-03-26 23:11:22 +0000 | [diff] [blame] | 1617 | skb_probe_transport_header(skb, 0); |
Jason Wang | f9ca8f7 | 2013-03-25 20:19:58 +0000 | [diff] [blame] | 1618 | |
Paul Durrant | b89587a | 2013-12-17 11:44:35 +0000 | [diff] [blame] | 1619 | /* If the packet is GSO then we will have just set up the |
| 1620 | * transport header offset in checksum_setup so it's now |
| 1621 | * straightforward to calculate gso_segs. |
| 1622 | */ |
| 1623 | if (skb_is_gso(skb)) { |
| 1624 | int mss = skb_shinfo(skb)->gso_size; |
| 1625 | int hdrlen = skb_transport_header(skb) - |
| 1626 | skb_mac_header(skb) + |
| 1627 | tcp_hdrlen(skb); |
| 1628 | |
| 1629 | skb_shinfo(skb)->gso_segs = |
| 1630 | DIV_ROUND_UP(skb->len - hdrlen, mss); |
| 1631 | } |
| 1632 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1633 | queue->stats.rx_bytes += skb->len; |
| 1634 | queue->stats.rx_packets++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1635 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1636 | work_done++; |
| 1637 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1638 | /* Set this flag right before netif_receive_skb, otherwise |
| 1639 | * someone might think this packet already left netback, and |
| 1640 | * do a skb_copy_ubufs while we are still in control of the |
| 1641 | * skb. E.g. the __pskb_pull_tail earlier can do such thing. |
| 1642 | */ |
Zoltan Kiss | 1bb332a | 2014-03-06 21:48:28 +0000 | [diff] [blame] | 1643 | if (skb_shinfo(skb)->destructor_arg) { |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1644 | skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1645 | queue->stats.tx_zerocopy_sent++; |
Zoltan Kiss | 1bb332a | 2014-03-06 21:48:28 +0000 | [diff] [blame] | 1646 | } |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1647 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1648 | netif_receive_skb(skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1649 | } |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1650 | |
| 1651 | return work_done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1652 | } |
| 1653 | |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 1654 | void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success) |
| 1655 | { |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1656 | unsigned long flags; |
| 1657 | pending_ring_idx_t index; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1658 | struct xenvif_queue *queue = ubuf_to_queue(ubuf); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1659 | |
| 1660 | /* This is the only place where we grab this lock, to protect callbacks |
| 1661 | * from each other. |
| 1662 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1663 | spin_lock_irqsave(&queue->callback_lock, flags); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1664 | do { |
| 1665 | u16 pending_idx = ubuf->desc; |
| 1666 | ubuf = (struct ubuf_info *) ubuf->ctx; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1667 | BUG_ON(queue->dealloc_prod - queue->dealloc_cons >= |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1668 | MAX_PENDING_REQS); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1669 | index = pending_index(queue->dealloc_prod); |
| 1670 | queue->dealloc_ring[index] = pending_idx; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1671 | /* Sync with xenvif_tx_dealloc_action: |
| 1672 | * insert idx then incr producer. |
| 1673 | */ |
| 1674 | smp_wmb(); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1675 | queue->dealloc_prod++; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1676 | } while (ubuf); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1677 | wake_up(&queue->dealloc_wq); |
| 1678 | spin_unlock_irqrestore(&queue->callback_lock, flags); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1679 | |
Zoltan Kiss | 1bb332a | 2014-03-06 21:48:28 +0000 | [diff] [blame] | 1680 | if (likely(zerocopy_success)) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1681 | queue->stats.tx_zerocopy_success++; |
Zoltan Kiss | 1bb332a | 2014-03-06 21:48:28 +0000 | [diff] [blame] | 1682 | else |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1683 | queue->stats.tx_zerocopy_fail++; |
Zoltan Kiss | 3e2234b | 2014-03-06 21:48:25 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1686 | static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue) |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1687 | { |
| 1688 | struct gnttab_unmap_grant_ref *gop; |
| 1689 | pending_ring_idx_t dc, dp; |
| 1690 | u16 pending_idx, pending_idx_release[MAX_PENDING_REQS]; |
| 1691 | unsigned int i = 0; |
| 1692 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1693 | dc = queue->dealloc_cons; |
| 1694 | gop = queue->tx_unmap_ops; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1695 | |
| 1696 | /* Free up any grants we have finished using */ |
| 1697 | do { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1698 | dp = queue->dealloc_prod; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1699 | |
| 1700 | /* Ensure we see all indices enqueued by all |
| 1701 | * xenvif_zerocopy_callback(). |
| 1702 | */ |
| 1703 | smp_rmb(); |
| 1704 | |
| 1705 | while (dc != dp) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1706 | BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1707 | pending_idx = |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1708 | queue->dealloc_ring[pending_index(dc++)]; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1709 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1710 | pending_idx_release[gop-queue->tx_unmap_ops] = |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1711 | pending_idx; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1712 | queue->pages_to_unmap[gop-queue->tx_unmap_ops] = |
| 1713 | queue->mmap_pages[pending_idx]; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1714 | gnttab_set_unmap_op(gop, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1715 | idx_to_kaddr(queue, pending_idx), |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1716 | GNTMAP_host_map, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1717 | queue->grant_tx_handle[pending_idx]); |
| 1718 | xenvif_grant_handle_reset(queue, pending_idx); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1719 | ++gop; |
| 1720 | } |
| 1721 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1722 | } while (dp != queue->dealloc_prod); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1723 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1724 | queue->dealloc_cons = dc; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1725 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1726 | if (gop - queue->tx_unmap_ops > 0) { |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1727 | int ret; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1728 | ret = gnttab_unmap_refs(queue->tx_unmap_ops, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1729 | NULL, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1730 | queue->pages_to_unmap, |
| 1731 | gop - queue->tx_unmap_ops); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1732 | if (ret) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1733 | netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n", |
| 1734 | gop - queue->tx_unmap_ops, ret); |
| 1735 | for (i = 0; i < gop - queue->tx_unmap_ops; ++i) { |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1736 | if (gop[i].status != GNTST_okay) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1737 | netdev_err(queue->vif->dev, |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1738 | " host_addr: %llx handle: %x status: %d\n", |
| 1739 | gop[i].host_addr, |
| 1740 | gop[i].handle, |
| 1741 | gop[i].status); |
| 1742 | } |
| 1743 | BUG(); |
| 1744 | } |
| 1745 | } |
| 1746 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1747 | for (i = 0; i < gop - queue->tx_unmap_ops; ++i) |
| 1748 | xenvif_idx_release(queue, pending_idx_release[i], |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1749 | XEN_NETIF_RSP_OKAY); |
| 1750 | } |
| 1751 | |
| 1752 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1753 | /* Called after netfront has transmitted */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1754 | int xenvif_tx_action(struct xenvif_queue *queue, int budget) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1755 | { |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1756 | unsigned nr_mops, nr_cops = 0; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1757 | int work_done, ret; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1758 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1759 | if (unlikely(!tx_work_todo(queue))) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1760 | return 0; |
| 1761 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1762 | xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1763 | |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1764 | if (nr_cops == 0) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1765 | return 0; |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 1766 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1767 | gnttab_batch_copy(queue->tx_copy_ops, nr_cops); |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1768 | if (nr_mops != 0) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1769 | ret = gnttab_map_refs(queue->tx_map_ops, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1770 | NULL, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1771 | queue->pages_to_map, |
Zoltan Kiss | bdab827 | 2014-04-02 18:04:58 +0100 | [diff] [blame] | 1772 | nr_mops); |
| 1773 | BUG_ON(ret); |
| 1774 | } |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1775 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1776 | work_done = xenvif_tx_submit(queue); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1777 | |
| 1778 | return work_done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1781 | static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1782 | u8 status) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1783 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1784 | struct pending_tx_info *pending_tx_info; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1785 | pending_ring_idx_t index; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1786 | unsigned long flags; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1787 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1788 | pending_tx_info = &queue->pending_tx_info[pending_idx]; |
| 1789 | spin_lock_irqsave(&queue->response_lock, flags); |
| 1790 | make_tx_response(queue, &pending_tx_info->req, status); |
| 1791 | index = pending_index(queue->pending_prod); |
| 1792 | queue->pending_ring[index] = pending_idx; |
Zoltan Kiss | 62bad31 | 2014-03-06 21:48:27 +0000 | [diff] [blame] | 1793 | /* TX shouldn't use the index before we give it back here */ |
| 1794 | mb(); |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1795 | queue->pending_prod++; |
| 1796 | spin_unlock_irqrestore(&queue->response_lock, flags); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1799 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1800 | static void make_tx_response(struct xenvif_queue *queue, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1801 | struct xen_netif_tx_request *txp, |
| 1802 | s8 st) |
| 1803 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1804 | RING_IDX i = queue->tx.rsp_prod_pvt; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1805 | struct xen_netif_tx_response *resp; |
| 1806 | int notify; |
| 1807 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1808 | resp = RING_GET_RESPONSE(&queue->tx, i); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1809 | resp->id = txp->id; |
| 1810 | resp->status = st; |
| 1811 | |
| 1812 | if (txp->flags & XEN_NETTXF_extra_info) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1813 | RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1814 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1815 | queue->tx.rsp_prod_pvt = ++i; |
| 1816 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1817 | if (notify) |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1818 | notify_remote_via_irq(queue->tx_irq); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1821 | static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1822 | u16 id, |
| 1823 | s8 st, |
| 1824 | u16 offset, |
| 1825 | u16 size, |
| 1826 | u16 flags) |
| 1827 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1828 | RING_IDX i = queue->rx.rsp_prod_pvt; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1829 | struct xen_netif_rx_response *resp; |
| 1830 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1831 | resp = RING_GET_RESPONSE(&queue->rx, i); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1832 | resp->offset = offset; |
| 1833 | resp->flags = flags; |
| 1834 | resp->id = id; |
| 1835 | resp->status = (s16)size; |
| 1836 | if (st < 0) |
| 1837 | resp->status = (s16)st; |
| 1838 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1839 | queue->rx.rsp_prod_pvt = ++i; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1840 | |
| 1841 | return resp; |
| 1842 | } |
| 1843 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1844 | void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx) |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1845 | { |
| 1846 | int ret; |
| 1847 | struct gnttab_unmap_grant_ref tx_unmap_op; |
| 1848 | |
| 1849 | gnttab_set_unmap_op(&tx_unmap_op, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1850 | idx_to_kaddr(queue, pending_idx), |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1851 | GNTMAP_host_map, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1852 | queue->grant_tx_handle[pending_idx]); |
| 1853 | xenvif_grant_handle_reset(queue, pending_idx); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1854 | |
| 1855 | ret = gnttab_unmap_refs(&tx_unmap_op, NULL, |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1856 | &queue->mmap_pages[pending_idx], 1); |
Zoltan Kiss | 7aceb47 | 2014-03-24 23:59:51 +0000 | [diff] [blame] | 1857 | if (ret) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1858 | netdev_err(queue->vif->dev, |
Zoltan Kiss | 7aceb47 | 2014-03-24 23:59:51 +0000 | [diff] [blame] | 1859 | "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n", |
| 1860 | ret, |
| 1861 | pending_idx, |
| 1862 | tx_unmap_op.host_addr, |
| 1863 | tx_unmap_op.handle, |
| 1864 | tx_unmap_op.status); |
| 1865 | BUG(); |
| 1866 | } |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1869 | static inline int rx_work_todo(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1870 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1871 | return (!skb_queue_empty(&queue->rx_queue) && |
| 1872 | xenvif_rx_ring_slots_available(queue, queue->rx_last_skb_slots)) || |
| 1873 | queue->rx_queue_purge; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1876 | static inline int tx_work_todo(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1877 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1878 | if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx))) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1879 | return 1; |
| 1880 | |
| 1881 | return 0; |
| 1882 | } |
| 1883 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1884 | static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue) |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1885 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1886 | return queue->dealloc_cons != queue->dealloc_prod; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1889 | void xenvif_unmap_frontend_rings(struct xenvif_queue *queue) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1890 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1891 | if (queue->tx.sring) |
| 1892 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif), |
| 1893 | queue->tx.sring); |
| 1894 | if (queue->rx.sring) |
| 1895 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif), |
| 1896 | queue->rx.sring); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1899 | int xenvif_map_frontend_rings(struct xenvif_queue *queue, |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1900 | grant_ref_t tx_ring_ref, |
| 1901 | grant_ref_t rx_ring_ref) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1902 | { |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1903 | void *addr; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1904 | struct xen_netif_tx_sring *txs; |
| 1905 | struct xen_netif_rx_sring *rxs; |
| 1906 | |
| 1907 | int err = -ENOMEM; |
| 1908 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1909 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1910 | tx_ring_ref, &addr); |
| 1911 | if (err) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1912 | goto err; |
| 1913 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1914 | txs = (struct xen_netif_tx_sring *)addr; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1915 | BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1916 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1917 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif), |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1918 | rx_ring_ref, &addr); |
| 1919 | if (err) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1920 | goto err; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1921 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1922 | rxs = (struct xen_netif_rx_sring *)addr; |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1923 | BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1924 | |
| 1925 | return 0; |
| 1926 | |
| 1927 | err: |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1928 | xenvif_unmap_frontend_rings(queue); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1929 | return err; |
| 1930 | } |
| 1931 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1932 | static void xenvif_start_queue(struct xenvif_queue *queue) |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1933 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1934 | if (xenvif_schedulable(queue->vif)) |
| 1935 | xenvif_wake_queue(queue); |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1936 | } |
| 1937 | |
Zoltan Kiss | 121fa4b | 2014-03-06 21:48:24 +0000 | [diff] [blame] | 1938 | int xenvif_kthread_guest_rx(void *data) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1939 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1940 | struct xenvif_queue *queue = data; |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1941 | struct sk_buff *skb; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1942 | |
| 1943 | while (!kthread_should_stop()) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1944 | wait_event_interruptible(queue->wq, |
| 1945 | rx_work_todo(queue) || |
| 1946 | queue->vif->disabled || |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1947 | kthread_should_stop()); |
Wei Liu | e9d8b2c | 2014-04-01 12:46:12 +0100 | [diff] [blame] | 1948 | |
| 1949 | /* This frontend is found to be rogue, disable it in |
| 1950 | * kthread context. Currently this is only set when |
| 1951 | * netback finds out frontend sends malformed packet, |
| 1952 | * but we cannot disable the interface in softirq |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1953 | * context so we defer it here, if this thread is |
| 1954 | * associated with queue 0. |
Wei Liu | e9d8b2c | 2014-04-01 12:46:12 +0100 | [diff] [blame] | 1955 | */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1956 | if (unlikely(queue->vif->disabled && netif_carrier_ok(queue->vif->dev) && queue->id == 0)) |
| 1957 | xenvif_carrier_off(queue->vif); |
Wei Liu | e9d8b2c | 2014-04-01 12:46:12 +0100 | [diff] [blame] | 1958 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1959 | if (kthread_should_stop()) |
| 1960 | break; |
| 1961 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1962 | if (queue->rx_queue_purge) { |
| 1963 | skb_queue_purge(&queue->rx_queue); |
| 1964 | queue->rx_queue_purge = false; |
Zoltan Kiss | 0935078 | 2014-03-06 21:48:30 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1967 | if (!skb_queue_empty(&queue->rx_queue)) |
| 1968 | xenvif_rx_action(queue); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1969 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1970 | if (skb_queue_empty(&queue->rx_queue) && |
| 1971 | xenvif_queue_stopped(queue)) { |
| 1972 | del_timer_sync(&queue->wake_queue); |
| 1973 | xenvif_start_queue(queue); |
Zoltan Kiss | 0935078 | 2014-03-06 21:48:30 +0000 | [diff] [blame] | 1974 | } |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1975 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1976 | cond_resched(); |
| 1977 | } |
| 1978 | |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1979 | /* Bin any remaining skbs */ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1980 | while ((skb = skb_dequeue(&queue->rx_queue)) != NULL) |
Paul Durrant | ca2f09f | 2013-12-06 16:36:07 +0000 | [diff] [blame] | 1981 | dev_kfree_skb(skb); |
| 1982 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1983 | return 0; |
| 1984 | } |
| 1985 | |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1986 | int xenvif_dealloc_kthread(void *data) |
| 1987 | { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1988 | struct xenvif_queue *queue = data; |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1989 | |
| 1990 | while (!kthread_should_stop()) { |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1991 | wait_event_interruptible(queue->dealloc_wq, |
| 1992 | tx_dealloc_work_todo(queue) || |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1993 | kthread_should_stop()); |
| 1994 | if (kthread_should_stop()) |
| 1995 | break; |
| 1996 | |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 1997 | xenvif_tx_dealloc_action(queue); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 1998 | cond_resched(); |
| 1999 | } |
| 2000 | |
| 2001 | /* Unmap anything remaining*/ |
Wei Liu | e9ce7cb | 2014-06-04 10:30:42 +0100 | [diff] [blame] | 2002 | if (tx_dealloc_work_todo(queue)) |
| 2003 | xenvif_tx_dealloc_action(queue); |
Zoltan Kiss | f53c3fe | 2014-03-06 21:48:26 +0000 | [diff] [blame] | 2004 | |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2008 | static int __init netback_init(void) |
| 2009 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2010 | int rc = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2011 | |
Daniel De Graaf | 2a14b244 | 2011-12-14 15:12:13 -0500 | [diff] [blame] | 2012 | if (!xen_domain()) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2013 | return -ENODEV; |
| 2014 | |
Andrew J. Bennieston | 8d3d53b | 2014-06-04 10:30:43 +0100 | [diff] [blame] | 2015 | /* Allow as many queues as there are CPUs, by default */ |
| 2016 | xenvif_max_queues = num_online_cpus(); |
| 2017 | |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 2018 | if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) { |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 2019 | pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n", |
| 2020 | fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 2021 | fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2024 | rc = xenvif_xenbus_init(); |
| 2025 | if (rc) |
| 2026 | goto failed_init; |
| 2027 | |
Zoltan Kiss | 0935078 | 2014-03-06 21:48:30 +0000 | [diff] [blame] | 2028 | rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs); |
| 2029 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2030 | return 0; |
| 2031 | |
| 2032 | failed_init: |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2033 | return rc; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | module_init(netback_init); |
| 2037 | |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 2038 | static void __exit netback_fini(void) |
| 2039 | { |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 2040 | xenvif_xenbus_fini(); |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 2041 | } |
| 2042 | module_exit(netback_fini); |
| 2043 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 2044 | MODULE_LICENSE("Dual BSD/GPL"); |
Bastian Blank | f984cec | 2011-06-30 11:19:09 -0700 | [diff] [blame] | 2045 | MODULE_ALIAS("xen-backend:vif"); |