Răsfoiți Sursa

totemconfig: refactor nodelist_to_interface func

Move finding of bindaddr in nodelist to generally usable function
totem_config_find_local_addr_in_nodelist and refactor
config_convert_nodelist_to_interface function to use it.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Fabio M. Di Nitto <fdinitto@redhat.com>
Jan Friesse 11 ani în urmă
părinte
comite
63bf09776f
2 a modificat fișierele cu 80 adăugiri și 25 ștergeri
  1. 75 24
      exec/totemconfig.c
  2. 5 1
      exec/totemconfig.h

+ 75 - 24
exec/totemconfig.c

@@ -571,29 +571,41 @@ static void put_nodelist_members_to_config(struct totem_config *totem_config)
 	icmap_iter_finalize(iter);
 }
 
-static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
+/*
+ * Tries to find node (node_pos) in config nodelist which address matches any
+ * local interface. Address can be stored in ring0_addr or if ipaddr_key_prefix is not NULL
+ * key with prefix ipaddr_key is used (there can be multiuple of them)
+ * This function differs  * from find_local_node_in_nodelist because it doesn't need bindnetaddr,
+ * but doesn't work when bind addr is network address (so IP must be exact
+ * match).
+ *
+ * Returns 1 on success (address was found, node_pos is then correctly set) or 0 on failure.
+ */
+int totem_config_find_local_addr_in_nodelist(const char *ipaddr_key_prefix, unsigned int *node_pos)
 {
-	icmap_iter_t iter;
-	const char *iter_key;
-	int res = 0;
-	unsigned int node_pos;
-	char tmp_key[ICMAP_KEYNAME_MAXLEN];
-	char tmp_key2[ICMAP_KEYNAME_MAXLEN];
-	char *node_addr_str;
-	unsigned int ringnumber = 0;
 	struct list_head addrs;
-	struct list_head *list;
 	struct totem_ip_if_address *if_addr;
+	icmap_iter_t iter, iter2;
+	const char *iter_key, *iter_key2;
+	struct list_head *list;
+	const char *ipaddr_key;
+	int ip_version;
 	struct totem_ip_address node_addr;
+	char *node_addr_str;
 	int node_found = 0;
+	int res = 0;
+	char tmp_key[ICMAP_KEYNAME_MAXLEN];
 
 	if (totemip_getifaddrs(&addrs) == -1) {
-		return ;
+		return 0;
 	}
 
+	ip_version = totem_config_get_ip_version();
+
 	iter = icmap_iter_init("nodelist.node.");
+
 	while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
-		res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
+		res = sscanf(iter_key, "nodelist.node.%u.%s", node_pos, tmp_key);
 		if (res != 2) {
 			continue;
 		}
@@ -606,25 +618,50 @@ static void config_convert_nodelist_to_interface(struct totem_config *totem_conf
 			continue ;
 		}
 
-		if (totemip_parse(&node_addr, node_addr_str, totem_config->ip_version) == -1) {
-			free(node_addr_str);
-			continue ;
-		}
 		free(node_addr_str);
 
 		/*
-		 * Try to find node in if_addrs
+		 * ring0_addr found -> let's iterate thru ipaddr_key_prefix
 		 */
-		node_found = 0;
-		for (list = addrs.next; list != &addrs; list = list->next) {
-			if_addr = list_entry(list, struct totem_ip_if_address, list);
+		snprintf(tmp_key, sizeof(tmp_key), "nodelist.node.%u.%s", *node_pos,
+		    (ipaddr_key_prefix != NULL ? ipaddr_key_prefix : "ring0_addr"));
+
+		iter2 = icmap_iter_init(tmp_key);
+		while ((iter_key2 = icmap_iter_next(iter2, NULL, NULL)) != NULL) {
+			/*
+			 * ring0_addr must be exact match, not prefix
+			 */
+			ipaddr_key = (ipaddr_key_prefix != NULL ? iter_key2 : tmp_key);
+			if (icmap_get_string(ipaddr_key, &node_addr_str) != CS_OK) {
+				continue ;
+			}
 
-			if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
-				node_found = 1;
-				break;
+			if (totemip_parse(&node_addr, node_addr_str, ip_version) == -1) {
+				free(node_addr_str);
+				continue ;
+			}
+			free(node_addr_str);
+
+			/*
+			 * Try to match ip with if_addrs
+			 */
+			node_found = 0;
+			for (list = addrs.next; list != &addrs; list = list->next) {
+				if_addr = list_entry(list, struct totem_ip_if_address, list);
+
+				if (totemip_equal(&node_addr, &if_addr->ip_addr)) {
+					node_found = 1;
+					break;
+				}
+			}
+
+			if (node_found) {
+				break ;
 			}
 		}
 
+		icmap_iter_finalize(iter2);
+
 		if (node_found) {
 			break ;
 		}
@@ -633,7 +670,21 @@ static void config_convert_nodelist_to_interface(struct totem_config *totem_conf
 	icmap_iter_finalize(iter);
 	totemip_freeifaddrs(&addrs);
 
-	if (node_found) {
+	return (node_found);
+}
+
+static void config_convert_nodelist_to_interface(struct totem_config *totem_config)
+{
+	int res = 0;
+	unsigned int node_pos;
+	char tmp_key[ICMAP_KEYNAME_MAXLEN];
+	char tmp_key2[ICMAP_KEYNAME_MAXLEN];
+	char *node_addr_str;
+	unsigned int ringnumber = 0;
+	icmap_iter_t iter;
+	const char *iter_key;
+
+	if (totem_config_find_local_addr_in_nodelist(NULL, &node_pos)) {
 		/*
 		 * We found node, so create interface section
 		 */

+ 5 - 1
exec/totemconfig.h

@@ -56,8 +56,12 @@ extern int totem_config_validate (
 	struct totem_config *totem_config,
 	const char **error_string);
 
-int totem_config_keyread (
+extern int totem_config_keyread (
 	struct totem_config *totem_config,
 	const char **error_string);
 
+extern int totem_config_find_local_addr_in_nodelist(
+	const char *ipaddr_key_prefix,
+	unsigned int *node_pos);
+
 #endif /* TOTEMCONFIG_H_DEFINED */