Skip to contentSkip to conversation

manager: add support for changing 'PreferredTechnologies' over DBus

PATCH In review Alexandru Ardelean 2 participants 3 messages Jul 5, 2024 – Aug 1, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 manager: add support for changing 'PreferredTechnologies' over DBus

This change adds support for reading/modifying the 'PreferredTechnologies' over DBus.
This can be extended to 'DefaultAutoConnectTechnologies', 'DefaultFavoriteTechnologies' & 'AlwaysConnectedTechnologies' as well. However the interest (so far) was in changing the 'PreferredTechnologies' order (at runtime) between cellular and wifi.
Also, 'PreferredTechnologies' was the only that was tested.

Changing 'PreferredTechnologies' seems to yield the desired result, as the switch between cellular/wifi does not need to be super-fast. And it changes the routing table (as desired) when cellular is preferred over WiFi (and vice-versa).

On some boxes that have both cellular & WiFi, it's often desired to prefer cellular (over WiFi) in case WiFi is not able to connect to the internet. The logic (for this switching) can be determined outside of connman. Setting 'PreferredTechnologies' helps in this case.
---

Changelog v1 -> v2:
* v1: https://lore.kernel.org/connman/[email protected]/
* removed 'tools/ip6tables-test'
* added to .gitignore via https://lore.kernel.org/connman/[email protected]/T/#u
* added a bit of documentation in 'src/main.conf' for the 'PreferredTechnologies' property now also being accessible over DBus

 doc/connman.conf.5.in |  2 ++
 doc/manager-api.txt   | 13 +++++++++
 include/setting.h     |  2 ++
 src/main.c            | 22 ++++++++++++++
 src/main.conf         |  4 +++
 src/manager.c         | 67 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 110 insertions(+)
doc/connman.conf.5.in +2 -0
index 3eb240f5..c8ce8eb6 100644
@@ -101,6 +101,8 @@ with state 'ready' or with a non-preferred type; a service
 of a preferred technology type in state 'online' will get
 the default route when compared to either a non-preferred
 type or a preferred type further down in the list.
+This property can be read and set over DBus as well, on
+the 'net.connman.Manager' DBus interface.
 .TP
 .BI NetworkInterfaceBlacklist= interface\fR[,...]
 List of blacklisted network interfaces separated by ",".
doc/manager-api.txt +13 -0
index 6eaa0a38..3be9cffc 100644
@@ -319,3 +319,16 @@ Properties	string State [readonly]
 			and does not affect ConnMan in any way.
 
 			The default value is false.
+
+		array{string} PreferredTechnologies [readwrite] [experminental]
+
+			This property is the same one that is defined in
+			/etc/connman/main.conf. It controls the order of
+			preferred technologies (e.g. wifi, cellular) at
+			runtime.
+			Changing it here will not have an immediate effect.
+			It's only when connman gets triggered via service changes
+			that this gets taken into consideration. Or, when it's
+			triggered via manual connect/disconnect of services.
+
+			The default value is defined in /etc/connman/main.conf.
include/setting.h +2 -0
index 920e6754..6a575dc4 100644
@@ -33,6 +33,8 @@ unsigned int connman_setting_get_uint(const char *key);
 char *connman_setting_get_string(const char *key);
 char **connman_setting_get_string_list(const char *key);
 unsigned int *connman_setting_get_uint_list(const char *key);
+int connman_setting_set_uint_list(const char *key, const unsigned int *lst,
+                                  int len);
 
 unsigned int connman_timeout_input_request(void);
 unsigned int connman_timeout_browser_launch(void);
src/main.c +22 -0
index f5da979b..7858b967 100644
@@ -1122,6 +1122,28 @@ unsigned int *connman_setting_get_uint_list(const char *key)
 	return NULL;
 }
 
