Wrap array accesses in macros, which also happen to be lvalues:

ifnet_addrs[i - 1]  -> ifaddr_byindex(i)
        ifindex2ifnet[i]    -> ifnet_byindex(i)

This is intended to ease the conversion to SMPng.
This commit is contained in:
jlemon 2001-09-06 02:40:43 +00:00
parent cb6b426d98
commit 3413c758de
24 changed files with 99 additions and 80 deletions

View File

@ -1385,7 +1385,7 @@ linux_ioctl_socket(struct proc *p, struct linux_ioctl_args *args)
* structure, as Linux interface names are all different.
*/
for (ifn = 0; ifn < if_index; ifn++) {
ifp = ifnet_addrs[ifn]->ifa_ifp;
ifp = ifnet_byindex(ifn);
if (ifp->if_type == IFT_ETHER) {
ifa = TAILQ_FIRST(&ifp->if_addrhead);
while (ifa) {

View File

@ -1001,7 +1001,7 @@ static void an_setdef(sc, areq)
case AN_RID_GENCONFIG:
cfg = (struct an_ltv_genconfig *)areq;
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
bcopy((char *)&cfg->an_macaddr, (char *)&sc->arpcom.ac_enaddr,
ETHER_ADDR_LEN);

View File

@ -1293,7 +1293,7 @@ static void wi_setdef(sc, wreq)
switch(wreq->wi_type) {
case WI_RID_MAC_NODE:
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
bcopy((char *)&wreq->wi_val, (char *)&sc->arpcom.ac_enaddr,
ETHER_ADDR_LEN);

View File

@ -71,7 +71,9 @@
#endif
static int ifconf(u_long, caddr_t);
static void ifinit(void *);
static void if_grow(void);
static void if_init(void *);
static void if_check(void *);
static void if_qflush(struct ifqueue *);
static void if_slowtimo(void *);
static void link_rtrequest(int, struct rtentry *, struct sockaddr *);
@ -87,17 +89,19 @@ extern void nd6_setmtu __P((struct ifnet *));
#endif
int if_index = 0;
struct ifaddr **ifnet_addrs;
struct ifnet **ifindex2ifnet = NULL;
struct ifindex_entry *ifindex_table = NULL;
int ifqmaxlen = IFQ_MAXLEN;
struct ifnethead ifnet; /* depend on static init XXX */
int if_cloners_count;
LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
static int if_indexlim = 8;
/*
* System initialization
*/
SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
@ -109,8 +113,34 @@ MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
* parameters.
*/
/* ARGSUSED*/
void
ifinit(dummy)
static void
if_init(dummy)
void *dummy;
{
TAILQ_INIT(&ifnet);
if_grow(); /* create initial table */
}
static void
if_grow(void)
{
u_int n;
struct ifindex_entry *e;
if_indexlim <<= 1;
n = if_indexlim * sizeof(*e);
e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
if (ifindex_table != NULL) {
memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
free((caddr_t)ifindex_table, M_IFADDR);
}
ifindex_table = e;
}
/* ARGSUSED*/
static void
if_check(dummy)
void *dummy;
{
struct ifnet *ifp;
@ -146,13 +176,6 @@ if_attach(ifp)
char workbuf[64];
register struct sockaddr_dl *sdl;
register struct ifaddr *ifa;
static int if_indexlim = 8;
static int inited;
if (!inited) {
TAILQ_INIT(&ifnet);
inited = 1;
}
TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
ifp->if_index = ++if_index;
@ -167,27 +190,10 @@ if_attach(ifp)
TAILQ_INIT(&ifp->if_prefixhead);
TAILQ_INIT(&ifp->if_multiaddrs);
getmicrotime(&ifp->if_lastchange);
if (ifnet_addrs == 0 || if_index >= if_indexlim) {
unsigned n = (if_indexlim <<= 1) * sizeof(ifa);
caddr_t q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
if (ifnet_addrs) {
bcopy((caddr_t)ifnet_addrs, (caddr_t)q, n/2);
free((caddr_t)ifnet_addrs, M_IFADDR);
}
ifnet_addrs = (struct ifaddr **)q;
/* grow ifindex2ifnet */
n = if_indexlim * sizeof(struct ifnet *);
q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
if (ifindex2ifnet) {
bcopy((caddr_t)ifindex2ifnet, q, n/2);
free((caddr_t)ifindex2ifnet, M_IFADDR);
}
ifindex2ifnet = (struct ifnet **)q;
}
ifindex2ifnet[if_index] = ifp;
if (if_index >= if_indexlim)
if_grow();
ifnet_byindex(if_index) = ifp;
mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF);
/*
@ -212,7 +218,7 @@ if_attach(ifp)
sdl->sdl_nlen = namelen;
sdl->sdl_index = ifp->if_index;
sdl->sdl_type = ifp->if_type;
ifnet_addrs[if_index - 1] = ifa;
ifaddr_byindex(if_index) = ifa;
ifa->ifa_ifp = ifp;
ifa->ifa_rtrequest = link_rtrequest;
ifa->ifa_addr = (struct sockaddr *)sdl;
@ -245,11 +251,12 @@ if_detach(ifp)
if_down(ifp);
/*
* Remove address from ifnet_addrs[] and maybe decrement if_index.
* Remove address from ifindex_table[] and maybe decrement if_index.
* Clean up all addresses.
*/
ifnet_addrs[ifp->if_index - 1] = 0;
while (if_index > 0 && ifnet_addrs[if_index - 1] == 0)
ifaddr_byindex(ifp->if_index) = NULL;
while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
if_index--;
for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
@ -600,7 +607,7 @@ ifa_ifwithnet(addr)
if (af == AF_LINK) {
register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
if (sdl->sdl_index && sdl->sdl_index <= if_index)
return (ifnet_addrs[sdl->sdl_index - 1]);
return (ifaddr_byindex(sdl->sdl_index));
}
/*
@ -719,7 +726,9 @@ ifaof_ifpforaddr(addr, ifp)
return (ifa);
}
}
return (ifa_maybe);
ifa = ifa_maybe;
done:
return (ifa);
}
#include <net/route.h>
@ -1553,7 +1562,7 @@ if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
struct sockaddr_dl *sdl;
struct ifaddr *ifa;
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
if (ifa == NULL)
return (EINVAL);
sdl = (struct sockaddr_dl *)ifa->ifa_addr;

View File

@ -134,8 +134,8 @@ ef_attach(struct efnet *sc)
ifp->if_type = IFT_XETHER;
ifp->if_flags |= IFF_RUNNING;
ifa1 = ifnet_addrs[ifp->if_index - 1];
ifa2 = ifnet_addrs[sc->ef_ifp->if_index - 1];
ifa1 = ifaddr_byindex(ifp->if_index);
ifa2 = ifaddr_byindex(sc->ef_ifp->if_index);
sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
sdl1->sdl_type = IFT_ETHER;

View File

@ -667,7 +667,7 @@ ether_ifattach(ifp, bpf)
ifp->if_resolvemulti = ether_resolvemulti;
if (ifp->if_baudrate == 0)
ifp->if_baudrate = 10000000;
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
sdl->sdl_type = IFT_ETHER;

View File

@ -526,7 +526,7 @@ fddi_ifattach(ifp)
ifp->if_flags |= IFF_NOTRAILERS;
#endif
#if defined(__FreeBSD__)
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
sdl->sdl_type = IFT_FDDI;
sdl->sdl_alen = ifp->if_addrlen;

View File

@ -102,7 +102,7 @@ iso88025_ifattach(struct ifnet *ifp)
if (ifp->if_mtu == 0)
ifp->if_mtu = ISO88025_DEFAULT_MTU;
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
if (ifa == 0) {
printf("iso88025_ifattach: no lladdr!\n");
return;

View File

@ -83,7 +83,7 @@ sysctl_ifdata(SYSCTL_HANDLER_ARGS) /* XXX bad syntax! */
if (name[0] <= 0 || name[0] > if_index)
return ENOENT;
ifp = ifnet_addrs[name[0] - 1]->ifa_ifp;
ifp = ifaddr_byindex(name[0])->ifa_ifp;
switch(name[1]) {
default:

View File

@ -81,6 +81,7 @@ struct ether_header;
#endif /* _KERNEL */
#include <sys/lock.h> /* XXX */
#include <sys/mutex.h> /* XXX */
#include <sys/event.h> /* XXX */
TAILQ_HEAD(ifnethead, ifnet); /* we use TAILQs so that the order of */
TAILQ_HEAD(ifaddrhead, ifaddr); /* instantiation is preserved in the list */
@ -109,7 +110,8 @@ struct ifnet {
char *if_name; /* name, e.g. ``en'' or ``lo'' */
TAILQ_ENTRY(ifnet) if_link; /* all struct ifnets are chained */
struct ifaddrhead if_addrhead; /* linked list of addresses per if */
int if_pcount; /* number of promiscuous listeners */
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 */
@ -371,12 +373,21 @@ struct ifmultiaddr {
(ifa)->ifa_refcnt--; \
} while (0)
struct ifindex_entry {
struct ifnet *ife_ifnet;
struct ifaddr *ife_ifnet_addr;
dev_t ife_dev;
};
#define ifnet_byindex(idx) ifindex_table[(idx)].ife_ifnet
#define ifaddr_byindex(idx) ifindex_table[(idx)].ife_ifnet_addr
#define ifdev_byindex(idx) ifindex_table[(idx)].ife_dev
extern struct ifnethead ifnet;
extern struct ifnet **ifindex2ifnet;
extern struct ifindex_entry *ifindex_table;
extern int ifqmaxlen;
extern struct ifnet *loif; /* first loopback interface */
extern int if_index;
extern struct ifaddr **ifnet_addrs;
void ether_ifattach __P((struct ifnet *, int));
void ether_ifdetach __P((struct ifnet *, int));

View File

@ -487,8 +487,8 @@ vlan_config(struct ifvlan *ifv, struct ifnet *p)
* Set up our ``Ethernet address'' to reflect the underlying
* physical interface's.
*/
ifa1 = ifnet_addrs[ifv->ifv_if.if_index - 1];
ifa2 = ifnet_addrs[p->if_index - 1];
ifa1 = ifaddr_byindex(ifv->ifv_if.if_index);
ifa2 = ifaddr_byindex(p->if_index);
sdl1 = (struct sockaddr_dl *)ifa1->ifa_addr;
sdl2 = (struct sockaddr_dl *)ifa2->ifa_addr;
sdl1->sdl_type = IFT_ETHER;
@ -549,7 +549,7 @@ vlan_unconfig(struct ifnet *ifp)
ifv->ifv_if.if_mtu = ETHERMTU;
/* Clear our MAC address. */
ifa = ifnet_addrs[ifv->ifv_if.if_index - 1];
ifa = ifaddr_byindex(ifv->ifv_if.if_index);
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
sdl->sdl_type = IFT_ETHER;
sdl->sdl_alen = ETHER_ADDR_LEN;

View File

@ -564,7 +564,7 @@ atm_physif_ioctl(code, data, arg)
* Set macaddr in <Link> address
*/
ifp->if_addrlen = 6;
ifa = ifnet_addrs[ifp->if_index - 1];
ifa = ifaddr_byindex(ifp->if_index);
if ( ifa ) {
sdl = (struct sockaddr_dl *)
ifa->ifa_addr;

View File

@ -410,7 +410,7 @@ in_control(so, cmd, data, ifp, p)
* XXX horrible hack to detect that we are being called
* from if_detach()
*/
if (!ifnet_addrs[ifp->if_index - 1]) {
if (ifaddr_byindex(ifp->if_index) != NULL) {
in_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
in_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
}

View File

@ -1842,8 +1842,8 @@ ip_savecontrol(inp, mp, ip, m)
if (((ifp = m->m_pkthdr.rcvif))
&& ( ifp->if_index && (ifp->if_index <= if_index))) {
sdp = (struct sockaddr_dl *)(ifnet_addrs
[ifp->if_index - 1]->ifa_addr);
sdp = (struct sockaddr_dl *)
(ifaddr_byindex(ifp->if_index)->ifa_addr);
/*
* Change our mind and don't try copy.
*/

View File

@ -1515,7 +1515,7 @@ ip_multicast_if(a, ifindexp)
ifindex = ntohl(a->s_addr) & 0xffffff;
if (ifindex < 0 || if_index < ifindex)
return NULL;
ifp = ifindex2ifnet[ifindex];
ifp = ifnet_byindex(ifindex);
if (ifindexp)
*ifindexp = ifindex;
} else {

View File

@ -291,7 +291,7 @@ in6_ifindex2scopeid(idx)
if (idx < 0 || if_index < idx)
return -1;
ifp = ifindex2ifnet[idx];
ifp = ifnet_byindex(idx);
TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)
{

View File

@ -424,8 +424,7 @@ in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
*/
if (pi && pi->ipi6_ifindex) {
/* XXX boundary check is assumed to be already done. */
ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
dst);
ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
if (ia6 == 0) {
*errorp = EADDRNOTAVAIL;
return(0);
@ -453,7 +452,7 @@ in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
*errorp = ENXIO; /* XXX: better error? */
return(0);
}
ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
dst);
if (ia6 == 0) {
*errorp = EADDRNOTAVAIL;

View File

@ -141,8 +141,7 @@ in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
*/
if (pi && pi->ipi6_ifindex) {
/* XXX boundary check is assumed to be already done. */
ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
dst);
ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
if (ia6 == 0) {
*errorp = EADDRNOTAVAIL;
return(0);
@ -170,7 +169,7 @@ in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
*errorp = ENXIO; /* XXX: better error? */
return(0);
}
ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
dst);
if (ia6 == 0) {
*errorp = EADDRNOTAVAIL;
@ -474,7 +473,7 @@ in6_embedscope(in6, sin6, in6p, ifpp)
if (in6p && in6p->in6p_outputopts &&
(pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
pi->ipi6_ifindex) {
ifp = ifindex2ifnet[pi->ipi6_ifindex];
ifp = ifnet_byindex(pi->ipi6_ifindex);
in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
in6p->in6p_moptions &&
@ -485,7 +484,7 @@ in6_embedscope(in6, sin6, in6p, ifpp)
/* boundary check */
if (scopeid < 0 || if_index < scopeid)
return ENXIO; /* XXX EINVAL? */
ifp = ifindex2ifnet[scopeid];
ifp = ifnet_byindex(scopeid);
/*XXX assignment to 16bit from 32bit variable */
in6->s6_addr16[1] = htons(scopeid & 0xffff);
}

View File

@ -546,7 +546,7 @@ add_m6if(mifcp)
return EADDRINUSE; /* XXX: is it appropriate? */
if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
return ENXIO;
ifp = ifindex2ifnet[mifcp->mif6c_pifi];
ifp = ifnet_byindex(mifcp->mif6c_pifi);
if (mifcp->mif6c_flags & MIFF_REGISTER) {
if (reg_mif_num == (mifi_t)-1) {

View File

@ -613,7 +613,7 @@ skip_ipsec2:;
* Boundary check for ifindex is assumed to be already done.
*/
if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex);
/*
* If the destination is a node-local scope multicast,
@ -795,9 +795,9 @@ skip_ipsec2:;
*/
origifp = NULL;
if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1]));
else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1]));
/*
* XXX: origifp can be NULL even in those two cases above.
* For example, if we remove the (only) link-local address
@ -1929,7 +1929,7 @@ ip6_setmoptions(optname, im6op, m)
error = ENXIO; /* XXX EINVAL? */
break;
}
ifp = ifindex2ifnet[ifindex];
ifp = ifnet_byindex(ifindex);
if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
error = EADDRNOTAVAIL;
break;
@ -2038,7 +2038,7 @@ ip6_setmoptions(optname, im6op, m)
rtfree(ro.ro_rt);
}
} else
ifp = ifindex2ifnet[mreq->ipv6mr_interface];
ifp = ifnet_byindex(mreq->ipv6mr_interface);
/*
* See if we found an interface, and confirm that it
@ -2114,7 +2114,7 @@ ip6_setmoptions(optname, im6op, m)
error = ENXIO; /* XXX EINVAL? */
break;
}
ifp = ifindex2ifnet[mreq->ipv6mr_interface];
ifp = ifnet_byindex(mreq->ipv6mr_interface);
/*
* Put interface index into the multicast address,
* if the address has link-local scope.

View File

@ -183,7 +183,7 @@ nd6_ifattach(ifp)
if (ND.basereachable)
return;
ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
ND.linkmtu = ifnet_byindex(ifp->if_index)->if_mtu;
ND.chlim = IPV6_DEFHLIM;
ND.basereachable = REACHABLE_TIME;
ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);

View File

@ -1959,7 +1959,7 @@ nd6_setdefaultiface(ifindex)
if (nd6_defifindex != ifindex) {
nd6_defifindex = ifindex;
if (nd6_defifindex > 0)
nd6_defifp = ifindex2ifnet[nd6_defifindex];
nd6_defifp = ifnet_byindex(nd6_defifindex);
else
nd6_defifp = NULL;

View File

@ -375,7 +375,7 @@ rip6_output(m, va_alist)
*/
if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
oifp = ifindex2ifnet[pi->ipi6_ifindex];
oifp = ifnet_byindex(pi->ipi6_ifindex);
} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
in6p->in6p_moptions &&
in6p->in6p_moptions->im6o_multicast_ifp) {
@ -410,7 +410,7 @@ rip6_output(m, va_alist)
}
ip6->ip6_src = *in6a;
if (in6p->in6p_route.ro_rt)
oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
oifp = ifnet_byindex(in6p->in6p_route.ro_rt->rt_ifp->if_index);
}
ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);

View File

@ -129,6 +129,7 @@ enum sysinit_sub_id {
SI_SUB_MBUF = 0x2700000, /* mbuf subsystem */
SI_SUB_INTR = 0x2800000, /* interrupt threads */
SI_SUB_SOFTINTR = 0x2800001, /* start soft interrupt thread */
SI_SUB_INIT_IF = 0x3000000, /* prep for net interfaces */
SI_SUB_DRIVERS = 0x3100000, /* Let Drivers initialize */
SI_SUB_CONFIGURE = 0x3800000, /* Configure devices */
SI_SUB_VFS = 0x4000000, /* virtual file system*/