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 thread
*ifnet_threads
[MAXCPU
];
166 static struct mtx ifnet_mtx
= MTX_INITIALIZER("ifnet");
168 static struct ifsubq_stage_head ifsubq_stage_heads
[MAXCPU
];
171 #define IFQ_KTR_STRING "ifq=%p"
172 #define IFQ_KTR_ARGS struct ifaltq *ifq
174 #define KTR_IFQ KTR_ALL
176 KTR_INFO_MASTER(ifq
);
177 KTR_INFO(KTR_IFQ
, ifq
, enqueue
, 0, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
178 KTR_INFO(KTR_IFQ
, ifq
, dequeue
, 1, IFQ_KTR_STRING
, IFQ_KTR_ARGS
);
179 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
181 #define IF_START_KTR_STRING "ifp=%p"
182 #define IF_START_KTR_ARGS struct ifnet *ifp
184 #define KTR_IF_START KTR_ALL
186 KTR_INFO_MASTER(if_start
);
187 KTR_INFO(KTR_IF_START
, if_start
, run
, 0,
188 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
189 KTR_INFO(KTR_IF_START
, if_start
, sched
, 1,
190 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
191 KTR_INFO(KTR_IF_START
, if_start
, avoid
, 2,
192 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
193 KTR_INFO(KTR_IF_START
, if_start
, contend_sched
, 3,
194 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
195 KTR_INFO(KTR_IF_START
, if_start
, chase_sched
, 4,
196 IF_START_KTR_STRING
, IF_START_KTR_ARGS
);
197 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
200 TAILQ_HEAD(, ifg_group
) ifg_head
= TAILQ_HEAD_INITIALIZER(ifg_head
);
203 * Network interface utility routines.
205 * Routines with ifa_ifwith* names take sockaddr *'s as
214 callout_init_mp(&if_slowtimo_timer
);
215 netmsg_init(&if_slowtimo_netmsg
, NULL
, &netisr_adone_rport
,
216 MSGF_PRIORITY
, if_slowtimo_dispatch
);
218 /* XXX is this necessary? */
220 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
221 if (ifp
->if_snd
.altq_maxlen
== 0) {
222 if_printf(ifp
, "XXX: driver didn't set altq_maxlen\n");
223 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
228 /* Start if_slowtimo */
229 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg
.lmsg
);
233 ifsq_ifstart_ipifunc(void *arg
)
235 struct ifaltq_subque
*ifsq
= arg
;
236 struct lwkt_msg
*lmsg
= ifsq_get_ifstart_lmsg(ifsq
, mycpuid
);
239 if (lmsg
->ms_flags
& MSGF_DONE
)
240 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid
), lmsg
);
245 ifsq_stage_remove(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
247 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
248 TAILQ_REMOVE(&head
->stg_head
, stage
, stg_link
);
249 stage
->stg_flags
&= ~(IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
);
255 ifsq_stage_insert(struct ifsubq_stage_head
*head
, struct ifsubq_stage
*stage
)
257 KKASSERT((stage
->stg_flags
&
258 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
259 stage
->stg_flags
|= IFSQ_STAGE_FLAG_QUED
;
260 TAILQ_INSERT_TAIL(&head
->stg_head
, stage
, stg_link
);
264 * Schedule ifnet.if_start on the subqueue owner CPU
267 ifsq_ifstart_schedule(struct ifaltq_subque
*ifsq
, int force
)
271 if (!force
&& curthread
->td_type
== TD_TYPE_NETISR
&&
272 ifsq_stage_cntmax
> 0) {
273 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
277 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
278 ifsq_stage_insert(&ifsubq_stage_heads
[mycpuid
], stage
);
279 stage
->stg_flags
|= IFSQ_STAGE_FLAG_SCHED
;
283 cpu
= ifsq_get_cpuid(ifsq
);
285 lwkt_send_ipiq(globaldata_find(cpu
), ifsq_ifstart_ipifunc
, ifsq
);
287 ifsq_ifstart_ipifunc(ifsq
);
292 * This function will release ifnet.if_start subqueue interlock,
293 * if ifnet.if_start for the subqueue does not need to be scheduled
296 ifsq_ifstart_need_schedule(struct ifaltq_subque
*ifsq
, int running
)
298 if (!running
|| ifsq_is_empty(ifsq
)
300 || ifsq
->ifsq_altq
->altq_tbr
!= NULL
305 * ifnet.if_start subqueue interlock is released, if:
306 * 1) Hardware can not take any packets, due to
307 * o interface is marked down
308 * o hardware queue is full (ifsq_is_oactive)
309 * Under the second situation, hardware interrupt
310 * or polling(4) will call/schedule ifnet.if_start
311 * on the subqueue when hardware queue is ready
312 * 2) There is no packet in the subqueue.
313 * Further ifq_dispatch or ifq_handoff will call/
314 * schedule ifnet.if_start on the subqueue.
315 * 3) TBR is used and it does not allow further
317 * TBR callout will call ifnet.if_start on the
320 if (!running
|| !ifsq_data_ready(ifsq
)) {
321 ifsq_clr_started(ifsq
);
322 ALTQ_SQ_UNLOCK(ifsq
);
325 ALTQ_SQ_UNLOCK(ifsq
);
331 ifsq_ifstart_dispatch(netmsg_t msg
)
333 struct lwkt_msg
*lmsg
= &msg
->base
.lmsg
;
334 struct ifaltq_subque
*ifsq
= lmsg
->u
.ms_resultp
;
335 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
336 struct globaldata
*gd
= mycpu
;
337 int running
= 0, need_sched
;
341 lwkt_replymsg(lmsg
, 0); /* reply ASAP */
343 if (gd
->gd_cpuid
!= ifsq_get_cpuid(ifsq
)) {
345 * We need to chase the subqueue owner CPU change.
347 ifsq_ifstart_schedule(ifsq
, 1);
352 ifsq_serialize_hw(ifsq
);
353 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
354 ifp
->if_start(ifp
, ifsq
);
355 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
358 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
359 ifsq_deserialize_hw(ifsq
);
363 * More data need to be transmitted, ifnet.if_start is
364 * scheduled on the subqueue owner CPU, and we keep going.
365 * NOTE: ifnet.if_start subqueue interlock is not released.
367 ifsq_ifstart_schedule(ifsq
, 0);
373 /* Device driver ifnet.if_start helper function */
375 ifsq_devstart(struct ifaltq_subque
*ifsq
)
377 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
380 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq
);
383 if (ifsq_is_started(ifsq
) || !ifsq_data_ready(ifsq
)) {
384 ALTQ_SQ_UNLOCK(ifsq
);
387 ifsq_set_started(ifsq
);
388 ALTQ_SQ_UNLOCK(ifsq
);
390 ifp
->if_start(ifp
, ifsq
);
392 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
395 if (ifsq_ifstart_need_schedule(ifsq
, running
)) {
397 * More data need to be transmitted, ifnet.if_start is
398 * scheduled on ifnet's CPU, and we keep going.
399 * NOTE: ifnet.if_start interlock is not released.
401 ifsq_ifstart_schedule(ifsq
, 0);
406 if_devstart(struct ifnet
*ifp
)
408 ifsq_devstart(ifq_get_subq_default(&ifp
->if_snd
));
411 /* Device driver ifnet.if_start schedule helper function */
413 ifsq_devstart_sched(struct ifaltq_subque
*ifsq
)
415 ifsq_ifstart_schedule(ifsq
, 1);
419 if_devstart_sched(struct ifnet
*ifp
)
421 ifsq_devstart_sched(ifq_get_subq_default(&ifp
->if_snd
));
425 if_default_serialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
427 lwkt_serialize_enter(ifp
->if_serializer
);
431 if_default_deserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
433 lwkt_serialize_exit(ifp
->if_serializer
);
437 if_default_tryserialize(struct ifnet
*ifp
, enum ifnet_serialize slz __unused
)
439 return lwkt_serialize_try(ifp
->if_serializer
);
444 if_default_serialize_assert(struct ifnet
*ifp
,
445 enum ifnet_serialize slz __unused
,
446 boolean_t serialized
)
449 ASSERT_SERIALIZED(ifp
->if_serializer
);
451 ASSERT_NOT_SERIALIZED(ifp
->if_serializer
);
456 * Attach an interface to the list of "active" interfaces.
458 * The serializer is optional.
461 if_attach(struct ifnet
*ifp
, lwkt_serialize_t serializer
)
464 int namelen
, masklen
;
465 struct sockaddr_dl
*sdl
, *sdl_addr
;
468 struct ifnet
**old_ifindex2ifnet
= NULL
;
469 struct ifnet_array
*old_ifnet_array
;
472 static int if_indexlim
= 8;
474 if (ifp
->if_serialize
!= NULL
) {
475 KASSERT(ifp
->if_deserialize
!= NULL
&&
476 ifp
->if_tryserialize
!= NULL
&&
477 ifp
->if_serialize_assert
!= NULL
,
478 ("serialize functions are partially setup"));
481 * If the device supplies serialize functions,
482 * then clear if_serializer to catch any invalid
483 * usage of this field.
485 KASSERT(serializer
== NULL
,
486 ("both serialize functions and default serializer "
488 ifp
->if_serializer
= NULL
;
490 KASSERT(ifp
->if_deserialize
== NULL
&&
491 ifp
->if_tryserialize
== NULL
&&
492 ifp
->if_serialize_assert
== NULL
,
493 ("serialize functions are partially setup"));
494 ifp
->if_serialize
= if_default_serialize
;
495 ifp
->if_deserialize
= if_default_deserialize
;
496 ifp
->if_tryserialize
= if_default_tryserialize
;
498 ifp
->if_serialize_assert
= if_default_serialize_assert
;
502 * The serializer can be passed in from the device,
503 * allowing the same serializer to be used for both
504 * the interrupt interlock and the device queue.
505 * If not specified, the netif structure will use an
506 * embedded serializer.
508 if (serializer
== NULL
) {
509 serializer
= &ifp
->if_default_serializer
;
510 lwkt_serialize_init(serializer
);
512 ifp
->if_serializer
= serializer
;
517 * The old code would work if the interface passed a pre-existing
518 * chain of ifaddrs to this code. We don't trust our callers to
519 * properly initialize the tailq, however, so we no longer allow
520 * this unlikely case.
522 ifp
->if_addrheads
= kmalloc(ncpus
* sizeof(struct ifaddrhead
),
523 M_IFADDR
, M_WAITOK
| M_ZERO
);
524 for (i
= 0; i
< ncpus
; ++i
)
525 TAILQ_INIT(&ifp
->if_addrheads
[i
]);
527 TAILQ_INIT(&ifp
->if_multiaddrs
);
528 TAILQ_INIT(&ifp
->if_groups
);
529 getmicrotime(&ifp
->if_lastchange
);
532 * create a Link Level name for this device
534 namelen
= strlen(ifp
->if_xname
);
535 masklen
= offsetof(struct sockaddr_dl
, sdl_data
[0]) + namelen
;
536 socksize
= masklen
+ ifp
->if_addrlen
;
537 if (socksize
< sizeof(*sdl
))
538 socksize
= sizeof(*sdl
);
539 socksize
= RT_ROUNDUP(socksize
);
540 ifa
= ifa_create(sizeof(struct ifaddr
) + 2 * socksize
);
541 sdl
= sdl_addr
= (struct sockaddr_dl
*)(ifa
+ 1);
542 sdl
->sdl_len
= socksize
;
543 sdl
->sdl_family
= AF_LINK
;
544 bcopy(ifp
->if_xname
, sdl
->sdl_data
, namelen
);
545 sdl
->sdl_nlen
= namelen
;
546 sdl
->sdl_type
= ifp
->if_type
;
547 ifp
->if_lladdr
= ifa
;
549 ifa
->ifa_rtrequest
= link_rtrequest
;
550 ifa
->ifa_addr
= (struct sockaddr
*)sdl
;
551 sdl
= (struct sockaddr_dl
*)(socksize
+ (caddr_t
)sdl
);
552 ifa
->ifa_netmask
= (struct sockaddr
*)sdl
;
553 sdl
->sdl_len
= masklen
;
555 sdl
->sdl_data
[--namelen
] = 0xff;
556 ifa_iflink(ifa
, ifp
, 0 /* Insert head */);
558 ifp
->if_data_pcpu
= kmalloc_cachealign(
559 ncpus
* sizeof(struct ifdata_pcpu
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
561 if (ifp
->if_mapsubq
== NULL
)
562 ifp
->if_mapsubq
= ifq_mapsubq_default
;
566 ifq
->altq_disc
= NULL
;
567 ifq
->altq_flags
&= ALTQF_CANTCHANGE
;
568 ifq
->altq_tbr
= NULL
;
571 if (ifq
->altq_subq_cnt
<= 0)
572 ifq
->altq_subq_cnt
= 1;
573 ifq
->altq_subq
= kmalloc_cachealign(
574 ifq
->altq_subq_cnt
* sizeof(struct ifaltq_subque
),
575 M_DEVBUF
, M_WAITOK
| M_ZERO
);
577 if (ifq
->altq_maxlen
== 0) {
578 if_printf(ifp
, "driver didn't set altq_maxlen\n");
579 ifq_set_maxlen(ifq
, ifqmaxlen
);
582 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
583 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
585 ALTQ_SQ_LOCK_INIT(ifsq
);
586 ifsq
->ifsq_index
= q
;
588 ifsq
->ifsq_altq
= ifq
;
589 ifsq
->ifsq_ifp
= ifp
;
591 ifsq
->ifsq_maxlen
= ifq
->altq_maxlen
;
592 ifsq
->ifsq_maxbcnt
= ifsq
->ifsq_maxlen
* MCLBYTES
;
593 ifsq
->ifsq_prepended
= NULL
;
594 ifsq
->ifsq_started
= 0;
595 ifsq
->ifsq_hw_oactive
= 0;
596 ifsq_set_cpuid(ifsq
, 0);
597 if (ifp
->if_serializer
!= NULL
)
598 ifsq_set_hw_serialize(ifsq
, ifp
->if_serializer
);
601 kmalloc_cachealign(ncpus
* sizeof(struct ifsubq_stage
),
602 M_DEVBUF
, M_WAITOK
| M_ZERO
);
603 for (i
= 0; i
< ncpus
; ++i
)
604 ifsq
->ifsq_stage
[i
].stg_subq
= ifsq
;
606 ifsq
->ifsq_ifstart_nmsg
=
607 kmalloc(ncpus
* sizeof(struct netmsg_base
),
608 M_LWKTMSG
, M_WAITOK
);
609 for (i
= 0; i
< ncpus
; ++i
) {
610 netmsg_init(&ifsq
->ifsq_ifstart_nmsg
[i
], NULL
,
611 &netisr_adone_rport
, 0, ifsq_ifstart_dispatch
);
612 ifsq
->ifsq_ifstart_nmsg
[i
].lmsg
.u
.ms_resultp
= ifsq
;
615 ifq_set_classic(ifq
);
618 * Increase mbuf cluster/jcluster limits for the mbufs that
619 * could sit on the device queues for quite some time.
621 if (ifp
->if_nmbclusters
> 0)
622 mcl_inclimit(ifp
->if_nmbclusters
);
623 if (ifp
->if_nmbjclusters
> 0)
624 mjcl_inclimit(ifp
->if_nmbjclusters
);
627 * Install this ifp into ifindex2inet, ifnet queue and ifnet
628 * array after it is setup.
630 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
631 * by ifnet lock, so that non-netisr threads could get a
636 /* Don't update if_index until ifindex2ifnet is setup */
637 ifp
->if_index
= if_index
+ 1;
638 sdl_addr
->sdl_index
= ifp
->if_index
;
641 * Install this ifp into ifindex2ifnet
643 if (ifindex2ifnet
== NULL
|| ifp
->if_index
>= if_indexlim
) {
651 n
= if_indexlim
* sizeof(*q
);
652 q
= kmalloc(n
, M_IFADDR
, M_WAITOK
| M_ZERO
);
653 if (ifindex2ifnet
!= NULL
) {
654 bcopy(ifindex2ifnet
, q
, n
/2);
655 /* Free old ifindex2ifnet after sync all netisrs */
656 old_ifindex2ifnet
= ifindex2ifnet
;
660 ifindex2ifnet
[ifp
->if_index
] = ifp
;
662 * Update if_index after this ifp is installed into ifindex2ifnet,
663 * so that netisrs could get a consistent view of ifindex2ifnet.
666 if_index
= ifp
->if_index
;
669 * Install this ifp into ifnet array.
671 /* Free old ifnet array after sync all netisrs */
672 old_ifnet_array
= ifnet_array
;
673 ifnet_array
= ifnet_array_add(ifp
, old_ifnet_array
);
676 * Install this ifp into ifnet queue.
678 TAILQ_INSERT_TAIL(&ifnetlist
, ifp
, if_link
);
683 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
684 * are no longer accessed and we can free them safely later on.
686 netmsg_service_sync();
687 if (old_ifindex2ifnet
!= NULL
)
688 kfree(old_ifindex2ifnet
, M_IFADDR
);
689 ifnet_array_free(old_ifnet_array
);
691 if (!SLIST_EMPTY(&domains
))
692 if_attachdomain1(ifp
);
694 /* Announce the interface. */
695 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
696 devctl_notify("IFNET", ifp
->if_xname
, "ATTACH", NULL
);
697 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
701 if_attachdomain(void *dummy
)
706 TAILQ_FOREACH(ifp
, &ifnetlist
, if_list
)
707 if_attachdomain1(ifp
);
710 SYSINIT(domainifattach
, SI_SUB_PROTO_IFATTACHDOMAIN
, SI_ORDER_FIRST
,
711 if_attachdomain
, NULL
);
714 if_attachdomain1(struct ifnet
*ifp
)
720 /* address family dependent data region */
721 bzero(ifp
->if_afdata
, sizeof(ifp
->if_afdata
));
722 SLIST_FOREACH(dp
, &domains
, dom_next
)
723 if (dp
->dom_ifattach
)
724 ifp
->if_afdata
[dp
->dom_family
] =
725 (*dp
->dom_ifattach
)(ifp
);
730 * Purge all addresses whose type is _not_ AF_LINK
733 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg
)
735 struct lwkt_msg
*lmsg
= &nmsg
->lmsg
;
736 struct ifnet
*ifp
= lmsg
->u
.ms_resultp
;
737 struct ifaddr_container
*ifac
, *next
;
742 * The ifaddr processing in the following loop will block,
743 * however, this function is called in netisr0, in which
744 * ifaddr list changes happen, so we don't care about the
745 * blockness of the ifaddr processing here.
747 TAILQ_FOREACH_MUTABLE(ifac
, &ifp
->if_addrheads
[mycpuid
],
749 struct ifaddr
*ifa
= ifac
->ifa
;
752 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
755 /* Leave link ifaddr as it is */
756 if (ifa
->ifa_addr
->sa_family
== AF_LINK
)
759 /* XXX: Ugly!! ad hoc just for INET */
760 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET
) {
761 struct ifaliasreq ifr
;
762 #ifdef IFADDR_DEBUG_VERBOSE
765 kprintf("purge in4 addr %p: ", ifa
);
766 for (i
= 0; i
< ncpus
; ++i
)
767 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
771 bzero(&ifr
, sizeof ifr
);
772 ifr
.ifra_addr
= *ifa
->ifa_addr
;
773 if (ifa
->ifa_dstaddr
)
774 ifr
.ifra_broadaddr
= *ifa
->ifa_dstaddr
;
775 if (in_control(SIOCDIFADDR
, (caddr_t
)&ifr
, ifp
,
781 if (ifa
->ifa_addr
&& ifa
->ifa_addr
->sa_family
== AF_INET6
) {
782 #ifdef IFADDR_DEBUG_VERBOSE
785 kprintf("purge in6 addr %p: ", ifa
);
786 for (i
= 0; i
< ncpus
; ++i
)
787 kprintf("%d ", ifa
->ifa_containers
[i
].ifa_refcnt
);
792 /* ifp_addrhead is already updated */
796 ifa_ifunlink(ifa
, ifp
);
800 lwkt_replymsg(lmsg
, 0);
804 if_purgeaddrs_nolink(struct ifnet
*ifp
)
806 struct netmsg_base nmsg
;
807 struct lwkt_msg
*lmsg
= &nmsg
.lmsg
;
809 ASSERT_CANDOMSG_NETISR0(curthread
);
811 netmsg_init(&nmsg
, NULL
, &curthread
->td_msgport
, 0,
812 if_purgeaddrs_nolink_dispatch
);
813 lmsg
->u
.ms_resultp
= ifp
;
814 lwkt_domsg(netisr_cpuport(0), lmsg
, 0);
818 ifq_stage_detach_handler(netmsg_t nmsg
)
820 struct ifaltq
*ifq
= nmsg
->lmsg
.u
.ms_resultp
;
823 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
824 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
825 struct ifsubq_stage
*stage
= ifsq_get_stage(ifsq
, mycpuid
);
827 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
)
828 ifsq_stage_remove(&ifsubq_stage_heads
[mycpuid
], stage
);
830 lwkt_replymsg(&nmsg
->lmsg
, 0);
834 ifq_stage_detach(struct ifaltq
*ifq
)
836 struct netmsg_base base
;
839 netmsg_init(&base
, NULL
, &curthread
->td_msgport
, 0,
840 ifq_stage_detach_handler
);
841 base
.lmsg
.u
.ms_resultp
= ifq
;
843 for (cpu
= 0; cpu
< ncpus
; ++cpu
)
844 lwkt_domsg(netisr_cpuport(cpu
), &base
.lmsg
, 0);
847 struct netmsg_if_rtdel
{
848 struct netmsg_base base
;
853 if_rtdel_dispatch(netmsg_t msg
)
855 struct netmsg_if_rtdel
*rmsg
= (void *)msg
;
859 for (i
= 1; i
<= AF_MAX
; i
++) {
860 struct radix_node_head
*rnh
;
862 if ((rnh
= rt_tables
[cpu
][i
]) == NULL
)
864 rnh
->rnh_walktree(rnh
, if_rtdel
, rmsg
->ifp
);
869 lwkt_forwardmsg(netisr_cpuport(nextcpu
), &rmsg
->base
.lmsg
);
871 lwkt_replymsg(&rmsg
->base
.lmsg
, 0);
875 * Detach an interface, removing it from the
876 * list of "active" interfaces.
879 if_detach(struct ifnet
*ifp
)
881 struct ifnet_array
*old_ifnet_array
;
882 struct netmsg_if_rtdel msg
;
886 /* Announce that the interface is gone. */
887 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
888 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
889 devctl_notify("IFNET", ifp
->if_xname
, "DETACH", NULL
);
892 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
893 * array before it is whacked.
895 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
896 * by ifnet lock, so that non-netisr threads could get a
902 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
904 ifindex2ifnet
[ifp
->if_index
] = NULL
;
905 while (if_index
> 0 && ifindex2ifnet
[if_index
] == NULL
)
909 * Remove this ifp from ifnet queue.
911 TAILQ_REMOVE(&ifnetlist
, ifp
, if_link
);
914 * Remove this ifp from ifnet array.
916 /* Free old ifnet array after sync all netisrs */
917 old_ifnet_array
= ifnet_array
;
918 ifnet_array
= ifnet_array_del(ifp
, old_ifnet_array
);
923 * Sync all netisrs so that the old ifnet array is no longer
924 * accessed and we can free it safely later on.
926 netmsg_service_sync();
927 ifnet_array_free(old_ifnet_array
);
930 * Remove routes and flush queues.
934 if (ifp
->if_flags
& IFF_NPOLLING
)
935 ifpoll_deregister(ifp
);
939 /* Decrease the mbuf clusters/jclusters limits increased by us */
940 if (ifp
->if_nmbclusters
> 0)
941 mcl_inclimit(-ifp
->if_nmbclusters
);
942 if (ifp
->if_nmbjclusters
> 0)
943 mjcl_inclimit(-ifp
->if_nmbjclusters
);
946 if (ifq_is_enabled(&ifp
->if_snd
))
947 altq_disable(&ifp
->if_snd
);
948 if (ifq_is_attached(&ifp
->if_snd
))
949 altq_detach(&ifp
->if_snd
);
953 * Clean up all addresses.
955 ifp
->if_lladdr
= NULL
;
957 if_purgeaddrs_nolink(ifp
);
958 if (!TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
])) {
961 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
962 KASSERT(ifa
->ifa_addr
->sa_family
== AF_LINK
,
963 ("non-link ifaddr is left on if_addrheads"));
965 ifa_ifunlink(ifa
, ifp
);
967 KASSERT(TAILQ_EMPTY(&ifp
->if_addrheads
[mycpuid
]),
968 ("there are still ifaddrs left on if_addrheads"));
973 * Remove all IPv4 kernel structures related to ifp.
980 * Remove all IPv6 kernel structs related to ifp. This should be done
981 * before removing routing entries below, since IPv6 interface direct
982 * routes are expected to be removed by the IPv6-specific kernel API.
983 * Otherwise, the kernel will detect some inconsistency and bark it.
989 * Delete all remaining routes using this interface
991 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, MSGF_PRIORITY
,
994 rt_domsg_global(&msg
.base
);
996 SLIST_FOREACH(dp
, &domains
, dom_next
)
997 if (dp
->dom_ifdetach
&& ifp
->if_afdata
[dp
->dom_family
])
998 (*dp
->dom_ifdetach
)(ifp
,
999 ifp
->if_afdata
[dp
->dom_family
]);
1001 kfree(ifp
->if_addrheads
, M_IFADDR
);
1003 lwkt_synchronize_ipiqs("if_detach");
1004 ifq_stage_detach(&ifp
->if_snd
);
1006 for (q
= 0; q
< ifp
->if_snd
.altq_subq_cnt
; ++q
) {
1007 struct ifaltq_subque
*ifsq
= &ifp
->if_snd
.altq_subq
[q
];
1009 kfree(ifsq
->ifsq_ifstart_nmsg
, M_LWKTMSG
);
1010 kfree(ifsq
->ifsq_stage
, M_DEVBUF
);
1012 kfree(ifp
->if_snd
.altq_subq
, M_DEVBUF
);
1014 kfree(ifp
->if_data_pcpu
, M_DEVBUF
);
1020 * Create interface group without members
1023 if_creategroup(const char *groupname
)
1025 struct ifg_group
*ifg
= NULL
;
1027 if ((ifg
= (struct ifg_group
*)kmalloc(sizeof(struct ifg_group
),
1028 M_TEMP
, M_NOWAIT
)) == NULL
)
1031 strlcpy(ifg
->ifg_group
, groupname
, sizeof(ifg
->ifg_group
));
1032 ifg
->ifg_refcnt
= 0;
1033 ifg
->ifg_carp_demoted
= 0;
1034 TAILQ_INIT(&ifg
->ifg_members
);
1036 pfi_attach_ifgroup(ifg
);
1038 TAILQ_INSERT_TAIL(&ifg_head
, ifg
, ifg_next
);
1044 * Add a group to an interface
1047 if_addgroup(struct ifnet
*ifp
, const char *groupname
)
1049 struct ifg_list
*ifgl
;
1050 struct ifg_group
*ifg
= NULL
;
1051 struct ifg_member
*ifgm
;
1053 if (groupname
[0] && groupname
[strlen(groupname
) - 1] >= '0' &&
1054 groupname
[strlen(groupname
) - 1] <= '9')
1057 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1058 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1061 if ((ifgl
= kmalloc(sizeof(*ifgl
), M_TEMP
, M_NOWAIT
)) == NULL
)
1064 if ((ifgm
= kmalloc(sizeof(*ifgm
), M_TEMP
, M_NOWAIT
)) == NULL
) {
1065 kfree(ifgl
, M_TEMP
);
1069 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1070 if (!strcmp(ifg
->ifg_group
, groupname
))
1073 if (ifg
== NULL
&& (ifg
= if_creategroup(groupname
)) == NULL
) {
1074 kfree(ifgl
, M_TEMP
);
1075 kfree(ifgm
, M_TEMP
);
1080 ifgl
->ifgl_group
= ifg
;
1081 ifgm
->ifgm_ifp
= ifp
;
1083 TAILQ_INSERT_TAIL(&ifg
->ifg_members
, ifgm
, ifgm_next
);
1084 TAILQ_INSERT_TAIL(&ifp
->if_groups
, ifgl
, ifgl_next
);
1087 pfi_group_change(groupname
);
1094 * Remove a group from an interface
1097 if_delgroup(struct ifnet
*ifp
, const char *groupname
)
1099 struct ifg_list
*ifgl
;
1100 struct ifg_member
*ifgm
;
1102 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1103 if (!strcmp(ifgl
->ifgl_group
->ifg_group
, groupname
))
1108 TAILQ_REMOVE(&ifp
->if_groups
, ifgl
, ifgl_next
);
1110 TAILQ_FOREACH(ifgm
, &ifgl
->ifgl_group
->ifg_members
, ifgm_next
)
1111 if (ifgm
->ifgm_ifp
== ifp
)
1115 TAILQ_REMOVE(&ifgl
->ifgl_group
->ifg_members
, ifgm
, ifgm_next
);
1116 kfree(ifgm
, M_TEMP
);
1119 if (--ifgl
->ifgl_group
->ifg_refcnt
== 0) {
1120 TAILQ_REMOVE(&ifg_head
, ifgl
->ifgl_group
, ifg_next
);
1122 pfi_detach_ifgroup(ifgl
->ifgl_group
);
1124 kfree(ifgl
->ifgl_group
, M_TEMP
);
1127 kfree(ifgl
, M_TEMP
);
1130 pfi_group_change(groupname
);
1137 * Stores all groups from an interface in memory pointed
1141 if_getgroup(caddr_t data
, struct ifnet
*ifp
)
1144 struct ifg_list
*ifgl
;
1145 struct ifg_req ifgrq
, *ifgp
;
1146 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1148 if (ifgr
->ifgr_len
== 0) {
1149 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
)
1150 ifgr
->ifgr_len
+= sizeof(struct ifg_req
);
1154 len
= ifgr
->ifgr_len
;
1155 ifgp
= ifgr
->ifgr_groups
;
1156 TAILQ_FOREACH(ifgl
, &ifp
->if_groups
, ifgl_next
) {
1157 if (len
< sizeof(ifgrq
))
1159 bzero(&ifgrq
, sizeof ifgrq
);
1160 strlcpy(ifgrq
.ifgrq_group
, ifgl
->ifgl_group
->ifg_group
,
1161 sizeof(ifgrq
.ifgrq_group
));
1162 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1163 sizeof(struct ifg_req
))))
1165 len
-= sizeof(ifgrq
);
1173 * Stores all members of a group in memory pointed to by data
1176 if_getgroupmembers(caddr_t data
)
1178 struct ifgroupreq
*ifgr
= (struct ifgroupreq
*)data
;
1179 struct ifg_group
*ifg
;
1180 struct ifg_member
*ifgm
;
1181 struct ifg_req ifgrq
, *ifgp
;
1184 TAILQ_FOREACH(ifg
, &ifg_head
, ifg_next
)
1185 if (!strcmp(ifg
->ifg_group
, ifgr
->ifgr_name
))
1190 if (ifgr
->ifgr_len
== 0) {
1191 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
)
1192 ifgr
->ifgr_len
+= sizeof(ifgrq
);
1196 len
= ifgr
->ifgr_len
;
1197 ifgp
= ifgr
->ifgr_groups
;
1198 TAILQ_FOREACH(ifgm
, &ifg
->ifg_members
, ifgm_next
) {
1199 if (len
< sizeof(ifgrq
))
1201 bzero(&ifgrq
, sizeof ifgrq
);
1202 strlcpy(ifgrq
.ifgrq_member
, ifgm
->ifgm_ifp
->if_xname
,
1203 sizeof(ifgrq
.ifgrq_member
));
1204 if ((error
= copyout((caddr_t
)&ifgrq
, (caddr_t
)ifgp
,
1205 sizeof(struct ifg_req
))))
1207 len
-= sizeof(ifgrq
);
1215 * Delete Routes for a Network Interface
1217 * Called for each routing entry via the rnh->rnh_walktree() call above
1218 * to delete all route entries referencing a detaching network interface.
1221 * rn pointer to node in the routing table
1222 * arg argument passed to rnh->rnh_walktree() - detaching interface
1226 * errno failed - reason indicated
1230 if_rtdel(struct radix_node
*rn
, void *arg
)
1232 struct rtentry
*rt
= (struct rtentry
*)rn
;
1233 struct ifnet
*ifp
= arg
;
1236 if (rt
->rt_ifp
== ifp
) {
1239 * Protect (sorta) against walktree recursion problems
1240 * with cloned routes
1242 if (!(rt
->rt_flags
& RTF_UP
))
1245 err
= rtrequest(RTM_DELETE
, rt_key(rt
), rt
->rt_gateway
,
1246 rt_mask(rt
), rt
->rt_flags
,
1249 log(LOG_WARNING
, "if_rtdel: error %d\n", err
);
1256 static __inline boolean_t
1257 ifa_prefer(const struct ifaddr
*cur_ifa
, const struct ifaddr
*old_ifa
)
1259 if (old_ifa
== NULL
)
1262 if ((old_ifa
->ifa_ifp
->if_flags
& IFF_UP
) == 0 &&
1263 (cur_ifa
->ifa_ifp
->if_flags
& IFF_UP
))
1265 if ((old_ifa
->ifa_flags
& IFA_ROUTE
) == 0 &&
1266 (cur_ifa
->ifa_flags
& IFA_ROUTE
))
1272 * Locate an interface based on a complete address.
1275 ifa_ifwithaddr(struct sockaddr
*addr
)
1277 const struct ifnet_array
*arr
;
1280 arr
= ifnet_array_get();
1281 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1282 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1283 struct ifaddr_container
*ifac
;
1285 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1286 struct ifaddr
*ifa
= ifac
->ifa
;
1288 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1290 if (sa_equal(addr
, ifa
->ifa_addr
))
1292 if ((ifp
->if_flags
& IFF_BROADCAST
) &&
1293 ifa
->ifa_broadaddr
&&
1294 /* IPv6 doesn't have broadcast */
1295 ifa
->ifa_broadaddr
->sa_len
!= 0 &&
1296 sa_equal(ifa
->ifa_broadaddr
, addr
))
1304 * Locate the point to point interface with a given destination address.
1307 ifa_ifwithdstaddr(struct sockaddr
*addr
)
1309 const struct ifnet_array
*arr
;
1312 arr
= ifnet_array_get();
1313 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1314 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1315 struct ifaddr_container
*ifac
;
1317 if (!(ifp
->if_flags
& IFF_POINTOPOINT
))
1320 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1321 struct ifaddr
*ifa
= ifac
->ifa
;
1323 if (ifa
->ifa_addr
->sa_family
!= addr
->sa_family
)
1325 if (ifa
->ifa_dstaddr
&&
1326 sa_equal(addr
, ifa
->ifa_dstaddr
))
1334 * Find an interface on a specific network. If many, choice
1335 * is most specific found.
1338 ifa_ifwithnet(struct sockaddr
*addr
)
1340 struct ifaddr
*ifa_maybe
= NULL
;
1341 u_int af
= addr
->sa_family
;
1342 char *addr_data
= addr
->sa_data
, *cplim
;
1343 const struct ifnet_array
*arr
;
1347 * AF_LINK addresses can be looked up directly by their index number,
1348 * so do that if we can.
1350 if (af
== AF_LINK
) {
1351 struct sockaddr_dl
*sdl
= (struct sockaddr_dl
*)addr
;
1353 if (sdl
->sdl_index
&& sdl
->sdl_index
<= if_index
)
1354 return (ifindex2ifnet
[sdl
->sdl_index
]->if_lladdr
);
1358 * Scan though each interface, looking for ones that have
1359 * addresses in this address family.
1361 arr
= ifnet_array_get();
1362 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1363 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1364 struct ifaddr_container
*ifac
;
1366 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1367 struct ifaddr
*ifa
= ifac
->ifa
;
1368 char *cp
, *cp2
, *cp3
;
1370 if (ifa
->ifa_addr
->sa_family
!= af
)
1372 if (af
== AF_INET
&& ifp
->if_flags
& IFF_POINTOPOINT
) {
1374 * This is a bit broken as it doesn't
1375 * take into account that the remote end may
1376 * be a single node in the network we are
1378 * The trouble is that we don't know the
1379 * netmask for the remote end.
1381 if (ifa
->ifa_dstaddr
!= NULL
&&
1382 sa_equal(addr
, ifa
->ifa_dstaddr
))
1386 * if we have a special address handler,
1387 * then use it instead of the generic one.
1389 if (ifa
->ifa_claim_addr
) {
1390 if ((*ifa
->ifa_claim_addr
)(ifa
, addr
)) {
1398 * Scan all the bits in the ifa's address.
1399 * If a bit dissagrees with what we are
1400 * looking for, mask it with the netmask
1401 * to see if it really matters.
1402 * (A byte at a time)
1404 if (ifa
->ifa_netmask
== 0)
1407 cp2
= ifa
->ifa_addr
->sa_data
;
1408 cp3
= ifa
->ifa_netmask
->sa_data
;
1409 cplim
= ifa
->ifa_netmask
->sa_len
+
1410 (char *)ifa
->ifa_netmask
;
1412 if ((*cp
++ ^ *cp2
++) & *cp3
++)
1413 goto next
; /* next address! */
1415 * If the netmask of what we just found
1416 * is more specific than what we had before
1417 * (if we had one) then remember the new one
1418 * before continuing to search for an even
1419 * better one. If the netmasks are equal,
1420 * we prefer the this ifa based on the result
1423 if (ifa_maybe
== NULL
||
1424 rn_refines((char *)ifa
->ifa_netmask
,
1425 (char *)ifa_maybe
->ifa_netmask
) ||
1426 (sa_equal(ifa_maybe
->ifa_netmask
,
1427 ifa
->ifa_netmask
) &&
1428 ifa_prefer(ifa
, ifa_maybe
)))
1437 * Find an interface address specific to an interface best matching
1441 ifaof_ifpforaddr(struct sockaddr
*addr
, struct ifnet
*ifp
)
1443 struct ifaddr_container
*ifac
;
1444 char *cp
, *cp2
, *cp3
;
1446 struct ifaddr
*ifa_maybe
= NULL
;
1447 u_int af
= addr
->sa_family
;
1451 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1452 struct ifaddr
*ifa
= ifac
->ifa
;
1454 if (ifa
->ifa_addr
->sa_family
!= af
)
1456 if (ifa_maybe
== NULL
)
1458 if (ifa
->ifa_netmask
== NULL
) {
1459 if (sa_equal(addr
, ifa
->ifa_addr
) ||
1460 (ifa
->ifa_dstaddr
!= NULL
&&
1461 sa_equal(addr
, ifa
->ifa_dstaddr
)))
1465 if (ifp
->if_flags
& IFF_POINTOPOINT
) {
1466 if (sa_equal(addr
, ifa
->ifa_dstaddr
))
1470 cp2
= ifa
->ifa_addr
->sa_data
;
1471 cp3
= ifa
->ifa_netmask
->sa_data
;
1472 cplim
= ifa
->ifa_netmask
->sa_len
+ (char *)ifa
->ifa_netmask
;
1473 for (; cp3
< cplim
; cp3
++)
1474 if ((*cp
++ ^ *cp2
++) & *cp3
)
1484 * Default action when installing a route with a Link Level gateway.
1485 * Lookup an appropriate real ifa to point to.
1486 * This should be moved to /sys/net/link.c eventually.
1489 link_rtrequest(int cmd
, struct rtentry
*rt
)
1492 struct sockaddr
*dst
;
1495 if (cmd
!= RTM_ADD
|| (ifa
= rt
->rt_ifa
) == NULL
||
1496 (ifp
= ifa
->ifa_ifp
) == NULL
|| (dst
= rt_key(rt
)) == NULL
)
1498 ifa
= ifaof_ifpforaddr(dst
, ifp
);
1500 IFAFREE(rt
->rt_ifa
);
1503 if (ifa
->ifa_rtrequest
&& ifa
->ifa_rtrequest
!= link_rtrequest
)
1504 ifa
->ifa_rtrequest(cmd
, rt
);
1508 struct netmsg_ifroute
{
1509 struct netmsg_base base
;
1516 * Mark an interface down and notify protocols of the transition.
1519 if_unroute_dispatch(netmsg_t nmsg
)
1521 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1522 struct ifnet
*ifp
= msg
->ifp
;
1523 int flag
= msg
->flag
, fam
= msg
->fam
;
1524 struct ifaddr_container
*ifac
;
1526 ifp
->if_flags
&= ~flag
;
1527 getmicrotime(&ifp
->if_lastchange
);
1529 * The ifaddr processing in the following loop will block,
1530 * however, this function is called in netisr0, in which
1531 * ifaddr list changes happen, so we don't care about the
1532 * blockness of the ifaddr processing here.
1534 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1535 struct ifaddr
*ifa
= ifac
->ifa
;
1538 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1541 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1542 kpfctlinput(PRC_IFDOWN
, ifa
->ifa_addr
);
1544 ifq_purge_all(&ifp
->if_snd
);
1547 lwkt_replymsg(&nmsg
->lmsg
, 0);
1551 if_unroute(struct ifnet
*ifp
, int flag
, int fam
)
1553 struct netmsg_ifroute msg
;
1555 ASSERT_CANDOMSG_NETISR0(curthread
);
1557 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1558 if_unroute_dispatch
);
1562 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1566 * Mark an interface up and notify protocols of the transition.
1569 if_route_dispatch(netmsg_t nmsg
)
1571 struct netmsg_ifroute
*msg
= (struct netmsg_ifroute
*)nmsg
;
1572 struct ifnet
*ifp
= msg
->ifp
;
1573 int flag
= msg
->flag
, fam
= msg
->fam
;
1574 struct ifaddr_container
*ifac
;
1576 ifq_purge_all(&ifp
->if_snd
);
1577 ifp
->if_flags
|= flag
;
1578 getmicrotime(&ifp
->if_lastchange
);
1580 * The ifaddr processing in the following loop will block,
1581 * however, this function is called in netisr0, in which
1582 * ifaddr list changes happen, so we don't care about the
1583 * blockness of the ifaddr processing here.
1585 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
1586 struct ifaddr
*ifa
= ifac
->ifa
;
1589 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
1592 if (fam
== PF_UNSPEC
|| (fam
== ifa
->ifa_addr
->sa_family
))
1593 kpfctlinput(PRC_IFUP
, ifa
->ifa_addr
);
1600 lwkt_replymsg(&nmsg
->lmsg
, 0);
1604 if_route(struct ifnet
*ifp
, int flag
, int fam
)
1606 struct netmsg_ifroute msg
;
1608 ASSERT_CANDOMSG_NETISR0(curthread
);
1610 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
, 0,
1615 lwkt_domsg(netisr_cpuport(0), &msg
.base
.lmsg
, 0);
1619 * Mark an interface down and notify protocols of the transition. An
1620 * interface going down is also considered to be a synchronizing event.
1621 * We must ensure that all packet processing related to the interface
1622 * has completed before we return so e.g. the caller can free the ifnet
1623 * structure that the mbufs may be referencing.
1625 * NOTE: must be called at splnet or eqivalent.
1628 if_down(struct ifnet
*ifp
)
1630 if_unroute(ifp
, IFF_UP
, AF_UNSPEC
);
1631 netmsg_service_sync();
1635 * Mark an interface up and notify protocols of
1637 * NOTE: must be called at splnet or eqivalent.
1640 if_up(struct ifnet
*ifp
)
1642 if_route(ifp
, IFF_UP
, AF_UNSPEC
);
1646 * Process a link state change.
1647 * NOTE: must be called at splsoftnet or equivalent.
1650 if_link_state_change(struct ifnet
*ifp
)
1652 int link_state
= ifp
->if_link_state
;
1655 devctl_notify("IFNET", ifp
->if_xname
,
1656 (link_state
== LINK_STATE_UP
) ? "LINK_UP" : "LINK_DOWN", NULL
);
1660 * Handle interface watchdog timer routines. Called
1661 * from softclock, we decrement timers (if set) and
1662 * call the appropriate interface routine on expiration.
1665 if_slowtimo_dispatch(netmsg_t nmsg
)
1667 struct globaldata
*gd
= mycpu
;
1668 const struct ifnet_array
*arr
;
1671 ASSERT_IN_NETISR(0);
1674 lwkt_replymsg(&nmsg
->lmsg
, 0); /* reply ASAP */
1677 arr
= ifnet_array_get();
1678 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1679 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1683 if (if_stats_compat
) {
1684 IFNET_STAT_GET(ifp
, ipackets
, ifp
->if_ipackets
);
1685 IFNET_STAT_GET(ifp
, ierrors
, ifp
->if_ierrors
);
1686 IFNET_STAT_GET(ifp
, opackets
, ifp
->if_opackets
);
1687 IFNET_STAT_GET(ifp
, oerrors
, ifp
->if_oerrors
);
1688 IFNET_STAT_GET(ifp
, collisions
, ifp
->if_collisions
);
1689 IFNET_STAT_GET(ifp
, ibytes
, ifp
->if_ibytes
);
1690 IFNET_STAT_GET(ifp
, obytes
, ifp
->if_obytes
);
1691 IFNET_STAT_GET(ifp
, imcasts
, ifp
->if_imcasts
);
1692 IFNET_STAT_GET(ifp
, omcasts
, ifp
->if_omcasts
);
1693 IFNET_STAT_GET(ifp
, iqdrops
, ifp
->if_iqdrops
);
1694 IFNET_STAT_GET(ifp
, noproto
, ifp
->if_noproto
);
1695 IFNET_STAT_GET(ifp
, oqdrops
, ifp
->if_oqdrops
);
1698 if (ifp
->if_timer
== 0 || --ifp
->if_timer
) {
1702 if (ifp
->if_watchdog
) {
1703 if (ifnet_tryserialize_all(ifp
)) {
1704 (*ifp
->if_watchdog
)(ifp
);
1705 ifnet_deserialize_all(ifp
);
1707 /* try again next timeout */
1715 callout_reset(&if_slowtimo_timer
, hz
/ IFNET_SLOWHZ
, if_slowtimo
, NULL
);
1719 if_slowtimo(void *arg __unused
)
1721 struct lwkt_msg
*lmsg
= &if_slowtimo_netmsg
.lmsg
;
1723 KASSERT(mycpuid
== 0, ("not on cpu0"));
1725 if (lmsg
->ms_flags
& MSGF_DONE
)
1726 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg
);
1731 * Map interface name to
1732 * interface structure pointer.
1735 ifunit(const char *name
)
1740 * Search all the interfaces for this name/number
1742 KASSERT(mtx_owned(&ifnet_mtx
), ("ifnet is not locked"));
1744 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
1745 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1752 ifunit_netisr(const char *name
)
1754 const struct ifnet_array
*arr
;
1758 * Search all the interfaces for this name/number
1761 arr
= ifnet_array_get();
1762 for (i
= 0; i
< arr
->ifnet_count
; ++i
) {
1763 struct ifnet
*ifp
= arr
->ifnet_arr
[i
];
1765 if (strncmp(ifp
->if_xname
, name
, IFNAMSIZ
) == 0)
1775 ifioctl(struct socket
*so
, u_long cmd
, caddr_t data
, struct ucred
*cred
)
1783 size_t namelen
, onamelen
;
1784 char new_name
[IFNAMSIZ
];
1786 struct sockaddr_dl
*sdl
;
1791 return (ifconf(cmd
, data
, cred
));
1796 ifr
= (struct ifreq
*)data
;
1801 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1803 return (if_clone_create(ifr
->ifr_name
, sizeof(ifr
->ifr_name
),
1804 cmd
== SIOCIFCREATE2
? ifr
->ifr_data
: NULL
));
1806 if ((error
= priv_check_cred(cred
, PRIV_ROOT
, 0)) != 0)
1808 return (if_clone_destroy(ifr
->ifr_name
));
1809 case SIOCIFGCLONERS
:
1810 return (if_clone_list((struct if_clonereq
*)data
));
1816 * Nominal ioctl through interface, lookup the ifp and obtain a
1817 * lock to serialize the ifconfig ioctl operation.
1821 ifp
= ifunit(ifr
->ifr_name
);
1830 ifr
->ifr_index
= ifp
->if_index
;
1834 ifr
->ifr_flags
= ifp
->if_flags
;
1835 ifr
->ifr_flagshigh
= ifp
->if_flags
>> 16;
1839 ifr
->ifr_reqcap
= ifp
->if_capabilities
;
1840 ifr
->ifr_curcap
= ifp
->if_capenable
;
1844 ifr
->ifr_metric
= ifp
->if_metric
;
1848 ifr
->ifr_mtu
= ifp
->if_mtu
;
1852 ifr
->ifr_tsolen
= ifp
->if_tsolen
;
1856 error
= copyout((caddr_t
)&ifp
->if_data
, ifr
->ifr_data
,
1857 sizeof(ifp
->if_data
));
1861 ifr
->ifr_phys
= ifp
->if_physical
;
1864 case SIOCGIFPOLLCPU
:
1865 ifr
->ifr_pollcpu
= -1;
1868 case SIOCSIFPOLLCPU
:
1872 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1875 new_flags
= (ifr
->ifr_flags
& 0xffff) |
1876 (ifr
->ifr_flagshigh
<< 16);
1877 if (ifp
->if_flags
& IFF_SMART
) {
1878 /* Smart drivers twiddle their own routes */
1879 } else if (ifp
->if_flags
& IFF_UP
&&
1880 (new_flags
& IFF_UP
) == 0) {
1884 } else if (new_flags
& IFF_UP
&&
1885 (ifp
->if_flags
& IFF_UP
) == 0) {
1891 #ifdef IFPOLL_ENABLE
1892 if ((new_flags
^ ifp
->if_flags
) & IFF_NPOLLING
) {
1893 if (new_flags
& IFF_NPOLLING
)
1894 ifpoll_register(ifp
);
1896 ifpoll_deregister(ifp
);
1900 ifp
->if_flags
= (ifp
->if_flags
& IFF_CANTCHANGE
) |
1901 (new_flags
&~ IFF_CANTCHANGE
);
1902 if (new_flags
& IFF_PPROMISC
) {
1903 /* Permanently promiscuous mode requested */
1904 ifp
->if_flags
|= IFF_PROMISC
;
1905 } else if (ifp
->if_pcount
== 0) {
1906 ifp
->if_flags
&= ~IFF_PROMISC
;
1908 if (ifp
->if_ioctl
) {
1909 ifnet_serialize_all(ifp
);
1910 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1911 ifnet_deserialize_all(ifp
);
1913 getmicrotime(&ifp
->if_lastchange
);
1917 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1920 if (ifr
->ifr_reqcap
& ~ifp
->if_capabilities
) {
1924 ifnet_serialize_all(ifp
);
1925 ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1926 ifnet_deserialize_all(ifp
);
1930 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1933 error
= copyinstr(ifr
->ifr_data
, new_name
, IFNAMSIZ
, NULL
);
1936 if (new_name
[0] == '\0') {
1940 if (ifunit(new_name
) != NULL
) {
1945 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
1947 /* Announce the departure of the interface. */
1948 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
1950 strlcpy(ifp
->if_xname
, new_name
, sizeof(ifp
->if_xname
));
1951 ifa
= TAILQ_FIRST(&ifp
->if_addrheads
[mycpuid
])->ifa
;
1952 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1953 namelen
= strlen(new_name
);
1954 onamelen
= sdl
->sdl_nlen
;
1956 * Move the address if needed. This is safe because we
1957 * allocate space for a name of length IFNAMSIZ when we
1958 * create this in if_attach().
1960 if (namelen
!= onamelen
) {
1961 bcopy(sdl
->sdl_data
+ onamelen
,
1962 sdl
->sdl_data
+ namelen
, sdl
->sdl_alen
);
1964 bcopy(new_name
, sdl
->sdl_data
, namelen
);
1965 sdl
->sdl_nlen
= namelen
;
1966 sdl
= (struct sockaddr_dl
*)ifa
->ifa_netmask
;
1967 bzero(sdl
->sdl_data
, onamelen
);
1968 while (namelen
!= 0)
1969 sdl
->sdl_data
[--namelen
] = 0xff;
1971 EVENTHANDLER_INVOKE(ifnet_attach_event
, ifp
);
1973 /* Announce the return of the interface. */
1974 rt_ifannouncemsg(ifp
, IFAN_ARRIVAL
);
1978 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1981 ifp
->if_metric
= ifr
->ifr_metric
;
1982 getmicrotime(&ifp
->if_lastchange
);
1986 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
1989 if (ifp
->if_ioctl
== NULL
) {
1993 ifnet_serialize_all(ifp
);
1994 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
1995 ifnet_deserialize_all(ifp
);
1997 getmicrotime(&ifp
->if_lastchange
);
2002 u_long oldmtu
= ifp
->if_mtu
;
2004 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2007 if (ifp
->if_ioctl
== NULL
) {
2011 if (ifr
->ifr_mtu
< IF_MINMTU
|| ifr
->ifr_mtu
> IF_MAXMTU
) {
2015 ifnet_serialize_all(ifp
);
2016 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2017 ifnet_deserialize_all(ifp
);
2019 getmicrotime(&ifp
->if_lastchange
);
2023 * If the link MTU changed, do network layer specific procedure.
2025 if (ifp
->if_mtu
!= oldmtu
) {
2034 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2038 /* XXX need driver supplied upper limit */
2039 if (ifr
->ifr_tsolen
<= 0) {
2043 ifp
->if_tsolen
= ifr
->ifr_tsolen
;
2048 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2052 /* Don't allow group membership on non-multicast interfaces. */
2053 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2058 /* Don't let users screw up protocols' entries. */
2059 if (ifr
->ifr_addr
.sa_family
!= AF_LINK
) {
2064 if (cmd
== SIOCADDMULTI
) {
2065 struct ifmultiaddr
*ifma
;
2066 error
= if_addmulti(ifp
, &ifr
->ifr_addr
, &ifma
);
2068 error
= if_delmulti(ifp
, &ifr
->ifr_addr
);
2071 getmicrotime(&ifp
->if_lastchange
);
2074 case SIOCSIFPHYADDR
:
2075 case SIOCDIFPHYADDR
:
2077 case SIOCSIFPHYADDR_IN6
:
2079 case SIOCSLIFPHYADDR
:
2081 case SIOCSIFGENERIC
:
2082 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2085 if (ifp
->if_ioctl
== 0) {
2089 ifnet_serialize_all(ifp
);
2090 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2091 ifnet_deserialize_all(ifp
);
2093 getmicrotime(&ifp
->if_lastchange
);
2097 ifs
= (struct ifstat
*)data
;
2098 ifs
->ascii
[0] = '\0';
2100 case SIOCGIFPSRCADDR
:
2101 case SIOCGIFPDSTADDR
:
2102 case SIOCGLIFPHYADDR
:
2104 case SIOCGIFGENERIC
:
2105 if (ifp
->if_ioctl
== NULL
) {
2109 ifnet_serialize_all(ifp
);
2110 error
= ifp
->if_ioctl(ifp
, cmd
, data
, cred
);
2111 ifnet_deserialize_all(ifp
);
2115 error
= priv_check_cred(cred
, PRIV_ROOT
, 0);
2118 error
= if_setlladdr(ifp
, ifr
->ifr_addr
.sa_data
,
2119 ifr
->ifr_addr
.sa_len
);
2120 EVENTHANDLER_INVOKE(iflladdr_event
, ifp
);
2124 oif_flags
= ifp
->if_flags
;
2125 if (so
->so_proto
== 0) {
2129 error
= so_pru_control_direct(so
, cmd
, data
, ifp
);
2131 if ((oif_flags
^ ifp
->if_flags
) & IFF_UP
) {
2133 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2134 if (ifp
->if_flags
& IFF_UP
) {
2149 * Set/clear promiscuous mode on interface ifp based on the truth value
2150 * of pswitch. The calls are reference counted so that only the first
2151 * "on" request actually has an effect, as does the final "off" request.
2152 * Results are undefined if the "off" and "on" requests are not matched.
2155 ifpromisc(struct ifnet
*ifp
, int pswitch
)
2161 oldflags
= ifp
->if_flags
;
2162 if (ifp
->if_flags
& IFF_PPROMISC
) {
2163 /* Do nothing if device is in permanently promiscuous mode */
2164 ifp
->if_pcount
+= pswitch
? 1 : -1;
2169 * If the device is not configured up, we cannot put it in
2172 if ((ifp
->if_flags
& IFF_UP
) == 0)
2174 if (ifp
->if_pcount
++ != 0)
2176 ifp
->if_flags
|= IFF_PROMISC
;
2177 log(LOG_INFO
, "%s: promiscuous mode enabled\n",
2180 if (--ifp
->if_pcount
> 0)
2182 ifp
->if_flags
&= ~IFF_PROMISC
;
2183 log(LOG_INFO
, "%s: promiscuous mode disabled\n",
2186 ifr
.ifr_flags
= ifp
->if_flags
;
2187 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2188 ifnet_serialize_all(ifp
);
2189 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, NULL
);
2190 ifnet_deserialize_all(ifp
);
2194 ifp
->if_flags
= oldflags
;
2199 * Return interface configuration
2200 * of system. List may be used
2201 * in later ioctl's (above) to get
2202 * other information.
2205 ifconf(u_long cmd
, caddr_t data
, struct ucred
*cred
)
2207 struct ifconf
*ifc
= (struct ifconf
*)data
;
2209 struct sockaddr
*sa
;
2210 struct ifreq ifr
, *ifrp
;
2211 int space
= ifc
->ifc_len
, error
= 0;
2213 ifrp
= ifc
->ifc_req
;
2216 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2217 struct ifaddr_container
*ifac
, *ifac_mark
;
2218 struct ifaddr_marker mark
;
2219 struct ifaddrhead
*head
;
2222 if (space
<= sizeof ifr
)
2226 * Zero the stack declared structure first to prevent
2227 * memory disclosure.
2229 bzero(&ifr
, sizeof(ifr
));
2230 if (strlcpy(ifr
.ifr_name
, ifp
->if_xname
, sizeof(ifr
.ifr_name
))
2231 >= sizeof(ifr
.ifr_name
)) {
2232 error
= ENAMETOOLONG
;
2237 * Add a marker, since copyout() could block and during that
2238 * period the list could be changed. Inserting the marker to
2239 * the header of the list will not cause trouble for the code
2240 * assuming that the first element of the list is AF_LINK; the
2241 * marker will be moved to the next position w/o blocking.
2243 ifa_marker_init(&mark
, ifp
);
2244 ifac_mark
= &mark
.ifac
;
2245 head
= &ifp
->if_addrheads
[mycpuid
];
2248 TAILQ_INSERT_HEAD(head
, ifac_mark
, ifa_link
);
2249 while ((ifac
= TAILQ_NEXT(ifac_mark
, ifa_link
)) != NULL
) {
2250 struct ifaddr
*ifa
= ifac
->ifa
;
2252 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2253 TAILQ_INSERT_AFTER(head
, ifac
, ifac_mark
, ifa_link
);
2256 if (ifa
->ifa_addr
->sa_family
== AF_UNSPEC
)
2259 if (space
<= sizeof ifr
)
2262 if (cred
->cr_prison
&&
2263 prison_if(cred
, sa
))
2267 * Keep a reference on this ifaddr, so that it will
2268 * not be destroyed when its address is copied to
2269 * the userland, which could block.
2272 if (sa
->sa_len
<= sizeof(*sa
)) {
2274 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2277 if (space
< (sizeof ifr
) + sa
->sa_len
-
2282 space
-= sa
->sa_len
- sizeof(*sa
);
2283 error
= copyout(&ifr
, ifrp
,
2284 sizeof ifr
.ifr_name
);
2286 error
= copyout(sa
, &ifrp
->ifr_addr
,
2288 ifrp
= (struct ifreq
*)
2289 (sa
->sa_len
+ (caddr_t
)&ifrp
->ifr_addr
);
2294 space
-= sizeof ifr
;
2296 TAILQ_REMOVE(head
, ifac_mark
, ifa_link
);
2300 bzero(&ifr
.ifr_addr
, sizeof ifr
.ifr_addr
);
2301 error
= copyout(&ifr
, ifrp
, sizeof ifr
);
2304 space
-= sizeof ifr
;
2310 ifc
->ifc_len
-= space
;
2315 * Just like if_promisc(), but for all-multicast-reception mode.
2318 if_allmulti(struct ifnet
*ifp
, int onswitch
)
2326 if (ifp
->if_amcount
++ == 0) {
2327 ifp
->if_flags
|= IFF_ALLMULTI
;
2328 ifr
.ifr_flags
= ifp
->if_flags
;
2329 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2330 ifnet_serialize_all(ifp
);
2331 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2333 ifnet_deserialize_all(ifp
);
2336 if (ifp
->if_amcount
> 1) {
2339 ifp
->if_amcount
= 0;
2340 ifp
->if_flags
&= ~IFF_ALLMULTI
;
2341 ifr
.ifr_flags
= ifp
->if_flags
;
2342 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2343 ifnet_serialize_all(ifp
);
2344 error
= ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2346 ifnet_deserialize_all(ifp
);
2358 * Add a multicast listenership to the interface in question.
2359 * The link layer provides a routine which converts
2362 if_addmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
,
2363 struct ifmultiaddr
**retifma
)
2365 struct sockaddr
*llsa
, *dupsa
;
2367 struct ifmultiaddr
*ifma
;
2369 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2372 * If the matching multicast address already exists
2373 * then don't add a new one, just add a reference
2375 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2376 if (sa_equal(sa
, ifma
->ifma_addr
)) {
2377 ifma
->ifma_refcount
++;
2385 * Give the link layer a chance to accept/reject it, and also
2386 * find out which AF_LINK address this maps to, if it isn't one
2389 if (ifp
->if_resolvemulti
) {
2390 error
= ifp
->if_resolvemulti(ifp
, &llsa
, sa
);
2397 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2398 dupsa
= kmalloc(sa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2399 bcopy(sa
, dupsa
, sa
->sa_len
);
2401 ifma
->ifma_addr
= dupsa
;
2402 ifma
->ifma_lladdr
= llsa
;
2403 ifma
->ifma_ifp
= ifp
;
2404 ifma
->ifma_refcount
= 1;
2405 ifma
->ifma_protospec
= NULL
;
2406 rt_newmaddrmsg(RTM_NEWMADDR
, ifma
);
2408 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2413 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
) {
2414 if (sa_equal(ifma
->ifma_addr
, llsa
))
2418 ifma
->ifma_refcount
++;
2420 ifma
= kmalloc(sizeof *ifma
, M_IFMADDR
, M_INTWAIT
);
2421 dupsa
= kmalloc(llsa
->sa_len
, M_IFMADDR
, M_INTWAIT
);
2422 bcopy(llsa
, dupsa
, llsa
->sa_len
);
2423 ifma
->ifma_addr
= dupsa
;
2424 ifma
->ifma_ifp
= ifp
;
2425 ifma
->ifma_refcount
= 1;
2426 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2430 * We are certain we have added something, so call down to the
2431 * interface to let them know about it.
2434 ifp
->if_ioctl(ifp
, SIOCADDMULTI
, 0, NULL
);
2440 if_addmulti(struct ifnet
*ifp
, struct sockaddr
*sa
,
2441 struct ifmultiaddr
**retifma
)
2445 ifnet_serialize_all(ifp
);
2446 error
= if_addmulti_serialized(ifp
, sa
, retifma
);
2447 ifnet_deserialize_all(ifp
);
2453 * Remove a reference to a multicast address on this interface. Yell
2454 * if the request does not match an existing membership.
2457 if_delmulti_serialized(struct ifnet
*ifp
, struct sockaddr
*sa
)
2459 struct ifmultiaddr
*ifma
;
2461 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2463 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2464 if (sa_equal(sa
, ifma
->ifma_addr
))
2469 if (ifma
->ifma_refcount
> 1) {
2470 ifma
->ifma_refcount
--;
2474 rt_newmaddrmsg(RTM_DELMADDR
, ifma
);
2475 sa
= ifma
->ifma_lladdr
;
2476 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2478 * Make sure the interface driver is notified
2479 * in the case of a link layer mcast group being left.
2481 if (ifma
->ifma_addr
->sa_family
== AF_LINK
&& sa
== NULL
)
2482 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2483 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2484 kfree(ifma
, M_IFMADDR
);
2489 * Now look for the link-layer address which corresponds to
2490 * this network address. It had been squirreled away in
2491 * ifma->ifma_lladdr for this purpose (so we don't have
2492 * to call ifp->if_resolvemulti() again), and we saved that
2493 * value in sa above. If some nasty deleted the
2494 * link-layer address out from underneath us, we can deal because
2495 * the address we stored was is not the same as the one which was
2496 * in the record for the link-layer address. (So we don't complain
2499 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2500 if (sa_equal(sa
, ifma
->ifma_addr
))
2505 if (ifma
->ifma_refcount
> 1) {
2506 ifma
->ifma_refcount
--;
2510 TAILQ_REMOVE(&ifp
->if_multiaddrs
, ifma
, ifma_link
);
2511 ifp
->if_ioctl(ifp
, SIOCDELMULTI
, 0, NULL
);
2512 kfree(ifma
->ifma_addr
, M_IFMADDR
);
2513 kfree(sa
, M_IFMADDR
);
2514 kfree(ifma
, M_IFMADDR
);
2520 if_delmulti(struct ifnet
*ifp
, struct sockaddr
*sa
)
2524 ifnet_serialize_all(ifp
);
2525 error
= if_delmulti_serialized(ifp
, sa
);
2526 ifnet_deserialize_all(ifp
);
2532 * Delete all multicast group membership for an interface.
2533 * Should be used to quickly flush all multicast filters.
2536 if_delallmulti_serialized(struct ifnet
*ifp
)
2538 struct ifmultiaddr
*ifma
, mark
;
2541 ASSERT_IFNET_SERIALIZED_ALL(ifp
);
2543 bzero(&sa
, sizeof(sa
));
2544 sa
.sa_family
= AF_UNSPEC
;
2545 sa
.sa_len
= sizeof(sa
);
2547 bzero(&mark
, sizeof(mark
));
2548 mark
.ifma_addr
= &sa
;
2550 TAILQ_INSERT_HEAD(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2551 while ((ifma
= TAILQ_NEXT(&mark
, ifma_link
)) != NULL
) {
2552 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2553 TAILQ_INSERT_AFTER(&ifp
->if_multiaddrs
, ifma
, &mark
,
2556 if (ifma
->ifma_addr
->sa_family
== AF_UNSPEC
)
2559 if_delmulti_serialized(ifp
, ifma
->ifma_addr
);
2561 TAILQ_REMOVE(&ifp
->if_multiaddrs
, &mark
, ifma_link
);
2566 * Set the link layer address on an interface.
2568 * At this time we only support certain types of interfaces,
2569 * and we don't allow the length of the address to change.
2572 if_setlladdr(struct ifnet
*ifp
, const u_char
*lladdr
, int len
)
2574 struct sockaddr_dl
*sdl
;
2577 sdl
= IF_LLSOCKADDR(ifp
);
2580 if (len
!= sdl
->sdl_alen
) /* don't allow length to change */
2582 switch (ifp
->if_type
) {
2583 case IFT_ETHER
: /* these types use struct arpcom */
2586 case IFT_IEEE8023ADLAG
:
2587 bcopy(lladdr
, ((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, len
);
2588 bcopy(lladdr
, LLADDR(sdl
), len
);
2594 * If the interface is already up, we need
2595 * to re-init it in order to reprogram its
2598 ifnet_serialize_all(ifp
);
2599 if ((ifp
->if_flags
& IFF_UP
) != 0) {
2601 struct ifaddr_container
*ifac
;
2604 ifp
->if_flags
&= ~IFF_UP
;
2605 ifr
.ifr_flags
= ifp
->if_flags
;
2606 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2607 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2609 ifp
->if_flags
|= IFF_UP
;
2610 ifr
.ifr_flags
= ifp
->if_flags
;
2611 ifr
.ifr_flagshigh
= ifp
->if_flags
>> 16;
2612 ifp
->if_ioctl(ifp
, SIOCSIFFLAGS
, (caddr_t
)&ifr
,
2616 * Also send gratuitous ARPs to notify other nodes about
2617 * the address change.
2619 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
2620 struct ifaddr
*ifa
= ifac
->ifa
;
2622 if (ifa
->ifa_addr
!= NULL
&&
2623 ifa
->ifa_addr
->sa_family
== AF_INET
)
2624 arp_gratuitous(ifp
, ifa
);
2628 ifnet_deserialize_all(ifp
);
2632 struct ifmultiaddr
*
2633 ifmaof_ifpforaddr(struct sockaddr
*sa
, struct ifnet
*ifp
)
2635 struct ifmultiaddr
*ifma
;
2637 /* TODO: need ifnet_serialize_main */
2638 ifnet_serialize_all(ifp
);
2639 TAILQ_FOREACH(ifma
, &ifp
->if_multiaddrs
, ifma_link
)
2640 if (sa_equal(ifma
->ifma_addr
, sa
))
2642 ifnet_deserialize_all(ifp
);
2648 * This function locates the first real ethernet MAC from a network
2649 * card and loads it into node, returning 0 on success or ENOENT if
2650 * no suitable interfaces were found. It is used by the uuid code to
2651 * generate a unique 6-byte number.
2654 if_getanyethermac(uint16_t *node
, int minlen
)
2657 struct sockaddr_dl
*sdl
;
2660 TAILQ_FOREACH(ifp
, &ifnetlist
, if_link
) {
2661 if (ifp
->if_type
!= IFT_ETHER
)
2663 sdl
= IF_LLSOCKADDR(ifp
);
2664 if (sdl
->sdl_alen
< minlen
)
2666 bcopy(((struct arpcom
*)ifp
->if_softc
)->ac_enaddr
, node
,
2676 * The name argument must be a pointer to storage which will last as
2677 * long as the interface does. For physical devices, the result of
2678 * device_get_name(dev) is a good choice and for pseudo-devices a
2679 * static string works well.
2682 if_initname(struct ifnet
*ifp
, const char *name
, int unit
)
2684 ifp
->if_dname
= name
;
2685 ifp
->if_dunit
= unit
;
2686 if (unit
!= IF_DUNIT_NONE
)
2687 ksnprintf(ifp
->if_xname
, IFNAMSIZ
, "%s%d", name
, unit
);
2689 strlcpy(ifp
->if_xname
, name
, IFNAMSIZ
);
2693 if_printf(struct ifnet
*ifp
, const char *fmt
, ...)
2698 retval
= kprintf("%s: ", ifp
->if_xname
);
2699 __va_start(ap
, fmt
);
2700 retval
+= kvprintf(fmt
, ap
);
2706 if_alloc(uint8_t type
)
2712 * XXX temporary hack until arpcom is setup in if_l2com
2714 if (type
== IFT_ETHER
)
2715 size
= sizeof(struct arpcom
);
2717 size
= sizeof(struct ifnet
);
2719 ifp
= kmalloc(size
, M_IFNET
, M_WAITOK
|M_ZERO
);
2721 ifp
->if_type
= type
;
2723 if (if_com_alloc
[type
] != NULL
) {
2724 ifp
->if_l2com
= if_com_alloc
[type
](type
, ifp
);
2725 if (ifp
->if_l2com
== NULL
) {
2726 kfree(ifp
, M_IFNET
);
2734 if_free(struct ifnet
*ifp
)
2736 kfree(ifp
, M_IFNET
);
2740 ifq_set_classic(struct ifaltq
*ifq
)
2742 ifq_set_methods(ifq
, ifq
->altq_ifp
->if_mapsubq
,
2743 ifsq_classic_enqueue
, ifsq_classic_dequeue
, ifsq_classic_request
);
2747 ifq_set_methods(struct ifaltq
*ifq
, altq_mapsubq_t mapsubq
,
2748 ifsq_enqueue_t enqueue
, ifsq_dequeue_t dequeue
, ifsq_request_t request
)
2752 KASSERT(mapsubq
!= NULL
, ("mapsubq is not specified"));
2753 KASSERT(enqueue
!= NULL
, ("enqueue is not specified"));
2754 KASSERT(dequeue
!= NULL
, ("dequeue is not specified"));
2755 KASSERT(request
!= NULL
, ("request is not specified"));
2757 ifq
->altq_mapsubq
= mapsubq
;
2758 for (q
= 0; q
< ifq
->altq_subq_cnt
; ++q
) {
2759 struct ifaltq_subque
*ifsq
= &ifq
->altq_subq
[q
];
2761 ifsq
->ifsq_enqueue
= enqueue
;
2762 ifsq
->ifsq_dequeue
= dequeue
;
2763 ifsq
->ifsq_request
= request
;
2768 ifsq_norm_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2770 m
->m_nextpkt
= NULL
;
2771 if (ifsq
->ifsq_norm_tail
== NULL
)
2772 ifsq
->ifsq_norm_head
= m
;
2774 ifsq
->ifsq_norm_tail
->m_nextpkt
= m
;
2775 ifsq
->ifsq_norm_tail
= m
;
2776 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2780 ifsq_prio_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
)
2782 m
->m_nextpkt
= NULL
;
2783 if (ifsq
->ifsq_prio_tail
== NULL
)
2784 ifsq
->ifsq_prio_head
= m
;
2786 ifsq
->ifsq_prio_tail
->m_nextpkt
= m
;
2787 ifsq
->ifsq_prio_tail
= m
;
2788 ALTQ_SQ_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2789 ALTQ_SQ_PRIO_CNTR_INC(ifsq
, m
->m_pkthdr
.len
);
2792 static struct mbuf
*
2793 ifsq_norm_dequeue(struct ifaltq_subque
*ifsq
)
2797 m
= ifsq
->ifsq_norm_head
;
2799 if ((ifsq
->ifsq_norm_head
= m
->m_nextpkt
) == NULL
)
2800 ifsq
->ifsq_norm_tail
= NULL
;
2801 m
->m_nextpkt
= NULL
;
2802 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2807 static struct mbuf
*
2808 ifsq_prio_dequeue(struct ifaltq_subque
*ifsq
)
2812 m
= ifsq
->ifsq_prio_head
;
2814 if ((ifsq
->ifsq_prio_head
= m
->m_nextpkt
) == NULL
)
2815 ifsq
->ifsq_prio_tail
= NULL
;
2816 m
->m_nextpkt
= NULL
;
2817 ALTQ_SQ_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2818 ALTQ_SQ_PRIO_CNTR_DEC(ifsq
, m
->m_pkthdr
.len
);
2824 ifsq_classic_enqueue(struct ifaltq_subque
*ifsq
, struct mbuf
*m
,
2825 struct altq_pktattr
*pa __unused
)
2828 if (ifsq
->ifsq_len
>= ifsq
->ifsq_maxlen
||
2829 ifsq
->ifsq_bcnt
>= ifsq
->ifsq_maxbcnt
) {
2830 if ((m
->m_flags
& M_PRIO
) &&
2831 ifsq
->ifsq_prio_len
< (ifsq
->ifsq_maxlen
/ 2) &&
2832 ifsq
->ifsq_prio_bcnt
< (ifsq
->ifsq_maxbcnt
/ 2)) {
2833 struct mbuf
*m_drop
;
2836 * Perform drop-head on normal queue
2838 m_drop
= ifsq_norm_dequeue(ifsq
);
2839 if (m_drop
!= NULL
) {
2841 ifsq_prio_enqueue(ifsq
, m
);
2844 /* XXX nothing could be dropped? */
2849 if (m
->m_flags
& M_PRIO
)
2850 ifsq_prio_enqueue(ifsq
, m
);
2852 ifsq_norm_enqueue(ifsq
, m
);
2858 ifsq_classic_dequeue(struct ifaltq_subque
*ifsq
, int op
)
2864 m
= ifsq
->ifsq_prio_head
;
2866 m
= ifsq
->ifsq_norm_head
;
2870 m
= ifsq_prio_dequeue(ifsq
);
2872 m
= ifsq_norm_dequeue(ifsq
);
2876 panic("unsupported ALTQ dequeue op: %d", op
);
2882 ifsq_classic_request(struct ifaltq_subque
*ifsq
, int req
, void *arg
)
2889 m
= ifsq_classic_dequeue(ifsq
, ALTDQ_REMOVE
);
2897 panic("unsupported ALTQ request: %d", req
);
2903 ifsq_ifstart_try(struct ifaltq_subque
*ifsq
, int force_sched
)
2905 struct ifnet
*ifp
= ifsq_get_ifp(ifsq
);
2906 int running
= 0, need_sched
;
2909 * Try to do direct ifnet.if_start on the subqueue first, if there is
2910 * contention on the subqueue hardware serializer, ifnet.if_start on
2911 * the subqueue will be scheduled on the subqueue owner CPU.
2913 if (!ifsq_tryserialize_hw(ifsq
)) {
2915 * Subqueue hardware serializer contention happened,
2916 * ifnet.if_start on the subqueue is scheduled on
2917 * the subqueue owner CPU, and we keep going.
2919 ifsq_ifstart_schedule(ifsq
, 1);
2923 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
)) {
2924 ifp
->if_start(ifp
, ifsq
);
2925 if ((ifp
->if_flags
& IFF_RUNNING
) && !ifsq_is_oactive(ifsq
))
2928 need_sched
= ifsq_ifstart_need_schedule(ifsq
, running
);
2930 ifsq_deserialize_hw(ifsq
);
2934 * More data need to be transmitted, ifnet.if_start on the
2935 * subqueue is scheduled on the subqueue owner CPU, and we
2937 * NOTE: ifnet.if_start subqueue interlock is not released.
2939 ifsq_ifstart_schedule(ifsq
, force_sched
);
2944 * Subqeue packets staging mechanism:
2946 * The packets enqueued into the subqueue are staged to a certain amount
2947 * before the ifnet.if_start on the subqueue is called. In this way, the
2948 * driver could avoid writing to hardware registers upon every packet,
2949 * instead, hardware registers could be written when certain amount of
2950 * packets are put onto hardware TX ring. The measurement on several modern
2951 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2952 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2953 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2954 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2955 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2957 * Subqueue packets staging is performed for two entry points into drivers'
2958 * transmission function:
2959 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2960 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2962 * Subqueue packets staging will be stopped upon any of the following
2964 * - If the count of packets enqueued on the current CPU is great than or
2965 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2966 * - If the total length of packets enqueued on the current CPU is great
2967 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2968 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2969 * is usually less than hardware's MTU.
2970 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2971 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2973 * - The if_start_rollup(), which is registered as low priority netisr
2974 * rollup function, is called; probably because no more work is pending
2978 * Currently subqueue packet staging is only performed in netisr threads.
2981 ifq_dispatch(struct ifnet
*ifp
, struct mbuf
*m
, struct altq_pktattr
*pa
)
2983 struct ifaltq
*ifq
= &ifp
->if_snd
;
2984 struct ifaltq_subque
*ifsq
;
2985 int error
, start
= 0, len
, mcast
= 0, avoid_start
= 0;
2986 struct ifsubq_stage_head
*head
= NULL
;
2987 struct ifsubq_stage
*stage
= NULL
;
2988 struct globaldata
*gd
= mycpu
;
2989 struct thread
*td
= gd
->gd_curthread
;
2991 crit_enter_quick(td
);
2993 ifsq
= ifq_map_subq(ifq
, gd
->gd_cpuid
);
2994 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq
);
2996 len
= m
->m_pkthdr
.len
;
2997 if (m
->m_flags
& M_MCAST
)
3000 if (td
->td_type
== TD_TYPE_NETISR
) {
3001 head
= &ifsubq_stage_heads
[mycpuid
];
3002 stage
= ifsq_get_stage(ifsq
, mycpuid
);
3005 stage
->stg_len
+= len
;
3006 if (stage
->stg_cnt
< ifsq_stage_cntmax
&&
3007 stage
->stg_len
< (ifp
->if_mtu
- max_protohdr
))
3012 error
= ifsq_enqueue_locked(ifsq
, m
, pa
);
3014 IFNET_STAT_INC(ifp
, oqdrops
, 1);
3015 if (!ifsq_data_ready(ifsq
)) {
3016 ALTQ_SQ_UNLOCK(ifsq
);
3017 crit_exit_quick(td
);
3022 if (!ifsq_is_started(ifsq
)) {
3024 ALTQ_SQ_UNLOCK(ifsq
);
3027 if ((stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) == 0)
3028 ifsq_stage_insert(head
, stage
);
3030 IFNET_STAT_INC(ifp
, obytes
, len
);
3032 IFNET_STAT_INC(ifp
, omcasts
, 1);
3033 crit_exit_quick(td
);
3038 * Hold the subqueue interlock of ifnet.if_start
3040 ifsq_set_started(ifsq
);
3043 ALTQ_SQ_UNLOCK(ifsq
);
3046 IFNET_STAT_INC(ifp
, obytes
, len
);
3048 IFNET_STAT_INC(ifp
, omcasts
, 1);
3051 if (stage
!= NULL
) {
3052 if (!start
&& (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)) {
3053 KKASSERT(stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
);
3055 ifsq_stage_remove(head
, stage
);
3056 ifsq_ifstart_schedule(ifsq
, 1);
3058 crit_exit_quick(td
);
3062 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_QUED
) {
3063 ifsq_stage_remove(head
, stage
);
3071 crit_exit_quick(td
);
3075 ifsq_ifstart_try(ifsq
, 0);
3077 crit_exit_quick(td
);
3082 ifa_create(int size
)
3087 KASSERT(size
>= sizeof(*ifa
), ("ifaddr size too small"));
3089 ifa
= kmalloc(size
, M_IFADDR
, M_INTWAIT
| M_ZERO
);
3090 ifa
->ifa_containers
=
3091 kmalloc_cachealign(ncpus
* sizeof(struct ifaddr_container
),
3092 M_IFADDR
, M_INTWAIT
| M_ZERO
);
3094 ifa
->ifa_ncnt
= ncpus
;
3095 for (i
= 0; i
< ncpus
; ++i
) {
3096 struct ifaddr_container
*ifac
= &ifa
->ifa_containers
[i
];
3098 ifac
->ifa_magic
= IFA_CONTAINER_MAGIC
;
3100 ifac
->ifa_refcnt
= 1;
3103 kprintf("alloc ifa %p %d\n", ifa
, size
);
3109 ifac_free(struct ifaddr_container
*ifac
, int cpu_id
)
3111 struct ifaddr
*ifa
= ifac
->ifa
;
3113 KKASSERT(ifac
->ifa_magic
== IFA_CONTAINER_MAGIC
);
3114 KKASSERT(ifac
->ifa_refcnt
== 0);
3115 KASSERT(ifac
->ifa_listmask
== 0,
3116 ("ifa is still on %#x lists", ifac
->ifa_listmask
));
3118 ifac
->ifa_magic
= IFA_CONTAINER_DEAD
;
3120 #ifdef IFADDR_DEBUG_VERBOSE
3121 kprintf("try free ifa %p cpu_id %d\n", ifac
->ifa
, cpu_id
);
3124 KASSERT(ifa
->ifa_ncnt
> 0 && ifa
->ifa_ncnt
<= ncpus
,
3125 ("invalid # of ifac, %d", ifa
->ifa_ncnt
));
3126 if (atomic_fetchadd_int(&ifa
->ifa_ncnt
, -1) == 1) {
3128 kprintf("free ifa %p\n", ifa
);
3130 kfree(ifa
->ifa_containers
, M_IFADDR
);
3131 kfree(ifa
, M_IFADDR
);
3136 ifa_iflink_dispatch(netmsg_t nmsg
)
3138 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3139 struct ifaddr
*ifa
= msg
->ifa
;
3140 struct ifnet
*ifp
= msg
->ifp
;
3142 struct ifaddr_container
*ifac
;
3146 ifac
= &ifa
->ifa_containers
[cpu
];
3147 ASSERT_IFAC_VALID(ifac
);
3148 KASSERT((ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
) == 0,
3149 ("ifaddr is on if_addrheads"));
3151 ifac
->ifa_listmask
|= IFA_LIST_IFADDRHEAD
;
3153 TAILQ_INSERT_TAIL(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3155 TAILQ_INSERT_HEAD(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3159 ifa_forwardmsg(&nmsg
->lmsg
, cpu
+ 1);
3163 ifa_iflink(struct ifaddr
*ifa
, struct ifnet
*ifp
, int tail
)
3165 struct netmsg_ifaddr msg
;
3167 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3168 0, ifa_iflink_dispatch
);
3173 ifa_domsg(&msg
.base
.lmsg
, 0);
3177 ifa_ifunlink_dispatch(netmsg_t nmsg
)
3179 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3180 struct ifaddr
*ifa
= msg
->ifa
;
3181 struct ifnet
*ifp
= msg
->ifp
;
3183 struct ifaddr_container
*ifac
;
3187 ifac
= &ifa
->ifa_containers
[cpu
];
3188 ASSERT_IFAC_VALID(ifac
);
3189 KASSERT(ifac
->ifa_listmask
& IFA_LIST_IFADDRHEAD
,
3190 ("ifaddr is not on if_addrhead"));
3192 TAILQ_REMOVE(&ifp
->if_addrheads
[cpu
], ifac
, ifa_link
);
3193 ifac
->ifa_listmask
&= ~IFA_LIST_IFADDRHEAD
;
3197 ifa_forwardmsg(&nmsg
->lmsg
, cpu
+ 1);
3201 ifa_ifunlink(struct ifaddr
*ifa
, struct ifnet
*ifp
)
3203 struct netmsg_ifaddr msg
;
3205 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3206 0, ifa_ifunlink_dispatch
);
3210 ifa_domsg(&msg
.base
.lmsg
, 0);
3214 ifa_destroy_dispatch(netmsg_t nmsg
)
3216 struct netmsg_ifaddr
*msg
= (struct netmsg_ifaddr
*)nmsg
;
3219 ifa_forwardmsg(&nmsg
->lmsg
, mycpuid
+ 1);
3223 ifa_destroy(struct ifaddr
*ifa
)
3225 struct netmsg_ifaddr msg
;
3227 netmsg_init(&msg
.base
, NULL
, &curthread
->td_msgport
,
3228 0, ifa_destroy_dispatch
);
3231 ifa_domsg(&msg
.base
.lmsg
, 0);
3235 ifnet_portfn(int cpu
)
3237 return &ifnet_threads
[cpu
]->td_msgport
;
3241 ifnet_forwardmsg(struct lwkt_msg
*lmsg
, int next_cpu
)
3243 KKASSERT(next_cpu
> mycpuid
&& next_cpu
<= ncpus
);
3245 if (next_cpu
< ncpus
)
3246 lwkt_forwardmsg(ifnet_portfn(next_cpu
), lmsg
);
3248 lwkt_replymsg(lmsg
, 0);
3252 ifnet_domsg(struct lwkt_msg
*lmsg
, int cpu
)
3254 KKASSERT(cpu
< ncpus
);
3255 return lwkt_domsg(ifnet_portfn(cpu
), lmsg
, 0);
3259 ifnet_sendmsg(struct lwkt_msg
*lmsg
, int cpu
)
3261 KKASSERT(cpu
< ncpus
);
3262 lwkt_sendmsg(ifnet_portfn(cpu
), lmsg
);
3266 * Generic netmsg service loop. Some protocols may roll their own but all
3267 * must do the basic command dispatch function call done here.
3270 ifnet_service_loop(void *arg __unused
)
3274 while ((msg
= lwkt_waitport(&curthread
->td_msgport
, 0))) {
3275 KASSERT(msg
->base
.nm_dispatch
, ("ifnet_service: badmsg"));
3276 msg
->base
.nm_dispatch(msg
);
3281 if_start_rollup(void)
3283 struct ifsubq_stage_head
*head
= &ifsubq_stage_heads
[mycpuid
];
3284 struct ifsubq_stage
*stage
;
3288 while ((stage
= TAILQ_FIRST(&head
->stg_head
)) != NULL
) {
3289 struct ifaltq_subque
*ifsq
= stage
->stg_subq
;
3292 if (stage
->stg_flags
& IFSQ_STAGE_FLAG_SCHED
)
3294 ifsq_stage_remove(head
, stage
);
3297 ifsq_ifstart_schedule(ifsq
, 1);
3302 if (!ifsq_is_started(ifsq
)) {
3304 * Hold the subqueue interlock of
3307 ifsq_set_started(ifsq
);
3310 ALTQ_SQ_UNLOCK(ifsq
);
3313 ifsq_ifstart_try(ifsq
, 1);
3315 KKASSERT((stage
->stg_flags
&
3316 (IFSQ_STAGE_FLAG_QUED
| IFSQ_STAGE_FLAG_SCHED
)) == 0);
3323 ifnetinit(void *dummy __unused
)
3327 for (i
= 0; i
< ncpus
; ++i
) {
3328 struct thread
**thr
= &ifnet_threads
[i
];
3330 lwkt_create(ifnet_service_loop
, NULL
, thr
, NULL
,
3331 TDF_NOSTART
|TDF_FORCE_SPINPORT
|TDF_FIXEDCPU
,
3333 netmsg_service_port_init(&(*thr
)->td_msgport
);
3334 lwkt_schedule(*thr
);
3337 for (i
= 0; i
< ncpus
; ++i
)
3338 TAILQ_INIT(&ifsubq_stage_heads
[i
].stg_head
);
3339 netisr_register_rollup(if_start_rollup
, NETISR_ROLLUP_PRIO_IFSTART
);
3343 if_register_com_alloc(u_char type
,
3344 if_com_alloc_t
*a
, if_com_free_t
*f
)
3347 KASSERT(if_com_alloc
[type
] == NULL
,
3348 ("if_register_com_alloc: %d already registered", type
));
3349 KASSERT(if_com_free
[type
] == NULL
,
3350 ("if_register_com_alloc: %d free already registered", type
));
3352 if_com_alloc
[type
] = a
;
3353 if_com_free
[type
] = f
;
3357 if_deregister_com_alloc(u_char type
)
3360 KASSERT(if_com_alloc
[type
] != NULL
,
3361 ("if_deregister_com_alloc: %d not registered", type
));
3362 KASSERT(if_com_free
[type
] != NULL
,
3363 ("if_deregister_com_alloc: %d free not registered", type
));
3364 if_com_alloc
[type
] = NULL
;
3365 if_com_free
[type
] = NULL
;
3369 if_ring_count2(int cnt
, int cnt_max
)
3373 KASSERT(cnt_max
>= 1 && powerof2(cnt_max
),
3374 ("invalid ring count max %d", cnt_max
));
3383 while ((1 << (shift
+ 1)) <= cnt
)
3387 KASSERT(cnt
>= 1 && cnt
<= ncpus2
&& cnt
<= cnt_max
,
3388 ("calculate cnt %d, ncpus2 %d, cnt max %d",
3389 cnt
, ncpus2
, cnt_max
));
3394 ifq_set_maxlen(struct ifaltq
*ifq
, int len
)
3396 ifq
->altq_maxlen
= len
+ (ncpus
* ifsq_stage_cntmax
);
3400 ifq_mapsubq_default(struct ifaltq
*ifq __unused
, int cpuid __unused
)
3402 return ALTQ_SUBQ_INDEX_DEFAULT
;
3406 ifq_mapsubq_mask(struct ifaltq
*ifq
, int cpuid
)
3408 return (cpuid
& ifq
->altq_subq_mask
);
3412 ifsq_watchdog(void *arg
)
3414 struct ifsubq_watchdog
*wd
= arg
;
3417 if (__predict_true(wd
->wd_timer
== 0 || --wd
->wd_timer
))
3420 ifp
= ifsq_get_ifp(wd
->wd_subq
);
3421 if (ifnet_tryserialize_all(ifp
)) {
3422 wd
->wd_watchdog(wd
->wd_subq
);
3423 ifnet_deserialize_all(ifp
);
3425 /* try again next timeout */
3429 ifsq_watchdog_reset(wd
);
3433 ifsq_watchdog_reset(struct ifsubq_watchdog
*wd
)
3435 callout_reset_bycpu(&wd
->wd_callout
, hz
, ifsq_watchdog
, wd
,
3436 ifsq_get_cpuid(wd
->wd_subq
));
3440 ifsq_watchdog_init(struct ifsubq_watchdog
*wd
, struct ifaltq_subque
*ifsq
,
3441 ifsq_watchdog_t watchdog
)
3443 callout_init_mp(&wd
->wd_callout
);
3446 wd
->wd_watchdog
= watchdog
;
3450 ifsq_watchdog_start(struct ifsubq_watchdog
*wd
)
3453 ifsq_watchdog_reset(wd
);
3457 ifsq_watchdog_stop(struct ifsubq_watchdog
*wd
)
3460 callout_stop(&wd
->wd_callout
);
3466 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3467 ("try holding ifnet lock in netisr"));
3468 mtx_lock(&ifnet_mtx
);
3474 KASSERT(curthread
->td_type
!= TD_TYPE_NETISR
,
3475 ("try holding ifnet lock in netisr"));
3476 mtx_unlock(&ifnet_mtx
);
3479 static struct ifnet_array
*
3480 ifnet_array_alloc(int count
)
3482 struct ifnet_array
*arr
;
3484 arr
= kmalloc(__offsetof(struct ifnet_array
, ifnet_arr
[count
]),
3486 arr
->ifnet_count
= count
;
3492 ifnet_array_free(struct ifnet_array
*arr
)
3494 if (arr
== &ifnet_array0
)
3496 kfree(arr
, M_IFNET
);
3499 static struct ifnet_array
*
3500 ifnet_array_add(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3502 struct ifnet_array
*arr
;
3505 KASSERT(old_arr
->ifnet_count
>= 0,
3506 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3507 count
= old_arr
->ifnet_count
+ 1;
3508 arr
= ifnet_array_alloc(count
);
3511 * Save the old ifnet array and append this ifp to the end of
3512 * the new ifnet array.
3514 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3515 KASSERT(old_arr
->ifnet_arr
[i
] != ifp
,
3516 ("%s is already in ifnet array", ifp
->if_xname
));
3517 arr
->ifnet_arr
[i
] = old_arr
->ifnet_arr
[i
];
3519 KASSERT(i
== count
- 1,
3520 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3521 ifp
->if_xname
, count
- 1, i
));
3522 arr
->ifnet_arr
[i
] = ifp
;
3527 static struct ifnet_array
*
3528 ifnet_array_del(struct ifnet
*ifp
, const struct ifnet_array
*old_arr
)
3530 struct ifnet_array
*arr
;
3531 int count
, i
, idx
, found
= 0;
3533 KASSERT(old_arr
->ifnet_count
> 0,
3534 ("invalid ifnet array count %d", old_arr
->ifnet_count
));
3535 count
= old_arr
->ifnet_count
- 1;
3536 arr
= ifnet_array_alloc(count
);
3539 * Save the old ifnet array, but skip this ifp.
3542 for (i
= 0; i
< old_arr
->ifnet_count
; ++i
) {
3543 if (old_arr
->ifnet_arr
[i
] == ifp
) {
3545 ("dup %s is in ifnet array", ifp
->if_xname
));
3549 KASSERT(idx
< count
,
3550 ("invalid ifnet array index %d, count %d", idx
, count
));
3551 arr
->ifnet_arr
[idx
] = old_arr
->ifnet_arr
[i
];
3554 KASSERT(found
, ("%s is not in ifnet array", ifp
->if_xname
));
3555 KASSERT(idx
== count
,
3556 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3557 ifp
->if_xname
, count
, idx
));
3562 const struct ifnet_array
*
3563 ifnet_array_get(void)
3565 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3570 ifnet_array_isempty(void)
3572 KASSERT(curthread
->td_type
== TD_TYPE_NETISR
, ("not in netisr"));
3573 if (ifnet_array
->ifnet_count
== 0)
3580 ifa_marker_init(struct ifaddr_marker
*mark
, struct ifnet
*ifp
)
3584 memset(mark
, 0, sizeof(*mark
));
3587 mark
->ifac
.ifa
= ifa
;
3589 ifa
->ifa_addr
= &mark
->addr
;
3590 ifa
->ifa_dstaddr
= &mark
->dstaddr
;
3591 ifa
->ifa_netmask
= &mark
->netmask
;