[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Linux-cluster] [PATCH] Add network interface select option for fence_xvmd



Hello,


# I sent this before but it looks disappered somewhere so that resend it
# again. Excuse me if you received the same mail twice.

It should be useful that fence_xvmd listen on a certain network
interface which manually specified under some conditions such as a
system has multiple network interfaces and the one to default route is
not prefered choice, I think.

The following patch adds the option "-I <interface_name>" to select
network interface fence_xvmd to listen on.

- satoru


 fence/agents/xvm/fence_xvmd.c |    8 ++++----
 fence/agents/xvm/mcast.c      |   21 ++++++++++++++++++---
 fence/agents/xvm/mcast.h      |    4 ++--
 fence/agents/xvm/options.c    |   13 +++++++++++++
 fence/agents/xvm/options.h    |    1 +
 fence/man/fence_xvmd.8        |    3 +++
 6 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/fence/agents/xvm/fence_xvmd.c b/fence/agents/xvm/fence_xvmd.c
index 888f24b..1dc5eba 100644
--- a/fence/agents/xvm/fence_xvmd.c
+++ b/fence/agents/xvm/fence_xvmd.c
@@ -921,7 +921,7 @@ main(int argc, char **argv)
 	unsigned int logmode = 0;
 	char key[MAX_KEY_LEN];
 	int key_len = 0, x;
-	char *my_options = "dfi:a:p:C:U:c:k:u?hLXV";
+	char *my_options = "dfi:a:I:p:C:U:c:k:u?hLXV";
 	cman_handle_t ch = NULL;
 	void *h = NULL;
 
@@ -1031,9 +1031,9 @@ main(int argc, char **argv)
 	}
 
 	if (args.family == PF_INET)
-		mc_sock = ipv4_recv_sk(args.addr, args.port);
+		mc_sock = ipv4_recv_sk(args.addr, args.port, args.ifindex);
 	else
-		mc_sock = ipv6_recv_sk(args.addr, args.port);
+		mc_sock = ipv6_recv_sk(args.addr, args.port, args.ifindex);
 	if (mc_sock < 0) {
 		log_printf(LOG_ERR,
 			   "Could not set up multicast listen socket\n");
@@ -1049,5 +1049,5 @@ main(int argc, char **argv)
 
 	//malloc_dump_table();
 
-	return 0;
+	exit(errno);
 }
diff --git a/fence/agents/xvm/mcast.c b/fence/agents/xvm/mcast.c
index db46328..001e3ac 100644
--- a/fence/agents/xvm/mcast.c
+++ b/fence/agents/xvm/mcast.c
@@ -31,11 +31,12 @@ LOGSYS_DECLARE_SUBSYS ("XVM", SYSLOGLEVEL);
   Sets up a multicast receive socket
  */
 int
