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 * 4. 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.
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
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 $
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
36 #include "opt_ifpoll.h"
38 #include <sys/param.h>
39 #include <sys/malloc.h>
41 #include <sys/systm.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/socketops.h>
48 #include <sys/protosw.h>
49 #include <sys/kernel.h>
51 #include <sys/mutex.h>
52 #include <sys/sockio.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
55 #include <sys/domain.h>
56 #include <sys/thread.h>
57 #include <sys/serialize.h>
60 #include <sys/thread2.h>
61 #include <sys/msgport2.h>
62 #include <sys/mutex2.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_var.h>
69 #include <net/ifq_var.h>
70 #include <net/radix.h>
71 #include <net/route.h>
72 #include <net/if_clone.h>
73 #include <net/netisr2.h>
74 #include <net/netmsg2.h>
76 #include <machine/atomic.h>
77 #include <machine/stdarg.h>
78 #include <machine/smp.h>
80 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
91 #if defined(COMPAT_43)
92 #include <emulation/43bsd/43bsd_socket.h>
93 #endif /* COMPAT_43 */
95 struct netmsg_ifaddr {
96 struct netmsg_base base;
102 struct ifsubq_stage_head {
103 TAILQ_HEAD(, ifsubq_stage) stg_head;
107 * System initialization
109 static void if_attachdomain(void *);
110 static void if_attachdomain1(struct ifnet *);
111 static int ifconf(u_long, caddr_t, struct ucred *);
112 static void ifinit(void *);
113 static void ifnetinit(void *);
114 static void if_slowtimo(void *);
115 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
116 static int if_rtdel(struct radix_node *, void *);
118 /* Helper functions */
119 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
123 * XXX: declare here to avoid to include many inet6 related files..
124 * should be more generalized?
126 extern void nd6_setmtu(struct ifnet *);
129 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
130 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
132 static int ifsq_stage_cntmax = 4;
133 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
134 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
135 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
137 static int if_stats_compat = 0;
138 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
139 &if_stats_compat, 0, "Compat the old ifnet stats");
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_cpuport(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 the subqueue owner 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 subqueue interlock,
282 * if ifnet.if_start for the subqueue 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 subqueue 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 (ifsq_is_oactive)
298 * Under the second situation, hardware interrupt
299 * or polling(4) will call/schedule ifnet.if_start
300 * on the subqueue when hardware queue is ready
301 * 2) There is no packet in the subqueue.
302 * Further ifq_dispatch or ifq_handoff will call/
303 * schedule ifnet.if_start on the subqueue.
304 * 3) TBR is used and it does not allow further
306 * TBR callout will call ifnet.if_start on the
309 if (!running || !ifsq_data_ready(ifsq)) {
310 ifsq_clr_started(ifsq);
311 ALTQ_SQ_UNLOCK(ifsq);
314 ALTQ_SQ_UNLOCK(ifsq);
320 ifsq_ifstart_dispatch(netmsg_t msg)
322 struct lwkt_msg *lmsg = &msg->base.lmsg;
323 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
324 struct ifnet *ifp = ifsq_get_ifp(ifsq);
325 int running = 0, need_sched;
328 lwkt_replymsg(lmsg, 0); /* reply ASAP */
331 if (mycpuid != ifsq_get_cpuid(ifsq)) {
333 * We need to chase the subqueue owner CPU change.
335 ifsq_ifstart_schedule(ifsq, 1);
339 ifsq_serialize_hw(ifsq);
340 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
341 ifp->if_start(ifp, ifsq);
342 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
345 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
346 ifsq_deserialize_hw(ifsq);
350 * More data need to be transmitted, ifnet.if_start is
351 * scheduled on the subqueue owner CPU, and we keep going.
352 * NOTE: ifnet.if_start subqueue interlock is not released.
354 ifsq_ifstart_schedule(ifsq, 0);
358 /* Device driver ifnet.if_start helper function */
360 ifsq_devstart(struct ifaltq_subque *ifsq)
362 struct ifnet *ifp = ifsq_get_ifp(ifsq);
365 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
368 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
369 ALTQ_SQ_UNLOCK(ifsq);
372 ifsq_set_started(ifsq);
373 ALTQ_SQ_UNLOCK(ifsq);
375 ifp->if_start(ifp, ifsq);
377 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
380 if (ifsq_ifstart_need_schedule(ifsq, running)) {
382 * More data need to be transmitted, ifnet.if_start is
383 * scheduled on ifnet's CPU, and we keep going.
384 * NOTE: ifnet.if_start interlock is not released.
386 ifsq_ifstart_schedule(ifsq, 0);
391 if_devstart(struct ifnet *ifp)
393 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
396 /* Device driver ifnet.if_start schedule helper function */
398 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
400 ifsq_ifstart_schedule(ifsq, 1);
404 if_devstart_sched(struct ifnet *ifp)
406 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
410 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
412 lwkt_serialize_enter(ifp->if_serializer);
416 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
418 lwkt_serialize_exit(ifp->if_serializer);
422 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
424 return lwkt_serialize_try(ifp->if_serializer);
429 if_default_serialize_assert(struct ifnet *ifp,
430 enum ifnet_serialize slz __unused,
431 boolean_t serialized)
434 ASSERT_SERIALIZED(ifp->if_serializer);
436 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
441 * Attach an interface to the list of "active" interfaces.
443 * The serializer is optional.
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 ifp->if_data_pcpu = kmalloc_cachealign(
570 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
572 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
573 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
575 if (ifp->if_mapsubq == NULL)
576 ifp->if_mapsubq = ifq_mapsubq_default;
580 ifq->altq_disc = NULL;
581 ifq->altq_flags &= ALTQF_CANTCHANGE;
582 ifq->altq_tbr = NULL;
585 if (ifq->altq_subq_cnt <= 0)
586 ifq->altq_subq_cnt = 1;
587 ifq->altq_subq = kmalloc_cachealign(
588 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
589 M_DEVBUF, M_WAITOK | M_ZERO);
591 if (ifq->altq_maxlen == 0) {
592 if_printf(ifp, "driver didn't set ifq_maxlen\n");
593 ifq_set_maxlen(ifq, ifqmaxlen);
596 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
597 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
599 ALTQ_SQ_LOCK_INIT(ifsq);
600 ifsq->ifsq_index = q;
602 ifsq->ifsq_altq = ifq;
603 ifsq->ifsq_ifp = ifp;
605 ifsq->ifq_maxlen = ifq->altq_maxlen;
606 ifsq->ifsq_prepended = NULL;
607 ifsq->ifsq_started = 0;
608 ifsq->ifsq_hw_oactive = 0;
609 ifsq_set_cpuid(ifsq, 0);
610 if (ifp->if_serializer != NULL)
611 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
614 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
615 M_DEVBUF, M_WAITOK | M_ZERO);
616 for (i = 0; i < ncpus; ++i)
617 ifsq->ifsq_stage[i].stg_subq = ifsq;
619 ifsq->ifsq_ifstart_nmsg =
620 kmalloc(ncpus * sizeof(struct netmsg_base),
621 M_LWKTMSG, M_WAITOK);
622 for (i = 0; i < ncpus; ++i) {
623 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
624 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
625 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
628 ifq_set_classic(ifq);
630 if (!SLIST_EMPTY(&domains))
631 if_attachdomain1(ifp);
633 /* Announce the interface. */
634 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
636 mtx_unlock(&ifp->if_ioctl_mtx);
640 if_attachdomain(void *dummy)
645 TAILQ_FOREACH(ifp, &ifnet, if_list)
646 if_attachdomain1(ifp);
649 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
650 if_attachdomain, NULL);
653 if_attachdomain1(struct ifnet *ifp)
659 /* address family dependent data region */
660 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
661 SLIST_FOREACH(dp, &domains, dom_next)
662 if (dp->dom_ifattach)
663 ifp->if_afdata[dp->dom_family] =
664 (*dp->dom_ifattach)(ifp);
669 * Purge all addresses whose type is _not_ AF_LINK
672 if_purgeaddrs_nolink(struct ifnet *ifp)
674 struct ifaddr_container *ifac, *next;
676 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
678 struct ifaddr *ifa = ifac->ifa;
680 /* Leave link ifaddr as it is */
681 if (ifa->ifa_addr->sa_family == AF_LINK)
684 /* XXX: Ugly!! ad hoc just for INET */
685 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
686 struct ifaliasreq ifr;
687 #ifdef IFADDR_DEBUG_VERBOSE
690 kprintf("purge in4 addr %p: ", ifa);
691 for (i = 0; i < ncpus; ++i)
692 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
696 bzero(&ifr, sizeof ifr);
697 ifr.ifra_addr = *ifa->ifa_addr;
698 if (ifa->ifa_dstaddr)
699 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
700 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
706 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
707 #ifdef IFADDR_DEBUG_VERBOSE
710 kprintf("purge in6 addr %p: ", ifa);
711 for (i = 0; i < ncpus; ++i)
712 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
717 /* ifp_addrhead is already updated */
721 ifa_ifunlink(ifa, ifp);
727 ifq_stage_detach_handler(netmsg_t nmsg)
729 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
732 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
733 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
734 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
736 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
737 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
739 lwkt_replymsg(&nmsg->lmsg, 0);
743 ifq_stage_detach(struct ifaltq *ifq)
745 struct netmsg_base base;
748 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
749 ifq_stage_detach_handler);
750 base.lmsg.u.ms_resultp = ifq;
752 for (cpu = 0; cpu < ncpus; ++cpu)
753 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
757 * Detach an interface, removing it from the
758 * list of "active" interfaces.
761 if_detach(struct ifnet *ifp)
763 struct radix_node_head *rnh;
768 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
771 * Remove routes and flush queues.
775 if (ifp->if_flags & IFF_NPOLLING)
776 ifpoll_deregister(ifp);
781 if (ifq_is_enabled(&ifp->if_snd))
782 altq_disable(&ifp->if_snd);
783 if (ifq_is_attached(&ifp->if_snd))
784 altq_detach(&ifp->if_snd);
788 * Clean up all addresses.
790 ifp->if_lladdr = NULL;
792 if_purgeaddrs_nolink(ifp);
793 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
796 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
797 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
798 ("non-link ifaddr is left on if_addrheads"));
800 ifa_ifunlink(ifa, ifp);
802 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
803 ("there are still ifaddrs left on if_addrheads"));
808 * Remove all IPv4 kernel structures related to ifp.
815 * Remove all IPv6 kernel structs related to ifp. This should be done
816 * before removing routing entries below, since IPv6 interface direct
817 * routes are expected to be removed by the IPv6-specific kernel API.
818 * Otherwise, the kernel will detect some inconsistency and bark it.
824 * Delete all remaining routes using this interface
825 * Unfortuneatly the only way to do this is to slog through
826 * the entire routing table looking for routes which point
827 * to this interface...oh well...
830 for (cpu = 0; cpu < ncpus; cpu++) {
831 lwkt_migratecpu(cpu);
832 for (i = 1; i <= AF_MAX; i++) {
833 if ((rnh = rt_tables[cpu][i]) == NULL)
835 rnh->rnh_walktree(rnh, if_rtdel, ifp);
838 lwkt_migratecpu(origcpu);
840 /* Announce that the interface is gone. */
841 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
842 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
844 SLIST_FOREACH(dp, &domains, dom_next)
845 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
846 (*dp->dom_ifdetach)(ifp,
847 ifp->if_afdata[dp->dom_family]);
850 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
852 ifindex2ifnet[ifp->if_index] = NULL;
853 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
856 TAILQ_REMOVE(&ifnet, ifp, if_link);
857 kfree(ifp->if_addrheads, M_IFADDR);
859 lwkt_synchronize_ipiqs("if_detach");
860 ifq_stage_detach(&ifp->if_snd);
862 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
863 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
865 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
866 kfree(ifsq->ifsq_stage, M_DEVBUF);
868 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
870 kfree(ifp->if_data_pcpu, M_DEVBUF);
876 * Create interface group without members
879 if_creategroup(const char *groupname)
881 struct ifg_group *ifg = NULL;
883 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
884 M_TEMP, M_NOWAIT)) == NULL)
887 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
889 ifg->ifg_carp_demoted = 0;
890 TAILQ_INIT(&ifg->ifg_members);
892 pfi_attach_ifgroup(ifg);
894 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
900 * Add a group to an interface
903 if_addgroup(struct ifnet *ifp, const char *groupname)
905 struct ifg_list *ifgl;
906 struct ifg_group *ifg = NULL;
907 struct ifg_member *ifgm;
909 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
910 groupname[strlen(groupname) - 1] <= '9')
913 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
914 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
917 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
920 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
925 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
926 if (!strcmp(ifg->ifg_group, groupname))
929 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
936 ifgl->ifgl_group = ifg;
937 ifgm->ifgm_ifp = ifp;
939 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
940 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
943 pfi_group_change(groupname);
950 * Remove a group from an interface
953 if_delgroup(struct ifnet *ifp, const char *groupname)
955 struct ifg_list *ifgl;
956 struct ifg_member *ifgm;
958 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
959 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
964 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
966 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
967 if (ifgm->ifgm_ifp == ifp)
971 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
975 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
976 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
978 pfi_detach_ifgroup(ifgl->ifgl_group);
980 kfree(ifgl->ifgl_group, M_TEMP);
986 pfi_group_change(groupname);
993 * Stores all groups from an interface in memory pointed
997 if_getgroup(caddr_t data, struct ifnet *ifp)
1000 struct ifg_list *ifgl;
1001 struct ifg_req ifgrq, *ifgp;
1002 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1004 if (ifgr->ifgr_len == 0) {
1005 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1006 ifgr->ifgr_len += sizeof(struct ifg_req);
1010 len = ifgr->ifgr_len;
1011 ifgp = ifgr->ifgr_groups;
1012 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1013 if (len < sizeof(ifgrq))
1015 bzero(&ifgrq, sizeof ifgrq);
1016 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1017 sizeof(ifgrq.ifgrq_group));
1018 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1019 sizeof(struct ifg_req))))
1021 len -= sizeof(ifgrq);
1029 * Stores all members of a group in memory pointed to by data
1032 if_getgroupmembers(caddr_t data)
1034 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1035 struct ifg_group *ifg;
1036 struct ifg_member *ifgm;
1037 struct ifg_req ifgrq, *ifgp;
1040 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1041 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1046 if (ifgr->ifgr_len == 0) {
1047 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1048 ifgr->ifgr_len += sizeof(ifgrq);
1052 len = ifgr->ifgr_len;
1053 ifgp = ifgr->ifgr_groups;
1054 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1055 if (len < sizeof(ifgrq))
1057 bzero(&ifgrq, sizeof ifgrq);
1058 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1059 sizeof(ifgrq.ifgrq_member));
1060 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1061 sizeof(struct ifg_req))))
1063 len -= sizeof(ifgrq);
1071 * Delete Routes for a Network Interface
1073 * Called for each routing entry via the rnh->rnh_walktree() call above
1074 * to delete all route entries referencing a detaching network interface.
1077 * rn pointer to node in the routing table
1078 * arg argument passed to rnh->rnh_walktree() - detaching interface
1082 * errno failed - reason indicated
1086 if_rtdel(struct radix_node *rn, void *arg)
1088 struct rtentry *rt = (struct rtentry *)rn;
1089 struct ifnet *ifp = arg;
1092 if (rt->rt_ifp == ifp) {
1095 * Protect (sorta) against walktree recursion problems
1096 * with cloned routes
1098 if (!(rt->rt_flags & RTF_UP))
1101 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1102 rt_mask(rt), rt->rt_flags,
1105 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1113 * Locate an interface based on a complete address.
1116 ifa_ifwithaddr(struct sockaddr *addr)
1120 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1121 struct ifaddr_container *ifac;
1123 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1124 struct ifaddr *ifa = ifac->ifa;
1126 if (ifa->ifa_addr->sa_family != addr->sa_family)
1128 if (sa_equal(addr, ifa->ifa_addr))
1130 if ((ifp->if_flags & IFF_BROADCAST) &&
1131 ifa->ifa_broadaddr &&
1132 /* IPv6 doesn't have broadcast */
1133 ifa->ifa_broadaddr->sa_len != 0 &&
1134 sa_equal(ifa->ifa_broadaddr, addr))
1141 * Locate the point to point interface with a given destination address.
1144 ifa_ifwithdstaddr(struct sockaddr *addr)
1148 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1149 struct ifaddr_container *ifac;
1151 if (!(ifp->if_flags & IFF_POINTOPOINT))
1154 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1155 struct ifaddr *ifa = ifac->ifa;
1157 if (ifa->ifa_addr->sa_family != addr->sa_family)
1159 if (ifa->ifa_dstaddr &&
1160 sa_equal(addr, ifa->ifa_dstaddr))
1168 * Find an interface on a specific network. If many, choice
1169 * is most specific found.
1172 ifa_ifwithnet(struct sockaddr *addr)
1175 struct ifaddr *ifa_maybe = NULL;
1176 u_int af = addr->sa_family;
1177 char *addr_data = addr->sa_data, *cplim;
1180 * AF_LINK addresses can be looked up directly by their index number,
1181 * so do that if we can.
1183 if (af == AF_LINK) {
1184 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1186 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1187 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1191 * Scan though each interface, looking for ones that have
1192 * addresses in this address family.
1194 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1195 struct ifaddr_container *ifac;
1197 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1198 struct ifaddr *ifa = ifac->ifa;
1199 char *cp, *cp2, *cp3;
1201 if (ifa->ifa_addr->sa_family != af)
1203 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1205 * This is a bit broken as it doesn't
1206 * take into account that the remote end may
1207 * be a single node in the network we are
1209 * The trouble is that we don't know the
1210 * netmask for the remote end.
1212 if (ifa->ifa_dstaddr != NULL &&
1213 sa_equal(addr, ifa->ifa_dstaddr))
1217 * if we have a special address handler,
1218 * then use it instead of the generic one.
1220 if (ifa->ifa_claim_addr) {
1221 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1229 * Scan all the bits in the ifa's address.
1230 * If a bit dissagrees with what we are
1231 * looking for, mask it with the netmask
1232 * to see if it really matters.
1233 * (A byte at a time)
1235 if (ifa->ifa_netmask == 0)
1238 cp2 = ifa->ifa_addr->sa_data;
1239 cp3 = ifa->ifa_netmask->sa_data;
1240 cplim = ifa->ifa_netmask->sa_len +
1241 (char *)ifa->ifa_netmask;
1243 if ((*cp++ ^ *cp2++) & *cp3++)
1244 goto next; /* next address! */
1246 * If the netmask of what we just found
1247 * is more specific than what we had before
1248 * (if we had one) then remember the new one
1249 * before continuing to search
1250 * for an even better one.
1252 if (ifa_maybe == NULL ||
1253 rn_refines((char *)ifa->ifa_netmask,
1254 (char *)ifa_maybe->ifa_netmask))
1263 * Find an interface address specific to an interface best matching
1267 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1269 struct ifaddr_container *ifac;
1270 char *cp, *cp2, *cp3;
1272 struct ifaddr *ifa_maybe = NULL;
1273 u_int af = addr->sa_family;
1277 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1278 struct ifaddr *ifa = ifac->ifa;
1280 if (ifa->ifa_addr->sa_family != af)
1282 if (ifa_maybe == NULL)
1284 if (ifa->ifa_netmask == NULL) {
1285 if (sa_equal(addr, ifa->ifa_addr) ||
1286 (ifa->ifa_dstaddr != NULL &&
1287 sa_equal(addr, ifa->ifa_dstaddr)))
1291 if (ifp->if_flags & IFF_POINTOPOINT) {
1292 if (sa_equal(addr, ifa->ifa_dstaddr))
1296 cp2 = ifa->ifa_addr->sa_data;
1297 cp3 = ifa->ifa_netmask->sa_data;
1298 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1299 for (; cp3 < cplim; cp3++)
1300 if ((*cp++ ^ *cp2++) & *cp3)
1310 * Default action when installing a route with a Link Level gateway.
1311 * Lookup an appropriate real ifa to point to.
1312 * This should be moved to /sys/net/link.c eventually.
1315 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1318 struct sockaddr *dst;
1321 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1322 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1324 ifa = ifaof_ifpforaddr(dst, ifp);
1326 IFAFREE(rt->rt_ifa);
1329 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1330 ifa->ifa_rtrequest(cmd, rt, info);
1335 * Mark an interface down and notify protocols of
1337 * NOTE: must be called at splnet or eqivalent.
1340 if_unroute(struct ifnet *ifp, int flag, int fam)
1342 struct ifaddr_container *ifac;
1344 ifp->if_flags &= ~flag;
1345 getmicrotime(&ifp->if_lastchange);
1346 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1347 struct ifaddr *ifa = ifac->ifa;
1349 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1350 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1352 ifq_purge_all(&ifp->if_snd);
1357 * Mark an interface up and notify protocols of
1359 * NOTE: must be called at splnet or eqivalent.
1362 if_route(struct ifnet *ifp, int flag, int fam)
1364 struct ifaddr_container *ifac;
1366 ifq_purge_all(&ifp->if_snd);
1367 ifp->if_flags |= flag;
1368 getmicrotime(&ifp->if_lastchange);
1369 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1370 struct ifaddr *ifa = ifac->ifa;
1372 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1373 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1382 * Mark an interface down and notify protocols of the transition. An
1383 * interface going down is also considered to be a synchronizing event.
1384 * We must ensure that all packet processing related to the interface
1385 * has completed before we return so e.g. the caller can free the ifnet
1386 * structure that the mbufs may be referencing.
1388 * NOTE: must be called at splnet or eqivalent.
1391 if_down(struct ifnet *ifp)
1393 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1394 netmsg_service_sync();
1398 * Mark an interface up and notify protocols of
1400 * NOTE: must be called at splnet or eqivalent.
1403 if_up(struct ifnet *ifp)
1405 if_route(ifp, IFF_UP, AF_UNSPEC);
1409 * Process a link state change.
1410 * NOTE: must be called at splsoftnet or equivalent.
1413 if_link_state_change(struct ifnet *ifp)
1415 int link_state = ifp->if_link_state;
1418 devctl_notify("IFNET", ifp->if_xname,
1419 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1423 * Handle interface watchdog timer routines. Called
1424 * from softclock, we decrement timers (if set) and
1425 * call the appropriate interface routine on expiration.
1428 if_slowtimo(void *arg)
1434 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1435 if (if_stats_compat) {
1436 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1437 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1438 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1439 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1440 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1441 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1442 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1443 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1444 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1445 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1446 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1449 if (ifp->if_timer == 0 || --ifp->if_timer)
1451 if (ifp->if_watchdog) {
1452 if (ifnet_tryserialize_all(ifp)) {
1453 (*ifp->if_watchdog)(ifp);
1454 ifnet_deserialize_all(ifp);
1456 /* try again next timeout */
1464 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1468 * Map interface name to
1469 * interface structure pointer.
1472 ifunit(const char *name)
1477 * Search all the interfaces for this name/number
1480 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1481 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1489 * Map interface name in a sockaddr_dl to
1490 * interface structure pointer.
1493 if_withname(struct sockaddr *sa)
1495 char ifname[IFNAMSIZ+1];
1496 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1498 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1499 (sdl->sdl_nlen > IFNAMSIZ) )
1503 * ifunit wants a null-terminated name. It may not be null-terminated
1504 * in the sockaddr. We don't want to change the caller's sockaddr,
1505 * and there might not be room to put the trailing null anyway, so we
1506 * make a local copy that we know we can null terminate safely.
1509 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1510 ifname[sdl->sdl_nlen] = '\0';
1511 return ifunit(ifname);
1519 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1530 size_t namelen, onamelen;
1531 char new_name[IFNAMSIZ];
1533 struct sockaddr_dl *sdl;
1538 return (ifconf(cmd, data, cred));
1543 ifr = (struct ifreq *)data;
1548 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1550 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1551 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1553 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1555 return (if_clone_destroy(ifr->ifr_name));
1556 case SIOCIFGCLONERS:
1557 return (if_clone_list((struct if_clonereq *)data));
1563 * Nominal ioctl through interface, lookup the ifp and obtain a
1564 * lock to serialize the ifconfig ioctl operation.
1566 ifp = ifunit(ifr->ifr_name);
1570 mtx_lock(&ifp->if_ioctl_mtx);
1574 ifr->ifr_index = ifp->if_index;
1578 ifr->ifr_flags = ifp->if_flags;
1579 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1583 ifr->ifr_reqcap = ifp->if_capabilities;
1584 ifr->ifr_curcap = ifp->if_capenable;
1588 ifr->ifr_metric = ifp->if_metric;
1592 ifr->ifr_mtu = ifp->if_mtu;
1596 ifr->ifr_tsolen = ifp->if_tsolen;
1600 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1601 sizeof(ifp->if_data));
1605 ifr->ifr_phys = ifp->if_physical;
1608 case SIOCGIFPOLLCPU:
1609 ifr->ifr_pollcpu = -1;
1612 case SIOCSIFPOLLCPU:
1616 error = priv_check_cred(cred, PRIV_ROOT, 0);
1619 new_flags = (ifr->ifr_flags & 0xffff) |
1620 (ifr->ifr_flagshigh << 16);
1621 if (ifp->if_flags & IFF_SMART) {
1622 /* Smart drivers twiddle their own routes */
1623 } else if (ifp->if_flags & IFF_UP &&
1624 (new_flags & IFF_UP) == 0) {
1628 } else if (new_flags & IFF_UP &&
1629 (ifp->if_flags & IFF_UP) == 0) {
1635 #ifdef IFPOLL_ENABLE
1636 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1637 if (new_flags & IFF_NPOLLING)
1638 ifpoll_register(ifp);
1640 ifpoll_deregister(ifp);
1644 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1645 (new_flags &~ IFF_CANTCHANGE);
1646 if (new_flags & IFF_PPROMISC) {
1647 /* Permanently promiscuous mode requested */
1648 ifp->if_flags |= IFF_PROMISC;
1649 } else if (ifp->if_pcount == 0) {
1650 ifp->if_flags &= ~IFF_PROMISC;
1652 if (ifp->if_ioctl) {
1653 ifnet_serialize_all(ifp);
1654 ifp->if_ioctl(ifp, cmd, data, cred);
1655 ifnet_deserialize_all(ifp);
1657 getmicrotime(&ifp->if_lastchange);
1661 error = priv_check_cred(cred, PRIV_ROOT, 0);
1664 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1668 ifnet_serialize_all(ifp);
1669 ifp->if_ioctl(ifp, cmd, data, cred);
1670 ifnet_deserialize_all(ifp);
1674 error = priv_check_cred(cred, PRIV_ROOT, 0);
1677 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1680 if (new_name[0] == '\0') {
1684 if (ifunit(new_name) != NULL) {
1689 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1691 /* Announce the departure of the interface. */
1692 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1694 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1695 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1696 /* XXX IFA_LOCK(ifa); */
1697 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1698 namelen = strlen(new_name);
1699 onamelen = sdl->sdl_nlen;
1701 * Move the address if needed. This is safe because we
1702 * allocate space for a name of length IFNAMSIZ when we
1703 * create this in if_attach().
1705 if (namelen != onamelen) {
1706 bcopy(sdl->sdl_data + onamelen,
1707 sdl->sdl_data + namelen, sdl->sdl_alen);
1709 bcopy(new_name, sdl->sdl_data, namelen);
1710 sdl->sdl_nlen = namelen;
1711 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1712 bzero(sdl->sdl_data, onamelen);
1713 while (namelen != 0)
1714 sdl->sdl_data[--namelen] = 0xff;
1715 /* XXX IFA_UNLOCK(ifa) */
1717 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1719 /* Announce the return of the interface. */
1720 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1724 error = priv_check_cred(cred, PRIV_ROOT, 0);
1727 ifp->if_metric = ifr->ifr_metric;
1728 getmicrotime(&ifp->if_lastchange);
1732 error = priv_check_cred(cred, PRIV_ROOT, 0);
1735 if (ifp->if_ioctl == NULL) {
1739 ifnet_serialize_all(ifp);
1740 error = ifp->if_ioctl(ifp, cmd, data, cred);
1741 ifnet_deserialize_all(ifp);
1743 getmicrotime(&ifp->if_lastchange);
1748 u_long oldmtu = ifp->if_mtu;
1750 error = priv_check_cred(cred, PRIV_ROOT, 0);
1753 if (ifp->if_ioctl == NULL) {
1757 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1761 ifnet_serialize_all(ifp);
1762 error = ifp->if_ioctl(ifp, cmd, data, cred);
1763 ifnet_deserialize_all(ifp);
1765 getmicrotime(&ifp->if_lastchange);
1769 * If the link MTU changed, do network layer specific procedure.
1771 if (ifp->if_mtu != oldmtu) {
1780 error = priv_check_cred(cred, PRIV_ROOT, 0);
1784 /* XXX need driver supplied upper limit */
1785 if (ifr->ifr_tsolen <= 0) {
1789 ifp->if_tsolen = ifr->ifr_tsolen;
1794 error = priv_check_cred(cred, PRIV_ROOT, 0);
1798 /* Don't allow group membership on non-multicast interfaces. */
1799 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1804 /* Don't let users screw up protocols' entries. */
1805 if (ifr->ifr_addr.sa_family != AF_LINK) {
1810 if (cmd == SIOCADDMULTI) {
1811 struct ifmultiaddr *ifma;
1812 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1814 error = if_delmulti(ifp, &ifr->ifr_addr);
1817 getmicrotime(&ifp->if_lastchange);
1820 case SIOCSIFPHYADDR:
1821 case SIOCDIFPHYADDR:
1823 case SIOCSIFPHYADDR_IN6:
1825 case SIOCSLIFPHYADDR:
1827 case SIOCSIFGENERIC:
1828 error = priv_check_cred(cred, PRIV_ROOT, 0);
1831 if (ifp->if_ioctl == 0) {
1835 ifnet_serialize_all(ifp);
1836 error = ifp->if_ioctl(ifp, cmd, data, cred);
1837 ifnet_deserialize_all(ifp);
1839 getmicrotime(&ifp->if_lastchange);
1843 ifs = (struct ifstat *)data;
1844 ifs->ascii[0] = '\0';
1846 case SIOCGIFPSRCADDR:
1847 case SIOCGIFPDSTADDR:
1848 case SIOCGLIFPHYADDR:
1850 case SIOCGIFGENERIC:
1851 if (ifp->if_ioctl == NULL) {
1855 ifnet_serialize_all(ifp);
1856 error = ifp->if_ioctl(ifp, cmd, data, cred);
1857 ifnet_deserialize_all(ifp);
1861 error = priv_check_cred(cred, PRIV_ROOT, 0);
1864 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1865 ifr->ifr_addr.sa_len);
1866 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1870 oif_flags = ifp->if_flags;
1871 if (so->so_proto == 0) {
1876 error = so_pru_control_direct(so, cmd, data, ifp);
1881 case SIOCSIFDSTADDR:
1883 case SIOCSIFBRDADDR:
1884 case SIOCSIFNETMASK:
1885 #if BYTE_ORDER != BIG_ENDIAN
1886 if (ifr->ifr_addr.sa_family == 0 &&
1887 ifr->ifr_addr.sa_len < 16) {
1888 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1889 ifr->ifr_addr.sa_len = 16;
1892 if (ifr->ifr_addr.sa_len == 0)
1893 ifr->ifr_addr.sa_len = 16;
1899 case OSIOCGIFDSTADDR:
1900 cmd = SIOCGIFDSTADDR;
1902 case OSIOCGIFBRDADDR:
1903 cmd = SIOCGIFBRDADDR;
1905 case OSIOCGIFNETMASK:
1906 cmd = SIOCGIFNETMASK;
1912 error = so_pru_control_direct(so, cmd, data, ifp);
1916 case OSIOCGIFDSTADDR:
1917 case OSIOCGIFBRDADDR:
1918 case OSIOCGIFNETMASK:
1919 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1922 #endif /* COMPAT_43 */
1924 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1926 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1927 if (ifp->if_flags & IFF_UP) {
1937 mtx_unlock(&ifp->if_ioctl_mtx);
1942 * Set/clear promiscuous mode on interface ifp based on the truth value
1943 * of pswitch. The calls are reference counted so that only the first
1944 * "on" request actually has an effect, as does the final "off" request.
1945 * Results are undefined if the "off" and "on" requests are not matched.
1948 ifpromisc(struct ifnet *ifp, int pswitch)
1954 oldflags = ifp->if_flags;
1955 if (ifp->if_flags & IFF_PPROMISC) {
1956 /* Do nothing if device is in permanently promiscuous mode */
1957 ifp->if_pcount += pswitch ? 1 : -1;
1962 * If the device is not configured up, we cannot put it in
1965 if ((ifp->if_flags & IFF_UP) == 0)
1967 if (ifp->if_pcount++ != 0)
1969 ifp->if_flags |= IFF_PROMISC;
1970 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1973 if (--ifp->if_pcount > 0)
1975 ifp->if_flags &= ~IFF_PROMISC;
1976 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1979 ifr.ifr_flags = ifp->if_flags;
1980 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1981 ifnet_serialize_all(ifp);
1982 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1983 ifnet_deserialize_all(ifp);
1987 ifp->if_flags = oldflags;
1992 * Return interface configuration
1993 * of system. List may be used
1994 * in later ioctl's (above) to get
1995 * other information.
1998 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2000 struct ifconf *ifc = (struct ifconf *)data;
2002 struct sockaddr *sa;
2003 struct ifreq ifr, *ifrp;
2004 int space = ifc->ifc_len, error = 0;
2006 ifrp = ifc->ifc_req;
2007 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2008 struct ifaddr_container *ifac;
2011 if (space <= sizeof ifr)
2015 * Zero the stack declared structure first to prevent
2016 * memory disclosure.
2018 bzero(&ifr, sizeof(ifr));
2019 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2020 >= sizeof(ifr.ifr_name)) {
2021 error = ENAMETOOLONG;
2026 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2027 struct ifaddr *ifa = ifac->ifa;
2029 if (space <= sizeof ifr)
2032 if (cred->cr_prison &&
2033 prison_if(cred, sa))
2037 if (cmd == OSIOCGIFCONF) {
2038 struct osockaddr *osa =
2039 (struct osockaddr *)&ifr.ifr_addr;
2041 osa->sa_family = sa->sa_family;
2042 error = copyout(&ifr, ifrp, sizeof ifr);
2046 if (sa->sa_len <= sizeof(*sa)) {
2048 error = copyout(&ifr, ifrp, sizeof ifr);
2051 if (space < (sizeof ifr) + sa->sa_len -
2054 space -= sa->sa_len - sizeof(*sa);
2055 error = copyout(&ifr, ifrp,
2056 sizeof ifr.ifr_name);
2058 error = copyout(sa, &ifrp->ifr_addr,
2060 ifrp = (struct ifreq *)
2061 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2065 space -= sizeof ifr;
2070 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2071 error = copyout(&ifr, ifrp, sizeof ifr);
2074 space -= sizeof ifr;
2078 ifc->ifc_len -= space;
2083 * Just like if_promisc(), but for all-multicast-reception mode.
2086 if_allmulti(struct ifnet *ifp, int onswitch)
2094 if (ifp->if_amcount++ == 0) {
2095 ifp->if_flags |= IFF_ALLMULTI;
2096 ifr.ifr_flags = ifp->if_flags;
2097 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2098 ifnet_serialize_all(ifp);
2099 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2101 ifnet_deserialize_all(ifp);
2104 if (ifp->if_amcount > 1) {
2107 ifp->if_amcount = 0;
2108 ifp->if_flags &= ~IFF_ALLMULTI;
2109 ifr.ifr_flags = ifp->if_flags;
2110 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2111 ifnet_serialize_all(ifp);
2112 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2114 ifnet_deserialize_all(ifp);
2126 * Add a multicast listenership to the interface in question.
2127 * The link layer provides a routine which converts
2131 struct ifnet *ifp, /* interface to manipulate */
2132 struct sockaddr *sa, /* address to add */
2133 struct ifmultiaddr **retifma)
2135 struct sockaddr *llsa, *dupsa;
2137 struct ifmultiaddr *ifma;
2140 * If the matching multicast address already exists
2141 * then don't add a new one, just add a reference
2143 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2144 if (sa_equal(sa, ifma->ifma_addr)) {
2145 ifma->ifma_refcount++;
2153 * Give the link layer a chance to accept/reject it, and also
2154 * find out which AF_LINK address this maps to, if it isn't one
2157 if (ifp->if_resolvemulti) {
2158 ifnet_serialize_all(ifp);
2159 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2160 ifnet_deserialize_all(ifp);
2167 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2168 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2169 bcopy(sa, dupsa, sa->sa_len);
2171 ifma->ifma_addr = dupsa;
2172 ifma->ifma_lladdr = llsa;
2173 ifma->ifma_ifp = ifp;
2174 ifma->ifma_refcount = 1;
2175 ifma->ifma_protospec = 0;
2176 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2179 * Some network interfaces can scan the address list at
2180 * interrupt time; lock them out.
2183 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2189 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2190 if (sa_equal(ifma->ifma_addr, llsa))
2194 ifma->ifma_refcount++;
2196 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2197 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2198 bcopy(llsa, dupsa, llsa->sa_len);
2199 ifma->ifma_addr = dupsa;
2200 ifma->ifma_ifp = ifp;
2201 ifma->ifma_refcount = 1;
2203 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2208 * We are certain we have added something, so call down to the
2209 * interface to let them know about it.
2212 ifnet_serialize_all(ifp);
2214 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2215 ifnet_deserialize_all(ifp);
2222 * Remove a reference to a multicast address on this interface. Yell
2223 * if the request does not match an existing membership.
2226 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2228 struct ifmultiaddr *ifma;
2230 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2231 if (sa_equal(sa, ifma->ifma_addr))
2236 if (ifma->ifma_refcount > 1) {
2237 ifma->ifma_refcount--;
2241 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2242 sa = ifma->ifma_lladdr;
2244 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2246 * Make sure the interface driver is notified
2247 * in the case of a link layer mcast group being left.
2249 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2250 ifnet_serialize_all(ifp);
2251 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2252 ifnet_deserialize_all(ifp);
2255 kfree(ifma->ifma_addr, M_IFMADDR);
2256 kfree(ifma, M_IFMADDR);
2261 * Now look for the link-layer address which corresponds to
2262 * this network address. It had been squirreled away in
2263 * ifma->ifma_lladdr for this purpose (so we don't have
2264 * to call ifp->if_resolvemulti() again), and we saved that
2265 * value in sa above. If some nasty deleted the
2266 * link-layer address out from underneath us, we can deal because
2267 * the address we stored was is not the same as the one which was
2268 * in the record for the link-layer address. (So we don't complain
2271 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2272 if (sa_equal(sa, ifma->ifma_addr))
2277 if (ifma->ifma_refcount > 1) {
2278 ifma->ifma_refcount--;
2283 ifnet_serialize_all(ifp);
2284 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2285 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2286 ifnet_deserialize_all(ifp);
2288 kfree(ifma->ifma_addr, M_IFMADDR);
2289 kfree(sa, M_IFMADDR);
2290 kfree(ifma, M_IFMADDR);
2296 * Delete all multicast group membership for an interface.
2297 * Should be used to quickly flush all multicast filters.
2300 if_delallmulti(struct ifnet *ifp)
2302 struct ifmultiaddr *ifma;
2303 struct ifmultiaddr *next;
2305 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2306 if_delmulti(ifp, ifma->ifma_addr);
2311 * Set the link layer address on an interface.
2313 * At this time we only support certain types of interfaces,
2314 * and we don't allow the length of the address to change.
2317 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2319 struct sockaddr_dl *sdl;
2322 sdl = IF_LLSOCKADDR(ifp);
2325 if (len != sdl->sdl_alen) /* don't allow length to change */
2327 switch (ifp->if_type) {
2328 case IFT_ETHER: /* these types use struct arpcom */
2331 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2332 bcopy(lladdr, LLADDR(sdl), len);
2338 * If the interface is already up, we need
2339 * to re-init it in order to reprogram its
2342 ifnet_serialize_all(ifp);
2343 if ((ifp->if_flags & IFF_UP) != 0) {
2345 struct ifaddr_container *ifac;
2348 ifp->if_flags &= ~IFF_UP;
2349 ifr.ifr_flags = ifp->if_flags;
2350 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2351 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2353 ifp->if_flags |= IFF_UP;
2354 ifr.ifr_flags = ifp->if_flags;
2355 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2356 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2360 * Also send gratuitous ARPs to notify other nodes about
2361 * the address change.
2363 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2364 struct ifaddr *ifa = ifac->ifa;
2366 if (ifa->ifa_addr != NULL &&
2367 ifa->ifa_addr->sa_family == AF_INET)
2368 arp_gratuitous(ifp, ifa);
2372 ifnet_deserialize_all(ifp);
2376 struct ifmultiaddr *
2377 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2379 struct ifmultiaddr *ifma;
2381 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2382 if (sa_equal(ifma->ifma_addr, sa))
2389 * This function locates the first real ethernet MAC from a network
2390 * card and loads it into node, returning 0 on success or ENOENT if
2391 * no suitable interfaces were found. It is used by the uuid code to
2392 * generate a unique 6-byte number.
2395 if_getanyethermac(uint16_t *node, int minlen)
2398 struct sockaddr_dl *sdl;
2400 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2401 if (ifp->if_type != IFT_ETHER)
2403 sdl = IF_LLSOCKADDR(ifp);
2404 if (sdl->sdl_alen < minlen)
2406 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2414 * The name argument must be a pointer to storage which will last as
2415 * long as the interface does. For physical devices, the result of
2416 * device_get_name(dev) is a good choice and for pseudo-devices a
2417 * static string works well.
2420 if_initname(struct ifnet *ifp, const char *name, int unit)
2422 ifp->if_dname = name;
2423 ifp->if_dunit = unit;
2424 if (unit != IF_DUNIT_NONE)
2425 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2427 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2431 if_printf(struct ifnet *ifp, const char *fmt, ...)
2436 retval = kprintf("%s: ", ifp->if_xname);
2437 __va_start(ap, fmt);
2438 retval += kvprintf(fmt, ap);
2444 if_alloc(uint8_t type)
2450 * XXX temporary hack until arpcom is setup in if_l2com
2452 if (type == IFT_ETHER)
2453 size = sizeof(struct arpcom);
2455 size = sizeof(struct ifnet);
2457 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2459 ifp->if_type = type;
2461 if (if_com_alloc[type] != NULL) {
2462 ifp->if_l2com = if_com_alloc[type](type, ifp);
2463 if (ifp->if_l2com == NULL) {
2464 kfree(ifp, M_IFNET);
2472 if_free(struct ifnet *ifp)
2474 kfree(ifp, M_IFNET);
2478 ifq_set_classic(struct ifaltq *ifq)
2480 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2481 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2485 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2486 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2490 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2491 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2492 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2493 KASSERT(request != NULL, ("request is not specified"));
2495 ifq->altq_mapsubq = mapsubq;
2496 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2497 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2499 ifsq->ifsq_enqueue = enqueue;
2500 ifsq->ifsq_dequeue = dequeue;
2501 ifsq->ifsq_request = request;
2506 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2507 struct altq_pktattr *pa __unused)
2509 if (IF_QFULL(ifsq)) {
2513 IF_ENQUEUE(ifsq, m);
2519 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, struct mbuf *mpolled, int op)
2528 IF_DEQUEUE(ifsq, m);
2531 panic("unsupported ALTQ dequeue op: %d", op);
2533 KKASSERT(mpolled == NULL || mpolled == m);
2538 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2545 panic("unsupported ALTQ request: %d", req);
2551 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2553 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2554 int running = 0, need_sched;
2557 * Try to do direct ifnet.if_start on the subqueue first, if there is
2558 * contention on the subqueue hardware serializer, ifnet.if_start on
2559 * the subqueue will be scheduled on the subqueue owner CPU.
2561 if (!ifsq_tryserialize_hw(ifsq)) {
2563 * Subqueue hardware serializer contention happened,
2564 * ifnet.if_start on the subqueue is scheduled on
2565 * the subqueue owner CPU, and we keep going.
2567 ifsq_ifstart_schedule(ifsq, 1);
2571 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2572 ifp->if_start(ifp, ifsq);
2573 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2576 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2578 ifsq_deserialize_hw(ifsq);
2582 * More data need to be transmitted, ifnet.if_start on the
2583 * subqueue is scheduled on the subqueue owner CPU, and we
2585 * NOTE: ifnet.if_start subqueue interlock is not released.
2587 ifsq_ifstart_schedule(ifsq, force_sched);
2592 * Subqeue packets staging mechanism:
2594 * The packets enqueued into the subqueue are staged to a certain amount
2595 * before the ifnet.if_start on the subqueue is called. In this way, the
2596 * driver could avoid writing to hardware registers upon every packet,
2597 * instead, hardware registers could be written when certain amount of
2598 * packets are put onto hardware TX ring. The measurement on several modern
2599 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2600 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2601 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2602 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2603 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2605 * Subqueue packets staging is performed for two entry points into drivers'
2606 * transmission function:
2607 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2608 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2610 * Subqueue packets staging will be stopped upon any of the following
2612 * - If the count of packets enqueued on the current CPU is great than or
2613 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2614 * - If the total length of packets enqueued on the current CPU is great
2615 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2616 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2617 * is usually less than hardware's MTU.
2618 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2619 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2621 * - The if_start_rollup(), which is registered as low priority netisr
2622 * rollup function, is called; probably because no more work is pending
2626 * Currently subqueue packet staging is only performed in netisr threads.
2629 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2631 struct ifaltq *ifq = &ifp->if_snd;
2632 struct ifaltq_subque *ifsq;
2633 int error, start = 0, len, mcast = 0, avoid_start = 0;
2634 struct ifsubq_stage_head *head = NULL;
2635 struct ifsubq_stage *stage = NULL;
2637 ifsq = ifq_map_subq(ifq, mycpuid);
2638 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2640 len = m->m_pkthdr.len;
2641 if (m->m_flags & M_MCAST)
2644 if (curthread->td_type == TD_TYPE_NETISR) {
2645 head = &ifsubq_stage_heads[mycpuid];
2646 stage = ifsq_get_stage(ifsq, mycpuid);
2649 stage->stg_len += len;
2650 if (stage->stg_cnt < ifsq_stage_cntmax &&
2651 stage->stg_len < (ifp->if_mtu - max_protohdr))
2656 error = ifsq_enqueue_locked(ifsq, m, pa);
2658 if (!ifsq_data_ready(ifsq)) {
2659 ALTQ_SQ_UNLOCK(ifsq);
2664 if (!ifsq_is_started(ifsq)) {
2666 ALTQ_SQ_UNLOCK(ifsq);
2669 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2670 ifsq_stage_insert(head, stage);
2672 IFNET_STAT_INC(ifp, obytes, len);
2674 IFNET_STAT_INC(ifp, omcasts, 1);
2679 * Hold the subqueue interlock of ifnet.if_start
2681 ifsq_set_started(ifsq);
2684 ALTQ_SQ_UNLOCK(ifsq);
2687 IFNET_STAT_INC(ifp, obytes, len);
2689 IFNET_STAT_INC(ifp, omcasts, 1);
2692 if (stage != NULL) {
2693 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2694 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
2696 ifsq_stage_remove(head, stage);
2697 ifsq_ifstart_schedule(ifsq, 1);
2702 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2703 ifsq_stage_remove(head, stage);
2713 ifsq_ifstart_try(ifsq, 0);
2718 ifa_create(int size, int flags)
2723 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2725 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2729 ifa->ifa_containers =
2730 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
2731 M_IFADDR, M_WAITOK | M_ZERO);
2732 ifa->ifa_ncnt = ncpus;
2733 for (i = 0; i < ncpus; ++i) {
2734 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2736 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2738 ifac->ifa_refcnt = 1;
2741 kprintf("alloc ifa %p %d\n", ifa, size);
2747 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2749 struct ifaddr *ifa = ifac->ifa;
2751 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2752 KKASSERT(ifac->ifa_refcnt == 0);
2753 KASSERT(ifac->ifa_listmask == 0,
2754 ("ifa is still on %#x lists", ifac->ifa_listmask));
2756 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2758 #ifdef IFADDR_DEBUG_VERBOSE
2759 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2762 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2763 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2764 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2766 kprintf("free ifa %p\n", ifa);
2768 kfree(ifa->ifa_containers, M_IFADDR);
2769 kfree(ifa, M_IFADDR);
2774 ifa_iflink_dispatch(netmsg_t nmsg)
2776 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2777 struct ifaddr *ifa = msg->ifa;
2778 struct ifnet *ifp = msg->ifp;
2780 struct ifaddr_container *ifac;
2784 ifac = &ifa->ifa_containers[cpu];
2785 ASSERT_IFAC_VALID(ifac);
2786 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2787 ("ifaddr is on if_addrheads"));
2789 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2791 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2793 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2797 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2801 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2803 struct netmsg_ifaddr msg;
2805 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2806 0, ifa_iflink_dispatch);
2811 ifa_domsg(&msg.base.lmsg, 0);
2815 ifa_ifunlink_dispatch(netmsg_t nmsg)
2817 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2818 struct ifaddr *ifa = msg->ifa;
2819 struct ifnet *ifp = msg->ifp;
2821 struct ifaddr_container *ifac;
2825 ifac = &ifa->ifa_containers[cpu];
2826 ASSERT_IFAC_VALID(ifac);
2827 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2828 ("ifaddr is not on if_addrhead"));
2830 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2831 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2835 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2839 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2841 struct netmsg_ifaddr msg;
2843 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2844 0, ifa_ifunlink_dispatch);
2848 ifa_domsg(&msg.base.lmsg, 0);
2852 ifa_destroy_dispatch(netmsg_t nmsg)
2854 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2857 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2861 ifa_destroy(struct ifaddr *ifa)
2863 struct netmsg_ifaddr msg;
2865 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2866 0, ifa_destroy_dispatch);
2869 ifa_domsg(&msg.base.lmsg, 0);
2873 ifnet_portfn(int cpu)
2875 return &ifnet_threads[cpu].td_msgport;
2879 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2881 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2883 if (next_cpu < ncpus)
2884 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2886 lwkt_replymsg(lmsg, 0);
2890 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2892 KKASSERT(cpu < ncpus);
2893 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2897 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2899 KKASSERT(cpu < ncpus);
2900 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2904 * Generic netmsg service loop. Some protocols may roll their own but all
2905 * must do the basic command dispatch function call done here.
2908 ifnet_service_loop(void *arg __unused)
2912 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2913 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2914 msg->base.nm_dispatch(msg);
2919 if_start_rollup(void)
2921 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
2922 struct ifsubq_stage *stage;
2924 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
2925 struct ifaltq_subque *ifsq = stage->stg_subq;
2928 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
2930 ifsq_stage_remove(head, stage);
2933 ifsq_ifstart_schedule(ifsq, 1);
2938 if (!ifsq_is_started(ifsq)) {
2940 * Hold the subqueue interlock of
2943 ifsq_set_started(ifsq);
2946 ALTQ_SQ_UNLOCK(ifsq);
2949 ifsq_ifstart_try(ifsq, 1);
2951 KKASSERT((stage->stg_flags &
2952 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
2957 ifnetinit(void *dummy __unused)
2961 for (i = 0; i < ncpus; ++i) {
2962 struct thread *thr = &ifnet_threads[i];
2964 lwkt_create(ifnet_service_loop, NULL, NULL,
2965 thr, TDF_NOSTART|TDF_FORCE_SPINPORT,
2967 netmsg_service_port_init(&thr->td_msgport);
2971 for (i = 0; i < ncpus; ++i)
2972 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
2973 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
2977 ifnet_byindex(unsigned short idx)
2981 return ifindex2ifnet[idx];
2985 ifaddr_byindex(unsigned short idx)
2989 ifp = ifnet_byindex(idx);
2992 return TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2996 if_register_com_alloc(u_char type,
2997 if_com_alloc_t *a, if_com_free_t *f)
3000 KASSERT(if_com_alloc[type] == NULL,
3001 ("if_register_com_alloc: %d already registered", type));
3002 KASSERT(if_com_free[type] == NULL,
3003 ("if_register_com_alloc: %d free already registered", type));
3005 if_com_alloc[type] = a;
3006 if_com_free[type] = f;
3010 if_deregister_com_alloc(u_char type)
3013 KASSERT(if_com_alloc[type] != NULL,
3014 ("if_deregister_com_alloc: %d not registered", type));
3015 KASSERT(if_com_free[type] != NULL,
3016 ("if_deregister_com_alloc: %d free not registered", type));
3017 if_com_alloc[type] = NULL;
3018 if_com_free[type] = NULL;
3022 if_ring_count2(int cnt, int cnt_max)
3026 KASSERT(cnt_max >= 1 && powerof2(cnt_max),
3027 ("invalid ring count max %d", cnt_max));
3036 while ((1 << (shift + 1)) <= cnt)
3040 KASSERT(cnt >= 1 && cnt <= ncpus2 && cnt <= cnt_max,
3041 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3042 cnt, ncpus2, cnt_max));
3047 ifq_set_maxlen(struct ifaltq *ifq, int len)
3049 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3053 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3055 return ALTQ_SUBQ_INDEX_DEFAULT;
3059 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3061 return (cpuid & ifq->altq_subq_mask);
3065 ifsq_watchdog(void *arg)
3067 struct ifsubq_watchdog *wd = arg;
3070 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3073 ifp = ifsq_get_ifp(wd->wd_subq);
3074 if (ifnet_tryserialize_all(ifp)) {
3075 wd->wd_watchdog(wd->wd_subq);
3076 ifnet_deserialize_all(ifp);
3078 /* try again next timeout */
3082 ifsq_watchdog_reset(wd);
3086 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3088 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3089 ifsq_get_cpuid(wd->wd_subq));
3093 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3094 ifsq_watchdog_t watchdog)
3096 callout_init_mp(&wd->wd_callout);
3099 wd->wd_watchdog = watchdog;
3103 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3106 ifsq_watchdog_reset(wd);
3110 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3113 callout_stop(&wd->wd_callout);