Skip to contentSkip to conversation

udevng: Track udev devices through devpath and not syspath

PATCH In review Pau Espin Pedrol 2 participants 4 messages Apr 1, 2019 – Apr 3, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 udevng: Track udev devices through devpath and not syspath

syspath is not really needed in there, and catching devpath instead allows us to easily match against DEVPATH_OLD in follow-up patches. Furthermore, previous code was misleading retrieving syspath in remove_device() and then passing it as devpath param of check_remove().

---
 plugins/udevng.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
plugins/udevng.c +6 -6
index f689b756..c518553a 100644
@@ -1427,16 +1427,16 @@ static gboolean check_remove(gpointer key, gpointer value, gpointer user_data)
 
 static void remove_device(struct udev_device *device)
 {
-	const char *syspath;
+	const char *devpath;
 
-	syspath = udev_device_get_syspath(device);
-	if (syspath == NULL)
+	devpath = udev_device_get_devpath(device);
+	if (devpath == NULL)
 		return;
 
-	DBG("%s", syspath);
+	DBG("%s", devpath);
 
 	g_hash_table_foreach_remove(modem_list, check_remove,
-						(char *) syspath);
+						(char *) devpath);
 }
 
 static gint compare_device(gconstpointer a, gconstpointer b)
@@ -1550,7 +1550,7 @@ static void add_device(const char *syspath, const char *devname,
 	struct device_info *info;
 	struct udev_device *parent;
 
-	devpath = udev_device_get_syspath(device);
+	devpath = udev_device_get_devpath(device);
 	if (devpath == NULL)
 		return;
 
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/2 udevng: Catch network interface renames

If for whatever reason a network interface belonging to a modem is renamed after ofono startup, ofono then keeps advertising the old non-existing interface name instead of the new one.
To catch the changes we use udev's "move" action together with DEVPATH_OLD variable, which points to the old devpath of the device.

Unfortunately current debian9 systemd version contains systemd bug which prevents this feature to work fine if the iface is renamed more than once, see [1] for more information.

[1] https://github.com/systemd/systemd/issues/9426

---
 plugins/udevng.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 83 insertions(+), 1 deletion(-)
plugins/udevng.c +83 -1
index c518553a..8338927b 100644
@@ -56,6 +56,7 @@ struct modem_info {
 	};
 	struct ofono_modem *modem;
 	const char *sysattr;
+	gboolean needs_update;
 };
 
 struct device_info {
@@ -1630,6 +1631,7 @@ static void add_device(const char *syspath, const char *devname,
 
 	modem->devices = g_slist_insert_sorted(modem->devices, info,
 							compare_device);
+	modem->needs_update = TRUE;
 }
 
 static struct {
@@ -1822,6 +1824,8 @@ static gboolean create_modem(gpointer key, gpointer value, gpointer user_data)
 	const char *syspath = key;
 	unsigned int i;
 
+	modem->needs_update = FALSE;
+
 	if (modem->modem != NULL)
 		return FALSE;
 
@@ -1909,6 +1913,81 @@ static gboolean check_modem_list(gpointer user_data)
 	return FALSE;
 }
 
+static void drop_old_device(gpointer key, gpointer value, gpointer user_data)
+{
+	const char *syspath = key;
+	struct modem_info *modem = value;
+	struct udev_device *device = user_data;
+	const char *devpath_old;
+	GSList *list;
+
+	devpath_old = udev_device_get_property_value(device, "DEVPATH_OLD");
+	if (!devpath_old)
+		return;
+
+	for (list = modem->devices; list; list = list->next) {
+		struct device_info *info = list->data;
+		struct udev_device *dev;
+
+		if (g_strcmp0(info->subsystem, "net") != 0)
+			continue;
+
+		if (g_strcmp0(info->devpath, devpath_old) != 0)
+			continue;
+
+		/* Drop no longer existing net iface device */
+		DBG("Dropping lost device %s", info->devpath);
+		modem->devices = g_slist_remove_link(modem->devices, list);
+		g_slist_free_1 (list);
+		device_info_free(info);
+		udev_device_unref(dev);
+		modem->needs_update = TRUE;
+	}
+}
+
+
+static void update_modem(gpointer key, gpointer value, gpointer user_data)
+{
+	const char *syspath = key;
+	struct modem_info *modem = value;
+	unsigned int i;
+
+	if (!modem->needs_update)
+		return;
+
+	DBG("Updating modem %s", syspath);
+
+	for (i = 0; driver_list[i].name; i++) {
+		if (g_str_equal(driver_list[i].name, modem->driver) == FALSE)
+			continue;
+		if (driver_list[i].setup(modem) == FALSE)
+			continue;
+	}
+}
+static void update_device(struct udev_device *device)
+{
+	const char *syspath;
+
+	syspath = udev_device_get_syspath(device);
+	if (syspath == NULL)
+		return;
+
+	DBG("%s", syspath);
+
+	/* We only care about net iface renames for now */
+	if (g_strcmp0(udev_device_get_subsystem(device), "net") != 0)
+		return;
+
+	/* Drop old device from modem */
+	g_hash_table_foreach(modem_list, drop_old_device, device);
+
+	/* Add new device to modem */
+	check_device(device);
+
+	/* Update modified modems */
+	g_hash_table_foreach(modem_list, update_modem, NULL);
+}
+
 static gboolean udev_event(GIOChannel *channel, GIOCondition cond,
 							gpointer user_data)
 {
@@ -1936,8 +2015,11 @@ static gboolean udev_event(GIOChannel *channel, GIOCondition cond,
 		check_device(device);
 
 		udev_delay = g_timeout_add_seconds(1, check_modem_list, NULL);
-	} else if (g_str_equal(action, "remove") == TRUE)
+	} else if (g_str_equal(action, "remove") == TRUE) {
 		remove_device(device);
+	} else if (g_str_equal(action, "move") == TRUE) {
+		update_device(device);
+	}
 
 	udev_device_unref(device);
 
Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH 2/2] udevng: Catch network interface renames

