Paul Durrant | 3254f83 | 2016-10-04 10:29:12 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016 Citrix Systems Inc. |
| 3 | * Copyright (c) 2002-2005, K A Fraser |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License version 2 |
| 7 | * as published by the Free Software Foundation; or, when distributed |
| 8 | * separately from the Linux kernel or incorporated into other |
| 9 | * software packages, subject to the following license: |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this source file (the "Software"), to deal in the Software without |
| 13 | * restriction, including without limitation the rights to use, copy, modify, |
| 14 | * merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 15 | * and to permit persons to whom the Software is furnished to do so, subject to |
| 16 | * the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 27 | * IN THE SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include "common.h" |
| 31 | |
| 32 | #include <linux/kthread.h> |
| 33 | |
| 34 | #include <xen/xen.h> |
| 35 | #include <xen/events.h> |
| 36 | |
| 37 | static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue) |
| 38 | { |
| 39 | RING_IDX prod, cons; |
| 40 | struct sk_buff *skb; |
| 41 | int needed; |
| 42 | |
| 43 | skb = skb_peek(&queue->rx_queue); |
| 44 | if (!skb) |
| 45 | return false; |
| 46 | |
| 47 | needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE); |
| 48 | if (skb_is_gso(skb)) |
| 49 | needed++; |
| 50 | if (skb->sw_hash) |
| 51 | needed++; |
| 52 | |
| 53 | do { |
| 54 | prod = queue->rx.sring->req_prod; |
| 55 | cons = queue->rx.req_cons; |
| 56 | |
| 57 | if (prod - cons >= needed) |
| 58 | return true; |
| 59 | |
| 60 | queue->rx.sring->req_event = prod + 1; |
| 61 | |
| 62 | /* Make sure event is visible before we check prod |
| 63 | * again. |
| 64 | */ |
| 65 | mb(); |
| 66 | } while (queue->rx.sring->req_prod != prod); |
| 67 | |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb) |
| 72 | { |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&queue->rx_queue.lock, flags); |
| 76 | |
| 77 | __skb_queue_tail(&queue->rx_queue, skb); |
| 78 | |
| 79 | queue->rx_queue_len += skb->len; |
| 80 | if (queue->rx_queue_len > queue->rx_queue_max) { |
| 81 | struct net_device *dev = queue->vif->dev; |
| 82 | |
| 83 | netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id)); |
| 84 | } |
| 85 | |
| 86 | spin_unlock_irqrestore(&queue->rx_queue.lock, flags); |
| 87 | } |
| 88 | |
| 89 | static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue) |
| 90 | { |
| 91 | struct sk_buff *skb; |
| 92 | |
| 93 | spin_lock_irq(&queue->rx_queue.lock); |
| 94 | |
| 95 | skb = __skb_dequeue(&queue->rx_queue); |
| 96 | if (skb) |
| 97 | queue->rx_queue_len -= skb->len; |
| 98 | |
| 99 | spin_unlock_irq(&queue->rx_queue.lock); |
| 100 | |
| 101 | return skb; |
| 102 | } |
| 103 | |
| 104 | static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue) |
| 105 | { |
| 106 | spin_lock_irq(&queue->rx_queue.lock); |
| 107 | |
| 108 | if (queue->rx_queue_len < queue->rx_queue_max) { |
| 109 | struct net_device *dev = queue->vif->dev; |
| 110 | |
| 111 | netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id)); |
| 112 | } |
| 113 | |
| 114 | spin_unlock_irq(&queue->rx_queue.lock); |
| 115 | } |
| 116 | |
| 117 | static void xenvif_rx_queue_purge(struct xenvif_queue *queue) |
| 118 | { |
| 119 | struct sk_buff *skb; |
| 120 | |
| 121 | while ((skb = xenvif_rx_dequeue(queue)) != NULL) |
| 122 | kfree_skb(skb); |
| 123 | } |
| 124 | |
| 125 | static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue) |
| 126 | { |
| 127 | struct sk_buff *skb; |
| 128 | |
| 129 | for (;;) { |
| 130 | skb = skb_peek(&queue->rx_queue); |
| 131 | if (!skb) |
| 132 | break; |
| 133 | if (time_before(jiffies, XENVIF_RX_CB(skb)->expires)) |
| 134 | break; |
| 135 | xenvif_rx_dequeue(queue); |
| 136 | kfree_skb(skb); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | struct netrx_pending_operations { |
| 141 | unsigned int copy_prod, copy_cons; |
| 142 | unsigned int meta_prod, meta_cons; |
| 143 | struct gnttab_copy *copy; |
| 144 | struct xenvif_rx_meta *meta; |
| 145 | int copy_off; |
| 146 | grant_ref_t copy_gref; |
| 147 | }; |
| 148 | |
| 149 | static struct xenvif_rx_meta *get_next_rx_buffer( |
| 150 | struct xenvif_queue *queue, |
| 151 | struct netrx_pending_operations *npo) |
| 152 | { |
| 153 | struct xenvif_rx_meta *meta; |
| 154 | struct xen_netif_rx_request req; |
| 155 | |
| 156 | RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req); |
| 157 | |
| 158 | meta = npo->meta + npo->meta_prod++; |
| 159 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; |
| 160 | meta->gso_size = 0; |
| 161 | meta->size = 0; |
| 162 | meta->id = req.id; |
| 163 | |
| 164 | npo->copy_off = 0; |
| 165 | npo->copy_gref = req.gref; |
| 166 | |
| 167 | return meta; |
| 168 | } |
| 169 | |
| 170 | struct gop_frag_copy { |
| 171 | struct xenvif_queue *queue; |
| 172 | struct netrx_pending_operations *npo; |
| 173 | struct xenvif_rx_meta *meta; |
| 174 | int head; |
| 175 | int gso_type; |
| 176 | int protocol; |
| 177 | int hash_present; |
| 178 | |
| 179 | struct page *page; |
| 180 | }; |
| 181 | |
| 182 | static void xenvif_setup_copy_gop(unsigned long gfn, |
| 183 | unsigned int offset, |
| 184 | unsigned int *len, |
| 185 | struct gop_frag_copy *info) |
| 186 | { |
| 187 | struct gnttab_copy *copy_gop; |
| 188 | struct xen_page_foreign *foreign; |
| 189 | /* Convenient aliases */ |
| 190 | struct xenvif_queue *queue = info->queue; |
| 191 | struct netrx_pending_operations *npo = info->npo; |
| 192 | struct page *page = info->page; |
| 193 | |
| 194 | WARN_ON(npo->copy_off > MAX_BUFFER_OFFSET); |
| 195 | |
| 196 | if (npo->copy_off == MAX_BUFFER_OFFSET) |
| 197 | info->meta = get_next_rx_buffer(queue, npo); |
| 198 | |
| 199 | if (npo->copy_off + *len > MAX_BUFFER_OFFSET) |
| 200 | *len = MAX_BUFFER_OFFSET - npo->copy_off; |
| 201 | |
| 202 | copy_gop = npo->copy + npo->copy_prod++; |
| 203 | copy_gop->flags = GNTCOPY_dest_gref; |
| 204 | copy_gop->len = *len; |
| 205 | |
| 206 | foreign = xen_page_foreign(page); |
| 207 | if (foreign) { |
| 208 | copy_gop->source.domid = foreign->domid; |
| 209 | copy_gop->source.u.ref = foreign->gref; |
| 210 | copy_gop->flags |= GNTCOPY_source_gref; |
| 211 | } else { |
| 212 | copy_gop->source.domid = DOMID_SELF; |
| 213 | copy_gop->source.u.gmfn = gfn; |
| 214 | } |
| 215 | copy_gop->source.offset = offset; |
| 216 | |
| 217 | copy_gop->dest.domid = queue->vif->domid; |
| 218 | copy_gop->dest.offset = npo->copy_off; |
| 219 | copy_gop->dest.u.ref = npo->copy_gref; |
| 220 | |
| 221 | npo->copy_off += *len; |
| 222 | info->meta->size += *len; |
| 223 | |
| 224 | if (!info->head) |
| 225 | return; |
| 226 | |
| 227 | /* Leave a gap for the GSO descriptor. */ |
| 228 | if ((1 << info->gso_type) & queue->vif->gso_mask) |
| 229 | queue->rx.req_cons++; |
| 230 | |
| 231 | /* Leave a gap for the hash extra segment. */ |
| 232 | if (info->hash_present) |
| 233 | queue->rx.req_cons++; |
| 234 | |
| 235 | info->head = 0; /* There must be something in this buffer now */ |
| 236 | } |
| 237 | |
| 238 | static void xenvif_gop_frag_copy_grant(unsigned long gfn, |
| 239 | unsigned int offset, |
| 240 | unsigned int len, |
| 241 | void *data) |
| 242 | { |
| 243 | unsigned int bytes; |
| 244 | |
| 245 | while (len) { |
| 246 | bytes = len; |
| 247 | xenvif_setup_copy_gop(gfn, offset, &bytes, data); |
| 248 | offset += bytes; |
| 249 | len -= bytes; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* Set up the grant operations for this fragment. If it's a flipping |
| 254 | * interface, we also set up the unmap request from here. |
| 255 | */ |
| 256 | static void xenvif_gop_frag_copy(struct xenvif_queue *queue, |
| 257 | struct sk_buff *skb, |
| 258 | struct netrx_pending_operations *npo, |
| 259 | struct page *page, unsigned long size, |
| 260 | unsigned long offset, int *head) |
| 261 | { |
| 262 | struct gop_frag_copy info = { |
| 263 | .queue = queue, |
| 264 | .npo = npo, |
| 265 | .head = *head, |
| 266 | .gso_type = XEN_NETIF_GSO_TYPE_NONE, |
| 267 | /* xenvif_set_skb_hash() will have either set a s/w |
| 268 | * hash or cleared the hash depending on |
| 269 | * whether the the frontend wants a hash for this skb. |
| 270 | */ |
| 271 | .hash_present = skb->sw_hash, |
| 272 | }; |
| 273 | unsigned long bytes; |
| 274 | |
| 275 | if (skb_is_gso(skb)) { |
| 276 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
| 277 | info.gso_type = XEN_NETIF_GSO_TYPE_TCPV4; |
| 278 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 279 | info.gso_type = XEN_NETIF_GSO_TYPE_TCPV6; |
| 280 | } |
| 281 | |
| 282 | /* Data must not cross a page boundary. */ |
| 283 | WARN_ON(size + offset > (PAGE_SIZE << compound_order(page))); |
| 284 | |
| 285 | info.meta = npo->meta + npo->meta_prod - 1; |
| 286 | |
| 287 | /* Skip unused frames from start of page */ |
| 288 | page += offset >> PAGE_SHIFT; |
| 289 | offset &= ~PAGE_MASK; |
| 290 | |
| 291 | while (size > 0) { |
| 292 | WARN_ON(offset >= PAGE_SIZE); |
| 293 | |
| 294 | bytes = PAGE_SIZE - offset; |
| 295 | if (bytes > size) |
| 296 | bytes = size; |
| 297 | |
| 298 | info.page = page; |
| 299 | gnttab_foreach_grant_in_range(page, offset, bytes, |
| 300 | xenvif_gop_frag_copy_grant, |
| 301 | &info); |
| 302 | size -= bytes; |
| 303 | offset = 0; |
| 304 | |
| 305 | /* Next page */ |
| 306 | if (size) { |
| 307 | WARN_ON(!PageCompound(page)); |
| 308 | page++; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | *head = info.head; |
| 313 | } |
| 314 | |
| 315 | /* Prepare an SKB to be transmitted to the frontend. |
| 316 | * |
| 317 | * This function is responsible for allocating grant operations, meta |
| 318 | * structures, etc. |
| 319 | * |
| 320 | * It returns the number of meta structures consumed. The number of |
| 321 | * ring slots used is always equal to the number of meta slots used |
| 322 | * plus the number of GSO descriptors used. Currently, we use either |
| 323 | * zero GSO descriptors (for non-GSO packets) or one descriptor (for |
| 324 | * frontend-side LRO). |
| 325 | */ |
| 326 | static int xenvif_gop_skb(struct sk_buff *skb, |
| 327 | struct netrx_pending_operations *npo, |
| 328 | struct xenvif_queue *queue) |
| 329 | { |
| 330 | struct xenvif *vif = netdev_priv(skb->dev); |
| 331 | int nr_frags = skb_shinfo(skb)->nr_frags; |
| 332 | int i; |
| 333 | struct xen_netif_rx_request req; |
| 334 | struct xenvif_rx_meta *meta; |
| 335 | unsigned char *data; |
| 336 | int head = 1; |
| 337 | int old_meta_prod; |
| 338 | int gso_type; |
| 339 | |
| 340 | old_meta_prod = npo->meta_prod; |
| 341 | |
| 342 | gso_type = XEN_NETIF_GSO_TYPE_NONE; |
| 343 | if (skb_is_gso(skb)) { |
| 344 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
| 345 | gso_type = XEN_NETIF_GSO_TYPE_TCPV4; |
| 346 | else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 347 | gso_type = XEN_NETIF_GSO_TYPE_TCPV6; |
| 348 | } |
| 349 | |
| 350 | /* Set up a GSO prefix descriptor, if necessary */ |
| 351 | if ((1 << gso_type) & vif->gso_prefix_mask) { |
| 352 | RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req); |
| 353 | meta = npo->meta + npo->meta_prod++; |
| 354 | meta->gso_type = gso_type; |
| 355 | meta->gso_size = skb_shinfo(skb)->gso_size; |
| 356 | meta->size = 0; |
| 357 | meta->id = req.id; |
| 358 | } |
| 359 | |
| 360 | RING_COPY_REQUEST(&queue->rx, queue->rx.req_cons++, &req); |
| 361 | meta = npo->meta + npo->meta_prod++; |
| 362 | |
| 363 | if ((1 << gso_type) & vif->gso_mask) { |
| 364 | meta->gso_type = gso_type; |
| 365 | meta->gso_size = skb_shinfo(skb)->gso_size; |
| 366 | } else { |
| 367 | meta->gso_type = XEN_NETIF_GSO_TYPE_NONE; |
| 368 | meta->gso_size = 0; |
| 369 | } |
| 370 | |
| 371 | meta->size = 0; |
| 372 | meta->id = req.id; |
| 373 | npo->copy_off = 0; |
| 374 | npo->copy_gref = req.gref; |
| 375 | |
| 376 | data = skb->data; |
| 377 | while (data < skb_tail_pointer(skb)) { |
| 378 | unsigned int offset = offset_in_page(data); |
| 379 | unsigned int len = PAGE_SIZE - offset; |
| 380 | |
| 381 | if (data + len > skb_tail_pointer(skb)) |
| 382 | len = skb_tail_pointer(skb) - data; |
| 383 | |
| 384 | xenvif_gop_frag_copy(queue, skb, npo, |
| 385 | virt_to_page(data), len, offset, &head); |
| 386 | data += len; |
| 387 | } |
| 388 | |
| 389 | for (i = 0; i < nr_frags; i++) { |
| 390 | xenvif_gop_frag_copy(queue, skb, npo, |
| 391 | skb_frag_page(&skb_shinfo(skb)->frags[i]), |
| 392 | skb_frag_size(&skb_shinfo(skb)->frags[i]), |
| 393 | skb_shinfo(skb)->frags[i].page_offset, |
| 394 | &head); |
| 395 | } |
| 396 | |
| 397 | return npo->meta_prod - old_meta_prod; |
| 398 | } |
| 399 | |
| 400 | /* This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was |
| 401 | * used to set up the operations on the top of |
| 402 | * netrx_pending_operations, which have since been done. Check that |
| 403 | * they didn't give any errors and advance over them. |
| 404 | */ |
| 405 | static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots, |
| 406 | struct netrx_pending_operations *npo) |
| 407 | { |
| 408 | struct gnttab_copy *copy_op; |
| 409 | int status = XEN_NETIF_RSP_OKAY; |
| 410 | int i; |
| 411 | |
| 412 | for (i = 0; i < nr_meta_slots; i++) { |
| 413 | copy_op = npo->copy + npo->copy_cons++; |
| 414 | if (copy_op->status != GNTST_okay) { |
| 415 | netdev_dbg(vif->dev, |
| 416 | "Bad status %d from copy to DOM%d.\n", |
| 417 | copy_op->status, vif->domid); |
| 418 | status = XEN_NETIF_RSP_ERROR; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return status; |
| 423 | } |
| 424 | |
| 425 | static struct xen_netif_rx_response *make_rx_response( |
| 426 | struct xenvif_queue *queue, u16 id, s8 st, u16 offset, u16 size, |
| 427 | u16 flags) |
| 428 | { |
| 429 | RING_IDX i = queue->rx.rsp_prod_pvt; |
| 430 | struct xen_netif_rx_response *resp; |
| 431 | |
| 432 | resp = RING_GET_RESPONSE(&queue->rx, i); |
| 433 | resp->offset = offset; |
| 434 | resp->flags = flags; |
| 435 | resp->id = id; |
| 436 | resp->status = (s16)size; |
| 437 | if (st < 0) |
| 438 | resp->status = (s16)st; |
| 439 | |
| 440 | queue->rx.rsp_prod_pvt = ++i; |
| 441 | |
| 442 | return resp; |
| 443 | } |
| 444 | |
| 445 | static void xenvif_add_frag_responses(struct xenvif_queue *queue, |
| 446 | int status, |
| 447 | struct xenvif_rx_meta *meta, |
| 448 | int nr_meta_slots) |
| 449 | { |
| 450 | int i; |
| 451 | unsigned long offset; |
| 452 | |
| 453 | /* No fragments used */ |
| 454 | if (nr_meta_slots <= 1) |
| 455 | return; |
| 456 | |
| 457 | nr_meta_slots--; |
| 458 | |
| 459 | for (i = 0; i < nr_meta_slots; i++) { |
| 460 | int flags; |
| 461 | |
| 462 | if (i == nr_meta_slots - 1) |
| 463 | flags = 0; |
| 464 | else |
| 465 | flags = XEN_NETRXF_more_data; |
| 466 | |
| 467 | offset = 0; |
| 468 | make_rx_response(queue, meta[i].id, status, offset, |
| 469 | meta[i].size, flags); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static void xenvif_rx_action(struct xenvif_queue *queue) |
| 474 | { |
| 475 | struct xenvif *vif = queue->vif; |
| 476 | s8 status; |
| 477 | u16 flags; |
| 478 | struct xen_netif_rx_response *resp; |
| 479 | struct sk_buff_head rxq; |
| 480 | struct sk_buff *skb; |
| 481 | LIST_HEAD(notify); |
| 482 | int ret; |
| 483 | unsigned long offset; |
| 484 | bool need_to_notify = false; |
| 485 | |
| 486 | struct netrx_pending_operations npo = { |
| 487 | .copy = queue->grant_copy_op, |
| 488 | .meta = queue->meta, |
| 489 | }; |
| 490 | |
| 491 | skb_queue_head_init(&rxq); |
| 492 | |
| 493 | while (xenvif_rx_ring_slots_available(queue) && |
| 494 | (skb = xenvif_rx_dequeue(queue)) != NULL) { |
| 495 | queue->last_rx_time = jiffies; |
| 496 | |
| 497 | XENVIF_RX_CB(skb)->meta_slots_used = |
| 498 | xenvif_gop_skb(skb, &npo, queue); |
| 499 | |
| 500 | __skb_queue_tail(&rxq, skb); |
| 501 | } |
| 502 | |
| 503 | WARN_ON(npo.meta_prod > ARRAY_SIZE(queue->meta)); |
| 504 | |
| 505 | if (!npo.copy_prod) |
| 506 | goto done; |
| 507 | |
| 508 | WARN_ON(npo.copy_prod > MAX_GRANT_COPY_OPS); |
| 509 | gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod); |
| 510 | |
| 511 | while ((skb = __skb_dequeue(&rxq)) != NULL) { |
| 512 | struct xen_netif_extra_info *extra = NULL; |
| 513 | |
| 514 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
| 515 | vif->gso_prefix_mask) { |
| 516 | resp = RING_GET_RESPONSE(&queue->rx, |
| 517 | queue->rx.rsp_prod_pvt++); |
| 518 | |
| 519 | resp->flags = XEN_NETRXF_gso_prefix | |
| 520 | XEN_NETRXF_more_data; |
| 521 | |
| 522 | resp->offset = queue->meta[npo.meta_cons].gso_size; |
| 523 | resp->id = queue->meta[npo.meta_cons].id; |
| 524 | resp->status = XENVIF_RX_CB(skb)->meta_slots_used; |
| 525 | |
| 526 | npo.meta_cons++; |
| 527 | XENVIF_RX_CB(skb)->meta_slots_used--; |
| 528 | } |
| 529 | |
| 530 | queue->stats.tx_bytes += skb->len; |
| 531 | queue->stats.tx_packets++; |
| 532 | |
| 533 | status = xenvif_check_gop(vif, |
| 534 | XENVIF_RX_CB(skb)->meta_slots_used, |
| 535 | &npo); |
| 536 | |
| 537 | if (XENVIF_RX_CB(skb)->meta_slots_used == 1) |
| 538 | flags = 0; |
| 539 | else |
| 540 | flags = XEN_NETRXF_more_data; |
| 541 | |
| 542 | if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */ |
| 543 | flags |= XEN_NETRXF_csum_blank | |
| 544 | XEN_NETRXF_data_validated; |
| 545 | else if (skb->ip_summed == CHECKSUM_UNNECESSARY) |
| 546 | /* remote but checksummed. */ |
| 547 | flags |= XEN_NETRXF_data_validated; |
| 548 | |
| 549 | offset = 0; |
| 550 | resp = make_rx_response(queue, queue->meta[npo.meta_cons].id, |
| 551 | status, offset, |
| 552 | queue->meta[npo.meta_cons].size, |
| 553 | flags); |
| 554 | |
| 555 | if ((1 << queue->meta[npo.meta_cons].gso_type) & |
| 556 | vif->gso_mask) { |
| 557 | extra = (struct xen_netif_extra_info *) |
| 558 | RING_GET_RESPONSE(&queue->rx, |
| 559 | queue->rx.rsp_prod_pvt++); |
| 560 | |
| 561 | resp->flags |= XEN_NETRXF_extra_info; |
| 562 | |
| 563 | extra->u.gso.type = queue->meta[npo.meta_cons].gso_type; |
| 564 | extra->u.gso.size = queue->meta[npo.meta_cons].gso_size; |
| 565 | extra->u.gso.pad = 0; |
| 566 | extra->u.gso.features = 0; |
| 567 | |
| 568 | extra->type = XEN_NETIF_EXTRA_TYPE_GSO; |
| 569 | extra->flags = 0; |
| 570 | } |
| 571 | |
| 572 | if (skb->sw_hash) { |
| 573 | /* Since the skb got here via xenvif_select_queue() |
| 574 | * we know that the hash has been re-calculated |
| 575 | * according to a configuration set by the frontend |
| 576 | * and therefore we know that it is legitimate to |
| 577 | * pass it to the frontend. |
| 578 | */ |
| 579 | if (resp->flags & XEN_NETRXF_extra_info) |
| 580 | extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE; |
| 581 | else |
| 582 | resp->flags |= XEN_NETRXF_extra_info; |
| 583 | |
| 584 | extra = (struct xen_netif_extra_info *) |
| 585 | RING_GET_RESPONSE(&queue->rx, |
| 586 | queue->rx.rsp_prod_pvt++); |
| 587 | |
| 588 | extra->u.hash.algorithm = |
| 589 | XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ; |
| 590 | |
| 591 | if (skb->l4_hash) |
| 592 | extra->u.hash.type = |
| 593 | skb->protocol == htons(ETH_P_IP) ? |
| 594 | _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP : |
| 595 | _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP; |
| 596 | else |
| 597 | extra->u.hash.type = |
| 598 | skb->protocol == htons(ETH_P_IP) ? |
| 599 | _XEN_NETIF_CTRL_HASH_TYPE_IPV4 : |
| 600 | _XEN_NETIF_CTRL_HASH_TYPE_IPV6; |
| 601 | |
| 602 | *(uint32_t *)extra->u.hash.value = |
| 603 | skb_get_hash_raw(skb); |
| 604 | |
| 605 | extra->type = XEN_NETIF_EXTRA_TYPE_HASH; |
| 606 | extra->flags = 0; |
| 607 | } |
| 608 | |
| 609 | xenvif_add_frag_responses(queue, status, |
| 610 | queue->meta + npo.meta_cons + 1, |
| 611 | XENVIF_RX_CB(skb)->meta_slots_used); |
| 612 | |
| 613 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret); |
| 614 | |
| 615 | need_to_notify |= !!ret; |
| 616 | |
| 617 | npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used; |
| 618 | dev_kfree_skb(skb); |
| 619 | } |
| 620 | |
| 621 | done: |
| 622 | if (need_to_notify) |
| 623 | notify_remote_via_irq(queue->rx_irq); |
| 624 | } |
| 625 | |
| 626 | static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue) |
| 627 | { |
| 628 | RING_IDX prod, cons; |
| 629 | |
| 630 | prod = queue->rx.sring->req_prod; |
| 631 | cons = queue->rx.req_cons; |
| 632 | |
| 633 | return !queue->stalled && |
| 634 | prod - cons < 1 && |
| 635 | time_after(jiffies, |
| 636 | queue->last_rx_time + queue->vif->stall_timeout); |
| 637 | } |
| 638 | |
| 639 | static bool xenvif_rx_queue_ready(struct xenvif_queue *queue) |
| 640 | { |
| 641 | RING_IDX prod, cons; |
| 642 | |
| 643 | prod = queue->rx.sring->req_prod; |
| 644 | cons = queue->rx.req_cons; |
| 645 | |
| 646 | return queue->stalled && prod - cons >= 1; |
| 647 | } |
| 648 | |
| 649 | static bool xenvif_have_rx_work(struct xenvif_queue *queue) |
| 650 | { |
| 651 | return xenvif_rx_ring_slots_available(queue) || |
| 652 | (queue->vif->stall_timeout && |
| 653 | (xenvif_rx_queue_stalled(queue) || |
| 654 | xenvif_rx_queue_ready(queue))) || |
| 655 | kthread_should_stop() || |
| 656 | queue->vif->disabled; |
| 657 | } |
| 658 | |
| 659 | static long xenvif_rx_queue_timeout(struct xenvif_queue *queue) |
| 660 | { |
| 661 | struct sk_buff *skb; |
| 662 | long timeout; |
| 663 | |
| 664 | skb = skb_peek(&queue->rx_queue); |
| 665 | if (!skb) |
| 666 | return MAX_SCHEDULE_TIMEOUT; |
| 667 | |
| 668 | timeout = XENVIF_RX_CB(skb)->expires - jiffies; |
| 669 | return timeout < 0 ? 0 : timeout; |
| 670 | } |
| 671 | |
| 672 | /* Wait until the guest Rx thread has work. |
| 673 | * |
| 674 | * The timeout needs to be adjusted based on the current head of the |
| 675 | * queue (and not just the head at the beginning). In particular, if |
| 676 | * the queue is initially empty an infinite timeout is used and this |
| 677 | * needs to be reduced when a skb is queued. |
| 678 | * |
| 679 | * This cannot be done with wait_event_timeout() because it only |
| 680 | * calculates the timeout once. |
| 681 | */ |
| 682 | static void xenvif_wait_for_rx_work(struct xenvif_queue *queue) |
| 683 | { |
| 684 | DEFINE_WAIT(wait); |
| 685 | |
| 686 | if (xenvif_have_rx_work(queue)) |
| 687 | return; |
| 688 | |
| 689 | for (;;) { |
| 690 | long ret; |
| 691 | |
| 692 | prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE); |
| 693 | if (xenvif_have_rx_work(queue)) |
| 694 | break; |
| 695 | ret = schedule_timeout(xenvif_rx_queue_timeout(queue)); |
| 696 | if (!ret) |
| 697 | break; |
| 698 | } |
| 699 | finish_wait(&queue->wq, &wait); |
| 700 | } |
| 701 | |
| 702 | static void xenvif_queue_carrier_off(struct xenvif_queue *queue) |
| 703 | { |
| 704 | struct xenvif *vif = queue->vif; |
| 705 | |
| 706 | queue->stalled = true; |
| 707 | |
| 708 | /* At least one queue has stalled? Disable the carrier. */ |
| 709 | spin_lock(&vif->lock); |
| 710 | if (vif->stalled_queues++ == 0) { |
| 711 | netdev_info(vif->dev, "Guest Rx stalled"); |
| 712 | netif_carrier_off(vif->dev); |
| 713 | } |
| 714 | spin_unlock(&vif->lock); |
| 715 | } |
| 716 | |
| 717 | static void xenvif_queue_carrier_on(struct xenvif_queue *queue) |
| 718 | { |
| 719 | struct xenvif *vif = queue->vif; |
| 720 | |
| 721 | queue->last_rx_time = jiffies; /* Reset Rx stall detection. */ |
| 722 | queue->stalled = false; |
| 723 | |
| 724 | /* All queues are ready? Enable the carrier. */ |
| 725 | spin_lock(&vif->lock); |
| 726 | if (--vif->stalled_queues == 0) { |
| 727 | netdev_info(vif->dev, "Guest Rx ready"); |
| 728 | netif_carrier_on(vif->dev); |
| 729 | } |
| 730 | spin_unlock(&vif->lock); |
| 731 | } |
| 732 | |
| 733 | int xenvif_kthread_guest_rx(void *data) |
| 734 | { |
| 735 | struct xenvif_queue *queue = data; |
| 736 | struct xenvif *vif = queue->vif; |
| 737 | |
| 738 | if (!vif->stall_timeout) |
| 739 | xenvif_queue_carrier_on(queue); |
| 740 | |
| 741 | for (;;) { |
| 742 | xenvif_wait_for_rx_work(queue); |
| 743 | |
| 744 | if (kthread_should_stop()) |
| 745 | break; |
| 746 | |
| 747 | /* This frontend is found to be rogue, disable it in |
| 748 | * kthread context. Currently this is only set when |
| 749 | * netback finds out frontend sends malformed packet, |
| 750 | * but we cannot disable the interface in softirq |
| 751 | * context so we defer it here, if this thread is |
| 752 | * associated with queue 0. |
| 753 | */ |
| 754 | if (unlikely(vif->disabled && queue->id == 0)) { |
| 755 | xenvif_carrier_off(vif); |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | if (!skb_queue_empty(&queue->rx_queue)) |
| 760 | xenvif_rx_action(queue); |
| 761 | |
| 762 | /* If the guest hasn't provided any Rx slots for a |
| 763 | * while it's probably not responsive, drop the |
| 764 | * carrier so packets are dropped earlier. |
| 765 | */ |
| 766 | if (vif->stall_timeout) { |
| 767 | if (xenvif_rx_queue_stalled(queue)) |
| 768 | xenvif_queue_carrier_off(queue); |
| 769 | else if (xenvif_rx_queue_ready(queue)) |
| 770 | xenvif_queue_carrier_on(queue); |
| 771 | } |
| 772 | |
| 773 | /* Queued packets may have foreign pages from other |
| 774 | * domains. These cannot be queued indefinitely as |
| 775 | * this would starve guests of grant refs and transmit |
| 776 | * slots. |
| 777 | */ |
| 778 | xenvif_rx_queue_drop_expired(queue); |
| 779 | |
| 780 | xenvif_rx_queue_maybe_wake(queue); |
| 781 | |
| 782 | cond_resched(); |
| 783 | } |
| 784 | |
| 785 | /* Bin any remaining skbs */ |
| 786 | xenvif_rx_queue_purge(queue); |
| 787 | |
| 788 | return 0; |
| 789 | } |