Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 14 Aug 2009 17:31:10 -0700
From:      Xin LI <delphij@delphij.net>
To:        Brooks Davis <brooks@freebsd.org>
Cc:        "Li, Qing" <qing.li@bluecoat.com>, d@delphij.net, freebsd-net@freebsd.org, Qing Li <qingli@freebsd.org>, "Andrey V. Elsukov" <bu7cher@yandex.ru>, Julian Elischer <julian@elischer.org>, "Bjoern A. Zeeb" <bzeeb-lists@lists.zabbadoz.net>
Subject:   [Take 2] Re: RFC: interface description
Message-ID:  <4A8601CE.5030205@delphij.net>
In-Reply-To: <20090814193303.GA21941@lor.one-eyed-alien.net>
References:  <4A83EEA8.5080202@delphij.net> <20090813182918.S93661@maildrop.int.zabbadoz.net> <B583FBF374231F4A89607B4D08578A430503351D@bcs-mail03.internal.cacheflow.com> <20090814193303.GA21941@lor.one-eyed-alien.net>

next in thread | previous in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.
--------------070600020208080409030004
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi, guys,

Here is a patch that implements the functionality in userland (as
setifdescription/getifdescription functions in libutil); with this I
think we can also provide an option that some kernel feature (like Qing
Li said it might be useful for embedded systems, but of course the
kernel feature would require more careful design) being used without
modifying programs.  This version uses if_dname plus if_dunit as
distinguished name, and a Berkeley DB (hash, /etc/ifdescr.db) to store
the information.

In order to obtain if_dname and if_duint I had to create a new ioctl, as
we don't seem to expose these two fields in an KBI-stable manner in the
past.  I have not took a look at bsnmp yet but I'll take a look at it to
see if we have some better ways to distinguish the interface name.

Cheers,
- --
Xin LI <delphij@delphij.net>	http://www.delphij.net/
FreeBSD - The Power to Serve!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (FreeBSD)

iEYEARECAAYFAkqGAc0ACgkQi+vbBBjt66D82QCeMV3G+2FepN5asaxvAwpc0qKS
1poAoIhQ719JdYPE7sq+lseTtszr++o0
=ph72
-----END PGP SIGNATURE-----

--------------070600020208080409030004
Content-Type: text/plain;
 name="ifdescr-userland.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ifdescr-userland.diff"

Index: sbin/ifconfig/ifconfig.c
===================================================================
--- sbin/ifconfig/ifconfig.c	(revision 196234)
+++ sbin/ifconfig/ifconfig.c	(working copy)
@@ -63,6 +63,7 @@ static const char rcsid[] =
 #include <netdb.h>
 
 #include <ifaddrs.h>
+#include <if_description.h>
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
@@ -822,6 +823,24 @@ setifname(const char *val, int dummy __unused, int
 	free(newname);
 }
 
+/* ARGSUSED */
+static void
+setifdescr(const char *val, int dummy __unused, int s, 
+    const struct afswtch *afp __unused)
+{
+
+	setifdescription(s, &ifr, val);
+}
+
+/* ARGSUSED */
+static void
+unsetifdescr(const char *val __unused, int dummy __unused, int s, 
+    const struct afswtch *afp __unused)
+{
+
+	setifdescription(s, &ifr, NULL);
+}
+
 #define	IFFBITS \
 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \
 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
