From f3766990f99ccaacb5d676cb59c9133e56d0ed6d Mon Sep 17 00:00:00 2001 From: "Nicholas J. Kain" Date: Sun, 25 Apr 2021 05:26:19 -0400 Subject: [PATCH] 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. --- src/ifchange.c | 2 +- src/ifchange.h | 2 +- src/ndhc.c | 4 ++++ src/ndhc.h | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ifchange.c b/src/ifchange.c index 900c2dd..7a5e64b 100644 --- a/src/ifchange.c +++ b/src/ifchange.c @@ -224,7 +224,7 @@ static int ifchwrite(const char buf[static 1], size_t count) return -1; } -bool carrier_isup(void) +bool ifchange_carrier_isup(void) { char buf[256]; snprintf(buf, sizeof buf, "carrier:;"); diff --git a/src/ifchange.h b/src/ifchange.h index af93028..d7f9875 100644 --- a/src/ifchange.h +++ b/src/ifchange.h @@ -31,7 +31,7 @@ #include -bool carrier_isup(void); +bool ifchange_carrier_isup(void); int ifchange_bind(struct client_state_t cs[static 1], struct dhcpmsg packet[static 1]); int ifchange_deconfig(struct client_state_t cs[static 1]); diff --git a/src/ndhc.c b/src/ndhc.c index 1be0c32..3c7f967 100644 --- a/src/ndhc.c +++ b/src/ndhc.c @@ -110,6 +110,8 @@ int signals_flagged(void) 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 print_version(void) @@ -311,6 +313,7 @@ static void do_ndhc_work(void) if (pfds[0].revents & POLLIN) { had_event = true; sev_nl = nl_event_get(&cs); + cs.carrier_up = (sev_nl == IFS_UP); } if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) { suicide("nlfd closed unexpectedly"); @@ -494,6 +497,7 @@ static void ndhc_main(void) { memset(chroot_dir, '\0', sizeof chroot_dir); nk_set_uidgid(ndhc_uid, ndhc_gid, (const unsigned char *)0, 0); + cs.carrier_up = ifchange_carrier_isup(); if (!carrier_isup()) { if (ifchange_deconfig(&cs) < 0) suicide("%s: can't deconfigure interface settings", __func__); diff --git a/src/ndhc.h b/src/ndhc.h index f0e968e..3e48c0b 100644 --- a/src/ndhc.h +++ b/src/ndhc.h @@ -61,7 +61,7 @@ struct client_state_t { enum arp_state server_arp_state, router_arp_state; enum fprint_state fp_state; 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; }; @@ -99,6 +99,7 @@ extern uid_t ndhc_uid; extern gid_t ndhc_gid; int signals_flagged(void); +bool carrier_isup(void); void set_client_addr(const char v[static 1]); void show_usage(void); void signal_exit(int status);