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