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