two more uses of siphash. better hash for ipv4. maybe not needed for rbtree
authortedu <tedu@openbsd.org>
Thu, 18 Dec 2014 19:28:44 +0000 (19:28 +0000)
committertedu <tedu@openbsd.org>
Thu, 18 Dec 2014 19:28:44 +0000 (19:28 +0000)
hint, but still pretty. ok deraadt

usr.sbin/bgpd/rde_rib.c
usr.sbin/bgpd/rde_update.c

index 8844802..45e1ce4 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde_rib.c,v 1.140 2014/12/12 18:15:51 tedu Exp $ */
+/*     $OpenBSD: rde_rib.c,v 1.141 2014/12/18 19:28:44 tedu Exp $ */
 
 /*
  * Copyright (c) 2003, 2004 Claudio Jeker <claudio@openbsd.org>
@@ -18,7 +18,6 @@
 
 #include <sys/types.h>
 #include <sys/queue.h>
-#include <sys/hash.h>
 
 #include <stdlib.h>
 #include <string.h>
@@ -1065,6 +1064,8 @@ struct nexthop_table {
        u_int32_t                                nexthop_hashmask;
 } nexthoptable;
 
+SIPHASH_KEY nexthoptablekey;
+
 void
 nexthop_init(u_int32_t hashsize)
 {
@@ -1078,6 +1079,7 @@ nexthop_init(u_int32_t hashsize)
 
        for (i = 0; i < hs; i++)
                LIST_INIT(&nexthoptable.nexthop_hashtbl[i]);
+       arc4random_buf(&nexthoptablekey, sizeof(nexthoptablekey));
 
        nexthoptable.nexthop_hashmask = hs - 1;
 }
@@ -1308,17 +1310,16 @@ nexthop_hash(struct bgpd_addr *nexthop)
 
        switch (nexthop->aid) {
        case AID_INET:
-               h = (AF_INET ^ ntohl(nexthop->v4.s_addr) ^
-                   ntohl(nexthop->v4.s_addr) >> 13) &
-                   nexthoptable.nexthop_hashmask;
+               h = SipHash24(&nexthoptablekey, &nexthop->v4.s_addr,
+                   sizeof(nexthop->v4.s_addr));
                break;
        case AID_INET6:
-               h = hash32_buf(&nexthop->v6, sizeof(struct in6_addr),
-                   HASHINIT) & nexthoptable.nexthop_hashmask;
+               h = SipHash24(&nexthoptablekey, &nexthop->v6,
+                   sizeof(struct in6_addr));
                break;
        default:
                fatalx("nexthop_hash: unsupported AF");
        }
-       return (&nexthoptable.nexthop_hashtbl[h]);
+       return (&nexthoptable.nexthop_hashtbl[h & nexthoptable.nexthop_hashmask]);
 }
 
index 911d081..b215f98 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: rde_update.c,v 1.81 2013/08/14 20:34:27 claudio Exp $ */
+/*     $OpenBSD: rde_update.c,v 1.82 2014/12/18 19:28:44 tedu Exp $ */
 
 /*
  * Copyright (c) 2004 Claudio Jeker <claudio@openbsd.org>
  */
 #include <sys/types.h>
 #include <sys/queue.h>
-#include <sys/hash.h>
 
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
+#include <siphash.h>
 
 #include "bgpd.h"
 #include "rde.h"
@@ -63,6 +63,8 @@ RB_GENERATE(uptree_prefix, update_prefix, entry, up_prefix_cmp)
 RB_PROTOTYPE(uptree_attr, update_attr, entry, up_attr_cmp)
 RB_GENERATE(uptree_attr, update_attr, entry, up_attr_cmp)
 
+SIPHASH_KEY uptree_key;
+
 void
 up_init(struct rde_peer *peer)
 {
@@ -78,6 +80,7 @@ up_init(struct rde_peer *peer)
        peer->up_acnt = 0;
        peer->up_nlricnt = 0;
        peer->up_wcnt = 0;
+       arc4random_buf(&uptree_key, sizeof(uptree_key));
 }
 
 void
@@ -363,6 +366,7 @@ up_generate(struct rde_peer *peer, struct rde_aspath *asp,
 {
        struct update_attr              *ua = NULL;
        struct update_prefix            *up;
+       SIPHASH_CTX                     ctx;
 
        if (asp) {
                ua = calloc(1, sizeof(struct update_attr));
@@ -378,10 +382,11 @@ up_generate(struct rde_peer *peer, struct rde_aspath *asp,
                 * use aspath_hash as attr_hash, this may be unoptimal
                 * but currently I don't care.
                 */
-               ua->attr_hash = hash32_buf(ua->attr, ua->attr_len, HASHINIT);
+               SipHash24_Init(&ctx, &uptree_key);
+               SipHash24_Update(&ctx, ua->attr, ua->attr_len);
                if (ua->mpattr)
-                       ua->attr_hash = hash32_buf(ua->mpattr, ua->mpattr_len,
-                           ua->attr_hash);
+                       SipHash24_Update(&ctx, ua->mpattr, ua->mpattr_len);
+               ua->attr_hash = SipHash24_End(&ctx);
        }
 
        up = calloc(1, sizeof(struct update_prefix));