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_cachealign(
572 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
574 if (ifp->if_mapsubq == NULL)
575 ifp->if_mapsubq = ifq_mapsubq_default;
579 ifq->altq_disc = NULL;
580 ifq->altq_flags &= ALTQF_CANTCHANGE;
581 ifq->altq_tbr = NULL;
584 if (ifq->altq_subq_cnt <= 0)
585 ifq->altq_subq_cnt = 1;
586 ifq->altq_subq = kmalloc_cachealign(
587 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
588 M_DEVBUF, M_WAITOK | M_ZERO);
590 if (ifq->altq_maxlen == 0) {
591 if_printf(ifp, "driver didn't set altq_maxlen\n");
592 ifq_set_maxlen(ifq, ifqmaxlen);
595 /* Allow user to override driver's setting. */
596 ksnprintf(qlenname, sizeof(qlenname), "net.%s.qlenmax", ifp->if_xname);
598 TUNABLE_INT_FETCH(qlenname, &qlen);
600 if_printf(ifp, "qlenmax -> %d\n", qlen);
601 ifq_set_maxlen(ifq, qlen);
604 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
605 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
607 ALTQ_SQ_LOCK_INIT(ifsq);
608 ifsq->ifsq_index = q;
610 ifsq->ifsq_altq = ifq;
611 ifsq->ifsq_ifp = ifp;
613 ifsq->ifsq_maxlen = ifq->altq_maxlen;
614 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
615 ifsq->ifsq_prepended = NULL;
616 ifsq->ifsq_started = 0;
617 ifsq->ifsq_hw_oactive = 0;
618 ifsq_set_cpuid(ifsq, 0);
619 if (ifp->if_serializer != NULL)
620 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
622 /* XXX: netisr_ncpus */
624 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
625 M_DEVBUF, M_WAITOK | M_ZERO);
626 for (i = 0; i < ncpus; ++i)
627 ifsq->ifsq_stage[i].stg_subq = ifsq;
630 * Allocate one if_start message for each CPU, since
631 * the hardware TX ring could be assigned to any CPU.
634 * If the hardware TX ring polling CPU and the hardware
635 * TX ring interrupt CPU are same, one if_start message
638 ifsq->ifsq_ifstart_nmsg =
639 kmalloc(ncpus * sizeof(struct netmsg_base),
640 M_LWKTMSG, M_WAITOK);
641 for (i = 0; i < ncpus; ++i) {
642 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
643 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
644 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
647 ifq_set_classic(ifq);
650 * Increase mbuf cluster/jcluster limits for the mbufs that
651 * could sit on the device queues for quite some time.
653 if (ifp->if_nmbclusters > 0)
654 mcl_inclimit(ifp->if_nmbclusters);
655 if (ifp->if_nmbjclusters > 0)
656 mjcl_inclimit(ifp->if_nmbjclusters);
659 * Install this ifp into ifindex2inet, ifnet queue and ifnet
660 * array after it is setup.
662 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
663 * by ifnet lock, so that non-netisr threads could get a
668 /* Don't update if_index until ifindex2ifnet is setup */
669 ifp->if_index = if_index + 1;
670 sdl_addr->sdl_index = ifp->if_index;
673 * Install this ifp into ifindex2ifnet
675 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
683 n = if_indexlim * sizeof(*q);
684 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
685 if (ifindex2ifnet != NULL) {
686 bcopy(ifindex2ifnet, q, n/2);
687 /* Free old ifindex2ifnet after sync all netisrs */
688 old_ifindex2ifnet = ifindex2ifnet;
692 ifindex2ifnet[ifp->if_index] = ifp;
694 * Update if_index after this ifp is installed into ifindex2ifnet,
695 * so that netisrs could get a consistent view of ifindex2ifnet.
698 if_index = ifp->if_index;
701 * Install this ifp into ifnet array.
703 /* Free old ifnet array after sync all netisrs */
704 old_ifnet_array = ifnet_array;
705 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
708 * Install this ifp into ifnet queue.
710 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
715 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
716 * are no longer accessed and we can free them safely later on.
718 netmsg_service_sync();
719 if (old_ifindex2ifnet != NULL)
720 kfree(old_ifindex2ifnet, M_IFADDR);
721 ifnet_array_free(old_ifnet_array);
723 if (!SLIST_EMPTY(&domains))
724 if_attachdomain1(ifp);
726 /* Announce the interface. */
727 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
728 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
729 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
733 if_attachdomain(void *dummy)
738 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
739 if_attachdomain1(ifp);
742 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
743 if_attachdomain, NULL);
746 if_attachdomain1(struct ifnet *ifp)
752 /* address family dependent data region */
753 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
754 SLIST_FOREACH(dp, &domains, dom_next)
755 if (dp->dom_ifattach)
756 ifp->if_afdata[dp->dom_family] =
757 (*dp->dom_ifattach)(ifp);
762 * Purge all addresses whose type is _not_ AF_LINK
765 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
767 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
768 struct ifaddr_container *ifac, *next;
773 * The ifaddr processing in the following loop will block,
774 * however, this function is called in netisr0, in which
775 * ifaddr list changes happen, so we don't care about the
776 * blockness of the ifaddr processing here.
778 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
780 struct ifaddr *ifa = ifac->ifa;
783 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
786 /* Leave link ifaddr as it is */
787 if (ifa->ifa_addr->sa_family == AF_LINK)
790 /* XXX: Ugly!! ad hoc just for INET */
791 if (ifa->ifa_addr->sa_family == AF_INET) {
792 struct ifaliasreq ifr;
793 struct sockaddr_in saved_addr, saved_dst;
794 #ifdef IFADDR_DEBUG_VERBOSE
797 kprintf("purge in4 addr %p: ", ifa);
798 for (i = 0; i < ncpus; ++i) {
800 ifa->ifa_containers[i].ifa_refcnt);
805 /* Save information for panic. */
806 memcpy(&saved_addr, ifa->ifa_addr, sizeof(saved_addr));
807 if (ifa->ifa_dstaddr != NULL) {
808 memcpy(&saved_dst, ifa->ifa_dstaddr,
811 memset(&saved_dst, 0, sizeof(saved_dst));
814 bzero(&ifr, sizeof ifr);
815 ifr.ifra_addr = *ifa->ifa_addr;
816 if (ifa->ifa_dstaddr)
817 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
818 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
822 /* MUST NOT HAPPEN */
823 panic("%s: in_control failed %x, dst %x", ifp->if_xname,
824 ntohl(saved_addr.sin_addr.s_addr),
825 ntohl(saved_dst.sin_addr.s_addr));
829 if (ifa->ifa_addr->sa_family == AF_INET6) {
830 #ifdef IFADDR_DEBUG_VERBOSE
833 kprintf("purge in6 addr %p: ", ifa);
834 for (i = 0; i < ncpus; ++i) {
836 ifa->ifa_containers[i].ifa_refcnt);
842 /* ifp_addrhead is already updated */
846 if_printf(ifp, "destroy ifaddr family %d\n",
847 ifa->ifa_addr->sa_family);
848 ifa_ifunlink(ifa, ifp);
852 netisr_replymsg(&nmsg->base, 0);
856 if_purgeaddrs_nolink(struct ifnet *ifp)
858 struct netmsg_base nmsg;
860 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
861 if_purgeaddrs_nolink_dispatch);
862 nmsg.lmsg.u.ms_resultp = ifp;
863 netisr_domsg(&nmsg, 0);
867 ifq_stage_detach_handler(netmsg_t nmsg)
869 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
872 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
873 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
874 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
876 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
877 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
879 lwkt_replymsg(&nmsg->lmsg, 0);
883 ifq_stage_detach(struct ifaltq *ifq)
885 struct netmsg_base base;
888 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
889 ifq_stage_detach_handler);
890 base.lmsg.u.ms_resultp = ifq;
892 /* XXX netisr_ncpus */
893 for (cpu = 0; cpu < ncpus; ++cpu)
894 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
897 struct netmsg_if_rtdel {
898 struct netmsg_base base;
903 if_rtdel_dispatch(netmsg_t msg)
905 struct netmsg_if_rtdel *rmsg = (void *)msg;
909 ASSERT_NETISR_NCPUS(cpu);
911 for (i = 1; i <= AF_MAX; i++) {
912 struct radix_node_head *rnh;
914 if ((rnh = rt_tables[cpu][i]) == NULL)
916 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
918 netisr_forwardmsg(&msg->base, cpu + 1);
922 * Detach an interface, removing it from the
923 * list of "active" interfaces.
926 if_detach(struct ifnet *ifp)
928 struct ifnet_array *old_ifnet_array;
929 struct ifg_list *ifgl;
930 struct netmsg_if_rtdel msg;
934 /* Announce that the interface is gone. */
935 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
936 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
937 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
940 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
941 * array before it is whacked.
943 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
944 * by ifnet lock, so that non-netisr threads could get a
950 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
952 ifindex2ifnet[ifp->if_index] = NULL;
953 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
957 * Remove this ifp from ifnet queue.
959 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
962 * Remove this ifp from ifnet array.
964 /* Free old ifnet array after sync all netisrs */
965 old_ifnet_array = ifnet_array;
966 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
970 ifgroup_lockmgr(LK_EXCLUSIVE);
971 while ((ifgl = TAILQ_FIRST(&ifp->if_groups)) != NULL)
972 if_delgroup_locked(ifp, ifgl->ifgl_group->ifg_group);
973 ifgroup_lockmgr(LK_RELEASE);
976 * Sync all netisrs so that the old ifnet array is no longer
977 * accessed and we can free it safely later on.
979 netmsg_service_sync();
980 ifnet_array_free(old_ifnet_array);
983 * Remove routes and flush queues.
987 if (ifp->if_flags & IFF_NPOLLING)
988 ifpoll_deregister(ifp);
992 /* Decrease the mbuf clusters/jclusters limits increased by us */
993 if (ifp->if_nmbclusters > 0)
994 mcl_inclimit(-ifp->if_nmbclusters);
995 if (ifp->if_nmbjclusters > 0)
996 mjcl_inclimit(-ifp->if_nmbjclusters);
999 if (ifq_is_enabled(&ifp->if_snd))
1000 altq_disable(&ifp->if_snd);
1001 if (ifq_is_attached(&ifp->if_snd))
1002 altq_detach(&ifp->if_snd);
1006 * Clean up all addresses.
1008 ifp->if_lladdr = NULL;
1010 if_purgeaddrs_nolink(ifp);
1011 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1014 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1015 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
1016 ("non-link ifaddr is left on if_addrheads"));
1018 ifa_ifunlink(ifa, ifp);
1020 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1021 ("there are still ifaddrs left on if_addrheads"));
1026 * Remove all IPv4 kernel structures related to ifp.
1033 * Remove all IPv6 kernel structs related to ifp. This should be done
1034 * before removing routing entries below, since IPv6 interface direct
1035 * routes are expected to be removed by the IPv6-specific kernel API.
1036 * Otherwise, the kernel will detect some inconsistency and bark it.
1042 * Delete all remaining routes using this interface
1044 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1047 netisr_domsg_global(&msg.base);
1049 SLIST_FOREACH(dp, &domains, dom_next) {
1050 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1051 (*dp->dom_ifdetach)(ifp,
1052 ifp->if_afdata[dp->dom_family]);
1055 kfree(ifp->if_addrheads, M_IFADDR);
1057 lwkt_synchronize_ipiqs("if_detach");
1058 ifq_stage_detach(&ifp->if_snd);
1060 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1061 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1063 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1064 kfree(ifsq->ifsq_stage, M_DEVBUF);
1066 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1068 kfree(ifp->if_data_pcpu, M_DEVBUF);
1074 ifgroup_lockmgr(u_int flags)
1076 return lockmgr(&ifgroup_lock, flags);
1080 * Create an empty interface group.
1082 static struct ifg_group *
1083 if_creategroup(const char *groupname)
1085 struct ifg_group *ifg;
1087 ifg = kmalloc(sizeof(*ifg), M_IFNET, M_WAITOK);
1088 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1089 ifg->ifg_refcnt = 0;
1090 ifg->ifg_carp_demoted = 0;
1091 TAILQ_INIT(&ifg->ifg_members);
1093 ifgroup_lockmgr(LK_EXCLUSIVE);
1094 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1095 ifgroup_lockmgr(LK_RELEASE);
1097 EVENTHANDLER_INVOKE(group_attach_event, ifg);
1103 * Destroy an empty interface group.
1106 if_destroygroup(struct ifg_group *ifg)
1108 KASSERT(ifg->ifg_refcnt == 0,
1109 ("trying to delete a non-empty interface group"));
1111 ifgroup_lockmgr(LK_EXCLUSIVE);
1112 TAILQ_REMOVE(&ifg_head, ifg, ifg_next);
1113 ifgroup_lockmgr(LK_RELEASE);
1115 EVENTHANDLER_INVOKE(group_detach_event, ifg);
1116 kfree(ifg, M_IFNET);
1122 * Add the interface to a group.
1123 * The target group will be created if it doesn't exist.
1126 if_addgroup(struct ifnet *ifp, const char *groupname)
1128 struct ifg_list *ifgl;
1129 struct ifg_group *ifg;
1130 struct ifg_member *ifgm;
1133 groupname[strlen(groupname) - 1] >= '0' &&
1134 groupname[strlen(groupname) - 1] <= '9')
1137 ifgroup_lockmgr(LK_SHARED);
1139 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1140 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0) {
1141 ifgroup_lockmgr(LK_RELEASE);
1146 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1147 if (strcmp(ifg->ifg_group, groupname) == 0)
1151 ifgroup_lockmgr(LK_RELEASE);
1154 ifg = if_creategroup(groupname);
1156 ifgl = kmalloc(sizeof(*ifgl), M_IFNET, M_WAITOK);
1157 ifgm = kmalloc(sizeof(*ifgm), M_IFNET, M_WAITOK);
1158 ifgl->ifgl_group = ifg;
1159 ifgm->ifgm_ifp = ifp;
1162 ifgroup_lockmgr(LK_EXCLUSIVE);
1163 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1164 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1165 ifgroup_lockmgr(LK_RELEASE);
1167 EVENTHANDLER_INVOKE(group_change_event, groupname);
1173 * Remove the interface from a group.
1174 * The group will be destroyed if it becomes empty.
1176 * The 'ifgroup_lock' must be hold exclusively when calling this.
1179 if_delgroup_locked(struct ifnet *ifp, const char *groupname)
1181 struct ifg_list *ifgl;
1182 struct ifg_member *ifgm;
1184 KKASSERT(lockstatus(&ifgroup_lock, curthread) == LK_EXCLUSIVE);
1186 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1187 if (strcmp(ifgl->ifgl_group->ifg_group, groupname) == 0)
1193 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1195 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) {
1196 if (ifgm->ifgm_ifp == ifp)
1201 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1203 ifgroup_lockmgr(LK_RELEASE);
1204 EVENTHANDLER_INVOKE(group_change_event, groupname);
1205 ifgroup_lockmgr(LK_EXCLUSIVE);
1207 kfree(ifgm, M_IFNET);
1208 ifgl->ifgl_group->ifg_refcnt--;
1211 if (ifgl->ifgl_group->ifg_refcnt == 0) {
1212 ifgroup_lockmgr(LK_RELEASE);
1213 if_destroygroup(ifgl->ifgl_group);
1214 ifgroup_lockmgr(LK_EXCLUSIVE);
1217 kfree(ifgl, M_IFNET);
1223 if_delgroup(struct ifnet *ifp, const char *groupname)
1227 ifgroup_lockmgr(LK_EXCLUSIVE);
1228 error = if_delgroup_locked(ifp, groupname);
1229 ifgroup_lockmgr(LK_RELEASE);
1235 * Store all the groups that the interface belongs to in memory
1236 * pointed to by data.
1239 if_getgroups(struct ifgroupreq *ifgr, struct ifnet *ifp)
1241 struct ifg_list *ifgl;
1242 struct ifg_req *ifgrq, *p;
1246 ifgroup_lockmgr(LK_SHARED);
1247 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1248 len += sizeof(struct ifg_req);
1249 ifgroup_lockmgr(LK_RELEASE);
1251 if (ifgr->ifgr_len == 0) {
1253 * Caller is asking how much memory should be allocated in
1254 * the next request in order to hold all the groups.
1256 ifgr->ifgr_len = len;
1258 } else if (ifgr->ifgr_len != len) {
1262 ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1266 ifgroup_lockmgr(LK_SHARED);
1268 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1269 if (len < sizeof(struct ifg_req)) {
1270 ifgroup_lockmgr(LK_RELEASE);
1274 strlcpy(p->ifgrq_group, ifgl->ifgl_group->ifg_group,
1275 sizeof(ifgrq->ifgrq_group));
1276 len -= sizeof(struct ifg_req);
1279 ifgroup_lockmgr(LK_RELEASE);
1281 error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1282 kfree(ifgrq, M_TEMP);
1290 * Store all the members of a group in memory pointed to by data.
1293 if_getgroupmembers(struct ifgroupreq *ifgr)
1295 struct ifg_group *ifg;
1296 struct ifg_member *ifgm;
1297 struct ifg_req *ifgrq, *p;
1300 ifgroup_lockmgr(LK_SHARED);
1302 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) {
1303 if (strcmp(ifg->ifg_group, ifgr->ifgr_name) == 0)
1307 ifgroup_lockmgr(LK_RELEASE);
1312 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1313 len += sizeof(struct ifg_req);
1315 ifgroup_lockmgr(LK_RELEASE);
1317 if (ifgr->ifgr_len == 0) {
1318 ifgr->ifgr_len = len;
1320 } else if (ifgr->ifgr_len != len) {
1324 ifgrq = kmalloc(len, M_TEMP, M_INTWAIT | M_NULLOK | M_ZERO);
1328 ifgroup_lockmgr(LK_SHARED);
1330 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1331 if (len < sizeof(struct ifg_req)) {
1332 ifgroup_lockmgr(LK_RELEASE);
1336 strlcpy(p->ifgrq_member, ifgm->ifgm_ifp->if_xname,
1337 sizeof(p->ifgrq_member));
1338 len -= sizeof(struct ifg_req);
1341 ifgroup_lockmgr(LK_RELEASE);
1343 error = copyout(ifgrq, ifgr->ifgr_groups, ifgr->ifgr_len);
1344 kfree(ifgrq, M_TEMP);
1352 * Delete Routes for a Network Interface
1354 * Called for each routing entry via the rnh->rnh_walktree() call above
1355 * to delete all route entries referencing a detaching network interface.
1358 * rn pointer to node in the routing table
1359 * arg argument passed to rnh->rnh_walktree() - detaching interface
1363 * errno failed - reason indicated
1367 if_rtdel(struct radix_node *rn, void *arg)
1369 struct rtentry *rt = (struct rtentry *)rn;
1370 struct ifnet *ifp = arg;
1373 if (rt->rt_ifp == ifp) {
1376 * Protect (sorta) against walktree recursion problems
1377 * with cloned routes
1379 if (!(rt->rt_flags & RTF_UP))
1382 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1383 rt_mask(rt), rt->rt_flags,
1386 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1393 static __inline boolean_t
1394 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1396 if (old_ifa == NULL)
1399 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1400 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1402 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1403 (cur_ifa->ifa_flags & IFA_ROUTE))
1409 * Locate an interface based on a complete address.
1412 ifa_ifwithaddr(struct sockaddr *addr)
1414 const struct ifnet_array *arr;
1417 arr = ifnet_array_get();
1418 for (i = 0; i < arr->ifnet_count; ++i) {
1419 struct ifnet *ifp = arr->ifnet_arr[i];
1420 struct ifaddr_container *ifac;
1422 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1423 struct ifaddr *ifa = ifac->ifa;
1425 if (ifa->ifa_addr->sa_family != addr->sa_family)
1427 if (sa_equal(addr, ifa->ifa_addr))
1429 if ((ifp->if_flags & IFF_BROADCAST) &&
1430 ifa->ifa_broadaddr &&
1431 /* IPv6 doesn't have broadcast */
1432 ifa->ifa_broadaddr->sa_len != 0 &&
1433 sa_equal(ifa->ifa_broadaddr, addr))
1441 * Locate the point to point interface with a given destination address.
1444 ifa_ifwithdstaddr(struct sockaddr *addr)
1446 const struct ifnet_array *arr;
1449 arr = ifnet_array_get();
1450 for (i = 0; i < arr->ifnet_count; ++i) {
1451 struct ifnet *ifp = arr->ifnet_arr[i];
1452 struct ifaddr_container *ifac;
1454 if (!(ifp->if_flags & IFF_POINTOPOINT))
1457 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1458 struct ifaddr *ifa = ifac->ifa;
1460 if (ifa->ifa_addr->sa_family != addr->sa_family)
1462 if (ifa->ifa_dstaddr &&
1463 sa_equal(addr, ifa->ifa_dstaddr))
1471 * Find an interface on a specific network. If many, choice
1472 * is most specific found.
1475 ifa_ifwithnet(struct sockaddr *addr)
1477 struct ifaddr *ifa_maybe = NULL;
1478 u_int af = addr->sa_family;
1479 char *addr_data = addr->sa_data, *cplim;
1480 const struct ifnet_array *arr;
1484 * AF_LINK addresses can be looked up directly by their index number,
1485 * so do that if we can.
1487 if (af == AF_LINK) {
1488 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1490 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1491 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1495 * Scan though each interface, looking for ones that have
1496 * addresses in this address family.
1498 arr = ifnet_array_get();
1499 for (i = 0; i < arr->ifnet_count; ++i) {
1500 struct ifnet *ifp = arr->ifnet_arr[i];
1501 struct ifaddr_container *ifac;
1503 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1504 struct ifaddr *ifa = ifac->ifa;
1505 char *cp, *cp2, *cp3;
1507 if (ifa->ifa_addr->sa_family != af)
1509 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1511 * This is a bit broken as it doesn't
1512 * take into account that the remote end may
1513 * be a single node in the network we are
1515 * The trouble is that we don't know the
1516 * netmask for the remote end.
1518 if (ifa->ifa_dstaddr != NULL &&
1519 sa_equal(addr, ifa->ifa_dstaddr))
1523 * if we have a special address handler,
1524 * then use it instead of the generic one.
1526 if (ifa->ifa_claim_addr) {
1527 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1535 * Scan all the bits in the ifa's address.
1536 * If a bit dissagrees with what we are
1537 * looking for, mask it with the netmask
1538 * to see if it really matters.
1539 * (A byte at a time)
1541 if (ifa->ifa_netmask == 0)
1544 cp2 = ifa->ifa_addr->sa_data;
1545 cp3 = ifa->ifa_netmask->sa_data;
1546 cplim = ifa->ifa_netmask->sa_len +
1547 (char *)ifa->ifa_netmask;
1549 if ((*cp++ ^ *cp2++) & *cp3++)
1550 goto next; /* next address! */
1552 * If the netmask of what we just found
1553 * is more specific than what we had before
1554 * (if we had one) then remember the new one
1555 * before continuing to search for an even
1556 * better one. If the netmasks are equal,
1557 * we prefer the this ifa based on the result
1560 if (ifa_maybe == NULL ||
1561 rn_refines((char *)ifa->ifa_netmask,
1562 (char *)ifa_maybe->ifa_netmask) ||
1563 (sa_equal(ifa_maybe->ifa_netmask,
1564 ifa->ifa_netmask) &&
1565 ifa_prefer(ifa, ifa_maybe)))
1574 * Find an interface address specific to an interface best matching
1578 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1580 struct ifaddr_container *ifac;
1581 char *cp, *cp2, *cp3;
1583 struct ifaddr *ifa_maybe = NULL;
1584 u_int af = addr->sa_family;
1588 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1589 struct ifaddr *ifa = ifac->ifa;
1591 if (ifa->ifa_addr->sa_family != af)
1593 if (ifa_maybe == NULL)
1595 if (ifa->ifa_netmask == NULL) {
1596 if (sa_equal(addr, ifa->ifa_addr) ||
1597 (ifa->ifa_dstaddr != NULL &&
1598 sa_equal(addr, ifa->ifa_dstaddr)))
1602 if (ifp->if_flags & IFF_POINTOPOINT) {
1603 if (sa_equal(addr, ifa->ifa_dstaddr))
1607 cp2 = ifa->ifa_addr->sa_data;
1608 cp3 = ifa->ifa_netmask->sa_data;
1609 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1610 for (; cp3 < cplim; cp3++)
1611 if ((*cp++ ^ *cp2++) & *cp3)
1621 * Default action when installing a route with a Link Level gateway.
1622 * Lookup an appropriate real ifa to point to.
1623 * This should be moved to /sys/net/link.c eventually.
1626 link_rtrequest(int cmd, struct rtentry *rt)
1629 struct sockaddr *dst;
1632 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1633 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1635 ifa = ifaof_ifpforaddr(dst, ifp);
1637 IFAFREE(rt->rt_ifa);
1640 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1641 ifa->ifa_rtrequest(cmd, rt);
1645 struct netmsg_ifroute {
1646 struct netmsg_base base;
1653 * Mark an interface down and notify protocols of the transition.
1656 if_unroute_dispatch(netmsg_t nmsg)
1658 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1659 struct ifnet *ifp = msg->ifp;
1660 int flag = msg->flag, fam = msg->fam;
1661 struct ifaddr_container *ifac;
1665 ifp->if_flags &= ~flag;
1666 getmicrotime(&ifp->if_lastchange);
1668 * The ifaddr processing in the following loop will block,
1669 * however, this function is called in netisr0, in which
1670 * ifaddr list changes happen, so we don't care about the
1671 * blockness of the ifaddr processing here.
1673 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1674 struct ifaddr *ifa = ifac->ifa;
1677 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1680 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1681 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1683 ifq_purge_all(&ifp->if_snd);
1686 netisr_replymsg(&nmsg->base, 0);
1690 if_unroute(struct ifnet *ifp, int flag, int fam)
1692 struct netmsg_ifroute msg;
1694 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1695 if_unroute_dispatch);
1699 netisr_domsg(&msg.base, 0);
1703 * Mark an interface up and notify protocols of the transition.
1706 if_route_dispatch(netmsg_t nmsg)
1708 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1709 struct ifnet *ifp = msg->ifp;
1710 int flag = msg->flag, fam = msg->fam;
1711 struct ifaddr_container *ifac;
1715 ifq_purge_all(&ifp->if_snd);
1716 ifp->if_flags |= flag;
1717 getmicrotime(&ifp->if_lastchange);
1719 * The ifaddr processing in the following loop will block,
1720 * however, this function is called in netisr0, in which
1721 * ifaddr list changes happen, so we don't care about the
1722 * blockness of the ifaddr processing here.
1724 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1725 struct ifaddr *ifa = ifac->ifa;
1728 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1731 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1732 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1739 netisr_replymsg(&nmsg->base, 0);
1743 if_route(struct ifnet *ifp, int flag, int fam)
1745 struct netmsg_ifroute msg;
1747 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1752 netisr_domsg(&msg.base, 0);
1756 * Mark an interface down and notify protocols of the transition. An
1757 * interface going down is also considered to be a synchronizing event.
1758 * We must ensure that all packet processing related to the interface
1759 * has completed before we return so e.g. the caller can free the ifnet
1760 * structure that the mbufs may be referencing.
1762 * NOTE: must be called at splnet or eqivalent.
1765 if_down(struct ifnet *ifp)
1767 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN);
1768 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1769 netmsg_service_sync();
1773 * Mark an interface up and notify protocols of
1775 * NOTE: must be called at splnet or eqivalent.
1778 if_up(struct ifnet *ifp)
1780 if_route(ifp, IFF_UP, AF_UNSPEC);
1781 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP);
1785 * Process a link state change.
1786 * NOTE: must be called at splsoftnet or equivalent.
1789 if_link_state_change(struct ifnet *ifp)
1791 int link_state = ifp->if_link_state;
1794 devctl_notify("IFNET", ifp->if_xname,
1795 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1799 * Handle interface watchdog timer routines. Called
1800 * from softclock, we decrement timers (if set) and
1801 * call the appropriate interface routine on expiration.
1804 if_slowtimo_dispatch(netmsg_t nmsg)
1806 struct globaldata *gd = mycpu;
1807 const struct ifnet_array *arr;
1813 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1816 arr = ifnet_array_get();
1817 for (i = 0; i < arr->ifnet_count; ++i) {
1818 struct ifnet *ifp = arr->ifnet_arr[i];
1822 if (if_stats_compat) {
1823 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1824 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1825 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1826 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1827 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1828 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1829 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1830 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1831 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1832 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1833 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1834 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1837 if (ifp->if_timer == 0 || --ifp->if_timer) {
1841 if (ifp->if_watchdog) {
1842 if (ifnet_tryserialize_all(ifp)) {
1843 (*ifp->if_watchdog)(ifp);
1844 ifnet_deserialize_all(ifp);
1846 /* try again next timeout */
1854 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1858 if_slowtimo(void *arg __unused)
1860 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1862 KASSERT(mycpuid == 0, ("not on cpu0"));
1864 if (lmsg->ms_flags & MSGF_DONE)
1865 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1870 * Map interface name to
1871 * interface structure pointer.
1874 ifunit(const char *name)
1879 * Search all the interfaces for this name/number
1881 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1883 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1884 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1891 ifunit_netisr(const char *name)
1893 const struct ifnet_array *arr;
1897 * Search all the interfaces for this name/number
1900 arr = ifnet_array_get();
1901 for (i = 0; i < arr->ifnet_count; ++i) {
1902 struct ifnet *ifp = arr->ifnet_arr[i];
1904 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1914 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1917 struct ifgroupreq *ifgr;
1920 int error, do_ifup = 0;
1923 size_t namelen, onamelen;
1924 char new_name[IFNAMSIZ];
1926 struct sockaddr_dl *sdl;
1930 return (ifconf(cmd, data, cred));
1935 ifr = (struct ifreq *)data;
1940 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1942 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1943 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1945 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1947 return (if_clone_destroy(ifr->ifr_name));
1948 case SIOCIFGCLONERS:
1949 return (if_clone_list((struct if_clonereq *)data));
1951 return (if_getgroupmembers((struct ifgroupreq *)data));
1957 * Nominal ioctl through interface, lookup the ifp and obtain a
1958 * lock to serialize the ifconfig ioctl operation.
1962 ifp = ifunit(ifr->ifr_name);
1971 ifr->ifr_index = ifp->if_index;
1975 ifr->ifr_flags = ifp->if_flags;
1976 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1980 ifr->ifr_reqcap = ifp->if_capabilities;
1981 ifr->ifr_curcap = ifp->if_capenable;
1985 ifr->ifr_metric = ifp->if_metric;
1989 ifr->ifr_mtu = ifp->if_mtu;
1993 ifr->ifr_tsolen = ifp->if_tsolen;
1997 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1998 sizeof(ifp->if_data));
2002 ifr->ifr_phys = ifp->if_physical;
2005 case SIOCGIFPOLLCPU:
2006 ifr->ifr_pollcpu = -1;
2009 case SIOCSIFPOLLCPU:
2013 error = priv_check_cred(cred, PRIV_ROOT, 0);
2016 new_flags = (ifr->ifr_flags & 0xffff) |
2017 (ifr->ifr_flagshigh << 16);
2018 if (ifp->if_flags & IFF_SMART) {
2019 /* Smart drivers twiddle their own routes */
2020 } else if (ifp->if_flags & IFF_UP &&
2021 (new_flags & IFF_UP) == 0) {
2023 } else if (new_flags & IFF_UP &&
2024 (ifp->if_flags & IFF_UP) == 0) {
2028 #ifdef IFPOLL_ENABLE
2029 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
2030 if (new_flags & IFF_NPOLLING)
2031 ifpoll_register(ifp);
2033 ifpoll_deregister(ifp);
2037 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
2038 (new_flags &~ IFF_CANTCHANGE);
2039 if (new_flags & IFF_PPROMISC) {
2040 /* Permanently promiscuous mode requested */
2041 ifp->if_flags |= IFF_PROMISC;
2042 } else if (ifp->if_pcount == 0) {
2043 ifp->if_flags &= ~IFF_PROMISC;
2045 if (ifp->if_ioctl) {
2046 ifnet_serialize_all(ifp);
2047 ifp->if_ioctl(ifp, cmd, data, cred);
2048 ifnet_deserialize_all(ifp);
2052 getmicrotime(&ifp->if_lastchange);
2056 error = priv_check_cred(cred, PRIV_ROOT, 0);
2059 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
2063 ifnet_serialize_all(ifp);
2064 ifp->if_ioctl(ifp, cmd, data, cred);
2065 ifnet_deserialize_all(ifp);
2069 error = priv_check_cred(cred, PRIV_ROOT, 0);
2072 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
2075 if (new_name[0] == '\0') {
2079 if (ifunit(new_name) != NULL) {
2084 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
2086 /* Announce the departure of the interface. */
2087 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
2089 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
2090 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
2091 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
2092 namelen = strlen(new_name);
2093 onamelen = sdl->sdl_nlen;
2095 * Move the address if needed. This is safe because we
2096 * allocate space for a name of length IFNAMSIZ when we
2097 * create this in if_attach().
2099 if (namelen != onamelen) {
2100 bcopy(sdl->sdl_data + onamelen,
2101 sdl->sdl_data + namelen, sdl->sdl_alen);
2103 bcopy(new_name, sdl->sdl_data, namelen);
2104 sdl->sdl_nlen = namelen;
2105 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
2106 bzero(sdl->sdl_data, onamelen);
2107 while (namelen != 0)
2108 sdl->sdl_data[--namelen] = 0xff;
2110 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2112 /* Announce the return of the interface. */
2113 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2117 error = priv_check_cred(cred, PRIV_ROOT, 0);
2120 ifp->if_metric = ifr->ifr_metric;
2121 getmicrotime(&ifp->if_lastchange);
2125 error = priv_check_cred(cred, PRIV_ROOT, 0);
2128 if (ifp->if_ioctl == NULL) {
2132 ifnet_serialize_all(ifp);
2133 error = ifp->if_ioctl(ifp, cmd, data, cred);
2134 ifnet_deserialize_all(ifp);
2136 getmicrotime(&ifp->if_lastchange);
2141 u_long oldmtu = ifp->if_mtu;
2143 error = priv_check_cred(cred, PRIV_ROOT, 0);
2146 if (ifp->if_ioctl == NULL) {
2150 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2154 ifnet_serialize_all(ifp);
2155 error = ifp->if_ioctl(ifp, cmd, data, cred);
2156 ifnet_deserialize_all(ifp);
2158 getmicrotime(&ifp->if_lastchange);
2162 * If the link MTU changed, do network layer specific procedure.
2164 if (ifp->if_mtu != oldmtu) {
2173 error = priv_check_cred(cred, PRIV_ROOT, 0);
2177 /* XXX need driver supplied upper limit */
2178 if (ifr->ifr_tsolen <= 0) {
2182 ifp->if_tsolen = ifr->ifr_tsolen;
2187 error = priv_check_cred(cred, PRIV_ROOT, 0);
2191 /* Don't allow group membership on non-multicast interfaces. */
2192 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2197 /* Don't let users screw up protocols' entries. */
2198 if (ifr->ifr_addr.sa_family != AF_LINK) {
2203 if (cmd == SIOCADDMULTI) {
2204 struct ifmultiaddr *ifma;
2205 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2207 error = if_delmulti(ifp, &ifr->ifr_addr);
2210 getmicrotime(&ifp->if_lastchange);
2213 case SIOCSIFPHYADDR:
2214 case SIOCDIFPHYADDR:
2216 case SIOCSIFPHYADDR_IN6:
2218 case SIOCSLIFPHYADDR:
2220 case SIOCSIFGENERIC:
2221 error = priv_check_cred(cred, PRIV_ROOT, 0);
2224 if (ifp->if_ioctl == 0) {
2228 ifnet_serialize_all(ifp);
2229 error = ifp->if_ioctl(ifp, cmd, data, cred);
2230 ifnet_deserialize_all(ifp);
2232 getmicrotime(&ifp->if_lastchange);
2236 ifs = (struct ifstat *)data;
2237 ifs->ascii[0] = '\0';
2239 case SIOCGIFPSRCADDR:
2240 case SIOCGIFPDSTADDR:
2241 case SIOCGLIFPHYADDR:
2243 case SIOCGIFGENERIC:
2244 if (ifp->if_ioctl == NULL) {
2248 ifnet_serialize_all(ifp);
2249 error = ifp->if_ioctl(ifp, cmd, data, cred);
2250 ifnet_deserialize_all(ifp);
2254 error = priv_check_cred(cred, PRIV_ROOT, 0);
2257 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2258 ifr->ifr_addr.sa_len);
2259 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2263 ifgr = (struct ifgroupreq *)ifr;
2264 if ((error = priv_check_cred(cred, PRIV_NET_ADDIFGROUP, 0)))
2266 if ((error = if_addgroup(ifp, ifgr->ifgr_group)))
2271 ifgr = (struct ifgroupreq *)ifr;
2272 if ((error = priv_check_cred(cred, PRIV_NET_DELIFGROUP, 0)))
2274 if ((error = if_delgroup(ifp, ifgr->ifgr_group)))
2279 ifgr = (struct ifgroupreq *)ifr;
2280 if ((error = if_getgroups(ifgr, ifp)))
2285 oif_flags = ifp->if_flags;
2286 if (so->so_proto == 0) {
2290 error = so_pru_control_direct(so, cmd, data, ifp);
2292 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2294 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2295 if (ifp->if_flags & IFF_UP) {
2310 * Set/clear promiscuous mode on interface ifp based on the truth value
2311 * of pswitch. The calls are reference counted so that only the first
2312 * "on" request actually has an effect, as does the final "off" request.
2313 * Results are undefined if the "off" and "on" requests are not matched.
2316 ifpromisc(struct ifnet *ifp, int pswitch)
2322 oldflags = ifp->if_flags;
2323 if (ifp->if_flags & IFF_PPROMISC) {
2324 /* Do nothing if device is in permanently promiscuous mode */
2325 ifp->if_pcount += pswitch ? 1 : -1;
2330 * If the device is not configured up, we cannot put it in
2333 if ((ifp->if_flags & IFF_UP) == 0)
2335 if (ifp->if_pcount++ != 0)
2337 ifp->if_flags |= IFF_PROMISC;
2338 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2341 if (--ifp->if_pcount > 0)
2343 ifp->if_flags &= ~IFF_PROMISC;
2344 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2347 ifr.ifr_flags = ifp->if_flags;
2348 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2349 ifnet_serialize_all(ifp);
2350 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2351 ifnet_deserialize_all(ifp);
2355 ifp->if_flags = oldflags;
2360 * Return interface configuration
2361 * of system. List may be used
2362 * in later ioctl's (above) to get
2363 * other information.
2366 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2368 struct ifconf *ifc = (struct ifconf *)data;
2370 struct sockaddr *sa;
2371 struct ifreq ifr, *ifrp;
2372 int space = ifc->ifc_len, error = 0;
2374 ifrp = ifc->ifc_req;
2377 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2378 struct ifaddr_container *ifac, *ifac_mark;
2379 struct ifaddr_marker mark;
2380 struct ifaddrhead *head;
2383 if (space <= sizeof ifr)
2387 * Zero the stack declared structure first to prevent
2388 * memory disclosure.
2390 bzero(&ifr, sizeof(ifr));
2391 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2392 >= sizeof(ifr.ifr_name)) {
2393 error = ENAMETOOLONG;
2398 * Add a marker, since copyout() could block and during that
2399 * period the list could be changed. Inserting the marker to
2400 * the header of the list will not cause trouble for the code
2401 * assuming that the first element of the list is AF_LINK; the
2402 * marker will be moved to the next position w/o blocking.
2404 ifa_marker_init(&mark, ifp);
2405 ifac_mark = &mark.ifac;
2406 head = &ifp->if_addrheads[mycpuid];
2409 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2410 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2411 struct ifaddr *ifa = ifac->ifa;
2413 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2414 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2417 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2420 if (space <= sizeof ifr)
2423 if (cred->cr_prison &&
2424 prison_if(cred, sa))
2428 * Keep a reference on this ifaddr, so that it will
2429 * not be destroyed when its address is copied to
2430 * the userland, which could block.
2433 if (sa->sa_len <= sizeof(*sa)) {
2435 error = copyout(&ifr, ifrp, sizeof ifr);
2438 if (space < (sizeof ifr) + sa->sa_len -
2443 space -= sa->sa_len - sizeof(*sa);
2444 error = copyout(&ifr, ifrp,
2445 sizeof ifr.ifr_name);
2447 error = copyout(sa, &ifrp->ifr_addr,
2449 ifrp = (struct ifreq *)
2450 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2455 space -= sizeof ifr;
2457 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2461 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2462 error = copyout(&ifr, ifrp, sizeof ifr);
2465 space -= sizeof ifr;
2471 ifc->ifc_len -= space;
2476 * Just like if_promisc(), but for all-multicast-reception mode.
2479 if_allmulti(struct ifnet *ifp, int onswitch)
2487 if (ifp->if_amcount++ == 0) {
2488 ifp->if_flags |= IFF_ALLMULTI;
2489 ifr.ifr_flags = ifp->if_flags;
2490 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2491 ifnet_serialize_all(ifp);
2492 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2494 ifnet_deserialize_all(ifp);
2497 if (ifp->if_amcount > 1) {
2500 ifp->if_amcount = 0;
2501 ifp->if_flags &= ~IFF_ALLMULTI;
2502 ifr.ifr_flags = ifp->if_flags;
2503 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2504 ifnet_serialize_all(ifp);
2505 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2507 ifnet_deserialize_all(ifp);
2519 * Add a multicast listenership to the interface in question.
2520 * The link layer provides a routine which converts
2523 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2524 struct ifmultiaddr **retifma)
2526 struct sockaddr *llsa, *dupsa;
2528 struct ifmultiaddr *ifma;
2530 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2533 * If the matching multicast address already exists
2534 * then don't add a new one, just add a reference
2536 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2537 if (sa_equal(sa, ifma->ifma_addr)) {
2538 ifma->ifma_refcount++;
2546 * Give the link layer a chance to accept/reject it, and also
2547 * find out which AF_LINK address this maps to, if it isn't one
2550 if (ifp->if_resolvemulti) {
2551 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2558 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2559 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2560 bcopy(sa, dupsa, sa->sa_len);
2562 ifma->ifma_addr = dupsa;
2563 ifma->ifma_lladdr = llsa;
2564 ifma->ifma_ifp = ifp;
2565 ifma->ifma_refcount = 1;
2566 ifma->ifma_protospec = NULL;
2567 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2569 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2574 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2575 if (sa_equal(ifma->ifma_addr, llsa))
2579 ifma->ifma_refcount++;
2581 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2582 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2583 bcopy(llsa, dupsa, llsa->sa_len);
2584 ifma->ifma_addr = dupsa;
2585 ifma->ifma_ifp = ifp;
2586 ifma->ifma_refcount = 1;
2587 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2591 * We are certain we have added something, so call down to the
2592 * interface to let them know about it.
2595 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2601 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2602 struct ifmultiaddr **retifma)
2606 ifnet_serialize_all(ifp);
2607 error = if_addmulti_serialized(ifp, sa, retifma);
2608 ifnet_deserialize_all(ifp);
2614 * Remove a reference to a multicast address on this interface. Yell
2615 * if the request does not match an existing membership.
2618 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2620 struct ifmultiaddr *ifma;
2622 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2624 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2625 if (sa_equal(sa, ifma->ifma_addr))
2630 if (ifma->ifma_refcount > 1) {
2631 ifma->ifma_refcount--;
2635 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2636 sa = ifma->ifma_lladdr;
2637 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2639 * Make sure the interface driver is notified
2640 * in the case of a link layer mcast group being left.
2642 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2643 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2644 kfree(ifma->ifma_addr, M_IFMADDR);
2645 kfree(ifma, M_IFMADDR);
2650 * Now look for the link-layer address which corresponds to
2651 * this network address. It had been squirreled away in
2652 * ifma->ifma_lladdr for this purpose (so we don't have
2653 * to call ifp->if_resolvemulti() again), and we saved that
2654 * value in sa above. If some nasty deleted the
2655 * link-layer address out from underneath us, we can deal because
2656 * the address we stored was is not the same as the one which was
2657 * in the record for the link-layer address. (So we don't complain
2660 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2661 if (sa_equal(sa, ifma->ifma_addr))
2666 if (ifma->ifma_refcount > 1) {
2667 ifma->ifma_refcount--;
2671 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2672 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2673 kfree(ifma->ifma_addr, M_IFMADDR);
2674 kfree(sa, M_IFMADDR);
2675 kfree(ifma, M_IFMADDR);
2681 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2685 ifnet_serialize_all(ifp);
2686 error = if_delmulti_serialized(ifp, sa);
2687 ifnet_deserialize_all(ifp);
2693 * Delete all multicast group membership for an interface.
2694 * Should be used to quickly flush all multicast filters.
2697 if_delallmulti_serialized(struct ifnet *ifp)
2699 struct ifmultiaddr *ifma, mark;
2702 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2704 bzero(&sa, sizeof(sa));
2705 sa.sa_family = AF_UNSPEC;
2706 sa.sa_len = sizeof(sa);
2708 bzero(&mark, sizeof(mark));
2709 mark.ifma_addr = &sa;
2711 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2712 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2713 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2714 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2717 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2720 if_delmulti_serialized(ifp, ifma->ifma_addr);
2722 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2727 * Set the link layer address on an interface.
2729 * At this time we only support certain types of interfaces,
2730 * and we don't allow the length of the address to change.
2733 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2735 struct sockaddr_dl *sdl;
2738 sdl = IF_LLSOCKADDR(ifp);
2741 if (len != sdl->sdl_alen) /* don't allow length to change */
2743 switch (ifp->if_type) {
2744 case IFT_ETHER: /* these types use struct arpcom */
2747 case IFT_IEEE8023ADLAG:
2748 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2749 bcopy(lladdr, LLADDR(sdl), len);
2755 * If the interface is already up, we need
2756 * to re-init it in order to reprogram its
2759 ifnet_serialize_all(ifp);
2760 if ((ifp->if_flags & IFF_UP) != 0) {
2762 struct ifaddr_container *ifac;
2765 ifp->if_flags &= ~IFF_UP;
2766 ifr.ifr_flags = ifp->if_flags;
2767 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2768 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2770 ifp->if_flags |= IFF_UP;
2771 ifr.ifr_flags = ifp->if_flags;
2772 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2773 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2777 * Also send gratuitous ARPs to notify other nodes about
2778 * the address change.
2780 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2781 struct ifaddr *ifa = ifac->ifa;
2783 if (ifa->ifa_addr != NULL &&
2784 ifa->ifa_addr->sa_family == AF_INET)
2785 arp_gratuitous(ifp, ifa);
2789 ifnet_deserialize_all(ifp);
2793 struct ifmultiaddr *
2794 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2796 struct ifmultiaddr *ifma;
2798 /* TODO: need ifnet_serialize_main */
2799 ifnet_serialize_all(ifp);
2800 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2801 if (sa_equal(ifma->ifma_addr, sa))
2803 ifnet_deserialize_all(ifp);
2809 * This function locates the first real ethernet MAC from a network
2810 * card and loads it into node, returning 0 on success or ENOENT if
2811 * no suitable interfaces were found. It is used by the uuid code to
2812 * generate a unique 6-byte number.
2815 if_getanyethermac(uint16_t *node, int minlen)
2818 struct sockaddr_dl *sdl;
2821 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2822 if (ifp->if_type != IFT_ETHER)
2824 sdl = IF_LLSOCKADDR(ifp);
2825 if (sdl->sdl_alen < minlen)
2827 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2837 * The name argument must be a pointer to storage which will last as
2838 * long as the interface does. For physical devices, the result of
2839 * device_get_name(dev) is a good choice and for pseudo-devices a
2840 * static string works well.
2843 if_initname(struct ifnet *ifp, const char *name, int unit)
2845 ifp->if_dname = name;
2846 ifp->if_dunit = unit;
2847 if (unit != IF_DUNIT_NONE)
2848 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2850 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2854 if_printf(struct ifnet *ifp, const char *fmt, ...)
2859 retval = kprintf("%s: ", ifp->if_xname);
2860 __va_start(ap, fmt);
2861 retval += kvprintf(fmt, ap);
2867 if_alloc(uint8_t type)
2873 * XXX temporary hack until arpcom is setup in if_l2com
2875 if (type == IFT_ETHER)
2876 size = sizeof(struct arpcom);
2878 size = sizeof(struct ifnet);
2880 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2882 ifp->if_type = type;
2884 if (if_com_alloc[type] != NULL) {
2885 ifp->if_l2com = if_com_alloc[type](type, ifp);
2886 if (ifp->if_l2com == NULL) {
2887 kfree(ifp, M_IFNET);
2895 if_free(struct ifnet *ifp)
2897 kfree(ifp, M_IFNET);
2901 ifq_set_classic(struct ifaltq *ifq)
2903 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2904 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2908 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2909 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2913 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2914 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2915 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2916 KASSERT(request != NULL, ("request is not specified"));
2918 ifq->altq_mapsubq = mapsubq;
2919 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2920 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2922 ifsq->ifsq_enqueue = enqueue;
2923 ifsq->ifsq_dequeue = dequeue;
2924 ifsq->ifsq_request = request;
2929 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2932 classq_add(&ifsq->ifsq_norm, m);
2933 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2937 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2940 classq_add(&ifsq->ifsq_prio, m);
2941 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2942 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2945 static struct mbuf *
2946 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2950 m = classq_get(&ifsq->ifsq_norm);
2952 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2956 static struct mbuf *
2957 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2961 m = classq_get(&ifsq->ifsq_prio);
2963 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2964 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2970 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2971 struct altq_pktattr *pa __unused)
2976 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2977 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2978 struct mbuf *m_drop;
2980 if (m->m_flags & M_PRIO) {
2982 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2983 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2984 /* Try dropping some from normal queue. */
2985 m_drop = ifsq_norm_dequeue(ifsq);
2988 m_drop = ifsq_prio_dequeue(ifsq);
2990 m_drop = ifsq_norm_dequeue(ifsq);
2992 if (m_drop != NULL) {
2993 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2998 * No old packets could be dropped!
2999 * NOTE: Caller increases oqdrops.
3004 if (m->m_flags & M_PRIO)
3005 ifsq_prio_enqueue(ifsq, m);
3007 ifsq_norm_enqueue(ifsq, m);
3013 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
3019 m = classq_head(&ifsq->ifsq_prio);
3021 m = classq_head(&ifsq->ifsq_norm);
3025 m = ifsq_prio_dequeue(ifsq);
3027 m = ifsq_norm_dequeue(ifsq);
3031 panic("unsupported ALTQ dequeue op: %d", op);
3037 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
3044 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
3052 panic("unsupported ALTQ request: %d", req);
3058 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
3060 struct ifnet *ifp = ifsq_get_ifp(ifsq);
3061 int running = 0, need_sched;
3064 * Try to do direct ifnet.if_start on the subqueue first, if there is
3065 * contention on the subqueue hardware serializer, ifnet.if_start on
3066 * the subqueue will be scheduled on the subqueue owner CPU.
3068 if (!ifsq_tryserialize_hw(ifsq)) {
3070 * Subqueue hardware serializer contention happened,
3071 * ifnet.if_start on the subqueue is scheduled on
3072 * the subqueue owner CPU, and we keep going.
3074 ifsq_ifstart_schedule(ifsq, 1);
3078 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
3079 ifp->if_start(ifp, ifsq);
3080 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
3083 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
3085 ifsq_deserialize_hw(ifsq);
3089 * More data need to be transmitted, ifnet.if_start on the
3090 * subqueue is scheduled on the subqueue owner CPU, and we
3092 * NOTE: ifnet.if_start subqueue interlock is not released.
3094 ifsq_ifstart_schedule(ifsq, force_sched);
3099 * Subqeue packets staging mechanism:
3101 * The packets enqueued into the subqueue are staged to a certain amount
3102 * before the ifnet.if_start on the subqueue is called. In this way, the
3103 * driver could avoid writing to hardware registers upon every packet,
3104 * instead, hardware registers could be written when certain amount of
3105 * packets are put onto hardware TX ring. The measurement on several modern
3106 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
3107 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
3108 * datagrams are transmitted at 1.48Mpps. The performance improvement by
3109 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
3110 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
3112 * Subqueue packets staging is performed for two entry points into drivers'
3113 * transmission function:
3114 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
3115 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
3117 * Subqueue packets staging will be stopped upon any of the following
3119 * - If the count of packets enqueued on the current CPU is great than or
3120 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
3121 * - If the total length of packets enqueued on the current CPU is great
3122 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
3123 * cut from the hardware's MTU mainly bacause a full TCP segment's size
3124 * is usually less than hardware's MTU.
3125 * - ifsq_ifstart_schedule() is not pending on the current CPU and
3126 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
3128 * - The if_start_rollup(), which is registered as low priority netisr
3129 * rollup function, is called; probably because no more work is pending
3133 * Currently subqueue packet staging is only performed in netisr threads.
3136 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3138 struct ifaltq *ifq = &ifp->if_snd;
3139 struct ifaltq_subque *ifsq;
3140 int error, start = 0, len, mcast = 0, avoid_start = 0;
3141 struct ifsubq_stage_head *head = NULL;
3142 struct ifsubq_stage *stage = NULL;
3143 struct globaldata *gd = mycpu;
3144 struct thread *td = gd->gd_curthread;
3146 crit_enter_quick(td);
3148 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3149 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3151 len = m->m_pkthdr.len;
3152 if (m->m_flags & M_MCAST)
3155 if (td->td_type == TD_TYPE_NETISR) {
3156 head = &ifsubq_stage_heads[mycpuid];
3157 stage = ifsq_get_stage(ifsq, mycpuid);
3160 stage->stg_len += len;
3161 if (stage->stg_cnt < ifsq_stage_cntmax &&
3162 stage->stg_len < (ifp->if_mtu - max_protohdr))
3167 error = ifsq_enqueue_locked(ifsq, m, pa);
3169 IFNET_STAT_INC(ifp, oqdrops, 1);
3170 if (!ifsq_data_ready(ifsq)) {
3171 ALTQ_SQ_UNLOCK(ifsq);
3172 crit_exit_quick(td);
3177 if (!ifsq_is_started(ifsq)) {
3179 ALTQ_SQ_UNLOCK(ifsq);
3182 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3183 ifsq_stage_insert(head, stage);
3185 IFNET_STAT_INC(ifp, obytes, len);
3187 IFNET_STAT_INC(ifp, omcasts, 1);
3188 crit_exit_quick(td);
3193 * Hold the subqueue interlock of ifnet.if_start
3195 ifsq_set_started(ifsq);
3198 ALTQ_SQ_UNLOCK(ifsq);
3201 IFNET_STAT_INC(ifp, obytes, len);
3203 IFNET_STAT_INC(ifp, omcasts, 1);
3206 if (stage != NULL) {
3207 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3208 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3210 ifsq_stage_remove(head, stage);
3211 ifsq_ifstart_schedule(ifsq, 1);
3213 crit_exit_quick(td);
3217 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3218 ifsq_stage_remove(head, stage);
3226 crit_exit_quick(td);
3230 ifsq_ifstart_try(ifsq, 0);
3232 crit_exit_quick(td);
3237 ifa_create(int size)
3242 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3244 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3247 * Make ifa_container availabel on all CPUs, since they
3248 * could be accessed by any threads.
3250 ifa->ifa_containers =
3251 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3252 M_IFADDR, M_INTWAIT | M_ZERO);
3254 ifa->ifa_ncnt = ncpus;
3255 for (i = 0; i < ncpus; ++i) {
3256 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3258 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3260 ifac->ifa_refcnt = 1;
3263 kprintf("alloc ifa %p %d\n", ifa, size);
3269 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3271 struct ifaddr *ifa = ifac->ifa;
3273 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3274 KKASSERT(ifac->ifa_refcnt == 0);
3275 KASSERT(ifac->ifa_listmask == 0,
3276 ("ifa is still on %#x lists", ifac->ifa_listmask));
3278 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3280 #ifdef IFADDR_DEBUG_VERBOSE
3281 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3284 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3285 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3286 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3288 kprintf("free ifa %p\n", ifa);
3290 kfree(ifa->ifa_containers, M_IFADDR);
3291 kfree(ifa, M_IFADDR);
3296 ifa_iflink_dispatch(netmsg_t nmsg)
3298 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3299 struct ifaddr *ifa = msg->ifa;
3300 struct ifnet *ifp = msg->ifp;
3302 struct ifaddr_container *ifac;
3306 ifac = &ifa->ifa_containers[cpu];
3307 ASSERT_IFAC_VALID(ifac);
3308 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3309 ("ifaddr is on if_addrheads"));
3311 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3313 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3315 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3319 netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3323 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3325 struct netmsg_ifaddr msg;
3327 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3328 0, ifa_iflink_dispatch);
3333 netisr_domsg(&msg.base, 0);
3337 ifa_ifunlink_dispatch(netmsg_t nmsg)
3339 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3340 struct ifaddr *ifa = msg->ifa;
3341 struct ifnet *ifp = msg->ifp;
3343 struct ifaddr_container *ifac;
3347 ifac = &ifa->ifa_containers[cpu];
3348 ASSERT_IFAC_VALID(ifac);
3349 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3350 ("ifaddr is not on if_addrhead"));
3352 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3353 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3357 netisr_forwardmsg_all(&nmsg->base, cpu + 1);
3361 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3363 struct netmsg_ifaddr msg;
3365 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3366 0, ifa_ifunlink_dispatch);
3370 netisr_domsg(&msg.base, 0);
3374 ifa_destroy_dispatch(netmsg_t nmsg)
3376 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3379 netisr_forwardmsg_all(&nmsg->base, mycpuid + 1);
3383 ifa_destroy(struct ifaddr *ifa)
3385 struct netmsg_ifaddr msg;
3387 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3388 0, ifa_destroy_dispatch);
3391 netisr_domsg(&msg.base, 0);
3395 if_start_rollup(void)
3397 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3398 struct ifsubq_stage *stage;
3402 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3403 struct ifaltq_subque *ifsq = stage->stg_subq;
3406 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3408 ifsq_stage_remove(head, stage);
3411 ifsq_ifstart_schedule(ifsq, 1);
3416 if (!ifsq_is_started(ifsq)) {
3418 * Hold the subqueue interlock of
3421 ifsq_set_started(ifsq);
3424 ALTQ_SQ_UNLOCK(ifsq);
3427 ifsq_ifstart_try(ifsq, 1);
3429 KKASSERT((stage->stg_flags &
3430 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3437 ifnetinit(void *dummy __unused)
3441 /* XXX netisr_ncpus */
3442 for (i = 0; i < ncpus; ++i)
3443 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3444 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3448 if_register_com_alloc(u_char type,
3449 if_com_alloc_t *a, if_com_free_t *f)
3452 KASSERT(if_com_alloc[type] == NULL,
3453 ("if_register_com_alloc: %d already registered", type));
3454 KASSERT(if_com_free[type] == NULL,
3455 ("if_register_com_alloc: %d free already registered", type));
3457 if_com_alloc[type] = a;
3458 if_com_free[type] = f;
3462 if_deregister_com_alloc(u_char type)
3465 KASSERT(if_com_alloc[type] != NULL,
3466 ("if_deregister_com_alloc: %d not registered", type));
3467 KASSERT(if_com_free[type] != NULL,
3468 ("if_deregister_com_alloc: %d free not registered", type));
3469 if_com_alloc[type] = NULL;
3470 if_com_free[type] = NULL;
3474 ifq_set_maxlen(struct ifaltq *ifq, int len)
3476 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3480 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3482 return ALTQ_SUBQ_INDEX_DEFAULT;
3486 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3489 return (cpuid % ifq->altq_subq_mappriv);
3493 ifsq_watchdog(void *arg)
3495 struct ifsubq_watchdog *wd = arg;
3498 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3501 ifp = ifsq_get_ifp(wd->wd_subq);
3502 if (ifnet_tryserialize_all(ifp)) {
3503 wd->wd_watchdog(wd->wd_subq);
3504 ifnet_deserialize_all(ifp);
3506 /* try again next timeout */
3510 ifsq_watchdog_reset(wd);
3514 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3516 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3517 ifsq_get_cpuid(wd->wd_subq));
3521 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3522 ifsq_watchdog_t watchdog)
3524 callout_init_mp(&wd->wd_callout);
3527 wd->wd_watchdog = watchdog;
3531 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3534 ifsq_watchdog_reset(wd);
3538 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3541 callout_stop(&wd->wd_callout);
3547 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3548 ("try holding ifnet lock in netisr"));
3549 mtx_lock(&ifnet_mtx);
3555 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3556 ("try holding ifnet lock in netisr"));
3557 mtx_unlock(&ifnet_mtx);
3560 static struct ifnet_array *
3561 ifnet_array_alloc(int count)
3563 struct ifnet_array *arr;
3565 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3567 arr->ifnet_count = count;
3573 ifnet_array_free(struct ifnet_array *arr)
3575 if (arr == &ifnet_array0)
3577 kfree(arr, M_IFNET);
3580 static struct ifnet_array *
3581 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3583 struct ifnet_array *arr;
3586 KASSERT(old_arr->ifnet_count >= 0,
3587 ("invalid ifnet array count %d", old_arr->ifnet_count));
3588 count = old_arr->ifnet_count + 1;
3589 arr = ifnet_array_alloc(count);
3592 * Save the old ifnet array and append this ifp to the end of
3593 * the new ifnet array.
3595 for (i = 0; i < old_arr->ifnet_count; ++i) {
3596 KASSERT(old_arr->ifnet_arr[i] != ifp,
3597 ("%s is already in ifnet array", ifp->if_xname));
3598 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3600 KASSERT(i == count - 1,
3601 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3602 ifp->if_xname, count - 1, i));
3603 arr->ifnet_arr[i] = ifp;
3608 static struct ifnet_array *
3609 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3611 struct ifnet_array *arr;
3612 int count, i, idx, found = 0;
3614 KASSERT(old_arr->ifnet_count > 0,
3615 ("invalid ifnet array count %d", old_arr->ifnet_count));
3616 count = old_arr->ifnet_count - 1;
3617 arr = ifnet_array_alloc(count);
3620 * Save the old ifnet array, but skip this ifp.
3623 for (i = 0; i < old_arr->ifnet_count; ++i) {
3624 if (old_arr->ifnet_arr[i] == ifp) {
3626 ("dup %s is in ifnet array", ifp->if_xname));
3630 KASSERT(idx < count,
3631 ("invalid ifnet array index %d, count %d", idx, count));
3632 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3635 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3636 KASSERT(idx == count,
3637 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3638 ifp->if_xname, count, idx));
3643 const struct ifnet_array *
3644 ifnet_array_get(void)
3646 const struct ifnet_array *ret;
3648 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3650 /* Make sure 'ret' is really used. */
3656 ifnet_array_isempty(void)
3658 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3659 if (ifnet_array->ifnet_count == 0)
3666 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3670 memset(mark, 0, sizeof(*mark));
3673 mark->ifac.ifa = ifa;
3675 ifa->ifa_addr = &mark->addr;
3676 ifa->ifa_dstaddr = &mark->dstaddr;
3677 ifa->ifa_netmask = &mark->netmask;
3682 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3685 KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3687 if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3688 ring_cnt = ring_cntmax;
3689 if (ring_cnt > netisr_ncpus)
3690 ring_cnt = netisr_ncpus;
3695 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3699 KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3700 KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3704 offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3705 for (i = 0; i < rm->rm_cnt; ++i) {
3706 rm->rm_cpumap[i] = offset + i;
3707 KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3708 ("invalid cpumap[%d] = %d, offset %d", i,
3709 rm->rm_cpumap[i], offset));
3713 static struct if_ringmap *
3714 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3717 struct if_ringmap *rm;
3718 int i, grid = 0, prev_grid;
3720 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3721 rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3722 M_DEVBUF, M_WAITOK | M_ZERO);
3724 rm->rm_cnt = ring_cnt;
3725 if (flags & RINGMAP_FLAG_POWEROF2)
3726 rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3728 prev_grid = netisr_ncpus;
3729 for (i = 0; i < netisr_ncpus; ++i) {
3730 if (netisr_ncpus % (i + 1) != 0)
3733 grid = netisr_ncpus / (i + 1);
3734 if (rm->rm_cnt > grid) {
3739 if (rm->rm_cnt > netisr_ncpus / (i + 2))
3743 if_ringmap_set_grid(dev, rm, grid);
3749 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3752 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3753 RINGMAP_FLAG_NONE));
3757 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3760 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3761 RINGMAP_FLAG_POWEROF2));
3765 if_ringmap_free(struct if_ringmap *rm)
3768 kfree(rm, M_DEVBUF);
3772 * Align the two ringmaps.
3774 * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3778 * CPU 0 1 2 3 4 5 6 7
3779 * NIC_RX n0 n1 n2 n3
3784 * CPU 0 1 2 3 4 5 6 7
3785 * NIC_RX n0 n1 n2 n3
3789 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3792 if (rm0->rm_grid > rm1->rm_grid)
3793 if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3794 else if (rm0->rm_grid < rm1->rm_grid)
3795 if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3799 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3801 int subset_grid, cnt, divisor, mod, offset, i;
3802 struct if_ringmap *subset_rm, *rm;
3803 int old_rm0_grid, old_rm1_grid;
3805 if (rm0->rm_grid == rm1->rm_grid)
3808 /* Save grid for later use */
3809 old_rm0_grid = rm0->rm_grid;
3810 old_rm1_grid = rm1->rm_grid;
3812 if_ringmap_align(dev, rm0, rm1);
3815 * Re-shuffle rings to get more even distribution.
3817 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3819 * CPU 0 1 2 3 4 5 6 7 8 9 10 11
3821 * NIC_RX a0 a1 a2 a3 b0 b1 b2 b3 c0 c1 c2 c3
3822 * NIC_TX A0 A1 B0 B1 C0 C1
3824 * NIC_RX d0 d1 d2 d3 e0 e1 e2 e3 f0 f1 f2 f3
3825 * NIC_TX D0 D1 E0 E1 F0 F1
3828 if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3830 subset_grid = old_rm1_grid;
3833 } else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3835 subset_grid = old_rm0_grid;
3839 /* No space to shuffle. */
3843 mod = cnt / subset_grid;
3845 divisor = netisr_ncpus / rm->rm_grid;
3846 offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3848 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3849 subset_rm->rm_cpumap[i] += offset;
3850 KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3851 ("match: invalid cpumap[%d] = %d, offset %d",
3852 i, subset_rm->rm_cpumap[i], offset));
3855 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3858 for (j = 0; j < rm->rm_cnt; ++j) {
3859 if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3862 KASSERT(j < rm->rm_cnt,
3863 ("subset cpumap[%d] = %d not found in superset",
3864 i, subset_rm->rm_cpumap[i]));
3870 if_ringmap_count(const struct if_ringmap *rm)
3873 return (rm->rm_cnt);
3877 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3880 KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3881 return (rm->rm_cpumap[ring]);
3885 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3887 int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3889 KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3890 ("invalid redirect table entries %d", table_nent));
3893 for (i = 0; i < NETISR_CPUMAX; ++i) {
3894 table[i] = grid_idx++ % rm->rm_cnt;
3896 if (grid_idx == rm->rm_grid)
3901 * Make the ring distributed more evenly for the remainder
3904 * e.g. 12 netisrs, rm contains 8 rings.
3906 * Redirect table before:
3908 * 0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
3909 * 4 5 6 7 0 1 2 3 0 1 2 3 4 5 6 7
3910 * 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3
3913 * Redirect table after being patched (pX, patched entries):
3915 * 0 1 2 3 4 5 6 7 p0 p1 p2 p3 0 1 2 3
3916 * 4 5 6 7 p4 p5 p6 p7 0 1 2 3 4 5 6 7
3917 * p0 p1 p2 p3 0 1 2 3 4 5 6 7 p4 p5 p6 p7
3920 patch_cnt = rm->rm_grid % rm->rm_cnt;
3923 patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3925 grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3927 for (i = 0; i < grid_cnt; ++i) {
3930 for (j = 0; j < patch_cnt; ++j) {
3933 fix_idx = (i * rm->rm_grid) + patch_off + j;
3934 if (fix_idx >= NETISR_CPUMAX)
3936 table[fix_idx] = grid_idx++ % rm->rm_cnt;
3941 * If the device supports larger redirect table, duplicate
3942 * the first NETISR_CPUMAX entries to the rest of the table,
3943 * so that it matches upper layer's expectation:
3944 * (hash & NETISR_CPUMASK) % netisr_ncpus
3946 ncopy = table_nent / NETISR_CPUMAX;
3947 for (i = 1; i < ncopy; ++i) {
3948 memcpy(&table[i * NETISR_CPUMAX], table,
3949 NETISR_CPUMAX * sizeof(table[0]));
3951 if (if_ringmap_dumprdr) {
3952 for (i = 0; i < table_nent; ++i) {
3953 if (i != 0 && i % 16 == 0)
3955 kprintf("%03d ", table[i]);
3962 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
3964 struct if_ringmap *rm = arg1;
3967 for (i = 0; i < rm->rm_cnt; ++i) {
3968 int cpu = rm->rm_cpumap[i];
3970 error = SYSCTL_OUT(req, &cpu, sizeof(cpu));