Skip to contentSkip to conversation

alfred: Add support for multiple interfaces per master

PATCH In review Sven Eckelmann 2 participants 3 messages Nov 12, 2014 – Nov 13, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 alfred: Add support for multiple interfaces per master

It may required in some situations that all alfred master servers are potentially connected to the same backbone network but cannot see each other through the mesh. The data may still be exchanged and thus a master instance must listen for requests on the mesh and synchronize data over the backbone network.

The limitation that data can only be synchronized over one master hop and not more still holds true. Also alfred servers in slave mode are not allowed to operate on multiple interfaces. This is especially important when switching from master to slave mode because this can fail now when multiple interfaces are configured.

---
 alfred.h     |  40 +++++++++----
 client.c     |  58 ++++++++++++++-----
 main.c       |  12 ++--
 man/alfred.8 |   4 +-
 netsock.c    | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 packet.h     |   4 +-
 recv.c       |  16 +++---
 send.c       |  65 ++++++++++++---------
 server.c     |  97 ++++++++++++++++---------------
 unix_sock.c  |  38 ++++++++-----
 10 files changed, 377 insertions(+), 140 deletions(-)
alfred.h +29 -11
index c83f7f0..cf94af4 100644
@@ -27,6 +27,8 @@
 #include <netinet/in.h>
 #include <stdint.h>
 #include <time.h>
+#include <sys/select.h>
+#include <sys/types.h>
 #include "list.h"
 #include "packet.h"
 
@@ -89,19 +91,27 @@ enum clientmode {
 	CLIENT_CHANGE_INTERFACE,
 };
 