-ipv4_recv_sk(char *addr, int port)
+ipv4_recv_sk(char *addr, int port, unsigned int ifindex)
 {
 	int sock;
 	struct ip_mreq mreq;
 	struct sockaddr_in sin;
+	struct ifreq ifreq;
 
 	/* Store multicast address */
 	if (inet_pton(PF_INET, addr,
@@ -74,7 +75,20 @@ ipv4_recv_sk(char *addr, int port)
 	 * Join multicast group
 	 */
 	/* mreq.imr_multiaddr.s_addr is set above */
-	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+	if (ifindex > 0 && if_indextoname(ifindex, ifreq.ifr_name) != NULL) {
+		ifreq.ifr_addr.sa_family = AF_INET;
+		if (ioctl(sock, SIOCGIFADDR, &ifreq) < 0) {
+			printf("Failed to get address of the interface %d\n",
+				ifindex);
+			mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+		} else {
+			memcpy(&mreq.imr_interface,
+				&((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
+				sizeof(struct in_addr));
+		}
+	} else {
+		mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+	}
 	dbg_printf(4, "Joining multicast group\n");
 	if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
 		       &mreq, sizeof(mreq)) == -1) {
@@ -184,7 +198,7 @@ ipv4_send_sk(char *send_addr, char *addr, int port, struct sockaddr *tgt,
   Sets up a multicast receive (ipv6) socket
  */
 int
-ipv6_recv_sk(char *addr, int port)
+ipv6_recv_sk(char *addr, int port, unsigned int ifindex)
 {
 	int sock, val;
 	struct ipv6_mreq mreq;
@@ -203,6 +217,7 @@ ipv6_recv_sk(char *addr, int port)
 	memcpy(&mreq.ipv6mr_multiaddr, &sin.sin6_addr,
 	       sizeof(struct in6_addr));
 
+	mreq.ipv6mr_interface = (ifindex > 0) ? ifindex : 0;
 
 	/********************************
 	 * SET UP MULTICAST RECV SOCKET *
diff --git a/fence/agents/xvm/mcast.h b/fence/agents/xvm/mcast.h
index 5113f04..08fd6de 100644
--- a/fence/agents/xvm/mcast.h
+++ b/fence/agents/xvm/mcast.h
@@ -4,11 +4,11 @@
 #define IPV4_MCAST_DEFAULT "225.0.0.12"
 #define IPV6_MCAST_DEFAULT "ff05::3:1"
 
-int ipv4_recv_sk(char *addr, int port);
+int ipv4_recv_sk(char *addr, int port, unsigned int ifindex);
 int ipv4_send_sk(char *src_addr, char *addr, int port,
 		 struct sockaddr *src, socklen_t slen,
 		 int ttl);
-int ipv6_recv_sk(char *addr, int port);
+int ipv6_recv_sk(char *addr, int port, unsigned int ifindex);
 int ipv6_send_sk(char *src_addr, char *addr, int port,
 		 struct sockaddr *src, socklen_t slen,
 		 int ttl);
diff --git a/fence/agents/xvm/options.c b/fence/agents/xvm/options.c
index 969ca8d..519f57e 100644
--- a/fence/agents/xvm/options.c
+++ b/fence/agents/xvm/options.c
@@ -82,6 +82,13 @@ assign_address(fence_xvm_args_t *args, struct arg_info *arg, char *value)
 
 
 static inline void
+assign_interface(fence_xvm_args_t *args, struct arg_info *arg, char *value)
+{
+	args->ifindex = if_nametoindex(value);
+}
+
+
+static inline void
 assign_ttl(fence_xvm_args_t *args, struct arg_info *arg, char *value)
 {
 	int ttl;
@@ -299,6 +306,10 @@ static struct arg_info _arg_info[] = {
 	  "Multicast address (default=225.0.0.12 / ff02::3:1)",
 	  assign_address },
 
+	{ 'I', "-I <interface>", NULL,
+	  "Network interface to listen on (default=auto; kernel selects)",
+	  assign_interface },
+
 	{ 'T', "-T <ttl>", "multicast_ttl",
 	  "Multicast time-to-live (in hops; default=2)",
 	  assign_ttl },
@@ -422,6 +433,7 @@ args_init(fence_xvm_args_t *args)
 	args->flags = 0;
 	args->debug = 0;
 	args->ttl = DEFAULT_TTL;
+	args->ifindex = 0;
 }
 
 
@@ -439,6 +451,7 @@ args_print(fence_xvm_args_t *args)
 {
 	dbg_printf(1, "-- args @ %p --\n", args);
 	_pr_str(args->addr);
+	_pr_int(args->ifindex);
 	_pr_str(args->domain);
 	_pr_str(args->key_file);
 	_pr_int(args->op);
diff --git a/fence/agents/xvm/options.h b/fence/agents/xvm/options.h
index 7a2dcca..8720366 100644
--- a/fence/agents/xvm/options.h
+++ b/fence/agents/xvm/options.h
@@ -29,6 +29,7 @@ typedef struct {
 	arg_flags_t flags;
 	int debug;
 	int ttl;
+	unsigned int ifindex;
 } fence_xvm_args_t;
 
 /* Private structure for commandline / stdin fencing args */
diff --git a/fence/man/fence_xvmd.8 b/fence/man/fence_xvmd.8
index 5a47211..05d4720 100644
--- a/fence/man/fence_xvmd.8
+++ b/fence/man/fence_xvmd.8
@@ -36,6 +36,9 @@ IP family to use (auto, ipv4, or ipv6; default = auto)
 Multicast address to listen on (default=225.0.0.12 for ipv4, ff02::3:1
 for ipv6)
 .TP
+\fB-I\fP \fIinterface\fP
+Network interface to use; e.g. eth0 (default: one[s] kernel choosed)
+.TP
 \fB-p\fP \fIport\fP
 Port to use (default=1229)
 .TP


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]