Commit 8c5a3ca3 authored by David S. Miller's avatar David S. Miller
Browse files

Merge branch 'netdevsim-Mostly-cleanup-in-sdev-bpf-iface-area'



Jiri Pirko says:

====================
netdevsim: Mostly cleanup in sdev/bpf iface area

This patches does mainly internal netdevsim code shuffle. Nothing
serious, just small changes to help readability and preparations for
future work.
====================

Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 3c91d114 4b3a84bc
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
obj-$(CONFIG_NETDEVSIM) += netdevsim.o

netdevsim-objs := \
	netdev.o devlink.o fib.o \
	netdev.o devlink.o fib.o sdev.o \

ifeq ($(CONFIG_BPF_SYSCALL),y)
netdevsim-objs += \
+51 −36
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
	bpf_verifier_log_write(env, "[netdevsim] " fmt, ##__VA_ARGS__)

struct nsim_bpf_bound_prog {
	struct netdevsim *ns;
	struct netdevsim_shared_dev *sdev;
	struct bpf_prog *prog;
	struct dentry *ddir;
	const char *state;
@@ -65,8 +65,8 @@ nsim_bpf_verify_insn(struct bpf_verifier_env *env, int insn_idx, int prev_insn)
	struct nsim_bpf_bound_prog *state;

	state = env->prog->aux->offload->dev_priv;
	if (state->ns->bpf_bind_verifier_delay && !insn_idx)
		msleep(state->ns->bpf_bind_verifier_delay);
	if (state->sdev->bpf_bind_verifier_delay && !insn_idx)
		msleep(state->sdev->bpf_bind_verifier_delay);

	if (insn_idx == env->prog->len - 1)
		pr_vlog(env, "Hello from netdevsim!\n");
@@ -213,7 +213,8 @@ nsim_xdp_set_prog(struct netdevsim *ns, struct netdev_bpf *bpf,
	return 0;
}

static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog)
static int nsim_bpf_create_prog(struct netdevsim_shared_dev *sdev,
				struct bpf_prog *prog)
{
	struct nsim_bpf_bound_prog *state;
	char name[16];
@@ -222,13 +223,13 @@ static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog)
	if (!state)
		return -ENOMEM;

	state->ns = ns;
	state->sdev = sdev;
	state->prog = prog;
	state->state = "verify";

	/* Program id is not populated yet when we create the state. */
	sprintf(name, "%u", ns->sdev->prog_id_gen++);
	state->ddir = debugfs_create_dir(name, ns->sdev->ddir_bpf_bound_progs);
	sprintf(name, "%u", sdev->prog_id_gen++);
	state->ddir = debugfs_create_dir(name, sdev->ddir_bpf_bound_progs);
	if (IS_ERR_OR_NULL(state->ddir)) {
		kfree(state);
		return -ENOMEM;
@@ -239,7 +240,7 @@ static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog)
			    &state->state, &nsim_bpf_string_fops);
	debugfs_create_bool("loaded", 0400, state->ddir, &state->is_loaded);

	list_add_tail(&state->l, &ns->sdev->bpf_bound_progs);
	list_add_tail(&state->l, &sdev->bpf_bound_progs);

	prog->aux->offload->dev_priv = state;

@@ -248,12 +249,13 @@ static int nsim_bpf_create_prog(struct netdevsim *ns, struct bpf_prog *prog)

static int nsim_bpf_verifier_prep(struct bpf_prog *prog)
{
	struct netdevsim *ns = bpf_offload_dev_priv(prog->aux->offload->offdev);
	struct netdevsim_shared_dev *sdev =
			bpf_offload_dev_priv(prog->aux->offload->offdev);

	if (!ns->bpf_bind_accept)
	if (!sdev->bpf_bind_accept)
		return -EOPNOTSUPP;

	return nsim_bpf_create_prog(ns, prog);
	return nsim_bpf_create_prog(sdev, prog);
}

static int nsim_bpf_translate(struct bpf_prog *prog)
@@ -576,39 +578,55 @@ int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
	}
}

