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/if_ringmap.h>
68 #include <net/ifq_var.h>
69 #include <net/radix.h>
70 #include <net/route.h>
71 #include <net/if_clone.h>
72 #include <net/netisr2.h>
73 #include <net/netmsg2.h>
75 #include <machine/atomic.h>
76 #include <machine/stdarg.h>
77 #include <machine/smp.h>
79 #if defined(INET) || defined(INET6)
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet6/in6_ifattach.h>
90 struct netmsg_ifaddr {
91 struct netmsg_base base;
97 struct ifsubq_stage_head {
98 TAILQ_HEAD(, ifsubq_stage) stg_head;
107 #define RINGMAP_FLAG_NONE 0x0
108 #define RINGMAP_FLAG_POWEROF2 0x1
111 * System initialization
113 static void if_attachdomain(void *);
114 static void if_attachdomain1(struct ifnet *);
115 static int ifconf(u_long, caddr_t, struct ucred *);
116 static void ifinit(void *);
117 static void ifnetinit(void *);
118 static void if_slowtimo(void *);
119 static void link_rtrequest(int, struct rtentry *);
120 static int if_rtdel(struct radix_node *, void *);
121 static void if_slowtimo_dispatch(netmsg_t);
123 /* Helper functions */
124 static void ifsq_watchdog_reset(struct ifsubq_watchdog *);
125 static int if_delmulti_serialized(struct ifnet *, struct sockaddr *);
126 static struct ifnet_array *ifnet_array_alloc(int);
127 static void ifnet_array_free(struct ifnet_array *);
128 static struct ifnet_array *ifnet_array_add(struct ifnet *,
129 const struct ifnet_array *);
130 static struct ifnet_array *ifnet_array_del(struct ifnet *,
131 const struct ifnet_array *);
135 * XXX: declare here to avoid to include many inet6 related files..
136 * should be more generalized?
138 extern void nd6_setmtu(struct ifnet *);
141 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
142 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
143 SYSCTL_NODE(_net_link, OID_AUTO, ringmap, CTLFLAG_RW, 0, "link ringmap");
145 static int ifsq_stage_cntmax = 4;
146 TUNABLE_INT("net.link.stage_cntmax", &ifsq_stage_cntmax);
147 SYSCTL_INT(_net_link, OID_AUTO, stage_cntmax, CTLFLAG_RW,
148 &ifsq_stage_cntmax, 0, "ifq staging packet count max");
150 static int if_stats_compat = 0;
151 SYSCTL_INT(_net_link, OID_AUTO, stats_compat, CTLFLAG_RW,
152 &if_stats_compat, 0, "Compat the old ifnet stats");
154 static int if_ringmap_dumprdr = 0;
155 SYSCTL_INT(_net_link_ringmap, OID_AUTO, dump_rdr, CTLFLAG_RW,
156 &if_ringmap_dumprdr, 0, "dump redirect table");
158 SYSINIT(interfaces, SI_SUB_PROTO_IF, SI_ORDER_FIRST, ifinit, NULL);
159 SYSINIT(ifnet, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, ifnetinit, NULL);
161 static if_com_alloc_t *if_com_alloc[256];
162 static if_com_free_t *if_com_free[256];
164 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
165 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
166 MALLOC_DEFINE(M_IFNET, "ifnet", "interface structure");
168 int ifqmaxlen = IFQ_MAXLEN;
169 struct ifnethead ifnet = TAILQ_HEAD_INITIALIZER(ifnet);
171 static struct ifnet_array ifnet_array0;
172 static struct ifnet_array *ifnet_array = &ifnet_array0;
174 static struct callout if_slowtimo_timer;
175 static struct netmsg_base if_slowtimo_netmsg;
178 struct ifnet **ifindex2ifnet = NULL;
179 static struct mtx ifnet_mtx = MTX_INITIALIZER("ifnet");
181 static struct ifsubq_stage_head ifsubq_stage_heads[MAXCPU];
184 #define IFQ_KTR_STRING "ifq=%p"
185 #define IFQ_KTR_ARGS struct ifaltq *ifq
187 #define KTR_IFQ KTR_ALL
189 KTR_INFO_MASTER(ifq);
190 KTR_INFO(KTR_IFQ, ifq, enqueue, 0, IFQ_KTR_STRING, IFQ_KTR_ARGS);
191 KTR_INFO(KTR_IFQ, ifq, dequeue, 1, IFQ_KTR_STRING, IFQ_KTR_ARGS);
192 #define logifq(name, arg) KTR_LOG(ifq_ ## name, arg)
194 #define IF_START_KTR_STRING "ifp=%p"
195 #define IF_START_KTR_ARGS struct ifnet *ifp
197 #define KTR_IF_START KTR_ALL
199 KTR_INFO_MASTER(if_start);
200 KTR_INFO(KTR_IF_START, if_start, run, 0,
201 IF_START_KTR_STRING, IF_START_KTR_ARGS);
202 KTR_INFO(KTR_IF_START, if_start, sched, 1,
203 IF_START_KTR_STRING, IF_START_KTR_ARGS);
204 KTR_INFO(KTR_IF_START, if_start, avoid, 2,
205 IF_START_KTR_STRING, IF_START_KTR_ARGS);
206 KTR_INFO(KTR_IF_START, if_start, contend_sched, 3,
207 IF_START_KTR_STRING, IF_START_KTR_ARGS);
208 KTR_INFO(KTR_IF_START, if_start, chase_sched, 4,
209 IF_START_KTR_STRING, IF_START_KTR_ARGS);
210 #define logifstart(name, arg) KTR_LOG(if_start_ ## name, arg)
213 TAILQ_HEAD(, ifg_group) ifg_head = TAILQ_HEAD_INITIALIZER(ifg_head);
216 * Network interface utility routines.
218 * Routines with ifa_ifwith* names take sockaddr *'s as
227 callout_init_mp(&if_slowtimo_timer);
228 netmsg_init(&if_slowtimo_netmsg, NULL, &netisr_adone_rport,
229 MSGF_PRIORITY, if_slowtimo_dispatch);
231 /* XXX is this necessary? */
233 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
234 if (ifp->if_snd.altq_maxlen == 0) {
235 if_printf(ifp, "XXX: driver didn't set altq_maxlen\n");
236 ifq_set_maxlen(&ifp->if_snd, ifqmaxlen);
241 /* Start if_slowtimo */
242 lwkt_sendmsg(netisr_cpuport(0), &if_slowtimo_netmsg.lmsg);
246 ifsq_ifstart_ipifunc(void *arg)
248 struct ifaltq_subque *ifsq = arg;
249 struct lwkt_msg *lmsg = ifsq_get_ifstart_lmsg(ifsq, mycpuid);
252 if (lmsg->ms_flags & MSGF_DONE)
253 lwkt_sendmsg_oncpu(netisr_cpuport(mycpuid), lmsg);
258 ifsq_stage_remove(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
260 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
261 TAILQ_REMOVE(&head->stg_head, stage, stg_link);
262 stage->stg_flags &= ~(IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED);
268 ifsq_stage_insert(struct ifsubq_stage_head *head, struct ifsubq_stage *stage)
270 KKASSERT((stage->stg_flags &
271 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
272 stage->stg_flags |= IFSQ_STAGE_FLAG_QUED;
273 TAILQ_INSERT_TAIL(&head->stg_head, stage, stg_link);
277 * Schedule ifnet.if_start on the subqueue owner CPU
280 ifsq_ifstart_schedule(struct ifaltq_subque *ifsq, int force)
284 if (!force && curthread->td_type == TD_TYPE_NETISR &&
285 ifsq_stage_cntmax > 0) {
286 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
290 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
291 ifsq_stage_insert(&ifsubq_stage_heads[mycpuid], stage);
292 stage->stg_flags |= IFSQ_STAGE_FLAG_SCHED;
296 cpu = ifsq_get_cpuid(ifsq);
298 lwkt_send_ipiq(globaldata_find(cpu), ifsq_ifstart_ipifunc, ifsq);
300 ifsq_ifstart_ipifunc(ifsq);
305 * This function will release ifnet.if_start subqueue interlock,
306 * if ifnet.if_start for the subqueue does not need to be scheduled
309 ifsq_ifstart_need_schedule(struct ifaltq_subque *ifsq, int running)
311 if (!running || ifsq_is_empty(ifsq)
313 || ifsq->ifsq_altq->altq_tbr != NULL
318 * ifnet.if_start subqueue interlock is released, if:
319 * 1) Hardware can not take any packets, due to
320 * o interface is marked down
321 * o hardware queue is full (ifsq_is_oactive)
322 * Under the second situation, hardware interrupt
323 * or polling(4) will call/schedule ifnet.if_start
324 * on the subqueue when hardware queue is ready
325 * 2) There is no packet in the subqueue.
326 * Further ifq_dispatch or ifq_handoff will call/
327 * schedule ifnet.if_start on the subqueue.
328 * 3) TBR is used and it does not allow further
330 * TBR callout will call ifnet.if_start on the
333 if (!running || !ifsq_data_ready(ifsq)) {
334 ifsq_clr_started(ifsq);
335 ALTQ_SQ_UNLOCK(ifsq);
338 ALTQ_SQ_UNLOCK(ifsq);
344 ifsq_ifstart_dispatch(netmsg_t msg)
346 struct lwkt_msg *lmsg = &msg->base.lmsg;
347 struct ifaltq_subque *ifsq = lmsg->u.ms_resultp;
348 struct ifnet *ifp = ifsq_get_ifp(ifsq);
349 struct globaldata *gd = mycpu;
350 int running = 0, need_sched;
354 lwkt_replymsg(lmsg, 0); /* reply ASAP */
356 if (gd->gd_cpuid != ifsq_get_cpuid(ifsq)) {
358 * We need to chase the subqueue owner CPU change.
360 ifsq_ifstart_schedule(ifsq, 1);
365 ifsq_serialize_hw(ifsq);
366 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
367 ifp->if_start(ifp, ifsq);
368 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
371 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
372 ifsq_deserialize_hw(ifsq);
376 * More data need to be transmitted, ifnet.if_start is
377 * scheduled on the subqueue owner CPU, and we keep going.
378 * NOTE: ifnet.if_start subqueue interlock is not released.
380 ifsq_ifstart_schedule(ifsq, 0);
386 /* Device driver ifnet.if_start helper function */
388 ifsq_devstart(struct ifaltq_subque *ifsq)
390 struct ifnet *ifp = ifsq_get_ifp(ifsq);
393 ASSERT_ALTQ_SQ_SERIALIZED_HW(ifsq);
396 if (ifsq_is_started(ifsq) || !ifsq_data_ready(ifsq)) {
397 ALTQ_SQ_UNLOCK(ifsq);
400 ifsq_set_started(ifsq);
401 ALTQ_SQ_UNLOCK(ifsq);
403 ifp->if_start(ifp, ifsq);
405 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
408 if (ifsq_ifstart_need_schedule(ifsq, running)) {
410 * More data need to be transmitted, ifnet.if_start is
411 * scheduled on ifnet's CPU, and we keep going.
412 * NOTE: ifnet.if_start interlock is not released.
414 ifsq_ifstart_schedule(ifsq, 0);
419 if_devstart(struct ifnet *ifp)
421 ifsq_devstart(ifq_get_subq_default(&ifp->if_snd));
424 /* Device driver ifnet.if_start schedule helper function */
426 ifsq_devstart_sched(struct ifaltq_subque *ifsq)
428 ifsq_ifstart_schedule(ifsq, 1);
432 if_devstart_sched(struct ifnet *ifp)
434 ifsq_devstart_sched(ifq_get_subq_default(&ifp->if_snd));
438 if_default_serialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
440 lwkt_serialize_enter(ifp->if_serializer);
444 if_default_deserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
446 lwkt_serialize_exit(ifp->if_serializer);
450 if_default_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz __unused)
452 return lwkt_serialize_try(ifp->if_serializer);
457 if_default_serialize_assert(struct ifnet *ifp,
458 enum ifnet_serialize slz __unused,
459 boolean_t serialized)
462 ASSERT_SERIALIZED(ifp->if_serializer);
464 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
469 * Attach an interface to the list of "active" interfaces.
471 * The serializer is optional.
474 if_attach(struct ifnet *ifp, lwkt_serialize_t serializer)
477 int namelen, masklen;
478 struct sockaddr_dl *sdl, *sdl_addr;
481 struct ifnet **old_ifindex2ifnet = NULL;
482 struct ifnet_array *old_ifnet_array;
485 static int if_indexlim = 8;
487 if (ifp->if_serialize != NULL) {
488 KASSERT(ifp->if_deserialize != NULL &&
489 ifp->if_tryserialize != NULL &&
490 ifp->if_serialize_assert != NULL,
491 ("serialize functions are partially setup"));
494 * If the device supplies serialize functions,
495 * then clear if_serializer to catch any invalid
496 * usage of this field.
498 KASSERT(serializer == NULL,
499 ("both serialize functions and default serializer "
501 ifp->if_serializer = NULL;
503 KASSERT(ifp->if_deserialize == NULL &&
504 ifp->if_tryserialize == NULL &&
505 ifp->if_serialize_assert == NULL,
506 ("serialize functions are partially setup"));
507 ifp->if_serialize = if_default_serialize;
508 ifp->if_deserialize = if_default_deserialize;
509 ifp->if_tryserialize = if_default_tryserialize;
511 ifp->if_serialize_assert = if_default_serialize_assert;
515 * The serializer can be passed in from the device,
516 * allowing the same serializer to be used for both
517 * the interrupt interlock and the device queue.
518 * If not specified, the netif structure will use an
519 * embedded serializer.
521 if (serializer == NULL) {
522 serializer = &ifp->if_default_serializer;
523 lwkt_serialize_init(serializer);
525 ifp->if_serializer = serializer;
530 * The old code would work if the interface passed a pre-existing
531 * chain of ifaddrs to this code. We don't trust our callers to
532 * properly initialize the tailq, however, so we no longer allow
533 * this unlikely case.
535 ifp->if_addrheads = kmalloc(ncpus * sizeof(struct ifaddrhead),
536 M_IFADDR, M_WAITOK | M_ZERO);
537 for (i = 0; i < ncpus; ++i)
538 TAILQ_INIT(&ifp->if_addrheads[i]);
540 TAILQ_INIT(&ifp->if_multiaddrs);
541 TAILQ_INIT(&ifp->if_groups);
542 getmicrotime(&ifp->if_lastchange);
545 * create a Link Level name for this device
547 namelen = strlen(ifp->if_xname);
548 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
549 socksize = masklen + ifp->if_addrlen;
550 if (socksize < sizeof(*sdl))
551 socksize = sizeof(*sdl);
552 socksize = RT_ROUNDUP(socksize);
553 ifa = ifa_create(sizeof(struct ifaddr) + 2 * socksize);
554 sdl = sdl_addr = (struct sockaddr_dl *)(ifa + 1);
555 sdl->sdl_len = socksize;
556 sdl->sdl_family = AF_LINK;
557 bcopy(ifp->if_xname, sdl->sdl_data, namelen);
558 sdl->sdl_nlen = namelen;
559 sdl->sdl_type = ifp->if_type;
560 ifp->if_lladdr = ifa;
562 ifa->ifa_rtrequest = link_rtrequest;
563 ifa->ifa_addr = (struct sockaddr *)sdl;
564 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
565 ifa->ifa_netmask = (struct sockaddr *)sdl;
566 sdl->sdl_len = masklen;
568 sdl->sdl_data[--namelen] = 0xff;
569 ifa_iflink(ifa, ifp, 0 /* Insert head */);
571 ifp->if_data_pcpu = kmalloc_cachealign(
572 ncpus * sizeof(struct ifdata_pcpu), M_DEVBUF, M_WAITOK | M_ZERO);
574 if (ifp->if_mapsubq == NULL)
575 ifp->if_mapsubq = ifq_mapsubq_default;
579 ifq->altq_disc = NULL;
580 ifq->altq_flags &= ALTQF_CANTCHANGE;
581 ifq->altq_tbr = NULL;
584 if (ifq->altq_subq_cnt <= 0)
585 ifq->altq_subq_cnt = 1;
586 ifq->altq_subq = kmalloc_cachealign(
587 ifq->altq_subq_cnt * sizeof(struct ifaltq_subque),
588 M_DEVBUF, M_WAITOK | M_ZERO);
590 if (ifq->altq_maxlen == 0) {
591 if_printf(ifp, "driver didn't set altq_maxlen\n");
592 ifq_set_maxlen(ifq, ifqmaxlen);
595 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
596 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
598 ALTQ_SQ_LOCK_INIT(ifsq);
599 ifsq->ifsq_index = q;
601 ifsq->ifsq_altq = ifq;
602 ifsq->ifsq_ifp = ifp;
604 ifsq->ifsq_maxlen = ifq->altq_maxlen;
605 ifsq->ifsq_maxbcnt = ifsq->ifsq_maxlen * MCLBYTES;
606 ifsq->ifsq_prepended = NULL;
607 ifsq->ifsq_started = 0;
608 ifsq->ifsq_hw_oactive = 0;
609 ifsq_set_cpuid(ifsq, 0);
610 if (ifp->if_serializer != NULL)
611 ifsq_set_hw_serialize(ifsq, ifp->if_serializer);
614 kmalloc_cachealign(ncpus * sizeof(struct ifsubq_stage),
615 M_DEVBUF, M_WAITOK | M_ZERO);
616 for (i = 0; i < ncpus; ++i)
617 ifsq->ifsq_stage[i].stg_subq = ifsq;
619 ifsq->ifsq_ifstart_nmsg =
620 kmalloc(ncpus * sizeof(struct netmsg_base),
621 M_LWKTMSG, M_WAITOK);
622 for (i = 0; i < ncpus; ++i) {
623 netmsg_init(&ifsq->ifsq_ifstart_nmsg[i], NULL,
624 &netisr_adone_rport, 0, ifsq_ifstart_dispatch);
625 ifsq->ifsq_ifstart_nmsg[i].lmsg.u.ms_resultp = ifsq;
628 ifq_set_classic(ifq);
631 * Increase mbuf cluster/jcluster limits for the mbufs that
632 * could sit on the device queues for quite some time.
634 if (ifp->if_nmbclusters > 0)
635 mcl_inclimit(ifp->if_nmbclusters);
636 if (ifp->if_nmbjclusters > 0)
637 mjcl_inclimit(ifp->if_nmbjclusters);
640 * Install this ifp into ifindex2inet, ifnet queue and ifnet
641 * array after it is setup.
643 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
644 * by ifnet lock, so that non-netisr threads could get a
649 /* Don't update if_index until ifindex2ifnet is setup */
650 ifp->if_index = if_index + 1;
651 sdl_addr->sdl_index = ifp->if_index;
654 * Install this ifp into ifindex2ifnet
656 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) {
664 n = if_indexlim * sizeof(*q);
665 q = kmalloc(n, M_IFADDR, M_WAITOK | M_ZERO);
666 if (ifindex2ifnet != NULL) {
667 bcopy(ifindex2ifnet, q, n/2);
668 /* Free old ifindex2ifnet after sync all netisrs */
669 old_ifindex2ifnet = ifindex2ifnet;
673 ifindex2ifnet[ifp->if_index] = ifp;
675 * Update if_index after this ifp is installed into ifindex2ifnet,
676 * so that netisrs could get a consistent view of ifindex2ifnet.
679 if_index = ifp->if_index;
682 * Install this ifp into ifnet array.
684 /* Free old ifnet array after sync all netisrs */
685 old_ifnet_array = ifnet_array;
686 ifnet_array = ifnet_array_add(ifp, old_ifnet_array);
689 * Install this ifp into ifnet queue.
691 TAILQ_INSERT_TAIL(&ifnetlist, ifp, if_link);
696 * Sync all netisrs so that the old ifindex2ifnet and ifnet array
697 * are no longer accessed and we can free them safely later on.
699 netmsg_service_sync();
700 if (old_ifindex2ifnet != NULL)
701 kfree(old_ifindex2ifnet, M_IFADDR);
702 ifnet_array_free(old_ifnet_array);
704 if (!SLIST_EMPTY(&domains))
705 if_attachdomain1(ifp);
707 /* Announce the interface. */
708 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
709 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL);
710 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
714 if_attachdomain(void *dummy)
719 TAILQ_FOREACH(ifp, &ifnetlist, if_list)
720 if_attachdomain1(ifp);
723 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST,
724 if_attachdomain, NULL);
727 if_attachdomain1(struct ifnet *ifp)
733 /* address family dependent data region */
734 bzero(ifp->if_afdata, sizeof(ifp->if_afdata));
735 SLIST_FOREACH(dp, &domains, dom_next)
736 if (dp->dom_ifattach)
737 ifp->if_afdata[dp->dom_family] =
738 (*dp->dom_ifattach)(ifp);
743 * Purge all addresses whose type is _not_ AF_LINK
746 if_purgeaddrs_nolink_dispatch(netmsg_t nmsg)
748 struct lwkt_msg *lmsg = &nmsg->lmsg;
749 struct ifnet *ifp = lmsg->u.ms_resultp;
750 struct ifaddr_container *ifac, *next;
755 * The ifaddr processing in the following loop will block,
756 * however, this function is called in netisr0, in which
757 * ifaddr list changes happen, so we don't care about the
758 * blockness of the ifaddr processing here.
760 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
762 struct ifaddr *ifa = ifac->ifa;
765 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
768 /* Leave link ifaddr as it is */
769 if (ifa->ifa_addr->sa_family == AF_LINK)
772 /* XXX: Ugly!! ad hoc just for INET */
773 if (ifa->ifa_addr->sa_family == AF_INET) {
774 struct ifaliasreq ifr;
775 struct sockaddr_in saved_addr, saved_dst;
776 #ifdef IFADDR_DEBUG_VERBOSE
779 kprintf("purge in4 addr %p: ", ifa);
780 for (i = 0; i < ncpus; ++i) {
782 ifa->ifa_containers[i].ifa_refcnt);
787 /* Save information for panic. */
788 memcpy(&saved_addr, ifa->ifa_addr, sizeof(saved_addr));
789 if (ifa->ifa_dstaddr != NULL) {
790 memcpy(&saved_dst, ifa->ifa_dstaddr,
793 memset(&saved_dst, 0, sizeof(saved_dst));
796 bzero(&ifr, sizeof ifr);
797 ifr.ifra_addr = *ifa->ifa_addr;
798 if (ifa->ifa_dstaddr)
799 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
800 if (in_control(SIOCDIFADDR, (caddr_t)&ifr, ifp,
804 /* MUST NOT HAPPEN */
805 panic("%s: in_control failed %x, dst %x", ifp->if_xname,
806 ntohl(saved_addr.sin_addr.s_addr),
807 ntohl(saved_dst.sin_addr.s_addr));
811 if (ifa->ifa_addr->sa_family == AF_INET6) {
812 #ifdef IFADDR_DEBUG_VERBOSE
815 kprintf("purge in6 addr %p: ", ifa);
816 for (i = 0; i < ncpus; ++i) {
818 ifa->ifa_containers[i].ifa_refcnt);
824 /* ifp_addrhead is already updated */
828 if_printf(ifp, "destrot ifaddr family %d\n",
829 ifa->ifa_addr->sa_family);
830 ifa_ifunlink(ifa, ifp);
834 lwkt_replymsg(lmsg, 0);
838 if_purgeaddrs_nolink(struct ifnet *ifp)
840 struct netmsg_base nmsg;
841 struct lwkt_msg *lmsg = &nmsg.lmsg;
843 ASSERT_CANDOMSG_NETISR0(curthread);
845 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
846 if_purgeaddrs_nolink_dispatch);
847 lmsg->u.ms_resultp = ifp;
848 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
852 ifq_stage_detach_handler(netmsg_t nmsg)
854 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
857 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
858 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
859 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
861 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
862 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
864 lwkt_replymsg(&nmsg->lmsg, 0);
868 ifq_stage_detach(struct ifaltq *ifq)
870 struct netmsg_base base;
873 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
874 ifq_stage_detach_handler);
875 base.lmsg.u.ms_resultp = ifq;
877 for (cpu = 0; cpu < ncpus; ++cpu)
878 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
881 struct netmsg_if_rtdel {
882 struct netmsg_base base;
887 if_rtdel_dispatch(netmsg_t msg)
889 struct netmsg_if_rtdel *rmsg = (void *)msg;
893 for (i = 1; i <= AF_MAX; i++) {
894 struct radix_node_head *rnh;
896 if ((rnh = rt_tables[cpu][i]) == NULL)
898 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
903 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
905 lwkt_replymsg(&rmsg->base.lmsg, 0);
909 * Detach an interface, removing it from the
910 * list of "active" interfaces.
913 if_detach(struct ifnet *ifp)
915 struct ifnet_array *old_ifnet_array;
916 struct netmsg_if_rtdel msg;
920 /* Announce that the interface is gone. */
921 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
922 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
923 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
926 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
927 * array before it is whacked.
929 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
930 * by ifnet lock, so that non-netisr threads could get a
936 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
938 ifindex2ifnet[ifp->if_index] = NULL;
939 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
943 * Remove this ifp from ifnet queue.
945 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
948 * Remove this ifp from ifnet array.
950 /* Free old ifnet array after sync all netisrs */
951 old_ifnet_array = ifnet_array;
952 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
957 * Sync all netisrs so that the old ifnet array is no longer
958 * accessed and we can free it safely later on.
960 netmsg_service_sync();
961 ifnet_array_free(old_ifnet_array);
964 * Remove routes and flush queues.
968 if (ifp->if_flags & IFF_NPOLLING)
969 ifpoll_deregister(ifp);
973 /* Decrease the mbuf clusters/jclusters limits increased by us */
974 if (ifp->if_nmbclusters > 0)
975 mcl_inclimit(-ifp->if_nmbclusters);
976 if (ifp->if_nmbjclusters > 0)
977 mjcl_inclimit(-ifp->if_nmbjclusters);
980 if (ifq_is_enabled(&ifp->if_snd))
981 altq_disable(&ifp->if_snd);
982 if (ifq_is_attached(&ifp->if_snd))
983 altq_detach(&ifp->if_snd);
987 * Clean up all addresses.
989 ifp->if_lladdr = NULL;
991 if_purgeaddrs_nolink(ifp);
992 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
995 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
996 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
997 ("non-link ifaddr is left on if_addrheads"));
999 ifa_ifunlink(ifa, ifp);
1001 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1002 ("there are still ifaddrs left on if_addrheads"));
1007 * Remove all IPv4 kernel structures related to ifp.
1014 * Remove all IPv6 kernel structs related to ifp. This should be done
1015 * before removing routing entries below, since IPv6 interface direct
1016 * routes are expected to be removed by the IPv6-specific kernel API.
1017 * Otherwise, the kernel will detect some inconsistency and bark it.
1023 * Delete all remaining routes using this interface
1025 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1028 rt_domsg_global(&msg.base);
1030 SLIST_FOREACH(dp, &domains, dom_next)
1031 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1032 (*dp->dom_ifdetach)(ifp,
1033 ifp->if_afdata[dp->dom_family]);
1035 kfree(ifp->if_addrheads, M_IFADDR);
1037 lwkt_synchronize_ipiqs("if_detach");
1038 ifq_stage_detach(&ifp->if_snd);
1040 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1041 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1043 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1044 kfree(ifsq->ifsq_stage, M_DEVBUF);
1046 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1048 kfree(ifp->if_data_pcpu, M_DEVBUF);
1054 * Create interface group without members
1057 if_creategroup(const char *groupname)
1059 struct ifg_group *ifg = NULL;
1061 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1062 M_TEMP, M_NOWAIT)) == NULL)
1065 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1066 ifg->ifg_refcnt = 0;
1067 ifg->ifg_carp_demoted = 0;
1068 TAILQ_INIT(&ifg->ifg_members);
1070 pfi_attach_ifgroup(ifg);
1072 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1078 * Add a group to an interface
1081 if_addgroup(struct ifnet *ifp, const char *groupname)
1083 struct ifg_list *ifgl;
1084 struct ifg_group *ifg = NULL;
1085 struct ifg_member *ifgm;
1087 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1088 groupname[strlen(groupname) - 1] <= '9')
1091 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1092 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1095 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1098 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1099 kfree(ifgl, M_TEMP);
1103 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1104 if (!strcmp(ifg->ifg_group, groupname))
1107 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1108 kfree(ifgl, M_TEMP);
1109 kfree(ifgm, M_TEMP);
1114 ifgl->ifgl_group = ifg;
1115 ifgm->ifgm_ifp = ifp;
1117 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1118 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1121 pfi_group_change(groupname);
1128 * Remove a group from an interface
1131 if_delgroup(struct ifnet *ifp, const char *groupname)
1133 struct ifg_list *ifgl;
1134 struct ifg_member *ifgm;
1136 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1137 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1142 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1144 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1145 if (ifgm->ifgm_ifp == ifp)
1149 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1150 kfree(ifgm, M_TEMP);
1153 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1154 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1156 pfi_detach_ifgroup(ifgl->ifgl_group);
1158 kfree(ifgl->ifgl_group, M_TEMP);
1161 kfree(ifgl, M_TEMP);
1164 pfi_group_change(groupname);
1171 * Stores all groups from an interface in memory pointed
1175 if_getgroup(caddr_t data, struct ifnet *ifp)
1178 struct ifg_list *ifgl;
1179 struct ifg_req ifgrq, *ifgp;
1180 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1182 if (ifgr->ifgr_len == 0) {
1183 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1184 ifgr->ifgr_len += sizeof(struct ifg_req);
1188 len = ifgr->ifgr_len;
1189 ifgp = ifgr->ifgr_groups;
1190 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1191 if (len < sizeof(ifgrq))
1193 bzero(&ifgrq, sizeof ifgrq);
1194 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1195 sizeof(ifgrq.ifgrq_group));
1196 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1197 sizeof(struct ifg_req))))
1199 len -= sizeof(ifgrq);
1207 * Stores all members of a group in memory pointed to by data
1210 if_getgroupmembers(caddr_t data)
1212 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1213 struct ifg_group *ifg;
1214 struct ifg_member *ifgm;
1215 struct ifg_req ifgrq, *ifgp;
1218 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1219 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1224 if (ifgr->ifgr_len == 0) {
1225 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1226 ifgr->ifgr_len += sizeof(ifgrq);
1230 len = ifgr->ifgr_len;
1231 ifgp = ifgr->ifgr_groups;
1232 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1233 if (len < sizeof(ifgrq))
1235 bzero(&ifgrq, sizeof ifgrq);
1236 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1237 sizeof(ifgrq.ifgrq_member));
1238 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1239 sizeof(struct ifg_req))))
1241 len -= sizeof(ifgrq);
1249 * Delete Routes for a Network Interface
1251 * Called for each routing entry via the rnh->rnh_walktree() call above
1252 * to delete all route entries referencing a detaching network interface.
1255 * rn pointer to node in the routing table
1256 * arg argument passed to rnh->rnh_walktree() - detaching interface
1260 * errno failed - reason indicated
1264 if_rtdel(struct radix_node *rn, void *arg)
1266 struct rtentry *rt = (struct rtentry *)rn;
1267 struct ifnet *ifp = arg;
1270 if (rt->rt_ifp == ifp) {
1273 * Protect (sorta) against walktree recursion problems
1274 * with cloned routes
1276 if (!(rt->rt_flags & RTF_UP))
1279 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1280 rt_mask(rt), rt->rt_flags,
1283 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1290 static __inline boolean_t
1291 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1293 if (old_ifa == NULL)
1296 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1297 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1299 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1300 (cur_ifa->ifa_flags & IFA_ROUTE))
1306 * Locate an interface based on a complete address.
1309 ifa_ifwithaddr(struct sockaddr *addr)
1311 const struct ifnet_array *arr;
1314 arr = ifnet_array_get();
1315 for (i = 0; i < arr->ifnet_count; ++i) {
1316 struct ifnet *ifp = arr->ifnet_arr[i];
1317 struct ifaddr_container *ifac;
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 (sa_equal(addr, ifa->ifa_addr))
1326 if ((ifp->if_flags & IFF_BROADCAST) &&
1327 ifa->ifa_broadaddr &&
1328 /* IPv6 doesn't have broadcast */
1329 ifa->ifa_broadaddr->sa_len != 0 &&
1330 sa_equal(ifa->ifa_broadaddr, addr))
1338 * Locate the point to point interface with a given destination address.
1341 ifa_ifwithdstaddr(struct sockaddr *addr)
1343 const struct ifnet_array *arr;
1346 arr = ifnet_array_get();
1347 for (i = 0; i < arr->ifnet_count; ++i) {
1348 struct ifnet *ifp = arr->ifnet_arr[i];
1349 struct ifaddr_container *ifac;
1351 if (!(ifp->if_flags & IFF_POINTOPOINT))
1354 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1355 struct ifaddr *ifa = ifac->ifa;
1357 if (ifa->ifa_addr->sa_family != addr->sa_family)
1359 if (ifa->ifa_dstaddr &&
1360 sa_equal(addr, ifa->ifa_dstaddr))
1368 * Find an interface on a specific network. If many, choice
1369 * is most specific found.
1372 ifa_ifwithnet(struct sockaddr *addr)
1374 struct ifaddr *ifa_maybe = NULL;
1375 u_int af = addr->sa_family;
1376 char *addr_data = addr->sa_data, *cplim;
1377 const struct ifnet_array *arr;
1381 * AF_LINK addresses can be looked up directly by their index number,
1382 * so do that if we can.
1384 if (af == AF_LINK) {
1385 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1387 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1388 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1392 * Scan though each interface, looking for ones that have
1393 * addresses in this address family.
1395 arr = ifnet_array_get();
1396 for (i = 0; i < arr->ifnet_count; ++i) {
1397 struct ifnet *ifp = arr->ifnet_arr[i];
1398 struct ifaddr_container *ifac;
1400 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1401 struct ifaddr *ifa = ifac->ifa;
1402 char *cp, *cp2, *cp3;
1404 if (ifa->ifa_addr->sa_family != af)
1406 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1408 * This is a bit broken as it doesn't
1409 * take into account that the remote end may
1410 * be a single node in the network we are
1412 * The trouble is that we don't know the
1413 * netmask for the remote end.
1415 if (ifa->ifa_dstaddr != NULL &&
1416 sa_equal(addr, ifa->ifa_dstaddr))
1420 * if we have a special address handler,
1421 * then use it instead of the generic one.
1423 if (ifa->ifa_claim_addr) {
1424 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1432 * Scan all the bits in the ifa's address.
1433 * If a bit dissagrees with what we are
1434 * looking for, mask it with the netmask
1435 * to see if it really matters.
1436 * (A byte at a time)
1438 if (ifa->ifa_netmask == 0)
1441 cp2 = ifa->ifa_addr->sa_data;
1442 cp3 = ifa->ifa_netmask->sa_data;
1443 cplim = ifa->ifa_netmask->sa_len +
1444 (char *)ifa->ifa_netmask;
1446 if ((*cp++ ^ *cp2++) & *cp3++)
1447 goto next; /* next address! */
1449 * If the netmask of what we just found
1450 * is more specific than what we had before
1451 * (if we had one) then remember the new one
1452 * before continuing to search for an even
1453 * better one. If the netmasks are equal,
1454 * we prefer the this ifa based on the result
1457 if (ifa_maybe == NULL ||
1458 rn_refines((char *)ifa->ifa_netmask,
1459 (char *)ifa_maybe->ifa_netmask) ||
1460 (sa_equal(ifa_maybe->ifa_netmask,
1461 ifa->ifa_netmask) &&
1462 ifa_prefer(ifa, ifa_maybe)))
1471 * Find an interface address specific to an interface best matching
1475 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1477 struct ifaddr_container *ifac;
1478 char *cp, *cp2, *cp3;
1480 struct ifaddr *ifa_maybe = NULL;
1481 u_int af = addr->sa_family;
1485 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1486 struct ifaddr *ifa = ifac->ifa;
1488 if (ifa->ifa_addr->sa_family != af)
1490 if (ifa_maybe == NULL)
1492 if (ifa->ifa_netmask == NULL) {
1493 if (sa_equal(addr, ifa->ifa_addr) ||
1494 (ifa->ifa_dstaddr != NULL &&
1495 sa_equal(addr, ifa->ifa_dstaddr)))
1499 if (ifp->if_flags & IFF_POINTOPOINT) {
1500 if (sa_equal(addr, ifa->ifa_dstaddr))
1504 cp2 = ifa->ifa_addr->sa_data;
1505 cp3 = ifa->ifa_netmask->sa_data;
1506 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1507 for (; cp3 < cplim; cp3++)
1508 if ((*cp++ ^ *cp2++) & *cp3)
1518 * Default action when installing a route with a Link Level gateway.
1519 * Lookup an appropriate real ifa to point to.
1520 * This should be moved to /sys/net/link.c eventually.
1523 link_rtrequest(int cmd, struct rtentry *rt)
1526 struct sockaddr *dst;
1529 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1530 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1532 ifa = ifaof_ifpforaddr(dst, ifp);
1534 IFAFREE(rt->rt_ifa);
1537 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1538 ifa->ifa_rtrequest(cmd, rt);
1542 struct netmsg_ifroute {
1543 struct netmsg_base base;
1550 * Mark an interface down and notify protocols of the transition.
1553 if_unroute_dispatch(netmsg_t nmsg)
1555 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1556 struct ifnet *ifp = msg->ifp;
1557 int flag = msg->flag, fam = msg->fam;
1558 struct ifaddr_container *ifac;
1560 ifp->if_flags &= ~flag;
1561 getmicrotime(&ifp->if_lastchange);
1563 * The ifaddr processing in the following loop will block,
1564 * however, this function is called in netisr0, in which
1565 * ifaddr list changes happen, so we don't care about the
1566 * blockness of the ifaddr processing here.
1568 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1569 struct ifaddr *ifa = ifac->ifa;
1572 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1575 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1576 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1578 ifq_purge_all(&ifp->if_snd);
1581 lwkt_replymsg(&nmsg->lmsg, 0);
1585 if_unroute(struct ifnet *ifp, int flag, int fam)
1587 struct netmsg_ifroute msg;
1589 ASSERT_CANDOMSG_NETISR0(curthread);
1591 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1592 if_unroute_dispatch);
1596 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1600 * Mark an interface up and notify protocols of the transition.
1603 if_route_dispatch(netmsg_t nmsg)
1605 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1606 struct ifnet *ifp = msg->ifp;
1607 int flag = msg->flag, fam = msg->fam;
1608 struct ifaddr_container *ifac;
1610 ifq_purge_all(&ifp->if_snd);
1611 ifp->if_flags |= flag;
1612 getmicrotime(&ifp->if_lastchange);
1614 * The ifaddr processing in the following loop will block,
1615 * however, this function is called in netisr0, in which
1616 * ifaddr list changes happen, so we don't care about the
1617 * blockness of the ifaddr processing here.
1619 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1620 struct ifaddr *ifa = ifac->ifa;
1623 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1626 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1627 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1634 lwkt_replymsg(&nmsg->lmsg, 0);
1638 if_route(struct ifnet *ifp, int flag, int fam)
1640 struct netmsg_ifroute msg;
1642 ASSERT_CANDOMSG_NETISR0(curthread);
1644 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1649 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1653 * Mark an interface down and notify protocols of the transition. An
1654 * interface going down is also considered to be a synchronizing event.
1655 * We must ensure that all packet processing related to the interface
1656 * has completed before we return so e.g. the caller can free the ifnet
1657 * structure that the mbufs may be referencing.
1659 * NOTE: must be called at splnet or eqivalent.
1662 if_down(struct ifnet *ifp)
1664 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1665 netmsg_service_sync();
1669 * Mark an interface up and notify protocols of
1671 * NOTE: must be called at splnet or eqivalent.
1674 if_up(struct ifnet *ifp)
1676 if_route(ifp, IFF_UP, AF_UNSPEC);
1680 * Process a link state change.
1681 * NOTE: must be called at splsoftnet or equivalent.
1684 if_link_state_change(struct ifnet *ifp)
1686 int link_state = ifp->if_link_state;
1689 devctl_notify("IFNET", ifp->if_xname,
1690 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1694 * Handle interface watchdog timer routines. Called
1695 * from softclock, we decrement timers (if set) and
1696 * call the appropriate interface routine on expiration.
1699 if_slowtimo_dispatch(netmsg_t nmsg)
1701 struct globaldata *gd = mycpu;
1702 const struct ifnet_array *arr;
1705 ASSERT_IN_NETISR(0);
1708 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1711 arr = ifnet_array_get();
1712 for (i = 0; i < arr->ifnet_count; ++i) {
1713 struct ifnet *ifp = arr->ifnet_arr[i];
1717 if (if_stats_compat) {
1718 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1719 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1720 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1721 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1722 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1723 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1724 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1725 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1726 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1727 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1728 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1729 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1732 if (ifp->if_timer == 0 || --ifp->if_timer) {
1736 if (ifp->if_watchdog) {
1737 if (ifnet_tryserialize_all(ifp)) {
1738 (*ifp->if_watchdog)(ifp);
1739 ifnet_deserialize_all(ifp);
1741 /* try again next timeout */
1749 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1753 if_slowtimo(void *arg __unused)
1755 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1757 KASSERT(mycpuid == 0, ("not on cpu0"));
1759 if (lmsg->ms_flags & MSGF_DONE)
1760 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1765 * Map interface name to
1766 * interface structure pointer.
1769 ifunit(const char *name)
1774 * Search all the interfaces for this name/number
1776 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1778 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1779 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1786 ifunit_netisr(const char *name)
1788 const struct ifnet_array *arr;
1792 * Search all the interfaces for this name/number
1795 arr = ifnet_array_get();
1796 for (i = 0; i < arr->ifnet_count; ++i) {
1797 struct ifnet *ifp = arr->ifnet_arr[i];
1799 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1809 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1814 int error, do_ifup = 0;
1817 size_t namelen, onamelen;
1818 char new_name[IFNAMSIZ];
1820 struct sockaddr_dl *sdl;
1825 return (ifconf(cmd, data, cred));
1830 ifr = (struct ifreq *)data;
1835 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1837 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1838 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1840 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1842 return (if_clone_destroy(ifr->ifr_name));
1843 case SIOCIFGCLONERS:
1844 return (if_clone_list((struct if_clonereq *)data));
1850 * Nominal ioctl through interface, lookup the ifp and obtain a
1851 * lock to serialize the ifconfig ioctl operation.
1855 ifp = ifunit(ifr->ifr_name);
1864 ifr->ifr_index = ifp->if_index;
1868 ifr->ifr_flags = ifp->if_flags;
1869 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1873 ifr->ifr_reqcap = ifp->if_capabilities;
1874 ifr->ifr_curcap = ifp->if_capenable;
1878 ifr->ifr_metric = ifp->if_metric;
1882 ifr->ifr_mtu = ifp->if_mtu;
1886 ifr->ifr_tsolen = ifp->if_tsolen;
1890 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1891 sizeof(ifp->if_data));
1895 ifr->ifr_phys = ifp->if_physical;
1898 case SIOCGIFPOLLCPU:
1899 ifr->ifr_pollcpu = -1;
1902 case SIOCSIFPOLLCPU:
1906 error = priv_check_cred(cred, PRIV_ROOT, 0);
1909 new_flags = (ifr->ifr_flags & 0xffff) |
1910 (ifr->ifr_flagshigh << 16);
1911 if (ifp->if_flags & IFF_SMART) {
1912 /* Smart drivers twiddle their own routes */
1913 } else if (ifp->if_flags & IFF_UP &&
1914 (new_flags & IFF_UP) == 0) {
1916 } else if (new_flags & IFF_UP &&
1917 (ifp->if_flags & IFF_UP) == 0) {
1921 #ifdef IFPOLL_ENABLE
1922 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1923 if (new_flags & IFF_NPOLLING)
1924 ifpoll_register(ifp);
1926 ifpoll_deregister(ifp);
1930 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1931 (new_flags &~ IFF_CANTCHANGE);
1932 if (new_flags & IFF_PPROMISC) {
1933 /* Permanently promiscuous mode requested */
1934 ifp->if_flags |= IFF_PROMISC;
1935 } else if (ifp->if_pcount == 0) {
1936 ifp->if_flags &= ~IFF_PROMISC;
1938 if (ifp->if_ioctl) {
1939 ifnet_serialize_all(ifp);
1940 ifp->if_ioctl(ifp, cmd, data, cred);
1941 ifnet_deserialize_all(ifp);
1945 getmicrotime(&ifp->if_lastchange);
1949 error = priv_check_cred(cred, PRIV_ROOT, 0);
1952 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1956 ifnet_serialize_all(ifp);
1957 ifp->if_ioctl(ifp, cmd, data, cred);
1958 ifnet_deserialize_all(ifp);
1962 error = priv_check_cred(cred, PRIV_ROOT, 0);
1965 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1968 if (new_name[0] == '\0') {
1972 if (ifunit(new_name) != NULL) {
1977 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1979 /* Announce the departure of the interface. */
1980 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1982 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1983 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1984 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1985 namelen = strlen(new_name);
1986 onamelen = sdl->sdl_nlen;
1988 * Move the address if needed. This is safe because we
1989 * allocate space for a name of length IFNAMSIZ when we
1990 * create this in if_attach().
1992 if (namelen != onamelen) {
1993 bcopy(sdl->sdl_data + onamelen,
1994 sdl->sdl_data + namelen, sdl->sdl_alen);
1996 bcopy(new_name, sdl->sdl_data, namelen);
1997 sdl->sdl_nlen = namelen;
1998 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1999 bzero(sdl->sdl_data, onamelen);
2000 while (namelen != 0)
2001 sdl->sdl_data[--namelen] = 0xff;
2003 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2005 /* Announce the return of the interface. */
2006 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2010 error = priv_check_cred(cred, PRIV_ROOT, 0);
2013 ifp->if_metric = ifr->ifr_metric;
2014 getmicrotime(&ifp->if_lastchange);
2018 error = priv_check_cred(cred, PRIV_ROOT, 0);
2021 if (ifp->if_ioctl == NULL) {
2025 ifnet_serialize_all(ifp);
2026 error = ifp->if_ioctl(ifp, cmd, data, cred);
2027 ifnet_deserialize_all(ifp);
2029 getmicrotime(&ifp->if_lastchange);
2034 u_long oldmtu = ifp->if_mtu;
2036 error = priv_check_cred(cred, PRIV_ROOT, 0);
2039 if (ifp->if_ioctl == NULL) {
2043 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2047 ifnet_serialize_all(ifp);
2048 error = ifp->if_ioctl(ifp, cmd, data, cred);
2049 ifnet_deserialize_all(ifp);
2051 getmicrotime(&ifp->if_lastchange);
2055 * If the link MTU changed, do network layer specific procedure.
2057 if (ifp->if_mtu != oldmtu) {
2066 error = priv_check_cred(cred, PRIV_ROOT, 0);
2070 /* XXX need driver supplied upper limit */
2071 if (ifr->ifr_tsolen <= 0) {
2075 ifp->if_tsolen = ifr->ifr_tsolen;
2080 error = priv_check_cred(cred, PRIV_ROOT, 0);
2084 /* Don't allow group membership on non-multicast interfaces. */
2085 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2090 /* Don't let users screw up protocols' entries. */
2091 if (ifr->ifr_addr.sa_family != AF_LINK) {
2096 if (cmd == SIOCADDMULTI) {
2097 struct ifmultiaddr *ifma;
2098 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2100 error = if_delmulti(ifp, &ifr->ifr_addr);
2103 getmicrotime(&ifp->if_lastchange);
2106 case SIOCSIFPHYADDR:
2107 case SIOCDIFPHYADDR:
2109 case SIOCSIFPHYADDR_IN6:
2111 case SIOCSLIFPHYADDR:
2113 case SIOCSIFGENERIC:
2114 error = priv_check_cred(cred, PRIV_ROOT, 0);
2117 if (ifp->if_ioctl == 0) {
2121 ifnet_serialize_all(ifp);
2122 error = ifp->if_ioctl(ifp, cmd, data, cred);
2123 ifnet_deserialize_all(ifp);
2125 getmicrotime(&ifp->if_lastchange);
2129 ifs = (struct ifstat *)data;
2130 ifs->ascii[0] = '\0';
2132 case SIOCGIFPSRCADDR:
2133 case SIOCGIFPDSTADDR:
2134 case SIOCGLIFPHYADDR:
2136 case SIOCGIFGENERIC:
2137 if (ifp->if_ioctl == NULL) {
2141 ifnet_serialize_all(ifp);
2142 error = ifp->if_ioctl(ifp, cmd, data, cred);
2143 ifnet_deserialize_all(ifp);
2147 error = priv_check_cred(cred, PRIV_ROOT, 0);
2150 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2151 ifr->ifr_addr.sa_len);
2152 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2156 oif_flags = ifp->if_flags;
2157 if (so->so_proto == 0) {
2161 error = so_pru_control_direct(so, cmd, data, ifp);
2163 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2165 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2166 if (ifp->if_flags & IFF_UP) {
2181 * Set/clear promiscuous mode on interface ifp based on the truth value
2182 * of pswitch. The calls are reference counted so that only the first
2183 * "on" request actually has an effect, as does the final "off" request.
2184 * Results are undefined if the "off" and "on" requests are not matched.
2187 ifpromisc(struct ifnet *ifp, int pswitch)
2193 oldflags = ifp->if_flags;
2194 if (ifp->if_flags & IFF_PPROMISC) {
2195 /* Do nothing if device is in permanently promiscuous mode */
2196 ifp->if_pcount += pswitch ? 1 : -1;
2201 * If the device is not configured up, we cannot put it in
2204 if ((ifp->if_flags & IFF_UP) == 0)
2206 if (ifp->if_pcount++ != 0)
2208 ifp->if_flags |= IFF_PROMISC;
2209 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2212 if (--ifp->if_pcount > 0)
2214 ifp->if_flags &= ~IFF_PROMISC;
2215 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2218 ifr.ifr_flags = ifp->if_flags;
2219 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2220 ifnet_serialize_all(ifp);
2221 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2222 ifnet_deserialize_all(ifp);
2226 ifp->if_flags = oldflags;
2231 * Return interface configuration
2232 * of system. List may be used
2233 * in later ioctl's (above) to get
2234 * other information.
2237 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2239 struct ifconf *ifc = (struct ifconf *)data;
2241 struct sockaddr *sa;
2242 struct ifreq ifr, *ifrp;
2243 int space = ifc->ifc_len, error = 0;
2245 ifrp = ifc->ifc_req;
2248 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2249 struct ifaddr_container *ifac, *ifac_mark;
2250 struct ifaddr_marker mark;
2251 struct ifaddrhead *head;
2254 if (space <= sizeof ifr)
2258 * Zero the stack declared structure first to prevent
2259 * memory disclosure.
2261 bzero(&ifr, sizeof(ifr));
2262 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2263 >= sizeof(ifr.ifr_name)) {
2264 error = ENAMETOOLONG;
2269 * Add a marker, since copyout() could block and during that
2270 * period the list could be changed. Inserting the marker to
2271 * the header of the list will not cause trouble for the code
2272 * assuming that the first element of the list is AF_LINK; the
2273 * marker will be moved to the next position w/o blocking.
2275 ifa_marker_init(&mark, ifp);
2276 ifac_mark = &mark.ifac;
2277 head = &ifp->if_addrheads[mycpuid];
2280 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2281 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2282 struct ifaddr *ifa = ifac->ifa;
2284 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2285 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2288 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2291 if (space <= sizeof ifr)
2294 if (cred->cr_prison &&
2295 prison_if(cred, sa))
2299 * Keep a reference on this ifaddr, so that it will
2300 * not be destroyed when its address is copied to
2301 * the userland, which could block.
2304 if (sa->sa_len <= sizeof(*sa)) {
2306 error = copyout(&ifr, ifrp, sizeof ifr);
2309 if (space < (sizeof ifr) + sa->sa_len -
2314 space -= sa->sa_len - sizeof(*sa);
2315 error = copyout(&ifr, ifrp,
2316 sizeof ifr.ifr_name);
2318 error = copyout(sa, &ifrp->ifr_addr,
2320 ifrp = (struct ifreq *)
2321 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2326 space -= sizeof ifr;
2328 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2332 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2333 error = copyout(&ifr, ifrp, sizeof ifr);
2336 space -= sizeof ifr;
2342 ifc->ifc_len -= space;
2347 * Just like if_promisc(), but for all-multicast-reception mode.
2350 if_allmulti(struct ifnet *ifp, int onswitch)
2358 if (ifp->if_amcount++ == 0) {
2359 ifp->if_flags |= IFF_ALLMULTI;
2360 ifr.ifr_flags = ifp->if_flags;
2361 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2362 ifnet_serialize_all(ifp);
2363 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2365 ifnet_deserialize_all(ifp);
2368 if (ifp->if_amcount > 1) {
2371 ifp->if_amcount = 0;
2372 ifp->if_flags &= ~IFF_ALLMULTI;
2373 ifr.ifr_flags = ifp->if_flags;
2374 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2375 ifnet_serialize_all(ifp);
2376 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2378 ifnet_deserialize_all(ifp);
2390 * Add a multicast listenership to the interface in question.
2391 * The link layer provides a routine which converts
2394 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2395 struct ifmultiaddr **retifma)
2397 struct sockaddr *llsa, *dupsa;
2399 struct ifmultiaddr *ifma;
2401 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2404 * If the matching multicast address already exists
2405 * then don't add a new one, just add a reference
2407 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2408 if (sa_equal(sa, ifma->ifma_addr)) {
2409 ifma->ifma_refcount++;
2417 * Give the link layer a chance to accept/reject it, and also
2418 * find out which AF_LINK address this maps to, if it isn't one
2421 if (ifp->if_resolvemulti) {
2422 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2429 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2430 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2431 bcopy(sa, dupsa, sa->sa_len);
2433 ifma->ifma_addr = dupsa;
2434 ifma->ifma_lladdr = llsa;
2435 ifma->ifma_ifp = ifp;
2436 ifma->ifma_refcount = 1;
2437 ifma->ifma_protospec = NULL;
2438 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2440 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2445 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2446 if (sa_equal(ifma->ifma_addr, llsa))
2450 ifma->ifma_refcount++;
2452 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2453 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2454 bcopy(llsa, dupsa, llsa->sa_len);
2455 ifma->ifma_addr = dupsa;
2456 ifma->ifma_ifp = ifp;
2457 ifma->ifma_refcount = 1;
2458 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2462 * We are certain we have added something, so call down to the
2463 * interface to let them know about it.
2466 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2472 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2473 struct ifmultiaddr **retifma)
2477 ifnet_serialize_all(ifp);
2478 error = if_addmulti_serialized(ifp, sa, retifma);
2479 ifnet_deserialize_all(ifp);
2485 * Remove a reference to a multicast address on this interface. Yell
2486 * if the request does not match an existing membership.
2489 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2491 struct ifmultiaddr *ifma;
2493 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2495 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2496 if (sa_equal(sa, ifma->ifma_addr))
2501 if (ifma->ifma_refcount > 1) {
2502 ifma->ifma_refcount--;
2506 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2507 sa = ifma->ifma_lladdr;
2508 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2510 * Make sure the interface driver is notified
2511 * in the case of a link layer mcast group being left.
2513 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2514 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2515 kfree(ifma->ifma_addr, M_IFMADDR);
2516 kfree(ifma, M_IFMADDR);
2521 * Now look for the link-layer address which corresponds to
2522 * this network address. It had been squirreled away in
2523 * ifma->ifma_lladdr for this purpose (so we don't have
2524 * to call ifp->if_resolvemulti() again), and we saved that
2525 * value in sa above. If some nasty deleted the
2526 * link-layer address out from underneath us, we can deal because
2527 * the address we stored was is not the same as the one which was
2528 * in the record for the link-layer address. (So we don't complain
2531 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2532 if (sa_equal(sa, ifma->ifma_addr))
2537 if (ifma->ifma_refcount > 1) {
2538 ifma->ifma_refcount--;
2542 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2543 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2544 kfree(ifma->ifma_addr, M_IFMADDR);
2545 kfree(sa, M_IFMADDR);
2546 kfree(ifma, M_IFMADDR);
2552 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2556 ifnet_serialize_all(ifp);
2557 error = if_delmulti_serialized(ifp, sa);
2558 ifnet_deserialize_all(ifp);
2564 * Delete all multicast group membership for an interface.
2565 * Should be used to quickly flush all multicast filters.
2568 if_delallmulti_serialized(struct ifnet *ifp)
2570 struct ifmultiaddr *ifma, mark;
2573 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2575 bzero(&sa, sizeof(sa));
2576 sa.sa_family = AF_UNSPEC;
2577 sa.sa_len = sizeof(sa);
2579 bzero(&mark, sizeof(mark));
2580 mark.ifma_addr = &sa;
2582 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2583 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2584 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2585 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2588 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2591 if_delmulti_serialized(ifp, ifma->ifma_addr);
2593 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2598 * Set the link layer address on an interface.
2600 * At this time we only support certain types of interfaces,
2601 * and we don't allow the length of the address to change.
2604 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2606 struct sockaddr_dl *sdl;
2609 sdl = IF_LLSOCKADDR(ifp);
2612 if (len != sdl->sdl_alen) /* don't allow length to change */
2614 switch (ifp->if_type) {
2615 case IFT_ETHER: /* these types use struct arpcom */
2618 case IFT_IEEE8023ADLAG:
2619 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2620 bcopy(lladdr, LLADDR(sdl), len);
2626 * If the interface is already up, we need
2627 * to re-init it in order to reprogram its
2630 ifnet_serialize_all(ifp);
2631 if ((ifp->if_flags & IFF_UP) != 0) {
2633 struct ifaddr_container *ifac;
2636 ifp->if_flags &= ~IFF_UP;
2637 ifr.ifr_flags = ifp->if_flags;
2638 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2639 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2641 ifp->if_flags |= IFF_UP;
2642 ifr.ifr_flags = ifp->if_flags;
2643 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2644 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2648 * Also send gratuitous ARPs to notify other nodes about
2649 * the address change.
2651 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2652 struct ifaddr *ifa = ifac->ifa;
2654 if (ifa->ifa_addr != NULL &&
2655 ifa->ifa_addr->sa_family == AF_INET)
2656 arp_gratuitous(ifp, ifa);
2660 ifnet_deserialize_all(ifp);
2664 struct ifmultiaddr *
2665 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2667 struct ifmultiaddr *ifma;
2669 /* TODO: need ifnet_serialize_main */
2670 ifnet_serialize_all(ifp);
2671 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2672 if (sa_equal(ifma->ifma_addr, sa))
2674 ifnet_deserialize_all(ifp);
2680 * This function locates the first real ethernet MAC from a network
2681 * card and loads it into node, returning 0 on success or ENOENT if
2682 * no suitable interfaces were found. It is used by the uuid code to
2683 * generate a unique 6-byte number.
2686 if_getanyethermac(uint16_t *node, int minlen)
2689 struct sockaddr_dl *sdl;
2692 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2693 if (ifp->if_type != IFT_ETHER)
2695 sdl = IF_LLSOCKADDR(ifp);
2696 if (sdl->sdl_alen < minlen)
2698 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2708 * The name argument must be a pointer to storage which will last as
2709 * long as the interface does. For physical devices, the result of
2710 * device_get_name(dev) is a good choice and for pseudo-devices a
2711 * static string works well.
2714 if_initname(struct ifnet *ifp, const char *name, int unit)
2716 ifp->if_dname = name;
2717 ifp->if_dunit = unit;
2718 if (unit != IF_DUNIT_NONE)
2719 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2721 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2725 if_printf(struct ifnet *ifp, const char *fmt, ...)
2730 retval = kprintf("%s: ", ifp->if_xname);
2731 __va_start(ap, fmt);
2732 retval += kvprintf(fmt, ap);
2738 if_alloc(uint8_t type)
2744 * XXX temporary hack until arpcom is setup in if_l2com
2746 if (type == IFT_ETHER)
2747 size = sizeof(struct arpcom);
2749 size = sizeof(struct ifnet);
2751 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2753 ifp->if_type = type;
2755 if (if_com_alloc[type] != NULL) {
2756 ifp->if_l2com = if_com_alloc[type](type, ifp);
2757 if (ifp->if_l2com == NULL) {
2758 kfree(ifp, M_IFNET);
2766 if_free(struct ifnet *ifp)
2768 kfree(ifp, M_IFNET);
2772 ifq_set_classic(struct ifaltq *ifq)
2774 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2775 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2779 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2780 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2784 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2785 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2786 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2787 KASSERT(request != NULL, ("request is not specified"));
2789 ifq->altq_mapsubq = mapsubq;
2790 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2791 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2793 ifsq->ifsq_enqueue = enqueue;
2794 ifsq->ifsq_dequeue = dequeue;
2795 ifsq->ifsq_request = request;
2800 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2803 classq_add(&ifsq->ifsq_norm, m);
2804 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2808 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2811 classq_add(&ifsq->ifsq_prio, m);
2812 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2813 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2816 static struct mbuf *
2817 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2821 m = classq_get(&ifsq->ifsq_norm);
2823 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2827 static struct mbuf *
2828 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2832 m = classq_get(&ifsq->ifsq_prio);
2834 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2835 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2841 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2842 struct altq_pktattr *pa __unused)
2847 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2848 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2849 struct mbuf *m_drop;
2851 if (m->m_flags & M_PRIO) {
2853 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2854 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2855 /* Try dropping some from normal queue. */
2856 m_drop = ifsq_norm_dequeue(ifsq);
2859 m_drop = ifsq_prio_dequeue(ifsq);
2861 m_drop = ifsq_norm_dequeue(ifsq);
2863 if (m_drop != NULL) {
2864 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2869 * No old packets could be dropped!
2870 * NOTE: Caller increases oqdrops.
2875 if (m->m_flags & M_PRIO)
2876 ifsq_prio_enqueue(ifsq, m);
2878 ifsq_norm_enqueue(ifsq, m);
2884 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2890 m = classq_head(&ifsq->ifsq_prio);
2892 m = classq_head(&ifsq->ifsq_norm);
2896 m = ifsq_prio_dequeue(ifsq);
2898 m = ifsq_norm_dequeue(ifsq);
2902 panic("unsupported ALTQ dequeue op: %d", op);
2908 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2915 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2923 panic("unsupported ALTQ request: %d", req);
2929 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2931 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2932 int running = 0, need_sched;
2935 * Try to do direct ifnet.if_start on the subqueue first, if there is
2936 * contention on the subqueue hardware serializer, ifnet.if_start on
2937 * the subqueue will be scheduled on the subqueue owner CPU.
2939 if (!ifsq_tryserialize_hw(ifsq)) {
2941 * Subqueue hardware serializer contention happened,
2942 * ifnet.if_start on the subqueue is scheduled on
2943 * the subqueue owner CPU, and we keep going.
2945 ifsq_ifstart_schedule(ifsq, 1);
2949 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2950 ifp->if_start(ifp, ifsq);
2951 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2954 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2956 ifsq_deserialize_hw(ifsq);
2960 * More data need to be transmitted, ifnet.if_start on the
2961 * subqueue is scheduled on the subqueue owner CPU, and we
2963 * NOTE: ifnet.if_start subqueue interlock is not released.
2965 ifsq_ifstart_schedule(ifsq, force_sched);
2970 * Subqeue packets staging mechanism:
2972 * The packets enqueued into the subqueue are staged to a certain amount
2973 * before the ifnet.if_start on the subqueue is called. In this way, the
2974 * driver could avoid writing to hardware registers upon every packet,
2975 * instead, hardware registers could be written when certain amount of
2976 * packets are put onto hardware TX ring. The measurement on several modern
2977 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2978 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2979 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2980 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2981 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2983 * Subqueue packets staging is performed for two entry points into drivers'
2984 * transmission function:
2985 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2986 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2988 * Subqueue packets staging will be stopped upon any of the following
2990 * - If the count of packets enqueued on the current CPU is great than or
2991 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2992 * - If the total length of packets enqueued on the current CPU is great
2993 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2994 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2995 * is usually less than hardware's MTU.
2996 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2997 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2999 * - The if_start_rollup(), which is registered as low priority netisr
3000 * rollup function, is called; probably because no more work is pending
3004 * Currently subqueue packet staging is only performed in netisr threads.
3007 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3009 struct ifaltq *ifq = &ifp->if_snd;
3010 struct ifaltq_subque *ifsq;
3011 int error, start = 0, len, mcast = 0, avoid_start = 0;
3012 struct ifsubq_stage_head *head = NULL;
3013 struct ifsubq_stage *stage = NULL;
3014 struct globaldata *gd = mycpu;
3015 struct thread *td = gd->gd_curthread;
3017 crit_enter_quick(td);
3019 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3020 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3022 len = m->m_pkthdr.len;
3023 if (m->m_flags & M_MCAST)
3026 if (td->td_type == TD_TYPE_NETISR) {
3027 head = &ifsubq_stage_heads[mycpuid];
3028 stage = ifsq_get_stage(ifsq, mycpuid);
3031 stage->stg_len += len;
3032 if (stage->stg_cnt < ifsq_stage_cntmax &&
3033 stage->stg_len < (ifp->if_mtu - max_protohdr))
3038 error = ifsq_enqueue_locked(ifsq, m, pa);
3040 IFNET_STAT_INC(ifp, oqdrops, 1);
3041 if (!ifsq_data_ready(ifsq)) {
3042 ALTQ_SQ_UNLOCK(ifsq);
3043 crit_exit_quick(td);
3048 if (!ifsq_is_started(ifsq)) {
3050 ALTQ_SQ_UNLOCK(ifsq);
3053 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3054 ifsq_stage_insert(head, stage);
3056 IFNET_STAT_INC(ifp, obytes, len);
3058 IFNET_STAT_INC(ifp, omcasts, 1);
3059 crit_exit_quick(td);
3064 * Hold the subqueue interlock of ifnet.if_start
3066 ifsq_set_started(ifsq);
3069 ALTQ_SQ_UNLOCK(ifsq);
3072 IFNET_STAT_INC(ifp, obytes, len);
3074 IFNET_STAT_INC(ifp, omcasts, 1);
3077 if (stage != NULL) {
3078 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3079 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3081 ifsq_stage_remove(head, stage);
3082 ifsq_ifstart_schedule(ifsq, 1);
3084 crit_exit_quick(td);
3088 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3089 ifsq_stage_remove(head, stage);
3097 crit_exit_quick(td);
3101 ifsq_ifstart_try(ifsq, 0);
3103 crit_exit_quick(td);
3108 ifa_create(int size)
3113 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3115 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3116 ifa->ifa_containers =
3117 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3118 M_IFADDR, M_INTWAIT | M_ZERO);
3120 ifa->ifa_ncnt = ncpus;
3121 for (i = 0; i < ncpus; ++i) {
3122 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3124 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3126 ifac->ifa_refcnt = 1;
3129 kprintf("alloc ifa %p %d\n", ifa, size);
3135 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3137 struct ifaddr *ifa = ifac->ifa;
3139 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3140 KKASSERT(ifac->ifa_refcnt == 0);
3141 KASSERT(ifac->ifa_listmask == 0,
3142 ("ifa is still on %#x lists", ifac->ifa_listmask));
3144 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3146 #ifdef IFADDR_DEBUG_VERBOSE
3147 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3150 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3151 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3152 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3154 kprintf("free ifa %p\n", ifa);
3156 kfree(ifa->ifa_containers, M_IFADDR);
3157 kfree(ifa, M_IFADDR);
3162 ifa_iflink_dispatch(netmsg_t nmsg)
3164 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3165 struct ifaddr *ifa = msg->ifa;
3166 struct ifnet *ifp = msg->ifp;
3168 struct ifaddr_container *ifac;
3172 ifac = &ifa->ifa_containers[cpu];
3173 ASSERT_IFAC_VALID(ifac);
3174 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3175 ("ifaddr is on if_addrheads"));
3177 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3179 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3181 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3185 netisr_forwardmsg(&nmsg->base, cpu + 1);
3189 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3191 struct netmsg_ifaddr msg;
3193 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3194 0, ifa_iflink_dispatch);
3199 netisr_domsg(&msg.base, 0);
3203 ifa_ifunlink_dispatch(netmsg_t nmsg)
3205 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3206 struct ifaddr *ifa = msg->ifa;
3207 struct ifnet *ifp = msg->ifp;
3209 struct ifaddr_container *ifac;
3213 ifac = &ifa->ifa_containers[cpu];
3214 ASSERT_IFAC_VALID(ifac);
3215 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3216 ("ifaddr is not on if_addrhead"));
3218 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3219 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3223 netisr_forwardmsg(&nmsg->base, cpu + 1);
3227 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3229 struct netmsg_ifaddr msg;
3231 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3232 0, ifa_ifunlink_dispatch);
3236 netisr_domsg(&msg.base, 0);
3240 ifa_destroy_dispatch(netmsg_t nmsg)
3242 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3245 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3249 ifa_destroy(struct ifaddr *ifa)
3251 struct netmsg_ifaddr msg;
3253 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3254 0, ifa_destroy_dispatch);
3257 netisr_domsg(&msg.base, 0);
3261 if_start_rollup(void)
3263 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3264 struct ifsubq_stage *stage;
3268 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3269 struct ifaltq_subque *ifsq = stage->stg_subq;
3272 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3274 ifsq_stage_remove(head, stage);
3277 ifsq_ifstart_schedule(ifsq, 1);
3282 if (!ifsq_is_started(ifsq)) {
3284 * Hold the subqueue interlock of
3287 ifsq_set_started(ifsq);
3290 ALTQ_SQ_UNLOCK(ifsq);
3293 ifsq_ifstart_try(ifsq, 1);
3295 KKASSERT((stage->stg_flags &
3296 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3303 ifnetinit(void *dummy __unused)
3307 for (i = 0; i < ncpus; ++i)
3308 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3309 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3313 if_register_com_alloc(u_char type,
3314 if_com_alloc_t *a, if_com_free_t *f)
3317 KASSERT(if_com_alloc[type] == NULL,
3318 ("if_register_com_alloc: %d already registered", type));
3319 KASSERT(if_com_free[type] == NULL,
3320 ("if_register_com_alloc: %d free already registered", type));
3322 if_com_alloc[type] = a;
3323 if_com_free[type] = f;
3327 if_deregister_com_alloc(u_char type)
3330 KASSERT(if_com_alloc[type] != NULL,
3331 ("if_deregister_com_alloc: %d not registered", type));
3332 KASSERT(if_com_free[type] != NULL,
3333 ("if_deregister_com_alloc: %d free not registered", type));
3334 if_com_alloc[type] = NULL;
3335 if_com_free[type] = NULL;
3339 ifq_set_maxlen(struct ifaltq *ifq, int len)
3341 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3345 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3347 return ALTQ_SUBQ_INDEX_DEFAULT;
3351 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3354 return (cpuid % ifq->altq_subq_mappriv);
3358 ifsq_watchdog(void *arg)
3360 struct ifsubq_watchdog *wd = arg;
3363 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3366 ifp = ifsq_get_ifp(wd->wd_subq);
3367 if (ifnet_tryserialize_all(ifp)) {
3368 wd->wd_watchdog(wd->wd_subq);
3369 ifnet_deserialize_all(ifp);
3371 /* try again next timeout */
3375 ifsq_watchdog_reset(wd);
3379 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3381 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3382 ifsq_get_cpuid(wd->wd_subq));
3386 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3387 ifsq_watchdog_t watchdog)
3389 callout_init_mp(&wd->wd_callout);
3392 wd->wd_watchdog = watchdog;
3396 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3399 ifsq_watchdog_reset(wd);
3403 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3406 callout_stop(&wd->wd_callout);
3412 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3413 ("try holding ifnet lock in netisr"));
3414 mtx_lock(&ifnet_mtx);
3420 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3421 ("try holding ifnet lock in netisr"));
3422 mtx_unlock(&ifnet_mtx);
3425 static struct ifnet_array *
3426 ifnet_array_alloc(int count)
3428 struct ifnet_array *arr;
3430 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3432 arr->ifnet_count = count;
3438 ifnet_array_free(struct ifnet_array *arr)
3440 if (arr == &ifnet_array0)
3442 kfree(arr, M_IFNET);
3445 static struct ifnet_array *
3446 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3448 struct ifnet_array *arr;
3451 KASSERT(old_arr->ifnet_count >= 0,
3452 ("invalid ifnet array count %d", old_arr->ifnet_count));
3453 count = old_arr->ifnet_count + 1;
3454 arr = ifnet_array_alloc(count);
3457 * Save the old ifnet array and append this ifp to the end of
3458 * the new ifnet array.
3460 for (i = 0; i < old_arr->ifnet_count; ++i) {
3461 KASSERT(old_arr->ifnet_arr[i] != ifp,
3462 ("%s is already in ifnet array", ifp->if_xname));
3463 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3465 KASSERT(i == count - 1,
3466 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3467 ifp->if_xname, count - 1, i));
3468 arr->ifnet_arr[i] = ifp;
3473 static struct ifnet_array *
3474 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3476 struct ifnet_array *arr;
3477 int count, i, idx, found = 0;
3479 KASSERT(old_arr->ifnet_count > 0,
3480 ("invalid ifnet array count %d", old_arr->ifnet_count));
3481 count = old_arr->ifnet_count - 1;
3482 arr = ifnet_array_alloc(count);
3485 * Save the old ifnet array, but skip this ifp.
3488 for (i = 0; i < old_arr->ifnet_count; ++i) {
3489 if (old_arr->ifnet_arr[i] == ifp) {
3491 ("dup %s is in ifnet array", ifp->if_xname));
3495 KASSERT(idx < count,
3496 ("invalid ifnet array index %d, count %d", idx, count));
3497 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3500 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3501 KASSERT(idx == count,
3502 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3503 ifp->if_xname, count, idx));
3508 const struct ifnet_array *
3509 ifnet_array_get(void)
3511 const struct ifnet_array *ret;
3513 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3515 /* Make sure 'ret' is really used. */
3521 ifnet_array_isempty(void)
3523 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3524 if (ifnet_array->ifnet_count == 0)
3531 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3535 memset(mark, 0, sizeof(*mark));
3538 mark->ifac.ifa = ifa;
3540 ifa->ifa_addr = &mark->addr;
3541 ifa->ifa_dstaddr = &mark->dstaddr;
3542 ifa->ifa_netmask = &mark->netmask;
3547 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3550 KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3552 if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3553 ring_cnt = ring_cntmax;
3554 if (ring_cnt > netisr_ncpus)
3555 ring_cnt = netisr_ncpus;
3560 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3564 KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3565 KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3569 offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3570 for (i = 0; i < rm->rm_cnt; ++i) {
3571 rm->rm_cpumap[i] = offset + i;
3572 KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3573 ("invalid cpumap[%d] = %d, offset %d", i,
3574 rm->rm_cpumap[i], offset));
3578 static struct if_ringmap *
3579 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3582 struct if_ringmap *rm;
3583 int i, grid = 0, prev_grid;
3585 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3586 rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3587 M_DEVBUF, M_WAITOK | M_ZERO);
3589 rm->rm_cnt = ring_cnt;
3590 if (flags & RINGMAP_FLAG_POWEROF2)
3591 rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3593 prev_grid = netisr_ncpus;
3594 for (i = 0; i < netisr_ncpus; ++i) {
3595 if (netisr_ncpus % (i + 1) != 0)
3598 grid = netisr_ncpus / (i + 1);
3599 if (rm->rm_cnt > grid) {
3604 if (rm->rm_cnt > netisr_ncpus / (i + 2))
3608 if_ringmap_set_grid(dev, rm, grid);
3614 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3617 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3618 RINGMAP_FLAG_NONE));
3622 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3625 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3626 RINGMAP_FLAG_POWEROF2));
3630 if_ringmap_free(struct if_ringmap *rm)
3633 kfree(rm, M_DEVBUF);
3637 * Align the two ringmaps.
3639 * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3643 * CPU 0 1 2 3 4 5 6 7
3644 * NIC_RX n0 n1 n2 n3
3649 * CPU 0 1 2 3 4 5 6 7
3650 * NIC_RX n0 n1 n2 n3
3654 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3657 if (rm0->rm_grid > rm1->rm_grid)
3658 if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3659 else if (rm0->rm_grid < rm1->rm_grid)
3660 if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3664 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3666 int subset_grid, cnt, divisor, mod, offset, i;
3667 struct if_ringmap *subset_rm, *rm;
3668 int old_rm0_grid, old_rm1_grid;
3670 if (rm0->rm_grid == rm1->rm_grid)
3673 /* Save grid for later use */
3674 old_rm0_grid = rm0->rm_grid;
3675 old_rm1_grid = rm1->rm_grid;
3677 if_ringmap_align(dev, rm0, rm1);
3680 * Re-shuffle rings to get more even distribution.
3682 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3684 * CPU 0 1 2 3 4 5 6 7 8 9 10 11
3686 * NIC_RX a0 a1 a2 a3 b0 b1 b2 b3 c0 c1 c2 c3
3687 * NIC_TX A0 A1 B0 B1 C0 C1
3689 * NIC_RX d0 d1 d2 d3 e0 e1 e2 e3 f0 f1 f2 f3
3690 * NIC_TX D0 D1 E0 E1 F0 F1
3693 if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3695 subset_grid = old_rm1_grid;
3698 } else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3700 subset_grid = old_rm0_grid;
3704 /* No space to shuffle. */
3708 mod = cnt / subset_grid;
3710 divisor = netisr_ncpus / rm->rm_grid;
3711 offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3713 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3714 subset_rm->rm_cpumap[i] += offset;
3715 KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3716 ("match: invalid cpumap[%d] = %d, offset %d",
3717 i, subset_rm->rm_cpumap[i], offset));
3720 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3723 for (j = 0; j < rm->rm_cnt; ++j) {
3724 if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3727 KASSERT(j < rm->rm_cnt,
3728 ("subset cpumap[%d] = %d not found in superset",
3729 i, subset_rm->rm_cpumap[i]));
3735 if_ringmap_count(const struct if_ringmap *rm)
3738 return (rm->rm_cnt);
3742 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3745 KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3746 return (rm->rm_cpumap[ring]);
3750 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3752 int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3754 KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3755 ("invalid redirect table entries %d", table_nent));
3758 for (i = 0; i < NETISR_CPUMAX; ++i) {
3759 table[i] = grid_idx++ % rm->rm_cnt;
3761 if (grid_idx == rm->rm_grid)
3766 * Make the ring distributed more evenly for the remainder
3769 * e.g. 12 netisrs, rm contains 8 rings.
3771 * Redirect table before:
3773 * 0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
3774 * 4 5 6 7 0 1 2 3 0 1 2 3 4 5 6 7
3775 * 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3
3778 * Redirect table after being patched (pX, patched entries):
3780 * 0 1 2 3 4 5 6 7 p0 p1 p2 p3 0 1 2 3
3781 * 4 5 6 7 p4 p5 p6 p7 0 1 2 3 4 5 6 7
3782 * p0 p1 p2 p3 0 1 2 3 4 5 6 7 p4 p5 p6 p7
3785 patch_cnt = rm->rm_grid % rm->rm_cnt;
3788 patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3790 grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3792 for (i = 0; i < grid_cnt; ++i) {
3795 for (j = 0; j < patch_cnt; ++j) {
3798 fix_idx = (i * rm->rm_grid) + patch_off + j;
3799 if (fix_idx >= NETISR_CPUMAX)
3801 table[fix_idx] = grid_idx++ % rm->rm_cnt;
3806 * If the device supports larger redirect table, duplicate
3807 * the first NETISR_CPUMAX entries to the rest of the table,
3808 * so that it matches upper layer's expectation:
3809 * (hash & NETISR_CPUMASK) % netisr_ncpus
3811 ncopy = table_nent / NETISR_CPUMAX;
3812 for (i = 1; i < ncopy; ++i) {
3813 memcpy(&table[i * NETISR_CPUMAX], table,
3814 NETISR_CPUMAX * sizeof(table[0]));
3816 if (if_ringmap_dumprdr) {
3817 for (i = 0; i < table_nent; ++i) {
3818 if (i != 0 && i % 16 == 0)
3820 kprintf("%03d ", table[i]);
3827 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
3829 struct if_ringmap *rm = arg1;
3832 for (i = 0; i < rm->rm_cnt; ++i) {
3833 int cpu = rm->rm_cpumap[i];
3835 error = SYSCTL_OUT(req, &cpu, sizeof(cpu));