diff mbox series

[RFC,iproute2-next,07/11] ip: nexthop: add cache helpers

Message ID 20210929152848.1710552-8-razor@blackwall.org (mailing list archive)
State Superseded
Delegated to: David Ahern
Headers show
Series ip: nexthop: cache nexthops and print routes' nh info | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Nikolay Aleksandrov Sept. 29, 2021, 3:28 p.m. UTC
From: Nikolay Aleksandrov <nikolay@nvidia.com>

Add a static nexthop cache in a hash with 1024 buckets and helpers to
manage it (link, unlink, find, add nexthop, del nexthop). Adding new
nexthops is done by creating a new rtnl handle and using it to retrieve
the nexthop so the helper is safe to use while already reading a
response (i.e. using the global rth).

Signed-off-by: Nikolay Aleksandrov <nikolay@nvidia.com>
---
 ip/ipnexthop.c | 112 +++++++++++++++++++++++++++++++++++++++++++++----
 ip/nh_common.h |   6 +++
 2 files changed, 111 insertions(+), 7 deletions(-)

Comments

David Ahern Sept. 30, 2021, 3:39 a.m. UTC | #1
On 9/29/21 9:28 AM, Nikolay Aleksandrov wrote:
> @@ -372,7 +409,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>  		if (RTA_PAYLOAD(tb[NHA_GATEWAY]) > sizeof(nhe->nh_gateway)) {
>  			fprintf(fp, "<nexthop id %u invalid gateway length %lu>\n",
>  				nhe->nh_id, RTA_PAYLOAD(tb[NHA_GATEWAY]));
> -			err = EINVAL;
> +			err = -EINVAL;
>  			goto out_err;
>  		}
>  		nhe->nh_gateway_len = RTA_PAYLOAD(tb[NHA_GATEWAY]);
> @@ -383,7 +420,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>  	if (tb[NHA_ENCAP]) {
>  		nhe->nh_encap = malloc(RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP])));
>  		if (!nhe->nh_encap) {
> -			err = ENOMEM;
> +			err = -ENOMEM;
>  			goto out_err;
>  		}
>  		memcpy(nhe->nh_encap, tb[NHA_ENCAP],
> @@ -396,13 +433,13 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>  		if (!__valid_nh_group_attr(tb[NHA_GROUP])) {
>  			fprintf(fp, "<nexthop id %u invalid nexthop group>",
>  				nhe->nh_id);
> -			err = EINVAL;
> +			err = -EINVAL;
>  			goto out_err;
>  		}
>  
>  		nhe->nh_groups = malloc(RTA_PAYLOAD(tb[NHA_GROUP]));
>  		if (!nhe->nh_groups) {
> -			err = ENOMEM;
> +			err = -ENOMEM;
>  			goto out_err;
>  		}
>  		nhe->nh_groups_cnt = RTA_PAYLOAD(tb[NHA_GROUP]) /

those should go with previous patches.
Nikolay Aleksandrov Sept. 30, 2021, 6:53 a.m. UTC | #2
On 30/09/2021 06:39, David Ahern wrote:
> On 9/29/21 9:28 AM, Nikolay Aleksandrov wrote:
>> @@ -372,7 +409,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>>  		if (RTA_PAYLOAD(tb[NHA_GATEWAY]) > sizeof(nhe->nh_gateway)) {
>>  			fprintf(fp, "<nexthop id %u invalid gateway length %lu>\n",
>>  				nhe->nh_id, RTA_PAYLOAD(tb[NHA_GATEWAY]));
>> -			err = EINVAL;
>> +			err = -EINVAL;
>>  			goto out_err;
>>  		}
>>  		nhe->nh_gateway_len = RTA_PAYLOAD(tb[NHA_GATEWAY]);
>> @@ -383,7 +420,7 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>>  	if (tb[NHA_ENCAP]) {
>>  		nhe->nh_encap = malloc(RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP])));
>>  		if (!nhe->nh_encap) {
>> -			err = ENOMEM;
>> +			err = -ENOMEM;
>>  			goto out_err;
>>  		}
>>  		memcpy(nhe->nh_encap, tb[NHA_ENCAP],
>> @@ -396,13 +433,13 @@ static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
>>  		if (!__valid_nh_group_attr(tb[NHA_GROUP])) {
>>  			fprintf(fp, "<nexthop id %u invalid nexthop group>",
>>  				nhe->nh_id);
>> -			err = EINVAL;
>> +			err = -EINVAL;
>>  			goto out_err;
>>  		}
>>  
>>  		nhe->nh_groups = malloc(RTA_PAYLOAD(tb[NHA_GROUP]));
>>  		if (!nhe->nh_groups) {
>> -			err = ENOMEM;
>> +			err = -ENOMEM;
>>  			goto out_err;
>>  		}
>>  		nhe->nh_groups_cnt = RTA_PAYLOAD(tb[NHA_GROUP]) /
> 
> those should go with previous patches.
> 

Oops, this one was a last minute change, I didn't notice I've done it in the wrong patch.
Of course I'll fix it for the non-rfc submission.

Thanks
diff mbox series

Patch

diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c
index 0a08230fc278..6e5ea47ac927 100644
--- a/ip/ipnexthop.c
+++ b/ip/ipnexthop.c
@@ -34,6 +34,8 @@  enum {
 #define RTM_NHA(h)  ((struct rtattr *)(((char *)(h)) + \
 			NLMSG_ALIGN(sizeof(struct nhmsg))))
 
