Speed up interface carrier checking.

This is done by performing one synchronous query for carrier state at
the start of the program; after that, we just monitor the nlsocket for
carrier state changes and update the cached state accordingly.

The benefit is that ifchd needs to do a lot less work and this should
reduce the CPU cycle consumption; prior to this commit, the CPU time
ends up being a few CPU-minutes per month.
This commit is contained in:
Nicholas J. Kain 2021-04-25 05:26:19 -04:00
parent ad1546ecb0
commit f3766990f9
4 changed files with 8 additions and 3 deletions

View File

@ -224,7 +224,7 @@ static int ifchwrite(const char buf[static 1], size_t count)
return -1; return -1;
} }
bool carrier_isup(void) bool ifchange_carrier_isup(void)
{ {
char buf[256]; char buf[256];
snprintf(buf, sizeof buf, "carrier:;"); snprintf(buf, sizeof buf, "carrier:;");

View File

@ -31,7 +31,7 @@
#include <stdbool.h> #include <stdbool.h>
bool carrier_isup(void); bool ifchange_carrier_isup(void);
int ifchange_bind(struct client_state_t cs[static 1], int ifchange_bind(struct client_state_t cs[static 1],
struct dhcpmsg packet[static 1]); struct dhcpmsg packet[static 1]);
int ifchange_deconfig(struct client_state_t cs[static 1]); int ifchange_deconfig(struct client_state_t cs[static 1]);

View File

@ -110,6 +110,8 @@ int signals_flagged(void)
return SIGNAL_NONE; return SIGNAL_NONE;
} }
bool carrier_isup(void) { return cs.carrier_up; }
void set_client_addr(const char v[static 1]) { cs.clientAddr = inet_addr(v); } void set_client_addr(const char v[static 1]) { cs.clientAddr = inet_addr(v); }
void print_version(void) void print_version(void)
@ -311,6 +313,7 @@ static void do_ndhc_work(void)
if (pfds[0].revents & POLLIN) { if (pfds[0].revents & POLLIN) {
had_event = true; had_event = true;
sev_nl = nl_event_get(&cs); sev_nl = nl_event_get(&cs);
cs.carrier_up = (sev_nl == IFS_UP);
} }
if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) { if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
suicide("nlfd closed unexpectedly"); suicide("nlfd closed unexpectedly");
@ -494,6 +497,7 @@ static void ndhc_main(void) {
memset(chroot_dir, '\0', sizeof chroot_dir); memset(chroot_dir, '\0', sizeof chroot_dir);
nk_set_uidgid(ndhc_uid, ndhc_gid, (const unsigned char *)0, 0); nk_set_uidgid(ndhc_uid, ndhc_gid, (const unsigned char *)0, 0);
cs.carrier_up = ifchange_carrier_isup();
if (!carrier_isup()) { if (!carrier_isup()) {
if (ifchange_deconfig(&cs) < 0) if (ifchange_deconfig(&cs) < 0)
suicide("%s: can't deconfigure interface settings", __func__); suicide("%s: can't deconfigure interface settings", __func__);

View File

@ -61,7 +61,7 @@ struct client_state_t {
enum arp_state server_arp_state, router_arp_state; enum arp_state server_arp_state, router_arp_state;
enum fprint_state fp_state; enum fprint_state fp_state;
bool using_dhcp_bpf, arp_is_defense, check_fingerprint, program_init, bool using_dhcp_bpf, arp_is_defense, check_fingerprint, program_init,
sent_renew_or_rebind; sent_renew_or_rebind, carrier_up;
bool sent_gw_query, sent_first_announce, sent_second_announce; bool sent_gw_query, sent_first_announce, sent_second_announce;
}; };
@ -99,6 +99,7 @@ extern uid_t ndhc_uid;
extern gid_t ndhc_gid; extern gid_t ndhc_gid;
int signals_flagged(void); int signals_flagged(void);
bool carrier_isup(void);
void set_client_addr(const char v[static 1]); void set_client_addr(const char v[static 1]);
void show_usage(void); void show_usage(void);
void signal_exit(int status); void signal_exit(int status);