2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.3 (Berkeley) 1/4/94
30 * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
33 #include "opt_inet6.h"
35 #include "opt_ifpoll.h"
37 #include <sys/param.h>
38 #include <sys/malloc.h>
40 #include <sys/systm.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/socketops.h>
47 #include <sys/kernel.h>
49 #include <sys/mutex.h>
50 #include <sys/sockio.h>
51 #include <sys/syslog.h>
52 #include <sys/sysctl.h>
53 #include <sys/domain.h>
54 #include <sys/thread.h>
55 #include <sys/serialize.h>
58 #include <sys/thread2.h>
59 #include <sys/msgport2.h>
60 #include <sys/mutex2.h>
63 #include <net/if_arp.h>
64 #include <net/if_dl.h>
65 #include <net/if_types.h>
66 #include <net/if_var.h>
67 #include <net/ifq_var.h>
68 #include <net/radix.h>
69 #include <net/route.h>
70 #include <net/if_clone.h>
71 #include <net/netisr2.h>
72 #include <net/netmsg2.h>
74 #include <machine/atomic.h>
75 #include <machine/stdarg.h>
76 #include <machine/smp.h>
78 #if defined(INET) || defined(INET6)
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82 #include <netinet/if_ether.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet6/in6_ifattach.h>
89 struct netmsg_ifaddr
{
90 struct netmsg_base base
;
96 struct ifsubq_stage_head
{
97 TAILQ_HEAD(, ifsubq_stage
) stg_head
;
101 * System initialization
103 static void if_attachdomain(void *);
104 static void if_attachdomain1(struct ifnet
*);
105 static int ifconf(u_long
, caddr_t
, struct ucred
*);
106 static void ifinit(void *);
107 static void ifnetinit(void *);
108 static void if_slowtimo(void *);
109 static void link_rtrequest(int, struct rtentry
*);
110 static int if_rtdel(struct radix_node
*, void *);
111 static void if_slowtimo_dispatch(netmsg_t
);
113 /* Helper functions */
114 static void ifsq_watchdog_reset(struct ifsubq_watchdog
*);
115 static int if_delmulti_serialized(struct ifnet
*, struct sockaddr
*);
116 static struct ifnet_array
*ifnet_array_alloc(int);
117 static void ifnet_array_free(struct ifnet_array
*);
118 static struct ifnet_array
*ifnet_array_add(struct ifnet
*,
119 const struct ifnet_array
*);
120 static struct ifnet_array
*ifnet_array_del(struct ifnet
*,
121 const struct ifnet_array
*);
125 * XXX: declare here to avoid to include many inet6 related files..
126 * should be more generalized?
128 extern void nd6_setmtu(struct ifnet
*);
131 SYSCTL_NODE(_net
, PF_LINK
, link
, CTLFLAG_RW
, 0, "Link layers");
132 SYSCTL_NODE(_net_link
, 0, generic
, CTLFLAG_RW
, 0, "Generic link-management");
134 static int ifsq_stage_cntmax
= 4;
135 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax
);
136 SYSCTL_INT(_net_link
, OID_AUTO
, stage_cntmax
, CTLFLAG_RW
,
137 &ifsq_stage_cntmax
, 0, "ifq staging packet count max");
139 static int if_stats_compat
= 0;
140 SYSCTL_INT(_net_link
, OID_AUTO
, stats_compat
, CTLFLAG_RW
,
141 &if_stats_compat
, 0, "Compat the old ifnet stats");
143 SYSINIT(interfaces
, SI_SUB_PROTO_IF
, SI_ORDER_FIRST
, ifinit
, NULL
);
144 SYSINIT(ifnet
, SI_SUB_PRE_DRIVERS
, SI_ORDER_ANY
, ifnetinit
, NULL
);
146 static if_com_alloc_t
*if_com_alloc
[256];
147 static if_com_free_t
*if_com_free
[256];
149 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
150 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
151 MALLOC_DEFINE(M_IFNET
, "ifnet", "interface structure");
153 int ifqmaxlen
= IFQ_MAXLEN
;
154 struct ifnethead ifnet
= TAILQ_HEAD_INITIALIZER(ifnet
);
156 static struct ifnet_array ifnet_array0
;
157 static struct ifnet_array
*ifnet_array
= &ifnet_array0
;
159 static struct callout if_slowtimo_timer
;
160 static struct netmsg_base if_slowtimo_netmsg
;
163 struct ifnet
**ifindex2ifnet
= NULL
;
164 static struct mtx ifnet_mtx
= MTX_INITIALIZER("ifnet");
166 static struct ifsubq_stage_head ifsubq_stage_heads
[MAXCPU
];
169 #define IFQ_KTR_STRING "ifq=%p"
170 #define IFQ_KTR_ARGS struct ifaltq *ifq
172 #define KTR_IFQ KTR_ALL
174 KTR_INFO_MASTER(ifq
);
175 KTR_INFO(KTR_IFQ
, ifq
, enqueue
, 0, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
176 KTR_INFO(KTR_IFQ
, ifq
, dequeue
, 1, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
177 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
179 #define IF_START_KTR_STRING "ifp=%p"
180 #define IF_START_KTR_ARGS struct ifnet *ifp
182 #define KTR_IF_START KTR_ALL
184 KTR_INFO_MASTER(if_start
);
185 KTR_INFO(KTR_IF_START
, if_start
, run
, 0,
186 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
187 KTR_INFO(KTR_IF_START
, if_start
, sched
, 1,
188 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
189 KTR_INFO(KTR_IF_START
, if_start
, avoid
, 2,
190 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
191 KTR_INFO(KTR_IF_START
, if_start
, contend_sched
, 3,
192 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
193 KTR_INFO(KTR_IF_START
, if_start
, chase_sched
, 4,
194 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
195 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
198 TAILQ_HEAD(, ifg_group
) ifg_head
= TAILQ_HEAD_INITIALIZER(ifg_head
);
201 * Network interface utility routines.
203 * Routines with ifa_ifwith* names take sockaddr *'s as
212 callout_init_mp(&if_slowtimo_timer
);
213 netmsg_init(&if_slowtimo_netmsg
, NULL
, &netisr_adone_rport
,
214 MSGF_PRIORITY
, if_slowtimo_dispatch
);
216 /* XXX is this necessary? */
218 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
219 if (ifp
->if_snd
.altq_maxlen
== 0) {
220 if_printf(ifp
, "XXX: driver didn't set altq_maxlen\n");
221 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
226 /* Start if_slowtimo */
227 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg
.lmsg
);
231 ifsq_ifstart_ipifunc(void *arg
)
233 struct ifaltq_subque
*ifsq
= arg
;
234 struct lwkt_msg
*lmsg
= ifsq_get_ifstart_lmsg(ifsq
, mycpuid
);
237 if (lmsg
->ms_flags
& MSGF_DONE
)
238 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid
), lmsg
);
243 ifsq_stage_remove(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
245 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
246 TAILQ_REMOVE(&head
->stg_head
, stage
, stg_link
);
247 stage
->stg_flags
&= ~(IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
);
253 ifsq_stage_insert(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
255 KKASSERT((stage
->stg_flags
&
256 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
257 stage
->stg_flags
|= IFSQ_STAGE_FLAG_QUED
;
258 TAILQ_INSERT_TAIL(&head
->stg_head
, stage
, stg_link
);
262 * Schedule ifnet.if_start on the subqueue owner CPU
265 ifsq_ifstart_schedule(struct ifaltq_subque
*ifsq
, int force
)
269 if (!force
&& curthread
->td_type
== TD_TYPE_NETISR
&&
270 ifsq_stage_cntmax
> 0) {
271 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
275 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
276 ifsq_stage_insert(&ifsubq_stage_heads
[mycpuid
], stage
);
277 stage
->stg_flags
|= IFSQ_STAGE_FLAG_SCHED
;
281 cpu
= ifsq_get_cpuid(ifsq
);
283 lwkt_send_ipiq(globaldata_find(cpu
), ifsq_ifstart_ipifunc
, ifsq
);
285 ifsq_ifstart_ipifunc(ifsq
);
290 * This function will release ifnet.if_start subqueue interlock,
291 * if ifnet.if_start for the subqueue does not need to be scheduled
294 ifsq_ifstart_need_schedule(struct ifaltq_subque
*ifsq
, int running
)
296 if (!running
|| ifsq_is_empty(ifsq
)
298 || ifsq
->ifsq_altq
->altq_tbr
!= NULL
303 * ifnet.if_start subqueue interlock is released, if:
304 * 1) Hardware can not take any packets, due to
305 * o interface is marked down
306 * o hardware queue is full (ifsq_is_oactive)
307 * Under the second situation, hardware interrupt
308 * or polling(4) will call/schedule ifnet.if_start
309 * on the subqueue when hardware queue is ready
310 * 2) There is no packet in the subqueue.
311 * Further ifq_dispatch or ifq_handoff will call/
312 * schedule ifnet.if_start on the subqueue.
313 * 3) TBR is used and it does not allow further
315 * TBR callout will call ifnet.if_start on the
318 if (!running
|| !ifsq_data_ready(ifsq
)) {
319 ifsq_clr_started(ifsq
);
320 ALTQ_SQ_UNLOCK(ifsq
);
323 ALTQ_SQ_UNLOCK(ifsq
);
329 ifsq_ifstart_dispatch(netmsg_t msg
)
331 struct lwkt_msg
*lmsg
= &msg
->base
.lmsg
;
332 struct ifaltq_subque
*ifsq
= lmsg
->u
.ms_resultp
;
333 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
334 struct globaldata
*gd
= mycpu
;
335 int running
= 0, need_sched
;
339 lwkt_replymsg(lmsg
, 0); /* reply ASAP */
341 if (gd
->gd_cpuid
!= ifsq_get_cpuid(ifsq
)) {
343 * We need to chase the subqueue owner CPU change.
345 ifsq_ifstart_schedule(ifsq
, 1);
350 ifsq_serialize_hw(ifsq
);
351 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
352 ifp
->if_start(ifp
, ifsq
);
353 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
356 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
357 ifsq_deserialize_hw(ifsq
);
361 * More data need to be transmitted, ifnet.if_start is
362 * scheduled on the subqueue owner CPU, and we keep going.
363 * NOTE: ifnet.if_start subqueue interlock is not released.
365 ifsq_ifstart_schedule(ifsq
, 0);
371 /* Device driver ifnet.if_start helper function */
373 ifsq_devstart(struct ifaltq_subque
*ifsq
)
375 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
378 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq
);
381 if (ifsq_is_started(ifsq
) || !ifsq_data_ready(ifsq
)) {
382 ALTQ_SQ_UNLOCK(ifsq
);
385 ifsq_set_started(ifsq
);
386 ALTQ_SQ_UNLOCK(ifsq
);
388 ifp
->if_start(ifp
, ifsq
);
390 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
393 if (ifsq_ifstart_need_schedule(ifsq
, running
)) {
395 * More data need to be transmitted, ifnet.if_start is
396 * scheduled on ifnet's CPU, and we keep going.
397 * NOTE: ifnet.if_start interlock is not released.
399 ifsq_ifstart_schedule(ifsq
, 0);
404 if_devstart(struct ifnet
*ifp
)
406 ifsq_devstart(ifq_get_subq_default(&ifp
->if_snd
));
409 /* Device driver ifnet.if_start schedule helper function */
411 ifsq_devstart_sched(struct ifaltq_subque
*ifsq
)
413 ifsq_ifstart_schedule(ifsq
, 1);
417 if_devstart_sched(struct ifnet
*ifp
)
419 ifsq_devstart_sched(ifq_get_subq_default(&ifp
->if_snd
));
423 if_default_serialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
425 lwkt_serialize_enter(ifp
->if_serializer
);
429 if_default_deserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
431 lwkt_serialize_exit(ifp
->if_serializer
);
435 if_default_tryserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
437 return lwkt_serialize_try(ifp
->if_serializer
);
442 if_default_serialize_assert(struct ifnet
*ifp
,
443 enum ifnet_serialize slz __unused
,
444 boolean_t serialized
)
447 ASSERT_SERIALIZED(ifp
->if_serializer
);
449 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
454 * Attach an interface to the list of "active" interfaces.
456 * The serializer is optional.
459 if_attach(struct ifnet
*ifp
, lwkt_serialize_t serializer
)
462 int namelen
, masklen
;
463 struct sockaddr_dl
*sdl
, *sdl_addr
;
466 struct ifnet
**old_ifindex2ifnet
= NULL
;
467 struct ifnet_array
*old_ifnet_array
;
470 static int if_indexlim
= 8;
472 if (ifp
->if_serialize
!= NULL
) {
473 KASSERT(ifp
->if_deserialize
!= NULL
&&
474 ifp
->if_tryserialize
!= NULL
&&
475 ifp
->if_serialize_assert
!= NULL
,
476 ("serialize functions are partially setup"));
479 * If the device supplies serialize functions,
480 * then clear if_serializer to catch any invalid
481 * usage of this field.
483 KASSERT(serializer
== NULL
,
484 ("both serialize functions and default serializer "
486 ifp
->if_serializer
= NULL
;
488 KASSERT(ifp
->if_deserialize
== NULL
&&
489 ifp
->if_tryserialize
== NULL
&&
490 ifp
->if_serialize_assert
== NULL
,
491 ("serialize functions are partially setup"));
492 ifp
->if_serialize
= if_default_serialize
;
493 ifp
->if_deserialize
= if_default_deserialize
;
494 ifp
->if_tryserialize
= if_default_tryserialize
;
496 ifp
->if_serialize_assert
= if_default_serialize_assert
;
500 * The serializer can be passed in from the device,
501 * allowing the same serializer to be used for both
502 * the interrupt interlock and the device queue.
503 * If not specified, the netif structure will use an
504 * embedded serializer.
506 if (serializer
== NULL
) {
507 serializer
= &ifp
->if_default_serializer
;
508 lwkt_serialize_init(serializer
);
510 ifp
->if_serializer
= serializer
;
515 * The old code would work if the interface passed a pre-existing
516 * chain of ifaddrs to this code. We don't trust our callers to
517 * properly initialize the tailq, however, so we no longer allow
518 * this unlikely case.
520 ifp
->if_addrheads
= kmalloc(ncpus
* sizeof(struct ifaddrhead
),
521 M_IFADDR
, M_WAITOK
| M_ZERO
);
522 for (i
= 0; i
< ncpus
; ++i
)
523 TAILQ_INIT(&ifp
->if_addrheads
[i
]);
525 TAILQ_INIT(&ifp
->if_multiaddrs
);
526 TAILQ_INIT(&ifp
->if_groups
);
527 getmicrotime(&ifp
->if_lastchange
);
530 * create a Link Level name for this device
532 namelen
= strlen(ifp
->if_xname
);
533 masklen
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
534 socksize
= masklen
+ ifp
->if_addrlen
;
535 if (socksize
< sizeof(*sdl
))
536 socksize
= sizeof(*sdl
);
537 socksize
= RT_ROUNDUP(socksize
);
538 ifa
= ifa_create(sizeof(struct ifaddr
) + 2 * socksize
);
539 sdl
= sdl_addr
= (struct sockaddr_dl
*)(ifa
+ 1);
540 sdl
->sdl_len
= socksize
;
541 sdl
->sdl_family
= AF_LINK
;
542 bcopy(ifp
->if_xname
, sdl
->sdl_data
, namelen
);
543 sdl
->sdl_nlen
= namelen
;
544 sdl
->sdl_type
= ifp
->if_type
;
545 ifp
->if_lladdr
= ifa
;
547 ifa
->ifa_rtrequest
= link_rtrequest
;
548 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
549 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
550 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
551 sdl
->sdl_len
= masklen
;
553 sdl
->sdl_data
[--namelen
] = 0xff;
554 ifa_iflink(ifa
, ifp
, 0 /* Insert head */);
556 ifp
->if_data_pcpu
= kmalloc_cachealign(
557 ncpus
* sizeof(struct ifdata_pcpu
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
559 if (ifp
->if_mapsubq
== NULL
)
560 ifp
->if_mapsubq
= ifq_mapsubq_default
;
564 ifq
->altq_disc
= NULL
;
565 ifq
->altq_flags
&= ALTQF_CANTCHANGE
;
566 ifq
->altq_tbr
= NULL
;
569 if (ifq
->altq_subq_cnt
<= 0)
570 ifq
->altq_subq_cnt
= 1;
571 ifq
->altq_subq
= kmalloc_cachealign(
572 ifq
->altq_subq_cnt
* sizeof(struct ifaltq_subque
),
573 M_DEVBUF
, M_WAITOK
| M_ZERO
);
575 if (ifq
->altq_maxlen
== 0) {
576 if_printf(ifp
, "driver didn't set altq_maxlen\n");
577 ifq_set_maxlen(ifq
, ifqmaxlen
);
580 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
581 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
583 ALTQ_SQ_LOCK_INIT(ifsq
);
584 ifsq
->ifsq_index
= q
;
586 ifsq
->ifsq_altq
= ifq
;
587 ifsq
->ifsq_ifp
= ifp
;
589 ifsq
->ifsq_maxlen
= ifq
->altq_maxlen
;
590 ifsq
->ifsq_maxbcnt
= ifsq
->ifsq_maxlen
* MCLBYTES
;
591 ifsq
->ifsq_prepended
= NULL
;
592 ifsq
->ifsq_started
= 0;
593 ifsq
->ifsq_hw_oactive
= 0;
594 ifsq_set_cpuid(ifsq
, 0);
595 if (ifp
->if_serializer
!= NULL
)
596 ifsq_set_hw_serialize(ifsq
, ifp
->if_serializer
);
599 kmalloc_cachealign(ncpus
* sizeof(struct ifsubq_stage
),
600 M_DEVBUF
, M_WAITOK
| M_ZERO
);
601 for (i
= 0; i
< ncpus
; ++i
)
602 ifsq
->ifsq_stage
[i
].stg_subq
= ifsq
;
604 ifsq
->ifsq_ifstart_nmsg
=
605 kmalloc(ncpus
* sizeof(struct netmsg_base
),
606 M_LWKTMSG
, M_WAITOK
);
607 for (i
= 0; i
< ncpus
; ++i
) {
608 netmsg_init(&ifsq
->ifsq_ifstart_nmsg
[i
], NULL
,
609 &netisr_adone_rport
, 0, ifsq_ifstart_dispatch
);
610 ifsq
->ifsq_ifstart_nmsg
[i
].lmsg
.u
.ms_resultp
= ifsq
;
613 ifq_set_classic(ifq
);
616 * Increase mbuf cluster/jcluster limits for the mbufs that
617 * could sit on the device queues for quite some time.
619 if (ifp
->if_nmbclusters
> 0)
620 mcl_inclimit(ifp
->if_nmbclusters
);
621 if (ifp
->if_nmbjclusters
> 0)
622 mjcl_inclimit(ifp
->if_nmbjclusters
);
625 * Install this ifp into ifindex2inet, ifnet queue and ifnet
626 * array after it is setup.
628 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
629 * by ifnet lock, so that non-netisr threads could get a
634 /* Don't update if_index until ifindex2ifnet is setup */
635 ifp
->if_index
= if_index
+ 1;
636 sdl_addr
->sdl_index
= ifp
->if_index
;
639 * Install this ifp into ifindex2ifnet
641 if (ifindex2ifnet
== NULL
|| ifp
->if_index
>= if_indexlim
) {
649 n
= if_indexlim
* sizeof(*q
);
650 q
= kmalloc(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
651 if (ifindex2ifnet
!= NULL
) {
652 bcopy(ifindex2ifnet
, q
, n
/2);
653 /* Free old ifindex2ifnet after sync all netisrs */
654 old_ifindex2ifnet
= ifindex2ifnet
;
658 ifindex2ifnet
[ifp
->if_index
] = ifp
;
660 * Update if_index after this ifp is installed into ifindex2ifnet,
661 * so that netisrs could get a consistent view of ifindex2ifnet.
664 if_index
= ifp
->if_index
;
667 * Install this ifp into ifnet array.
669 /* Free old ifnet array after sync all netisrs */
670 old_ifnet_array
= ifnet_array
;
671 ifnet_array
= ifnet_array_add(ifp
, old_ifnet_array
);
674 * Install this ifp into ifnet queue.
676 TAILQ_INSERT_TAIL(&ifnetlist
, ifp
, if_link
);
681 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
682 * are no longer accessed and we can free them safely later on.
684 netmsg_service_sync();
685 if (old_ifindex2ifnet
!= NULL
)
686 kfree(old_ifindex2ifnet
, M_IFADDR
);
687 ifnet_array_free(old_ifnet_array
);
689 if (!SLIST_EMPTY(&domains
))
690 if_attachdomain1(ifp
);
692 /* Announce the interface. */
693 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
694 devctl_notify("IFNET", ifp
->if_xname
, "ATTACH", NULL
);
695 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
699 if_attachdomain(void *dummy
)
704 TAILQ_FOREACH(ifp
, &ifnetlist
, if_list
)
705 if_attachdomain1(ifp
);
708 SYSINIT(domainifattach
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_FIRST
,
709 if_attachdomain
, NULL
);
712 if_attachdomain1(struct ifnet
*ifp
)
718 /* address family dependent data region */
719 bzero(ifp
->if_afdata
, sizeof(ifp
->if_afdata
));
720 SLIST_FOREACH(dp
, &domains
, dom_next
)
721 if (dp
->dom_ifattach
)
722 ifp
->if_afdata
[dp
->dom_family
] =
723 (*dp
->dom_ifattach
)(ifp
);
728 * Purge all addresses whose type is _not_ AF_LINK
731 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg
)
733 struct lwkt_msg
*lmsg
= &nmsg
->lmsg
;
734 struct ifnet
*ifp
= lmsg
->u
.ms_resultp
;
735 struct ifaddr_container
*ifac
, *next
;
740 * The ifaddr processing in the following loop will block,
741 * however, this function is called in netisr0, in which
742 * ifaddr list changes happen, so we don't care about the
743 * blockness of the ifaddr processing here.
745 TAILQ_FOREACH_MUTABLE(ifac
, &ifp
->if_addrheads
[mycpuid
],
747 struct ifaddr
*ifa
= ifac
->ifa
;
750 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
753 /* Leave link ifaddr as it is */
754 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
757 /* XXX: Ugly!! ad hoc just for INET */
758 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
759 struct ifaliasreq ifr
;
760 #ifdef IFADDR_DEBUG_VERBOSE
763 kprintf("purge in4 addr %p: ", ifa
);
764 for (i
= 0; i
< ncpus
; ++i
)
765 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
769 bzero(&ifr
, sizeof ifr
);
770 ifr
.ifra_addr
= *ifa
->ifa_addr
;
771 if (ifa
->ifa_dstaddr
)
772 ifr
.ifra_broadaddr
= *ifa
->ifa_dstaddr
;
773 if (in_control(SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
779 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET6
) {
780 #ifdef IFADDR_DEBUG_VERBOSE
783 kprintf("purge in6 addr %p: ", ifa
);
784 for (i
= 0; i
< ncpus
; ++i
)
785 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
790 /* ifp_addrhead is already updated */
794 ifa_ifunlink(ifa
, ifp
);
798 lwkt_replymsg(lmsg
, 0);
802 if_purgeaddrs_nolink(struct ifnet
*ifp
)
804 struct netmsg_base nmsg
;
805 struct lwkt_msg
*lmsg
= &nmsg
.lmsg
;
807 ASSERT_CANDOMSG_NETISR0(curthread
);
809 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
, 0,
810 if_purgeaddrs_nolink_dispatch
);
811 lmsg
->u
.ms_resultp
= ifp
;
812 lwkt_domsg(netisr_cpuport(0), lmsg
, 0);
816 ifq_stage_detach_handler(netmsg_t nmsg
)
818 struct ifaltq
*ifq
= nmsg
->lmsg
.u
.ms_resultp
;
821 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
822 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
823 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
825 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
)
826 ifsq_stage_remove(&ifsubq_stage_heads
[mycpuid
], stage
);
828 lwkt_replymsg(&nmsg
->lmsg
, 0);
832 ifq_stage_detach(struct ifaltq
*ifq
)
834 struct netmsg_base base
;
837 netmsg_init(&base
, NULL
, &curthread
->td_msgport
, 0,
838 ifq_stage_detach_handler
);
839 base
.lmsg
.u
.ms_resultp
= ifq
;
841 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
842 lwkt_domsg(netisr_cpuport(cpu
), &base
.lmsg
, 0);
845 struct netmsg_if_rtdel
{
846 struct netmsg_base base
;
851 if_rtdel_dispatch(netmsg_t msg
)
853 struct netmsg_if_rtdel
*rmsg
= (void *)msg
;
857 for (i
= 1; i
<= AF_MAX
; i
++) {
858 struct radix_node_head
*rnh
;
860 if ((rnh
= rt_tables
[cpu
][i
]) == NULL
)
862 rnh
->rnh_walktree(rnh
, if_rtdel
, rmsg
->ifp
);
867 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
869 lwkt_replymsg(&rmsg
->base
.lmsg
, 0);
873 * Detach an interface, removing it from the
874 * list of "active" interfaces.
877 if_detach(struct ifnet
*ifp
)
879 struct ifnet_array
*old_ifnet_array
;
880 struct netmsg_if_rtdel msg
;
884 /* Announce that the interface is gone. */
885 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
886 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
887 devctl_notify("IFNET", ifp
->if_xname
, "DETACH", NULL
);
890 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
891 * array before it is whacked.
893 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
894 * by ifnet lock, so that non-netisr threads could get a
900 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
902 ifindex2ifnet
[ifp
->if_index
] = NULL
;
903 while (if_index
> 0 && ifindex2ifnet
[if_index
] == NULL
)
907 * Remove this ifp from ifnet queue.
909 TAILQ_REMOVE(&ifnetlist
, ifp
, if_link
);
912 * Remove this ifp from ifnet array.
914 /* Free old ifnet array after sync all netisrs */
915 old_ifnet_array
= ifnet_array
;
916 ifnet_array
= ifnet_array_del(ifp
, old_ifnet_array
);
921 * Sync all netisrs so that the old ifnet array is no longer
922 * accessed and we can free it safely later on.
924 netmsg_service_sync();
925 ifnet_array_free(old_ifnet_array
);
928 * Remove routes and flush queues.
932 if (ifp
->if_flags
& IFF_NPOLLING
)
933 ifpoll_deregister(ifp
);
937 /* Decrease the mbuf clusters/jclusters limits increased by us */
938 if (ifp
->if_nmbclusters
> 0)
939 mcl_inclimit(-ifp
->if_nmbclusters
);
940 if (ifp
->if_nmbjclusters
> 0)
941 mjcl_inclimit(-ifp
->if_nmbjclusters
);
944 if (ifq_is_enabled(&ifp
->if_snd
))
945 altq_disable(&ifp
->if_snd
);
946 if (ifq_is_attached(&ifp
->if_snd
))
947 altq_detach(&ifp
->if_snd
);
951 * Clean up all addresses.
953 ifp
->if_lladdr
= NULL
;
955 if_purgeaddrs_nolink(ifp
);
956 if (!TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
])) {
959 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
960 KASSERT(ifa
->ifa_addr
->sa_family
== AF_LINK
,
961 ("non-link ifaddr is left on if_addrheads"));
963 ifa_ifunlink(ifa
, ifp
);
965 KASSERT(TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
]),
966 ("there are still ifaddrs left on if_addrheads"));
971 * Remove all IPv4 kernel structures related to ifp.
978 * Remove all IPv6 kernel structs related to ifp. This should be done
979 * before removing routing entries below, since IPv6 interface direct
980 * routes are expected to be removed by the IPv6-specific kernel API.
981 * Otherwise, the kernel will detect some inconsistency and bark it.
987 * Delete all remaining routes using this interface
989 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, MSGF_PRIORITY
,
992 rt_domsg_global(&msg
.base
);
994 SLIST_FOREACH(dp
, &domains
, dom_next
)
995 if (dp
->dom_ifdetach
&& ifp
->if_afdata
[dp
->dom_family
])
996 (*dp
->dom_ifdetach
)(ifp
,
997 ifp
->if_afdata
[dp
->dom_family
]);
999 kfree(ifp
->if_addrheads
, M_IFADDR
);
1001 lwkt_synchronize_ipiqs("if_detach");
1002 ifq_stage_detach(&ifp
->if_snd
);
1004 for (q
= 0; q
< ifp
->if_snd
.altq_subq_cnt
; ++q
) {
1005 struct ifaltq_subque
*ifsq
= &ifp
->if_snd
.altq_subq
[q
];
1007 kfree(ifsq
->ifsq_ifstart_nmsg
, M_LWKTMSG
);
1008 kfree(ifsq
->ifsq_stage
, M_DEVBUF
);
1010 kfree(ifp
->if_snd
.altq_subq
, M_DEVBUF
);
1012 kfree(ifp
->if_data_pcpu
, M_DEVBUF
);
1018 * Create interface group without members
1021 if_creategroup(const char *groupname
)
1023 struct ifg_group
*ifg
= NULL
;
1025 if ((ifg
= (struct ifg_group
*)kmalloc(sizeof(struct ifg_group
),
1026 M_TEMP
, M_NOWAIT
)) == NULL
)
1029 strlcpy(ifg
->ifg_group
, groupname
, sizeof(ifg
->ifg_group
));
1030 ifg
->ifg_refcnt
= 0;
1031 ifg
->ifg_carp_demoted
= 0;
1032 TAILQ_INIT(&ifg
->ifg_members
);
1034 pfi_attach_ifgroup(ifg
);
1036 TAILQ_INSERT_TAIL(&ifg_head
, ifg
, ifg_next
);
1042 * Add a group to an interface
1045 if_addgroup(struct ifnet
*ifp
, const char *groupname
)
1047 struct ifg_list
*ifgl
;
1048 struct ifg_group
*ifg
= NULL
;
1049 struct ifg_member
*ifgm
;
1051 if (groupname
[0] && groupname
[strlen(groupname
) - 1] >= '0' &&
1052 groupname
[strlen(groupname
) - 1] <= '9')
1055 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1056 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1059 if ((ifgl
= kmalloc(sizeof(*ifgl
), M_TEMP
, M_NOWAIT
)) == NULL
)
1062 if ((ifgm
= kmalloc(sizeof(*ifgm
), M_TEMP
, M_NOWAIT
)) == NULL
) {
1063 kfree(ifgl
, M_TEMP
);
1067 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1068 if (!strcmp(ifg
->ifg_group
, groupname
))
1071 if (ifg
== NULL
&& (ifg
= if_creategroup(groupname
)) == NULL
) {
1072 kfree(ifgl
, M_TEMP
);
1073 kfree(ifgm
, M_TEMP
);
1078 ifgl
->ifgl_group
= ifg
;
1079 ifgm
->ifgm_ifp
= ifp
;
1081 TAILQ_INSERT_TAIL(&ifg
->ifg_members
, ifgm
, ifgm_next
);
1082 TAILQ_INSERT_TAIL(&ifp
->if_groups
, ifgl
, ifgl_next
);
1085 pfi_group_change(groupname
);
1092 * Remove a group from an interface
1095 if_delgroup(struct ifnet
*ifp
, const char *groupname
)
1097 struct ifg_list
*ifgl
;
1098 struct ifg_member
*ifgm
;
1100 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1101 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1106 TAILQ_REMOVE(&ifp
->if_groups
, ifgl
, ifgl_next
);
1108 TAILQ_FOREACH(ifgm
, &ifgl
->ifgl_group
->ifg_members
, ifgm_next
)
1109 if (ifgm
->ifgm_ifp
== ifp
)
1113 TAILQ_REMOVE(&ifgl
->ifgl_group
->ifg_members
, ifgm
, ifgm_next
);
1114 kfree(ifgm
, M_TEMP
);
1117 if (--ifgl
->ifgl_group
->ifg_refcnt
== 0) {
1118 TAILQ_REMOVE(&ifg_head
, ifgl
->ifgl_group
, ifg_next
);
1120 pfi_detach_ifgroup(ifgl
->ifgl_group
);
1122 kfree(ifgl
->ifgl_group
, M_TEMP
);
1125 kfree(ifgl
, M_TEMP
);
1128 pfi_group_change(groupname
);
1135 * Stores all groups from an interface in memory pointed
1139 if_getgroup(caddr_t data
, struct ifnet
*ifp
)
1142 struct ifg_list
*ifgl
;
1143 struct ifg_req ifgrq
, *ifgp
;
1144 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1146 if (ifgr
->ifgr_len
== 0) {
1147 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1148 ifgr
->ifgr_len
+= sizeof(struct ifg_req
);
1152 len
= ifgr
->ifgr_len
;
1153 ifgp
= ifgr
->ifgr_groups
;
1154 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
) {
1155 if (len
< sizeof(ifgrq
))
1157 bzero(&ifgrq
, sizeof ifgrq
);
1158 strlcpy(ifgrq
.ifgrq_group
, ifgl
->ifgl_group
->ifg_group
,
1159 sizeof(ifgrq
.ifgrq_group
));
1160 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1161 sizeof(struct ifg_req
))))
1163 len
-= sizeof(ifgrq
);
1171 * Stores all members of a group in memory pointed to by data
1174 if_getgroupmembers(caddr_t data
)
1176 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1177 struct ifg_group
*ifg
;
1178 struct ifg_member
*ifgm
;
1179 struct ifg_req ifgrq
, *ifgp
;
1182 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1183 if (!strcmp(ifg
->ifg_group
, ifgr
->ifgr_name
))
1188 if (ifgr
->ifgr_len
== 0) {
1189 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
)
1190 ifgr
->ifgr_len
+= sizeof(ifgrq
);
1194 len
= ifgr
->ifgr_len
;
1195 ifgp
= ifgr
->ifgr_groups
;
1196 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
) {
1197 if (len
< sizeof(ifgrq
))
1199 bzero(&ifgrq
, sizeof ifgrq
);
1200 strlcpy(ifgrq
.ifgrq_member
, ifgm
->ifgm_ifp
->if_xname
,
1201 sizeof(ifgrq
.ifgrq_member
));
1202 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1203 sizeof(struct ifg_req
))))
1205 len
-= sizeof(ifgrq
);
1213 * Delete Routes for a Network Interface
1215 * Called for each routing entry via the rnh->rnh_walktree() call above
1216 * to delete all route entries referencing a detaching network interface.
1219 * rn pointer to node in the routing table
1220 * arg argument passed to rnh->rnh_walktree() - detaching interface
1224 * errno failed - reason indicated
1228 if_rtdel(struct radix_node
*rn
, void *arg
)
1230 struct rtentry
*rt
= (struct rtentry
*)rn
;
1231 struct ifnet
*ifp
= arg
;
1234 if (rt
->rt_ifp
== ifp
) {
1237 * Protect (sorta) against walktree recursion problems
1238 * with cloned routes
1240 if (!(rt
->rt_flags
& RTF_UP
))
1243 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1244 rt_mask(rt
), rt
->rt_flags
,
1247 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1254 static __inline boolean_t
1255 ifa_prefer(const struct ifaddr
*cur_ifa
, const struct ifaddr
*old_ifa
)
1257 if (old_ifa
== NULL
)
1260 if ((old_ifa
->ifa_ifp
->if_flags
& IFF_UP
) == 0 &&
1261 (cur_ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1263 if ((old_ifa
->ifa_flags
& IFA_ROUTE
) == 0 &&
1264 (cur_ifa
->ifa_flags
& IFA_ROUTE
))
1270 * Locate an interface based on a complete address.
1273 ifa_ifwithaddr(struct sockaddr
*addr
)
1275 const struct ifnet_array
*arr
;
1278 arr
= ifnet_array_get();
1279 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1280 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1281 struct ifaddr_container
*ifac
;
1283 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1284 struct ifaddr
*ifa
= ifac
->ifa
;
1286 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1288 if (sa_equal(addr
, ifa
->ifa_addr
))
1290 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1291 ifa
->ifa_broadaddr
&&
1292 /* IPv6 doesn't have broadcast */
1293 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1294 sa_equal(ifa
->ifa_broadaddr
, addr
))
1302 * Locate the point to point interface with a given destination address.
1305 ifa_ifwithdstaddr(struct sockaddr
*addr
)
1307 const struct ifnet_array
*arr
;
1310 arr
= ifnet_array_get();
1311 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1312 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1313 struct ifaddr_container
*ifac
;
1315 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
1318 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1319 struct ifaddr
*ifa
= ifac
->ifa
;
1321 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1323 if (ifa
->ifa_dstaddr
&&
1324 sa_equal(addr
, ifa
->ifa_dstaddr
))
1332 * Find an interface on a specific network. If many, choice
1333 * is most specific found.
1336 ifa_ifwithnet(struct sockaddr
*addr
)
1338 struct ifaddr
*ifa_maybe
= NULL
;
1339 u_int af
= addr
->sa_family
;
1340 char *addr_data
= addr
->sa_data
, *cplim
;
1341 const struct ifnet_array
*arr
;
1345 * AF_LINK addresses can be looked up directly by their index number,
1346 * so do that if we can.
1348 if (af
== AF_LINK
) {
1349 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
1351 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
1352 return (ifindex2ifnet
[sdl
->sdl_index
]->if_lladdr
);
1356 * Scan though each interface, looking for ones that have
1357 * addresses in this address family.
1359 arr
= ifnet_array_get();
1360 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1361 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1362 struct ifaddr_container
*ifac
;
1364 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1365 struct ifaddr
*ifa
= ifac
->ifa
;
1366 char *cp
, *cp2
, *cp3
;
1368 if (ifa
->ifa_addr
->sa_family
!= af
)
1370 if (af
== AF_INET
&& ifp
->if_flags
& IFF_POINTOPOINT
) {
1372 * This is a bit broken as it doesn't
1373 * take into account that the remote end may
1374 * be a single node in the network we are
1376 * The trouble is that we don't know the
1377 * netmask for the remote end.
1379 if (ifa
->ifa_dstaddr
!= NULL
&&
1380 sa_equal(addr
, ifa
->ifa_dstaddr
))
1384 * if we have a special address handler,
1385 * then use it instead of the generic one.
1387 if (ifa
->ifa_claim_addr
) {
1388 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
1396 * Scan all the bits in the ifa's address.
1397 * If a bit dissagrees with what we are
1398 * looking for, mask it with the netmask
1399 * to see if it really matters.
1400 * (A byte at a time)
1402 if (ifa
->ifa_netmask
== 0)
1405 cp2
= ifa
->ifa_addr
->sa_data
;
1406 cp3
= ifa
->ifa_netmask
->sa_data
;
1407 cplim
= ifa
->ifa_netmask
->sa_len
+
1408 (char *)ifa
->ifa_netmask
;
1410 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1411 goto next
; /* next address! */
1413 * If the netmask of what we just found
1414 * is more specific than what we had before
1415 * (if we had one) then remember the new one
1416 * before continuing to search for an even
1417 * better one. If the netmasks are equal,
1418 * we prefer the this ifa based on the result
1421 if (ifa_maybe
== NULL
||
1422 rn_refines((char *)ifa
->ifa_netmask
,
1423 (char *)ifa_maybe
->ifa_netmask
) ||
1424 (sa_equal(ifa_maybe
->ifa_netmask
,
1425 ifa
->ifa_netmask
) &&
1426 ifa_prefer(ifa
, ifa_maybe
)))
1435 * Find an interface address specific to an interface best matching
1439 ifaof_ifpforaddr(struct sockaddr
*addr
, struct ifnet
*ifp
)
1441 struct ifaddr_container
*ifac
;
1442 char *cp
, *cp2
, *cp3
;
1444 struct ifaddr
*ifa_maybe
= NULL
;
1445 u_int af
= addr
->sa_family
;
1449 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1450 struct ifaddr
*ifa
= ifac
->ifa
;
1452 if (ifa
->ifa_addr
->sa_family
!= af
)
1454 if (ifa_maybe
== NULL
)
1456 if (ifa
->ifa_netmask
== NULL
) {
1457 if (sa_equal(addr
, ifa
->ifa_addr
) ||
1458 (ifa
->ifa_dstaddr
!= NULL
&&
1459 sa_equal(addr
, ifa
->ifa_dstaddr
)))
1463 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1464 if (sa_equal(addr
, ifa
->ifa_dstaddr
))
1468 cp2
= ifa
->ifa_addr
->sa_data
;
1469 cp3
= ifa
->ifa_netmask
->sa_data
;
1470 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
1471 for (; cp3
< cplim
; cp3
++)
1472 if ((*cp
++ ^ *cp2
++) & *cp3
)
1482 * Default action when installing a route with a Link Level gateway.
1483 * Lookup an appropriate real ifa to point to.
1484 * This should be moved to /sys/net/link.c eventually.
1487 link_rtrequest(int cmd
, struct rtentry
*rt
)
1490 struct sockaddr
*dst
;
1493 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
1494 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_key(rt
)) == NULL
)
1496 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1498 IFAFREE(rt
->rt_ifa
);
1501 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
1502 ifa
->ifa_rtrequest(cmd
, rt
);
1506 struct netmsg_ifroute
{
1507 struct netmsg_base base
;
1514 * Mark an interface down and notify protocols of the transition.
1517 if_unroute_dispatch(netmsg_t nmsg
)
1519 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1520 struct ifnet
*ifp
= msg
->ifp
;
1521 int flag
= msg
->flag
, fam
= msg
->fam
;
1522 struct ifaddr_container
*ifac
;
1524 ifp
->if_flags
&= ~flag
;
1525 getmicrotime(&ifp
->if_lastchange
);
1527 * The ifaddr processing in the following loop will block,
1528 * however, this function is called in netisr0, in which
1529 * ifaddr list changes happen, so we don't care about the
1530 * blockness of the ifaddr processing here.
1532 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1533 struct ifaddr
*ifa
= ifac
->ifa
;
1536 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1539 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1540 kpfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
1542 ifq_purge_all(&ifp
->if_snd
);
1545 lwkt_replymsg(&nmsg
->lmsg
, 0);
1549 if_unroute(struct ifnet
*ifp
, int flag
, int fam
)
1551 struct netmsg_ifroute msg
;
1553 ASSERT_CANDOMSG_NETISR0(curthread
);
1555 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1556 if_unroute_dispatch
);
1560 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1564 * Mark an interface up and notify protocols of the transition.
1567 if_route_dispatch(netmsg_t nmsg
)
1569 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1570 struct ifnet
*ifp
= msg
->ifp
;
1571 int flag
= msg
->flag
, fam
= msg
->fam
;
1572 struct ifaddr_container
*ifac
;
1574 ifq_purge_all(&ifp
->if_snd
);
1575 ifp
->if_flags
|= flag
;
1576 getmicrotime(&ifp
->if_lastchange
);
1578 * The ifaddr processing in the following loop will block,
1579 * however, this function is called in netisr0, in which
1580 * ifaddr list changes happen, so we don't care about the
1581 * blockness of the ifaddr processing here.
1583 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1584 struct ifaddr
*ifa
= ifac
->ifa
;
1587 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1590 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1591 kpfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
1598 lwkt_replymsg(&nmsg
->lmsg
, 0);
1602 if_route(struct ifnet
*ifp
, int flag
, int fam
)
1604 struct netmsg_ifroute msg
;
1606 ASSERT_CANDOMSG_NETISR0(curthread
);
1608 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1613 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1617 * Mark an interface down and notify protocols of the transition. An
1618 * interface going down is also considered to be a synchronizing event.
1619 * We must ensure that all packet processing related to the interface
1620 * has completed before we return so e.g. the caller can free the ifnet
1621 * structure that the mbufs may be referencing.
1623 * NOTE: must be called at splnet or eqivalent.
1626 if_down(struct ifnet
*ifp
)
1628 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
1629 netmsg_service_sync();
1633 * Mark an interface up and notify protocols of
1635 * NOTE: must be called at splnet or eqivalent.
1638 if_up(struct ifnet
*ifp
)
1640 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
1644 * Process a link state change.
1645 * NOTE: must be called at splsoftnet or equivalent.
1648 if_link_state_change(struct ifnet
*ifp
)
1650 int link_state
= ifp
->if_link_state
;
1653 devctl_notify("IFNET", ifp
->if_xname
,
1654 (link_state
== LINK_STATE_UP
) ? "LINK_UP" : "LINK_DOWN", NULL
);
1658 * Handle interface watchdog timer routines. Called
1659 * from softclock, we decrement timers (if set) and
1660 * call the appropriate interface routine on expiration.
1663 if_slowtimo_dispatch(netmsg_t nmsg
)
1665 struct globaldata
*gd
= mycpu
;
1666 const struct ifnet_array
*arr
;
1669 ASSERT_IN_NETISR(0);
1672 lwkt_replymsg(&nmsg
->lmsg
, 0); /* reply ASAP */
1675 arr
= ifnet_array_get();
1676 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1677 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1681 if (if_stats_compat
) {
1682 IFNET_STAT_GET(ifp
, ipackets
, ifp
->if_ipackets
);
1683 IFNET_STAT_GET(ifp
, ierrors
, ifp
->if_ierrors
);
1684 IFNET_STAT_GET(ifp
, opackets
, ifp
->if_opackets
);
1685 IFNET_STAT_GET(ifp
, oerrors
, ifp
->if_oerrors
);
1686 IFNET_STAT_GET(ifp
, collisions
, ifp
->if_collisions
);
1687 IFNET_STAT_GET(ifp
, ibytes
, ifp
->if_ibytes
);
1688 IFNET_STAT_GET(ifp
, obytes
, ifp
->if_obytes
);
1689 IFNET_STAT_GET(ifp
, imcasts
, ifp
->if_imcasts
);
1690 IFNET_STAT_GET(ifp
, omcasts
, ifp
->if_omcasts
);
1691 IFNET_STAT_GET(ifp
, iqdrops
, ifp
->if_iqdrops
);
1692 IFNET_STAT_GET(ifp
, noproto
, ifp
->if_noproto
);
1693 IFNET_STAT_GET(ifp
, oqdrops
, ifp
->if_oqdrops
);
1696 if (ifp
->if_timer
== 0 || --ifp
->if_timer
) {
1700 if (ifp
->if_watchdog
) {
1701 if (ifnet_tryserialize_all(ifp
)) {
1702 (*ifp
->if_watchdog
)(ifp
);
1703 ifnet_deserialize_all(ifp
);
1705 /* try again next timeout */
1713 callout_reset(&if_slowtimo_timer
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
1717 if_slowtimo(void *arg __unused
)
1719 struct lwkt_msg
*lmsg
= &if_slowtimo_netmsg
.lmsg
;
1721 KASSERT(mycpuid
== 0, ("not on cpu0"));
1723 if (lmsg
->ms_flags
& MSGF_DONE
)
1724 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg
);
1729 * Map interface name to
1730 * interface structure pointer.
1733 ifunit(const char *name
)
1738 * Search all the interfaces for this name/number
1740 KASSERT(mtx_owned(&ifnet_mtx
), ("ifnet is not locked"));
1742 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
1743 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1750 ifunit_netisr(const char *name
)
1752 const struct ifnet_array
*arr
;
1756 * Search all the interfaces for this name/number
1759 arr
= ifnet_array_get();
1760 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1761 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1763 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1773 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ucred
*cred
)
1778 int error
, do_ifup
= 0;
1781 size_t namelen
, onamelen
;
1782 char new_name
[IFNAMSIZ
];
1784 struct sockaddr_dl
*sdl
;
1789 return (ifconf(cmd
, data
, cred
));
1794 ifr
= (struct ifreq
*)data
;
1799 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1801 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1802 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
1804 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1806 return (if_clone_destroy(ifr
->ifr_name
));
1807 case SIOCIFGCLONERS
:
1808 return (if_clone_list((struct if_clonereq
*)data
));
1814 * Nominal ioctl through interface, lookup the ifp and obtain a
1815 * lock to serialize the ifconfig ioctl operation.
1819 ifp
= ifunit(ifr
->ifr_name
);
1828 ifr
->ifr_index
= ifp
->if_index
;
1832 ifr
->ifr_flags
= ifp
->if_flags
;
1833 ifr
->ifr_flagshigh
= ifp
->if_flags
>> 16;
1837 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1838 ifr
->ifr_curcap
= ifp
->if_capenable
;
1842 ifr
->ifr_metric
= ifp
->if_metric
;
1846 ifr
->ifr_mtu
= ifp
->if_mtu
;
1850 ifr
->ifr_tsolen
= ifp
->if_tsolen
;
1854 error
= copyout((caddr_t
)&ifp
->if_data
, ifr
->ifr_data
,
1855 sizeof(ifp
->if_data
));
1859 ifr
->ifr_phys
= ifp
->if_physical
;
1862 case SIOCGIFPOLLCPU
:
1863 ifr
->ifr_pollcpu
= -1;
1866 case SIOCSIFPOLLCPU
:
1870 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1873 new_flags
= (ifr
->ifr_flags
& 0xffff) |
1874 (ifr
->ifr_flagshigh
<< 16);
1875 if (ifp
->if_flags
& IFF_SMART
) {
1876 /* Smart drivers twiddle their own routes */
1877 } else if (ifp
->if_flags
& IFF_UP
&&
1878 (new_flags
& IFF_UP
) == 0) {
1880 } else if (new_flags
& IFF_UP
&&
1881 (ifp
->if_flags
& IFF_UP
) == 0) {
1885 #ifdef IFPOLL_ENABLE
1886 if ((new_flags
^ ifp
->if_flags
) & IFF_NPOLLING
) {
1887 if (new_flags
& IFF_NPOLLING
)
1888 ifpoll_register(ifp
);
1890 ifpoll_deregister(ifp
);
1894 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1895 (new_flags
&~ IFF_CANTCHANGE
);
1896 if (new_flags
& IFF_PPROMISC
) {
1897 /* Permanently promiscuous mode requested */
1898 ifp
->if_flags
|= IFF_PROMISC
;
1899 } else if (ifp
->if_pcount
== 0) {
1900 ifp
->if_flags
&= ~IFF_PROMISC
;
1902 if (ifp
->if_ioctl
) {
1903 ifnet_serialize_all(ifp
);
1904 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1905 ifnet_deserialize_all(ifp
);
1909 getmicrotime(&ifp
->if_lastchange
);
1913 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1916 if (ifr
->ifr_reqcap
& ~ifp
->if_capabilities
) {
1920 ifnet_serialize_all(ifp
);
1921 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1922 ifnet_deserialize_all(ifp
);
1926 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1929 error
= copyinstr(ifr
->ifr_data
, new_name
, IFNAMSIZ
, NULL
);
1932 if (new_name
[0] == '\0') {
1936 if (ifunit(new_name
) != NULL
) {
1941 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
1943 /* Announce the departure of the interface. */
1944 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
1946 strlcpy(ifp
->if_xname
, new_name
, sizeof(ifp
->if_xname
));
1947 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
1948 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1949 namelen
= strlen(new_name
);
1950 onamelen
= sdl
->sdl_nlen
;
1952 * Move the address if needed. This is safe because we
1953 * allocate space for a name of length IFNAMSIZ when we
1954 * create this in if_attach().
1956 if (namelen
!= onamelen
) {
1957 bcopy(sdl
->sdl_data
+ onamelen
,
1958 sdl
->sdl_data
+ namelen
, sdl
->sdl_alen
);
1960 bcopy(new_name
, sdl
->sdl_data
, namelen
);
1961 sdl
->sdl_nlen
= namelen
;
1962 sdl
= (struct sockaddr_dl
*)ifa
->ifa_netmask
;
1963 bzero(sdl
->sdl_data
, onamelen
);
1964 while (namelen
!= 0)
1965 sdl
->sdl_data
[--namelen
] = 0xff;
1967 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
1969 /* Announce the return of the interface. */
1970 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
1974 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1977 ifp
->if_metric
= ifr
->ifr_metric
;
1978 getmicrotime(&ifp
->if_lastchange
);
1982 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1985 if (ifp
->if_ioctl
== NULL
) {
1989 ifnet_serialize_all(ifp
);
1990 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1991 ifnet_deserialize_all(ifp
);
1993 getmicrotime(&ifp
->if_lastchange
);
1998 u_long oldmtu
= ifp
->if_mtu
;
2000 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2003 if (ifp
->if_ioctl
== NULL
) {
2007 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2011 ifnet_serialize_all(ifp
);
2012 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2013 ifnet_deserialize_all(ifp
);
2015 getmicrotime(&ifp
->if_lastchange
);
2019 * If the link MTU changed, do network layer specific procedure.
2021 if (ifp
->if_mtu
!= oldmtu
) {
2030 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2034 /* XXX need driver supplied upper limit */
2035 if (ifr
->ifr_tsolen
<= 0) {
2039 ifp
->if_tsolen
= ifr
->ifr_tsolen
;
2044 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2048 /* Don't allow group membership on non-multicast interfaces. */
2049 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2054 /* Don't let users screw up protocols' entries. */
2055 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2060 if (cmd
== SIOCADDMULTI
) {
2061 struct ifmultiaddr
*ifma
;
2062 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
2064 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
2067 getmicrotime(&ifp
->if_lastchange
);
2070 case SIOCSIFPHYADDR
:
2071 case SIOCDIFPHYADDR
:
2073 case SIOCSIFPHYADDR_IN6
:
2075 case SIOCSLIFPHYADDR
:
2077 case SIOCSIFGENERIC
:
2078 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2081 if (ifp
->if_ioctl
== 0) {
2085 ifnet_serialize_all(ifp
);
2086 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2087 ifnet_deserialize_all(ifp
);
2089 getmicrotime(&ifp
->if_lastchange
);
2093 ifs
= (struct ifstat
*)data
;
2094 ifs
->ascii
[0] = '\0';
2096 case SIOCGIFPSRCADDR
:
2097 case SIOCGIFPDSTADDR
:
2098 case SIOCGLIFPHYADDR
:
2100 case SIOCGIFGENERIC
:
2101 if (ifp
->if_ioctl
== NULL
) {
2105 ifnet_serialize_all(ifp
);
2106 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2107 ifnet_deserialize_all(ifp
);
2111 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2114 error
= if_setlladdr(ifp
, ifr
->ifr_addr
.sa_data
,
2115 ifr
->ifr_addr
.sa_len
);
2116 EVENTHANDLER_INVOKE(iflladdr_event
, ifp
);
2120 oif_flags
= ifp
->if_flags
;
2121 if (so
->so_proto
== 0) {
2125 error
= so_pru_control_direct(so
, cmd
, data
, ifp
);
2127 if ((oif_flags
^ ifp
->if_flags
) & IFF_UP
) {
2129 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2130 if (ifp
->if_flags
& IFF_UP
) {
2145 * Set/clear promiscuous mode on interface ifp based on the truth value
2146 * of pswitch. The calls are reference counted so that only the first
2147 * "on" request actually has an effect, as does the final "off" request.
2148 * Results are undefined if the "off" and "on" requests are not matched.
2151 ifpromisc(struct ifnet
*ifp
, int pswitch
)
2157 oldflags
= ifp
->if_flags
;
2158 if (ifp
->if_flags
& IFF_PPROMISC
) {
2159 /* Do nothing if device is in permanently promiscuous mode */
2160 ifp
->if_pcount
+= pswitch
? 1 : -1;
2165 * If the device is not configured up, we cannot put it in
2168 if ((ifp
->if_flags
& IFF_UP
) == 0)
2170 if (ifp
->if_pcount
++ != 0)
2172 ifp
->if_flags
|= IFF_PROMISC
;
2173 log(LOG_INFO
, "%s: promiscuous mode enabled\n",
2176 if (--ifp
->if_pcount
> 0)
2178 ifp
->if_flags
&= ~IFF_PROMISC
;
2179 log(LOG_INFO
, "%s: promiscuous mode disabled\n",
2182 ifr
.ifr_flags
= ifp
->if_flags
;
2183 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2184 ifnet_serialize_all(ifp
);
2185 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, NULL
);
2186 ifnet_deserialize_all(ifp
);
2190 ifp
->if_flags
= oldflags
;
2195 * Return interface configuration
2196 * of system. List may be used
2197 * in later ioctl's (above) to get
2198 * other information.
2201 ifconf(u_long cmd
, caddr_t data
, struct ucred
*cred
)
2203 struct ifconf
*ifc
= (struct ifconf
*)data
;
2205 struct sockaddr
*sa
;
2206 struct ifreq ifr
, *ifrp
;
2207 int space
= ifc
->ifc_len
, error
= 0;
2209 ifrp
= ifc
->ifc_req
;
2212 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2213 struct ifaddr_container
*ifac
, *ifac_mark
;
2214 struct ifaddr_marker mark
;
2215 struct ifaddrhead
*head
;
2218 if (space
<= sizeof ifr
)
2222 * Zero the stack declared structure first to prevent
2223 * memory disclosure.
2225 bzero(&ifr
, sizeof(ifr
));
2226 if (strlcpy(ifr
.ifr_name
, ifp
->if_xname
, sizeof(ifr
.ifr_name
))
2227 >= sizeof(ifr
.ifr_name
)) {
2228 error
= ENAMETOOLONG
;
2233 * Add a marker, since copyout() could block and during that
2234 * period the list could be changed. Inserting the marker to
2235 * the header of the list will not cause trouble for the code
2236 * assuming that the first element of the list is AF_LINK; the
2237 * marker will be moved to the next position w/o blocking.
2239 ifa_marker_init(&mark
, ifp
);
2240 ifac_mark
= &mark
.ifac
;
2241 head
= &ifp
->if_addrheads
[mycpuid
];
2244 TAILQ_INSERT_HEAD(head
, ifac_mark
, ifa_link
);
2245 while ((ifac
= TAILQ_NEXT(ifac_mark
, ifa_link
)) != NULL
) {
2246 struct ifaddr
*ifa
= ifac
->ifa
;
2248 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2249 TAILQ_INSERT_AFTER(head
, ifac
, ifac_mark
, ifa_link
);
2252 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
2255 if (space
<= sizeof ifr
)
2258 if (cred
->cr_prison
&&
2259 prison_if(cred
, sa
))
2263 * Keep a reference on this ifaddr, so that it will
2264 * not be destroyed when its address is copied to
2265 * the userland, which could block.
2268 if (sa
->sa_len
<= sizeof(*sa
)) {
2270 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2273 if (space
< (sizeof ifr
) + sa
->sa_len
-
2278 space
-= sa
->sa_len
- sizeof(*sa
);
2279 error
= copyout(&ifr
, ifrp
,
2280 sizeof ifr
.ifr_name
);
2282 error
= copyout(sa
, &ifrp
->ifr_addr
,
2284 ifrp
= (struct ifreq
*)
2285 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
2290 space
-= sizeof ifr
;
2292 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2296 bzero(&ifr
.ifr_addr
, sizeof ifr
.ifr_addr
);
2297 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2300 space
-= sizeof ifr
;
2306 ifc
->ifc_len
-= space
;
2311 * Just like if_promisc(), but for all-multicast-reception mode.
2314 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2322 if (ifp
->if_amcount
++ == 0) {
2323 ifp
->if_flags
|= IFF_ALLMULTI
;
2324 ifr
.ifr_flags
= ifp
->if_flags
;
2325 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2326 ifnet_serialize_all(ifp
);
2327 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2329 ifnet_deserialize_all(ifp
);
2332 if (ifp
->if_amcount
> 1) {
2335 ifp
->if_amcount
= 0;
2336 ifp
->if_flags
&= ~IFF_ALLMULTI
;
2337 ifr
.ifr_flags
= ifp
->if_flags
;
2338 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2339 ifnet_serialize_all(ifp
);
2340 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2342 ifnet_deserialize_all(ifp
);
2354 * Add a multicast listenership to the interface in question.
2355 * The link layer provides a routine which converts
2358 if_addmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
,
2359 struct ifmultiaddr
**retifma
)
2361 struct sockaddr
*llsa
, *dupsa
;
2363 struct ifmultiaddr
*ifma
;
2365 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2368 * If the matching multicast address already exists
2369 * then don't add a new one, just add a reference
2371 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2372 if (sa_equal(sa
, ifma
->ifma_addr
)) {
2373 ifma
->ifma_refcount
++;
2381 * Give the link layer a chance to accept/reject it, and also
2382 * find out which AF_LINK address this maps to, if it isn't one
2385 if (ifp
->if_resolvemulti
) {
2386 error
= ifp
->if_resolvemulti(ifp
, &llsa
, sa
);
2393 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2394 dupsa
= kmalloc(sa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2395 bcopy(sa
, dupsa
, sa
->sa_len
);
2397 ifma
->ifma_addr
= dupsa
;
2398 ifma
->ifma_lladdr
= llsa
;
2399 ifma
->ifma_ifp
= ifp
;
2400 ifma
->ifma_refcount
= 1;
2401 ifma
->ifma_protospec
= NULL
;
2402 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
2404 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2409 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2410 if (sa_equal(ifma
->ifma_addr
, llsa
))
2414 ifma
->ifma_refcount
++;
2416 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2417 dupsa
= kmalloc(llsa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2418 bcopy(llsa
, dupsa
, llsa
->sa_len
);
2419 ifma
->ifma_addr
= dupsa
;
2420 ifma
->ifma_ifp
= ifp
;
2421 ifma
->ifma_refcount
= 1;
2422 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2426 * We are certain we have added something, so call down to the
2427 * interface to let them know about it.
2430 ifp
->if_ioctl(ifp
, SIOCADDMULTI
, 0, NULL
);
2436 if_addmulti(struct ifnet
*ifp
, struct sockaddr
*sa
,
2437 struct ifmultiaddr
**retifma
)
2441 ifnet_serialize_all(ifp
);
2442 error
= if_addmulti_serialized(ifp
, sa
, retifma
);
2443 ifnet_deserialize_all(ifp
);
2449 * Remove a reference to a multicast address on this interface. Yell
2450 * if the request does not match an existing membership.
2453 if_delmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
)
2455 struct ifmultiaddr
*ifma
;
2457 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2459 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2460 if (sa_equal(sa
, ifma
->ifma_addr
))
2465 if (ifma
->ifma_refcount
> 1) {
2466 ifma
->ifma_refcount
--;
2470 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2471 sa
= ifma
->ifma_lladdr
;
2472 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2474 * Make sure the interface driver is notified
2475 * in the case of a link layer mcast group being left.
2477 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== NULL
)
2478 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2479 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2480 kfree(ifma
, M_IFMADDR
);
2485 * Now look for the link-layer address which corresponds to
2486 * this network address. It had been squirreled away in
2487 * ifma->ifma_lladdr for this purpose (so we don't have
2488 * to call ifp->if_resolvemulti() again), and we saved that
2489 * value in sa above. If some nasty deleted the
2490 * link-layer address out from underneath us, we can deal because
2491 * the address we stored was is not the same as the one which was
2492 * in the record for the link-layer address. (So we don't complain
2495 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2496 if (sa_equal(sa
, ifma
->ifma_addr
))
2501 if (ifma
->ifma_refcount
> 1) {
2502 ifma
->ifma_refcount
--;
2506 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2507 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2508 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2509 kfree(sa
, M_IFMADDR
);
2510 kfree(ifma
, M_IFMADDR
);
2516 if_delmulti(struct ifnet
*ifp
, struct sockaddr
*sa
)
2520 ifnet_serialize_all(ifp
);
2521 error
= if_delmulti_serialized(ifp
, sa
);
2522 ifnet_deserialize_all(ifp
);
2528 * Delete all multicast group membership for an interface.
2529 * Should be used to quickly flush all multicast filters.
2532 if_delallmulti_serialized(struct ifnet
*ifp
)
2534 struct ifmultiaddr
*ifma
, mark
;
2537 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2539 bzero(&sa
, sizeof(sa
));
2540 sa
.sa_family
= AF_UNSPEC
;
2541 sa
.sa_len
= sizeof(sa
);
2543 bzero(&mark
, sizeof(mark
));
2544 mark
.ifma_addr
= &sa
;
2546 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2547 while ((ifma
= TAILQ_NEXT(&mark
, ifma_link
)) != NULL
) {
2548 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2549 TAILQ_INSERT_AFTER(&ifp
->if_multiaddrs
, ifma
, &mark
,
2552 if (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
)
2555 if_delmulti_serialized(ifp
, ifma
->ifma_addr
);
2557 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2562 * Set the link layer address on an interface.
2564 * At this time we only support certain types of interfaces,
2565 * and we don't allow the length of the address to change.
2568 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2570 struct sockaddr_dl
*sdl
;
2573 sdl
= IF_LLSOCKADDR(ifp
);
2576 if (len
!= sdl
->sdl_alen
) /* don't allow length to change */
2578 switch (ifp
->if_type
) {
2579 case IFT_ETHER
: /* these types use struct arpcom */
2582 case IFT_IEEE8023ADLAG
:
2583 bcopy(lladdr
, ((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, len
);
2584 bcopy(lladdr
, LLADDR(sdl
), len
);
2590 * If the interface is already up, we need
2591 * to re-init it in order to reprogram its
2594 ifnet_serialize_all(ifp
);
2595 if ((ifp
->if_flags
& IFF_UP
) != 0) {
2597 struct ifaddr_container
*ifac
;
2600 ifp
->if_flags
&= ~IFF_UP
;
2601 ifr
.ifr_flags
= ifp
->if_flags
;
2602 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2603 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2605 ifp
->if_flags
|= IFF_UP
;
2606 ifr
.ifr_flags
= ifp
->if_flags
;
2607 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2608 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2612 * Also send gratuitous ARPs to notify other nodes about
2613 * the address change.
2615 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
2616 struct ifaddr
*ifa
= ifac
->ifa
;
2618 if (ifa
->ifa_addr
!= NULL
&&
2619 ifa
->ifa_addr
->sa_family
== AF_INET
)
2620 arp_gratuitous(ifp
, ifa
);
2624 ifnet_deserialize_all(ifp
);
2628 struct ifmultiaddr
*
2629 ifmaof_ifpforaddr(struct sockaddr
*sa
, struct ifnet
*ifp
)
2631 struct ifmultiaddr
*ifma
;
2633 /* TODO: need ifnet_serialize_main */
2634 ifnet_serialize_all(ifp
);
2635 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2636 if (sa_equal(ifma
->ifma_addr
, sa
))
2638 ifnet_deserialize_all(ifp
);
2644 * This function locates the first real ethernet MAC from a network
2645 * card and loads it into node, returning 0 on success or ENOENT if
2646 * no suitable interfaces were found. It is used by the uuid code to
2647 * generate a unique 6-byte number.
2650 if_getanyethermac(uint16_t *node
, int minlen
)
2653 struct sockaddr_dl
*sdl
;
2656 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2657 if (ifp
->if_type
!= IFT_ETHER
)
2659 sdl
= IF_LLSOCKADDR(ifp
);
2660 if (sdl
->sdl_alen
< minlen
)
2662 bcopy(((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, node
,
2672 * The name argument must be a pointer to storage which will last as
2673 * long as the interface does. For physical devices, the result of
2674 * device_get_name(dev) is a good choice and for pseudo-devices a
2675 * static string works well.
2678 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
2680 ifp
->if_dname
= name
;
2681 ifp
->if_dunit
= unit
;
2682 if (unit
!= IF_DUNIT_NONE
)
2683 ksnprintf(ifp
->if_xname
, IFNAMSIZ
, "%s%d", name
, unit
);
2685 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
2689 if_printf(struct ifnet
*ifp
, const char *fmt
, ...)
2694 retval
= kprintf("%s: ", ifp
->if_xname
);
2695 __va_start(ap
, fmt
);
2696 retval
+= kvprintf(fmt
, ap
);
2702 if_alloc(uint8_t type
)
2708 * XXX temporary hack until arpcom is setup in if_l2com
2710 if (type
== IFT_ETHER
)
2711 size
= sizeof(struct arpcom
);
2713 size
= sizeof(struct ifnet
);
2715 ifp
= kmalloc(size
, M_IFNET
, M_WAITOK
|M_ZERO
);
2717 ifp
->if_type
= type
;
2719 if (if_com_alloc
[type
] != NULL
) {
2720 ifp
->if_l2com
= if_com_alloc
[type
](type
, ifp
);
2721 if (ifp
->if_l2com
== NULL
) {
2722 kfree(ifp
, M_IFNET
);
2730 if_free(struct ifnet
*ifp
)
2732 kfree(ifp
, M_IFNET
);
2736 ifq_set_classic(struct ifaltq
*ifq
)
2738 ifq_set_methods(ifq
, ifq
->altq_ifp
->if_mapsubq
,
2739 ifsq_classic_enqueue
, ifsq_classic_dequeue
, ifsq_classic_request
);
2743 ifq_set_methods(struct ifaltq
*ifq
, altq_mapsubq_t mapsubq
,
2744 ifsq_enqueue_t enqueue
, ifsq_dequeue_t dequeue
, ifsq_request_t request
)
2748 KASSERT(mapsubq
!= NULL
, ("mapsubq is not specified"));
2749 KASSERT(enqueue
!= NULL
, ("enqueue is not specified"));
2750 KASSERT(dequeue
!= NULL
, ("dequeue is not specified"));
2751 KASSERT(request
!= NULL
, ("request is not specified"));
2753 ifq
->altq_mapsubq
= mapsubq
;
2754 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
2755 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
2757 ifsq
->ifsq_enqueue
= enqueue
;
2758 ifsq
->ifsq_dequeue
= dequeue
;
2759 ifsq
->ifsq_request
= request
;
2764 ifsq_norm_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2767 classq_add(&ifsq
->ifsq_norm
, m
);
2768 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2772 ifsq_prio_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2775 classq_add(&ifsq
->ifsq_prio
, m
);
2776 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2777 ALTQ_SQ_PRIO_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2780 static struct mbuf
*
2781 ifsq_norm_dequeue(struct ifaltq_subque
*ifsq
)
2785 m
= classq_get(&ifsq
->ifsq_norm
);
2787 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2791 static struct mbuf
*
2792 ifsq_prio_dequeue(struct ifaltq_subque
*ifsq
)
2796 m
= classq_get(&ifsq
->ifsq_prio
);
2798 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2799 ALTQ_SQ_PRIO_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2805 ifsq_classic_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
,
2806 struct altq_pktattr
*pa __unused
)
2811 if (ifsq
->ifsq_len
>= ifsq
->ifsq_maxlen
||
2812 ifsq
->ifsq_bcnt
>= ifsq
->ifsq_maxbcnt
) {
2813 struct mbuf
*m_drop
;
2815 if (m
->m_flags
& M_PRIO
) {
2817 if (ifsq
->ifsq_prio_len
< (ifsq
->ifsq_maxlen
>> 1) &&
2818 ifsq
->ifsq_prio_bcnt
< (ifsq
->ifsq_maxbcnt
>> 1)) {
2819 /* Try dropping some from normal queue. */
2820 m_drop
= ifsq_norm_dequeue(ifsq
);
2823 m_drop
= ifsq_prio_dequeue(ifsq
);
2825 m_drop
= ifsq_norm_dequeue(ifsq
);
2827 if (m_drop
!= NULL
) {
2828 IFNET_STAT_INC(ifsq
->ifsq_ifp
, oqdrops
, 1);
2833 * No old packets could be dropped!
2834 * NOTE: Caller increases oqdrops.
2839 if (m
->m_flags
& M_PRIO
)
2840 ifsq_prio_enqueue(ifsq
, m
);
2842 ifsq_norm_enqueue(ifsq
, m
);
2848 ifsq_classic_dequeue(struct ifaltq_subque
*ifsq
, int op
)
2854 m
= classq_head(&ifsq
->ifsq_prio
);
2856 m
= classq_head(&ifsq
->ifsq_norm
);
2860 m
= ifsq_prio_dequeue(ifsq
);
2862 m
= ifsq_norm_dequeue(ifsq
);
2866 panic("unsupported ALTQ dequeue op: %d", op
);
2872 ifsq_classic_request(struct ifaltq_subque
*ifsq
, int req
, void *arg
)
2879 m
= ifsq_classic_dequeue(ifsq
, ALTDQ_REMOVE
);
2887 panic("unsupported ALTQ request: %d", req
);
2893 ifsq_ifstart_try(struct ifaltq_subque
*ifsq
, int force_sched
)
2895 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
2896 int running
= 0, need_sched
;
2899 * Try to do direct ifnet.if_start on the subqueue first, if there is
2900 * contention on the subqueue hardware serializer, ifnet.if_start on
2901 * the subqueue will be scheduled on the subqueue owner CPU.
2903 if (!ifsq_tryserialize_hw(ifsq
)) {
2905 * Subqueue hardware serializer contention happened,
2906 * ifnet.if_start on the subqueue is scheduled on
2907 * the subqueue owner CPU, and we keep going.
2909 ifsq_ifstart_schedule(ifsq
, 1);
2913 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
2914 ifp
->if_start(ifp
, ifsq
);
2915 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
2918 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
2920 ifsq_deserialize_hw(ifsq
);
2924 * More data need to be transmitted, ifnet.if_start on the
2925 * subqueue is scheduled on the subqueue owner CPU, and we
2927 * NOTE: ifnet.if_start subqueue interlock is not released.
2929 ifsq_ifstart_schedule(ifsq
, force_sched
);
2934 * Subqeue packets staging mechanism:
2936 * The packets enqueued into the subqueue are staged to a certain amount
2937 * before the ifnet.if_start on the subqueue is called. In this way, the
2938 * driver could avoid writing to hardware registers upon every packet,
2939 * instead, hardware registers could be written when certain amount of
2940 * packets are put onto hardware TX ring. The measurement on several modern
2941 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2942 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2943 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2944 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2945 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2947 * Subqueue packets staging is performed for two entry points into drivers'
2948 * transmission function:
2949 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2950 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2952 * Subqueue packets staging will be stopped upon any of the following
2954 * - If the count of packets enqueued on the current CPU is great than or
2955 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2956 * - If the total length of packets enqueued on the current CPU is great
2957 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2958 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2959 * is usually less than hardware's MTU.
2960 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2961 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2963 * - The if_start_rollup(), which is registered as low priority netisr
2964 * rollup function, is called; probably because no more work is pending
2968 * Currently subqueue packet staging is only performed in netisr threads.
2971 ifq_dispatch(struct ifnet
*ifp
, struct mbuf
*m
, struct altq_pktattr
*pa
)
2973 struct ifaltq
*ifq
= &ifp
->if_snd
;
2974 struct ifaltq_subque
*ifsq
;
2975 int error
, start
= 0, len
, mcast
= 0, avoid_start
= 0;
2976 struct ifsubq_stage_head
*head
= NULL
;
2977 struct ifsubq_stage
*stage
= NULL
;
2978 struct globaldata
*gd
= mycpu
;
2979 struct thread
*td
= gd
->gd_curthread
;
2981 crit_enter_quick(td
);
2983 ifsq
= ifq_map_subq(ifq
, gd
->gd_cpuid
);
2984 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq
);
2986 len
= m
->m_pkthdr
.len
;
2987 if (m
->m_flags
& M_MCAST
)
2990 if (td
->td_type
== TD_TYPE_NETISR
) {
2991 head
= &ifsubq_stage_heads
[mycpuid
];
2992 stage
= ifsq_get_stage(ifsq
, mycpuid
);
2995 stage
->stg_len
+= len
;
2996 if (stage
->stg_cnt
< ifsq_stage_cntmax
&&
2997 stage
->stg_len
< (ifp
->if_mtu
- max_protohdr
))
3002 error
= ifsq_enqueue_locked(ifsq
, m
, pa
);
3004 IFNET_STAT_INC(ifp
, oqdrops
, 1);
3005 if (!ifsq_data_ready(ifsq
)) {
3006 ALTQ_SQ_UNLOCK(ifsq
);
3007 crit_exit_quick(td
);
3012 if (!ifsq_is_started(ifsq
)) {
3014 ALTQ_SQ_UNLOCK(ifsq
);
3017 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
3018 ifsq_stage_insert(head
, stage
);
3020 IFNET_STAT_INC(ifp
, obytes
, len
);
3022 IFNET_STAT_INC(ifp
, omcasts
, 1);
3023 crit_exit_quick(td
);
3028 * Hold the subqueue interlock of ifnet.if_start
3030 ifsq_set_started(ifsq
);
3033 ALTQ_SQ_UNLOCK(ifsq
);
3036 IFNET_STAT_INC(ifp
, obytes
, len
);
3038 IFNET_STAT_INC(ifp
, omcasts
, 1);
3041 if (stage
!= NULL
) {
3042 if (!start
&& (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)) {
3043 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
3045 ifsq_stage_remove(head
, stage
);
3046 ifsq_ifstart_schedule(ifsq
, 1);
3048 crit_exit_quick(td
);
3052 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) {
3053 ifsq_stage_remove(head
, stage
);
3061 crit_exit_quick(td
);
3065 ifsq_ifstart_try(ifsq
, 0);
3067 crit_exit_quick(td
);
3072 ifa_create(int size
)
3077 KASSERT(size
>= sizeof(*ifa
), ("ifaddr size too small"));
3079 ifa
= kmalloc(size
, M_IFADDR
, M_INTWAIT
| M_ZERO
);
3080 ifa
->ifa_containers
=
3081 kmalloc_cachealign(ncpus
* sizeof(struct ifaddr_container
),
3082 M_IFADDR
, M_INTWAIT
| M_ZERO
);
3084 ifa
->ifa_ncnt
= ncpus
;
3085 for (i
= 0; i
< ncpus
; ++i
) {
3086 struct ifaddr_container
*ifac
= &ifa
->ifa_containers
[i
];
3088 ifac
->ifa_magic
= IFA_CONTAINER_MAGIC
;
3090 ifac
->ifa_refcnt
= 1;
3093 kprintf("alloc ifa %p %d\n", ifa
, size
);
3099 ifac_free(struct ifaddr_container
*ifac
, int cpu_id
)
3101 struct ifaddr
*ifa
= ifac
->ifa
;
3103 KKASSERT(ifac
->ifa_magic
== IFA_CONTAINER_MAGIC
);
3104 KKASSERT(ifac
->ifa_refcnt
== 0);
3105 KASSERT(ifac
->ifa_listmask
== 0,
3106 ("ifa is still on %#x lists", ifac
->ifa_listmask
));
3108 ifac
->ifa_magic
= IFA_CONTAINER_DEAD
;
3110 #ifdef IFADDR_DEBUG_VERBOSE
3111 kprintf("try free ifa %p cpu_id %d\n", ifac
->ifa
, cpu_id
);
3114 KASSERT(ifa
->ifa_ncnt
> 0 && ifa
->ifa_ncnt
<= ncpus
,
3115 ("invalid # of ifac, %d", ifa
->ifa_ncnt
));
3116 if (atomic_fetchadd_int(&ifa
->ifa_ncnt
, -1) == 1) {
3118 kprintf("free ifa %p\n", ifa
);
3120 kfree(ifa
->ifa_containers
, M_IFADDR
);
3121 kfree(ifa
, M_IFADDR
);
3126 ifa_iflink_dispatch(netmsg_t nmsg
)
3128 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3129 struct ifaddr
*ifa
= msg
->ifa
;
3130 struct ifnet
*ifp
= msg
->ifp
;
3132 struct ifaddr_container
*ifac
;
3136 ifac
= &ifa
->ifa_containers
[cpu
];
3137 ASSERT_IFAC_VALID(ifac
);
3138 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
) == 0,
3139 ("ifaddr is on if_addrheads"));
3141 ifac
->ifa_listmask
|= IFA_LIST_IFADDRHEAD
;
3143 TAILQ_INSERT_TAIL(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3145 TAILQ_INSERT_HEAD(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3149 netisr_forwardmsg(&nmsg
->base
, cpu
+ 1);
3153 ifa_iflink(struct ifaddr
*ifa
, struct ifnet
*ifp
, int tail
)
3155 struct netmsg_ifaddr msg
;
3157 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3158 0, ifa_iflink_dispatch
);
3163 netisr_domsg(&msg
.base
, 0);
3167 ifa_ifunlink_dispatch(netmsg_t nmsg
)
3169 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3170 struct ifaddr
*ifa
= msg
->ifa
;
3171 struct ifnet
*ifp
= msg
->ifp
;
3173 struct ifaddr_container
*ifac
;
3177 ifac
= &ifa
->ifa_containers
[cpu
];
3178 ASSERT_IFAC_VALID(ifac
);
3179 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
,
3180 ("ifaddr is not on if_addrhead"));
3182 TAILQ_REMOVE(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3183 ifac
->ifa_listmask
&= ~IFA_LIST_IFADDRHEAD
;
3187 netisr_forwardmsg(&nmsg
->base
, cpu
+ 1);
3191 ifa_ifunlink(struct ifaddr
*ifa
, struct ifnet
*ifp
)
3193 struct netmsg_ifaddr msg
;
3195 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3196 0, ifa_ifunlink_dispatch
);
3200 netisr_domsg(&msg
.base
, 0);
3204 ifa_destroy_dispatch(netmsg_t nmsg
)
3206 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3209 netisr_forwardmsg(&nmsg
->base
, mycpuid
+ 1);
3213 ifa_destroy(struct ifaddr
*ifa
)
3215 struct netmsg_ifaddr msg
;
3217 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3218 0, ifa_destroy_dispatch
);
3221 netisr_domsg(&msg
.base
, 0);
3225 if_start_rollup(void)
3227 struct ifsubq_stage_head
*head
= &ifsubq_stage_heads
[mycpuid
];
3228 struct ifsubq_stage
*stage
;
3232 while ((stage
= TAILQ_FIRST(&head
->stg_head
)) != NULL
) {
3233 struct ifaltq_subque
*ifsq
= stage
->stg_subq
;
3236 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)
3238 ifsq_stage_remove(head
, stage
);
3241 ifsq_ifstart_schedule(ifsq
, 1);
3246 if (!ifsq_is_started(ifsq
)) {
3248 * Hold the subqueue interlock of
3251 ifsq_set_started(ifsq
);
3254 ALTQ_SQ_UNLOCK(ifsq
);
3257 ifsq_ifstart_try(ifsq
, 1);
3259 KKASSERT((stage
->stg_flags
&
3260 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
3267 ifnetinit(void *dummy __unused
)
3271 for (i
= 0; i
< ncpus
; ++i
)
3272 TAILQ_INIT(&ifsubq_stage_heads
[i
].stg_head
);
3273 netisr_register_rollup(if_start_rollup
, NETISR_ROLLUP_PRIO_IFSTART
);
3277 if_register_com_alloc(u_char type
,
3278 if_com_alloc_t
*a
, if_com_free_t
*f
)
3281 KASSERT(if_com_alloc
[type
] == NULL
,
3282 ("if_register_com_alloc: %d already registered", type
));
3283 KASSERT(if_com_free
[type
] == NULL
,
3284 ("if_register_com_alloc: %d free already registered", type
));
3286 if_com_alloc
[type
] = a
;
3287 if_com_free
[type
] = f
;
3291 if_deregister_com_alloc(u_char type
)
3294 KASSERT(if_com_alloc
[type
] != NULL
,
3295 ("if_deregister_com_alloc: %d not registered", type
));
3296 KASSERT(if_com_free
[type
] != NULL
,
3297 ("if_deregister_com_alloc: %d free not registered", type
));
3298 if_com_alloc
[type
] = NULL
;
3299 if_com_free
[type
] = NULL
;
3303 if_ring_count2(int cnt
, int cnt_max
)
3307 KASSERT(cnt_max
>= 1 && powerof2(cnt_max
),
3308 ("invalid ring count max %d", cnt_max
));
3317 while ((1 << (shift
+ 1)) <= cnt
)
3321 KASSERT(cnt
>= 1 && cnt
<= ncpus2
&& cnt
<= cnt_max
,
3322 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3323 cnt
, ncpus2
, cnt_max
));
3328 ifq_set_maxlen(struct ifaltq
*ifq
, int len
)
3330 ifq
->altq_maxlen
= len
+ (ncpus
* ifsq_stage_cntmax
);
3334 ifq_mapsubq_default(struct ifaltq
*ifq __unused
, int cpuid __unused
)
3336 return ALTQ_SUBQ_INDEX_DEFAULT
;
3340 ifq_mapsubq_mask(struct ifaltq
*ifq
, int cpuid
)
3342 return (cpuid
& ifq
->altq_subq_mask
);
3346 ifsq_watchdog(void *arg
)
3348 struct ifsubq_watchdog
*wd
= arg
;
3351 if (__predict_true(wd
->wd_timer
== 0 || --wd
->wd_timer
))
3354 ifp
= ifsq_get_ifp(wd
->wd_subq
);
3355 if (ifnet_tryserialize_all(ifp
)) {
3356 wd
->wd_watchdog(wd
->wd_subq
);
3357 ifnet_deserialize_all(ifp
);
3359 /* try again next timeout */
3363 ifsq_watchdog_reset(wd
);
3367 ifsq_watchdog_reset(struct ifsubq_watchdog
*wd
)
3369 callout_reset_bycpu(&wd
->wd_callout
, hz
, ifsq_watchdog
, wd
,
3370 ifsq_get_cpuid(wd
->wd_subq
));
3374 ifsq_watchdog_init(struct ifsubq_watchdog
*wd
, struct ifaltq_subque
*ifsq
,
3375 ifsq_watchdog_t watchdog
)
3377 callout_init_mp(&wd
->wd_callout
);
3380 wd
->wd_watchdog
= watchdog
;
3384 ifsq_watchdog_start(struct ifsubq_watchdog
*wd
)
3387 ifsq_watchdog_reset(wd
);
3391 ifsq_watchdog_stop(struct ifsubq_watchdog
*wd
)
3394 callout_stop(&wd
->wd_callout
);
3400 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3401 ("try holding ifnet lock in netisr"));
3402 mtx_lock(&ifnet_mtx
);
3408 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3409 ("try holding ifnet lock in netisr"));
3410 mtx_unlock(&ifnet_mtx
);
3413 static struct ifnet_array
*
3414 ifnet_array_alloc(int count
)
3416 struct ifnet_array
*arr
;
3418 arr
= kmalloc(__offsetof(struct ifnet_array
, ifnet_arr
[count
]),
3420 arr
->ifnet_count
= count
;
3426 ifnet_array_free(struct ifnet_array
*arr
)
3428 if (arr
== &ifnet_array0
)
3430 kfree(arr
, M_IFNET
);
3433 static struct ifnet_array
*
3434 ifnet_array_add(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3436 struct ifnet_array
*arr
;
3439 KASSERT(old_arr
->ifnet_count
>= 0,
3440 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3441 count
= old_arr
->ifnet_count
+ 1;
3442 arr
= ifnet_array_alloc(count
);
3445 * Save the old ifnet array and append this ifp to the end of
3446 * the new ifnet array.
3448 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3449 KASSERT(old_arr
->ifnet_arr
[i
] != ifp
,
3450 ("%s is already in ifnet array", ifp
->if_xname
));
3451 arr
->ifnet_arr
[i
] = old_arr
->ifnet_arr
[i
];
3453 KASSERT(i
== count
- 1,
3454 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3455 ifp
->if_xname
, count
- 1, i
));
3456 arr
->ifnet_arr
[i
] = ifp
;
3461 static struct ifnet_array
*
3462 ifnet_array_del(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3464 struct ifnet_array
*arr
;
3465 int count
, i
, idx
, found
= 0;
3467 KASSERT(old_arr
->ifnet_count
> 0,
3468 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3469 count
= old_arr
->ifnet_count
- 1;
3470 arr
= ifnet_array_alloc(count
);
3473 * Save the old ifnet array, but skip this ifp.
3476 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3477 if (old_arr
->ifnet_arr
[i
] == ifp
) {
3479 ("dup %s is in ifnet array", ifp
->if_xname
));
3483 KASSERT(idx
< count
,
3484 ("invalid ifnet array index %d, count %d", idx
, count
));
3485 arr
->ifnet_arr
[idx
] = old_arr
->ifnet_arr
[i
];
3488 KASSERT(found
, ("%s is not in ifnet array", ifp
->if_xname
));
3489 KASSERT(idx
== count
,
3490 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3491 ifp
->if_xname
, count
, idx
));
3496 const struct ifnet_array
*
3497 ifnet_array_get(void)
3499 const struct ifnet_array
*ret
;
3501 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3503 /* Make sure 'ret' is really used. */
3509 ifnet_array_isempty(void)
3511 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3512 if (ifnet_array
->ifnet_count
== 0)
3519 ifa_marker_init(struct ifaddr_marker
*mark
, struct ifnet
*ifp
)
3523 memset(mark
, 0, sizeof(*mark
));
3526 mark
->ifac
.ifa
= ifa
;
3528 ifa
->ifa_addr
= &mark
->addr
;
3529 ifa
->ifa_dstaddr
= &mark
->dstaddr
;
3530 ifa
->ifa_netmask
= &mark
->netmask
;