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