Skip to content

Commit 04b1d4e

Browse files
idoschdavem330
authored andcommitted
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 <idosch@mellanox.com> Signed-off-by: Jiri Pirko <jiri@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 80cf0b4 commit 04b1d4e

13 files changed

Lines changed: 282 additions & 93 deletions

File tree

‎drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <net/addrconf.h>
5454
#include <net/ndisc.h>
5555
#include <net/ipv6.h>
56+
#include <net/fib_notifier.h>
5657

5758
#include "spectrum.h"
5859
#include "core.h"

‎drivers/net/ethernet/rocker/rocker_main.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <net/netevent.h>
3535
#include <net/arp.h>
3636
#include <net/fib_rules.h>
37+
#include <net/fib_notifier.h>
3738
#include <linux/io-64-nonatomic-lo-hi.h>
3839
#include <generated/utsrelease.h>
3940

‎include/net/fib_notifier.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef __NET_FIB_NOTIFIER_H
2+
#define __NET_FIB_NOTIFIER_H
3+
4+
#include <linux/types.h>
5+
#include <linux/notifier.h>
6+
#include <net/net_namespace.h>
7+
8+
struct fib_notifier_info {
9+
struct net *net;
10+
int family;
11+
};
12+
13+
enum fib_event_type {
14+
FIB_EVENT_ENTRY_REPLACE,
15+
FIB_EVENT_ENTRY_APPEND,
16+
FIB_EVENT_ENTRY_ADD,
17+
FIB_EVENT_ENTRY_DEL,
18+
FIB_EVENT_RULE_ADD,
19+
FIB_EVENT_RULE_DEL,
20+
FIB_EVENT_NH_ADD,
21+
FIB_EVENT_NH_DEL,
22+
};
23+
24+
struct fib_notifier_ops {
25+
int family;
26+
struct list_head list;
27+
unsigned int (*fib_seq_read)(struct net *net);
28+
int (*fib_dump)(struct net *net, struct notifier_block *nb);
29+
struct rcu_head rcu;
30+
};
31+
32+
int call_fib_notifier(struct notifier_block *nb, struct net *net,
33+
enum fib_event_type event_type,
34+
struct fib_notifier_info *info);
35+
int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
36+
struct fib_notifier_info *info);
37+
int register_fib_notifier(struct notifier_block *nb,
38+
void (*cb)(struct notifier_block *nb));
39+
int unregister_fib_notifier(struct notifier_block *nb);
40+
struct fib_notifier_ops *
41+
fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net);
42+
void fib_notifier_ops_unregister(struct fib_notifier_ops *ops);
43+
44+
#endif

‎include/net/ip_fib.h‎

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <net/flow.h>
2020
#include <linux/seq_file.h>
2121
#include <linux/rcupdate.h>
22+
#include <net/fib_notifier.h>
2223
#include <net/fib_rules.h>
2324
#include <net/inetpeer.h>
2425
#include <linux/percpu.h>
@@ -188,10 +189,6 @@ __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh);
188189
#define FIB_RES_PREFSRC(net, res) ((res).fi->fib_prefsrc ? : \
189190
FIB_RES_SADDR(net, res))
190191

