xref: /dragonfly/sys/net/if.c (revision caaec4e3)
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if.c	8.3 (Berkeley) 1/4/94
30  * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
31  */
32 
33 #include "opt_inet6.h"
34 #include "opt_inet.h"
35 #include "opt_ifpoll.h"
36 
37 #include <sys/param.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/priv.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketops.h>
47 #include <sys/kernel.h>
48 #include <sys/ktr.h>
49 #include <sys/mutex.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
53 #include <sys/domain.h>
54 #include <sys/thread.h>
55 #include <sys/serialize.h>
56 #include <sys/bus.h>
57 
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
60 #include <sys/mutex2.h>
61 
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/if_ringmap.h>
68 #include <net/ifq_var.h>
69 #include <net/radix.h>
70 #include <net/route.h>
71 #include <net/if_clone.h>
72 #include <net/netisr2.h>
73 #include <net/netmsg2.h>
74 
75 #include <machine/atomic.h>
76 #include <machine/stdarg.h>
77 #include <machine/smp.h>
78 
79 #if defined(INET) || defined(INET6)
80 /*XXX*/
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
84 #ifdef INET6
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
87 #endif
88 #endif
89 
90 struct netmsg_ifaddr {
91 	struct netmsg_base base;
92 	struct ifaddr	*ifa;
93 	struct ifnet	*ifp;
94 	int		tail;
95 };
96 
97 struct ifsubq_stage_head {
98 	TAILQ_HEAD(, ifsubq_stage)	stg_head;
99 } __cachealign;
100 
101 struct if_ringmap {
102 	int		rm_cnt;
103 	int		rm_grid;
104 	int		rm_cpumap[];
105 };
106 
107 #define RINGMAP_FLAG_NONE		0x0
108 #define RINGMAP_FLAG_POWEROF2		0x1
109 
110 /*
111  * System initialization
112  */
113 static void	if_attachdomain(void *);
114 static void	if_attachdomain1(struct ifnet *);
115 static int	ifconf(u_long, caddr_t, struct ucred *);
116 static void	ifinit(void *);
117 static void	ifnetinit(void *);
118 static void	if_slowtimo(void *);
119 static void	link_rtrequest(int, struct rtentry *);
120 static int	if_rtdel(struct radix_node *, void *);
121 static void	if_slowtimo_dispatch(netmsg_t);
122 
123 /* Helper functions */
124 static void	ifsq_watchdog_reset(struct ifsubq_watchdog *);
125 static int	if_delmulti_serialized(struct ifnet *, struct sockaddr *);
126 static struct ifnet_array *ifnet_array_alloc(int);
127 static void	ifnet_array_free(struct ifnet_array *);
128 static struct ifnet_array *ifnet_array_add(struct ifnet *,
129 		    const struct ifnet_array *);
130 static struct ifnet_array *ifnet_array_del(struct ifnet *,
131 		    const struct ifnet_array *);
132 
133 #ifdef INET6
134 /*
135  * XXX: declare here to avoid to include many inet6 related files..
136  * should be more generalized?
137  */
138 extern void	nd6_setmtu(struct ifnet *);
139 #endif
140 
141 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
142 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
143 SYSCTL_NODE(_net_link, OID_AUTO, ringmap, CTLFLAG_RW, 0, "link ringmap");
144 
145 static int ifsq_stage_cntmax = 16;
146 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
147 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
148     &ifsq_stage_cntmax, 0, "ifq staging packet count max");
149 
150 static int if_stats_compat = 0;
151 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
152     &if_stats_compat, 0, "Compat the old ifnet stats");
153 
154 static int if_ringmap_dumprdr = 0;
155 SYSCTL_INT(_net_link_ringmap, OID_AUTO, dump_rdr, CTLFLAG_RW,
156     &if_ringmap_dumprdr, 0, "dump redirect table");
157 
158 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
159 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
160 
161 static  if_com_alloc_t *if_com_alloc[256];
162 static  if_com_free_t *if_com_free[256];
163 
164 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
165 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
166 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
167 
168 int			ifqmaxlen = IFQ_MAXLEN;
169 struct ifnethead	ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
170 
171 static struct ifnet_array	ifnet_array0;
172 static struct ifnet_array	*ifnet_array = &ifnet_array0;
173 
174 static struct callout		if_slowtimo_timer;
175 static struct netmsg_base	if_slowtimo_netmsg;
176 
177 int			if_index = 0;
178 struct ifnet		**ifindex2ifnet = NULL;
179 static struct mtx	ifnet_mtx = MTX_INITIALIZER("ifnet");
180 
181 static struct ifsubq_stage_head	ifsubq_stage_heads[MAXCPU];
182 
183 #ifdef notyet
184 #define IFQ_KTR_STRING		"ifq=%p"
185 #define IFQ_KTR_ARGS	struct ifaltq *ifq
186 #ifndef KTR_IFQ
187 #define KTR_IFQ			KTR_ALL
188 #endif
189 KTR_INFO_MASTER(ifq);
190 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
191 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
192 #define logifq(name, arg)	KTR_LOG(ifq_ ## name, arg)
193 
194 #define IF_START_KTR_STRING	"ifp=%p"
195 #define IF_START_KTR_ARGS	struct ifnet *ifp
196 #ifndef KTR_IF_START
197 #define KTR_IF_START		KTR_ALL
198 #endif
199 KTR_INFO_MASTER(if_start);
200 KTR_INFO(KTR_IF_START, if_start, run, 0,
201 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
202 KTR_INFO(KTR_IF_START, if_start, sched, 1,
203 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
204 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
205 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
206 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
207 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
208 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
209 	 IF_START_KTR_STRING, IF_START_KTR_ARGS);
210 #define logifstart(name, arg)	KTR_LOG(if_start_ ## name, arg)
211 #endif
212 
213 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
214 
215 /*
216  * Network interface utility routines.
217  *
218  * Routines with ifa_ifwith* names take sockaddr *'s as
219  * parameters.
220  */
221 /* ARGSUSED*/
222 static void
223 ifinit(void *dummy)
224 {
225 	struct ifnet *ifp;
226 
227 	callout_init_mp(&if_slowtimo_timer);
228 	netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
229 	    MSGF_PRIORITY, if_slowtimo_dispatch);
230 
231 	/* XXX is this necessary? */
232 	ifnet_lock();
233 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
234 		if (ifp->if_snd.altq_maxlen == 0) {
235 			if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
236 			ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
237 		}
238 	}
239 	ifnet_unlock();
240 
241 	/* Start if_slowtimo */
242 	lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
243 }
244 
245 static void
246 ifsq_ifstart_ipifunc(void *arg)
247 {
248 	struct ifaltq_subque *ifsq = arg;
249 	struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
250 
251 	crit_enter();
252 	if (lmsg->ms_flags & MSGF_DONE)
253 		lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
254 	crit_exit();
255 }
256 
257 static __inline void
258 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
259 {
260 	KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
261 	TAILQ_REMOVE(&head->stg_head, stage, stg_link);
262 	stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
263 	stage->stg_cnt = 0;
264 	stage->stg_len = 0;
265 }
266 
267 static __inline void
268 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
269 {
270 	KKASSERT((stage->stg_flags &
271 	    (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
272 	stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
273 	TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
274 }
275 
276 /*
277  * Schedule ifnet.if_start on the subqueue owner CPU
278  */
279 static void
280 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
281 {
282 	int cpu;
283 
284 	if (!force && curthread->td_type == TD_TYPE_NETISR &&
285 	    ifsq_stage_cntmax > 0) {
286 		struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
287 
288 		stage->stg_cnt = 0;
289 		stage->stg_len = 0;
290 		if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
291 			ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
292 		stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
293 		return;
294 	}
295 
296 	cpu = ifsq_get_cpuid(ifsq);
297 	if (cpu != mycpuid)
298 		lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
299 	else
300 		ifsq_ifstart_ipifunc(ifsq);
301 }
302 
303 /*
304  * NOTE:
305  * This function will release ifnet.if_start subqueue interlock,
306  * if ifnet.if_start for the subqueue does not need to be scheduled
307  */
308 static __inline int
309 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
310 {
311 	if (!running || ifsq_is_empty(ifsq)
312 #ifdef ALTQ
313 	    || ifsq->ifsq_altq->altq_tbr != NULL
314 #endif
315 	) {
316 		ALTQ_SQ_LOCK(ifsq);
317 		/*
318 		 * ifnet.if_start subqueue interlock is released, if:
319 		 * 1) Hardware can not take any packets, due to
320 		 *    o  interface is marked down
321 		 *    o  hardware queue is full (ifsq_is_oactive)
322 		 *    Under the second situation, hardware interrupt
323 		 *    or polling(4) will call/schedule ifnet.if_start
324 		 *    on the subqueue when hardware queue is ready
325 		 * 2) There is no packet in the subqueue.
326 		 *    Further ifq_dispatch or ifq_handoff will call/
327 		 *    schedule ifnet.if_start on the subqueue.
328 		 * 3) TBR is used and it does not allow further
329 		 *    dequeueing.
330 		 *    TBR callout will call ifnet.if_start on the
331 		 *    subqueue.
332 		 */
333 		if (!running || !ifsq_data_ready(ifsq)) {
334 			ifsq_clr_started(ifsq);
335 			ALTQ_SQ_UNLOCK(ifsq);
336 			return 0;
337 		}
338 		ALTQ_SQ_UNLOCK(ifsq);
339 	}
340 	return 1;
341 }
342 
343 static void
344 ifsq_ifstart_dispatch(netmsg_t msg)
345 {
346 	struct lwkt_msg *lmsg = &msg->base.lmsg;
347 	struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
348 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
349 	struct globaldata *gd = mycpu;
350 	int running = 0, need_sched;
351 
352 	crit_enter_gd(gd);
353 
354 	lwkt_replymsg(lmsg, 0);	/* reply ASAP */
355 
356 	if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
357 		/*
358 		 * We need to chase the subqueue owner CPU change.
359 		 */
360 		ifsq_ifstart_schedule(ifsq, 1);
361 		crit_exit_gd(gd);
362 		return;
363 	}
364 
365 	ifsq_serialize_hw(ifsq);
366 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
367 		ifp->if_start(ifp, ifsq);
368 		if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
369 			running = 1;
370 	}
371 	need_sched = ifsq_ifstart_need_schedule(ifsq, running);
372 	ifsq_deserialize_hw(ifsq);
373 
374 	if (need_sched) {
375 		/*
376 		 * More data need to be transmitted, ifnet.if_start is
377 		 * scheduled on the subqueue owner CPU, and we keep going.
378 		 * NOTE: ifnet.if_start subqueue interlock is not released.
379 		 */
380 		ifsq_ifstart_schedule(ifsq, 0);
381 	}
382 
383 	crit_exit_gd(gd);
384 }
385 
386 /* Device driver ifnet.if_start helper function */
387 void
388 ifsq_devstart(struct ifaltq_subque *ifsq)
389 {
390 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
391 	int running = 0;
392 
393 	ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
394 
395 	ALTQ_SQ_LOCK(ifsq);
396 	if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
397 		ALTQ_SQ_UNLOCK(ifsq);
398 		return;
399 	}
400 	ifsq_set_started(ifsq);
401 	ALTQ_SQ_UNLOCK(ifsq);
402 
403 	ifp->if_start(ifp, ifsq);
404 
405 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
406 		running = 1;
407 
408 	if (ifsq_ifstart_need_schedule(ifsq, running)) {
409 		/*
410 		 * More data need to be transmitted, ifnet.if_start is
411 		 * scheduled on ifnet's CPU, and we keep going.
412 		 * NOTE: ifnet.if_start interlock is not released.
413 		 */
414 		ifsq_ifstart_schedule(ifsq, 0);
415 	}
416 }
417 
418 void
419 if_devstart(struct ifnet *ifp)
420 {
421 	ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
422 }
423 
424 /* Device driver ifnet.if_start schedule helper function */
425 void
426 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
427 {
428 	ifsq_ifstart_schedule(ifsq, 1);
429 }
430 
431 void
432 if_devstart_sched(struct ifnet *ifp)
433 {
434 	ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
435 }
436 
437 static void
438 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
439 {
440 	lwkt_serialize_enter(ifp->if_serializer);
441 }
442 
443 static void
444 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
445 {
446 	lwkt_serialize_exit(ifp->if_serializer);
447 }
448 
449 static int
450 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
451 {
452 	return lwkt_serialize_try(ifp->if_serializer);
453 }
454 
455 #ifdef INVARIANTS
456 static void
457 if_default_serialize_assert(struct ifnet *ifp,
458 			    enum ifnet_serialize slz __unused,
459 			    boolean_t serialized)
460 {
461 	if (serialized)
462 		ASSERT_SERIALIZED(ifp->if_serializer);
463 	else
464 		ASSERT_NOT_SERIALIZED(ifp->if_serializer);
465 }
466 #endif
467 
468 /*
469  * Attach an interface to the list of "active" interfaces.
470  *
471  * The serializer is optional.
472  */
473 void
474 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
475 {
476 	unsigned socksize;
477 	int namelen, masklen;
478 	struct sockaddr_dl *sdl, *sdl_addr;
479 	struct ifaddr *ifa;
480 	struct ifaltq *ifq;
481 	struct ifnet **old_ifindex2ifnet = NULL;
482 	struct ifnet_array *old_ifnet_array;
483 	int i, q;
484 
485 	static int if_indexlim = 8;
486 
487 	if (ifp->if_serialize != NULL) {
488 		KASSERT(ifp->if_deserialize != NULL &&
489 			ifp->if_tryserialize != NULL &&
490 			ifp->if_serialize_assert != NULL,
491 			("serialize functions are partially setup"));
492 
493 		/*
494 		 * If the device supplies serialize functions,
495 		 * then clear if_serializer to catch any invalid
496 		 * usage of this field.
497 		 */
498 		KASSERT(serializer == NULL,
499 			("both serialize functions and default serializer "
500 			 "are supplied"));
501 		ifp->if_serializer = NULL;
502 	} else {
503 		KASSERT(ifp->if_deserialize == NULL &&
504 			ifp->if_tryserialize == NULL &&
505 			ifp->if_serialize_assert == NULL,
506 			("serialize functions are partially setup"));
507 		ifp->if_serialize = if_default_serialize;
508 		ifp->if_deserialize = if_default_deserialize;
509 		ifp->if_tryserialize = if_default_tryserialize;
510 #ifdef INVARIANTS
511 		ifp->if_serialize_assert = if_default_serialize_assert;
512 #endif
513 
514 		/*
515 		 * The serializer can be passed in from the device,
516 		 * allowing the same serializer to be used for both
517 		 * the interrupt interlock and the device queue.
518 		 * If not specified, the netif structure will use an
519 		 * embedded serializer.
520 		 */
521 		if (serializer == NULL) {
522 			serializer = &ifp->if_default_serializer;
523 			lwkt_serialize_init(serializer);
524 		}
525 		ifp->if_serializer = serializer;
526 	}
527 
528 	/*
529 	 * Make if_addrhead available on all CPUs, since they
530 	 * could be accessed by any threads.
531 	 */
532 	ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
533 				    M_IFADDR, M_WAITOK | M_ZERO);
534 	for (i = 0; i < ncpus; ++i)
535 		TAILQ_INIT(&ifp->if_addrheads[i]);
536 
537 	TAILQ_INIT(&ifp->if_multiaddrs);
538 	TAILQ_INIT(&ifp->if_groups);
539 	getmicrotime(&ifp->if_lastchange);
540 
541 	/*
542 	 * create a Link Level name for this device
543 	 */
544 	namelen = strlen(ifp->if_xname);
545 	masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
546 	socksize = masklen + ifp->if_addrlen;
547 	if (socksize < sizeof(*sdl))
548 		socksize = sizeof(*sdl);
549 	socksize = RT_ROUNDUP(socksize);
550 	ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
551 	sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
552 	sdl->sdl_len = socksize;
553 	sdl->sdl_family = AF_LINK;
554 	bcopy(ifp->if_xname, sdl->sdl_data, namelen);
555 	sdl->sdl_nlen = namelen;
556 	sdl->sdl_type = ifp->if_type;
557 	ifp->if_lladdr = ifa;
558 	ifa->ifa_ifp = ifp;
559 	ifa->ifa_rtrequest = link_rtrequest;
560 	ifa->ifa_addr = (struct sockaddr *)sdl;
561 	sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
562 	ifa->ifa_netmask = (struct sockaddr *)sdl;
563 	sdl->sdl_len = masklen;
564 	while (namelen != 0)
565 		sdl->sdl_data[--namelen] = 0xff;
566 	ifa_iflink(ifa, ifp, 0 /* Insert head */);
567 
568 	/*
569 	 * Make if_data available on all CPUs, since they could
570 	 * be updated by hardware interrupt routing, which could
571 	 * be bound to any CPU.
572 	 */
573 	ifp->if_data_pcpu = kmalloc_cachealign(
574 	    ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
575 
576 	if (ifp->if_mapsubq == NULL)
577 		ifp->if_mapsubq = ifq_mapsubq_default;
578 
579 	ifq = &ifp->if_snd;
580 	ifq->altq_type = 0;
581 	ifq->altq_disc = NULL;
582 	ifq->altq_flags &= ALTQF_CANTCHANGE;
583 	ifq->altq_tbr = NULL;
584 	ifq->altq_ifp = ifp;
585 
586 	if (ifq->altq_subq_cnt <= 0)
587 		ifq->altq_subq_cnt = 1;
588 	ifq->altq_subq = kmalloc_cachealign(
589 	    ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
590 	    M_DEVBUF, M_WAITOK | M_ZERO);
591 
592 	if (ifq->altq_maxlen == 0) {
593 		if_printf(ifp, "driver didn't set altq_maxlen\n");
594 		ifq_set_maxlen(ifq, ifqmaxlen);
595 	}
596 
597 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
598 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
599 
600 		ALTQ_SQ_LOCK_INIT(ifsq);
601 		ifsq->ifsq_index = q;
602 
603 		ifsq->ifsq_altq = ifq;
604 		ifsq->ifsq_ifp = ifp;
605 
606 		ifsq->ifsq_maxlen = ifq->altq_maxlen;
607 		ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
608 		ifsq->ifsq_prepended = NULL;
609 		ifsq->ifsq_started = 0;
610 		ifsq->ifsq_hw_oactive = 0;
611 		ifsq_set_cpuid(ifsq, 0);
612 		if (ifp->if_serializer != NULL)
613 			ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
614 
615 		/* XXX: netisr_ncpus */
616 		ifsq->ifsq_stage =
617 		    kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
618 		    M_DEVBUF, M_WAITOK | M_ZERO);
619 		for (i = 0; i < ncpus; ++i)
620 			ifsq->ifsq_stage[i].stg_subq = ifsq;
621 
622 		/*
623 		 * Allocate one if_start message for each CPU, since
624 		 * the hardware TX ring could be assigned to any CPU.
625 		 *
626 		 * NOTE:
627 		 * If the hardware TX ring polling CPU and the hardware
628 		 * TX ring interrupt CPU are same, one if_start message
629 		 * should be enough.
630 		 */
631 		ifsq->ifsq_ifstart_nmsg =
632 		    kmalloc(ncpus * sizeof(struct netmsg_base),
633 		    M_LWKTMSG, M_WAITOK);
634 		for (i = 0; i < ncpus; ++i) {
635 			netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
636 			    &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
637 			ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
638 		}
639 	}
640 	ifq_set_classic(ifq);
641 
642 	/*
643 	 * Increase mbuf cluster/jcluster limits for the mbufs that
644 	 * could sit on the device queues for quite some time.
645 	 */
646 	if (ifp->if_nmbclusters > 0)
647 		mcl_inclimit(ifp->if_nmbclusters);
648 	if (ifp->if_nmbjclusters > 0)
649 		mjcl_inclimit(ifp->if_nmbjclusters);
650 
651 	/*
652 	 * Install this ifp into ifindex2inet, ifnet queue and ifnet
653 	 * array after it is setup.
654 	 *
655 	 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
656 	 * by ifnet lock, so that non-netisr threads could get a
657 	 * consistent view.
658 	 */
659 	ifnet_lock();
660 
661 	/* Don't update if_index until ifindex2ifnet is setup */
662 	ifp->if_index = if_index + 1;
663 	sdl_addr->sdl_index = ifp->if_index;
664 
665 	/*
666 	 * Install this ifp into ifindex2ifnet
667 	 */
668 	if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
669 		unsigned int n;
670 		struct ifnet **q;
671 
672 		/*
673 		 * Grow ifindex2ifnet
674 		 */
675 		if_indexlim <<= 1;
676 		n = if_indexlim * sizeof(*q);
677 		q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
678 		if (ifindex2ifnet != NULL) {
679 			bcopy(ifindex2ifnet, q, n/2);
680 			/* Free old ifindex2ifnet after sync all netisrs */
681 			old_ifindex2ifnet = ifindex2ifnet;
682 		}
683 		ifindex2ifnet = q;
684 	}
685 	ifindex2ifnet[ifp->if_index] = ifp;
686 	/*
687 	 * Update if_index after this ifp is installed into ifindex2ifnet,
688 	 * so that netisrs could get a consistent view of ifindex2ifnet.
689 	 */
690 	cpu_sfence();
691 	if_index = ifp->if_index;
692 
693 	/*
694 	 * Install this ifp into ifnet array.
695 	 */
696 	/* Free old ifnet array after sync all netisrs */
697 	old_ifnet_array = ifnet_array;
698 	ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
699 
700 	/*
701 	 * Install this ifp into ifnet queue.
702 	 */
703 	TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
704 
705 	ifnet_unlock();
706 
707 	/*
708 	 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
709 	 * are no longer accessed and we can free them safely later on.
710 	 */
711 	netmsg_service_sync();
712 	if (old_ifindex2ifnet != NULL)
713 		kfree(old_ifindex2ifnet, M_IFADDR);
714 	ifnet_array_free(old_ifnet_array);
715 
716 	if (!SLIST_EMPTY(&domains))
717 		if_attachdomain1(ifp);
718 
719 	/* Announce the interface. */
720 	EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
721 	devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
722 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
723 }
724 
725 static void
726 if_attachdomain(void *dummy)
727 {
728 	struct ifnet *ifp;
729 
730 	ifnet_lock();
731 	TAILQ_FOREACH(ifp, &ifnetlist, if_list)
732 		if_attachdomain1(ifp);
733 	ifnet_unlock();
734 }
735 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
736 	if_attachdomain, NULL);
737 
738 static void
739 if_attachdomain1(struct ifnet *ifp)
740 {
741 	struct domain *dp;
742 
743 	crit_enter();
744 
745 	/* address family dependent data region */
746 	bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
747 	SLIST_FOREACH(dp, &domains, dom_next)
748 		if (dp->dom_ifattach)
749 			ifp->if_afdata[dp->dom_family] =
750 				(*dp->dom_ifattach)(ifp);
751 	crit_exit();
752 }
753 
754 /*
755  * Purge all addresses whose type is _not_ AF_LINK
756  */
757 static void
758 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
759 {
760 	struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
761 	struct ifaddr_container *ifac, *next;
762 
763 	ASSERT_NETISR0;
764 
765 	/*
766 	 * The ifaddr processing in the following loop will block,
767 	 * however, this function is called in netisr0, in which
768 	 * ifaddr list changes happen, so we don't care about the
769 	 * blockness of the ifaddr processing here.
770 	 */
771 	TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
772 			      ifa_link, next) {
773 		struct ifaddr *ifa = ifac->ifa;
774 
775 		/* Ignore marker */
776 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
777 			continue;
778 
779 		/* Leave link ifaddr as it is */
780 		if (ifa->ifa_addr->sa_family == AF_LINK)
781 			continue;
782 #ifdef INET
783 		/* XXX: Ugly!! ad hoc just for INET */
784 		if (ifa->ifa_addr->sa_family == AF_INET) {
785 			struct ifaliasreq ifr;
786 			struct sockaddr_in saved_addr, saved_dst;
787 #ifdef IFADDR_DEBUG_VERBOSE
788 			int i;
789 
790 			kprintf("purge in4 addr %p: ", ifa);
791 			for (i = 0; i < ncpus; ++i) {
792 				kprintf("%d ",
793 				    ifa->ifa_containers[i].ifa_refcnt);
794 			}
795 			kprintf("\n");
796 #endif
797 
798 			/* Save information for panic. */
799 			memcpy(&saved_addr, ifa->ifa_addr, sizeof(saved_addr));
800 			if (ifa->ifa_dstaddr != NULL) {
801 				memcpy(&saved_dst, ifa->ifa_dstaddr,
802 				    sizeof(saved_dst));
803 			} else {
804 				memset(&saved_dst, 0, sizeof(saved_dst));
805 			}
806 
807 			bzero(&ifr, sizeof ifr);
808 			ifr.ifra_addr = *ifa->ifa_addr;
809 			if (ifa->ifa_dstaddr)
810 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
811 			if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
812 				       NULL) == 0)
813 				continue;
814 
815 			/* MUST NOT HAPPEN */
816 			panic("%s: in_control failed %x, dst %x", ifp->if_xname,
817 			    ntohl(saved_addr.sin_addr.s_addr),
818 			    ntohl(saved_dst.sin_addr.s_addr));
819 		}
820 #endif /* INET */
821 #ifdef INET6
822 		if (ifa->ifa_addr->sa_family == AF_INET6) {
823 #ifdef IFADDR_DEBUG_VERBOSE
824 			int i;
825 
826 			kprintf("purge in6 addr %p: ", ifa);
827 			for (i = 0; i < ncpus; ++i) {
828 				kprintf("%d ",
829 				    ifa->ifa_containers[i].ifa_refcnt);
830 			}
831 			kprintf("\n");
832 #endif
833 
834 			in6_purgeaddr(ifa);
835 			/* ifp_addrhead is already updated */
836 			continue;
837 		}
838 #endif /* INET6 */
839 		if_printf(ifp, "destroy ifaddr family %d\n",
840 		    ifa->ifa_addr->sa_family);
841 		ifa_ifunlink(ifa, ifp);
842 		ifa_destroy(ifa);
843 	}
844 
845 	netisr_replymsg(&nmsg->base, 0);
846 }
847 
848 void
849 if_purgeaddrs_nolink(struct ifnet *ifp)
850 {
851 	struct netmsg_base nmsg;
852 
853 	netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
854 	    if_purgeaddrs_nolink_dispatch);
855 	nmsg.lmsg.u.ms_resultp = ifp;
856 	netisr_domsg(&nmsg, 0);
857 }
858 
859 static void
860 ifq_stage_detach_handler(netmsg_t nmsg)
861 {
862 	struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
863 	int q;
864 
865 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
866 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
867 		struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
868 
869 		if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
870 			ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
871 	}
872 	lwkt_replymsg(&nmsg->lmsg, 0);
873 }
874 
875 static void
876 ifq_stage_detach(struct ifaltq *ifq)
877 {
878 	struct netmsg_base base;
879 	int cpu;
880 
881 	netmsg_init(&base, NULL, &curthread->td_msgport, 0,
882 	    ifq_stage_detach_handler);
883 	base.lmsg.u.ms_resultp = ifq;
884 
885 	/* XXX netisr_ncpus */
886 	for (cpu = 0; cpu < ncpus; ++cpu)
887 		lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
888 }
889 
890 struct netmsg_if_rtdel {
891 	struct netmsg_base	base;
892 	struct ifnet		*ifp;
893 };
894 
895 static void
896 if_rtdel_dispatch(netmsg_t msg)
897 {
898 	struct netmsg_if_rtdel *rmsg = (void *)msg;
899 	int i, cpu;
900 
901 	cpu = mycpuid;
902 	ASSERT_NETISR_NCPUS(cpu);
903 
904 	for (i = 1; i <= AF_MAX; i++) {
905 		struct radix_node_head	*rnh;
906 
907 		if ((rnh = rt_tables[cpu][i]) == NULL)
908 			continue;
909 		rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
910 	}
911 	netisr_forwardmsg(&msg->base, cpu + 1);
912 }
913 
914 /*
915  * Detach an interface, removing it from the
916  * list of "active" interfaces.
917  */
918 void
919 if_detach(struct ifnet *ifp)
920 {
921 	struct ifnet_array *old_ifnet_array;
922 	struct netmsg_if_rtdel msg;
923 	struct domain *dp;
924 	int q;
925 
926 	/* Announce that the interface is gone. */
927 	EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
928 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
929 	devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
930 
931 	/*
932 	 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
933 	 * array before it is whacked.
934 	 *
935 	 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
936 	 * by ifnet lock, so that non-netisr threads could get a
937 	 * consistent view.
938 	 */
939 	ifnet_lock();
940 
941 	/*
942 	 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
943 	 */
944 	ifindex2ifnet[ifp->if_index] = NULL;
945 	while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
946 		if_index--;
947 
948 	/*
949 	 * Remove this ifp from ifnet queue.
950 	 */
951 	TAILQ_REMOVE(&ifnetlist, ifp, if_link);
952 
953 	/*
954 	 * Remove this ifp from ifnet array.
955 	 */
956 	/* Free old ifnet array after sync all netisrs */
957 	old_ifnet_array = ifnet_array;
958 	ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
959 
960 	ifnet_unlock();
961 
962 	/*
963 	 * Sync all netisrs so that the old ifnet array is no longer
964 	 * accessed and we can free it safely later on.
965 	 */
966 	netmsg_service_sync();
967 	ifnet_array_free(old_ifnet_array);
968 
969 	/*
970 	 * Remove routes and flush queues.
971 	 */
972 	crit_enter();
973 #ifdef IFPOLL_ENABLE
974 	if (ifp->if_flags & IFF_NPOLLING)
975 		ifpoll_deregister(ifp);
976 #endif
977 	if_down(ifp);
978 
979 	/* Decrease the mbuf clusters/jclusters limits increased by us */
980 	if (ifp->if_nmbclusters > 0)
981 		mcl_inclimit(-ifp->if_nmbclusters);
982 	if (ifp->if_nmbjclusters > 0)
983 		mjcl_inclimit(-ifp->if_nmbjclusters);
984 
985 #ifdef ALTQ
986 	if (ifq_is_enabled(&ifp->if_snd))
987 		altq_disable(&ifp->if_snd);
988 	if (ifq_is_attached(&ifp->if_snd))
989 		altq_detach(&ifp->if_snd);
990 #endif
991 
992 	/*
993 	 * Clean up all addresses.
994 	 */
995 	ifp->if_lladdr = NULL;
996 
997 	if_purgeaddrs_nolink(ifp);
998 	if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
999 		struct ifaddr *ifa;
1000 
1001 		ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1002 		KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
1003 			("non-link ifaddr is left on if_addrheads"));
1004 
1005 		ifa_ifunlink(ifa, ifp);
1006 		ifa_destroy(ifa);
1007 		KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1008 			("there are still ifaddrs left on if_addrheads"));
1009 	}
1010 
1011 #ifdef INET
1012 	/*
1013 	 * Remove all IPv4 kernel structures related to ifp.
1014 	 */
1015 	in_ifdetach(ifp);
1016 #endif
1017 
1018 #ifdef INET6
1019 	/*
1020 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
1021 	 * before removing routing entries below, since IPv6 interface direct
1022 	 * routes are expected to be removed by the IPv6-specific kernel API.
1023 	 * Otherwise, the kernel will detect some inconsistency and bark it.
1024 	 */
1025 	in6_ifdetach(ifp);
1026 #endif
1027 
1028 	/*
1029 	 * Delete all remaining routes using this interface
1030 	 */
1031 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1032 	    if_rtdel_dispatch);
1033 	msg.ifp = ifp;
1034 	netisr_domsg_global(&msg.base);
1035 
1036 	SLIST_FOREACH(dp, &domains, dom_next)
1037 		if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1038 			(*dp->dom_ifdetach)(ifp,
1039 				ifp->if_afdata[dp->dom_family]);
1040 
1041 	kfree(ifp->if_addrheads, M_IFADDR);
1042 
1043 	lwkt_synchronize_ipiqs("if_detach");
1044 	ifq_stage_detach(&ifp->if_snd);
1045 
1046 	for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1047 		struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1048 
1049 		kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1050 		kfree(ifsq->ifsq_stage, M_DEVBUF);
1051 	}
1052 	kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1053 
1054 	kfree(ifp->if_data_pcpu, M_DEVBUF);
1055 
1056 	crit_exit();
1057 }
1058 
1059 /*
1060  * Create interface group without members
1061  */
1062 struct ifg_group *
1063 if_creategroup(const char *groupname)
1064 {
1065         struct ifg_group        *ifg = NULL;
1066 
1067         if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1068             M_TEMP, M_NOWAIT)) == NULL)
1069                 return (NULL);
1070 
1071         strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1072         ifg->ifg_refcnt = 0;
1073         ifg->ifg_carp_demoted = 0;
1074         TAILQ_INIT(&ifg->ifg_members);
1075 #if NPF > 0
1076         pfi_attach_ifgroup(ifg);
1077 #endif
1078         TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1079 
1080         return (ifg);
1081 }
1082 
1083 /*
1084  * Add a group to an interface
1085  */
1086 int
1087 if_addgroup(struct ifnet *ifp, const char *groupname)
1088 {
1089 	struct ifg_list		*ifgl;
1090 	struct ifg_group	*ifg = NULL;
1091 	struct ifg_member	*ifgm;
1092 
1093 	if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1094 	    groupname[strlen(groupname) - 1] <= '9')
1095 		return (EINVAL);
1096 
1097 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1098 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1099 			return (EEXIST);
1100 
1101 	if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1102 		return (ENOMEM);
1103 
1104 	if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1105 		kfree(ifgl, M_TEMP);
1106 		return (ENOMEM);
1107 	}
1108 
1109 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1110 		if (!strcmp(ifg->ifg_group, groupname))
1111 			break;
1112 
1113 	if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1114 		kfree(ifgl, M_TEMP);
1115 		kfree(ifgm, M_TEMP);
1116 		return (ENOMEM);
1117 	}
1118 
1119 	ifg->ifg_refcnt++;
1120 	ifgl->ifgl_group = ifg;
1121 	ifgm->ifgm_ifp = ifp;
1122 
1123 	TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1124 	TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1125 
1126 #if NPF > 0
1127 	pfi_group_change(groupname);
1128 #endif
1129 
1130 	return (0);
1131 }
1132 
1133 /*
1134  * Remove a group from an interface
1135  */
1136 int
1137 if_delgroup(struct ifnet *ifp, const char *groupname)
1138 {
1139 	struct ifg_list		*ifgl;
1140 	struct ifg_member	*ifgm;
1141 
1142 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1143 		if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1144 			break;
1145 	if (ifgl == NULL)
1146 		return (ENOENT);
1147 
1148 	TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1149 
1150 	TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1151 		if (ifgm->ifgm_ifp == ifp)
1152 			break;
1153 
1154 	if (ifgm != NULL) {
1155 		TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1156 		kfree(ifgm, M_TEMP);
1157 	}
1158 
1159 	if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1160 		TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1161 #if NPF > 0
1162 		pfi_detach_ifgroup(ifgl->ifgl_group);
1163 #endif
1164 		kfree(ifgl->ifgl_group, M_TEMP);
1165 	}
1166 
1167 	kfree(ifgl, M_TEMP);
1168 
1169 #if NPF > 0
1170 	pfi_group_change(groupname);
1171 #endif
1172 
1173 	return (0);
1174 }
1175 
1176 /*
1177  * Stores all groups from an interface in memory pointed
1178  * to by data
1179  */
1180 int
1181 if_getgroup(caddr_t data, struct ifnet *ifp)
1182 {
1183 	int			 len, error;
1184 	struct ifg_list		*ifgl;
1185 	struct ifg_req		 ifgrq, *ifgp;
1186 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
1187 
1188 	if (ifgr->ifgr_len == 0) {
1189 		TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1190 			ifgr->ifgr_len += sizeof(struct ifg_req);
1191 		return (0);
1192 	}
1193 
1194 	len = ifgr->ifgr_len;
1195 	ifgp = ifgr->ifgr_groups;
1196 	TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1197 		if (len < sizeof(ifgrq))
1198 			return (EINVAL);
1199 		bzero(&ifgrq, sizeof ifgrq);
1200 		strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1201 		    sizeof(ifgrq.ifgrq_group));
1202 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1203 		    sizeof(struct ifg_req))))
1204 			return (error);
1205 		len -= sizeof(ifgrq);
1206 		ifgp++;
1207 	}
1208 
1209 	return (0);
1210 }
1211 
1212 /*
1213  * Stores all members of a group in memory pointed to by data
1214  */
1215 int
1216 if_getgroupmembers(caddr_t data)
1217 {
1218 	struct ifgroupreq	*ifgr = (struct ifgroupreq *)data;
1219 	struct ifg_group	*ifg;
1220 	struct ifg_member	*ifgm;
1221 	struct ifg_req		 ifgrq, *ifgp;
1222 	int			 len, error;
1223 
1224 	TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1225 		if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1226 			break;
1227 	if (ifg == NULL)
1228 		return (ENOENT);
1229 
1230 	if (ifgr->ifgr_len == 0) {
1231 		TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1232 			ifgr->ifgr_len += sizeof(ifgrq);
1233 		return (0);
1234 	}
1235 
1236 	len = ifgr->ifgr_len;
1237 	ifgp = ifgr->ifgr_groups;
1238 	TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1239 		if (len < sizeof(ifgrq))
1240 			return (EINVAL);
1241 		bzero(&ifgrq, sizeof ifgrq);
1242 		strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1243 		    sizeof(ifgrq.ifgrq_member));
1244 		if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1245 		    sizeof(struct ifg_req))))
1246 			return (error);
1247 		len -= sizeof(ifgrq);
1248 		ifgp++;
1249 	}
1250 
1251 	return (0);
1252 }
1253 
1254 /*
1255  * Delete Routes for a Network Interface
1256  *
1257  * Called for each routing entry via the rnh->rnh_walktree() call above
1258  * to delete all route entries referencing a detaching network interface.
1259  *
1260  * Arguments:
1261  *	rn	pointer to node in the routing table
1262  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
1263  *
1264  * Returns:
1265  *	0	successful
1266  *	errno	failed - reason indicated
1267  *
1268  */
1269 static int
1270 if_rtdel(struct radix_node *rn, void *arg)
1271 {
1272 	struct rtentry	*rt = (struct rtentry *)rn;
1273 	struct ifnet	*ifp = arg;
1274 	int		err;
1275 
1276 	if (rt->rt_ifp == ifp) {
1277 
1278 		/*
1279 		 * Protect (sorta) against walktree recursion problems
1280 		 * with cloned routes
1281 		 */
1282 		if (!(rt->rt_flags & RTF_UP))
1283 			return (0);
1284 
1285 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1286 				rt_mask(rt), rt->rt_flags,
1287 				NULL);
1288 		if (err) {
1289 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
1290 		}
1291 	}
1292 
1293 	return (0);
1294 }
1295 
1296 static __inline boolean_t
1297 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1298 {
1299 	if (old_ifa == NULL)
1300 		return TRUE;
1301 
1302 	if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1303 	    (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1304 		return TRUE;
1305 	if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1306 	    (cur_ifa->ifa_flags & IFA_ROUTE))
1307 		return TRUE;
1308 	return FALSE;
1309 }
1310 
1311 /*
1312  * Locate an interface based on a complete address.
1313  */
1314 struct ifaddr *
1315 ifa_ifwithaddr(struct sockaddr *addr)
1316 {
1317 	const struct ifnet_array *arr;
1318 	int i;
1319 
1320 	arr = ifnet_array_get();
1321 	for (i = 0; i < arr->ifnet_count; ++i) {
1322 		struct ifnet *ifp = arr->ifnet_arr[i];
1323 		struct ifaddr_container *ifac;
1324 
1325 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1326 			struct ifaddr *ifa = ifac->ifa;
1327 
1328 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1329 				continue;
1330 			if (sa_equal(addr, ifa->ifa_addr))
1331 				return (ifa);
1332 			if ((ifp->if_flags & IFF_BROADCAST) &&
1333 			    ifa->ifa_broadaddr &&
1334 			    /* IPv6 doesn't have broadcast */
1335 			    ifa->ifa_broadaddr->sa_len != 0 &&
1336 			    sa_equal(ifa->ifa_broadaddr, addr))
1337 				return (ifa);
1338 		}
1339 	}
1340 	return (NULL);
1341 }
1342 
1343 /*
1344  * Locate the point to point interface with a given destination address.
1345  */
1346 struct ifaddr *
1347 ifa_ifwithdstaddr(struct sockaddr *addr)
1348 {
1349 	const struct ifnet_array *arr;
1350 	int i;
1351 
1352 	arr = ifnet_array_get();
1353 	for (i = 0; i < arr->ifnet_count; ++i) {
1354 		struct ifnet *ifp = arr->ifnet_arr[i];
1355 		struct ifaddr_container *ifac;
1356 
1357 		if (!(ifp->if_flags & IFF_POINTOPOINT))
1358 			continue;
1359 
1360 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1361 			struct ifaddr *ifa = ifac->ifa;
1362 
1363 			if (ifa->ifa_addr->sa_family != addr->sa_family)
1364 				continue;
1365 			if (ifa->ifa_dstaddr &&
1366 			    sa_equal(addr, ifa->ifa_dstaddr))
1367 				return (ifa);
1368 		}
1369 	}
1370 	return (NULL);
1371 }
1372 
1373 /*
1374  * Find an interface on a specific network.  If many, choice
1375  * is most specific found.
1376  */
1377 struct ifaddr *
1378 ifa_ifwithnet(struct sockaddr *addr)
1379 {
1380 	struct ifaddr *ifa_maybe = NULL;
1381 	u_int af = addr->sa_family;
1382 	char *addr_data = addr->sa_data, *cplim;
1383 	const struct ifnet_array *arr;
1384 	int i;
1385 
1386 	/*
1387 	 * AF_LINK addresses can be looked up directly by their index number,
1388 	 * so do that if we can.
1389 	 */
1390 	if (af == AF_LINK) {
1391 		struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1392 
1393 		if (sdl->sdl_index && sdl->sdl_index <= if_index)
1394 			return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1395 	}
1396 
1397 	/*
1398 	 * Scan though each interface, looking for ones that have
1399 	 * addresses in this address family.
1400 	 */
1401 	arr = ifnet_array_get();
1402 	for (i = 0; i < arr->ifnet_count; ++i) {
1403 		struct ifnet *ifp = arr->ifnet_arr[i];
1404 		struct ifaddr_container *ifac;
1405 
1406 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1407 			struct ifaddr *ifa = ifac->ifa;
1408 			char *cp, *cp2, *cp3;
1409 
1410 			if (ifa->ifa_addr->sa_family != af)
1411 next:				continue;
1412 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1413 				/*
1414 				 * This is a bit broken as it doesn't
1415 				 * take into account that the remote end may
1416 				 * be a single node in the network we are
1417 				 * looking for.
1418 				 * The trouble is that we don't know the
1419 				 * netmask for the remote end.
1420 				 */
1421 				if (ifa->ifa_dstaddr != NULL &&
1422 				    sa_equal(addr, ifa->ifa_dstaddr))
1423 					return (ifa);
1424 			} else {
1425 				/*
1426 				 * if we have a special address handler,
1427 				 * then use it instead of the generic one.
1428 				 */
1429 				if (ifa->ifa_claim_addr) {
1430 					if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1431 						return (ifa);
1432 					} else {
1433 						continue;
1434 					}
1435 				}
1436 
1437 				/*
1438 				 * Scan all the bits in the ifa's address.
1439 				 * If a bit dissagrees with what we are
1440 				 * looking for, mask it with the netmask
1441 				 * to see if it really matters.
1442 				 * (A byte at a time)
1443 				 */
1444 				if (ifa->ifa_netmask == 0)
1445 					continue;
1446 				cp = addr_data;
1447 				cp2 = ifa->ifa_addr->sa_data;
1448 				cp3 = ifa->ifa_netmask->sa_data;
1449 				cplim = ifa->ifa_netmask->sa_len +
1450 					(char *)ifa->ifa_netmask;
1451 				while (cp3 < cplim)
1452 					if ((*cp++ ^ *cp2++) & *cp3++)
1453 						goto next; /* next address! */
1454 				/*
1455 				 * If the netmask of what we just found
1456 				 * is more specific than what we had before
1457 				 * (if we had one) then remember the new one
1458 				 * before continuing to search for an even
1459 				 * better one.  If the netmasks are equal,
1460 				 * we prefer the this ifa based on the result
1461 				 * of ifa_prefer().
1462 				 */
1463 				if (ifa_maybe == NULL ||
1464 				    rn_refines((char *)ifa->ifa_netmask,
1465 				        (char *)ifa_maybe->ifa_netmask) ||
1466 				    (sa_equal(ifa_maybe->ifa_netmask,
1467 				        ifa->ifa_netmask) &&
1468 				     ifa_prefer(ifa, ifa_maybe)))
1469 					ifa_maybe = ifa;
1470 			}
1471 		}
1472 	}
1473 	return (ifa_maybe);
1474 }
1475 
1476 /*
1477  * Find an interface address specific to an interface best matching
1478  * a given address.
1479  */
1480 struct ifaddr *
1481 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1482 {
1483 	struct ifaddr_container *ifac;
1484 	char *cp, *cp2, *cp3;
1485 	char *cplim;
1486 	struct ifaddr *ifa_maybe = NULL;
1487 	u_int af = addr->sa_family;
1488 
1489 	if (af >= AF_MAX)
1490 		return (0);
1491 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1492 		struct ifaddr *ifa = ifac->ifa;
1493 
1494 		if (ifa->ifa_addr->sa_family != af)
1495 			continue;
1496 		if (ifa_maybe == NULL)
1497 			ifa_maybe = ifa;
1498 		if (ifa->ifa_netmask == NULL) {
1499 			if (sa_equal(addr, ifa->ifa_addr) ||
1500 			    (ifa->ifa_dstaddr != NULL &&
1501 			     sa_equal(addr, ifa->ifa_dstaddr)))
1502 				return (ifa);
1503 			continue;
1504 		}
1505 		if (ifp->if_flags & IFF_POINTOPOINT) {
1506 			if (sa_equal(addr, ifa->ifa_dstaddr))
1507 				return (ifa);
1508 		} else {
1509 			cp = addr->sa_data;
1510 			cp2 = ifa->ifa_addr->sa_data;
1511 			cp3 = ifa->ifa_netmask->sa_data;
1512 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1513 			for (; cp3 < cplim; cp3++)
1514 				if ((*cp++ ^ *cp2++) & *cp3)
1515 					break;
1516 			if (cp3 == cplim)
1517 				return (ifa);
1518 		}
1519 	}
1520 	return (ifa_maybe);
1521 }
1522 
1523 /*
1524  * Default action when installing a route with a Link Level gateway.
1525  * Lookup an appropriate real ifa to point to.
1526  * This should be moved to /sys/net/link.c eventually.
1527  */
1528 static void
1529 link_rtrequest(int cmd, struct rtentry *rt)
1530 {
1531 	struct ifaddr *ifa;
1532 	struct sockaddr *dst;
1533 	struct ifnet *ifp;
1534 
1535 	if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1536 	    (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1537 		return;
1538 	ifa = ifaof_ifpforaddr(dst, ifp);
1539 	if (ifa != NULL) {
1540 		IFAFREE(rt->rt_ifa);
1541 		IFAREF(ifa);
1542 		rt->rt_ifa = ifa;
1543 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1544 			ifa->ifa_rtrequest(cmd, rt);
1545 	}
1546 }
1547 
1548 struct netmsg_ifroute {
1549 	struct netmsg_base	base;
1550 	struct ifnet		*ifp;
1551 	int			flag;
1552 	int			fam;
1553 };
1554 
1555 /*
1556  * Mark an interface down and notify protocols of the transition.
1557  */
1558 static void
1559 if_unroute_dispatch(netmsg_t nmsg)
1560 {
1561 	struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1562 	struct ifnet *ifp = msg->ifp;
1563 	int flag = msg->flag, fam = msg->fam;
1564 	struct ifaddr_container *ifac;
1565 
1566 	ASSERT_NETISR0;
1567 
1568 	ifp->if_flags &= ~flag;
1569 	getmicrotime(&ifp->if_lastchange);
1570 	/*
1571 	 * The ifaddr processing in the following loop will block,
1572 	 * however, this function is called in netisr0, in which
1573 	 * ifaddr list changes happen, so we don't care about the
1574 	 * blockness of the ifaddr processing here.
1575 	 */
1576 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1577 		struct ifaddr *ifa = ifac->ifa;
1578 
1579 		/* Ignore marker */
1580 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1581 			continue;
1582 
1583 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1584 			kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1585 	}
1586 	ifq_purge_all(&ifp->if_snd);
1587 	rt_ifmsg(ifp);
1588 
1589 	netisr_replymsg(&nmsg->base, 0);
1590 }
1591 
1592 void
1593 if_unroute(struct ifnet *ifp, int flag, int fam)
1594 {
1595 	struct netmsg_ifroute msg;
1596 
1597 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1598 	    if_unroute_dispatch);
1599 	msg.ifp = ifp;
1600 	msg.flag = flag;
1601 	msg.fam = fam;
1602 	netisr_domsg(&msg.base, 0);
1603 }
1604 
1605 /*
1606  * Mark an interface up and notify protocols of the transition.
1607  */
1608 static void
1609 if_route_dispatch(netmsg_t nmsg)
1610 {
1611 	struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1612 	struct ifnet *ifp = msg->ifp;
1613 	int flag = msg->flag, fam = msg->fam;
1614 	struct ifaddr_container *ifac;
1615 
1616 	ASSERT_NETISR0;
1617 
1618 	ifq_purge_all(&ifp->if_snd);
1619 	ifp->if_flags |= flag;
1620 	getmicrotime(&ifp->if_lastchange);
1621 	/*
1622 	 * The ifaddr processing in the following loop will block,
1623 	 * however, this function is called in netisr0, in which
1624 	 * ifaddr list changes happen, so we don't care about the
1625 	 * blockness of the ifaddr processing here.
1626 	 */
1627 	TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1628 		struct ifaddr *ifa = ifac->ifa;
1629 
1630 		/* Ignore marker */
1631 		if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1632 			continue;
1633 
1634 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1635 			kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1636 	}
1637 	rt_ifmsg(ifp);
1638 #ifdef INET6
1639 	in6_if_up(ifp);
1640 #endif
1641 
1642 	netisr_replymsg(&nmsg->base, 0);
1643 }
1644 
1645 void
1646 if_route(struct ifnet *ifp, int flag, int fam)
1647 {
1648 	struct netmsg_ifroute msg;
1649 
1650 	netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1651 	    if_route_dispatch);
1652 	msg.ifp = ifp;
1653 	msg.flag = flag;
1654 	msg.fam = fam;
1655 	netisr_domsg(&msg.base, 0);
1656 }
1657 
1658 /*
1659  * Mark an interface down and notify protocols of the transition.  An
1660  * interface going down is also considered to be a synchronizing event.
1661  * We must ensure that all packet processing related to the interface
1662  * has completed before we return so e.g. the caller can free the ifnet
1663  * structure that the mbufs may be referencing.
1664  *
1665  * NOTE: must be called at splnet or eqivalent.
1666  */
1667 void
1668 if_down(struct ifnet *ifp)
1669 {
1670 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1671 	netmsg_service_sync();
1672 }
1673 
1674 /*
1675  * Mark an interface up and notify protocols of
1676  * the transition.
1677  * NOTE: must be called at splnet or eqivalent.
1678  */
1679 void
1680 if_up(struct ifnet *ifp)
1681 {
1682 	if_route(ifp, IFF_UP, AF_UNSPEC);
1683 }
1684 
1685 /*
1686  * Process a link state change.
1687  * NOTE: must be called at splsoftnet or equivalent.
1688  */
1689 void
1690 if_link_state_change(struct ifnet *ifp)
1691 {
1692 	int link_state = ifp->if_link_state;
1693 
1694 	rt_ifmsg(ifp);
1695 	devctl_notify("IFNET", ifp->if_xname,
1696 	    (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1697 }
1698 
1699 /*
1700  * Handle interface watchdog timer routines.  Called
1701  * from softclock, we decrement timers (if set) and
1702  * call the appropriate interface routine on expiration.
1703  */
1704 static void
1705 if_slowtimo_dispatch(netmsg_t nmsg)
1706 {
1707 	struct globaldata *gd = mycpu;
1708 	const struct ifnet_array *arr;
1709 	int i;
1710 
1711 	ASSERT_NETISR0;
1712 
1713 	crit_enter_gd(gd);
1714 	lwkt_replymsg(&nmsg->lmsg, 0);  /* reply ASAP */
1715 	crit_exit_gd(gd);
1716 
1717 	arr = ifnet_array_get();
1718 	for (i = 0; i < arr->ifnet_count; ++i) {
1719 		struct ifnet *ifp = arr->ifnet_arr[i];
1720 
1721 		crit_enter_gd(gd);
1722 
1723 		if (if_stats_compat) {
1724 			IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1725 			IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1726 			IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1727 			IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1728 			IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1729 			IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1730 			IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1731 			IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1732 			IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1733 			IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1734 			IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1735 			IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1736 		}
1737 
1738 		if (ifp->if_timer == 0 || --ifp->if_timer) {
1739 			crit_exit_gd(gd);
1740 			continue;
1741 		}
1742 		if (ifp->if_watchdog) {
1743 			if (ifnet_tryserialize_all(ifp)) {
1744 				(*ifp->if_watchdog)(ifp);
1745 				ifnet_deserialize_all(ifp);
1746 			} else {
1747 				/* try again next timeout */
1748 				++ifp->if_timer;
1749 			}
1750 		}
1751 
1752 		crit_exit_gd(gd);
1753 	}
1754 
1755 	callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1756 }
1757 
1758 static void
1759 if_slowtimo(void *arg __unused)
1760 {
1761 	struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1762 
1763 	KASSERT(mycpuid == 0, ("not on cpu0"));
1764 	crit_enter();
1765 	if (lmsg->ms_flags & MSGF_DONE)
1766 		lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1767 	crit_exit();
1768 }
1769 
1770 /*
1771  * Map interface name to
1772  * interface structure pointer.
1773  */
1774 struct ifnet *
1775 ifunit(const char *name)
1776 {
1777 	struct ifnet *ifp;
1778 
1779 	/*
1780 	 * Search all the interfaces for this name/number
1781 	 */
1782 	KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1783 
1784 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1785 		if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1786 			break;
1787 	}
1788 	return (ifp);
1789 }
1790 
1791 struct ifnet *
1792 ifunit_netisr(const char *name)
1793 {
1794 	const struct ifnet_array *arr;
1795 	int i;
1796 
1797 	/*
1798 	 * Search all the interfaces for this name/number
1799 	 */
1800 
1801 	arr = ifnet_array_get();
1802 	for (i = 0; i < arr->ifnet_count; ++i) {
1803 		struct ifnet *ifp = arr->ifnet_arr[i];
1804 
1805 		if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1806 			return ifp;
1807 	}
1808 	return NULL;
1809 }
1810 
1811 /*
1812  * Interface ioctls.
1813  */
1814 int
1815 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1816 {
1817 	struct ifnet *ifp;
1818 	struct ifreq *ifr;
1819 	struct ifstat *ifs;
1820 	int error, do_ifup = 0;
1821 	short oif_flags;
1822 	int new_flags;
1823 	size_t namelen, onamelen;
1824 	char new_name[IFNAMSIZ];
1825 	struct ifaddr *ifa;
1826 	struct sockaddr_dl *sdl;
1827 
1828 	switch (cmd) {
1829 	case SIOCGIFCONF:
1830 	case OSIOCGIFCONF:
1831 		return (ifconf(cmd, data, cred));
1832 	default:
1833 		break;
1834 	}
1835 
1836 	ifr = (struct ifreq *)data;
1837 
1838 	switch (cmd) {
1839 	case SIOCIFCREATE:
1840 	case SIOCIFCREATE2:
1841 		if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1842 			return (error);
1843 		return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1844 		    	cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1845 	case SIOCIFDESTROY:
1846 		if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1847 			return (error);
1848 		return (if_clone_destroy(ifr->ifr_name));
1849 	case SIOCIFGCLONERS:
1850 		return (if_clone_list((struct if_clonereq *)data));
1851 	default:
1852 		break;
1853 	}
1854 
1855 	/*
1856 	 * Nominal ioctl through interface, lookup the ifp and obtain a
1857 	 * lock to serialize the ifconfig ioctl operation.
1858 	 */
1859 	ifnet_lock();
1860 
1861 	ifp = ifunit(ifr->ifr_name);
1862 	if (ifp == NULL) {
1863 		ifnet_unlock();
1864 		return (ENXIO);
1865 	}
1866 	error = 0;
1867 
1868 	switch (cmd) {
1869 	case SIOCGIFINDEX:
1870 		ifr->ifr_index = ifp->if_index;
1871 		break;
1872 
1873 	case SIOCGIFFLAGS:
1874 		ifr->ifr_flags = ifp->if_flags;
1875 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1876 		break;
1877 
1878 	case SIOCGIFCAP:
1879 		ifr->ifr_reqcap = ifp->if_capabilities;
1880 		ifr->ifr_curcap = ifp->if_capenable;
1881 		break;
1882 
1883 	case SIOCGIFMETRIC:
1884 		ifr->ifr_metric = ifp->if_metric;
1885 		break;
1886 
1887 	case SIOCGIFMTU:
1888 		ifr->ifr_mtu = ifp->if_mtu;
1889 		break;
1890 
1891 	case SIOCGIFTSOLEN:
1892 		ifr->ifr_tsolen = ifp->if_tsolen;
1893 		break;
1894 
1895 	case SIOCGIFDATA:
1896 		error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1897 				sizeof(ifp->if_data));
1898 		break;
1899 
1900 	case SIOCGIFPHYS:
1901 		ifr->ifr_phys = ifp->if_physical;
1902 		break;
1903 
1904 	case SIOCGIFPOLLCPU:
1905 		ifr->ifr_pollcpu = -1;
1906 		break;
1907 
1908 	case SIOCSIFPOLLCPU:
1909 		break;
1910 
1911 	case SIOCSIFFLAGS:
1912 		error = priv_check_cred(cred, PRIV_ROOT, 0);
1913 		if (error)
1914 			break;
1915 		new_flags = (ifr->ifr_flags & 0xffff) |
1916 		    (ifr->ifr_flagshigh << 16);
1917 		if (ifp->if_flags & IFF_SMART) {
1918 			/* Smart drivers twiddle their own routes */
1919 		} else if (ifp->if_flags & IFF_UP &&
1920 		    (new_flags & IFF_UP) == 0) {
1921 			if_down(ifp);
1922 		} else if (new_flags & IFF_UP &&
1923 		    (ifp->if_flags & IFF_UP) == 0) {
1924 			do_ifup = 1;
1925 		}
1926 
1927 #ifdef IFPOLL_ENABLE
1928 		if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1929 			if (new_flags & IFF_NPOLLING)
1930 				ifpoll_register(ifp);
1931 			else
1932 				ifpoll_deregister(ifp);
1933 		}
1934 #endif
1935 
1936 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1937 			(new_flags &~ IFF_CANTCHANGE);
1938 		if (new_flags & IFF_PPROMISC) {
1939 			/* Permanently promiscuous mode requested */
1940 			ifp->if_flags |= IFF_PROMISC;
1941 		} else if (ifp->if_pcount == 0) {
1942 			ifp->if_flags &= ~IFF_PROMISC;
1943 		}
1944 		if (ifp->if_ioctl) {
1945 			ifnet_serialize_all(ifp);
1946 			ifp->if_ioctl(ifp, cmd, data, cred);
1947 			ifnet_deserialize_all(ifp);
1948 		}
1949 		if (do_ifup)
1950 			if_up(ifp);
1951 		getmicrotime(&ifp->if_lastchange);
1952 		break;
1953 
1954 	case SIOCSIFCAP:
1955 		error = priv_check_cred(cred, PRIV_ROOT, 0);
1956 		if (error)
1957 			break;
1958 		if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1959 			error = EINVAL;
1960 			break;
1961 		}
1962 		ifnet_serialize_all(ifp);
1963 		ifp->if_ioctl(ifp, cmd, data, cred);
1964 		ifnet_deserialize_all(ifp);
1965 		break;
1966 
1967 	case SIOCSIFNAME:
1968 		error = priv_check_cred(cred, PRIV_ROOT, 0);
1969 		if (error)
1970 			break;
1971 		error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1972 		if (error)
1973 			break;
1974 		if (new_name[0] == '\0') {
1975 			error = EINVAL;
1976 			break;
1977 		}
1978 		if (ifunit(new_name) != NULL) {
1979 			error = EEXIST;
1980 			break;
1981 		}
1982 
1983 		EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1984 
1985 		/* Announce the departure of the interface. */
1986 		rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1987 
1988 		strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1989 		ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1990 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1991 		namelen = strlen(new_name);
1992 		onamelen = sdl->sdl_nlen;
1993 		/*
1994 		 * Move the address if needed.  This is safe because we
1995 		 * allocate space for a name of length IFNAMSIZ when we
1996 		 * create this in if_attach().
1997 		 */
1998 		if (namelen != onamelen) {
1999 			bcopy(sdl->sdl_data + onamelen,
2000 			    sdl->sdl_data + namelen, sdl->sdl_alen);
2001 		}
2002 		bcopy(new_name, sdl->sdl_data, namelen);
2003 		sdl->sdl_nlen = namelen;
2004 		sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2005 		bzero(sdl->sdl_data, onamelen);
2006 		while (namelen != 0)
2007 			sdl->sdl_data[--namelen] = 0xff;
2008 
2009 		EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2010 
2011 		/* Announce the return of the interface. */
2012 		rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2013 		break;
2014 
2015 	case SIOCSIFMETRIC:
2016 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2017 		if (error)
2018 			break;
2019 		ifp->if_metric = ifr->ifr_metric;
2020 		getmicrotime(&ifp->if_lastchange);
2021 		break;
2022 
2023 	case SIOCSIFPHYS:
2024 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2025 		if (error)
2026 			break;
2027 		if (ifp->if_ioctl == NULL) {
2028 		        error = EOPNOTSUPP;
2029 			break;
2030 		}
2031 		ifnet_serialize_all(ifp);
2032 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2033 		ifnet_deserialize_all(ifp);
2034 		if (error == 0)
2035 			getmicrotime(&ifp->if_lastchange);
2036 		break;
2037 
2038 	case SIOCSIFMTU:
2039 	{
2040 		u_long oldmtu = ifp->if_mtu;
2041 
2042 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2043 		if (error)
2044 			break;
2045 		if (ifp->if_ioctl == NULL) {
2046 			error = EOPNOTSUPP;
2047 			break;
2048 		}
2049 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2050 			error = EINVAL;
2051 			break;
2052 		}
2053 		ifnet_serialize_all(ifp);
2054 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2055 		ifnet_deserialize_all(ifp);
2056 		if (error == 0) {
2057 			getmicrotime(&ifp->if_lastchange);
2058 			rt_ifmsg(ifp);
2059 		}
2060 		/*
2061 		 * If the link MTU changed, do network layer specific procedure.
2062 		 */
2063 		if (ifp->if_mtu != oldmtu) {
2064 #ifdef INET6
2065 			nd6_setmtu(ifp);
2066 #endif
2067 		}
2068 		break;
2069 	}
2070 
2071 	case SIOCSIFTSOLEN:
2072 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2073 		if (error)
2074 			break;
2075 
2076 		/* XXX need driver supplied upper limit */
2077 		if (ifr->ifr_tsolen <= 0) {
2078 			error = EINVAL;
2079 			break;
2080 		}
2081 		ifp->if_tsolen = ifr->ifr_tsolen;
2082 		break;
2083 
2084 	case SIOCADDMULTI:
2085 	case SIOCDELMULTI:
2086 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2087 		if (error)
2088 			break;
2089 
2090 		/* Don't allow group membership on non-multicast interfaces. */
2091 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2092 			error = EOPNOTSUPP;
2093 			break;
2094 		}
2095 
2096 		/* Don't let users screw up protocols' entries. */
2097 		if (ifr->ifr_addr.sa_family != AF_LINK) {
2098 			error = EINVAL;
2099 			break;
2100 		}
2101 
2102 		if (cmd == SIOCADDMULTI) {
2103 			struct ifmultiaddr *ifma;
2104 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2105 		} else {
2106 			error = if_delmulti(ifp, &ifr->ifr_addr);
2107 		}
2108 		if (error == 0)
2109 			getmicrotime(&ifp->if_lastchange);
2110 		break;
2111 
2112 	case SIOCSIFPHYADDR:
2113 	case SIOCDIFPHYADDR:
2114 #ifdef INET6
2115 	case SIOCSIFPHYADDR_IN6:
2116 #endif
2117 	case SIOCSLIFPHYADDR:
2118         case SIOCSIFMEDIA:
2119 	case SIOCSIFGENERIC:
2120 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2121 		if (error)
2122 			break;
2123 		if (ifp->if_ioctl == 0) {
2124 			error = EOPNOTSUPP;
2125 			break;
2126 		}
2127 		ifnet_serialize_all(ifp);
2128 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2129 		ifnet_deserialize_all(ifp);
2130 		if (error == 0)
2131 			getmicrotime(&ifp->if_lastchange);
2132 		break;
2133 
2134 	case SIOCGIFSTATUS:
2135 		ifs = (struct ifstat *)data;
2136 		ifs->ascii[0] = '\0';
2137 		/* fall through */
2138 	case SIOCGIFPSRCADDR:
2139 	case SIOCGIFPDSTADDR:
2140 	case SIOCGLIFPHYADDR:
2141 	case SIOCGIFMEDIA:
2142 	case SIOCGIFGENERIC:
2143 		if (ifp->if_ioctl == NULL) {
2144 			error = EOPNOTSUPP;
2145 			break;
2146 		}
2147 		ifnet_serialize_all(ifp);
2148 		error = ifp->if_ioctl(ifp, cmd, data, cred);
2149 		ifnet_deserialize_all(ifp);
2150 		break;
2151 
2152 	case SIOCSIFLLADDR:
2153 		error = priv_check_cred(cred, PRIV_ROOT, 0);
2154 		if (error)
2155 			break;
2156 		error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2157 				     ifr->ifr_addr.sa_len);
2158 		EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2159 		break;
2160 
2161 	default:
2162 		oif_flags = ifp->if_flags;
2163 		if (so->so_proto == 0) {
2164 			error = EOPNOTSUPP;
2165 			break;
2166 		}
2167 		error = so_pru_control_direct(so, cmd, data, ifp);
2168 
2169 		if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2170 #ifdef INET6
2171 			DELAY(100);/* XXX: temporary workaround for fxp issue*/
2172 			if (ifp->if_flags & IFF_UP) {
2173 				crit_enter();
2174 				in6_if_up(ifp);
2175 				crit_exit();
2176 			}
2177 #endif
2178 		}
2179 		break;
2180 	}
2181 
2182 	ifnet_unlock();
2183 	return (error);
2184 }
2185 
2186 /*
2187  * Set/clear promiscuous mode on interface ifp based on the truth value
2188  * of pswitch.  The calls are reference counted so that only the first
2189  * "on" request actually has an effect, as does the final "off" request.
2190  * Results are undefined if the "off" and "on" requests are not matched.
2191  */
2192 int
2193 ifpromisc(struct ifnet *ifp, int pswitch)
2194 {
2195 	struct ifreq ifr;
2196 	int error;
2197 	int oldflags;
2198 
2199 	oldflags = ifp->if_flags;
2200 	if (ifp->if_flags & IFF_PPROMISC) {
2201 		/* Do nothing if device is in permanently promiscuous mode */
2202 		ifp->if_pcount += pswitch ? 1 : -1;
2203 		return (0);
2204 	}
2205 	if (pswitch) {
2206 		/*
2207 		 * If the device is not configured up, we cannot put it in
2208 		 * promiscuous mode.
2209 		 */
2210 		if ((ifp->if_flags & IFF_UP) == 0)
2211 			return (ENETDOWN);
2212 		if (ifp->if_pcount++ != 0)
2213 			return (0);
2214 		ifp->if_flags |= IFF_PROMISC;
2215 		log(LOG_INFO, "%s: promiscuous mode enabled\n",
2216 		    ifp->if_xname);
2217 	} else {
2218 		if (--ifp->if_pcount > 0)
2219 			return (0);
2220 		ifp->if_flags &= ~IFF_PROMISC;
2221 		log(LOG_INFO, "%s: promiscuous mode disabled\n",
2222 		    ifp->if_xname);
2223 	}
2224 	ifr.ifr_flags = ifp->if_flags;
2225 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
2226 	ifnet_serialize_all(ifp);
2227 	error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2228 	ifnet_deserialize_all(ifp);
2229 	if (error == 0)
2230 		rt_ifmsg(ifp);
2231 	else
2232 		ifp->if_flags = oldflags;
2233 	return error;
2234 }
2235 
2236 /*
2237  * Return interface configuration
2238  * of system.  List may be used
2239  * in later ioctl's (above) to get
2240  * other information.
2241  */
2242 static int
2243 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2244 {
2245 	struct ifconf *ifc = (struct ifconf *)data;
2246 	struct ifnet *ifp;
2247 	struct sockaddr *sa;
2248 	struct ifreq ifr, *ifrp;
2249 	int space = ifc->ifc_len, error = 0;
2250 
2251 	ifrp = ifc->ifc_req;
2252 
2253 	ifnet_lock();
2254 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2255 		struct ifaddr_container *ifac, *ifac_mark;
2256 		struct ifaddr_marker mark;
2257 		struct ifaddrhead *head;
2258 		int addrs;
2259 
2260 		if (space <= sizeof ifr)
2261 			break;
2262 
2263 		/*
2264 		 * Zero the stack declared structure first to prevent
2265 		 * memory disclosure.
2266 		 */
2267 		bzero(&ifr, sizeof(ifr));
2268 		if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2269 		    >= sizeof(ifr.ifr_name)) {
2270 			error = ENAMETOOLONG;
2271 			break;
2272 		}
2273 
2274 		/*
2275 		 * Add a marker, since copyout() could block and during that
2276 		 * period the list could be changed.  Inserting the marker to
2277 		 * the header of the list will not cause trouble for the code
2278 		 * assuming that the first element of the list is AF_LINK; the
2279 		 * marker will be moved to the next position w/o blocking.
2280 		 */
2281 		ifa_marker_init(&mark, ifp);
2282 		ifac_mark = &mark.ifac;
2283 		head = &ifp->if_addrheads[mycpuid];
2284 
2285 		addrs = 0;
2286 		TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2287 		while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2288 			struct ifaddr *ifa = ifac->ifa;
2289 
2290 			TAILQ_REMOVE(head, ifac_mark, ifa_link);
2291 			TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2292 
2293 			/* Ignore marker */
2294 			if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2295 				continue;
2296 
2297 			if (space <= sizeof ifr)
2298 				break;
2299 			sa = ifa->ifa_addr;
2300 			if (cred->cr_prison &&
2301 			    prison_if(cred, sa))
2302 				continue;
2303 			addrs++;
2304 			/*
2305 			 * Keep a reference on this ifaddr, so that it will
2306 			 * not be destroyed when its address is copied to
2307 			 * the userland, which could block.
2308 			 */
2309 			IFAREF(ifa);
2310 			if (sa->sa_len <= sizeof(*sa)) {
2311 				ifr.ifr_addr = *sa;
2312 				error = copyout(&ifr, ifrp, sizeof ifr);
2313 				ifrp++;
2314 			} else {
2315 				if (space < (sizeof ifr) + sa->sa_len -
2316 					    sizeof(*sa)) {
2317 					IFAFREE(ifa);
2318 					break;
2319 				}
2320 				space -= sa->sa_len - sizeof(*sa);
2321 				error = copyout(&ifr, ifrp,
2322 						sizeof ifr.ifr_name);
2323 				if (error == 0)
2324 					error = copyout(sa, &ifrp->ifr_addr,
2325 							sa->sa_len);
2326 				ifrp = (struct ifreq *)
2327 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2328 			}
2329 			IFAFREE(ifa);
2330 			if (error)
2331 				break;
2332 			space -= sizeof ifr;
2333 		}
2334 		TAILQ_REMOVE(head, ifac_mark, ifa_link);
2335 		if (error)
2336 			break;
2337 		if (!addrs) {
2338 			bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2339 			error = copyout(&ifr, ifrp, sizeof ifr);
2340 			if (error)
2341 				break;
2342 			space -= sizeof ifr;
2343 			ifrp++;
2344 		}
2345 	}
2346 	ifnet_unlock();
2347 
2348 	ifc->ifc_len -= space;
2349 	return (error);
2350 }
2351 
2352 /*
2353  * Just like if_promisc(), but for all-multicast-reception mode.
2354  */
2355 int
2356 if_allmulti(struct ifnet *ifp, int onswitch)
2357 {
2358 	int error = 0;
2359 	struct ifreq ifr;
2360 
2361 	crit_enter();
2362 
2363 	if (onswitch) {
2364 		if (ifp->if_amcount++ == 0) {
2365 			ifp->if_flags |= IFF_ALLMULTI;
2366 			ifr.ifr_flags = ifp->if_flags;
2367 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2368 			ifnet_serialize_all(ifp);
2369 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2370 					      NULL);
2371 			ifnet_deserialize_all(ifp);
2372 		}
2373 	} else {
2374 		if (ifp->if_amcount > 1) {
2375 			ifp->if_amcount--;
2376 		} else {
2377 			ifp->if_amcount = 0;
2378 			ifp->if_flags &= ~IFF_ALLMULTI;
2379 			ifr.ifr_flags = ifp->if_flags;
2380 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
2381 			ifnet_serialize_all(ifp);
2382 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2383 					      NULL);
2384 			ifnet_deserialize_all(ifp);
2385 		}
2386 	}
2387 
2388 	crit_exit();
2389 
2390 	if (error == 0)
2391 		rt_ifmsg(ifp);
2392 	return error;
2393 }
2394 
2395 /*
2396  * Add a multicast listenership to the interface in question.
2397  * The link layer provides a routine which converts
2398  */
2399 int
2400 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2401     struct ifmultiaddr **retifma)
2402 {
2403 	struct sockaddr *llsa, *dupsa;
2404 	int error;
2405 	struct ifmultiaddr *ifma;
2406 
2407 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2408 
2409 	/*
2410 	 * If the matching multicast address already exists
2411 	 * then don't add a new one, just add a reference
2412 	 */
2413 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2414 		if (sa_equal(sa, ifma->ifma_addr)) {
2415 			ifma->ifma_refcount++;
2416 			if (retifma)
2417 				*retifma = ifma;
2418 			return 0;
2419 		}
2420 	}
2421 
2422 	/*
2423 	 * Give the link layer a chance to accept/reject it, and also
2424 	 * find out which AF_LINK address this maps to, if it isn't one
2425 	 * already.
2426 	 */
2427 	if (ifp->if_resolvemulti) {
2428 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
2429 		if (error)
2430 			return error;
2431 	} else {
2432 		llsa = NULL;
2433 	}
2434 
2435 	ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2436 	dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2437 	bcopy(sa, dupsa, sa->sa_len);
2438 
2439 	ifma->ifma_addr = dupsa;
2440 	ifma->ifma_lladdr = llsa;
2441 	ifma->ifma_ifp = ifp;
2442 	ifma->ifma_refcount = 1;
2443 	ifma->ifma_protospec = NULL;
2444 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2445 
2446 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2447 	if (retifma)
2448 		*retifma = ifma;
2449 
2450 	if (llsa != NULL) {
2451 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2452 			if (sa_equal(ifma->ifma_addr, llsa))
2453 				break;
2454 		}
2455 		if (ifma) {
2456 			ifma->ifma_refcount++;
2457 		} else {
2458 			ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2459 			dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2460 			bcopy(llsa, dupsa, llsa->sa_len);
2461 			ifma->ifma_addr = dupsa;
2462 			ifma->ifma_ifp = ifp;
2463 			ifma->ifma_refcount = 1;
2464 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2465 		}
2466 	}
2467 	/*
2468 	 * We are certain we have added something, so call down to the
2469 	 * interface to let them know about it.
2470 	 */
2471 	if (ifp->if_ioctl)
2472 		ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2473 
2474 	return 0;
2475 }
2476 
2477 int
2478 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2479     struct ifmultiaddr **retifma)
2480 {
2481 	int error;
2482 
2483 	ifnet_serialize_all(ifp);
2484 	error = if_addmulti_serialized(ifp, sa, retifma);
2485 	ifnet_deserialize_all(ifp);
2486 
2487 	return error;
2488 }
2489 
2490 /*
2491  * Remove a reference to a multicast address on this interface.  Yell
2492  * if the request does not match an existing membership.
2493  */
2494 static int
2495 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2496 {
2497 	struct ifmultiaddr *ifma;
2498 
2499 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2500 
2501 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2502 		if (sa_equal(sa, ifma->ifma_addr))
2503 			break;
2504 	if (ifma == NULL)
2505 		return ENOENT;
2506 
2507 	if (ifma->ifma_refcount > 1) {
2508 		ifma->ifma_refcount--;
2509 		return 0;
2510 	}
2511 
2512 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
2513 	sa = ifma->ifma_lladdr;
2514 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2515 	/*
2516 	 * Make sure the interface driver is notified
2517 	 * in the case of a link layer mcast group being left.
2518 	 */
2519 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2520 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2521 	kfree(ifma->ifma_addr, M_IFMADDR);
2522 	kfree(ifma, M_IFMADDR);
2523 	if (sa == NULL)
2524 		return 0;
2525 
2526 	/*
2527 	 * Now look for the link-layer address which corresponds to
2528 	 * this network address.  It had been squirreled away in
2529 	 * ifma->ifma_lladdr for this purpose (so we don't have
2530 	 * to call ifp->if_resolvemulti() again), and we saved that
2531 	 * value in sa above.  If some nasty deleted the
2532 	 * link-layer address out from underneath us, we can deal because
2533 	 * the address we stored was is not the same as the one which was
2534 	 * in the record for the link-layer address.  (So we don't complain
2535 	 * in that case.)
2536 	 */
2537 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2538 		if (sa_equal(sa, ifma->ifma_addr))
2539 			break;
2540 	if (ifma == NULL)
2541 		return 0;
2542 
2543 	if (ifma->ifma_refcount > 1) {
2544 		ifma->ifma_refcount--;
2545 		return 0;
2546 	}
2547 
2548 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2549 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2550 	kfree(ifma->ifma_addr, M_IFMADDR);
2551 	kfree(sa, M_IFMADDR);
2552 	kfree(ifma, M_IFMADDR);
2553 
2554 	return 0;
2555 }
2556 
2557 int
2558 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2559 {
2560 	int error;
2561 
2562 	ifnet_serialize_all(ifp);
2563 	error = if_delmulti_serialized(ifp, sa);
2564 	ifnet_deserialize_all(ifp);
2565 
2566 	return error;
2567 }
2568 
2569 /*
2570  * Delete all multicast group membership for an interface.
2571  * Should be used to quickly flush all multicast filters.
2572  */
2573 void
2574 if_delallmulti_serialized(struct ifnet *ifp)
2575 {
2576 	struct ifmultiaddr *ifma, mark;
2577 	struct sockaddr sa;
2578 
2579 	ASSERT_IFNET_SERIALIZED_ALL(ifp);
2580 
2581 	bzero(&sa, sizeof(sa));
2582 	sa.sa_family = AF_UNSPEC;
2583 	sa.sa_len = sizeof(sa);
2584 
2585 	bzero(&mark, sizeof(mark));
2586 	mark.ifma_addr = &sa;
2587 
2588 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2589 	while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2590 		TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2591 		TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2592 		    ifma_link);
2593 
2594 		if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2595 			continue;
2596 
2597 		if_delmulti_serialized(ifp, ifma->ifma_addr);
2598 	}
2599 	TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2600 }
2601 
2602 
2603 /*
2604  * Set the link layer address on an interface.
2605  *
2606  * At this time we only support certain types of interfaces,
2607  * and we don't allow the length of the address to change.
2608  */
2609 int
2610 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2611 {
2612 	struct sockaddr_dl *sdl;
2613 	struct ifreq ifr;
2614 
2615 	sdl = IF_LLSOCKADDR(ifp);
2616 	if (sdl == NULL)
2617 		return (EINVAL);
2618 	if (len != sdl->sdl_alen)	/* don't allow length to change */
2619 		return (EINVAL);
2620 	switch (ifp->if_type) {
2621 	case IFT_ETHER:			/* these types use struct arpcom */
2622 	case IFT_XETHER:
2623 	case IFT_L2VLAN:
2624 	case IFT_IEEE8023ADLAG:
2625 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2626 		bcopy(lladdr, LLADDR(sdl), len);
2627 		break;
2628 	default:
2629 		return (ENODEV);
2630 	}
2631 	/*
2632 	 * If the interface is already up, we need
2633 	 * to re-init it in order to reprogram its
2634 	 * address filter.
2635 	 */
2636 	ifnet_serialize_all(ifp);
2637 	if ((ifp->if_flags & IFF_UP) != 0) {
2638 #ifdef INET
2639 		struct ifaddr_container *ifac;
2640 #endif
2641 
2642 		ifp->if_flags &= ~IFF_UP;
2643 		ifr.ifr_flags = ifp->if_flags;
2644 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2645 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2646 			      NULL);
2647 		ifp->if_flags |= IFF_UP;
2648 		ifr.ifr_flags = ifp->if_flags;
2649 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
2650 		ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2651 				 NULL);
2652 #ifdef INET
2653 		/*
2654 		 * Also send gratuitous ARPs to notify other nodes about
2655 		 * the address change.
2656 		 */
2657 		TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2658 			struct ifaddr *ifa = ifac->ifa;
2659 
2660 			if (ifa->ifa_addr != NULL &&
2661 			    ifa->ifa_addr->sa_family == AF_INET)
2662 				arp_gratuitous(ifp, ifa);
2663 		}
2664 #endif
2665 	}
2666 	ifnet_deserialize_all(ifp);
2667 	return (0);
2668 }
2669 
2670 struct ifmultiaddr *
2671 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2672 {
2673 	struct ifmultiaddr *ifma;
2674 
2675 	/* TODO: need ifnet_serialize_main */
2676 	ifnet_serialize_all(ifp);
2677 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2678 		if (sa_equal(ifma->ifma_addr, sa))
2679 			break;
2680 	ifnet_deserialize_all(ifp);
2681 
2682 	return ifma;
2683 }
2684 
2685 /*
2686  * This function locates the first real ethernet MAC from a network
2687  * card and loads it into node, returning 0 on success or ENOENT if
2688  * no suitable interfaces were found.  It is used by the uuid code to
2689  * generate a unique 6-byte number.
2690  */
2691 int
2692 if_getanyethermac(uint16_t *node, int minlen)
2693 {
2694 	struct ifnet *ifp;
2695 	struct sockaddr_dl *sdl;
2696 
2697 	ifnet_lock();
2698 	TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2699 		if (ifp->if_type != IFT_ETHER)
2700 			continue;
2701 		sdl = IF_LLSOCKADDR(ifp);
2702 		if (sdl->sdl_alen < minlen)
2703 			continue;
2704 		bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2705 		      minlen);
2706 		ifnet_unlock();
2707 		return(0);
2708 	}
2709 	ifnet_unlock();
2710 	return (ENOENT);
2711 }
2712 
2713 /*
2714  * The name argument must be a pointer to storage which will last as
2715  * long as the interface does.  For physical devices, the result of
2716  * device_get_name(dev) is a good choice and for pseudo-devices a
2717  * static string works well.
2718  */
2719 void
2720 if_initname(struct ifnet *ifp, const char *name, int unit)
2721 {
2722 	ifp->if_dname = name;
2723 	ifp->if_dunit = unit;
2724 	if (unit != IF_DUNIT_NONE)
2725 		ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2726 	else
2727 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
2728 }
2729 
2730 int
2731 if_printf(struct ifnet *ifp, const char *fmt, ...)
2732 {
2733 	__va_list ap;
2734 	int retval;
2735 
2736 	retval = kprintf("%s: ", ifp->if_xname);
2737 	__va_start(ap, fmt);
2738 	retval += kvprintf(fmt, ap);
2739 	__va_end(ap);
2740 	return (retval);
2741 }
2742 
2743 struct ifnet *
2744 if_alloc(uint8_t type)
2745 {
2746         struct ifnet *ifp;
2747 	size_t size;
2748 
2749 	/*
2750 	 * XXX temporary hack until arpcom is setup in if_l2com
2751 	 */
2752 	if (type == IFT_ETHER)
2753 		size = sizeof(struct arpcom);
2754 	else
2755 		size = sizeof(struct ifnet);
2756 
2757 	ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2758 
2759 	ifp->if_type = type;
2760 
2761 	if (if_com_alloc[type] != NULL) {
2762 		ifp->if_l2com = if_com_alloc[type](type, ifp);
2763 		if (ifp->if_l2com == NULL) {
2764 			kfree(ifp, M_IFNET);
2765 			return (NULL);
2766 		}
2767 	}
2768 	return (ifp);
2769 }
2770 
2771 void
2772 if_free(struct ifnet *ifp)
2773 {
2774 	kfree(ifp, M_IFNET);
2775 }
2776 
2777 void
2778 ifq_set_classic(struct ifaltq *ifq)
2779 {
2780 	ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2781 	    ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2782 }
2783 
2784 void
2785 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2786     ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2787 {
2788 	int q;
2789 
2790 	KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2791 	KASSERT(enqueue != NULL, ("enqueue is not specified"));
2792 	KASSERT(dequeue != NULL, ("dequeue is not specified"));
2793 	KASSERT(request != NULL, ("request is not specified"));
2794 
2795 	ifq->altq_mapsubq = mapsubq;
2796 	for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2797 		struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2798 
2799 		ifsq->ifsq_enqueue = enqueue;
2800 		ifsq->ifsq_dequeue = dequeue;
2801 		ifsq->ifsq_request = request;
2802 	}
2803 }
2804 
2805 static void
2806 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2807 {
2808 
2809 	classq_add(&ifsq->ifsq_norm, m);
2810 	ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2811 }
2812 
2813 static void
2814 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2815 {
2816 
2817 	classq_add(&ifsq->ifsq_prio, m);
2818 	ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2819 	ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2820 }
2821 
2822 static struct mbuf *
2823 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2824 {
2825 	struct mbuf *m;
2826 
2827 	m = classq_get(&ifsq->ifsq_norm);
2828 	if (m != NULL)
2829 		ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2830 	return (m);
2831 }
2832 
2833 static struct mbuf *
2834 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2835 {
2836 	struct mbuf *m;
2837 
2838 	m = classq_get(&ifsq->ifsq_prio);
2839 	if (m != NULL) {
2840 		ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2841 		ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2842 	}
2843 	return (m);
2844 }
2845 
2846 int
2847 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2848     struct altq_pktattr *pa __unused)
2849 {
2850 
2851 	M_ASSERTPKTHDR(m);
2852 again:
2853 	if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2854 	    ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2855 		struct mbuf *m_drop;
2856 
2857 		if (m->m_flags & M_PRIO) {
2858 			m_drop = NULL;
2859 			if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2860 			    ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2861 				/* Try dropping some from normal queue. */
2862 				m_drop = ifsq_norm_dequeue(ifsq);
2863 			}
2864 			if (m_drop == NULL)
2865 				m_drop = ifsq_prio_dequeue(ifsq);
2866 		} else {
2867 			m_drop = ifsq_norm_dequeue(ifsq);
2868 		}
2869 		if (m_drop != NULL) {
2870 			IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2871 			m_freem(m_drop);
2872 			goto again;
2873 		}
2874 		/*
2875 		 * No old packets could be dropped!
2876 		 * NOTE: Caller increases oqdrops.
2877 		 */
2878 		m_freem(m);
2879 		return (ENOBUFS);
2880 	} else {
2881 		if (m->m_flags & M_PRIO)
2882 			ifsq_prio_enqueue(ifsq, m);
2883 		else
2884 			ifsq_norm_enqueue(ifsq, m);
2885 		return (0);
2886 	}
2887 }
2888 
2889 struct mbuf *
2890 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2891 {
2892 	struct mbuf *m;
2893 
2894 	switch (op) {
2895 	case ALTDQ_POLL:
2896 		m = classq_head(&ifsq->ifsq_prio);
2897 		if (m == NULL)
2898 			m = classq_head(&ifsq->ifsq_norm);
2899 		break;
2900 
2901 	case ALTDQ_REMOVE:
2902 		m = ifsq_prio_dequeue(ifsq);
2903 		if (m == NULL)
2904 			m = ifsq_norm_dequeue(ifsq);
2905 		break;
2906 
2907 	default:
2908 		panic("unsupported ALTQ dequeue op: %d", op);
2909 	}
2910 	return m;
2911 }
2912 
2913 int
2914 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2915 {
2916 	switch (req) {
2917 	case ALTRQ_PURGE:
2918 		for (;;) {
2919 			struct mbuf *m;
2920 
2921 			m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2922 			if (m == NULL)
2923 				break;
2924 			m_freem(m);
2925 		}
2926 		break;
2927 
2928 	default:
2929 		panic("unsupported ALTQ request: %d", req);
2930 	}
2931 	return 0;
2932 }
2933 
2934 static void
2935 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2936 {
2937 	struct ifnet *ifp = ifsq_get_ifp(ifsq);
2938 	int running = 0, need_sched;
2939 
2940 	/*
2941 	 * Try to do direct ifnet.if_start on the subqueue first, if there is
2942 	 * contention on the subqueue hardware serializer, ifnet.if_start on
2943 	 * the subqueue will be scheduled on the subqueue owner CPU.
2944 	 */
2945 	if (!ifsq_tryserialize_hw(ifsq)) {
2946 		/*
2947 		 * Subqueue hardware serializer contention happened,
2948 		 * ifnet.if_start on the subqueue is scheduled on
2949 		 * the subqueue owner CPU, and we keep going.
2950 		 */
2951 		ifsq_ifstart_schedule(ifsq, 1);
2952 		return;
2953 	}
2954 
2955 	if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2956 		ifp->if_start(ifp, ifsq);
2957 		if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2958 			running = 1;
2959 	}
2960 	need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2961 
2962 	ifsq_deserialize_hw(ifsq);
2963 
2964 	if (need_sched) {
2965 		/*
2966 		 * More data need to be transmitted, ifnet.if_start on the
2967 		 * subqueue is scheduled on the subqueue owner CPU, and we
2968 		 * keep going.
2969 		 * NOTE: ifnet.if_start subqueue interlock is not released.
2970 		 */
2971 		ifsq_ifstart_schedule(ifsq, force_sched);
2972 	}
2973 }
2974 
2975 /*
2976  * Subqeue packets staging mechanism:
2977  *
2978  * The packets enqueued into the subqueue are staged to a certain amount
2979  * before the ifnet.if_start on the subqueue is called.  In this way, the
2980  * driver could avoid writing to hardware registers upon every packet,
2981  * instead, hardware registers could be written when certain amount of
2982  * packets are put onto hardware TX ring.  The measurement on several modern
2983  * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2984  * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2985  * datagrams are transmitted at 1.48Mpps.  The performance improvement by
2986  * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2987  * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2988  *
2989  * Subqueue packets staging is performed for two entry points into drivers'
2990  * transmission function:
2991  * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2992  * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2993  *
2994  * Subqueue packets staging will be stopped upon any of the following
2995  * conditions:
2996  * - If the count of packets enqueued on the current CPU is great than or
2997  *   equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2998  * - If the total length of packets enqueued on the current CPU is great
2999  *   than or equal to the hardware's MTU - max_protohdr.  max_protohdr is
3000  *   cut from the hardware's MTU mainly bacause a full TCP segment's size
3001  *   is usually less than hardware's MTU.
3002  * - ifsq_ifstart_schedule() is not pending on the current CPU and
3003  *   ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
3004  *   released.
3005  * - The if_start_rollup(), which is registered as low priority netisr
3006  *   rollup function, is called; probably because no more work is pending
3007  *   for netisr.
3008  *
3009  * NOTE:
3010  * Currently subqueue packet staging is only performed in netisr threads.
3011  */
3012 int
3013 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3014 {
3015 	struct ifaltq *ifq = &ifp->if_snd;
3016 	struct ifaltq_subque *ifsq;
3017 	int error, start = 0, len, mcast = 0, avoid_start = 0;
3018 	struct ifsubq_stage_head *head = NULL;
3019 	struct ifsubq_stage *stage = NULL;
3020 	struct globaldata *gd = mycpu;
3021 	struct thread *td = gd->gd_curthread;
3022 
3023 	crit_enter_quick(td);
3024 
3025 	ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3026 	ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3027 
3028 	len = m->m_pkthdr.len;
3029 	if (m->m_flags & M_MCAST)
3030 		mcast = 1;
3031 
3032 	if (td->td_type == TD_TYPE_NETISR) {
3033 		head = &ifsubq_stage_heads[mycpuid];
3034 		stage = ifsq_get_stage(ifsq, mycpuid);
3035 
3036 		stage->stg_cnt++;
3037 		stage->stg_len += len;
3038 		if (stage->stg_cnt < ifsq_stage_cntmax &&
3039 		    stage->stg_len < (ifp->if_mtu - max_protohdr))
3040 			avoid_start = 1;
3041 	}
3042 
3043 	ALTQ_SQ_LOCK(ifsq);
3044 	error = ifsq_enqueue_locked(ifsq, m, pa);
3045 	if (error) {
3046 		IFNET_STAT_INC(ifp, oqdrops, 1);
3047 		if (!ifsq_data_ready(ifsq)) {
3048 			ALTQ_SQ_UNLOCK(ifsq);
3049 			crit_exit_quick(td);
3050 			return error;
3051 		}
3052 		avoid_start = 0;
3053 	}
3054 	if (!ifsq_is_started(ifsq)) {
3055 		if (avoid_start) {
3056 			ALTQ_SQ_UNLOCK(ifsq);
3057 
3058 			KKASSERT(!error);
3059 			if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3060 				ifsq_stage_insert(head, stage);
3061 
3062 			IFNET_STAT_INC(ifp, obytes, len);
3063 			if (mcast)
3064 				IFNET_STAT_INC(ifp, omcasts, 1);
3065 			crit_exit_quick(td);
3066 			return error;
3067 		}
3068 
3069 		/*
3070 		 * Hold the subqueue interlock of ifnet.if_start
3071 		 */
3072 		ifsq_set_started(ifsq);
3073 		start = 1;
3074 	}
3075 	ALTQ_SQ_UNLOCK(ifsq);
3076 
3077 	if (!error) {
3078 		IFNET_STAT_INC(ifp, obytes, len);
3079 		if (mcast)
3080 			IFNET_STAT_INC(ifp, omcasts, 1);
3081 	}
3082 
3083 	if (stage != NULL) {
3084 		if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3085 			KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3086 			if (!avoid_start) {
3087 				ifsq_stage_remove(head, stage);
3088 				ifsq_ifstart_schedule(ifsq, 1);
3089 			}
3090 			crit_exit_quick(td);
3091 			return error;
3092 		}
3093 
3094 		if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3095 			ifsq_stage_remove(head, stage);
3096 		} else {
3097 			stage->stg_cnt = 0;
3098 			stage->stg_len = 0;
3099 		}
3100 	}
3101 
3102 	if (!start) {
3103 		crit_exit_quick(td);
3104 		return error;
3105 	}
3106 
3107 	ifsq_ifstart_try(ifsq, 0);
3108 
3109 	crit_exit_quick(td);
3110 	return error;
3111 }
3112 
3113 void *
3114 ifa_create(int size)
3115 {
3116 	struct ifaddr *ifa;
3117 	int i;
3118 
3119 	KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3120 
3121 	ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3122 
3123 	/*
3124 	 * Make ifa_container availabel on all CPUs, since they
3125 	 * could be accessed by any threads.
3126 	 */
3127 	ifa->ifa_containers =
3128 	    kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3129 	        M_IFADDR, M_INTWAIT | M_ZERO);
3130 
3131 	ifa->ifa_ncnt = ncpus;
3132 	for (i = 0; i < ncpus; ++i) {
3133 		struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3134 
3135 		ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3136 		ifac->ifa = ifa;
3137 		ifac->ifa_refcnt = 1;
3138 	}
3139 #ifdef IFADDR_DEBUG
3140 	kprintf("alloc ifa %p %d\n", ifa, size);
3141 #endif
3142 	return ifa;
3143 }
3144 
3145 void
3146 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3147 {
3148 	struct ifaddr *ifa = ifac->ifa;
3149 
3150 	KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3151 	KKASSERT(ifac->ifa_refcnt == 0);
3152 	KASSERT(ifac->ifa_listmask == 0,
3153 		("ifa is still on %#x lists", ifac->ifa_listmask));
3154 
3155 	ifac->ifa_magic = IFA_CONTAINER_DEAD;
3156 
3157 #ifdef IFADDR_DEBUG_VERBOSE
3158 	kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3159 #endif
3160 
3161 	KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3162 		("invalid # of ifac, %d", ifa->ifa_ncnt));
3163 	if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3164 #ifdef IFADDR_DEBUG
3165 		kprintf("free ifa %p\n", ifa);
3166 #endif
3167 		kfree(ifa->ifa_containers, M_IFADDR);
3168 		kfree(ifa, M_IFADDR);
3169 	}
3170 }
3171 
3172 static void
3173 ifa_iflink_dispatch(netmsg_t nmsg)
3174 {
3175 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3176 	struct ifaddr *ifa = msg->ifa;
3177 	struct ifnet *ifp = msg->ifp;
3178 	int cpu = mycpuid;
3179 	struct ifaddr_container *ifac;
3180 
3181 	crit_enter();
3182 
3183 	ifac = &ifa->ifa_containers[cpu];
3184 	ASSERT_IFAC_VALID(ifac);
3185 	KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3186 		("ifaddr is on if_addrheads"));
3187 
3188 	ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3189 	if (msg->tail)
3190 		TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3191 	else
3192 		TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3193 
3194 	crit_exit();
3195 
3196 	netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3197 }
3198 
3199 void
3200 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3201 {
3202 	struct netmsg_ifaddr msg;
3203 
3204 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3205 		    0, ifa_iflink_dispatch);
3206 	msg.ifa = ifa;
3207 	msg.ifp = ifp;
3208 	msg.tail = tail;
3209 
3210 	netisr_domsg(&msg.base, 0);
3211 }
3212 
3213 static void
3214 ifa_ifunlink_dispatch(netmsg_t nmsg)
3215 {
3216 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3217 	struct ifaddr *ifa = msg->ifa;
3218 	struct ifnet *ifp = msg->ifp;
3219 	int cpu = mycpuid;
3220 	struct ifaddr_container *ifac;
3221 
3222 	crit_enter();
3223 
3224 	ifac = &ifa->ifa_containers[cpu];
3225 	ASSERT_IFAC_VALID(ifac);
3226 	KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3227 		("ifaddr is not on if_addrhead"));
3228 
3229 	TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3230 	ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3231 
3232 	crit_exit();
3233 
3234 	netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3235 }
3236 
3237 void
3238 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3239 {
3240 	struct netmsg_ifaddr msg;
3241 
3242 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3243 		    0, ifa_ifunlink_dispatch);
3244 	msg.ifa = ifa;
3245 	msg.ifp = ifp;
3246 
3247 	netisr_domsg(&msg.base, 0);
3248 }
3249 
3250 static void
3251 ifa_destroy_dispatch(netmsg_t nmsg)
3252 {
3253 	struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3254 
3255 	IFAFREE(msg->ifa);
3256 	netisr_forwardmsg_all(&nmsg->base, mycpuid + 1);
3257 }
3258 
3259 void
3260 ifa_destroy(struct ifaddr *ifa)
3261 {
3262 	struct netmsg_ifaddr msg;
3263 
3264 	netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3265 		    0, ifa_destroy_dispatch);
3266 	msg.ifa = ifa;
3267 
3268 	netisr_domsg(&msg.base, 0);
3269 }
3270 
3271 static void
3272 if_start_rollup(void)
3273 {
3274 	struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3275 	struct ifsubq_stage *stage;
3276 
3277 	crit_enter();
3278 
3279 	while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3280 		struct ifaltq_subque *ifsq = stage->stg_subq;
3281 		int is_sched = 0;
3282 
3283 		if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3284 			is_sched = 1;
3285 		ifsq_stage_remove(head, stage);
3286 
3287 		if (is_sched) {
3288 			ifsq_ifstart_schedule(ifsq, 1);
3289 		} else {
3290 			int start = 0;
3291 
3292 			ALTQ_SQ_LOCK(ifsq);
3293 			if (!ifsq_is_started(ifsq)) {
3294 				/*
3295 				 * Hold the subqueue interlock of
3296 				 * ifnet.if_start
3297 				 */
3298 				ifsq_set_started(ifsq);
3299 				start = 1;
3300 			}
3301 			ALTQ_SQ_UNLOCK(ifsq);
3302 
3303 			if (start)
3304 				ifsq_ifstart_try(ifsq, 1);
3305 		}
3306 		KKASSERT((stage->stg_flags &
3307 		    (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3308 	}
3309 
3310 	crit_exit();
3311 }
3312 
3313 static void
3314 ifnetinit(void *dummy __unused)
3315 {
3316 	int i;
3317 
3318 	/* XXX netisr_ncpus */
3319 	for (i = 0; i < ncpus; ++i)
3320 		TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3321 	netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3322 }
3323 
3324 void
3325 if_register_com_alloc(u_char type,
3326     if_com_alloc_t *a, if_com_free_t *f)
3327 {
3328 
3329         KASSERT(if_com_alloc[type] == NULL,
3330             ("if_register_com_alloc: %d already registered", type));
3331         KASSERT(if_com_free[type] == NULL,
3332             ("if_register_com_alloc: %d free already registered", type));
3333 
3334         if_com_alloc[type] = a;
3335         if_com_free[type] = f;
3336 }
3337 
3338 void
3339 if_deregister_com_alloc(u_char type)
3340 {
3341 
3342         KASSERT(if_com_alloc[type] != NULL,
3343             ("if_deregister_com_alloc: %d not registered", type));
3344         KASSERT(if_com_free[type] != NULL,
3345             ("if_deregister_com_alloc: %d free not registered", type));
3346         if_com_alloc[type] = NULL;
3347         if_com_free[type] = NULL;
3348 }
3349 
3350 void
3351 ifq_set_maxlen(struct ifaltq *ifq, int len)
3352 {
3353 	ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3354 }
3355 
3356 int
3357 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3358 {
3359 	return ALTQ_SUBQ_INDEX_DEFAULT;
3360 }
3361 
3362 int
3363 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3364 {
3365 
3366 	return (cpuid % ifq->altq_subq_mappriv);
3367 }
3368 
3369 static void
3370 ifsq_watchdog(void *arg)
3371 {
3372 	struct ifsubq_watchdog *wd = arg;
3373 	struct ifnet *ifp;
3374 
3375 	if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3376 		goto done;
3377 
3378 	ifp = ifsq_get_ifp(wd->wd_subq);
3379 	if (ifnet_tryserialize_all(ifp)) {
3380 		wd->wd_watchdog(wd->wd_subq);
3381 		ifnet_deserialize_all(ifp);
3382 	} else {
3383 		/* try again next timeout */
3384 		wd->wd_timer = 1;
3385 	}
3386 done:
3387 	ifsq_watchdog_reset(wd);
3388 }
3389 
3390 static void
3391 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3392 {
3393 	callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3394 	    ifsq_get_cpuid(wd->wd_subq));
3395 }
3396 
3397 void
3398 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3399     ifsq_watchdog_t watchdog)
3400 {
3401 	callout_init_mp(&wd->wd_callout);
3402 	wd->wd_timer = 0;
3403 	wd->wd_subq = ifsq;
3404 	wd->wd_watchdog = watchdog;
3405 }
3406 
3407 void
3408 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3409 {
3410 	wd->wd_timer = 0;
3411 	ifsq_watchdog_reset(wd);
3412 }
3413 
3414 void
3415 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3416 {
3417 	wd->wd_timer = 0;
3418 	callout_stop(&wd->wd_callout);
3419 }
3420 
3421 void
3422 ifnet_lock(void)
3423 {
3424 	KASSERT(curthread->td_type != TD_TYPE_NETISR,
3425 	    ("try holding ifnet lock in netisr"));
3426 	mtx_lock(&ifnet_mtx);
3427 }
3428 
3429 void
3430 ifnet_unlock(void)
3431 {
3432 	KASSERT(curthread->td_type != TD_TYPE_NETISR,
3433 	    ("try holding ifnet lock in netisr"));
3434 	mtx_unlock(&ifnet_mtx);
3435 }
3436 
3437 static struct ifnet_array *
3438 ifnet_array_alloc(int count)
3439 {
3440 	struct ifnet_array *arr;
3441 
3442 	arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3443 	    M_IFNET, M_WAITOK);
3444 	arr->ifnet_count = count;
3445 
3446 	return arr;
3447 }
3448 
3449 static void
3450 ifnet_array_free(struct ifnet_array *arr)
3451 {
3452 	if (arr == &ifnet_array0)
3453 		return;
3454 	kfree(arr, M_IFNET);
3455 }
3456 
3457 static struct ifnet_array *
3458 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3459 {
3460 	struct ifnet_array *arr;
3461 	int count, i;
3462 
3463 	KASSERT(old_arr->ifnet_count >= 0,
3464 	    ("invalid ifnet array count %d", old_arr->ifnet_count));
3465 	count = old_arr->ifnet_count + 1;
3466 	arr = ifnet_array_alloc(count);
3467 
3468 	/*
3469 	 * Save the old ifnet array and append this ifp to the end of
3470 	 * the new ifnet array.
3471 	 */
3472 	for (i = 0; i < old_arr->ifnet_count; ++i) {
3473 		KASSERT(old_arr->ifnet_arr[i] != ifp,
3474 		    ("%s is already in ifnet array", ifp->if_xname));
3475 		arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3476 	}
3477 	KASSERT(i == count - 1,
3478 	    ("add %s, ifnet array index mismatch, should be %d, but got %d",
3479 	     ifp->if_xname, count - 1, i));
3480 	arr->ifnet_arr[i] = ifp;
3481 
3482 	return arr;
3483 }
3484 
3485 static struct ifnet_array *
3486 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3487 {
3488 	struct ifnet_array *arr;
3489 	int count, i, idx, found = 0;
3490 
3491 	KASSERT(old_arr->ifnet_count > 0,
3492 	    ("invalid ifnet array count %d", old_arr->ifnet_count));
3493 	count = old_arr->ifnet_count - 1;
3494 	arr = ifnet_array_alloc(count);
3495 
3496 	/*
3497 	 * Save the old ifnet array, but skip this ifp.
3498 	 */
3499 	idx = 0;
3500 	for (i = 0; i < old_arr->ifnet_count; ++i) {
3501 		if (old_arr->ifnet_arr[i] == ifp) {
3502 			KASSERT(!found,
3503 			    ("dup %s is in ifnet array", ifp->if_xname));
3504 			found = 1;
3505 			continue;
3506 		}
3507 		KASSERT(idx < count,
3508 		    ("invalid ifnet array index %d, count %d", idx, count));
3509 		arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3510 		++idx;
3511 	}
3512 	KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3513 	KASSERT(idx == count,
3514 	    ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3515 	     ifp->if_xname, count, idx));
3516 
3517 	return arr;
3518 }
3519 
3520 const struct ifnet_array *
3521 ifnet_array_get(void)
3522 {
3523 	const struct ifnet_array *ret;
3524 
3525 	KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3526 	ret = ifnet_array;
3527 	/* Make sure 'ret' is really used. */
3528 	cpu_ccfence();
3529 	return (ret);
3530 }
3531 
3532 int
3533 ifnet_array_isempty(void)
3534 {
3535 	KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3536 	if (ifnet_array->ifnet_count == 0)
3537 		return 1;
3538 	else
3539 		return 0;
3540 }
3541 
3542 void
3543 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3544 {
3545 	struct ifaddr *ifa;
3546 
3547 	memset(mark, 0, sizeof(*mark));
3548 	ifa = &mark->ifa;
3549 
3550 	mark->ifac.ifa = ifa;
3551 
3552 	ifa->ifa_addr = &mark->addr;
3553 	ifa->ifa_dstaddr = &mark->dstaddr;
3554 	ifa->ifa_netmask = &mark->netmask;
3555 	ifa->ifa_ifp = ifp;
3556 }
3557 
3558 static int
3559 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3560 {
3561 
3562 	KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3563 
3564 	if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3565 		ring_cnt = ring_cntmax;
3566 	if (ring_cnt > netisr_ncpus)
3567 		ring_cnt = netisr_ncpus;
3568 	return (ring_cnt);
3569 }
3570 
3571 static void
3572 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3573 {
3574 	int i, offset;
3575 
3576 	KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3577 	KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3578 	    grid, rm->rm_cnt));
3579 	rm->rm_grid = grid;
3580 
3581 	offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3582 	for (i = 0; i < rm->rm_cnt; ++i) {
3583 		rm->rm_cpumap[i] = offset + i;
3584 		KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3585 		    ("invalid cpumap[%d] = %d, offset %d", i,
3586 		     rm->rm_cpumap[i], offset));
3587 	}
3588 }
3589 
3590 static struct if_ringmap *
3591 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3592     uint32_t flags)
3593 {
3594 	struct if_ringmap *rm;
3595 	int i, grid = 0, prev_grid;
3596 
3597 	ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3598 	rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3599 	    M_DEVBUF, M_WAITOK | M_ZERO);
3600 
3601 	rm->rm_cnt = ring_cnt;
3602 	if (flags & RINGMAP_FLAG_POWEROF2)
3603 		rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3604 
3605 	prev_grid = netisr_ncpus;
3606 	for (i = 0; i < netisr_ncpus; ++i) {
3607 		if (netisr_ncpus % (i + 1) != 0)
3608 			continue;
3609 
3610 		grid = netisr_ncpus / (i + 1);
3611 		if (rm->rm_cnt > grid) {
3612 			grid = prev_grid;
3613 			break;
3614 		}
3615 
3616 		if (rm->rm_cnt > netisr_ncpus / (i + 2))
3617 			break;
3618 		prev_grid = grid;
3619 	}
3620 	if_ringmap_set_grid(dev, rm, grid);
3621 
3622 	return (rm);
3623 }
3624 
3625 struct if_ringmap *
3626 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3627 {
3628 
3629 	return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3630 	    RINGMAP_FLAG_NONE));
3631 }
3632 
3633 struct if_ringmap *
3634 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3635 {
3636 
3637 	return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3638 	    RINGMAP_FLAG_POWEROF2));
3639 }
3640 
3641 void
3642 if_ringmap_free(struct if_ringmap *rm)
3643 {
3644 
3645 	kfree(rm, M_DEVBUF);
3646 }
3647 
3648 /*
3649  * Align the two ringmaps.
3650  *
3651  * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3652  *
3653  * Before:
3654  *
3655  * CPU      0  1  2  3   4  5  6  7
3656  * NIC_RX               n0 n1 n2 n3
3657  * NIC_TX        N0 N1
3658  *
3659  * After:
3660  *
3661  * CPU      0  1  2  3   4  5  6  7
3662  * NIC_RX               n0 n1 n2 n3
3663  * NIC_TX               N0 N1
3664  */
3665 void
3666 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3667 {
3668 
3669 	if (rm0->rm_grid > rm1->rm_grid)
3670 		if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3671 	else if (rm0->rm_grid < rm1->rm_grid)
3672 		if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3673 }
3674 
3675 void
3676 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3677 {
3678 	int subset_grid, cnt, divisor, mod, offset, i;
3679 	struct if_ringmap *subset_rm, *rm;
3680 	int old_rm0_grid, old_rm1_grid;
3681 
3682 	if (rm0->rm_grid == rm1->rm_grid)
3683 		return;
3684 
3685 	/* Save grid for later use */
3686 	old_rm0_grid = rm0->rm_grid;
3687 	old_rm1_grid = rm1->rm_grid;
3688 
3689 	if_ringmap_align(dev, rm0, rm1);
3690 
3691 	/*
3692 	 * Re-shuffle rings to get more even distribution.
3693 	 *
3694 	 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3695 	 *
3696 	 * CPU       0  1  2  3   4  5  6  7   8  9 10 11
3697 	 *
3698 	 * NIC_RX   a0 a1 a2 a3  b0 b1 b2 b3  c0 c1 c2 c3
3699 	 * NIC_TX   A0 A1        B0 B1        C0 C1
3700 	 *
3701 	 * NIC_RX   d0 d1 d2 d3  e0 e1 e2 e3  f0 f1 f2 f3
3702 	 * NIC_TX         D0 D1        E0 E1        F0 F1
3703 	 */
3704 
3705 	if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3706 		cnt = rm0->rm_cnt;
3707 		subset_grid = old_rm1_grid;
3708 		subset_rm = rm1;
3709 		rm = rm0;
3710 	} else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3711 		cnt = rm1->rm_cnt;
3712 		subset_grid = old_rm0_grid;
3713 		subset_rm = rm0;
3714 		rm = rm1;
3715 	} else {
3716 		/* No space to shuffle. */
3717 		return;
3718 	}
3719 
3720 	mod = cnt / subset_grid;
3721 	KKASSERT(mod >= 2);
3722 	divisor = netisr_ncpus / rm->rm_grid;
3723 	offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3724 
3725 	for (i = 0; i < subset_rm->rm_cnt; ++i) {
3726 		subset_rm->rm_cpumap[i] += offset;
3727 		KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3728 		    ("match: invalid cpumap[%d] = %d, offset %d",
3729 		     i, subset_rm->rm_cpumap[i], offset));
3730 	}
3731 #ifdef INVARIANTS
3732 	for (i = 0; i < subset_rm->rm_cnt; ++i) {
3733 		int j;
3734 
3735 		for (j = 0; j < rm->rm_cnt; ++j) {
3736 			if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3737 				break;
3738 		}
3739 		KASSERT(j < rm->rm_cnt,
3740 		    ("subset cpumap[%d] = %d not found in superset",
3741 		     i, subset_rm->rm_cpumap[i]));
3742 	}
3743 #endif
3744 }
3745 
3746 int
3747 if_ringmap_count(const struct if_ringmap *rm)
3748 {
3749 
3750 	return (rm->rm_cnt);
3751 }
3752 
3753 int
3754 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3755 {
3756 
3757 	KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3758 	return (rm->rm_cpumap[ring]);
3759 }
3760 
3761 void
3762 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3763 {
3764 	int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3765 
3766 	KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3767 	    ("invalid redirect table entries %d", table_nent));
3768 
3769 	grid_idx = 0;
3770 	for (i = 0; i < NETISR_CPUMAX; ++i) {
3771 		table[i] = grid_idx++ % rm->rm_cnt;
3772 
3773 		if (grid_idx == rm->rm_grid)
3774 			grid_idx = 0;
3775 	}
3776 
3777 	/*
3778 	 * Make the ring distributed more evenly for the remainder
3779 	 * of each grid.
3780 	 *
3781 	 * e.g. 12 netisrs, rm contains 8 rings.
3782 	 *
3783 	 * Redirect table before:
3784 	 *
3785 	 *  0  1  2  3  4  5  6  7  0  1  2  3  0  1  2  3
3786 	 *  4  5  6  7  0  1  2  3  0  1  2  3  4  5  6  7
3787 	 *  0  1  2  3  0  1  2  3  4  5  6  7  0  1  2  3
3788 	 *  ....
3789 	 *
3790 	 * Redirect table after being patched (pX, patched entries):
3791 	 *
3792 	 *  0  1  2  3  4  5  6  7 p0 p1 p2 p3  0  1  2  3
3793 	 *  4  5  6  7 p4 p5 p6 p7  0  1  2  3  4  5  6  7
3794 	 * p0 p1 p2 p3  0  1  2  3  4  5  6  7 p4 p5 p6 p7
3795 	 *  ....
3796 	 */
3797 	patch_cnt = rm->rm_grid % rm->rm_cnt;
3798 	if (patch_cnt == 0)
3799 		goto done;
3800 	patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3801 
3802 	grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3803 	grid_idx = 0;
3804 	for (i = 0; i < grid_cnt; ++i) {
3805 		int j;
3806 
3807 		for (j = 0; j < patch_cnt; ++j) {
3808 			int fix_idx;
3809 
3810 			fix_idx = (i * rm->rm_grid) + patch_off + j;
3811 			if (fix_idx >= NETISR_CPUMAX)
3812 				goto done;
3813 			table[fix_idx] = grid_idx++ % rm->rm_cnt;
3814 		}
3815 	}
3816 done:
3817 	/*
3818 	 * If the device supports larger redirect table, duplicate
3819 	 * the first NETISR_CPUMAX entries to the rest of the table,
3820 	 * so that it matches upper layer's expectation:
3821 	 * (hash & NETISR_CPUMASK) % netisr_ncpus
3822 	 */
3823 	ncopy = table_nent / NETISR_CPUMAX;
3824 	for (i = 1; i < ncopy; ++i) {
3825 		memcpy(&table[i * NETISR_CPUMAX], table,
3826 		    NETISR_CPUMAX * sizeof(table[0]));
3827 	}
3828 	if (if_ringmap_dumprdr) {
3829 		for (i = 0; i < table_nent; ++i) {
3830 			if (i != 0 && i % 16 == 0)
3831 				kprintf("\n");
3832 			kprintf("%03d ", table[i]);
3833 		}
3834 		kprintf("\n");
3835 	}
3836 }
3837 
3838 int
3839 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
3840 {
3841 	struct if_ringmap *rm = arg1;
3842 	int i, error = 0;
3843 
3844 	for (i = 0; i < rm->rm_cnt; ++i) {
3845 		int cpu = rm->rm_cpumap[i];
3846 
3847 		error = SYSCTL_OUT(req, &cpu, sizeof(cpu));
3848 		if (error)
3849 			break;
3850 	}
3851 	return (error);
3852 }
3853