Commit 59c84b9f authored by David Ahern's avatar David Ahern Committed by David S. Miller

netdevsim: Restore per-network namespace accounting for fib entries

Prior to the commit in the fixes tag, the resource controller in netdevsim
tracked fib entries and rules per network namespace. Restore that behavior.

Fixes: 5fc49422 ("netdevsim: create devlink instance per netdevsim instance")
Signed-off-by: default avatarDavid Ahern <dsahern@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 9481382b
...@@ -73,46 +73,47 @@ static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port) ...@@ -73,46 +73,47 @@ static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port)
debugfs_remove_recursive(nsim_dev_port->ddir); debugfs_remove_recursive(nsim_dev_port->ddir);
} }
static struct net *nsim_devlink_net(struct devlink *devlink)
{
return &init_net;
}
static u64 nsim_dev_ipv4_fib_resource_occ_get(void *priv) static u64 nsim_dev_ipv4_fib_resource_occ_get(void *priv)
{ {
struct nsim_dev *nsim_dev = priv; struct net *net = priv;
return nsim_fib_get_val(nsim_dev->fib_data, return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, false);
NSIM_RESOURCE_IPV4_FIB, false);
} }
static u64 nsim_dev_ipv4_fib_rules_res_occ_get(void *priv) static u64 nsim_dev_ipv4_fib_rules_res_occ_get(void *priv)
{ {
struct nsim_dev *nsim_dev = priv; struct net *net = priv;
return nsim_fib_get_val(nsim_dev->fib_data, return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, false);
NSIM_RESOURCE_IPV4_FIB_RULES, false);
} }
static u64 nsim_dev_ipv6_fib_resource_occ_get(void *priv) static u64 nsim_dev_ipv6_fib_resource_occ_get(void *priv)
{ {
struct nsim_dev *nsim_dev = priv; struct net *net = priv;
return nsim_fib_get_val(nsim_dev->fib_data, return nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, false);
NSIM_RESOURCE_IPV6_FIB, false);
} }
static u64 nsim_dev_ipv6_fib_rules_res_occ_get(void *priv) static u64 nsim_dev_ipv6_fib_rules_res_occ_get(void *priv)
{ {
struct nsim_dev *nsim_dev = priv; struct net *net = priv;
return nsim_fib_get_val(nsim_dev->fib_data, return nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB_RULES, false);
NSIM_RESOURCE_IPV6_FIB_RULES, false);
} }
static int nsim_dev_resources_register(struct devlink *devlink) static int nsim_dev_resources_register(struct devlink *devlink)
{ {
struct nsim_dev *nsim_dev = devlink_priv(devlink);
struct devlink_resource_size_params params = { struct devlink_resource_size_params params = {
.size_max = (u64)-1, .size_max = (u64)-1,
.size_granularity = 1, .size_granularity = 1,
.unit = DEVLINK_RESOURCE_UNIT_ENTRY .unit = DEVLINK_RESOURCE_UNIT_ENTRY
}; };
struct net *net = nsim_devlink_net(devlink);
int err; int err;
u64 n; u64 n;
...@@ -126,8 +127,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -126,8 +127,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
goto out; goto out;
} }
n = nsim_fib_get_val(nsim_dev->fib_data, n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, true);
NSIM_RESOURCE_IPV4_FIB, true);
err = devlink_resource_register(devlink, "fib", n, err = devlink_resource_register(devlink, "fib", n,
NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB,
NSIM_RESOURCE_IPV4, &params); NSIM_RESOURCE_IPV4, &params);
...@@ -136,8 +136,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -136,8 +136,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
return err; return err;
} }
n = nsim_fib_get_val(nsim_dev->fib_data, n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, true);
NSIM_RESOURCE_IPV4_FIB_RULES, true);
err = devlink_resource_register(devlink, "fib-rules", n, err = devlink_resource_register(devlink, "fib-rules", n,
NSIM_RESOURCE_IPV4_FIB_RULES, NSIM_RESOURCE_IPV4_FIB_RULES,
NSIM_RESOURCE_IPV4, &params); NSIM_RESOURCE_IPV4, &params);
...@@ -156,8 +155,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -156,8 +155,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
goto out; goto out;
} }
n = nsim_fib_get_val(nsim_dev->fib_data, n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, true);
NSIM_RESOURCE_IPV6_FIB, true);
err = devlink_resource_register(devlink, "fib", n, err = devlink_resource_register(devlink, "fib", n,
NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB,
NSIM_RESOURCE_IPV6, &params); NSIM_RESOURCE_IPV6, &params);
...@@ -166,8 +164,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -166,8 +164,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
return err; return err;
} }
n = nsim_fib_get_val(nsim_dev->fib_data, n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB_RULES, true);
NSIM_RESOURCE_IPV6_FIB_RULES, true);
err = devlink_resource_register(devlink, "fib-rules", n, err = devlink_resource_register(devlink, "fib-rules", n,
NSIM_RESOURCE_IPV6_FIB_RULES, NSIM_RESOURCE_IPV6_FIB_RULES,
NSIM_RESOURCE_IPV6, &params); NSIM_RESOURCE_IPV6, &params);
...@@ -179,19 +176,19 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -179,19 +176,19 @@ static int nsim_dev_resources_register(struct devlink *devlink)
devlink_resource_occ_get_register(devlink, devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB,
nsim_dev_ipv4_fib_resource_occ_get, nsim_dev_ipv4_fib_resource_occ_get,
nsim_dev); net);
devlink_resource_occ_get_register(devlink, devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB_RULES, NSIM_RESOURCE_IPV4_FIB_RULES,
nsim_dev_ipv4_fib_rules_res_occ_get, nsim_dev_ipv4_fib_rules_res_occ_get,
nsim_dev); net);
devlink_resource_occ_get_register(devlink, devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB,
nsim_dev_ipv6_fib_resource_occ_get, nsim_dev_ipv6_fib_resource_occ_get,
nsim_dev); net);
devlink_resource_occ_get_register(devlink, devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB_RULES, NSIM_RESOURCE_IPV6_FIB_RULES,
nsim_dev_ipv6_fib_rules_res_occ_get, nsim_dev_ipv6_fib_rules_res_occ_get,
nsim_dev); net);
out: out:
return err; return err;
} }
...@@ -199,11 +196,11 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -199,11 +196,11 @@ static int nsim_dev_resources_register(struct devlink *devlink)
static int nsim_dev_reload(struct devlink *devlink, static int nsim_dev_reload(struct devlink *devlink,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
struct nsim_dev *nsim_dev = devlink_priv(devlink);
enum nsim_resource_id res_ids[] = { enum nsim_resource_id res_ids[] = {
NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES, NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES
}; };
struct net *net = nsim_devlink_net(devlink);
int i; int i;
for (i = 0; i < ARRAY_SIZE(res_ids); ++i) { for (i = 0; i < ARRAY_SIZE(res_ids); ++i) {
...@@ -212,8 +209,7 @@ static int nsim_dev_reload(struct devlink *devlink, ...@@ -212,8 +209,7 @@ static int nsim_dev_reload(struct devlink *devlink,
err = devlink_resource_size_get(devlink, res_ids[i], &val); err = devlink_resource_size_get(devlink, res_ids[i], &val);
if (!err) { if (!err) {
err = nsim_fib_set_max(nsim_dev->fib_data, err = nsim_fib_set_max(net, res_ids[i], val, extack);
res_ids[i], val, extack);
if (err) if (err)
return err; return err;
} }
...@@ -285,15 +281,9 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count) ...@@ -285,15 +281,9 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
mutex_init(&nsim_dev->port_list_lock); mutex_init(&nsim_dev->port_list_lock);
nsim_dev->fw_update_status = true; nsim_dev->fw_update_status = true;
nsim_dev->fib_data = nsim_fib_create();
if (IS_ERR(nsim_dev->fib_data)) {
err = PTR_ERR(nsim_dev->fib_data);
goto err_devlink_free;
}
err = nsim_dev_resources_register(devlink); err = nsim_dev_resources_register(devlink);
if (err) if (err)
goto err_fib_destroy; goto err_devlink_free;
err = devlink_register(devlink, &nsim_bus_dev->dev); err = devlink_register(devlink, &nsim_bus_dev->dev);
if (err) if (err)
...@@ -315,8 +305,6 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count) ...@@ -315,8 +305,6 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
devlink_unregister(devlink); devlink_unregister(devlink);
err_resources_unregister: err_resources_unregister:
devlink_resources_unregister(devlink, NULL); devlink_resources_unregister(devlink, NULL);
err_fib_destroy:
nsim_fib_destroy(nsim_dev->fib_data);
err_devlink_free: err_devlink_free:
devlink_free(devlink); devlink_free(devlink);
return ERR_PTR(err); return ERR_PTR(err);
...@@ -330,7 +318,6 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev) ...@@ -330,7 +318,6 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev)
nsim_dev_debugfs_exit(nsim_dev); nsim_dev_debugfs_exit(nsim_dev);
devlink_unregister(devlink); devlink_unregister(devlink);
devlink_resources_unregister(devlink, NULL); devlink_resources_unregister(devlink, NULL);
nsim_fib_destroy(nsim_dev->fib_data);
mutex_destroy(&nsim_dev->port_list_lock); mutex_destroy(&nsim_dev->port_list_lock);
devlink_free(devlink); devlink_free(devlink);
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <net/ip_fib.h> #include <net/ip_fib.h>
#include <net/ip6_fib.h> #include <net/ip6_fib.h>
#include <net/fib_rules.h> #include <net/fib_rules.h>
#include <net/netns/generic.h>
#include "netdevsim.h" #include "netdevsim.h"
...@@ -32,14 +33,15 @@ struct nsim_per_fib_data { ...@@ -32,14 +33,15 @@ struct nsim_per_fib_data {
}; };
struct nsim_fib_data { struct nsim_fib_data {
struct notifier_block fib_nb;
struct nsim_per_fib_data ipv4; struct nsim_per_fib_data ipv4;
struct nsim_per_fib_data ipv6; struct nsim_per_fib_data ipv6;
}; };
u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, static unsigned int nsim_fib_net_id;
enum nsim_resource_id res_id, bool max)
u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max)
{ {
struct nsim_fib_data *fib_data = net_generic(net, nsim_fib_net_id);
struct nsim_fib_entry *entry; struct nsim_fib_entry *entry;
switch (res_id) { switch (res_id) {
...@@ -62,10 +64,10 @@ u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, ...@@ -62,10 +64,10 @@ u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
return max ? entry->max : entry->num; return max ? entry->max : entry->num;
} }
int nsim_fib_set_max(struct nsim_fib_data *fib_data, int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
enum nsim_resource_id res_id, u64 val,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
struct nsim_fib_data *fib_data = net_generic(net, nsim_fib_net_id);
struct nsim_fib_entry *entry; struct nsim_fib_entry *entry;
int err = 0; int err = 0;
...@@ -118,9 +120,9 @@ static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add, ...@@ -118,9 +120,9 @@ static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
return err; return err;
} }
static int nsim_fib_rule_event(struct nsim_fib_data *data, static int nsim_fib_rule_event(struct fib_notifier_info *info, bool add)
struct fib_notifier_info *info, bool add)
{ {
struct nsim_fib_data *data = net_generic(info->net, nsim_fib_net_id);
struct netlink_ext_ack *extack = info->extack; struct netlink_ext_ack *extack = info->extack;
int err = 0; int err = 0;
...@@ -155,9 +157,9 @@ static int nsim_fib_account(struct nsim_fib_entry *entry, bool add, ...@@ -155,9 +157,9 @@ static int nsim_fib_account(struct nsim_fib_entry *entry, bool add,
return err; return err;
} }
static int nsim_fib_event(struct nsim_fib_data *data, static int nsim_fib_event(struct fib_notifier_info *info, bool add)
struct fib_notifier_info *info, bool add)
{ {
struct nsim_fib_data *data = net_generic(info->net, nsim_fib_net_id);
struct netlink_ext_ack *extack = info->extack; struct netlink_ext_ack *extack = info->extack;
int err = 0; int err = 0;
...@@ -176,22 +178,18 @@ static int nsim_fib_event(struct nsim_fib_data *data, ...@@ -176,22 +178,18 @@ static int nsim_fib_event(struct nsim_fib_data *data,
static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event, static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
void *ptr) void *ptr)
{ {
struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
fib_nb);
struct fib_notifier_info *info = ptr; struct fib_notifier_info *info = ptr;
int err = 0; int err = 0;
switch (event) { switch (event) {
case FIB_EVENT_RULE_ADD: /* fall through */ case FIB_EVENT_RULE_ADD: /* fall through */
case FIB_EVENT_RULE_DEL: case FIB_EVENT_RULE_DEL:
err = nsim_fib_rule_event(data, info, err = nsim_fib_rule_event(info, event == FIB_EVENT_RULE_ADD);
event == FIB_EVENT_RULE_ADD);
break; break;
case FIB_EVENT_ENTRY_ADD: /* fall through */ case FIB_EVENT_ENTRY_ADD: /* fall through */
case FIB_EVENT_ENTRY_DEL: case FIB_EVENT_ENTRY_DEL:
err = nsim_fib_event(data, info, err = nsim_fib_event(info, event == FIB_EVENT_ENTRY_ADD);
event == FIB_EVENT_ENTRY_ADD);
break; break;
} }
...@@ -201,23 +199,30 @@ static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event, ...@@ -201,23 +199,30 @@ static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
/* inconsistent dump, trying again */ /* inconsistent dump, trying again */
static void nsim_fib_dump_inconsistent(struct notifier_block *nb) static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
{ {
struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data, struct nsim_fib_data *data;
fib_nb); struct net *net;
rcu_read_lock();
for_each_net_rcu(net) {
data = net_generic(net, nsim_fib_net_id);
data->ipv4.fib.num = 0ULL;
data->ipv4.rules.num = 0ULL;
data->ipv4.fib.num = 0ULL; data->ipv6.fib.num = 0ULL;
data->ipv4.rules.num = 0ULL; data->ipv6.rules.num = 0ULL;
data->ipv6.fib.num = 0ULL; }
data->ipv6.rules.num = 0ULL; rcu_read_unlock();
} }
struct nsim_fib_data *nsim_fib_create(void) static struct notifier_block nsim_fib_nb = {
{ .notifier_call = nsim_fib_event_nb,
struct nsim_fib_data *data; };
int err;
data = kzalloc(sizeof(*data), GFP_KERNEL); /* Initialize per network namespace state */
if (!data) static int __net_init nsim_fib_netns_init(struct net *net)
return ERR_PTR(-ENOMEM); {
struct nsim_fib_data *data = net_generic(net, nsim_fib_net_id);
data->ipv4.fib.max = (u64)-1; data->ipv4.fib.max = (u64)-1;
data->ipv4.rules.max = (u64)-1; data->ipv4.rules.max = (u64)-1;
...@@ -225,22 +230,37 @@ struct nsim_fib_data *nsim_fib_create(void) ...@@ -225,22 +230,37 @@ struct nsim_fib_data *nsim_fib_create(void)
data->ipv6.fib.max = (u64)-1; data->ipv6.fib.max = (u64)-1;
data->ipv6.rules.max = (u64)-1; data->ipv6.rules.max = (u64)-1;
data->fib_nb.notifier_call = nsim_fib_event_nb; return 0;
err = register_fib_notifier(&data->fib_nb, nsim_fib_dump_inconsistent); }
if (err) {
pr_err("Failed to register fib notifier\n");
goto err_out;
}
return data; static struct pernet_operations nsim_fib_net_ops = {
.init = nsim_fib_netns_init,
.id = &nsim_fib_net_id,
.size = sizeof(struct nsim_fib_data),
};
err_out: void nsim_fib_exit(void)
kfree(data); {
return ERR_PTR(err); unregister_pernet_subsys(&nsim_fib_net_ops);
unregister_fib_notifier(&nsim_fib_nb);
} }
void nsim_fib_destroy(struct nsim_fib_data *data) int nsim_fib_init(void)
{ {
unregister_fib_notifier(&data->fib_nb); int err;
kfree(data);
err = register_pernet_subsys(&nsim_fib_net_ops);
if (err < 0) {
pr_err("Failed to register pernet subsystem\n");
goto err_out;
}
err = register_fib_notifier(&nsim_fib_nb, nsim_fib_dump_inconsistent);
if (err < 0) {
pr_err("Failed to register fib notifier\n");
goto err_out;
}
err_out:
return err;
} }
...@@ -357,12 +357,18 @@ static int __init nsim_module_init(void) ...@@ -357,12 +357,18 @@ static int __init nsim_module_init(void)
if (err) if (err)
goto err_dev_exit; goto err_dev_exit;
err = rtnl_link_register(&nsim_link_ops); err = nsim_fib_init();
if (err) if (err)
goto err_bus_exit; goto err_bus_exit;
err = rtnl_link_register(&nsim_link_ops);
if (err)
goto err_fib_exit;
return 0; return 0;
err_fib_exit:
nsim_fib_exit();
err_bus_exit: err_bus_exit:
nsim_bus_exit(); nsim_bus_exit();
err_dev_exit: err_dev_exit:
...@@ -373,6 +379,7 @@ static int __init nsim_module_init(void) ...@@ -373,6 +379,7 @@ static int __init nsim_module_init(void)
static void __exit nsim_module_exit(void) static void __exit nsim_module_exit(void)
{ {
rtnl_link_unregister(&nsim_link_ops); rtnl_link_unregister(&nsim_link_ops);
nsim_fib_exit();
nsim_bus_exit(); nsim_bus_exit();
nsim_dev_exit(); nsim_dev_exit();
} }
......
...@@ -169,12 +169,10 @@ int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, ...@@ -169,12 +169,10 @@ int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev,
int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev, int nsim_dev_port_del(struct nsim_bus_dev *nsim_bus_dev,
unsigned int port_index); unsigned int port_index);
struct nsim_fib_data *nsim_fib_create(void); int nsim_fib_init(void);
void nsim_fib_destroy(struct nsim_fib_data *fib_data); void nsim_fib_exit(void);
u64 nsim_fib_get_val(struct nsim_fib_data *fib_data, u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max);
enum nsim_resource_id res_id, bool max); int nsim_fib_set_max(struct net *net, enum nsim_resource_id res_id, u64 val,
int nsim_fib_set_max(struct nsim_fib_data *fib_data,
enum nsim_resource_id res_id, u64 val,
struct netlink_ext_ack *extack); struct netlink_ext_ack *extack);
#if IS_ENABLED(CONFIG_XFRM_OFFLOAD) #if IS_ENABLED(CONFIG_XFRM_OFFLOAD)
......
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