@@ -843,6 +862,7 @@ status(const struct afswtch *afp, const struct soc
 	struct ifaddrs *ift;
 	int allfamilies, s;
 	struct ifstat ifs;
+	char *description = NULL;
 
 	if (afp == NULL) {
 		allfamilies = 1;
@@ -866,6 +886,11 @@ status(const struct afswtch *afp, const struct soc
 		printf(" mtu %d", ifr.ifr_mtu);
 	putchar('\n');
 
+	if (getifdescription(s, &ifr, &description) == 0 && description != NULL)
+		printf("\tdescription: %s\n", description);
+	free(description);
+	description = NULL;
+
 	if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) {
 		if (ifr.ifr_curcap != 0) {
 			printb("\toptions", ifr.ifr_curcap, IFCAPBITS);
@@ -1035,6 +1060,10 @@ static struct cmd basic_cmds[] = {
 	DEF_CMD("-arp",		IFF_NOARP,	setifflags),
 	DEF_CMD("debug",	IFF_DEBUG,	setifflags),
 	DEF_CMD("-debug",	-IFF_DEBUG,	setifflags),
+	DEF_CMD_ARG("description",		setifdescr),
+	DEF_CMD_ARG("descr",			setifdescr),
+	DEF_CMD("-description",	0,		unsetifdescr),
+	DEF_CMD("-descr",	0,		unsetifdescr),
 	DEF_CMD("promisc",	IFF_PPROMISC,	setifflags),
 	DEF_CMD("-promisc",	-IFF_PPROMISC,	setifflags),
 	DEF_CMD("add",		IFF_UP,		notealias),
Index: sbin/ifconfig/Makefile
===================================================================
--- sbin/ifconfig/Makefile	(revision 196234)
+++ sbin/ifconfig/Makefile	(working copy)
@@ -27,8 +27,8 @@ SRCS+=	ifgre.c			# GRE keys etc
 SRCS+=	ifgif.c			# GIF reversed header workaround
 
 SRCS+=	ifieee80211.c regdomain.c # SIOC[GS]IEEE80211 support
-DPADD+=	${LIBBSDXML} ${LIBSBUF} ${LIBJAIL}
-LDADD+=	-lbsdxml -ljail -lsbuf
+DPADD+=	${LIBBSDXML} ${LIBSBUF} ${LIBJAIL} ${LIBUTIL}
+LDADD+=	-lbsdxml -ljail -lsbuf -lutil
 
 SRCS+=	ifcarp.c		# SIOC[GS]VH support
 SRCS+=	ifgroup.c		# ...
Index: lib/libutil/Makefile
===================================================================
--- lib/libutil/Makefile	(revision 196234)
+++ lib/libutil/Makefile	(working copy)
@@ -10,11 +10,12 @@ SHLIB_MAJOR= 8
 
 SRCS=	_secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \
 	hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \
+	if_description.c \
 	login.c login_auth.c login_cap.c \
 	login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \
 	logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \
 	stub.c trimdomain.c uucplock.c
-INCS=	libutil.h login_cap.h
+INCS=	if_description.h libutil.h login_cap.h
 
 WARNS?=	6
 
@@ -31,7 +32,7 @@ MAN+=	kld.3 login.3 login_auth.3 login_tty.3 logou
 	_secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \
 	realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \
 	pidfile.3 flopen.3 expand_number.3 hexdump.3 \
-	kinfo_getfile.3 kinfo_getvmmap.3
+	kinfo_getfile.3 kinfo_getvmmap.3 if_description.3
 MAN+=	login.conf.5 auth.conf.5
 MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3
 MLINKS+= property.3 properties_read.3  property.3 properties_free.3
@@ -59,5 +60,6 @@ MLINKS+=pidfile.3 pidfile_open.3 \
 	pidfile.3 pidfile_write.3 \
 	pidfile.3 pidfile_close.3 \
 	pidfile.3 pidfile_remove.3
+MLINKS+= if_description.3 setifdescription.3 if_description.3 getifdescription.3
 
 .include <bsd.lib.mk>
Index: lib/libutil/if_description.3
===================================================================
--- lib/libutil/if_description.3	(revision 0)
+++ lib/libutil/if_description.3	(revision 0)
@@ -0,0 +1,90 @@
+.\"-
+.\" Copyright (c) 2009 Xin LI <delphij@FreeBSD.org>
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd September 15, 2009
+.Os
+.Dt IFSETDESCRIPTION 3
+.Sh NAME
+.Nm ifsetdescription ,
+.Nm ifgetdescription
+.Nd ifnet description utility functions
+.Sh LIBRARY
+.Lb libutil
+.Sh SYNOPSIS
+.In if_description.h
+.Ft int
+.Fn setifdescription "int s" "struct ifreq *ifr" "const char *val"
+.Ft int
+.Fn getifdescription "int s" "struct ifreq *ifr" "char **val"
+.Sh DESCRIPTION
+These functions manipulates auxiliary interface description facility
+from userland.
+.Pp
+The
+.Fn setifdescription
+function store the description
+.Fa val
+on the socket
+.Fa s
+referencing the interface and a struct ifreq pointer
+.Fa ifr
+that is already filled in with appropriate request information.
+If
+.Fa val
+is NULL or is a pointer to empty string,
+the interface description is removed.
+.Pp
+The
+.Fn getifdescription
+function reads and fills the description information into
+.Fa *val
+from the socket
+.Fa s
+with a struct ifreq pointer
+.Fa ifr
+that is already filled in with appropriate request information.
+The previous
+.Fa *val
+will be freed and reallocated if it is not NULL.
+.Sh RETURN VALUES
+.Rv -std
+.Sh SEE ALSO
+.Xr netintro 4 ,
+.Sh HISTORY
+The
+.Fn ifsetdescription
+and
+.Fn ifgetdescrition
+functions first appeared in
+.Fx 9.0 .
+.Sh AUTHORS
+The
+.Fn ifsetdescription
+and
+.Fn ifgetdescription
+functions and this manual page were written by
+.An Xin LI Aq delphij@FreeBSD.org .

Property changes on: lib/libutil/if_description.3
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + FreeBSD=%H
Added: svn:eol-style
   + native

Index: lib/libutil/if_description.c
===================================================================
--- lib/libutil/if_description.c	(revision 0)
+++ lib/libutil/if_description.c	(revision 0)
@@ -0,0 +1,107 @@
+/*-
+ * Copyright (c) 2009 Xin LI <delphij@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/cdefs.h>
+#include <net/if.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <db.h>
+#include <if_description.h>
+
+int
+setifdescription(int s, struct ifreq *ifr, const char *val)
+{
+	DB	*res;
+	DBT	key, entry;
+	char	dname[IFNAMSIZ];
+
+	bzero(dname, sizeof(dname));
+	ifr->ifr_data = (caddr_t)dname;
+
+	errno = ioctl(s, SIOCGIFDNAME, ifr);
+	if (errno == 0) {
+		res = dbopen(_PATH_IFDESCR_DB,
+			O_RDWR|O_CREAT|O_EXLOCK,
+			S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH,
+			DB_HASH, NULL);
+		if (res != NULL) {
+			key.data = dname;
+			key.size = strlen(dname);
+
+			if (val == NULL || strlen(val) == 0) {
+				(res->del)(res, &key, 0);
+			} else {
+				entry.data = __DECONST(void *, val);
+				entry.size = strlen(val) + 1;
+				(res->put)(res, &key, &entry, 0);
+			}
+			(res->close)(res);
+			return 0;
+		}
+	}
+
+	return -1;
+}
+
+int
+getifdescription(int s, struct ifreq *ifr, char **val)
+{
+	DB	*res;
+	DBT	key, entry;
+	char	dname[IFNAMSIZ];
+
+	bzero(dname, sizeof(dname));
+	ifr->ifr_data = (caddr_t)dname;
+
+	free(*val);
+	*val = NULL;
+
+	errno = ioctl(s, SIOCGIFDNAME, ifr);
+	if (errno == 0) {
+		res = dbopen(_PATH_IFDESCR_DB,
+			O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
+		if (res != NULL) {
+			key.data = dname;
+			key.size = strlen(dname);
+
+			if ((res->get)(res, &key, &entry, 0) == 0) {
+				*val = malloc(entry.size);
+				strlcpy(*val, entry.data, entry.size);
+			}
+			(res->close)(res);
+		}
+	}
+	return 0;
+}
+

Property changes on: lib/libutil/if_description.c
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + FreeBSD=%H
Added: svn:eol-style
   + native

Index: lib/libutil/if_description.h
===================================================================
--- lib/libutil/if_description.h	(revision 0)
+++ lib/libutil/if_description.h	(revision 0)
@@ -0,0 +1,37 @@
+/*-
+ * Copyright (c) 2009 Xin LI <delphij@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef	_IF_DESCRIPTION_H_
+#define	_IF_DESCRIPTION_H_
+
+#define	_PATH_IFDESCR_DB	"/etc/ifdescr.db"
+
+int setifdescription(int s, struct ifreq *ifr, const char *val);
+int getifdescription(int s, struct ifreq *ifr, char **val);
+
+#endif /* _IF_DESCRIPTION_H_ */

Property changes on: lib/libutil/if_description.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + FreeBSD=%H
Added: svn:eol-style
   + native

Index: sys/net/if.c
===================================================================
--- sys/net/if.c	(revision 196234)
+++ sys/net/if.c	(working copy)
@@ -1927,6 +1927,7 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t d
 	int new_flags, temp_flags;
 	size_t namelen, onamelen;
 	char new_name[IFNAMSIZ];
+	char dname[IFNAMSIZ];
 	struct ifaddr *ifa;
 	struct sockaddr_dl *sdl;
 
@@ -1965,6 +1966,18 @@ ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t d
 		ifr->ifr_phys = ifp->if_physical;
 		break;
 
+	case SIOCGIFDNAME:
+		bzero(dname, sizeof(dname));
+		if (ifp->if_dunit != IF_DUNIT_NONE)
+			snprintf(dname, IFNAMSIZ, "%s%d", ifp->if_dname,
+				 ifp->if_dunit);
+		else
+			strlcpy(dname, ifp->if_dname, sizeof(dname));
+		error = copyout(dname, ifr->ifr_data, sizeof(dname));
+		if (error)
+			return (error);
+		break;
+
 	case SIOCSIFFLAGS:
 		error = priv_check(td, PRIV_NET_SETIFFLAGS);
 		if (error)
Index: sys/sys/sockio.h
===================================================================
--- sys/sys/sockio.h	(revision 196234)
+++ sys/sys/sockio.h	(working copy)
@@ -82,6 +82,7 @@
 #define	SIOCGIFMAC	_IOWR('i', 38, struct ifreq)	/* get IF MAC label */
 #define	SIOCSIFMAC	 _IOW('i', 39, struct ifreq)	/* set IF MAC label */
 #define	SIOCSIFNAME	 _IOW('i', 40, struct ifreq)	/* set IF name */
+#define	SIOCGIFDNAME	_IOWR('i', 41, struct ifreq)	/* get IF dname */
 
 #define	SIOCADDMULTI	 _IOW('i', 49, struct ifreq)	/* add m'cast addr */
 #define	SIOCDELMULTI	 _IOW('i', 50, struct ifreq)	/* del m'cast addr */

--------------070600020208080409030004--



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