Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 22 May 2019 05:37:30 +0000 (UTC)
From:      Dmitry Chagin <dchagin@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r348096 - stable/12/sys/compat/linsysfs
Message-ID:  <201905220537.x4M5bUxR000263@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dchagin
Date: Wed May 22 05:37:29 2019
New Revision: 348096
URL: https://svnweb.freebsd.org/changeset/base/348096

Log:
  MFC r347204:
  
  Adds sys/class/net devices to linsysfs.
  
  Only two interfaces are created eth0 and lo and they expose
  the following properties:
  address, addr_len, flags, ifindex, mty, tx_queue_len and type.
  
  Initial patch developed by Carlos Neira in 2017 and finished by me.
  
  MFC r347218:
  
  Remove wrong copyright line. Discussed with Carlos Neira.

Modified:
  stable/12/sys/compat/linsysfs/linsysfs.c
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/compat/linsysfs/linsysfs.c
==============================================================================
--- stable/12/sys/compat/linsysfs/linsysfs.c	Wed May 22 05:35:35 2019	(r348095)
+++ stable/12/sys/compat/linsysfs/linsysfs.c	Wed May 22 05:37:29 2019	(r348096)
@@ -31,6 +31,7 @@ __FBSDID("$FreeBSD$");
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/ctype.h>
 #include <sys/kernel.h>
 #include <sys/malloc.h>
 #include <sys/mount.h>
@@ -44,7 +45,11 @@ __FBSDID("$FreeBSD$");
 #include <dev/pci/pcireg.h>
 
 #include <net/if.h>
+#include <net/if_var.h>
+#include <net/if_dl.h>
 
+#include <compat/linux/linux.h>
+#include <compat/linux/linux_common.h>
 #include <compat/linux/linux_util.h>
 #include <fs/pseudofs/pseudofs.h>
 
@@ -64,6 +69,146 @@ atoi(const char *str)
 	return (int)strtol(str, (char **)NULL, 10);
 }
 
+static int
+linsysfs_ifnet_addr(PFS_FILL_ARGS)
+{
+	struct l_sockaddr lsa;
+	struct ifnet *ifp;
+
+	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
+	if (ifp == NULL)
+		return (ENOENT);
+	if (linux_ifhwaddr(ifp, &lsa) != 0)
+		return (ENOENT);
+	sbuf_printf(sb, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
+	    lsa.sa_data[0], lsa.sa_data[1], lsa.sa_data[2],
+	    lsa.sa_data[3], lsa.sa_data[4], lsa.sa_data[5]);
+	return (0);
+}
+
+static int
+linsysfs_ifnet_addrlen(PFS_FILL_ARGS)
+{
+
+	sbuf_printf(sb, "%d\n", LINUX_IFHWADDRLEN);
+	return (0);
+}
+
+static int
+linsysfs_ifnet_flags(PFS_FILL_ARGS)
+{
+	struct ifnet *ifp;
+	unsigned short flags;
+
+	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
+	if (ifp == NULL)
+		return (ENOENT);
+	linux_ifflags(ifp, &flags);
+	sbuf_printf(sb, "0x%x\n", flags);
+	return (0);
+}
+
+static int
+linsysfs_ifnet_ifindex(PFS_FILL_ARGS)
+{
+	struct ifnet *ifp;
+
+	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
+	if (ifp == NULL)
+		return (ENOENT);
+	sbuf_printf(sb, "%u\n", ifp->if_index);
+	return (0);
+}
+
+static int
+linsysfs_ifnet_mtu(PFS_FILL_ARGS)
+{
+	struct ifnet *ifp;
+
+	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
+	if (ifp == NULL)
+		return (ENOENT);
+	sbuf_printf(sb, "%u\n", ifp->if_mtu);
+	return (0);
+}
+
+static int
+linsysfs_ifnet_tx_queue_len(PFS_FILL_ARGS)
+{
+
+	/* XXX */
+	sbuf_printf(sb, "1000\n");
+	return (0);
+}
+
+static int
+linsysfs_ifnet_type(PFS_FILL_ARGS)
+{
+	struct l_sockaddr lsa;
+	struct ifnet *ifp;
+
+	ifp = ifname_linux_to_bsd(td, pn->pn_parent->pn_name, NULL);
+	if (ifp == NULL)
+		return (ENOENT);
+	if (linux_ifhwaddr(ifp, &lsa) != 0)
+		return (ENOENT);
+	sbuf_printf(sb, "%d\n", lsa.sa_family);
+	return (0);
+}
+
+static void
+linsysfs_listnics(struct pfs_node *dir)
+{
+	struct pfs_node *nic;
+	struct pfs_node *lo;
+
+	nic = pfs_create_dir(dir, "eth0", NULL, NULL, NULL, 0);
+
+	pfs_create_file(nic, "address", &linsysfs_ifnet_addr,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "addr_len", &linsysfs_ifnet_addrlen,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "flags", &linsysfs_ifnet_flags,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "ifindex", &linsysfs_ifnet_ifindex,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "mtu", &linsysfs_ifnet_mtu,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(nic, "type", &linsysfs_ifnet_type,
+	    NULL, NULL, NULL, PFS_RD);
+
+	lo = pfs_create_dir(dir, "lo", NULL, NULL, NULL, 0);
+
+	pfs_create_file(lo, "address", &linsysfs_ifnet_addr,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "addr_len", &linsysfs_ifnet_addrlen,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "flags", &linsysfs_ifnet_flags,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "ifindex", &linsysfs_ifnet_ifindex,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "mtu", &linsysfs_ifnet_mtu,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "tx_queue_len", &linsysfs_ifnet_tx_queue_len,
+	    NULL, NULL, NULL, PFS_RD);
+
+	pfs_create_file(lo, "type", &linsysfs_ifnet_type,
+	    NULL, NULL, NULL, PFS_RD);
+}
+
 /*
  * Filler function for proc_name
  */
@@ -475,6 +620,7 @@ linsysfs_init(PFS_INIT_ARGS)
 	struct pfs_node *drm;
 	struct pfs_node *pci;
 	struct pfs_node *scsi;
+	struct pfs_node *net;
 	struct pfs_node *devdir, *chardev;
 	devclass_t devclass;
 	device_t dev;
@@ -488,6 +634,9 @@ linsysfs_init(PFS_INIT_ARGS)
 	scsi = pfs_create_dir(class, "scsi_host", NULL, NULL, NULL, 0);
 	drm = pfs_create_dir(class, "drm", NULL, NULL, NULL, 0);
 
+	/* /sys/class/net/.. */
+	net = pfs_create_dir(class, "net", NULL, NULL, NULL, 0);
+
 	/* /sys/dev/... */
 	devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0);
 	chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0);
@@ -514,6 +663,7 @@ linsysfs_init(PFS_INIT_ARGS)
 	    NULL, NULL, NULL, PFS_RD);
 
 	linsysfs_listcpus(cpu);
+	linsysfs_listnics(net);
 
 	return (0);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201905220537.x4M5bUxR000263>