Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp57258imm; Mon, 21 May 2018 02:07:58 -0700 (PDT) X-Google-Smtp-Source: AB8JxZrE9p4R3aOhhPfKcny/2TghwHfjhvrqXTv3IcH9Nf6sXJ8ydXCJFYfFXL+xD/MuE6giUaat X-Received: by 2002:a63:93:: with SMTP id 141-v6mr15421143pga.322.1526893678684; Mon, 21 May 2018 02:07:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526893678; cv=none; d=google.com; s=arc-20160816; b=ouxoFPVVAdDO7wY9Ph2x/bu+atdhIdwJn51SyJpEsQtSip5uFu1sL8MtN6QX+OqxZ0 Vco07HIaN3QZhxS8AbCvI03RHDyEOhNi9AdiEPHFpNzhLqwr1wc1ZWs/nyDq2C1g2zHk EA0zibqqCaznl/2D/JD7OFjUPoMUJALhiYCDnJHUoe4RCDNwfR3AWUflLlZVOhU+RiyW iqMtr0PEPUyVrNsJoMgfahJw/uuaso7ng6/YfjWL48rOQQE55VapXH4/Xlklssl/FW9K gmWoZfDjn9/EqoVQmmwSJI6lAPmPjGCZVE5JTCOnyi7DidB4q0mWwJ0eYcoTithGBJSm 7MMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=1oRW690eWPP0XYh8NFWz/+n21MbgutwbxtvUh/ehDew=; b=k+y7oe6YaGH9ME9UEMCYZ5KvnSX2/K0f09dcss0BH6LV7eYWbHoVHgfD+nREi9oy1K nVWpWh0/EafjSoh/Mn+IHFi2QJI3n8MTVSGLBLCfD3YuC7aVrhAMhmsYTLbE2pgNVSoT GiHRkwiCN0KPlj0iqVvdC3nBcTgjY9OBHYxRU/fE1m6C8d1vswHUsTxZ8UAQPfWU6PzB mWR+2PRMrPhlMTKFSDHKNq626gmOaDEvDbQxuS9JGfSAF5F+88YlEDQRdhSJkF5gk5n7 jfSudCjF3OntaGKfc3fWuWJ0+kZ845XCeFYyTR+upuML2c2jl4fKild+avsu/RS/VEpU MZnw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3-v6si10189567plq.56.2018.05.21.02.07.44; Mon, 21 May 2018 02:07:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752771AbeEUJFd (ORCPT + 99 others); Mon, 21 May 2018 05:05:33 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:54682 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751368AbeEUJFV (ORCPT ); Mon, 21 May 2018 05:05:21 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DD8E1400E9A7; Mon, 21 May 2018 09:05:20 +0000 (UTC) Received: from jason-ThinkPad-T450s.redhat.com (ovpn-12-30.pek2.redhat.com [10.72.12.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id B50844AF1; Mon, 21 May 2018 09:05:15 +0000 (UTC) From: Jason Wang To: mst@redhat.com, jasowang@redhat.com Cc: kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH net-next 09/12] tuntap: split out XDP logic Date: Mon, 21 May 2018 17:04:30 +0800 Message-Id: <1526893473-20128-10-git-send-email-jasowang@redhat.com> In-Reply-To: <1526893473-20128-1-git-send-email-jasowang@redhat.com> References: <1526893473-20128-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 21 May 2018 09:05:20 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Mon, 21 May 2018 09:05:20 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'jasowang@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch split out XDP logic into a single function. This make it to be reused by XDP batching path in the following patch. Signed-off-by: Jason Wang --- drivers/net/tun.c | 85 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/drivers/net/tun.c b/drivers/net/tun.c index ed04291..2560378 100644 --- a/drivers/net/tun.c +++ b/drivers/net/tun.c @@ -1605,6 +1605,44 @@ static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, return true; } +static u32 tun_do_xdp(struct tun_struct *tun, + struct tun_file *tfile, + struct bpf_prog *xdp_prog, + struct xdp_buff *xdp, + int *err) +{ + u32 act = bpf_prog_run_xdp(xdp_prog, xdp); + + switch (act) { + case XDP_REDIRECT: + *err = xdp_do_redirect(tun->dev, xdp, xdp_prog); + xdp_do_flush_map(); + if (*err) + break; + goto out; + case XDP_TX: + *err = tun_xdp_tx(tun->dev, xdp); + if (*err) + break; + tun_xdp_flush(tun->dev); + goto out; + case XDP_PASS: + goto out; + default: + bpf_warn_invalid_xdp_action(act); + /* fall through */ + case XDP_ABORTED: + trace_xdp_exception(tun->dev, xdp_prog, act); + /* fall through */ + case XDP_DROP: + break; + } + + put_page(virt_to_head_page(xdp->data_hard_start)); +out: + return act; +} + static struct sk_buff *tun_build_skb(struct tun_struct *tun, struct tun_file *tfile, struct iov_iter *from, @@ -1615,10 +1653,10 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, struct sk_buff *skb = NULL; struct bpf_prog *xdp_prog; int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); - unsigned int delta = 0; char *buf; size_t copied; - int err, pad = TUN_RX_PAD; + int pad = TUN_RX_PAD; + int err = 0; rcu_read_lock(); xdp_prog = rcu_dereference(tun->xdp_prog); @@ -1655,9 +1693,8 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, preempt_disable(); rcu_read_lock(); xdp_prog = rcu_dereference(tun->xdp_prog); - if (xdp_prog && !*skb_xdp) { + if (xdp_prog) { struct xdp_buff xdp; - void *orig_data; u32 act; xdp.data_hard_start = buf; @@ -1665,34 +1702,14 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, xdp_set_data_meta_invalid(&xdp); xdp.data_end = xdp.data + len; xdp.rxq = &tfile->xdp_rxq; - orig_data = xdp.data; - act = bpf_prog_run_xdp(xdp_prog, &xdp); - - switch (act) { - case XDP_REDIRECT: - err = xdp_do_redirect(tun->dev, &xdp, xdp_prog); - xdp_do_flush_map(); - if (err) - goto err_xdp; - goto out; - case XDP_TX: - if (tun_xdp_tx(tun->dev, &xdp)) - goto err_xdp; - tun_xdp_flush(tun->dev); - goto out; - case XDP_PASS: - delta = orig_data - xdp.data; - len = xdp.data_end - xdp.data; - break; - default: - bpf_warn_invalid_xdp_action(act); - /* fall through */ - case XDP_ABORTED: - trace_xdp_exception(tun->dev, xdp_prog, act); - /* fall through */ - case XDP_DROP: + act = tun_do_xdp(tun, tfile, xdp_prog, &xdp, &err); + if (err) goto err_xdp; - } + if (act != XDP_PASS) + goto out; + + pad = xdp.data - xdp.data_hard_start; + len = xdp.data_end - xdp.data; } rcu_read_unlock(); preempt_enable(); @@ -1700,18 +1717,18 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun, build: skb = build_skb(buf, buflen); if (!skb) { + put_page(alloc_frag->page); skb = ERR_PTR(-ENOMEM); goto out; } - skb_reserve(skb, pad - delta); + skb_reserve(skb, pad); skb_put(skb, len); return skb; err_xdp: - alloc_frag->offset -= buflen; - put_page(alloc_frag->page); + this_cpu_inc(tun->pcpu_stats->rx_dropped); out: rcu_read_unlock(); preempt_enable(); -- 2.7.4