+int connman_setting_set_uint_list(const char *key, const unsigned int *lst,
+				  int len)
+{
+	unsigned int *new_list;
+
+	new_list = g_try_new0(unsigned int, len + 1);
+	if (!new_list)
+		return -1;
+
+	memcpy(new_list, lst, sizeof(unsigned int) * len);
+
+	if (g_str_equal(key, CONF_PREFERRED_TECHS)) {
+		g_free(connman_settings.preferred_techs);
+		connman_settings.preferred_techs = new_list;
+		return 0;
+	}
+
+	g_free(new_list);
+
+	return -1;
+}
+
 unsigned int connman_timeout_input_request(void)
 {
 	return connman_settings.timeout_inputreq;
src/main.conf +4 -0
index 5357edb8..80b4fb83 100644
@@ -60,6 +60,10 @@
 # of a preferred technology type in state 'online' will get
 # the default route when compared to either a non-preferred
 # type or a preferred type further down in the list.
+# This property is also configurable via DBus through the
+# 'net.connman.Manager' & SetProperty method.
+# The GetProperties DBus method will list this property
+# and its current value.
 # PreferredTechnologies =
 
 # List of blacklisted network interfaces separated by ",".
src/manager.c +67 -0

91 lines not on this page · raw message

Signature · 1 line
2.45.2
#1 lore raw

Hi Alexandru,

Quoted 8 lines · Alexandru Ardelean

This change adds support for reading/modifying the 'PreferredTechnologies' over DBus.
This can be extended to 'DefaultAutoConnectTechnologies', 'DefaultFavoriteTechnologies' & 'AlwaysConnectedTechnologies' as well. However the interest (so far) was in changing the 'PreferredTechnologies' order (at runtime) between cellular and wifi.
Also, 'PreferredTechnologies' was the only that was tested.

Quoted 5 lines

Changing 'PreferredTechnologies' seems to yield the desired result, as the switch between cellular/wifi does not need to be super-fast. And it changes the routing table (as desired) when cellular is preferred over WiFi (and vice-versa).

Can you elaborate? If setting the property doesn't trigger service re-ordering immediately, how does it happen?

Quoted 21 lines

On some boxes that have both cellular & WiFi, it's often desired to prefer cellular (over WiFi) in case WiFi is not able to connect to the internet. The logic (for this switching) can be determined outside of connman. Setting 'PreferredTechnologies' helps in this case.
---

Changelog v1 -> v2:
* v1: https://lore.kernel.org/connman/[email protected]/
* removed 'tools/ip6tables-test'
* added to .gitignore via https://lore.kernel.org/connman/[email protected]/T/#u
* added a bit of documentation in 'src/main.conf' for the 'PreferredTechnologies' property now also being accessible over DBus

  doc/connman.conf.5.in |  2 ++
  doc/manager-api.txt   | 13 +++++++++
  include/setting.h     |  2 ++
  src/main.c            | 22 ++++++++++++++
  src/main.conf         |  4 +++
  src/manager.c         | 67 +++++++++++++++++++++++++++++++++++++++++++
  6 files changed, 110 insertions(+)

<snip>

doc/manager-api.txt Properties string State [readonly] · line 323

Quoted 10 lines
doc/manager-api.txt +2 -0
index 6eaa0a38..3be9cffc 100644
@@ -319,3 +319,16 @@ Properties	string State [readonly]
  			and does not affect ConnMan in any way.
 
  			The default value is false.
+
+		array{string} PreferredTechnologies [readwrite] [experminental]

Typo 'experminental'

doc/manager-api.txt Properties string State [readonly] · line 334

Quoted 11 lines
doc/manager-api.txt +11 -0
+
+			This property is the same one that is defined in
+			/etc/connman/main.conf. It controls the order of
+			preferred technologies (e.g. wifi, cellular) at
+			runtime.
+			Changing it here will not have an immediate effect.
+			It's only when connman gets triggered via service changes
+			that this gets taken into consideration. Or, when it's
+			triggered via manual connect/disconnect of services.
+
+			The default value is defined in /etc/connman/main.conf.

Have you considered looking into how the Online check is implemented? Perhaps if the interface is not Online, it should not be prioritized ahead of one that is?

Quoted 5 lines
src/main.c +0 -0
index f5da979b..7858b967 100644

src/main.c unsigned int *connman_setting_get_uint_list(const char *key) · line 1130

Quoted 9 lines
src/main.c +6 -0
@@ -1122,6 +1122,28 @@ unsigned int *connman_setting_get_uint_list(const char *key)
  	return NULL;
  }
 
