Skip to content

Instantly share code, notes, and snippets.

@T-X
Last active July 22, 2024 18:22
Show Gist options
  • Select an option

  • Save T-X/e73a77009d4bfb9d13d3855348c96ce2 to your computer and use it in GitHub Desktop.

Select an option

Save T-X/e73a77009d4bfb9d13d3855348c96ce2 to your computer and use it in GitHub Desktop.
batman-adv DAT DHT jenkins test for 10.130.0.0/16
#include <stdio.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
//typedef u16 batadv_dat_addr_t;
typedef uint16_t batadv_dat_addr_t;
#define BATADV_DAT_ADDR_MAX ((batadv_dat_addr_t)~(batadv_dat_addr_t)0)
struct batadv_dat_entry {
/** @ip: the IPv4 corresponding to this DAT/ARP entry */
//__be32 ip;
uint32_t ip;
/** @mac_addr: the MAC address associated to the stored IPv4 */
//u8 mac_addr[ETH_ALEN];
uint8_t mac_addr[ETH_ALEN];
/** @vid: the vlan ID associated to this entry */
unsigned short vid;
/**
* @last_update: time in jiffies when this entry was refreshed last time
*/
unsigned long last_update;
/** @hash_entry: hlist node for &batadv_priv_dat.hash */
// struct hlist_node hash_entry;
/** @refcount: number of contexts the object is used */
// struct kref refcount;
/** @rcu: struct used for freeing in an RCU-safe manner */
// struct rcu_head rcu;
};
static uint32_t batadv_hash_dat(const void *data, uint32_t size)
{
uint32_t hash = 0;
const struct batadv_dat_entry *dat = data;
const unsigned char *key;
//__be16 vid;
uint16_t vid;
uint32_t i;
//key = (__force const unsigned char *)&dat->ip;
key = (const unsigned char *)&dat->ip;
for (i = 0; i < sizeof(dat->ip); i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
vid = htons(dat->vid);
key = (const unsigned char *)&vid;
for (i = 0; i < sizeof(dat->vid); i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash % size;
}
int main(int argc, char *argv[])
{
struct batadv_dat_entry dat;
batadv_dat_addr_t last_max = BATADV_DAT_ADDR_MAX, ip_key;
struct in_addr ip;
int ret;
if (argc < 2)
return 1;
ret = inet_aton(argv[1], &ip);
memset(&dat, 0, sizeof(dat));
dat.ip = htonl((uint32_t)ip.s_addr);
dat.vid = 0;
ip_key = batadv_hash_dat(&dat, BATADV_DAT_ADDR_MAX);
printf("0x%04x\n", ip_key);
return 0;
}
#!/bin/bash
for i in `seq 0 255`; do for j in `seq 0 255`; do ./test-jhash 10.130.$i.$j; done; done | sort | uniq -c | sort -n | awk '{ print $1 }' | uniq -c
$ ./test-jhash.sh
24164 1
12070 2
3977 3
1018 4
208 5
29 6
1 7
1 8
$ echo "Used hashes: $((24164+12070+3977+1018+208+29+1+1))"
41468
$ echo "Unused hashes: $((2**16-(24164+12070+3977+1018+208+29+1+1)))"
Unused hashes: 24068
$ echo "Hashed IP addresses total: $((24164+12070*2+3977*3+1018*4+208*5+29*6+1*7+1*8))"
Hashed IP addresses total: 65536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment