Skip to contentSkip to conversation

service/wispr: Add Per-service-unique WISPr Host Route Metric/Priority

PATCH In review Grant Erickson 2 participants 13 messages Dec 16, 2023

lore
Share & tools
lore
#0 lore raw

cover service/wispr: Add Per-service-unique WISPr Host Route Metric/Priority

This add and leverages '__connman_service_get_route_metric' to select an appropriate service-specific route metric/priority when adding or deleting WISPr host routes.

This allows multiple such routes to coexist simultaneously, supporting "continuous" mode online checks in which one or more services may be conducting "online" WISPr-based Internet reachability checks as they move in and out of reachability success/failure.

'__connman_service_get_route_metric' is a non-public service interface, which attempts to get the route metric/priority for the specified service based on the current service and services state.

If the service is the default or if it is the only service, then the metric is zero (0). Otherwise, a low-priority metric (metric > 0) unique to the service and its underlying network interface is computed and returned.

Grant Erickson (11):
service: Add '__connman_service_get_route_metric'.
service: Document '__connman_service_get_route_metric'.
wispr: Expand 'DBG' in 'wispr_portal_detect'.
wispr: Add 'DBG' to '__connman_wispr_start' error path.
wispr: Update 'DBG' in '__connman_wispr_start'.
wispr: Refactor 'free_wispr_routes'.
wispr: Simplify IPv4 vs. IPv6 host route management.
wispr: Document 'free_wispr_route{,s}'.
wispr: Document 'wispr_route'.
wispr: Document 'wispr_portal_context_route_ops'.
wispr: Leverage '__connman_service_get_route_metric'.

 src/connman.h |   2 +
 src/service.c |  98 ++++++++++++++++++++++++
 src/wispr.c   | 207 +++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 271 insertions(+), 36 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/11 service: Add '__connman_service_get_route_metric'.

This adds a new non-public service interface, '__connman_service_get_route_metric', which attempts to get the route metric/priority for the specified service based on the current service and services state.

If the service is the default or if it is the only service, then the metric is zero (0). Otherwise, a low-priority metric (metric > 0) unique toservice and its underlying network interface is computed and returned.

---
 src/connman.h |  2 ++
 src/service.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
src/connman.h +2 -0
index 24e667d8e1f4..90d5a34b8c58 100644
@@ -733,6 +733,8 @@ void __connman_service_remove_from_network(struct connman_network *network);
 void __connman_service_read_ip4config(struct connman_service *service);
 void __connman_service_read_ip6config(struct connman_service *service);
 
+int __connman_service_get_route_metric(const struct connman_service *service,
+				uint32_t *metric);
 struct connman_ipconfig *__connman_service_get_ip4config(
 				struct connman_service *service);
 struct connman_ipconfig *__connman_service_get_ip6config(
src/service.c +64 -0

70 lines not on this page · raw message

Signature · 1 line
2.42.0
#2 lore raw

PATCH 2/11 service: Document '__connman_service_get_route_metric'.

In reply to Grant Erickson

This adds documentation to the '__connman_service_get_route_metric' function.

---
 src/service.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
src/service.c +34 -0
index f0b37e9a0d41..1f2c9f54db9a 100644
@@ -6539,11 +6539,45 @@ __connman_service_get_network(struct connman_service *service)
 	return service->network;
 }
 
+/**
+ *  @brief
+ *    Return the current service count.
+ *
+ *  @returns
+ *    The current service count.
+ *
+ */
 static size_t service_get_count(void)
 {
 	return service_list ? g_list_length(service_list) : 0;
 }
 
