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>
37 * Update the needed ring page slots for the first SKB queued.
38 * Note that any call sequence outside the RX thread calling this function
39 * needs to wake up the RX thread via a call of xenvif_kick_thread()
40 * afterwards in order to avoid a race with putting the thread to sleep.
42 static void xenvif_update_needed_slots(struct xenvif_queue *queue,
43 const struct sk_buff *skb)
45 unsigned int needed = 0;
48 needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
55 WRITE_ONCE(queue->rx_slots_needed, needed);
58 static bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue)
63 needed = READ_ONCE(queue->rx_slots_needed);
68 prod = queue->rx.sring->req_prod;
69 cons = queue->rx.req_cons;
71 if (prod - cons >= needed)
74 queue->rx.sring->req_event = prod + 1;
76 /* Make sure event is visible before we check prod
80 } while (queue->rx.sring->req_prod != prod);
85 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
89 spin_lock_irqsave(&queue->rx_queue.lock, flags);
91 if (queue->rx_queue_len >= queue->rx_queue_max) {
92 struct net_device *dev = queue->vif->dev;
94 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
96 queue->vif->dev->stats.rx_dropped++;
98 if (skb_queue_empty(&queue->rx_queue))
99 xenvif_update_needed_slots(queue, skb);
101 __skb_queue_tail(&queue->rx_queue, skb);
103 queue->rx_queue_len += skb->len;
106 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
109 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
113 spin_lock_irq(&queue->rx_queue.lock);
115 skb = __skb_dequeue(&queue->rx_queue);
117 xenvif_update_needed_slots(queue, skb_peek(&queue->rx_queue));
119 queue->rx_queue_len -= skb->len;
120 if (queue->rx_queue_len < queue->rx_queue_max) {
121 struct netdev_queue *txq;
123 txq = netdev_get_tx_queue(queue->vif->dev, queue->id);
124 netif_tx_wake_queue(txq);
128 spin_unlock_irq(&queue->rx_queue.lock);
133 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
137 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
141 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
146 skb = skb_peek(&queue->rx_queue);
149 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
151 xenvif_rx_dequeue(queue);
153 queue->vif->dev->stats.rx_dropped++;
157 static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
162 gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
164 for (i = 0; i < queue->rx_copy.num; i++) {
165 struct gnttab_copy *op;
167 op = &queue->rx_copy.op[i];
169 /* If the copy failed, overwrite the status field in
170 * the corresponding response.
172 if (unlikely(op->status != GNTST_okay)) {
173 struct xen_netif_rx_response *rsp;
175 rsp = RING_GET_RESPONSE(&queue->rx,
176 queue->rx_copy.idx[i]);
177 rsp->status = op->status;
181 queue->rx_copy.num = 0;
183 /* Push responses for all completed packets. */
184 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
186 notify_remote_via_irq(queue->rx_irq);
188 __skb_queue_purge(queue->rx_copy.completed);
191 static void xenvif_rx_copy_add(struct xenvif_queue *queue,
192 struct xen_netif_rx_request *req,
193 unsigned int offset, void *data, size_t len)
195 struct gnttab_copy *op;
197 struct xen_page_foreign *foreign;
199 if (queue->rx_copy.num == COPY_BATCH_SIZE)
200 xenvif_rx_copy_flush(queue);
202 op = &queue->rx_copy.op[queue->rx_copy.num];
204 page = virt_to_page(data);
206 op->flags = GNTCOPY_dest_gref;
208 foreign = xen_page_foreign(page);
210 op->source.domid = foreign->domid;
211 op->source.u.ref = foreign->gref;
212 op->flags |= GNTCOPY_source_gref;
214 op->source.u.gmfn = virt_to_gfn(data);
215 op->source.domid = DOMID_SELF;
218 op->source.offset = xen_offset_in_page(data);
219 op->dest.u.ref = req->gref;
220 op->dest.domid = queue->vif->domid;
221 op->dest.offset = offset;
224 queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
225 queue->rx_copy.num++;
228 static unsigned int xenvif_gso_type(struct sk_buff *skb)
230 if (skb_is_gso(skb)) {
231 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
232 return XEN_NETIF_GSO_TYPE_TCPV4;
234 return XEN_NETIF_GSO_TYPE_TCPV6;
236 return XEN_NETIF_GSO_TYPE_NONE;
239 struct xenvif_pkt_state {
241 size_t remaining_len;
242 struct sk_buff *frag_iter;
243 int frag; /* frag == -1 => frag_iter->head */
244 unsigned int frag_offset;
245 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
246 unsigned int extra_count;
250 static void xenvif_rx_next_skb(struct xenvif_queue *queue,
251 struct xenvif_pkt_state *pkt)
254 unsigned int gso_type;
256 skb = xenvif_rx_dequeue(queue);
258 queue->stats.tx_bytes += skb->len;
259 queue->stats.tx_packets++;
261 /* Reset packet state. */
262 memset(pkt, 0, sizeof(struct xenvif_pkt_state));
265 pkt->frag_iter = skb;
266 pkt->remaining_len = skb->len;
269 gso_type = xenvif_gso_type(skb);
270 if ((1 << gso_type) & queue->vif->gso_mask) {
271 struct xen_netif_extra_info *extra;
273 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
275 extra->u.gso.type = gso_type;
276 extra->u.gso.size = skb_shinfo(skb)->gso_size;
277 extra->u.gso.pad = 0;
278 extra->u.gso.features = 0;
279 extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
286 struct xen_netif_extra_info *extra;
288 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
290 extra->u.hash.algorithm =
291 XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
295 skb->protocol == htons(ETH_P_IP) ?
296 _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
297 _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
300 skb->protocol == htons(ETH_P_IP) ?
301 _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
302 _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
304 *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
306 extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
313 static void xenvif_rx_complete(struct xenvif_queue *queue,
314 struct xenvif_pkt_state *pkt)
316 /* All responses are ready to be pushed. */
317 queue->rx.rsp_prod_pvt = queue->rx.req_cons;
319 __skb_queue_tail(queue->rx_copy.completed, pkt->skb);
322 static void xenvif_rx_next_frag(struct xenvif_pkt_state *pkt)
324 struct sk_buff *frag_iter = pkt->frag_iter;
325 unsigned int nr_frags = skb_shinfo(frag_iter)->nr_frags;
328 pkt->frag_offset = 0;
330 if (pkt->frag >= nr_frags) {
331 if (frag_iter == pkt->skb)
332 pkt->frag_iter = skb_shinfo(frag_iter)->frag_list;
334 pkt->frag_iter = frag_iter->next;
340 static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
341 struct xenvif_pkt_state *pkt,
342 unsigned int offset, void **data,
345 struct sk_buff *frag_iter = pkt->frag_iter;
347 size_t frag_len, chunk_len;
351 if (pkt->frag == -1) {
352 frag_data = frag_iter->data;
353 frag_len = skb_headlen(frag_iter);
355 skb_frag_t *frag = &skb_shinfo(frag_iter)->frags[pkt->frag];
357 frag_data = skb_frag_address(frag);
358 frag_len = skb_frag_size(frag);
361 frag_data += pkt->frag_offset;
362 frag_len -= pkt->frag_offset;
364 chunk_len = min_t(size_t, frag_len, XEN_PAGE_SIZE - offset);
365 chunk_len = min_t(size_t, chunk_len, XEN_PAGE_SIZE -
366 xen_offset_in_page(frag_data));
368 pkt->frag_offset += chunk_len;
370 /* Advance to next frag? */
371 if (frag_len == chunk_len)
372 xenvif_rx_next_frag(pkt);
378 static void xenvif_rx_data_slot(struct xenvif_queue *queue,
379 struct xenvif_pkt_state *pkt,
380 struct xen_netif_rx_request *req,
381 struct xen_netif_rx_response *rsp)
383 unsigned int offset = 0;
390 xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
391 xenvif_rx_copy_add(queue, req, offset, data, len);
394 pkt->remaining_len -= len;
396 } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
398 if (pkt->remaining_len > 0)
399 flags = XEN_NETRXF_more_data;
403 if (pkt->slot == 0) {
404 struct sk_buff *skb = pkt->skb;
406 if (skb->ip_summed == CHECKSUM_PARTIAL)
407 flags |= XEN_NETRXF_csum_blank |
408 XEN_NETRXF_data_validated;
409 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
410 flags |= XEN_NETRXF_data_validated;
412 if (pkt->extra_count != 0)
413 flags |= XEN_NETRXF_extra_info;
419 rsp->status = (s16)offset;
422 static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
423 struct xenvif_pkt_state *pkt,
424 struct xen_netif_rx_request *req,
425 struct xen_netif_rx_response *rsp)
427 struct xen_netif_extra_info *extra = (void *)rsp;
432 for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
433 if (pkt->extras[i].type) {
434 *extra = pkt->extras[i];
436 if (pkt->extra_count != 0)
437 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
439 pkt->extras[i].type = 0;
446 void xenvif_rx_skb(struct xenvif_queue *queue)
448 struct xenvif_pkt_state pkt;
450 xenvif_rx_next_skb(queue, &pkt);
452 queue->last_rx_time = jiffies;
455 struct xen_netif_rx_request *req;
456 struct xen_netif_rx_response *rsp;
458 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
459 rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
461 /* Extras must go after the first data slot */
462 if (pkt.slot != 0 && pkt.extra_count != 0)
463 xenvif_rx_extra_slot(queue, &pkt, req, rsp);
465 xenvif_rx_data_slot(queue, &pkt, req, rsp);
467 queue->rx.req_cons++;
469 } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
471 xenvif_rx_complete(queue, &pkt);
474 #define RX_BATCH_SIZE 64
476 void xenvif_rx_action(struct xenvif_queue *queue)
478 struct sk_buff_head completed_skbs;
479 unsigned int work_done = 0;
481 __skb_queue_head_init(&completed_skbs);
482 queue->rx_copy.completed = &completed_skbs;
484 while (xenvif_rx_ring_slots_available(queue) &&
485 work_done < RX_BATCH_SIZE) {
486 xenvif_rx_skb(queue);
490 /* Flush any pending copies and complete all skbs. */
491 xenvif_rx_copy_flush(queue);
494 static RING_IDX xenvif_rx_queue_slots(const struct xenvif_queue *queue)
498 prod = queue->rx.sring->req_prod;
499 cons = queue->rx.req_cons;
504 static bool xenvif_rx_queue_stalled(const struct xenvif_queue *queue)
506 unsigned int needed = READ_ONCE(queue->rx_slots_needed);
508 return !queue->stalled &&
509 xenvif_rx_queue_slots(queue) < needed &&
511 queue->last_rx_time + queue->vif->stall_timeout);
514 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
516 unsigned int needed = READ_ONCE(queue->rx_slots_needed);
518 return queue->stalled && xenvif_rx_queue_slots(queue) >= needed;
521 bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread)
523 return xenvif_rx_ring_slots_available(queue) ||
524 (queue->vif->stall_timeout &&
525 (xenvif_rx_queue_stalled(queue) ||
526 xenvif_rx_queue_ready(queue))) ||
527 (test_kthread && kthread_should_stop()) ||
528 queue->vif->disabled;
531 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
536 skb = skb_peek(&queue->rx_queue);
538 return MAX_SCHEDULE_TIMEOUT;
540 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
541 return timeout < 0 ? 0 : timeout;
544 /* Wait until the guest Rx thread has work.
546 * The timeout needs to be adjusted based on the current head of the
547 * queue (and not just the head at the beginning). In particular, if
548 * the queue is initially empty an infinite timeout is used and this
549 * needs to be reduced when a skb is queued.
551 * This cannot be done with wait_event_timeout() because it only
552 * calculates the timeout once.
554 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
558 if (xenvif_have_rx_work(queue, true))
564 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
565 if (xenvif_have_rx_work(queue, true))
567 if (atomic_fetch_andnot(NETBK_RX_EOI | NETBK_COMMON_EOI,
568 &queue->eoi_pending) &
569 (NETBK_RX_EOI | NETBK_COMMON_EOI))
570 xen_irq_lateeoi(queue->rx_irq, 0);
572 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
576 finish_wait(&queue->wq, &wait);
579 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
581 struct xenvif *vif = queue->vif;
583 queue->stalled = true;
585 /* At least one queue has stalled? Disable the carrier. */
586 spin_lock(&vif->lock);
587 if (vif->stalled_queues++ == 0) {
588 netdev_info(vif->dev, "Guest Rx stalled");
589 netif_carrier_off(vif->dev);
591 spin_unlock(&vif->lock);
594 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
596 struct xenvif *vif = queue->vif;
598 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
599 queue->stalled = false;
601 /* All queues are ready? Enable the carrier. */
602 spin_lock(&vif->lock);
603 if (--vif->stalled_queues == 0) {
604 netdev_info(vif->dev, "Guest Rx ready");
605 netif_carrier_on(vif->dev);
607 spin_unlock(&vif->lock);
610 int xenvif_kthread_guest_rx(void *data)
612 struct xenvif_queue *queue = data;
613 struct xenvif *vif = queue->vif;
615 if (!vif->stall_timeout)
616 xenvif_queue_carrier_on(queue);
619 xenvif_wait_for_rx_work(queue);
621 if (kthread_should_stop())
624 /* This frontend is found to be rogue, disable it in
625 * kthread context. Currently this is only set when
626 * netback finds out frontend sends malformed packet,
627 * but we cannot disable the interface in softirq
628 * context so we defer it here, if this thread is
629 * associated with queue 0.
631 if (unlikely(vif->disabled && queue->id == 0)) {
632 xenvif_carrier_off(vif);
636 if (!skb_queue_empty(&queue->rx_queue))
637 xenvif_rx_action(queue);
639 /* If the guest hasn't provided any Rx slots for a
640 * while it's probably not responsive, drop the
641 * carrier so packets are dropped earlier.
643 if (vif->stall_timeout) {
644 if (xenvif_rx_queue_stalled(queue))
645 xenvif_queue_carrier_off(queue);
646 else if (xenvif_rx_queue_ready(queue))
647 xenvif_queue_carrier_on(queue);
650 /* Queued packets may have foreign pages from other
651 * domains. These cannot be queued indefinitely as
652 * this would starve guests of grant refs and transmit
655 xenvif_rx_queue_drop_expired(queue);
660 /* Bin any remaining skbs */
661 xenvif_rx_queue_purge(queue);