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