forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: core: Make the FIB notification chain generic
The FIB notification chain is currently soley used by IPv4 code. However, we're going to introduce IPv6 FIB offload support, which requires these notification as well. As explained in commit c3852ef ("ipv4: fib: Replay events when registering FIB notifier"), upon registration to the chain, the callee receives a full dump of the FIB tables and rules by traversing all the net namespaces. The integrity of the dump is ensured by a per-namespace sequence counter that is incremented whenever a change to the tables or rules occurs. In order to allow more address families to use the chain, each family is expected to register its fib_notifier_ops in its pernet init. These operations allow the common code to read the family's sequence counter as well as dump its tables and rules in the given net namespace. Additionally, a 'family' parameter is added to sent notifications, so that listeners could distinguish between the different families. Implement the common code that allows listeners to register to the chain and for address families to register their fib_notifier_ops. Subsequent patches will implement these operations in IPv6. In the future, ipmr and ip6mr will be extended to provide these notifications as well. Signed-off-by: Ido Schimmel <[email protected]> Signed-off-by: Jiri Pirko <[email protected]> Signed-off-by: David S. Miller <[email protected]>
- Loading branch information
Showing
13 changed files
with
282 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#ifndef __NET_FIB_NOTIFIER_H | ||
#define __NET_FIB_NOTIFIER_H | ||
|
||
#include <linux/types.h> | ||
#include <linux/notifier.h> | ||
#include <net/net_namespace.h> | ||
|
||
struct fib_notifier_info { | ||
struct net *net; | ||
int family; | ||
}; | ||
|
||
enum fib_event_type { | ||
FIB_EVENT_ENTRY_REPLACE, | ||
FIB_EVENT_ENTRY_APPEND, | ||
FIB_EVENT_ENTRY_ADD, | ||
FIB_EVENT_ENTRY_DEL, | ||
FIB_EVENT_RULE_ADD, | ||
FIB_EVENT_RULE_DEL, | ||
FIB_EVENT_NH_ADD, | ||
FIB_EVENT_NH_DEL, | ||
}; | ||
|
||
struct fib_notifier_ops { | ||
int family; | ||
struct list_head list; | ||
unsigned int (*fib_seq_read)(struct net *net); | ||
int (*fib_dump)(struct net *net, struct notifier_block *nb); | ||
struct rcu_head rcu; | ||
}; | ||
|
||
int call_fib_notifier(struct notifier_block *nb, struct net *net, | ||
enum fib_event_type event_type, | ||
struct fib_notifier_info *info); | ||
int call_fib_notifiers(struct net *net, enum fib_event_type event_type, | ||
struct fib_notifier_info *info); | ||
int register_fib_notifier(struct notifier_block *nb, | ||
void (*cb)(struct notifier_block *nb)); | ||
int unregister_fib_notifier(struct notifier_block *nb); | ||
struct fib_notifier_ops * | ||
fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net); | ||
void fib_notifier_ops_unregister(struct fib_notifier_ops *ops); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#include <linux/rtnetlink.h> | ||
#include <linux/notifier.h> | ||
#include <linux/rcupdate.h> | ||
#include <linux/kernel.h> | ||
#include <linux/init.h> | ||
#include <net/net_namespace.h> | ||
#include <net/fib_notifier.h> | ||
|
||
static ATOMIC_NOTIFIER_HEAD(fib_chain); | ||
|
||
int call_fib_notifier(struct notifier_block *nb, struct net *net, | ||
enum fib_event_type event_type, | ||
struct fib_notifier_info *info) | ||
{ | ||
info->net = net; | ||
return nb->notifier_call(nb, event_type, info); | ||
} | ||
EXPORT_SYMBOL(call_fib_notifier); | ||
|
||
int call_fib_notifiers(struct net *net, enum fib_event_type event_type, | ||
struct fib_notifier_info *info) | ||
{ | ||
info->net = net; | ||
return atomic_notifier_call_chain(&fib_chain, event_type, info); | ||
} | ||
EXPORT_SYMBOL(call_fib_notifiers); | ||
|
||
static unsigned int fib_seq_sum(void) | ||
{ | ||
struct fib_notifier_ops *ops; | ||
unsigned int fib_seq = 0; | ||
struct net *net; | ||
|
||
rtnl_lock(); | ||
for_each_net(net) { | ||
list_for_each_entry(ops, &net->fib_notifier_ops, list) | ||
fib_seq += ops->fib_seq_read(net); | ||
} | ||
rtnl_unlock(); | ||
|
||
return fib_seq; | ||
} | ||
|
||
static int fib_net_dump(struct net *net, struct notifier_block *nb) | ||
{ | ||
struct fib_notifier_ops *ops; | ||
|
||
list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) { | ||
int err = ops->fib_dump(net, nb); | ||
|
||
if (err) | ||
return err; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static bool fib_dump_is_consistent(struct notifier_block *nb, | ||
void (*cb)(struct notifier_block *nb), | ||
unsigned int fib_seq) | ||
{ | ||
atomic_notifier_chain_register(&fib_chain, nb); | ||
if (fib_seq == fib_seq_sum()) | ||
return true; | ||
atomic_notifier_chain_unregister(&fib_chain, nb); | ||
if (cb) | ||
cb(nb); | ||
return false; | ||
} | ||
|
||
#define FIB_DUMP_MAX_RETRIES 5 | ||
int register_fib_notifier(struct notifier_block *nb, | ||
void (*cb)(struct notifier_block *nb)) | ||
{ | ||
int retries = 0; | ||
int err; | ||
|
||
do { | ||
unsigned int fib_seq = fib_seq_sum(); | ||
struct net *net; | ||
|
||
rcu_read_lock(); | ||
for_each_net_rcu(net) { | ||
err = fib_net_dump(net, nb); | ||
if (err) | ||
goto err_fib_net_dump; | ||
} | ||
rcu_read_unlock(); | ||
|
||
if (fib_dump_is_consistent(nb, cb, fib_seq)) | ||
return 0; | ||
} while (++retries < FIB_DUMP_MAX_RETRIES); | ||
|
||
return -EBUSY; | ||
|
||
err_fib_net_dump: | ||
rcu_read_unlock(); | ||
return err; | ||
} | ||
EXPORT_SYMBOL(register_fib_notifier); | ||
|
||
int unregister_fib_notifier(struct notifier_block *nb) | ||
{ | ||
return atomic_notifier_chain_unregister(&fib_chain, nb); | ||
} | ||
EXPORT_SYMBOL(unregister_fib_notifier); | ||
|
||
static int __fib_notifier_ops_register(struct fib_notifier_ops *ops, | ||
struct net *net) | ||
{ | ||
struct fib_notifier_ops *o; | ||
|
||
list_for_each_entry(o, &net->fib_notifier_ops, list) | ||
if (ops->family == o->family) | ||
return -EEXIST; | ||
list_add_tail_rcu(&ops->list, &net->fib_notifier_ops); | ||
return 0; | ||
} | ||
|
||
struct fib_notifier_ops * | ||
fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net) | ||
{ | ||
struct fib_notifier_ops *ops; | ||
int err; | ||
|
||
ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL); | ||
if (!ops) | ||
return ERR_PTR(-ENOMEM); | ||
|
||
err = __fib_notifier_ops_register(ops, net); | ||
if (err) | ||
goto err_register; | ||
|
||
return ops; | ||
|
||
err_register: | ||
kfree(ops); | ||
return ERR_PTR(err); | ||
} | ||
EXPORT_SYMBOL(fib_notifier_ops_register); | ||
|
||
void fib_notifier_ops_unregister(struct fib_notifier_ops *ops) | ||
{ | ||
list_del_rcu(&ops->list); | ||
kfree_rcu(ops, rcu); | ||
} | ||
EXPORT_SYMBOL(fib_notifier_ops_unregister); | ||
|
||
static int __net_init fib_notifier_net_init(struct net *net) | ||
{ | ||
INIT_LIST_HEAD(&net->fib_notifier_ops); | ||
return 0; | ||
} | ||
|
||
static struct pernet_operations fib_notifier_net_ops = { | ||
.init = fib_notifier_net_init, | ||
}; | ||
|
||
static int __init fib_notifier_init(void) | ||
{ | ||
return register_pernet_subsys(&fib_notifier_net_ops); | ||
} | ||
|
||
subsys_initcall(fib_notifier_init); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.