Commit 792d1932e319ff8ba01361e7d151b1794c55c31f
Committed by
David S. Miller
1 parent
3687b1dc6f
Exists in
master
and in
39 other branches
[NET]: Network Event Notifier Mechanism.
This patch uses notifier blocks to implement a network event notifier mechanism. Clients register their callback function by calling register_netevent_notifier() like this: static struct notifier_block nb = { .notifier_call = my_callback_func }; ... register_netevent_notifier(&nb); Signed-off-by: Tom Tucker <tom@opengridcomputing.com> Signed-off-by: Steve Wise <swise@opengridcomputing.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Showing 2 changed files with 102 additions and 0 deletions Side-by-side Diff
include/net/netevent.h
1 | +#ifndef _NET_EVENT_H | |
2 | +#define _NET_EVENT_H | |
3 | + | |
4 | +/* | |
5 | + * Generic netevent notifiers | |
6 | + * | |
7 | + * Authors: | |
8 | + * Tom Tucker <tom@opengridcomputing.com> | |
9 | + * Steve Wise <swise@opengridcomputing.com> | |
10 | + * | |
11 | + * Changes: | |
12 | + */ | |
13 | +#ifdef __KERNEL__ | |
14 | + | |
15 | +#include <net/dst.h> | |
16 | + | |
17 | +struct netevent_redirect { | |
18 | + struct dst_entry *old; | |
19 | + struct dst_entry *new; | |
20 | +}; | |
21 | + | |
22 | +enum netevent_notif_type { | |
23 | + NETEVENT_NEIGH_UPDATE = 1, /* arg is struct neighbour ptr */ | |
24 | + NETEVENT_PMTU_UPDATE, /* arg is struct dst_entry ptr */ | |
25 | + NETEVENT_REDIRECT, /* arg is struct netevent_redirect ptr */ | |
26 | +}; | |
27 | + | |
28 | +extern int register_netevent_notifier(struct notifier_block *nb); | |
29 | +extern int unregister_netevent_notifier(struct notifier_block *nb); | |
30 | +extern int call_netevent_notifiers(unsigned long val, void *v); | |
31 | + | |
32 | +#endif | |
33 | +#endif |
net/core/netevent.c
1 | +/* | |
2 | + * Network event notifiers | |
3 | + * | |
4 | + * Authors: | |
5 | + * Tom Tucker <tom@opengridcomputing.com> | |
6 | + * Steve Wise <swise@opengridcomputing.com> | |
7 | + * | |
8 | + * This program is free software; you can redistribute it and/or | |
9 | + * modify it under the terms of the GNU General Public License | |
10 | + * as published by the Free Software Foundation; either version | |
11 | + * 2 of the License, or (at your option) any later version. | |
12 | + * | |
13 | + * Fixes: | |
14 | + */ | |
15 | + | |
16 | +#include <linux/rtnetlink.h> | |
17 | +#include <linux/notifier.h> | |
18 | + | |
19 | +static ATOMIC_NOTIFIER_HEAD(netevent_notif_chain); | |
20 | + | |
21 | +/** | |
22 | + * register_netevent_notifier - register a netevent notifier block | |
23 | + * @nb: notifier | |
24 | + * | |
25 | + * Register a notifier to be called when a netevent occurs. | |
26 | + * The notifier passed is linked into the kernel structures and must | |
27 | + * not be reused until it has been unregistered. A negative errno code | |
28 | + * is returned on a failure. | |
29 | + */ | |
30 | +int register_netevent_notifier(struct notifier_block *nb) | |
31 | +{ | |
32 | + int err; | |
33 | + | |
34 | + err = atomic_notifier_chain_register(&netevent_notif_chain, nb); | |
35 | + return err; | |
36 | +} | |
37 | + | |
38 | +/** | |
39 | + * netevent_unregister_notifier - unregister a netevent notifier block | |
40 | + * @nb: notifier | |
41 | + * | |
42 | + * Unregister a notifier previously registered by | |
43 | + * register_neigh_notifier(). The notifier is unlinked into the | |
44 | + * kernel structures and may then be reused. A negative errno code | |
45 | + * is returned on a failure. | |
46 | + */ | |
47 | + | |
48 | +int unregister_netevent_notifier(struct notifier_block *nb) | |
49 | +{ | |
50 | + return atomic_notifier_chain_unregister(&netevent_notif_chain, nb); | |
51 | +} | |
52 | + | |
53 | +/** | |
54 | + * call_netevent_notifiers - call all netevent notifier blocks | |
55 | + * @val: value passed unmodified to notifier function | |
56 | + * @v: pointer passed unmodified to notifier function | |
57 | + * | |
58 | + * Call all neighbour notifier blocks. Parameters and return value | |
59 | + * are as for notifier_call_chain(). | |
60 | + */ | |
61 | + | |
62 | +int call_netevent_notifiers(unsigned long val, void *v) | |
63 | +{ | |
64 | + return atomic_notifier_call_chain(&netevent_notif_chain, val, v); | |
65 | +} | |
66 | + | |
67 | +EXPORT_SYMBOL_GPL(register_netevent_notifier); | |
68 | +EXPORT_SYMBOL_GPL(unregister_netevent_notifier); | |
69 | +EXPORT_SYMBOL_GPL(call_netevent_notifiers); |