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)
43 spin_lock_irqsave(&queue->rx_queue.lock, flags);
45 skb = skb_peek(&queue->rx_queue);
47 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
51 needed = DIV_ROUND_UP(skb->len, XEN_PAGE_SIZE);
57 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
60 prod = queue->rx.sring->req_prod;
61 cons = queue->rx.req_cons;
63 if (prod - cons >= needed)
66 queue->rx.sring->req_event = prod + 1;
68 /* Make sure event is visible before we check prod
72 } while (queue->rx.sring->req_prod != prod);
77 void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
81 spin_lock_irqsave(&queue->rx_queue.lock, flags);
83 __skb_queue_tail(&queue->rx_queue, skb);
85 queue->rx_queue_len += skb->len;
86 if (queue->rx_queue_len > queue->rx_queue_max) {
87 struct net_device *dev = queue->vif->dev;
89 netif_tx_stop_queue(netdev_get_tx_queue(dev, queue->id));
92 spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
95 static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
99 spin_lock_irq(&queue->rx_queue.lock);
101 skb = __skb_dequeue(&queue->rx_queue);
103 queue->rx_queue_len -= skb->len;
104 if (queue->rx_queue_len < queue->rx_queue_max) {
105 struct netdev_queue *txq;
107 txq = netdev_get_tx_queue(queue->vif->dev, queue->id);
108 netif_tx_wake_queue(txq);
112 spin_unlock_irq(&queue->rx_queue.lock);
117 static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
121 while ((skb = xenvif_rx_dequeue(queue)) != NULL)
125 static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
130 skb = skb_peek(&queue->rx_queue);
133 if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
135 xenvif_rx_dequeue(queue);
140 static void xenvif_rx_copy_flush(struct xenvif_queue *queue)
145 gnttab_batch_copy(queue->rx_copy.op, queue->rx_copy.num);
147 for (i = 0; i < queue->rx_copy.num; i++) {
148 struct gnttab_copy *op;
150 op = &queue->rx_copy.op[i];
152 /* If the copy failed, overwrite the status field in
153 * the corresponding response.
155 if (unlikely(op->status != GNTST_okay)) {
156 struct xen_netif_rx_response *rsp;
158 rsp = RING_GET_RESPONSE(&queue->rx,
159 queue->rx_copy.idx[i]);
160 rsp->status = op->status;
164 queue->rx_copy.num = 0;
166 /* Push responses for all completed packets. */
167 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, notify);
169 notify_remote_via_irq(queue->rx_irq);
171 __skb_queue_purge(queue->rx_copy.completed);
174 static void xenvif_rx_copy_add(struct xenvif_queue *queue,
175 struct xen_netif_rx_request *req,
176 unsigned int offset, void *data, size_t len)
178 struct gnttab_copy *op;
180 struct xen_page_foreign *foreign;
182 if (queue->rx_copy.num == COPY_BATCH_SIZE)
183 xenvif_rx_copy_flush(queue);
185 op = &queue->rx_copy.op[queue->rx_copy.num];
187 page = virt_to_page(data);
189 op->flags = GNTCOPY_dest_gref;
191 foreign = xen_page_foreign(page);
193 op->source.domid = foreign->domid;
194 op->source.u.ref = foreign->gref;
195 op->flags |= GNTCOPY_source_gref;
197 op->source.u.gmfn = virt_to_gfn(data);
198 op->source.domid = DOMID_SELF;
201 op->source.offset = xen_offset_in_page(data);
202 op->dest.u.ref = req->gref;
203 op->dest.domid = queue->vif->domid;
204 op->dest.offset = offset;
207 queue->rx_copy.idx[queue->rx_copy.num] = queue->rx.req_cons;
208 queue->rx_copy.num++;
211 static unsigned int xenvif_gso_type(struct sk_buff *skb)
213 if (skb_is_gso(skb)) {
214 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
215 return XEN_NETIF_GSO_TYPE_TCPV4;
217 return XEN_NETIF_GSO_TYPE_TCPV6;
219 return XEN_NETIF_GSO_TYPE_NONE;
222 struct xenvif_pkt_state {
224 size_t remaining_len;
225 struct sk_buff *frag_iter;
226 int frag; /* frag == -1 => frag_iter->head */
227 unsigned int frag_offset;
228 struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
229 unsigned int extra_count;
233 static void xenvif_rx_next_skb(struct xenvif_queue *queue,
234 struct xenvif_pkt_state *pkt)
237 unsigned int gso_type;
239 skb = xenvif_rx_dequeue(queue);
241 queue->stats.tx_bytes += skb->len;
242 queue->stats.tx_packets++;
244 /* Reset packet state. */
245 memset(pkt, 0, sizeof(struct xenvif_pkt_state));
248 pkt->frag_iter = skb;
249 pkt->remaining_len = skb->len;
252 gso_type = xenvif_gso_type(skb);
253 if ((1 << gso_type) & queue->vif->gso_mask) {
254 struct xen_netif_extra_info *extra;
256 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
258 extra->u.gso.type = gso_type;
259 extra->u.gso.size = skb_shinfo(skb)->gso_size;
260 extra->u.gso.pad = 0;
261 extra->u.gso.features = 0;
262 extra->type = XEN_NETIF_EXTRA_TYPE_GSO;
269 struct xen_netif_extra_info *extra;
271 extra = &pkt->extras[XEN_NETIF_EXTRA_TYPE_HASH - 1];
273 extra->u.hash.algorithm =
274 XEN_NETIF_CTRL_HASH_ALGORITHM_TOEPLITZ;
278 skb->protocol == htons(ETH_P_IP) ?
279 _XEN_NETIF_CTRL_HASH_TYPE_IPV4_TCP :
280 _XEN_NETIF_CTRL_HASH_TYPE_IPV6_TCP;
283 skb->protocol == htons(ETH_P_IP) ?
284 _XEN_NETIF_CTRL_HASH_TYPE_IPV4 :
285 _XEN_NETIF_CTRL_HASH_TYPE_IPV6;
287 *(uint32_t *)extra->u.hash.value = skb_get_hash_raw(skb);
289 extra->type = XEN_NETIF_EXTRA_TYPE_HASH;
296 static void xenvif_rx_complete(struct xenvif_queue *queue,
297 struct xenvif_pkt_state *pkt)
299 /* All responses are ready to be pushed. */
300 queue->rx.rsp_prod_pvt = queue->rx.req_cons;
302 __skb_queue_tail(queue->rx_copy.completed, pkt->skb);
305 static void xenvif_rx_next_frag(struct xenvif_pkt_state *pkt)
307 struct sk_buff *frag_iter = pkt->frag_iter;
308 unsigned int nr_frags = skb_shinfo(frag_iter)->nr_frags;
311 pkt->frag_offset = 0;
313 if (pkt->frag >= nr_frags) {
314 if (frag_iter == pkt->skb)
315 pkt->frag_iter = skb_shinfo(frag_iter)->frag_list;
317 pkt->frag_iter = frag_iter->next;
323 static void xenvif_rx_next_chunk(struct xenvif_queue *queue,
324 struct xenvif_pkt_state *pkt,
325 unsigned int offset, void **data,
328 struct sk_buff *frag_iter = pkt->frag_iter;
330 size_t frag_len, chunk_len;
334 if (pkt->frag == -1) {
335 frag_data = frag_iter->data;
336 frag_len = skb_headlen(frag_iter);
338 skb_frag_t *frag = &skb_shinfo(frag_iter)->frags[pkt->frag];
340 frag_data = skb_frag_address(frag);
341 frag_len = skb_frag_size(frag);
344 frag_data += pkt->frag_offset;
345 frag_len -= pkt->frag_offset;
347 chunk_len = min_t(size_t, frag_len, XEN_PAGE_SIZE - offset);
348 chunk_len = min_t(size_t, chunk_len, XEN_PAGE_SIZE -
349 xen_offset_in_page(frag_data));
351 pkt->frag_offset += chunk_len;
353 /* Advance to next frag? */
354 if (frag_len == chunk_len)
355 xenvif_rx_next_frag(pkt);
361 static void xenvif_rx_data_slot(struct xenvif_queue *queue,
362 struct xenvif_pkt_state *pkt,
363 struct xen_netif_rx_request *req,
364 struct xen_netif_rx_response *rsp)
366 unsigned int offset = 0;
373 xenvif_rx_next_chunk(queue, pkt, offset, &data, &len);
374 xenvif_rx_copy_add(queue, req, offset, data, len);
377 pkt->remaining_len -= len;
379 } while (offset < XEN_PAGE_SIZE && pkt->remaining_len > 0);
381 if (pkt->remaining_len > 0)
382 flags = XEN_NETRXF_more_data;
386 if (pkt->slot == 0) {
387 struct sk_buff *skb = pkt->skb;
389 if (skb->ip_summed == CHECKSUM_PARTIAL)
390 flags |= XEN_NETRXF_csum_blank |
391 XEN_NETRXF_data_validated;
392 else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
393 flags |= XEN_NETRXF_data_validated;
395 if (pkt->extra_count != 0)
396 flags |= XEN_NETRXF_extra_info;
402 rsp->status = (s16)offset;
405 static void xenvif_rx_extra_slot(struct xenvif_queue *queue,
406 struct xenvif_pkt_state *pkt,
407 struct xen_netif_rx_request *req,
408 struct xen_netif_rx_response *rsp)
410 struct xen_netif_extra_info *extra = (void *)rsp;
415 for (i = 0; i < ARRAY_SIZE(pkt->extras); i++) {
416 if (pkt->extras[i].type) {
417 *extra = pkt->extras[i];
419 if (pkt->extra_count != 0)
420 extra->flags |= XEN_NETIF_EXTRA_FLAG_MORE;
422 pkt->extras[i].type = 0;
429 void xenvif_rx_skb(struct xenvif_queue *queue)
431 struct xenvif_pkt_state pkt;
433 xenvif_rx_next_skb(queue, &pkt);
435 queue->last_rx_time = jiffies;
438 struct xen_netif_rx_request *req;
439 struct xen_netif_rx_response *rsp;
441 req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons);
442 rsp = RING_GET_RESPONSE(&queue->rx, queue->rx.req_cons);
444 /* Extras must go after the first data slot */
445 if (pkt.slot != 0 && pkt.extra_count != 0)
446 xenvif_rx_extra_slot(queue, &pkt, req, rsp);
448 xenvif_rx_data_slot(queue, &pkt, req, rsp);
450 queue->rx.req_cons++;
452 } while (pkt.remaining_len > 0 || pkt.extra_count != 0);
454 xenvif_rx_complete(queue, &pkt);
457 #define RX_BATCH_SIZE 64
459 void xenvif_rx_action(struct xenvif_queue *queue)
461 struct sk_buff_head completed_skbs;
462 unsigned int work_done = 0;
464 __skb_queue_head_init(&completed_skbs);
465 queue->rx_copy.completed = &completed_skbs;
467 while (xenvif_rx_ring_slots_available(queue) &&
468 work_done < RX_BATCH_SIZE) {
469 xenvif_rx_skb(queue);
473 /* Flush any pending copies and complete all skbs. */
474 xenvif_rx_copy_flush(queue);
477 static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
481 prod = queue->rx.sring->req_prod;
482 cons = queue->rx.req_cons;
484 return !queue->stalled &&
487 queue->last_rx_time + queue->vif->stall_timeout);
490 static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
494 prod = queue->rx.sring->req_prod;
495 cons = queue->rx.req_cons;
497 return queue->stalled && prod - cons >= 1;
500 bool xenvif_have_rx_work(struct xenvif_queue *queue, bool test_kthread)
502 return xenvif_rx_ring_slots_available(queue) ||
503 (queue->vif->stall_timeout &&
504 (xenvif_rx_queue_stalled(queue) ||
505 xenvif_rx_queue_ready(queue))) ||
506 (test_kthread && kthread_should_stop()) ||
507 queue->vif->disabled;
510 static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
515 skb = skb_peek(&queue->rx_queue);
517 return MAX_SCHEDULE_TIMEOUT;
519 timeout = XENVIF_RX_CB(skb)->expires - jiffies;
520 return timeout < 0 ? 0 : timeout;
523 /* Wait until the guest Rx thread has work.
525 * The timeout needs to be adjusted based on the current head of the
526 * queue (and not just the head at the beginning). In particular, if
527 * the queue is initially empty an infinite timeout is used and this
528 * needs to be reduced when a skb is queued.
530 * This cannot be done with wait_event_timeout() because it only
531 * calculates the timeout once.
533 static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
537 if (xenvif_have_rx_work(queue, true))
543 prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
544 if (xenvif_have_rx_work(queue, true))
546 if (atomic_fetch_andnot(NETBK_RX_EOI | NETBK_COMMON_EOI,
547 &queue->eoi_pending) &
548 (NETBK_RX_EOI | NETBK_COMMON_EOI))
549 xen_irq_lateeoi(queue->rx_irq, 0);
551 ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
555 finish_wait(&queue->wq, &wait);
558 static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
560 struct xenvif *vif = queue->vif;
562 queue->stalled = true;
564 /* At least one queue has stalled? Disable the carrier. */
565 spin_lock(&vif->lock);
566 if (vif->stalled_queues++ == 0) {
567 netdev_info(vif->dev, "Guest Rx stalled");
568 netif_carrier_off(vif->dev);
570 spin_unlock(&vif->lock);
573 static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
575 struct xenvif *vif = queue->vif;
577 queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
578 queue->stalled = false;
580 /* All queues are ready? Enable the carrier. */
581 spin_lock(&vif->lock);
582 if (--vif->stalled_queues == 0) {
583 netdev_info(vif->dev, "Guest Rx ready");
584 netif_carrier_on(vif->dev);
586 spin_unlock(&vif->lock);
589 int xenvif_kthread_guest_rx(void *data)
591 struct xenvif_queue *queue = data;
592 struct xenvif *vif = queue->vif;
594 if (!vif->stall_timeout)
595 xenvif_queue_carrier_on(queue);
598 xenvif_wait_for_rx_work(queue);
600 if (kthread_should_stop())
603 /* This frontend is found to be rogue, disable it in
604 * kthread context. Currently this is only set when
605 * netback finds out frontend sends malformed packet,
606 * but we cannot disable the interface in softirq
607 * context so we defer it here, if this thread is
608 * associated with queue 0.
610 if (unlikely(vif->disabled && queue->id == 0)) {
611 xenvif_carrier_off(vif);
615 if (!skb_queue_empty(&queue->rx_queue))
616 xenvif_rx_action(queue);
618 /* If the guest hasn't provided any Rx slots for a
619 * while it's probably not responsive, drop the
620 * carrier so packets are dropped earlier.
622 if (vif->stall_timeout) {
623 if (xenvif_rx_queue_stalled(queue))
624 xenvif_queue_carrier_off(queue);
625 else if (xenvif_rx_queue_ready(queue))
626 xenvif_queue_carrier_on(queue);
629 /* Queued packets may have foreign pages from other
630 * domains. These cannot be queued indefinitely as
631 * this would starve guests of grant refs and transmit
634 xenvif_rx_queue_drop_expired(queue);
639 /* Bin any remaining skbs */
640 xenvif_rx_queue_purge(queue);