2 * Copyright (c) 2016 Citrix Systems Inc.
3 * Copyright (c) 2002-2005, K A Fraser
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:
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
31 #include <linux/kthread.h>
34 #include <xen/events.h>
36 static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue)
42 skb = skb_peek(&queue->rx_queue);
46 needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
53 prod = queue->rx.sring->req_prod;
54 cons = queue->rx.req_cons;
56 if (prod - cons >= needed)
59 queue->rx.sring->req_event = prod + 1;
61 /* Make sure event is visible before we check prod
65 } while (queue->rx.sring->req_prod != prod);
70 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
74 spin_lock_irqsave(&queue->rx_queue.lock, flags);
76 __skb_queue_tail(&queue->rx_queue, skb);
78 queue->rx_queue_len += skb->len;
79 if (queue->rx_queue_len > queue->rx_queue_max) {
80 struct net_device *dev = queue->vif->dev;
82 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
85 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
88 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
92 spin_lock_irq(&queue->rx_queue.lock);
94 skb = __skb_dequeue(&queue->rx_queue);
96 queue->rx_queue_len -= skb->len;
98 spin_unlock_irq(&queue->rx_queue.lock);
103 static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
105 spin_lock_irq(&queue->rx_queue.lock);
107 if (queue->rx_queue_len < queue->rx_queue_max) {
108 struct net_device *dev = queue->vif->dev;
110 netif_tx_wake_queue(netdev_get_tx_queue(dev, queue->id));
113 spin_unlock_irq(&queue->rx_queue.lock);
116 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
120 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
124 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
129 skb = skb_peek(&queue->rx_queue);
132 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
134 xenvif_rx_dequeue(queue);
139 static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
143 gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
145 for (i = 0; i < queue->rx_copy.num; i++) {
146 struct gnttab_copy *op;
148 op = &queue->rx_copy.op[i];
150 /* If the copy failed, overwrite the status field in
151 * the corresponding response.
153 if (unlikely(op->status != GNTST_okay)) {
154 struct xen_netif_rx_response *rsp;
156 rsp = RING_GET_RESPONSE(&queue->rx,
157 queue->rx_copy.idx[i]);
158 rsp->status = op->status;
162 queue->rx_copy.num = 0;
165 static void xenvif_rx_copy_add(struct xenvif_queue *queue,
166 struct xen_netif_rx_request *req,
167 unsigned int offset, void *data, size_t len)
169 struct gnttab_copy *op;
171 struct xen_page_foreign *foreign;
173 if (queue->rx_copy.num == COPY_BATCH_SIZE)
174 xenvif_rx_copy_flush(queue);
176 op = &queue->rx_copy.op[queue->rx_copy.num];
178 page = virt_to_page(data);
180 op->flags = GNTCOPY_dest_gref;
182 foreign = xen_page_foreign(page);
184 op->source.domid = foreign->domid;
185 op->source.u.ref = foreign->gref;
186 op->flags |= GNTCOPY_source_gref;
188 op->source.u.gmfn = virt_to_gfn(data);
189 op->source.domid = DOMID_SELF;
192 op->source.offset = xen_offset_in_page(data);
193 op->dest.u.ref = req->gref;
194 op->dest.domid = queue->vif->domid;
195 op->dest.offset = offset;
198 queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
199 queue->rx_copy.num++;
202 static unsigned int xenvif_gso_type(struct sk_buff *skb)
204 if (skb_is_gso(skb)) {
205 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
206 return XEN_NETIF_GSO_TYPE_TCPV4;
208 return XEN_NETIF_GSO_TYPE_TCPV6;
210 return XEN_NETIF_GSO_TYPE_NONE;
213 struct xenvif_pkt_state {
215 size_t remaining_len;
216 int frag; /* frag == -1 => skb->head */
217 unsigned int frag_offset;
218 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
219 unsigned int extra_count;
223 static void xenvif_rx_next_skb(struct xenvif_queue *queue,
224 struct xenvif_pkt_state *pkt)
227 unsigned int gso_type;
229 skb = xenvif_rx_dequeue(queue);
231 queue->stats.tx_bytes += skb->len;
232 queue->stats.tx_packets++;
234 /* Reset packet state. */
235 memset(pkt, 0, sizeof(struct xenvif_pkt_state));
238 pkt->remaining_len = skb->len;
241 gso_type = xenvif_gso_type(skb);
242 if ((1 << gso_type) & queue->vif->gso_mask) {
243 struct xen_netif_extra_info *extra;
245 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
247 extra->u.gso.type = gso_type;
248 extra->u.gso.size = skb_shinfo(skb)->gso_size;
249 extra->u.gso.pad = 0;
250 extra->u.gso.features = 0;
251 extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
258 struct xen_netif_extra_info *extra;
260 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
262 extra->u.hash.algorithm =
263 XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
267 skb->protocol == htons(ETH_P_IP) ?
268 _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
269 _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
272 skb->protocol == htons(ETH_P_IP) ?
273 _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
274 _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
276 *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
278 extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
285 static void xenvif_rx_complete(struct xenvif_queue *queue,
286 struct xenvif_pkt_state *pkt)
290 /* Complete any outstanding copy ops for this skb. */
291 xenvif_rx_copy_flush(queue);
293 /* Push responses and notify. */
294 queue->rx.rsp_prod_pvt = queue->rx.req_cons;
295 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
297 notify_remote_via_irq(queue->rx_irq);
299 dev_kfree_skb(pkt->skb);
302 static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
303 struct xenvif_pkt_state *pkt,
304 unsigned int offset, void **data,
307 struct sk_buff *skb = pkt->skb;
309 size_t frag_len, chunk_len;
311 if (pkt->frag == -1) {
312 frag_data = skb->data;
313 frag_len = skb_headlen(skb);
315 skb_frag_t *frag = &skb_shinfo(skb)->frags[pkt->frag];
317 frag_data = skb_frag_address(frag);
318 frag_len = skb_frag_size(frag);
321 frag_data += pkt->frag_offset;
322 frag_len -= pkt->frag_offset;
324 chunk_len = min(frag_len, XEN_PAGE_SIZE - offset);
325 chunk_len = min(chunk_len,
326 XEN_PAGE_SIZE - xen_offset_in_page(frag_data));
328 pkt->frag_offset += chunk_len;
330 /* Advance to next frag? */
331 if (frag_len == chunk_len) {
333 pkt->frag_offset = 0;
340 static void xenvif_rx_data_slot(struct xenvif_queue *queue,
341 struct xenvif_pkt_state *pkt,
342 struct xen_netif_rx_request *req,
343 struct xen_netif_rx_response *rsp)
345 unsigned int offset = 0;
352 xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
353 xenvif_rx_copy_add(queue, req, offset, data, len);
356 pkt->remaining_len -= len;
358 } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
360 if (pkt->remaining_len > 0)
361 flags = XEN_NETRXF_more_data;
365 if (pkt->slot == 0) {
366 struct sk_buff *skb = pkt->skb;
368 if (skb->ip_summed == CHECKSUM_PARTIAL)
369 flags |= XEN_NETRXF_csum_blank |
370 XEN_NETRXF_data_validated;
371 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
372 flags |= XEN_NETRXF_data_validated;
374 if (pkt->extra_count != 0)
375 flags |= XEN_NETRXF_extra_info;
381 rsp->status = (s16)offset;
384 static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
385 struct xenvif_pkt_state *pkt,
386 struct xen_netif_rx_request *req,
387 struct xen_netif_rx_response *rsp)
389 struct xen_netif_extra_info *extra = (void *)rsp;
394 for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
395 if (pkt->extras[i].type) {
396 *extra = pkt->extras[i];
398 if (pkt->extra_count != 0)
399 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
401 pkt->extras[i].type = 0;
408 void xenvif_rx_action(struct xenvif_queue *queue)
410 struct xenvif_pkt_state pkt;
412 xenvif_rx_next_skb(queue, &pkt);
415 struct xen_netif_rx_request *req;
416 struct xen_netif_rx_response *rsp;
418 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
419 rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
421 /* Extras must go after the first data slot */
422 if (pkt.slot != 0 && pkt.extra_count != 0)
423 xenvif_rx_extra_slot(queue, &pkt, req, rsp);
425 xenvif_rx_data_slot(queue, &pkt, req, rsp);
427 queue->rx.req_cons++;
429 } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
431 xenvif_rx_complete(queue, &pkt);
434 static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
438 prod = queue->rx.sring->req_prod;
439 cons = queue->rx.req_cons;
441 return !queue->stalled &&
444 queue->last_rx_time + queue->vif->stall_timeout);
447 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
451 prod = queue->rx.sring->req_prod;
452 cons = queue->rx.req_cons;
454 return queue->stalled && prod - cons >= 1;
457 static bool xenvif_have_rx_work(struct xenvif_queue *queue)
459 return xenvif_rx_ring_slots_available(queue) ||
460 (queue->vif->stall_timeout &&
461 (xenvif_rx_queue_stalled(queue) ||
462 xenvif_rx_queue_ready(queue))) ||
463 kthread_should_stop() ||
464 queue->vif->disabled;
467 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
472 skb = skb_peek(&queue->rx_queue);
474 return MAX_SCHEDULE_TIMEOUT;
476 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
477 return timeout < 0 ? 0 : timeout;
480 /* Wait until the guest Rx thread has work.
482 * The timeout needs to be adjusted based on the current head of the
483 * queue (and not just the head at the beginning). In particular, if
484 * the queue is initially empty an infinite timeout is used and this
485 * needs to be reduced when a skb is queued.
487 * This cannot be done with wait_event_timeout() because it only
488 * calculates the timeout once.
490 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
494 if (xenvif_have_rx_work(queue))
500 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
501 if (xenvif_have_rx_work(queue))
503 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
507 finish_wait(&queue->wq, &wait);
510 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
512 struct xenvif *vif = queue->vif;
514 queue->stalled = true;
516 /* At least one queue has stalled? Disable the carrier. */
517 spin_lock(&vif->lock);
518 if (vif->stalled_queues++ == 0) {
519 netdev_info(vif->dev, "Guest Rx stalled");
520 netif_carrier_off(vif->dev);
522 spin_unlock(&vif->lock);
525 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
527 struct xenvif *vif = queue->vif;
529 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
530 queue->stalled = false;
532 /* All queues are ready? Enable the carrier. */
533 spin_lock(&vif->lock);
534 if (--vif->stalled_queues == 0) {
535 netdev_info(vif->dev, "Guest Rx ready");
536 netif_carrier_on(vif->dev);
538 spin_unlock(&vif->lock);
541 int xenvif_kthread_guest_rx(void *data)
543 struct xenvif_queue *queue = data;
544 struct xenvif *vif = queue->vif;
546 if (!vif->stall_timeout)
547 xenvif_queue_carrier_on(queue);
550 xenvif_wait_for_rx_work(queue);
552 if (kthread_should_stop())
555 /* This frontend is found to be rogue, disable it in
556 * kthread context. Currently this is only set when
557 * netback finds out frontend sends malformed packet,
558 * but we cannot disable the interface in softirq
559 * context so we defer it here, if this thread is
560 * associated with queue 0.
562 if (unlikely(vif->disabled && queue->id == 0)) {
563 xenvif_carrier_off(vif);
567 if (!skb_queue_empty(&queue->rx_queue))
568 xenvif_rx_action(queue);
570 /* If the guest hasn't provided any Rx slots for a
571 * while it's probably not responsive, drop the
572 * carrier so packets are dropped earlier.
574 if (vif->stall_timeout) {
575 if (xenvif_rx_queue_stalled(queue))
576 xenvif_queue_carrier_off(queue);
577 else if (xenvif_rx_queue_ready(queue))
578 xenvif_queue_carrier_on(queue);
581 /* Queued packets may have foreign pages from other
582 * domains. These cannot be queued indefinitely as
583 * this would starve guests of grant refs and transmit
586 xenvif_rx_queue_drop_expired(queue);
588 xenvif_rx_queue_maybe_wake(queue);
593 /* Bin any remaining skbs */
594 xenvif_rx_queue_purge(queue);