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