191-
struct fib_notifier_info {
192-
struct net *net;
193-
};
194-
195192
struct fib_entry_notifier_info {
196193
struct fib_notifier_info info; /* must be first */
197194
u32 dst;
@@ -212,25 +209,14 @@ struct fib_nh_notifier_info {
212209
struct fib_nh *fib_nh;
213210
};
214211

215-
enum fib_event_type {
216-
FIB_EVENT_ENTRY_REPLACE,
217-
FIB_EVENT_ENTRY_APPEND,
218-
FIB_EVENT_ENTRY_ADD,
219-
FIB_EVENT_ENTRY_DEL,
220-
FIB_EVENT_RULE_ADD,
221-
FIB_EVENT_RULE_DEL,
222-
FIB_EVENT_NH_ADD,
223-
FIB_EVENT_NH_DEL,
224-
};
225-
226-
int register_fib_notifier(struct notifier_block *nb,
227-
void (*cb)(struct notifier_block *nb));
228-
int unregister_fib_notifier(struct notifier_block *nb);
229-
int call_fib_notifier(struct notifier_block *nb, struct net *net,
230-
enum fib_event_type event_type,
231-
struct fib_notifier_info *info);
232-
int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
212+
int call_fib4_notifier(struct notifier_block *nb, struct net *net,
213+
enum fib_event_type event_type,
233214
struct fib_notifier_info *info);
215+
int call_fib4_notifiers(struct net *net, enum fib_event_type event_type,
216+
struct fib_notifier_info *info);
217+
218+
int __net_init fib4_notifier_init(struct net *net);
219+
void __net_exit fib4_notifier_exit(struct net *net);
234220

235221
void fib_notify(struct net *net, struct notifier_block *nb);
236222
#ifdef CONFIG_IP_MULTIPLE_TABLES

‎include/net/net_namespace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct net {
8888
/* core fib_rules */
8989
struct list_head rules_ops;
9090

91+
struct list_head fib_notifier_ops; /* protected by net_mutex */
9192

9293
struct net_device *loopback_dev; /* The loopback */
9394
struct netns_core core;

‎include/net/netns/ipv4.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ struct netns_ipv4 {
159159
int sysctl_fib_multipath_hash_policy;
160160
#endif
161161

162+
struct fib_notifier_ops *notifier_ops;
162163
unsigned int fib_seq; /* protected by rtnl_mutex */
163164

164165
atomic_t rt_genid;

‎net/core/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o
99

1010
obj-y += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \
1111
neighbour.o rtnetlink.o utils.o link_watch.o filter.o \
12-
sock_diag.o dev_ioctl.o tso.o sock_reuseport.o
12+
sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \
13+
fib_notifier.o
1314

1415
obj-y += net-sysfs.o
1516
obj-$(CONFIG_PROC_FS) += net-procfs.o

‎net/core/fib_notifier.c‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <linux/rtnetlink.h>
2+
#include <linux/notifier.h>
3+
#include <linux/rcupdate.h>
4+
#include <linux/kernel.h>
5+
#include <linux/init.h>
6+
#include <net/net_namespace.h>
7+
#include <net/fib_notifier.h>
8+
9+
static ATOMIC_NOTIFIER_HEAD(fib_chain);
10+
11+
int call_fib_notifier(struct notifier_block *nb, struct net *net,
12+
enum fib_event_type event_type,
13+
struct fib_notifier_info *info)
14+
{
15+
info->net = net;
16+
return nb->notifier_call(nb, event_type, info);
17+
}
18+
EXPORT_SYMBOL(call_fib_notifier);
19+
20+
int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
21+
struct fib_notifier_info *info)
22+
{
23+
info->net = net;
24+
return atomic_notifier_call_chain(&fib_chain, event_type, info);
25+
}
26+
EXPORT_SYMBOL(call_fib_notifiers);
27+
28+
static unsigned int fib_seq_sum(void)
29+
{
30+
struct fib_notifier_ops *ops;
31+
unsigned int fib_seq = 0;
32+
struct net *net;
33+
34+
rtnl_lock();
35+
for_each_net(net) {
36+
list_for_each_entry(ops, &net->fib_notifier_ops, list)
37+
fib_seq += ops->fib_seq_read(net);
38+
}
39+
rtnl_unlock();
40+
41+
return fib_seq;
42+
}
43+
44+
static int fib_net_dump(struct net *net, struct notifier_block *nb)
45+
{
46+
struct fib_notifier_ops *ops;
47+
48+
list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
49+
int err = ops->fib_dump(net, nb);
50+
51+
if (err)
52+
return err;
53+
}
54+
55+
return 0;
56+
}
57+
58+
static bool fib_dump_is_consistent(struct notifier_block *nb,
59+
void (*cb)(struct notifier_block *nb),
60+
unsigned int fib_seq)
61+
{
62+
atomic_notifier_chain_register(&fib_chain, nb);
63+
if (fib_seq == fib_seq_sum())
64+
return true;
65+
atomic_notifier_chain_unregister(&fib_chain, nb);
66+
if (cb)
67+
cb(nb);
68+
return false;
69+
}
70+
71+
#define FIB_DUMP_MAX_RETRIES 5
72+
int register_fib_notifier(struct notifier_block *nb,
73+
void (*cb)(struct notifier_block *nb))
74+
{
75+
int retries = 0;
76+
int err;
77+
78+
do {
79+
unsigned int fib_seq = fib_seq_sum();
80+
struct net *net;
81+
82+
rcu_read_lock();
83+
for_each_net_rcu(net) {
84+
err = fib_net_dump(net, nb);
85+
if (err)
86+
goto err_fib_net_dump;
87+
}
88+
rcu_read_unlock();
89+
90+
if (fib_dump_is_consistent(nb, cb, fib_seq))
91+
return 0;
92+
} while (++retries < FIB_DUMP_MAX_RETRIES);
93+
94+
return -EBUSY;
95+
96+
err_fib_net_dump:
97+
rcu_read_unlock();
98+
return err;
99+
}
100+
EXPORT_SYMBOL(register_fib_notifier);
101+
102+
int unregister_fib_notifier(struct notifier_block *nb)
103+
{
104+
return atomic_notifier_chain_unregister(&fib_chain, nb);
105+
}
106+
EXPORT_SYMBOL(unregister_fib_notifier);
107+
108+
static int __fib_notifier_ops_register(struct fib_notifier_ops *ops,
109+
struct net *net)
110+
{
111+
struct fib_notifier_ops *o;
112+
113+
list_for_each_entry(o, &net->fib_notifier_ops, list)
114+
if (ops->family == o->family)
115+
return -EEXIST;
116+
list_add_tail_rcu(&ops->list, &net->fib_notifier_ops);
117+
return 0;
118+
}
119+
120+
struct fib_notifier_ops *
121+
fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net)
122+
{
123+
struct fib_notifier_ops *ops;
124+
int err;
125+
126+
ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
127+
if (!ops)
128+
return ERR_PTR(-ENOMEM);
129+
130+
err = __fib_notifier_ops_register(ops, net);
131+
if (err)
132+
goto err_register;
133+
134+
return ops;
135+
136+
err_register:
137+
kfree(ops);
138+
return ERR_PTR(err);
139+
}
140+
EXPORT_SYMBOL(fib_notifier_ops_register);
141+
142+
void fib_notifier_ops_unregister(struct fib_notifier_ops *ops)
143+
{
144+
list_del_rcu(&ops->list);
145+
kfree_rcu(ops, rcu);
146+
}
147+
EXPORT_SYMBOL(fib_notifier_ops_unregister);
148+
149+
static int __net_init fib_notifier_net_init(struct net *net)
150+
{
151+
INIT_LIST_HEAD(&net->fib_notifier_ops);
152+
return 0;
153+
}
154+
155+
static struct pernet_operations fib_notifier_net_ops = {
156+
.init = fib_notifier_net_init,
157+
};
158+
159+
static int __init fib_notifier_init(void)
160+
{
161+
return register_pernet_subsys(&fib_notifier_net_ops);
162+
}
163+
164+
subsys_initcall(fib_notifier_init);

