Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tipc: make node number calculation reproducible

The 32-bit node number, aka node hash or node address, is calculated
based on the 128-bit node identity when it is not set explicitly by
the user. In future commits we will need to perform this hash operation
on peer nodes while feeling safe that we obtain the same result.

We do this by interpreting the initial hash as a network byte order
number. Whenever we need to use the number locally on a node
we must therefore translate it to host byte order to obtain an
architecure independent result.

Furthermore, given the context where we use this number, we must not
allow it to be zero unless the node identity also is zero. Hence, in
the rare cases when the xor-ed hash value may end up as zero we replace
it with a fix number, knowing that the code anyway is capable of
handling hash collisions.

Acked-by: Ying Xue <ying.xue@windriver.com>
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Jon Maloy and committed by
Jakub Kicinski
5f75e0a0 60c102ee

+16 -4
+3 -4
net/tipc/addr.c
··· 55 55 void tipc_set_node_id(struct net *net, u8 *id) 56 56 { 57 57 struct tipc_net *tn = tipc_net(net); 58 - u32 *tmp = (u32 *)id; 59 58 60 59 memcpy(tn->node_id, id, NODE_ID_LEN); 61 60 tipc_nodeid2string(tn->node_id_string, id); 62 - tn->trial_addr = tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3]; 63 - pr_info("Own node identity %s, cluster identity %u\n", 61 + tn->trial_addr = hash128to32(id); 62 + pr_info("Node identity %s, cluster identity %u\n", 64 63 tipc_own_id_string(net), tn->net_id); 65 64 } 66 65 ··· 75 76 } 76 77 tn->trial_addr = addr; 77 78 tn->addr_trial_end = jiffies; 78 - pr_info("32-bit node address hash set to %x\n", addr); 79 + pr_info("Node number set to %u\n", addr); 79 80 } 80 81 81 82 char *tipc_nodeid2string(char *str, u8 *id)
+1
net/tipc/addr.h
··· 3 3 * 4 4 * Copyright (c) 2000-2006, 2018, Ericsson AB 5 5 * Copyright (c) 2004-2005, Wind River Systems 6 + * Copyright (c) 2020, Red Hat Inc 6 7 * All rights reserved. 7 8 * 8 9 * Redistribution and use in source and binary forms, with or without
+12
net/tipc/core.h
··· 3 3 * 4 4 * Copyright (c) 2005-2006, 2013-2018 Ericsson AB 5 5 * Copyright (c) 2005-2007, 2010-2013, Wind River Systems 6 + * Copyright (c) 2020, Red Hat Inc 6 7 * All rights reserved. 7 8 * 8 9 * Redistribution and use in source and binary forms, with or without ··· 209 208 static inline u32 tipc_net_hash_mixes(struct net *net, int tn_rand) 210 209 { 211 210 return net_hash_mix(&init_net) ^ net_hash_mix(net) ^ tn_rand; 211 + } 212 + 213 + static inline u32 hash128to32(char *bytes) 214 + { 215 + __be32 *tmp = (__be32 *)bytes; 216 + u32 res; 217 + 218 + res = ntohl(tmp[0] ^ tmp[1] ^ tmp[2] ^ tmp[3]); 219 + if (likely(res)) 220 + return res; 221 + return ntohl(tmp[0] | tmp[1] | tmp[2] | tmp[3]); 212 222 } 213 223 214 224 #ifdef CONFIG_SYSCTL