Skip to content

Commit

Permalink
netdevsim: merge sdev into dev
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jpirko authored and davem330 committed Apr 26, 2019
1 parent a60f9e4 commit d514f41
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 119 deletions.
2 changes: 1 addition & 1 deletion drivers/net/netdevsim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand Down
79 changes: 33 additions & 46 deletions drivers/net/netdevsim/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -644,20 +639,12 @@ 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)
{
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);
}
47 changes: 47 additions & 0 deletions drivers/net/netdevsim/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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:
Expand Down Expand Up @@ -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);
}
49 changes: 13 additions & 36 deletions drivers/net/netdevsim/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down
Loading

0 comments on commit d514f41

Please sign in to comment.