-struct globals {
+struct interface {
 	struct ether_addr hwaddr;
 	struct in6_addr address;
 	uint32_t scope_id;
-	struct server *best_server;	/* NULL if we are a server ourselves */
 	char *interface;
+	int netsock;
+
+	struct list_head list;
+};
+
+struct globals {
+	struct list_head interfaces;
+
+	char *change_interface;
+	struct server *best_server;	/* NULL if we are a server ourselves */
 	const char *mesh_iface;
 	enum opmode opmode;
 	enum clientmode clientmode;
 	int clientmode_arg;
 	int clientmode_version;
 
-	int netsock;
 	int unix_sock;
 	const char *unix_path;
 
@@ -128,7 +138,7 @@ int alfred_client_set_data(struct globals *globals);
 int alfred_client_modeswitch(struct globals *globals);
 int alfred_client_change_interface(struct globals *globals);
 /* recv.c */
-int recv_alfred_packet(struct globals *globals);
+int recv_alfred_packet(struct globals *globals, struct interface *interface);
 struct transaction_head *
 transaction_add(struct globals *globals, struct ether_addr mac, uint16_t id);
 struct transaction_head *
@@ -137,14 +147,14 @@ transaction_clean_hash(struct globals *globals,
 struct transaction_head *transaction_clean(struct globals *globals,
 					   struct transaction_head *head);
 /* send.c */
-int push_data(struct globals *globals, struct in6_addr *destination,
-	      enum data_source max_source_level, int type_filter,
-	      uint16_t tx_id);
+int push_data(struct globals *globals, struct interface *interface,
+	      struct in6_addr *destination, enum data_source max_source_level,
+	      int type_filter, uint16_t tx_id);
 int announce_master(struct globals *globals);
 int push_local_data(struct globals *globals);
 int sync_data(struct globals *globals);
-ssize_t send_alfred_packet(struct globals *globals, const struct in6_addr *dest,
-			   void *buf, int length);
+ssize_t send_alfred_packet(struct interface *interface,
+			   const struct in6_addr *dest, void *buf, int length);
 /* unix_sock.c */
 int unix_sock_read(struct globals *globals);
 int unix_sock_open_daemon(struct globals *globals);
@@ -155,8 +165,16 @@ int unix_sock_req_data_finish(struct globals *globals,
 /* vis.c */
 int vis_update_data(struct globals *globals);
 /* netsock.c */
-int netsock_open(struct globals *globals);
-int netsock_close(int sock);
+int netsock_open_all(struct globals *globals);
+void netsock_close_all(struct globals *globals);
+int netsock_set_interfaces(struct globals *globals, char *interfaces);
+struct interface *netsock_first_interface(struct globals *globals);
+void netsock_reopen(struct globals *globals);
+int netsock_prepare_select(struct globals *globals, fd_set *fds, int maxsock);
+void netsock_check_error(struct globals *globals, fd_set *errfds);
+int netsock_receive_packet(struct globals *globals, fd_set *fds);
+int netsock_own_address(const struct globals *globals,
+			const struct in6_addr *address);
 /* util.c */
 int time_diff(struct timespec *tv1, struct timespec *tv2,
 	      struct timespec *tvdiff);
client.c +44 -14

92 lines not on this page · raw message

main.c +7 -5
index 5a4f1e6..7eb8baa 100644
@@ -26,6 +26,7 @@
 #include <string.h>
 #include "alfred.h"
 #include "packet.h"
+#include "list.h"
 
 static struct globals alfred_globals;
 
@@ -40,10 +41,10 @@ static void alfred_usage(void)
 	printf("  -V, --req-version                   specify the data version set for -s\n");
 	printf("  -M, --modeswitch master             switch daemon to mode master\n");
 	printf("                   slave              switch daemon to mode slave\n");
-	printf("  -I, --change-interface [interface]  change to the specified interface\n");
+	printf("  -I, --change-interface [interface]  change to the specified interface(s)\n");
 	printf("\n");
 	printf("server mode options:\n");
-	printf("  -i, --interface                     specify the interface to listen on\n");
+	printf("  -i, --interface                     specify the interface (or comma separated list of interfaes) to listen on\n");
 	printf("  -b                                  specify the batman-adv interface\n");
 	printf("                                      configured on the system (default: bat0)\n");
 	printf("                                      use 'none' to disable the batman-adv\n");
@@ -80,9 +81,10 @@ static struct globals *alfred_init(int argc, char *argv[])
 	globals = &alfred_globals;
 	memset(globals, 0, sizeof(*globals));
 
+	INIT_LIST_HEAD(&globals->interfaces);
+	globals->change_interface = NULL;
 	globals->opmode = OPMODE_SLAVE;
 	globals->clientmode = CLIENT_NONE;
-	globals->interface = NULL;
 	globals->best_server = NULL;
 	globals->clientmode_version = 0;
 	globals->mesh_iface = "bat0";
@@ -116,7 +118,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 			globals->opmode = OPMODE_MASTER;
 			break;
 		case 'i':
-			globals->interface = strdup(optarg);
+			netsock_set_interfaces(globals, optarg);
 			break;
 		case 'b':
 			globals->mesh_iface = strdup(optarg);
@@ -142,7 +144,7 @@ static struct globals *alfred_init(int argc, char *argv[])
 			break;
 		case 'I':
 			globals->clientmode = CLIENT_CHANGE_INTERFACE;
-			globals->interface = strdup(optarg);
+			globals->change_interface = strdup(optarg);
 			break;
 		case 'u':
 			globals->unix_path = optarg;
man/alfred.8 +2 -2
index 35dee45..0449189 100644
@@ -82,12 +82,12 @@ to 0 ('\fB\-V\fP 0').
 .fi
 .TP
 \fB\-I\fP, \fB\-\-change\-interface\fP \fIinterface\fP
-Change the alfred server to use the new \fBinterface\fP
+Change the alfred server to use the new \fBinterface\fP(s)
 .
 .SH SERVER OPTIONS
 .TP
 \fB\-i\fP, \fB\-\-interface\fP \fiface\fP
-Specify the interface to listen on
+Specify the interface (or comma separated list of interfaes) to listen on
 .TP
 \fB\-b\fP \fIbatmanif\fP
 Specify the batman-adv interface configured on the system (default: bat0).
netsock.c +172 -11

241 lines not on this page · raw message

packet.h +2 -2
index dc06d65..87ad263 100644
@@ -149,13 +149,13 @@ struct alfred_modeswitch_v0 {
 /**
  * struct alfred_change_interface_v0 - Request to change the interface
  * @header: TLV header describing the complete packet
- * @iface: interface name to be changed to
+ * @ifaces: interface list (comma separated) to be changed to
  *
  * Sent to the daemon by client
  */
 struct alfred_change_interface_v0 {
 	struct alfred_tlv header;
-	char iface[IFNAMSIZ];
+	char ifaces[IFNAMSIZ * 16];
 } __packed;
 
 
recv.c +8 -8
index 2948547..6a257d7 100644
@@ -287,6 +287,7 @@ process_alfred_announce_master(struct globals *globals,
 
 
 static int process_alfred_request(struct globals *globals,
+				  struct interface *interface,
 				  struct in6_addr *source,
 				  struct alfred_request_v0 *request)
 {
@@ -300,8 +301,8 @@ static int process_alfred_request(struct globals *globals,
 	if (len != (sizeof(*request) - sizeof(request->header)))
 		return -1;
 
-	push_data(globals, source, SOURCE_SYNCED, request->requested_type,
-		  request->tx_id);
+	push_data(globals, interface, source, SOURCE_SYNCED,
+		  request->requested_type, request->tx_id);
 
 	return 0;
 }
@@ -370,7 +371,7 @@ static int process_alfred_status_txend(struct globals *globals,
 }
 
 
-int recv_alfred_packet(struct globals *globals)
+int recv_alfred_packet(struct globals *globals, struct interface *interface)
 {
 	uint8_t buf[MAX_PAYLOAD];
 	ssize_t length;
@@ -378,11 +379,11 @@ int recv_alfred_packet(struct globals *globals)
 	struct sockaddr_in6 source;
 	socklen_t sourcelen;
 
-	if (globals->netsock < 0)
+	if (interface->netsock < 0)
 		return -1;
 
 	sourcelen = sizeof(source);
-	length = recvfrom(globals->netsock, buf, sizeof(buf), 0,
+	length = recvfrom(interface->netsock, buf, sizeof(buf), 0,
 			  (struct sockaddr *)&source, &sourcelen);
 	if (length <= 0) {
 		perror("read from network socket failed");
@@ -396,8 +397,7 @@ int recv_alfred_packet(struct globals *globals)
 		return -1;
 
 	/* drop packets from ourselves */
-	if (0 == memcmp(&source.sin6_addr, &globals->address,
-			sizeof(source.sin6_addr)))
+	if (netsock_own_address(globals, &source.sin6_addr))
 		return -1;
 
 	/* drop truncated packets */
@@ -418,7 +418,7 @@ int recv_alfred_packet(struct globals *globals)
 					       (struct alfred_announce_master_v0 *)packet);
 		break;
 	case ALFRED_REQUEST:
-		process_alfred_request(globals, &source.sin6_addr,
+		process_alfred_request(globals, interface, &source.sin6_addr,
 				       (struct alfred_request_v0 *)packet);
 		break;
 	case ALFRED_STATUS_TXEND:
send.c +38 -27

139 lines not on this page · raw message

server.c +52 -45

192 lines not on this page · raw message

unix_sock.c +23 -15
index 516c0a5..8a99327 100644
@@ -31,6 +31,7 @@
 #include <sys/un.h>
 #include <time.h>
 #include <unistd.h>
+#include <errno.h>
 #include "alfred.h"
 #include "hash.h"
 #include "packet.h"
@@ -98,6 +99,11 @@ static int unix_sock_add_data(struct globals *globals,
 	struct alfred_data *data;
 	struct dataset *dataset;
 	int len, data_len, ret = -1;
+	struct interface *interface;
+
+	interface = netsock_first_interface(globals);
+	if (!interface)
+		goto err;
 
 	len = ntohs(push->header.length);
 
@@ -112,7 +118,7 @@ static int unix_sock_add_data(struct globals *globals,
 
 	data = push->data;
 	data_len = ntohs(data->header.length);
-	memcpy(data->source, &globals->hwaddr, sizeof(globals->hwaddr));
+	memcpy(data->source, &interface->hwaddr, sizeof(interface->hwaddr));
 
 	if ((int)(data_len + sizeof(*data)) > len)
 		goto err;
@@ -204,6 +210,7 @@ static int unix_sock_req_data(struct globals *globals,
 	int len;
 	uint16_t id;
 	struct transaction_head *head = NULL;
+	struct interface *interface;
 
 	len = ntohs(request->header.length);
 
@@ -212,8 +219,10 @@ static int unix_sock_req_data(struct globals *globals,
 
 	id = ntohs(request->tx_id);
 
+	interface = netsock_first_interface(globals);
+
 	/* no server to send the request to, only give back what we have now. */
-	if (!globals->best_server)
+	if (!globals->best_server || !interface)
 		return unix_sock_req_data_reply(globals, client_sock, id,
 						request->requested_type);
 
@@ -229,7 +238,7 @@ static int unix_sock_req_data(struct globals *globals,
 	head->client_socket = client_sock;
 	head->requested_type = request->requested_type;
 
-	send_alfred_packet(globals, &globals->best_server->address,
+	send_alfred_packet(interface, &globals->best_server->address,
 			   request, sizeof(*request));
 
 	return 0;
@@ -283,6 +292,9 @@ static int unix_sock_modesw(struct globals *globals,
 
 	switch (modeswitch->mode) {
 	case ALFRED_MODESWITCH_SLAVE:
+		if (!list_is_singular(&globals->interfaces))
+			goto err;
+
 		globals->opmode = OPMODE_SLAVE;
 		break;
 	case ALFRED_MODESWITCH_MASTER:
@@ -304,25 +316,21 @@ unix_sock_change_iface(struct globals *globals,
 		       int client_sock)
 {
 	int len, ret = -1;
-	char *iface;
 
 	len = ntohs(change_iface->header.length);
 
 	if (len < (int)(sizeof(*change_iface) - sizeof(change_iface->header)))
 		goto err;
 
-	iface = malloc(IFNAMSIZ + 1);
-	if (!iface)
-		goto err;
+	if (globals->opmode == OPMODE_SLAVE) {
+		if (strstr(change_iface->ifaces, ",") != NULL) {
+			ret = -EINVAL;
+			fprintf(stderr, "Tried to set multiple interfaces in slave mode\n");
+			goto err;
+		}
+	}
 
-	memcpy(iface, change_iface->iface, IFNAMSIZ);
-	iface[IFNAMSIZ] = 0;
-
-	netsock_close(globals->netsock);
-
-	free(globals->interface);
-	globals->interface = iface;
-	netsock_open(globals);
+	netsock_set_interfaces(globals, change_iface->ifaces);
 
 	ret = 0;
 err:
Signature · 1 line
2.1.3
#1 lore raw

PATCH 2/2 alfred: Synchronize data only over correct interfaces

The server announcements are received over a specific interface. It is enough to synchronize data through the interface where an announcement was received. It only increases the overhead by alfred when sending data over an interface to an server without knowing that he is also reachable over it.

---
 alfred.h  |  3 ++-
 netsock.c | 47 ++++++++++++++++++++++++++++++++++++++++++-
 recv.c    |  8 +++++---
 send.c    |  2 +-
 server.c  | 69 +++++++++++++++++++++------------------------------------------
 5 files changed, 77 insertions(+), 52 deletions(-)
alfred.h +2 -1
index cf94af4..05ac870 100644
@@ -98,6 +98,8 @@ struct interface {
 	char *interface;
 	int netsock;
 
+	struct hashtable_t *server_hash;
+
 	struct list_head list;
 };
 
@@ -117,7 +119,6 @@ struct globals {
 
 	struct timespec if_check;
 
-	struct hashtable_t *server_hash;
 	struct hashtable_t *data_hash;
 	struct hashtable_t *transaction_hash;
 };
netsock.c +46 -1

87 lines not on this page · raw message

recv.c +5 -3
index 6a257d7..90db0b3 100644
@@ -231,6 +231,7 @@ err:
 
 static int
 process_alfred_announce_master(struct globals *globals,
+			       struct interface *interface,
 			       struct in6_addr *source,
 			       struct alfred_announce_master_v0 *announce)
 {
@@ -252,7 +253,7 @@ process_alfred_announce_master(struct globals *globals,
 	if (len != (sizeof(*announce) - sizeof(announce->header)))
 		return -1;
 
-	server = hash_find(globals->server_hash, &mac);
+	server = hash_find(interface->server_hash, &mac);
 	if (!server) {
 		server = malloc(sizeof(*server));
 		if (!server)
@@ -261,7 +262,7 @@ process_alfred_announce_master(struct globals *globals,
 		memcpy(&server->hwaddr, &mac, ETH_ALEN);
 		memcpy(&server->address, source, sizeof(*source));
 
-		if (hash_add(globals->server_hash, server)) {
+		if (hash_add(interface->server_hash, server)) {
 			free(server);
 			return -1;
 		}
@@ -414,7 +415,8 @@ int recv_alfred_packet(struct globals *globals, struct interface *interface)
 					 (struct alfred_push_data_v0 *)packet);
 		break;
 	case ALFRED_ANNOUNCE_MASTER:
-		process_alfred_announce_master(globals, &source.sin6_addr,
+		process_alfred_announce_master(globals, interface,
+					       &source.sin6_addr,
 					       (struct alfred_announce_master_v0 *)packet);
 		break;
 	case ALFRED_REQUEST:
send.c +1 -1
index c1379bd..19cc867 100644
@@ -134,7 +134,7 @@ int sync_data(struct globals *globals)
 
 	/* send local data and data from our clients to (all) other servers */
 	list_for_each_entry(interface, &globals->interfaces, list) {
-		while (NULL != (hashit = hash_iterate(globals->server_hash,
+		while (NULL != (hashit = hash_iterate(interface->server_hash,
 						      hashit))) {
 			struct server *server = hashit->bucket->data;
 
server.c +23 -46

106 lines not on this page · raw message

Signature · 1 line
2.1.3
#2 lore raw
In reply to Sven Eckelmann
Quoted 6 lines · On Wednesday 12 November 2014 14:54:29 Sven Eckelmann wrote:

It may required in some situations that all alfred master servers are potentially connected to the same backbone network but cannot see each other through the mesh. The data may still be exchanged and thus a master instance must listen for requests on the mesh and synchronize data over the backbone network.

Quoted 7 lines

The limitation that data can only be synchronized over one master hop and not more still holds true. Also alfred servers in slave mode are not allowed to operate on multiple interfaces. This is especially important when switching from master to slave mode because this can fail now when multiple interfaces are configured.

Series applied in a822b86 and eb8b960.

Thank you!!
Simon