diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-03-19 09:01:52 -0400 |
|---|---|---|
| committer | Donald Sharp <sharpd@cumulusnetworks.com> | 2018-04-06 13:22:43 -0400 |
| commit | d36d0d57711ec79bb7ff4360b62d80b1a9809f6f (patch) | |
| tree | 7e9f9c3fc7865e2bb9129a546389d857fff6d641 /lib | |
| parent | 6568993b1b624637db7999b7f00cc62b7501bd4f (diff) | |
lib: Add hash and use const a bit more intelligently
This commit adds code to notify the compiler that we
will not be changing the arguments to nexthop2str
and we expect thre return to be treated the same.
Additionally we add some code to allow nexthops to
be hashed to be used in a hash.
Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/nexthop.c | 15 | ||||
| -rw-r--r-- | lib/nexthop.h | 19 |
2 files changed, 32 insertions, 2 deletions
diff --git a/lib/nexthop.c b/lib/nexthop.c index b1e9582b20..3603050f06 100644 --- a/lib/nexthop.c +++ b/lib/nexthop.c @@ -31,6 +31,7 @@ #include "prefix.h" #include "nexthop.h" #include "mpls.h" +#include "jhash.h" DEFINE_MTYPE_STATIC(LIB, NEXTHOP, "Nexthop") DEFINE_MTYPE_STATIC(LIB, NH_LABEL, "Nexthop label") @@ -240,7 +241,7 @@ void nexthop_del_labels(struct nexthop *nexthop) } } -const char *nexthop2str(struct nexthop *nexthop, char *str, int size) +const char *nexthop2str(const struct nexthop *nexthop, char *str, int size) { switch (nexthop->type) { case NEXTHOP_TYPE_IFINDEX: @@ -310,3 +311,15 @@ unsigned int nexthop_level(struct nexthop *nexthop) return rv; } + +uint32_t nexthop_hash(struct nexthop *nexthop) +{ + uint32_t key; + + key = jhash_1word(nexthop->vrf_id, 0x45afe398); + key = jhash_1word(nexthop->ifindex, key); + key = jhash_1word(nexthop->type, key); + key = jhash(&nexthop->gate, sizeof(union g_addr), key); + + return key; +} diff --git a/lib/nexthop.h b/lib/nexthop.h index 036fc5b888..e4af405d5f 100644 --- a/lib/nexthop.h +++ b/lib/nexthop.h @@ -118,6 +118,23 @@ void nexthop_add_labels(struct nexthop *, enum lsp_types_t, uint8_t, mpls_label_t *); void nexthop_del_labels(struct nexthop *); +/* + * Hash a nexthop. Suitable for use with hash tables. + * + * This function uses the following values when computing the hash: + * - vrf_id + * - ifindex + * - type + * - gate + * + * nexthop + * The nexthop to hash + * + * Returns: + * 32-bit hash of nexthop + */ +uint32_t nexthop_hash(struct nexthop *nexthop); + extern bool nexthop_same(const struct nexthop *nh1, const struct nexthop *nh2); extern const char *nexthop_type_to_str(enum nexthop_types_t nh_type); @@ -126,7 +143,7 @@ extern int nexthop_same_no_recurse(const struct nexthop *next1, extern int nexthop_labels_match(struct nexthop *nh1, struct nexthop *nh2); extern int nexthop_same_firsthop(struct nexthop *next1, struct nexthop *next2); -extern const char *nexthop2str(struct nexthop *nexthop, char *str, int size); +extern const char *nexthop2str(const struct nexthop *nexthop, char *str, int size); extern struct nexthop *nexthop_next(struct nexthop *nexthop); extern unsigned int nexthop_level(struct nexthop *nexthop); #endif /*_LIB_NEXTHOP_H */ |
