Commit 05296620 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Daniel Borkmann

xdp: factor out common program/flags handling from drivers

Basic operations drivers perform during xdp setup and query can
be moved to helpers in the core.  Encapsulate program and flags
into a structure and add helpers.  Note that the structure is
intended as the "main" program information source in the driver.
Most drivers will additionally place the program pointer in their
fast path or ring structures.

The helpers don't have a huge impact now, but they will
decrease the code duplication when programs can be installed
in HW and driver at the same time.  Encapsulating the basic
operations in helpers will hopefully also reduce the number
of changes to drivers which adopt them.

Helpers could really be static inline, but they depend on
definition of struct netdev_bpf which means they'd have
to be placed in netdevice.h, an already 4500 line header.
Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 6b867589
...@@ -553,8 +553,7 @@ struct nfp_net_dp { ...@@ -553,8 +553,7 @@ struct nfp_net_dp {
* @rss_cfg: RSS configuration * @rss_cfg: RSS configuration
* @rss_key: RSS secret key * @rss_key: RSS secret key
* @rss_itbl: RSS indirection table * @rss_itbl: RSS indirection table
* @xdp_flags: Flags with which XDP prog was loaded * @xdp: Information about the attached XDP program
* @xdp_prog: XDP prog (for ctrl path, both DRV and HW modes)
* @max_r_vecs: Number of allocated interrupt vectors for RX/TX * @max_r_vecs: Number of allocated interrupt vectors for RX/TX
* @max_tx_rings: Maximum number of TX rings supported by the Firmware * @max_tx_rings: Maximum number of TX rings supported by the Firmware
* @max_rx_rings: Maximum number of RX rings supported by the Firmware * @max_rx_rings: Maximum number of RX rings supported by the Firmware
...@@ -610,8 +609,7 @@ struct nfp_net { ...@@ -610,8 +609,7 @@ struct nfp_net {
u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ]; u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ]; u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];
u32 xdp_flags; struct xdp_attachment_info xdp;
struct bpf_prog *xdp_prog;
unsigned int max_tx_rings; unsigned int max_tx_rings;
unsigned int max_rx_rings; unsigned int max_rx_rings;
......
...@@ -3417,34 +3417,29 @@ nfp_net_xdp_setup_drv(struct nfp_net *nn, struct bpf_prog *prog, ...@@ -3417,34 +3417,29 @@ nfp_net_xdp_setup_drv(struct nfp_net *nn, struct bpf_prog *prog,
return nfp_net_ring_reconfig(nn, dp, extack); return nfp_net_ring_reconfig(nn, dp, extack);
} }
static int static int nfp_net_xdp_setup(struct nfp_net *nn, struct netdev_bpf *bpf)
nfp_net_xdp_setup(struct nfp_net *nn, struct bpf_prog *prog, u32 flags,
struct netlink_ext_ack *extack)
{ {
struct bpf_prog *drv_prog, *offload_prog; struct bpf_prog *drv_prog, *offload_prog;
int err; int err;
if (nn->xdp_prog && (flags ^ nn->xdp_flags) & XDP_FLAGS_MODES) if (!xdp_attachment_flags_ok(&nn->xdp, bpf))
return -EBUSY; return -EBUSY;
/* Load both when no flags set to allow easy activation of driver path /* Load both when no flags set to allow easy activation of driver path
* when program is replaced by one which can't be offloaded. * when program is replaced by one which can't be offloaded.
*/ */
drv_prog = flags & XDP_FLAGS_HW_MODE ? NULL : prog; drv_prog = bpf->flags & XDP_FLAGS_HW_MODE ? NULL : bpf->prog;
offload_prog = flags & XDP_FLAGS_DRV_MODE ? NULL : prog; offload_prog = bpf->flags & XDP_FLAGS_DRV_MODE ? NULL : bpf->prog;
err = nfp_net_xdp_setup_drv(nn, drv_prog, extack); err = nfp_net_xdp_setup_drv(nn, drv_prog, bpf->extack);
if (err) if (err)
return err; return err;
err = nfp_app_xdp_offload(nn->app, nn, offload_prog, extack); err = nfp_app_xdp_offload(nn->app, nn, offload_prog, bpf->extack);
if (err && flags & XDP_FLAGS_HW_MODE) if (err && bpf->flags & XDP_FLAGS_HW_MODE)
return err; return err;
if (nn->xdp_prog) xdp_attachment_setup(&nn->xdp, bpf);
bpf_prog_put(nn->xdp_prog);
nn->xdp_prog = prog;
nn->xdp_flags = flags;
return 0; return 0;
} }
...@@ -3456,12 +3451,9 @@ static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp) ...@@ -3456,12 +3451,9 @@ static int nfp_net_xdp(struct net_device *netdev, struct netdev_bpf *xdp)
switch (xdp->command) { switch (xdp->command) {
case XDP_SETUP_PROG: case XDP_SETUP_PROG:
case XDP_SETUP_PROG_HW: case XDP_SETUP_PROG_HW:
return nfp_net_xdp_setup(nn, xdp->prog, xdp->flags, return nfp_net_xdp_setup(nn, xdp);
xdp->extack);
case XDP_QUERY_PROG: case XDP_QUERY_PROG:
xdp->prog_id = nn->xdp_prog ? nn->xdp_prog->aux->id : 0; return xdp_attachment_query(&nn->xdp, xdp);
xdp->prog_flags = nn->xdp_prog ? nn->xdp_flags : 0;
return 0;
default: default:
return nfp_app_bpf(nn->app, nn, xdp); return nfp_app_bpf(nn->app, nn, xdp);
} }
......
...@@ -199,10 +199,8 @@ static int nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf) ...@@ -199,10 +199,8 @@ static int nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf)
{ {
int err; int err;
if (ns->xdp_prog && (bpf->flags ^ ns->xdp_flags) & XDP_FLAGS_MODES) { if (!xdp_attachment_flags_ok(&ns->xdp, bpf))
NSIM_EA(bpf->extack, "program loaded with different flags");
return -EBUSY; return -EBUSY;
}
if (bpf->command == XDP_SETUP_PROG && !ns->bpf_xdpdrv_accept) { if (bpf->command == XDP_SETUP_PROG && !ns->bpf_xdpdrv_accept) {
NSIM_EA(bpf->extack, "driver XDP disabled in DebugFS"); NSIM_EA(bpf->extack, "driver XDP disabled in DebugFS");
...@@ -219,11 +217,7 @@ static int nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf) ...@@ -219,11 +217,7 @@ static int nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf)
return err; return err;
} }
if (ns->xdp_prog) xdp_attachment_setup(&ns->xdp, bpf);
bpf_prog_put(ns->xdp_prog);
ns->xdp_prog = bpf->prog;
ns->xdp_flags = bpf->flags;
if (!bpf->prog) if (!bpf->prog)
ns->xdp_prog_mode = XDP_ATTACHED_NONE; ns->xdp_prog_mode = XDP_ATTACHED_NONE;
...@@ -567,9 +561,7 @@ int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf) ...@@ -567,9 +561,7 @@ int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
nsim_bpf_destroy_prog(bpf->offload.prog); nsim_bpf_destroy_prog(bpf->offload.prog);
return 0; return 0;
case XDP_QUERY_PROG: case XDP_QUERY_PROG:
bpf->prog_id = ns->xdp_prog ? ns->xdp_prog->aux->id : 0; return xdp_attachment_query(&ns->xdp, bpf);
bpf->prog_flags = ns->xdp_prog ? ns->xdp_flags : 0;
return 0;
case XDP_SETUP_PROG: case XDP_SETUP_PROG:
err = nsim_setup_prog_checks(ns, bpf); err = nsim_setup_prog_checks(ns, bpf);
if (err) if (err)
...@@ -636,6 +628,6 @@ void nsim_bpf_uninit(struct netdevsim *ns) ...@@ -636,6 +628,6 @@ void nsim_bpf_uninit(struct netdevsim *ns)
{ {
WARN_ON(!list_empty(&ns->bpf_bound_progs)); WARN_ON(!list_empty(&ns->bpf_bound_progs));
WARN_ON(!list_empty(&ns->bpf_bound_maps)); WARN_ON(!list_empty(&ns->bpf_bound_maps));
WARN_ON(ns->xdp_prog); WARN_ON(ns->xdp.prog);
WARN_ON(ns->bpf_offloaded); WARN_ON(ns->bpf_offloaded);
} }
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <linux/list.h> #include <linux/list.h>
#include <linux/netdevice.h> #include <linux/netdevice.h>
#include <linux/u64_stats_sync.h> #include <linux/u64_stats_sync.h>
#include <net/xdp.h>
#define DRV_NAME "netdevsim" #define DRV_NAME "netdevsim"
...@@ -67,9 +68,8 @@ struct netdevsim { ...@@ -67,9 +68,8 @@ struct netdevsim {
struct bpf_prog *bpf_offloaded; struct bpf_prog *bpf_offloaded;
u32 bpf_offloaded_id; u32 bpf_offloaded_id;
u32 xdp_flags; struct xdp_attachment_info xdp;
int xdp_prog_mode; int xdp_prog_mode;
struct bpf_prog *xdp_prog;
u32 prog_id_gen; u32 prog_id_gen;
......
...@@ -144,4 +144,17 @@ xdp_data_meta_unsupported(const struct xdp_buff *xdp) ...@@ -144,4 +144,17 @@ xdp_data_meta_unsupported(const struct xdp_buff *xdp)
return unlikely(xdp->data_meta > xdp->data); return unlikely(xdp->data_meta > xdp->data);
} }
struct xdp_attachment_info {
struct bpf_prog *prog;
u32 flags;
};
struct netdev_bpf;
int xdp_attachment_query(struct xdp_attachment_info *info,
struct netdev_bpf *bpf);
bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
struct netdev_bpf *bpf);
void xdp_attachment_setup(struct xdp_attachment_info *info,
struct netdev_bpf *bpf);
#endif /* __LINUX_NET_XDP_H__ */ #endif /* __LINUX_NET_XDP_H__ */
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
* Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc. * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
* Released under terms in GPL version 2. See COPYING. * Released under terms in GPL version 2. See COPYING.
*/ */
#include <linux/bpf.h>
#include <linux/filter.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/netdevice.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/rhashtable.h> #include <linux/rhashtable.h>
...@@ -370,3 +373,34 @@ void xdp_return_buff(struct xdp_buff *xdp) ...@@ -370,3 +373,34 @@ void xdp_return_buff(struct xdp_buff *xdp)
__xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle); __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle);
} }
EXPORT_SYMBOL_GPL(xdp_return_buff); EXPORT_SYMBOL_GPL(xdp_return_buff);
int xdp_attachment_query(struct xdp_attachment_info *info,
struct netdev_bpf *bpf)
{
bpf->prog_id = info->prog ? info->prog->aux->id : 0;
bpf->prog_flags = info->prog ? info->flags : 0;
return 0;
}
EXPORT_SYMBOL_GPL(xdp_attachment_query);
bool xdp_attachment_flags_ok(struct xdp_attachment_info *info,
struct netdev_bpf *bpf)
{
if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) {
NL_SET_ERR_MSG(bpf->extack,
"program loaded with different flags");
return false;
}
return true;
}
EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok);
void xdp_attachment_setup(struct xdp_attachment_info *info,
struct netdev_bpf *bpf)
{
if (info->prog)
bpf_prog_put(info->prog);
info->prog = bpf->prog;
info->flags = bpf->flags;
}
EXPORT_SYMBOL_GPL(xdp_attachment_setup);
...@@ -821,7 +821,7 @@ try: ...@@ -821,7 +821,7 @@ try:
ret, _, err = sim.set_xdp(obj, "", force=True, ret, _, err = sim.set_xdp(obj, "", force=True,
fail=False, include_stderr=True) fail=False, include_stderr=True)
fail(ret == 0, "Replaced XDP program with a program in different mode") fail(ret == 0, "Replaced XDP program with a program in different mode")
check_extack_nsim(err, "program loaded with different flags.", args) check_extack(err, "program loaded with different flags.", args)
start_test("Test XDP prog remove with bad flags...") start_test("Test XDP prog remove with bad flags...")
ret, _, err = sim.unset_xdp("offload", force=True, ret, _, err = sim.unset_xdp("offload", force=True,
...@@ -831,7 +831,7 @@ try: ...@@ -831,7 +831,7 @@ try:
ret, _, err = sim.unset_xdp("", force=True, ret, _, err = sim.unset_xdp("", force=True,
fail=False, include_stderr=True) fail=False, include_stderr=True)
fail(ret == 0, "Removed program with a bad mode") fail(ret == 0, "Removed program with a bad mode")
check_extack_nsim(err, "program loaded with different flags.", args) check_extack(err, "program loaded with different flags.", args)
start_test("Test MTU restrictions...") start_test("Test MTU restrictions...")
ret, _ = sim.set_mtu(9000, fail=False) ret, _ = sim.set_mtu(9000, fail=False)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment