Skip to content

Commit f00476a

Browse files
committed
broker: add FLUX_IPADDR_INTERFACE environment var
Problem: there is no way to force a flux instance to use a specific network interface for overlay communication. Add an environment variable: FLUX_IPADDR_INTERFACE. If set to an interface name, that will select the preferred network.
1 parent 556d42c commit f00476a

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/common/libutil/ipaddr.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ static struct ifaddrs *find_ifaddr (struct ifaddrs *ifaddr,
100100
return ifa;
101101
}
102102

103-
static int getprimary_ifaddr (char *buf, int len, int prefer_family,
104-
char *errstr, int errstrsz)
103+
static int getnamed_ifaddr (char *buf,
104+
int len,
105+
const char *name,
106+
int prefer_family,
107+
char *errstr,
108+
int errstrsz)
105109
{
106110
struct ifaddrs *ifaddr;
107111
struct ifaddrs *ifa;
108-
char name[64];
109112
int error;
110113

111-
if (getprimary_iface4 (name, sizeof (name), errstr, errstrsz) < 0)
112-
return -1;
113114
if (getifaddrs (&ifaddr) < 0) {
114115
esprintf (errstr, errstrsz, "getifaddrs: %s", strerror (errno));
115116
return -1;
@@ -141,6 +142,18 @@ static int getprimary_ifaddr (char *buf, int len, int prefer_family,
141142
return 0;
142143
}
143144

145+
static int getprimary_ifaddr (char *buf,
146+
int len,
147+
int prefer_family,
148+
char *errstr,
149+
int errstrsz)
150+
{
151+
char name[64];
152+
if (getprimary_iface4 (name, sizeof (name), errstr, errstrsz) < 0)
153+
return -1;
154+
return getnamed_ifaddr (buf, len, name, prefer_family, errstr, errstrsz);
155+
}
156+
144157
static struct addrinfo *find_addrinfo (struct addrinfo *addrinfo, int family)
145158
{
146159
struct addrinfo *ai;
@@ -205,12 +218,28 @@ int ipaddr_getprimary (char *buf, int len,
205218
char *errstr, int errstrsz)
206219
{
207220
int prefer_family = getenv ("FLUX_IPADDR_V6") ? AF_INET6 : AF_INET;
221+
const char *iface_name;
208222
int rc = -1;
209223

210-
if (getenv ("FLUX_IPADDR_HOSTNAME") == NULL)
211-
rc = getprimary_ifaddr (buf, len, prefer_family, errstr, errstrsz);
212-
if (rc < 0)
213-
rc = getprimary_hostaddr (buf, len, prefer_family, errstr, errstrsz);
224+
if ((iface_name = getenv ("FLUX_IPADDR_INTERFACE"))) {
225+
rc = getnamed_ifaddr (buf,
226+
len,
227+
iface_name,
228+
prefer_family,
229+
errstr,
230+
errstrsz);
231+
}
232+
else {
233+
if (getenv ("FLUX_IPADDR_HOSTNAME") == NULL)
234+
rc = getprimary_ifaddr (buf, len, prefer_family, errstr, errstrsz);
235+
if (rc < 0) {
236+
rc = getprimary_hostaddr (buf,
237+
len,
238+
prefer_family,
239+
errstr,
240+
errstrsz);
241+
}
242+
}
214243
return rc;
215244
}
216245

src/common/libutil/ipaddr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* 1. Find the interface associated with the default route, then
1919
* look up address of that interface.
2020
* 2. Look up address associated with the hostname
21+
* 3. Look up address associated with a specific interface.
2122
*
2223
* Main use case: determine bind address for a PMI-bootstrapped flux broker.
2324
*
@@ -29,6 +30,8 @@
2930
* FLUX_IPADDR_HOSTNAME
3031
* if set, only method 2 is tried above
3132
* if unset, first method 1 is tried, then if that fails, method 2 is tried
33+
* FLUX_IPADDR_INTERFACE
34+
* if set, only method 3 is tried above
3235
*
3336
* Return address as a string in buf (up to len bytes, always null terminated)
3437
* Return 0 on success, -1 on error with error message written to errstr

0 commit comments

Comments
 (0)