2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)if.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
37 #include "opt_compat.h"
38 #include "opt_inet6.h"
40 #include "opt_ifpoll.h"
42 #include <sys/param.h>
43 #include <sys/malloc.h>
45 #include <sys/systm.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/socketops.h>
52 #include <sys/protosw.h>
53 #include <sys/kernel.h>
55 #include <sys/mutex.h>
56 #include <sys/sockio.h>
57 #include <sys/syslog.h>
58 #include <sys/sysctl.h>
59 #include <sys/domain.h>
60 #include <sys/thread.h>
61 #include <sys/serialize.h>
64 #include <sys/thread2.h>
65 #include <sys/msgport2.h>
66 #include <sys/mutex2.h>
69 #include <net/if_arp.h>
70 #include <net/if_dl.h>
71 #include <net/if_types.h>
72 #include <net/if_var.h>
73 #include <net/ifq_var.h>
74 #include <net/radix.h>
75 #include <net/route.h>
76 #include <net/if_clone.h>
77 #include <net/netisr.h>
78 #include <net/netmsg2.h>
80 #include <machine/atomic.h>
81 #include <machine/stdarg.h>
82 #include <machine/smp.h>
84 #if defined(INET) || defined(INET6)
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/if_ether.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet6/in6_ifattach.h>
95 #if defined(COMPAT_43)
96 #include <emulation/43bsd/43bsd_socket.h>
97 #endif /* COMPAT_43 */
99 struct netmsg_ifaddr {
100 struct netmsg_base base;
106 struct ifsubq_stage_head {
107 TAILQ_HEAD(, ifsubq_stage) stg_head;
111 * System initialization
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 *, struct rt_addrinfo *);
120 static int if_rtdel(struct radix_node *, void *);
122 /* Helper functions */
123 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
127 * XXX: declare here to avoid to include many inet6 related files..
128 * should be more generalized?
130 extern void nd6_setmtu(struct ifnet *);
133 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
134 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
136 static int ifsq_stage_cntmax = 4;
137 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
138 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
139 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
141 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
142 /* Must be after netisr_init */
143 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
145 static if_com_alloc_t *if_com_alloc[256];
146 static if_com_free_t *if_com_free[256];
148 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
149 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
150 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
152 int ifqmaxlen = IFQ_MAXLEN;
153 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
155 struct callout if_slowtimo_timer;
158 struct ifnet **ifindex2ifnet = NULL;
159 static struct thread ifnet_threads[MAXCPU];
161 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
164 #define IFQ_KTR_STRING "ifq=%p"
165 #define IFQ_KTR_ARGS struct ifaltq *ifq
167 #define KTR_IFQ KTR_ALL
169 KTR_INFO_MASTER(ifq);
170 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
171 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
172 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
174 #define IF_START_KTR_STRING "ifp=%p"
175 #define IF_START_KTR_ARGS struct ifnet *ifp
177 #define KTR_IF_START KTR_ALL
179 KTR_INFO_MASTER(if_start);
180 KTR_INFO(KTR_IF_START, if_start, run, 0,
181 IF_START_KTR_STRING, IF_START_KTR_ARGS);
182 KTR_INFO(KTR_IF_START, if_start, sched, 1,
183 IF_START_KTR_STRING, IF_START_KTR_ARGS);
184 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
185 IF_START_KTR_STRING, IF_START_KTR_ARGS);
186 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
188 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
190 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
193 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
196 * Network interface utility routines.
198 * Routines with ifa_ifwith* names take sockaddr *'s as
207 callout_init(&if_slowtimo_timer);
210 TAILQ_FOREACH(ifp, &ifnet, if_link) {
211 if (ifp->if_snd.altq_maxlen == 0) {
212 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
213 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
222 ifsq_ifstart_ipifunc(void *arg)
224 struct ifaltq_subque *ifsq = arg;
225 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
228 if (lmsg->ms_flags & MSGF_DONE)
229 lwkt_sendmsg(netisr_portfn(mycpuid), lmsg);
234 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
236 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
237 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
238 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
244 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
246 KKASSERT((stage->stg_flags &
247 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
248 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
249 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
253 * Schedule ifnet.if_start on ifnet's CPU
256 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
260 if (!force && curthread->td_type == TD_TYPE_NETISR &&
261 ifsq_stage_cntmax > 0) {
262 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
266 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
267 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
268 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
272 cpu = ifsq_get_cpuid(ifsq);
274 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
276 ifsq_ifstart_ipifunc(ifsq);
281 * This function will release ifnet.if_start interlock,
282 * if ifnet.if_start does not need to be scheduled
285 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
287 if (!running || ifsq_is_empty(ifsq)
289 || ifsq->ifsq_altq->altq_tbr != NULL
294 * ifnet.if_start interlock is released, if:
295 * 1) Hardware can not take any packets, due to
296 * o interface is marked down
297 * o hardware queue is full (ifq_is_oactive)
298 * Under the second situation, hardware interrupt
299 * or polling(4) will call/schedule ifnet.if_start
300 * when hardware queue is ready
301 * 2) There is not packet in the ifnet.if_snd.
302 * Further ifq_dispatch or ifq_handoff will call/
303 * schedule ifnet.if_start
304 * 3) TBR is used and it does not allow further
306 * TBR callout will call ifnet.if_start
308 if (!running || !ifsq_data_ready(ifsq)) {
309 ifsq_clr_started(ifsq);
310 ALTQ_SQ_UNLOCK(ifsq);
313 ALTQ_SQ_UNLOCK(ifsq);
319 ifsq_ifstart_dispatch(netmsg_t msg)
321 struct lwkt_msg *lmsg = &msg->base.lmsg;
322 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
323 struct ifnet *ifp = ifsq_get_ifp(ifsq);
324 int running = 0, need_sched;
327 lwkt_replymsg(lmsg, 0); /* reply ASAP */
330 if (mycpuid != ifsq_get_cpuid(ifsq)) {
332 * We need to chase the ifnet CPU change.
334 ifsq_ifstart_schedule(ifsq, 1);
338 ifnet_serialize_tx(ifp, ifsq);
339 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
340 ifp->if_start(ifp, ifsq);
341 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
344 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
345 ifnet_deserialize_tx(ifp, ifsq);
349 * More data need to be transmitted, ifnet.if_start is
350 * scheduled on ifnet's CPU, and we keep going.
351 * NOTE: ifnet.if_start interlock is not released.
353 ifsq_ifstart_schedule(ifsq, 0);
357 /* Device driver ifnet.if_start helper function */
359 ifsq_devstart(struct ifaltq_subque *ifsq)
361 struct ifnet *ifp = ifsq_get_ifp(ifsq);
364 ASSERT_IFNET_SERIALIZED_TX(ifp, ifsq);
367 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
368 ALTQ_SQ_UNLOCK(ifsq);
371 ifsq_set_started(ifsq);
372 ALTQ_SQ_UNLOCK(ifsq);
374 ifp->if_start(ifp, ifsq);
376 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
379 if (ifsq_ifstart_need_schedule(ifsq, running)) {
381 * More data need to be transmitted, ifnet.if_start is
382 * scheduled on ifnet's CPU, and we keep going.
383 * NOTE: ifnet.if_start interlock is not released.
385 ifsq_ifstart_schedule(ifsq, 0);
390 if_devstart(struct ifnet *ifp)
392 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
395 /* Device driver ifnet.if_start schedule helper function */
397 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
399 ifsq_ifstart_schedule(ifsq, 1);
403 if_devstart_sched(struct ifnet *ifp)
405 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
409 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
411 lwkt_serialize_enter(ifp->if_serializer);
415 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
417 lwkt_serialize_exit(ifp->if_serializer);
421 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
423 return lwkt_serialize_try(ifp->if_serializer);
428 if_default_serialize_assert(struct ifnet *ifp,
429 enum ifnet_serialize slz __unused,
430 boolean_t serialized)
433 ASSERT_SERIALIZED(ifp->if_serializer);
435 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
440 * Attach an interface to the list of "active" interfaces.
442 * The serializer is optional. If non-NULL access to the interface
446 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
448 unsigned socksize, ifasize;
449 int namelen, masklen;
450 struct sockaddr_dl *sdl;
455 static int if_indexlim = 8;
457 if (ifp->if_serialize != NULL) {
458 KASSERT(ifp->if_deserialize != NULL &&
459 ifp->if_tryserialize != NULL &&
460 ifp->if_serialize_assert != NULL,
461 ("serialize functions are partially setup"));
464 * If the device supplies serialize functions,
465 * then clear if_serializer to catch any invalid
466 * usage of this field.
468 KASSERT(serializer == NULL,
469 ("both serialize functions and default serializer "
471 ifp->if_serializer = NULL;
473 KASSERT(ifp->if_deserialize == NULL &&
474 ifp->if_tryserialize == NULL &&
475 ifp->if_serialize_assert == NULL,
476 ("serialize functions are partially setup"));
477 ifp->if_serialize = if_default_serialize;
478 ifp->if_deserialize = if_default_deserialize;
479 ifp->if_tryserialize = if_default_tryserialize;
481 ifp->if_serialize_assert = if_default_serialize_assert;
485 * The serializer can be passed in from the device,
486 * allowing the same serializer to be used for both
487 * the interrupt interlock and the device queue.
488 * If not specified, the netif structure will use an
489 * embedded serializer.
491 if (serializer == NULL) {
492 serializer = &ifp->if_default_serializer;
493 lwkt_serialize_init(serializer);
495 ifp->if_serializer = serializer;
498 mtx_init(&ifp->if_ioctl_mtx);
499 mtx_lock(&ifp->if_ioctl_mtx);
501 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
502 ifp->if_index = ++if_index;
506 * The old code would work if the interface passed a pre-existing
507 * chain of ifaddrs to this code. We don't trust our callers to
508 * properly initialize the tailq, however, so we no longer allow
509 * this unlikely case.
511 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
512 M_IFADDR, M_WAITOK | M_ZERO);
513 for (i = 0; i < ncpus; ++i)
514 TAILQ_INIT(&ifp->if_addrheads[i]);
516 TAILQ_INIT(&ifp->if_prefixhead);
517 TAILQ_INIT(&ifp->if_multiaddrs);
518 TAILQ_INIT(&ifp->if_groups);
519 getmicrotime(&ifp->if_lastchange);
520 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
526 /* grow ifindex2ifnet */
527 n = if_indexlim * sizeof(*q);
528 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
530 bcopy(ifindex2ifnet, q, n/2);
531 kfree(ifindex2ifnet, M_IFADDR);
536 ifindex2ifnet[if_index] = ifp;
539 * create a Link Level name for this device
541 namelen = strlen(ifp->if_xname);
542 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
543 socksize = masklen + ifp->if_addrlen;
544 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
545 if (socksize < sizeof(*sdl))
546 socksize = sizeof(*sdl);
547 socksize = ROUNDUP(socksize);
549 ifasize = sizeof(struct ifaddr) + 2 * socksize;
550 ifa = ifa_create(ifasize, M_WAITOK);
551 sdl = (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_index = ifp->if_index;
557 sdl->sdl_type = ifp->if_type;
558 ifp->if_lladdr = ifa;
560 ifa->ifa_rtrequest = link_rtrequest;
561 ifa->ifa_addr = (struct sockaddr *)sdl;
562 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
563 ifa->ifa_netmask = (struct sockaddr *)sdl;
564 sdl->sdl_len = masklen;
566 sdl->sdl_data[--namelen] = 0xff;
567 ifa_iflink(ifa, ifp, 0 /* Insert head */);
569 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
570 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
572 if (ifp->if_mapsubq == NULL)
573 ifp->if_mapsubq = ifq_mapsubq_default;
577 ifq->altq_disc = NULL;
578 ifq->altq_flags &= ALTQF_CANTCHANGE;
579 ifq->altq_tbr = NULL;
582 if (ifq->altq_subq_cnt <= 0)
583 ifq->altq_subq_cnt = 1;
584 ifq->altq_subq = kmalloc_cachealign(
585 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
586 M_DEVBUF, M_WAITOK | M_ZERO);
588 if (ifq->altq_maxlen == 0) {
589 if_printf(ifp, "driver didn't set ifq_maxlen\n");
590 ifq_set_maxlen(ifq, ifqmaxlen);
593 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
594 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
596 ALTQ_SQ_LOCK_INIT(ifsq);
597 ifsq->ifsq_index = q;
599 ifsq->ifsq_altq = ifq;
600 ifsq->ifsq_ifp = ifp;
602 ifsq->ifq_maxlen = ifq->altq_maxlen;
603 ifsq->ifsq_prepended = NULL;
604 ifsq->ifsq_started = 0;
605 ifsq->ifsq_hw_oactive = 0;
606 ifsq_set_cpuid(ifsq, 0);
609 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
610 M_DEVBUF, M_WAITOK | M_ZERO);
611 for (i = 0; i < ncpus; ++i)
612 ifsq->ifsq_stage[i].stg_subq = ifsq;
614 ifsq->ifsq_ifstart_nmsg =
615 kmalloc(ncpus * sizeof(struct netmsg_base),
616 M_LWKTMSG, M_WAITOK);
617 for (i = 0; i < ncpus; ++i) {
618 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
619 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
620 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
623 ifq_set_classic(ifq);
625 if (!SLIST_EMPTY(&domains))
626 if_attachdomain1(ifp);
628 /* Announce the interface. */
629 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
631 mtx_unlock(&ifp->if_ioctl_mtx);
635 if_attachdomain(void *dummy)
640 TAILQ_FOREACH(ifp, &ifnet, if_list)
641 if_attachdomain1(ifp);
644 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
645 if_attachdomain, NULL);
648 if_attachdomain1(struct ifnet *ifp)
654 /* address family dependent data region */
655 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
656 SLIST_FOREACH(dp, &domains, dom_next)
657 if (dp->dom_ifattach)
658 ifp->if_afdata[dp->dom_family] =
659 (*dp->dom_ifattach)(ifp);
664 * Purge all addresses whose type is _not_ AF_LINK
667 if_purgeaddrs_nolink(struct ifnet *ifp)
669 struct ifaddr_container *ifac, *next;
671 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
673 struct ifaddr *ifa = ifac->ifa;
675 /* Leave link ifaddr as it is */
676 if (ifa->ifa_addr->sa_family == AF_LINK)
679 /* XXX: Ugly!! ad hoc just for INET */
680 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
681 struct ifaliasreq ifr;
682 #ifdef IFADDR_DEBUG_VERBOSE
685 kprintf("purge in4 addr %p: ", ifa);
686 for (i = 0; i < ncpus; ++i)
687 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
691 bzero(&ifr, sizeof ifr);
692 ifr.ifra_addr = *ifa->ifa_addr;
693 if (ifa->ifa_dstaddr)
694 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
695 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
701 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
702 #ifdef IFADDR_DEBUG_VERBOSE
705 kprintf("purge in6 addr %p: ", ifa);
706 for (i = 0; i < ncpus; ++i)
707 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
712 /* ifp_addrhead is already updated */
716 ifa_ifunlink(ifa, ifp);
722 ifq_stage_detach_handler(netmsg_t nmsg)
724 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
727 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
728 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
729 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
731 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
732 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
734 lwkt_replymsg(&nmsg->lmsg, 0);
738 ifq_stage_detach(struct ifaltq *ifq)
740 struct netmsg_base base;
743 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
744 ifq_stage_detach_handler);
745 base.lmsg.u.ms_resultp = ifq;
747 for (cpu = 0; cpu < ncpus; ++cpu)
748 lwkt_domsg(netisr_portfn(cpu), &base.lmsg, 0);
752 * Detach an interface, removing it from the
753 * list of "active" interfaces.
756 if_detach(struct ifnet *ifp)
758 struct radix_node_head *rnh;
763 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
766 * Remove routes and flush queues.
770 if (ifp->if_flags & IFF_NPOLLING)
771 ifpoll_deregister(ifp);
776 if (ifq_is_enabled(&ifp->if_snd))
777 altq_disable(&ifp->if_snd);
778 if (ifq_is_attached(&ifp->if_snd))
779 altq_detach(&ifp->if_snd);
783 * Clean up all addresses.
785 ifp->if_lladdr = NULL;
787 if_purgeaddrs_nolink(ifp);
788 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
791 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
792 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
793 ("non-link ifaddr is left on if_addrheads"));
795 ifa_ifunlink(ifa, ifp);
797 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
798 ("there are still ifaddrs left on if_addrheads"));
803 * Remove all IPv4 kernel structures related to ifp.
810 * Remove all IPv6 kernel structs related to ifp. This should be done
811 * before removing routing entries below, since IPv6 interface direct
812 * routes are expected to be removed by the IPv6-specific kernel API.
813 * Otherwise, the kernel will detect some inconsistency and bark it.
819 * Delete all remaining routes using this interface
820 * Unfortuneatly the only way to do this is to slog through
821 * the entire routing table looking for routes which point
822 * to this interface...oh well...
825 for (cpu = 0; cpu < ncpus; cpu++) {
826 lwkt_migratecpu(cpu);
827 for (i = 1; i <= AF_MAX; i++) {
828 if ((rnh = rt_tables[cpu][i]) == NULL)
830 rnh->rnh_walktree(rnh, if_rtdel, ifp);
833 lwkt_migratecpu(origcpu);
835 /* Announce that the interface is gone. */
836 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
837 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
839 SLIST_FOREACH(dp, &domains, dom_next)
840 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
841 (*dp->dom_ifdetach)(ifp,
842 ifp->if_afdata[dp->dom_family]);
845 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
847 ifindex2ifnet[ifp->if_index] = NULL;
848 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
851 TAILQ_REMOVE(&ifnet, ifp, if_link);
852 kfree(ifp->if_addrheads, M_IFADDR);
854 lwkt_synchronize_ipiqs("if_detach");
855 ifq_stage_detach(&ifp->if_snd);
857 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
858 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
860 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
861 kfree(ifsq->ifsq_stage, M_DEVBUF);
863 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
869 * Create interface group without members
872 if_creategroup(const char *groupname)
874 struct ifg_group *ifg = NULL;
876 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
877 M_TEMP, M_NOWAIT)) == NULL)
880 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
882 ifg->ifg_carp_demoted = 0;
883 TAILQ_INIT(&ifg->ifg_members);
885 pfi_attach_ifgroup(ifg);
887 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
893 * Add a group to an interface
896 if_addgroup(struct ifnet *ifp, const char *groupname)
898 struct ifg_list *ifgl;
899 struct ifg_group *ifg = NULL;
900 struct ifg_member *ifgm;
902 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
903 groupname[strlen(groupname) - 1] <= '9')
906 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
907 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
910 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
913 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
918 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
919 if (!strcmp(ifg->ifg_group, groupname))
922 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
929 ifgl->ifgl_group = ifg;
930 ifgm->ifgm_ifp = ifp;
932 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
933 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
936 pfi_group_change(groupname);
943 * Remove a group from an interface
946 if_delgroup(struct ifnet *ifp, const char *groupname)
948 struct ifg_list *ifgl;
949 struct ifg_member *ifgm;
951 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
952 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
957 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
959 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
960 if (ifgm->ifgm_ifp == ifp)
964 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
968 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
969 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
971 pfi_detach_ifgroup(ifgl->ifgl_group);
973 kfree(ifgl->ifgl_group, M_TEMP);
979 pfi_group_change(groupname);
986 * Stores all groups from an interface in memory pointed
990 if_getgroup(caddr_t data, struct ifnet *ifp)
993 struct ifg_list *ifgl;
994 struct ifg_req ifgrq, *ifgp;
995 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
997 if (ifgr->ifgr_len == 0) {
998 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
999 ifgr->ifgr_len += sizeof(struct ifg_req);
1003 len = ifgr->ifgr_len;
1004 ifgp = ifgr->ifgr_groups;
1005 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1006 if (len < sizeof(ifgrq))
1008 bzero(&ifgrq, sizeof ifgrq);
1009 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1010 sizeof(ifgrq.ifgrq_group));
1011 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1012 sizeof(struct ifg_req))))
1014 len -= sizeof(ifgrq);
1022 * Stores all members of a group in memory pointed to by data
1025 if_getgroupmembers(caddr_t data)
1027 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1028 struct ifg_group *ifg;
1029 struct ifg_member *ifgm;
1030 struct ifg_req ifgrq, *ifgp;
1033 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1034 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1039 if (ifgr->ifgr_len == 0) {
1040 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1041 ifgr->ifgr_len += sizeof(ifgrq);
1045 len = ifgr->ifgr_len;
1046 ifgp = ifgr->ifgr_groups;
1047 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1048 if (len < sizeof(ifgrq))
1050 bzero(&ifgrq, sizeof ifgrq);
1051 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1052 sizeof(ifgrq.ifgrq_member));
1053 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1054 sizeof(struct ifg_req))))
1056 len -= sizeof(ifgrq);
1064 * Delete Routes for a Network Interface
1066 * Called for each routing entry via the rnh->rnh_walktree() call above
1067 * to delete all route entries referencing a detaching network interface.
1070 * rn pointer to node in the routing table
1071 * arg argument passed to rnh->rnh_walktree() - detaching interface
1075 * errno failed - reason indicated
1079 if_rtdel(struct radix_node *rn, void *arg)
1081 struct rtentry *rt = (struct rtentry *)rn;
1082 struct ifnet *ifp = arg;
1085 if (rt->rt_ifp == ifp) {
1088 * Protect (sorta) against walktree recursion problems
1089 * with cloned routes
1091 if (!(rt->rt_flags & RTF_UP))
1094 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1095 rt_mask(rt), rt->rt_flags,
1098 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1106 * Locate an interface based on a complete address.
1109 ifa_ifwithaddr(struct sockaddr *addr)
1113 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1114 struct ifaddr_container *ifac;
1116 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1117 struct ifaddr *ifa = ifac->ifa;
1119 if (ifa->ifa_addr->sa_family != addr->sa_family)
1121 if (sa_equal(addr, ifa->ifa_addr))
1123 if ((ifp->if_flags & IFF_BROADCAST) &&
1124 ifa->ifa_broadaddr &&
1125 /* IPv6 doesn't have broadcast */
1126 ifa->ifa_broadaddr->sa_len != 0 &&
1127 sa_equal(ifa->ifa_broadaddr, addr))
1134 * Locate the point to point interface with a given destination address.
1137 ifa_ifwithdstaddr(struct sockaddr *addr)
1141 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1142 struct ifaddr_container *ifac;
1144 if (!(ifp->if_flags & IFF_POINTOPOINT))
1147 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1148 struct ifaddr *ifa = ifac->ifa;
1150 if (ifa->ifa_addr->sa_family != addr->sa_family)
1152 if (ifa->ifa_dstaddr &&
1153 sa_equal(addr, ifa->ifa_dstaddr))
1161 * Find an interface on a specific network. If many, choice
1162 * is most specific found.
1165 ifa_ifwithnet(struct sockaddr *addr)
1168 struct ifaddr *ifa_maybe = NULL;
1169 u_int af = addr->sa_family;
1170 char *addr_data = addr->sa_data, *cplim;
1173 * AF_LINK addresses can be looked up directly by their index number,
1174 * so do that if we can.
1176 if (af == AF_LINK) {
1177 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1179 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1180 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1184 * Scan though each interface, looking for ones that have
1185 * addresses in this address family.
1187 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1188 struct ifaddr_container *ifac;
1190 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1191 struct ifaddr *ifa = ifac->ifa;
1192 char *cp, *cp2, *cp3;
1194 if (ifa->ifa_addr->sa_family != af)
1196 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1198 * This is a bit broken as it doesn't
1199 * take into account that the remote end may
1200 * be a single node in the network we are
1202 * The trouble is that we don't know the
1203 * netmask for the remote end.
1205 if (ifa->ifa_dstaddr != NULL &&
1206 sa_equal(addr, ifa->ifa_dstaddr))
1210 * if we have a special address handler,
1211 * then use it instead of the generic one.
1213 if (ifa->ifa_claim_addr) {
1214 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1222 * Scan all the bits in the ifa's address.
1223 * If a bit dissagrees with what we are
1224 * looking for, mask it with the netmask
1225 * to see if it really matters.
1226 * (A byte at a time)
1228 if (ifa->ifa_netmask == 0)
1231 cp2 = ifa->ifa_addr->sa_data;
1232 cp3 = ifa->ifa_netmask->sa_data;
1233 cplim = ifa->ifa_netmask->sa_len +
1234 (char *)ifa->ifa_netmask;
1236 if ((*cp++ ^ *cp2++) & *cp3++)
1237 goto next; /* next address! */
1239 * If the netmask of what we just found
1240 * is more specific than what we had before
1241 * (if we had one) then remember the new one
1242 * before continuing to search
1243 * for an even better one.
1245 if (ifa_maybe == NULL ||
1246 rn_refines((char *)ifa->ifa_netmask,
1247 (char *)ifa_maybe->ifa_netmask))
1256 * Find an interface address specific to an interface best matching
1260 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1262 struct ifaddr_container *ifac;
1263 char *cp, *cp2, *cp3;
1265 struct ifaddr *ifa_maybe = NULL;
1266 u_int af = addr->sa_family;
1270 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1271 struct ifaddr *ifa = ifac->ifa;
1273 if (ifa->ifa_addr->sa_family != af)
1275 if (ifa_maybe == NULL)
1277 if (ifa->ifa_netmask == NULL) {
1278 if (sa_equal(addr, ifa->ifa_addr) ||
1279 (ifa->ifa_dstaddr != NULL &&
1280 sa_equal(addr, ifa->ifa_dstaddr)))
1284 if (ifp->if_flags & IFF_POINTOPOINT) {
1285 if (sa_equal(addr, ifa->ifa_dstaddr))
1289 cp2 = ifa->ifa_addr->sa_data;
1290 cp3 = ifa->ifa_netmask->sa_data;
1291 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1292 for (; cp3 < cplim; cp3++)
1293 if ((*cp++ ^ *cp2++) & *cp3)
1303 * Default action when installing a route with a Link Level gateway.
1304 * Lookup an appropriate real ifa to point to.
1305 * This should be moved to /sys/net/link.c eventually.
1308 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1311 struct sockaddr *dst;
1314 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1315 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1317 ifa = ifaof_ifpforaddr(dst, ifp);
1319 IFAFREE(rt->rt_ifa);
1322 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1323 ifa->ifa_rtrequest(cmd, rt, info);
1328 * Mark an interface down and notify protocols of
1330 * NOTE: must be called at splnet or eqivalent.
1333 if_unroute(struct ifnet *ifp, int flag, int fam)
1335 struct ifaddr_container *ifac;
1337 ifp->if_flags &= ~flag;
1338 getmicrotime(&ifp->if_lastchange);
1339 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1340 struct ifaddr *ifa = ifac->ifa;
1342 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1343 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1345 ifq_purge_all(&ifp->if_snd);
1350 * Mark an interface up and notify protocols of
1352 * NOTE: must be called at splnet or eqivalent.
1355 if_route(struct ifnet *ifp, int flag, int fam)
1357 struct ifaddr_container *ifac;
1359 ifq_purge_all(&ifp->if_snd);
1360 ifp->if_flags |= flag;
1361 getmicrotime(&ifp->if_lastchange);
1362 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1363 struct ifaddr *ifa = ifac->ifa;
1365 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1366 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1375 * Mark an interface down and notify protocols of the transition. An
1376 * interface going down is also considered to be a synchronizing event.
1377 * We must ensure that all packet processing related to the interface
1378 * has completed before we return so e.g. the caller can free the ifnet
1379 * structure that the mbufs may be referencing.
1381 * NOTE: must be called at splnet or eqivalent.
1384 if_down(struct ifnet *ifp)
1386 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1387 netmsg_service_sync();
1391 * Mark an interface up and notify protocols of
1393 * NOTE: must be called at splnet or eqivalent.
1396 if_up(struct ifnet *ifp)
1398 if_route(ifp, IFF_UP, AF_UNSPEC);
1402 * Process a link state change.
1403 * NOTE: must be called at splsoftnet or equivalent.
1406 if_link_state_change(struct ifnet *ifp)
1408 int link_state = ifp->if_link_state;
1411 devctl_notify("IFNET", ifp->if_xname,
1412 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1416 * Handle interface watchdog timer routines. Called
1417 * from softclock, we decrement timers (if set) and
1418 * call the appropriate interface routine on expiration.
1421 if_slowtimo(void *arg)
1427 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1428 if (ifp->if_timer == 0 || --ifp->if_timer)
1430 if (ifp->if_watchdog) {
1431 if (ifnet_tryserialize_all(ifp)) {
1432 (*ifp->if_watchdog)(ifp);
1433 ifnet_deserialize_all(ifp);
1435 /* try again next timeout */
1443 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1447 * Map interface name to
1448 * interface structure pointer.
1451 ifunit(const char *name)
1456 * Search all the interfaces for this name/number
1459 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1460 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1468 * Map interface name in a sockaddr_dl to
1469 * interface structure pointer.
1472 if_withname(struct sockaddr *sa)
1474 char ifname[IFNAMSIZ+1];
1475 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1477 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1478 (sdl->sdl_nlen > IFNAMSIZ) )
1482 * ifunit wants a null-terminated name. It may not be null-terminated
1483 * in the sockaddr. We don't want to change the caller's sockaddr,
1484 * and there might not be room to put the trailing null anyway, so we
1485 * make a local copy that we know we can null terminate safely.
1488 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1489 ifname[sdl->sdl_nlen] = '\0';
1490 return ifunit(ifname);
1498 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1509 size_t namelen, onamelen;
1510 char new_name[IFNAMSIZ];
1512 struct sockaddr_dl *sdl;
1517 return (ifconf(cmd, data, cred));
1522 ifr = (struct ifreq *)data;
1527 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1529 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1530 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1532 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1534 return (if_clone_destroy(ifr->ifr_name));
1535 case SIOCIFGCLONERS:
1536 return (if_clone_list((struct if_clonereq *)data));
1542 * Nominal ioctl through interface, lookup the ifp and obtain a
1543 * lock to serialize the ifconfig ioctl operation.
1545 ifp = ifunit(ifr->ifr_name);
1549 mtx_lock(&ifp->if_ioctl_mtx);
1553 ifr->ifr_index = ifp->if_index;
1557 ifr->ifr_flags = ifp->if_flags;
1558 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1562 ifr->ifr_reqcap = ifp->if_capabilities;
1563 ifr->ifr_curcap = ifp->if_capenable;
1567 ifr->ifr_metric = ifp->if_metric;
1571 ifr->ifr_mtu = ifp->if_mtu;
1575 ifr->ifr_tsolen = ifp->if_tsolen;
1579 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1580 sizeof(ifp->if_data));
1584 ifr->ifr_phys = ifp->if_physical;
1587 case SIOCGIFPOLLCPU:
1588 ifr->ifr_pollcpu = -1;
1591 case SIOCSIFPOLLCPU:
1595 error = priv_check_cred(cred, PRIV_ROOT, 0);
1598 new_flags = (ifr->ifr_flags & 0xffff) |
1599 (ifr->ifr_flagshigh << 16);
1600 if (ifp->if_flags & IFF_SMART) {
1601 /* Smart drivers twiddle their own routes */
1602 } else if (ifp->if_flags & IFF_UP &&
1603 (new_flags & IFF_UP) == 0) {
1607 } else if (new_flags & IFF_UP &&
1608 (ifp->if_flags & IFF_UP) == 0) {
1614 #ifdef IFPOLL_ENABLE
1615 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1616 if (new_flags & IFF_NPOLLING)
1617 ifpoll_register(ifp);
1619 ifpoll_deregister(ifp);
1623 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1624 (new_flags &~ IFF_CANTCHANGE);
1625 if (new_flags & IFF_PPROMISC) {
1626 /* Permanently promiscuous mode requested */
1627 ifp->if_flags |= IFF_PROMISC;
1628 } else if (ifp->if_pcount == 0) {
1629 ifp->if_flags &= ~IFF_PROMISC;
1631 if (ifp->if_ioctl) {
1632 ifnet_serialize_all(ifp);
1633 ifp->if_ioctl(ifp, cmd, data, cred);
1634 ifnet_deserialize_all(ifp);
1636 getmicrotime(&ifp->if_lastchange);
1640 error = priv_check_cred(cred, PRIV_ROOT, 0);
1643 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1647 ifnet_serialize_all(ifp);
1648 ifp->if_ioctl(ifp, cmd, data, cred);
1649 ifnet_deserialize_all(ifp);
1653 error = priv_check_cred(cred, PRIV_ROOT, 0);
1656 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1659 if (new_name[0] == '\0') {
1663 if (ifunit(new_name) != NULL) {
1668 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1670 /* Announce the departure of the interface. */
1671 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1673 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1674 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1675 /* XXX IFA_LOCK(ifa); */
1676 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1677 namelen = strlen(new_name);
1678 onamelen = sdl->sdl_nlen;
1680 * Move the address if needed. This is safe because we
1681 * allocate space for a name of length IFNAMSIZ when we
1682 * create this in if_attach().
1684 if (namelen != onamelen) {
1685 bcopy(sdl->sdl_data + onamelen,
1686 sdl->sdl_data + namelen, sdl->sdl_alen);
1688 bcopy(new_name, sdl->sdl_data, namelen);
1689 sdl->sdl_nlen = namelen;
1690 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1691 bzero(sdl->sdl_data, onamelen);
1692 while (namelen != 0)
1693 sdl->sdl_data[--namelen] = 0xff;
1694 /* XXX IFA_UNLOCK(ifa) */
1696 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1698 /* Announce the return of the interface. */
1699 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1703 error = priv_check_cred(cred, PRIV_ROOT, 0);
1706 ifp->if_metric = ifr->ifr_metric;
1707 getmicrotime(&ifp->if_lastchange);
1711 error = priv_check_cred(cred, PRIV_ROOT, 0);
1714 if (ifp->if_ioctl == NULL) {
1718 ifnet_serialize_all(ifp);
1719 error = ifp->if_ioctl(ifp, cmd, data, cred);
1720 ifnet_deserialize_all(ifp);
1722 getmicrotime(&ifp->if_lastchange);
1727 u_long oldmtu = ifp->if_mtu;
1729 error = priv_check_cred(cred, PRIV_ROOT, 0);
1732 if (ifp->if_ioctl == NULL) {
1736 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1740 ifnet_serialize_all(ifp);
1741 error = ifp->if_ioctl(ifp, cmd, data, cred);
1742 ifnet_deserialize_all(ifp);
1744 getmicrotime(&ifp->if_lastchange);
1748 * If the link MTU changed, do network layer specific procedure.
1750 if (ifp->if_mtu != oldmtu) {
1759 error = priv_check_cred(cred, PRIV_ROOT, 0);
1763 /* XXX need driver supplied upper limit */
1764 if (ifr->ifr_tsolen <= 0) {
1768 ifp->if_tsolen = ifr->ifr_tsolen;
1773 error = priv_check_cred(cred, PRIV_ROOT, 0);
1777 /* Don't allow group membership on non-multicast interfaces. */
1778 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1783 /* Don't let users screw up protocols' entries. */
1784 if (ifr->ifr_addr.sa_family != AF_LINK) {
1789 if (cmd == SIOCADDMULTI) {
1790 struct ifmultiaddr *ifma;
1791 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1793 error = if_delmulti(ifp, &ifr->ifr_addr);
1796 getmicrotime(&ifp->if_lastchange);
1799 case SIOCSIFPHYADDR:
1800 case SIOCDIFPHYADDR:
1802 case SIOCSIFPHYADDR_IN6:
1804 case SIOCSLIFPHYADDR:
1806 case SIOCSIFGENERIC:
1807 error = priv_check_cred(cred, PRIV_ROOT, 0);
1810 if (ifp->if_ioctl == 0) {
1814 ifnet_serialize_all(ifp);
1815 error = ifp->if_ioctl(ifp, cmd, data, cred);
1816 ifnet_deserialize_all(ifp);
1818 getmicrotime(&ifp->if_lastchange);
1822 ifs = (struct ifstat *)data;
1823 ifs->ascii[0] = '\0';
1825 case SIOCGIFPSRCADDR:
1826 case SIOCGIFPDSTADDR:
1827 case SIOCGLIFPHYADDR:
1829 case SIOCGIFGENERIC:
1830 if (ifp->if_ioctl == NULL) {
1834 ifnet_serialize_all(ifp);
1835 error = ifp->if_ioctl(ifp, cmd, data, cred);
1836 ifnet_deserialize_all(ifp);
1840 error = priv_check_cred(cred, PRIV_ROOT, 0);
1843 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1844 ifr->ifr_addr.sa_len);
1845 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1849 oif_flags = ifp->if_flags;
1850 if (so->so_proto == 0) {
1855 error = so_pru_control_direct(so, cmd, data, ifp);
1860 case SIOCSIFDSTADDR:
1862 case SIOCSIFBRDADDR:
1863 case SIOCSIFNETMASK:
1864 #if BYTE_ORDER != BIG_ENDIAN
1865 if (ifr->ifr_addr.sa_family == 0 &&
1866 ifr->ifr_addr.sa_len < 16) {
1867 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1868 ifr->ifr_addr.sa_len = 16;
1871 if (ifr->ifr_addr.sa_len == 0)
1872 ifr->ifr_addr.sa_len = 16;
1878 case OSIOCGIFDSTADDR:
1879 cmd = SIOCGIFDSTADDR;
1881 case OSIOCGIFBRDADDR:
1882 cmd = SIOCGIFBRDADDR;
1884 case OSIOCGIFNETMASK:
1885 cmd = SIOCGIFNETMASK;
1891 error = so_pru_control_direct(so, cmd, data, ifp);
1895 case OSIOCGIFDSTADDR:
1896 case OSIOCGIFBRDADDR:
1897 case OSIOCGIFNETMASK:
1898 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1901 #endif /* COMPAT_43 */
1903 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1905 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1906 if (ifp->if_flags & IFF_UP) {
1916 mtx_unlock(&ifp->if_ioctl_mtx);
1921 * Set/clear promiscuous mode on interface ifp based on the truth value
1922 * of pswitch. The calls are reference counted so that only the first
1923 * "on" request actually has an effect, as does the final "off" request.
1924 * Results are undefined if the "off" and "on" requests are not matched.
1927 ifpromisc(struct ifnet *ifp, int pswitch)
1933 oldflags = ifp->if_flags;
1934 if (ifp->if_flags & IFF_PPROMISC) {
1935 /* Do nothing if device is in permanently promiscuous mode */
1936 ifp->if_pcount += pswitch ? 1 : -1;
1941 * If the device is not configured up, we cannot put it in
1944 if ((ifp->if_flags & IFF_UP) == 0)
1946 if (ifp->if_pcount++ != 0)
1948 ifp->if_flags |= IFF_PROMISC;
1949 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1952 if (--ifp->if_pcount > 0)
1954 ifp->if_flags &= ~IFF_PROMISC;
1955 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1958 ifr.ifr_flags = ifp->if_flags;
1959 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1960 ifnet_serialize_all(ifp);
1961 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1962 ifnet_deserialize_all(ifp);
1966 ifp->if_flags = oldflags;
1971 * Return interface configuration
1972 * of system. List may be used
1973 * in later ioctl's (above) to get
1974 * other information.
1977 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
1979 struct ifconf *ifc = (struct ifconf *)data;
1981 struct sockaddr *sa;
1982 struct ifreq ifr, *ifrp;
1983 int space = ifc->ifc_len, error = 0;
1985 ifrp = ifc->ifc_req;
1986 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1987 struct ifaddr_container *ifac;
1990 if (space <= sizeof ifr)
1994 * Zero the stack declared structure first to prevent
1995 * memory disclosure.
1997 bzero(&ifr, sizeof(ifr));
1998 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
1999 >= sizeof(ifr.ifr_name)) {
2000 error = ENAMETOOLONG;
2005 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2006 struct ifaddr *ifa = ifac->ifa;
2008 if (space <= sizeof ifr)
2011 if (cred->cr_prison &&
2012 prison_if(cred, sa))
2016 if (cmd == OSIOCGIFCONF) {
2017 struct osockaddr *osa =
2018 (struct osockaddr *)&ifr.ifr_addr;
2020 osa->sa_family = sa->sa_family;
2021 error = copyout(&ifr, ifrp, sizeof ifr);
2025 if (sa->sa_len <= sizeof(*sa)) {
2027 error = copyout(&ifr, ifrp, sizeof ifr);
2030 if (space < (sizeof ifr) + sa->sa_len -
2033 space -= sa->sa_len - sizeof(*sa);
2034 error = copyout(&ifr, ifrp,
2035 sizeof ifr.ifr_name);
2037 error = copyout(sa, &ifrp->ifr_addr,
2039 ifrp = (struct ifreq *)
2040 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2044 space -= sizeof ifr;
2049 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2050 error = copyout(&ifr, ifrp, sizeof ifr);
2053 space -= sizeof ifr;
2057 ifc->ifc_len -= space;
2062 * Just like if_promisc(), but for all-multicast-reception mode.
2065 if_allmulti(struct ifnet *ifp, int onswitch)
2073 if (ifp->if_amcount++ == 0) {
2074 ifp->if_flags |= IFF_ALLMULTI;
2075 ifr.ifr_flags = ifp->if_flags;
2076 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2077 ifnet_serialize_all(ifp);
2078 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2080 ifnet_deserialize_all(ifp);
2083 if (ifp->if_amcount > 1) {
2086 ifp->if_amcount = 0;
2087 ifp->if_flags &= ~IFF_ALLMULTI;
2088 ifr.ifr_flags = ifp->if_flags;
2089 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2090 ifnet_serialize_all(ifp);
2091 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2093 ifnet_deserialize_all(ifp);
2105 * Add a multicast listenership to the interface in question.
2106 * The link layer provides a routine which converts
2110 struct ifnet *ifp, /* interface to manipulate */
2111 struct sockaddr *sa, /* address to add */
2112 struct ifmultiaddr **retifma)
2114 struct sockaddr *llsa, *dupsa;
2116 struct ifmultiaddr *ifma;
2119 * If the matching multicast address already exists
2120 * then don't add a new one, just add a reference
2122 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2123 if (sa_equal(sa, ifma->ifma_addr)) {
2124 ifma->ifma_refcount++;
2132 * Give the link layer a chance to accept/reject it, and also
2133 * find out which AF_LINK address this maps to, if it isn't one
2136 if (ifp->if_resolvemulti) {
2137 ifnet_serialize_all(ifp);
2138 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2139 ifnet_deserialize_all(ifp);
2146 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2147 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2148 bcopy(sa, dupsa, sa->sa_len);
2150 ifma->ifma_addr = dupsa;
2151 ifma->ifma_lladdr = llsa;
2152 ifma->ifma_ifp = ifp;
2153 ifma->ifma_refcount = 1;
2154 ifma->ifma_protospec = 0;
2155 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2158 * Some network interfaces can scan the address list at
2159 * interrupt time; lock them out.
2162 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2168 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2169 if (sa_equal(ifma->ifma_addr, llsa))
2173 ifma->ifma_refcount++;
2175 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2176 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2177 bcopy(llsa, dupsa, llsa->sa_len);
2178 ifma->ifma_addr = dupsa;
2179 ifma->ifma_ifp = ifp;
2180 ifma->ifma_refcount = 1;
2182 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2187 * We are certain we have added something, so call down to the
2188 * interface to let them know about it.
2191 ifnet_serialize_all(ifp);
2193 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2194 ifnet_deserialize_all(ifp);
2201 * Remove a reference to a multicast address on this interface. Yell
2202 * if the request does not match an existing membership.
2205 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2207 struct ifmultiaddr *ifma;
2209 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2210 if (sa_equal(sa, ifma->ifma_addr))
2215 if (ifma->ifma_refcount > 1) {
2216 ifma->ifma_refcount--;
2220 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2221 sa = ifma->ifma_lladdr;
2223 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2225 * Make sure the interface driver is notified
2226 * in the case of a link layer mcast group being left.
2228 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2229 ifnet_serialize_all(ifp);
2230 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2231 ifnet_deserialize_all(ifp);
2234 kfree(ifma->ifma_addr, M_IFMADDR);
2235 kfree(ifma, M_IFMADDR);
2240 * Now look for the link-layer address which corresponds to
2241 * this network address. It had been squirreled away in
2242 * ifma->ifma_lladdr for this purpose (so we don't have
2243 * to call ifp->if_resolvemulti() again), and we saved that
2244 * value in sa above. If some nasty deleted the
2245 * link-layer address out from underneath us, we can deal because
2246 * the address we stored was is not the same as the one which was
2247 * in the record for the link-layer address. (So we don't complain
2250 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2251 if (sa_equal(sa, ifma->ifma_addr))
2256 if (ifma->ifma_refcount > 1) {
2257 ifma->ifma_refcount--;
2262 ifnet_serialize_all(ifp);
2263 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2264 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2265 ifnet_deserialize_all(ifp);
2267 kfree(ifma->ifma_addr, M_IFMADDR);
2268 kfree(sa, M_IFMADDR);
2269 kfree(ifma, M_IFMADDR);
2275 * Delete all multicast group membership for an interface.
2276 * Should be used to quickly flush all multicast filters.
2279 if_delallmulti(struct ifnet *ifp)
2281 struct ifmultiaddr *ifma;
2282 struct ifmultiaddr *next;
2284 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2285 if_delmulti(ifp, ifma->ifma_addr);
2290 * Set the link layer address on an interface.
2292 * At this time we only support certain types of interfaces,
2293 * and we don't allow the length of the address to change.
2296 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2298 struct sockaddr_dl *sdl;
2301 sdl = IF_LLSOCKADDR(ifp);
2304 if (len != sdl->sdl_alen) /* don't allow length to change */
2306 switch (ifp->if_type) {
2307 case IFT_ETHER: /* these types use struct arpcom */
2310 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2311 bcopy(lladdr, LLADDR(sdl), len);
2317 * If the interface is already up, we need
2318 * to re-init it in order to reprogram its
2321 ifnet_serialize_all(ifp);
2322 if ((ifp->if_flags & IFF_UP) != 0) {
2324 struct ifaddr_container *ifac;
2327 ifp->if_flags &= ~IFF_UP;
2328 ifr.ifr_flags = ifp->if_flags;
2329 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2330 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2332 ifp->if_flags |= IFF_UP;
2333 ifr.ifr_flags = ifp->if_flags;
2334 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2335 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2339 * Also send gratuitous ARPs to notify other nodes about
2340 * the address change.
2342 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2343 struct ifaddr *ifa = ifac->ifa;
2345 if (ifa->ifa_addr != NULL &&
2346 ifa->ifa_addr->sa_family == AF_INET)
2347 arp_gratuitous(ifp, ifa);
2351 ifnet_deserialize_all(ifp);
2355 struct ifmultiaddr *
2356 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2358 struct ifmultiaddr *ifma;
2360 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2361 if (sa_equal(ifma->ifma_addr, sa))
2368 * This function locates the first real ethernet MAC from a network
2369 * card and loads it into node, returning 0 on success or ENOENT if
2370 * no suitable interfaces were found. It is used by the uuid code to
2371 * generate a unique 6-byte number.
2374 if_getanyethermac(uint16_t *node, int minlen)
2377 struct sockaddr_dl *sdl;
2379 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2380 if (ifp->if_type != IFT_ETHER)
2382 sdl = IF_LLSOCKADDR(ifp);
2383 if (sdl->sdl_alen < minlen)
2385 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2393 * The name argument must be a pointer to storage which will last as
2394 * long as the interface does. For physical devices, the result of
2395 * device_get_name(dev) is a good choice and for pseudo-devices a
2396 * static string works well.
2399 if_initname(struct ifnet *ifp, const char *name, int unit)
2401 ifp->if_dname = name;
2402 ifp->if_dunit = unit;
2403 if (unit != IF_DUNIT_NONE)
2404 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2406 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2410 if_printf(struct ifnet *ifp, const char *fmt, ...)
2415 retval = kprintf("%s: ", ifp->if_xname);
2416 __va_start(ap, fmt);
2417 retval += kvprintf(fmt, ap);
2423 if_alloc(uint8_t type)
2429 * XXX temporary hack until arpcom is setup in if_l2com
2431 if (type == IFT_ETHER)
2432 size = sizeof(struct arpcom);
2434 size = sizeof(struct ifnet);
2436 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2438 ifp->if_type = type;
2440 if (if_com_alloc[type] != NULL) {
2441 ifp->if_l2com = if_com_alloc[type](type, ifp);
2442 if (ifp->if_l2com == NULL) {
2443 kfree(ifp, M_IFNET);
2451 if_free(struct ifnet *ifp)
2453 kfree(ifp, M_IFNET);
2457 ifq_set_classic(struct ifaltq *ifq)
2459 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2460 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2464 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2465 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2469 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2470 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2471 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2472 KASSERT(request != NULL, ("request is not specified"));
2474 ifq->altq_mapsubq = mapsubq;
2475 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2476 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2478 ifsq->ifsq_enqueue = enqueue;
2479 ifsq->ifsq_dequeue = dequeue;
2480 ifsq->ifsq_request = request;
2485 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2486 struct altq_pktattr *pa __unused)
2488 if (IF_QFULL(ifsq)) {
2492 IF_ENQUEUE(ifsq, m);
2498 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, struct mbuf *mpolled, int op)
2507 IF_DEQUEUE(ifsq, m);
2510 panic("unsupported ALTQ dequeue op: %d", op);
2512 KKASSERT(mpolled == NULL || mpolled == m);
2517 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2524 panic("unsupported ALTQ request: %d", req);
2530 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2532 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2533 int running = 0, need_sched;
2536 * Try to do direct ifnet.if_start first, if there is
2537 * contention on ifnet's serializer, ifnet.if_start will
2538 * be scheduled on ifnet's CPU.
2540 if (!ifnet_tryserialize_tx(ifp, ifsq)) {
2542 * ifnet serializer contention happened,
2543 * ifnet.if_start is scheduled on ifnet's
2544 * CPU, and we keep going.
2546 ifsq_ifstart_schedule(ifsq, 1);
2550 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2551 ifp->if_start(ifp, ifsq);
2552 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2555 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2557 ifnet_deserialize_tx(ifp, ifsq);
2561 * More data need to be transmitted, ifnet.if_start is
2562 * scheduled on ifnet's CPU, and we keep going.
2563 * NOTE: ifnet.if_start interlock is not released.
2565 ifsq_ifstart_schedule(ifsq, force_sched);
2570 * IFSUBQ packets staging mechanism:
2572 * The packets enqueued into IFSUBQ are staged to a certain amount before the
2573 * ifnet's if_start is called. In this way, the driver could avoid writing
2574 * to hardware registers upon every packet, instead, hardware registers
2575 * could be written when certain amount of packets are put onto hardware
2576 * TX ring. The measurement on several modern NICs (emx(4), igb(4), bnx(4),
2577 * bge(4), jme(4)) shows that the hardware registers writing aggregation
2578 * could save ~20% CPU time when 18bytes UDP datagrams are transmitted at
2579 * 1.48Mpps. The performance improvement by hardware registers writing
2580 * aggeregation is also mentioned by Luigi Rizzo's netmap paper
2581 * (http://info.iet.unipi.it/~luigi/netmap/).
2583 * IFSUBQ packets staging is performed for two entry points into drivers's
2584 * transmission function:
2585 * - Direct ifnet's if_start calling, i.e. ifsq_ifstart_try()
2586 * - ifnet's if_start scheduling, i.e. ifsq_ifstart_schedule()
2588 * IFSUBQ packets staging will be stopped upon any of the following conditions:
2589 * - If the count of packets enqueued on the current CPU is great than or
2590 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2591 * - If the total length of packets enqueued on the current CPU is great
2592 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2593 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2594 * is usually less than hardware's MTU.
2595 * - ifsq_ifstart_schedule() is not pending on the current CPU and if_start
2596 * interlock (if_snd.altq_started) is not released.
2597 * - The if_start_rollup(), which is registered as low priority netisr
2598 * rollup function, is called; probably because no more work is pending
2602 * Currently IFSUBQ packet staging is only performed in netisr threads.
2605 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2607 struct ifaltq *ifq = &ifp->if_snd;
2608 struct ifaltq_subque *ifsq;
2609 int error, start = 0, len, mcast = 0, avoid_start = 0;
2610 struct ifsubq_stage_head *head = NULL;
2611 struct ifsubq_stage *stage = NULL;
2613 ifsq = ifq_map_subq(ifq, mycpuid);
2614 ASSERT_IFNET_NOT_SERIALIZED_TX(ifp, ifsq);
2616 len = m->m_pkthdr.len;
2617 if (m->m_flags & M_MCAST)
2620 if (curthread->td_type == TD_TYPE_NETISR) {
2621 head = &ifsubq_stage_heads[mycpuid];
2622 stage = ifsq_get_stage(ifsq, mycpuid);
2625 stage->stg_len += len;
2626 if (stage->stg_cnt < ifsq_stage_cntmax &&
2627 stage->stg_len < (ifp->if_mtu - max_protohdr))
2632 error = ifsq_enqueue_locked(ifsq, m, pa);
2634 if (!ifsq_data_ready(ifsq)) {
2635 ALTQ_SQ_UNLOCK(ifsq);
2640 if (!ifsq_is_started(ifsq)) {
2642 ALTQ_SQ_UNLOCK(ifsq);
2645 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2646 ifsq_stage_insert(head, stage);
2648 ifp->if_obytes += len;
2655 * Hold the interlock of ifnet.if_start
2657 ifsq_set_started(ifsq);
2660 ALTQ_SQ_UNLOCK(ifsq);
2663 ifp->if_obytes += len;
2668 if (stage != NULL) {
2669 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2670 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
2672 ifsq_stage_remove(head, stage);
2673 ifsq_ifstart_schedule(ifsq, 1);
2678 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2679 ifsq_stage_remove(head, stage);
2689 ifsq_ifstart_try(ifsq, 0);
2694 ifa_create(int size, int flags)
2699 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2701 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2705 ifa->ifa_containers = kmalloc(ncpus * sizeof(struct ifaddr_container),
2706 M_IFADDR, M_WAITOK | M_ZERO);
2707 ifa->ifa_ncnt = ncpus;
2708 for (i = 0; i < ncpus; ++i) {
2709 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2711 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2713 ifac->ifa_refcnt = 1;
2716 kprintf("alloc ifa %p %d\n", ifa, size);
2722 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2724 struct ifaddr *ifa = ifac->ifa;
2726 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2727 KKASSERT(ifac->ifa_refcnt == 0);
2728 KASSERT(ifac->ifa_listmask == 0,
2729 ("ifa is still on %#x lists", ifac->ifa_listmask));
2731 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2733 #ifdef IFADDR_DEBUG_VERBOSE
2734 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2737 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2738 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2739 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2741 kprintf("free ifa %p\n", ifa);
2743 kfree(ifa->ifa_containers, M_IFADDR);
2744 kfree(ifa, M_IFADDR);
2749 ifa_iflink_dispatch(netmsg_t nmsg)
2751 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2752 struct ifaddr *ifa = msg->ifa;
2753 struct ifnet *ifp = msg->ifp;
2755 struct ifaddr_container *ifac;
2759 ifac = &ifa->ifa_containers[cpu];
2760 ASSERT_IFAC_VALID(ifac);
2761 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2762 ("ifaddr is on if_addrheads"));
2764 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2766 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2768 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2772 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2776 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2778 struct netmsg_ifaddr msg;
2780 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2781 0, ifa_iflink_dispatch);
2786 ifa_domsg(&msg.base.lmsg, 0);
2790 ifa_ifunlink_dispatch(netmsg_t nmsg)
2792 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2793 struct ifaddr *ifa = msg->ifa;
2794 struct ifnet *ifp = msg->ifp;
2796 struct ifaddr_container *ifac;
2800 ifac = &ifa->ifa_containers[cpu];
2801 ASSERT_IFAC_VALID(ifac);
2802 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2803 ("ifaddr is not on if_addrhead"));
2805 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2806 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2810 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2814 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2816 struct netmsg_ifaddr msg;
2818 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2819 0, ifa_ifunlink_dispatch);
2823 ifa_domsg(&msg.base.lmsg, 0);
2827 ifa_destroy_dispatch(netmsg_t nmsg)
2829 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2832 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2836 ifa_destroy(struct ifaddr *ifa)
2838 struct netmsg_ifaddr msg;
2840 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2841 0, ifa_destroy_dispatch);
2844 ifa_domsg(&msg.base.lmsg, 0);
2848 ifnet_portfn(int cpu)
2850 return &ifnet_threads[cpu].td_msgport;
2854 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2856 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2858 if (next_cpu < ncpus)
2859 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2861 lwkt_replymsg(lmsg, 0);
2865 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2867 KKASSERT(cpu < ncpus);
2868 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2872 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2874 KKASSERT(cpu < ncpus);
2875 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2879 * Generic netmsg service loop. Some protocols may roll their own but all
2880 * must do the basic command dispatch function call done here.
2883 ifnet_service_loop(void *arg __unused)
2887 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2888 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2889 msg->base.nm_dispatch(msg);
2894 if_start_rollup(void)
2896 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
2897 struct ifsubq_stage *stage;
2899 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
2900 struct ifaltq_subque *ifsq = stage->stg_subq;
2903 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
2905 ifsq_stage_remove(head, stage);
2908 ifsq_ifstart_schedule(ifsq, 1);
2913 if (!ifsq_is_started(ifsq)) {
2915 * Hold the interlock of ifnet.if_start
2917 ifsq_set_started(ifsq);
2920 ALTQ_SQ_UNLOCK(ifsq);
2923 ifsq_ifstart_try(ifsq, 1);
2925 KKASSERT((stage->stg_flags &
2926 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
2931 ifnetinit(void *dummy __unused)
2935 for (i = 0; i < ncpus; ++i) {
2936 struct thread *thr = &ifnet_threads[i];
2938 lwkt_create(ifnet_service_loop, NULL, NULL,
2939 thr, TDF_NOSTART|TDF_FORCE_SPINPORT,
2941 netmsg_service_port_init(&thr->td_msgport);
2945 for (i = 0; i < ncpus; ++i)
2946 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
2947 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
2951 ifnet_byindex(unsigned short idx)
2955 return ifindex2ifnet[idx];
2959 ifaddr_byindex(unsigned short idx)
2963 ifp = ifnet_byindex(idx);
2966 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2970 if_register_com_alloc(u_char type,
2971 if_com_alloc_t *a, if_com_free_t *f)
2974 KASSERT(if_com_alloc[type] == NULL,
2975 ("if_register_com_alloc: %d already registered", type));
2976 KASSERT(if_com_free[type] == NULL,
2977 ("if_register_com_alloc: %d free already registered", type));
2979 if_com_alloc[type] = a;
2980 if_com_free[type] = f;
2984 if_deregister_com_alloc(u_char type)
2987 KASSERT(if_com_alloc[type] != NULL,
2988 ("if_deregister_com_alloc: %d not registered", type));
2989 KASSERT(if_com_free[type] != NULL,
2990 ("if_deregister_com_alloc: %d free not registered", type));
2991 if_com_alloc[type] = NULL;
2992 if_com_free[type] = NULL;
2996 if_ring_count2(int cnt, int cnt_max)
3000 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3001 ("invalid ring count max %d", cnt_max));
3010 while ((1 << (shift + 1)) <= cnt)
3014 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3015 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3016 cnt, ncpus2, cnt_max));
3021 ifq_set_maxlen(struct ifaltq *ifq, int len)
3023 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3027 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3029 return ALTQ_SUBQ_INDEX_DEFAULT;
3033 ifsq_watchdog(void *arg)
3035 struct ifsubq_watchdog *wd = arg;
3038 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3041 ifp = ifsq_get_ifp(wd->wd_subq);
3042 if (ifnet_tryserialize_all(ifp)) {
3043 wd->wd_watchdog(wd->wd_subq);
3044 ifnet_deserialize_all(ifp);
3046 /* try again next timeout */
3050 ifsq_watchdog_reset(wd);
3054 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3056 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3057 ifsq_get_cpuid(wd->wd_subq));
3061 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3062 ifsq_watchdog_t watchdog)
3064 callout_init_mp(&wd->wd_callout);
3067 wd->wd_watchdog = watchdog;
3071 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3074 ifsq_watchdog_reset(wd);
3078 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3081 callout_stop(&wd->wd_callout);