From patchwork Thu Mar 19 13:13:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 222236 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01619C4332E for ; Thu, 19 Mar 2020 13:13:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C6AAC217D8 for ; Thu, 19 Mar 2020 13:13:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="GqVt+PZ9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727766AbgCSNNX (ORCPT ); Thu, 19 Mar 2020 09:13:23 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:41323 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727457AbgCSNNS (ORCPT ); Thu, 19 Mar 2020 09:13:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584623597; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FKAKULdXX3NFx+6MxVA4WzdMEWsVTjMw9Bd0ud5/okA=; b=GqVt+PZ9WFTpHGR/7X2YVEtsFJ3UZY277vYS8DjQ54JDnogfvlHdQjH78Zstg6qkIfyZGW eInBkwHUOaW0+GFBBvstDfj0lj7n9uN3ojHKCGoP2Sh+fnwCBio9BUgO0CGdcKi4fbougg jnspbM23GNW6xXPIWdst4c+eTHC+UHY= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-162-Kx9RQI7oOUG8fYE9AaGKTA-1; Thu, 19 Mar 2020 09:13:15 -0400 X-MC-Unique: Kx9RQI7oOUG8fYE9AaGKTA-1 Received: by mail-wm1-f70.google.com with SMTP id n188so685242wmf.0 for ; Thu, 19 Mar 2020 06:13:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=FKAKULdXX3NFx+6MxVA4WzdMEWsVTjMw9Bd0ud5/okA=; b=fb7Zj/H7WTWUpNjUIuBhrmhjuKle9J4aJEs60iZt+NjrXK595/6eXCBHRQ7X8D43G0 kkRkHAxtb0rJgRuPkIf1fzhq0p1i8xp9Qfil1M4LgqfKBUi9cq1VykFNP0EHIf9Chsee V1OFEhNUumIEzsoXYQFhN5mq5hLqN0yH2pqAUF6anBpvoUOfy8v1i3fM5kbTJIqKZObG KCQmr8V9+joZQ3mFSSLC1qjmsfhLvcrdaMWQYJ3iCSH7qhu6OvDRsdbjX4nZ49j3wFMQ NkagFQ2x2FJ8eKv6+JGJbYlYb7UbpnRYWzopa1HmuT3j4Xtt+uFVyCvnMv6SESlIxxMN K0oA== X-Gm-Message-State: ANhLgQ2KKewdDrbH1zjtdd8+T2M50WQx+889bt451eu+63htkBF+WUkK RMS0Dijv/K8zZgk4Pro3wcmn4i12Wk4zgqcLeZZlWmYN25Ydkbqjxj4NbUU4PxjPXpwlPJF0cZC JIR7Vjya++vckvLYB X-Received: by 2002:a5d:5089:: with SMTP id a9mr4323484wrt.187.1584623594474; Thu, 19 Mar 2020 06:13:14 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvnYxlGJTIbDXWeGk2deGexfPbab5uRwaBVl91k2hv/ZO57xVT+n3WEZmpNy8u3dfpZ+nIhTg== X-Received: by 2002:a5d:5089:: with SMTP id a9mr4323455wrt.187.1584623594269; Thu, 19 Mar 2020 06:13:14 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id b17sm3568133wrn.87.2020.03.19.06.13.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Mar 2020 06:13:13 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 3D56318038E; Thu, 19 Mar 2020 14:13:13 +0100 (CET) Subject: [PATCH bpf-next 1/4] xdp: Support specifying expected existing program when attaching XDP From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Lorenz Bauer , Andrey Ignatov , netdev@vger.kernel.org, bpf@vger.kernel.org Date: Thu, 19 Mar 2020 14:13:13 +0100 Message-ID: <158462359315.164779.13931660750493121404.stgit@toke.dk> In-Reply-To: <158462359206.164779.15902346296781033076.stgit@toke.dk> References: <158462359206.164779.15902346296781033076.stgit@toke.dk> User-Agent: StGit/0.22 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Toke Høiland-Jørgensen While it is currently possible for userspace to specify that an existing XDP program should not be replaced when attaching to an interface, there is no mechanism to safely replace a specific XDP program with another. This patch adds a new netlink attribute, IFLA_XDP_EXPECTED_FD, which can be set along with IFLA_XDP_FD. If set, the kernel will check that the program currently loaded on the interface matches the expected one, and fail the operation if it does not. This corresponds to a 'cmpxchg' memory operation. A new companion flag, XDP_FLAGS_EXPECT_FD, is also added to explicitly request checking of the EXPECTED_FD attribute. This is needed for userspace to discover whether the kernel supports the new attribute. Signed-off-by: Toke Høiland-Jørgensen --- include/linux/netdevice.h | 2 +- include/uapi/linux/if_link.h | 4 +++- net/core/dev.c | 25 ++++++++++++++++++++----- net/core/rtnetlink.c | 11 +++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index b6fedd54cd8e..40b12bd93913 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -3767,7 +3767,7 @@ struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, typedef int (*bpf_op_t)(struct net_device *dev, struct netdev_bpf *bpf); int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, - int fd, u32 flags); + int fd, int expected_fd, u32 flags); u32 __dev_xdp_query(struct net_device *dev, bpf_op_t xdp_op, enum bpf_netdev_command cmd); int xdp_umem_query(struct net_device *dev, u16 queue_id); diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h index 61e0801c82df..314173f8079e 100644 --- a/include/uapi/linux/if_link.h +++ b/include/uapi/linux/if_link.h @@ -972,11 +972,12 @@ enum { #define XDP_FLAGS_SKB_MODE (1U << 1) #define XDP_FLAGS_DRV_MODE (1U << 2) #define XDP_FLAGS_HW_MODE (1U << 3) +#define XDP_FLAGS_EXPECT_FD (1U << 4) #define XDP_FLAGS_MODES (XDP_FLAGS_SKB_MODE | \ XDP_FLAGS_DRV_MODE | \ XDP_FLAGS_HW_MODE) #define XDP_FLAGS_MASK (XDP_FLAGS_UPDATE_IF_NOEXIST | \ - XDP_FLAGS_MODES) + XDP_FLAGS_MODES | XDP_FLAGS_EXPECT_FD) /* These are stored into IFLA_XDP_ATTACHED on dump. */ enum { @@ -996,6 +997,7 @@ enum { IFLA_XDP_DRV_PROG_ID, IFLA_XDP_SKB_PROG_ID, IFLA_XDP_HW_PROG_ID, + IFLA_XDP_EXPECTED_FD, __IFLA_XDP_MAX, }; diff --git a/net/core/dev.c b/net/core/dev.c index 25dab1598803..44095326b8d5 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -8654,15 +8654,17 @@ static void dev_xdp_uninstall(struct net_device *dev) * @dev: device * @extack: netlink extended ack * @fd: new program fd or negative value to clear + * @expected_fd: old program fd that userspace expects to replace or clear * @flags: xdp-related flags * * Set or clear a bpf program for a device */ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, - int fd, u32 flags) + int fd, int expected_fd, u32 flags) { const struct net_device_ops *ops = dev->netdev_ops; enum bpf_netdev_command query; + u32 prog_id, expected_id = 0; struct bpf_prog *prog = NULL; bpf_op_t bpf_op, bpf_chk; bool offload; @@ -8683,15 +8685,28 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, if (bpf_op == bpf_chk) bpf_chk = generic_xdp_install; - if (fd >= 0) { - u32 prog_id; + prog_id = __dev_xdp_query(dev, bpf_op, query); + if (expected_fd >= 0 || (flags & XDP_FLAGS_EXPECT_FD)) { + if (expected_fd >= 0) { + prog = bpf_prog_get_type_dev(expected_fd, BPF_PROG_TYPE_XDP, + bpf_op == ops->ndo_bpf); + if (IS_ERR(prog)) + return PTR_ERR(prog); + expected_id = prog->aux->id; + bpf_prog_put(prog); + } + if (prog_id != expected_id) { + NL_SET_ERR_MSG(extack, "Active program does not match expected"); + return -EEXIST; + } + } + if (fd >= 0) { if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) { NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time"); return -EEXIST; } - prog_id = __dev_xdp_query(dev, bpf_op, query); if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) { NL_SET_ERR_MSG(extack, "XDP program already attached"); return -EBUSY; @@ -8714,7 +8729,7 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, return 0; } } else { - if (!__dev_xdp_query(dev, bpf_op, query)) + if (!prog_id) return 0; } diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c index 14e6ea21c378..09c08980f6f6 100644 --- a/net/core/rtnetlink.c +++ b/net/core/rtnetlink.c @@ -1873,6 +1873,7 @@ static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = { [IFLA_XDP_FD] = { .type = NLA_S32 }, + [IFLA_XDP_EXPECTED_FD] = { .type = NLA_S32 }, [IFLA_XDP_ATTACHED] = { .type = NLA_U8 }, [IFLA_XDP_FLAGS] = { .type = NLA_U32 }, [IFLA_XDP_PROG_ID] = { .type = NLA_U32 }, @@ -2799,8 +2800,18 @@ static int do_setlink(const struct sk_buff *skb, } if (xdp[IFLA_XDP_FD]) { + int expected_fd = -1; + + if (xdp[IFLA_XDP_EXPECTED_FD]) { + expected_fd = nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]); + } else if(xdp_flags & XDP_FLAGS_EXPECT_FD) { + err = -EINVAL; + goto errout; + } + err = dev_change_xdp_fd(dev, extack, nla_get_s32(xdp[IFLA_XDP_FD]), + expected_fd, xdp_flags); if (err) goto errout; From patchwork Thu Mar 19 13:13:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 222235 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 779A8C4332B for ; Thu, 19 Mar 2020 13:33:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 50318207FC for ; Thu, 19 Mar 2020 13:33:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YE4a3W58" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727735AbgCSNNW (ORCPT ); Thu, 19 Mar 2020 09:13:22 -0400 Received: from us-smtp-delivery-74.mimecast.com ([63.128.21.74]:27996 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728956AbgCSNNT (ORCPT ); Thu, 19 Mar 2020 09:13:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584623599; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uWJEiu/Z2FQlJcrWSPoV5kXif2wXXa5HAsBAGFtr6yE=; b=YE4a3W58KTnWwulu6K7NJZc0Q9av+ENxcGJxQgkmEBs1JRAZTlAL2EeHYS3NGp7j2/RhXp +XRuTHVUgNdqUDk8w2iOa8bqscp8jnh/PXUV7e6MDuwdskQqcE2ekPv3YrSP3BuhNFCOLz ug68m5Qw3u1H6vcVirvNf2Zpw8he/j8= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-284-wjZ6lfuOMGiMs1RNWF1gGw-1; Thu, 19 Mar 2020 09:13:17 -0400 X-MC-Unique: wjZ6lfuOMGiMs1RNWF1gGw-1 Received: by mail-wm1-f71.google.com with SMTP id m4so681962wmi.5 for ; Thu, 19 Mar 2020 06:13:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=uWJEiu/Z2FQlJcrWSPoV5kXif2wXXa5HAsBAGFtr6yE=; b=uZCTj88Yhc7exRqn0yxRo0WKto1qPmwEkEwW2isSuCLbyk7PQS2GAA6epN1WXigOl3 Fpl3L721iU0HkdY1Gt5h6ZWkBxgNKfX4agkapCsfuaLxXxruUhueSHYqxrgoGoSBRm3Y /jKa4ZLtVjxUzptlBtXEUqI4sYUBhfEC7D/ki6xxVkkQGof3g588DHWIl0/2qcNG7K4/ dMCQiiJ3NeO8R0SkBeuh+zoVlCIgUMFziMazUqgxd/PK8vuJrffqaFo3YhTsdQPO/VFk C2j625rQJsx7k0gcECmPAl3nY9odmfFYVzMeuJVf5Yxc82lHhTgBkuqvzEApRsafJ41e 75eQ== X-Gm-Message-State: ANhLgQ004lr3h4W4xbm7c6Oj+gHYbUnU6X7zEzbhRC1upEgYpyzrCZO1 CPqfpKxyqyvgm6d8no6QFt/VDux4FP8+yOeAUubQ3aVNm6kWrSwDmCfhZkACYsctkkf0qeSUOHi o5hiY2nTwMtoeZKnB X-Received: by 2002:a1c:6385:: with SMTP id x127mr3647788wmb.141.1584623596257; Thu, 19 Mar 2020 06:13:16 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt+Z7JUM/KQLOWds3rAEs1ALcIo+ruqq8ub+P9UEsTFzaY1gCY2UKTJFwmi0Sx1Eeh22JidQg== X-Received: by 2002:a1c:6385:: with SMTP id x127mr3647765wmb.141.1584623596070; Thu, 19 Mar 2020 06:13:16 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id p10sm3563491wrx.81.2020.03.19.06.13.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Mar 2020 06:13:14 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 493B9180371; Thu, 19 Mar 2020 14:13:14 +0100 (CET) Subject: [PATCH bpf-next 2/4] tools: Add EXPECTED_FD-related definitions in if_link.h From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Lorenz Bauer , Andrey Ignatov , netdev@vger.kernel.org, bpf@vger.kernel.org Date: Thu, 19 Mar 2020 14:13:14 +0100 Message-ID: <158462359425.164779.4804283164480162318.stgit@toke.dk> In-Reply-To: <158462359206.164779.15902346296781033076.stgit@toke.dk> References: <158462359206.164779.15902346296781033076.stgit@toke.dk> User-Agent: StGit/0.22 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Toke Høiland-Jørgensen This adds the IFLA_XDP_EXPECTED_FD netlink attribute definition and the XDP_FLAGS_EXPECT_FD flag to if_link.h in tools/include. Signed-off-by: Toke Høiland-Jørgensen --- tools/include/uapi/linux/if_link.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/include/uapi/linux/if_link.h b/tools/include/uapi/linux/if_link.h index 024af2d1d0af..e5eced1c28f4 100644 --- a/tools/include/uapi/linux/if_link.h +++ b/tools/include/uapi/linux/if_link.h @@ -960,11 +960,12 @@ enum { #define XDP_FLAGS_SKB_MODE (1U << 1) #define XDP_FLAGS_DRV_MODE (1U << 2) #define XDP_FLAGS_HW_MODE (1U << 3) +#define XDP_FLAGS_EXPECT_FD (1U << 4) #define XDP_FLAGS_MODES (XDP_FLAGS_SKB_MODE | \ XDP_FLAGS_DRV_MODE | \ XDP_FLAGS_HW_MODE) #define XDP_FLAGS_MASK (XDP_FLAGS_UPDATE_IF_NOEXIST | \ - XDP_FLAGS_MODES) + XDP_FLAGS_MODES | XDP_FLAGS_EXPECT_FD) /* These are stored into IFLA_XDP_ATTACHED on dump. */ enum { @@ -984,6 +985,7 @@ enum { IFLA_XDP_DRV_PROG_ID, IFLA_XDP_SKB_PROG_ID, IFLA_XDP_HW_PROG_ID, + IFLA_XDP_EXPECTED_FD, __IFLA_XDP_MAX, }; From patchwork Thu Mar 19 13:13:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= X-Patchwork-Id: 222234 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F842C4332B for ; Thu, 19 Mar 2020 13:33:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 159F020409 for ; Thu, 19 Mar 2020 13:33:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="WATt6Lso" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727640AbgCSNdm (ORCPT ); Thu, 19 Mar 2020 09:33:42 -0400 Received: from us-smtp-delivery-74.mimecast.com ([216.205.24.74]:54111 "EHLO us-smtp-delivery-74.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727911AbgCSNNW (ORCPT ); Thu, 19 Mar 2020 09:13:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1584623600; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/kUFoLbkDwJxBHQR60+eXJpgtbwabFaqSxr61HTPqBQ=; b=WATt6LsoHCb6JnwPC4FugBPcf34oV0T4HaPAGQEXex7ZyO1g9nxOhIsV1HK9+VrBUnLzop lJDUSxJ7gOuIMaWS9UlEgk5X2ECgaH7sPXpjACwiTy2bKTmg6FU8Vv/vaDeoYr6wHx2v66 EJoq/9ZTf4JFmcgIrtVXkuZ6wszdEAw= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-61--mtAwPDKOD6iirygXAW4sA-1; Thu, 19 Mar 2020 09:13:19 -0400 X-MC-Unique: -mtAwPDKOD6iirygXAW4sA-1 Received: by mail-wr1-f69.google.com with SMTP id q18so990565wrw.5 for ; Thu, 19 Mar 2020 06:13:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/kUFoLbkDwJxBHQR60+eXJpgtbwabFaqSxr61HTPqBQ=; b=FrBe+zsXPUVyif3aJctTTMCfxK2m/MT9sPEItmxlYs+AxFJ/K+cuWDHRjlXNIFSu/V eRG2KGKZQG1Sf3DL9tQsEUhXW/2YTttTADXJtj2eUBEZO2crwV0c8B2jJaoAKt+YfWEa 7yTTm9UDjkX7Ia2UzrJDkxw7MbriohcvIPO9IWUiM0DKnOyt94SCraTwpdJ0mVYm/5MJ V6dIrClqdyfWtf1OPcVo64WvmbLQ+7YZXsPBA9QngkKhl+yZJEb16yNVaq91K4P2xzxW yH7Jqw4mysA0NAY1/WDYYMCr8tYSNqK9WKCt+tHCZW+d4Sq6/jXfMRYuYyT2Nr9/GWcn yFjA== X-Gm-Message-State: ANhLgQ3cMGbQlYRDC4jpSxApd8ZMz3SXjJvjdiXcrOz7jCjETxsE+8HW 2iBi5dAWjsPrb8FVaAMD7bnTF58cV6PnK3Q4l2l7HrL4QhYcVPu8DxKvEi0tb/XYxhtEKCYqu+z +86i3fIyfk95K1G91 X-Received: by 2002:a1c:b144:: with SMTP id a65mr3924152wmf.54.1584623598213; Thu, 19 Mar 2020 06:13:18 -0700 (PDT) X-Google-Smtp-Source: ADFU+vtb4AFemoXjuxCguui6x5/tVwN8upTBqzpFjs8i3B/Neo6gumRxsaU0uvtR50TUfxR1z1K9zw== X-Received: by 2002:a1c:b144:: with SMTP id a65mr3924119wmf.54.1584623598017; Thu, 19 Mar 2020 06:13:18 -0700 (PDT) Received: from alrua-x1.borgediget.toke.dk ([45.145.92.2]) by smtp.gmail.com with ESMTPSA id f15sm3429776wru.83.2020.03.19.06.13.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Mar 2020 06:13:17 -0700 (PDT) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 621DE18038E; Thu, 19 Mar 2020 14:13:15 +0100 (CET) Subject: [PATCH bpf-next 3/4] libbpf: Add function to set link XDP fd while specifying old fd From: =?utf-8?q?Toke_H=C3=B8iland-J=C3=B8rgensen?= To: Alexei Starovoitov Cc: Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , "David S. Miller" , Jakub Kicinski , Jesper Dangaard Brouer , John Fastabend , Lorenz Bauer , Andrey Ignatov , netdev@vger.kernel.org, bpf@vger.kernel.org Date: Thu, 19 Mar 2020 14:13:15 +0100 Message-ID: <158462359530.164779.12468969809718921559.stgit@toke.dk> In-Reply-To: <158462359206.164779.15902346296781033076.stgit@toke.dk> References: <158462359206.164779.15902346296781033076.stgit@toke.dk> User-Agent: StGit/0.22 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Toke Høiland-Jørgensen This adds a new function to set the XDP fd while specifying the old fd to replace, using the newly added IFLA_XDP_EXPECTED_FD netlink parameter. Signed-off-by: Toke Høiland-Jørgensen --- tools/lib/bpf/libbpf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + tools/lib/bpf/netlink.c | 22 +++++++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index d38d7a629417..b5ca4f741e28 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -445,6 +445,8 @@ struct xdp_link_info { }; LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); +LIBBPF_API int bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, + __u32 flags); LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info, size_t info_size, __u32 flags); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 5129283c0284..154f1d94fa63 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -244,4 +244,5 @@ LIBBPF_0.0.8 { bpf_link__pin_path; bpf_link__unpin; bpf_program__set_attach_target; + bpf_set_link_xdp_fd_replace; } LIBBPF_0.0.7; diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c index 431bd25c6cdb..39bd0ead1546 100644 --- a/tools/lib/bpf/netlink.c +++ b/tools/lib/bpf/netlink.c @@ -132,7 +132,8 @@ static int bpf_netlink_recv(int sock, __u32 nl_pid, int seq, return ret; } -int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) +static int __bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, + __u32 flags) { int sock, seq = 0, ret; struct nlattr *nla, *nla_xdp; @@ -178,6 +179,14 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) nla->nla_len += nla_xdp->nla_len; } + if (flags & XDP_FLAGS_EXPECT_FD) { + nla_xdp = (struct nlattr *)((char *)nla + nla->nla_len); + nla_xdp->nla_type = IFLA_XDP_EXPECTED_FD; + nla_xdp->nla_len = NLA_HDRLEN + sizeof(int); + memcpy((char *)nla_xdp + NLA_HDRLEN, &old_fd, sizeof(old_fd)); + nla->nla_len += nla_xdp->nla_len; + } + req.nh.nlmsg_len += NLA_ALIGN(nla->nla_len); if (send(sock, &req, req.nh.nlmsg_len, 0) < 0) { @@ -191,6 +200,17 @@ int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) return ret; } +int bpf_set_link_xdp_fd_replace(int ifindex, int fd, int old_fd, __u32 flags) +{ + return __bpf_set_link_xdp_fd_replace(ifindex, fd, old_fd, + flags | XDP_FLAGS_EXPECT_FD); +} + +int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags) +{ + return __bpf_set_link_xdp_fd_replace(ifindex, fd, -1, flags); +} + static int __dump_link_nlmsg(struct nlmsghdr *nlh, libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie) {