2018-03-28 08:22:00 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Cumulus Networks. All rights reserved.
|
|
|
|
* Copyright (c) 2018 David Ahern <dsa@cumulusnetworks.com>
|
|
|
|
*
|
|
|
|
* This software is licensed under the GNU General License Version 2,
|
|
|
|
* June 1991 as shown in the file COPYING in the top-level directory of this
|
|
|
|
* source tree.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS"
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
|
|
|
|
* OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
|
|
|
|
* THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <net/fib_notifier.h>
|
|
|
|
#include <net/ip_fib.h>
|
|
|
|
#include <net/ip6_fib.h>
|
|
|
|
#include <net/fib_rules.h>
|
2019-10-03 16:49:26 +07:00
|
|
|
#include <net/net_namespace.h>
|
2018-03-28 08:22:00 +07:00
|
|
|
|
|
|
|
#include "netdevsim.h"
|
|
|
|
|
|
|
|
struct nsim_fib_entry {
|
|
|
|
u64 max;
|
|
|
|
u64 num;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsim_per_fib_data {
|
|
|
|
struct nsim_fib_entry fib;
|
|
|
|
struct nsim_fib_entry rules;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsim_fib_data {
|
2019-10-03 16:49:26 +07:00
|
|
|
struct notifier_block fib_nb;
|
2018-03-28 08:22:00 +07:00
|
|
|
struct nsim_per_fib_data ipv4;
|
|
|
|
struct nsim_per_fib_data ipv6;
|
|
|
|
};
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
u64 nsim_fib_get_val(struct nsim_fib_data *fib_data,
|
|
|
|
enum nsim_resource_id res_id, bool max)
|
2018-03-28 08:22:00 +07:00
|
|
|
{
|
|
|
|
struct nsim_fib_entry *entry;
|
|
|
|
|
|
|
|
switch (res_id) {
|
|
|
|
case NSIM_RESOURCE_IPV4_FIB:
|
|
|
|
entry = &fib_data->ipv4.fib;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV4_FIB_RULES:
|
|
|
|
entry = &fib_data->ipv4.rules;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV6_FIB:
|
|
|
|
entry = &fib_data->ipv6.fib;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV6_FIB_RULES:
|
|
|
|
entry = &fib_data->ipv6.rules;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return max ? entry->max : entry->num;
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:36 +07:00
|
|
|
static void nsim_fib_set_max(struct nsim_fib_data *fib_data,
|
|
|
|
enum nsim_resource_id res_id, u64 val)
|
2018-03-28 08:22:00 +07:00
|
|
|
{
|
|
|
|
struct nsim_fib_entry *entry;
|
|
|
|
|
|
|
|
switch (res_id) {
|
|
|
|
case NSIM_RESOURCE_IPV4_FIB:
|
|
|
|
entry = &fib_data->ipv4.fib;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV4_FIB_RULES:
|
|
|
|
entry = &fib_data->ipv4.rules;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV6_FIB:
|
|
|
|
entry = &fib_data->ipv6.fib;
|
|
|
|
break;
|
|
|
|
case NSIM_RESOURCE_IPV6_FIB_RULES:
|
|
|
|
entry = &fib_data->ipv6.rules;
|
|
|
|
break;
|
|
|
|
default:
|
2019-10-03 16:49:36 +07:00
|
|
|
WARN_ON(1);
|
|
|
|
return;
|
2018-06-05 22:14:10 +07:00
|
|
|
}
|
2019-10-03 16:49:36 +07:00
|
|
|
entry->max = val;
|
2018-03-28 08:22:00 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nsim_fib_rule_account(struct nsim_fib_entry *entry, bool add,
|
|
|
|
struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
if (entry->num < entry->max) {
|
|
|
|
entry->num++;
|
|
|
|
} else {
|
|
|
|
err = -ENOSPC;
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib rule entries");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry->num--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static int nsim_fib_rule_event(struct nsim_fib_data *data,
|
|
|
|
struct fib_notifier_info *info, bool add)
|
2018-03-28 08:22:00 +07:00
|
|
|
{
|
|
|
|
struct netlink_ext_ack *extack = info->extack;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (info->family) {
|
|
|
|
case AF_INET:
|
|
|
|
err = nsim_fib_rule_account(&data->ipv4.rules, add, extack);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
err = nsim_fib_rule_account(&data->ipv6.rules, add, extack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nsim_fib_account(struct nsim_fib_entry *entry, bool add,
|
|
|
|
struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
if (entry->num < entry->max) {
|
|
|
|
entry->num++;
|
|
|
|
} else {
|
|
|
|
err = -ENOSPC;
|
|
|
|
NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported fib entries");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entry->num--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static int nsim_fib_event(struct nsim_fib_data *data,
|
|
|
|
struct fib_notifier_info *info, bool add)
|
2018-03-28 08:22:00 +07:00
|
|
|
{
|
|
|
|
struct netlink_ext_ack *extack = info->extack;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (info->family) {
|
|
|
|
case AF_INET:
|
|
|
|
err = nsim_fib_account(&data->ipv4.fib, add, extack);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
err = nsim_fib_account(&data->ipv6.fib, add, extack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nsim_fib_event_nb(struct notifier_block *nb, unsigned long event,
|
|
|
|
void *ptr)
|
|
|
|
{
|
2019-10-03 16:49:26 +07:00
|
|
|
struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
|
|
|
|
fib_nb);
|
2018-03-28 08:22:00 +07:00
|
|
|
struct fib_notifier_info *info = ptr;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case FIB_EVENT_RULE_ADD: /* fall through */
|
|
|
|
case FIB_EVENT_RULE_DEL:
|
2019-10-03 16:49:26 +07:00
|
|
|
err = nsim_fib_rule_event(data, info,
|
|
|
|
event == FIB_EVENT_RULE_ADD);
|
2018-03-28 08:22:00 +07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FIB_EVENT_ENTRY_ADD: /* fall through */
|
|
|
|
case FIB_EVENT_ENTRY_DEL:
|
2019-10-03 16:49:26 +07:00
|
|
|
err = nsim_fib_event(data, info,
|
|
|
|
event == FIB_EVENT_ENTRY_ADD);
|
2018-03-28 08:22:00 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifier_from_errno(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* inconsistent dump, trying again */
|
|
|
|
static void nsim_fib_dump_inconsistent(struct notifier_block *nb)
|
|
|
|
{
|
2019-10-03 16:49:26 +07:00
|
|
|
struct nsim_fib_data *data = container_of(nb, struct nsim_fib_data,
|
|
|
|
fib_nb);
|
2019-08-07 02:15:17 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
data->ipv4.fib.num = 0ULL;
|
|
|
|
data->ipv4.rules.num = 0ULL;
|
|
|
|
data->ipv6.fib.num = 0ULL;
|
|
|
|
data->ipv6.rules.num = 0ULL;
|
|
|
|
}
|
2019-08-07 02:15:17 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static u64 nsim_fib_ipv4_resource_occ_get(void *priv)
|
|
|
|
{
|
|
|
|
struct nsim_fib_data *data = priv;
|
2018-03-28 08:22:00 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB, false);
|
2018-03-28 08:22:00 +07:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static u64 nsim_fib_ipv4_rules_res_occ_get(void *priv)
|
2019-08-07 02:15:17 +07:00
|
|
|
{
|
2019-10-03 16:49:26 +07:00
|
|
|
struct nsim_fib_data *data = priv;
|
2018-03-28 08:22:00 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
return nsim_fib_get_val(data, NSIM_RESOURCE_IPV4_FIB_RULES, false);
|
|
|
|
}
|
2018-03-28 08:22:00 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static u64 nsim_fib_ipv6_resource_occ_get(void *priv)
|
|
|
|
{
|
|
|
|
struct nsim_fib_data *data = priv;
|
2018-03-28 08:22:00 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB, false);
|
2019-08-07 02:15:17 +07:00
|
|
|
}
|
2018-03-28 08:22:00 +07:00
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
static u64 nsim_fib_ipv6_rules_res_occ_get(void *priv)
|
2019-08-07 02:15:17 +07:00
|
|
|
{
|
2019-10-03 16:49:26 +07:00
|
|
|
struct nsim_fib_data *data = priv;
|
|
|
|
|
|
|
|
return nsim_fib_get_val(data, NSIM_RESOURCE_IPV6_FIB_RULES, false);
|
2019-04-25 20:59:42 +07:00
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:36 +07:00
|
|
|
static void nsim_fib_set_max_all(struct nsim_fib_data *data,
|
|
|
|
struct devlink *devlink)
|
|
|
|
{
|
|
|
|
enum nsim_resource_id res_ids[] = {
|
|
|
|
NSIM_RESOURCE_IPV4_FIB, NSIM_RESOURCE_IPV4_FIB_RULES,
|
|
|
|
NSIM_RESOURCE_IPV6_FIB, NSIM_RESOURCE_IPV6_FIB_RULES
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(res_ids); i++) {
|
|
|
|
int err;
|
|
|
|
u64 val;
|
|
|
|
|
|
|
|
err = devlink_resource_size_get(devlink, res_ids[i], &val);
|
|
|
|
if (err)
|
|
|
|
val = (u64) -1;
|
|
|
|
nsim_fib_set_max(data, res_ids[i], val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nsim_fib_data *nsim_fib_create(struct devlink *devlink,
|
|
|
|
struct netlink_ext_ack *extack)
|
2019-04-25 20:59:42 +07:00
|
|
|
{
|
2019-10-03 16:49:26 +07:00
|
|
|
struct nsim_fib_data *data;
|
2019-08-07 02:15:17 +07:00
|
|
|
int err;
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
data = kzalloc(sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2019-08-07 02:15:17 +07:00
|
|
|
|
2019-10-03 16:49:36 +07:00
|
|
|
nsim_fib_set_max_all(data, devlink);
|
2019-10-03 16:49:26 +07:00
|
|
|
|
|
|
|
data->fib_nb.notifier_call = nsim_fib_event_nb;
|
2019-10-03 16:49:38 +07:00
|
|
|
err = register_fib_notifier(devlink_net(devlink), &data->fib_nb,
|
2019-10-03 16:49:36 +07:00
|
|
|
nsim_fib_dump_inconsistent, extack);
|
2019-10-03 16:49:26 +07:00
|
|
|
if (err) {
|
2019-08-07 02:15:17 +07:00
|
|
|
pr_err("Failed to register fib notifier\n");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
2019-10-03 16:49:26 +07:00
|
|
|
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;
|
|
|
|
|
2019-08-07 02:15:17 +07:00
|
|
|
err_out:
|
2019-10-03 16:49:26 +07:00
|
|
|
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);
|
2019-10-03 16:49:38 +07:00
|
|
|
unregister_fib_notifier(devlink_net(devlink), &data->fib_nb);
|
2019-10-03 16:49:26 +07:00
|
|
|
kfree(data);
|
2018-03-28 08:22:00 +07:00
|
|
|
}
|