[patch net-next v4 10/16] netdevsim: merge sdev into dev

Jiri Pirko
2019-04-25T14:00:15Z

As previously introduce dev which is mapped 1:1 to a bus device covers
the purpose of the original shared device, merge the sdev code into dev.

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
---
rfc->v1:
- rebased
---
drivers/net/netdevsim/Makefile | 2 +-
drivers/net/netdevsim/bpf.c | 79 +++++++++------------
drivers/net/netdevsim/dev.c | 47 ++++++++++++
drivers/net/netdevsim/netdev.c | 49 ++++---------
drivers/net/netdevsim/netdevsim.h | 54 ++++++--------
tools/testing/selftests/bpf/test_offload.py | 8 +--
6 files changed, 120 insertions(+), 119 deletions(-)

diff --git a/drivers/net/netdevsim/Makefile b/drivers/net/netdevsim/Makefile
index ab7e2c42088f..09f1315d2f2a 100644
--- a/drivers/net/netdevsim/Makefile
+++ b/drivers/net/netdevsim/Makefile
@@ -3,7 +3,7 @@
obj-$(CONFIG_NETDEVSIM) += netdevsim.o

netdevsim-objs := \
- netdev.o dev.o fib.o sdev.o bus.o
+ netdev.o dev.o fib.o bus.o

ifeq ($(CONFIG_BPF_SYSCALL),y)
netdevsim-objs += \
diff --git a/drivers/net/netdevsim/bpf.c b/drivers/net/netdevsim/bpf.c
index a93aafe87db3..89980b223adc 100644
--- a/drivers/net/netdevsim/bpf.c
+++ b/drivers/net/netdevsim/bpf.c
@@ -27,7 +27,7 @@
bpf_verifier_log_write(env, "[netdevsim] " fmt, ##__VA_ARGS__)

struct nsim_bpf_bound_prog {
- struct netdevsim_shared_dev *sdev;
+ struct nsim_dev *nsim_dev;
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->sdev->bpf_bind_verifier_delay && !insn_idx)
- msleep(state->sdev->bpf_bind_verifier_delay);
+ if (state->nsim_dev->bpf_bind_verifier_delay && !insn_idx)
+ msleep(state->nsim_dev->bpf_bind_verifier_delay);

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

-static int nsim_bpf_create_prog(struct netdevsim_shared_dev *sdev,
+static int nsim_bpf_create_prog(struct nsim_dev *nsim_dev,
struct bpf_prog *prog)
{
struct nsim_bpf_bound_prog *state;
@@ -223,13 +223,13 @@ static int nsim_bpf_create_prog(struct netdevsim_shared_dev *sdev,
if (!state)
return -ENOMEM;

- state->sdev = sdev;
+ state->nsim_dev = nsim_dev;
state->prog = prog;
state->state = "verify";

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

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

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

@@ -249,13 +249,13 @@ static int nsim_bpf_create_prog(struct netdevsim_shared_dev *sdev,

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

- if (!sdev->bpf_bind_accept)
+ if (!nsim_dev->bpf_bind_accept)
return -EOPNOTSUPP;

- return nsim_bpf_create_prog(sdev, prog);
+ return nsim_bpf_create_prog(nsim_dev, prog);
}

static int nsim_bpf_translate(struct bpf_prog *prog)
@@ -514,7 +514,7 @@ nsim_bpf_map_alloc(struct netdevsim *ns, struct bpf_offloaded_map *offmap)
}

offmap->dev_ops = &nsim_bpf_map_ops;
- list_add_tail(&nmap->l, &ns->sdev->bpf_bound_maps);
+ list_add_tail(&nmap->l, &ns->nsim_dev->bpf_bound_maps);

return 0;

@@ -578,51 +578,46 @@ int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf)
}
}

-static int nsim_bpf_sdev_init(struct netdevsim_shared_dev *sdev)
+int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
{
int err;

- INIT_LIST_HEAD(&sdev->bpf_bound_progs);
- INIT_LIST_HEAD(&sdev->bpf_bound_maps);
+ INIT_LIST_HEAD(&nsim_dev->bpf_bound_progs);
+ INIT_LIST_HEAD(&nsim_dev->bpf_bound_maps);

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

- sdev->bpf_dev = bpf_offload_dev_create(&nsim_bpf_dev_ops, sdev);
- err = PTR_ERR_OR_ZERO(sdev->bpf_dev);
+ nsim_dev->bpf_dev = bpf_offload_dev_create(&nsim_bpf_dev_ops, nsim_dev);
+ err = PTR_ERR_OR_ZERO(nsim_dev->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);
+ nsim_dev->bpf_bind_accept = true;
+ debugfs_create_bool("bpf_bind_accept", 0600, nsim_dev->ddir,
+ &nsim_dev->bpf_bind_accept);
+ debugfs_create_u32("bpf_bind_verifier_delay", 0600, nsim_dev->ddir,
+ &nsim_dev->bpf_bind_verifier_delay);
return 0;
}

