summaryrefslogtreecommitdiff
path: root/system-linux.c
diff options
context:
space:
mode:
authorFlorian Eckert <fe@dev.tdt.de>2022-01-11 15:42:59 +0100
committerHans Dedecker <dedeckeh@gmail.com>2022-01-12 20:28:33 +0100
commit29e6acf3bd34633fa1a3d51559cc1294b772f88e (patch)
treedbff176edc241c750dc845d8ecae21c4d86c7adf /system-linux.c
parent7ccbf08570619a4d3eccf6401791075908bb1d78 (diff)
downloadnetifd-29e6acf3bd34633fa1a3d51559cc1294b772f88e.tar.gz
netifd: add devtype to ubus call
Every network device has a type but there is no standard interface here. The type can be determined either from the file '/sys/class/net/<device>/uevent' or, if no information is found there, from the file '/sys/class/net/<device>/type'. This new function first checks whether there is a DEVTYPE=<type> string in the 'uevent' file and uses it. If it does not find this information, the 'type' is used as a fallback and mapped the number to a character sequence. This new 'devtype' information can be found in the network.device ubus call. Command: ubus call network.device status Output: { "eth0": { "devtype": "ethernet", Signed-off-by: Florian Eckert <fe@dev.tdt.de> Signed-off-by: Hans Dedecker <dedeckeh@gmail.com> [commit rewording]
Diffstat (limited to 'system-linux.c')
-rw-r--r--system-linux.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/system-linux.c b/system-linux.c
index e768853..654f2ac 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -2395,6 +2395,58 @@ system_if_force_external(const char *ifname)
return stat(dev_sysfs_path(ifname, "phy80211"), &s) == 0;
}
+static const char *
+system_netdevtype_name(unsigned short dev_type)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(netdev_types); i++) {
+ if (netdev_types[i].id == dev_type)
+ return netdev_types[i].name;
+ }
+
+ /* the last key is used by default */
+ i = ARRAY_SIZE(netdev_types) - 1;
+
+ return netdev_types[i].name;
+}
+
+static void
+system_add_devtype(struct blob_buf *b, const char *ifname)
+{
+ char buf[100];
+ bool found = false;
+
+ if (!system_get_dev_sysfs("uevent", ifname, buf, sizeof(buf))) {
+ const char *info = "DEVTYPE=";
+ char *context = NULL;
+ const char *line = strtok_r(buf, "\r\n", &context);
+
+ while (line != NULL) {
+ char *index = strstr(line, info);
+
+ if (index != NULL) {
+ blobmsg_add_string(b, "devtype", index + strlen(info));
+ found = true;
+ break;
+ }
+
+ line = strtok_r(NULL, "\r\n", &context);
+ }
+ }
+
+ if (!found) {
+ unsigned short number = 0;
+ const char *name = NULL;
+
+ if (!system_get_dev_sysfs("type", ifname, buf, sizeof(buf))) {
+ number = strtoul(buf, NULL, 0);
+ name = system_netdevtype_name(number);
+ blobmsg_add_string(b, "devtype", name);
+ }
+ }
+}
+
int
system_if_dump_info(struct device *dev, struct blob_buf *b)
{
@@ -2430,6 +2482,8 @@ system_if_dump_info(struct device *dev, struct blob_buf *b)
blobmsg_add_u8(b, "autoneg", !!ecmd.autoneg);
}
+ system_add_devtype(b, dev->ifname);
+
return 0;
}