Skip to content

Commit 596b073

Browse files
authored
Merge pull request #5707 from garlick/ipaddr_interface
broker: add FLUX_IPADDR_INTERFACE to select broker network interface
2 parents 556d42c + fa4fdf5 commit 596b073

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

doc/man7/flux-environment.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ affect the broker's PMI client.
229229
version 4 addresses. Setting this variable to any value in the broker's
230230
environment causes it to prefer version 6 addresses.
231231

232+
.. envvar:: FLUX_IPADDR_HOSTNAME
233+
234+
Force PMI bootstrap to assign the broker an address associated with a
235+
particular network interface, like ``eth0``.
236+
232237

233238
CUSTOM OUTPUT FORMATS
234239
=====================

src/broker/boot_pmi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ static int format_bind_uri (char *buf, int bufsz, attr_t *attrs, int rank)
151151

152152
if (ipaddr_getprimary (ipaddr, sizeof (ipaddr),
153153
error, sizeof (error)) < 0) {
154-
log_err ("%s", error);
154+
log_msg ("%s", error);
155155
return -1;
156156
}
157157
if (snprintf (buf, bufsz, "tcp://%s:*", ipaddr) >= bufsz)

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

t/t0001-basic.t

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,17 @@ test_expect_success 'tbon.endpoint uses tcp:// if tbon.prefertcp is set' '
429429
flux getattr tbon.endpoint >endpoint2.out &&
430430
grep "^tcp" endpoint2.out
431431
'
432+
test_expect_success 'FLUX_IPADDR_INTERFACE=lo works' '
433+
FLUX_IPADDR_INTERFACE=lo flux start \
434+
${ARGS} -s2 -o,-Stbon.prefertcp=1 \
435+
flux getattr tbon.endpoint >endpoint3.out &&
436+
grep "127.0.0.1" endpoint3.out
437+
'
438+
test_expect_success 'FLUX_IPADDR_INTERFACE=badiface fails' '
439+
(export FLUX_IPADDR_INTERFACE=badiface; \
440+
test_expect_code 137 flux start \
441+
${ARGS} --test-exit-timeout=1s -s2 -o,-Stbon.prefertcp=1 true)
442+
'
432443
test_expect_success 'tbon.endpoint cannot be set' '
433444
test_must_fail flux start ${ARGS} -s2 \
434445
-o,--setattr=tbon.endpoint=ipc:///tmp/customflux /bin/true

0 commit comments

Comments
 (0)