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>
51 #include <sys/sockio.h>
52 #include <sys/syslog.h>
53 #include <sys/sysctl.h>
54 #include <sys/domain.h>
55 #include <sys/thread.h>
56 #include <sys/serialize.h>
60 #include <sys/thread2.h>
61 #include <sys/msgport2.h>
62 #include <sys/mutex2.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_var.h>
69 #include <net/if_ringmap.h>
70 #include <net/ifq_var.h>
71 #include <net/radix.h>
72 #include <net/route.h>
73 #include <net/if_clone.h>
74 #include <net/netisr2.h>
75 #include <net/netmsg2.h>
77 #include <machine/atomic.h>
78 #include <machine/stdarg.h>
79 #include <machine/smp.h>
81 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet/if_ether.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet6/in6_ifattach.h>
89 #endif /* INET || INET6 */
91 struct netmsg_ifaddr {
92 struct netmsg_base base;
98 struct ifsubq_stage_head {
99 TAILQ_HEAD(, ifsubq_stage) stg_head;
108 #define RINGMAP_FLAG_NONE 0x0
109 #define RINGMAP_FLAG_POWEROF2 0x1
112 * System initialization
114 static void if_attachdomain(void *);
115 static void if_attachdomain1(struct ifnet *);
116 static int ifconf(u_long, caddr_t, struct ucred *);
117 static void ifinit(void *);
118 static void ifnetinit(void *);
119 static void if_slowtimo(void *);
120 static void link_rtrequest(int, struct rtentry *);
121 static int if_rtdel(struct radix_node *, void *);
122 static void if_slowtimo_dispatch(netmsg_t);
124 /* Helper functions */
125 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
126 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
127 static struct ifnet_array *ifnet_array_alloc(int);
128 static void ifnet_array_free(struct ifnet_array *);
129 static struct ifnet_array *ifnet_array_add(struct ifnet *,
130 const struct ifnet_array *);
131 static struct ifnet_array *ifnet_array_del(struct ifnet *,
132 const struct ifnet_array *);
133 static struct ifg_group *if_creategroup(const char *);
134 static int if_destroygroup(struct ifg_group *);
135 static int if_delgroup_locked(struct ifnet *, const char *);
136 static int if_getgroups(struct ifgroupreq *, struct ifnet *);
137 static int if_getgroupmembers(struct ifgroupreq *);
141 * XXX: declare here to avoid to include many inet6 related files..
142 * should be more generalized?
144 extern void nd6_setmtu(struct ifnet *);
147 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
148 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
149 SYSCTL_NODE(_net_link, OID_AUTO, ringmap, CTLFLAG_RW, 0, "link ringmap");
151 static int ifsq_stage_cntmax = 16;
152 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
153 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
154 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
156 static int if_stats_compat = 0;
157 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
158 &if_stats_compat, 0, "Compat the old ifnet stats");
160 static int if_ringmap_dumprdr = 0;
161 SYSCTL_INT(_net_link_ringmap, OID_AUTO, dump_rdr, CTLFLAG_RW,
162 &if_ringmap_dumprdr, 0, "dump redirect table");
164 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
165 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
167 static if_com_alloc_t *if_com_alloc[256];
168 static if_com_free_t *if_com_free[256];
170 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
171 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
172 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
174 int ifqmaxlen = IFQ_MAXLEN;
175 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
176 struct ifgrouphead ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
177 static struct lock ifgroup_lock;
179 static struct ifnet_array ifnet_array0;
180 static struct ifnet_array *ifnet_array = &ifnet_array0;
182 static struct callout if_slowtimo_timer;
183 static struct netmsg_base if_slowtimo_netmsg;
186 struct ifnet **ifindex2ifnet = NULL;
187 static struct mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
189 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
192 #define IFQ_KTR_STRING "ifq=%p"
193 #define IFQ_KTR_ARGS struct ifaltq *ifq
195 #define KTR_IFQ KTR_ALL
197 KTR_INFO_MASTER(ifq);
198 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
199 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
200 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
202 #define IF_START_KTR_STRING "ifp=%p"
203 #define IF_START_KTR_ARGS struct ifnet *ifp
205 #define KTR_IF_START KTR_ALL
207 KTR_INFO_MASTER(if_start);
208 KTR_INFO(KTR_IF_START, if_start, run, 0,
209 IF_START_KTR_STRING, IF_START_KTR_ARGS);
210 KTR_INFO(KTR_IF_START, if_start, sched, 1,
211 IF_START_KTR_STRING, IF_START_KTR_ARGS);
212 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
213 IF_START_KTR_STRING, IF_START_KTR_ARGS);
214 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
215 IF_START_KTR_STRING, IF_START_KTR_ARGS);
216 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
217 IF_START_KTR_STRING, IF_START_KTR_ARGS);
218 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
222 * Network interface utility routines.
224 * Routines with ifa_ifwith* names take sockaddr *'s as
231 lockinit(&ifgroup_lock, "ifgroup", 0, 0);
233 callout_init_mp(&if_slowtimo_timer);
234 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
235 MSGF_PRIORITY, if_slowtimo_dispatch);
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;
482 static int if_indexlim = 8;
484 if (ifp->if_serialize != NULL) {
485 KASSERT(ifp->if_deserialize != NULL &&
486 ifp->if_tryserialize != NULL &&
487 ifp->if_serialize_assert != NULL,
488 ("serialize functions are partially setup"));
491 * If the device supplies serialize functions,
492 * then clear if_serializer to catch any invalid
493 * usage of this field.
495 KASSERT(serializer == NULL,
496 ("both serialize functions and default serializer "
498 ifp->if_serializer = NULL;
500 KASSERT(ifp->if_deserialize == NULL &&
501 ifp->if_tryserialize == NULL &&
502 ifp->if_serialize_assert == NULL,
503 ("serialize functions are partially setup"));
504 ifp->if_serialize = if_default_serialize;
505 ifp->if_deserialize = if_default_deserialize;
506 ifp->if_tryserialize = if_default_tryserialize;
508 ifp->if_serialize_assert = if_default_serialize_assert;
512 * The serializer can be passed in from the device,
513 * allowing the same serializer to be used for both
514 * the interrupt interlock and the device queue.
515 * If not specified, the netif structure will use an
516 * embedded serializer.
518 if (serializer == NULL) {
519 serializer = &ifp->if_default_serializer;
520 lwkt_serialize_init(serializer);
522 ifp->if_serializer = serializer;
526 * Make if_addrhead available on all CPUs, since they
527 * could be accessed by any threads.
529 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
530 M_IFADDR, M_WAITOK | M_ZERO);
531 for (i = 0; i < ncpus; ++i)
532 TAILQ_INIT(&ifp->if_addrheads[i]);
534 TAILQ_INIT(&ifp->if_multiaddrs);
535 TAILQ_INIT(&ifp->if_groups);
536 getmicrotime(&ifp->if_lastchange);
537 if_addgroup(ifp, IFG_ALL);
540 * create a Link Level name for this device
542 namelen = strlen(ifp->if_xname);
543 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
544 socksize = masklen + ifp->if_addrlen;
545 if (socksize < sizeof(*sdl))
546 socksize = sizeof(*sdl);
547 socksize = RT_ROUNDUP(socksize);
548 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
549 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
550 sdl->sdl_len = socksize;
551 sdl->sdl_family = AF_LINK;
552 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
553 sdl->sdl_nlen = namelen;
554 sdl->sdl_type = ifp->if_type;
555 ifp->if_lladdr = ifa;
557 ifa->ifa_rtrequest = link_rtrequest;
558 ifa->ifa_addr = (struct sockaddr *)sdl;
559 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
560 ifa->ifa_netmask = (struct sockaddr *)sdl;
561 sdl->sdl_len = masklen;
563 sdl->sdl_data[--namelen] = 0xff;
564 ifa_iflink(ifa, ifp, 0 /* Insert head */);
567 * Make if_data available on all CPUs, since they could
568 * be updated by hardware interrupt routing, which could
569 * be bound to any CPU.
571 ifp->if_data_pcpu = kmalloc(ncpus * sizeof(struct ifdata_pcpu),
573 M_WAITOK | M_ZERO | M_CACHEALIGN);
575 if (ifp->if_mapsubq == NULL)
576 ifp->if_mapsubq = ifq_mapsubq_default;
580 ifq->altq_disc = NULL;
581 ifq->altq_flags &= ALTQF_CANTCHANGE;
582 ifq->altq_tbr = NULL;
585 if (ifq->altq_subq_cnt <= 0)
586 ifq->altq_subq_cnt = 1;
588 kmalloc(ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
590 M_WAITOK | M_ZERO | M_CACHEALIGN);
592 if (ifq->altq_maxlen == 0) {
593 if_printf(ifp, "driver didn't set altq_maxlen\n");
594 ifq_set_maxlen(ifq, ifqmaxlen);
597 /* Allow user to override driver's setting. */
598 ksnprintf(qlenname, sizeof(qlenname), "net.%s.qlenmax", ifp->if_xname);
600 TUNABLE_INT_FETCH(qlenname, &qlen);
602 if_printf(ifp, "qlenmax -> %d\n", qlen);
603 ifq_set_maxlen(ifq, qlen);
606 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
607 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
609 ALTQ_SQ_LOCK_INIT(ifsq);
610 ifsq->ifsq_index = q;
612 ifsq->ifsq_altq = ifq;
613 ifsq->ifsq_ifp = ifp;
615 ifsq->ifsq_maxlen = ifq->altq_maxlen;
616 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
617 ifsq->ifsq_prepended = NULL;
618 ifsq->ifsq_started = 0;
619 ifsq->ifsq_hw_oactive = 0;
620 ifsq_set_cpuid(ifsq, 0);
621 if (ifp->if_serializer != NULL)
622 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
624 /* XXX: netisr_ncpus */
626 kmalloc(ncpus * sizeof(struct ifsubq_stage),
628 M_WAITOK | M_ZERO | M_CACHEALIGN);
629 for (i = 0; i < ncpus; ++i)
630 ifsq->ifsq_stage[i].stg_subq = ifsq;
633 * Allocate one if_start message for each CPU, since
634 * the hardware TX ring could be assigned to any CPU.
637 * If the hardware TX ring polling CPU and the hardware
638 * TX ring interrupt CPU are same, one if_start message
641 ifsq->ifsq_ifstart_nmsg =
642 kmalloc(ncpus * sizeof(struct netmsg_base),
643 M_LWKTMSG, M_WAITOK);
644 for (i = 0; i < ncpus; ++i) {
645 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
646 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
647 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
650 ifq_set_classic(ifq);
653 * Increase mbuf cluster/jcluster limits for the mbufs that
654 * could sit on the device queues for quite some time.
656 if (ifp->if_nmbclusters > 0)
657 mcl_inclimit(ifp->if_nmbclusters);
658 if (ifp->if_nmbjclusters > 0)
659 mjcl_inclimit(ifp->if_nmbjclusters);
662 * Install this ifp into ifindex2inet, ifnet queue and ifnet
663 * array after it is setup.
665 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
666 * by ifnet lock, so that non-netisr threads could get a
671 /* Don't update if_index until ifindex2ifnet is setup */
672 ifp->if_index = if_index + 1;
673 sdl_addr->sdl_index = ifp->if_index;
676 * Install this ifp into ifindex2ifnet
678 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
686 n = if_indexlim * sizeof(*q);
687 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
688 if (ifindex2ifnet != NULL) {
689 bcopy(ifindex2ifnet, q, n/2);
690 /* Free old ifindex2ifnet after sync all netisrs */
691 old_ifindex2ifnet = ifindex2ifnet;
695 ifindex2ifnet[ifp->if_index] = ifp;
697 * Update if_index after this ifp is installed into ifindex2ifnet,
698 * so that netisrs could get a consistent view of ifindex2ifnet.
701 if_index = ifp->if_index;
704 * Install this ifp into ifnet array.
706 /* Free old ifnet array after sync all netisrs */
707 old_ifnet_array = ifnet_array;
708 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
711 * Install this ifp into ifnet queue.
713 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
718 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
719 * are no longer accessed and we can free them safely later on.
721 netmsg_service_sync();
722 if (old_ifindex2ifnet != NULL)
723 kfree(old_ifindex2ifnet, M_IFADDR);
724 ifnet_array_free(old_ifnet_array);
726 if (!SLIST_EMPTY(&domains))
727 if_attachdomain1(ifp);
729 /* Announce the interface. */
730 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
731 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
732 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
736 if_attachdomain(void *dummy)
741 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
742 if_attachdomain1(ifp);
745 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
746 if_attachdomain, NULL);
749 if_attachdomain1(struct ifnet *ifp)
755 /* address family dependent data region */
756 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
757 SLIST_FOREACH(dp, &domains, dom_next)
758 if (dp->dom_ifattach)
759 ifp->if_afdata[dp->dom_family] =
760 (*dp->dom_ifattach)(ifp);
765 * Purge all addresses whose type is _not_ AF_LINK
768 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
770 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
771 struct ifaddr_container *ifac, *next;
776 * The ifaddr processing in the following loop will block,
777 * however, this function is called in netisr0, in which
778 * ifaddr list changes happen, so we don't care about the
779 * blockness of the ifaddr processing here.
781 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
783 struct ifaddr *ifa = ifac->ifa;
786 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
789 /* Leave link ifaddr as it is */
790 if (ifa->ifa_addr->sa_family == AF_LINK)
793 /* XXX: Ugly!! ad hoc just for INET */
794 if (ifa->ifa_addr->sa_family == AF_INET) {
795 struct ifaliasreq ifr;
796 struct sockaddr_in saved_addr, saved_dst;
797 #ifdef IFADDR_DEBUG_VERBOSE
800 kprintf("purge in4 addr %p: ", ifa);
801 for (i = 0; i < ncpus; ++i) {
803 ifa->ifa_containers[i].ifa_refcnt);
808 /* Save information for panic. */
809 memcpy(&saved_addr, ifa->ifa_addr, sizeof(saved_addr));
810 if (ifa->ifa_dstaddr != NULL) {
811 memcpy(&saved_dst, ifa->ifa_dstaddr,
814 memset(&saved_dst, 0, sizeof(saved_dst));
817 bzero(&ifr, sizeof ifr);
818 ifr.ifra_addr = *ifa->ifa_addr;
819 if (ifa->ifa_dstaddr)
820 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
821 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
825 /* MUST NOT HAPPEN */
826 panic("%s: in_control failed %x, dst %x", ifp->if_xname,
827 ntohl(saved_addr.sin_addr.s_addr),
828 ntohl(saved_dst.sin_addr.s_addr));
832 if (ifa->ifa_addr->sa_family == AF_INET6) {
833 #ifdef IFADDR_DEBUG_VERBOSE
836 kprintf("purge in6 addr %p: ", ifa);
837 for (i = 0; i < ncpus; ++i) {
839 ifa->ifa_containers[i].ifa_refcnt);
845 /* ifp_addrhead is already updated */
849 if_printf(ifp, "destroy ifaddr family %d\n",
850 ifa->ifa_addr->sa_family);
851 ifa_ifunlink(ifa, ifp);
855 netisr_replymsg(&nmsg->base, 0);
859 if_purgeaddrs_nolink(struct ifnet *ifp)
861 struct netmsg_base nmsg;
863 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
864 if_purgeaddrs_nolink_dispatch);
865 nmsg.lmsg.u.ms_resultp = ifp;
866 netisr_domsg(&nmsg, 0);
870 ifq_stage_detach_handler(netmsg_t nmsg)
872 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
875 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
876 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
877 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
879 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
880 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
882 lwkt_replymsg(&nmsg->lmsg, 0);
886 ifq_stage_detach(struct ifaltq *ifq)
888 struct netmsg_base base;
891 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
892 ifq_stage_detach_handler);
893 base.lmsg.u.ms_resultp = ifq;
895 /* XXX netisr_ncpus */
896 for (cpu = 0; cpu < ncpus; ++cpu)
897 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
900 struct netmsg_if_rtdel {
901 struct netmsg_base base;
906 if_rtdel_dispatch(netmsg_t msg)
908 struct netmsg_if_rtdel *rmsg = (void *)msg;
912 ASSERT_NETISR_NCPUS(cpu);
914 for (i = 1; i <= AF_MAX; i++) {
915 struct radix_node_head *rnh;
917 if ((rnh = rt_tables[cpu][i]) == NULL)
919 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
921 netisr_forwardmsg(&msg->base, cpu + 1);
925 * Detach an interface, removing it from the
926 * list of "active" interfaces.
929 if_detach(struct ifnet *ifp)
931 struct ifnet_array *old_ifnet_array;
932 struct ifg_list *ifgl;
933 struct netmsg_if_rtdel msg;
937 /* Announce that the interface is gone. */
938 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
939 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
940 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
943 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
944 * array before it is whacked.
946 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
947 * by ifnet lock, so that non-netisr threads could get a
953 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
955 ifindex2ifnet[ifp->if_index] = NULL;
956 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
960 * Remove this ifp from ifnet queue.
962 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
965 * Remove this ifp from ifnet array.
967 /* Free old ifnet array after sync all netisrs */
968 old_ifnet_array = ifnet_array;
969 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
973 ifgroup_lockmgr(LK_EXCLUSIVE);
974 while ((ifgl = TAILQ_FIRST(&ifp->if_groups)) != NULL)
975 if_delgroup_locked(ifp, ifgl->ifgl_group->ifg_group);
976 ifgroup_lockmgr(LK_RELEASE);
979 * Sync all netisrs so that the old ifnet array is no longer
980 * accessed and we can free it safely later on.
982 netmsg_service_sync();
983 ifnet_array_free(old_ifnet_array);
986 * Remove routes and flush queues.
990 if (ifp->if_flags & IFF_NPOLLING)
991 ifpoll_deregister(ifp);
995 /* Decrease the mbuf clusters/jclusters limits increased by us */
996 if (ifp->if_nmbclusters > 0)
997 mcl_inclimit(-ifp->if_nmbclusters);
998 if (ifp->if_nmbjclusters > 0)
999 mjcl_inclimit(-ifp->if_nmbjclusters);
1002 if (ifq_is_enabled(&ifp->if_snd))
1003 altq_disable(&ifp->if_snd);
1004 if (ifq_is_attached(&ifp->if_snd))
1005 altq_detach(&ifp->if_snd);
1009 * Clean up all addresses.
1011 ifp->if_lladdr = NULL;
1013 if_purgeaddrs_nolink(ifp);
1014 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1017 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1018 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
1019 ("non-link ifaddr is left on if_addrheads"));
1021 ifa_ifunlink(ifa, ifp);
1023 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1024 ("there are still ifaddrs left on if_addrheads"));
1029 * Remove all IPv4 kernel structures related to ifp.
1036 * Remove all IPv6 kernel structs related to ifp. This should be done
1037 * before removing routing entries below, since IPv6 interface direct
1038 * routes are expected to be removed by the IPv6-specific kernel API.
1039 * Otherwise, the kernel will detect some inconsistency and bark it.
1045 * Delete all remaining routes using this interface
1047 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1050 netisr_domsg_global(&msg.base);
1052 SLIST_FOREACH(dp, &domains, dom_next) {
1053 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1054 (*dp->dom_ifdetach)(ifp,
1055 ifp->if_afdata[dp->dom_family]);
1058 kfree(ifp->if_addrheads, M_IFADDR);
1060 lwkt_synchronize_ipiqs("if_detach");
1061 ifq_stage_detach(&ifp->if_snd);
1063 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1064 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1066 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1067 kfree(ifsq->ifsq_stage, M_DEVBUF);
1069 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1071 kfree(ifp->if_data_pcpu, M_DEVBUF);
1077 ifgroup_lockmgr(u_int flags)
1079 return lockmgr(&ifgroup_lock, flags);
1083 * Create an empty interface group.
1085 static struct ifg_group *
1086 if_creategroup(const char *groupname)
1088 struct ifg_group *ifg;
1090 ifg = kmalloc(sizeof(*ifg), M_IFNET, M_WAITOK);
1091 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1092 ifg->ifg_refcnt = 0;
1093 ifg->ifg_carp_demoted = 0;
1094 TAILQ_INIT(&ifg->ifg_members);
1096 ifgroup_lockmgr(LK_EXCLUSIVE);
1097 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1098 ifgroup_lockmgr(LK_RELEASE);
1100 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1106 * Destroy an empty interface group.
1109 if_destroygroup(struct ifg_group *ifg)
1111 KASSERT(ifg->ifg_refcnt == 0,
1112 ("trying to delete a non-empty interface group"));
1114 ifgroup_lockmgr(LK_EXCLUSIVE);
1115 TAILQ_REMOVE(&ifg_head, ifg, ifg_next);
1116 ifgroup_lockmgr(LK_RELEASE);
1118 EVENTHANDLER_INVOKE(group_detach_event, ifg);
1119 kfree(ifg, M_IFNET);
1125 * Add the interface to a group.
1126 * The target group will be created if it doesn't exist.
1129 if_addgroup(struct ifnet *ifp, const char *groupname)
1131 struct ifg_list *ifgl;
1132 struct ifg_group *ifg;
1133 struct ifg_member *ifgm;
1136 groupname[strlen(groupname) - 1] >= '0' &&
1137 groupname[strlen(groupname) - 1] <= '9')
1140 ifgroup_lockmgr(LK_SHARED);
1142 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1143 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0) {
1144 ifgroup_lockmgr(LK_RELEASE);
1149 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1150 if (strcmp(ifg->ifg_group, groupname) == 0)
1154 ifgroup_lockmgr(LK_RELEASE);
1157 ifg = if_creategroup(groupname);
1159 ifgl = kmalloc(sizeof(*ifgl), M_IFNET, M_WAITOK);
1160 ifgm = kmalloc(sizeof(*ifgm), M_IFNET, M_WAITOK);
1161 ifgl->ifgl_group = ifg;
1162 ifgm->ifgm_ifp = ifp;
1165 ifgroup_lockmgr(LK_EXCLUSIVE);
1166 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1167 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1168 ifgroup_lockmgr(LK_RELEASE);
1170 EVENTHANDLER_INVOKE(group_change_event, groupname);
1176 * Remove the interface from a group.
1177 * The group will be destroyed if it becomes empty.
1179 * The 'ifgroup_lock' must be hold exclusively when calling this.
1182 if_delgroup_locked(struct ifnet *ifp, const char *groupname)
1184 struct ifg_list *ifgl;
1185 struct ifg_member *ifgm;
1187 KKASSERT(lockstatus(&ifgroup_lock, curthread) == LK_EXCLUSIVE);
1189 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1190 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1196 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1198 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1199 if (ifgm->ifgm_ifp == ifp)
1204 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1206 ifgroup_lockmgr(LK_RELEASE);
1207 EVENTHANDLER_INVOKE(group_change_event, groupname);
1208 ifgroup_lockmgr(LK_EXCLUSIVE);
1210 kfree(ifgm, M_IFNET);
1211 ifgl->ifgl_group->ifg_refcnt--;
1214 if (ifgl->ifgl_group->ifg_refcnt == 0) {
1215 ifgroup_lockmgr(LK_RELEASE);
1216 if_destroygroup(ifgl->ifgl_group);
1217 ifgroup_lockmgr(LK_EXCLUSIVE);
1220 kfree(ifgl, M_IFNET);
1226 if_delgroup(struct ifnet *ifp, const char *groupname)
1230 ifgroup_lockmgr(LK_EXCLUSIVE);
1231 error = if_delgroup_locked(ifp, groupname);
1232 ifgroup_lockmgr(LK_RELEASE);
1238 * Store all the groups that the interface belongs to in memory
1239 * pointed to by data.
1242 if_getgroups(struct ifgroupreq *ifgr, struct ifnet *ifp)
1244 struct ifg_list *ifgl;
1245 struct ifg_req *ifgrq, *p;
1249 ifgroup_lockmgr(LK_SHARED);
1250 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1251 len += sizeof(struct ifg_req);
1252 ifgroup_lockmgr(LK_RELEASE);
1254 if (ifgr->ifgr_len == 0) {
1256 * Caller is asking how much memory should be allocated in
1257 * the next request in order to hold all the groups.
1259 ifgr->ifgr_len = len;
1261 } else if (ifgr->ifgr_len != len) {
1265 ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1269 ifgroup_lockmgr(LK_SHARED);
1271 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1272 if (len < sizeof(struct ifg_req)) {
1273 ifgroup_lockmgr(LK_RELEASE);
1277 strlcpy(p->ifgrq_group, ifgl->ifgl_group->ifg_group,
1278 sizeof(ifgrq->ifgrq_group));
1279 len -= sizeof(struct ifg_req);
1282 ifgroup_lockmgr(LK_RELEASE);
1284 error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1285 kfree(ifgrq, M_TEMP);
1293 * Store all the members of a group in memory pointed to by data.
1296 if_getgroupmembers(struct ifgroupreq *ifgr)
1298 struct ifg_group *ifg;
1299 struct ifg_member *ifgm;
1300 struct ifg_req *ifgrq, *p;
1303 ifgroup_lockmgr(LK_SHARED);
1305 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1306 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1310 ifgroup_lockmgr(LK_RELEASE);
1315 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1316 len += sizeof(struct ifg_req);
1318 ifgroup_lockmgr(LK_RELEASE);
1320 if (ifgr->ifgr_len == 0) {
1321 ifgr->ifgr_len = len;
1323 } else if (ifgr->ifgr_len != len) {
1327 ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1331 ifgroup_lockmgr(LK_SHARED);
1333 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1334 if (len < sizeof(struct ifg_req)) {
1335 ifgroup_lockmgr(LK_RELEASE);
1339 strlcpy(p->ifgrq_member, ifgm->ifgm_ifp->if_xname,
1340 sizeof(p->ifgrq_member));
1341 len -= sizeof(struct ifg_req);
1344 ifgroup_lockmgr(LK_RELEASE);
1346 error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1347 kfree(ifgrq, M_TEMP);
1355 * Delete Routes for a Network Interface
1357 * Called for each routing entry via the rnh->rnh_walktree() call above
1358 * to delete all route entries referencing a detaching network interface.
1361 * rn pointer to node in the routing table
1362 * arg argument passed to rnh->rnh_walktree() - detaching interface
1366 * errno failed - reason indicated
1370 if_rtdel(struct radix_node *rn, void *arg)
1372 struct rtentry *rt = (struct rtentry *)rn;
1373 struct ifnet *ifp = arg;
1376 if (rt->rt_ifp == ifp) {
1379 * Protect (sorta) against walktree recursion problems
1380 * with cloned routes
1382 if (!(rt->rt_flags & RTF_UP))
1385 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1386 rt_mask(rt), rt->rt_flags,
1389 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1396 static __inline boolean_t
1397 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1399 if (old_ifa == NULL)
1402 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1403 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1405 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1406 (cur_ifa->ifa_flags & IFA_ROUTE))
1412 * Locate an interface based on a complete address.
1415 ifa_ifwithaddr(struct sockaddr *addr)
1417 const struct ifnet_array *arr;
1420 arr = ifnet_array_get();
1421 for (i = 0; i < arr->ifnet_count; ++i) {
1422 struct ifnet *ifp = arr->ifnet_arr[i];
1423 struct ifaddr_container *ifac;
1425 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1426 struct ifaddr *ifa = ifac->ifa;
1428 if (ifa->ifa_addr->sa_family != addr->sa_family)
1430 if (sa_equal(addr, ifa->ifa_addr))
1432 if ((ifp->if_flags & IFF_BROADCAST) &&
1433 ifa->ifa_broadaddr &&
1434 /* IPv6 doesn't have broadcast */
1435 ifa->ifa_broadaddr->sa_len != 0 &&
1436 sa_equal(ifa->ifa_broadaddr, addr))
1444 * Locate the point to point interface with a given destination address.
1447 ifa_ifwithdstaddr(struct sockaddr *addr)
1449 const struct ifnet_array *arr;
1452 arr = ifnet_array_get();
1453 for (i = 0; i < arr->ifnet_count; ++i) {
1454 struct ifnet *ifp = arr->ifnet_arr[i];
1455 struct ifaddr_container *ifac;
1457 if (!(ifp->if_flags & IFF_POINTOPOINT))
1460 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1461 struct ifaddr *ifa = ifac->ifa;
1463 if (ifa->ifa_addr->sa_family != addr->sa_family)
1465 if (ifa->ifa_dstaddr &&
1466 sa_equal(addr, ifa->ifa_dstaddr))
1474 * Find an interface on a specific network. If many, choice
1475 * is most specific found.
1478 ifa_ifwithnet(struct sockaddr *addr)
1480 struct ifaddr *ifa_maybe = NULL;
1481 u_int af = addr->sa_family;
1482 char *addr_data = addr->sa_data, *cplim;
1483 const struct ifnet_array *arr;
1487 * AF_LINK addresses can be looked up directly by their index number,
1488 * so do that if we can.
1490 if (af == AF_LINK) {
1491 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1493 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1494 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1498 * Scan though each interface, looking for ones that have
1499 * addresses in this address family.
1501 arr = ifnet_array_get();
1502 for (i = 0; i < arr->ifnet_count; ++i) {
1503 struct ifnet *ifp = arr->ifnet_arr[i];
1504 struct ifaddr_container *ifac;
1506 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1507 struct ifaddr *ifa = ifac->ifa;
1508 char *cp, *cp2, *cp3;
1510 if (ifa->ifa_addr->sa_family != af)
1512 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1514 * This is a bit broken as it doesn't
1515 * take into account that the remote end may
1516 * be a single node in the network we are
1518 * The trouble is that we don't know the
1519 * netmask for the remote end.
1521 if (ifa->ifa_dstaddr != NULL &&
1522 sa_equal(addr, ifa->ifa_dstaddr))
1526 * if we have a special address handler,
1527 * then use it instead of the generic one.
1529 if (ifa->ifa_claim_addr) {
1530 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1538 * Scan all the bits in the ifa's address.
1539 * If a bit dissagrees with what we are
1540 * looking for, mask it with the netmask
1541 * to see if it really matters.
1542 * (A byte at a time)
1544 if (ifa->ifa_netmask == 0)
1547 cp2 = ifa->ifa_addr->sa_data;
1548 cp3 = ifa->ifa_netmask->sa_data;
1549 cplim = ifa->ifa_netmask->sa_len +
1550 (char *)ifa->ifa_netmask;
1552 if ((*cp++ ^ *cp2++) & *cp3++)
1553 goto next; /* next address! */
1555 * If the netmask of what we just found
1556 * is more specific than what we had before
1557 * (if we had one) then remember the new one
1558 * before continuing to search for an even
1559 * better one. If the netmasks are equal,
1560 * we prefer the this ifa based on the result
1563 if (ifa_maybe == NULL ||
1564 rn_refines((char *)ifa->ifa_netmask,
1565 (char *)ifa_maybe->ifa_netmask) ||
1566 (sa_equal(ifa_maybe->ifa_netmask,
1567 ifa->ifa_netmask) &&
1568 ifa_prefer(ifa, ifa_maybe)))
1577 * Find an interface address specific to an interface best matching
1581 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1583 struct ifaddr_container *ifac;
1584 char *cp, *cp2, *cp3;
1586 struct ifaddr *ifa_maybe = NULL;
1587 u_int af = addr->sa_family;
1591 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1592 struct ifaddr *ifa = ifac->ifa;
1594 if (ifa->ifa_addr->sa_family != af)
1596 if (ifa_maybe == NULL)
1598 if (ifa->ifa_netmask == NULL) {
1599 if (sa_equal(addr, ifa->ifa_addr) ||
1600 (ifa->ifa_dstaddr != NULL &&
1601 sa_equal(addr, ifa->ifa_dstaddr)))
1605 if (ifp->if_flags & IFF_POINTOPOINT) {
1606 if (sa_equal(addr, ifa->ifa_dstaddr))
1610 cp2 = ifa->ifa_addr->sa_data;
1611 cp3 = ifa->ifa_netmask->sa_data;
1612 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1613 for (; cp3 < cplim; cp3++)
1614 if ((*cp++ ^ *cp2++) & *cp3)
1624 * Default action when installing a route with a Link Level gateway.
1625 * Lookup an appropriate real ifa to point to.
1626 * This should be moved to /sys/net/link.c eventually.
1629 link_rtrequest(int cmd, struct rtentry *rt)
1632 struct sockaddr *dst;
1635 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1636 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1638 ifa = ifaof_ifpforaddr(dst, ifp);
1640 IFAFREE(rt->rt_ifa);
1643 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1644 ifa->ifa_rtrequest(cmd, rt);
1648 struct netmsg_ifroute {
1649 struct netmsg_base base;
1656 * Mark an interface down and notify protocols of the transition.
1659 if_unroute_dispatch(netmsg_t nmsg)
1661 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1662 struct ifnet *ifp = msg->ifp;
1663 int flag = msg->flag, fam = msg->fam;
1664 struct ifaddr_container *ifac;
1669 ifp->if_flags &= ~flag;
1670 getmicrotime(&ifp->if_lastchange);
1674 * The ifaddr processing in the following loop will block,
1675 * however, this function is called in netisr0, in which
1676 * ifaddr list changes happen, so we don't care about the
1677 * blockness of the ifaddr processing here.
1679 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1680 struct ifaddr *ifa = ifac->ifa;
1683 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1686 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1687 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1690 SLIST_FOREACH(dp, &domains, dom_next)
1691 if (dp->dom_if_down != NULL)
1692 dp->dom_if_down(ifp);
1694 ifq_purge_all(&ifp->if_snd);
1695 netisr_replymsg(&nmsg->base, 0);
1699 if_unroute(struct ifnet *ifp, int flag, int fam)
1701 struct netmsg_ifroute msg;
1703 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1704 if_unroute_dispatch);
1708 netisr_domsg(&msg.base, 0);
1712 * Mark an interface up and notify protocols of the transition.
1715 if_route_dispatch(netmsg_t nmsg)
1717 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1718 struct ifnet *ifp = msg->ifp;
1719 int flag = msg->flag, fam = msg->fam;
1720 struct ifaddr_container *ifac;
1725 ifq_purge_all(&ifp->if_snd);
1726 ifp->if_flags |= flag;
1727 getmicrotime(&ifp->if_lastchange);
1731 * The ifaddr processing in the following loop will block,
1732 * however, this function is called in netisr0, in which
1733 * ifaddr list changes happen, so we don't care about the
1734 * blockness of the ifaddr processing here.
1736 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1737 struct ifaddr *ifa = ifac->ifa;
1740 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1743 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1744 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1747 SLIST_FOREACH(dp, &domains, dom_next)
1748 if (dp->dom_if_up != NULL)
1751 netisr_replymsg(&nmsg->base, 0);
1755 if_route(struct ifnet *ifp, int flag, int fam)
1757 struct netmsg_ifroute msg;
1759 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1764 netisr_domsg(&msg.base, 0);
1768 * Mark an interface down and notify protocols of the transition. An
1769 * interface going down is also considered to be a synchronizing event.
1770 * We must ensure that all packet processing related to the interface
1771 * has completed before we return so e.g. the caller can free the ifnet
1772 * structure that the mbufs may be referencing.
1774 * NOTE: must be called at splnet or eqivalent.
1777 if_down(struct ifnet *ifp)
1779 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
1780 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1781 netmsg_service_sync();
1785 * Mark an interface up and notify protocols of
1787 * NOTE: must be called at splnet or eqivalent.
1790 if_up(struct ifnet *ifp)
1792 if_route(ifp, IFF_UP, AF_UNSPEC);
1793 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
1797 * Process a link state change.
1798 * NOTE: must be called at splsoftnet or equivalent.
1801 if_link_state_change(struct ifnet *ifp)
1803 int link_state = ifp->if_link_state;
1806 devctl_notify("IFNET", ifp->if_xname,
1807 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1809 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state);
1813 * Handle interface watchdog timer routines. Called
1814 * from softclock, we decrement timers (if set) and
1815 * call the appropriate interface routine on expiration.
1818 if_slowtimo_dispatch(netmsg_t nmsg)
1820 struct globaldata *gd = mycpu;
1821 const struct ifnet_array *arr;
1827 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1830 arr = ifnet_array_get();
1831 for (i = 0; i < arr->ifnet_count; ++i) {
1832 struct ifnet *ifp = arr->ifnet_arr[i];
1836 if (if_stats_compat) {
1837 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1838 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1839 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1840 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1841 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1842 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1843 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1844 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1845 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1846 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1847 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1848 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1851 if (ifp->if_timer == 0 || --ifp->if_timer) {
1855 if (ifp->if_watchdog) {
1856 if (ifnet_tryserialize_all(ifp)) {
1857 (*ifp->if_watchdog)(ifp);
1858 ifnet_deserialize_all(ifp);
1860 /* try again next timeout */
1868 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1872 if_slowtimo(void *arg __unused)
1874 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1876 KASSERT(mycpuid == 0, ("not on cpu0"));
1878 if (lmsg->ms_flags & MSGF_DONE)
1879 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1884 * Map interface name to
1885 * interface structure pointer.
1888 ifunit(const char *name)
1893 * Search all the interfaces for this name/number
1895 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1897 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1898 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1905 ifunit_netisr(const char *name)
1907 const struct ifnet_array *arr;
1911 * Search all the interfaces for this name/number
1914 arr = ifnet_array_get();
1915 for (i = 0; i < arr->ifnet_count; ++i) {
1916 struct ifnet *ifp = arr->ifnet_arr[i];
1918 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1928 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1931 struct ifgroupreq *ifgr;
1934 int error, do_ifup = 0;
1937 size_t namelen, onamelen;
1938 char new_name[IFNAMSIZ];
1940 struct sockaddr_dl *sdl;
1944 return (ifconf(cmd, data, cred));
1949 ifr = (struct ifreq *)data;
1954 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1956 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1957 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1959 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1961 return (if_clone_destroy(ifr->ifr_name));
1962 case SIOCIFGCLONERS:
1963 return (if_clone_list((struct if_clonereq *)data));
1965 return (if_getgroupmembers((struct ifgroupreq *)data));
1971 * Nominal ioctl through interface, lookup the ifp and obtain a
1972 * lock to serialize the ifconfig ioctl operation.
1976 ifp = ifunit(ifr->ifr_name);
1985 ifr->ifr_index = ifp->if_index;
1989 ifr->ifr_flags = ifp->if_flags;
1990 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1994 ifr->ifr_reqcap = ifp->if_capabilities;
1995 ifr->ifr_curcap = ifp->if_capenable;
1999 ifr->ifr_metric = ifp->if_metric;
2003 ifr->ifr_mtu = ifp->if_mtu;
2007 ifr->ifr_tsolen = ifp->if_tsolen;
2011 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
2012 sizeof(ifp->if_data));
2016 ifr->ifr_phys = ifp->if_physical;
2019 case SIOCGIFPOLLCPU:
2020 ifr->ifr_pollcpu = -1;
2023 case SIOCSIFPOLLCPU:
2027 error = priv_check_cred(cred, PRIV_ROOT, 0);
2030 new_flags = (ifr->ifr_flags & 0xffff) |
2031 (ifr->ifr_flagshigh << 16);
2032 if (ifp->if_flags & IFF_SMART) {
2033 /* Smart drivers twiddle their own routes */
2034 } else if (ifp->if_flags & IFF_UP &&
2035 (new_flags & IFF_UP) == 0) {
2037 } else if (new_flags & IFF_UP &&
2038 (ifp->if_flags & IFF_UP) == 0) {
2042 #ifdef IFPOLL_ENABLE
2043 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
2044 if (new_flags & IFF_NPOLLING)
2045 ifpoll_register(ifp);
2047 ifpoll_deregister(ifp);
2051 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2052 (new_flags &~ IFF_CANTCHANGE);
2053 if (new_flags & IFF_PPROMISC) {
2054 /* Permanently promiscuous mode requested */
2055 ifp->if_flags |= IFF_PROMISC;
2056 } else if (ifp->if_pcount == 0) {
2057 ifp->if_flags &= ~IFF_PROMISC;
2059 if (ifp->if_ioctl) {
2060 ifnet_serialize_all(ifp);
2061 ifp->if_ioctl(ifp, cmd, data, cred);
2062 ifnet_deserialize_all(ifp);
2066 getmicrotime(&ifp->if_lastchange);
2070 error = priv_check_cred(cred, PRIV_ROOT, 0);
2073 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
2077 ifnet_serialize_all(ifp);
2078 ifp->if_ioctl(ifp, cmd, data, cred);
2079 ifnet_deserialize_all(ifp);
2083 error = priv_check_cred(cred, PRIV_ROOT, 0);
2086 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2089 if (new_name[0] == '\0') {
2093 if (ifunit(new_name) != NULL) {
2098 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
2100 /* Announce the departure of the interface. */
2101 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2103 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2104 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2105 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2106 namelen = strlen(new_name);
2107 onamelen = sdl->sdl_nlen;
2109 * Move the address if needed. This is safe because we
2110 * allocate space for a name of length IFNAMSIZ when we
2111 * create this in if_attach().
2113 if (namelen != onamelen) {
2114 bcopy(sdl->sdl_data + onamelen,
2115 sdl->sdl_data + namelen, sdl->sdl_alen);
2117 bcopy(new_name, sdl->sdl_data, namelen);
2118 sdl->sdl_nlen = namelen;
2119 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2120 bzero(sdl->sdl_data, onamelen);
2121 while (namelen != 0)
2122 sdl->sdl_data[--namelen] = 0xff;
2124 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2126 /* Announce the return of the interface. */
2127 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2131 error = priv_check_cred(cred, PRIV_ROOT, 0);
2134 ifp->if_metric = ifr->ifr_metric;
2135 getmicrotime(&ifp->if_lastchange);
2139 error = priv_check_cred(cred, PRIV_ROOT, 0);
2142 if (ifp->if_ioctl == NULL) {
2146 ifnet_serialize_all(ifp);
2147 error = ifp->if_ioctl(ifp, cmd, data, cred);
2148 ifnet_deserialize_all(ifp);
2150 getmicrotime(&ifp->if_lastchange);
2155 u_long oldmtu = ifp->if_mtu;
2157 error = priv_check_cred(cred, PRIV_ROOT, 0);
2160 if (ifp->if_ioctl == NULL) {
2164 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2168 ifnet_serialize_all(ifp);
2169 error = ifp->if_ioctl(ifp, cmd, data, cred);
2170 ifnet_deserialize_all(ifp);
2172 getmicrotime(&ifp->if_lastchange);
2176 * If the link MTU changed, do network layer specific procedure.
2178 if (ifp->if_mtu != oldmtu) {
2187 error = priv_check_cred(cred, PRIV_ROOT, 0);
2191 /* XXX need driver supplied upper limit */
2192 if (ifr->ifr_tsolen <= 0) {
2196 ifp->if_tsolen = ifr->ifr_tsolen;
2201 error = priv_check_cred(cred, PRIV_ROOT, 0);
2205 /* Don't allow group membership on non-multicast interfaces. */
2206 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2211 /* Don't let users screw up protocols' entries. */
2212 if (ifr->ifr_addr.sa_family != AF_LINK) {
2217 if (cmd == SIOCADDMULTI) {
2218 struct ifmultiaddr *ifma;
2219 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2221 error = if_delmulti(ifp, &ifr->ifr_addr);
2224 getmicrotime(&ifp->if_lastchange);
2227 case SIOCSIFPHYADDR:
2228 case SIOCDIFPHYADDR:
2230 case SIOCSIFPHYADDR_IN6:
2232 case SIOCSLIFPHYADDR:
2234 case SIOCSIFGENERIC:
2235 error = priv_check_cred(cred, PRIV_ROOT, 0);
2238 if (ifp->if_ioctl == NULL) {
2242 ifnet_serialize_all(ifp);
2243 error = ifp->if_ioctl(ifp, cmd, data, cred);
2244 ifnet_deserialize_all(ifp);
2246 getmicrotime(&ifp->if_lastchange);
2250 ifs = (struct ifstat *)data;
2251 ifs->ascii[0] = '\0';
2253 case SIOCGIFPSRCADDR:
2254 case SIOCGIFPDSTADDR:
2255 case SIOCGLIFPHYADDR:
2257 case SIOCGIFGENERIC:
2258 if (ifp->if_ioctl == NULL) {
2262 ifnet_serialize_all(ifp);
2263 error = ifp->if_ioctl(ifp, cmd, data, cred);
2264 ifnet_deserialize_all(ifp);
2268 error = priv_check_cred(cred, PRIV_ROOT, 0);
2271 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2272 ifr->ifr_addr.sa_len);
2273 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2277 ifgr = (struct ifgroupreq *)ifr;
2278 if ((error = priv_check_cred(cred, PRIV_NET_ADDIFGROUP, 0)))
2280 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2285 ifgr = (struct ifgroupreq *)ifr;
2286 if ((error = priv_check_cred(cred, PRIV_NET_DELIFGROUP, 0)))
2288 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2293 ifgr = (struct ifgroupreq *)ifr;
2294 if ((error = if_getgroups(ifgr, ifp)))
2299 oif_flags = ifp->if_flags;
2300 if (so->so_proto == 0) {
2304 error = so_pru_control_direct(so, cmd, data, ifp);
2307 * If the socket control method returns EOPNOTSUPP, pass the
2308 * request directly to the interface.
2310 * Exclude the SIOCSIF{ADDR,BRDADDR,DSTADDR,NETMASK} ioctls,
2311 * because drivers may trust these ioctls to come from an
2312 * already privileged layer and thus do not perform credentials
2313 * checks or input validation.
2315 if (error == EOPNOTSUPP &&
2316 ifp->if_ioctl != NULL &&
2317 cmd != SIOCSIFADDR &&
2318 cmd != SIOCSIFBRDADDR &&
2319 cmd != SIOCSIFDSTADDR &&
2320 cmd != SIOCSIFNETMASK) {
2321 ifnet_serialize_all(ifp);
2322 error = ifp->if_ioctl(ifp, cmd, data, cred);
2323 ifnet_deserialize_all(ifp);
2326 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2328 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2329 if (ifp->if_flags & IFF_UP) {
2344 * Set/clear promiscuous mode on interface ifp based on the truth value
2345 * of pswitch. The calls are reference counted so that only the first
2346 * "on" request actually has an effect, as does the final "off" request.
2347 * Results are undefined if the "off" and "on" requests are not matched.
2350 ifpromisc(struct ifnet *ifp, int pswitch)
2356 oldflags = ifp->if_flags;
2357 if (ifp->if_flags & IFF_PPROMISC) {
2358 /* Do nothing if device is in permanently promiscuous mode */
2359 ifp->if_pcount += pswitch ? 1 : -1;
2364 * If the device is not configured up, we cannot put it in
2367 if ((ifp->if_flags & IFF_UP) == 0)
2369 if (ifp->if_pcount++ != 0)
2371 ifp->if_flags |= IFF_PROMISC;
2372 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2375 if (--ifp->if_pcount > 0)
2377 ifp->if_flags &= ~IFF_PROMISC;
2378 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2381 ifr.ifr_flags = ifp->if_flags;
2382 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2383 ifnet_serialize_all(ifp);
2384 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2385 ifnet_deserialize_all(ifp);
2389 ifp->if_flags = oldflags;
2394 * Return interface configuration
2395 * of system. List may be used
2396 * in later ioctl's (above) to get
2397 * other information.
2400 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2402 struct ifconf *ifc = (struct ifconf *)data;
2404 struct sockaddr *sa;
2405 struct ifreq ifr, *ifrp;
2406 int space = ifc->ifc_len, error = 0;
2408 ifrp = ifc->ifc_req;
2411 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2412 struct ifaddr_container *ifac, *ifac_mark;
2413 struct ifaddr_marker mark;
2414 struct ifaddrhead *head;
2417 if (space <= sizeof ifr)
2421 * Zero the stack declared structure first to prevent
2422 * memory disclosure.
2424 bzero(&ifr, sizeof(ifr));
2425 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2426 >= sizeof(ifr.ifr_name)) {
2427 error = ENAMETOOLONG;
2432 * Add a marker, since copyout() could block and during that
2433 * period the list could be changed. Inserting the marker to
2434 * the header of the list will not cause trouble for the code
2435 * assuming that the first element of the list is AF_LINK; the
2436 * marker will be moved to the next position w/o blocking.
2438 ifa_marker_init(&mark, ifp);
2439 ifac_mark = &mark.ifac;
2440 head = &ifp->if_addrheads[mycpuid];
2443 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2444 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2445 struct ifaddr *ifa = ifac->ifa;
2447 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2448 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2451 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2454 if (space <= sizeof ifr)
2457 if (cred->cr_prison &&
2458 prison_if(cred, sa))
2462 * Keep a reference on this ifaddr, so that it will
2463 * not be destroyed when its address is copied to
2464 * the userland, which could block.
2467 if (sa->sa_len <= sizeof(*sa)) {
2469 error = copyout(&ifr, ifrp, sizeof ifr);
2472 if (space < (sizeof ifr) + sa->sa_len -
2477 space -= sa->sa_len - sizeof(*sa);
2478 error = copyout(&ifr, ifrp,
2479 sizeof ifr.ifr_name);
2481 error = copyout(sa, &ifrp->ifr_addr,
2483 ifrp = (struct ifreq *)
2484 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2489 space -= sizeof ifr;
2491 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2495 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2496 error = copyout(&ifr, ifrp, sizeof ifr);
2499 space -= sizeof ifr;
2505 ifc->ifc_len -= space;
2510 * Just like if_promisc(), but for all-multicast-reception mode.
2513 if_allmulti(struct ifnet *ifp, int onswitch)
2521 if (ifp->if_amcount++ == 0) {
2522 ifp->if_flags |= IFF_ALLMULTI;
2523 ifr.ifr_flags = ifp->if_flags;
2524 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2525 ifnet_serialize_all(ifp);
2526 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2528 ifnet_deserialize_all(ifp);
2531 if (ifp->if_amcount > 1) {
2534 ifp->if_amcount = 0;
2535 ifp->if_flags &= ~IFF_ALLMULTI;
2536 ifr.ifr_flags = ifp->if_flags;
2537 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2538 ifnet_serialize_all(ifp);
2539 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2541 ifnet_deserialize_all(ifp);
2553 * Add a multicast listenership to the interface in question.
2554 * The link layer provides a routine which converts
2557 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2558 struct ifmultiaddr **retifma)
2560 struct sockaddr *llsa, *dupsa;
2562 struct ifmultiaddr *ifma;
2564 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2567 * If the matching multicast address already exists
2568 * then don't add a new one, just add a reference
2570 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2571 if (sa_equal(sa, ifma->ifma_addr)) {
2572 ifma->ifma_refcount++;
2580 * Give the link layer a chance to accept/reject it, and also
2581 * find out which AF_LINK address this maps to, if it isn't one
2584 if (ifp->if_resolvemulti) {
2585 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2592 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2593 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2594 bcopy(sa, dupsa, sa->sa_len);
2596 ifma->ifma_addr = dupsa;
2597 ifma->ifma_lladdr = llsa;
2598 ifma->ifma_ifp = ifp;
2599 ifma->ifma_refcount = 1;
2600 ifma->ifma_protospec = NULL;
2601 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2603 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2608 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2609 if (sa_equal(ifma->ifma_addr, llsa))
2613 ifma->ifma_refcount++;
2615 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2616 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2617 bcopy(llsa, dupsa, llsa->sa_len);
2618 ifma->ifma_addr = dupsa;
2619 ifma->ifma_ifp = ifp;
2620 ifma->ifma_refcount = 1;
2621 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2625 * We are certain we have added something, so call down to the
2626 * interface to let them know about it.
2629 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2635 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2636 struct ifmultiaddr **retifma)
2640 ifnet_serialize_all(ifp);
2641 error = if_addmulti_serialized(ifp, sa, retifma);
2642 ifnet_deserialize_all(ifp);
2648 * Remove a reference to a multicast address on this interface. Yell
2649 * if the request does not match an existing membership.
2652 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2654 struct ifmultiaddr *ifma;
2656 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2658 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2659 if (sa_equal(sa, ifma->ifma_addr))
2664 if (ifma->ifma_refcount > 1) {
2665 ifma->ifma_refcount--;
2669 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2670 sa = ifma->ifma_lladdr;
2671 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2673 * Make sure the interface driver is notified
2674 * in the case of a link layer mcast group being left.
2676 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2677 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2678 kfree(ifma->ifma_addr, M_IFMADDR);
2679 kfree(ifma, M_IFMADDR);
2684 * Now look for the link-layer address which corresponds to
2685 * this network address. It had been squirreled away in
2686 * ifma->ifma_lladdr for this purpose (so we don't have
2687 * to call ifp->if_resolvemulti() again), and we saved that
2688 * value in sa above. If some nasty deleted the
2689 * link-layer address out from underneath us, we can deal because
2690 * the address we stored was is not the same as the one which was
2691 * in the record for the link-layer address. (So we don't complain
2694 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2695 if (sa_equal(sa, ifma->ifma_addr))
2700 if (ifma->ifma_refcount > 1) {
2701 ifma->ifma_refcount--;
2705 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2706 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2707 kfree(ifma->ifma_addr, M_IFMADDR);
2708 kfree(sa, M_IFMADDR);
2709 kfree(ifma, M_IFMADDR);
2715 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2719 ifnet_serialize_all(ifp);
2720 error = if_delmulti_serialized(ifp, sa);
2721 ifnet_deserialize_all(ifp);
2727 * Delete all multicast group membership for an interface.
2728 * Should be used to quickly flush all multicast filters.
2731 if_delallmulti_serialized(struct ifnet *ifp)
2733 struct ifmultiaddr *ifma, mark;
2736 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2738 bzero(&sa, sizeof(sa));
2739 sa.sa_family = AF_UNSPEC;
2740 sa.sa_len = sizeof(sa);
2742 bzero(&mark, sizeof(mark));
2743 mark.ifma_addr = &sa;
2745 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2746 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2747 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2748 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2751 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2754 if_delmulti_serialized(ifp, ifma->ifma_addr);
2756 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2761 * Set the link layer address on an interface.
2763 * At this time we only support certain types of interfaces,
2764 * and we don't allow the length of the address to change.
2767 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2769 struct sockaddr_dl *sdl;
2772 sdl = IF_LLSOCKADDR(ifp);
2775 if (len != sdl->sdl_alen) /* don't allow length to change */
2777 switch (ifp->if_type) {
2778 case IFT_ETHER: /* these types use struct arpcom */
2781 case IFT_IEEE8023ADLAG:
2782 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2783 bcopy(lladdr, LLADDR(sdl), len);
2789 * If the interface is already up, we need
2790 * to re-init it in order to reprogram its
2793 ifnet_serialize_all(ifp);
2794 if ((ifp->if_flags & IFF_UP) != 0) {
2796 struct ifaddr_container *ifac;
2799 ifp->if_flags &= ~IFF_UP;
2800 ifr.ifr_flags = ifp->if_flags;
2801 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2802 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2804 ifp->if_flags |= IFF_UP;
2805 ifr.ifr_flags = ifp->if_flags;
2806 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2807 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2811 * Also send gratuitous ARPs to notify other nodes about
2812 * the address change.
2814 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2815 struct ifaddr *ifa = ifac->ifa;
2817 if (ifa->ifa_addr != NULL &&
2818 ifa->ifa_addr->sa_family == AF_INET)
2819 arp_gratuitous(ifp, ifa);
2823 ifnet_deserialize_all(ifp);
2829 * Locate an interface based on a complete address.
2832 if_bylla(const void *lla, unsigned char lla_len)
2834 const struct ifnet_array *arr;
2836 struct sockaddr_dl *sdl;
2839 arr = ifnet_array_get();
2840 for (i = 0; i < arr->ifnet_count; ++i) {
2841 ifp = arr->ifnet_arr[i];
2842 if (ifp->if_addrlen != lla_len)
2845 sdl = IF_LLSOCKADDR(ifp);
2846 if (memcmp(lla, LLADDR(sdl), lla_len) == 0)
2852 struct ifmultiaddr *
2853 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2855 struct ifmultiaddr *ifma;
2857 /* TODO: need ifnet_serialize_main */
2858 ifnet_serialize_all(ifp);
2859 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2860 if (sa_equal(ifma->ifma_addr, sa))
2862 ifnet_deserialize_all(ifp);
2868 * This function locates the first real ethernet MAC from a network
2869 * card and loads it into node, returning 0 on success or ENOENT if
2870 * no suitable interfaces were found. It is used by the uuid code to
2871 * generate a unique 6-byte number.
2874 if_getanyethermac(uint16_t *node, int minlen)
2877 struct sockaddr_dl *sdl;
2880 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2881 if (ifp->if_type != IFT_ETHER)
2883 sdl = IF_LLSOCKADDR(ifp);
2884 if (sdl->sdl_alen < minlen)
2886 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2896 * The name argument must be a pointer to storage which will last as
2897 * long as the interface does. For physical devices, the result of
2898 * device_get_name(dev) is a good choice and for pseudo-devices a
2899 * static string works well.
2902 if_initname(struct ifnet *ifp, const char *name, int unit)
2904 ifp->if_dname = name;
2905 ifp->if_dunit = unit;
2906 if (unit != IF_DUNIT_NONE)
2907 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2909 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2913 if_printf(struct ifnet *ifp, const char *fmt, ...)
2918 retval = kprintf("%s: ", ifp->if_xname);
2919 __va_start(ap, fmt);
2920 retval += kvprintf(fmt, ap);
2926 if_alloc(uint8_t type)
2932 * XXX temporary hack until arpcom is setup in if_l2com
2934 if (type == IFT_ETHER)
2935 size = sizeof(struct arpcom);
2937 size = sizeof(struct ifnet);
2939 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2941 ifp->if_type = type;
2943 if (if_com_alloc[type] != NULL) {
2944 ifp->if_l2com = if_com_alloc[type](type, ifp);
2945 if (ifp->if_l2com == NULL) {
2946 kfree(ifp, M_IFNET);
2954 if_free(struct ifnet *ifp)
2956 kfree(ifp, M_IFNET);
2960 ifq_set_classic(struct ifaltq *ifq)
2962 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2963 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2967 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2968 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2972 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2973 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2974 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2975 KASSERT(request != NULL, ("request is not specified"));
2977 ifq->altq_mapsubq = mapsubq;
2978 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2979 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2981 ifsq->ifsq_enqueue = enqueue;
2982 ifsq->ifsq_dequeue = dequeue;
2983 ifsq->ifsq_request = request;
2988 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2991 classq_add(&ifsq->ifsq_norm, m);
2992 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2996 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2999 classq_add(&ifsq->ifsq_prio, m);
3000 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
3001 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
3004 static struct mbuf *
3005 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
3009 m = classq_get(&ifsq->ifsq_norm);
3011 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
3015 static struct mbuf *
3016 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
3020 m = classq_get(&ifsq->ifsq_prio);
3022 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
3023 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
3029 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
3030 struct altq_pktattr *pa __unused)
3035 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
3036 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
3037 struct mbuf *m_drop;
3039 if (m->m_flags & M_PRIO) {
3041 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
3042 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
3043 /* Try dropping some from normal queue. */
3044 m_drop = ifsq_norm_dequeue(ifsq);
3047 m_drop = ifsq_prio_dequeue(ifsq);
3049 m_drop = ifsq_norm_dequeue(ifsq);
3051 if (m_drop != NULL) {
3052 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
3057 * No old packets could be dropped!
3058 * NOTE: Caller increases oqdrops.
3063 if (m->m_flags & M_PRIO)
3064 ifsq_prio_enqueue(ifsq, m);
3066 ifsq_norm_enqueue(ifsq, m);
3072 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
3078 m = classq_head(&ifsq->ifsq_prio);
3080 m = classq_head(&ifsq->ifsq_norm);
3084 m = ifsq_prio_dequeue(ifsq);
3086 m = ifsq_norm_dequeue(ifsq);
3090 panic("unsupported ALTQ dequeue op: %d", op);
3096 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
3103 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
3111 panic("unsupported ALTQ request: %d", req);
3117 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
3119 struct ifnet *ifp = ifsq_get_ifp(ifsq);
3120 int running = 0, need_sched;
3123 * Try to do direct ifnet.if_start on the subqueue first, if there is
3124 * contention on the subqueue hardware serializer, ifnet.if_start on
3125 * the subqueue will be scheduled on the subqueue owner CPU.
3127 if (!ifsq_tryserialize_hw(ifsq)) {
3129 * Subqueue hardware serializer contention happened,
3130 * ifnet.if_start on the subqueue is scheduled on
3131 * the subqueue owner CPU, and we keep going.
3133 ifsq_ifstart_schedule(ifsq, 1);
3137 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
3138 ifp->if_start(ifp, ifsq);
3139 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
3142 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
3144 ifsq_deserialize_hw(ifsq);
3148 * More data need to be transmitted, ifnet.if_start on the
3149 * subqueue is scheduled on the subqueue owner CPU, and we
3151 * NOTE: ifnet.if_start subqueue interlock is not released.
3153 ifsq_ifstart_schedule(ifsq, force_sched);
3158 * Subqeue packets staging mechanism:
3160 * The packets enqueued into the subqueue are staged to a certain amount
3161 * before the ifnet.if_start on the subqueue is called. In this way, the
3162 * driver could avoid writing to hardware registers upon every packet,
3163 * instead, hardware registers could be written when certain amount of
3164 * packets are put onto hardware TX ring. The measurement on several modern
3165 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
3166 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
3167 * datagrams are transmitted at 1.48Mpps. The performance improvement by
3168 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
3169 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
3171 * Subqueue packets staging is performed for two entry points into drivers'
3172 * transmission function:
3173 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
3174 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
3176 * Subqueue packets staging will be stopped upon any of the following
3178 * - If the count of packets enqueued on the current CPU is great than or
3179 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
3180 * - If the total length of packets enqueued on the current CPU is great
3181 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
3182 * cut from the hardware's MTU mainly bacause a full TCP segment's size
3183 * is usually less than hardware's MTU.
3184 * - ifsq_ifstart_schedule() is not pending on the current CPU and
3185 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
3187 * - The if_start_rollup(), which is registered as low priority netisr
3188 * rollup function, is called; probably because no more work is pending
3192 * Currently subqueue packet staging is only performed in netisr threads.
3195 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3197 struct ifaltq *ifq = &ifp->if_snd;
3198 struct ifaltq_subque *ifsq;
3199 int error, start = 0, len, mcast = 0, avoid_start = 0;
3200 struct ifsubq_stage_head *head = NULL;
3201 struct ifsubq_stage *stage = NULL;
3202 struct globaldata *gd = mycpu;
3203 struct thread *td = gd->gd_curthread;
3205 crit_enter_quick(td);
3207 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3208 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3210 len = m->m_pkthdr.len;
3211 if (m->m_flags & M_MCAST)
3214 if (td->td_type == TD_TYPE_NETISR) {
3215 head = &ifsubq_stage_heads[mycpuid];
3216 stage = ifsq_get_stage(ifsq, mycpuid);
3219 stage->stg_len += len;
3220 if (stage->stg_cnt < ifsq_stage_cntmax &&
3221 stage->stg_len < (ifp->if_mtu - max_protohdr))
3226 error = ifsq_enqueue_locked(ifsq, m, pa);
3228 IFNET_STAT_INC(ifp, oqdrops, 1);
3229 if (!ifsq_data_ready(ifsq)) {
3230 ALTQ_SQ_UNLOCK(ifsq);
3231 crit_exit_quick(td);
3236 if (!ifsq_is_started(ifsq)) {
3238 ALTQ_SQ_UNLOCK(ifsq);
3241 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3242 ifsq_stage_insert(head, stage);
3244 IFNET_STAT_INC(ifp, obytes, len);
3246 IFNET_STAT_INC(ifp, omcasts, 1);
3247 crit_exit_quick(td);
3252 * Hold the subqueue interlock of ifnet.if_start
3254 ifsq_set_started(ifsq);
3257 ALTQ_SQ_UNLOCK(ifsq);
3260 IFNET_STAT_INC(ifp, obytes, len);
3262 IFNET_STAT_INC(ifp, omcasts, 1);
3265 if (stage != NULL) {
3266 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3267 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3269 ifsq_stage_remove(head, stage);
3270 ifsq_ifstart_schedule(ifsq, 1);
3272 crit_exit_quick(td);
3276 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3277 ifsq_stage_remove(head, stage);
3285 crit_exit_quick(td);
3289 ifsq_ifstart_try(ifsq, 0);
3291 crit_exit_quick(td);
3296 ifa_create(int size)
3301 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3303 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3306 * Make ifa_container availabel on all CPUs, since they
3307 * could be accessed by any threads.
3309 ifa->ifa_containers =
3310 kmalloc(ncpus * sizeof(struct ifaddr_container),
3312 M_INTWAIT | M_ZERO | M_CACHEALIGN);
3314 ifa->ifa_ncnt = ncpus;
3315 for (i = 0; i < ncpus; ++i) {
3316 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3318 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3320 ifac->ifa_refcnt = 1;
3323 kprintf("alloc ifa %p %d\n", ifa, size);
3329 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3331 struct ifaddr *ifa = ifac->ifa;
3333 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3334 KKASSERT(ifac->ifa_refcnt == 0);
3335 KASSERT(ifac->ifa_listmask == 0,
3336 ("ifa is still on %#x lists", ifac->ifa_listmask));
3338 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3340 #ifdef IFADDR_DEBUG_VERBOSE
3341 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3344 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3345 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3346 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3348 kprintf("free ifa %p\n", ifa);
3350 kfree(ifa->ifa_containers, M_IFADDR);
3351 kfree(ifa, M_IFADDR);
3356 ifa_iflink_dispatch(netmsg_t nmsg)
3358 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3359 struct ifaddr *ifa = msg->ifa;
3360 struct ifnet *ifp = msg->ifp;
3362 struct ifaddr_container *ifac;
3366 ifac = &ifa->ifa_containers[cpu];
3367 ASSERT_IFAC_VALID(ifac);
3368 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3369 ("ifaddr is on if_addrheads"));
3371 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3373 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3375 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3379 netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3383 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3385 struct netmsg_ifaddr msg;
3387 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3388 0, ifa_iflink_dispatch);
3393 netisr_domsg(&msg.base, 0);
3397 ifa_ifunlink_dispatch(netmsg_t nmsg)
3399 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3400 struct ifaddr *ifa = msg->ifa;
3401 struct ifnet *ifp = msg->ifp;
3403 struct ifaddr_container *ifac;
3407 ifac = &ifa->ifa_containers[cpu];
3408 ASSERT_IFAC_VALID(ifac);
3409 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3410 ("ifaddr is not on if_addrhead"));
3412 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3413 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3417 netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3421 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3423 struct netmsg_ifaddr msg;
3425 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3426 0, ifa_ifunlink_dispatch);
3430 netisr_domsg(&msg.base, 0);
3434 ifa_destroy_dispatch(netmsg_t nmsg)
3436 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3439 netisr_forwardmsg_all(&nmsg->base, mycpuid + 1);
3443 ifa_destroy(struct ifaddr *ifa)
3445 struct netmsg_ifaddr msg;
3447 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3448 0, ifa_destroy_dispatch);
3451 netisr_domsg(&msg.base, 0);
3455 if_start_rollup(void)
3457 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3458 struct ifsubq_stage *stage;
3462 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3463 struct ifaltq_subque *ifsq = stage->stg_subq;
3466 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3468 ifsq_stage_remove(head, stage);
3471 ifsq_ifstart_schedule(ifsq, 1);
3476 if (!ifsq_is_started(ifsq)) {
3478 * Hold the subqueue interlock of
3481 ifsq_set_started(ifsq);
3484 ALTQ_SQ_UNLOCK(ifsq);
3487 ifsq_ifstart_try(ifsq, 1);
3489 KKASSERT((stage->stg_flags &
3490 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3497 ifnetinit(void *dummy __unused)
3501 /* XXX netisr_ncpus */
3502 for (i = 0; i < ncpus; ++i)
3503 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3504 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3508 if_register_com_alloc(u_char type,
3509 if_com_alloc_t *a, if_com_free_t *f)
3512 KASSERT(if_com_alloc[type] == NULL,
3513 ("if_register_com_alloc: %d already registered", type));
3514 KASSERT(if_com_free[type] == NULL,
3515 ("if_register_com_alloc: %d free already registered", type));
3517 if_com_alloc[type] = a;
3518 if_com_free[type] = f;
3522 if_deregister_com_alloc(u_char type)
3525 KASSERT(if_com_alloc[type] != NULL,
3526 ("if_deregister_com_alloc: %d not registered", type));
3527 KASSERT(if_com_free[type] != NULL,
3528 ("if_deregister_com_alloc: %d free not registered", type));
3529 if_com_alloc[type] = NULL;
3530 if_com_free[type] = NULL;
3534 ifq_set_maxlen(struct ifaltq *ifq, int len)
3536 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3540 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3542 return ALTQ_SUBQ_INDEX_DEFAULT;
3546 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3549 return (cpuid % ifq->altq_subq_mappriv);
3553 ifsq_watchdog(void *arg)
3555 struct ifsubq_watchdog *wd = arg;
3558 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3561 ifp = ifsq_get_ifp(wd->wd_subq);
3562 if (ifnet_tryserialize_all(ifp)) {
3563 wd->wd_watchdog(wd->wd_subq);
3564 ifnet_deserialize_all(ifp);
3566 /* try again next timeout */
3570 ifsq_watchdog_reset(wd);
3574 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3576 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3577 ifsq_get_cpuid(wd->wd_subq));
3581 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3582 ifsq_watchdog_t watchdog)
3584 callout_init_mp(&wd->wd_callout);
3587 wd->wd_watchdog = watchdog;
3591 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3594 ifsq_watchdog_reset(wd);
3598 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3601 callout_stop(&wd->wd_callout);
3607 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3608 ("try holding ifnet lock in netisr"));
3609 mtx_lock(&ifnet_mtx);
3615 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3616 ("try holding ifnet lock in netisr"));
3617 mtx_unlock(&ifnet_mtx);
3620 static struct ifnet_array *
3621 ifnet_array_alloc(int count)
3623 struct ifnet_array *arr;
3625 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3627 arr->ifnet_count = count;
3633 ifnet_array_free(struct ifnet_array *arr)
3635 if (arr == &ifnet_array0)
3637 kfree(arr, M_IFNET);
3640 static struct ifnet_array *
3641 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3643 struct ifnet_array *arr;
3646 KASSERT(old_arr->ifnet_count >= 0,
3647 ("invalid ifnet array count %d", old_arr->ifnet_count));
3648 count = old_arr->ifnet_count + 1;
3649 arr = ifnet_array_alloc(count);
3652 * Save the old ifnet array and append this ifp to the end of
3653 * the new ifnet array.
3655 for (i = 0; i < old_arr->ifnet_count; ++i) {
3656 KASSERT(old_arr->ifnet_arr[i] != ifp,
3657 ("%s is already in ifnet array", ifp->if_xname));
3658 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3660 KASSERT(i == count - 1,
3661 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3662 ifp->if_xname, count - 1, i));
3663 arr->ifnet_arr[i] = ifp;
3668 static struct ifnet_array *
3669 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3671 struct ifnet_array *arr;
3672 int count, i, idx, found = 0;
3674 KASSERT(old_arr->ifnet_count > 0,
3675 ("invalid ifnet array count %d", old_arr->ifnet_count));
3676 count = old_arr->ifnet_count - 1;
3677 arr = ifnet_array_alloc(count);
3680 * Save the old ifnet array, but skip this ifp.
3683 for (i = 0; i < old_arr->ifnet_count; ++i) {
3684 if (old_arr->ifnet_arr[i] == ifp) {
3686 ("dup %s is in ifnet array", ifp->if_xname));
3690 KASSERT(idx < count,
3691 ("invalid ifnet array index %d, count %d", idx, count));
3692 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3695 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3696 KASSERT(idx == count,
3697 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3698 ifp->if_xname, count, idx));
3703 const struct ifnet_array *
3704 ifnet_array_get(void)
3706 const struct ifnet_array *ret;
3708 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3710 /* Make sure 'ret' is really used. */
3716 ifnet_array_isempty(void)
3718 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3719 if (ifnet_array->ifnet_count == 0)
3726 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3730 memset(mark, 0, sizeof(*mark));
3733 mark->ifac.ifa = ifa;
3735 ifa->ifa_addr = &mark->addr;
3736 ifa->ifa_dstaddr = &mark->dstaddr;
3737 ifa->ifa_netmask = &mark->netmask;
3742 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3745 KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3747 if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3748 ring_cnt = ring_cntmax;
3749 if (ring_cnt > netisr_ncpus)
3750 ring_cnt = netisr_ncpus;
3755 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3759 KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3760 KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3764 offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3765 for (i = 0; i < rm->rm_cnt; ++i) {
3766 rm->rm_cpumap[i] = offset + i;
3767 KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3768 ("invalid cpumap[%d] = %d, offset %d", i,
3769 rm->rm_cpumap[i], offset));
3773 static struct if_ringmap *
3774 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3777 struct if_ringmap *rm;
3778 int i, grid = 0, prev_grid;
3780 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3781 rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3782 M_DEVBUF, M_WAITOK | M_ZERO);
3784 rm->rm_cnt = ring_cnt;
3785 if (flags & RINGMAP_FLAG_POWEROF2)
3786 rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3788 prev_grid = netisr_ncpus;
3789 for (i = 0; i < netisr_ncpus; ++i) {
3790 if (netisr_ncpus % (i + 1) != 0)
3793 grid = netisr_ncpus / (i + 1);
3794 if (rm->rm_cnt > grid) {
3799 if (rm->rm_cnt > netisr_ncpus / (i + 2))
3803 if_ringmap_set_grid(dev, rm, grid);
3809 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3812 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3813 RINGMAP_FLAG_NONE));
3817 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3820 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3821 RINGMAP_FLAG_POWEROF2));
3825 if_ringmap_free(struct if_ringmap *rm)
3828 kfree(rm, M_DEVBUF);
3832 * Align the two ringmaps.
3834 * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3838 * CPU 0 1 2 3 4 5 6 7
3839 * NIC_RX n0 n1 n2 n3
3844 * CPU 0 1 2 3 4 5 6 7
3845 * NIC_RX n0 n1 n2 n3
3849 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3852 if (rm0->rm_grid > rm1->rm_grid)
3853 if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3854 else if (rm0->rm_grid < rm1->rm_grid)
3855 if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3859 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3861 int subset_grid, cnt, divisor, mod, offset, i;
3862 struct if_ringmap *subset_rm, *rm;
3863 int old_rm0_grid, old_rm1_grid;
3865 if (rm0->rm_grid == rm1->rm_grid)
3868 /* Save grid for later use */
3869 old_rm0_grid = rm0->rm_grid;
3870 old_rm1_grid = rm1->rm_grid;
3872 if_ringmap_align(dev, rm0, rm1);
3875 * Re-shuffle rings to get more even distribution.
3877 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3879 * CPU 0 1 2 3 4 5 6 7 8 9 10 11
3881 * NIC_RX a0 a1 a2 a3 b0 b1 b2 b3 c0 c1 c2 c3
3882 * NIC_TX A0 A1 B0 B1 C0 C1
3884 * NIC_RX d0 d1 d2 d3 e0 e1 e2 e3 f0 f1 f2 f3
3885 * NIC_TX D0 D1 E0 E1 F0 F1
3888 if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3890 subset_grid = old_rm1_grid;
3893 } else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3895 subset_grid = old_rm0_grid;
3899 /* No space to shuffle. */
3903 mod = cnt / subset_grid;
3905 divisor = netisr_ncpus / rm->rm_grid;
3906 offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3908 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3909 subset_rm->rm_cpumap[i] += offset;
3910 KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3911 ("match: invalid cpumap[%d] = %d, offset %d",
3912 i, subset_rm->rm_cpumap[i], offset));
3915 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3918 for (j = 0; j < rm->rm_cnt; ++j) {
3919 if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3922 KASSERT(j < rm->rm_cnt,
3923 ("subset cpumap[%d] = %d not found in superset",
3924 i, subset_rm->rm_cpumap[i]));
3930 if_ringmap_count(const struct if_ringmap *rm)
3933 return (rm->rm_cnt);
3937 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3940 KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3941 return (rm->rm_cpumap[ring]);
3945 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3947 int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3949 KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3950 ("invalid redirect table entries %d", table_nent));
3953 for (i = 0; i < NETISR_CPUMAX; ++i) {
3954 table[i] = grid_idx++ % rm->rm_cnt;
3956 if (grid_idx == rm->rm_grid)
3961 * Make the ring distributed more evenly for the remainder
3964 * e.g. 12 netisrs, rm contains 8 rings.
3966 * Redirect table before:
3968 * 0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
3969 * 4 5 6 7 0 1 2 3 0 1 2 3 4 5 6 7
3970 * 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3
3973 * Redirect table after being patched (pX, patched entries):
3975 * 0 1 2 3 4 5 6 7 p0 p1 p2 p3 0 1 2 3
3976 * 4 5 6 7 p4 p5 p6 p7 0 1 2 3 4 5 6 7
3977 * p0 p1 p2 p3 0 1 2 3 4 5 6 7 p4 p5 p6 p7
3980 patch_cnt = rm->rm_grid % rm->rm_cnt;
3983 patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3985 grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3987 for (i = 0; i < grid_cnt; ++i) {
3990 for (j = 0; j < patch_cnt; ++j) {
3993 fix_idx = (i * rm->rm_grid) + patch_off + j;
3994 if (fix_idx >= NETISR_CPUMAX)
3996 table[fix_idx] = grid_idx++ % rm->rm_cnt;
4001 * If the device supports larger redirect table, duplicate
4002 * the first NETISR_CPUMAX entries to the rest of the table,
4003 * so that it matches upper layer's expectation:
4004 * (hash & NETISR_CPUMASK) % netisr_ncpus
4006 ncopy = table_nent / NETISR_CPUMAX;
4007 for (i = 1; i < ncopy; ++i) {
4008 memcpy(&table[i * NETISR_CPUMAX], table,
4009 NETISR_CPUMAX * sizeof(table[0]));
4011 if (if_ringmap_dumprdr) {
4012 for (i = 0; i < table_nent; ++i) {
4013 if (i != 0 && i % 16 == 0)
4015 kprintf("%03d ", table[i]);
4022 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
4024 struct if_ringmap *rm = arg1;
4027 for (i = 0; i < rm->rm_cnt; ++i) {
4028 int cpu = rm->rm_cpumap[i];
4030 error = SYSCTL_OUT(req, &cpu, sizeof(cpu));