From patchwork Tue Jan 11 14:42:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Eckert X-Patchwork-Id: 1578521 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=Q2d86zhb; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JYD8Y67jbz9s1l for ; Wed, 12 Jan 2022 01:48:29 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=Fep9N1CX8ncf5v7uBByUhLeqVvgYC/JExsled+rjxSU=; b=Q2d86zhbRC4m0+ xl3frAZm/Zb8OPIuzphYdQ/oaTFaRir5vZf9DffuBoBmV/i1I1LBKhXfIcUU5BJC8TzhMcglX6Daz mqtqSlNmie6Eyccb6KCRAsHDDnpLCpDRBW+iE54pOjiQyZAD/SGPIhhTnKrQWVCfGEgvmYpEbDoHr fuVNGnBcsMXs17M1qTrl0fwmLbPBE/c9ERIR8OlZc/mEANhxHMQif35PT2mO6nWMlQd/+poA7hSRV iVqEOGKc6WVUt6hPnblcdU+ejQNQsZFqY/xOVkgg83D03JlU0N0a7KJ5SCWVEOFA7LMwtFKlf8o1Y jlnpvVvWJmn+BFAWseLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7IMq-00Gazh-P7; Tue, 11 Jan 2022 14:43:20 +0000 Received: from mxout70.expurgate.net ([91.198.224.70]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1n7IMl-00GaxC-HV for openwrt-devel@lists.openwrt.org; Tue, 11 Jan 2022 14:43:18 +0000 Received: from [127.0.0.1] (helo=localhost) by relay.expurgate.net with smtp (Exim 4.92) (envelope-from ) id 1n7IMi-0005ml-5o; Tue, 11 Jan 2022 15:43:12 +0100 Received: from [195.243.126.94] (helo=securemail.tdt.de) by relay.expurgate.net with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7IMh-0009XL-Bg; Tue, 11 Jan 2022 15:43:11 +0100 Received: from securemail.tdt.de (localhost [127.0.0.1]) by securemail.tdt.de (Postfix) with ESMTP id 1C17824004B; Tue, 11 Jan 2022 15:43:11 +0100 (CET) Received: from mail.dev.tdt.de (unknown [10.2.4.42]) by securemail.tdt.de (Postfix) with ESMTP id AD6F6240049; Tue, 11 Jan 2022 15:43:10 +0100 (CET) Received: from localhost.localdomain (unknown [10.2.3.40]) by mail.dev.tdt.de (Postfix) with ESMTPSA id A294120AFF; Tue, 11 Jan 2022 15:43:05 +0100 (CET) From: Florian Eckert To: jo@mein.io, dedeckeh@gmail.com Cc: Eckert.Florian@googlemail.com, openwrt-devel@lists.openwrt.org Subject: [PATCH v4 1/1] netifd: add devtype to ubus call Date: Tue, 11 Jan 2022 15:42:59 +0100 Message-ID: <20220111144259.29637-1-fe@dev.tdt.de> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED, URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.dev.tdt.de X-purgate: clean X-purgate-ID: 151534::1641912191-00000DA7-38B0F2F7/0/0 X-purgate-type: clean X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220111_064315_763028_74B9AECA X-CRM114-Status: GOOD ( 17.19 ) X-Spam-Score: -0.7 (/) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Every network device has a type. There is no standard interface here. The type can be determined either from the file '/sys/class/net//uevent' or, if no information is found there, from the fi [...] Content analysis details: (-0.7 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [91.198.224.70 listed in list.dnswl.org] 0.0 SPF_NONE SPF: sender does not publish an SPF Record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Every network device has a type. There is no standard interface here. The type can be determined either from the file '/sys/class/net//uevent' or, if no information is found there, from the file '/sys/class/net//type'. This new function first checks whether there is a DEVTYPE= sring 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 --- v2: - Remove debug log output v3: - Use the information mainly from file 'uevent'. - If 'uevent' does not provide the information use file 'type' v4: - Fix coding style - Merge arrays system-linux.c | 47 ++++++++++++++++++++++++++++++++++++ system.h | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/system-linux.c b/system-linux.c index e768853..e44058e 100644 --- a/system-linux.c +++ b/system-linux.c @@ -2395,6 +2395,51 @@ 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) +{ + 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 +2475,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; } diff --git a/system.h b/system.h index 1f7037d..40ef2fe 100644 --- a/system.h +++ b/system.h @@ -23,6 +23,71 @@ #include "iprule.h" #include "utils.h" +struct netdev_type { + unsigned short id; + const char *name; +}; + +static const struct netdev_type netdev_types[] = { + {ARPHRD_NETROM, "netrom"}, + {ARPHRD_ETHER, "ethernet"}, + {ARPHRD_EETHER, "eethernet"}, + {ARPHRD_AX25, "ax25"}, + {ARPHRD_PRONET, "pronet"}, + {ARPHRD_CHAOS, "chaos"}, + {ARPHRD_IEEE802, "ieee802"}, + {ARPHRD_ARCNET, "arcnet"}, + {ARPHRD_APPLETLK, "appletlk"}, + {ARPHRD_DLCI, "dlci"}, + {ARPHRD_ATM, "atm"}, + {ARPHRD_METRICOM, "metricom"}, + {ARPHRD_IEEE1394, "ieee1394"}, + {ARPHRD_EUI64, "eui64"}, + {ARPHRD_INFINIBAND, "infiniband"}, + {ARPHRD_SLIP, "slip"}, + {ARPHRD_CSLIP, "cslip"}, + {ARPHRD_SLIP6, "slip6"}, + {ARPHRD_CSLIP6, "cslip6"}, + {ARPHRD_RSRVD, "rsrvd"}, + {ARPHRD_ADAPT, "adapt"}, + {ARPHRD_ROSE, "rose"}, + {ARPHRD_X25, "x25"}, + {ARPHRD_HWX25, "hwx25"}, + {ARPHRD_PPP, "ppp"}, + {ARPHRD_CISCO, "cisco"}, + {ARPHRD_LAPB, "lapb"}, + {ARPHRD_DDCMP, "ddcmp"}, + {ARPHRD_RAWHDLC, "rawhdlc"}, + {ARPHRD_TUNNEL, "tunnel"}, + {ARPHRD_TUNNEL6, "tunnel6"}, + {ARPHRD_FRAD, "frad"}, + {ARPHRD_SKIP, "skip"}, + {ARPHRD_LOOPBACK, "loopback"}, + {ARPHRD_LOCALTLK, "localtlk"}, + {ARPHRD_FDDI, "fddi"}, + {ARPHRD_BIF, "bif"}, + {ARPHRD_SIT, "sit"}, + {ARPHRD_IPDDP, "ipddp"}, + {ARPHRD_IPGRE, "ipgre"}, + {ARPHRD_PIMREG,"pimreg"}, + {ARPHRD_HIPPI, "hippi"}, + {ARPHRD_ASH, "ash"}, + {ARPHRD_ECONET, "econet"}, + {ARPHRD_IRDA, "irda"}, + {ARPHRD_FCPP, "fcpp"}, + {ARPHRD_FCAL, "fcal"}, + {ARPHRD_FCPL, "fcpl"}, + {ARPHRD_FCFABRIC, "fcfabric"}, + {ARPHRD_IEEE80211, "ieee80211"}, + {ARPHRD_IEEE80211_PRISM, "ie80211-prism"}, + {ARPHRD_IEEE80211_RADIOTAP, "ieee80211-radiotap"}, + {ARPHRD_PHONET, "phonet"}, + {ARPHRD_PHONET_PIPE, "phonet-pipe"}, + {ARPHRD_IEEE802154, "ieee802154"}, + {ARPHRD_VOID, "void"}, + {ARPHRD_NONE, "none"} +}; + enum tunnel_param { TUNNEL_ATTR_TYPE, TUNNEL_ATTR_REMOTE,