From a8c14745625c0862fd77b6307ea00c2f67c9e5e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= Date: Tue, 2 Mar 2021 21:38:02 +0000 Subject: [PATCH] Obtain MTU of interface only when it would be used MTU were obtained early during iface_allowed check. But often it returned from the function without ever using it. Because calls to kernel might be costy, move fetching it only when it would be assigned. --- src/network.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/network.c b/src/network.c index 77eb6ad..3b4ac97 100644 --- a/src/network.c +++ b/src/network.c @@ -232,7 +232,7 @@ static int iface_allowed(struct iface_param *param, int if_index, char *label, union mysockaddr *addr, struct in_addr netmask, int prefixlen, int iface_flags) { struct irec *iface; - int mtu = 0, loopback; + int loopback; struct ifreq ifr; int tftp_ok = !!option_bool(OPT_TFTP); int dhcp_ok = 1; @@ -253,9 +253,6 @@ static int iface_allowed(struct iface_param *param, int if_index, char *label, if (loopback) dhcp_ok = 0; - if (ioctl(param->fd, SIOCGIFMTU, &ifr) != -1) - mtu = ifr.ifr_mtu; - if (!label) label = ifr.ifr_name; else @@ -458,6 +455,11 @@ static int iface_allowed(struct iface_param *param, int if_index, char *label, /* add to list */ if ((iface = whine_malloc(sizeof(struct irec)))) { + int mtu = 0; + + if (ioctl(param->fd, SIOCGIFMTU, &ifr) != -1) + mtu = ifr.ifr_mtu; + iface->addr = *addr; iface->netmask = netmask; iface->tftp_ok = tftp_ok;