network: Handle ip route priority

When there's a new default route with higher priority, switch to it.
This commit is contained in:
Anthony PERARD 2021-06-02 22:15:34 +01:00
parent ce97df34e6
commit efaac20d82
2 changed files with 17 additions and 10 deletions

View File

@ -72,6 +72,7 @@ class Network : public ALabel {
int32_t signal_strength_dbm_; int32_t signal_strength_dbm_;
uint8_t signal_strength_; uint8_t signal_strength_;
uint32_t frequency_; uint32_t frequency_;
uint32_t route_priority;
util::SleeperThread thread_; util::SleeperThread thread_;
util::SleeperThread thread_timer_; util::SleeperThread thread_timer_;

View File

@ -573,11 +573,7 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
bool has_gateway = false; bool has_gateway = false;
bool has_destination = false; bool has_destination = false;
int temp_idx = -1; int temp_idx = -1;
uint32_t priority = 0;
/* If we found the correct answer, skip parsing the attributes. */
if (!is_del_event && net->ifid_ != -1) {
return NL_OK;
}
/* Find the message(s) concerting the main routing table, each message /* Find the message(s) concerting the main routing table, each message
* corresponds to a single routing table entry. * corresponds to a single routing table entry.
@ -621,6 +617,9 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
/* The output interface index. */ /* The output interface index. */
temp_idx = *static_cast<int *>(RTA_DATA(attr)); temp_idx = *static_cast<int *>(RTA_DATA(attr));
break; break;
case RTA_PRIORITY:
priority = *(uint32_t*)RTA_DATA(attr);
break;
default: default:
break; break;
} }
@ -628,10 +627,16 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
// Check if we have a default route. // Check if we have a default route.
if (has_gateway && !has_destination && temp_idx != -1) { if (has_gateway && !has_destination && temp_idx != -1) {
if (!is_del_event) { // Check if this is the first default route we see, or if this new
// route have a higher priority.
if (!is_del_event && ((net->ifid_ == -1) || (priority < net->route_priority))) {
// Clear if's state for the case were there is a higher priority
// route on a different interface.
net->clearIface();
net->ifid_ = temp_idx; net->ifid_ = temp_idx;
net->route_priority = priority;
spdlog::debug("network: new default route via if{}", temp_idx); spdlog::debug("network: new default route via if{} metric {}", temp_idx, priority);
/* Ask ifname associated with temp_idx as well as carrier status */ /* Ask ifname associated with temp_idx as well as carrier status */
struct ifinfomsg ifinfo_hdr = { struct ifinfomsg ifinfo_hdr = {
@ -653,9 +658,10 @@ int waybar::modules::Network::handleEvents(struct nl_msg *msg, void *data) {
net->want_addr_dump_ = true; net->want_addr_dump_ = true;
net->askForStateDump(); net->askForStateDump();
net->thread_timer_.wake_up(); net->thread_timer_.wake_up();
} else if (is_del_event && temp_idx == net->ifid_) { } else if (is_del_event && temp_idx == net->ifid_
spdlog::debug("network: default route deleted {}/if{}", && net->route_priority == priority) {
net->ifname_, temp_idx); spdlog::debug("network: default route deleted {}/if{} metric {}",
net->ifname_, temp_idx, priority);
net->clearIface(); net->clearIface();
net->dp.emit(); net->dp.emit();