+static struct hlist_head nh_cache[NH_CACHE_SIZE];
+
 static void usage(void) __attribute__((noreturn));
 
 static void usage(void)
@@ -347,6 +349,41 @@  static void ipnh_destroy_entry(struct nh_entry *nhe)
 		free(nhe->nh_groups);
 }
 
+static struct hlist_head *ipnh_cache_head(__u32 nh_id)
+{
+	nh_id ^= nh_id >> 20;
+	nh_id ^= nh_id >> 10;
+
+	return &nh_cache[nh_id % NH_CACHE_SIZE];
+}
+
+static void ipnh_cache_link_entry(struct nh_entry *nhe)
+{
+	struct hlist_head *head = ipnh_cache_head(nhe->nh_id);
+
+	hlist_add_head(&nhe->nh_hash, head);
+}
+
+static void ipnh_cache_unlink_entry(struct nh_entry *nhe)
+{
+	hlist_del(&nhe->nh_hash);
+}
+
+static struct nh_entry *ipnh_cache_get(__u32 nh_id)
+{
+	struct hlist_head *head = ipnh_cache_head(nh_id);
+	struct nh_entry *nhe;
+	struct hlist_node *n;
+
+	hlist_for_each(n, head) {
+		nhe = container_of(n, struct nh_entry, nh_hash);
+		if (nhe->nh_id == nh_id)
+			return nhe;
+	}
+
+	return NULL;
+}
+
 /* parse nhmsg into nexthop entry struct which must be destroyed by
  * ipnh_destroy_enty when it's not needed anymore
  */
@@ -372,7 +409,7 @@  static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
 		if (RTA_PAYLOAD(tb[NHA_GATEWAY]) > sizeof(nhe->nh_gateway)) {
 			fprintf(fp, "<nexthop id %u invalid gateway length %lu>\n",
 				nhe->nh_id, RTA_PAYLOAD(tb[NHA_GATEWAY]));
-			err = EINVAL;
+			err = -EINVAL;
 			goto out_err;
 		}
 		nhe->nh_gateway_len = RTA_PAYLOAD(tb[NHA_GATEWAY]);
