From owner-freebsd-current@FreeBSD.ORG Thu Oct 23 19:49:18 2003 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 952FB16A4B3 for ; Thu, 23 Oct 2003 19:49:18 -0700 (PDT) Received: from odin.ac.hmc.edu (Odin.AC.HMC.Edu [134.173.32.75]) by mx1.FreeBSD.org (Postfix) with ESMTP id 84DB943FE3 for ; Thu, 23 Oct 2003 19:49:17 -0700 (PDT) (envelope-from brdavis@odin.ac.hmc.edu) Received: from odin.ac.hmc.edu (IDENT:brdavis@localhost.localdomain [127.0.0.1]) by odin.ac.hmc.edu (8.12.9/8.12.3) with ESMTP id h9O2nEMO018615 for ; Thu, 23 Oct 2003 19:49:14 -0700 Received: (from brdavis@localhost) by odin.ac.hmc.edu (8.12.9/8.12.3/Submit) id h9O2nEk0018614 for current@freebsd.org; Thu, 23 Oct 2003 19:49:14 -0700 Date: Thu, 23 Oct 2003 19:49:14 -0700 From: Brooks Davis To: current@freebsd.org Message-ID: <20031024024914.GA12941@Odin.AC.HMC.Edu> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="LZvS9be/3tNcYl/X" Content-Disposition: inline User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-milter (http://amavis.org/) on odin.ac.hmc.edu Subject: HEADSUP if_xname incoming in one week X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 24 Oct 2003 02:49:18 -0000 --LZvS9be/3tNcYl/X Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable In approximately one week, I plan to commit the conversion of the if_name and if_unit members of struct ifnet to if_xname. Initially, this was the sum total of the commit, but code requiring knowledge of the underlying driver name and unit number necessitated the addition of two more members, if_dname and if_dunit. The current patch is available at): http://people.freebsd.org/~brooks/patches/xname.diff The diffs to sys/net/if_var.h and sys/net/if.c are included below along with an example of the driver conversion required in most cases. The working copy of the changes in in perforce at //depot/brooks/xname. I have been running this on my laptop for over a year now. It has also been tested on amd64 and sparc64 machines and compiles on alpha. However, I don't have access to anything but Ethernet and 802.11 adapters so testing on other interface types would be appreciated. Previous versions of this patch have been reviewed. I don't anticipate serious breaking on any systems since the changes are generally mechanical in nature and in the year-plus I've been hacking on them I've only created one non-bootable kernel. Some FAQs regarding this change: Q: Where was this change discussed? A: -net and -arch. re@ approved it in principle at BSDCon. Q: Why do this? A: Several reasons. - The primary motivation is to allow interface renaming. To do this we need to store the name in a type of storage we can modify, hence the use of an array of length IFNAMSIZ in struct ifnet. We also need to stop tracking the interface by its name. - NetBSD and OpenBSD compatibility. They switched to if_xname 5-6 years ago. - Breaking the "" naming scheme will allow us more freedom to give interfaces sensible names. For instance, creating an interface named fxp0.10 count create a vlan interface on fxp0 receiving traffic tagged "10". The ef(4) device and stf(4) are both examples of devices where this naming scheme doesn't work. Q: Won't this make ipfw slow to match interfaces? A: Not for most people. When interface rules are currently matched, the unit is checked and then if it succeeds, the name is strcmp'd. Now, we just check the name. If you have many interfaces with the same name, this may create a slow down. This could be remedied with an ipfw interface change to use the if_index, but I choose not to do that for now. The other case is if the unit is a wild card. Since the goal is to break the name+unit naming scheme, I decided to use fnmatch in this case. You can now specify full shell globs for interface names in ipfw. This will be slower then a strcmp, but vastly more powerful since now you could match "fxp[124]" instead of "fxp*". -- Brooks diff -ru /usr/p4/freebsd/sys/net/if_var.h ./sys/net/if_var.h --- /usr/p4/freebsd/sys/net/if_var.h Fri Oct 17 15:15:56 2003 +++ ./sys/net/if_var.h Fri Oct 17 15:15:58 2003 @@ -84,6 +84,8 @@ #include /* XXX */ #include /* XXX */ =20 +#define IF_DUNIT_NONE -1 + TAILQ_HEAD(ifnethead, ifnet); /* we use TAILQs so that the order of */ TAILQ_HEAD(ifaddrhead, ifaddr); /* instantiation is preserved in the list = */ TAILQ_HEAD(ifprefixhead, ifprefix); @@ -128,14 +130,15 @@ */ struct ifnet { void *if_softc; /* pointer to driver state */ - char *if_name; /* name, e.g. ``en'' or ``lo'' */ TAILQ_ENTRY(ifnet) if_link; /* all struct ifnets are chained */ + char if_xname[IFNAMSIZ]; /* external name (name + unit) */ + const char *if_dname; /* driver name */ + int if_dunit; /* unit or IF_DUNIT_NONE */ struct ifaddrhead if_addrhead; /* linked list of addresses per if */ struct klist if_klist; /* events attached to this if */ int if_pcount; /* number of promiscuous listeners */ struct bpf_if *if_bpf; /* packet filter structure */ u_short if_index; /* numeric abbreviation for this if */ - short if_unit; /* sub-unit for lower level driver */ short if_timer; /* time 'til if_watchdog called */ u_short if_nvlans; /* number of active vlans */ int if_flags; /* up/down, broadcast, etc. */ @@ -442,6 +445,7 @@ int if_delmulti(struct ifnet *, struct sockaddr *); void if_detach(struct ifnet *); void if_down(struct ifnet *); +void if_initname(struct ifnet *, const char *, int); int if_printf(struct ifnet *, const char *, ...) __printflike(2, 3); void if_route(struct ifnet *, int flag, int fam); int if_setlladdr(struct ifnet *, const u_char *, int); diff -ru /usr/p4/freebsd/sys/net/if.c ./sys/net/if.c --- /usr/p4/freebsd/sys/net/if.c Thu Oct 23 10:02:58 2003 +++ ./sys/net/if.c Thu Oct 23 10:05:29 2003 @@ -290,13 +290,13 @@ IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */ TAILQ_FOREACH(ifp, &ifnet, if_link) { if (ifp->if_snd.ifq_maxlen =3D=3D 0) { - printf("%s%d XXX: driver didn't set ifq_maxlen\n", - ifp->if_name, ifp->if_unit); + printf("%s XXX: driver didn't set ifq_maxlen\n", + ifp->if_xname); ifp->if_snd.ifq_maxlen =3D ifqmaxlen; } if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { - printf("%s%d XXX: driver didn't initialize queue mtx\n", - ifp->if_name, ifp->if_unit); + printf("%s XXX: driver didn't initialize queue mtx\n", + ifp->if_xname); mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_NETWORK_LOCK, MTX_DEF); } @@ -326,7 +326,7 @@ eaddr[0] =3D '\0'; break; } - snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit); + strlcpy(devname, ifp->if_xname, sizeof(devname)); name =3D net_cdevsw.d_name; i =3D 0; while ((resource_find_dev(&i, name, &unit, NULL, NULL)) =3D=3D 0) { @@ -365,7 +365,6 @@ { unsigned socksize, ifasize; int namelen, masklen; - char workbuf[64]; struct sockaddr_dl *sdl; struct ifaddr *ifa; =20 @@ -398,18 +397,17 @@ =20 ifnet_byindex(ifp->if_index) =3D ifp; ifdev_byindex(ifp->if_index) =3D make_dev(&net_cdevsw, ifp->if_index, - UID_ROOT, GID_WHEEL, 0600, "%s/%s%d", - net_cdevsw.d_name, ifp->if_name, ifp->if_unit); + UID_ROOT, GID_WHEEL, 0600, "%s/%s", + net_cdevsw.d_name, ifp->if_xname); make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", net_cdevsw.d_name, ifp->if_index); =20 - mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF); + mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); =20 /* * create a Link Level name for this device */ - namelen =3D snprintf(workbuf, sizeof(workbuf), - "%s%d", ifp->if_name, ifp->if_unit); + namelen =3D strlen(ifp->if_xname); #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) masklen =3D _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; socksize =3D masklen + ifp->if_addrlen; @@ -424,7 +422,7 @@ sdl =3D (struct sockaddr_dl *)(ifa + 1); sdl->sdl_len =3D socksize; sdl->sdl_family =3D AF_LINK; - bcopy(workbuf, sdl->sdl_data, namelen); + bcopy(ifp->if_xname, sdl->sdl_data, namelen); sdl->sdl_nlen =3D namelen; sdl->sdl_index =3D ifp->if_index; sdl->sdl_type =3D ifp->if_type; @@ -863,8 +861,7 @@ =20 for (ifc =3D LIST_FIRST(&if_cloners); ifc !=3D NULL && count !=3D 0; ifc =3D LIST_NEXT(ifc, ifc_list), count--, dst +=3D IFNAMSIZ) { - strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); - outbuf[IFNAMSIZ - 1] =3D '\0'; /* sanity */ + strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ); error =3D copyout(outbuf, dst, IFNAMSIZ); if (error) break; @@ -1625,8 +1622,8 @@ ifr.ifr_flagshigh =3D ifp->if_flags >> 16; error =3D (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); if (error =3D=3D 0) { - log(LOG_INFO, "%s%d: promiscuous mode %s\n", - ifp->if_name, ifp->if_unit, + log(LOG_INFO, "%s: promiscuous mode %s\n", + ifp->if_xname, (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); rt_ifmsg(ifp); } else { @@ -1655,18 +1652,14 @@ ifrp =3D ifc->ifc_req; IFNET_RLOCK(); /* could sleep XXX */ TAILQ_FOREACH(ifp, &ifnet, if_link) { - char workbuf[64]; - int ifnlen, addrs; + int addrs; =20 if (space < sizeof(ifr)) break; - ifnlen =3D snprintf(workbuf, sizeof(workbuf), - "%s%d", ifp->if_name, ifp->if_unit); - if(ifnlen + 1 > sizeof ifr.ifr_name) { + if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) + >=3D sizeof(ifr.ifr_name)) { error =3D ENAMETOOLONG; break; - } else { - strcpy(ifr.ifr_name, workbuf); } =20 addrs =3D 0; @@ -2001,13 +1994,30 @@ return ifma; } =20 +/* + * The name argument must be a pointer to storage which will last as + * long as the interface does. For physical devices, the result of + * device_get_name(dev) is a good choice and for pseudo-devices a + * static string works well. + */ +void +if_initname(struct ifnet *ifp, const char *name, int unit) +{ + ifp->if_dname =3D name; + ifp->if_dunit =3D unit; + if (unit !=3D IF_DUNIT_NONE) + snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); + else + strlcpy(ifp->if_xname, name, IFNAMSIZ); +} + int if_printf(struct ifnet *ifp, const char * fmt, ...) { va_list ap; int retval; =20 - retval =3D printf("%s%d: ", ifp->if_name, ifp->if_unit); + retval =3D printf("%s: ", ifp->if_xname); va_start(ap, fmt); retval +=3D vprintf(fmt, ap); va_end(ap); diff -ru /usr/p4/freebsd/sys/pci/if_dc.c ./sys/pci/if_dc.c --- /usr/p4/freebsd/sys/pci/if_dc.c Thu Oct 23 10:02:59 2003 +++ ./sys/pci/if_dc.c Thu Oct 23 10:05:50 2003 @@ -2228,8 +2228,7 @@ =20 ifp =3D &sc->arpcom.ac_if; ifp->if_softc =3D sc; - ifp->if_unit =3D unit; - ifp->if_name =3D "dc"; + if_initname(ifp, device_get_name(dev), device_get_unit(dev)); /* XXX: bleah, MTU gets overwritten in ether_ifattach() */ ifp->if_mtu =3D ETHERMTU; ifp->if_flags =3D IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; --=20 Any statement of the form "X is the one, true Y" is FALSE. PGP fingerprint 655D 519C 26A7 82E7 2529 9BF0 5D8E 8BE9 F238 1AD4 --LZvS9be/3tNcYl/X Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE/mJMpXY6L6fI4GtQRAqRCAKC/+ONN+56Dc/+Ad73oKetEqPG4MQCgxyPU Eyor8Hgv+h1ThQ6nS7nJ5gY= =vZUA -----END PGP SIGNATURE----- --LZvS9be/3tNcYl/X--