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 /* Must be after netisr_init */
145 SYSINIT(ifnet
, SI_SUB_PRE_DRIVERS
, SI_ORDER_SECOND
, ifnetinit
, NULL
);
147 static if_com_alloc_t
*if_com_alloc
[256];
148 static if_com_free_t
*if_com_free
[256];
150 MALLOC_DEFINE(M_IFADDR
, "ifaddr", "interface address");
151 MALLOC_DEFINE(M_IFMADDR
, "ether_multi", "link-level multicast address");
152 MALLOC_DEFINE(M_IFNET
, "ifnet", "interface structure");
154 int ifqmaxlen
= IFQ_MAXLEN
;
155 struct ifnethead ifnet
= TAILQ_HEAD_INITIALIZER(ifnet
);
157 static struct ifnet_array ifnet_array0
;
158 static struct ifnet_array
*ifnet_array
= &ifnet_array0
;
160 static struct callout if_slowtimo_timer
;
161 static struct netmsg_base if_slowtimo_netmsg
;
164 struct ifnet
**ifindex2ifnet
= NULL
;
165 static struct mtx ifnet_mtx
= MTX_INITIALIZER("ifnet");
167 static struct ifsubq_stage_head ifsubq_stage_heads
[MAXCPU
];
170 #define IFQ_KTR_STRING "ifq=%p"
171 #define IFQ_KTR_ARGS struct ifaltq *ifq
173 #define KTR_IFQ KTR_ALL
175 KTR_INFO_MASTER(ifq
);
176 KTR_INFO(KTR_IFQ
, ifq
, enqueue
, 0, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
177 KTR_INFO(KTR_IFQ
, ifq
, dequeue
, 1, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
178 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
180 #define IF_START_KTR_STRING "ifp=%p"
181 #define IF_START_KTR_ARGS struct ifnet *ifp
183 #define KTR_IF_START KTR_ALL
185 KTR_INFO_MASTER(if_start
);
186 KTR_INFO(KTR_IF_START
, if_start
, run
, 0,
187 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
188 KTR_INFO(KTR_IF_START
, if_start
, sched
, 1,
189 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
190 KTR_INFO(KTR_IF_START
, if_start
, avoid
, 2,
191 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
192 KTR_INFO(KTR_IF_START
, if_start
, contend_sched
, 3,
193 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
194 KTR_INFO(KTR_IF_START
, if_start
, chase_sched
, 4,
195 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
196 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
199 TAILQ_HEAD(, ifg_group
) ifg_head
= TAILQ_HEAD_INITIALIZER(ifg_head
);
202 * Network interface utility routines.
204 * Routines with ifa_ifwith* names take sockaddr *'s as
213 callout_init_mp(&if_slowtimo_timer
);
214 netmsg_init(&if_slowtimo_netmsg
, NULL
, &netisr_adone_rport
,
215 MSGF_PRIORITY
, if_slowtimo_dispatch
);
217 /* XXX is this necessary? */
219 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
220 if (ifp
->if_snd
.altq_maxlen
== 0) {
221 if_printf(ifp
, "XXX: driver didn't set altq_maxlen\n");
222 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
227 /* Start if_slowtimo */
228 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg
.lmsg
);
232 ifsq_ifstart_ipifunc(void *arg
)
234 struct ifaltq_subque
*ifsq
= arg
;
235 struct lwkt_msg
*lmsg
= ifsq_get_ifstart_lmsg(ifsq
, mycpuid
);
238 if (lmsg
->ms_flags
& MSGF_DONE
)
239 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid
), lmsg
);
244 ifsq_stage_remove(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
246 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
247 TAILQ_REMOVE(&head
->stg_head
, stage
, stg_link
);
248 stage
->stg_flags
&= ~(IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
);
254 ifsq_stage_insert(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
256 KKASSERT((stage
->stg_flags
&
257 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
258 stage
->stg_flags
|= IFSQ_STAGE_FLAG_QUED
;
259 TAILQ_INSERT_TAIL(&head
->stg_head
, stage
, stg_link
);
263 * Schedule ifnet.if_start on the subqueue owner CPU
266 ifsq_ifstart_schedule(struct ifaltq_subque
*ifsq
, int force
)
270 if (!force
&& curthread
->td_type
== TD_TYPE_NETISR
&&
271 ifsq_stage_cntmax
> 0) {
272 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
276 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
277 ifsq_stage_insert(&ifsubq_stage_heads
[mycpuid
], stage
);
278 stage
->stg_flags
|= IFSQ_STAGE_FLAG_SCHED
;
282 cpu
= ifsq_get_cpuid(ifsq
);
284 lwkt_send_ipiq(globaldata_find(cpu
), ifsq_ifstart_ipifunc
, ifsq
);
286 ifsq_ifstart_ipifunc(ifsq
);
291 * This function will release ifnet.if_start subqueue interlock,
292 * if ifnet.if_start for the subqueue does not need to be scheduled
295 ifsq_ifstart_need_schedule(struct ifaltq_subque
*ifsq
, int running
)
297 if (!running
|| ifsq_is_empty(ifsq
)
299 || ifsq
->ifsq_altq
->altq_tbr
!= NULL
304 * ifnet.if_start subqueue interlock is released, if:
305 * 1) Hardware can not take any packets, due to
306 * o interface is marked down
307 * o hardware queue is full (ifsq_is_oactive)
308 * Under the second situation, hardware interrupt
309 * or polling(4) will call/schedule ifnet.if_start
310 * on the subqueue when hardware queue is ready
311 * 2) There is no packet in the subqueue.
312 * Further ifq_dispatch or ifq_handoff will call/
313 * schedule ifnet.if_start on the subqueue.
314 * 3) TBR is used and it does not allow further
316 * TBR callout will call ifnet.if_start on the
319 if (!running
|| !ifsq_data_ready(ifsq
)) {
320 ifsq_clr_started(ifsq
);
321 ALTQ_SQ_UNLOCK(ifsq
);
324 ALTQ_SQ_UNLOCK(ifsq
);
330 ifsq_ifstart_dispatch(netmsg_t msg
)
332 struct lwkt_msg
*lmsg
= &msg
->base
.lmsg
;
333 struct ifaltq_subque
*ifsq
= lmsg
->u
.ms_resultp
;
334 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
335 struct globaldata
*gd
= mycpu
;
336 int running
= 0, need_sched
;
340 lwkt_replymsg(lmsg
, 0); /* reply ASAP */
342 if (gd
->gd_cpuid
!= ifsq_get_cpuid(ifsq
)) {
344 * We need to chase the subqueue owner CPU change.
346 ifsq_ifstart_schedule(ifsq
, 1);
351 ifsq_serialize_hw(ifsq
);
352 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
353 ifp
->if_start(ifp
, ifsq
);
354 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
357 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
358 ifsq_deserialize_hw(ifsq
);
362 * More data need to be transmitted, ifnet.if_start is
363 * scheduled on the subqueue owner CPU, and we keep going.
364 * NOTE: ifnet.if_start subqueue interlock is not released.
366 ifsq_ifstart_schedule(ifsq
, 0);
372 /* Device driver ifnet.if_start helper function */
374 ifsq_devstart(struct ifaltq_subque
*ifsq
)
376 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
379 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq
);
382 if (ifsq_is_started(ifsq
) || !ifsq_data_ready(ifsq
)) {
383 ALTQ_SQ_UNLOCK(ifsq
);
386 ifsq_set_started(ifsq
);
387 ALTQ_SQ_UNLOCK(ifsq
);
389 ifp
->if_start(ifp
, ifsq
);
391 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
394 if (ifsq_ifstart_need_schedule(ifsq
, running
)) {
396 * More data need to be transmitted, ifnet.if_start is
397 * scheduled on ifnet's CPU, and we keep going.
398 * NOTE: ifnet.if_start interlock is not released.
400 ifsq_ifstart_schedule(ifsq
, 0);
405 if_devstart(struct ifnet
*ifp
)
407 ifsq_devstart(ifq_get_subq_default(&ifp
->if_snd
));
410 /* Device driver ifnet.if_start schedule helper function */
412 ifsq_devstart_sched(struct ifaltq_subque
*ifsq
)
414 ifsq_ifstart_schedule(ifsq
, 1);
418 if_devstart_sched(struct ifnet
*ifp
)
420 ifsq_devstart_sched(ifq_get_subq_default(&ifp
->if_snd
));
424 if_default_serialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
426 lwkt_serialize_enter(ifp
->if_serializer
);
430 if_default_deserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
432 lwkt_serialize_exit(ifp
->if_serializer
);
436 if_default_tryserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
438 return lwkt_serialize_try(ifp
->if_serializer
);
443 if_default_serialize_assert(struct ifnet
*ifp
,
444 enum ifnet_serialize slz __unused
,
445 boolean_t serialized
)
448 ASSERT_SERIALIZED(ifp
->if_serializer
);
450 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
455 * Attach an interface to the list of "active" interfaces.
457 * The serializer is optional.
460 if_attach(struct ifnet
*ifp
, lwkt_serialize_t serializer
)
463 int namelen
, masklen
;
464 struct sockaddr_dl
*sdl
, *sdl_addr
;
467 struct ifnet
**old_ifindex2ifnet
= NULL
;
468 struct ifnet_array
*old_ifnet_array
;
471 static int if_indexlim
= 8;
473 if (ifp
->if_serialize
!= NULL
) {
474 KASSERT(ifp
->if_deserialize
!= NULL
&&
475 ifp
->if_tryserialize
!= NULL
&&
476 ifp
->if_serialize_assert
!= NULL
,
477 ("serialize functions are partially setup"));
480 * If the device supplies serialize functions,
481 * then clear if_serializer to catch any invalid
482 * usage of this field.
484 KASSERT(serializer
== NULL
,
485 ("both serialize functions and default serializer "
487 ifp
->if_serializer
= NULL
;
489 KASSERT(ifp
->if_deserialize
== NULL
&&
490 ifp
->if_tryserialize
== NULL
&&
491 ifp
->if_serialize_assert
== NULL
,
492 ("serialize functions are partially setup"));
493 ifp
->if_serialize
= if_default_serialize
;
494 ifp
->if_deserialize
= if_default_deserialize
;
495 ifp
->if_tryserialize
= if_default_tryserialize
;
497 ifp
->if_serialize_assert
= if_default_serialize_assert
;
501 * The serializer can be passed in from the device,
502 * allowing the same serializer to be used for both
503 * the interrupt interlock and the device queue.
504 * If not specified, the netif structure will use an
505 * embedded serializer.
507 if (serializer
== NULL
) {
508 serializer
= &ifp
->if_default_serializer
;
509 lwkt_serialize_init(serializer
);
511 ifp
->if_serializer
= serializer
;
516 * The old code would work if the interface passed a pre-existing
517 * chain of ifaddrs to this code. We don't trust our callers to
518 * properly initialize the tailq, however, so we no longer allow
519 * this unlikely case.
521 ifp
->if_addrheads
= kmalloc(ncpus
* sizeof(struct ifaddrhead
),
522 M_IFADDR
, M_WAITOK
| M_ZERO
);
523 for (i
= 0; i
< ncpus
; ++i
)
524 TAILQ_INIT(&ifp
->if_addrheads
[i
]);
526 TAILQ_INIT(&ifp
->if_multiaddrs
);
527 TAILQ_INIT(&ifp
->if_groups
);
528 getmicrotime(&ifp
->if_lastchange
);
531 * create a Link Level name for this device
533 namelen
= strlen(ifp
->if_xname
);
534 masklen
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
535 socksize
= masklen
+ ifp
->if_addrlen
;
536 if (socksize
< sizeof(*sdl
))
537 socksize
= sizeof(*sdl
);
538 socksize
= RT_ROUNDUP(socksize
);
539 ifa
= ifa_create(sizeof(struct ifaddr
) + 2 * socksize
);
540 sdl
= sdl_addr
= (struct sockaddr_dl
*)(ifa
+ 1);
541 sdl
->sdl_len
= socksize
;
542 sdl
->sdl_family
= AF_LINK
;
543 bcopy(ifp
->if_xname
, sdl
->sdl_data
, namelen
);
544 sdl
->sdl_nlen
= namelen
;
545 sdl
->sdl_type
= ifp
->if_type
;
546 ifp
->if_lladdr
= ifa
;
548 ifa
->ifa_rtrequest
= link_rtrequest
;
549 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
550 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
551 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
552 sdl
->sdl_len
= masklen
;
554 sdl
->sdl_data
[--namelen
] = 0xff;
555 ifa_iflink(ifa
, ifp
, 0 /* Insert head */);
557 ifp
->if_data_pcpu
= kmalloc_cachealign(
558 ncpus
* sizeof(struct ifdata_pcpu
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
560 if (ifp
->if_mapsubq
== NULL
)
561 ifp
->if_mapsubq
= ifq_mapsubq_default
;
565 ifq
->altq_disc
= NULL
;
566 ifq
->altq_flags
&= ALTQF_CANTCHANGE
;
567 ifq
->altq_tbr
= NULL
;
570 if (ifq
->altq_subq_cnt
<= 0)
571 ifq
->altq_subq_cnt
= 1;
572 ifq
->altq_subq
= kmalloc_cachealign(
573 ifq
->altq_subq_cnt
* sizeof(struct ifaltq_subque
),
574 M_DEVBUF
, M_WAITOK
| M_ZERO
);
576 if (ifq
->altq_maxlen
== 0) {
577 if_printf(ifp
, "driver didn't set altq_maxlen\n");
578 ifq_set_maxlen(ifq
, ifqmaxlen
);
581 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
582 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
584 ALTQ_SQ_LOCK_INIT(ifsq
);
585 ifsq
->ifsq_index
= q
;
587 ifsq
->ifsq_altq
= ifq
;
588 ifsq
->ifsq_ifp
= ifp
;
590 ifsq
->ifsq_maxlen
= ifq
->altq_maxlen
;
591 ifsq
->ifsq_maxbcnt
= ifsq
->ifsq_maxlen
* MCLBYTES
;
592 ifsq
->ifsq_prepended
= NULL
;
593 ifsq
->ifsq_started
= 0;
594 ifsq
->ifsq_hw_oactive
= 0;
595 ifsq_set_cpuid(ifsq
, 0);
596 if (ifp
->if_serializer
!= NULL
)
597 ifsq_set_hw_serialize(ifsq
, ifp
->if_serializer
);
600 kmalloc_cachealign(ncpus
* sizeof(struct ifsubq_stage
),
601 M_DEVBUF
, M_WAITOK
| M_ZERO
);
602 for (i
= 0; i
< ncpus
; ++i
)
603 ifsq
->ifsq_stage
[i
].stg_subq
= ifsq
;
605 ifsq
->ifsq_ifstart_nmsg
=
606 kmalloc(ncpus
* sizeof(struct netmsg_base
),
607 M_LWKTMSG
, M_WAITOK
);
608 for (i
= 0; i
< ncpus
; ++i
) {
609 netmsg_init(&ifsq
->ifsq_ifstart_nmsg
[i
], NULL
,
610 &netisr_adone_rport
, 0, ifsq_ifstart_dispatch
);
611 ifsq
->ifsq_ifstart_nmsg
[i
].lmsg
.u
.ms_resultp
= ifsq
;
614 ifq_set_classic(ifq
);
617 * Increase mbuf cluster/jcluster limits for the mbufs that
618 * could sit on the device queues for quite some time.
620 if (ifp
->if_nmbclusters
> 0)
621 mcl_inclimit(ifp
->if_nmbclusters
);
622 if (ifp
->if_nmbjclusters
> 0)
623 mjcl_inclimit(ifp
->if_nmbjclusters
);
626 * Install this ifp into ifindex2inet, ifnet queue and ifnet
627 * array after it is setup.
629 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
630 * by ifnet lock, so that non-netisr threads could get a
635 /* Don't update if_index until ifindex2ifnet is setup */
636 ifp
->if_index
= if_index
+ 1;
637 sdl_addr
->sdl_index
= ifp
->if_index
;
640 * Install this ifp into ifindex2ifnet
642 if (ifindex2ifnet
== NULL
|| ifp
->if_index
>= if_indexlim
) {
650 n
= if_indexlim
* sizeof(*q
);
651 q
= kmalloc(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
652 if (ifindex2ifnet
!= NULL
) {
653 bcopy(ifindex2ifnet
, q
, n
/2);
654 /* Free old ifindex2ifnet after sync all netisrs */
655 old_ifindex2ifnet
= ifindex2ifnet
;
659 ifindex2ifnet
[ifp
->if_index
] = ifp
;
661 * Update if_index after this ifp is installed into ifindex2ifnet,
662 * so that netisrs could get a consistent view of ifindex2ifnet.
665 if_index
= ifp
->if_index
;
668 * Install this ifp into ifnet array.
670 /* Free old ifnet array after sync all netisrs */
671 old_ifnet_array
= ifnet_array
;
672 ifnet_array
= ifnet_array_add(ifp
, old_ifnet_array
);
675 * Install this ifp into ifnet queue.
677 TAILQ_INSERT_TAIL(&ifnetlist
, ifp
, if_link
);
682 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
683 * are no longer accessed and we can free them safely later on.
685 netmsg_service_sync();
686 if (old_ifindex2ifnet
!= NULL
)
687 kfree(old_ifindex2ifnet
, M_IFADDR
);
688 ifnet_array_free(old_ifnet_array
);
690 if (!SLIST_EMPTY(&domains
))
691 if_attachdomain1(ifp
);
693 /* Announce the interface. */
694 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
695 devctl_notify("IFNET", ifp
->if_xname
, "ATTACH", NULL
);
696 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
700 if_attachdomain(void *dummy
)
705 TAILQ_FOREACH(ifp
, &ifnetlist
, if_list
)
706 if_attachdomain1(ifp
);
709 SYSINIT(domainifattach
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_FIRST
,
710 if_attachdomain
, NULL
);
713 if_attachdomain1(struct ifnet
*ifp
)
719 /* address family dependent data region */
720 bzero(ifp
->if_afdata
, sizeof(ifp
->if_afdata
));
721 SLIST_FOREACH(dp
, &domains
, dom_next
)
722 if (dp
->dom_ifattach
)
723 ifp
->if_afdata
[dp
->dom_family
] =
724 (*dp
->dom_ifattach
)(ifp
);
729 * Purge all addresses whose type is _not_ AF_LINK
732 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg
)
734 struct lwkt_msg
*lmsg
= &nmsg
->lmsg
;
735 struct ifnet
*ifp
= lmsg
->u
.ms_resultp
;
736 struct ifaddr_container
*ifac
, *next
;
741 * The ifaddr processing in the following loop will block,
742 * however, this function is called in netisr0, in which
743 * ifaddr list changes happen, so we don't care about the
744 * blockness of the ifaddr processing here.
746 TAILQ_FOREACH_MUTABLE(ifac
, &ifp
->if_addrheads
[mycpuid
],
748 struct ifaddr
*ifa
= ifac
->ifa
;
751 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
754 /* Leave link ifaddr as it is */
755 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
758 /* XXX: Ugly!! ad hoc just for INET */
759 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
760 struct ifaliasreq ifr
;
761 #ifdef IFADDR_DEBUG_VERBOSE
764 kprintf("purge in4 addr %p: ", ifa
);
765 for (i
= 0; i
< ncpus
; ++i
)
766 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
770 bzero(&ifr
, sizeof ifr
);
771 ifr
.ifra_addr
= *ifa
->ifa_addr
;
772 if (ifa
->ifa_dstaddr
)
773 ifr
.ifra_broadaddr
= *ifa
->ifa_dstaddr
;
774 if (in_control(SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
780 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET6
) {
781 #ifdef IFADDR_DEBUG_VERBOSE
784 kprintf("purge in6 addr %p: ", ifa
);
785 for (i
= 0; i
< ncpus
; ++i
)
786 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
791 /* ifp_addrhead is already updated */
795 ifa_ifunlink(ifa
, ifp
);
799 lwkt_replymsg(lmsg
, 0);
803 if_purgeaddrs_nolink(struct ifnet
*ifp
)
805 struct netmsg_base nmsg
;
806 struct lwkt_msg
*lmsg
= &nmsg
.lmsg
;
808 ASSERT_CANDOMSG_NETISR0(curthread
);
810 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
, 0,
811 if_purgeaddrs_nolink_dispatch
);
812 lmsg
->u
.ms_resultp
= ifp
;
813 lwkt_domsg(netisr_cpuport(0), lmsg
, 0);
817 ifq_stage_detach_handler(netmsg_t nmsg
)
819 struct ifaltq
*ifq
= nmsg
->lmsg
.u
.ms_resultp
;
822 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
823 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
824 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
826 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
)
827 ifsq_stage_remove(&ifsubq_stage_heads
[mycpuid
], stage
);
829 lwkt_replymsg(&nmsg
->lmsg
, 0);
833 ifq_stage_detach(struct ifaltq
*ifq
)
835 struct netmsg_base base
;
838 netmsg_init(&base
, NULL
, &curthread
->td_msgport
, 0,
839 ifq_stage_detach_handler
);
840 base
.lmsg
.u
.ms_resultp
= ifq
;
842 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
843 lwkt_domsg(netisr_cpuport(cpu
), &base
.lmsg
, 0);
846 struct netmsg_if_rtdel
{
847 struct netmsg_base base
;
852 if_rtdel_dispatch(netmsg_t msg
)
854 struct netmsg_if_rtdel
*rmsg
= (void *)msg
;
858 for (i
= 1; i
<= AF_MAX
; i
++) {
859 struct radix_node_head
*rnh
;
861 if ((rnh
= rt_tables
[cpu
][i
]) == NULL
)
863 rnh
->rnh_walktree(rnh
, if_rtdel
, rmsg
->ifp
);
868 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
870 lwkt_replymsg(&rmsg
->base
.lmsg
, 0);
874 * Detach an interface, removing it from the
875 * list of "active" interfaces.
878 if_detach(struct ifnet
*ifp
)
880 struct ifnet_array
*old_ifnet_array
;
881 struct netmsg_if_rtdel msg
;
885 /* Announce that the interface is gone. */
886 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
887 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
888 devctl_notify("IFNET", ifp
->if_xname
, "DETACH", NULL
);
891 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
892 * array before it is whacked.
894 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
895 * by ifnet lock, so that non-netisr threads could get a
901 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
903 ifindex2ifnet
[ifp
->if_index
] = NULL
;
904 while (if_index
> 0 && ifindex2ifnet
[if_index
] == NULL
)
908 * Remove this ifp from ifnet queue.
910 TAILQ_REMOVE(&ifnetlist
, ifp
, if_link
);
913 * Remove this ifp from ifnet array.
915 /* Free old ifnet array after sync all netisrs */
916 old_ifnet_array
= ifnet_array
;
917 ifnet_array
= ifnet_array_del(ifp
, old_ifnet_array
);
922 * Sync all netisrs so that the old ifnet array is no longer
923 * accessed and we can free it safely later on.
925 netmsg_service_sync();
926 ifnet_array_free(old_ifnet_array
);
929 * Remove routes and flush queues.
933 if (ifp
->if_flags
& IFF_NPOLLING
)
934 ifpoll_deregister(ifp
);
938 /* Decrease the mbuf clusters/jclusters limits increased by us */
939 if (ifp
->if_nmbclusters
> 0)
940 mcl_inclimit(-ifp
->if_nmbclusters
);
941 if (ifp
->if_nmbjclusters
> 0)
942 mjcl_inclimit(-ifp
->if_nmbjclusters
);
945 if (ifq_is_enabled(&ifp
->if_snd
))
946 altq_disable(&ifp
->if_snd
);
947 if (ifq_is_attached(&ifp
->if_snd
))
948 altq_detach(&ifp
->if_snd
);
952 * Clean up all addresses.
954 ifp
->if_lladdr
= NULL
;
956 if_purgeaddrs_nolink(ifp
);
957 if (!TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
])) {
960 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
961 KASSERT(ifa
->ifa_addr
->sa_family
== AF_LINK
,
962 ("non-link ifaddr is left on if_addrheads"));
964 ifa_ifunlink(ifa
, ifp
);
966 KASSERT(TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
]),
967 ("there are still ifaddrs left on if_addrheads"));
972 * Remove all IPv4 kernel structures related to ifp.
979 * Remove all IPv6 kernel structs related to ifp. This should be done
980 * before removing routing entries below, since IPv6 interface direct
981 * routes are expected to be removed by the IPv6-specific kernel API.
982 * Otherwise, the kernel will detect some inconsistency and bark it.
988 * Delete all remaining routes using this interface
990 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, MSGF_PRIORITY
,
993 rt_domsg_global(&msg
.base
);
995 SLIST_FOREACH(dp
, &domains
, dom_next
)
996 if (dp
->dom_ifdetach
&& ifp
->if_afdata
[dp
->dom_family
])
997 (*dp
->dom_ifdetach
)(ifp
,
998 ifp
->if_afdata
[dp
->dom_family
]);
1000 kfree(ifp
->if_addrheads
, M_IFADDR
);
1002 lwkt_synchronize_ipiqs("if_detach");
1003 ifq_stage_detach(&ifp
->if_snd
);
1005 for (q
= 0; q
< ifp
->if_snd
.altq_subq_cnt
; ++q
) {
1006 struct ifaltq_subque
*ifsq
= &ifp
->if_snd
.altq_subq
[q
];
1008 kfree(ifsq
->ifsq_ifstart_nmsg
, M_LWKTMSG
);
1009 kfree(ifsq
->ifsq_stage
, M_DEVBUF
);
1011 kfree(ifp
->if_snd
.altq_subq
, M_DEVBUF
);
1013 kfree(ifp
->if_data_pcpu
, M_DEVBUF
);
1019 * Create interface group without members
1022 if_creategroup(const char *groupname
)
1024 struct ifg_group
*ifg
= NULL
;
1026 if ((ifg
= (struct ifg_group
*)kmalloc(sizeof(struct ifg_group
),
1027 M_TEMP
, M_NOWAIT
)) == NULL
)
1030 strlcpy(ifg
->ifg_group
, groupname
, sizeof(ifg
->ifg_group
));
1031 ifg
->ifg_refcnt
= 0;
1032 ifg
->ifg_carp_demoted
= 0;
1033 TAILQ_INIT(&ifg
->ifg_members
);
1035 pfi_attach_ifgroup(ifg
);
1037 TAILQ_INSERT_TAIL(&ifg_head
, ifg
, ifg_next
);
1043 * Add a group to an interface
1046 if_addgroup(struct ifnet
*ifp
, const char *groupname
)
1048 struct ifg_list
*ifgl
;
1049 struct ifg_group
*ifg
= NULL
;
1050 struct ifg_member
*ifgm
;
1052 if (groupname
[0] && groupname
[strlen(groupname
) - 1] >= '0' &&
1053 groupname
[strlen(groupname
) - 1] <= '9')
1056 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1057 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1060 if ((ifgl
= kmalloc(sizeof(*ifgl
), M_TEMP
, M_NOWAIT
)) == NULL
)
1063 if ((ifgm
= kmalloc(sizeof(*ifgm
), M_TEMP
, M_NOWAIT
)) == NULL
) {
1064 kfree(ifgl
, M_TEMP
);
1068 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1069 if (!strcmp(ifg
->ifg_group
, groupname
))
1072 if (ifg
== NULL
&& (ifg
= if_creategroup(groupname
)) == NULL
) {
1073 kfree(ifgl
, M_TEMP
);
1074 kfree(ifgm
, M_TEMP
);
1079 ifgl
->ifgl_group
= ifg
;
1080 ifgm
->ifgm_ifp
= ifp
;
1082 TAILQ_INSERT_TAIL(&ifg
->ifg_members
, ifgm
, ifgm_next
);
1083 TAILQ_INSERT_TAIL(&ifp
->if_groups
, ifgl
, ifgl_next
);
1086 pfi_group_change(groupname
);
1093 * Remove a group from an interface
1096 if_delgroup(struct ifnet
*ifp
, const char *groupname
)
1098 struct ifg_list
*ifgl
;
1099 struct ifg_member
*ifgm
;
1101 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1102 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1107 TAILQ_REMOVE(&ifp
->if_groups
, ifgl
, ifgl_next
);
1109 TAILQ_FOREACH(ifgm
, &ifgl
->ifgl_group
->ifg_members
, ifgm_next
)
1110 if (ifgm
->ifgm_ifp
== ifp
)
1114 TAILQ_REMOVE(&ifgl
->ifgl_group
->ifg_members
, ifgm
, ifgm_next
);
1115 kfree(ifgm
, M_TEMP
);
1118 if (--ifgl
->ifgl_group
->ifg_refcnt
== 0) {
1119 TAILQ_REMOVE(&ifg_head
, ifgl
->ifgl_group
, ifg_next
);
1121 pfi_detach_ifgroup(ifgl
->ifgl_group
);
1123 kfree(ifgl
->ifgl_group
, M_TEMP
);
1126 kfree(ifgl
, M_TEMP
);
1129 pfi_group_change(groupname
);
1136 * Stores all groups from an interface in memory pointed
1140 if_getgroup(caddr_t data
, struct ifnet
*ifp
)
1143 struct ifg_list
*ifgl
;
1144 struct ifg_req ifgrq
, *ifgp
;
1145 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1147 if (ifgr
->ifgr_len
== 0) {
1148 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1149 ifgr
->ifgr_len
+= sizeof(struct ifg_req
);
1153 len
= ifgr
->ifgr_len
;
1154 ifgp
= ifgr
->ifgr_groups
;
1155 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
) {
1156 if (len
< sizeof(ifgrq
))
1158 bzero(&ifgrq
, sizeof ifgrq
);
1159 strlcpy(ifgrq
.ifgrq_group
, ifgl
->ifgl_group
->ifg_group
,
1160 sizeof(ifgrq
.ifgrq_group
));
1161 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1162 sizeof(struct ifg_req
))))
1164 len
-= sizeof(ifgrq
);
1172 * Stores all members of a group in memory pointed to by data
1175 if_getgroupmembers(caddr_t data
)
1177 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1178 struct ifg_group
*ifg
;
1179 struct ifg_member
*ifgm
;
1180 struct ifg_req ifgrq
, *ifgp
;
1183 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1184 if (!strcmp(ifg
->ifg_group
, ifgr
->ifgr_name
))
1189 if (ifgr
->ifgr_len
== 0) {
1190 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
)
1191 ifgr
->ifgr_len
+= sizeof(ifgrq
);
1195 len
= ifgr
->ifgr_len
;
1196 ifgp
= ifgr
->ifgr_groups
;
1197 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
) {
1198 if (len
< sizeof(ifgrq
))
1200 bzero(&ifgrq
, sizeof ifgrq
);
1201 strlcpy(ifgrq
.ifgrq_member
, ifgm
->ifgm_ifp
->if_xname
,
1202 sizeof(ifgrq
.ifgrq_member
));
1203 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1204 sizeof(struct ifg_req
))))
1206 len
-= sizeof(ifgrq
);
1214 * Delete Routes for a Network Interface
1216 * Called for each routing entry via the rnh->rnh_walktree() call above
1217 * to delete all route entries referencing a detaching network interface.
1220 * rn pointer to node in the routing table
1221 * arg argument passed to rnh->rnh_walktree() - detaching interface
1225 * errno failed - reason indicated
1229 if_rtdel(struct radix_node
*rn
, void *arg
)
1231 struct rtentry
*rt
= (struct rtentry
*)rn
;
1232 struct ifnet
*ifp
= arg
;
1235 if (rt
->rt_ifp
== ifp
) {
1238 * Protect (sorta) against walktree recursion problems
1239 * with cloned routes
1241 if (!(rt
->rt_flags
& RTF_UP
))
1244 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1245 rt_mask(rt
), rt
->rt_flags
,
1248 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1255 static __inline boolean_t
1256 ifa_prefer(const struct ifaddr
*cur_ifa
, const struct ifaddr
*old_ifa
)
1258 if (old_ifa
== NULL
)
1261 if ((old_ifa
->ifa_ifp
->if_flags
& IFF_UP
) == 0 &&
1262 (cur_ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1264 if ((old_ifa
->ifa_flags
& IFA_ROUTE
) == 0 &&
1265 (cur_ifa
->ifa_flags
& IFA_ROUTE
))
1271 * Locate an interface based on a complete address.
1274 ifa_ifwithaddr(struct sockaddr
*addr
)
1276 const struct ifnet_array
*arr
;
1279 arr
= ifnet_array_get();
1280 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1281 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1282 struct ifaddr_container
*ifac
;
1284 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1285 struct ifaddr
*ifa
= ifac
->ifa
;
1287 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1289 if (sa_equal(addr
, ifa
->ifa_addr
))
1291 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1292 ifa
->ifa_broadaddr
&&
1293 /* IPv6 doesn't have broadcast */
1294 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1295 sa_equal(ifa
->ifa_broadaddr
, addr
))
1303 * Locate the point to point interface with a given destination address.
1306 ifa_ifwithdstaddr(struct sockaddr
*addr
)
1308 const struct ifnet_array
*arr
;
1311 arr
= ifnet_array_get();
1312 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1313 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1314 struct ifaddr_container
*ifac
;
1316 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
1319 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1320 struct ifaddr
*ifa
= ifac
->ifa
;
1322 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1324 if (ifa
->ifa_dstaddr
&&
1325 sa_equal(addr
, ifa
->ifa_dstaddr
))
1333 * Find an interface on a specific network. If many, choice
1334 * is most specific found.
1337 ifa_ifwithnet(struct sockaddr
*addr
)
1339 struct ifaddr
*ifa_maybe
= NULL
;
1340 u_int af
= addr
->sa_family
;
1341 char *addr_data
= addr
->sa_data
, *cplim
;
1342 const struct ifnet_array
*arr
;
1346 * AF_LINK addresses can be looked up directly by their index number,
1347 * so do that if we can.
1349 if (af
== AF_LINK
) {
1350 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
1352 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
1353 return (ifindex2ifnet
[sdl
->sdl_index
]->if_lladdr
);
1357 * Scan though each interface, looking for ones that have
1358 * addresses in this address family.
1360 arr
= ifnet_array_get();
1361 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1362 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1363 struct ifaddr_container
*ifac
;
1365 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1366 struct ifaddr
*ifa
= ifac
->ifa
;
1367 char *cp
, *cp2
, *cp3
;
1369 if (ifa
->ifa_addr
->sa_family
!= af
)
1371 if (af
== AF_INET
&& ifp
->if_flags
& IFF_POINTOPOINT
) {
1373 * This is a bit broken as it doesn't
1374 * take into account that the remote end may
1375 * be a single node in the network we are
1377 * The trouble is that we don't know the
1378 * netmask for the remote end.
1380 if (ifa
->ifa_dstaddr
!= NULL
&&
1381 sa_equal(addr
, ifa
->ifa_dstaddr
))
1385 * if we have a special address handler,
1386 * then use it instead of the generic one.
1388 if (ifa
->ifa_claim_addr
) {
1389 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
1397 * Scan all the bits in the ifa's address.
1398 * If a bit dissagrees with what we are
1399 * looking for, mask it with the netmask
1400 * to see if it really matters.
1401 * (A byte at a time)
1403 if (ifa
->ifa_netmask
== 0)
1406 cp2
= ifa
->ifa_addr
->sa_data
;
1407 cp3
= ifa
->ifa_netmask
->sa_data
;
1408 cplim
= ifa
->ifa_netmask
->sa_len
+
1409 (char *)ifa
->ifa_netmask
;
1411 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1412 goto next
; /* next address! */
1414 * If the netmask of what we just found
1415 * is more specific than what we had before
1416 * (if we had one) then remember the new one
1417 * before continuing to search for an even
1418 * better one. If the netmasks are equal,
1419 * we prefer the this ifa based on the result
1422 if (ifa_maybe
== NULL
||
1423 rn_refines((char *)ifa
->ifa_netmask
,
1424 (char *)ifa_maybe
->ifa_netmask
) ||
1425 (sa_equal(ifa_maybe
->ifa_netmask
,
1426 ifa
->ifa_netmask
) &&
1427 ifa_prefer(ifa
, ifa_maybe
)))
1436 * Find an interface address specific to an interface best matching
1440 ifaof_ifpforaddr(struct sockaddr
*addr
, struct ifnet
*ifp
)
1442 struct ifaddr_container
*ifac
;
1443 char *cp
, *cp2
, *cp3
;
1445 struct ifaddr
*ifa_maybe
= NULL
;
1446 u_int af
= addr
->sa_family
;
1450 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1451 struct ifaddr
*ifa
= ifac
->ifa
;
1453 if (ifa
->ifa_addr
->sa_family
!= af
)
1455 if (ifa_maybe
== NULL
)
1457 if (ifa
->ifa_netmask
== NULL
) {
1458 if (sa_equal(addr
, ifa
->ifa_addr
) ||
1459 (ifa
->ifa_dstaddr
!= NULL
&&
1460 sa_equal(addr
, ifa
->ifa_dstaddr
)))
1464 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1465 if (sa_equal(addr
, ifa
->ifa_dstaddr
))
1469 cp2
= ifa
->ifa_addr
->sa_data
;
1470 cp3
= ifa
->ifa_netmask
->sa_data
;
1471 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
1472 for (; cp3
< cplim
; cp3
++)
1473 if ((*cp
++ ^ *cp2
++) & *cp3
)
1483 * Default action when installing a route with a Link Level gateway.
1484 * Lookup an appropriate real ifa to point to.
1485 * This should be moved to /sys/net/link.c eventually.
1488 link_rtrequest(int cmd
, struct rtentry
*rt
)
1491 struct sockaddr
*dst
;
1494 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
1495 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_key(rt
)) == NULL
)
1497 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1499 IFAFREE(rt
->rt_ifa
);
1502 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
1503 ifa
->ifa_rtrequest(cmd
, rt
);
1507 struct netmsg_ifroute
{
1508 struct netmsg_base base
;
1515 * Mark an interface down and notify protocols of the transition.
1518 if_unroute_dispatch(netmsg_t nmsg
)
1520 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1521 struct ifnet
*ifp
= msg
->ifp
;
1522 int flag
= msg
->flag
, fam
= msg
->fam
;
1523 struct ifaddr_container
*ifac
;
1525 ifp
->if_flags
&= ~flag
;
1526 getmicrotime(&ifp
->if_lastchange
);
1528 * The ifaddr processing in the following loop will block,
1529 * however, this function is called in netisr0, in which
1530 * ifaddr list changes happen, so we don't care about the
1531 * blockness of the ifaddr processing here.
1533 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1534 struct ifaddr
*ifa
= ifac
->ifa
;
1537 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1540 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1541 kpfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
1543 ifq_purge_all(&ifp
->if_snd
);
1546 lwkt_replymsg(&nmsg
->lmsg
, 0);
1550 if_unroute(struct ifnet
*ifp
, int flag
, int fam
)
1552 struct netmsg_ifroute msg
;
1554 ASSERT_CANDOMSG_NETISR0(curthread
);
1556 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1557 if_unroute_dispatch
);
1561 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1565 * Mark an interface up and notify protocols of the transition.
1568 if_route_dispatch(netmsg_t nmsg
)
1570 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1571 struct ifnet
*ifp
= msg
->ifp
;
1572 int flag
= msg
->flag
, fam
= msg
->fam
;
1573 struct ifaddr_container
*ifac
;
1575 ifq_purge_all(&ifp
->if_snd
);
1576 ifp
->if_flags
|= flag
;
1577 getmicrotime(&ifp
->if_lastchange
);
1579 * The ifaddr processing in the following loop will block,
1580 * however, this function is called in netisr0, in which
1581 * ifaddr list changes happen, so we don't care about the
1582 * blockness of the ifaddr processing here.
1584 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1585 struct ifaddr
*ifa
= ifac
->ifa
;
1588 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1591 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1592 kpfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
1599 lwkt_replymsg(&nmsg
->lmsg
, 0);
1603 if_route(struct ifnet
*ifp
, int flag
, int fam
)
1605 struct netmsg_ifroute msg
;
1607 ASSERT_CANDOMSG_NETISR0(curthread
);
1609 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1614 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1618 * Mark an interface down and notify protocols of the transition. An
1619 * interface going down is also considered to be a synchronizing event.
1620 * We must ensure that all packet processing related to the interface
1621 * has completed before we return so e.g. the caller can free the ifnet
1622 * structure that the mbufs may be referencing.
1624 * NOTE: must be called at splnet or eqivalent.
1627 if_down(struct ifnet
*ifp
)
1629 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
1630 netmsg_service_sync();
1634 * Mark an interface up and notify protocols of
1636 * NOTE: must be called at splnet or eqivalent.
1639 if_up(struct ifnet
*ifp
)
1641 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
1645 * Process a link state change.
1646 * NOTE: must be called at splsoftnet or equivalent.
1649 if_link_state_change(struct ifnet
*ifp
)
1651 int link_state
= ifp
->if_link_state
;
1654 devctl_notify("IFNET", ifp
->if_xname
,
1655 (link_state
== LINK_STATE_UP
) ? "LINK_UP" : "LINK_DOWN", NULL
);
1659 * Handle interface watchdog timer routines. Called
1660 * from softclock, we decrement timers (if set) and
1661 * call the appropriate interface routine on expiration.
1664 if_slowtimo_dispatch(netmsg_t nmsg
)
1666 struct globaldata
*gd
= mycpu
;
1667 const struct ifnet_array
*arr
;
1670 ASSERT_IN_NETISR(0);
1673 lwkt_replymsg(&nmsg
->lmsg
, 0); /* reply ASAP */
1676 arr
= ifnet_array_get();
1677 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1678 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1682 if (if_stats_compat
) {
1683 IFNET_STAT_GET(ifp
, ipackets
, ifp
->if_ipackets
);
1684 IFNET_STAT_GET(ifp
, ierrors
, ifp
->if_ierrors
);
1685 IFNET_STAT_GET(ifp
, opackets
, ifp
->if_opackets
);
1686 IFNET_STAT_GET(ifp
, oerrors
, ifp
->if_oerrors
);
1687 IFNET_STAT_GET(ifp
, collisions
, ifp
->if_collisions
);
1688 IFNET_STAT_GET(ifp
, ibytes
, ifp
->if_ibytes
);
1689 IFNET_STAT_GET(ifp
, obytes
, ifp
->if_obytes
);
1690 IFNET_STAT_GET(ifp
, imcasts
, ifp
->if_imcasts
);
1691 IFNET_STAT_GET(ifp
, omcasts
, ifp
->if_omcasts
);
1692 IFNET_STAT_GET(ifp
, iqdrops
, ifp
->if_iqdrops
);
1693 IFNET_STAT_GET(ifp
, noproto
, ifp
->if_noproto
);
1694 IFNET_STAT_GET(ifp
, oqdrops
, ifp
->if_oqdrops
);
1697 if (ifp
->if_timer
== 0 || --ifp
->if_timer
) {
1701 if (ifp
->if_watchdog
) {
1702 if (ifnet_tryserialize_all(ifp
)) {
1703 (*ifp
->if_watchdog
)(ifp
);
1704 ifnet_deserialize_all(ifp
);
1706 /* try again next timeout */
1714 callout_reset(&if_slowtimo_timer
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
1718 if_slowtimo(void *arg __unused
)
1720 struct lwkt_msg
*lmsg
= &if_slowtimo_netmsg
.lmsg
;
1722 KASSERT(mycpuid
== 0, ("not on cpu0"));
1724 if (lmsg
->ms_flags
& MSGF_DONE
)
1725 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg
);
1730 * Map interface name to
1731 * interface structure pointer.
1734 ifunit(const char *name
)
1739 * Search all the interfaces for this name/number
1741 KASSERT(mtx_owned(&ifnet_mtx
), ("ifnet is not locked"));
1743 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
1744 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1751 ifunit_netisr(const char *name
)
1753 const struct ifnet_array
*arr
;
1757 * Search all the interfaces for this name/number
1760 arr
= ifnet_array_get();
1761 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1762 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1764 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1774 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ucred
*cred
)
1779 int error
, do_ifup
= 0;
1782 size_t namelen
, onamelen
;
1783 char new_name
[IFNAMSIZ
];
1785 struct sockaddr_dl
*sdl
;
1790 return (ifconf(cmd
, data
, cred
));
1795 ifr
= (struct ifreq
*)data
;
1800 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1802 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1803 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
1805 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1807 return (if_clone_destroy(ifr
->ifr_name
));
1808 case SIOCIFGCLONERS
:
1809 return (if_clone_list((struct if_clonereq
*)data
));
1815 * Nominal ioctl through interface, lookup the ifp and obtain a
1816 * lock to serialize the ifconfig ioctl operation.
1820 ifp
= ifunit(ifr
->ifr_name
);
1829 ifr
->ifr_index
= ifp
->if_index
;
1833 ifr
->ifr_flags
= ifp
->if_flags
;
1834 ifr
->ifr_flagshigh
= ifp
->if_flags
>> 16;
1838 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1839 ifr
->ifr_curcap
= ifp
->if_capenable
;
1843 ifr
->ifr_metric
= ifp
->if_metric
;
1847 ifr
->ifr_mtu
= ifp
->if_mtu
;
1851 ifr
->ifr_tsolen
= ifp
->if_tsolen
;
1855 error
= copyout((caddr_t
)&ifp
->if_data
, ifr
->ifr_data
,
1856 sizeof(ifp
->if_data
));
1860 ifr
->ifr_phys
= ifp
->if_physical
;
1863 case SIOCGIFPOLLCPU
:
1864 ifr
->ifr_pollcpu
= -1;
1867 case SIOCSIFPOLLCPU
:
1871 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1874 new_flags
= (ifr
->ifr_flags
& 0xffff) |
1875 (ifr
->ifr_flagshigh
<< 16);
1876 if (ifp
->if_flags
& IFF_SMART
) {
1877 /* Smart drivers twiddle their own routes */
1878 } else if (ifp
->if_flags
& IFF_UP
&&
1879 (new_flags
& IFF_UP
) == 0) {
1881 } else if (new_flags
& IFF_UP
&&
1882 (ifp
->if_flags
& IFF_UP
) == 0) {
1886 #ifdef IFPOLL_ENABLE
1887 if ((new_flags
^ ifp
->if_flags
) & IFF_NPOLLING
) {
1888 if (new_flags
& IFF_NPOLLING
)
1889 ifpoll_register(ifp
);
1891 ifpoll_deregister(ifp
);
1895 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1896 (new_flags
&~ IFF_CANTCHANGE
);
1897 if (new_flags
& IFF_PPROMISC
) {
1898 /* Permanently promiscuous mode requested */
1899 ifp
->if_flags
|= IFF_PROMISC
;
1900 } else if (ifp
->if_pcount
== 0) {
1901 ifp
->if_flags
&= ~IFF_PROMISC
;
1903 if (ifp
->if_ioctl
) {
1904 ifnet_serialize_all(ifp
);
1905 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1906 ifnet_deserialize_all(ifp
);
1910 getmicrotime(&ifp
->if_lastchange
);
1914 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1917 if (ifr
->ifr_reqcap
& ~ifp
->if_capabilities
) {
1921 ifnet_serialize_all(ifp
);
1922 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1923 ifnet_deserialize_all(ifp
);
1927 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1930 error
= copyinstr(ifr
->ifr_data
, new_name
, IFNAMSIZ
, NULL
);
1933 if (new_name
[0] == '\0') {
1937 if (ifunit(new_name
) != NULL
) {
1942 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
1944 /* Announce the departure of the interface. */
1945 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
1947 strlcpy(ifp
->if_xname
, new_name
, sizeof(ifp
->if_xname
));
1948 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
1949 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1950 namelen
= strlen(new_name
);
1951 onamelen
= sdl
->sdl_nlen
;
1953 * Move the address if needed. This is safe because we
1954 * allocate space for a name of length IFNAMSIZ when we
1955 * create this in if_attach().
1957 if (namelen
!= onamelen
) {
1958 bcopy(sdl
->sdl_data
+ onamelen
,
1959 sdl
->sdl_data
+ namelen
, sdl
->sdl_alen
);
1961 bcopy(new_name
, sdl
->sdl_data
, namelen
);
1962 sdl
->sdl_nlen
= namelen
;
1963 sdl
= (struct sockaddr_dl
*)ifa
->ifa_netmask
;
1964 bzero(sdl
->sdl_data
, onamelen
);
1965 while (namelen
!= 0)
1966 sdl
->sdl_data
[--namelen
] = 0xff;
1968 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
1970 /* Announce the return of the interface. */
1971 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
1975 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1978 ifp
->if_metric
= ifr
->ifr_metric
;
1979 getmicrotime(&ifp
->if_lastchange
);
1983 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1986 if (ifp
->if_ioctl
== NULL
) {
1990 ifnet_serialize_all(ifp
);
1991 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1992 ifnet_deserialize_all(ifp
);
1994 getmicrotime(&ifp
->if_lastchange
);
1999 u_long oldmtu
= ifp
->if_mtu
;
2001 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2004 if (ifp
->if_ioctl
== NULL
) {
2008 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2012 ifnet_serialize_all(ifp
);
2013 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2014 ifnet_deserialize_all(ifp
);
2016 getmicrotime(&ifp
->if_lastchange
);
2020 * If the link MTU changed, do network layer specific procedure.
2022 if (ifp
->if_mtu
!= oldmtu
) {
2031 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2035 /* XXX need driver supplied upper limit */
2036 if (ifr
->ifr_tsolen
<= 0) {
2040 ifp
->if_tsolen
= ifr
->ifr_tsolen
;
2045 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2049 /* Don't allow group membership on non-multicast interfaces. */
2050 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2055 /* Don't let users screw up protocols' entries. */
2056 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2061 if (cmd
== SIOCADDMULTI
) {
2062 struct ifmultiaddr
*ifma
;
2063 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
2065 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
2068 getmicrotime(&ifp
->if_lastchange
);
2071 case SIOCSIFPHYADDR
:
2072 case SIOCDIFPHYADDR
:
2074 case SIOCSIFPHYADDR_IN6
:
2076 case SIOCSLIFPHYADDR
:
2078 case SIOCSIFGENERIC
:
2079 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2082 if (ifp
->if_ioctl
== 0) {
2086 ifnet_serialize_all(ifp
);
2087 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2088 ifnet_deserialize_all(ifp
);
2090 getmicrotime(&ifp
->if_lastchange
);
2094 ifs
= (struct ifstat
*)data
;
2095 ifs
->ascii
[0] = '\0';
2097 case SIOCGIFPSRCADDR
:
2098 case SIOCGIFPDSTADDR
:
2099 case SIOCGLIFPHYADDR
:
2101 case SIOCGIFGENERIC
:
2102 if (ifp
->if_ioctl
== NULL
) {
2106 ifnet_serialize_all(ifp
);
2107 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2108 ifnet_deserialize_all(ifp
);
2112 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2115 error
= if_setlladdr(ifp
, ifr
->ifr_addr
.sa_data
,
2116 ifr
->ifr_addr
.sa_len
);
2117 EVENTHANDLER_INVOKE(iflladdr_event
, ifp
);
2121 oif_flags
= ifp
->if_flags
;
2122 if (so
->so_proto
== 0) {
2126 error
= so_pru_control_direct(so
, cmd
, data
, ifp
);
2128 if ((oif_flags
^ ifp
->if_flags
) & IFF_UP
) {
2130 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2131 if (ifp
->if_flags
& IFF_UP
) {
2146 * Set/clear promiscuous mode on interface ifp based on the truth value
2147 * of pswitch. The calls are reference counted so that only the first
2148 * "on" request actually has an effect, as does the final "off" request.
2149 * Results are undefined if the "off" and "on" requests are not matched.
2152 ifpromisc(struct ifnet
*ifp
, int pswitch
)
2158 oldflags
= ifp
->if_flags
;
2159 if (ifp
->if_flags
& IFF_PPROMISC
) {
2160 /* Do nothing if device is in permanently promiscuous mode */
2161 ifp
->if_pcount
+= pswitch
? 1 : -1;
2166 * If the device is not configured up, we cannot put it in
2169 if ((ifp
->if_flags
& IFF_UP
) == 0)
2171 if (ifp
->if_pcount
++ != 0)
2173 ifp
->if_flags
|= IFF_PROMISC
;
2174 log(LOG_INFO
, "%s: promiscuous mode enabled\n",
2177 if (--ifp
->if_pcount
> 0)
2179 ifp
->if_flags
&= ~IFF_PROMISC
;
2180 log(LOG_INFO
, "%s: promiscuous mode disabled\n",
2183 ifr
.ifr_flags
= ifp
->if_flags
;
2184 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2185 ifnet_serialize_all(ifp
);
2186 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, NULL
);
2187 ifnet_deserialize_all(ifp
);
2191 ifp
->if_flags
= oldflags
;
2196 * Return interface configuration
2197 * of system. List may be used
2198 * in later ioctl's (above) to get
2199 * other information.
2202 ifconf(u_long cmd
, caddr_t data
, struct ucred
*cred
)
2204 struct ifconf
*ifc
= (struct ifconf
*)data
;
2206 struct sockaddr
*sa
;
2207 struct ifreq ifr
, *ifrp
;
2208 int space
= ifc
->ifc_len
, error
= 0;
2210 ifrp
= ifc
->ifc_req
;
2213 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2214 struct ifaddr_container
*ifac
, *ifac_mark
;
2215 struct ifaddr_marker mark
;
2216 struct ifaddrhead
*head
;
2219 if (space
<= sizeof ifr
)
2223 * Zero the stack declared structure first to prevent
2224 * memory disclosure.
2226 bzero(&ifr
, sizeof(ifr
));
2227 if (strlcpy(ifr
.ifr_name
, ifp
->if_xname
, sizeof(ifr
.ifr_name
))
2228 >= sizeof(ifr
.ifr_name
)) {
2229 error
= ENAMETOOLONG
;
2234 * Add a marker, since copyout() could block and during that
2235 * period the list could be changed. Inserting the marker to
2236 * the header of the list will not cause trouble for the code
2237 * assuming that the first element of the list is AF_LINK; the
2238 * marker will be moved to the next position w/o blocking.
2240 ifa_marker_init(&mark
, ifp
);
2241 ifac_mark
= &mark
.ifac
;
2242 head
= &ifp
->if_addrheads
[mycpuid
];
2245 TAILQ_INSERT_HEAD(head
, ifac_mark
, ifa_link
);
2246 while ((ifac
= TAILQ_NEXT(ifac_mark
, ifa_link
)) != NULL
) {
2247 struct ifaddr
*ifa
= ifac
->ifa
;
2249 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2250 TAILQ_INSERT_AFTER(head
, ifac
, ifac_mark
, ifa_link
);
2253 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
2256 if (space
<= sizeof ifr
)
2259 if (cred
->cr_prison
&&
2260 prison_if(cred
, sa
))
2264 * Keep a reference on this ifaddr, so that it will
2265 * not be destroyed when its address is copied to
2266 * the userland, which could block.
2269 if (sa
->sa_len
<= sizeof(*sa
)) {
2271 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2274 if (space
< (sizeof ifr
) + sa
->sa_len
-
2279 space
-= sa
->sa_len
- sizeof(*sa
);
2280 error
= copyout(&ifr
, ifrp
,
2281 sizeof ifr
.ifr_name
);
2283 error
= copyout(sa
, &ifrp
->ifr_addr
,
2285 ifrp
= (struct ifreq
*)
2286 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
2291 space
-= sizeof ifr
;
2293 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2297 bzero(&ifr
.ifr_addr
, sizeof ifr
.ifr_addr
);
2298 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2301 space
-= sizeof ifr
;
2307 ifc
->ifc_len
-= space
;
2312 * Just like if_promisc(), but for all-multicast-reception mode.
2315 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2323 if (ifp
->if_amcount
++ == 0) {
2324 ifp
->if_flags
|= IFF_ALLMULTI
;
2325 ifr
.ifr_flags
= ifp
->if_flags
;
2326 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2327 ifnet_serialize_all(ifp
);
2328 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2330 ifnet_deserialize_all(ifp
);
2333 if (ifp
->if_amcount
> 1) {
2336 ifp
->if_amcount
= 0;
2337 ifp
->if_flags
&= ~IFF_ALLMULTI
;
2338 ifr
.ifr_flags
= ifp
->if_flags
;
2339 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2340 ifnet_serialize_all(ifp
);
2341 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2343 ifnet_deserialize_all(ifp
);
2355 * Add a multicast listenership to the interface in question.
2356 * The link layer provides a routine which converts
2359 if_addmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
,
2360 struct ifmultiaddr
**retifma
)
2362 struct sockaddr
*llsa
, *dupsa
;
2364 struct ifmultiaddr
*ifma
;
2366 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2369 * If the matching multicast address already exists
2370 * then don't add a new one, just add a reference
2372 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2373 if (sa_equal(sa
, ifma
->ifma_addr
)) {
2374 ifma
->ifma_refcount
++;
2382 * Give the link layer a chance to accept/reject it, and also
2383 * find out which AF_LINK address this maps to, if it isn't one
2386 if (ifp
->if_resolvemulti
) {
2387 error
= ifp
->if_resolvemulti(ifp
, &llsa
, sa
);
2394 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2395 dupsa
= kmalloc(sa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2396 bcopy(sa
, dupsa
, sa
->sa_len
);
2398 ifma
->ifma_addr
= dupsa
;
2399 ifma
->ifma_lladdr
= llsa
;
2400 ifma
->ifma_ifp
= ifp
;
2401 ifma
->ifma_refcount
= 1;
2402 ifma
->ifma_protospec
= NULL
;
2403 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
2405 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2410 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2411 if (sa_equal(ifma
->ifma_addr
, llsa
))
2415 ifma
->ifma_refcount
++;
2417 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2418 dupsa
= kmalloc(llsa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2419 bcopy(llsa
, dupsa
, llsa
->sa_len
);
2420 ifma
->ifma_addr
= dupsa
;
2421 ifma
->ifma_ifp
= ifp
;
2422 ifma
->ifma_refcount
= 1;
2423 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2427 * We are certain we have added something, so call down to the
2428 * interface to let them know about it.
2431 ifp
->if_ioctl(ifp
, SIOCADDMULTI
, 0, NULL
);
2437 if_addmulti(struct ifnet
*ifp
, struct sockaddr
*sa
,
2438 struct ifmultiaddr
**retifma
)
2442 ifnet_serialize_all(ifp
);
2443 error
= if_addmulti_serialized(ifp
, sa
, retifma
);
2444 ifnet_deserialize_all(ifp
);
2450 * Remove a reference to a multicast address on this interface. Yell
2451 * if the request does not match an existing membership.
2454 if_delmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
)
2456 struct ifmultiaddr
*ifma
;
2458 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2460 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2461 if (sa_equal(sa
, ifma
->ifma_addr
))
2466 if (ifma
->ifma_refcount
> 1) {
2467 ifma
->ifma_refcount
--;
2471 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2472 sa
= ifma
->ifma_lladdr
;
2473 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2475 * Make sure the interface driver is notified
2476 * in the case of a link layer mcast group being left.
2478 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== NULL
)
2479 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2480 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2481 kfree(ifma
, M_IFMADDR
);
2486 * Now look for the link-layer address which corresponds to
2487 * this network address. It had been squirreled away in
2488 * ifma->ifma_lladdr for this purpose (so we don't have
2489 * to call ifp->if_resolvemulti() again), and we saved that
2490 * value in sa above. If some nasty deleted the
2491 * link-layer address out from underneath us, we can deal because
2492 * the address we stored was is not the same as the one which was
2493 * in the record for the link-layer address. (So we don't complain
2496 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2497 if (sa_equal(sa
, ifma
->ifma_addr
))
2502 if (ifma
->ifma_refcount
> 1) {
2503 ifma
->ifma_refcount
--;
2507 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2508 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2509 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2510 kfree(sa
, M_IFMADDR
);
2511 kfree(ifma
, M_IFMADDR
);
2517 if_delmulti(struct ifnet
*ifp
, struct sockaddr
*sa
)
2521 ifnet_serialize_all(ifp
);
2522 error
= if_delmulti_serialized(ifp
, sa
);
2523 ifnet_deserialize_all(ifp
);
2529 * Delete all multicast group membership for an interface.
2530 * Should be used to quickly flush all multicast filters.
2533 if_delallmulti_serialized(struct ifnet
*ifp
)
2535 struct ifmultiaddr
*ifma
, mark
;
2538 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2540 bzero(&sa
, sizeof(sa
));
2541 sa
.sa_family
= AF_UNSPEC
;
2542 sa
.sa_len
= sizeof(sa
);
2544 bzero(&mark
, sizeof(mark
));
2545 mark
.ifma_addr
= &sa
;
2547 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2548 while ((ifma
= TAILQ_NEXT(&mark
, ifma_link
)) != NULL
) {
2549 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2550 TAILQ_INSERT_AFTER(&ifp
->if_multiaddrs
, ifma
, &mark
,
2553 if (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
)
2556 if_delmulti_serialized(ifp
, ifma
->ifma_addr
);
2558 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2563 * Set the link layer address on an interface.
2565 * At this time we only support certain types of interfaces,
2566 * and we don't allow the length of the address to change.
2569 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2571 struct sockaddr_dl
*sdl
;
2574 sdl
= IF_LLSOCKADDR(ifp
);
2577 if (len
!= sdl
->sdl_alen
) /* don't allow length to change */
2579 switch (ifp
->if_type
) {
2580 case IFT_ETHER
: /* these types use struct arpcom */
2583 case IFT_IEEE8023ADLAG
:
2584 bcopy(lladdr
, ((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, len
);
2585 bcopy(lladdr
, LLADDR(sdl
), len
);
2591 * If the interface is already up, we need
2592 * to re-init it in order to reprogram its
2595 ifnet_serialize_all(ifp
);
2596 if ((ifp
->if_flags
& IFF_UP
) != 0) {
2598 struct ifaddr_container
*ifac
;
2601 ifp
->if_flags
&= ~IFF_UP
;
2602 ifr
.ifr_flags
= ifp
->if_flags
;
2603 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2604 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2606 ifp
->if_flags
|= IFF_UP
;
2607 ifr
.ifr_flags
= ifp
->if_flags
;
2608 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2609 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2613 * Also send gratuitous ARPs to notify other nodes about
2614 * the address change.
2616 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
2617 struct ifaddr
*ifa
= ifac
->ifa
;
2619 if (ifa
->ifa_addr
!= NULL
&&
2620 ifa
->ifa_addr
->sa_family
== AF_INET
)
2621 arp_gratuitous(ifp
, ifa
);
2625 ifnet_deserialize_all(ifp
);
2629 struct ifmultiaddr
*
2630 ifmaof_ifpforaddr(struct sockaddr
*sa
, struct ifnet
*ifp
)
2632 struct ifmultiaddr
*ifma
;
2634 /* TODO: need ifnet_serialize_main */
2635 ifnet_serialize_all(ifp
);
2636 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2637 if (sa_equal(ifma
->ifma_addr
, sa
))
2639 ifnet_deserialize_all(ifp
);
2645 * This function locates the first real ethernet MAC from a network
2646 * card and loads it into node, returning 0 on success or ENOENT if
2647 * no suitable interfaces were found. It is used by the uuid code to
2648 * generate a unique 6-byte number.
2651 if_getanyethermac(uint16_t *node
, int minlen
)
2654 struct sockaddr_dl
*sdl
;
2657 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2658 if (ifp
->if_type
!= IFT_ETHER
)
2660 sdl
= IF_LLSOCKADDR(ifp
);
2661 if (sdl
->sdl_alen
< minlen
)
2663 bcopy(((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, node
,
2673 * The name argument must be a pointer to storage which will last as
2674 * long as the interface does. For physical devices, the result of
2675 * device_get_name(dev) is a good choice and for pseudo-devices a
2676 * static string works well.
2679 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
2681 ifp
->if_dname
= name
;
2682 ifp
->if_dunit
= unit
;
2683 if (unit
!= IF_DUNIT_NONE
)
2684 ksnprintf(ifp
->if_xname
, IFNAMSIZ
, "%s%d", name
, unit
);
2686 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
2690 if_printf(struct ifnet
*ifp
, const char *fmt
, ...)
2695 retval
= kprintf("%s: ", ifp
->if_xname
);
2696 __va_start(ap
, fmt
);
2697 retval
+= kvprintf(fmt
, ap
);
2703 if_alloc(uint8_t type
)
2709 * XXX temporary hack until arpcom is setup in if_l2com
2711 if (type
== IFT_ETHER
)
2712 size
= sizeof(struct arpcom
);
2714 size
= sizeof(struct ifnet
);
2716 ifp
= kmalloc(size
, M_IFNET
, M_WAITOK
|M_ZERO
);
2718 ifp
->if_type
= type
;
2720 if (if_com_alloc
[type
] != NULL
) {
2721 ifp
->if_l2com
= if_com_alloc
[type
](type
, ifp
);
2722 if (ifp
->if_l2com
== NULL
) {
2723 kfree(ifp
, M_IFNET
);
2731 if_free(struct ifnet
*ifp
)
2733 kfree(ifp
, M_IFNET
);
2737 ifq_set_classic(struct ifaltq
*ifq
)
2739 ifq_set_methods(ifq
, ifq
->altq_ifp
->if_mapsubq
,
2740 ifsq_classic_enqueue
, ifsq_classic_dequeue
, ifsq_classic_request
);
2744 ifq_set_methods(struct ifaltq
*ifq
, altq_mapsubq_t mapsubq
,
2745 ifsq_enqueue_t enqueue
, ifsq_dequeue_t dequeue
, ifsq_request_t request
)
2749 KASSERT(mapsubq
!= NULL
, ("mapsubq is not specified"));
2750 KASSERT(enqueue
!= NULL
, ("enqueue is not specified"));
2751 KASSERT(dequeue
!= NULL
, ("dequeue is not specified"));
2752 KASSERT(request
!= NULL
, ("request is not specified"));
2754 ifq
->altq_mapsubq
= mapsubq
;
2755 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
2756 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
2758 ifsq
->ifsq_enqueue
= enqueue
;
2759 ifsq
->ifsq_dequeue
= dequeue
;
2760 ifsq
->ifsq_request
= request
;
2765 ifsq_norm_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2768 classq_add(&ifsq
->ifsq_norm
, m
);
2769 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2773 ifsq_prio_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2776 classq_add(&ifsq
->ifsq_prio
, m
);
2777 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2778 ALTQ_SQ_PRIO_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2781 static struct mbuf
*
2782 ifsq_norm_dequeue(struct ifaltq_subque
*ifsq
)
2786 m
= classq_get(&ifsq
->ifsq_norm
);
2788 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2792 static struct mbuf
*
2793 ifsq_prio_dequeue(struct ifaltq_subque
*ifsq
)
2797 m
= classq_get(&ifsq
->ifsq_prio
);
2799 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2800 ALTQ_SQ_PRIO_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2806 ifsq_classic_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
,
2807 struct altq_pktattr
*pa __unused
)
2812 if (ifsq
->ifsq_len
>= ifsq
->ifsq_maxlen
||
2813 ifsq
->ifsq_bcnt
>= ifsq
->ifsq_maxbcnt
) {
2814 struct mbuf
*m_drop
;
2816 if (m
->m_flags
& M_PRIO
) {
2818 if (ifsq
->ifsq_prio_len
< (ifsq
->ifsq_maxlen
>> 1) &&
2819 ifsq
->ifsq_prio_bcnt
< (ifsq
->ifsq_maxbcnt
>> 1)) {
2820 /* Try dropping some from normal queue. */
2821 m_drop
= ifsq_norm_dequeue(ifsq
);
2824 m_drop
= ifsq_prio_dequeue(ifsq
);
2826 m_drop
= ifsq_norm_dequeue(ifsq
);
2828 if (m_drop
!= NULL
) {
2829 IFNET_STAT_INC(ifsq
->ifsq_ifp
, oqdrops
, 1);
2834 * No old packets could be dropped!
2835 * NOTE: Caller increases oqdrops.
2840 if (m
->m_flags
& M_PRIO
)
2841 ifsq_prio_enqueue(ifsq
, m
);
2843 ifsq_norm_enqueue(ifsq
, m
);
2849 ifsq_classic_dequeue(struct ifaltq_subque
*ifsq
, int op
)
2855 m
= classq_head(&ifsq
->ifsq_prio
);
2857 m
= classq_head(&ifsq
->ifsq_norm
);
2861 m
= ifsq_prio_dequeue(ifsq
);
2863 m
= ifsq_norm_dequeue(ifsq
);
2867 panic("unsupported ALTQ dequeue op: %d", op
);
2873 ifsq_classic_request(struct ifaltq_subque
*ifsq
, int req
, void *arg
)
2880 m
= ifsq_classic_dequeue(ifsq
, ALTDQ_REMOVE
);
2888 panic("unsupported ALTQ request: %d", req
);
2894 ifsq_ifstart_try(struct ifaltq_subque
*ifsq
, int force_sched
)
2896 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
2897 int running
= 0, need_sched
;
2900 * Try to do direct ifnet.if_start on the subqueue first, if there is
2901 * contention on the subqueue hardware serializer, ifnet.if_start on
2902 * the subqueue will be scheduled on the subqueue owner CPU.
2904 if (!ifsq_tryserialize_hw(ifsq
)) {
2906 * Subqueue hardware serializer contention happened,
2907 * ifnet.if_start on the subqueue is scheduled on
2908 * the subqueue owner CPU, and we keep going.
2910 ifsq_ifstart_schedule(ifsq
, 1);
2914 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
2915 ifp
->if_start(ifp
, ifsq
);
2916 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
2919 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
2921 ifsq_deserialize_hw(ifsq
);
2925 * More data need to be transmitted, ifnet.if_start on the
2926 * subqueue is scheduled on the subqueue owner CPU, and we
2928 * NOTE: ifnet.if_start subqueue interlock is not released.
2930 ifsq_ifstart_schedule(ifsq
, force_sched
);
2935 * Subqeue packets staging mechanism:
2937 * The packets enqueued into the subqueue are staged to a certain amount
2938 * before the ifnet.if_start on the subqueue is called. In this way, the
2939 * driver could avoid writing to hardware registers upon every packet,
2940 * instead, hardware registers could be written when certain amount of
2941 * packets are put onto hardware TX ring. The measurement on several modern
2942 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2943 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2944 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2945 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2946 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2948 * Subqueue packets staging is performed for two entry points into drivers'
2949 * transmission function:
2950 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2951 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2953 * Subqueue packets staging will be stopped upon any of the following
2955 * - If the count of packets enqueued on the current CPU is great than or
2956 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2957 * - If the total length of packets enqueued on the current CPU is great
2958 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2959 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2960 * is usually less than hardware's MTU.
2961 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2962 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2964 * - The if_start_rollup(), which is registered as low priority netisr
2965 * rollup function, is called; probably because no more work is pending
2969 * Currently subqueue packet staging is only performed in netisr threads.
2972 ifq_dispatch(struct ifnet
*ifp
, struct mbuf
*m
, struct altq_pktattr
*pa
)
2974 struct ifaltq
*ifq
= &ifp
->if_snd
;
2975 struct ifaltq_subque
*ifsq
;
2976 int error
, start
= 0, len
, mcast
= 0, avoid_start
= 0;
2977 struct ifsubq_stage_head
*head
= NULL
;
2978 struct ifsubq_stage
*stage
= NULL
;
2979 struct globaldata
*gd
= mycpu
;
2980 struct thread
*td
= gd
->gd_curthread
;
2982 crit_enter_quick(td
);
2984 ifsq
= ifq_map_subq(ifq
, gd
->gd_cpuid
);
2985 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq
);
2987 len
= m
->m_pkthdr
.len
;
2988 if (m
->m_flags
& M_MCAST
)
2991 if (td
->td_type
== TD_TYPE_NETISR
) {
2992 head
= &ifsubq_stage_heads
[mycpuid
];
2993 stage
= ifsq_get_stage(ifsq
, mycpuid
);
2996 stage
->stg_len
+= len
;
2997 if (stage
->stg_cnt
< ifsq_stage_cntmax
&&
2998 stage
->stg_len
< (ifp
->if_mtu
- max_protohdr
))
3003 error
= ifsq_enqueue_locked(ifsq
, m
, pa
);
3005 IFNET_STAT_INC(ifp
, oqdrops
, 1);
3006 if (!ifsq_data_ready(ifsq
)) {
3007 ALTQ_SQ_UNLOCK(ifsq
);
3008 crit_exit_quick(td
);
3013 if (!ifsq_is_started(ifsq
)) {
3015 ALTQ_SQ_UNLOCK(ifsq
);
3018 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
3019 ifsq_stage_insert(head
, stage
);
3021 IFNET_STAT_INC(ifp
, obytes
, len
);
3023 IFNET_STAT_INC(ifp
, omcasts
, 1);
3024 crit_exit_quick(td
);
3029 * Hold the subqueue interlock of ifnet.if_start
3031 ifsq_set_started(ifsq
);
3034 ALTQ_SQ_UNLOCK(ifsq
);
3037 IFNET_STAT_INC(ifp
, obytes
, len
);
3039 IFNET_STAT_INC(ifp
, omcasts
, 1);
3042 if (stage
!= NULL
) {
3043 if (!start
&& (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)) {
3044 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
3046 ifsq_stage_remove(head
, stage
);
3047 ifsq_ifstart_schedule(ifsq
, 1);
3049 crit_exit_quick(td
);
3053 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) {
3054 ifsq_stage_remove(head
, stage
);
3062 crit_exit_quick(td
);
3066 ifsq_ifstart_try(ifsq
, 0);
3068 crit_exit_quick(td
);
3073 ifa_create(int size
)
3078 KASSERT(size
>= sizeof(*ifa
), ("ifaddr size too small"));
3080 ifa
= kmalloc(size
, M_IFADDR
, M_INTWAIT
| M_ZERO
);
3081 ifa
->ifa_containers
=
3082 kmalloc_cachealign(ncpus
* sizeof(struct ifaddr_container
),
3083 M_IFADDR
, M_INTWAIT
| M_ZERO
);
3085 ifa
->ifa_ncnt
= ncpus
;
3086 for (i
= 0; i
< ncpus
; ++i
) {
3087 struct ifaddr_container
*ifac
= &ifa
->ifa_containers
[i
];
3089 ifac
->ifa_magic
= IFA_CONTAINER_MAGIC
;
3091 ifac
->ifa_refcnt
= 1;
3094 kprintf("alloc ifa %p %d\n", ifa
, size
);
3100 ifac_free(struct ifaddr_container
*ifac
, int cpu_id
)
3102 struct ifaddr
*ifa
= ifac
->ifa
;
3104 KKASSERT(ifac
->ifa_magic
== IFA_CONTAINER_MAGIC
);
3105 KKASSERT(ifac
->ifa_refcnt
== 0);
3106 KASSERT(ifac
->ifa_listmask
== 0,
3107 ("ifa is still on %#x lists", ifac
->ifa_listmask
));
3109 ifac
->ifa_magic
= IFA_CONTAINER_DEAD
;
3111 #ifdef IFADDR_DEBUG_VERBOSE
3112 kprintf("try free ifa %p cpu_id %d\n", ifac
->ifa
, cpu_id
);
3115 KASSERT(ifa
->ifa_ncnt
> 0 && ifa
->ifa_ncnt
<= ncpus
,
3116 ("invalid # of ifac, %d", ifa
->ifa_ncnt
));
3117 if (atomic_fetchadd_int(&ifa
->ifa_ncnt
, -1) == 1) {
3119 kprintf("free ifa %p\n", ifa
);
3121 kfree(ifa
->ifa_containers
, M_IFADDR
);
3122 kfree(ifa
, M_IFADDR
);
3127 ifa_iflink_dispatch(netmsg_t nmsg
)
3129 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3130 struct ifaddr
*ifa
= msg
->ifa
;
3131 struct ifnet
*ifp
= msg
->ifp
;
3133 struct ifaddr_container
*ifac
;
3137 ifac
= &ifa
->ifa_containers
[cpu
];
3138 ASSERT_IFAC_VALID(ifac
);
3139 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
) == 0,
3140 ("ifaddr is on if_addrheads"));
3142 ifac
->ifa_listmask
|= IFA_LIST_IFADDRHEAD
;
3144 TAILQ_INSERT_TAIL(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3146 TAILQ_INSERT_HEAD(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3150 netisr_forwardmsg(&nmsg
->base
, cpu
+ 1);
3154 ifa_iflink(struct ifaddr
*ifa
, struct ifnet
*ifp
, int tail
)
3156 struct netmsg_ifaddr msg
;
3158 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3159 0, ifa_iflink_dispatch
);
3164 netisr_domsg(&msg
.base
, 0);
3168 ifa_ifunlink_dispatch(netmsg_t nmsg
)
3170 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3171 struct ifaddr
*ifa
= msg
->ifa
;
3172 struct ifnet
*ifp
= msg
->ifp
;
3174 struct ifaddr_container
*ifac
;
3178 ifac
= &ifa
->ifa_containers
[cpu
];
3179 ASSERT_IFAC_VALID(ifac
);
3180 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
,
3181 ("ifaddr is not on if_addrhead"));
3183 TAILQ_REMOVE(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3184 ifac
->ifa_listmask
&= ~IFA_LIST_IFADDRHEAD
;
3188 netisr_forwardmsg(&nmsg
->base
, cpu
+ 1);
3192 ifa_ifunlink(struct ifaddr
*ifa
, struct ifnet
*ifp
)
3194 struct netmsg_ifaddr msg
;
3196 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3197 0, ifa_ifunlink_dispatch
);
3201 netisr_domsg(&msg
.base
, 0);
3205 ifa_destroy_dispatch(netmsg_t nmsg
)
3207 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3210 netisr_forwardmsg(&nmsg
->base
, mycpuid
+ 1);
3214 ifa_destroy(struct ifaddr
*ifa
)
3216 struct netmsg_ifaddr msg
;
3218 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3219 0, ifa_destroy_dispatch
);
3222 netisr_domsg(&msg
.base
, 0);
3226 if_start_rollup(void)
3228 struct ifsubq_stage_head
*head
= &ifsubq_stage_heads
[mycpuid
];
3229 struct ifsubq_stage
*stage
;
3233 while ((stage
= TAILQ_FIRST(&head
->stg_head
)) != NULL
) {
3234 struct ifaltq_subque
*ifsq
= stage
->stg_subq
;
3237 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)
3239 ifsq_stage_remove(head
, stage
);
3242 ifsq_ifstart_schedule(ifsq
, 1);
3247 if (!ifsq_is_started(ifsq
)) {
3249 * Hold the subqueue interlock of
3252 ifsq_set_started(ifsq
);
3255 ALTQ_SQ_UNLOCK(ifsq
);
3258 ifsq_ifstart_try(ifsq
, 1);
3260 KKASSERT((stage
->stg_flags
&
3261 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
3268 ifnetinit(void *dummy __unused
)
3272 for (i
= 0; i
< ncpus
; ++i
)
3273 TAILQ_INIT(&ifsubq_stage_heads
[i
].stg_head
);
3274 netisr_register_rollup(if_start_rollup
, NETISR_ROLLUP_PRIO_IFSTART
);
3278 if_register_com_alloc(u_char type
,
3279 if_com_alloc_t
*a
, if_com_free_t
*f
)
3282 KASSERT(if_com_alloc
[type
] == NULL
,
3283 ("if_register_com_alloc: %d already registered", type
));
3284 KASSERT(if_com_free
[type
] == NULL
,
3285 ("if_register_com_alloc: %d free already registered", type
));
3287 if_com_alloc
[type
] = a
;
3288 if_com_free
[type
] = f
;
3292 if_deregister_com_alloc(u_char type
)
3295 KASSERT(if_com_alloc
[type
] != NULL
,
3296 ("if_deregister_com_alloc: %d not registered", type
));
3297 KASSERT(if_com_free
[type
] != NULL
,
3298 ("if_deregister_com_alloc: %d free not registered", type
));
3299 if_com_alloc
[type
] = NULL
;
3300 if_com_free
[type
] = NULL
;
3304 if_ring_count2(int cnt
, int cnt_max
)
3308 KASSERT(cnt_max
>= 1 && powerof2(cnt_max
),
3309 ("invalid ring count max %d", cnt_max
));
3318 while ((1 << (shift
+ 1)) <= cnt
)
3322 KASSERT(cnt
>= 1 && cnt
<= ncpus2
&& cnt
<= cnt_max
,
3323 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3324 cnt
, ncpus2
, cnt_max
));
3329 ifq_set_maxlen(struct ifaltq
*ifq
, int len
)
3331 ifq
->altq_maxlen
= len
+ (ncpus
* ifsq_stage_cntmax
);
3335 ifq_mapsubq_default(struct ifaltq
*ifq __unused
, int cpuid __unused
)
3337 return ALTQ_SUBQ_INDEX_DEFAULT
;
3341 ifq_mapsubq_mask(struct ifaltq
*ifq
, int cpuid
)
3343 return (cpuid
& ifq
->altq_subq_mask
);
3347 ifsq_watchdog(void *arg
)
3349 struct ifsubq_watchdog
*wd
= arg
;
3352 if (__predict_true(wd
->wd_timer
== 0 || --wd
->wd_timer
))
3355 ifp
= ifsq_get_ifp(wd
->wd_subq
);
3356 if (ifnet_tryserialize_all(ifp
)) {
3357 wd
->wd_watchdog(wd
->wd_subq
);
3358 ifnet_deserialize_all(ifp
);
3360 /* try again next timeout */
3364 ifsq_watchdog_reset(wd
);
3368 ifsq_watchdog_reset(struct ifsubq_watchdog
*wd
)
3370 callout_reset_bycpu(&wd
->wd_callout
, hz
, ifsq_watchdog
, wd
,
3371 ifsq_get_cpuid(wd
->wd_subq
));
3375 ifsq_watchdog_init(struct ifsubq_watchdog
*wd
, struct ifaltq_subque
*ifsq
,
3376 ifsq_watchdog_t watchdog
)
3378 callout_init_mp(&wd
->wd_callout
);
3381 wd
->wd_watchdog
= watchdog
;
3385 ifsq_watchdog_start(struct ifsubq_watchdog
*wd
)
3388 ifsq_watchdog_reset(wd
);
3392 ifsq_watchdog_stop(struct ifsubq_watchdog
*wd
)
3395 callout_stop(&wd
->wd_callout
);
3401 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3402 ("try holding ifnet lock in netisr"));
3403 mtx_lock(&ifnet_mtx
);
3409 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3410 ("try holding ifnet lock in netisr"));
3411 mtx_unlock(&ifnet_mtx
);
3414 static struct ifnet_array
*
3415 ifnet_array_alloc(int count
)
3417 struct ifnet_array
*arr
;
3419 arr
= kmalloc(__offsetof(struct ifnet_array
, ifnet_arr
[count
]),
3421 arr
->ifnet_count
= count
;
3427 ifnet_array_free(struct ifnet_array
*arr
)
3429 if (arr
== &ifnet_array0
)
3431 kfree(arr
, M_IFNET
);
3434 static struct ifnet_array
*
3435 ifnet_array_add(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3437 struct ifnet_array
*arr
;
3440 KASSERT(old_arr
->ifnet_count
>= 0,
3441 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3442 count
= old_arr
->ifnet_count
+ 1;
3443 arr
= ifnet_array_alloc(count
);
3446 * Save the old ifnet array and append this ifp to the end of
3447 * the new ifnet array.
3449 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3450 KASSERT(old_arr
->ifnet_arr
[i
] != ifp
,
3451 ("%s is already in ifnet array", ifp
->if_xname
));
3452 arr
->ifnet_arr
[i
] = old_arr
->ifnet_arr
[i
];
3454 KASSERT(i
== count
- 1,
3455 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3456 ifp
->if_xname
, count
- 1, i
));
3457 arr
->ifnet_arr
[i
] = ifp
;
3462 static struct ifnet_array
*
3463 ifnet_array_del(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3465 struct ifnet_array
*arr
;
3466 int count
, i
, idx
, found
= 0;
3468 KASSERT(old_arr
->ifnet_count
> 0,
3469 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3470 count
= old_arr
->ifnet_count
- 1;
3471 arr
= ifnet_array_alloc(count
);
3474 * Save the old ifnet array, but skip this ifp.
3477 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3478 if (old_arr
->ifnet_arr
[i
] == ifp
) {
3480 ("dup %s is in ifnet array", ifp
->if_xname
));
3484 KASSERT(idx
< count
,
3485 ("invalid ifnet array index %d, count %d", idx
, count
));
3486 arr
->ifnet_arr
[idx
] = old_arr
->ifnet_arr
[i
];
3489 KASSERT(found
, ("%s is not in ifnet array", ifp
->if_xname
));
3490 KASSERT(idx
== count
,
3491 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3492 ifp
->if_xname
, count
, idx
));
3497 const struct ifnet_array
*
3498 ifnet_array_get(void)
3500 const struct ifnet_array
*ret
;
3502 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3504 /* Make sure 'ret' is really used. */
3510 ifnet_array_isempty(void)
3512 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3513 if (ifnet_array
->ifnet_count
== 0)
3520 ifa_marker_init(struct ifaddr_marker
*mark
, struct ifnet
*ifp
)
3524 memset(mark
, 0, sizeof(*mark
));
3527 mark
->ifac
.ifa
= ifa
;
3529 ifa
->ifa_addr
= &mark
->addr
;
3530 ifa
->ifa_dstaddr
= &mark
->dstaddr
;
3531 ifa
->ifa_netmask
= &mark
->netmask
;