int nsim_bpf_init(struct netdevsim *ns)
static int nsim_bpf_sdev_init(struct netdevsim_shared_dev *sdev)
{
	int err;

	if (ns->sdev->refcnt == 1) {
		INIT_LIST_HEAD(&ns->sdev->bpf_bound_progs);
		INIT_LIST_HEAD(&ns->sdev->bpf_bound_maps);
	INIT_LIST_HEAD(&sdev->bpf_bound_progs);
	INIT_LIST_HEAD(&sdev->bpf_bound_maps);

		ns->sdev->ddir_bpf_bound_progs =
			debugfs_create_dir("bpf_bound_progs", ns->sdev->ddir);
		if (IS_ERR_OR_NULL(ns->sdev->ddir_bpf_bound_progs))
	sdev->ddir_bpf_bound_progs =
		debugfs_create_dir("bpf_bound_progs", sdev->ddir);
	if (IS_ERR_OR_NULL(sdev->ddir_bpf_bound_progs))
		return -ENOMEM;

		ns->sdev->bpf_dev = bpf_offload_dev_create(&nsim_bpf_dev_ops,
							   ns);
		err = PTR_ERR_OR_ZERO(ns->sdev->bpf_dev);
	sdev->bpf_dev = bpf_offload_dev_create(&nsim_bpf_dev_ops, sdev);
	err = PTR_ERR_OR_ZERO(sdev->bpf_dev);
	if (err)
		return err;

	sdev->bpf_bind_accept = true;
	debugfs_create_bool("bpf_bind_accept", 0600, sdev->ddir,
			    &sdev->bpf_bind_accept);
	debugfs_create_u32("bpf_bind_verifier_delay", 0600, sdev->ddir,
			   &sdev->bpf_bind_verifier_delay);
	return 0;
}

static void nsim_bpf_sdev_uninit(struct netdevsim_shared_dev *sdev)
{
	WARN_ON(!list_empty(&sdev->bpf_bound_progs));
	WARN_ON(!list_empty(&sdev->bpf_bound_maps));
	bpf_offload_dev_destroy(sdev->bpf_dev);
}

int nsim_bpf_init(struct netdevsim *ns)
{
	int err;

	if (ns->sdev->refcnt == 1) {
		err = nsim_bpf_sdev_init(ns->sdev);
		if (err)
			return err;
	}

	err = bpf_offload_dev_netdev_register(ns->sdev->bpf_dev, ns->netdev);
	if (err)
		goto err_destroy_bdev;
		goto err_bpf_sdev_uninit;

	debugfs_create_u32("bpf_offloaded_id", 0400, ns->ddir,
			   &ns->bpf_offloaded_id);

	ns->bpf_bind_accept = true;
	debugfs_create_bool("bpf_bind_accept", 0600, ns->ddir,
			    &ns->bpf_bind_accept);
	debugfs_create_u32("bpf_bind_verifier_delay", 0600, ns->ddir,
			   &ns->bpf_bind_verifier_delay);

	ns->bpf_tc_accept = true;
	debugfs_create_bool("bpf_tc_accept", 0600, ns->ddir,
			    &ns->bpf_tc_accept);
@@ -627,9 +645,9 @@ int nsim_bpf_init(struct netdevsim *ns)

	return 0;

err_destroy_bdev:
err_bpf_sdev_uninit:
	if (ns->sdev->refcnt == 1)
		bpf_offload_dev_destroy(ns->sdev->bpf_dev);
		nsim_bpf_sdev_uninit(ns->sdev);
	return err;
}

@@ -640,9 +658,6 @@ void nsim_bpf_uninit(struct netdevsim *ns)
	WARN_ON(ns->bpf_offloaded);
	bpf_offload_dev_netdev_unregister(ns->sdev->bpf_dev, ns->netdev);

	if (ns->sdev->refcnt == 1) {
		WARN_ON(!list_empty(&ns->sdev->bpf_bound_progs));
		WARN_ON(!list_empty(&ns->sdev->bpf_bound_maps));
		bpf_offload_dev_destroy(ns->sdev->bpf_dev);
	}
	if (ns->sdev->refcnt == 1)
		nsim_bpf_sdev_uninit(ns->sdev);
}
+27 −49
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@

#include "netdevsim.h"

static u32 nsim_dev_id;

