Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | /* * Copyright (c) 2019 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(conn_mgr, CONFIG_NET_CONNECTION_MANAGER_LOG_LEVEL); #include <zephyr/init.h> #include <zephyr/kernel.h> #include <errno.h> #include <zephyr/net/net_core.h> #include <zephyr/net/net_if.h> #include <zephyr/net/net_mgmt.h> #include <zephyr/sys/iterable_sections.h> #include <zephyr/net/conn_mgr_connectivity.h> #include "conn_mgr_private.h" #if defined(CONFIG_NET_TC_THREAD_COOPERATIVE) #define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1) #else #define THREAD_PRIORITY K_PRIO_PREEMPT(7) #endif /* Internal state array tracking readiness, flags, and other state information for all available * ifaces. Note that indexing starts at 0, whereas Zephyr iface indices start at 1. * conn_mgr_get_if_by_index and conn_mgr_get_index_for_if are used to go back and forth between * iface_states indices and Zephyr iface pointers. */ uint16_t iface_states[CONN_MGR_IFACE_MAX]; /* Tracks the total number of L4-ready ifaces */ static uint16_t ready_count; /* Tracks the last ifaces to change state in each respective direction */ static struct net_if *last_iface_down; static struct net_if *last_iface_up; /* Used to signal when modifications have been made that need to be responded to */ K_SEM_DEFINE(conn_mgr_event_signal, 1, 1); /* Used to protect conn_mgr state */ K_MUTEX_DEFINE(conn_mgr_lock); /** * @brief Retrieves pointer to an iface by the index that corresponds to it in iface_states * * @param index - The index in iface_states to find the corresponding iface for. * @return net_if* - The corresponding iface. */ static struct net_if *conn_mgr_get_if_by_index(int index) { return net_if_get_by_index(index + 1); } /** * @brief Gets the index in iface_states for the state corresponding to a provided iface. * * @param iface - iface to find the index of. * @return int - The index found. */ static int conn_mgr_get_index_for_if(struct net_if *iface) { return net_if_get_by_iface(iface) - 1; } /** * @brief Marks an iface as ready or unready and updates all associated state tracking. * * @param idx - index (in iface_states) of the iface to mark ready or unready * @param readiness - true if the iface should be considered ready, otherwise false */ static void conn_mgr_set_ready(int idx, bool readiness) { /* Clear and then update the L4-readiness bit */ iface_states[idx] &= ~CONN_MGR_IF_READY; if (readiness) { iface_states[idx] |= CONN_MGR_IF_READY; ready_count += 1; last_iface_up = conn_mgr_get_if_by_index(idx); } else { ready_count -= 1; last_iface_down = conn_mgr_get_if_by_index(idx); } } static void conn_mgr_act_on_changes(void) { int idx; int original_ready_count; bool is_ip_ready; bool is_ipv6_ready; bool is_ipv4_ready; bool is_l4_ready; bool is_oper_up; bool was_l4_ready; bool is_ignored; k_mutex_lock(&conn_mgr_lock, K_FOREVER); original_ready_count = ready_count; 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; is_ipv4_ready = iface_states[idx] & CONN_MGR_IF_IPV4_SET; is_oper_up = iface_states[idx] & CONN_MGR_IF_UP; is_ignored = iface_states[idx] & CONN_MGR_IF_IGNORED; is_ip_ready = is_ipv6_ready || is_ipv4_ready; is_l4_ready = is_oper_up && is_ip_ready && !is_ignored; /* Respond to changes to iface readiness */ if (was_l4_ready != is_l4_ready) { /* Track the iface readiness change */ conn_mgr_set_ready(idx, is_l4_ready); } } /* If the total number of ready ifaces changed, possibly send an event */ if (ready_count != original_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) { /* We just gained connectivity */ net_mgmt_event_notify(NET_EVENT_L4_CONNECTED, last_iface_up); } } k_mutex_unlock(&conn_mgr_lock); } /** * @brief Initialize the internal state flags for the given iface using its current status * * @param iface - iface to initialize from. */ static void conn_mgr_initial_state(struct net_if *iface) { int idx = net_if_get_by_iface(iface) - 1; k_mutex_lock(&conn_mgr_lock, K_FOREVER); if (net_if_is_up(iface)) { NET_DBG("Iface %p UP", iface); iface_states[idx] |= CONN_MGR_IF_UP; } if (IS_ENABLED(CONFIG_NET_NATIVE_IPV6)) { if (net_if_ipv6_get_global_addr(NET_ADDR_PREFERRED, &iface)) { NET_DBG("IPv6 addr set"); iface_states[idx] |= CONN_MGR_IF_IPV6_SET; } } if (IS_ENABLED(CONFIG_NET_NATIVE_IPV4)) { if (net_if_ipv4_get_global_addr(iface, NET_ADDR_PREFERRED)) { NET_DBG("IPv4 addr set"); iface_states[idx] |= CONN_MGR_IF_IPV4_SET; } } iface_states[idx] |= CONN_MGR_IF_CHANGED; k_mutex_unlock(&conn_mgr_lock); } static void conn_mgr_init_cb(struct net_if *iface, void *user_data) { ARG_UNUSED(user_data); conn_mgr_initial_state(iface); } static void conn_mgr_handler(void) { k_mutex_lock(&conn_mgr_lock, K_FOREVER); conn_mgr_conn_init(); conn_mgr_init_events_handler(); net_if_foreach(conn_mgr_init_cb, NULL); k_mutex_unlock(&conn_mgr_lock); NET_DBG("Connection Manager started"); while (true) { /* Wait for changes */ k_sem_take(&conn_mgr_event_signal, K_FOREVER); /* Respond to changes */ conn_mgr_act_on_changes(); } } K_THREAD_DEFINE(conn_mgr, CONFIG_NET_CONNECTION_MANAGER_STACK_SIZE, (k_thread_entry_t)conn_mgr_handler, NULL, NULL, NULL, THREAD_PRIORITY, 0, 0); void conn_mgr_resend_status(void) { k_mutex_lock(&conn_mgr_lock, K_FOREVER); if (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); } k_mutex_unlock(&conn_mgr_lock); } void conn_mgr_ignore_iface(struct net_if *iface) { int idx = conn_mgr_get_index_for_if(iface); k_mutex_lock(&conn_mgr_lock, K_FOREVER); 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_event_signal); } k_mutex_unlock(&conn_mgr_lock); } void conn_mgr_watch_iface(struct net_if *iface) { int idx = conn_mgr_get_index_for_if(iface); k_mutex_lock(&conn_mgr_lock, K_FOREVER); 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_event_signal); } k_mutex_unlock(&conn_mgr_lock); } bool conn_mgr_is_iface_ignored(struct net_if *iface) { int idx = conn_mgr_get_index_for_if(iface); bool ret = false; k_mutex_lock(&conn_mgr_lock, K_FOREVER); ret = iface_states[idx] & CONN_MGR_IF_IGNORED; k_mutex_unlock(&conn_mgr_lock); return ret; } /** * @brief Check whether a provided iface uses the provided L2. * * @param iface - iface to check. * @param l2 - L2 to check. NULL will match offloaded ifaces. * @retval true if the iface uses the provided L2. * @retval false otherwise. */ static bool iface_uses_l2(struct net_if *iface, const struct net_l2 *l2) { return (!l2 && net_if_offload(iface)) || (net_if_l2(iface) == l2); } void conn_mgr_ignore_l2(const struct net_l2 *l2) { /* conn_mgr_ignore_iface already locks the mutex, but we lock it here too * so that all matching ifaces are updated simultaneously. */ k_mutex_lock(&conn_mgr_lock, K_FOREVER); STRUCT_SECTION_FOREACH(net_if, iface) { if (iface_uses_l2(iface, l2)) { conn_mgr_ignore_iface(iface); } } k_mutex_unlock(&conn_mgr_lock); } void conn_mgr_watch_l2(const struct net_l2 *l2) { /* conn_mgr_watch_iface already locks the mutex, but we lock it here too * so that all matching ifaces are updated simultaneously. */ k_mutex_lock(&conn_mgr_lock, K_FOREVER); STRUCT_SECTION_FOREACH(net_if, iface) { if (iface_uses_l2(iface, l2)) { conn_mgr_watch_iface(iface); } } k_mutex_unlock(&conn_mgr_lock); } static int conn_mgr_init(void) { int i; for (i = 0; i < ARRAY_SIZE(iface_states); i++) { iface_states[i] = 0; } k_thread_start(conn_mgr); return 0; } SYS_INIT(conn_mgr_init, APPLICATION, CONFIG_NET_CONNECTION_MANAGER_PRIORITY); |