Commit a5facc4c authored by Jiri Pirko's avatar Jiri Pirko Committed by David S. Miller

netdevsim: change fib accounting and limitations to be per-device

Currently, the accounting is done per-namespace. However, devlink
instance is always in init_net namespace for now, so only the accounting
related to init_net is used. Limitations set using devlink resources
are only considered for init_net. nsim_devlink_net() always
returns init_net always.

Make the accounting per-device. This brings no functional change.
Per-device accounting has the same values as per-net.
For a single netdevsim instance, the behaviour is exactly the same
as before. When multiple netdevsim instances are created, each
can have different limits.

This is in prepare to implement proper devlink netns support. After
that, the devlink instance which would exist in particular netns would
account and limit that netns.
Signed-off-by: default avatarJiri Pirko <jiri@mellanox.com>
Acked-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 9077f052
...@@ -123,39 +123,6 @@ static void nsim_dev_port_debugfs_exit(struct nsim_dev_port *nsim_dev_port) ...@@ -123,39 +123,6 @@ 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)
{
struct net *net = priv;
return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, false);
}
static u64 nsim_dev_ipv4_fib_rules_res_occ_get(void *priv)
{
struct net *net = priv;
return nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, false);
}
static u64 nsim_dev_ipv6_fib_resource_occ_get(void *priv)
{
struct net *net = priv;
return nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, false);
}
static u64 nsim_dev_ipv6_fib_rules_res_occ_get(void *priv)
{
struct net *net = priv;
return nsim_fib_get_val(net, 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 devlink_resource_size_params params = { struct devlink_resource_size_params params = {
...@@ -163,9 +130,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -163,9 +130,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
.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;
/* Resources for IPv4 */ /* Resources for IPv4 */
err = devlink_resource_register(devlink, "IPv4", (u64)-1, err = devlink_resource_register(devlink, "IPv4", (u64)-1,
...@@ -177,8 +142,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -177,8 +142,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
goto out; goto out;
} }
n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB, true); err = devlink_resource_register(devlink, "fib", (u64)-1,
err = devlink_resource_register(devlink, "fib", n,
NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB,
NSIM_RESOURCE_IPV4, &params); NSIM_RESOURCE_IPV4, &params);
if (err) { if (err) {
...@@ -186,8 +150,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -186,8 +150,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
return err; return err;
} }
n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV4_FIB_RULES, true); err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
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);
if (err) { if (err) {
...@@ -205,8 +168,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -205,8 +168,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
goto out; goto out;
} }
n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB, true); err = devlink_resource_register(devlink, "fib", (u64)-1,
err = devlink_resource_register(devlink, "fib", n,
NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB,
NSIM_RESOURCE_IPV6, &params); NSIM_RESOURCE_IPV6, &params);
if (err) { if (err) {
...@@ -214,8 +176,7 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -214,8 +176,7 @@ static int nsim_dev_resources_register(struct devlink *devlink)
return err; return err;
} }
n = nsim_fib_get_val(net, NSIM_RESOURCE_IPV6_FIB_RULES, true); err = devlink_resource_register(devlink, "fib-rules", (u64)-1,
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);
if (err) { if (err) {
...@@ -223,22 +184,6 @@ static int nsim_dev_resources_register(struct devlink *devlink) ...@@ -223,22 +184,6 @@ static int nsim_dev_resources_register(struct devlink *devlink)
return err; return err;
} }
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB,
nsim_dev_ipv4_fib_resource_occ_get,
net);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB_RULES,
nsim_dev_ipv4_fib_rules_res_occ_get,
net);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB,
nsim_dev_ipv6_fib_resource_occ_get,
net);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB_RULES,
nsim_dev_ipv6_fib_rules_res_occ_get,
net);
out: out:
return err; return err;
} }
...@@ -533,11 +478,11 @@ static int nsim_dev_reload_down(struct devlink *devlink, ...@@ -533,11 +478,11 @@ static int nsim_dev_reload_down(struct devlink *devlink,
static int nsim_dev_reload_up(struct devlink *devlink, static int nsim_dev_reload_up(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) {
...@@ -546,7 +491,8 @@ static int nsim_dev_reload_up(struct devlink *devlink, ...@@ -546,7 +491,8 @@ static int nsim_dev_reload_up(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(net, res_ids[i], val, extack); err = nsim_fib_set_max(nsim_dev->fib_data,
res_ids[i], val, extack);
if (err) if (err)
return err; return err;
} }
...@@ -681,9 +627,15 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count) ...@@ -681,9 +627,15 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
if (err) if (err)
goto err_devlink_free; goto err_devlink_free;
nsim_dev->fib_data = nsim_fib_create(devlink);
if (IS_ERR(nsim_dev->fib_data)) {
err = PTR_ERR(nsim_dev->fib_data);
goto err_resources_unregister;
}
err = devlink_register(devlink, &nsim_bus_dev->dev); err = devlink_register(devlink, &nsim_bus_dev->dev);
if (err) if (err)
goto err_resources_unregister; goto err_fib_destroy;
err = devlink_params_register(devlink, nsim_devlink_params, err = devlink_params_register(devlink, nsim_devlink_params,
ARRAY_SIZE(nsim_devlink_params)); ARRAY_SIZE(nsim_devlink_params));
...@@ -721,6 +673,8 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count) ...@@ -721,6 +673,8 @@ nsim_dev_create(struct nsim_bus_dev *nsim_bus_dev, unsigned int port_count)
ARRAY_SIZE(nsim_devlink_params)); ARRAY_SIZE(nsim_devlink_params));
err_dl_unregister: err_dl_unregister:
devlink_unregister(devlink); devlink_unregister(devlink);
err_fib_destroy:
nsim_fib_destroy(devlink, nsim_dev->fib_data);
err_resources_unregister: err_resources_unregister:
devlink_resources_unregister(devlink, NULL); devlink_resources_unregister(devlink, NULL);
err_devlink_free: err_devlink_free:
...@@ -739,6 +693,7 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev) ...@@ -739,6 +693,7 @@ static void nsim_dev_destroy(struct nsim_dev *nsim_dev)
devlink_params_unregister(devlink, nsim_devlink_params, devlink_params_unregister(devlink, nsim_devlink_params,
ARRAY_SIZE(nsim_devlink_params)); ARRAY_SIZE(nsim_devlink_params));
devlink_unregister(devlink); devlink_unregister(devlink);
nsim_fib_destroy(devlink, nsim_dev->fib_data);
devlink_resources_unregister(devlink, NULL); devlink_resources_unregister(devlink, NULL);
mutex_destroy(&nsim_dev->port_list_lock); mutex_destroy(&nsim_dev->port_list_lock);
devlink_free(devlink); devlink_free(devlink);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +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 <net/net_namespace.h>
#include "netdevsim.h" #include "netdevsim.h"
...@@ -33,15 +33,14 @@ struct nsim_per_fib_data { ...@@ -33,15 +33,14 @@ 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;
}; };
static unsigned int nsim_fib_net_id; u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
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) {
...@@ -64,10 +63,10 @@ u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max) ...@@ -64,10 +63,10 @@ u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max)
return max ? entry->max : entry->num; return max ? entry->max : entry->num;
} }
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)
{ {
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;
...@@ -120,9 +119,9 @@ static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add, ...@@ -120,9 +119,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 fib_notifier_info *info, bool add) static int nsim_fib_rule_event(struct nsim_fib_data *data,
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;
...@@ -157,9 +156,9 @@ static int nsim_fib_account(struct nsim_fib_entry *entry, bool add, ...@@ -157,9 +156,9 @@ static int nsim_fib_account(struct nsim_fib_entry *entry, bool add,
return err; return err;
} }
static int nsim_fib_event(struct fib_notifier_info *info, bool add) static int nsim_fib_event(struct nsim_fib_data *data,
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;
...@@ -178,18 +177,25 @@ static int nsim_fib_event(struct fib_notifier_info *info, bool add) ...@@ -178,18 +177,25 @@ static int nsim_fib_event(struct fib_notifier_info *info, bool add)
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;
if (!net_eq(info->net, &init_net))
return NOTIFY_DONE;
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(info, event == FIB_EVENT_RULE_ADD); err = nsim_fib_rule_event(data, info,
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(info, event == FIB_EVENT_ENTRY_ADD); err = nsim_fib_event(data, info,
event == FIB_EVENT_ENTRY_ADD);
break; break;
} }
...@@ -199,68 +205,98 @@ static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event, ...@@ -199,68 +205,98 @@ 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; struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
struct net *net; fib_nb);
rcu_read_lock(); data->ipv4.fib.num = 0ULL;
for_each_net_rcu(net) { data->ipv4.rules.num = 0ULL;
data = net_generic(net, nsim_fib_net_id); data->ipv6.fib.num = 0ULL;
data->ipv6.rules.num = 0ULL;
}
data->ipv4.fib.num = 0ULL; static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
data->ipv4.rules.num = 0ULL; {
struct nsim_fib_data *data = priv;
data->ipv6.fib.num = 0ULL; return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
data->ipv6.rules.num = 0ULL;
}
rcu_read_unlock();
} }
static struct notifier_block nsim_fib_nb = { static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
.notifier_call = nsim_fib_event_nb,
};
/* Initialize per network namespace state */
static int __net_init nsim_fib_netns_init(struct net *net)
{ {
struct nsim_fib_data *data = net_generic(net, nsim_fib_net_id); struct nsim_fib_data *data = priv;
data->ipv4.fib.max = (u64)-1; return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
data->ipv4.rules.max = (u64)-1; }
data->ipv6.fib.max = (u64)-1; static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
data->ipv6.rules.max = (u64)-1; {
struct nsim_fib_data *data = priv;
return 0; return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
} }
static struct pernet_operations nsim_fib_net_ops = { static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
.init = nsim_fib_netns_init,
.id = &nsim_fib_net_id,
.size = sizeof(struct nsim_fib_data),
};
void nsim_fib_exit(void)
{ {
unregister_pernet_subsys(&nsim_fib_net_ops); struct nsim_fib_data *data = priv;
unregister_fib_notifier(&nsim_fib_nb);
return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
} }
int nsim_fib_init(void) struct nsim_fib_data *nsim_fib_create(struct devlink *devlink)
{ {
struct nsim_fib_data *data;
int err; int err;
err = register_pernet_subsys(&nsim_fib_net_ops); data = kzalloc(sizeof(*data), GFP_KERNEL);
if (err < 0) { if (!data)
pr_err("Failed to register pernet subsystem\n"); return ERR_PTR(-ENOMEM);
goto err_out;
}
err = register_fib_notifier(&nsim_fib_nb, nsim_fib_dump_inconsistent); data->ipv4.fib.max = (u64)-1;
if (err < 0) { data->ipv4.rules.max = (u64)-1;
data->ipv6.fib.max = (u64)-1;
data->ipv6.rules.max = (u64)-1;
data->fib_nb.notifier_call = nsim_fib_event_nb;
err = register_fib_notifier(&data->fib_nb, nsim_fib_dump_inconsistent);
if (err) {
pr_err("Failed to register fib notifier\n"); pr_err("Failed to register fib notifier\n");
goto err_out; goto err_out;
} }
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB,
nsim_fib_ipv4_resource_occ_get,
data);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV4_FIB_RULES,
nsim_fib_ipv4_rules_res_occ_get,
data);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB,
nsim_fib_ipv6_resource_occ_get,
data);
devlink_resource_occ_get_register(devlink,
NSIM_RESOURCE_IPV6_FIB_RULES,
nsim_fib_ipv6_rules_res_occ_get,
data);
return data;
err_out: err_out:
return err; kfree(data);
return ERR_PTR(err);
}
void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data)
{
devlink_resource_occ_get_unregister(devlink,
NSIM_RESOURCE_IPV6_FIB_RULES);
devlink_resource_occ_get_unregister(devlink,
NSIM_RESOURCE_IPV6_FIB);
devlink_resource_occ_get_unregister(devlink,
NSIM_RESOURCE_IPV4_FIB_RULES);
devlink_resource_occ_get_unregister(devlink,
NSIM_RESOURCE_IPV4_FIB);
unregister_fib_notifier(&data->fib_nb);
kfree(data);
} }
...@@ -357,18 +357,12 @@ static int __init nsim_module_init(void) ...@@ -357,18 +357,12 @@ static int __init nsim_module_init(void)
if (err) if (err)
goto err_dev_exit; goto err_dev_exit;
err = nsim_fib_init();
if (err)
goto err_bus_exit;
err = rtnl_link_register(&nsim_link_ops); err = rtnl_link_register(&nsim_link_ops);
if (err) if (err)
goto err_fib_exit; goto err_bus_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:
...@@ -379,7 +373,6 @@ static int __init nsim_module_init(void) ...@@ -379,7 +373,6 @@ 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();
} }
......
...@@ -173,10 +173,12 @@ int nsim_dev_port_add(struct nsim_bus_dev *nsim_bus_dev, ...@@ -173,10 +173,12 @@ 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);
int nsim_fib_init(void); struct nsim_fib_data *nsim_fib_create(struct devlink *devlink);
void nsim_fib_exit(void); void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data);
u64 nsim_fib_get_val(struct net *net, enum nsim_resource_id res_id, bool max); u64 nsim_fib_get_val(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, bool max);
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