struct nsim_vf_config {
	int link_state;
	u16 min_tx_rate;
@@ -38,10 +40,7 @@ struct nsim_vf_config {
	bool rss_query_enabled;
};

static u32 nsim_dev_id;

static struct dentry *nsim_ddir;
static struct dentry *nsim_sdev_ddir;

static int nsim_num_vf(struct device *dev)
{
@@ -158,8 +157,8 @@ static int nsim_get_port_parent_id(struct net_device *dev,

static int nsim_init(struct net_device *dev)
{
	char sdev_ddir_name[10], sdev_link_name[32];
	struct netdevsim *ns = netdev_priv(dev);
	char sdev_link_name[32];
	int err;

	ns->netdev = dev;
@@ -167,32 +166,13 @@ static int nsim_init(struct net_device *dev)
	if (IS_ERR_OR_NULL(ns->ddir))
		return -ENOMEM;

	if (!ns->sdev) {
		ns->sdev = kzalloc(sizeof(*ns->sdev), GFP_KERNEL);
		if (!ns->sdev) {
			err = -ENOMEM;
			goto err_debugfs_destroy;
		}
		ns->sdev->refcnt = 1;
		ns->sdev->switch_id = nsim_dev_id;
		sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id);
		ns->sdev->ddir = debugfs_create_dir(sdev_ddir_name,
						    nsim_sdev_ddir);
		if (IS_ERR_OR_NULL(ns->sdev->ddir)) {
			err = PTR_ERR_OR_ZERO(ns->sdev->ddir) ?: -EINVAL;
			goto err_sdev_free;
		}
	} else {
		sprintf(sdev_ddir_name, "%u", ns->sdev->switch_id);
		ns->sdev->refcnt++;
	}

	sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%s", sdev_ddir_name);
	sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%u",
		ns->sdev->switch_id);
	debugfs_create_symlink("sdev", ns->ddir, sdev_link_name);

	err = nsim_bpf_init(ns);
	if (err)
		goto err_sdev_destroy;
		goto err_debugfs_destroy;

	ns->dev.id = nsim_dev_id++;
	ns->dev.bus = &nsim_bus;
@@ -215,12 +195,6 @@ err_unreg_dev:
	device_unregister(&ns->dev);
err_bpf_uninit:
	nsim_bpf_uninit(ns);
err_sdev_destroy:
	if (!--ns->sdev->refcnt) {
		debugfs_remove_recursive(ns->sdev->ddir);
err_sdev_free:
		kfree(ns->sdev);
	}
err_debugfs_destroy:
	debugfs_remove_recursive(ns->ddir);
	return err;
@@ -234,10 +208,6 @@ static void nsim_uninit(struct net_device *dev)
	nsim_devlink_teardown(ns);
	debugfs_remove_recursive(ns->ddir);
	nsim_bpf_uninit(ns);
	if (!--ns->sdev->refcnt) {
		debugfs_remove_recursive(ns->sdev->ddir);
		kfree(ns->sdev);
	}
}

static void nsim_free(struct net_device *dev)
@@ -246,6 +216,7 @@ static void nsim_free(struct net_device *dev)

	device_unregister(&ns->dev);
	/* netdev and vf state will be freed out of device_release() */
	nsim_sdev_put(ns->sdev);
}

static netdev_tx_t nsim_start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -523,10 +494,11 @@ static int nsim_newlink(struct net *src_net, struct net_device *dev,
			struct netlink_ext_ack *extack)
{
	struct netdevsim *ns = netdev_priv(dev);
	struct netdevsim *joinns = NULL;
	int err;

	if (tb[IFLA_LINK]) {
		struct net_device *joindev;
		struct netdevsim *joinns;

		joindev = __dev_get_by_index(src_net,
					     nla_get_u32(tb[IFLA_LINK]));
@@ -536,12 +508,20 @@ static int nsim_newlink(struct net *src_net, struct net_device *dev,
			return -EINVAL;

		joinns = netdev_priv(joindev);
		if (!joinns->sdev || !joinns->sdev->refcnt)
			return -EINVAL;
		ns->sdev = joinns->sdev;
	}

	return register_netdevice(dev);
	ns->sdev = nsim_sdev_get(joinns);
	if (IS_ERR(ns->sdev))
		return PTR_ERR(ns->sdev);

	err = register_netdevice(dev);
	if (err)
		goto err_sdev_put;
	return 0;

err_sdev_put:
	nsim_sdev_put(ns->sdev);
	return err;
}

static struct rtnl_link_ops nsim_link_ops __read_mostly = {
@@ -560,15 +540,13 @@ static int __init nsim_module_init(void)
	if (IS_ERR_OR_NULL(nsim_ddir))
		return -ENOMEM;