‎net/ipv4/fib_frontend.c‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,22 +1247,28 @@ static int __net_init ip_fib_net_init(struct net *net)
12471247
int err;
12481248
size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
12491249

1250-
net->ipv4.fib_seq = 0;
1250+
err = fib4_notifier_init(net);
1251+
if (err)
1252+
return err;
12511253

12521254
/* Avoid false sharing : Use at least a full cache line */
12531255
size = max_t(size_t, size, L1_CACHE_BYTES);
12541256

12551257
net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
1256-
if (!net->ipv4.fib_table_hash)
1257-
return -ENOMEM;
1258+
if (!net->ipv4.fib_table_hash) {
1259+
err = -ENOMEM;
1260+
goto err_table_hash_alloc;
1261+
}
12581262

12591263
err = fib4_rules_init(net);
12601264
if (err < 0)
1261-
goto fail;
1265+
goto err_rules_init;
12621266
return 0;
12631267

1264-
fail:
1268+
err_rules_init:
12651269
kfree(net->ipv4.fib_table_hash);
1270+
err_table_hash_alloc:
1271+
fib4_notifier_exit(net);
12661272
return err;
12671273
}
12681274

@@ -1292,6 +1298,7 @@ static void ip_fib_net_exit(struct net *net)
12921298
#endif
12931299
rtnl_unlock();
12941300
kfree(net->ipv4.fib_table_hash);
1301+
fib4_notifier_exit(net);
12951302
}
12961303

12971304
static int __net_init fib_net_init(struct net *net)

0 commit comments

Comments
 (0)