-static void nsim_bpf_sdev_uninit(struct netdevsim_shared_dev *sdev)
+void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
{
- WARN_ON(!list_empty(&sdev->bpf_bound_progs));
- WARN_ON(!list_empty(&sdev->bpf_bound_maps));
- bpf_offload_dev_destroy(sdev->bpf_dev);
+ WARN_ON(!list_empty(&nsim_dev->bpf_bound_progs));
+ WARN_ON(!list_empty(&nsim_dev->bpf_bound_maps));
+ bpf_offload_dev_destroy(nsim_dev->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);
+ err = bpf_offload_dev_netdev_register(ns->nsim_dev->bpf_dev,
+ ns->netdev);
if (err)
- goto err_bpf_sdev_uninit;
+ return err;

debugfs_create_u32("bpf_offloaded_id", 0400, ns->ddir,
&ns->bpf_offloaded_id);
@@ -644,11 +639,6 @@ int nsim_bpf_init(struct netdevsim *ns)
&ns->bpf_map_accept);

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

void nsim_bpf_uninit(struct netdevsim *ns)
@@ -656,8 +646,5 @@ void nsim_bpf_uninit(struct netdevsim *ns)
WARN_ON(ns->xdp.prog);
WARN_ON(ns->xdp_hw.prog);
WARN_ON(ns->bpf_offloaded);
- bpf_offload_dev_netdev_unregister(ns->sdev->bpf_dev, ns->netdev);
-
- if (ns->sdev->refcnt == 1)
- nsim_bpf_sdev_uninit(ns->sdev);
+ bpf_offload_dev_netdev_unregister(ns->nsim_dev->bpf_dev, ns->netdev);
}
diff --git a/drivers/net/netdevsim/dev.c b/drivers/net/netdevsim/dev.c
index 664b9329a65c..93abd0b6c1f9 100644
--- a/drivers/net/netdevsim/dev.c
+++ b/drivers/net/netdevsim/dev.c
@@ -15,12 +15,31 @@
* THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/

+#include <linux/debugfs.h>
#include <linux/device.h>
#include <linux/rtnetlink.h>
#include <net/devlink.h>

#include "netdevsim.h"

+static struct dentry *nsim_dev_ddir;
+
+static int nsim_dev_debugfs_init(struct nsim_dev *nsim_dev)
+{
+ char dev_ddir_name[10];
+
+ sprintf(dev_ddir_name, "%u", nsim_dev->nsim_bus_dev->dev.id);
+ nsim_dev->ddir = debugfs_create_dir(dev_ddir_name, nsim_dev_ddir);
+ if (IS_ERR_OR_NULL(nsim_dev->ddir))
+ return PTR_ERR_OR_ZERO(nsim_dev->ddir) ?: -EINVAL;
+ return 0;
+}
+
+static void nsim_dev_debugfs_exit(struct nsim_dev *nsim_dev)
+{
+ debugfs_remove_recursive(nsim_dev->ddir);
+}
+
static u64 nsim_dev_ipv4_fib_resource_occ_get(void *priv)
{
struct nsim_dev *nsim_dev = priv;
@@ -184,6 +203,7 @@ static struct nsim_dev *nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev)
if (!devlink)
return ERR_PTR(-ENOMEM);
nsim_dev = devlink_priv(devlink);
+ nsim_dev->nsim_bus_dev = nsim_bus_dev;

nsim_dev->fib_data = nsim_fib_create();
if (IS_ERR(nsim_dev->fib_data)) {
@@ -199,8 +219,20 @@ static struct nsim_dev *nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev)
if (err)
goto err_resources_unregister;

+ err = nsim_dev_debugfs_init(nsim_dev);
+ if (err)
+ goto err_dl_unregister;
+
+ err = nsim_bpf_dev_init(nsim_dev);
+ if (err)
+ goto err_debugfs_exit;
+
return nsim_dev;

