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. 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_inet6.h"
35 #include "opt_ifpoll.h"
37 #include <sys/param.h>
38 #include <sys/malloc.h>
40 #include <sys/systm.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketops.h>
47 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
53 #include <sys/domain.h>
54 #include <sys/thread.h>
55 #include <sys/serialize.h>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
60 #include <sys/mutex2.h>
63 #include <net/if_arp.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/ifq_var.h>
68 #include <net/radix.h>
69 #include <net/route.h>
70 #include <net/if_clone.h>
71 #include <net/netisr2.h>
72 #include <net/netmsg2.h>
74 #include <machine/atomic.h>
75 #include <machine/stdarg.h>
76 #include <machine/smp.h>
78 #if defined(INET) || defined(INET6)
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82 #include <netinet/if_ether.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet6/in6_ifattach.h>
89 struct netmsg_ifaddr {
90 struct netmsg_base base;
96 struct ifsubq_stage_head {
97 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 *);
116 static int if_rtdel(struct radix_node *, void *);
117 static void if_slowtimo_dispatch(netmsg_t);
119 /* Helper functions */
120 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
121 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
122 static struct ifnet_array *ifnet_array_alloc(int);
123 static void ifnet_array_free(struct ifnet_array *);
124 static struct ifnet_array *ifnet_array_add(struct ifnet *,
125 const struct ifnet_array *);
126 static struct ifnet_array *ifnet_array_del(struct ifnet *,
127 const struct ifnet_array *);
131 * XXX: declare here to avoid to include many inet6 related files..
132 * should be more generalized?
134 extern void nd6_setmtu(struct ifnet *);
137 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
138 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
139 SYSCTL_NODE(_net_link, OID_AUTO, ringmap, CTLFLAG_RW, 0, "link ringmap");
141 static int ifsq_stage_cntmax = 4;
142 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
143 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
144 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
146 static int if_stats_compat = 0;
147 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
148 &if_stats_compat, 0, "Compat the old ifnet stats");
150 static int if_ringmap_dumprdr = 0;
151 SYSCTL_INT(_net_link_ringmap, OID_AUTO, dump_rdr, CTLFLAG_RW,
152 &if_ringmap_dumprdr, 0, "dump redirect table");
154 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
155 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
157 static if_com_alloc_t *if_com_alloc[256];
158 static if_com_free_t *if_com_free[256];
160 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
161 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
162 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
164 int ifqmaxlen = IFQ_MAXLEN;
165 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
167 static struct ifnet_array ifnet_array0;
168 static struct ifnet_array *ifnet_array = &ifnet_array0;
170 static struct callout if_slowtimo_timer;
171 static struct netmsg_base if_slowtimo_netmsg;
174 struct ifnet **ifindex2ifnet = NULL;
175 static struct mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
177 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
180 #define IFQ_KTR_STRING "ifq=%p"
181 #define IFQ_KTR_ARGS struct ifaltq *ifq
183 #define KTR_IFQ KTR_ALL
185 KTR_INFO_MASTER(ifq);
186 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
187 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
188 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
190 #define IF_START_KTR_STRING "ifp=%p"
191 #define IF_START_KTR_ARGS struct ifnet *ifp
193 #define KTR_IF_START KTR_ALL
195 KTR_INFO_MASTER(if_start);
196 KTR_INFO(KTR_IF_START, if_start, run, 0,
197 IF_START_KTR_STRING, IF_START_KTR_ARGS);
198 KTR_INFO(KTR_IF_START, if_start, sched, 1,
199 IF_START_KTR_STRING, IF_START_KTR_ARGS);
200 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
201 IF_START_KTR_STRING, IF_START_KTR_ARGS);
202 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
203 IF_START_KTR_STRING, IF_START_KTR_ARGS);
204 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
205 IF_START_KTR_STRING, IF_START_KTR_ARGS);
206 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
209 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
212 * Network interface utility routines.
214 * Routines with ifa_ifwith* names take sockaddr *'s as
223 callout_init_mp(&if_slowtimo_timer);
224 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
225 MSGF_PRIORITY, if_slowtimo_dispatch);
227 /* XXX is this necessary? */
229 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
230 if (ifp->if_snd.altq_maxlen == 0) {
231 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
232 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
237 /* Start if_slowtimo */
238 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
242 ifsq_ifstart_ipifunc(void *arg)
244 struct ifaltq_subque *ifsq = arg;
245 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
248 if (lmsg->ms_flags & MSGF_DONE)
249 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
254 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
256 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
257 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
258 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
264 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
266 KKASSERT((stage->stg_flags &
267 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
268 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
269 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
273 * Schedule ifnet.if_start on the subqueue owner CPU
276 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
280 if (!force && curthread->td_type == TD_TYPE_NETISR &&
281 ifsq_stage_cntmax > 0) {
282 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
286 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
287 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
288 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
292 cpu = ifsq_get_cpuid(ifsq);
294 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
296 ifsq_ifstart_ipifunc(ifsq);
301 * This function will release ifnet.if_start subqueue interlock,
302 * if ifnet.if_start for the subqueue does not need to be scheduled
305 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
307 if (!running || ifsq_is_empty(ifsq)
309 || ifsq->ifsq_altq->altq_tbr != NULL
314 * ifnet.if_start subqueue interlock is released, if:
315 * 1) Hardware can not take any packets, due to
316 * o interface is marked down
317 * o hardware queue is full (ifsq_is_oactive)
318 * Under the second situation, hardware interrupt
319 * or polling(4) will call/schedule ifnet.if_start
320 * on the subqueue when hardware queue is ready
321 * 2) There is no packet in the subqueue.
322 * Further ifq_dispatch or ifq_handoff will call/
323 * schedule ifnet.if_start on the subqueue.
324 * 3) TBR is used and it does not allow further
326 * TBR callout will call ifnet.if_start on the
329 if (!running || !ifsq_data_ready(ifsq)) {
330 ifsq_clr_started(ifsq);
331 ALTQ_SQ_UNLOCK(ifsq);
334 ALTQ_SQ_UNLOCK(ifsq);
340 ifsq_ifstart_dispatch(netmsg_t msg)
342 struct lwkt_msg *lmsg = &msg->base.lmsg;
343 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
344 struct ifnet *ifp = ifsq_get_ifp(ifsq);
345 struct globaldata *gd = mycpu;
346 int running = 0, need_sched;
350 lwkt_replymsg(lmsg, 0); /* reply ASAP */
352 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
354 * We need to chase the subqueue owner CPU change.
356 ifsq_ifstart_schedule(ifsq, 1);
361 ifsq_serialize_hw(ifsq);
362 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
363 ifp->if_start(ifp, ifsq);
364 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
367 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
368 ifsq_deserialize_hw(ifsq);
372 * More data need to be transmitted, ifnet.if_start is
373 * scheduled on the subqueue owner CPU, and we keep going.
374 * NOTE: ifnet.if_start subqueue interlock is not released.
376 ifsq_ifstart_schedule(ifsq, 0);
382 /* Device driver ifnet.if_start helper function */
384 ifsq_devstart(struct ifaltq_subque *ifsq)
386 struct ifnet *ifp = ifsq_get_ifp(ifsq);
389 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
392 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
393 ALTQ_SQ_UNLOCK(ifsq);
396 ifsq_set_started(ifsq);
397 ALTQ_SQ_UNLOCK(ifsq);
399 ifp->if_start(ifp, ifsq);
401 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
404 if (ifsq_ifstart_need_schedule(ifsq, running)) {
406 * More data need to be transmitted, ifnet.if_start is
407 * scheduled on ifnet's CPU, and we keep going.
408 * NOTE: ifnet.if_start interlock is not released.
410 ifsq_ifstart_schedule(ifsq, 0);
415 if_devstart(struct ifnet *ifp)
417 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
420 /* Device driver ifnet.if_start schedule helper function */
422 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
424 ifsq_ifstart_schedule(ifsq, 1);
428 if_devstart_sched(struct ifnet *ifp)
430 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
434 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
436 lwkt_serialize_enter(ifp->if_serializer);
440 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
442 lwkt_serialize_exit(ifp->if_serializer);
446 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
448 return lwkt_serialize_try(ifp->if_serializer);
453 if_default_serialize_assert(struct ifnet *ifp,
454 enum ifnet_serialize slz __unused,
455 boolean_t serialized)
458 ASSERT_SERIALIZED(ifp->if_serializer);
460 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
465 * Attach an interface to the list of "active" interfaces.
467 * The serializer is optional.
470 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
473 int namelen, masklen;
474 struct sockaddr_dl *sdl, *sdl_addr;
477 struct ifnet **old_ifindex2ifnet = NULL;
478 struct ifnet_array *old_ifnet_array;
481 static int if_indexlim = 8;
483 if (ifp->if_serialize != NULL) {
484 KASSERT(ifp->if_deserialize != NULL &&
485 ifp->if_tryserialize != NULL &&
486 ifp->if_serialize_assert != NULL,
487 ("serialize functions are partially setup"));
490 * If the device supplies serialize functions,
491 * then clear if_serializer to catch any invalid
492 * usage of this field.
494 KASSERT(serializer == NULL,
495 ("both serialize functions and default serializer "
497 ifp->if_serializer = NULL;
499 KASSERT(ifp->if_deserialize == NULL &&
500 ifp->if_tryserialize == NULL &&
501 ifp->if_serialize_assert == NULL,
502 ("serialize functions are partially setup"));
503 ifp->if_serialize = if_default_serialize;
504 ifp->if_deserialize = if_default_deserialize;
505 ifp->if_tryserialize = if_default_tryserialize;
507 ifp->if_serialize_assert = if_default_serialize_assert;
511 * The serializer can be passed in from the device,
512 * allowing the same serializer to be used for both
513 * the interrupt interlock and the device queue.
514 * If not specified, the netif structure will use an
515 * embedded serializer.
517 if (serializer == NULL) {
518 serializer = &ifp->if_default_serializer;
519 lwkt_serialize_init(serializer);
521 ifp->if_serializer = serializer;
526 * The old code would work if the interface passed a pre-existing
527 * chain of ifaddrs to this code. We don't trust our callers to
528 * properly initialize the tailq, however, so we no longer allow
529 * this unlikely case.
531 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
532 M_IFADDR, M_WAITOK | M_ZERO);
533 for (i = 0; i < ncpus; ++i)
534 TAILQ_INIT(&ifp->if_addrheads[i]);
536 TAILQ_INIT(&ifp->if_multiaddrs);
537 TAILQ_INIT(&ifp->if_groups);
538 getmicrotime(&ifp->if_lastchange);
541 * create a Link Level name for this device
543 namelen = strlen(ifp->if_xname);
544 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
545 socksize = masklen + ifp->if_addrlen;
546 if (socksize < sizeof(*sdl))
547 socksize = sizeof(*sdl);
548 socksize = RT_ROUNDUP(socksize);
549 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
550 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
551 sdl->sdl_len = socksize;
552 sdl->sdl_family = AF_LINK;
553 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
554 sdl->sdl_nlen = namelen;
555 sdl->sdl_type = ifp->if_type;
556 ifp->if_lladdr = ifa;
558 ifa->ifa_rtrequest = link_rtrequest;
559 ifa->ifa_addr = (struct sockaddr *)sdl;
560 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
561 ifa->ifa_netmask = (struct sockaddr *)sdl;
562 sdl->sdl_len = masklen;
564 sdl->sdl_data[--namelen] = 0xff;
565 ifa_iflink(ifa, ifp, 0 /* Insert head */);
567 ifp->if_data_pcpu = kmalloc_cachealign(
568 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
570 if (ifp->if_mapsubq == NULL)
571 ifp->if_mapsubq = ifq_mapsubq_default;
575 ifq->altq_disc = NULL;
576 ifq->altq_flags &= ALTQF_CANTCHANGE;
577 ifq->altq_tbr = NULL;
580 if (ifq->altq_subq_cnt <= 0)
581 ifq->altq_subq_cnt = 1;
582 ifq->altq_subq = kmalloc_cachealign(
583 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
584 M_DEVBUF, M_WAITOK | M_ZERO);
586 if (ifq->altq_maxlen == 0) {
587 if_printf(ifp, "driver didn't set altq_maxlen\n");
588 ifq_set_maxlen(ifq, ifqmaxlen);
591 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
592 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
594 ALTQ_SQ_LOCK_INIT(ifsq);
595 ifsq->ifsq_index = q;
597 ifsq->ifsq_altq = ifq;
598 ifsq->ifsq_ifp = ifp;
600 ifsq->ifsq_maxlen = ifq->altq_maxlen;
601 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
602 ifsq->ifsq_prepended = NULL;
603 ifsq->ifsq_started = 0;
604 ifsq->ifsq_hw_oactive = 0;
605 ifsq_set_cpuid(ifsq, 0);
606 if (ifp->if_serializer != NULL)
607 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
610 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
611 M_DEVBUF, M_WAITOK | M_ZERO);
612 for (i = 0; i < ncpus; ++i)
613 ifsq->ifsq_stage[i].stg_subq = ifsq;
615 ifsq->ifsq_ifstart_nmsg =
616 kmalloc(ncpus * sizeof(struct netmsg_base),
617 M_LWKTMSG, M_WAITOK);
618 for (i = 0; i < ncpus; ++i) {
619 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
620 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
621 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
624 ifq_set_classic(ifq);
627 * Increase mbuf cluster/jcluster limits for the mbufs that
628 * could sit on the device queues for quite some time.
630 if (ifp->if_nmbclusters > 0)
631 mcl_inclimit(ifp->if_nmbclusters);
632 if (ifp->if_nmbjclusters > 0)
633 mjcl_inclimit(ifp->if_nmbjclusters);
636 * Install this ifp into ifindex2inet, ifnet queue and ifnet
637 * array after it is setup.
639 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
640 * by ifnet lock, so that non-netisr threads could get a
645 /* Don't update if_index until ifindex2ifnet is setup */
646 ifp->if_index = if_index + 1;
647 sdl_addr->sdl_index = ifp->if_index;
650 * Install this ifp into ifindex2ifnet
652 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
660 n = if_indexlim * sizeof(*q);
661 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
662 if (ifindex2ifnet != NULL) {
663 bcopy(ifindex2ifnet, q, n/2);
664 /* Free old ifindex2ifnet after sync all netisrs */
665 old_ifindex2ifnet = ifindex2ifnet;
669 ifindex2ifnet[ifp->if_index] = ifp;
671 * Update if_index after this ifp is installed into ifindex2ifnet,
672 * so that netisrs could get a consistent view of ifindex2ifnet.
675 if_index = ifp->if_index;
678 * Install this ifp into ifnet array.
680 /* Free old ifnet array after sync all netisrs */
681 old_ifnet_array = ifnet_array;
682 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
685 * Install this ifp into ifnet queue.
687 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
692 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
693 * are no longer accessed and we can free them safely later on.
695 netmsg_service_sync();
696 if (old_ifindex2ifnet != NULL)
697 kfree(old_ifindex2ifnet, M_IFADDR);
698 ifnet_array_free(old_ifnet_array);
700 if (!SLIST_EMPTY(&domains))
701 if_attachdomain1(ifp);
703 /* Announce the interface. */
704 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
705 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
706 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
710 if_attachdomain(void *dummy)
715 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
716 if_attachdomain1(ifp);
719 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
720 if_attachdomain, NULL);
723 if_attachdomain1(struct ifnet *ifp)
729 /* address family dependent data region */
730 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
731 SLIST_FOREACH(dp, &domains, dom_next)
732 if (dp->dom_ifattach)
733 ifp->if_afdata[dp->dom_family] =
734 (*dp->dom_ifattach)(ifp);
739 * Purge all addresses whose type is _not_ AF_LINK
742 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
744 struct lwkt_msg *lmsg = &nmsg->lmsg;
745 struct ifnet *ifp = lmsg->u.ms_resultp;
746 struct ifaddr_container *ifac, *next;
751 * The ifaddr processing in the following loop will block,
752 * however, this function is called in netisr0, in which
753 * ifaddr list changes happen, so we don't care about the
754 * blockness of the ifaddr processing here.
756 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
758 struct ifaddr *ifa = ifac->ifa;
761 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
764 /* Leave link ifaddr as it is */
765 if (ifa->ifa_addr->sa_family == AF_LINK)
768 /* XXX: Ugly!! ad hoc just for INET */
769 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
770 struct ifaliasreq ifr;
771 #ifdef IFADDR_DEBUG_VERBOSE
774 kprintf("purge in4 addr %p: ", ifa);
775 for (i = 0; i < ncpus; ++i)
776 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
780 bzero(&ifr, sizeof ifr);
781 ifr.ifra_addr = *ifa->ifa_addr;
782 if (ifa->ifa_dstaddr)
783 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
784 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
790 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
791 #ifdef IFADDR_DEBUG_VERBOSE
794 kprintf("purge in6 addr %p: ", ifa);
795 for (i = 0; i < ncpus; ++i)
796 kprintf("%d ", ifa->ifa_containers[i].ifa_refcnt);
801 /* ifp_addrhead is already updated */
805 ifa_ifunlink(ifa, ifp);
809 lwkt_replymsg(lmsg, 0);
813 if_purgeaddrs_nolink(struct ifnet *ifp)
815 struct netmsg_base nmsg;
816 struct lwkt_msg *lmsg = &nmsg.lmsg;
818 ASSERT_CANDOMSG_NETISR0(curthread);
820 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
821 if_purgeaddrs_nolink_dispatch);
822 lmsg->u.ms_resultp = ifp;
823 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
827 ifq_stage_detach_handler(netmsg_t nmsg)
829 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
832 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
833 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
834 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
836 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
837 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
839 lwkt_replymsg(&nmsg->lmsg, 0);
843 ifq_stage_detach(struct ifaltq *ifq)
845 struct netmsg_base base;
848 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
849 ifq_stage_detach_handler);
850 base.lmsg.u.ms_resultp = ifq;
852 for (cpu = 0; cpu < ncpus; ++cpu)
853 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
856 struct netmsg_if_rtdel {
857 struct netmsg_base base;
862 if_rtdel_dispatch(netmsg_t msg)
864 struct netmsg_if_rtdel *rmsg = (void *)msg;
868 for (i = 1; i <= AF_MAX; i++) {
869 struct radix_node_head *rnh;
871 if ((rnh = rt_tables[cpu][i]) == NULL)
873 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
878 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
880 lwkt_replymsg(&rmsg->base.lmsg, 0);
884 * Detach an interface, removing it from the
885 * list of "active" interfaces.
888 if_detach(struct ifnet *ifp)
890 struct ifnet_array *old_ifnet_array;
891 struct netmsg_if_rtdel msg;
895 /* Announce that the interface is gone. */
896 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
897 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
898 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
901 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
902 * array before it is whacked.
904 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
905 * by ifnet lock, so that non-netisr threads could get a
911 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
913 ifindex2ifnet[ifp->if_index] = NULL;
914 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
918 * Remove this ifp from ifnet queue.
920 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
923 * Remove this ifp from ifnet array.
925 /* Free old ifnet array after sync all netisrs */
926 old_ifnet_array = ifnet_array;
927 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
932 * Sync all netisrs so that the old ifnet array is no longer
933 * accessed and we can free it safely later on.
935 netmsg_service_sync();
936 ifnet_array_free(old_ifnet_array);
939 * Remove routes and flush queues.
943 if (ifp->if_flags & IFF_NPOLLING)
944 ifpoll_deregister(ifp);
948 /* Decrease the mbuf clusters/jclusters limits increased by us */
949 if (ifp->if_nmbclusters > 0)
950 mcl_inclimit(-ifp->if_nmbclusters);
951 if (ifp->if_nmbjclusters > 0)
952 mjcl_inclimit(-ifp->if_nmbjclusters);
955 if (ifq_is_enabled(&ifp->if_snd))
956 altq_disable(&ifp->if_snd);
957 if (ifq_is_attached(&ifp->if_snd))
958 altq_detach(&ifp->if_snd);
962 * Clean up all addresses.
964 ifp->if_lladdr = NULL;
966 if_purgeaddrs_nolink(ifp);
967 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
970 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
971 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
972 ("non-link ifaddr is left on if_addrheads"));
974 ifa_ifunlink(ifa, ifp);
976 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
977 ("there are still ifaddrs left on if_addrheads"));
982 * Remove all IPv4 kernel structures related to ifp.
989 * Remove all IPv6 kernel structs related to ifp. This should be done
990 * before removing routing entries below, since IPv6 interface direct
991 * routes are expected to be removed by the IPv6-specific kernel API.
992 * Otherwise, the kernel will detect some inconsistency and bark it.
998 * Delete all remaining routes using this interface
1000 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1003 rt_domsg_global(&msg.base);
1005 SLIST_FOREACH(dp, &domains, dom_next)
1006 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1007 (*dp->dom_ifdetach)(ifp,
1008 ifp->if_afdata[dp->dom_family]);
1010 kfree(ifp->if_addrheads, M_IFADDR);
1012 lwkt_synchronize_ipiqs("if_detach");
1013 ifq_stage_detach(&ifp->if_snd);
1015 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1016 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1018 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1019 kfree(ifsq->ifsq_stage, M_DEVBUF);
1021 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1023 kfree(ifp->if_data_pcpu, M_DEVBUF);
1029 * Create interface group without members
1032 if_creategroup(const char *groupname)
1034 struct ifg_group *ifg = NULL;
1036 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1037 M_TEMP, M_NOWAIT)) == NULL)
1040 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1041 ifg->ifg_refcnt = 0;
1042 ifg->ifg_carp_demoted = 0;
1043 TAILQ_INIT(&ifg->ifg_members);
1045 pfi_attach_ifgroup(ifg);
1047 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1053 * Add a group to an interface
1056 if_addgroup(struct ifnet *ifp, const char *groupname)
1058 struct ifg_list *ifgl;
1059 struct ifg_group *ifg = NULL;
1060 struct ifg_member *ifgm;
1062 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1063 groupname[strlen(groupname) - 1] <= '9')
1066 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1067 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1070 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1073 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1074 kfree(ifgl, M_TEMP);
1078 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1079 if (!strcmp(ifg->ifg_group, groupname))
1082 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1083 kfree(ifgl, M_TEMP);
1084 kfree(ifgm, M_TEMP);
1089 ifgl->ifgl_group = ifg;
1090 ifgm->ifgm_ifp = ifp;
1092 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1093 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1096 pfi_group_change(groupname);
1103 * Remove a group from an interface
1106 if_delgroup(struct ifnet *ifp, const char *groupname)
1108 struct ifg_list *ifgl;
1109 struct ifg_member *ifgm;
1111 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1112 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1117 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1119 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1120 if (ifgm->ifgm_ifp == ifp)
1124 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1125 kfree(ifgm, M_TEMP);
1128 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1129 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1131 pfi_detach_ifgroup(ifgl->ifgl_group);
1133 kfree(ifgl->ifgl_group, M_TEMP);
1136 kfree(ifgl, M_TEMP);
1139 pfi_group_change(groupname);
1146 * Stores all groups from an interface in memory pointed
1150 if_getgroup(caddr_t data, struct ifnet *ifp)
1153 struct ifg_list *ifgl;
1154 struct ifg_req ifgrq, *ifgp;
1155 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1157 if (ifgr->ifgr_len == 0) {
1158 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1159 ifgr->ifgr_len += sizeof(struct ifg_req);
1163 len = ifgr->ifgr_len;
1164 ifgp = ifgr->ifgr_groups;
1165 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1166 if (len < sizeof(ifgrq))
1168 bzero(&ifgrq, sizeof ifgrq);
1169 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1170 sizeof(ifgrq.ifgrq_group));
1171 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1172 sizeof(struct ifg_req))))
1174 len -= sizeof(ifgrq);
1182 * Stores all members of a group in memory pointed to by data
1185 if_getgroupmembers(caddr_t data)
1187 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1188 struct ifg_group *ifg;
1189 struct ifg_member *ifgm;
1190 struct ifg_req ifgrq, *ifgp;
1193 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1194 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1199 if (ifgr->ifgr_len == 0) {
1200 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1201 ifgr->ifgr_len += sizeof(ifgrq);
1205 len = ifgr->ifgr_len;
1206 ifgp = ifgr->ifgr_groups;
1207 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1208 if (len < sizeof(ifgrq))
1210 bzero(&ifgrq, sizeof ifgrq);
1211 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1212 sizeof(ifgrq.ifgrq_member));
1213 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1214 sizeof(struct ifg_req))))
1216 len -= sizeof(ifgrq);
1224 * Delete Routes for a Network Interface
1226 * Called for each routing entry via the rnh->rnh_walktree() call above
1227 * to delete all route entries referencing a detaching network interface.
1230 * rn pointer to node in the routing table
1231 * arg argument passed to rnh->rnh_walktree() - detaching interface
1235 * errno failed - reason indicated
1239 if_rtdel(struct radix_node *rn, void *arg)
1241 struct rtentry *rt = (struct rtentry *)rn;
1242 struct ifnet *ifp = arg;
1245 if (rt->rt_ifp == ifp) {
1248 * Protect (sorta) against walktree recursion problems
1249 * with cloned routes
1251 if (!(rt->rt_flags & RTF_UP))
1254 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1255 rt_mask(rt), rt->rt_flags,
1258 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1265 static __inline boolean_t
1266 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1268 if (old_ifa == NULL)
1271 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1272 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1274 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1275 (cur_ifa->ifa_flags & IFA_ROUTE))
1281 * Locate an interface based on a complete address.
1284 ifa_ifwithaddr(struct sockaddr *addr)
1286 const struct ifnet_array *arr;
1289 arr = ifnet_array_get();
1290 for (i = 0; i < arr->ifnet_count; ++i) {
1291 struct ifnet *ifp = arr->ifnet_arr[i];
1292 struct ifaddr_container *ifac;
1294 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1295 struct ifaddr *ifa = ifac->ifa;
1297 if (ifa->ifa_addr->sa_family != addr->sa_family)
1299 if (sa_equal(addr, ifa->ifa_addr))
1301 if ((ifp->if_flags & IFF_BROADCAST) &&
1302 ifa->ifa_broadaddr &&
1303 /* IPv6 doesn't have broadcast */
1304 ifa->ifa_broadaddr->sa_len != 0 &&
1305 sa_equal(ifa->ifa_broadaddr, addr))
1313 * Locate the point to point interface with a given destination address.
1316 ifa_ifwithdstaddr(struct sockaddr *addr)
1318 const struct ifnet_array *arr;
1321 arr = ifnet_array_get();
1322 for (i = 0; i < arr->ifnet_count; ++i) {
1323 struct ifnet *ifp = arr->ifnet_arr[i];
1324 struct ifaddr_container *ifac;
1326 if (!(ifp->if_flags & IFF_POINTOPOINT))
1329 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1330 struct ifaddr *ifa = ifac->ifa;
1332 if (ifa->ifa_addr->sa_family != addr->sa_family)
1334 if (ifa->ifa_dstaddr &&
1335 sa_equal(addr, ifa->ifa_dstaddr))
1343 * Find an interface on a specific network. If many, choice
1344 * is most specific found.
1347 ifa_ifwithnet(struct sockaddr *addr)
1349 struct ifaddr *ifa_maybe = NULL;
1350 u_int af = addr->sa_family;
1351 char *addr_data = addr->sa_data, *cplim;
1352 const struct ifnet_array *arr;
1356 * AF_LINK addresses can be looked up directly by their index number,
1357 * so do that if we can.
1359 if (af == AF_LINK) {
1360 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1362 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1363 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1367 * Scan though each interface, looking for ones that have
1368 * addresses in this address family.
1370 arr = ifnet_array_get();
1371 for (i = 0; i < arr->ifnet_count; ++i) {
1372 struct ifnet *ifp = arr->ifnet_arr[i];
1373 struct ifaddr_container *ifac;
1375 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1376 struct ifaddr *ifa = ifac->ifa;
1377 char *cp, *cp2, *cp3;
1379 if (ifa->ifa_addr->sa_family != af)
1381 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1383 * This is a bit broken as it doesn't
1384 * take into account that the remote end may
1385 * be a single node in the network we are
1387 * The trouble is that we don't know the
1388 * netmask for the remote end.
1390 if (ifa->ifa_dstaddr != NULL &&
1391 sa_equal(addr, ifa->ifa_dstaddr))
1395 * if we have a special address handler,
1396 * then use it instead of the generic one.
1398 if (ifa->ifa_claim_addr) {
1399 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1407 * Scan all the bits in the ifa's address.
1408 * If a bit dissagrees with what we are
1409 * looking for, mask it with the netmask
1410 * to see if it really matters.
1411 * (A byte at a time)
1413 if (ifa->ifa_netmask == 0)
1416 cp2 = ifa->ifa_addr->sa_data;
1417 cp3 = ifa->ifa_netmask->sa_data;
1418 cplim = ifa->ifa_netmask->sa_len +
1419 (char *)ifa->ifa_netmask;
1421 if ((*cp++ ^ *cp2++) & *cp3++)
1422 goto next; /* next address! */
1424 * If the netmask of what we just found
1425 * is more specific than what we had before
1426 * (if we had one) then remember the new one
1427 * before continuing to search for an even
1428 * better one. If the netmasks are equal,
1429 * we prefer the this ifa based on the result
1432 if (ifa_maybe == NULL ||
1433 rn_refines((char *)ifa->ifa_netmask,
1434 (char *)ifa_maybe->ifa_netmask) ||
1435 (sa_equal(ifa_maybe->ifa_netmask,
1436 ifa->ifa_netmask) &&
1437 ifa_prefer(ifa, ifa_maybe)))
1446 * Find an interface address specific to an interface best matching
1450 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1452 struct ifaddr_container *ifac;
1453 char *cp, *cp2, *cp3;
1455 struct ifaddr *ifa_maybe = NULL;
1456 u_int af = addr->sa_family;
1460 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1461 struct ifaddr *ifa = ifac->ifa;
1463 if (ifa->ifa_addr->sa_family != af)
1465 if (ifa_maybe == NULL)
1467 if (ifa->ifa_netmask == NULL) {
1468 if (sa_equal(addr, ifa->ifa_addr) ||
1469 (ifa->ifa_dstaddr != NULL &&
1470 sa_equal(addr, ifa->ifa_dstaddr)))
1474 if (ifp->if_flags & IFF_POINTOPOINT) {
1475 if (sa_equal(addr, ifa->ifa_dstaddr))
1479 cp2 = ifa->ifa_addr->sa_data;
1480 cp3 = ifa->ifa_netmask->sa_data;
1481 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1482 for (; cp3 < cplim; cp3++)
1483 if ((*cp++ ^ *cp2++) & *cp3)
1493 * Default action when installing a route with a Link Level gateway.
1494 * Lookup an appropriate real ifa to point to.
1495 * This should be moved to /sys/net/link.c eventually.
1498 link_rtrequest(int cmd, struct rtentry *rt)
1501 struct sockaddr *dst;
1504 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1505 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1507 ifa = ifaof_ifpforaddr(dst, ifp);
1509 IFAFREE(rt->rt_ifa);
1512 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1513 ifa->ifa_rtrequest(cmd, rt);
1517 struct netmsg_ifroute {
1518 struct netmsg_base base;
1525 * Mark an interface down and notify protocols of the transition.
1528 if_unroute_dispatch(netmsg_t nmsg)
1530 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1531 struct ifnet *ifp = msg->ifp;
1532 int flag = msg->flag, fam = msg->fam;
1533 struct ifaddr_container *ifac;
1535 ifp->if_flags &= ~flag;
1536 getmicrotime(&ifp->if_lastchange);
1538 * The ifaddr processing in the following loop will block,
1539 * however, this function is called in netisr0, in which
1540 * ifaddr list changes happen, so we don't care about the
1541 * blockness of the ifaddr processing here.
1543 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1544 struct ifaddr *ifa = ifac->ifa;
1547 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1550 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1551 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1553 ifq_purge_all(&ifp->if_snd);
1556 lwkt_replymsg(&nmsg->lmsg, 0);
1560 if_unroute(struct ifnet *ifp, int flag, int fam)
1562 struct netmsg_ifroute msg;
1564 ASSERT_CANDOMSG_NETISR0(curthread);
1566 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1567 if_unroute_dispatch);
1571 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1575 * Mark an interface up and notify protocols of the transition.
1578 if_route_dispatch(netmsg_t nmsg)
1580 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1581 struct ifnet *ifp = msg->ifp;
1582 int flag = msg->flag, fam = msg->fam;
1583 struct ifaddr_container *ifac;
1585 ifq_purge_all(&ifp->if_snd);
1586 ifp->if_flags |= flag;
1587 getmicrotime(&ifp->if_lastchange);
1589 * The ifaddr processing in the following loop will block,
1590 * however, this function is called in netisr0, in which
1591 * ifaddr list changes happen, so we don't care about the
1592 * blockness of the ifaddr processing here.
1594 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1595 struct ifaddr *ifa = ifac->ifa;
1598 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1601 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1602 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1609 lwkt_replymsg(&nmsg->lmsg, 0);
1613 if_route(struct ifnet *ifp, int flag, int fam)
1615 struct netmsg_ifroute msg;
1617 ASSERT_CANDOMSG_NETISR0(curthread);
1619 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1624 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1628 * Mark an interface down and notify protocols of the transition. An
1629 * interface going down is also considered to be a synchronizing event.
1630 * We must ensure that all packet processing related to the interface
1631 * has completed before we return so e.g. the caller can free the ifnet
1632 * structure that the mbufs may be referencing.
1634 * NOTE: must be called at splnet or eqivalent.
1637 if_down(struct ifnet *ifp)
1639 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1640 netmsg_service_sync();
1644 * Mark an interface up and notify protocols of
1646 * NOTE: must be called at splnet or eqivalent.
1649 if_up(struct ifnet *ifp)
1651 if_route(ifp, IFF_UP, AF_UNSPEC);
1655 * Process a link state change.
1656 * NOTE: must be called at splsoftnet or equivalent.
1659 if_link_state_change(struct ifnet *ifp)
1661 int link_state = ifp->if_link_state;
1664 devctl_notify("IFNET", ifp->if_xname,
1665 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1669 * Handle interface watchdog timer routines. Called
1670 * from softclock, we decrement timers (if set) and
1671 * call the appropriate interface routine on expiration.
1674 if_slowtimo_dispatch(netmsg_t nmsg)
1676 struct globaldata *gd = mycpu;
1677 const struct ifnet_array *arr;
1680 ASSERT_IN_NETISR(0);
1683 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1686 arr = ifnet_array_get();
1687 for (i = 0; i < arr->ifnet_count; ++i) {
1688 struct ifnet *ifp = arr->ifnet_arr[i];
1692 if (if_stats_compat) {
1693 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1694 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1695 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1696 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1697 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1698 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1699 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1700 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1701 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1702 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1703 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1704 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1707 if (ifp->if_timer == 0 || --ifp->if_timer) {
1711 if (ifp->if_watchdog) {
1712 if (ifnet_tryserialize_all(ifp)) {
1713 (*ifp->if_watchdog)(ifp);
1714 ifnet_deserialize_all(ifp);
1716 /* try again next timeout */
1724 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1728 if_slowtimo(void *arg __unused)
1730 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1732 KASSERT(mycpuid == 0, ("not on cpu0"));
1734 if (lmsg->ms_flags & MSGF_DONE)
1735 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1740 * Map interface name to
1741 * interface structure pointer.
1744 ifunit(const char *name)
1749 * Search all the interfaces for this name/number
1751 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1753 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1754 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1761 ifunit_netisr(const char *name)
1763 const struct ifnet_array *arr;
1767 * Search all the interfaces for this name/number
1770 arr = ifnet_array_get();
1771 for (i = 0; i < arr->ifnet_count; ++i) {
1772 struct ifnet *ifp = arr->ifnet_arr[i];
1774 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1784 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1789 int error, do_ifup = 0;
1792 size_t namelen, onamelen;
1793 char new_name[IFNAMSIZ];
1795 struct sockaddr_dl *sdl;
1800 return (ifconf(cmd, data, cred));
1805 ifr = (struct ifreq *)data;
1810 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1812 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1813 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1815 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1817 return (if_clone_destroy(ifr->ifr_name));
1818 case SIOCIFGCLONERS:
1819 return (if_clone_list((struct if_clonereq *)data));
1825 * Nominal ioctl through interface, lookup the ifp and obtain a
1826 * lock to serialize the ifconfig ioctl operation.
1830 ifp = ifunit(ifr->ifr_name);
1839 ifr->ifr_index = ifp->if_index;
1843 ifr->ifr_flags = ifp->if_flags;
1844 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1848 ifr->ifr_reqcap = ifp->if_capabilities;
1849 ifr->ifr_curcap = ifp->if_capenable;
1853 ifr->ifr_metric = ifp->if_metric;
1857 ifr->ifr_mtu = ifp->if_mtu;
1861 ifr->ifr_tsolen = ifp->if_tsolen;
1865 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1866 sizeof(ifp->if_data));
1870 ifr->ifr_phys = ifp->if_physical;
1873 case SIOCGIFPOLLCPU:
1874 ifr->ifr_pollcpu = -1;
1877 case SIOCSIFPOLLCPU:
1881 error = priv_check_cred(cred, PRIV_ROOT, 0);
1884 new_flags = (ifr->ifr_flags & 0xffff) |
1885 (ifr->ifr_flagshigh << 16);
1886 if (ifp->if_flags & IFF_SMART) {
1887 /* Smart drivers twiddle their own routes */
1888 } else if (ifp->if_flags & IFF_UP &&
1889 (new_flags & IFF_UP) == 0) {
1891 } else if (new_flags & IFF_UP &&
1892 (ifp->if_flags & IFF_UP) == 0) {
1896 #ifdef IFPOLL_ENABLE
1897 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1898 if (new_flags & IFF_NPOLLING)
1899 ifpoll_register(ifp);
1901 ifpoll_deregister(ifp);
1905 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1906 (new_flags &~ IFF_CANTCHANGE);
1907 if (new_flags & IFF_PPROMISC) {
1908 /* Permanently promiscuous mode requested */
1909 ifp->if_flags |= IFF_PROMISC;
1910 } else if (ifp->if_pcount == 0) {
1911 ifp->if_flags &= ~IFF_PROMISC;
1913 if (ifp->if_ioctl) {
1914 ifnet_serialize_all(ifp);
1915 ifp->if_ioctl(ifp, cmd, data, cred);
1916 ifnet_deserialize_all(ifp);
1920 getmicrotime(&ifp->if_lastchange);
1924 error = priv_check_cred(cred, PRIV_ROOT, 0);
1927 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1931 ifnet_serialize_all(ifp);
1932 ifp->if_ioctl(ifp, cmd, data, cred);
1933 ifnet_deserialize_all(ifp);
1937 error = priv_check_cred(cred, PRIV_ROOT, 0);
1940 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1943 if (new_name[0] == '\0') {
1947 if (ifunit(new_name) != NULL) {
1952 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1954 /* Announce the departure of the interface. */
1955 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1957 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1958 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1959 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1960 namelen = strlen(new_name);
1961 onamelen = sdl->sdl_nlen;
1963 * Move the address if needed. This is safe because we
1964 * allocate space for a name of length IFNAMSIZ when we
1965 * create this in if_attach().
1967 if (namelen != onamelen) {
1968 bcopy(sdl->sdl_data + onamelen,
1969 sdl->sdl_data + namelen, sdl->sdl_alen);
1971 bcopy(new_name, sdl->sdl_data, namelen);
1972 sdl->sdl_nlen = namelen;
1973 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1974 bzero(sdl->sdl_data, onamelen);
1975 while (namelen != 0)
1976 sdl->sdl_data[--namelen] = 0xff;
1978 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
1980 /* Announce the return of the interface. */
1981 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
1985 error = priv_check_cred(cred, PRIV_ROOT, 0);
1988 ifp->if_metric = ifr->ifr_metric;
1989 getmicrotime(&ifp->if_lastchange);
1993 error = priv_check_cred(cred, PRIV_ROOT, 0);
1996 if (ifp->if_ioctl == NULL) {
2000 ifnet_serialize_all(ifp);
2001 error = ifp->if_ioctl(ifp, cmd, data, cred);
2002 ifnet_deserialize_all(ifp);
2004 getmicrotime(&ifp->if_lastchange);
2009 u_long oldmtu = ifp->if_mtu;
2011 error = priv_check_cred(cred, PRIV_ROOT, 0);
2014 if (ifp->if_ioctl == NULL) {
2018 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2022 ifnet_serialize_all(ifp);
2023 error = ifp->if_ioctl(ifp, cmd, data, cred);
2024 ifnet_deserialize_all(ifp);
2026 getmicrotime(&ifp->if_lastchange);
2030 * If the link MTU changed, do network layer specific procedure.
2032 if (ifp->if_mtu != oldmtu) {
2041 error = priv_check_cred(cred, PRIV_ROOT, 0);
2045 /* XXX need driver supplied upper limit */
2046 if (ifr->ifr_tsolen <= 0) {
2050 ifp->if_tsolen = ifr->ifr_tsolen;
2055 error = priv_check_cred(cred, PRIV_ROOT, 0);
2059 /* Don't allow group membership on non-multicast interfaces. */
2060 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2065 /* Don't let users screw up protocols' entries. */
2066 if (ifr->ifr_addr.sa_family != AF_LINK) {
2071 if (cmd == SIOCADDMULTI) {
2072 struct ifmultiaddr *ifma;
2073 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2075 error = if_delmulti(ifp, &ifr->ifr_addr);
2078 getmicrotime(&ifp->if_lastchange);
2081 case SIOCSIFPHYADDR:
2082 case SIOCDIFPHYADDR:
2084 case SIOCSIFPHYADDR_IN6:
2086 case SIOCSLIFPHYADDR:
2088 case SIOCSIFGENERIC:
2089 error = priv_check_cred(cred, PRIV_ROOT, 0);
2092 if (ifp->if_ioctl == 0) {
2096 ifnet_serialize_all(ifp);
2097 error = ifp->if_ioctl(ifp, cmd, data, cred);
2098 ifnet_deserialize_all(ifp);
2100 getmicrotime(&ifp->if_lastchange);
2104 ifs = (struct ifstat *)data;
2105 ifs->ascii[0] = '\0';
2107 case SIOCGIFPSRCADDR:
2108 case SIOCGIFPDSTADDR:
2109 case SIOCGLIFPHYADDR:
2111 case SIOCGIFGENERIC:
2112 if (ifp->if_ioctl == NULL) {
2116 ifnet_serialize_all(ifp);
2117 error = ifp->if_ioctl(ifp, cmd, data, cred);
2118 ifnet_deserialize_all(ifp);
2122 error = priv_check_cred(cred, PRIV_ROOT, 0);
2125 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2126 ifr->ifr_addr.sa_len);
2127 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2131 oif_flags = ifp->if_flags;
2132 if (so->so_proto == 0) {
2136 error = so_pru_control_direct(so, cmd, data, ifp);
2138 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2140 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2141 if (ifp->if_flags & IFF_UP) {
2156 * Set/clear promiscuous mode on interface ifp based on the truth value
2157 * of pswitch. The calls are reference counted so that only the first
2158 * "on" request actually has an effect, as does the final "off" request.
2159 * Results are undefined if the "off" and "on" requests are not matched.
2162 ifpromisc(struct ifnet *ifp, int pswitch)
2168 oldflags = ifp->if_flags;
2169 if (ifp->if_flags & IFF_PPROMISC) {
2170 /* Do nothing if device is in permanently promiscuous mode */
2171 ifp->if_pcount += pswitch ? 1 : -1;
2176 * If the device is not configured up, we cannot put it in
2179 if ((ifp->if_flags & IFF_UP) == 0)
2181 if (ifp->if_pcount++ != 0)
2183 ifp->if_flags |= IFF_PROMISC;
2184 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2187 if (--ifp->if_pcount > 0)
2189 ifp->if_flags &= ~IFF_PROMISC;
2190 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2193 ifr.ifr_flags = ifp->if_flags;
2194 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2195 ifnet_serialize_all(ifp);
2196 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2197 ifnet_deserialize_all(ifp);
2201 ifp->if_flags = oldflags;
2206 * Return interface configuration
2207 * of system. List may be used
2208 * in later ioctl's (above) to get
2209 * other information.
2212 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2214 struct ifconf *ifc = (struct ifconf *)data;
2216 struct sockaddr *sa;
2217 struct ifreq ifr, *ifrp;
2218 int space = ifc->ifc_len, error = 0;
2220 ifrp = ifc->ifc_req;
2223 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2224 struct ifaddr_container *ifac, *ifac_mark;
2225 struct ifaddr_marker mark;
2226 struct ifaddrhead *head;
2229 if (space <= sizeof ifr)
2233 * Zero the stack declared structure first to prevent
2234 * memory disclosure.
2236 bzero(&ifr, sizeof(ifr));
2237 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2238 >= sizeof(ifr.ifr_name)) {
2239 error = ENAMETOOLONG;
2244 * Add a marker, since copyout() could block and during that
2245 * period the list could be changed. Inserting the marker to
2246 * the header of the list will not cause trouble for the code
2247 * assuming that the first element of the list is AF_LINK; the
2248 * marker will be moved to the next position w/o blocking.
2250 ifa_marker_init(&mark, ifp);
2251 ifac_mark = &mark.ifac;
2252 head = &ifp->if_addrheads[mycpuid];
2255 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2256 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2257 struct ifaddr *ifa = ifac->ifa;
2259 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2260 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2263 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2266 if (space <= sizeof ifr)
2269 if (cred->cr_prison &&
2270 prison_if(cred, sa))
2274 * Keep a reference on this ifaddr, so that it will
2275 * not be destroyed when its address is copied to
2276 * the userland, which could block.
2279 if (sa->sa_len <= sizeof(*sa)) {
2281 error = copyout(&ifr, ifrp, sizeof ifr);
2284 if (space < (sizeof ifr) + sa->sa_len -
2289 space -= sa->sa_len - sizeof(*sa);
2290 error = copyout(&ifr, ifrp,
2291 sizeof ifr.ifr_name);
2293 error = copyout(sa, &ifrp->ifr_addr,
2295 ifrp = (struct ifreq *)
2296 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2301 space -= sizeof ifr;
2303 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2307 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2308 error = copyout(&ifr, ifrp, sizeof ifr);
2311 space -= sizeof ifr;
2317 ifc->ifc_len -= space;
2322 * Just like if_promisc(), but for all-multicast-reception mode.
2325 if_allmulti(struct ifnet *ifp, int onswitch)
2333 if (ifp->if_amcount++ == 0) {
2334 ifp->if_flags |= IFF_ALLMULTI;
2335 ifr.ifr_flags = ifp->if_flags;
2336 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2337 ifnet_serialize_all(ifp);
2338 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2340 ifnet_deserialize_all(ifp);
2343 if (ifp->if_amcount > 1) {
2346 ifp->if_amcount = 0;
2347 ifp->if_flags &= ~IFF_ALLMULTI;
2348 ifr.ifr_flags = ifp->if_flags;
2349 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2350 ifnet_serialize_all(ifp);
2351 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2353 ifnet_deserialize_all(ifp);
2365 * Add a multicast listenership to the interface in question.
2366 * The link layer provides a routine which converts
2369 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2370 struct ifmultiaddr **retifma)
2372 struct sockaddr *llsa, *dupsa;
2374 struct ifmultiaddr *ifma;
2376 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2379 * If the matching multicast address already exists
2380 * then don't add a new one, just add a reference
2382 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2383 if (sa_equal(sa, ifma->ifma_addr)) {
2384 ifma->ifma_refcount++;
2392 * Give the link layer a chance to accept/reject it, and also
2393 * find out which AF_LINK address this maps to, if it isn't one
2396 if (ifp->if_resolvemulti) {
2397 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2404 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2405 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2406 bcopy(sa, dupsa, sa->sa_len);
2408 ifma->ifma_addr = dupsa;
2409 ifma->ifma_lladdr = llsa;
2410 ifma->ifma_ifp = ifp;
2411 ifma->ifma_refcount = 1;
2412 ifma->ifma_protospec = NULL;
2413 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2415 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2420 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2421 if (sa_equal(ifma->ifma_addr, llsa))
2425 ifma->ifma_refcount++;
2427 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2428 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2429 bcopy(llsa, dupsa, llsa->sa_len);
2430 ifma->ifma_addr = dupsa;
2431 ifma->ifma_ifp = ifp;
2432 ifma->ifma_refcount = 1;
2433 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2437 * We are certain we have added something, so call down to the
2438 * interface to let them know about it.
2441 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2447 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2448 struct ifmultiaddr **retifma)
2452 ifnet_serialize_all(ifp);
2453 error = if_addmulti_serialized(ifp, sa, retifma);
2454 ifnet_deserialize_all(ifp);
2460 * Remove a reference to a multicast address on this interface. Yell
2461 * if the request does not match an existing membership.
2464 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2466 struct ifmultiaddr *ifma;
2468 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2470 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2471 if (sa_equal(sa, ifma->ifma_addr))
2476 if (ifma->ifma_refcount > 1) {
2477 ifma->ifma_refcount--;
2481 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2482 sa = ifma->ifma_lladdr;
2483 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2485 * Make sure the interface driver is notified
2486 * in the case of a link layer mcast group being left.
2488 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2489 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2490 kfree(ifma->ifma_addr, M_IFMADDR);
2491 kfree(ifma, M_IFMADDR);
2496 * Now look for the link-layer address which corresponds to
2497 * this network address. It had been squirreled away in
2498 * ifma->ifma_lladdr for this purpose (so we don't have
2499 * to call ifp->if_resolvemulti() again), and we saved that
2500 * value in sa above. If some nasty deleted the
2501 * link-layer address out from underneath us, we can deal because
2502 * the address we stored was is not the same as the one which was
2503 * in the record for the link-layer address. (So we don't complain
2506 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2507 if (sa_equal(sa, ifma->ifma_addr))
2512 if (ifma->ifma_refcount > 1) {
2513 ifma->ifma_refcount--;
2517 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2518 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2519 kfree(ifma->ifma_addr, M_IFMADDR);
2520 kfree(sa, M_IFMADDR);
2521 kfree(ifma, M_IFMADDR);
2527 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2531 ifnet_serialize_all(ifp);
2532 error = if_delmulti_serialized(ifp, sa);
2533 ifnet_deserialize_all(ifp);
2539 * Delete all multicast group membership for an interface.
2540 * Should be used to quickly flush all multicast filters.
2543 if_delallmulti_serialized(struct ifnet *ifp)
2545 struct ifmultiaddr *ifma, mark;
2548 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2550 bzero(&sa, sizeof(sa));
2551 sa.sa_family = AF_UNSPEC;
2552 sa.sa_len = sizeof(sa);
2554 bzero(&mark, sizeof(mark));
2555 mark.ifma_addr = &sa;
2557 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2558 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2559 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2560 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2563 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2566 if_delmulti_serialized(ifp, ifma->ifma_addr);
2568 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2573 * Set the link layer address on an interface.
2575 * At this time we only support certain types of interfaces,
2576 * and we don't allow the length of the address to change.
2579 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2581 struct sockaddr_dl *sdl;
2584 sdl = IF_LLSOCKADDR(ifp);
2587 if (len != sdl->sdl_alen) /* don't allow length to change */
2589 switch (ifp->if_type) {
2590 case IFT_ETHER: /* these types use struct arpcom */
2593 case IFT_IEEE8023ADLAG:
2594 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2595 bcopy(lladdr, LLADDR(sdl), len);
2601 * If the interface is already up, we need
2602 * to re-init it in order to reprogram its
2605 ifnet_serialize_all(ifp);
2606 if ((ifp->if_flags & IFF_UP) != 0) {
2608 struct ifaddr_container *ifac;
2611 ifp->if_flags &= ~IFF_UP;
2612 ifr.ifr_flags = ifp->if_flags;
2613 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2614 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2616 ifp->if_flags |= IFF_UP;
2617 ifr.ifr_flags = ifp->if_flags;
2618 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2619 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2623 * Also send gratuitous ARPs to notify other nodes about
2624 * the address change.
2626 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2627 struct ifaddr *ifa = ifac->ifa;
2629 if (ifa->ifa_addr != NULL &&
2630 ifa->ifa_addr->sa_family == AF_INET)
2631 arp_gratuitous(ifp, ifa);
2635 ifnet_deserialize_all(ifp);
2639 struct ifmultiaddr *
2640 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2642 struct ifmultiaddr *ifma;
2644 /* TODO: need ifnet_serialize_main */
2645 ifnet_serialize_all(ifp);
2646 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2647 if (sa_equal(ifma->ifma_addr, sa))
2649 ifnet_deserialize_all(ifp);
2655 * This function locates the first real ethernet MAC from a network
2656 * card and loads it into node, returning 0 on success or ENOENT if
2657 * no suitable interfaces were found. It is used by the uuid code to
2658 * generate a unique 6-byte number.
2661 if_getanyethermac(uint16_t *node, int minlen)
2664 struct sockaddr_dl *sdl;
2667 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2668 if (ifp->if_type != IFT_ETHER)
2670 sdl = IF_LLSOCKADDR(ifp);
2671 if (sdl->sdl_alen < minlen)
2673 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2683 * The name argument must be a pointer to storage which will last as
2684 * long as the interface does. For physical devices, the result of
2685 * device_get_name(dev) is a good choice and for pseudo-devices a
2686 * static string works well.
2689 if_initname(struct ifnet *ifp, const char *name, int unit)
2691 ifp->if_dname = name;
2692 ifp->if_dunit = unit;
2693 if (unit != IF_DUNIT_NONE)
2694 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2696 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2700 if_printf(struct ifnet *ifp, const char *fmt, ...)
2705 retval = kprintf("%s: ", ifp->if_xname);
2706 __va_start(ap, fmt);
2707 retval += kvprintf(fmt, ap);
2713 if_alloc(uint8_t type)
2719 * XXX temporary hack until arpcom is setup in if_l2com
2721 if (type == IFT_ETHER)
2722 size = sizeof(struct arpcom);
2724 size = sizeof(struct ifnet);
2726 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2728 ifp->if_type = type;
2730 if (if_com_alloc[type] != NULL) {
2731 ifp->if_l2com = if_com_alloc[type](type, ifp);
2732 if (ifp->if_l2com == NULL) {
2733 kfree(ifp, M_IFNET);
2741 if_free(struct ifnet *ifp)
2743 kfree(ifp, M_IFNET);
2747 ifq_set_classic(struct ifaltq *ifq)
2749 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2750 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2754 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2755 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2759 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2760 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2761 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2762 KASSERT(request != NULL, ("request is not specified"));
2764 ifq->altq_mapsubq = mapsubq;
2765 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2766 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2768 ifsq->ifsq_enqueue = enqueue;
2769 ifsq->ifsq_dequeue = dequeue;
2770 ifsq->ifsq_request = request;
2775 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2778 classq_add(&ifsq->ifsq_norm, m);
2779 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2783 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2786 classq_add(&ifsq->ifsq_prio, m);
2787 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2788 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2791 static struct mbuf *
2792 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2796 m = classq_get(&ifsq->ifsq_norm);
2798 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2802 static struct mbuf *
2803 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2807 m = classq_get(&ifsq->ifsq_prio);
2809 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2810 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2816 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2817 struct altq_pktattr *pa __unused)
2822 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2823 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2824 struct mbuf *m_drop;
2826 if (m->m_flags & M_PRIO) {
2828 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2829 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2830 /* Try dropping some from normal queue. */
2831 m_drop = ifsq_norm_dequeue(ifsq);
2834 m_drop = ifsq_prio_dequeue(ifsq);
2836 m_drop = ifsq_norm_dequeue(ifsq);
2838 if (m_drop != NULL) {
2839 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2844 * No old packets could be dropped!
2845 * NOTE: Caller increases oqdrops.
2850 if (m->m_flags & M_PRIO)
2851 ifsq_prio_enqueue(ifsq, m);
2853 ifsq_norm_enqueue(ifsq, m);
2859 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2865 m = classq_head(&ifsq->ifsq_prio);
2867 m = classq_head(&ifsq->ifsq_norm);
2871 m = ifsq_prio_dequeue(ifsq);
2873 m = ifsq_norm_dequeue(ifsq);
2877 panic("unsupported ALTQ dequeue op: %d", op);
2883 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2890 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2898 panic("unsupported ALTQ request: %d", req);
2904 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2906 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2907 int running = 0, need_sched;
2910 * Try to do direct ifnet.if_start on the subqueue first, if there is
2911 * contention on the subqueue hardware serializer, ifnet.if_start on
2912 * the subqueue will be scheduled on the subqueue owner CPU.
2914 if (!ifsq_tryserialize_hw(ifsq)) {
2916 * Subqueue hardware serializer contention happened,
2917 * ifnet.if_start on the subqueue is scheduled on
2918 * the subqueue owner CPU, and we keep going.
2920 ifsq_ifstart_schedule(ifsq, 1);
2924 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2925 ifp->if_start(ifp, ifsq);
2926 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2929 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2931 ifsq_deserialize_hw(ifsq);
2935 * More data need to be transmitted, ifnet.if_start on the
2936 * subqueue is scheduled on the subqueue owner CPU, and we
2938 * NOTE: ifnet.if_start subqueue interlock is not released.
2940 ifsq_ifstart_schedule(ifsq, force_sched);
2945 * Subqeue packets staging mechanism:
2947 * The packets enqueued into the subqueue are staged to a certain amount
2948 * before the ifnet.if_start on the subqueue is called. In this way, the
2949 * driver could avoid writing to hardware registers upon every packet,
2950 * instead, hardware registers could be written when certain amount of
2951 * packets are put onto hardware TX ring. The measurement on several modern
2952 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2953 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2954 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2955 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2956 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2958 * Subqueue packets staging is performed for two entry points into drivers'
2959 * transmission function:
2960 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2961 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2963 * Subqueue packets staging will be stopped upon any of the following
2965 * - If the count of packets enqueued on the current CPU is great than or
2966 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2967 * - If the total length of packets enqueued on the current CPU is great
2968 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2969 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2970 * is usually less than hardware's MTU.
2971 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2972 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2974 * - The if_start_rollup(), which is registered as low priority netisr
2975 * rollup function, is called; probably because no more work is pending
2979 * Currently subqueue packet staging is only performed in netisr threads.
2982 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
2984 struct ifaltq *ifq = &ifp->if_snd;
2985 struct ifaltq_subque *ifsq;
2986 int error, start = 0, len, mcast = 0, avoid_start = 0;
2987 struct ifsubq_stage_head *head = NULL;
2988 struct ifsubq_stage *stage = NULL;
2989 struct globaldata *gd = mycpu;
2990 struct thread *td = gd->gd_curthread;
2992 crit_enter_quick(td);
2994 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
2995 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
2997 len = m->m_pkthdr.len;
2998 if (m->m_flags & M_MCAST)
3001 if (td->td_type == TD_TYPE_NETISR) {
3002 head = &ifsubq_stage_heads[mycpuid];
3003 stage = ifsq_get_stage(ifsq, mycpuid);
3006 stage->stg_len += len;
3007 if (stage->stg_cnt < ifsq_stage_cntmax &&
3008 stage->stg_len < (ifp->if_mtu - max_protohdr))
3013 error = ifsq_enqueue_locked(ifsq, m, pa);
3015 IFNET_STAT_INC(ifp, oqdrops, 1);
3016 if (!ifsq_data_ready(ifsq)) {
3017 ALTQ_SQ_UNLOCK(ifsq);
3018 crit_exit_quick(td);
3023 if (!ifsq_is_started(ifsq)) {
3025 ALTQ_SQ_UNLOCK(ifsq);
3028 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3029 ifsq_stage_insert(head, stage);
3031 IFNET_STAT_INC(ifp, obytes, len);
3033 IFNET_STAT_INC(ifp, omcasts, 1);
3034 crit_exit_quick(td);
3039 * Hold the subqueue interlock of ifnet.if_start
3041 ifsq_set_started(ifsq);
3044 ALTQ_SQ_UNLOCK(ifsq);
3047 IFNET_STAT_INC(ifp, obytes, len);
3049 IFNET_STAT_INC(ifp, omcasts, 1);
3052 if (stage != NULL) {
3053 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3054 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3056 ifsq_stage_remove(head, stage);
3057 ifsq_ifstart_schedule(ifsq, 1);
3059 crit_exit_quick(td);
3063 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3064 ifsq_stage_remove(head, stage);
3072 crit_exit_quick(td);
3076 ifsq_ifstart_try(ifsq, 0);
3078 crit_exit_quick(td);
3083 ifa_create(int size)
3088 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3090 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3091 ifa->ifa_containers =
3092 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3093 M_IFADDR, M_INTWAIT | M_ZERO);
3095 ifa->ifa_ncnt = ncpus;
3096 for (i = 0; i < ncpus; ++i) {
3097 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3099 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3101 ifac->ifa_refcnt = 1;
3104 kprintf("alloc ifa %p %d\n", ifa, size);
3110 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3112 struct ifaddr *ifa = ifac->ifa;
3114 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3115 KKASSERT(ifac->ifa_refcnt == 0);
3116 KASSERT(ifac->ifa_listmask == 0,
3117 ("ifa is still on %#x lists", ifac->ifa_listmask));
3119 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3121 #ifdef IFADDR_DEBUG_VERBOSE
3122 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3125 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3126 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3127 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3129 kprintf("free ifa %p\n", ifa);
3131 kfree(ifa->ifa_containers, M_IFADDR);
3132 kfree(ifa, M_IFADDR);
3137 ifa_iflink_dispatch(netmsg_t nmsg)
3139 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3140 struct ifaddr *ifa = msg->ifa;
3141 struct ifnet *ifp = msg->ifp;
3143 struct ifaddr_container *ifac;
3147 ifac = &ifa->ifa_containers[cpu];
3148 ASSERT_IFAC_VALID(ifac);
3149 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3150 ("ifaddr is on if_addrheads"));
3152 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3154 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3156 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3160 netisr_forwardmsg(&nmsg->base, cpu + 1);
3164 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3166 struct netmsg_ifaddr msg;
3168 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3169 0, ifa_iflink_dispatch);
3174 netisr_domsg(&msg.base, 0);
3178 ifa_ifunlink_dispatch(netmsg_t nmsg)
3180 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3181 struct ifaddr *ifa = msg->ifa;
3182 struct ifnet *ifp = msg->ifp;
3184 struct ifaddr_container *ifac;
3188 ifac = &ifa->ifa_containers[cpu];
3189 ASSERT_IFAC_VALID(ifac);
3190 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3191 ("ifaddr is not on if_addrhead"));
3193 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3194 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3198 netisr_forwardmsg(&nmsg->base, cpu + 1);
3202 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3204 struct netmsg_ifaddr msg;
3206 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3207 0, ifa_ifunlink_dispatch);
3211 netisr_domsg(&msg.base, 0);
3215 ifa_destroy_dispatch(netmsg_t nmsg)
3217 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3220 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3224 ifa_destroy(struct ifaddr *ifa)
3226 struct netmsg_ifaddr msg;
3228 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3229 0, ifa_destroy_dispatch);
3232 netisr_domsg(&msg.base, 0);
3236 if_start_rollup(void)
3238 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3239 struct ifsubq_stage *stage;
3243 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3244 struct ifaltq_subque *ifsq = stage->stg_subq;
3247 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3249 ifsq_stage_remove(head, stage);
3252 ifsq_ifstart_schedule(ifsq, 1);
3257 if (!ifsq_is_started(ifsq)) {
3259 * Hold the subqueue interlock of
3262 ifsq_set_started(ifsq);
3265 ALTQ_SQ_UNLOCK(ifsq);
3268 ifsq_ifstart_try(ifsq, 1);
3270 KKASSERT((stage->stg_flags &
3271 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3278 ifnetinit(void *dummy __unused)
3282 for (i = 0; i < ncpus; ++i)
3283 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3284 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3288 if_register_com_alloc(u_char type,
3289 if_com_alloc_t *a, if_com_free_t *f)
3292 KASSERT(if_com_alloc[type] == NULL,
3293 ("if_register_com_alloc: %d already registered", type));
3294 KASSERT(if_com_free[type] == NULL,
3295 ("if_register_com_alloc: %d free already registered", type));
3297 if_com_alloc[type] = a;
3298 if_com_free[type] = f;
3302 if_deregister_com_alloc(u_char type)
3305 KASSERT(if_com_alloc[type] != NULL,
3306 ("if_deregister_com_alloc: %d not registered", type));
3307 KASSERT(if_com_free[type] != NULL,
3308 ("if_deregister_com_alloc: %d free not registered", type));
3309 if_com_alloc[type] = NULL;
3310 if_com_free[type] = NULL;
3314 ifq_set_maxlen(struct ifaltq *ifq, int len)
3316 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3320 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3322 return ALTQ_SUBQ_INDEX_DEFAULT;
3326 ifq_mapsubq_mask(struct ifaltq *ifq, int cpuid)
3329 return (cpuid & ifq->altq_subq_mappriv);
3333 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3336 return (cpuid % ifq->altq_subq_mappriv);
3340 ifsq_watchdog(void *arg)
3342 struct ifsubq_watchdog *wd = arg;
3345 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3348 ifp = ifsq_get_ifp(wd->wd_subq);
3349 if (ifnet_tryserialize_all(ifp)) {
3350 wd->wd_watchdog(wd->wd_subq);
3351 ifnet_deserialize_all(ifp);
3353 /* try again next timeout */
3357 ifsq_watchdog_reset(wd);
3361 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3363 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3364 ifsq_get_cpuid(wd->wd_subq));
3368 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3369 ifsq_watchdog_t watchdog)
3371 callout_init_mp(&wd->wd_callout);
3374 wd->wd_watchdog = watchdog;
3378 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3381 ifsq_watchdog_reset(wd);
3385 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3388 callout_stop(&wd->wd_callout);
3394 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3395 ("try holding ifnet lock in netisr"));
3396 mtx_lock(&ifnet_mtx);
3402 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3403 ("try holding ifnet lock in netisr"));
3404 mtx_unlock(&ifnet_mtx);
3407 static struct ifnet_array *
3408 ifnet_array_alloc(int count)
3410 struct ifnet_array *arr;
3412 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3414 arr->ifnet_count = count;
3420 ifnet_array_free(struct ifnet_array *arr)
3422 if (arr == &ifnet_array0)
3424 kfree(arr, M_IFNET);
3427 static struct ifnet_array *
3428 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3430 struct ifnet_array *arr;
3433 KASSERT(old_arr->ifnet_count >= 0,
3434 ("invalid ifnet array count %d", old_arr->ifnet_count));
3435 count = old_arr->ifnet_count + 1;
3436 arr = ifnet_array_alloc(count);
3439 * Save the old ifnet array and append this ifp to the end of
3440 * the new ifnet array.
3442 for (i = 0; i < old_arr->ifnet_count; ++i) {
3443 KASSERT(old_arr->ifnet_arr[i] != ifp,
3444 ("%s is already in ifnet array", ifp->if_xname));
3445 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3447 KASSERT(i == count - 1,
3448 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3449 ifp->if_xname, count - 1, i));
3450 arr->ifnet_arr[i] = ifp;
3455 static struct ifnet_array *
3456 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3458 struct ifnet_array *arr;
3459 int count, i, idx, found = 0;
3461 KASSERT(old_arr->ifnet_count > 0,
3462 ("invalid ifnet array count %d", old_arr->ifnet_count));
3463 count = old_arr->ifnet_count - 1;
3464 arr = ifnet_array_alloc(count);
3467 * Save the old ifnet array, but skip this ifp.
3470 for (i = 0; i < old_arr->ifnet_count; ++i) {
3471 if (old_arr->ifnet_arr[i] == ifp) {
3473 ("dup %s is in ifnet array", ifp->if_xname));
3477 KASSERT(idx < count,
3478 ("invalid ifnet array index %d, count %d", idx, count));
3479 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3482 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3483 KASSERT(idx == count,
3484 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3485 ifp->if_xname, count, idx));
3490 const struct ifnet_array *
3491 ifnet_array_get(void)
3493 const struct ifnet_array *ret;
3495 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3497 /* Make sure 'ret' is really used. */
3503 ifnet_array_isempty(void)
3505 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3506 if (ifnet_array->ifnet_count == 0)
3513 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3517 memset(mark, 0, sizeof(*mark));
3520 mark->ifac.ifa = ifa;
3522 ifa->ifa_addr = &mark->addr;
3523 ifa->ifa_dstaddr = &mark->dstaddr;
3524 ifa->ifa_netmask = &mark->netmask;
3529 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3532 KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3533 if (ring_cnt == 1 || ring_cntmax == 1 || netisr_ncpus == 1)
3536 if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3537 ring_cnt = ring_cntmax;
3538 if (ring_cnt > netisr_ncpus)
3539 ring_cnt = netisr_ncpus;
3544 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3548 KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3551 offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3552 for (i = 0; i < rm->rm_cnt; ++i) {
3553 rm->rm_cpumap[i] = offset + i;
3554 KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3555 ("invalid cpumap[%d] = %d, offset %d", i,
3556 rm->rm_cpumap[i], offset));
3561 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3563 struct if_ringmap *rm;
3566 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3567 rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3568 M_DEVBUF, M_WAITOK | M_ZERO);
3570 rm->rm_cnt = ring_cnt;
3571 for (i = 0; i < netisr_ncpus; ++i) {
3572 if (netisr_ncpus % (i + 1) != 0)
3575 if (rm->rm_cnt > netisr_ncpus / (i + 2)) {
3576 grid = netisr_ncpus / (i + 1);
3577 if (rm->rm_cnt > grid)
3582 if_ringmap_set_grid(dev, rm, grid);
3588 if_ringmap_free(struct if_ringmap *rm)
3591 kfree(rm, M_DEVBUF);
3595 * Align the two ringmaps.
3597 * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3601 * CPU 0 1 2 3 4 5 6 7
3602 * NIC_RX n0 n1 n2 n3
3607 * CPU 0 1 2 3 4 5 6 7
3608 * NIC_RX n0 n1 n2 n3
3612 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3615 if (rm0->rm_grid > rm1->rm_grid)
3616 if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3617 else if (rm0->rm_grid < rm1->rm_grid)
3618 if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3622 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3624 int subset_grid, cnt, divisor, mod, offset, i;
3625 struct if_ringmap *subset_rm, *rm;
3626 int old_rm0_grid, old_rm1_grid;
3628 if (rm0->rm_grid == rm1->rm_grid)
3631 /* Save grid for later use */
3632 old_rm0_grid = rm0->rm_grid;
3633 old_rm1_grid = rm1->rm_grid;
3635 if_ringmap_align(dev, rm0, rm1);
3638 * Re-shuffle rings to get more even distribution.
3640 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3642 * CPU 0 1 2 3 4 5 6 7 8 9 10 11
3644 * NIC_RX a0 a1 a2 a3 b0 b1 b2 b3 c0 c1 c2 c3
3645 * NIC_TX A0 A1 B0 B1 C0 C1
3647 * NIC_RX d0 d1 d2 d3 e0 e1 e2 e3 f0 f1 f2 f3
3648 * NIC_TX D0 D1 E0 E1 F0 F1
3651 if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3653 subset_grid = old_rm1_grid;
3656 } else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3658 subset_grid = old_rm0_grid;
3662 /* No space to shuffle. */
3666 mod = cnt / subset_grid;
3668 divisor = netisr_ncpus / rm->rm_grid;
3669 offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3671 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3672 subset_rm->rm_cpumap[i] += offset;
3673 KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3674 ("match: invalid cpumap[%d] = %d, offset %d",
3675 i, subset_rm->rm_cpumap[i], offset));
3678 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3681 for (j = 0; j < rm->rm_cnt; ++j) {
3682 if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3685 KASSERT(j < rm->rm_cnt,
3686 ("subset cpumap[%d] = %d not found in superset",
3687 i, subset_rm->rm_cpumap[i]));
3693 if_ringmap_count(const struct if_ringmap *rm)
3696 return (rm->rm_cnt);
3700 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3703 KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3704 return (rm->rm_cpumap[ring]);
3708 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3710 int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3712 KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3713 ("invalid redirect table entries %d", table_nent));
3716 for (i = 0; i < NETISR_CPUMAX; ++i) {
3717 table[i] = grid_idx++ % rm->rm_cnt;
3719 if (grid_idx == rm->rm_grid)
3724 * Make the ring distributed more evenly for the remainder
3727 * e.g. 12 netisrs, rm contains 8 rings.
3729 * Redirect table before:
3731 * 0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
3732 * 4 5 6 7 0 1 2 3 0 1 2 3 4 5 6 7
3733 * 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3
3736 * Redirect table after being patched (pX, patched entries):
3738 * 0 1 2 3 4 5 6 7 p0 p1 p2 p3 0 1 2 3
3739 * 4 5 6 7 p4 p5 p6 p7 0 1 2 3 4 5 6 7
3740 * p0 p1 p2 p3 0 1 2 3 4 5 6 7 p4 p5 p6 p7
3743 patch_cnt = rm->rm_grid % rm->rm_cnt;
3746 patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3748 grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3750 for (i = 0; i < grid_cnt; ++i) {
3753 for (j = 0; j < patch_cnt; ++j) {
3756 fix_idx = (i * rm->rm_grid) + patch_off + j;
3757 if (fix_idx >= NETISR_CPUMAX)
3759 table[fix_idx] = grid_idx++ % rm->rm_cnt;
3764 * If the device supports larger redirect table, duplicate
3765 * the first NETISR_CPUMAX entries to the rest of the table,
3766 * so that it matches upper layer's expectation:
3767 * (hash & NETISR_CPUMASK) % netisr_ncpus
3769 ncopy = table_nent / NETISR_CPUMAX;
3770 for (i = 1; i < ncopy; ++i) {
3771 memcpy(&table[i * NETISR_CPUMAX], table,
3772 NETISR_CPUMAX * sizeof(table[0]));
3774 if (if_ringmap_dumprdr) {
3775 for (i = 0; i < table_nent; ++i) {
3776 if (i != 0 && i % 16 == 0)
3778 kprintf("%03d ", table[i]);
3785 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
3787 struct if_ringmap *rm = arg1;
3790 for (i = 0; i < rm->rm_cnt; ++i) {
3791 int cpu = rm->rm_cpumap[i];
3793 error = SYSCTL_OUT(req, &cpu, sizeof(cpu));
3801 if_ring_count2(int ring_cnt, int ring_cntmax)
3804 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3805 return (1 << (fls(ring_cnt) - 1));