Module Name: src Committed By: rmind Date: Sun May 18 00:33:20 UTC 2014
Modified Files: src/sys/net: if.c if.h Log Message: - Move ifnet_list (and lo0ifp while here) under #ifdef _KERNEL. - Make ifindex2ifnet, if_indexlim and some other variables static. - Move if_index generation into its own function. - if_alloc/if_free: replace malloc with kmem. To generate a diff of this commit: cvs rdiff -u -r1.273 -r1.274 src/sys/net/if.c cvs rdiff -u -r1.164 -r1.165 src/sys/net/if.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/net/if.c diff -u src/sys/net/if.c:1.273 src/sys/net/if.c:1.274 --- src/sys/net/if.c:1.273 Sat Apr 26 11:16:22 2014 +++ src/sys/net/if.c Sun May 18 00:33:20 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: if.c,v 1.273 2014/04/26 11:16:22 pooka Exp $ */ +/* $NetBSD: if.c,v 1.274 2014/05/18 00:33:20 rmind Exp $ */ /*- * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc. @@ -90,7 +90,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.273 2014/04/26 11:16:22 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.274 2014/05/18 00:33:20 rmind Exp $"); #include "opt_inet.h" @@ -138,6 +138,7 @@ __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.273 #include "carp.h" #if NCARP > 0 +#include <netinet/in_var.h> #include <netinet/ip_carp.h> #endif @@ -147,10 +148,23 @@ __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.273 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); -int ifqmaxlen = IFQ_MAXLEN; -callout_t if_slowtimo_ch; +/* + * Global list of interfaces. + */ +struct ifnet_head ifnet_list; +static ifnet_t ** ifindex2ifnet = NULL; + +static u_int if_index = 1; +static size_t if_indexlim = 0; +static uint64_t index_gen; +static kmutex_t index_gen_mtx; -int netisr; /* scheduling bits for network */ +static struct ifaddr ** ifnet_addrs = NULL; + +static callout_t if_slowtimo_ch; + +struct ifnet *lo0ifp; +int ifqmaxlen = IFQ_MAXLEN; static int if_rt_walktree(struct rtentry *, void *); @@ -160,9 +174,6 @@ static int if_clone_list(struct if_clone static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); static int if_cloners_count; -static uint64_t index_gen; -static kmutex_t index_gen_mtx; - /* Packet filtering hook for interfaces. */ pfil_head_t * if_pfil; @@ -240,20 +251,23 @@ void ifinit1(void) { mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE); + TAILQ_INIT(&ifnet_list); + if_indexlim = 8; + if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL); KASSERT(if_pfil != NULL); } -struct ifnet * +ifnet_t * if_alloc(u_char type) { - return malloc(sizeof(struct ifnet), M_DEVBUF, M_WAITOK|M_ZERO); + return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP); } void -if_free(struct ifnet *ifp) +if_free(ifnet_t *ifp) { - free(ifp, M_DEVBUF); + kmem_free(ifp, sizeof(ifnet_t)); } void @@ -329,13 +343,6 @@ if_nulldrain(struct ifnet *ifp) /* Nothing. */ } -static u_int if_index = 1; -struct ifnet_head ifnet; -size_t if_indexlim = 0; -struct ifaddr **ifnet_addrs = NULL; -struct ifnet **ifindex2ifnet = NULL; -struct ifnet *lo0ifp; - void if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory) { @@ -488,61 +495,47 @@ if_free_sadl(struct ifnet *ifp) splx(s); } -/* - * Attach an interface to the - * list of "active" interfaces. - */ -void -if_attach(struct ifnet *ifp) +static void +if_getindex(ifnet_t *ifp) { - int indexlim = 0; - - if (if_indexlim == 0) { - TAILQ_INIT(&ifnet); - if_indexlim = 8; - } - TAILQ_INIT(&ifp->if_addrlist); - TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); - - if (ifioctl_attach(ifp) != 0) - panic("%s: ifioctl_attach() failed", __func__); + bool hitlimit = false; mutex_enter(&index_gen_mtx); ifp->if_index_gen = index_gen++; mutex_exit(&index_gen_mtx); ifp->if_index = if_index; - if (ifindex2ifnet == NULL) + if (ifindex2ifnet == NULL) { if_index++; - else - while (ifp->if_index < if_indexlim && - ifindex2ifnet[ifp->if_index] != NULL) { - ++if_index; - if (if_index == 0) - if_index = 1; + goto skip; + } + while (if_byindex(ifp->if_index)) { + /* + * If we hit USHRT_MAX, we skip back to 0 since + * there are a number of places where the value + * of if_index or if_index itself is compared + * to or stored in an unsigned short. By + * jumping back, we won't botch those assignments + * or comparisons. + */ + if (++if_index == 0) { + if_index = 1; + } else if (if_index == USHRT_MAX) { /* - * If we hit USHRT_MAX, we skip back to 0 since - * there are a number of places where the value - * of if_index or if_index itself is compared - * to or stored in an unsigned short. By - * jumping back, we won't botch those assignments - * or comparisons. + * However, if we have to jump back to + * zero *twice* without finding an empty + * slot in ifindex2ifnet[], then there + * there are too many (>65535) interfaces. */ - else if (if_index == USHRT_MAX) { - /* - * However, if we have to jump back to - * zero *twice* without finding an empty - * slot in ifindex2ifnet[], then there - * there are too many (>65535) interfaces. - */ - if (indexlim++) - panic("too many interfaces"); - else - if_index = 1; + if (hitlimit) { + panic("too many interfaces"); } - ifp->if_index = if_index; + hitlimit = true; + if_index = 1; } - + ifp->if_index = if_index; + } +skip: /* * We have some arrays that should be indexed by if_index. * since if_index will grow dynamically, they should grow too. @@ -578,8 +571,23 @@ if_attach(struct ifnet *ifp) } ifindex2ifnet = (struct ifnet **)q; } - ifindex2ifnet[ifp->if_index] = ifp; +} + +/* + * Attach an interface to the list of "active" interfaces. + */ +void +if_attach(ifnet_t *ifp) +{ + KASSERT(if_indexlim > 0); + TAILQ_INIT(&ifp->if_addrlist); + TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list); + + if (ifioctl_attach(ifp) != 0) + panic("%s: ifioctl_attach() failed", __func__); + + if_getindex(ifp); /* * Link level name is allocated later by a separate call to @@ -844,7 +852,7 @@ again: ifindex2ifnet[ifp->if_index] = NULL; - TAILQ_REMOVE(&ifnet, ifp, if_list); + TAILQ_REMOVE(&ifnet_list, ifp, if_list); ifioctl_detach(ifp); @@ -871,13 +879,9 @@ if_detach_queues(struct ifnet *ifp, stru prev = NULL; for (m = q->ifq_head; m != NULL; m = next) { + KASSERT((m->m_flags & M_PKTHDR) != 0); + next = m->m_nextpkt; -#ifdef DIAGNOSTIC - if ((m->m_flags & M_PKTHDR) == 0) { - prev = m; - continue; - } -#endif if (m->m_pkthdr.rcvif != ifp) { prev = m; continue; @@ -1542,7 +1546,6 @@ ifunit(const char *name) ifnet_t * if_byindex(u_int idx) { - return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL; } Index: src/sys/net/if.h diff -u src/sys/net/if.h:1.164 src/sys/net/if.h:1.165 --- src/sys/net/if.h:1.164 Sat May 17 20:44:24 2014 +++ src/sys/net/if.h Sun May 18 00:33:20 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: if.h,v 1.164 2014/05/17 20:44:24 rmind Exp $ */ +/* $NetBSD: if.h,v 1.165 2014/05/18 00:33:20 rmind Exp $ */ /*- * Copyright (c) 1999, 2000, 2001 The NetBSD Foundation, Inc. @@ -845,11 +845,6 @@ do { \ MALLOC_DECLARE(M_IFADDR); MALLOC_DECLARE(M_IFMADDR); -extern struct ifnet_head ifnet; -extern struct ifnet **ifindex2ifnet; -extern struct ifnet *lo0ifp; -extern size_t if_indexlim; - int ifreq_setaddr(u_long, struct ifreq *, const struct sockaddr *); struct ifnet *if_alloc(u_char); @@ -945,15 +940,18 @@ __END_DECLS #ifdef _KERNEL -#define IFNET_FIRST() TAILQ_FIRST(&ifnet) +#define IFNET_FIRST() TAILQ_FIRST(&ifnet_list) #define IFNET_NEXT(__ifp) TAILQ_NEXT((__ifp), if_list) -#define IFNET_FOREACH(__ifp) TAILQ_FOREACH(__ifp, &ifnet, if_list) +#define IFNET_FOREACH(__ifp) TAILQ_FOREACH(__ifp, &ifnet_list, if_list) #define IFADDR_FIRST(__ifp) TAILQ_FIRST(&(__ifp)->if_addrlist) #define IFADDR_NEXT(__ifa) TAILQ_NEXT((__ifa), ifa_list) #define IFADDR_FOREACH(__ifa, __ifp) TAILQ_FOREACH(__ifa, \ &(__ifp)->if_addrlist, ifa_list) #define IFADDR_EMPTY(__ifp) TAILQ_EMPTY(&(__ifp)->if_addrlist) +extern struct ifnet_head ifnet_list; +extern struct ifnet *lo0ifp; + ifnet_t * if_byindex(u_int); /*