Skip to contentSkip to conversation

ipv6 route: Aggregate table getting code

PATCH In review Masashi Honma 2 participants 5 messages Oct 18, 2015 – Oct 27, 2015

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

PATCH 1/1 ipv6 route: Aggregate table getting code

These lines could be aggregated to one line because fib6_new_table() calls fib6_get_table() inside on both cases CONFIG_IPV6_MULTIPLE_TABLES is enabled or not.

---
 net/ipv6/route.c | 11 +----------
 1 file changed, 1 insertion(+), 10 deletions(-)
net/ipv6/route.c +1 -10
index cb32ce2..1ff4130 100644
@@ -1778,16 +1778,7 @@ int ip6_route_info_create(struct fib6_config *cfg, struct rt6_info **rt_ret)
 		cfg->fc_metric = IP6_RT_PRIO_USER;
 
 	err = -ENOBUFS;
-	if (cfg->fc_nlinfo.nlh &&
-	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
-		table = fib6_get_table(net, cfg->fc_table);
-		if (!table) {
-			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
-			table = fib6_new_table(net, cfg->fc_table);
-		}
-	} else {
-		table = fib6_new_table(net, cfg->fc_table);
-	}
+	table = fib6_new_table(net, cfg->fc_table);
 
 	if (!table)
 		goto out;
Signature · 1 line
1.9.1
maintainer
#1 lore raw

From: Masashi Honma <[email protected]>
Date: Sun, 18 Oct 2015 09:51:39 +0900

Quoted 5 lines

These lines could be aggregated to one line because fib6_new_table() calls fib6_get_table() inside on both cases CONFIG_IPV6_MULTIPLE_TABLES is enabled or not.

This is not correct.

The whole point of the test is so that the kernel log message warning for failing to provide NLM_F_CREATE can be printed in precisely the correct conditions.

#2 lore raw
Quoted 5 lines · David Miller

This is not correct.

The whole point of the test is so that the kernel log message warning for failing to provide NLM_F_CREATE can be printed in precisely the correct conditions.

Thanks. Now I understand importance of the warning.

Though fib6_get_table() is called twice to show the warning. So I made another patch to reduce calling the function.

#3 lore raw

PATCH 1/1 ipv6 route: Use flag instead of calling fib6_get_table() twice

The fib6_get_table() is called twice to show the warning. This patch reduces calling the function.

---
 include/net/ip6_fib.h |  2 +-
 net/ipv6/fib6_rules.c |  3 ++-
 net/ipv6/ip6_fib.c    | 10 +++++++---
 net/ipv6/route.c      | 13 ++++---------
 4 files changed, 14 insertions(+), 14 deletions(-)
include/net/ip6_fib.h +1 -1
index aaf9700..d6c5dff 100644
@@ -256,7 +256,7 @@ typedef struct rt6_info *(*pol_lookup_t)(struct net *,
  */
 
 struct fib6_table *fib6_get_table(struct net *net, u32 id);
-struct fib6_table *fib6_new_table(struct net *net, u32 id);
+struct fib6_table *fib6_new_table(struct net *net, u32 id, int *exist);
 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
 				   int flags, pol_lookup_t lookup);
 
net/ipv6/fib6_rules.c +2 -1
index 9f777ec..7512941 100644
@@ -187,12 +187,13 @@ static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
 	int err = -EINVAL;
 	struct net *net = sock_net(skb->sk);
 	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+	int exist;
 
 	if (rule->action == FR_ACT_TO_TBL) {
 		if (rule->table == RT6_TABLE_UNSPEC)
 			goto errout;
 
-		if (fib6_new_table(net, rule->table) == NULL) {
+		if (fib6_new_table(net, rule->table, &exist) == NULL) {
 			err = -ENOBUFS;
 			goto errout;
 		}
net/ipv6/ip6_fib.c +7 -3
index 7d2e002..abf65ef 100644
@@ -226,16 +226,19 @@ static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
 	return table;
 }
 
-struct fib6_table *fib6_new_table(struct net *net, u32 id)
+struct fib6_table *fib6_new_table(struct net *net, u32 id, int *exist)
 {
 	struct fib6_table *tb;
 
 	if (id == 0)
 		id = RT6_TABLE_MAIN;
 	tb = fib6_get_table(net, id);
-	if (tb)
+	if (tb) {
+		*exist = 1;
 		return tb;
+	}
 
+	*exist = 0;
 	tb = fib6_alloc_table(net, id);
 	if (tb)
 		fib6_link_table(net, tb);
@@ -272,8 +275,9 @@ static void __net_init fib6_tables_init(struct net *net)
 }
 #else
 
-struct fib6_table *fib6_new_table(struct net *net, u32 id)
+struct fib6_table *fib6_new_table(struct net *net, u32 id, int *exist)
 {
+	*exist = 1;
 	return fib6_get_table(net, id);
 }
 
net/ipv6/route.c +4 -9
index cb32ce2..7c4e0c2 100644
@@ -1757,6 +1757,7 @@ int ip6_route_info_create(struct fib6_config *cfg, struct rt6_info **rt_ret)
 	struct inet6_dev *idev = NULL;
 	struct fib6_table *table;
 	int addr_type;
+	int exist;
 
 	if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
 		return -EINVAL;
@@ -1778,16 +1779,10 @@ int ip6_route_info_create(struct fib6_config *cfg, struct rt6_info **rt_ret)
 		cfg->fc_metric = IP6_RT_PRIO_USER;
 
 	err = -ENOBUFS;
+	table = fib6_new_table(net, cfg->fc_table, &exist);
 	if (cfg->fc_nlinfo.nlh &&
-	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) {
-		table = fib6_get_table(net, cfg->fc_table);
-		if (!table) {
-			pr_warn("NLM_F_CREATE should be specified when creating new route\n");
-			table = fib6_new_table(net, cfg->fc_table);
-		}
-	} else {
-		table = fib6_new_table(net, cfg->fc_table);
-	}
+	    !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE) && !exist)
+		pr_warn("NLM_F_CREATE should be specified when creating new route\n");
 
 	if (!table)
 		goto out;
Signature · 1 line
1.9.1
maintainer
#4 lore raw

Re: [PATCH] ipv6 route: Use flag instead of calling fib6_get_table() twice

From: Masashi Honma <[email protected]>
Date: Sun, 25 Oct 2015 11:44:27 +0900

Quoted 4 lines

The fib6_get_table() is called twice to show the warning. This patch reduces calling the function.

I think the added cost of passing a reference to an on-stack variable exceeds the value of whatever you think you are improving here.

The code as-is is fine as far as I'm concerned.