Hi Pau,

Quoted 62 lines

If for whatever reason a network interface belonging to a modem is renamed after ofono startup, ofono then keeps advertising the old non-existing interface name instead of the new one.
To catch the changes we use udev's "move" action together with DEVPATH_OLD variable, which points to the old devpath of the device.

Unfortunately current debian9 systemd version contains systemd bug which prevents this feature to work fine if the iface is renamed more than once, see [1] for more information.

[1] https://github.com/systemd/systemd/issues/9426
---
plugins/udevng.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++-

1 file changed, 83 insertions(+), 1 deletion(-)
plugins/udevng.c +18 -0
index c518553a..8338927b 100644
@@ -56,6 +56,7 @@ struct modem_info {
 	};
 	struct ofono_modem *modem;
 	const char *sysattr;
+	gboolean needs_update;
 };
 
 struct device_info {
@@ -1630,6 +1631,7 @@ static void add_device(const char *syspath, const char *devname,
 
 	modem->devices = g_slist_insert_sorted(modem->devices, info,
 							compare_device);
+	modem->needs_update = TRUE;
 }
 
 static struct {
@@ -1822,6 +1824,8 @@ static gboolean create_modem(gpointer key, gpointer value, gpointer user_data)
 	const char *syspath = key;
 	unsigned int i;
 
+	modem->needs_update = FALSE;
+
 	if (modem->modem != NULL)
 		return FALSE;
 
@@ -1909,6 +1913,81 @@ static gboolean check_modem_list(gpointer user_data)
 	return FALSE;
 }
 
+static void drop_old_device(gpointer key, gpointer value, gpointer user_data)
+{
+	const char *syspath = key;
+	struct modem_info *modem = value;
+	struct udev_device *device = user_data;
+	const char *devpath_old;
+	GSList *list;
+
+	devpath_old = udev_device_get_property_value(device, "DEVPATH_OLD");
+	if (!devpath_old)
+		return;
+
+	for (list = modem->devices; list; list = list->next) {
+		struct device_info *info = list->data;

plugins/udevng.c static gboolean check_modem_list(gpointer user_data) · line 1939

Quoted 10 lines
plugins/udevng.c +10 -0
+		struct udev_device *dev;
+
+		if (g_strcmp0(info->subsystem, "net") != 0)
+			continue;
+
+		if (g_strcmp0(info->devpath, devpath_old) != 0)
+			continue;
+
+		/* Drop no longer existing net iface device */
+		DBG("Dropping lost device %s", info->devpath);

I really prefer that DBG is surrounded by empty lines.

plugins/udevng.c static gboolean check_modem_list(gpointer user_data) · line 1941

Quoted 2 lines
plugins/udevng.c +2 -0
+		modem->devices = g_slist_remove_link(modem->devices, list);
+		g_slist_free_1 (list);

No space between 1 and ( here.

Quoted 54 lines
plugins/udevng.c +49 -0
+		device_info_free(info);
+		udev_device_unref(dev);
+		modem->needs_update = TRUE;
+	}
+}
+
+
+static void update_modem(gpointer key, gpointer value, gpointer user_data)
+{
+	const char *syspath = key;
+	struct modem_info *modem = value;
+	unsigned int i;
+
+	if (!modem->needs_update)
+		return;
+
+	DBG("Updating modem %s", syspath);
+
+	for (i = 0; driver_list[i].name; i++) {
+		if (g_str_equal(driver_list[i].name, modem->driver) == FALSE)
+			continue;
+		if (driver_list[i].setup(modem) == FALSE)
+			continue;
+	}
+}
+static void update_device(struct udev_device *device)
+{
+	const char *syspath;
+
+	syspath = udev_device_get_syspath(device);
+	if (syspath == NULL)
+		return;
+
+	DBG("%s", syspath);
+
+	/* We only care about net iface renames for now */
+	if (g_strcmp0(udev_device_get_subsystem(device), "net") != 0)
+		return;
+
+	/* Drop old device from modem */
+	g_hash_table_foreach(modem_list, drop_old_device, device);
+
+	/* Add new device to modem */
+	check_device(device);
+
+	/* Update modified modems */
+	g_hash_table_foreach(modem_list, update_modem, NULL);
+}
+
 static gboolean udev_event(GIOChannel *channel, GIOCondition cond,
 							gpointer user_data)
 {
@@ -1936,8 +2015,11 @@ static gboolean udev_event(GIOChannel *channel, GIOCondition cond,
 		check_device(device);

plugins/udevng.c static gboolean udev_event(GIOChannel *channel, GIOCondition cond, · line 2024

Quoted 10 lines
plugins/udevng.c +4 -1
 
 		udev_delay = g_timeout_add_seconds(1, check_modem_list, NULL);
-	} else if (g_str_equal(action, "remove") == TRUE)
+	} else if (g_str_equal(action, "remove") == TRUE) {
 		remove_device(device);
+	} else if (g_str_equal(action, "move") == TRUE) {
+		update_device(device);
+	}
 
 	udev_device_unref(device);

Wouldn’t it generally be better for reading the name after oFono has brought up the interface. In case we need this for the D-Bus API somehow. And for sake of matching we start and handing this to the plugins and drivers we should start using the ifindex.

Regards

Marcel

#3 lore raw

Re: [PATCH 2/2] udevng: Catch network interface renames

Hi,

Quoted 3 lines · Marcel Holtmann

Wouldn’t it generally be better for reading the name after oFono has brought up the interface. In case we need this for the D-Bus API somehow. And for sake of matching we start and handing this to the plugins and drivers we should start using the ifindex.

I'm sorry but I didn't fully understand you here. Can you rephrase/extend your thoughts? We need it for the D-Bus API, it is announced to dbus clients when activating a PDP Context tgether with other IP information in order to let the clients know which iface to use for it.

== General scenario for the patch:
You have a modem and it has iface name wwan0. You decide you want to rename it manually:
ip link set wwan0 down && ip link set wwan0 name mymodem && ip link set wwan0 up
Currently ofono is unable to catch this change and will continue to advertise "wwan0" on the DBUs API.

== Our specific scenario:
As you may have heard already before, at Osmocom [1] we use ofono in order to operate QMI modems (EC20, SierraW, gobi2000, etc.) to test our core networks on a wired HW setup, all orchestrated by osmo-gsm-tester [2][3].

In order to provide correct routing (avoid loops) from different modems to different GGSNs on the same machine, we move modem network interfaces to their own network namespace. In order to do that, we required adding a small hack-patch [4] into our own ofono branch [5] to prevent ofono from dropping the whole modem when the net iface disappears (because udev is unable to track different net namespaces than default, which means from udev point of view the iface is not there).

Furthermore, we experience sporadic crashes in the modem firmware while executing tests, which from linux system point of view means the interface is removed and re-appears. When it is re-registered with the kernel, it may give a new different iface name than the previous one. That also means the ifindex of this new interface is not the same it used to be before. Since due to our patch we didn't drop the modem during netif removed (moved to different netns), the modem is kept alive during crash but still points to old net iface name. More information about this scenario can be found in [6].

I'm so far fixing it using persistent naming in our net ifaces by means of udev rules.

[1] https://osmocom.org/
[2] https://osmocom.org/projects/osmo-gsm-tester
[3] https://git.osmocom.org/osmo-gsm-tester/
[4]
https://git.sysmocom.de/ofono/commit/?h=osmo-gsm-tester&id=e77b82a1bd1986aab42a119e0556c5941795992e
[5] https://git.sysmocom.de/ofono/log/?h=osmo-gsm-tester
[6] https://osmocom.org/issues/3881

Signature · 7 lines
- Pau Espin Pedrol <[email protected]>         http://www.sysmocom.de/
=======================================================================
* sysmocom - systems for mobile communications GmbH
* Alt-Moabit 93
* 10559 Berlin, Germany
* Sitz / Registered office: Berlin, HRB 134158 B
* Geschaeftsfuehrer / Managing Director: Harald Welte