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/netisr2.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 static int if_stats_compat = 0;
142 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
143 &if_stats_compat, 0, "Compat the old ifnet stats");
145 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL)
146 /* Must be after netisr_init */
147 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_SECOND, ifnetinit, NULL)
149 static if_com_alloc_t *if_com_alloc[256];
150 static if_com_free_t *if_com_free[256];
152 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
153 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
154 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
156 int ifqmaxlen = IFQ_MAXLEN;
157 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
159 struct callout if_slowtimo_timer;
162 struct ifnet **ifindex2ifnet = NULL;
163 static struct thread ifnet_threads[MAXCPU];
165 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
168 #define IFQ_KTR_STRING "ifq=%p"
169 #define IFQ_KTR_ARGS struct ifaltq *ifq
171 #define KTR_IFQ KTR_ALL
173 KTR_INFO_MASTER(ifq);
174 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
175 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
176 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
178 #define IF_START_KTR_STRING "ifp=%p"
179 #define IF_START_KTR_ARGS struct ifnet *ifp
181 #define KTR_IF_START KTR_ALL
183 KTR_INFO_MASTER(if_start);
184 KTR_INFO(KTR_IF_START, if_start, run, 0,
185 IF_START_KTR_STRING, IF_START_KTR_ARGS);
186 KTR_INFO(KTR_IF_START, if_start, sched, 1,
187 IF_START_KTR_STRING, IF_START_KTR_ARGS);
188 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
189 IF_START_KTR_STRING, IF_START_KTR_ARGS);
190 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
191 IF_START_KTR_STRING, IF_START_KTR_ARGS);
192 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
193 IF_START_KTR_STRING, IF_START_KTR_ARGS);
194 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
197 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
200 * Network interface utility routines.
202 * Routines with ifa_ifwith* names take sockaddr *'s as
211 callout_init(&if_slowtimo_timer);
214 TAILQ_FOREACH(ifp, &ifnet, if_link) {
215 if (ifp->if_snd.altq_maxlen == 0) {
216 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n");
217 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
226 ifsq_ifstart_ipifunc(void *arg)
228 struct ifaltq_subque *ifsq = arg;
229 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
232 if (lmsg->ms_flags & MSGF_DONE)
233 lwkt_sendmsg(netisr_cpuport(mycpuid), lmsg);
238 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
240 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
241 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
242 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
248 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
250 KKASSERT((stage->stg_flags &
251 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
252 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
253 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
257 * Schedule ifnet.if_start on ifnet's CPU
260 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
264 if (!force && curthread->td_type == TD_TYPE_NETISR &&
265 ifsq_stage_cntmax > 0) {
266 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
270 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
271 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
272 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
276 cpu = ifsq_get_cpuid(ifsq);
278 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
280 ifsq_ifstart_ipifunc(ifsq);
285 * This function will release ifnet.if_start interlock,
286 * if ifnet.if_start does not need to be scheduled
289 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
291 if (!running || ifsq_is_empty(ifsq)
293 || ifsq->ifsq_altq->altq_tbr != NULL
298 * ifnet.if_start interlock is released, if:
299 * 1) Hardware can not take any packets, due to
300 * o interface is marked down
301 * o hardware queue is full (ifq_is_oactive)
302 * Under the second situation, hardware interrupt
303 * or polling(4) will call/schedule ifnet.if_start
304 * when hardware queue is ready
305 * 2) There is not packet in the ifnet.if_snd.
306 * Further ifq_dispatch or ifq_handoff will call/
307 * schedule ifnet.if_start
308 * 3) TBR is used and it does not allow further
310 * TBR callout will call ifnet.if_start
312 if (!running || !ifsq_data_ready(ifsq)) {
313 ifsq_clr_started(ifsq);
314 ALTQ_SQ_UNLOCK(ifsq);
317 ALTQ_SQ_UNLOCK(ifsq);
323 ifsq_ifstart_dispatch(netmsg_t msg)
325 struct lwkt_msg *lmsg = &msg->base.lmsg;
326 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
327 struct ifnet *ifp = ifsq_get_ifp(ifsq);
328 int running = 0, need_sched;
331 lwkt_replymsg(lmsg, 0); /* reply ASAP */
334 if (mycpuid != ifsq_get_cpuid(ifsq)) {
336 * We need to chase the ifnet CPU change.
338 ifsq_ifstart_schedule(ifsq, 1);
342 ifsq_serialize_hw(ifsq);
343 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
344 ifp->if_start(ifp, ifsq);
345 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
348 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
349 ifsq_deserialize_hw(ifsq);
353 * More data need to be transmitted, ifnet.if_start is
354 * scheduled on ifnet's CPU, and we keep going.
355 * NOTE: ifnet.if_start interlock is not released.
357 ifsq_ifstart_schedule(ifsq, 0);
361 /* Device driver ifnet.if_start helper function */
363 ifsq_devstart(struct ifaltq_subque *ifsq)
365 struct ifnet *ifp = ifsq_get_ifp(ifsq);
368 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
371 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
372 ALTQ_SQ_UNLOCK(ifsq);
375 ifsq_set_started(ifsq);
376 ALTQ_SQ_UNLOCK(ifsq);
378 ifp->if_start(ifp, ifsq);
380 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
383 if (ifsq_ifstart_need_schedule(ifsq, running)) {
385 * More data need to be transmitted, ifnet.if_start is
386 * scheduled on ifnet's CPU, and we keep going.
387 * NOTE: ifnet.if_start interlock is not released.
389 ifsq_ifstart_schedule(ifsq, 0);
394 if_devstart(struct ifnet *ifp)
396 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
399 /* Device driver ifnet.if_start schedule helper function */
401 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
403 ifsq_ifstart_schedule(ifsq, 1);
407 if_devstart_sched(struct ifnet *ifp)
409 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
413 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
415 lwkt_serialize_enter(ifp->if_serializer);
419 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
421 lwkt_serialize_exit(ifp->if_serializer);
425 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
427 return lwkt_serialize_try(ifp->if_serializer);
432 if_default_serialize_assert(struct ifnet *ifp,
433 enum ifnet_serialize slz __unused,
434 boolean_t serialized)
437 ASSERT_SERIALIZED(ifp->if_serializer);
439 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
444 * Attach an interface to the list of "active" interfaces.
446 * The serializer is optional. If non-NULL access to the interface
450 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
452 unsigned socksize, ifasize;
453 int namelen, masklen;
454 struct sockaddr_dl *sdl;
459 static int if_indexlim = 8;
461 if (ifp->if_serialize != NULL) {
462 KASSERT(ifp->if_deserialize != NULL &&
463 ifp->if_tryserialize != NULL &&
464 ifp->if_serialize_assert != NULL,
465 ("serialize functions are partially setup"));
468 * If the device supplies serialize functions,
469 * then clear if_serializer to catch any invalid
470 * usage of this field.
472 KASSERT(serializer == NULL,
473 ("both serialize functions and default serializer "
475 ifp->if_serializer = NULL;
477 KASSERT(ifp->if_deserialize == NULL &&
478 ifp->if_tryserialize == NULL &&
479 ifp->if_serialize_assert == NULL,
480 ("serialize functions are partially setup"));
481 ifp->if_serialize = if_default_serialize;
482 ifp->if_deserialize = if_default_deserialize;
483 ifp->if_tryserialize = if_default_tryserialize;
485 ifp->if_serialize_assert = if_default_serialize_assert;
489 * The serializer can be passed in from the device,
490 * allowing the same serializer to be used for both
491 * the interrupt interlock and the device queue.
492 * If not specified, the netif structure will use an
493 * embedded serializer.
495 if (serializer == NULL) {
496 serializer = &ifp->if_default_serializer;
497 lwkt_serialize_init(serializer);
499 ifp->if_serializer = serializer;
502 mtx_init(&ifp->if_ioctl_mtx);
503 mtx_lock(&ifp->if_ioctl_mtx);
505 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
506 ifp->if_index = ++if_index;
510 * The old code would work if the interface passed a pre-existing
511 * chain of ifaddrs to this code. We don't trust our callers to
512 * properly initialize the tailq, however, so we no longer allow
513 * this unlikely case.
515 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
516 M_IFADDR, M_WAITOK | M_ZERO);
517 for (i = 0; i < ncpus; ++i)
518 TAILQ_INIT(&ifp->if_addrheads[i]);
520 TAILQ_INIT(&ifp->if_prefixhead);
521 TAILQ_INIT(&ifp->if_multiaddrs);
522 TAILQ_INIT(&ifp->if_groups);
523 getmicrotime(&ifp->if_lastchange);
524 if (ifindex2ifnet == NULL || if_index >= if_indexlim) {
530 /* grow ifindex2ifnet */
531 n = if_indexlim * sizeof(*q);
532 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
534 bcopy(ifindex2ifnet, q, n/2);
535 kfree(ifindex2ifnet, M_IFADDR);
540 ifindex2ifnet[if_index] = ifp;
543 * create a Link Level name for this device
545 namelen = strlen(ifp->if_xname);
546 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
547 socksize = masklen + ifp->if_addrlen;
548 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
549 if (socksize < sizeof(*sdl))
550 socksize = sizeof(*sdl);
551 socksize = ROUNDUP(socksize);
553 ifasize = sizeof(struct ifaddr) + 2 * socksize;
554 ifa = ifa_create(ifasize, M_WAITOK);
555 sdl = (struct sockaddr_dl *)(ifa + 1);
556 sdl->sdl_len = socksize;
557 sdl->sdl_family = AF_LINK;
558 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
559 sdl->sdl_nlen = namelen;
560 sdl->sdl_index = ifp->if_index;
561 sdl->sdl_type = ifp->if_type;
562 ifp->if_lladdr = ifa;
564 ifa->ifa_rtrequest = link_rtrequest;
565 ifa->ifa_addr = (struct sockaddr *)sdl;
566 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
567 ifa->ifa_netmask = (struct sockaddr *)sdl;
568 sdl->sdl_len = masklen;
570 sdl->sdl_data[--namelen] = 0xff;
571 ifa_iflink(ifa, ifp, 0 /* Insert head */);
573 ifp->if_data_pcpu = kmalloc_cachealign(
574 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
576 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
577 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
579 if (ifp->if_mapsubq == NULL)
580 ifp->if_mapsubq = ifq_mapsubq_default;
584 ifq->altq_disc = NULL;
585 ifq->altq_flags &= ALTQF_CANTCHANGE;
586 ifq->altq_tbr = NULL;
589 if (ifq->altq_subq_cnt <= 0)
590 ifq->altq_subq_cnt = 1;
591 ifq->altq_subq = kmalloc_cachealign(
592 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
593 M_DEVBUF, M_WAITOK | M_ZERO);
595 if (ifq->altq_maxlen == 0) {
596 if_printf(ifp, "driver didn't set ifq_maxlen\n");
597 ifq_set_maxlen(ifq, ifqmaxlen);
600 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
601 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
603 ALTQ_SQ_LOCK_INIT(ifsq);
604 ifsq->ifsq_index = q;
606 ifsq->ifsq_altq = ifq;
607 ifsq->ifsq_ifp = ifp;
609 ifsq->ifq_maxlen = ifq->altq_maxlen;
610 ifsq->ifsq_prepended = NULL;
611 ifsq->ifsq_started = 0;
612 ifsq->ifsq_hw_oactive = 0;
613 ifsq_set_cpuid(ifsq, 0);
614 if (ifp->if_serializer != NULL)
615 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
618 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
619 M_DEVBUF, M_WAITOK | M_ZERO);
620 for (i = 0; i < ncpus; ++i)
621 ifsq->ifsq_stage[i].stg_subq = ifsq;
623 ifsq->ifsq_ifstart_nmsg =
624 kmalloc(ncpus * sizeof(struct netmsg_base),
625 M_LWKTMSG, M_WAITOK);
626 for (i = 0; i < ncpus; ++i) {
627 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
628 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
629 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
632 ifq_set_classic(ifq);
634 if (!SLIST_EMPTY(&domains))
635 if_attachdomain1(ifp);
637 /* Announce the interface. */
638 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
640 mtx_unlock(&ifp->if_ioctl_mtx);
644 if_attachdomain(void *dummy)
649 TAILQ_FOREACH(ifp, &ifnet, if_list)
650 if_attachdomain1(ifp);
653 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
654 if_attachdomain, NULL);
657 if_attachdomain1(struct ifnet *ifp)
663 /* address family dependent data region */
664 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
665 SLIST_FOREACH(dp, &domains, dom_next)
666 if (dp->dom_ifattach)
667 ifp->if_afdata[dp->dom_family] =
668 (*dp->dom_ifattach)(ifp);
673 * Purge all addresses whose type is _not_ AF_LINK
676 if_purgeaddrs_nolink(struct ifnet *ifp)
678 struct ifaddr_container *ifac, *next;
680 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
682 struct ifaddr *ifa = ifac->ifa;
684 /* Leave link ifaddr as it is */
685 if (ifa->ifa_addr->sa_family == AF_LINK)
688 /* XXX: Ugly!! ad hoc just for INET */
689 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
690 struct ifaliasreq ifr;
691 #ifdef IFADDR_DEBUG_VERBOSE
694 kprintf("purge in4 addr %p: ", ifa);
695 for (i = 0; i < ncpus; ++i)
696 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
700 bzero(&ifr, sizeof ifr);
701 ifr.ifra_addr = *ifa->ifa_addr;
702 if (ifa->ifa_dstaddr)
703 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
704 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
710 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
711 #ifdef IFADDR_DEBUG_VERBOSE
714 kprintf("purge in6 addr %p: ", ifa);
715 for (i = 0; i < ncpus; ++i)
716 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
721 /* ifp_addrhead is already updated */
725 ifa_ifunlink(ifa, ifp);
731 ifq_stage_detach_handler(netmsg_t nmsg)
733 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
736 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
737 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
738 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
740 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
741 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
743 lwkt_replymsg(&nmsg->lmsg, 0);
747 ifq_stage_detach(struct ifaltq *ifq)
749 struct netmsg_base base;
752 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
753 ifq_stage_detach_handler);
754 base.lmsg.u.ms_resultp = ifq;
756 for (cpu = 0; cpu < ncpus; ++cpu)
757 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
761 * Detach an interface, removing it from the
762 * list of "active" interfaces.
765 if_detach(struct ifnet *ifp)
767 struct radix_node_head *rnh;
772 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
775 * Remove routes and flush queues.
779 if (ifp->if_flags & IFF_NPOLLING)
780 ifpoll_deregister(ifp);
785 if (ifq_is_enabled(&ifp->if_snd))
786 altq_disable(&ifp->if_snd);
787 if (ifq_is_attached(&ifp->if_snd))
788 altq_detach(&ifp->if_snd);
792 * Clean up all addresses.
794 ifp->if_lladdr = NULL;
796 if_purgeaddrs_nolink(ifp);
797 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
800 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
801 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
802 ("non-link ifaddr is left on if_addrheads"));
804 ifa_ifunlink(ifa, ifp);
806 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
807 ("there are still ifaddrs left on if_addrheads"));
812 * Remove all IPv4 kernel structures related to ifp.
819 * Remove all IPv6 kernel structs related to ifp. This should be done
820 * before removing routing entries below, since IPv6 interface direct
821 * routes are expected to be removed by the IPv6-specific kernel API.
822 * Otherwise, the kernel will detect some inconsistency and bark it.
828 * Delete all remaining routes using this interface
829 * Unfortuneatly the only way to do this is to slog through
830 * the entire routing table looking for routes which point
831 * to this interface...oh well...
834 for (cpu = 0; cpu < ncpus; cpu++) {
835 lwkt_migratecpu(cpu);
836 for (i = 1; i <= AF_MAX; i++) {
837 if ((rnh = rt_tables[cpu][i]) == NULL)
839 rnh->rnh_walktree(rnh, if_rtdel, ifp);
842 lwkt_migratecpu(origcpu);
844 /* Announce that the interface is gone. */
845 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
846 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
848 SLIST_FOREACH(dp, &domains, dom_next)
849 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
850 (*dp->dom_ifdetach)(ifp,
851 ifp->if_afdata[dp->dom_family]);
854 * Remove interface from ifindex2ifp[] and maybe decrement if_index.
856 ifindex2ifnet[ifp->if_index] = NULL;
857 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
860 TAILQ_REMOVE(&ifnet, ifp, if_link);
861 kfree(ifp->if_addrheads, M_IFADDR);
863 lwkt_synchronize_ipiqs("if_detach");
864 ifq_stage_detach(&ifp->if_snd);
866 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
867 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
869 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
870 kfree(ifsq->ifsq_stage, M_DEVBUF);
872 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
874 kfree(ifp->if_data_pcpu, M_DEVBUF);
880 * Create interface group without members
883 if_creategroup(const char *groupname)
885 struct ifg_group *ifg = NULL;
887 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
888 M_TEMP, M_NOWAIT)) == NULL)
891 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
893 ifg->ifg_carp_demoted = 0;
894 TAILQ_INIT(&ifg->ifg_members);
896 pfi_attach_ifgroup(ifg);
898 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
904 * Add a group to an interface
907 if_addgroup(struct ifnet *ifp, const char *groupname)
909 struct ifg_list *ifgl;
910 struct ifg_group *ifg = NULL;
911 struct ifg_member *ifgm;
913 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
914 groupname[strlen(groupname) - 1] <= '9')
917 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
918 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
921 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
924 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
929 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
930 if (!strcmp(ifg->ifg_group, groupname))
933 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
940 ifgl->ifgl_group = ifg;
941 ifgm->ifgm_ifp = ifp;
943 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
944 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
947 pfi_group_change(groupname);
954 * Remove a group from an interface
957 if_delgroup(struct ifnet *ifp, const char *groupname)
959 struct ifg_list *ifgl;
960 struct ifg_member *ifgm;
962 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
963 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
968 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
970 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
971 if (ifgm->ifgm_ifp == ifp)
975 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
979 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
980 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
982 pfi_detach_ifgroup(ifgl->ifgl_group);
984 kfree(ifgl->ifgl_group, M_TEMP);
990 pfi_group_change(groupname);
997 * Stores all groups from an interface in memory pointed
1001 if_getgroup(caddr_t data, struct ifnet *ifp)
1004 struct ifg_list *ifgl;
1005 struct ifg_req ifgrq, *ifgp;
1006 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1008 if (ifgr->ifgr_len == 0) {
1009 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1010 ifgr->ifgr_len += sizeof(struct ifg_req);
1014 len = ifgr->ifgr_len;
1015 ifgp = ifgr->ifgr_groups;
1016 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1017 if (len < sizeof(ifgrq))
1019 bzero(&ifgrq, sizeof ifgrq);
1020 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1021 sizeof(ifgrq.ifgrq_group));
1022 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1023 sizeof(struct ifg_req))))
1025 len -= sizeof(ifgrq);
1033 * Stores all members of a group in memory pointed to by data
1036 if_getgroupmembers(caddr_t data)
1038 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1039 struct ifg_group *ifg;
1040 struct ifg_member *ifgm;
1041 struct ifg_req ifgrq, *ifgp;
1044 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1045 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1050 if (ifgr->ifgr_len == 0) {
1051 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1052 ifgr->ifgr_len += sizeof(ifgrq);
1056 len = ifgr->ifgr_len;
1057 ifgp = ifgr->ifgr_groups;
1058 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1059 if (len < sizeof(ifgrq))
1061 bzero(&ifgrq, sizeof ifgrq);
1062 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1063 sizeof(ifgrq.ifgrq_member));
1064 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1065 sizeof(struct ifg_req))))
1067 len -= sizeof(ifgrq);
1075 * Delete Routes for a Network Interface
1077 * Called for each routing entry via the rnh->rnh_walktree() call above
1078 * to delete all route entries referencing a detaching network interface.
1081 * rn pointer to node in the routing table
1082 * arg argument passed to rnh->rnh_walktree() - detaching interface
1086 * errno failed - reason indicated
1090 if_rtdel(struct radix_node *rn, void *arg)
1092 struct rtentry *rt = (struct rtentry *)rn;
1093 struct ifnet *ifp = arg;
1096 if (rt->rt_ifp == ifp) {
1099 * Protect (sorta) against walktree recursion problems
1100 * with cloned routes
1102 if (!(rt->rt_flags & RTF_UP))
1105 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1106 rt_mask(rt), rt->rt_flags,
1109 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1117 * Locate an interface based on a complete address.
1120 ifa_ifwithaddr(struct sockaddr *addr)
1124 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1125 struct ifaddr_container *ifac;
1127 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1128 struct ifaddr *ifa = ifac->ifa;
1130 if (ifa->ifa_addr->sa_family != addr->sa_family)
1132 if (sa_equal(addr, ifa->ifa_addr))
1134 if ((ifp->if_flags & IFF_BROADCAST) &&
1135 ifa->ifa_broadaddr &&
1136 /* IPv6 doesn't have broadcast */
1137 ifa->ifa_broadaddr->sa_len != 0 &&
1138 sa_equal(ifa->ifa_broadaddr, addr))
1145 * Locate the point to point interface with a given destination address.
1148 ifa_ifwithdstaddr(struct sockaddr *addr)
1152 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1153 struct ifaddr_container *ifac;
1155 if (!(ifp->if_flags & IFF_POINTOPOINT))
1158 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1159 struct ifaddr *ifa = ifac->ifa;
1161 if (ifa->ifa_addr->sa_family != addr->sa_family)
1163 if (ifa->ifa_dstaddr &&
1164 sa_equal(addr, ifa->ifa_dstaddr))
1172 * Find an interface on a specific network. If many, choice
1173 * is most specific found.
1176 ifa_ifwithnet(struct sockaddr *addr)
1179 struct ifaddr *ifa_maybe = NULL;
1180 u_int af = addr->sa_family;
1181 char *addr_data = addr->sa_data, *cplim;
1184 * AF_LINK addresses can be looked up directly by their index number,
1185 * so do that if we can.
1187 if (af == AF_LINK) {
1188 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1190 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1191 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1195 * Scan though each interface, looking for ones that have
1196 * addresses in this address family.
1198 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1199 struct ifaddr_container *ifac;
1201 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1202 struct ifaddr *ifa = ifac->ifa;
1203 char *cp, *cp2, *cp3;
1205 if (ifa->ifa_addr->sa_family != af)
1207 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1209 * This is a bit broken as it doesn't
1210 * take into account that the remote end may
1211 * be a single node in the network we are
1213 * The trouble is that we don't know the
1214 * netmask for the remote end.
1216 if (ifa->ifa_dstaddr != NULL &&
1217 sa_equal(addr, ifa->ifa_dstaddr))
1221 * if we have a special address handler,
1222 * then use it instead of the generic one.
1224 if (ifa->ifa_claim_addr) {
1225 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1233 * Scan all the bits in the ifa's address.
1234 * If a bit dissagrees with what we are
1235 * looking for, mask it with the netmask
1236 * to see if it really matters.
1237 * (A byte at a time)
1239 if (ifa->ifa_netmask == 0)
1242 cp2 = ifa->ifa_addr->sa_data;
1243 cp3 = ifa->ifa_netmask->sa_data;
1244 cplim = ifa->ifa_netmask->sa_len +
1245 (char *)ifa->ifa_netmask;
1247 if ((*cp++ ^ *cp2++) & *cp3++)
1248 goto next; /* next address! */
1250 * If the netmask of what we just found
1251 * is more specific than what we had before
1252 * (if we had one) then remember the new one
1253 * before continuing to search
1254 * for an even better one.
1256 if (ifa_maybe == NULL ||
1257 rn_refines((char *)ifa->ifa_netmask,
1258 (char *)ifa_maybe->ifa_netmask))
1267 * Find an interface address specific to an interface best matching
1271 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1273 struct ifaddr_container *ifac;
1274 char *cp, *cp2, *cp3;
1276 struct ifaddr *ifa_maybe = NULL;
1277 u_int af = addr->sa_family;
1281 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1282 struct ifaddr *ifa = ifac->ifa;
1284 if (ifa->ifa_addr->sa_family != af)
1286 if (ifa_maybe == NULL)
1288 if (ifa->ifa_netmask == NULL) {
1289 if (sa_equal(addr, ifa->ifa_addr) ||
1290 (ifa->ifa_dstaddr != NULL &&
1291 sa_equal(addr, ifa->ifa_dstaddr)))
1295 if (ifp->if_flags & IFF_POINTOPOINT) {
1296 if (sa_equal(addr, ifa->ifa_dstaddr))
1300 cp2 = ifa->ifa_addr->sa_data;
1301 cp3 = ifa->ifa_netmask->sa_data;
1302 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1303 for (; cp3 < cplim; cp3++)
1304 if ((*cp++ ^ *cp2++) & *cp3)
1314 * Default action when installing a route with a Link Level gateway.
1315 * Lookup an appropriate real ifa to point to.
1316 * This should be moved to /sys/net/link.c eventually.
1319 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info)
1322 struct sockaddr *dst;
1325 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1326 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1328 ifa = ifaof_ifpforaddr(dst, ifp);
1330 IFAFREE(rt->rt_ifa);
1333 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1334 ifa->ifa_rtrequest(cmd, rt, info);
1339 * Mark an interface down and notify protocols of
1341 * NOTE: must be called at splnet or eqivalent.
1344 if_unroute(struct ifnet *ifp, int flag, int fam)
1346 struct ifaddr_container *ifac;
1348 ifp->if_flags &= ~flag;
1349 getmicrotime(&ifp->if_lastchange);
1350 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1351 struct ifaddr *ifa = ifac->ifa;
1353 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1354 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1356 ifq_purge_all(&ifp->if_snd);
1361 * Mark an interface up and notify protocols of
1363 * NOTE: must be called at splnet or eqivalent.
1366 if_route(struct ifnet *ifp, int flag, int fam)
1368 struct ifaddr_container *ifac;
1370 ifq_purge_all(&ifp->if_snd);
1371 ifp->if_flags |= flag;
1372 getmicrotime(&ifp->if_lastchange);
1373 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1374 struct ifaddr *ifa = ifac->ifa;
1376 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1377 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1386 * Mark an interface down and notify protocols of the transition. An
1387 * interface going down is also considered to be a synchronizing event.
1388 * We must ensure that all packet processing related to the interface
1389 * has completed before we return so e.g. the caller can free the ifnet
1390 * structure that the mbufs may be referencing.
1392 * NOTE: must be called at splnet or eqivalent.
1395 if_down(struct ifnet *ifp)
1397 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1398 netmsg_service_sync();
1402 * Mark an interface up and notify protocols of
1404 * NOTE: must be called at splnet or eqivalent.
1407 if_up(struct ifnet *ifp)
1409 if_route(ifp, IFF_UP, AF_UNSPEC);
1413 * Process a link state change.
1414 * NOTE: must be called at splsoftnet or equivalent.
1417 if_link_state_change(struct ifnet *ifp)
1419 int link_state = ifp->if_link_state;
1422 devctl_notify("IFNET", ifp->if_xname,
1423 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1427 * Handle interface watchdog timer routines. Called
1428 * from softclock, we decrement timers (if set) and
1429 * call the appropriate interface routine on expiration.
1432 if_slowtimo(void *arg)
1438 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1439 if (if_stats_compat) {
1440 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1441 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1442 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1443 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1444 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1445 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1446 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1447 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1448 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1449 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1450 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1453 if (ifp->if_timer == 0 || --ifp->if_timer)
1455 if (ifp->if_watchdog) {
1456 if (ifnet_tryserialize_all(ifp)) {
1457 (*ifp->if_watchdog)(ifp);
1458 ifnet_deserialize_all(ifp);
1460 /* try again next timeout */
1468 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1472 * Map interface name to
1473 * interface structure pointer.
1476 ifunit(const char *name)
1481 * Search all the interfaces for this name/number
1484 TAILQ_FOREACH(ifp, &ifnet, if_link) {
1485 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1493 * Map interface name in a sockaddr_dl to
1494 * interface structure pointer.
1497 if_withname(struct sockaddr *sa)
1499 char ifname[IFNAMSIZ+1];
1500 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
1502 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
1503 (sdl->sdl_nlen > IFNAMSIZ) )
1507 * ifunit wants a null-terminated name. It may not be null-terminated
1508 * in the sockaddr. We don't want to change the caller's sockaddr,
1509 * and there might not be room to put the trailing null anyway, so we
1510 * make a local copy that we know we can null terminate safely.
1513 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
1514 ifname[sdl->sdl_nlen] = '\0';
1515 return ifunit(ifname);
1523 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1534 size_t namelen, onamelen;
1535 char new_name[IFNAMSIZ];
1537 struct sockaddr_dl *sdl;
1542 return (ifconf(cmd, data, cred));
1547 ifr = (struct ifreq *)data;
1552 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1554 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1555 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1557 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1559 return (if_clone_destroy(ifr->ifr_name));
1560 case SIOCIFGCLONERS:
1561 return (if_clone_list((struct if_clonereq *)data));
1567 * Nominal ioctl through interface, lookup the ifp and obtain a
1568 * lock to serialize the ifconfig ioctl operation.
1570 ifp = ifunit(ifr->ifr_name);
1574 mtx_lock(&ifp->if_ioctl_mtx);
1578 ifr->ifr_index = ifp->if_index;
1582 ifr->ifr_flags = ifp->if_flags;
1583 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1587 ifr->ifr_reqcap = ifp->if_capabilities;
1588 ifr->ifr_curcap = ifp->if_capenable;
1592 ifr->ifr_metric = ifp->if_metric;
1596 ifr->ifr_mtu = ifp->if_mtu;
1600 ifr->ifr_tsolen = ifp->if_tsolen;
1604 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1605 sizeof(ifp->if_data));
1609 ifr->ifr_phys = ifp->if_physical;
1612 case SIOCGIFPOLLCPU:
1613 ifr->ifr_pollcpu = -1;
1616 case SIOCSIFPOLLCPU:
1620 error = priv_check_cred(cred, PRIV_ROOT, 0);
1623 new_flags = (ifr->ifr_flags & 0xffff) |
1624 (ifr->ifr_flagshigh << 16);
1625 if (ifp->if_flags & IFF_SMART) {
1626 /* Smart drivers twiddle their own routes */
1627 } else if (ifp->if_flags & IFF_UP &&
1628 (new_flags & IFF_UP) == 0) {
1632 } else if (new_flags & IFF_UP &&
1633 (ifp->if_flags & IFF_UP) == 0) {
1639 #ifdef IFPOLL_ENABLE
1640 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1641 if (new_flags & IFF_NPOLLING)
1642 ifpoll_register(ifp);
1644 ifpoll_deregister(ifp);
1648 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1649 (new_flags &~ IFF_CANTCHANGE);
1650 if (new_flags & IFF_PPROMISC) {
1651 /* Permanently promiscuous mode requested */
1652 ifp->if_flags |= IFF_PROMISC;
1653 } else if (ifp->if_pcount == 0) {
1654 ifp->if_flags &= ~IFF_PROMISC;
1656 if (ifp->if_ioctl) {
1657 ifnet_serialize_all(ifp);
1658 ifp->if_ioctl(ifp, cmd, data, cred);
1659 ifnet_deserialize_all(ifp);
1661 getmicrotime(&ifp->if_lastchange);
1665 error = priv_check_cred(cred, PRIV_ROOT, 0);
1668 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1672 ifnet_serialize_all(ifp);
1673 ifp->if_ioctl(ifp, cmd, data, cred);
1674 ifnet_deserialize_all(ifp);
1678 error = priv_check_cred(cred, PRIV_ROOT, 0);
1681 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1684 if (new_name[0] == '\0') {
1688 if (ifunit(new_name) != NULL) {
1693 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1695 /* Announce the departure of the interface. */
1696 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1698 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1699 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1700 /* XXX IFA_LOCK(ifa); */
1701 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1702 namelen = strlen(new_name);
1703 onamelen = sdl->sdl_nlen;
1705 * Move the address if needed. This is safe because we
1706 * allocate space for a name of length IFNAMSIZ when we
1707 * create this in if_attach().
1709 if (namelen != onamelen) {
1710 bcopy(sdl->sdl_data + onamelen,
1711 sdl->sdl_data + namelen, sdl->sdl_alen);
1713 bcopy(new_name, sdl->sdl_data, namelen);
1714 sdl->sdl_nlen = namelen;
1715 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1716 bzero(sdl->sdl_data, onamelen);
1717 while (namelen != 0)
1718 sdl->sdl_data[--namelen] = 0xff;
1719 /* XXX IFA_UNLOCK(ifa) */
1721 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1723 /* Announce the return of the interface. */
1724 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1728 error = priv_check_cred(cred, PRIV_ROOT, 0);
1731 ifp->if_metric = ifr->ifr_metric;
1732 getmicrotime(&ifp->if_lastchange);
1736 error = priv_check_cred(cred, PRIV_ROOT, 0);
1739 if (ifp->if_ioctl == NULL) {
1743 ifnet_serialize_all(ifp);
1744 error = ifp->if_ioctl(ifp, cmd, data, cred);
1745 ifnet_deserialize_all(ifp);
1747 getmicrotime(&ifp->if_lastchange);
1752 u_long oldmtu = ifp->if_mtu;
1754 error = priv_check_cred(cred, PRIV_ROOT, 0);
1757 if (ifp->if_ioctl == NULL) {
1761 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
1765 ifnet_serialize_all(ifp);
1766 error = ifp->if_ioctl(ifp, cmd, data, cred);
1767 ifnet_deserialize_all(ifp);
1769 getmicrotime(&ifp->if_lastchange);
1773 * If the link MTU changed, do network layer specific procedure.
1775 if (ifp->if_mtu != oldmtu) {
1784 error = priv_check_cred(cred, PRIV_ROOT, 0);
1788 /* XXX need driver supplied upper limit */
1789 if (ifr->ifr_tsolen <= 0) {
1793 ifp->if_tsolen = ifr->ifr_tsolen;
1798 error = priv_check_cred(cred, PRIV_ROOT, 0);
1802 /* Don't allow group membership on non-multicast interfaces. */
1803 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1808 /* Don't let users screw up protocols' entries. */
1809 if (ifr->ifr_addr.sa_family != AF_LINK) {
1814 if (cmd == SIOCADDMULTI) {
1815 struct ifmultiaddr *ifma;
1816 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1818 error = if_delmulti(ifp, &ifr->ifr_addr);
1821 getmicrotime(&ifp->if_lastchange);
1824 case SIOCSIFPHYADDR:
1825 case SIOCDIFPHYADDR:
1827 case SIOCSIFPHYADDR_IN6:
1829 case SIOCSLIFPHYADDR:
1831 case SIOCSIFGENERIC:
1832 error = priv_check_cred(cred, PRIV_ROOT, 0);
1835 if (ifp->if_ioctl == 0) {
1839 ifnet_serialize_all(ifp);
1840 error = ifp->if_ioctl(ifp, cmd, data, cred);
1841 ifnet_deserialize_all(ifp);
1843 getmicrotime(&ifp->if_lastchange);
1847 ifs = (struct ifstat *)data;
1848 ifs->ascii[0] = '\0';
1850 case SIOCGIFPSRCADDR:
1851 case SIOCGIFPDSTADDR:
1852 case SIOCGLIFPHYADDR:
1854 case SIOCGIFGENERIC:
1855 if (ifp->if_ioctl == NULL) {
1859 ifnet_serialize_all(ifp);
1860 error = ifp->if_ioctl(ifp, cmd, data, cred);
1861 ifnet_deserialize_all(ifp);
1865 error = priv_check_cred(cred, PRIV_ROOT, 0);
1868 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
1869 ifr->ifr_addr.sa_len);
1870 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
1874 oif_flags = ifp->if_flags;
1875 if (so->so_proto == 0) {
1880 error = so_pru_control_direct(so, cmd, data, ifp);
1885 case SIOCSIFDSTADDR:
1887 case SIOCSIFBRDADDR:
1888 case SIOCSIFNETMASK:
1889 #if BYTE_ORDER != BIG_ENDIAN
1890 if (ifr->ifr_addr.sa_family == 0 &&
1891 ifr->ifr_addr.sa_len < 16) {
1892 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1893 ifr->ifr_addr.sa_len = 16;
1896 if (ifr->ifr_addr.sa_len == 0)
1897 ifr->ifr_addr.sa_len = 16;
1903 case OSIOCGIFDSTADDR:
1904 cmd = SIOCGIFDSTADDR;
1906 case OSIOCGIFBRDADDR:
1907 cmd = SIOCGIFBRDADDR;
1909 case OSIOCGIFNETMASK:
1910 cmd = SIOCGIFNETMASK;
1916 error = so_pru_control_direct(so, cmd, data, ifp);
1920 case OSIOCGIFDSTADDR:
1921 case OSIOCGIFBRDADDR:
1922 case OSIOCGIFNETMASK:
1923 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
1926 #endif /* COMPAT_43 */
1928 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
1930 DELAY(100);/* XXX: temporary workaround for fxp issue*/
1931 if (ifp->if_flags & IFF_UP) {
1941 mtx_unlock(&ifp->if_ioctl_mtx);
1946 * Set/clear promiscuous mode on interface ifp based on the truth value
1947 * of pswitch. The calls are reference counted so that only the first
1948 * "on" request actually has an effect, as does the final "off" request.
1949 * Results are undefined if the "off" and "on" requests are not matched.
1952 ifpromisc(struct ifnet *ifp, int pswitch)
1958 oldflags = ifp->if_flags;
1959 if (ifp->if_flags & IFF_PPROMISC) {
1960 /* Do nothing if device is in permanently promiscuous mode */
1961 ifp->if_pcount += pswitch ? 1 : -1;
1966 * If the device is not configured up, we cannot put it in
1969 if ((ifp->if_flags & IFF_UP) == 0)
1971 if (ifp->if_pcount++ != 0)
1973 ifp->if_flags |= IFF_PROMISC;
1974 log(LOG_INFO, "%s: promiscuous mode enabled\n",
1977 if (--ifp->if_pcount > 0)
1979 ifp->if_flags &= ~IFF_PROMISC;
1980 log(LOG_INFO, "%s: promiscuous mode disabled\n",
1983 ifr.ifr_flags = ifp->if_flags;
1984 ifr.ifr_flagshigh = ifp->if_flags >> 16;
1985 ifnet_serialize_all(ifp);
1986 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
1987 ifnet_deserialize_all(ifp);
1991 ifp->if_flags = oldflags;
1996 * Return interface configuration
1997 * of system. List may be used
1998 * in later ioctl's (above) to get
1999 * other information.
2002 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2004 struct ifconf *ifc = (struct ifconf *)data;
2006 struct sockaddr *sa;
2007 struct ifreq ifr, *ifrp;
2008 int space = ifc->ifc_len, error = 0;
2010 ifrp = ifc->ifc_req;
2011 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2012 struct ifaddr_container *ifac;
2015 if (space <= sizeof ifr)
2019 * Zero the stack declared structure first to prevent
2020 * memory disclosure.
2022 bzero(&ifr, sizeof(ifr));
2023 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2024 >= sizeof(ifr.ifr_name)) {
2025 error = ENAMETOOLONG;
2030 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2031 struct ifaddr *ifa = ifac->ifa;
2033 if (space <= sizeof ifr)
2036 if (cred->cr_prison &&
2037 prison_if(cred, sa))
2041 if (cmd == OSIOCGIFCONF) {
2042 struct osockaddr *osa =
2043 (struct osockaddr *)&ifr.ifr_addr;
2045 osa->sa_family = sa->sa_family;
2046 error = copyout(&ifr, ifrp, sizeof ifr);
2050 if (sa->sa_len <= sizeof(*sa)) {
2052 error = copyout(&ifr, ifrp, sizeof ifr);
2055 if (space < (sizeof ifr) + sa->sa_len -
2058 space -= sa->sa_len - sizeof(*sa);
2059 error = copyout(&ifr, ifrp,
2060 sizeof ifr.ifr_name);
2062 error = copyout(sa, &ifrp->ifr_addr,
2064 ifrp = (struct ifreq *)
2065 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2069 space -= sizeof ifr;
2074 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2075 error = copyout(&ifr, ifrp, sizeof ifr);
2078 space -= sizeof ifr;
2082 ifc->ifc_len -= space;
2087 * Just like if_promisc(), but for all-multicast-reception mode.
2090 if_allmulti(struct ifnet *ifp, int onswitch)
2098 if (ifp->if_amcount++ == 0) {
2099 ifp->if_flags |= IFF_ALLMULTI;
2100 ifr.ifr_flags = ifp->if_flags;
2101 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2102 ifnet_serialize_all(ifp);
2103 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2105 ifnet_deserialize_all(ifp);
2108 if (ifp->if_amcount > 1) {
2111 ifp->if_amcount = 0;
2112 ifp->if_flags &= ~IFF_ALLMULTI;
2113 ifr.ifr_flags = ifp->if_flags;
2114 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2115 ifnet_serialize_all(ifp);
2116 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2118 ifnet_deserialize_all(ifp);
2130 * Add a multicast listenership to the interface in question.
2131 * The link layer provides a routine which converts
2135 struct ifnet *ifp, /* interface to manipulate */
2136 struct sockaddr *sa, /* address to add */
2137 struct ifmultiaddr **retifma)
2139 struct sockaddr *llsa, *dupsa;
2141 struct ifmultiaddr *ifma;
2144 * If the matching multicast address already exists
2145 * then don't add a new one, just add a reference
2147 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2148 if (sa_equal(sa, ifma->ifma_addr)) {
2149 ifma->ifma_refcount++;
2157 * Give the link layer a chance to accept/reject it, and also
2158 * find out which AF_LINK address this maps to, if it isn't one
2161 if (ifp->if_resolvemulti) {
2162 ifnet_serialize_all(ifp);
2163 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2164 ifnet_deserialize_all(ifp);
2171 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2172 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_WAITOK);
2173 bcopy(sa, dupsa, sa->sa_len);
2175 ifma->ifma_addr = dupsa;
2176 ifma->ifma_lladdr = llsa;
2177 ifma->ifma_ifp = ifp;
2178 ifma->ifma_refcount = 1;
2179 ifma->ifma_protospec = 0;
2180 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2183 * Some network interfaces can scan the address list at
2184 * interrupt time; lock them out.
2187 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2193 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2194 if (sa_equal(ifma->ifma_addr, llsa))
2198 ifma->ifma_refcount++;
2200 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_WAITOK);
2201 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_WAITOK);
2202 bcopy(llsa, dupsa, llsa->sa_len);
2203 ifma->ifma_addr = dupsa;
2204 ifma->ifma_ifp = ifp;
2205 ifma->ifma_refcount = 1;
2207 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2212 * We are certain we have added something, so call down to the
2213 * interface to let them know about it.
2216 ifnet_serialize_all(ifp);
2218 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2219 ifnet_deserialize_all(ifp);
2226 * Remove a reference to a multicast address on this interface. Yell
2227 * if the request does not match an existing membership.
2230 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2232 struct ifmultiaddr *ifma;
2234 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2235 if (sa_equal(sa, ifma->ifma_addr))
2240 if (ifma->ifma_refcount > 1) {
2241 ifma->ifma_refcount--;
2245 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2246 sa = ifma->ifma_lladdr;
2248 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2250 * Make sure the interface driver is notified
2251 * in the case of a link layer mcast group being left.
2253 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL) {
2254 ifnet_serialize_all(ifp);
2255 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2256 ifnet_deserialize_all(ifp);
2259 kfree(ifma->ifma_addr, M_IFMADDR);
2260 kfree(ifma, M_IFMADDR);
2265 * Now look for the link-layer address which corresponds to
2266 * this network address. It had been squirreled away in
2267 * ifma->ifma_lladdr for this purpose (so we don't have
2268 * to call ifp->if_resolvemulti() again), and we saved that
2269 * value in sa above. If some nasty deleted the
2270 * link-layer address out from underneath us, we can deal because
2271 * the address we stored was is not the same as the one which was
2272 * in the record for the link-layer address. (So we don't complain
2275 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2276 if (sa_equal(sa, ifma->ifma_addr))
2281 if (ifma->ifma_refcount > 1) {
2282 ifma->ifma_refcount--;
2287 ifnet_serialize_all(ifp);
2288 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2289 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2290 ifnet_deserialize_all(ifp);
2292 kfree(ifma->ifma_addr, M_IFMADDR);
2293 kfree(sa, M_IFMADDR);
2294 kfree(ifma, M_IFMADDR);
2300 * Delete all multicast group membership for an interface.
2301 * Should be used to quickly flush all multicast filters.
2304 if_delallmulti(struct ifnet *ifp)
2306 struct ifmultiaddr *ifma;
2307 struct ifmultiaddr *next;
2309 TAILQ_FOREACH_MUTABLE(ifma, &ifp->if_multiaddrs, ifma_link, next)
2310 if_delmulti(ifp, ifma->ifma_addr);
2315 * Set the link layer address on an interface.
2317 * At this time we only support certain types of interfaces,
2318 * and we don't allow the length of the address to change.
2321 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2323 struct sockaddr_dl *sdl;
2326 sdl = IF_LLSOCKADDR(ifp);
2329 if (len != sdl->sdl_alen) /* don't allow length to change */
2331 switch (ifp->if_type) {
2332 case IFT_ETHER: /* these types use struct arpcom */
2335 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2336 bcopy(lladdr, LLADDR(sdl), len);
2342 * If the interface is already up, we need
2343 * to re-init it in order to reprogram its
2346 ifnet_serialize_all(ifp);
2347 if ((ifp->if_flags & IFF_UP) != 0) {
2349 struct ifaddr_container *ifac;
2352 ifp->if_flags &= ~IFF_UP;
2353 ifr.ifr_flags = ifp->if_flags;
2354 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2355 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2357 ifp->if_flags |= IFF_UP;
2358 ifr.ifr_flags = ifp->if_flags;
2359 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2360 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2364 * Also send gratuitous ARPs to notify other nodes about
2365 * the address change.
2367 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2368 struct ifaddr *ifa = ifac->ifa;
2370 if (ifa->ifa_addr != NULL &&
2371 ifa->ifa_addr->sa_family == AF_INET)
2372 arp_gratuitous(ifp, ifa);
2376 ifnet_deserialize_all(ifp);
2380 struct ifmultiaddr *
2381 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2383 struct ifmultiaddr *ifma;
2385 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2386 if (sa_equal(ifma->ifma_addr, sa))
2393 * This function locates the first real ethernet MAC from a network
2394 * card and loads it into node, returning 0 on success or ENOENT if
2395 * no suitable interfaces were found. It is used by the uuid code to
2396 * generate a unique 6-byte number.
2399 if_getanyethermac(uint16_t *node, int minlen)
2402 struct sockaddr_dl *sdl;
2404 TAILQ_FOREACH(ifp, &ifnet, if_link) {
2405 if (ifp->if_type != IFT_ETHER)
2407 sdl = IF_LLSOCKADDR(ifp);
2408 if (sdl->sdl_alen < minlen)
2410 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2418 * The name argument must be a pointer to storage which will last as
2419 * long as the interface does. For physical devices, the result of
2420 * device_get_name(dev) is a good choice and for pseudo-devices a
2421 * static string works well.
2424 if_initname(struct ifnet *ifp, const char *name, int unit)
2426 ifp->if_dname = name;
2427 ifp->if_dunit = unit;
2428 if (unit != IF_DUNIT_NONE)
2429 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2431 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2435 if_printf(struct ifnet *ifp, const char *fmt, ...)
2440 retval = kprintf("%s: ", ifp->if_xname);
2441 __va_start(ap, fmt);
2442 retval += kvprintf(fmt, ap);
2448 if_alloc(uint8_t type)
2454 * XXX temporary hack until arpcom is setup in if_l2com
2456 if (type == IFT_ETHER)
2457 size = sizeof(struct arpcom);
2459 size = sizeof(struct ifnet);
2461 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2463 ifp->if_type = type;
2465 if (if_com_alloc[type] != NULL) {
2466 ifp->if_l2com = if_com_alloc[type](type, ifp);
2467 if (ifp->if_l2com == NULL) {
2468 kfree(ifp, M_IFNET);
2476 if_free(struct ifnet *ifp)
2478 kfree(ifp, M_IFNET);
2482 ifq_set_classic(struct ifaltq *ifq)
2484 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2485 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2489 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2490 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2494 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2495 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2496 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2497 KASSERT(request != NULL, ("request is not specified"));
2499 ifq->altq_mapsubq = mapsubq;
2500 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2501 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2503 ifsq->ifsq_enqueue = enqueue;
2504 ifsq->ifsq_dequeue = dequeue;
2505 ifsq->ifsq_request = request;
2510 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2511 struct altq_pktattr *pa __unused)
2513 if (IF_QFULL(ifsq)) {
2517 IF_ENQUEUE(ifsq, m);
2523 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, struct mbuf *mpolled, int op)
2532 IF_DEQUEUE(ifsq, m);
2535 panic("unsupported ALTQ dequeue op: %d", op);
2537 KKASSERT(mpolled == NULL || mpolled == m);
2542 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2549 panic("unsupported ALTQ request: %d", req);
2555 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2557 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2558 int running = 0, need_sched;
2561 * Try to do direct ifnet.if_start first, if there is
2562 * contention on ifnet's serializer, ifnet.if_start will
2563 * be scheduled on ifnet's CPU.
2565 if (!ifsq_tryserialize_hw(ifsq)) {
2567 * ifnet serializer contention happened,
2568 * ifnet.if_start is scheduled on ifnet's
2569 * CPU, and we keep going.
2571 ifsq_ifstart_schedule(ifsq, 1);
2575 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2576 ifp->if_start(ifp, ifsq);
2577 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2580 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2582 ifsq_deserialize_hw(ifsq);
2586 * More data need to be transmitted, ifnet.if_start is
2587 * scheduled on ifnet's CPU, and we keep going.
2588 * NOTE: ifnet.if_start interlock is not released.
2590 ifsq_ifstart_schedule(ifsq, force_sched);
2595 * IFSUBQ packets staging mechanism:
2597 * The packets enqueued into IFSUBQ are staged to a certain amount before the
2598 * ifnet's if_start is called. In this way, the driver could avoid writing
2599 * to hardware registers upon every packet, instead, hardware registers
2600 * could be written when certain amount of packets are put onto hardware
2601 * TX ring. The measurement on several modern NICs (emx(4), igb(4), bnx(4),
2602 * bge(4), jme(4)) shows that the hardware registers writing aggregation
2603 * could save ~20% CPU time when 18bytes UDP datagrams are transmitted at
2604 * 1.48Mpps. The performance improvement by hardware registers writing
2605 * aggeregation is also mentioned by Luigi Rizzo's netmap paper
2606 * (http://info.iet.unipi.it/~luigi/netmap/).
2608 * IFSUBQ packets staging is performed for two entry points into drivers's
2609 * transmission function:
2610 * - Direct ifnet's if_start calling, i.e. ifsq_ifstart_try()
2611 * - ifnet's if_start scheduling, i.e. ifsq_ifstart_schedule()
2613 * IFSUBQ packets staging will be stopped upon any of the following conditions:
2614 * - If the count of packets enqueued on the current CPU is great than or
2615 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2616 * - If the total length of packets enqueued on the current CPU is great
2617 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2618 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2619 * is usually less than hardware's MTU.
2620 * - ifsq_ifstart_schedule() is not pending on the current CPU and if_start
2621 * interlock (if_snd.altq_started) is not released.
2622 * - The if_start_rollup(), which is registered as low priority netisr
2623 * rollup function, is called; probably because no more work is pending
2627 * Currently IFSUBQ packet staging is only performed in netisr threads.
2630 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2632 struct ifaltq *ifq = &ifp->if_snd;
2633 struct ifaltq_subque *ifsq;
2634 int error, start = 0, len, mcast = 0, avoid_start = 0;
2635 struct ifsubq_stage_head *head = NULL;
2636 struct ifsubq_stage *stage = NULL;
2638 ifsq = ifq_map_subq(ifq, mycpuid);
2639 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2641 len = m->m_pkthdr.len;
2642 if (m->m_flags & M_MCAST)
2645 if (curthread->td_type == TD_TYPE_NETISR) {
2646 head = &ifsubq_stage_heads[mycpuid];
2647 stage = ifsq_get_stage(ifsq, mycpuid);
2650 stage->stg_len += len;
2651 if (stage->stg_cnt < ifsq_stage_cntmax &&
2652 stage->stg_len < (ifp->if_mtu - max_protohdr))
2657 error = ifsq_enqueue_locked(ifsq, m, pa);
2659 if (!ifsq_data_ready(ifsq)) {
2660 ALTQ_SQ_UNLOCK(ifsq);
2665 if (!ifsq_is_started(ifsq)) {
2667 ALTQ_SQ_UNLOCK(ifsq);
2670 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
2671 ifsq_stage_insert(head, stage);
2673 IFNET_STAT_INC(ifp, obytes, len);
2675 IFNET_STAT_INC(ifp, omcasts, 1);
2680 * Hold the interlock of ifnet.if_start
2682 ifsq_set_started(ifsq);
2685 ALTQ_SQ_UNLOCK(ifsq);
2688 IFNET_STAT_INC(ifp, obytes, len);
2690 IFNET_STAT_INC(ifp, omcasts, 1);
2693 if (stage != NULL) {
2694 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
2695 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
2697 ifsq_stage_remove(head, stage);
2698 ifsq_ifstart_schedule(ifsq, 1);
2703 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
2704 ifsq_stage_remove(head, stage);
2714 ifsq_ifstart_try(ifsq, 0);
2719 ifa_create(int size, int flags)
2724 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
2726 ifa = kmalloc(size, M_IFADDR, flags | M_ZERO);
2730 ifa->ifa_containers =
2731 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
2732 M_IFADDR, M_WAITOK | M_ZERO);
2733 ifa->ifa_ncnt = ncpus;
2734 for (i = 0; i < ncpus; ++i) {
2735 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
2737 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
2739 ifac->ifa_refcnt = 1;
2742 kprintf("alloc ifa %p %d\n", ifa, size);
2748 ifac_free(struct ifaddr_container *ifac, int cpu_id)
2750 struct ifaddr *ifa = ifac->ifa;
2752 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
2753 KKASSERT(ifac->ifa_refcnt == 0);
2754 KASSERT(ifac->ifa_listmask == 0,
2755 ("ifa is still on %#x lists", ifac->ifa_listmask));
2757 ifac->ifa_magic = IFA_CONTAINER_DEAD;
2759 #ifdef IFADDR_DEBUG_VERBOSE
2760 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
2763 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
2764 ("invalid # of ifac, %d", ifa->ifa_ncnt));
2765 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
2767 kprintf("free ifa %p\n", ifa);
2769 kfree(ifa->ifa_containers, M_IFADDR);
2770 kfree(ifa, M_IFADDR);
2775 ifa_iflink_dispatch(netmsg_t nmsg)
2777 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2778 struct ifaddr *ifa = msg->ifa;
2779 struct ifnet *ifp = msg->ifp;
2781 struct ifaddr_container *ifac;
2785 ifac = &ifa->ifa_containers[cpu];
2786 ASSERT_IFAC_VALID(ifac);
2787 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
2788 ("ifaddr is on if_addrheads"));
2790 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
2792 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
2794 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
2798 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2802 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
2804 struct netmsg_ifaddr msg;
2806 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2807 0, ifa_iflink_dispatch);
2812 ifa_domsg(&msg.base.lmsg, 0);
2816 ifa_ifunlink_dispatch(netmsg_t nmsg)
2818 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2819 struct ifaddr *ifa = msg->ifa;
2820 struct ifnet *ifp = msg->ifp;
2822 struct ifaddr_container *ifac;
2826 ifac = &ifa->ifa_containers[cpu];
2827 ASSERT_IFAC_VALID(ifac);
2828 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
2829 ("ifaddr is not on if_addrhead"));
2831 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
2832 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
2836 ifa_forwardmsg(&nmsg->lmsg, cpu + 1);
2840 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
2842 struct netmsg_ifaddr msg;
2844 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2845 0, ifa_ifunlink_dispatch);
2849 ifa_domsg(&msg.base.lmsg, 0);
2853 ifa_destroy_dispatch(netmsg_t nmsg)
2855 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
2858 ifa_forwardmsg(&nmsg->lmsg, mycpuid + 1);
2862 ifa_destroy(struct ifaddr *ifa)
2864 struct netmsg_ifaddr msg;
2866 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
2867 0, ifa_destroy_dispatch);
2870 ifa_domsg(&msg.base.lmsg, 0);
2874 ifnet_portfn(int cpu)
2876 return &ifnet_threads[cpu].td_msgport;
2880 ifnet_forwardmsg(struct lwkt_msg *lmsg, int next_cpu)
2882 KKASSERT(next_cpu > mycpuid && next_cpu <= ncpus);
2884 if (next_cpu < ncpus)
2885 lwkt_forwardmsg(ifnet_portfn(next_cpu), lmsg);
2887 lwkt_replymsg(lmsg, 0);
2891 ifnet_domsg(struct lwkt_msg *lmsg, int cpu)
2893 KKASSERT(cpu < ncpus);
2894 return lwkt_domsg(ifnet_portfn(cpu), lmsg, 0);
2898 ifnet_sendmsg(struct lwkt_msg *lmsg, int cpu)
2900 KKASSERT(cpu < ncpus);
2901 lwkt_sendmsg(ifnet_portfn(cpu), lmsg);
2905 * Generic netmsg service loop. Some protocols may roll their own but all
2906 * must do the basic command dispatch function call done here.
2909 ifnet_service_loop(void *arg __unused)
2913 while ((msg = lwkt_waitport(&curthread->td_msgport, 0))) {
2914 KASSERT(msg->base.nm_dispatch, ("ifnet_service: badmsg"));
2915 msg->base.nm_dispatch(msg);
2920 if_start_rollup(void)
2922 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
2923 struct ifsubq_stage *stage;
2925 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
2926 struct ifaltq_subque *ifsq = stage->stg_subq;
2929 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
2931 ifsq_stage_remove(head, stage);
2934 ifsq_ifstart_schedule(ifsq, 1);
2939 if (!ifsq_is_started(ifsq)) {
2941 * Hold the interlock of ifnet.if_start
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);