@@ -383,7 +420,7 @@  static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
 	if (tb[NHA_ENCAP]) {
 		nhe->nh_encap = malloc(RTA_LENGTH(RTA_PAYLOAD(tb[NHA_ENCAP])));
 		if (!nhe->nh_encap) {
-			err = ENOMEM;
+			err = -ENOMEM;
 			goto out_err;
 		}
 		memcpy(nhe->nh_encap, tb[NHA_ENCAP],
@@ -396,13 +433,13 @@  static int ipnh_parse_nhmsg(FILE *fp, const struct nhmsg *nhm, int len,
 		if (!__valid_nh_group_attr(tb[NHA_GROUP])) {
 			fprintf(fp, "<nexthop id %u invalid nexthop group>",
 				nhe->nh_id);
-			err = EINVAL;
+			err = -EINVAL;
 			goto out_err;
 		}
 
 		nhe->nh_groups = malloc(RTA_PAYLOAD(tb[NHA_GROUP]));
 		if (!nhe->nh_groups) {
-			err = ENOMEM;
+			err = -ENOMEM;
 			goto out_err;
 		}
 		nhe->nh_groups_cnt = RTA_PAYLOAD(tb[NHA_GROUP]) /
@@ -450,6 +487,67 @@  static int  __ipnh_get_id(struct rtnl_handle *rthp, __u32 nh_id,
 	return rtnl_talk(rthp, &req.n, answer);
 }
 
+static int __ipnh_cache_parse_nlmsg(const struct nlmsghdr *n,
+				    struct nh_entry *nhe)
+{
+	int err, len;
+
+	len = n->nlmsg_len - NLMSG_SPACE(sizeof(struct nhmsg));
+	if (len < 0) {
+		fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
+		return -EINVAL;
+	}
+
+	err = ipnh_parse_nhmsg(stderr, NLMSG_DATA(n), len, nhe);
+	if (err) {
+		fprintf(stderr, "Error parsing nexthop: %s\n", strerror(-err));
+		return err;
+	}
+
+	return 0;
+}
+
+static struct nh_entry *ipnh_cache_add(__u32 nh_id)
+{
+	struct rtnl_handle cache_rth = { .fd = -1 };
+	struct nlmsghdr *answer = NULL;
+	struct nh_entry *nhe = NULL;
+
+	if (rtnl_open(&cache_rth, 0) < 0)
+		goto out;
+
+	if (__ipnh_get_id(&cache_rth, nh_id, &answer) < 0)
+		goto out;
+
+	nhe = malloc(sizeof(*nhe));
+	if (!nhe)
+		goto out;
+
+	if (__ipnh_cache_parse_nlmsg(answer, nhe))
+		goto out_free_nhe;
+
+	ipnh_cache_link_entry(nhe);
+
+out:
+	if (answer)
+		free(answer);
+	rtnl_close(&cache_rth);
+
+	return nhe;
+
+out_free_nhe:
+	free(nhe);
+	nhe = NULL;
+	goto out;
+}
+
+static void ipnh_cache_del(struct nh_entry *nhe)
+{
+	ipnh_cache_unlink_entry(nhe);
+	ipnh_destroy_entry(nhe);
+	free(nhe);
+}
+
 int print_nexthop(struct nlmsghdr *n, void *arg)
 {
 	struct nhmsg *nhm = NLMSG_DATA(n);
@@ -476,10 +574,10 @@  int print_nexthop(struct nlmsghdr *n, void *arg)
 	if (filter.proto && filter.proto != nhm->nh_protocol)
 		return 0;
 
-	err = parse_nexthop_rta(fp, nhm, len, &nhe);
+	err = ipnh_parse_nhmsg(fp, nhm, len, &nhe);
 	if (err) {
 		close_json_object();
-		fprintf(stderr, "Error parsing nexthop: %s\n", strerror(err));
+		fprintf(stderr, "Error parsing nexthop: %s\n", strerror(-err));
 		return -1;
 	}
 	open_json_object(NULL);
@@ -530,7 +628,7 @@  int print_nexthop(struct nlmsghdr *n, void *arg)
 	print_string(PRINT_FP, NULL, "%s", "\n");
 	close_json_object();
 	fflush(fp);
-	destroy_nexthop_entry(&nhe);
+	ipnh_destroy_entry(&nhe);
 
 	return 0;
 }
diff --git a/ip/nh_common.h b/ip/nh_common.h
index 8c96f9993562..a34b0d20916e 100644
--- a/ip/nh_common.h
+++ b/ip/nh_common.h
@@ -2,6 +2,10 @@ 
 #ifndef __NH_COMMON_H__
 #define __NH_COMMON_H__ 1
 
+#include <list.h>
+
+#define NH_CACHE_SIZE		1024
+
 struct nha_res_grp {
 	__u16			buckets;
 	__u32			idle_timer;
@@ -10,6 +14,8 @@  struct nha_res_grp {
 };
 
 struct nh_entry {
+	struct hlist_node	nh_hash;
+
 	__u32			nh_id;
 	__u32			nh_oif;
 	__u32			nh_flags;