+int connman_setting_set_uint_list(const char *key, const unsigned int *lst,
+				  int len)
+{
+	unsigned int *new_list;
+
+	new_list = g_try_new0(unsigned int, len + 1);

General advice is that for small allocations g_try_new0 is not necessary.

Quoted 19 lines
src/main.c +16 -0
+	if (!new_list)
+		return -1;
+
+	memcpy(new_list, lst, sizeof(unsigned int) * len);
+
+	if (g_str_equal(key, CONF_PREFERRED_TECHS)) {
+		g_free(connman_settings.preferred_techs);
+		connman_settings.preferred_techs = new_list;
+		return 0;
+	}
+
+	g_free(new_list);
+
+	return -1;
+}
+
  unsigned int connman_timeout_input_request(void)
  {
  	return connman_settings.timeout_inputreq;

<snip>

src/manager.c line 40

Quoted 12 lines
src/manager.c +4 -0
index 892d3a42..62f34c50 100644
@@ -34,6 +34,27 @@
  static bool connman_state_idle;
  static dbus_bool_t sessionmode;
 
+static const char *tech_property_names[] = {
+	"PreferredTechnologies",
+	NULL,
+};

How would this be extended in the future?

<snip>

src/manager.c static DBusMessage *get_properties(DBusConnection *conn, · line 93

Quoted 9 lines
src/manager.c +5 -0
@@ -64,6 +86,13 @@ static DBusMessage *get_properties(DBusConnection *conn,
  					DBUS_TYPE_BOOLEAN,
  					&sessionmode);
 
+	for (i = 0; tech_property_names[i]; i++) {
+		const char *name = tech_property_names[i];
+		unsigned int *lst = connman_setting_get_uint_list(name);
+		connman_dbus_dict_append_array(&dict, name, DBUS_TYPE_STRING,
+						append_tech_list, lst);

This logic seems pretty specific to PreferredTechnologies?

Quoted 32 lines
src/manager.c +25 -0
+	}
+
  	connman_dbus_dict_close(&array, &dict);
 
  	return reply;
@@ -110,6 +139,44 @@ static DBusMessage *set_property(DBusConnection *conn,
 
  		dbus_message_iter_get_basic(&value, &sessionmode);
 
+	} else if (g_str_equal(name, "PreferredTechnologies")) {
+		unsigned int techs[MAX_CONNMAN_SERVICE_TYPES] = {};
+		DBusMessageIter entry;
+		int cnt;
+
+		if (type != DBUS_TYPE_ARRAY)
+			return __connman_error_invalid_arguments(msg);
+
+		dbus_message_iter_recurse(&value, &entry);
+
+		cnt = 0;
+		while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
+			enum connman_service_type type;
+			const char *val;
+			dbus_message_iter_get_basic(&entry, &val);
+			dbus_message_iter_next(&entry);
+			int i;
+
+			if (!val[0])
+				continue;
+
+			type = __connman_service_string2type(val);
+			if (type == CONNMAN_SERVICE_TYPE_UNKNOWN)

src/manager.c static DBusMessage *set_property(DBusConnection *conn, · line 174

Quoted 10 lines
src/manager.c +10 -0
+				return __connman_error_invalid_arguments(msg);
+
+			if (cnt >= MAX_CONNMAN_SERVICE_TYPES)
+				return __connman_error_invalid_arguments(msg);
+
+			/* check for duplicates */
+			for (i = 0; i < cnt; i++) {
+				if (type == techs[i])
+					return __connman_error_invalid_arguments(msg);
+			}

There's a few lines > 80 chars. Have you run this through checkpatch?

src/manager.c static DBusMessage *set_property(DBusConnection *conn, · line 182

Quoted 8 lines
src/manager.c +5 -0
+
+			techs[cnt++] = type;
+		}
+
+		connman_setting_set_uint_list(name, techs, cnt);
  	} else
  		return __connman_error_invalid_property(msg);
 

Regards,
-Denis

#2 lore raw
Quoted 12 lines · Denis Kenzior

Hi Alexandru,

Quoted 8 lines · Alexandru Ardelean

This change adds support for reading/modifying the 'PreferredTechnologies' over DBus.
This can be extended to 'DefaultAutoConnectTechnologies', 'DefaultFavoriteTechnologies' & 'AlwaysConnectedTechnologies' as well. However the interest (so far) was in changing the 'PreferredTechnologies' order (at runtime) between cellular and wifi.
Also, 'PreferredTechnologies' was the only that was tested.

Quoted 8 lines
Quoted 5 lines

Changing 'PreferredTechnologies' seems to yield the desired result, as the switch between cellular/wifi does not need to be super-fast. And it changes the routing table (as desired) when cellular is preferred over WiFi (and vice-versa).

Can you elaborate? If setting the property doesn't trigger service re-ordering immediately, how does it happen?

Hello,

Apologies for the slow reply.
I got wrapped up in tons of work.

For the context (in which this was being run), just setting the properties would be sufficient.
There would be various triggering/re-retriggering of services such that cellular would be preferred over WiFi (and vice-versa). (This is valid in our case).

In the meantime (since doing this patch), I also found (in the main branch) the support for secondary gateways, which is actually what we'd want.
In connman (up to 1.42), if a ppp0 interface would come up, it would setup an interface (default) route, which would not allow something like "ping -I wlan0 8.8.8.8"
We've been a implementing a "ping -I ppp0" & "ping -I wlan0" type of logic, to be able to change the primary route (in this case via PreferredTechnologies) in case WiFi is connected, but not able to reach a certain address (via ping).

The secondary gateway stuff, makes this patch not-needed. It's only for connman 1.42 (or older) that this is needed (to be order to prioritize WiFi over GSM/LTE and vice-versa).

Thank you :)
And apologies for the noise
Alex

Quoted 178 lines
Quoted 22 lines

On some boxes that have both cellular & WiFi, it's often desired to prefer cellular (over WiFi) in case WiFi is not able to connect to the internet. The logic (for this switching) can be determined outside of connman. Setting 'PreferredTechnologies' helps in this case.
---

Changelog v1 -> v2:
* v1: https://lore.kernel.org/connman/[email protected]/
* removed 'tools/ip6tables-test'
* added to .gitignore via https://lore.kernel.org/connman/[email protected]/T/#u
* added a bit of documentation in 'src/main.conf' for the 'PreferredTechnologies' property now also being accessible over DBus

  doc/connman.conf.5.in |  2 ++
  doc/manager-api.txt   | 13 +++++++++
  include/setting.h     |  2 ++
  src/main.c            | 22 ++++++++++++++
  src/main.conf         |  4 +++
  src/manager.c         | 67 +++++++++++++++++++++++++++++++++++++++++++
  6 files changed, 110 insertions(+)

<snip>

doc/manager-api.txt Properties string State [readonly] · line 323

Quoted 10 lines
doc/manager-api.txt +2 -0
index 6eaa0a38..3be9cffc 100644
@@ -319,3 +319,16 @@ Properties       string State [readonly]
                      and does not affect ConnMan in any way.
 
                      The default value is false.
+
+             array{string} PreferredTechnologies [readwrite] [experminental]

Typo 'experminental'

doc/manager-api.txt Properties string State [readonly] · line 334

Quoted 11 lines
doc/manager-api.txt +11 -0
+
+                     This property is the same one that is defined in
+                     /etc/connman/main.conf. It controls the order of
+                     preferred technologies (e.g. wifi, cellular) at
+                     runtime.
+                     Changing it here will not have an immediate effect.
+                     It's only when connman gets triggered via service changes
+                     that this gets taken into consideration. Or, when it's
+                     triggered via manual connect/disconnect of services.
+
+                     The default value is defined in /etc/connman/main.conf.

Have you considered looking into how the Online check is implemented? Perhaps if the interface is not Online, it should not be prioritized ahead of one that is?

Quoted 5 lines
src/main.c +0 -0
index f5da979b..7858b967 100644

src/main.c unsigned int *connman_setting_get_uint_list(const char *key) · line 1130

Quoted 9 lines
src/main.c +6 -0
@@ -1122,6 +1122,28 @@ unsigned int *connman_setting_get_uint_list(const char *key)
      return NULL;
  }
 
+int connman_setting_set_uint_list(const char *key, const unsigned int *lst,
+                               int len)
+{
+     unsigned int *new_list;
+
+     new_list = g_try_new0(unsigned int, len + 1);

General advice is that for small allocations g_try_new0 is not necessary.

Quoted 19 lines
src/main.c +16 -0
+     if (!new_list)
+             return -1;
+
+     memcpy(new_list, lst, sizeof(unsigned int) * len);
+
+     if (g_str_equal(key, CONF_PREFERRED_TECHS)) {
+             g_free(connman_settings.preferred_techs);
+             connman_settings.preferred_techs = new_list;
+             return 0;
+     }
+
+     g_free(new_list);
+
+     return -1;
+}
+
  unsigned int connman_timeout_input_request(void)
  {
      return connman_settings.timeout_inputreq;

<snip>

src/manager.c line 40

Quoted 12 lines
src/manager.c +4 -0
index 892d3a42..62f34c50 100644
@@ -34,6 +34,27 @@
  static bool connman_state_idle;
  static dbus_bool_t sessionmode;
 
+static const char *tech_property_names[] = {
+     "PreferredTechnologies",
+     NULL,
+};

How would this be extended in the future?

<snip>

src/manager.c static DBusMessage *get_properties(DBusConnection *conn, · line 93

Quoted 9 lines
src/manager.c +5 -0
@@ -64,6 +86,13 @@ static DBusMessage *get_properties(DBusConnection *conn,
                                      DBUS_TYPE_BOOLEAN,
                                      &sessionmode);
 
+     for (i = 0; tech_property_names[i]; i++) {
+             const char *name = tech_property_names[i];
+             unsigned int *lst = connman_setting_get_uint_list(name);
+             connman_dbus_dict_append_array(&dict, name, DBUS_TYPE_STRING,
+                                             append_tech_list, lst);

This logic seems pretty specific to PreferredTechnologies?

Quoted 32 lines
src/manager.c +25 -0
+     }
+
      connman_dbus_dict_close(&array, &dict);
 
      return reply;
@@ -110,6 +139,44 @@ static DBusMessage *set_property(DBusConnection *conn,
 
              dbus_message_iter_get_basic(&value, &sessionmode);
 
+     } else if (g_str_equal(name, "PreferredTechnologies")) {
+             unsigned int techs[MAX_CONNMAN_SERVICE_TYPES] = {};
+             DBusMessageIter entry;
+             int cnt;
+
+             if (type != DBUS_TYPE_ARRAY)
+                     return __connman_error_invalid_arguments(msg);
+
+             dbus_message_iter_recurse(&value, &entry);
+
+             cnt = 0;
+             while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
+                     enum connman_service_type type;
+                     const char *val;
+                     dbus_message_iter_get_basic(&entry, &val);
+                     dbus_message_iter_next(&entry);
+                     int i;
+
+                     if (!val[0])
+                             continue;
+
+                     type = __connman_service_string2type(val);
+                     if (type == CONNMAN_SERVICE_TYPE_UNKNOWN)

src/manager.c static DBusMessage *set_property(DBusConnection *conn, · line 174

Quoted 10 lines
src/manager.c +10 -0
+                             return __connman_error_invalid_arguments(msg);
+
+                     if (cnt >= MAX_CONNMAN_SERVICE_TYPES)
+                             return __connman_error_invalid_arguments(msg);
+
+                     /* check for duplicates */
+                     for (i = 0; i < cnt; i++) {
+                             if (type == techs[i])
+                                     return __connman_error_invalid_arguments(msg);
+                     }

There's a few lines > 80 chars. Have you run this through checkpatch?

src/manager.c static DBusMessage *set_property(DBusConnection *conn, · line 182

Quoted 8 lines
src/manager.c +5 -0
+
+                     techs[cnt++] = type;
+             }
+
+             connman_setting_set_uint_list(name, techs, cnt);
      } else
              return __connman_error_invalid_property(msg);
 

Regards,
-Denis