+/**
+ *  @brief
+ *    Get the route metric/priority for the specified service.
+ *
+ *  This attempts to get the route metric/priority for the specified
+ *  service based on the current service and services state.
+ *
+ *  If the service is the default or if it is the only service, then
+ *  the metric is zero (0). Otherwise, a low-priority metric (metric >
+ *  0) unique to @a service and its underlying network interface is
+ *  computed and returned.
+ *
+ *  @param[in]      service  A pointer to the immutable service for
+ *                           which to get the route metric/priority.
+ *  @param[in,out]  metric   A pointer to storage for the route
+ *                           metric/priority, populated with the route
+ *                           metric/priority on success.
+ *
+ *  @retval  0        If successful.
+ *  @retval  -EINVAL  If @a service or @a metric are null.
+ *  @retval  -ENXIO   If the network interface index associated with
+ *                    @a service is invalid.
+ *
+ *  @sa connman_service_is_default
+ *
+ */
 int __connman_service_get_route_metric(const struct connman_service *service,
 				uint32_t *metric)
 {
Signature · 1 line
2.42.0
#3 lore raw

PATCH 3/11 wispr: Expand 'DBG' in 'wispr_portal_detect'.

In reply to Grant Erickson

This includes both the network interface index and name in the 'DBG' in 'wispr_portal_detect' to make it easier to identify on which network interface the WISPr portal detection is being made.

---
 src/wispr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/wispr.c +2 -2
index 9cd1e7db501d..ba774a62dd48 100644
@@ -1104,8 +1104,6 @@ static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context,
 	if (!interface)
 		return -EINVAL;
 
-	DBG("interface %s", interface);
-
 	if_index = connman_inet_ifindex(interface);
 	if (if_index < 0) {
 		DBG("Could not get ifindex");
@@ -1113,6 +1111,8 @@ static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context,
 		goto done;
 	}
 
+	DBG("index %d (%s)", if_index, interface);
+
 	nameservers = connman_service_get_nameservers(wp_context->service);
 	if (!nameservers) {
 		DBG("Could not get nameservers");
Signature · 1 line
2.42.0
#4 lore raw

PATCH 4/11 wispr: Add 'DBG' to '__connman_wispr_start' error path.

In reply to Grant Erickson

This adds a 'DBG' to the '__connman_wispr_start' error path that includes both the error and the WISPr portal context associated with the error.

---
 src/wispr.c | 2 ++
 1 file changed, 2 insertions(+)
src/wispr.c +2 -0
index ba774a62dd48..19af1e7bede2 100644
@@ -1338,6 +1338,8 @@ int __connman_wispr_start(struct connman_service *service,
 	return 0;
 
 free_wp:
+	DBG("err %d wp_context %p", err, wp_context);
+
 	wp_context->cb(wp_context->service, wp_context->type, false, err);
 
 	g_hash_table_remove(wispr_portal_hash, GINT_TO_POINTER(index));
Signature · 1 line
2.42.0
#5 lore raw

PATCH 5/11 wispr: Update 'DBG' in '__connman_wispr_start'.

In reply to Grant Erickson

This updates the initial 'DBG' in '__connman_wispr_start' to ensure there is space after the connect timeout value and the "ms" unit designation.

---
 src/wispr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/wispr.c +1 -1
index 19af1e7bede2..04229d045cab 100644
@@ -1281,7 +1281,7 @@ int __connman_wispr_start(struct connman_service *service,
 	struct connman_wispr_portal *wispr_portal = NULL;
 	int index, err;
 
-	DBG("service %p (%s) type %d (%s) connect_timeout %ums callback %p",
+	DBG("service %p (%s) type %d (%s) connect_timeout %u ms callback %p",
 		service, connman_service_get_identifier(service),
 		type, __connman_ipconfig_type2string(type),
 		connect_timeout_ms, callback);
Signature · 1 line
2.42.0
#6 lore raw

PATCH 6/11 wispr: Refactor 'free_wispr_routes'.

In reply to Grant Erickson

This refactors 'free_wispr_routes' into a second, helper function 'free_wispr_route' such that host route deallocation is separated from container iteration.

---
 src/wispr.c | 45 ++++++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 19 deletions(-)
src/wispr.c +26 -19
index 04229d045cab..568b7f0fc8a7 100644
@@ -135,30 +135,37 @@ static void connman_wispr_message_init(struct connman_wispr_message *msg)
 	msg->location_name = NULL;
 }
 
+static void free_wispr_route(
+		const struct connman_wispr_portal_context *wp_context,
+		struct wispr_route *route)
+{
+	DBG("free route to %s if %d type %d", route->address,
+			route->if_index, wp_context->type);
+
+	switch (wp_context->type) {
+	case CONNMAN_IPCONFIG_TYPE_IPV4:
+		connman_inet_del_host_route(route->if_index,
+				route->address);
+		break;
+	case CONNMAN_IPCONFIG_TYPE_IPV6:
+		connman_inet_del_ipv6_host_route(route->if_index,
+				route->address);
+		break;
+	case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
+	case CONNMAN_IPCONFIG_TYPE_ALL:
+		break;
+	}
+
+	g_free(route->address);
+	route->address = NULL;
+}
+
 static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
 {
 	while (wp_context->route_list) {
 		struct wispr_route *route = wp_context->route_list->data;
 
-		DBG("free route to %s if %d type %d", route->address,
-				route->if_index, wp_context->type);
-
-		switch (wp_context->type) {
-		case CONNMAN_IPCONFIG_TYPE_IPV4:
-			connman_inet_del_host_route(route->if_index,
-					route->address);
-			break;
-		case CONNMAN_IPCONFIG_TYPE_IPV6:
-			connman_inet_del_ipv6_host_route(route->if_index,
-					route->address);
-			break;
-		case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
-		case CONNMAN_IPCONFIG_TYPE_ALL:
-			break;
-		}
-
-		g_free(route->address);
-		route->address = NULL;
+		free_wispr_route(wp_context, route);
 
 		g_free(route);
 		wp_context->route_list->data = NULL;
Signature · 1 line
2.42.0
#7 lore raw

PATCH 7/11 wispr: Simplify IPv4 vs. IPv6 host route management.

In reply to Grant Erickson

This uses the recently-added, symmetric 'connman_inet_{add,del}_{,ipv6_}host_route_with_metric' functions and route operations tables to simplify IPv4 vs. IPv6 host route management for WISPr requests.

---
 src/wispr.c | 112 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 82 insertions(+), 30 deletions(-)
src/wispr.c +82 -30
index 568b7f0fc8a7..5ea573ca7769 100644
@@ -53,12 +53,25 @@ enum connman_wispr_result {
 struct wispr_route {
 	char *address;
 	int if_index;
+	uint32_t metric;
+};
+
+struct wispr_portal_context_route_ops {
+	int (*add_host_route_with_metric)(int index,
+		const char *host,
+		const char *gateway,
+		uint32_t metric);
+	int (*del_host_route_with_metric)(int index,
+		const char *host,
+		const char *gateway,
+		uint32_t metric);
 };
 
 struct connman_wispr_portal_context {
 	int refcount;
 	struct connman_service *service;
 	enum connman_ipconfig_type type;
+	const struct wispr_portal_context_route_ops *ops;
 	__connman_wispr_cb_t cb;
 	struct connman_wispr_portal *wispr_portal;
 
@@ -103,6 +116,22 @@ static GHashTable *wispr_portal_hash = NULL;
 static char *online_check_ipv4_url = NULL;
 static char *online_check_ipv6_url = NULL;
 
+static const struct wispr_portal_context_route_ops
+	ipv4_wispr_portal_context_route_ops = {
+	.add_host_route_with_metric    =
+		connman_inet_add_host_route_with_metric,
+	.del_host_route_with_metric    =
+		connman_inet_del_host_route_with_metric
+};
+
+static const struct wispr_portal_context_route_ops
+	ipv6_wispr_portal_context_route_ops = {
+	.add_host_route_with_metric    =
+		connman_inet_add_ipv6_host_route_with_metric,
+	.del_host_route_with_metric    =
+		connman_inet_del_ipv6_host_route_with_metric
+};
+
 #define wispr_portal_context_ref(wp_context) \
 	wispr_portal_context_ref_debug(wp_context, __FILE__, __LINE__, __func__)
 #define wispr_portal_context_unref(wp_context) \
@@ -139,23 +168,39 @@ static void free_wispr_route(
 		const struct connman_wispr_portal_context *wp_context,
 		struct wispr_route *route)
 {
-	DBG("free route to %s if %d type %d", route->address,
-			route->if_index, wp_context->type);
+	g_autofree char *interface = NULL;
+	const char *gateway;
 
-	switch (wp_context->type) {
-	case CONNMAN_IPCONFIG_TYPE_IPV4:
-		connman_inet_del_host_route(route->if_index,
-				route->address);
-		break;
-	case CONNMAN_IPCONFIG_TYPE_IPV6:
-		connman_inet_del_ipv6_host_route(route->if_index,
-				route->address);
-		break;
-	case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
-	case CONNMAN_IPCONFIG_TYPE_ALL:
-		break;
-	}
+	gateway = __connman_ipconfig_get_gateway_from_index(route->if_index,
+		wp_context->type);
+
+	/*
+	 * If gateway was null, as with wispr_route_request, there was no
+	 * host route created. Consequently, there is no host route to
+	 * delete. Simply free the address.
+	 */
+	if (!gateway)
+		goto free;
+
+	interface = connman_inet_ifname(route->if_index);
 
+	DBG("delete route to %s via %s dev %d (%s) metric %u type %d (%s) "
+		"ops %p",
+		route->address,
+		gateway,
+		route->if_index, interface,
+		route->metric,
+		wp_context->type,
+		__connman_ipconfig_type2string(wp_context->type),
+		wp_context->ops);
+
+	wp_context->ops->del_host_route_with_metric(
+				route->if_index,
+				route->address,
+				gateway,
+				route->metric);
+
+free:
 	g_free(route->address);
 	route->address = NULL;
 }
@@ -584,6 +629,7 @@ static bool wispr_route_request(const char *address, int ai_family,
 	struct connman_wispr_portal_context *wp_context = user_data;
 	const char *gateway;
 	struct wispr_route *route;
+	uint32_t metric = 0;
 
 	gateway = __connman_ipconfig_get_gateway_from_index(if_index,
 		wp_context->type);
@@ -599,20 +645,16 @@ static bool wispr_route_request(const char *address, int ai_family,
 		return false;
 	}
 
-	switch (wp_context->type) {
-	case CONNMAN_IPCONFIG_TYPE_IPV4:
-		result = connman_inet_add_host_route(if_index, address,
-				gateway);
-		break;
-	case CONNMAN_IPCONFIG_TYPE_IPV6:
-		result = connman_inet_add_ipv6_host_route(if_index, address,
-				gateway);
-		break;
-	case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
-	case CONNMAN_IPCONFIG_TYPE_ALL:
-		break;
-	}
+	DBG("service %p (%s) metric %u",
+		wp_context->service,
+		connman_service_get_identifier(wp_context->service),
+		metric);
 
+	result = wp_context->ops->add_host_route_with_metric(
+					if_index,
+					address,
+					gateway,
+					metric);
 	if (result < 0) {
 		g_free(route);
 		return false;
@@ -620,6 +662,8 @@ static bool wispr_route_request(const char *address, int ai_family,
 
 	route->address = g_strdup(address);
 	route->if_index = if_index;
+	route->metric = metric;
+
 	wp_context->route_list = g_slist_prepend(wp_context->route_list, route);
 
 	return true;
@@ -1334,10 +1378,18 @@ int __connman_wispr_start(struct connman_service *service,
 	wp_context->cb = callback;
 	wp_context->wispr_portal = wispr_portal;
 
-	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
+	if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
 		wispr_portal->ipv4_context = wp_context;
-	else
+		wispr_portal->ipv4_context->ops =
+			&ipv4_wispr_portal_context_route_ops;
+	} else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
 		wispr_portal->ipv6_context = wp_context;
+		wispr_portal->ipv6_context->ops =
+			&ipv6_wispr_portal_context_route_ops;
+	} else {
+		err = -EINVAL;
+		goto free_wp;
+	}
 
 	err = wispr_portal_detect(wp_context, connect_timeout_ms);
 	if (err)
Signature · 1 line
2.42.0
#8 lore raw

PATCH 8/11 wispr: Document 'free_wispr_route{,s}'.

In reply to Grant Erickson

This adds documentation to the 'free_wispr_route{,s}' functions.

---
 src/wispr.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
src/wispr.c +34 -0
index 5ea573ca7769..581205c6662c 100644
@@ -164,6 +164,27 @@ static void connman_wispr_message_init(struct connman_wispr_message *msg)
 	msg->location_name = NULL;
 }
 
+/**
+ *  @brief
+ *    Deallocate resources associated with the specified WISPr host
+ *    route.
+ *
+ *  This attempts to deallocate resources, including host routes,
+ *  associated with the specified WISPr host route belonging to the
+ *  specified WISPr portal context.
+ *
+ *  @param[in]      wp_context  A pointer to the immutable WISPr
+ *                              portal context associated with @a
+ *                              route.
+ *  @param[in,out]  route       A pointer to the mutable WISPr host
+ *                              route structure for which to delete an
+ *                              associated host route and to
+ *                              deallocate any dynamically-allocated
+ *                              resources associated with it.
+ *
+ *  @sa wispr_route_request
+ *
+ */
 static void free_wispr_route(
 		const struct connman_wispr_portal_context *wp_context,
 		struct wispr_route *route)
@@ -205,6 +226,19 @@ free:
 	route->address = NULL;
 }
 
+/**
+ *  @brief
+ *    Deallocate host route resources associated with the specified
+ *    WISPr portal context.
+ *
+ *  This attempts to deallocate host route resources associated with
+ *  the specified WISPr specified WISPr portal context.
+ *
+ *  @param[in]      wp_context  A pointer to the mutable WISPr
+ *                              portal context for which to deallocate
+ *                              resources associated with host routes.
+ *
+ */
 static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
 {
 	while (wp_context->route_list) {
Signature · 1 line
2.42.0
#9 lore raw

PATCH 9/11 wispr: Document 'wispr_route'.

In reply to Grant Erickson

This adds documentation to the 'wispr_route' structure.

---
 src/wispr.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
src/wispr.c +19 -0
index 581205c6662c..317146af8cb8 100644
@@ -50,9 +50,28 @@ enum connman_wispr_result {
 	CONNMAN_WISPR_RESULT_FAILED  = 3,
 };
 
+/**
+ *  State for host routes used for a WISPr request.
+ */
 struct wispr_route {
+	/**
+	 *	A pointer to a mutable, dynamically-allocated null-terminated
+	 *	C string containing the text-formatted address of the WISPr
+	 *	host.
+	 */
 	char *address;
+
+	/**
+	 *	The network interface index associated with the underlying
+	 *	network interface over which the WISPr request will sent and
+	 *	the reply received.
+	 */
 	int if_index;
+
+	/**
+	 *	The route metric/priority used for the host route created for
+	 *	the WISPr host.
+	 */
 	uint32_t metric;
 };
 
Signature · 1 line
2.42.0
#10 lore raw

PATCH 10/11 wispr: Document 'wispr_portal_context_route_ops'.

In reply to Grant Erickson

This adds documentation to the 'wispr_portal_context_route_ops' structure.

---
 src/wispr.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/wispr.c +10 -0
index 317146af8cb8..1af880118e14 100644
@@ -75,6 +75,10 @@ struct wispr_route {
 	uint32_t metric;
 };
 
+/**
+ *  Gateway configuration function pointers for IP configuration
+ *  type-specific route set/clear/add/delete operations.
+ */
 struct wispr_portal_context_route_ops {
 	int (*add_host_route_with_metric)(int index,
 		const char *host,
@@ -90,7 +94,13 @@ struct connman_wispr_portal_context {
 	int refcount;
 	struct connman_service *service;
 	enum connman_ipconfig_type type;
+
+	/**
+	 *	A pointer to immutable function pointers for route
+	 *	set/clear/add/delete operations.
+	 */
 	const struct wispr_portal_context_route_ops *ops;
+
 	__connman_wispr_cb_t cb;
 	struct connman_wispr_portal *wispr_portal;
 
Signature · 1 line
2.42.0
#11 lore raw

PATCH 11/11 wispr: Leverage '__connman_service_get_route_metric'.

In reply to Grant Erickson

This leverages '__connman_service_get_route_metric' to select an appropriate service-specific route metric/priority when adding or deleting WISPr host routes.

This allows multiple such routes to coexist simultaneously, supporting "continuous" mode online checks in which one or more services may be conducting "online" WISPr-based Internet reachability checks as they move in and out of reachability success/failure.

---
 src/wispr.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
src/wispr.c +11 -0
index 1af880118e14..efc0ca2fbaa9 100644
@@ -708,6 +708,17 @@ static bool wispr_route_request(const char *address, int ai_family,
 		return false;
 	}
 
+	result = __connman_service_get_route_metric(wp_context->service,
+				&metric);
+	if (result < 0) {
+		DBG("failed to get metric for service %p (%s): %s (%d)",
+			wp_context->service,
+			connman_service_get_identifier(wp_context->service),
+			strerror(-result), result);
+
+		return false;
+	}
+
 	DBG("service %p (%s) metric %u",
 		wp_context->service,
 		connman_service_get_identifier(wp_context->service),
Signature · 1 line
2.42.0
#12 lore raw
In reply to Grant Erickson

Hi Grant,

Quoted 31 lines

This add and leverages '__connman_service_get_route_metric' to select an appropriate service-specific route metric/priority when adding or deleting WISPr host routes.

This allows multiple such routes to coexist simultaneously, supporting "continuous" mode online checks in which one or more services may be conducting "online" WISPr-based Internet reachability checks as they move in and out of reachability success/failure.

'__connman_service_get_route_metric' is a non-public service interface, which attempts to get the route metric/priority for the specified service based on the current service and services state.

If the service is the default or if it is the only service, then the metric is zero (0). Otherwise, a low-priority metric (metric > 0) unique to the service and its underlying network interface is computed and returned.

Grant Erickson (11):
service: Add '__connman_service_get_route_metric'.
service: Document '__connman_service_get_route_metric'.
wispr: Expand 'DBG' in 'wispr_portal_detect'.
wispr: Add 'DBG' to '__connman_wispr_start' error path.
wispr: Update 'DBG' in '__connman_wispr_start'.
wispr: Refactor 'free_wispr_routes'.
wispr: Simplify IPv4 vs. IPv6 host route management.
wispr: Document 'free_wispr_route{,s}'.
wispr: Document 'wispr_route'.
wispr: Document 'wispr_portal_context_route_ops'.
wispr: Leverage '__connman_service_get_route_metric'.

Quoted 4 lines
src/connman.h |   2 +
src/service.c |  98 ++++++++++++++++++++++++
src/wispr.c   | 207 +++++++++++++++++++++++++++++++++++++++++---------
3 files changed, 271 insertions(+), 36 deletions(-)

all 11 patches have been applied.

Regards

Marcel