Skip to contentSkip to conversation

fib_rules: Flush route cache after rule modifications

NET Thomas Graf 2 participants 3 messages Mar 27, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

The results of FIB rules lookups are cached in the routing cache except for IPv6 as no such cache exists. So far, it was the responsibility of the user to flush the cache after modifying any rules. This lead to many false bug reports due to misunderstanding of this concept.

This patch automatically flushes the route cache after inserting or deleting a rule.

include/net/fib_rules.h +4 -0
Index: net-2.6.22/include/net/fib_rules.h
===================================================================
@@ -59,6 +59,10 @@ struct fib_rules_ops
 	u32			(*default_pref)(void);
 	size_t			(*nlmsg_payload)(struct fib_rule *);
 
+	/* Called after modifications to the rules set, must flush
+	 * the route cache if one exists. */
+	void			(*flush_cache)(void);
+
 	int			nlgroup;
 	struct nla_policy	*policy;
 	struct list_head	*rules_list;
net/core/fib_rules.c +8 -0
Index: net-2.6.22/net/core/fib_rules.c
===================================================================
@@ -44,6 +44,12 @@ static void rules_ops_put(struct fib_rul
 		module_put(ops->owner);
 }
 
+static void flush_route_cache(struct fib_rules_ops *ops)
+{
+	if (ops->flush_cache)
+		ops->flush_cache();
+}
+
 int fib_rules_register(struct fib_rules_ops *ops)
 {
 	int err = -EEXIST;
@@ -315,6 +321,7 @@ static int fib_nl_newrule(struct sk_buff
 
 	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
 	rules_ops_put(ops);
+	flush_route_cache(ops);
 	return 0;
 
 errout_free:
@@ -405,6 +412,7 @@ static int fib_nl_delrule(struct sk_buff
 				   NETLINK_CB(skb).pid);
 		fib_rule_put(rule);
 		rules_ops_put(ops);
+		flush_route_cache(ops);
 		return 0;
 	}
 
net/ipv4/fib_rules.c +6 -0
Index: net-2.6.22/net/ipv4/fib_rules.c
===================================================================
@@ -298,6 +298,11 @@ static size_t fib4_rule_nlmsg_payload(st
 	       + nla_total_size(4); /* flow */
 }
 
+static void fib4_rule_flush_cache(void)
+{
+	rt_cache_flush(0);
+}
+
 static struct fib_rules_ops fib4_rules_ops = {
 	.family		= AF_INET,
 	.rule_size	= sizeof(struct fib4_rule),
@@ -309,6 +314,7 @@ static struct fib_rules_ops fib4_rules_o
 	.fill		= fib4_rule_fill,
 	.default_pref	= fib4_rule_default_pref,
 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
+	.flush_cache	= fib4_rule_flush_cache,
 	.nlgroup	= RTNLGRP_IPV4_RULE,
 	.policy		= fib4_rule_policy,
 	.rules_list	= &fib4_rules,
net/decnet/dn_rules.c +7 -0
Index: net-2.6.22/net/decnet/dn_rules.c
===================================================================
@@ -31,6 +31,7 @@
 #include <net/dn_fib.h>
 #include <net/dn_neigh.h>
 #include <net/dn_dev.h>
+#include <net/dn_route.h>
 
 static struct fib_rules_ops dn_fib_rules_ops;
 
@@ -239,6 +240,11 @@ static u32 dn_fib_rule_default_pref(void
 	return 0;
 }
 
+static void dn_fib_rule_flush_cache(void)
+{
+	dn_rt_cache_flush(0);
+}
+
 static struct fib_rules_ops dn_fib_rules_ops = {
 	.family		= AF_DECnet,
 	.rule_size	= sizeof(struct dn_fib_rule),
@@ -249,6 +255,7 @@ static struct fib_rules_ops dn_fib_rules
 	.compare	= dn_fib_rule_compare,
 	.fill		= dn_fib_rule_fill,
 	.default_pref	= dn_fib_rule_default_pref,
+	.flush_cache	= dn_fib_rule_flush_cache,
 	.nlgroup	= RTNLGRP_DECnet_RULE,
 	.policy		= dn_fib_rule_policy,
 	.rules_list	= &dn_fib_rules,
#1 lore raw
Quoted 52 lines · Thomas Graf

The results of FIB rules lookups are cached in the routing cache except for IPv6 as no such cache exists. So far, it was the responsibility of the user to flush the cache after modifying any rules. This lead to many false bug reports due to misunderstanding of this concept.

This patch automatically flushes the route cache after inserting or deleting a rule.

Index: net-2.6.22/include/net/fib_rules.h
===================================================================

include/net/fib_rules.h +4 -0
@@ -59,6 +59,10 @@ struct fib_rules_ops
 	u32			(*default_pref)(void);
 	size_t			(*nlmsg_payload)(struct fib_rule *);
 
+	/* Called after modifications to the rules set, must flush
+	 * the route cache if one exists. */
+	void			(*flush_cache)(void);
+
 	int			nlgroup;
 	struct nla_policy	*policy;
 	struct list_head	*rules_list;

Index: net-2.6.22/net/core/fib_rules.c
===================================================================

net/core/fib_rules.c +7 -0
@@ -44,6 +44,12 @@ static void rules_ops_put(struct fib_rul
 		module_put(ops->owner);
 }
 
+static void flush_route_cache(struct fib_rules_ops *ops)
+{
+	if (ops->flush_cache)
+		ops->flush_cache();
+}
+
 int fib_rules_register(struct fib_rules_ops *ops)
 {
 	int err = -EEXIST;
@@ -315,6 +321,7 @@ static int fib_nl_newrule(struct sk_buff
 
 	notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
 	rules_ops_put(ops);
+	flush_route_cache(ops);
 	return 0;
 
 errout_free:

net/core/fib_rules.c static int fib_nl_delrule(struct sk_buff · line 417

Quoted 6 lines
net/core/fib_rules.c +1 -0
@@ -405,6 +412,7 @@ static int fib_nl_delrule(struct sk_buff
 				   NETLINK_CB(skb).pid);
 		fib_rule_put(rule);
 		rules_ops_put(ops);
+		flush_route_cache(ops);
 		return 0;
 	}

That looks like a bug - shouldn't we flush the cache first, then do the rules_ops_put()?

Cheers,
Muli

#2 lore raw

* Muli Ben-Yehuda <[email protected]> 2007-03-27 15:30

Quoted 2 lines

That looks like a bug - shouldn't we flush the cache first, then do the rules_ops_put()?

Good catch, it's unlikely to happen but it is a bug.