batman-adv: make aggregated hash keys easier to identify
Sven Eckelmann
maintainerPATCH v2 1/6 batman-adv: bla: use dedicated key type for claim hash
In reply to Sven EckelmannThe claim hash lookup functions are given a struct batadv_bla_claim. For a lookup, a dummy claim is created on the stack and only its addr and vid members are filled. All other members stay uninitialized, and only by reading the hash and compare callbacks can one tell which members form the key.
Move addr and vid into a new struct batadv_bla_claim_key, embedded as batadv_bla_claim::key. The helpers are only using this key for hashing/comparison.
--- net/batman-adv/bridge_loop_avoidance.c | 98 +++++++++++++++++----------------- net/batman-adv/types.h | 12 ++++- 2 files changed, 60 insertions(+), 50 deletions(-)
net/batman-adv/bridge_loop_avoidance.c +50 -48
@@ -59,18 +59,18 @@ batadv_bla_send_announce(struct batadv_priv *bat_priv,/** * batadv_choose_claim() - choose the right bucket for a claim. -* @data: data to hash +* @data: &struct batadv_bla_claim_key to hash * @size: size of the hash table * * Return: the hash index of the claim */ static inline u32 batadv_choose_claim (const void *data,u32 size) { -const struct batadv_bla_claim *claim = data; +const struct batadv_bla_claim_key *key = data;u32 hash =0 ; - hash =jhash (&claim->addr,sizeof (claim->addr), hash); - hash =jhash (&claim->vid,sizeof (claim->vid), hash); + hash =jhash (&key->addr,sizeof (key->addr), hash); + hash =jhash (&key->vid,sizeof (key->vid), hash);return hash % size; }
@@ -119,24 +119,24 @@ static bool batadv_compare_backbone_gw(const struct hlist_node *node, }/** -* batadv_compare_claim() - compare address and vid of two claims -* @node: list node of the first entry to compare -* @data2: pointer to the second claims +* batadv_compare_claim() - compare key of a claim with a search key +* @node: list node of the claim to compare +* @data2: &struct batadv_bla_claim_key to compare with * -* Return: true if the claims have the same data, false otherwise +* Return: true if the claim has the given key, false otherwise */ static bool batadv_compare_claim (const struct hlist_node *node,const void *data2) { - const void *data1 = container_of(node, struct batadv_bla_claim, - hash_entry); -const struct batadv_bla_claim *cl1 = data1; -const struct batadv_bla_claim *cl2 = data2; +const struct batadv_bla_claim_key *key = data2; +const struct batadv_bla_claim *claim; -if (!batadv_compare_eth (cl1->addr, cl2->addr)) + claim = container_of(node, struct batadv_bla_claim, hash_entry); + +if (!batadv_compare_eth (claim->key.addr, key->addr))return false; -if (cl1->vid != cl2->vid) +if (claim->key.vid != key->vid)return false;return true;
@@ -188,7 +188,7 @@ static void batadv_claim_release(struct kref *ref)spin_unlock_bh (&claim->backbone_lock);spin_lock_bh (&old_backbone_gw->crc_lock); - old_backbone_gw->crc ^=crc16 (0 , claim->addr,ETH_ALEN ); + old_backbone_gw->crc ^=crc16 (0 , claim->key.addr,ETH_ALEN );spin_unlock_bh (&old_backbone_gw->crc_lock);batadv_backbone_gw_put (old_backbone_gw);
@@ -211,13 +211,13 @@ static void batadv_claim_put(struct batadv_bla_claim *claim)/** * batadv_claim_hash_find() - looks for a claim in the claim hash * @bat_priv: the bat priv with all the mesh interface information -* @data: search data (may be local/static data) +* @key: key of the searched claim * * Return: claim if found or NULL otherwise. */ static struct batadv_bla_claim *batadv_claim_hash_find (struct batadv_priv *bat_priv, -struct batadv_bla_claim *data) +const struct batadv_bla_claim_key *key) {struct batadv_hashtable *hash = bat_priv->bla.claim_hash;struct batadv_bla_claim *claim_tmp =NULL ;
@@ -228,12 +228,12 @@ batadv_claim_hash_find(struct batadv_priv *bat_priv,if (!hash)return NULL ; - index =batadv_choose_claim (data, hash->size); + index =batadv_choose_claim (key, hash->size); head = &hash->table[index];rcu_read_lock ();hlist_for_each_entry_rcu (claim, head, hash_entry) { -if (!batadv_compare_claim (&claim->hash_entry, data)) +if (!batadv_compare_claim (&claim->hash_entry, key))continue ;if (!kref_get_unless_zero (&claim->refcount))
@@ -615,7 +615,8 @@ static void batadv_bla_answer_request(struct batadv_priv *bat_priv,if (claim->backbone_gw != backbone_gw)continue ; -batadv_bla_send_claim (bat_priv, claim->addr, claim->vid, +batadv_bla_send_claim (bat_priv, claim->key.addr, + claim->key.vid,BATADV_CLAIM_TYPE_CLAIM ); }rcu_read_unlock ();
@@ -688,15 +689,15 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv, struct batadv_bla_backbone_gw *backbone_gw) {struct batadv_bla_backbone_gw *old_backbone_gw; -struct batadv_bla_claim search_claim; +struct batadv_bla_claim_key claim_key;struct batadv_bla_claim *claim;int hash_added;u16 claim_crc;bool changed; -ether_addr_copy (search_claim.addr, mac); - search_claim.vid = vid; - claim =batadv_claim_hash_find (bat_priv, &search_claim); +ether_addr_copy (claim_key.addr, mac); + claim_key.vid = vid; + claim =batadv_claim_hash_find (bat_priv, &claim_key); claim_crc =crc16 (0 , mac,ETH_ALEN );/* create a new claim entry if it does not exist yet. */
@@ -705,9 +706,9 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,if (!claim)return ; -ether_addr_copy (claim->addr, mac); +ether_addr_copy (claim->key.addr, mac); + claim->key.vid = vid;spin_lock_init (&claim->backbone_lock); - claim->vid = vid;WRITE_ONCE (claim->lasttime, jiffies);kref_get (&backbone_gw->refcount); claim->backbone_gw = backbone_gw;
@@ -720,7 +721,7 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,kref_get (&claim->refcount); hash_added =batadv_hash_add (bat_priv->bla.claim_hash, batadv_compare_claim, - batadv_choose_claim, claim, + batadv_choose_claim, &claim->key, &claim->hash_entry);if (unlikely (hash_added !=0 )) {
@@ -813,12 +814,12 @@ static void batadv_bla_del_claim(struct batadv_priv *bat_priv, {struct batadv_bla_claim *claim_removed_entry;struct hlist_node *claim_removed_node; -struct batadv_bla_claim search_claim; +struct batadv_bla_claim_key claim_key;struct batadv_bla_claim *claim; -ether_addr_copy (search_claim.addr, mac); - search_claim.vid = vid; - claim =batadv_claim_hash_find (bat_priv, &search_claim); +ether_addr_copy (claim_key.addr, mac); + claim_key.vid = vid; + claim =batadv_claim_hash_find (bat_priv, &claim_key);if (!claim)return ;
@@ -827,7 +828,8 @@ static void batadv_bla_del_claim(struct batadv_priv *bat_priv, claim_removed_node =batadv_hash_remove (bat_priv->bla.claim_hash, batadv_compare_claim, - batadv_choose_claim, claim); + batadv_choose_claim, + &claim->key);if (!claim_removed_node) goto free_claim;
@@ -1354,11 +1356,11 @@ static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, purge_now:batadv_dbg (BATADV_DBG_BLA , bat_priv,"%s(): %pM, vid %d\n" , __func__, - claim->addr, claim->vid); + claim->key.addr, claim->key.vid);batadv_handle_unclaim (bat_priv, primary_if, backbone_gw->orig, - claim->addr, claim->vid); + claim->key.addr, claim->key.vid); skip:batadv_backbone_gw_put (backbone_gw);batadv_claim_put (claim);
@@ -1959,8 +1961,8 @@ bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, unsigned short vid, int packet_type) {struct batadv_bla_backbone_gw *backbone_gw; +struct batadv_bla_claim_key claim_key;struct batadv_bla_claim *claim =NULL ; -struct batadv_bla_claim search_claim;struct batadv_hard_iface *primary_if;struct ethhdr *ethhdr;bool own_claim;
@@ -2006,9 +2008,9 @@ bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb, batadv_bla_check_ucast_duplist(bat_priv, skb)) goto handled; -ether_addr_copy (search_claim.addr, ethhdr->h_source); - search_claim.vid = vid; - claim =batadv_claim_hash_find (bat_priv, &search_claim); +ether_addr_copy (claim_key.addr, ethhdr->h_source); + claim_key.vid = vid; + claim =batadv_claim_hash_find (bat_priv, &claim_key);if (!claim) {bool local =batadv_is_my_client (bat_priv, ethhdr->h_source, vid);
@@ -2097,8 +2099,8 @@ bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb, unsigned short vid) {struct batadv_bla_backbone_gw *backbone_gw; +struct batadv_bla_claim_key claim_key;struct batadv_bla_claim *claim =NULL ; -struct batadv_bla_claim search_claim;struct batadv_hard_iface *primary_if;struct ethhdr *ethhdr;bool client_roamed;
@@ -2121,10 +2123,10 @@ bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,if (is_multicast_ether_addr (ethhdr->h_dest)) goto handled; -ether_addr_copy (search_claim.addr, ethhdr->h_source); - search_claim.vid = vid; +ether_addr_copy (claim_key.addr, ethhdr->h_source); + claim_key.vid = vid; - claim =batadv_claim_hash_find (bat_priv, &search_claim); + claim =batadv_claim_hash_find (bat_priv, &claim_key);/* if no claim exists, allow it. */ if (!claim)
@@ -2230,8 +2232,8 @@ batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, goto put_backbone_gw; } -if (nla_put (msg,BATADV_ATTR_BLA_ADDRESS ,ETH_ALEN , claim->addr) || -nla_put_u16 (msg,BATADV_ATTR_BLA_VID , claim->vid) || +if (nla_put (msg,BATADV_ATTR_BLA_ADDRESS ,ETH_ALEN , claim->key.addr) || +nla_put_u16 (msg,BATADV_ATTR_BLA_VID , claim->key.vid) ||nla_put (msg,BATADV_ATTR_BLA_BACKBONE ,ETH_ALEN , backbone_gw->orig) || nla_put_u16(msg, BATADV_ATTR_BLA_CRC,
@@ -2524,8 +2526,8 @@ bool batadv_bla_check_claim(struct batadv_priv *bat_priv, {struct batadv_hard_iface *primary_if =NULL ;struct batadv_bla_backbone_gw *backbone_gw; +struct batadv_bla_claim_key claim_key;struct batadv_bla_claim *claim =NULL ; -struct batadv_bla_claim search_claim;bool ret = true;if (!READ_ONCE (bat_priv->bridge_loop_avoidance))
@@ -2536,10 +2538,10 @@ bool batadv_bla_check_claim(struct batadv_priv *bat_priv,return ret;/* First look if the mac address is claimed */ -ether_addr_copy (search_claim.addr, addr); - search_claim.vid = vid; +ether_addr_copy (claim_key.addr, addr); + claim_key.vid = vid; - claim =batadv_claim_hash_find (bat_priv, &search_claim); + claim =batadv_claim_hash_find (bat_priv, &claim_key);/* If there is a claim and we are not owner of the claim, * return false.
net/batman-adv/types.h +10 -2
@@ -1825,14 +1825,22 @@ struct batadv_bla_backbone_gw { };/** -* struct batadv_bla_claim - claimed non-mesh client structure +* struct batadv_bla_claim_key - key of a claim in &batadv_priv_bla.claim_hash */ -struct batadv_bla_claim { +struct batadv_bla_claim_key {/** @addr: mac address of claimed non-mesh client */ u8 addr[ETH_ALEN ]__aligned(2) ;/** @vid: vlan id this client was detected on */ unsigned short vid; +}; + +/** +* struct batadv_bla_claim - claimed non-mesh client structure +*/ +struct batadv_bla_claim { +/** @key: hash key identifying the claim */ +struct batadv_bla_claim_key key;/** @backbone_gw: pointer to backbone gw claiming this client */ struct batadv_bla_backbone_gw *backbone_gw;
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v2 3/6 batman-adv: dat: use dedicated key type for DAT hash
In reply to Sven EckelmannThe DAT hash and the DHT candidate selection hash a dummy struct batadv_dat_entry on the stack in which only ip and vid are filled. batadv_dat_entry_hash_find() then compares ip and vid inline instead of using batadv_compare_dat(), so the key is spelled out in three places.
Move ip and vid into a new struct batadv_dat_entry_key, embedded as batadv_dat_entry::key. The helpers are only using this key for hashing/comparison.
--- net/batman-adv/distributed-arp-table.c | 73 ++++++++++++++++------------------ net/batman-adv/types.h | 19 ++++++--- 2 files changed, 49 insertions(+), 43 deletions(-)
net/batman-adv/distributed-arp-table.c +35 -38
@@ -275,19 +275,18 @@ static void batadv_dat_purge(struct work_struct *work)/** * batadv_compare_dat() - comparing function used in the local DAT hash table * @node: node in the local table -* @data2: second object to compare the node to +* @data2: &struct batadv_dat_entry_key to compare the node to * -* Return: true if the two entries are the same, false otherwise. +* Return: true if the entry has the given key, false otherwise. */ static bool batadv_compare_dat (const struct hlist_node *node,const void *data2) { -const struct batadv_dat_entry *entry1; -const struct batadv_dat_entry *entry2; +const struct batadv_dat_entry_key *key = data2; +const struct batadv_dat_entry *entry; - entry1 = container_of(node, struct batadv_dat_entry, hash_entry); - entry2 = data2; + entry = container_of(node, struct batadv_dat_entry, hash_entry); -return entry1->ip == entry2->ip && entry1->vid == entry2->vid; +return entry->key.ip == key->ip && entry->key.vid == key->vid; }/**
@@ -351,30 +350,30 @@ static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)/** * batadv_hash_dat() - compute the hash value for an IP address -* @data: data to hash +* @data: &struct batadv_dat_entry_key to hash * @size: size of the hash table * * Return: the selected index in the hash table for the given data. */ static u32 batadv_hash_dat (const void *data,u32 size) { -const struct batadv_dat_entry *dat = data; -const unsigned char *key; +const struct batadv_dat_entry_key *key = data; +const unsigned char *buf;u32 hash =0 ;__be16 vid;u32 i; - key = (__force const unsigned char *)&dat->ip; -for (i =0 ; i <sizeof (dat->ip); i++) { - hash += key[i]; + buf = (__force const unsigned char *)&key->ip; +for (i =0 ; i <sizeof (key->ip); i++) { + hash += buf[i]; hash += (hash <<10 ); hash ^= (hash >>6 ); } - vid =htons (dat->vid); - key = (__force const unsigned char *)&vid; -for (i =0 ; i <sizeof (dat->vid); i++) { - hash += key[i]; + vid =htons (key->vid); + buf = (__force const unsigned char *)&vid; +for (i =0 ; i <sizeof (key->vid); i++) { + hash += buf[i]; hash += (hash <<10 ); hash ^= (hash >>6 ); }
@@ -401,26 +400,23 @@ batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip, {struct batadv_hashtable *hash = bat_priv->dat.hash;struct batadv_dat_entry *dat_entry_tmp =NULL ; +struct batadv_dat_entry_key key = { + .ip = ip, + .vid = vid, + };struct batadv_dat_entry *dat_entry; -struct batadv_dat_entry to_find;struct hlist_head *head;u32 index;if (!hash)return NULL ; - to_find.ip = ip; - to_find.vid = vid; - - index =batadv_hash_dat (&to_find, hash->size); + index =batadv_hash_dat (&key, hash->size); head = &hash->table[index];rcu_read_lock ();hlist_for_each_entry_rcu (dat_entry, head, hash_entry) { -if (dat_entry->ip != ip) -continue ; - -if (dat_entry->vid != vid) +if (!batadv_compare_dat (&dat_entry->hash_entry, &key))continue ;if (!kref_get_unless_zero (&dat_entry->refcount))
@@ -456,7 +452,7 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, dat_entry->last_update = jiffies;batadv_dbg (BATADV_DBG_DAT , bat_priv,"Entry updated: %pI4 %pM (vid: %d)\n" , - &dat_entry->ip, mac_addr, + &dat_entry->key.ip, mac_addr,batadv_print_vid (vid)); goto out; }
@@ -465,15 +461,15 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,if (!dat_entry) goto out; - dat_entry->ip = ip; - dat_entry->vid = vid; + dat_entry->key.ip = ip; + dat_entry->key.vid = vid;atomic64_set (&dat_entry->mac_addr, u64_mac); dat_entry->last_update = jiffies;kref_init (&dat_entry->refcount);kref_get (&dat_entry->refcount); hash_added =batadv_hash_add (bat_priv->dat.hash, batadv_compare_dat, - batadv_hash_dat, dat_entry, + batadv_hash_dat, &dat_entry->key, &dat_entry->hash_entry);if (unlikely (hash_added !=0 )) {
@@ -483,7 +479,7 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, }batadv_dbg (BATADV_DBG_DAT , bat_priv,"New entry added: %pI4 %pM (vid: %d)\n" , - &dat_entry->ip, mac_addr,batadv_print_vid (vid)); + &dat_entry->key.ip, mac_addr,batadv_print_vid (vid)); out:batadv_dat_entry_put (dat_entry);
@@ -711,8 +707,11 @@ batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst, unsigned short vid) {batadv_dat_addr_t last_max =BATADV_DAT_ADDR_MAX ; +struct batadv_dat_entry_key key = { + .ip = ip_dst, + .vid = vid, + };struct batadv_dat_candidate *res; -struct batadv_dat_entry dat;batadv_dat_addr_t ip_key;int select;
@@ -723,9 +722,7 @@ batadv_dat_select_candidates(struct batadv_priv *bat_priv, __be32 ip_dst,if (!res)return NULL ; - dat.ip = ip_dst; - dat.vid = vid; - ip_key = (batadv_dat_addr_t )batadv_hash_dat (&dat, + ip_key = (batadv_dat_addr_t )batadv_hash_dat (&key,BATADV_DAT_ADDR_MAX ); batadv_dbg(BATADV_DBG_DAT, bat_priv,
@@ -957,9 +954,9 @@ batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,u64_to_ether_addr (u64_mac, mac);if (nla_put_in_addr (msg,BATADV_ATTR_DAT_CACHE_IP4ADDRESS , - dat_entry->ip) || + dat_entry->key.ip) ||nla_put (msg,BATADV_ATTR_DAT_CACHE_HWADDRESS ,ETH_ALEN , mac) || -nla_put_u16 (msg,BATADV_ATTR_DAT_CACHE_VID , dat_entry->vid) || +nla_put_u16 (msg,BATADV_ATTR_DAT_CACHE_VID , dat_entry->key.vid) ||nla_put_u32 (msg,BATADV_ATTR_LAST_SEEN_MSECS , msecs)) {genlmsg_cancel (msg, hdr);return -EMSGSIZE ;
@@ -1518,7 +1515,7 @@ bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,batadv_dbg (BATADV_DBG_DAT , bat_priv,"Doubled ARP reply removed: ARP MSG = [src: %pM-%pI4 dst: %pM-%pI4]; dat_entry: %pM-%pI4\n" , hw_src, &ip_src, hw_dst, &ip_dst, - mac, &dat_entry->ip); + mac, &dat_entry->key.ip); dropped = true; } }
net/batman-adv/types.h +14 -5
@@ -2212,20 +2212,29 @@ struct batadv_algo_ops {struct batadv_algo_gw_ops gw; }; +/** +* struct batadv_dat_entry_key - key of a DAT entry in &batadv_priv_dat.hash +* and in the DHT +*/ +struct batadv_dat_entry_key { +/** @ip: the IPv4 corresponding to this DAT/ARP entry */ +__be32 ip; + +/** @vid: the vlan ID associated to this entry */ +unsigned short vid; +}; +/** * struct batadv_dat_entry - it is a single entry of batman-adv ARP backend. It * is used to stored ARP entries needed for the global DAT cache */ struct batadv_dat_entry { -/** @ip: the IPv4 corresponding to this DAT/ARP entry */ -__be32 ip; +/** @key: hash key identifying the DAT entry */ +struct batadv_dat_entry_key key;/** @mac_addr: the MAC address associated to the stored IPv4 */ atomic64_t mac_addr; -/** @vid: the vlan ID associated to this entry */ -unsigned short vid; -/** * @last_update: time in jiffies when this entry was refreshed last time */
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v2 4/6 batman-adv: tt: use dedicated key type for hashes
In reply to Sven EckelmannThe TT hash lookup functions are given a struct batadv_tt_common_entry. For a lookup, a dummy TT entry is created on the stack and only its addr and vid members are filled. All other members stay uninitialized, and only by reading the hash and compare callbacks can one tell which members form the key.
Move addr and vid into a new struct batadv_tt_common_entry_key, embedded as batadv_tt_common_entry::key. The helpers are only using this key for hashing/comparison.
--- net/batman-adv/translation-table.c | 177 +++++++++++++++++++------------------ net/batman-adv/types.h | 13 ++- 2 files changed, 101 insertions(+), 89 deletions(-)
net/batman-adv/translation-table.c +90 -87
@@ -79,60 +79,62 @@ static void batadv_tt_global_del(struct batadv_priv *bat_priv, bool roaming);/** -* batadv_compare_tt() - check if two TT entries are the same -* @node: the list element pointer of the first TT entry -* @data2: pointer to the tt_common_entry of the second TT entry +* batadv_compare_tt() - check if a TT entry has the given key +* @node: the list element pointer of the TT entry +* @data2: &struct batadv_tt_common_entry_key to compare with * -* Compare the MAC address and the VLAN ID of the two TT entries and check if -* they are the same TT client. -* Return: true if the two TT clients are the same, false otherwise +* Compare the MAC address and the VLAN ID of the TT entry with the key and +* check if they are the same TT client. +* Return: true if the TT entry has the given key, false otherwise */ static bool batadv_compare_tt (const struct hlist_node *node,const void *data2) { - const void *data1 = container_of(node, struct batadv_tt_common_entry, - hash_entry); -const struct batadv_tt_common_entry *tt1 = data1; -const struct batadv_tt_common_entry *tt2 = data2; +const struct batadv_tt_common_entry_key *key = data2; +const struct batadv_tt_common_entry *tt; -return (tt1->vid == tt2->vid) &&batadv_compare_eth (data1, data2); + tt = container_of(node, struct batadv_tt_common_entry, hash_entry); + +return tt->key.vid == key->vid && +batadv_compare_eth (tt->key.addr, key->addr); }/** * batadv_compare_tt_entry() - check if a hash node is a specific TT entry * @node: the list element pointer of the TT entry stored in the bucket -* @data2: pointer to the tt_common_entry which is looked for +* @data2: &batadv_tt_common_entry.key of the TT entry which is looked for * -* Unlike batadv_compare_tt(), this only matches the very object which is -* passed as @data2 and not just any entry for the same TT client. It is meant -* for batadv_hash_remove() callers which must not unlink an entry they did not -* look up themselves. +* Unlike batadv_compare_tt(), this only matches the very object which embeds +* the key passed as @data2 and not just any entry for the same TT client. It is +* meant for batadv_hash_remove() callers which must not unlink an entry they +* did not look up themselves. * -* Return: true if @node belongs to @data2, false otherwise +* Return: true if @node belongs to the entry of @data2, false otherwise */ static bool batadv_compare_tt_entry (const struct hlist_node *node,const void *data2) { -const struct batadv_tt_common_entry *tt = data2; +const struct batadv_tt_common_entry *tt; + + tt = container_of(data2, struct batadv_tt_common_entry, key);return node == &tt->hash_entry; }/** * batadv_choose_tt() - return the index of the tt entry in the hash table -* @data: pointer to the tt_common_entry object to map +* @data: &struct batadv_tt_common_entry_key to map * @size: the size of the hash table * -* Return: the hash index where the object represented by 'data' should be +* Return: the hash index where the object with the key 'data' should be * stored at. */ static inline u32 batadv_choose_tt (const void *data,u32 size) { -const struct batadv_tt_common_entry *tt; +const struct batadv_tt_common_entry_key *key = data;u32 hash =0 ; - tt = data; - hash =jhash (&tt->addr,ETH_ALEN , hash); - hash =jhash (&tt->vid,sizeof (tt->vid), hash); + hash =jhash (&key->addr,ETH_ALEN , hash); + hash =jhash (&key->vid,sizeof (key->vid), hash);return hash % size; }
@@ -151,7 +153,7 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr, unsigned short vid) {struct batadv_tt_common_entry *tt_tmp =NULL ; -struct batadv_tt_common_entry to_search; +struct batadv_tt_common_entry_key key;struct batadv_tt_common_entry *tt;struct hlist_head *head;u32 index;
@@ -159,18 +161,15 @@ batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,if (!hash)return NULL ; -ether_addr_copy (to_search.addr, addr); - to_search.vid = vid; +ether_addr_copy (key.addr, addr); + key.vid = vid; - index =batadv_choose_tt (&to_search, hash->size); + index =batadv_choose_tt (&key, hash->size); head = &hash->table[index];rcu_read_lock ();hlist_for_each_entry_rcu (tt, head, hash_entry) { -if (!batadv_compare_eth (tt, addr)) -continue ; - -if (tt->vid != vid) +if (!batadv_compare_tt (&tt->hash_entry, &key))continue ;if (!kref_get_unless_zero (&tt->refcount))
@@ -640,8 +639,8 @@ static void __batadv_tt_local_event(struct batadv_priv *bat_priv,memset (tt_change_node->change.reserved,0 ,sizeof (tt_change_node->change.reserved)); -ether_addr_copy (tt_change_node->change.addr, common->addr); - tt_change_node->change.vid =htons (common->vid); +ether_addr_copy (tt_change_node->change.addr, common->key.addr); + tt_change_node->change.vid =htons (common->key.vid); tt_change_node->change.flags = flags; del_op_requested = flags &BATADV_TT_CLIENT_DEL ;
@@ -651,7 +650,7 @@ static void __batadv_tt_local_event(struct batadv_priv *bat_priv, changes =READ_ONCE (bat_priv->tt.local_changes);list_for_each_entry_safe (entry, safe, &bat_priv->tt.changes_list, list) { -if (!batadv_compare_eth (entry->change.addr, common->addr)) +if (!batadv_compare_eth (entry->change.addr, common->key.addr))continue ;if (entry->change.vid != tt_change_node->change.vid)
@@ -780,14 +779,14 @@ static void batadv_tt_global_free(struct batadv_priv *bat_priv,batadv_dbg (BATADV_DBG_TT , bat_priv,"Deleting global tt entry %pM (vid: %d): %s\n" , - tt_global->common.addr, -batadv_print_vid (tt_global->common.vid), message); + tt_global->common.key.addr, +batadv_print_vid (tt_global->common.key.vid), message);/* remove exactly this object when still present in hash */ tt_removed_node =batadv_hash_remove (bat_priv->tt.global_hash, batadv_compare_tt_entry, batadv_choose_tt, - &tt_global->common); + &tt_global->common.key);if (!tt_removed_node)return ;
@@ -836,8 +835,8 @@ static void batadv_tt_local_add_roam(struct batadv_priv *bat_priv, head = &tt_global->orig_list;rcu_read_lock ();hlist_for_each_entry_rcu (orig_entry, head, list) { -batadv_send_roam_adv (bat_priv, tt_global->common.addr, - tt_global->common.vid, +batadv_send_roam_adv (bat_priv, tt_global->common.key.addr, + tt_global->common.key.vid, orig_entry->orig_node); }rcu_read_unlock ();
@@ -893,7 +892,8 @@ static bool batadv_tt_local_add_existing(struct batadv_priv *bat_priv,if (common->flags &BATADV_TT_CLIENT_PENDING ) {batadv_dbg (BATADV_DBG_TT , bat_priv,"Re-adding pending client %pM (vid: %d)\n" , - common->addr,batadv_print_vid (common->vid)); + common->key.addr, +batadv_print_vid (common->key.vid));/* whatever the reason why the PENDING flag was set, * this is a client which was enqueued to be removed in * this orig_interval. Since it popped up again, the
@@ -907,7 +907,8 @@ static bool batadv_tt_local_add_existing(struct batadv_priv *bat_priv,if (common->flags &BATADV_TT_CLIENT_ROAM ) {batadv_dbg (BATADV_DBG_TT , bat_priv,"Roaming client %pM (vid: %d) came back to its original location\n" , - common->addr,batadv_print_vid (common->vid)); + common->key.addr, +batadv_print_vid (common->key.vid));/* the ROAM flag is set because this client roamed away * and the node got a roaming_advertisement message. Now * that the client popped up again at its original
@@ -968,8 +969,8 @@ batadv_tt_local_create(struct net_device *mesh_iface, const u8 *addr, addr,batadv_print_vid (vid), (u8)atomic_read(&bat_priv->tt.vn)); -ether_addr_copy (tt_local->common.addr, addr); - tt_local->common.vid = vid; +ether_addr_copy (tt_local->common.key.addr, addr); + tt_local->common.key.vid = vid;kref_init (&tt_local->common.refcount); tt_local->last_seen = jiffies; tt_local->common.added_at = tt_local->last_seen;
@@ -1083,7 +1084,7 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,kref_get (&tt_local->common.refcount); hash_added =batadv_hash_add (bat_priv->tt.local_hash, batadv_compare_tt, - batadv_choose_tt, &tt_local->common, + batadv_choose_tt, &tt_local->common.key, &tt_local->common.hash_entry);if (unlikely (hash_added !=0 )) {
@@ -1434,7 +1435,7 @@ batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, local = container_of(common, struct batadv_tt_local_entry, common); last_seen_msecs =jiffies_to_msecs (jiffies - local->last_seen); - vlan =batadv_meshif_vlan_get (bat_priv, common->vid); + vlan =batadv_meshif_vlan_get (bat_priv, common->key.vid);if (!vlan)return 0 ;
@@ -1452,9 +1453,9 @@ batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, flags =batadv_tt_flags_get (common); -if (nla_put (msg,BATADV_ATTR_TT_ADDRESS ,ETH_ALEN , common->addr) || +if (nla_put (msg,BATADV_ATTR_TT_ADDRESS ,ETH_ALEN , common->key.addr) ||nla_put_u32 (msg,BATADV_ATTR_TT_CRC32 , crc) || -nla_put_u16 (msg,BATADV_ATTR_TT_VID , common->vid) || +nla_put_u16 (msg,BATADV_ATTR_TT_VID , common->key.vid) ||nla_put_u32 (msg,BATADV_ATTR_TT_FLAGS , flags)) goto nla_put_failure;
@@ -1590,7 +1591,7 @@ batadv_tt_local_set_pending(struct batadv_priv *bat_priv,struct batadv_hashtable *hash = bat_priv->tt.local_hash;u32 i; - i =batadv_choose_tt (common, hash->size); + i =batadv_choose_tt (&common->key, hash->size);lockdep_assert_held (&hash->list_locks[i]);lockdep_assert_held (&common->flags_lock);
@@ -1599,8 +1600,8 @@ batadv_tt_local_set_pending(struct batadv_priv *bat_priv,batadv_dbg (BATADV_DBG_TT , bat_priv,"Local tt entry (%pM, vid: %d) pending to be removed: %s\n" , - tt_local_entry->common.addr, -batadv_print_vid (tt_local_entry->common.vid), message); + tt_local_entry->common.key.addr, +batadv_print_vid (tt_local_entry->common.key.vid), message); }/**
@@ -1632,7 +1633,7 @@ batadv_tt_local_mark_removed(struct batadv_priv *bat_priv,u16 flags;u32 i; - i =batadv_choose_tt (common, hash->size); + i =batadv_choose_tt (&common->key, hash->size); list_lock = &hash->list_locks[i];spin_lock_bh (list_lock);
@@ -1686,7 +1687,7 @@ batadv_tt_local_remove_now(struct batadv_priv *bat_priv,/* remove exactly this object when still present in hash */ tt_removed_node =batadv_hash_remove (bat_priv->tt.local_hash, batadv_compare_tt_entry, - batadv_choose_tt, common); + batadv_choose_tt, &common->key);if (!tt_removed_node)return ;
@@ -1697,7 +1698,7 @@ batadv_tt_local_remove_now(struct batadv_priv *bat_priv,* BATADV_TT_CLIENT_NEW check in batadv_tt_local_mark_removed(). */ if (!(batadv_tt_flags_get (common) &BATADV_TT_CLIENT_NEW )) -batadv_tt_local_size_dec (bat_priv, common->vid); +batadv_tt_local_size_dec (bat_priv, common->key.vid);/* drop reference of remove hash entry */ batadv_tt_local_entry_put (tt_local_entry);
@@ -2015,7 +2016,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,INIT_HLIST_NODE (&orig_entry->list);kref_get (&orig_node->refcount); -batadv_tt_global_size_inc (orig_node, tt_global->common.vid); +batadv_tt_global_size_inc (orig_node, tt_global->common.key.vid); orig_entry->orig_node = orig_node; orig_entry->ttvn = ttvn; orig_entry->flags = flags;
@@ -2056,11 +2057,11 @@ batadv_tt_global_purge_local(struct batadv_priv *bat_priv,/* Do not remove multicast addresses from the local hash on * global additions */ -if (is_multicast_ether_addr (common->addr)) +if (is_multicast_ether_addr (common->key.addr))return ;/* remove address from local hash if present */ - local_flags =batadv_tt_local_remove (bat_priv, common->addr, common->vid, + local_flags =batadv_tt_local_remove (bat_priv, common->key.addr, common->key.vid,"global tt received" , flags &BATADV_TT_CLIENT_ROAM );
@@ -2106,7 +2107,7 @@ batadv_tt_global_merge_flags(struct batadv_tt_global_entry *tt_global_entry,* TT_CLIENT_TEMP, therefore they have to be copied in the * client entry */ -if (!is_multicast_ether_addr (common->addr)) +if (!is_multicast_ether_addr (common->key.addr)) common->flags |= flags & (~BATADV_TT_SYNC_MASK );/* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
@@ -2150,11 +2151,11 @@ batadv_tt_global_create(const unsigned char *tt_addr, unsigned short vid,return NULL ; common = &tt_global_entry->common; -ether_addr_copy (common->addr, tt_addr); - common->vid = vid; +ether_addr_copy (common->key.addr, tt_addr); + common->key.vid = vid;spin_lock_init (&common->flags_lock); -if (!is_multicast_ether_addr (common->addr)) { +if (!is_multicast_ether_addr (common->key.addr)) {scoped_guard (spinlock_bh, &common->flags_lock) common->flags = flags & (~BATADV_TT_SYNC_MASK); }
@@ -2236,7 +2237,7 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,kref_get (&common->refcount); hash_added =batadv_hash_add (bat_priv->tt.global_hash, batadv_compare_tt, - batadv_choose_tt, common, + batadv_choose_tt, &common->key, &common->hash_entry);if (unlikely (hash_added !=0 )) {
@@ -2280,7 +2281,7 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,batadv_dbg (BATADV_DBG_TT , bat_priv,"Creating new global tt entry: %pM (vid: %d, via %pM)\n" , - common->addr,batadv_print_vid (common->vid), + common->key.addr,batadv_print_vid (common->key.vid), orig_node->orig); ret = true;
@@ -2362,7 +2363,7 @@ batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,u32 crc; vlan =batadv_orig_node_vlan_get (orig->orig_node, - common->vid); + common->key.vid);if (!vlan)return 0 ;
@@ -2379,13 +2380,13 @@ batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq, flags = (batadv_tt_flags_get (common) & (~BATADV_TT_SYNC_MASK )) | orig->flags; last_ttvn =READ_ONCE (orig->orig_node->last_ttvn); -if (nla_put (msg,BATADV_ATTR_TT_ADDRESS ,ETH_ALEN , common->addr) || +if (nla_put (msg,BATADV_ATTR_TT_ADDRESS ,ETH_ALEN , common->key.addr) ||nla_put (msg,BATADV_ATTR_ORIG_ADDRESS ,ETH_ALEN , orig->orig_node->orig) ||nla_put_u8 (msg,BATADV_ATTR_TT_TTVN , orig->ttvn) ||nla_put_u8 (msg,BATADV_ATTR_TT_LAST_TTVN , last_ttvn) ||nla_put_u32 (msg,BATADV_ATTR_TT_CRC32 , crc) || -nla_put_u16 (msg,BATADV_ATTR_TT_VID , common->vid) || +nla_put_u16 (msg,BATADV_ATTR_TT_VID , common->key.vid) ||nla_put_u32 (msg,BATADV_ATTR_TT_FLAGS , flags)) goto nla_put_failure;
@@ -2561,7 +2562,7 @@ _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,lockdep_assert_held (&tt_global_entry->list_lock);batadv_tt_global_size_dec (orig_entry->orig_node, - tt_global_entry->common.vid); + tt_global_entry->common.key.vid);atomic_dec (&tt_global_entry->orig_list_count);/* requires holding tt_global_entry->list_lock and orig_entry->list * being part of a list
@@ -2614,11 +2615,11 @@ batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv, head = &tt_global_entry->orig_list;hlist_for_each_entry_safe (orig_entry, safe, head, list) {if (orig_entry->orig_node == orig_node) { - vid = tt_global_entry->common.vid; + vid = tt_global_entry->common.key.vid;batadv_dbg (BATADV_DBG_TT , bat_priv,"Deleting %pM from global tt entry %pM (vid: %d): %s\n" , orig_node->orig, - tt_global_entry->common.addr, + tt_global_entry->common.key.addr,batadv_print_vid (vid), message);_batadv_tt_global_del_orig_entry (tt_global_entry, orig_entry);
@@ -2726,7 +2727,7 @@ static void batadv_tt_global_del(struct batadv_priv *bat_priv,* the global entry, since it is useless now. */ local_entry =batadv_tt_local_hash_find (bat_priv, - tt_global_entry->common.addr, + tt_global_entry->common.key.addr, vid);if (local_entry) {/* local entry exists, case 2: client roamed to us. */
@@ -2777,7 +2778,8 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,hlist_for_each_entry_safe (tt_common_entry, safe, head, hash_entry) {/* remove only matching entries */ -if (match_vid >=0 && tt_common_entry->vid != match_vid) +if (match_vid >=0 && + tt_common_entry->key.vid != match_vid)continue ; tt_global = container_of(tt_common_entry,
@@ -2788,10 +2790,10 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, orig_node, message);if (hlist_empty (&tt_global->orig_list)) { - vid = tt_global->common.vid; + vid = tt_global->common.key.vid;batadv_dbg (BATADV_DBG_TT , bat_priv,"Deleting global tt entry %pM (vid: %d): %s\n" , - tt_global->common.addr, + tt_global->common.key.addr,batadv_print_vid (vid), message);hlist_del_rcu (&tt_common_entry->hash_entry);batadv_tt_global_entry_put (tt_global);
@@ -2869,8 +2871,8 @@ static void batadv_tt_global_purge(struct batadv_priv *bat_priv)batadv_dbg (BATADV_DBG_TT , bat_priv,"Deleting global tt entry %pM (vid: %d): %s\n" , - tt_global->common.addr, -batadv_print_vid (tt_global->common.vid), + tt_global->common.key.addr, +batadv_print_vid (tt_global->common.key.vid), msg);hlist_del_rcu (&tt_common->hash_entry);
@@ -3070,7 +3072,7 @@ static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,/* compute the CRC only for entries belonging to the * VLAN identified by the vid passed as parameter */ -if (tt_common->vid != vid) +if (tt_common->key.vid != vid)continue ; tt_flags =batadv_tt_flags_get (tt_common);
@@ -3101,7 +3103,7 @@ static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,/* use network order to read the VID: this ensures that * every node reads the bytes in the same order. */ - tmp_vid =htons (tt_common->vid); + tmp_vid =htons (tt_common->key.vid); crc_tmp =crc32c (0 , &tmp_vid,sizeof (tmp_vid));/* compute the CRC on flags that have to be kept in sync
@@ -3110,7 +3112,7 @@ static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv, flags = tt_orig->flags; crc_tmp =crc32c (crc_tmp, &flags,sizeof (flags)); - crc ^=crc32c (crc_tmp, tt_common->addr,ETH_ALEN ); + crc ^=crc32c (crc_tmp, tt_common->key.addr,ETH_ALEN );batadv_tt_orig_list_entry_put (tt_orig); }
@@ -3152,7 +3154,7 @@ static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,/* compute the CRC only for entries belonging to the * VLAN identified by vid */ -if (tt_common->vid != vid) +if (tt_common->key.vid != vid)continue ; tt_flags =batadv_tt_flags_get (tt_common);
@@ -3168,7 +3170,7 @@ static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,/* use network order to read the VID: this ensures that * every node reads the bytes in the same order. */ - tmp_vid =htons (tt_common->vid); + tmp_vid =htons (tt_common->key.vid); crc_tmp =crc32c (0 , &tmp_vid,sizeof (tmp_vid));/* compute the CRC on flags that have to be kept in sync
@@ -3176,7 +3178,7 @@ static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,*/ crc_tmp =crc32c (crc_tmp, &flags,sizeof (flags)); - crc ^=crc32c (crc_tmp, tt_common->addr,ETH_ALEN ); + crc ^=crc32c (crc_tmp, tt_common->key.addr,ETH_ALEN ); }rcu_read_unlock (); }
@@ -3433,9 +3435,10 @@ static u16 batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,if (!ret)continue ; -ether_addr_copy (tt_change->addr, tt_common_entry->addr); +ether_addr_copy (tt_change->addr, + tt_common_entry->key.addr); tt_change->flags = flags; - tt_change->vid =htons (tt_common_entry->vid); + tt_change->vid =htons (tt_common_entry->key.vid);memset (tt_change->reserved,0 ,sizeof (tt_change->reserved));
@@ -4334,7 +4337,7 @@ static void batadv_tt_local_transition_new(struct batadv_priv *bat_priv)continue ;batadv_tt_local_size_inc (bat_priv, - tt_common_entry->vid); + tt_common_entry->key.vid); }spin_unlock_bh (list_lock); }
@@ -4378,8 +4381,8 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)batadv_dbg (BATADV_DBG_TT , bat_priv,"Deleting local tt entry (%pM, vid: %d): pending\n" , - tt_common->addr, -batadv_print_vid (tt_common->vid)); + tt_common->key.addr, +batadv_print_vid (tt_common->key.vid));hlist_del_rcu (&tt_common->hash_entry);
@@ -4387,7 +4390,7 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)* never counted and must not be uncounted here. */ if (!(tt_common->flags &BATADV_TT_CLIENT_NEW )) -batadv_tt_local_size_dec (bat_priv, tt_common->vid); +batadv_tt_local_size_dec (bat_priv, tt_common->key.vid); }if (cont)
net/batman-adv/types.h +11 -2
@@ -1872,14 +1872,23 @@ struct batadv_bla_claim {#endif /** -* struct batadv_tt_common_entry - tt local & tt global common data +* struct batadv_tt_common_entry_key - key of a TT entry in +* &batadv_priv_tt.local_hash or &batadv_priv_tt.global_hash */ -struct batadv_tt_common_entry { +struct batadv_tt_common_entry_key {/** @addr: mac address of non-mesh client */ u8 addr[ETH_ALEN ]__aligned(2) ;/** @vid: VLAN identifier */ unsigned short vid; +}; + +/** +* struct batadv_tt_common_entry - tt local & tt global common data +*/ +struct batadv_tt_common_entry { +/** @key: hash key identifying the TT client */ +struct batadv_tt_common_entry_key key;/** * @hash_entry: hlist node for &batadv_priv_tt.local_hash or for
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerv2 cover batman-adv: make aggregated hash keys easier to identify
Some of the fixes in the recent past were about the incorrectly hashing and/or comparison of hash keys. I took the opportunity to have an easier identifiable key for the hashes by using a separate struct in case they are aggregated types.
As side effect, it is now possible to reserve less stack space when trying to find an hash entry. Only they is now needed for the search.
---
Changes in v2:
- resend due to problems in the sashiko pipeline
- Link to v1: https://patch.msgid.link/[email protected]
---
Sven Eckelmann (6):
batman-adv: bla: use dedicated key type for claim hash
batman-adv: bla: use dedicated key type for backbone gateway hash
batman-adv: dat: use dedicated key type for DAT hash
batman-adv: tt: use dedicated key type for hashes
batman-adv: originator: pass explicit key to orig_hash
batman-adv: hash: document key argument of hash operations
net/batman-adv/bat_iv_ogm.c | 2 +- net/batman-adv/bat_v_ogm.c | 2 +- net/batman-adv/bridge_loop_avoidance.c | 201 +++++++++++++++++---------------- net/batman-adv/distributed-arp-table.c | 73 ++++++------ net/batman-adv/hash.h | 13 +-- net/batman-adv/originator.c | 19 ++-- net/batman-adv/originator.h | 2 +- net/batman-adv/translation-table.c | 177 +++++++++++++++-------------- net/batman-adv/types.h | 57 ++++++++-- 9 files changed, 291 insertions(+), 255 deletions(-)
---
base-commit: 0a79fef370780da6a67906bf2d83be8e2bd4e24d
change-id: 20261013-hash-key-73f787e86734
Best regards,
Signature · 1 line
Sven Eckelmann <[email protected]>
Sven Eckelmann
maintainerPATCH v2 6/6 batman-adv: hash: document key argument of hash operations
With all users converted, the data passed to batadv_hash_add(), batadv_hash_remove() and the compare/choose callbacks is always the key of the hash table, never a (partially initialized) element. The kernel-doc of batadv_hash_remove() even still suggested to pass "the structure you use with just the key filled".
Document the actual key contract.
--- net/batman-adv/hash.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
net/batman-adv/hash.h +6 -7
@@ -21,7 +21,7 @@/** * typedef batadv_hashdata_compare_cb - hash element comparison callback * @node: hlist node of the element currently stored in the bucket -* @key: opaque payload to compare @node's key against +* @key: key to compare @node's key against * * Compare hash element by its keys. *
@@ -32,7 +32,7 @@ typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *node,/** * typedef batadv_hashdata_choose_cb - hash bucket selection callback -* @key: opaque payload whose key selects the bucket +* @key: key which selects the bucket * @size: number of buckets in the hash table * * Return: bucket index derived from the key in @key and the table @size.
@@ -80,7 +80,8 @@ void batadv_hash_destroy(struct batadv_hashtable *hash);* @hash: storage hash table * @compare: callback to determine if 2 hash elements are identical * @choose: callback calculating the hash index -* @data: data passed to the aforementioned callbacks as argument +* @data: key of the element to add (the key embedded in the element), +* passed to the aforementioned callbacks as argument * @data_node: to be added element * * Return: 0 on success, 1 if the element already is in the hash
@@ -132,10 +133,8 @@ static inline int batadv_hash_add(struct batadv_hashtable *hash,* @hash: hash table * @compare: callback to determine if 2 hash elements are identical * @choose: callback calculating the hash index -* @data: data passed to the aforementioned callbacks as argument -* -* data could be the structure you use with just the key filled, we just need -* the key for comparing. +* @data: key of the element to remove, passed to the aforementioned callbacks +* as argument * * Return: returns pointer to hlist_node on success, so you can remove the used * structure yourself, or NULL on error
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v2 2/6 batman-adv: bla: use dedicated key type for backbone gateway hash
In reply to Sven Eckelmannbatadv_backbone_hash_find() creates a dummy struct batadv_bla_backbone_gw on the stack and only fills orig and vid for the lookup. Which members are part of the key is only visible in the hash and compare callbacks.
Move orig and vid into a new struct batadv_bla_backbone_gw_key, embedded as batadv_bla_backbone_gw::key. The helpers are only using this key for hashing/comparison.
--- net/batman-adv/bridge_loop_avoidance.c | 103 ++++++++++++++++----------------- net/batman-adv/types.h | 13 ++++- 2 files changed, 62 insertions(+), 54 deletions(-)
net/batman-adv/bridge_loop_avoidance.c +51 -52
@@ -77,42 +77,41 @@ static inline u32 batadv_choose_claim(const void *data, u32 size)/** * batadv_choose_backbone_gw() - choose the right bucket for a backbone gateway. -* @data: data to hash +* @data: &struct batadv_bla_backbone_gw_key to hash * @size: size of the hash table * * Return: the hash index of the backbone gateway */ static inline u32 batadv_choose_backbone_gw (const void *data,u32 size) { -const struct batadv_bla_backbone_gw *gw; +const struct batadv_bla_backbone_gw_key *key = data;u32 hash =0 ; - gw = data; - hash =jhash (&gw->orig,sizeof (gw->orig), hash); - hash =jhash (&gw->vid,sizeof (gw->vid), hash); + hash =jhash (&key->orig,sizeof (key->orig), hash); + hash =jhash (&key->vid,sizeof (key->vid), hash);return hash % size; }/** -* batadv_compare_backbone_gw() - compare address and vid of two backbone gws -* @node: list node of the first entry to compare -* @data2: pointer to the second backbone gateway +* batadv_compare_backbone_gw() - compare key of a backbone gw with a search key +* @node: list node of the backbone gateway to compare +* @data2: &struct batadv_bla_backbone_gw_key to compare with * -* Return: true if the backbones have the same data, false otherwise +* Return: true if the backbone gateway has the given key, false otherwise */ static bool batadv_compare_backbone_gw (const struct hlist_node *node,const void *data2) { - const void *data1 = container_of(node, struct batadv_bla_backbone_gw, - hash_entry); -const struct batadv_bla_backbone_gw *gw1 = data1; -const struct batadv_bla_backbone_gw *gw2 = data2; +const struct batadv_bla_backbone_gw_key *key = data2; +const struct batadv_bla_backbone_gw *gw; -if (!batadv_compare_eth (gw1->orig, gw2->orig)) + gw = container_of(node, struct batadv_bla_backbone_gw, hash_entry); + +if (!batadv_compare_eth (gw->key.orig, key->orig))return false; -if (gw1->vid != gw2->vid) +if (gw->key.vid != key->vid)return false;return true;
@@ -261,24 +260,23 @@ batadv_backbone_hash_find(struct batadv_priv *bat_priv, const u8 *addr, {struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;struct batadv_bla_backbone_gw *backbone_gw_tmp =NULL ; -struct batadv_bla_backbone_gw search_entry;struct batadv_bla_backbone_gw *backbone_gw; +struct batadv_bla_backbone_gw_key key;struct hlist_head *head;int index;if (!hash)return NULL ; -ether_addr_copy (search_entry.orig, addr); - search_entry.vid = vid; +ether_addr_copy (key.orig, addr); + key.vid = vid; - index =batadv_choose_backbone_gw (&search_entry, hash->size); + index =batadv_choose_backbone_gw (&key, hash->size); head = &hash->table[index];rcu_read_lock ();hlist_for_each_entry_rcu (backbone_gw, head, hash_entry) { -if (!batadv_compare_backbone_gw (&backbone_gw->hash_entry, - &search_entry)) +if (!batadv_compare_backbone_gw (&backbone_gw->hash_entry, &key))continue ;if (!kref_get_unless_zero (&backbone_gw->refcount))
@@ -462,9 +460,9 @@ static void batadv_bla_loopdetect_report(struct work_struct *work)batadv_info (bat_priv->mesh_iface,"Possible loop on VLAN %d detected which can't be handled by BLA - please check your network setup!\n" , -batadv_print_vid (backbone_gw->vid)); +batadv_print_vid (backbone_gw->key.vid));snprintf (vid_str,sizeof (vid_str),"%d" , -batadv_print_vid (backbone_gw->vid)); +batadv_print_vid (backbone_gw->key.vid)); vid_str[sizeof (vid_str) -1 ] =0 ; batadv_throw_uevent(bat_priv, BATADV_UEV_BLA, BATADV_UEV_LOOPDETECT,
@@ -503,21 +501,21 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, const u8 *orig,if (!entry)return NULL ; - entry->vid = vid; + entry->key.vid = vid;WRITE_ONCE (entry->lasttime, jiffies); entry->crc =BATADV_BLA_CRC_INIT ; entry->bat_priv = bat_priv;spin_lock_init (&entry->crc_lock); entry->state =BATADV_BLA_BACKBONE_GW_SYNCED ; entry->wait_periods =0 ; -ether_addr_copy (entry->orig, orig); +ether_addr_copy (entry->key.orig, orig);INIT_WORK (&entry->report_work, batadv_bla_loopdetect_report);kref_init (&entry->refcount);kref_get (&entry->refcount); hash_added =batadv_hash_add (bat_priv->bla.backbone_hash, batadv_compare_backbone_gw, - batadv_choose_backbone_gw, entry, + batadv_choose_backbone_gw, &entry->key, &entry->hash_entry);if (unlikely (hash_added !=0 )) {
@@ -641,11 +639,11 @@ static void batadv_bla_send_request(struct batadv_bla_backbone_gw *backbone_gw)batadv_bla_del_backbone_claims (backbone_gw);batadv_dbg (BATADV_DBG_BLA , backbone_gw->bat_priv, -"Sending REQUEST to %pM\n" , backbone_gw->orig); +"Sending REQUEST to %pM\n" , backbone_gw->key.orig);/* send request */ -batadv_bla_send_claim (backbone_gw->bat_priv, backbone_gw->orig, - backbone_gw->vid,BATADV_CLAIM_TYPE_REQUEST ); +batadv_bla_send_claim (backbone_gw->bat_priv, backbone_gw->key.orig, + backbone_gw->key.vid,BATADV_CLAIM_TYPE_REQUEST );/* no local broadcasts should be sent or received, for now. */ spin_lock_bh (&backbone_gw->bat_priv->bla.num_requests_lock);
@@ -673,7 +671,7 @@ static void batadv_bla_send_announce(struct batadv_priv *bat_priv,spin_unlock_bh (&backbone_gw->crc_lock);memcpy (&mac[4 ], &crc,2 ); -batadv_bla_send_claim (bat_priv, mac, backbone_gw->vid, +batadv_bla_send_claim (bat_priv, mac, backbone_gw->key.vid,BATADV_CLAIM_TYPE_ANNOUNCE ); }
@@ -761,7 +759,7 @@ static void batadv_bla_add_claim(struct batadv_priv *bat_priv,batadv_dbg (BATADV_DBG_BLA , bat_priv,"%s(): changing ownership for %pM, vid %d to gw %pM\n" , __func__, mac,batadv_print_vid (vid), - backbone_gw->orig); + backbone_gw->key.orig);/* add claim address to new backbone_gw */ spin_lock_bh (&backbone_gw->crc_lock);
@@ -874,7 +872,7 @@ static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,batadv_dbg (BATADV_DBG_BLA , bat_priv,"%s(): ANNOUNCE vid %d (sent by %pM)... CRC = %#.4x\n" , - __func__,batadv_print_vid (vid), backbone_gw->orig, crc); + __func__,batadv_print_vid (vid), backbone_gw->key.orig, crc);spin_lock_bh (&backbone_gw->crc_lock); backbone_crc = backbone_gw->crc;
@@ -883,8 +881,8 @@ static bool batadv_handle_announce(struct batadv_priv *bat_priv, u8 *an_addr,if (backbone_crc != crc) {batadv_dbg (BATADV_DBG_BLA , backbone_gw->bat_priv,"%s(): CRC FAILED for %pM/%d (my = %#.4x, sent = %#.4x)\n" , - __func__, backbone_gw->orig, -batadv_print_vid (backbone_gw->vid), + __func__, backbone_gw->key.orig, +batadv_print_vid (backbone_gw->key.vid), backbone_crc, crc);batadv_bla_send_request (backbone_gw);
@@ -1271,7 +1269,7 @@ static void batadv_bla_purge_backbone_gw(struct batadv_priv *bat_priv, int now)batadv_dbg (BATADV_DBG_BLA , backbone_gw->bat_priv,"%s(): backbone gw %pM timed out\n" , - __func__, backbone_gw->orig); + __func__, backbone_gw->key.orig); purge_now: purged = true;
@@ -1342,7 +1340,7 @@ static void batadv_bla_purge_claims(struct batadv_priv *bat_priv,if (now) goto purge_now; -if (!batadv_compare_eth (backbone_gw->orig, +if (!batadv_compare_eth (backbone_gw->key.orig, primary_if->net_dev->dev_addr)) goto skip;
@@ -1359,7 +1357,7 @@ static void batadv_bla_purge_claims(struct batadv_priv *bat_priv, claim->key.addr, claim->key.vid);batadv_handle_unclaim (bat_priv, primary_if, - backbone_gw->orig, + backbone_gw->key.orig, claim->key.addr, claim->key.vid); skip:batadv_backbone_gw_put (backbone_gw);
@@ -1410,11 +1408,11 @@ void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,rcu_read_lock ();hlist_for_each_entry_rcu (backbone_gw, head, hash_entry) {/* own orig still holds the old value. */ -if (!batadv_compare_eth (backbone_gw->orig, +if (!batadv_compare_eth (backbone_gw->key.orig, oldif->net_dev->dev_addr))continue ; -ether_addr_copy (backbone_gw->orig, +ether_addr_copy (backbone_gw->key.orig, primary_if->net_dev->dev_addr);/* send an announce frame so others will ask for our * claims and update their tables.
@@ -1440,9 +1438,10 @@ batadv_bla_send_loopdetect(struct batadv_priv *bat_priv, struct batadv_bla_backbone_gw *backbone_gw) {batadv_dbg (BATADV_DBG_BLA , bat_priv,"Send loopdetect frame for vid %d\n" , - backbone_gw->vid); + backbone_gw->key.vid);batadv_bla_send_claim (bat_priv, bat_priv->bla.loopdetect_addr, - backbone_gw->vid,BATADV_CLAIM_TYPE_LOOPDETECT ); + backbone_gw->key.vid, +BATADV_CLAIM_TYPE_LOOPDETECT ); }/**
@@ -1523,7 +1522,7 @@ static void batadv_bla_periodic_work(struct work_struct *work)rcu_read_lock ();hlist_for_each_entry_rcu (backbone_gw, head, hash_entry) { -if (!batadv_compare_eth (backbone_gw->orig, +if (!batadv_compare_eth (backbone_gw->key.orig, primary_if->net_dev->dev_addr))continue ;
@@ -1809,8 +1808,8 @@ bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, u8 *orig,rcu_read_lock ();hlist_for_each_entry_rcu (backbone_gw, head, hash_entry) { -if (batadv_compare_eth (backbone_gw->orig, orig) && - backbone_gw->vid == vid) { +if (batadv_compare_eth (backbone_gw->key.orig, orig) && + backbone_gw->key.vid == vid) {rcu_read_unlock ();return true; }
@@ -2030,7 +2029,7 @@ bool batadv_bla_rx(struct batadv_priv *bat_priv, struct sk_buff *skb,/* if it is our own claim ... */ backbone_gw =batadv_bla_claim_get_backbone_gw (claim); - own_claim =batadv_compare_eth (backbone_gw->orig, + own_claim =batadv_compare_eth (backbone_gw->key.orig, primary_if->net_dev->dev_addr);batadv_backbone_gw_put (backbone_gw);
@@ -2134,7 +2133,7 @@ bool batadv_bla_tx(struct batadv_priv *bat_priv, struct sk_buff *skb,/* check if we are responsible. */ backbone_gw =batadv_bla_claim_get_backbone_gw (claim); - client_roamed =batadv_compare_eth (backbone_gw->orig, + client_roamed =batadv_compare_eth (backbone_gw->key.orig, primary_if->net_dev->dev_addr);batadv_backbone_gw_put (backbone_gw);
@@ -2220,7 +2219,7 @@ batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid, backbone_gw =batadv_bla_claim_get_backbone_gw (claim); - is_own =batadv_compare_eth (backbone_gw->orig, primary_addr); + is_own =batadv_compare_eth (backbone_gw->key.orig, primary_addr);spin_lock_bh (&backbone_gw->crc_lock); backbone_crc = backbone_gw->crc;
@@ -2235,7 +2234,7 @@ batadv_bla_claim_dump_entry(struct sk_buff *msg, u32 portid,if (nla_put (msg,BATADV_ATTR_BLA_ADDRESS ,ETH_ALEN , claim->key.addr) ||nla_put_u16 (msg,BATADV_ATTR_BLA_VID , claim->key.vid) ||nla_put (msg,BATADV_ATTR_BLA_BACKBONE ,ETH_ALEN , - backbone_gw->orig) || + backbone_gw->key.orig) ||nla_put_u16 (msg,BATADV_ATTR_BLA_CRC , backbone_crc)) {genlmsg_cancel (msg, hdr);
@@ -2381,7 +2380,7 @@ batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid,genl_dump_check_consistent (cb, hdr); - is_own =batadv_compare_eth (backbone_gw->orig, primary_addr); + is_own =batadv_compare_eth (backbone_gw->key.orig, primary_addr);spin_lock_bh (&backbone_gw->crc_lock); backbone_crc = backbone_gw->crc;
@@ -2396,8 +2395,8 @@ batadv_bla_backbone_dump_entry(struct sk_buff *msg, u32 portid, }if (nla_put (msg,BATADV_ATTR_BLA_BACKBONE ,ETH_ALEN , - backbone_gw->orig) || -nla_put_u16 (msg,BATADV_ATTR_BLA_VID , backbone_gw->vid) || + backbone_gw->key.orig) || +nla_put_u16 (msg,BATADV_ATTR_BLA_VID , backbone_gw->key.vid) ||nla_put_u16 (msg,BATADV_ATTR_BLA_CRC , backbone_crc) ||nla_put_u32 (msg,BATADV_ATTR_LAST_SEEN_MSECS , msecs)) {
@@ -2549,7 +2548,7 @@ bool batadv_bla_check_claim(struct batadv_priv *bat_priv,if (claim) { backbone_gw =batadv_bla_claim_get_backbone_gw (claim); -if (!batadv_compare_eth (backbone_gw->orig, +if (!batadv_compare_eth (backbone_gw->key.orig, primary_if->net_dev->dev_addr)) ret = false;
net/batman-adv/types.h +11 -2
@@ -1777,9 +1777,10 @@ enum batadv_bla_backbone_gw_state { };/** -* struct batadv_bla_backbone_gw - batman-adv gateway bridged into the LAN +* struct batadv_bla_backbone_gw_key - key of a backbone gateway in +* &batadv_priv_bla.backbone_hash */ -struct batadv_bla_backbone_gw { +struct batadv_bla_backbone_gw_key {/** * @orig: originator address of backbone node (mac address of primary * iface)
@@ -1788,6 +1789,14 @@ struct batadv_bla_backbone_gw {/** @vid: vlan id this gateway was detected on */ unsigned short vid; +}; + +/** +* struct batadv_bla_backbone_gw - batman-adv gateway bridged into the LAN +*/ +struct batadv_bla_backbone_gw { +/** @key: hash key identifying the backbone gateway */ +struct batadv_bla_backbone_gw_key key;/** @hash_entry: hlist node for &batadv_priv_bla.backbone_hash */ struct hlist_node hash_entry;
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerPATCH v2 5/6 batman-adv: originator: pass explicit key to orig_hash
In reply to Sven EckelmannThe key of the orig_hash is the originator address. But batadv_hash_add() was given the whole orig_node, while batadv_orig_hash_find() is given the bare address, and batadv_compare_orig() and the lookup compare the orig_node pointer itself against the address. This only works because orig is the first member of struct batadv_orig_node.
Pass orig_node->orig as key to batadv_hash_add(), compare explicitly against orig_node->orig in batadv_compare_orig(), use it for the lookup, and let batadv_orig_hash_find() take a const u8 * address instead of an untyped pointer.
--- net/batman-adv/bat_iv_ogm.c | 2 +- net/batman-adv/bat_v_ogm.c | 2 +- net/batman-adv/originator.c | 19 ++++++++++--------- net/batman-adv/originator.h | 2 +- 4 files changed, 13 insertions(+), 12 deletions(-)
net/batman-adv/bat_iv_ogm.c +1 -1
@@ -155,7 +155,7 @@ batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)kref_get (&orig_node->refcount); hash_added =batadv_hash_add (bat_priv->orig_hash, batadv_compare_orig, - batadv_choose_orig, orig_node, + batadv_choose_orig, orig_node->orig, &orig_node->hash_entry);if (hash_added !=0 ) goto free_orig_node_hash;
net/batman-adv/bat_v_ogm.c +1 -1
@@ -68,7 +68,7 @@ struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,kref_get (&orig_node->refcount); hash_added =batadv_hash_add (bat_priv->orig_hash, batadv_compare_orig, - batadv_choose_orig, orig_node, + batadv_choose_orig, orig_node->orig, &orig_node->hash_entry);if (hash_added !=0 ) {/* remove refcnt for newly created orig_node and hash entry */
net/batman-adv/originator.c +10 -9
@@ -45,12 +45,12 @@ static struct lock_class_key batadv_orig_hash_lock_class_key;/** * batadv_orig_hash_find() - Find and return originator from orig_hash * @bat_priv: the bat priv with all the mesh interface information -* @data: mac address of the originator +* @addr: mac address of the originator * * Return: orig_node (with increased refcnt), NULL on errors */ struct batadv_orig_node * -batadv_orig_hash_find (struct batadv_priv *bat_priv,const void *data) +batadv_orig_hash_find (struct batadv_priv *bat_priv,const u8 *addr) {struct batadv_hashtable *hash = bat_priv->orig_hash;struct batadv_orig_node *orig_node_tmp =NULL ;
@@ -61,12 +61,12 @@ batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)if (!hash)return NULL ; - index =batadv_choose_orig (data, hash->size); + index =batadv_choose_orig (addr, hash->size); head = &hash->table[index];rcu_read_lock ();hlist_for_each_entry_rcu (orig_node, head, hash_entry) { -if (!batadv_compare_eth (orig_node, data)) +if (!batadv_compare_orig (&orig_node->hash_entry, addr))continue ;if (!kref_get_unless_zero (&orig_node->refcount))
@@ -85,16 +85,17 @@ static void batadv_purge_orig(struct work_struct *work);/** * batadv_compare_orig() - comparing function used in the originator hash table * @node: node in the local table -* @data2: second object to compare the node to +* @data2: mac address of the originator to compare the node to * -* Return: true if they are the same originator +* Return: true if the node is the originator with the given address */ bool batadv_compare_orig (const struct hlist_node *node,const void *data2) { - const void *data1 = container_of(node, struct batadv_orig_node, - hash_entry); +const struct batadv_orig_node *orig_node; -return batadv_compare_eth (data1, data2); + orig_node = container_of(node, struct batadv_orig_node, hash_entry); + +return batadv_compare_eth (orig_node->orig, data2); }/**
net/batman-adv/originator.h +1 -1
@@ -83,7 +83,7 @@ static inline u32 batadv_choose_orig(const void *data, u32 size) }struct batadv_orig_node * -batadv_orig_hash_find (struct batadv_priv *bat_priv,const void *data); +batadv_orig_hash_find (struct batadv_priv *bat_priv,const u8 *addr);/** * batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
Signature · 1 line
2.47.3
Sven Eckelmann
maintainerRe: [PATCH batadv v2 2/6] batman-adv: bla: use dedicated key type for backbone gateway hash
In reply to a message not in the archiveOn Wednesday, 14 October 2026 09:57:28 CEST [email protected] wrote:
[...]
Quoted 16 lines
[Severity: High]
Does this in-place key modification corrupt the hash table?As seen in
batadv_choose_backbone_gw()above, key.orig is used to determine the hash bucket. If the key is updated here without removing and re-inserting the object into the correct bucket, the element becomes orphaned in the wrong bucket. Future fast-path lookups for the new MAC address would then search a different bucket and potentially create a duplicate entry.Additionally, since
batadv_bla_periodic_work()iterates over all hash buckets instead of doing a key lookup, could it find both the newly created entry and the old orphaned entry? It appears this would repeatedly refresh lasttime for both entries. This would preventbatadv_bla_purge_backbone_gw()from ever cleaning up the orphaned entry, leading to a memory leak and redundant loop-detect network broadcasts.
Quoted 10 lines
Finally, is there a data race here? The 6-byte MAC address modification via
ether_addr_copy()is not atomic and runs underrcu_read_lock()rather than the bucket's spinlock. Can concurrent lockless RCU readers observe torn reads when performing equality checks?Quoted 3 lines
net/batman-adv/bridge_loop_avoidance.c +0
-0/* send an announce frame so others will ask for our * claims and update their tables. */
Not introduced in this patch. It is known and waiting for a fix by Simon:
https://www.open-mesh.org/issues/452
Regards,
Sven