From patchwork Fri Jul 15 23:28:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brenden Blanco X-Patchwork-Id: 649040 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3rrpgk3ChMz9sCZ for ; Sat, 16 Jul 2016 09:29:38 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=plumgrid-com.20150623.gappssmtp.com header.i=@plumgrid-com.20150623.gappssmtp.com header.b=dOX+KM+S; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751822AbcGOX3d (ORCPT ); Fri, 15 Jul 2016 19:29:33 -0400 Received: from mail-pf0-f172.google.com ([209.85.192.172]:34094 "EHLO mail-pf0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751799AbcGOX33 (ORCPT ); Fri, 15 Jul 2016 19:29:29 -0400 Received: by mail-pf0-f172.google.com with SMTP id p64so11684889pfb.1 for ; Fri, 15 Jul 2016 16:29:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plumgrid-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=E0JgDC2QJWvZ7/1BhuWlKbyCkUV2i+JyHQjlVUpontE=; b=dOX+KM+Sfel2lnzMoAzrg+t52OyC1pio8hpLqyfVSk3P5H22610b+SWpj5xsqgIDV5 +OvAwxfj8Lop7XzOfPJ1UTmnaGv4BR3e7O7mmbvx8OfiVORm+DrbU3uBnUdDVp59Dj8o zkhdubk20nsg19azPtmaVWgkej+lkl0FLix3bFDRf9Y7TupX2rAURVKgSL+MnvsElh+C hQoOUM070vGNuoFa2vvyMxeKrPfI/eqERk4uIAScazOclPohM1E4DfZ44l2kyuGxRM1M 5G/NEtazq1YttkmZ5K4ZrWkFcRxYAPEmqmFPngx27FDPDKYpdumOkTobOiklxQuh0x8g oykA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=E0JgDC2QJWvZ7/1BhuWlKbyCkUV2i+JyHQjlVUpontE=; b=QU3O9wOlZMZPyw4I50taBTmNtpUMtt32/KzaM6CD+kMpPlzaoXU3ghclLwp0q/oqNE tDCmsYrHpi4GoLkLBi3DK2BAxr7I4nEqo8Enus4oziHYdFsHSNd2kBWeAg02uMiFJbBg oSLVN7w4bmoLRLhCtflBGTM+sPV3DgznxiqXK3q9Zg33T5axayK4K5o7f+/Py7SEHNdj yucG02C6md/P7xmd1Ba4Gb42EPQzPqGQmUTTM5cwWPjdA3CM3eSsONn7vkgK3YO6BhaW CWUoPfoJkTxZCnqvQvdlTB1VL7ARsVezVJT/wUROq2kNcl0xb095l1q1qz4phuwJR7r/ UgIA== X-Gm-Message-State: ALyK8tIDx4xnWFxfvLccLLJSFYqoXPkxT4hnx84GsD89op+SgMesMgikW9hR/IGOFUlSNC7L X-Received: by 10.98.29.201 with SMTP id d192mr19715392pfd.142.1468625368332; Fri, 15 Jul 2016 16:29:28 -0700 (PDT) Received: from iovisor-test1.plumgrid.com ([12.97.19.201]) by smtp.gmail.com with ESMTPSA id nd4sm6990993pab.14.2016.07.15.16.29.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 15 Jul 2016 16:29:27 -0700 (PDT) From: Brenden Blanco To: davem@davemloft.net, netdev@vger.kernel.org Cc: Brenden Blanco , Jamal Hadi Salim , Saeed Mahameed , Martin KaFai Lau , Jesper Dangaard Brouer , Ari Saha , Alexei Starovoitov , Or Gerlitz , john.fastabend@gmail.com, hannes@stressinduktion.org, Thomas Graf , Tom Herbert , Daniel Borkmann Subject: [PATCH v9 02/11] net: add ndo to setup/query xdp prog in adapter rx Date: Fri, 15 Jul 2016 16:28:49 -0700 Message-Id: <1468625338-32391-3-git-send-email-bblanco@plumgrid.com> X-Mailer: git-send-email 2.8.2 In-Reply-To: <1468625338-32391-1-git-send-email-bblanco@plumgrid.com> References: <1468625338-32391-1-git-send-email-bblanco@plumgrid.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add one new netdev op for drivers implementing the BPF_PROG_TYPE_XDP filter. The single op is used for both setup/query of the xdp program, modelled after ndo_setup_tc. Signed-off-by: Brenden Blanco --- include/linux/netdevice.h | 34 ++++++++++++++++++++++++++++++++++ net/core/dev.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h index 49736a3..fab9a1c 100644 --- a/include/linux/netdevice.h +++ b/include/linux/netdevice.h @@ -63,6 +63,7 @@ struct wpan_dev; struct mpls_dev; /* UDP Tunnel offloads */ struct udp_tunnel_info; +struct bpf_prog; void netdev_set_default_ethtool_ops(struct net_device *dev, const struct ethtool_ops *ops); @@ -799,6 +800,33 @@ struct tc_to_netdev { }; }; +/* These structures hold the attributes of xdp state that are being passed + * to the netdevice through the xdp op. + */ +enum xdp_netdev_command { + /* Set or clear a bpf program used in the earliest stages of packet + * rx. The prog will have been loaded as BPF_PROG_TYPE_XDP. The callee + * is responsible for calling bpf_prog_put on any old progs that are + * stored. In case of error, the callee need not release the new prog + * reference, but on success it takes ownership and must bpf_prog_put + * when it is no longer used. + */ + XDP_SETUP_PROG, + /* Check if a bpf program is set on the device. The callee should + * return true if a program is currently attached and running. + */ + XDP_QUERY_PROG, +}; + +struct netdev_xdp { + enum xdp_netdev_command command; + union { + /* XDP_SETUP_PROG */ + struct bpf_prog *prog; + /* XDP_QUERY_PROG */ + bool prog_attached; + }; +}; /* * This structure defines the management hooks for network devices. @@ -1087,6 +1115,9 @@ struct tc_to_netdev { * appropriate rx headroom value allows avoiding skb head copy on * forward. Setting a negative value resets the rx headroom to the * default value. + * int (*ndo_xdp)(struct net_device *dev, struct netdev_xdp *xdp); + * This function is used to set or query state related to XDP on the + * netdevice. See definition of enum xdp_netdev_command for details. * */ struct net_device_ops { @@ -1271,6 +1302,8 @@ struct net_device_ops { struct sk_buff *skb); void (*ndo_set_rx_headroom)(struct net_device *dev, int needed_headroom); + int (*ndo_xdp)(struct net_device *dev, + struct netdev_xdp *xdp); }; /** @@ -3257,6 +3290,7 @@ int dev_get_phys_port_id(struct net_device *dev, int dev_get_phys_port_name(struct net_device *dev, char *name, size_t len); int dev_change_proto_down(struct net_device *dev, bool proto_down); +int dev_change_xdp_fd(struct net_device *dev, int fd); struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev); struct sk_buff *dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, struct netdev_queue *txq, int *ret); diff --git a/net/core/dev.c b/net/core/dev.c index 7894e40..2a9c39f 100644 --- a/net/core/dev.c +++ b/net/core/dev.c @@ -94,6 +94,7 @@ #include #include #include +#include #include #include #include @@ -6615,6 +6616,38 @@ int dev_change_proto_down(struct net_device *dev, bool proto_down) EXPORT_SYMBOL(dev_change_proto_down); /** + * dev_change_xdp_fd - set or clear a bpf program for a device rx path + * @dev: device + * @fd: new program fd or negative value to clear + * + * Set or clear a bpf program for a device + */ +int dev_change_xdp_fd(struct net_device *dev, int fd) +{ + const struct net_device_ops *ops = dev->netdev_ops; + struct bpf_prog *prog = NULL; + struct netdev_xdp xdp = {}; + int err; + + if (!ops->ndo_xdp) + return -EOPNOTSUPP; + if (fd >= 0) { + prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP); + if (IS_ERR(prog)) + return PTR_ERR(prog); + } + + xdp.command = XDP_SETUP_PROG; + xdp.prog = prog; + err = ops->ndo_xdp(dev, &xdp); + if (err < 0 && prog) + bpf_prog_put(prog); + + return err; +} +EXPORT_SYMBOL(dev_change_xdp_fd); + +/** * dev_new_index - allocate an ifindex * @net: the applicable net namespace *