+err_debugfs_exit:
+ nsim_dev_debugfs_exit(nsim_dev);
+err_dl_unregister:
+ devlink_unregister(devlink);
err_resources_unregister:
devlink_resources_unregister(devlink, NULL);
err_fib_destroy:
@@ -228,8 +260,23 @@ void nsim_dev_destroy(struct nsim_dev *nsim_dev)
{
struct devlink *devlink = priv_to_devlink(nsim_dev);

+ nsim_bpf_dev_exit(nsim_dev);
+ nsim_dev_debugfs_exit(nsim_dev);
devlink_unregister(devlink);
devlink_resources_unregister(devlink, NULL);
nsim_fib_destroy(nsim_dev->fib_data);
devlink_free(devlink);
}
+
+int nsim_dev_init(void)
+{
+ nsim_dev_ddir = debugfs_create_dir(DRV_NAME "_dev", NULL);
+ if (IS_ERR_OR_NULL(nsim_dev_ddir))
+ return -ENOMEM;
+ return 0;
+}
+
+void nsim_dev_exit(void)
+{
+ debugfs_remove_recursive(nsim_dev_ddir);
+}
diff --git a/drivers/net/netdevsim/netdev.c b/drivers/net/netdevsim/netdev.c
index 28231bfbc989..c5f4bbb9716f 100644
--- a/drivers/net/netdevsim/netdev.c
+++ b/drivers/net/netdevsim/netdev.c
@@ -32,24 +32,24 @@ static int nsim_get_port_parent_id(struct net_device *dev,
{
struct netdevsim *ns = netdev_priv(dev);

- ppid->id_len = sizeof(ns->sdev->switch_id);
- memcpy(&ppid->id, &ns->sdev->switch_id, ppid->id_len);
+ ppid->id_len = sizeof(ns->nsim_dev->nsim_bus_dev->dev.id);
+ memcpy(&ppid->id, &ns->nsim_dev->nsim_bus_dev->dev.id, ppid->id_len);
return 0;
}

static int nsim_init(struct net_device *dev)
{
struct netdevsim *ns = netdev_priv(dev);
- char sdev_link_name[32];
+ char dev_link_name[32];
int err;

ns->ddir = debugfs_create_dir(netdev_name(dev), nsim_ddir);
if (IS_ERR_OR_NULL(ns->ddir))
return -ENOMEM;

- sprintf(sdev_link_name, "../../" DRV_NAME "_sdev/%u",
- ns->sdev->switch_id);
- debugfs_create_symlink("sdev", ns->ddir, sdev_link_name);
+ sprintf(dev_link_name, "../../" DRV_NAME "_dev/%u",
+ ns->nsim_dev->nsim_bus_dev->dev.id);
+ debugfs_create_symlink("dev", ns->ddir, dev_link_name);

err = nsim_bpf_init(ns);
if (err)
@@ -80,7 +80,6 @@ static void nsim_free(struct net_device *dev)
nsim_dev_destroy(ns->nsim_dev);
nsim_bus_dev_del(ns->nsim_bus_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)
@@ -366,31 +365,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;
-
- joindev = __dev_get_by_index(src_net,
- nla_get_u32(tb[IFLA_LINK]));
- if (!joindev)
- return -ENODEV;
- if (joindev->netdev_ops != &nsim_netdev_ops)
- return -EINVAL;
-
- joinns = netdev_priv(joindev);
- }
-
- ns->sdev = nsim_sdev_get(joinns);
- if (IS_ERR(ns->sdev))
- return PTR_ERR(ns->sdev);
-
ns->nsim_bus_dev = nsim_bus_dev_new(~0, 0);
- if (IS_ERR(ns->nsim_bus_dev)) {
- err = PTR_ERR(ns->nsim_bus_dev);
- goto err_sdev_put;
- }
+ if (IS_ERR(ns->nsim_bus_dev))
+ return PTR_ERR(ns->nsim_bus_dev);

SET_NETDEV_DEV(dev, &ns->nsim_bus_dev->dev);
ns->netdev = dev;
@@ -410,8 +389,6 @@ static int nsim_newlink(struct net *src_net, struct net_device *dev,
nsim_dev_destroy(ns->nsim_dev);
err_dev_del:
nsim_bus_dev_del(ns->nsim_bus_dev);
-err_sdev_put:
- nsim_sdev_put(ns->sdev);
return err;
}

@@ -431,13 +408,13 @@ static int __init nsim_module_init(void)
if (IS_ERR_OR_NULL(nsim_ddir))
return -ENOMEM;

- err = nsim_sdev_init();
+ err = nsim_dev_init();
if (err)
goto err_debugfs_destroy;

err = nsim_bus_init();
if (err)
- goto err_sdev_exit;
+ goto err_dev_exit;

err = rtnl_link_register(&nsim_link_ops);
if (err)
@@ -447,8 +424,8 @@ static int __init nsim_module_init(void)

err_bus_exit:
nsim_bus_exit();
-err_sdev_exit:
- nsim_sdev_exit();
+err_dev_exit:
+ nsim_dev_exit();
err_debugfs_destroy:
debugfs_remove_recursive(nsim_ddir);
return err;
@@ -458,7 +435,7 @@ static void __exit nsim_module_exit(void)
{
rtnl_link_unregister(&nsim_link_ops);
nsim_bus_exit();
- nsim_sdev_exit();
+ nsim_dev_exit();
debugfs_remove_recursive(nsim_ddir);
}

diff --git a/drivers/net/netdevsim/netdevsim.h b/drivers/net/netdevsim/netdevsim.h
index d6b3668f9afd..4ef44a9538db 100644
--- a/drivers/net/netdevsim/netdevsim.h
+++ b/drivers/net/netdevsim/netdevsim.h
@@ -26,37 +26,6 @@

#define NSIM_EA(extack, msg) NL_SET_ERR_MSG_MOD((extack), msg)

-struct bpf_prog;
-struct bpf_offload_dev;
-struct dentry;
-struct nsim_vf_config;
-struct nsim_fib_data;
-
-struct netdevsim_shared_dev {
- unsigned int refcnt;
- u32 switch_id;
-
- struct dentry *ddir;
-
- 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;
-
- struct list_head bpf_bound_progs;
- 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)

@@ -87,7 +56,6 @@ struct netdevsim {
struct u64_stats_sync syncp;

struct nsim_bus_dev *nsim_bus_dev;
- struct netdevsim_shared_dev *sdev;

struct dentry *ddir;

@@ -107,6 +75,8 @@ struct netdevsim {
};

#ifdef CONFIG_BPF_SYSCALL
+int nsim_bpf_dev_init(struct nsim_dev *nsim_dev);
+void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev);
int nsim_bpf_init(struct netdevsim *ns);
void nsim_bpf_uninit(struct netdevsim *ns);
int nsim_bpf(struct net_device *dev, struct netdev_bpf *bpf);
@@ -114,6 +84,15 @@ int nsim_bpf_disable_tc(struct netdevsim *ns);
int nsim_bpf_setup_tc_block_cb(enum tc_setup_type type,
void *type_data, void *cb_priv);
#else
+
+static inline int nsim_bpf_dev_init(struct nsim_dev *nsim_dev)
+{
+ return 0;
+}
+
+static inline void nsim_bpf_dev_exit(struct nsim_dev *nsim_dev)
+{
+}
static inline int nsim_bpf_init(struct netdevsim *ns)
{
return 0;
@@ -152,13 +131,24 @@ enum nsim_resource_id {
};

struct nsim_dev {
+ struct nsim_bus_dev *nsim_bus_dev;
struct nsim_fib_data *fib_data;
+ struct dentry *ddir;
+ 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;
+ struct list_head bpf_bound_progs;
+ struct list_head bpf_bound_maps;
};

struct nsim_dev *
nsim_dev_create_with_ns(struct nsim_bus_dev *nsim_bus_dev,
struct netdevsim *ns);
void nsim_dev_destroy(struct nsim_dev *nsim_dev);
+int nsim_dev_init(void);
+void nsim_dev_exit(void);

struct nsim_fib_data *nsim_fib_create(void);
void nsim_fib_destroy(struct nsim_fib_data *fib_data);
diff --git a/tools/testing/selftests/bpf/test_offload.py b/tools/testing/selftests/bpf/test_offload.py
index a7f95106119f..91dfe876a707 100755
--- a/tools/testing/selftests/bpf/test_offload.py
+++ b/tools/testing/selftests/bpf/test_offload.py
@@ -335,7 +335,7 @@ class NetdevSim:
self.ns = ""

self.dfs_dir = '/sys/kernel/debug/netdevsim/%s' % (self.dev['ifname'])
- self.sdev_dir = self.dfs_dir + '/sdev/'
+ self.dev_dir = self.dfs_dir + '/dev/'
self.dfs_refresh()

def __getitem__(self, key):
@@ -368,12 +368,12 @@ class NetdevSim:
return data.strip()

def dfs_num_bound_progs(self):
- path = os.path.join(self.sdev_dir, "bpf_bound_progs")
+ path = os.path.join(self.dev_dir, "bpf_bound_progs")
_, progs = cmd('ls %s' % (path))
return len(progs.split())

def dfs_get_bound_progs(self, expected):
- progs = DebugfsDir(os.path.join(self.sdev_dir, "bpf_bound_progs"))
+ progs = DebugfsDir(os.path.join(self.dev_dir, "bpf_bound_progs"))
if expected is not None:
if len(progs) != expected:
fail(True, "%d BPF programs bound, expected %d" %
@@ -1055,7 +1055,7 @@ try:

start_test("Test if netdev removal waits for translation...")
delay_msec = 500
- sim.dfs["sdev/bpf_bind_verifier_delay"] = delay_msec
+ sim.dfs["dev/bpf_bind_verifier_delay"] = delay_msec
start = time.time()
cmd_line = "tc filter add dev %s ingress bpf %s da skip_sw" % \
(sim['ifname'], obj)
--
2.17.2