	nsim_sdev_ddir = debugfs_create_dir(DRV_NAME "_sdev", NULL);
	if (IS_ERR_OR_NULL(nsim_sdev_ddir)) {
		err = -ENOMEM;
	err = nsim_sdev_init();
	if (err)
		goto err_debugfs_destroy;
	}

	err = bus_register(&nsim_bus);
	if (err)
		goto err_sdir_destroy;
		goto err_sdev_exit;

	err = nsim_devlink_init();
	if (err)
@@ -584,8 +562,8 @@ err_dl_fini:
	nsim_devlink_exit();
err_unreg_bus:
	bus_unregister(&nsim_bus);
err_sdir_destroy:
	debugfs_remove_recursive(nsim_sdev_ddir);
err_sdev_exit:
	nsim_sdev_exit();
err_debugfs_destroy:
	debugfs_remove_recursive(nsim_ddir);
	return err;
@@ -596,7 +574,7 @@ static void __exit nsim_module_exit(void)
	rtnl_link_unregister(&nsim_link_ops);
	nsim_devlink_exit();
	bus_unregister(&nsim_bus);
	debugfs_remove_recursive(nsim_sdev_ddir);
	nsim_sdev_exit();
	debugfs_remove_recursive(nsim_ddir);
}

+10 −3
Original line number Diff line number Diff line
@@ -39,6 +39,9 @@ struct netdevsim_shared_dev {

	struct bpf_offload_dev *bpf_dev;

	bool bpf_bind_accept;
	u32 bpf_bind_verifier_delay;

	struct dentry *ddir_bpf_bound_progs;
	u32 prog_id_gen;

@@ -46,6 +49,13 @@ struct netdevsim_shared_dev {
	struct list_head bpf_bound_maps;
};

struct netdevsim;

struct netdevsim_shared_dev *nsim_sdev_get(struct netdevsim *joinns);
void nsim_sdev_put(struct netdevsim_shared_dev *sdev);
int nsim_sdev_init(void);
void nsim_sdev_exit(void);

#define NSIM_IPSEC_MAX_SA_COUNT		33
#define NSIM_IPSEC_VALID		BIT(31)

@@ -88,9 +98,6 @@ struct netdevsim {
	struct xdp_attachment_info xdp;
	struct xdp_attachment_info xdp_hw;

	bool bpf_bind_accept;
	u32 bpf_bind_verifier_delay;

	bool bpf_tc_accept;
	bool bpf_tc_non_bound_accept;
	bool bpf_xdpdrv_accept;
+69 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2019 Mellanox Technologies. All rights reserved */

#include <linux/debugfs.h>
#include <linux/err.h>
#include <linux/kernel.h>
#include <linux/slab.h>

#include "netdevsim.h"

static struct dentry *nsim_sdev_ddir;

static u32 nsim_sdev_id;

struct netdevsim_shared_dev *nsim_sdev_get(struct netdevsim *joinns)
{
	struct netdevsim_shared_dev *sdev;
	char sdev_ddir_name[10];
	int err;

	if (joinns) {
		if (WARN_ON(!joinns->sdev))
			return ERR_PTR(-EINVAL);
		sdev = joinns->sdev;
		sdev->refcnt++;
		return sdev;
	}

	sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
	if (!sdev)
		return ERR_PTR(-ENOMEM);
	sdev->refcnt = 1;
	sdev->switch_id = nsim_sdev_id++;

	sprintf(sdev_ddir_name, "%u", sdev->switch_id);
	sdev->ddir = debugfs_create_dir(sdev_ddir_name, nsim_sdev_ddir);
	if (IS_ERR_OR_NULL(sdev->ddir)) {
		err = PTR_ERR_OR_ZERO(sdev->ddir) ?: -EINVAL;
		goto err_sdev_free;
	}

	return sdev;

err_sdev_free:
	nsim_sdev_id--;
	kfree(sdev);
	return ERR_PTR(err);
}

void nsim_sdev_put(struct netdevsim_shared_dev *sdev)
{
	if (--sdev->refcnt)
		return;
	debugfs_remove_recursive(sdev->ddir);
	kfree(sdev);
}

int nsim_sdev_init(void)
{
	nsim_sdev_ddir = debugfs_create_dir(DRV_NAME "_sdev", NULL);
	if (IS_ERR_OR_NULL(nsim_sdev_ddir))
		return -ENOMEM;
	return 0;
}

void nsim_sdev_exit(void)
{
	debugfs_remove_recursive(nsim_sdev_ddir);
}
Loading