Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 36fd61cb authored by Sven Eckelmann's avatar Sven Eckelmann Committed by Antonio Quartulli
Browse files

batman-adv: Use common Jenkins Hash implementation



An unoptimized version of the Jenkins one-at-a-time hash function is used
and partially copied all over the code wherever an hashtable is used.
Instead the optimized version shared between the whole kernel should be
used to reduce code duplication and use better optimized code.

Only the DAT code must use the old implementation because it is used as
distributed hash function which has to be common for all nodes.

Signed-off-by: default avatarSven Eckelmann <sven@narfation.org>
Signed-off-by: default avatarMarek Lindner <mareklindner@neomailbox.ch>
parent d691f9e8
Loading
Loading
Loading
Loading
+4 −12
Original line number Diff line number Diff line
@@ -42,12 +42,8 @@ static inline uint32_t batadv_choose_claim(const void *data, uint32_t size)
	struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
	uint32_t hash = 0;

	hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
	hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));

	hash += (hash << 3);
	hash ^= (hash >> 11);
	hash += (hash << 15);
	hash = jhash(&claim->addr, sizeof(claim->addr), hash);
	hash = jhash(&claim->vid, sizeof(claim->vid), hash);

	return hash % size;
}
@@ -59,12 +55,8 @@ static inline uint32_t batadv_choose_backbone_gw(const void *data,
	const struct batadv_bla_claim *claim = (struct batadv_bla_claim *)data;
	uint32_t hash = 0;

	hash = batadv_hash_bytes(hash, &claim->addr, sizeof(claim->addr));
	hash = batadv_hash_bytes(hash, &claim->vid, sizeof(claim->vid));

	hash += (hash << 3);
	hash ^= (hash >> 11);
	hash += (hash << 15);
	hash = jhash(&claim->addr, sizeof(claim->addr), hash);
	hash = jhash(&claim->vid, sizeof(claim->vid), hash);

	return hash % size;
}
+15 −2
Original line number Diff line number Diff line
@@ -206,9 +206,22 @@ 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;
	uint32_t i;

	key = (const unsigned char *)&dat->ip;
	for (i = 0; i < sizeof(dat->ip); i++) {
		hash += key[i];
		hash += (hash << 10);
		hash ^= (hash >> 6);
	}

	hash = batadv_hash_bytes(hash, &dat->ip, sizeof(dat->ip));
	hash = batadv_hash_bytes(hash, &dat->vid, sizeof(dat->vid));
	key = (const unsigned char *)&dat->vid;
	for (i = 0; i < sizeof(dat->vid); i++) {
		hash += key[i];
		hash += (hash << 10);
		hash ^= (hash >> 6);
	}

	hash += (hash << 3);
	hash ^= (hash >> 11);
+0 −22
Original line number Diff line number Diff line
@@ -79,28 +79,6 @@ static inline void batadv_hash_delete(struct batadv_hashtable *hash,
	batadv_hash_destroy(hash);
}

/**
 *	batadv_hash_bytes - hash some bytes and add them to the previous hash
 *	@hash: previous hash value
 *	@data: data to be hashed
 *	@size: number of bytes to be hashed
 *
 *	Returns the new hash value.
 */
static inline uint32_t batadv_hash_bytes(uint32_t hash, const void *data,
					 uint32_t size)
{
	const unsigned char *key = data;
	int i;

	for (i = 0; i < size; i++) {
		hash += key[i];
		hash += (hash << 10);
		hash ^= (hash >> 6);
	}
	return hash;
}

/**
 *	batadv_hash_add - adds data to the hashtable
 *	@hash: storage hash table
+1 −0
Original line number Diff line number Diff line
@@ -174,6 +174,7 @@ enum batadv_uev_type {
#include <linux/workqueue.h>	/* workqueue */
#include <linux/percpu.h>
#include <linux/slab.h>
#include <linux/jhash.h>
#include <net/sock.h>		/* struct sock */
#include <net/addrconf.h>	/* ipv6 address stuff */
#include <linux/ip.h>
+2 −8
Original line number Diff line number Diff line
@@ -453,14 +453,8 @@ static uint32_t batadv_nc_hash_choose(const void *data, uint32_t size)
	const struct batadv_nc_path *nc_path = data;
	uint32_t hash = 0;

	hash = batadv_hash_bytes(hash, &nc_path->prev_hop,
				 sizeof(nc_path->prev_hop));
	hash = batadv_hash_bytes(hash, &nc_path->next_hop,
				 sizeof(nc_path->next_hop));

	hash += (hash << 3);
	hash ^= (hash >> 11);
	hash += (hash << 15);
	hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
	hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);

	return hash % size;
}
Loading