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