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> |
| 40 | |
| 41 | #include <net/tcp.h> |
| 42 | |
Stefano Stabellini | ca98163 | 2012-08-08 17:21:23 +0000 | [diff] [blame] | 43 | #include <xen/xen.h> |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 44 | #include <xen/events.h> |
| 45 | #include <xen/interface/memory.h> |
| 46 | |
| 47 | #include <asm/xen/hypercall.h> |
| 48 | #include <asm/xen/page.h> |
| 49 | |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 50 | /* Provide an option to disable split event channels at load time as |
| 51 | * event channels are limited resource. Split event channels are |
| 52 | * enabled by default. |
| 53 | */ |
| 54 | bool separate_tx_rx_irq = 1; |
| 55 | module_param(separate_tx_rx_irq, bool, 0644); |
| 56 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 57 | /* |
| 58 | * This is the maximum slots a skb can have. If a guest sends a skb |
| 59 | * which exceeds this limit it is considered malicious. |
| 60 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 61 | #define FATAL_SKB_SLOTS_DEFAULT 20 |
| 62 | static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT; |
| 63 | module_param(fatal_skb_slots, uint, 0444); |
| 64 | |
| 65 | /* |
| 66 | * To avoid confusion, we define XEN_NETBK_LEGACY_SLOTS_MAX indicating |
| 67 | * the maximum slots a valid packet can use. Now this value is defined |
| 68 | * to be XEN_NETIF_NR_SLOTS_MIN, which is supposed to be supported by |
| 69 | * all backend. |
| 70 | */ |
| 71 | #define XEN_NETBK_LEGACY_SLOTS_MAX XEN_NETIF_NR_SLOTS_MIN |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 72 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 73 | /* |
| 74 | * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of |
| 75 | * one or more merged tx requests, otherwise it is the continuation of |
| 76 | * previous tx request. |
| 77 | */ |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 78 | static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx) |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 79 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 80 | return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 83 | static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx, |
| 84 | u8 status); |
| 85 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 86 | static void make_tx_response(struct xenvif *vif, |
| 87 | struct xen_netif_tx_request *txp, |
| 88 | s8 st); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 89 | |
| 90 | static inline int tx_work_todo(struct xenvif *vif); |
| 91 | static inline int rx_work_todo(struct xenvif *vif); |
| 92 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 93 | static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif, |
| 94 | u16 id, |
| 95 | s8 st, |
| 96 | u16 offset, |
| 97 | u16 size, |
| 98 | u16 flags); |
| 99 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 100 | static inline unsigned long idx_to_pfn(struct xenvif *vif, |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 101 | u16 idx) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 102 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 103 | return page_to_pfn(vif->mmap_pages[idx]); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 106 | static inline unsigned long idx_to_kaddr(struct xenvif *vif, |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 107 | u16 idx) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 108 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 109 | return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 112 | /* This is a miniumum size for the linear area to avoid lots of |
| 113 | * calls to __pskb_pull_tail() as we set up checksum offsets. The |
| 114 | * value 128 was chosen as it covers all IPv4 and most likely |
| 115 | * IPv6 headers. |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 116 | */ |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 117 | #define PKT_PROT_LEN 128 |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 118 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 119 | static u16 frag_get_pending_idx(skb_frag_t *frag) |
| 120 | { |
| 121 | return (u16)frag->page_offset; |
| 122 | } |
| 123 | |
| 124 | static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx) |
| 125 | { |
| 126 | frag->page_offset = pending_idx; |
| 127 | } |
| 128 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 129 | static inline pending_ring_idx_t pending_index(unsigned i) |
| 130 | { |
| 131 | return i & (MAX_PENDING_REQS-1); |
| 132 | } |
| 133 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 134 | static inline pending_ring_idx_t nr_pending_reqs(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 135 | { |
| 136 | return MAX_PENDING_REQS - |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 137 | vif->pending_prod + vif->pending_cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int max_required_rx_slots(struct xenvif *vif) |
| 141 | { |
| 142 | int max = DIV_ROUND_UP(vif->dev->mtu, PAGE_SIZE); |
| 143 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 144 | /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 145 | if (vif->can_sg || vif->gso || vif->gso_prefix) |
| 146 | max += MAX_SKB_FRAGS + 1; /* extra_info + frags */ |
| 147 | |
| 148 | return max; |
| 149 | } |
| 150 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 151 | int xenvif_rx_ring_full(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 152 | { |
| 153 | RING_IDX peek = vif->rx_req_cons_peek; |
| 154 | RING_IDX needed = max_required_rx_slots(vif); |
| 155 | |
| 156 | return ((vif->rx.sring->req_prod - peek) < needed) || |
| 157 | ((vif->rx.rsp_prod_pvt + XEN_NETIF_RX_RING_SIZE - peek) < needed); |
| 158 | } |
| 159 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 160 | int xenvif_must_stop_queue(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 161 | { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 162 | if (!xenvif_rx_ring_full(vif)) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 163 | return 0; |
| 164 | |
| 165 | vif->rx.sring->req_event = vif->rx_req_cons_peek + |
| 166 | max_required_rx_slots(vif); |
| 167 | mb(); /* request notification /then/ check the queue */ |
| 168 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 169 | return xenvif_rx_ring_full(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Returns true if we should start a new receive buffer instead of |
| 174 | * adding 'size' bytes to a buffer which currently contains 'offset' |
| 175 | * bytes. |
| 176 | */ |
| 177 | static bool start_new_rx_buffer(int offset, unsigned long size, int head) |
| 178 | { |
| 179 | /* simple case: we have completely filled the current buffer. */ |
| 180 | if (offset == MAX_BUFFER_OFFSET) |
| 181 | return true; |
| 182 | |
| 183 | /* |
| 184 | * complex case: start a fresh buffer if the current frag |
| 185 | * would overflow the current buffer but only if: |
| 186 | * (i) this frag would fit completely in the next buffer |
| 187 | * and (ii) there is already some data in the current buffer |
| 188 | * and (iii) this is not the head buffer. |
| 189 | * |
| 190 | * Where: |
| 191 | * - (i) stops us splitting a frag into two copies |
| 192 | * unless the frag is too large for a single buffer. |
| 193 | * - (ii) stops us from leaving a buffer pointlessly empty. |
| 194 | * - (iii) stops us leaving the first buffer |
| 195 | * empty. Strictly speaking this is already covered |
| 196 | * by (ii) but is explicitly checked because |
| 197 | * netfront relies on the first buffer being |
| 198 | * non-empty and can crash otherwise. |
| 199 | * |
| 200 | * This means we will effectively linearise small |
| 201 | * frags but do not needlessly split large buffers |
| 202 | * into multiple copies tend to give large frags their |
| 203 | * own buffers as before. |
| 204 | */ |
| 205 | if ((offset + size > MAX_BUFFER_OFFSET) && |
| 206 | (size <= MAX_BUFFER_OFFSET) && offset && !head) |
| 207 | return true; |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 212 | struct xenvif_count_slot_state { |
| 213 | unsigned long copy_off; |
| 214 | bool head; |
| 215 | }; |
| 216 | |
| 217 | unsigned int xenvif_count_frag_slots(struct xenvif *vif, |
| 218 | unsigned long offset, unsigned long size, |
| 219 | struct xenvif_count_slot_state *state) |
| 220 | { |
| 221 | unsigned count = 0; |
| 222 | |
| 223 | offset &= ~PAGE_MASK; |
| 224 | |
| 225 | while (size > 0) { |
| 226 | unsigned long bytes; |
| 227 | |
| 228 | bytes = PAGE_SIZE - offset; |
| 229 | |
| 230 | if (bytes > size) |
| 231 | bytes = size; |
| 232 | |
| 233 | if (start_new_rx_buffer(state->copy_off, bytes, state->head)) { |
| 234 | count++; |
| 235 | state->copy_off = 0; |
| 236 | } |
| 237 | |
| 238 | if (state->copy_off + bytes > MAX_BUFFER_OFFSET) |
| 239 | bytes = MAX_BUFFER_OFFSET - state->copy_off; |
| 240 | |
| 241 | state->copy_off += bytes; |
| 242 | |
| 243 | offset += bytes; |
| 244 | size -= bytes; |
| 245 | |
| 246 | if (offset == PAGE_SIZE) |
| 247 | offset = 0; |
| 248 | |
| 249 | state->head = false; |
| 250 | } |
| 251 | |
| 252 | return count; |
| 253 | } |
| 254 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Figure out how many ring slots we're going to need to send @skb to |
| 257 | * the guest. This function is essentially a dry run of |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 258 | * xenvif_gop_frag_copy. |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 259 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 260 | unsigned int xenvif_count_skb_slots(struct xenvif *vif, struct sk_buff *skb) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 261 | { |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 262 | struct xenvif_count_slot_state state; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 263 | unsigned int count; |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 264 | unsigned char *data; |
| 265 | unsigned i; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 266 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 267 | state.head = true; |
| 268 | state.copy_off = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 269 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 270 | /* Slot for the first (partial) page of data. */ |
| 271 | count = 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 272 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 273 | /* Need a slot for the GSO prefix for GSO extra data? */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 274 | if (skb_shinfo(skb)->gso_size) |
| 275 | count++; |
| 276 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 277 | data = skb->data; |
| 278 | while (data < skb_tail_pointer(skb)) { |
| 279 | unsigned long offset = offset_in_page(data); |
| 280 | unsigned long size = PAGE_SIZE - offset; |
| 281 | |
| 282 | if (data + size > skb_tail_pointer(skb)) |
| 283 | size = skb_tail_pointer(skb) - data; |
| 284 | |
| 285 | count += xenvif_count_frag_slots(vif, offset, size, &state); |
| 286 | |
| 287 | data += size; |
| 288 | } |
| 289 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 290 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 291 | unsigned long size = skb_frag_size(&skb_shinfo(skb)->frags[i]); |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 292 | unsigned long offset = skb_shinfo(skb)->frags[i].page_offset; |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 293 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 294 | count += xenvif_count_frag_slots(vif, offset, size, &state); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 295 | } |
| 296 | return count; |
| 297 | } |
| 298 | |
| 299 | struct netrx_pending_operations { |
| 300 | unsigned copy_prod, copy_cons; |
| 301 | unsigned meta_prod, meta_cons; |
| 302 | struct gnttab_copy *copy; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 303 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 304 | int copy_off; |
| 305 | grant_ref_t copy_gref; |
| 306 | }; |
| 307 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 308 | static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif, |
| 309 | struct netrx_pending_operations *npo) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 310 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 311 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 312 | struct xen_netif_rx_request *req; |
| 313 | |
| 314 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); |
| 315 | |
| 316 | meta = npo->meta + npo->meta_prod++; |
| 317 | meta->gso_size = 0; |
| 318 | meta->size = 0; |
| 319 | meta->id = req->id; |
| 320 | |
| 321 | npo->copy_off = 0; |
| 322 | npo->copy_gref = req->gref; |
| 323 | |
| 324 | return meta; |
| 325 | } |
| 326 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Set up the grant operations for this fragment. If it's a flipping |
| 329 | * interface, we also set up the unmap request from here. |
| 330 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 331 | static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb, |
| 332 | struct netrx_pending_operations *npo, |
| 333 | struct page *page, unsigned long size, |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 334 | unsigned long offset, int *head) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 335 | { |
| 336 | struct gnttab_copy *copy_gop; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 337 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 338 | unsigned long bytes; |
| 339 | |
| 340 | /* Data must not cross a page boundary. */ |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 341 | BUG_ON(size + offset > PAGE_SIZE<<compound_order(page)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 342 | |
| 343 | meta = npo->meta + npo->meta_prod - 1; |
| 344 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 345 | /* Skip unused frames from start of page */ |
| 346 | page += offset >> PAGE_SHIFT; |
| 347 | offset &= ~PAGE_MASK; |
| 348 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 349 | while (size > 0) { |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 350 | BUG_ON(offset >= PAGE_SIZE); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 351 | BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET); |
| 352 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 353 | bytes = PAGE_SIZE - offset; |
| 354 | |
| 355 | if (bytes > size) |
| 356 | bytes = size; |
| 357 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 358 | if (start_new_rx_buffer(npo->copy_off, bytes, *head)) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 359 | /* |
| 360 | * Netfront requires there to be some data in the head |
| 361 | * buffer. |
| 362 | */ |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 363 | BUG_ON(*head); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 364 | |
| 365 | meta = get_next_rx_buffer(vif, npo); |
| 366 | } |
| 367 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 368 | if (npo->copy_off + bytes > MAX_BUFFER_OFFSET) |
| 369 | bytes = MAX_BUFFER_OFFSET - npo->copy_off; |
| 370 | |
| 371 | copy_gop = npo->copy + npo->copy_prod++; |
| 372 | copy_gop->flags = GNTCOPY_dest_gref; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 373 | copy_gop->len = bytes; |
| 374 | |
Wei Liu | 43e9d19 | 2013-08-26 12:59:37 +0100 | [diff] [blame] | 375 | copy_gop->source.domid = DOMID_SELF; |
| 376 | copy_gop->source.u.gmfn = virt_to_mfn(page_address(page)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 377 | copy_gop->source.offset = offset; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 378 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 379 | copy_gop->dest.domid = vif->domid; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 380 | copy_gop->dest.offset = npo->copy_off; |
| 381 | copy_gop->dest.u.ref = npo->copy_gref; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 382 | |
| 383 | npo->copy_off += bytes; |
| 384 | meta->size += bytes; |
| 385 | |
| 386 | offset += bytes; |
| 387 | size -= bytes; |
| 388 | |
Ian Campbell | 6a8ed46 | 2012-10-10 03:48:42 +0000 | [diff] [blame] | 389 | /* Next frame */ |
| 390 | if (offset == PAGE_SIZE && size) { |
| 391 | BUG_ON(!PageCompound(page)); |
| 392 | page++; |
| 393 | offset = 0; |
| 394 | } |
| 395 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 396 | /* Leave a gap for the GSO descriptor. */ |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 397 | if (*head && skb_shinfo(skb)->gso_size && !vif->gso_prefix) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 398 | vif->rx.req_cons++; |
| 399 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 400 | *head = 0; /* There must be something in this buffer now. */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 401 | |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Prepare an SKB to be transmitted to the frontend. |
| 407 | * |
| 408 | * This function is responsible for allocating grant operations, meta |
| 409 | * structures, etc. |
| 410 | * |
| 411 | * It returns the number of meta structures consumed. The number of |
| 412 | * ring slots used is always equal to the number of meta slots used |
| 413 | * plus the number of GSO descriptors used. Currently, we use either |
| 414 | * zero GSO descriptors (for non-GSO packets) or one descriptor (for |
| 415 | * frontend-side LRO). |
| 416 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 417 | static int xenvif_gop_skb(struct sk_buff *skb, |
| 418 | struct netrx_pending_operations *npo) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 419 | { |
| 420 | struct xenvif *vif = netdev_priv(skb->dev); |
| 421 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 422 | int i; |
| 423 | struct xen_netif_rx_request *req; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 424 | struct xenvif_rx_meta *meta; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 425 | unsigned char *data; |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 426 | int head = 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 427 | int old_meta_prod; |
| 428 | |
| 429 | old_meta_prod = npo->meta_prod; |
| 430 | |
| 431 | /* Set up a GSO prefix descriptor, if necessary */ |
| 432 | if (skb_shinfo(skb)->gso_size && vif->gso_prefix) { |
| 433 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); |
| 434 | meta = npo->meta + npo->meta_prod++; |
| 435 | meta->gso_size = skb_shinfo(skb)->gso_size; |
| 436 | meta->size = 0; |
| 437 | meta->id = req->id; |
| 438 | } |
| 439 | |
| 440 | req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++); |
| 441 | meta = npo->meta + npo->meta_prod++; |
| 442 | |
| 443 | if (!vif->gso_prefix) |
| 444 | meta->gso_size = skb_shinfo(skb)->gso_size; |
| 445 | else |
| 446 | meta->gso_size = 0; |
| 447 | |
| 448 | meta->size = 0; |
| 449 | meta->id = req->id; |
| 450 | npo->copy_off = 0; |
| 451 | npo->copy_gref = req->gref; |
| 452 | |
| 453 | data = skb->data; |
| 454 | while (data < skb_tail_pointer(skb)) { |
| 455 | unsigned int offset = offset_in_page(data); |
| 456 | unsigned int len = PAGE_SIZE - offset; |
| 457 | |
| 458 | if (data + len > skb_tail_pointer(skb)) |
| 459 | len = skb_tail_pointer(skb) - data; |
| 460 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 461 | xenvif_gop_frag_copy(vif, skb, npo, |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 462 | virt_to_page(data), len, offset, &head); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 463 | data += len; |
| 464 | } |
| 465 | |
| 466 | for (i = 0; i < nr_frags; i++) { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 467 | xenvif_gop_frag_copy(vif, skb, npo, |
| 468 | skb_frag_page(&skb_shinfo(skb)->frags[i]), |
| 469 | skb_frag_size(&skb_shinfo(skb)->frags[i]), |
| 470 | skb_shinfo(skb)->frags[i].page_offset, |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 471 | &head); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return npo->meta_prod - old_meta_prod; |
| 475 | } |
| 476 | |
| 477 | /* |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 478 | * 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] | 479 | * used to set up the operations on the top of |
| 480 | * netrx_pending_operations, which have since been done. Check that |
| 481 | * they didn't give any errors and advance over them. |
| 482 | */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 483 | static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots, |
| 484 | struct netrx_pending_operations *npo) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 485 | { |
| 486 | struct gnttab_copy *copy_op; |
| 487 | int status = XEN_NETIF_RSP_OKAY; |
| 488 | int i; |
| 489 | |
| 490 | for (i = 0; i < nr_meta_slots; i++) { |
| 491 | copy_op = npo->copy + npo->copy_cons++; |
| 492 | if (copy_op->status != GNTST_okay) { |
| 493 | netdev_dbg(vif->dev, |
| 494 | "Bad status %d from copy to DOM%d.\n", |
| 495 | copy_op->status, vif->domid); |
| 496 | status = XEN_NETIF_RSP_ERROR; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | return status; |
| 501 | } |
| 502 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 503 | static void xenvif_add_frag_responses(struct xenvif *vif, int status, |
| 504 | struct xenvif_rx_meta *meta, |
| 505 | int nr_meta_slots) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 506 | { |
| 507 | int i; |
| 508 | unsigned long offset; |
| 509 | |
| 510 | /* No fragments used */ |
| 511 | if (nr_meta_slots <= 1) |
| 512 | return; |
| 513 | |
| 514 | nr_meta_slots--; |
| 515 | |
| 516 | for (i = 0; i < nr_meta_slots; i++) { |
| 517 | int flags; |
| 518 | if (i == nr_meta_slots - 1) |
| 519 | flags = 0; |
| 520 | else |
| 521 | flags = XEN_NETRXF_more_data; |
| 522 | |
| 523 | offset = 0; |
| 524 | make_rx_response(vif, meta[i].id, status, offset, |
| 525 | meta[i].size, flags); |
| 526 | } |
| 527 | } |
| 528 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 529 | struct skb_cb_overlay { |
| 530 | int meta_slots_used; |
| 531 | }; |
| 532 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 533 | static void xenvif_kick_thread(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 534 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 535 | wake_up(&vif->wq); |
| 536 | } |
| 537 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 538 | void xenvif_rx_action(struct xenvif *vif) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 539 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 540 | s8 status; |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 541 | u16 flags; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 542 | struct xen_netif_rx_response *resp; |
| 543 | struct sk_buff_head rxq; |
| 544 | struct sk_buff *skb; |
| 545 | LIST_HEAD(notify); |
| 546 | int ret; |
| 547 | int nr_frags; |
| 548 | int count; |
| 549 | unsigned long offset; |
| 550 | struct skb_cb_overlay *sco; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 551 | int need_to_notify = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 552 | |
| 553 | struct netrx_pending_operations npo = { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 554 | .copy = vif->grant_copy_op, |
| 555 | .meta = vif->meta, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 556 | }; |
| 557 | |
| 558 | skb_queue_head_init(&rxq); |
| 559 | |
| 560 | count = 0; |
| 561 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 562 | while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 563 | vif = netdev_priv(skb->dev); |
| 564 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 565 | |
| 566 | sco = (struct skb_cb_overlay *)skb->cb; |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 567 | sco->meta_slots_used = xenvif_gop_skb(skb, &npo); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 568 | |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 569 | count += nr_frags + 1; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 570 | |
| 571 | __skb_queue_tail(&rxq, skb); |
| 572 | |
| 573 | /* Filled the batch queue? */ |
Wei Liu | 33bc801 | 2013-10-08 10:54:21 +0100 | [diff] [blame] | 574 | /* XXX FIXME: RX path dependent on MAX_SKB_FRAGS */ |
| 575 | if (count + MAX_SKB_FRAGS >= XEN_NETIF_RX_RING_SIZE) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 579 | BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 580 | |
| 581 | if (!npo.copy_prod) |
| 582 | return; |
| 583 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 584 | BUG_ON(npo.copy_prod > ARRAY_SIZE(vif->grant_copy_op)); |
| 585 | gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 586 | |
| 587 | while ((skb = __skb_dequeue(&rxq)) != NULL) { |
| 588 | sco = (struct skb_cb_overlay *)skb->cb; |
| 589 | |
| 590 | vif = netdev_priv(skb->dev); |
| 591 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 592 | if (vif->meta[npo.meta_cons].gso_size && vif->gso_prefix) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 593 | resp = RING_GET_RESPONSE(&vif->rx, |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 594 | vif->rx.rsp_prod_pvt++); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 595 | |
| 596 | resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data; |
| 597 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 598 | resp->offset = vif->meta[npo.meta_cons].gso_size; |
| 599 | resp->id = vif->meta[npo.meta_cons].id; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 600 | resp->status = sco->meta_slots_used; |
| 601 | |
| 602 | npo.meta_cons++; |
| 603 | sco->meta_slots_used--; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | vif->dev->stats.tx_bytes += skb->len; |
| 608 | vif->dev->stats.tx_packets++; |
| 609 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 610 | status = xenvif_check_gop(vif, sco->meta_slots_used, &npo); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 611 | |
| 612 | if (sco->meta_slots_used == 1) |
| 613 | flags = 0; |
| 614 | else |
| 615 | flags = XEN_NETRXF_more_data; |
| 616 | |
| 617 | if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */ |
| 618 | flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated; |
| 619 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
| 620 | /* remote but checksummed. */ |
| 621 | flags |= XEN_NETRXF_data_validated; |
| 622 | |
| 623 | offset = 0; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 624 | resp = make_rx_response(vif, vif->meta[npo.meta_cons].id, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 625 | status, offset, |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 626 | vif->meta[npo.meta_cons].size, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 627 | flags); |
| 628 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 629 | if (vif->meta[npo.meta_cons].gso_size && !vif->gso_prefix) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 630 | struct xen_netif_extra_info *gso = |
| 631 | (struct xen_netif_extra_info *) |
| 632 | RING_GET_RESPONSE(&vif->rx, |
| 633 | vif->rx.rsp_prod_pvt++); |
| 634 | |
| 635 | resp->flags |= XEN_NETRXF_extra_info; |
| 636 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 637 | gso->u.gso.size = vif->meta[npo.meta_cons].gso_size; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 638 | gso->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4; |
| 639 | gso->u.gso.pad = 0; |
| 640 | gso->u.gso.features = 0; |
| 641 | |
| 642 | gso->type = XEN_NETIF_EXTRA_TYPE_GSO; |
| 643 | gso->flags = 0; |
| 644 | } |
| 645 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 646 | xenvif_add_frag_responses(vif, status, |
| 647 | vif->meta + npo.meta_cons + 1, |
| 648 | sco->meta_slots_used); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 649 | |
| 650 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 651 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 652 | if (ret) |
| 653 | need_to_notify = 1; |
| 654 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 655 | xenvif_notify_tx_completion(vif); |
| 656 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 657 | npo.meta_cons += sco->meta_slots_used; |
| 658 | dev_kfree_skb(skb); |
| 659 | } |
| 660 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 661 | if (need_to_notify) |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 662 | notify_remote_via_irq(vif->rx_irq); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 663 | |
| 664 | /* More work to do? */ |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 665 | if (!skb_queue_empty(&vif->rx_queue)) |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 666 | xenvif_kick_thread(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 669 | void xenvif_queue_tx_skb(struct xenvif *vif, struct sk_buff *skb) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 670 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 671 | skb_queue_tail(&vif->rx_queue, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 672 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 673 | xenvif_kick_thread(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 676 | void xenvif_check_rx_xenvif(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 677 | { |
| 678 | int more_to_do; |
| 679 | |
| 680 | RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do); |
| 681 | |
| 682 | if (more_to_do) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 683 | napi_schedule(&vif->napi); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static void tx_add_credit(struct xenvif *vif) |
| 687 | { |
| 688 | unsigned long max_burst, max_credit; |
| 689 | |
| 690 | /* |
| 691 | * Allow a burst big enough to transmit a jumbo packet of up to 128kB. |
| 692 | * Otherwise the interface can seize up due to insufficient credit. |
| 693 | */ |
| 694 | max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size; |
| 695 | max_burst = min(max_burst, 131072UL); |
| 696 | max_burst = max(max_burst, vif->credit_bytes); |
| 697 | |
| 698 | /* Take care that adding a new chunk of credit doesn't wrap to zero. */ |
| 699 | max_credit = vif->remaining_credit + vif->credit_bytes; |
| 700 | if (max_credit < vif->remaining_credit) |
| 701 | max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */ |
| 702 | |
| 703 | vif->remaining_credit = min(max_credit, max_burst); |
| 704 | } |
| 705 | |
| 706 | static void tx_credit_callback(unsigned long data) |
| 707 | { |
| 708 | struct xenvif *vif = (struct xenvif *)data; |
| 709 | tx_add_credit(vif); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 710 | xenvif_check_rx_xenvif(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 713 | static void xenvif_tx_err(struct xenvif *vif, |
| 714 | struct xen_netif_tx_request *txp, RING_IDX end) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 715 | { |
| 716 | RING_IDX cons = vif->tx.req_cons; |
| 717 | |
| 718 | do { |
| 719 | make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR); |
Ian Campbell | b914972 | 2013-02-06 23:41:38 +0000 | [diff] [blame] | 720 | if (cons == end) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 721 | break; |
| 722 | txp = RING_GET_REQUEST(&vif->tx, cons++); |
| 723 | } while (1); |
| 724 | vif->tx.req_cons = cons; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 727 | static void xenvif_fatal_tx_err(struct xenvif *vif) |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 728 | { |
| 729 | netdev_err(vif->dev, "fatal error; disabling device\n"); |
| 730 | xenvif_carrier_off(vif); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 733 | static int xenvif_count_requests(struct xenvif *vif, |
| 734 | struct xen_netif_tx_request *first, |
| 735 | struct xen_netif_tx_request *txp, |
| 736 | int work_to_do) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 737 | { |
| 738 | RING_IDX cons = vif->tx.req_cons; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 739 | int slots = 0; |
| 740 | int drop_err = 0; |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 741 | int more_data; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 742 | |
| 743 | if (!(first->flags & XEN_NETTXF_more_data)) |
| 744 | return 0; |
| 745 | |
| 746 | do { |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 747 | struct xen_netif_tx_request dropped_tx = { 0 }; |
| 748 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 749 | if (slots >= work_to_do) { |
| 750 | netdev_err(vif->dev, |
| 751 | "Asked for %d slots but exceeds this limit\n", |
| 752 | work_to_do); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 753 | xenvif_fatal_tx_err(vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 754 | return -ENODATA; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 757 | /* This guest is really using too many slots and |
| 758 | * considered malicious. |
| 759 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 760 | if (unlikely(slots >= fatal_skb_slots)) { |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 761 | netdev_err(vif->dev, |
| 762 | "Malicious frontend using %d slots, threshold %u\n", |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 763 | slots, fatal_skb_slots); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 764 | xenvif_fatal_tx_err(vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 765 | return -E2BIG; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 768 | /* Xen network protocol had implicit dependency on |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 769 | * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to |
| 770 | * the historical MAX_SKB_FRAGS value 18 to honor the |
| 771 | * same behavior as before. Any packet using more than |
| 772 | * 18 slots but less than fatal_skb_slots slots is |
| 773 | * dropped |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 774 | */ |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 775 | if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) { |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 776 | if (net_ratelimit()) |
| 777 | netdev_dbg(vif->dev, |
| 778 | "Too many slots (%d) exceeding limit (%d), dropping packet\n", |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 779 | slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 780 | drop_err = -E2BIG; |
| 781 | } |
| 782 | |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 783 | if (drop_err) |
| 784 | txp = &dropped_tx; |
| 785 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 786 | memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots), |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 787 | sizeof(*txp)); |
Wei Liu | 03393fd5 | 2013-04-22 02:20:43 +0000 | [diff] [blame] | 788 | |
| 789 | /* If the guest submitted a frame >= 64 KiB then |
| 790 | * first->size overflowed and following slots will |
| 791 | * appear to be larger than the frame. |
| 792 | * |
| 793 | * This cannot be fatal error as there are buggy |
| 794 | * frontends that do this. |
| 795 | * |
| 796 | * Consume all slots and drop the packet. |
| 797 | */ |
| 798 | if (!drop_err && txp->size > first->size) { |
| 799 | if (net_ratelimit()) |
| 800 | netdev_dbg(vif->dev, |
| 801 | "Invalid tx request, slot size %u > remaining size %u\n", |
| 802 | txp->size, first->size); |
| 803 | drop_err = -EIO; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | first->size -= txp->size; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 807 | slots++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 808 | |
| 809 | if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) { |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 810 | netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n", |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 811 | txp->offset, txp->size); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 812 | xenvif_fatal_tx_err(vif); |
David Vrabel | 35876b5 | 2013-02-14 03:18:57 +0000 | [diff] [blame] | 813 | return -EINVAL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 814 | } |
Wei Liu | 59ccb4e | 2013-05-02 00:43:58 +0000 | [diff] [blame] | 815 | |
| 816 | more_data = txp->flags & XEN_NETTXF_more_data; |
| 817 | |
| 818 | if (!drop_err) |
| 819 | txp++; |
| 820 | |
| 821 | } while (more_data); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 822 | |
| 823 | if (drop_err) { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 824 | xenvif_tx_err(vif, first, cons + slots); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 825 | return drop_err; |
| 826 | } |
| 827 | |
| 828 | return slots; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 831 | static struct page *xenvif_alloc_page(struct xenvif *vif, |
| 832 | u16 pending_idx) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 833 | { |
| 834 | struct page *page; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 835 | |
| 836 | page = alloc_page(GFP_ATOMIC|__GFP_COLD); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 837 | if (!page) |
| 838 | return NULL; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 839 | vif->mmap_pages[pending_idx] = page; |
| 840 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 841 | return page; |
| 842 | } |
| 843 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 844 | static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif, |
| 845 | struct sk_buff *skb, |
| 846 | struct xen_netif_tx_request *txp, |
| 847 | struct gnttab_copy *gop) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 848 | { |
| 849 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 850 | skb_frag_t *frags = shinfo->frags; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 851 | u16 pending_idx = *((u16 *)skb->data); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 852 | u16 head_idx = 0; |
| 853 | int slot, start; |
| 854 | struct page *page; |
| 855 | pending_ring_idx_t index, start_idx = 0; |
| 856 | uint16_t dst_offset; |
| 857 | unsigned int nr_slots; |
| 858 | struct pending_tx_info *first = NULL; |
| 859 | |
| 860 | /* At this point shinfo->nr_frags is in fact the number of |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 861 | * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX. |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 862 | */ |
| 863 | nr_slots = shinfo->nr_frags; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 864 | |
| 865 | /* 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] | 866 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 867 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 868 | /* Coalesce tx requests, at this point the packet passed in |
| 869 | * should be <= 64K. Any packets larger than 64K have been |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 870 | * handled in xenvif_count_requests(). |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 871 | */ |
| 872 | for (shinfo->nr_frags = slot = start; slot < nr_slots; |
| 873 | shinfo->nr_frags++) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 874 | struct pending_tx_info *pending_tx_info = |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 875 | vif->pending_tx_info; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 876 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 877 | page = alloc_page(GFP_ATOMIC|__GFP_COLD); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 878 | if (!page) |
Ian Campbell | 4cc7c1c | 2013-02-06 23:41:37 +0000 | [diff] [blame] | 879 | goto err; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 880 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 881 | dst_offset = 0; |
| 882 | first = NULL; |
| 883 | while (dst_offset < PAGE_SIZE && slot < nr_slots) { |
| 884 | gop->flags = GNTCOPY_source_gref; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 885 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 886 | gop->source.u.ref = txp->gref; |
| 887 | gop->source.domid = vif->domid; |
| 888 | gop->source.offset = txp->offset; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 889 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 890 | gop->dest.domid = DOMID_SELF; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 891 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 892 | gop->dest.offset = dst_offset; |
| 893 | gop->dest.u.gmfn = virt_to_mfn(page_address(page)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 894 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 895 | if (dst_offset + txp->size > PAGE_SIZE) { |
| 896 | /* This page can only merge a portion |
| 897 | * of tx request. Do not increment any |
| 898 | * pointer / counter here. The txp |
| 899 | * will be dealt with in future |
| 900 | * rounds, eventually hitting the |
| 901 | * `else` branch. |
| 902 | */ |
| 903 | gop->len = PAGE_SIZE - dst_offset; |
| 904 | txp->offset += gop->len; |
| 905 | txp->size -= gop->len; |
| 906 | dst_offset += gop->len; /* quit loop */ |
| 907 | } else { |
| 908 | /* This tx request can be merged in the page */ |
| 909 | gop->len = txp->size; |
| 910 | dst_offset += gop->len; |
| 911 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 912 | index = pending_index(vif->pending_cons++); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 913 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 914 | pending_idx = vif->pending_ring[index]; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 915 | |
| 916 | memcpy(&pending_tx_info[pending_idx].req, txp, |
| 917 | sizeof(*txp)); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 918 | |
| 919 | /* Poison these fields, corresponding |
| 920 | * fields for head tx req will be set |
| 921 | * to correct values after the loop. |
| 922 | */ |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 923 | vif->mmap_pages[pending_idx] = (void *)(~0UL); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 924 | pending_tx_info[pending_idx].head = |
| 925 | INVALID_PENDING_RING_IDX; |
| 926 | |
| 927 | if (!first) { |
| 928 | first = &pending_tx_info[pending_idx]; |
| 929 | start_idx = index; |
| 930 | head_idx = pending_idx; |
| 931 | } |
| 932 | |
| 933 | txp++; |
| 934 | slot++; |
| 935 | } |
| 936 | |
| 937 | gop++; |
| 938 | } |
| 939 | |
| 940 | first->req.offset = 0; |
| 941 | first->req.size = dst_offset; |
| 942 | first->head = start_idx; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 943 | vif->mmap_pages[head_idx] = page; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 944 | frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 947 | BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS); |
| 948 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 949 | return gop; |
Ian Campbell | 4cc7c1c | 2013-02-06 23:41:37 +0000 | [diff] [blame] | 950 | err: |
| 951 | /* Unwind, freeing all pages and sending error responses. */ |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 952 | while (shinfo->nr_frags-- > start) { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 953 | xenvif_idx_release(vif, |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 954 | frag_get_pending_idx(&frags[shinfo->nr_frags]), |
| 955 | XEN_NETIF_RSP_ERROR); |
Ian Campbell | 4cc7c1c | 2013-02-06 23:41:37 +0000 | [diff] [blame] | 956 | } |
| 957 | /* The head too, if necessary. */ |
| 958 | if (start) |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 959 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
Ian Campbell | 4cc7c1c | 2013-02-06 23:41:37 +0000 | [diff] [blame] | 960 | |
| 961 | return NULL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 964 | static int xenvif_tx_check_gop(struct xenvif *vif, |
| 965 | struct sk_buff *skb, |
| 966 | struct gnttab_copy **gopp) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 967 | { |
| 968 | struct gnttab_copy *gop = *gopp; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 969 | u16 pending_idx = *((u16 *)skb->data); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 970 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 971 | struct pending_tx_info *tx_info; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 972 | int nr_frags = shinfo->nr_frags; |
| 973 | int i, err, start; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 974 | u16 peek; /* peek into next tx request */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 975 | |
| 976 | /* Check status of header. */ |
| 977 | err = gop->status; |
Matthew Daley | 7d5145d | 2013-02-06 23:41:36 +0000 | [diff] [blame] | 978 | if (unlikely(err)) |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 979 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 980 | |
| 981 | /* 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] | 982 | start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 983 | |
| 984 | for (i = start; i < nr_frags; i++) { |
| 985 | int j, newerr; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 986 | pending_ring_idx_t head; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 987 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 988 | pending_idx = frag_get_pending_idx(&shinfo->frags[i]); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 989 | tx_info = &vif->pending_tx_info[pending_idx]; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 990 | head = tx_info->head; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 991 | |
| 992 | /* Check error status: if okay then remember grant handle. */ |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 993 | do { |
| 994 | newerr = (++gop)->status; |
| 995 | if (newerr) |
| 996 | break; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 997 | peek = vif->pending_ring[pending_index(++head)]; |
| 998 | } while (!pending_tx_is_head(vif, peek)); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 999 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1000 | if (likely(!newerr)) { |
| 1001 | /* Had a previous error? Invalidate this fragment. */ |
| 1002 | if (unlikely(err)) |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1003 | xenvif_idx_release(vif, pending_idx, |
| 1004 | XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1005 | continue; |
| 1006 | } |
| 1007 | |
| 1008 | /* Error on this fragment: respond to client with an error. */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1009 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1010 | |
| 1011 | /* Not the first error? Preceding frags already invalidated. */ |
| 1012 | if (err) |
| 1013 | continue; |
| 1014 | |
| 1015 | /* First error: invalidate header and preceding fragments. */ |
| 1016 | pending_idx = *((u16 *)skb->data); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1017 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1018 | for (j = start; j < i; j++) { |
Jan Beulich | 5ccb3ea | 2011-11-18 05:42:05 +0000 | [diff] [blame] | 1019 | pending_idx = frag_get_pending_idx(&shinfo->frags[j]); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1020 | xenvif_idx_release(vif, pending_idx, |
| 1021 | XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* Remember the error: invalidate all subsequent fragments. */ |
| 1025 | err = newerr; |
| 1026 | } |
| 1027 | |
| 1028 | *gopp = gop + 1; |
| 1029 | return err; |
| 1030 | } |
| 1031 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1032 | static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1033 | { |
| 1034 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 1035 | int nr_frags = shinfo->nr_frags; |
| 1036 | int i; |
| 1037 | |
| 1038 | for (i = 0; i < nr_frags; i++) { |
| 1039 | skb_frag_t *frag = shinfo->frags + i; |
| 1040 | struct xen_netif_tx_request *txp; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1041 | struct page *page; |
| 1042 | u16 pending_idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1043 | |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1044 | pending_idx = frag_get_pending_idx(frag); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1045 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1046 | txp = &vif->pending_tx_info[pending_idx].req; |
| 1047 | page = virt_to_page(idx_to_kaddr(vif, pending_idx)); |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1048 | __skb_fill_page_desc(skb, i, page, txp->offset, txp->size); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1049 | skb->len += txp->size; |
| 1050 | skb->data_len += txp->size; |
| 1051 | skb->truesize += txp->size; |
| 1052 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1053 | /* Take an extra reference to offset xenvif_idx_release */ |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1054 | get_page(vif->mmap_pages[pending_idx]); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1055 | xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1059 | static int xenvif_get_extras(struct xenvif *vif, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1060 | struct xen_netif_extra_info *extras, |
| 1061 | int work_to_do) |
| 1062 | { |
| 1063 | struct xen_netif_extra_info extra; |
| 1064 | RING_IDX cons = vif->tx.req_cons; |
| 1065 | |
| 1066 | do { |
| 1067 | if (unlikely(work_to_do-- <= 0)) { |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1068 | netdev_err(vif->dev, "Missing extra info\n"); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1069 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1070 | return -EBADR; |
| 1071 | } |
| 1072 | |
| 1073 | memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons), |
| 1074 | sizeof(extra)); |
| 1075 | if (unlikely(!extra.type || |
| 1076 | extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) { |
| 1077 | vif->tx.req_cons = ++cons; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1078 | netdev_err(vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1079 | "Invalid extra type: %d\n", extra.type); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1080 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | |
| 1084 | memcpy(&extras[extra.type - 1], &extra, sizeof(extra)); |
| 1085 | vif->tx.req_cons = ++cons; |
| 1086 | } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); |
| 1087 | |
| 1088 | return work_to_do; |
| 1089 | } |
| 1090 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1091 | static int xenvif_set_skb_gso(struct xenvif *vif, |
| 1092 | struct sk_buff *skb, |
| 1093 | struct xen_netif_extra_info *gso) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1094 | { |
| 1095 | if (!gso->u.gso.size) { |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1096 | netdev_err(vif->dev, "GSO size must not be zero.\n"); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1097 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | |
| 1101 | /* Currently only TCPv4 S.O. is supported. */ |
| 1102 | if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) { |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1103 | 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] | 1104 | xenvif_fatal_tx_err(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | |
| 1108 | skb_shinfo(skb)->gso_size = gso->u.gso.size; |
| 1109 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 1110 | |
| 1111 | /* Header must be checked, and gso_segs computed. */ |
| 1112 | skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; |
| 1113 | skb_shinfo(skb)->gso_segs = 0; |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 1118 | static inline void maybe_pull_tail(struct sk_buff *skb, unsigned int len) |
| 1119 | { |
| 1120 | if (skb_is_nonlinear(skb) && skb_headlen(skb) < len) { |
| 1121 | /* If we need to pullup then pullup to the max, so we |
| 1122 | * won't need to do it again. |
| 1123 | */ |
| 1124 | int target = min_t(int, skb->len, MAX_TCP_HEADER); |
| 1125 | __pskb_pull_tail(skb, target - skb_headlen(skb)); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb, |
| 1130 | int recalculate_partial_csum) |
| 1131 | { |
| 1132 | struct iphdr *iph = (void *)skb->data; |
| 1133 | unsigned int header_size; |
| 1134 | unsigned int off; |
| 1135 | int err = -EPROTO; |
| 1136 | |
| 1137 | off = sizeof(struct iphdr); |
| 1138 | |
| 1139 | header_size = skb->network_header + off + MAX_IPOPTLEN; |
| 1140 | maybe_pull_tail(skb, header_size); |
| 1141 | |
| 1142 | off = iph->ihl * 4; |
| 1143 | |
| 1144 | switch (iph->protocol) { |
| 1145 | case IPPROTO_TCP: |
| 1146 | if (!skb_partial_csum_set(skb, off, |
| 1147 | offsetof(struct tcphdr, check))) |
| 1148 | goto out; |
| 1149 | |
| 1150 | if (recalculate_partial_csum) { |
| 1151 | struct tcphdr *tcph = tcp_hdr(skb); |
| 1152 | |
| 1153 | header_size = skb->network_header + |
| 1154 | off + |
| 1155 | sizeof(struct tcphdr); |
| 1156 | maybe_pull_tail(skb, header_size); |
| 1157 | |
| 1158 | tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 1159 | skb->len - off, |
| 1160 | IPPROTO_TCP, 0); |
| 1161 | } |
| 1162 | break; |
| 1163 | case IPPROTO_UDP: |
| 1164 | if (!skb_partial_csum_set(skb, off, |
| 1165 | offsetof(struct udphdr, check))) |
| 1166 | goto out; |
| 1167 | |
| 1168 | if (recalculate_partial_csum) { |
| 1169 | struct udphdr *udph = udp_hdr(skb); |
| 1170 | |
| 1171 | header_size = skb->network_header + |
| 1172 | off + |
| 1173 | sizeof(struct udphdr); |
| 1174 | maybe_pull_tail(skb, header_size); |
| 1175 | |
| 1176 | udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 1177 | skb->len - off, |
| 1178 | IPPROTO_UDP, 0); |
| 1179 | } |
| 1180 | break; |
| 1181 | default: |
| 1182 | if (net_ratelimit()) |
| 1183 | netdev_err(vif->dev, |
| 1184 | "Attempting to checksum a non-TCP/UDP packet, " |
| 1185 | "dropping a protocol %d packet\n", |
| 1186 | iph->protocol); |
| 1187 | goto out; |
| 1188 | } |
| 1189 | |
| 1190 | err = 0; |
| 1191 | |
| 1192 | out: |
| 1193 | return err; |
| 1194 | } |
| 1195 | |
| 1196 | static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb, |
| 1197 | int recalculate_partial_csum) |
| 1198 | { |
| 1199 | int err = -EPROTO; |
| 1200 | struct ipv6hdr *ipv6h = (void *)skb->data; |
| 1201 | u8 nexthdr; |
| 1202 | unsigned int header_size; |
| 1203 | unsigned int off; |
| 1204 | bool fragment; |
| 1205 | bool done; |
| 1206 | |
| 1207 | done = false; |
| 1208 | |
| 1209 | off = sizeof(struct ipv6hdr); |
| 1210 | |
| 1211 | header_size = skb->network_header + off; |
| 1212 | maybe_pull_tail(skb, header_size); |
| 1213 | |
| 1214 | nexthdr = ipv6h->nexthdr; |
| 1215 | |
| 1216 | while ((off <= sizeof(struct ipv6hdr) + ntohs(ipv6h->payload_len)) && |
| 1217 | !done) { |
| 1218 | switch (nexthdr) { |
| 1219 | case IPPROTO_DSTOPTS: |
| 1220 | case IPPROTO_HOPOPTS: |
| 1221 | case IPPROTO_ROUTING: { |
| 1222 | struct ipv6_opt_hdr *hp = (void *)(skb->data + off); |
| 1223 | |
| 1224 | header_size = skb->network_header + |
| 1225 | off + |
| 1226 | sizeof(struct ipv6_opt_hdr); |
| 1227 | maybe_pull_tail(skb, header_size); |
| 1228 | |
| 1229 | nexthdr = hp->nexthdr; |
| 1230 | off += ipv6_optlen(hp); |
| 1231 | break; |
| 1232 | } |
| 1233 | case IPPROTO_AH: { |
| 1234 | struct ip_auth_hdr *hp = (void *)(skb->data + off); |
| 1235 | |
| 1236 | header_size = skb->network_header + |
| 1237 | off + |
| 1238 | sizeof(struct ip_auth_hdr); |
| 1239 | maybe_pull_tail(skb, header_size); |
| 1240 | |
| 1241 | nexthdr = hp->nexthdr; |
| 1242 | off += (hp->hdrlen+2)<<2; |
| 1243 | break; |
| 1244 | } |
| 1245 | case IPPROTO_FRAGMENT: |
| 1246 | fragment = true; |
| 1247 | /* fall through */ |
| 1248 | default: |
| 1249 | done = true; |
| 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | if (!done) { |
| 1255 | if (net_ratelimit()) |
| 1256 | netdev_err(vif->dev, "Failed to parse packet header\n"); |
| 1257 | goto out; |
| 1258 | } |
| 1259 | |
| 1260 | if (fragment) { |
| 1261 | if (net_ratelimit()) |
| 1262 | netdev_err(vif->dev, "Packet is a fragment!\n"); |
| 1263 | goto out; |
| 1264 | } |
| 1265 | |
| 1266 | switch (nexthdr) { |
| 1267 | case IPPROTO_TCP: |
| 1268 | if (!skb_partial_csum_set(skb, off, |
| 1269 | offsetof(struct tcphdr, check))) |
| 1270 | goto out; |
| 1271 | |
| 1272 | if (recalculate_partial_csum) { |
| 1273 | struct tcphdr *tcph = tcp_hdr(skb); |
| 1274 | |
| 1275 | header_size = skb->network_header + |
| 1276 | off + |
| 1277 | sizeof(struct tcphdr); |
| 1278 | maybe_pull_tail(skb, header_size); |
| 1279 | |
| 1280 | tcph->check = ~csum_ipv6_magic(&ipv6h->saddr, |
| 1281 | &ipv6h->daddr, |
| 1282 | skb->len - off, |
| 1283 | IPPROTO_TCP, 0); |
| 1284 | } |
| 1285 | break; |
| 1286 | case IPPROTO_UDP: |
| 1287 | if (!skb_partial_csum_set(skb, off, |
| 1288 | offsetof(struct udphdr, check))) |
| 1289 | goto out; |
| 1290 | |
| 1291 | if (recalculate_partial_csum) { |
| 1292 | struct udphdr *udph = udp_hdr(skb); |
| 1293 | |
| 1294 | header_size = skb->network_header + |
| 1295 | off + |
| 1296 | sizeof(struct udphdr); |
| 1297 | maybe_pull_tail(skb, header_size); |
| 1298 | |
| 1299 | udph->check = ~csum_ipv6_magic(&ipv6h->saddr, |
| 1300 | &ipv6h->daddr, |
| 1301 | skb->len - off, |
| 1302 | IPPROTO_UDP, 0); |
| 1303 | } |
| 1304 | break; |
| 1305 | default: |
| 1306 | if (net_ratelimit()) |
| 1307 | netdev_err(vif->dev, |
| 1308 | "Attempting to checksum a non-TCP/UDP packet, " |
| 1309 | "dropping a protocol %d packet\n", |
| 1310 | nexthdr); |
| 1311 | goto out; |
| 1312 | } |
| 1313 | |
| 1314 | err = 0; |
| 1315 | |
| 1316 | out: |
| 1317 | return err; |
| 1318 | } |
| 1319 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1320 | static int checksum_setup(struct xenvif *vif, struct sk_buff *skb) |
| 1321 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1322 | int err = -EPROTO; |
| 1323 | int recalculate_partial_csum = 0; |
| 1324 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 1325 | /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1326 | * peers can fail to set NETRXF_csum_blank when sending a GSO |
| 1327 | * frame. In this case force the SKB to CHECKSUM_PARTIAL and |
| 1328 | * recalculate the partial checksum. |
| 1329 | */ |
| 1330 | if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) { |
| 1331 | vif->rx_gso_checksum_fixup++; |
| 1332 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 1333 | recalculate_partial_csum = 1; |
| 1334 | } |
| 1335 | |
| 1336 | /* A non-CHECKSUM_PARTIAL SKB does not require setup. */ |
| 1337 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 1338 | return 0; |
| 1339 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 1340 | if (skb->protocol == htons(ETH_P_IP)) |
| 1341 | err = checksum_setup_ip(vif, skb, recalculate_partial_csum); |
| 1342 | else if (skb->protocol == htons(ETH_P_IPV6)) |
| 1343 | err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1344 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1345 | return err; |
| 1346 | } |
| 1347 | |
| 1348 | static bool tx_credit_exceeded(struct xenvif *vif, unsigned size) |
| 1349 | { |
| 1350 | unsigned long now = jiffies; |
| 1351 | unsigned long next_credit = |
| 1352 | vif->credit_timeout.expires + |
| 1353 | msecs_to_jiffies(vif->credit_usec / 1000); |
| 1354 | |
| 1355 | /* Timer could already be pending in rare cases. */ |
| 1356 | if (timer_pending(&vif->credit_timeout)) |
| 1357 | return true; |
| 1358 | |
| 1359 | /* Passed the point where we can replenish credit? */ |
| 1360 | if (time_after_eq(now, next_credit)) { |
| 1361 | vif->credit_timeout.expires = now; |
| 1362 | tx_add_credit(vif); |
| 1363 | } |
| 1364 | |
| 1365 | /* Still too big to send right now? Set a callback. */ |
| 1366 | if (size > vif->remaining_credit) { |
| 1367 | vif->credit_timeout.data = |
| 1368 | (unsigned long)vif; |
| 1369 | vif->credit_timeout.function = |
| 1370 | tx_credit_callback; |
| 1371 | mod_timer(&vif->credit_timeout, |
| 1372 | next_credit); |
| 1373 | |
| 1374 | return true; |
| 1375 | } |
| 1376 | |
| 1377 | return false; |
| 1378 | } |
| 1379 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1380 | static unsigned xenvif_tx_build_gops(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1381 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1382 | struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1383 | struct sk_buff *skb; |
| 1384 | int ret; |
| 1385 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1386 | while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX |
| 1387 | < MAX_PENDING_REQS)) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1388 | struct xen_netif_tx_request txreq; |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1389 | struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1390 | struct page *page; |
| 1391 | struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1]; |
| 1392 | u16 pending_idx; |
| 1393 | RING_IDX idx; |
| 1394 | int work_to_do; |
| 1395 | unsigned int data_len; |
| 1396 | pending_ring_idx_t index; |
| 1397 | |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1398 | if (vif->tx.sring->req_prod - vif->tx.req_cons > |
| 1399 | XEN_NETIF_TX_RING_SIZE) { |
| 1400 | netdev_err(vif->dev, |
| 1401 | "Impossible number of requests. " |
| 1402 | "req_prod %d, req_cons %d, size %ld\n", |
| 1403 | vif->tx.sring->req_prod, vif->tx.req_cons, |
| 1404 | XEN_NETIF_TX_RING_SIZE); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1405 | xenvif_fatal_tx_err(vif); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1406 | continue; |
| 1407 | } |
| 1408 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1409 | RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1410 | if (!work_to_do) |
| 1411 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1412 | |
| 1413 | idx = vif->tx.req_cons; |
| 1414 | rmb(); /* Ensure that we see the request before we copy it. */ |
| 1415 | memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq)); |
| 1416 | |
| 1417 | /* Credit-based scheduling. */ |
| 1418 | if (txreq.size > vif->remaining_credit && |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1419 | tx_credit_exceeded(vif, txreq.size)) |
| 1420 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1421 | |
| 1422 | vif->remaining_credit -= txreq.size; |
| 1423 | |
| 1424 | work_to_do--; |
| 1425 | vif->tx.req_cons = ++idx; |
| 1426 | |
| 1427 | memset(extras, 0, sizeof(extras)); |
| 1428 | if (txreq.flags & XEN_NETTXF_extra_info) { |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1429 | work_to_do = xenvif_get_extras(vif, extras, |
| 1430 | work_to_do); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1431 | idx = vif->tx.req_cons; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1432 | if (unlikely(work_to_do < 0)) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1433 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1436 | ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do); |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1437 | if (unlikely(ret < 0)) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1438 | break; |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1439 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1440 | idx += ret; |
| 1441 | |
| 1442 | if (unlikely(txreq.size < ETH_HLEN)) { |
| 1443 | netdev_dbg(vif->dev, |
| 1444 | "Bad packet size: %d\n", txreq.size); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1445 | xenvif_tx_err(vif, &txreq, idx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1446 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* No crossing a page as the payload mustn't fragment. */ |
| 1450 | if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) { |
Ian Campbell | 48856286 | 2013-02-06 23:41:35 +0000 | [diff] [blame] | 1451 | netdev_err(vif->dev, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1452 | "txreq.offset: %x, size: %u, end: %lu\n", |
| 1453 | txreq.offset, txreq.size, |
| 1454 | (txreq.offset&~PAGE_MASK) + txreq.size); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1455 | xenvif_fatal_tx_err(vif); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1456 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1459 | index = pending_index(vif->pending_cons); |
| 1460 | pending_idx = vif->pending_ring[index]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1461 | |
| 1462 | data_len = (txreq.size > PKT_PROT_LEN && |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1463 | ret < XEN_NETBK_LEGACY_SLOTS_MAX) ? |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1464 | PKT_PROT_LEN : txreq.size; |
| 1465 | |
| 1466 | skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN, |
| 1467 | GFP_ATOMIC | __GFP_NOWARN); |
| 1468 | if (unlikely(skb == NULL)) { |
| 1469 | netdev_dbg(vif->dev, |
| 1470 | "Can't allocate a skb in start_xmit.\n"); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1471 | xenvif_tx_err(vif, &txreq, idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | /* Packets passed to netif_rx() must have some headroom. */ |
| 1476 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); |
| 1477 | |
| 1478 | if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) { |
| 1479 | struct xen_netif_extra_info *gso; |
| 1480 | gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1]; |
| 1481 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1482 | if (xenvif_set_skb_gso(vif, skb, gso)) { |
| 1483 | /* Failure in xenvif_set_skb_gso is fatal. */ |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1484 | kfree_skb(skb); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1485 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* XXX could copy straight to head */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1490 | page = xenvif_alloc_page(vif, pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1491 | if (!page) { |
| 1492 | kfree_skb(skb); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1493 | xenvif_tx_err(vif, &txreq, idx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1494 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1497 | gop->source.u.ref = txreq.gref; |
| 1498 | gop->source.domid = vif->domid; |
| 1499 | gop->source.offset = txreq.offset; |
| 1500 | |
| 1501 | gop->dest.u.gmfn = virt_to_mfn(page_address(page)); |
| 1502 | gop->dest.domid = DOMID_SELF; |
| 1503 | gop->dest.offset = txreq.offset; |
| 1504 | |
| 1505 | gop->len = txreq.size; |
| 1506 | gop->flags = GNTCOPY_source_gref; |
| 1507 | |
| 1508 | gop++; |
| 1509 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1510 | memcpy(&vif->pending_tx_info[pending_idx].req, |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1511 | &txreq, sizeof(txreq)); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1512 | vif->pending_tx_info[pending_idx].head = index; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1513 | *((u16 *)skb->data) = pending_idx; |
| 1514 | |
| 1515 | __skb_put(skb, data_len); |
| 1516 | |
| 1517 | skb_shinfo(skb)->nr_frags = ret; |
| 1518 | if (data_len < txreq.size) { |
| 1519 | skb_shinfo(skb)->nr_frags++; |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1520 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
| 1521 | pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1522 | } else { |
Ian Campbell | ea066ad | 2011-10-05 00:28:46 +0000 | [diff] [blame] | 1523 | frag_set_pending_idx(&skb_shinfo(skb)->frags[0], |
| 1524 | INVALID_PENDING_IDX); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1527 | vif->pending_cons++; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1528 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1529 | request_gop = xenvif_get_requests(vif, skb, txfrags, gop); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1530 | if (request_gop == NULL) { |
| 1531 | kfree_skb(skb); |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1532 | xenvif_tx_err(vif, &txreq, idx); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1533 | break; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1534 | } |
| 1535 | gop = request_gop; |
| 1536 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1537 | __skb_queue_tail(&vif->tx_queue, skb); |
Annie Li | 1e0b6ea | 2012-06-27 00:46:58 +0000 | [diff] [blame] | 1538 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1539 | vif->tx.req_cons = idx; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1540 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1541 | if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops)) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | } |
| 1544 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1545 | return gop - vif->tx_copy_ops; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1548 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1549 | static int xenvif_tx_submit(struct xenvif *vif, int budget) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1550 | { |
| 1551 | struct gnttab_copy *gop = vif->tx_copy_ops; |
| 1552 | struct sk_buff *skb; |
| 1553 | int work_done = 0; |
| 1554 | |
| 1555 | while (work_done < budget && |
| 1556 | (skb = __skb_dequeue(&vif->tx_queue)) != NULL) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1557 | struct xen_netif_tx_request *txp; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1558 | u16 pending_idx; |
| 1559 | unsigned data_len; |
| 1560 | |
| 1561 | pending_idx = *((u16 *)skb->data); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1562 | txp = &vif->pending_tx_info[pending_idx].req; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1563 | |
| 1564 | /* Check the remap error code. */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1565 | if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1566 | netdev_dbg(vif->dev, "netback grant failed.\n"); |
| 1567 | skb_shinfo(skb)->nr_frags = 0; |
| 1568 | kfree_skb(skb); |
| 1569 | continue; |
| 1570 | } |
| 1571 | |
| 1572 | data_len = skb->len; |
| 1573 | memcpy(skb->data, |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1574 | (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset), |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1575 | data_len); |
| 1576 | if (data_len < txp->size) { |
| 1577 | /* Append the packet payload as a fragment. */ |
| 1578 | txp->offset += data_len; |
| 1579 | txp->size -= data_len; |
| 1580 | } else { |
| 1581 | /* Schedule a response immediately. */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1582 | xenvif_idx_release(vif, pending_idx, |
| 1583 | XEN_NETIF_RSP_OKAY); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | if (txp->flags & XEN_NETTXF_csum_blank) |
| 1587 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 1588 | else if (txp->flags & XEN_NETTXF_data_validated) |
| 1589 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1590 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1591 | xenvif_fill_frags(vif, skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1592 | |
Paul Durrant | 2eba61d | 2013-10-16 17:50:29 +0100 | [diff] [blame^] | 1593 | if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1594 | int target = min_t(int, skb->len, PKT_PROT_LEN); |
| 1595 | __pskb_pull_tail(skb, target - skb_headlen(skb)); |
| 1596 | } |
| 1597 | |
| 1598 | skb->dev = vif->dev; |
| 1599 | skb->protocol = eth_type_trans(skb, skb->dev); |
Jason Wang | f9ca8f7 | 2013-03-25 20:19:58 +0000 | [diff] [blame] | 1600 | skb_reset_network_header(skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1601 | |
| 1602 | if (checksum_setup(vif, skb)) { |
| 1603 | netdev_dbg(vif->dev, |
| 1604 | "Can't setup checksum in net_tx_action\n"); |
| 1605 | kfree_skb(skb); |
| 1606 | continue; |
| 1607 | } |
| 1608 | |
Jason Wang | 40893fd | 2013-03-26 23:11:22 +0000 | [diff] [blame] | 1609 | skb_probe_transport_header(skb, 0); |
Jason Wang | f9ca8f7 | 2013-03-25 20:19:58 +0000 | [diff] [blame] | 1610 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1611 | vif->dev->stats.rx_bytes += skb->len; |
| 1612 | vif->dev->stats.rx_packets++; |
| 1613 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1614 | work_done++; |
| 1615 | |
| 1616 | netif_receive_skb(skb); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1617 | } |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1618 | |
| 1619 | return work_done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* Called after netfront has transmitted */ |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1623 | int xenvif_tx_action(struct xenvif *vif, int budget) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1624 | { |
| 1625 | unsigned nr_gops; |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1626 | int work_done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1627 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1628 | if (unlikely(!tx_work_todo(vif))) |
| 1629 | return 0; |
| 1630 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1631 | nr_gops = xenvif_tx_build_gops(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (nr_gops == 0) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1634 | return 0; |
Andres Lagar-Cavilla | c571898 | 2012-09-14 14:26:59 +0000 | [diff] [blame] | 1635 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1636 | gnttab_batch_copy(vif->tx_copy_ops, nr_gops); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1637 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1638 | work_done = xenvif_tx_submit(vif, nr_gops); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1639 | |
| 1640 | return work_done; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1643 | static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx, |
| 1644 | u8 status) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1645 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1646 | struct pending_tx_info *pending_tx_info; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1647 | pending_ring_idx_t head; |
| 1648 | u16 peek; /* peek into next tx request */ |
| 1649 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1650 | BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL)); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1651 | |
| 1652 | /* Already complete? */ |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1653 | if (vif->mmap_pages[pending_idx] == NULL) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1654 | return; |
| 1655 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1656 | pending_tx_info = &vif->pending_tx_info[pending_idx]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1657 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1658 | head = pending_tx_info->head; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1659 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1660 | BUG_ON(!pending_tx_is_head(vif, head)); |
| 1661 | BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1662 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1663 | do { |
| 1664 | pending_ring_idx_t index; |
| 1665 | pending_ring_idx_t idx = pending_index(head); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1666 | u16 info_idx = vif->pending_ring[idx]; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1667 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1668 | pending_tx_info = &vif->pending_tx_info[info_idx]; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1669 | make_tx_response(vif, &pending_tx_info->req, status); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1670 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1671 | /* Setting any number other than |
| 1672 | * INVALID_PENDING_RING_IDX indicates this slot is |
| 1673 | * starting a new packet / ending a previous packet. |
| 1674 | */ |
| 1675 | pending_tx_info->head = 0; |
| 1676 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1677 | index = pending_index(vif->pending_prod++); |
| 1678 | vif->pending_ring[index] = vif->pending_ring[info_idx]; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1679 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1680 | peek = vif->pending_ring[pending_index(++head)]; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1681 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1682 | } while (!pending_tx_is_head(vif, peek)); |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1683 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1684 | put_page(vif->mmap_pages[pending_idx]); |
| 1685 | vif->mmap_pages[pending_idx] = NULL; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1688 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1689 | static void make_tx_response(struct xenvif *vif, |
| 1690 | struct xen_netif_tx_request *txp, |
| 1691 | s8 st) |
| 1692 | { |
| 1693 | RING_IDX i = vif->tx.rsp_prod_pvt; |
| 1694 | struct xen_netif_tx_response *resp; |
| 1695 | int notify; |
| 1696 | |
| 1697 | resp = RING_GET_RESPONSE(&vif->tx, i); |
| 1698 | resp->id = txp->id; |
| 1699 | resp->status = st; |
| 1700 | |
| 1701 | if (txp->flags & XEN_NETTXF_extra_info) |
| 1702 | RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL; |
| 1703 | |
| 1704 | vif->tx.rsp_prod_pvt = ++i; |
| 1705 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify); |
| 1706 | if (notify) |
Wei Liu | e1f00a69 | 2013-05-22 06:34:45 +0000 | [diff] [blame] | 1707 | notify_remote_via_irq(vif->tx_irq); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif, |
| 1711 | u16 id, |
| 1712 | s8 st, |
| 1713 | u16 offset, |
| 1714 | u16 size, |
| 1715 | u16 flags) |
| 1716 | { |
| 1717 | RING_IDX i = vif->rx.rsp_prod_pvt; |
| 1718 | struct xen_netif_rx_response *resp; |
| 1719 | |
| 1720 | resp = RING_GET_RESPONSE(&vif->rx, i); |
| 1721 | resp->offset = offset; |
| 1722 | resp->flags = flags; |
| 1723 | resp->id = id; |
| 1724 | resp->status = (s16)size; |
| 1725 | if (st < 0) |
| 1726 | resp->status = (s16)st; |
| 1727 | |
| 1728 | vif->rx.rsp_prod_pvt = ++i; |
| 1729 | |
| 1730 | return resp; |
| 1731 | } |
| 1732 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1733 | static inline int rx_work_todo(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1734 | { |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1735 | return !skb_queue_empty(&vif->rx_queue); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1738 | static inline int tx_work_todo(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1739 | { |
| 1740 | |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1741 | if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) && |
| 1742 | (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX |
| 1743 | < MAX_PENDING_REQS)) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1744 | return 1; |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1749 | void xenvif_unmap_frontend_rings(struct xenvif *vif) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1750 | { |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1751 | if (vif->tx.sring) |
| 1752 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif), |
| 1753 | vif->tx.sring); |
| 1754 | if (vif->rx.sring) |
| 1755 | xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif), |
| 1756 | vif->rx.sring); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1759 | int xenvif_map_frontend_rings(struct xenvif *vif, |
| 1760 | grant_ref_t tx_ring_ref, |
| 1761 | grant_ref_t rx_ring_ref) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1762 | { |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1763 | void *addr; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1764 | struct xen_netif_tx_sring *txs; |
| 1765 | struct xen_netif_rx_sring *rxs; |
| 1766 | |
| 1767 | int err = -ENOMEM; |
| 1768 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1769 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif), |
| 1770 | tx_ring_ref, &addr); |
| 1771 | if (err) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1772 | goto err; |
| 1773 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1774 | txs = (struct xen_netif_tx_sring *)addr; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1775 | BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE); |
| 1776 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1777 | err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif), |
| 1778 | rx_ring_ref, &addr); |
| 1779 | if (err) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1780 | goto err; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1781 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1782 | rxs = (struct xen_netif_rx_sring *)addr; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1783 | BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE); |
| 1784 | |
David Vrabel | c9d6369 | 2011-09-29 16:53:31 +0100 | [diff] [blame] | 1785 | vif->rx_req_cons_peek = 0; |
| 1786 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1787 | return 0; |
| 1788 | |
| 1789 | err: |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1790 | xenvif_unmap_frontend_rings(vif); |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1791 | return err; |
| 1792 | } |
| 1793 | |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1794 | int xenvif_kthread(void *data) |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1795 | { |
| 1796 | struct xenvif *vif = data; |
| 1797 | |
| 1798 | while (!kthread_should_stop()) { |
| 1799 | wait_event_interruptible(vif->wq, |
| 1800 | rx_work_todo(vif) || |
| 1801 | kthread_should_stop()); |
| 1802 | if (kthread_should_stop()) |
| 1803 | break; |
| 1804 | |
| 1805 | if (rx_work_todo(vif)) |
Wei Liu | 7376419 | 2013-08-26 12:59:39 +0100 | [diff] [blame] | 1806 | xenvif_rx_action(vif); |
Wei Liu | b3f980b | 2013-08-26 12:59:38 +0100 | [diff] [blame] | 1807 | |
| 1808 | cond_resched(); |
| 1809 | } |
| 1810 | |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1814 | static int __init netback_init(void) |
| 1815 | { |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1816 | int rc = 0; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1817 | |
Daniel De Graaf | 2a14b244 | 2011-12-14 15:12:13 -0500 | [diff] [blame] | 1818 | if (!xen_domain()) |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1819 | return -ENODEV; |
| 1820 | |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1821 | if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) { |
Joe Perches | 383eda3 | 2013-06-27 21:57:49 -0700 | [diff] [blame] | 1822 | pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n", |
| 1823 | fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX); |
Wei Liu | 3764149 | 2013-05-02 00:43:59 +0000 | [diff] [blame] | 1824 | fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX; |
Wei Liu | 2810e5b | 2013-04-22 02:20:42 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1827 | rc = xenvif_xenbus_init(); |
| 1828 | if (rc) |
| 1829 | goto failed_init; |
| 1830 | |
| 1831 | return 0; |
| 1832 | |
| 1833 | failed_init: |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1834 | return rc; |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | module_init(netback_init); |
| 1838 | |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 1839 | static void __exit netback_fini(void) |
| 1840 | { |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 1841 | xenvif_xenbus_fini(); |
Wei Liu | b103f35 | 2013-05-16 23:26:11 +0000 | [diff] [blame] | 1842 | } |
| 1843 | module_exit(netback_fini); |
| 1844 | |
Ian Campbell | f942dc2 | 2011-03-15 00:06:18 +0000 | [diff] [blame] | 1845 | MODULE_LICENSE("Dual BSD/GPL"); |
Bastian Blank | f984cec | 2011-06-30 11:19:09 -0700 | [diff] [blame] | 1846 | MODULE_ALIAS("xen-backend:vif"); |