Skip to content

Commit

Permalink
net: conn_mgr: Track ready count ephemerally
Browse files Browse the repository at this point in the history
Instead of incrementing and decrementing global counter,
just recompute the ready-count from scratch every time
conn_mgr_mon_handle_update is called.

This will simplify the introduction of additional ready count types.

This should have no externally observable impact on the behavior of
conn_mgr.

Signed-off-by: Georges Oates_Larsen <[email protected]>
  • Loading branch information
glarsennordic committed May 21, 2024
1 parent 76559f2 commit d10a993
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
37 changes: 15 additions & 22 deletions subsys/net/conn_mgr/conn_mgr_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ static struct k_thread conn_mgr_mon_thread;
*/
uint16_t iface_states[CONN_MGR_IFACE_MAX];

/* Tracks the total number of L4-ready ifaces */
static uint16_t ready_count;
/* Tracks the most recent total quantity of L4-ready ifaces */
static uint16_t last_ready_count;

/* Tracks the last ifaces to change state in each respective direction */
static struct net_if *last_iface_down;
Expand Down Expand Up @@ -82,19 +82,16 @@ static void conn_mgr_mon_set_ready(int idx, bool readiness)

if (readiness) {
iface_states[idx] |= CONN_MGR_IF_READY;

ready_count += 1;
last_iface_up = conn_mgr_mon_get_if_by_index(idx);
} else {
ready_count -= 1;
last_iface_down = conn_mgr_mon_get_if_by_index(idx);
}
}

static void conn_mgr_mon_handle_update(void)
{
int idx;
int original_ready_count;
int ready_count;
bool is_ip_ready;
bool is_ipv6_ready;
bool is_ipv4_ready;
Expand All @@ -105,21 +102,13 @@ static void conn_mgr_mon_handle_update(void)

k_mutex_lock(&conn_mgr_mon_lock, K_FOREVER);

original_ready_count = ready_count;
ready_count = 0;
for (idx = 0; idx < ARRAY_SIZE(iface_states); idx++) {
if (iface_states[idx] == 0) {
/* This interface is not used */
continue;
}

if (!(iface_states[idx] & CONN_MGR_IF_CHANGED)) {
/* No changes on this iface */
continue;
}

/* Clear the state-change flag */
iface_states[idx] &= ~CONN_MGR_IF_CHANGED;

/* Detect whether the iface is currently or was L4 ready */
was_l4_ready = iface_states[idx] & CONN_MGR_IF_READY;
is_ipv6_ready = iface_states[idx] & CONN_MGR_IF_IPV6_SET;
Expand All @@ -134,19 +123,27 @@ static void conn_mgr_mon_handle_update(void)
/* Track the iface readiness change */
conn_mgr_mon_set_ready(idx, is_l4_ready);
}

/* Track ready iface count */
if (is_l4_ready) {
ready_count += 1;
}

}

/* If the total number of ready ifaces changed, possibly send an event */
if (ready_count != original_ready_count) {
if (ready_count != last_ready_count) {
if (ready_count == 0) {
/* We just lost connectivity */
net_mgmt_event_notify(NET_EVENT_L4_DISCONNECTED, last_iface_down);
} else if (original_ready_count == 0) {
} else if (last_ready_count == 0) {
/* We just gained connectivity */
net_mgmt_event_notify(NET_EVENT_L4_CONNECTED, last_iface_up);
}
}

last_ready_count = ready_count;

k_mutex_unlock(&conn_mgr_mon_lock);
}

Expand Down Expand Up @@ -181,8 +178,6 @@ static void conn_mgr_mon_initial_state(struct net_if *iface)

}

iface_states[idx] |= CONN_MGR_IF_CHANGED;

k_mutex_unlock(&conn_mgr_mon_lock);
}

Expand Down Expand Up @@ -224,7 +219,7 @@ void conn_mgr_mon_resend_status(void)
{
k_mutex_lock(&conn_mgr_mon_lock, K_FOREVER);

if (ready_count == 0) {
if (last_ready_count == 0) {
net_mgmt_event_notify(NET_EVENT_L4_DISCONNECTED, last_iface_down);
} else {
net_mgmt_event_notify(NET_EVENT_L4_CONNECTED, last_iface_up);
Expand All @@ -242,7 +237,6 @@ void conn_mgr_ignore_iface(struct net_if *iface)
if (!(iface_states[idx] & CONN_MGR_IF_IGNORED)) {
/* Set ignored flag and mark state as changed */
iface_states[idx] |= CONN_MGR_IF_IGNORED;
iface_states[idx] |= CONN_MGR_IF_CHANGED;
k_sem_give(&conn_mgr_mon_updated);
}

Expand All @@ -258,7 +252,6 @@ void conn_mgr_watch_iface(struct net_if *iface)
if (iface_states[idx] & CONN_MGR_IF_IGNORED) {
/* Clear ignored flag and mark state as changed */
iface_states[idx] &= ~CONN_MGR_IF_IGNORED;
iface_states[idx] |= CONN_MGR_IF_CHANGED;
k_sem_give(&conn_mgr_mon_updated);
}

Expand Down
2 changes: 0 additions & 2 deletions subsys/net/conn_mgr/conn_mgr_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
/* Internal state flags */
#define CONN_MGR_IF_READY BIT(14)

/* Event flags */
#define CONN_MGR_IF_CHANGED BIT(15)

/* NET_MGMT event masks */
#define CONN_MGR_IFACE_EVENTS_MASK (NET_EVENT_IF_DOWN | \
Expand Down
4 changes: 0 additions & 4 deletions subsys/net/conn_mgr/events_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ static void conn_mgr_iface_events_handler(struct net_mgmt_event_callback *cb,
default:
goto done;
}

iface_states[idx] |= CONN_MGR_IF_CHANGED;
k_sem_give(&conn_mgr_mon_updated);

done:
Expand Down Expand Up @@ -95,7 +93,6 @@ static void conn_mgr_ipv6_events_handler(struct net_mgmt_event_callback *cb,
goto done;
}

iface_states[idx] |= CONN_MGR_IF_CHANGED;
k_sem_give(&conn_mgr_mon_updated);

done:
Expand Down Expand Up @@ -148,7 +145,6 @@ static void conn_mgr_ipv4_events_handler(struct net_mgmt_event_callback *cb,
goto done;
}

iface_states[idx] |= CONN_MGR_IF_CHANGED;
k_sem_give(&conn_mgr_mon_updated);

done:
Expand Down

0 comments on commit d10a993

Please sign in to comment.