From b7b294266781089242e24ee449539f0635e9f7c3 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 2 Sep 2024 20:26:54 +0200 Subject: [PATCH] device: add more debugging code Helps figure out corner cases Signed-off-by: Felix Fietkau --- device.c | 39 +++++++++++++++++++++++++++++++++++---- device.h | 21 +++++++++++++++++++-- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/device.c b/device.c index 322e009..32e42a6 100644 --- a/device.c +++ b/device.c @@ -617,11 +617,15 @@ static int device_broadcast_cb(void *ctx, struct safe_list *list) return 0; } -void device_broadcast_event(struct device *dev, enum device_event ev) +const char *device_event_name(enum device_event ev) { static const char * const event_names[] = { [DEV_EVENT_ADD] = "add", [DEV_EVENT_REMOVE] = "remove", + [DEV_EVENT_UPDATE_IFNAME] = "update_ifname", + [DEV_EVENT_UPDATE_IFINDEX] = "update_ifindex", + [DEV_EVENT_SETUP] = "setup", + [DEV_EVENT_TEARDOWN] = "teardown", [DEV_EVENT_UP] = "up", [DEV_EVENT_DOWN] = "down", [DEV_EVENT_AUTH_UP] = "auth_up", @@ -629,12 +633,37 @@ void device_broadcast_event(struct device *dev, enum device_event ev) [DEV_EVENT_LINK_DOWN] = "link_down", [DEV_EVENT_TOPO_CHANGE] = "topo_change", }; + + if (ev >= ARRAY_SIZE(event_names) || !event_names[ev]) + return "unknown"; + + return event_names[ev]; +} + +void __device_broadcast_event(struct device *dev, enum device_event ev) +{ + const char *ev_name; int dev_ev = ev; safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev); safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev); - if (ev >= ARRAY_SIZE(event_names) || !event_names[ev] || !dev->ifname[0]) + switch (ev) { + case DEV_EVENT_ADD: + case DEV_EVENT_REMOVE: + case DEV_EVENT_UP: + case DEV_EVENT_DOWN: + case DEV_EVENT_AUTH_UP: + case DEV_EVENT_LINK_UP: + case DEV_EVENT_LINK_DOWN: + case DEV_EVENT_TOPO_CHANGE: + break; + default: + return; + } + + ev_name = device_event_name(ev); + if (!dev->ifname[0]) return; blob_buf_init(&b, 0); @@ -643,7 +672,7 @@ void device_broadcast_event(struct device *dev, enum device_event ev) blobmsg_add_u8(&b, "present", dev->present); blobmsg_add_u8(&b, "active", dev->active); blobmsg_add_u8(&b, "link_active", dev->link_active); - netifd_ubus_device_notify(event_names[ev], b.head, -1); + netifd_ubus_device_notify(ev_name, b.head, -1); } static void @@ -910,6 +939,8 @@ device_refresh_present(struct device *dev) if (dev->disabled || dev->deferred) state = false; + D(DEVICE, "refresh device %s present: sys=%d disabled=%d deferred=%d\n", + dev->ifname, dev->sys_present, dev->disabled, dev->deferred); __device_set_present(dev, state, false); } @@ -943,7 +974,7 @@ device_set_auth_status(struct device *dev, bool value, struct blob_attr *vlans) device_broadcast_event(dev, DEV_EVENT_LINK_UP); } -void device_set_present(struct device *dev, bool state) +void _device_set_present(struct device *dev, bool state) { if (dev->sys_present == state) return; diff --git a/device.h b/device.h index b2ea0fa..8e0cc0f 100644 --- a/device.h +++ b/device.h @@ -365,9 +365,26 @@ static inline struct device *device_get(const char *name, int create) void device_add_user(struct device_user *dep, struct device *dev); void device_remove_user(struct device_user *dep); -void device_broadcast_event(struct device *dev, enum device_event ev); +const char *device_event_name(enum device_event ev); +void __device_broadcast_event(struct device *dev, enum device_event ev); +#define device_broadcast_event(dev, ev) do { \ + struct device *__ev_dev = (dev); \ + D(DEVICE, "%s: event (%s)\n", \ + (__ev_dev && __ev_dev->ifname[0] ? __ev_dev->ifname : "(none)"), \ + device_event_name(ev)); \ + __device_broadcast_event(__ev_dev, ev); \ +} while (0) + +void _device_set_present(struct device *dev, bool state); +#define device_set_present(dev, state) do { \ + struct device *__ev_dev = (dev); \ + bool __ev_state = state; \ + D(DEVICE, "%s: set present=%d\n", \ + (__ev_dev && __ev_dev->ifname[0] ? __ev_dev->ifname : "(none)"), \ + __ev_state); \ + _device_set_present(__ev_dev, __ev_state); \ +} while (0) -void device_set_present(struct device *dev, bool state); void device_set_link(struct device *dev, bool state); void device_set_ifindex(struct device *dev, int ifindex); int device_set_ifname(struct device *dev, const char *name); -- 2.30.2