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 ifa_ifunlink(ifa, ifp);
832 lwkt_replymsg(lmsg, 0);
836 if_purgeaddrs_nolink(struct ifnet *ifp)
838 struct netmsg_base nmsg;
839 struct lwkt_msg *lmsg = &nmsg.lmsg;
841 ASSERT_CANDOMSG_NETISR0(curthread);
843 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0,
844 if_purgeaddrs_nolink_dispatch);
845 lmsg->u.ms_resultp = ifp;
846 lwkt_domsg(netisr_cpuport(0), lmsg, 0);
850 ifq_stage_detach_handler(netmsg_t nmsg)
852 struct ifaltq *ifq = nmsg->lmsg.u.ms_resultp;
855 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
856 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
857 struct ifsubq_stage *stage = ifsq_get_stage(ifsq, mycpuid);
859 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED)
860 ifsq_stage_remove(&ifsubq_stage_heads[mycpuid], stage);
862 lwkt_replymsg(&nmsg->lmsg, 0);
866 ifq_stage_detach(struct ifaltq *ifq)
868 struct netmsg_base base;
871 netmsg_init(&base, NULL, &curthread->td_msgport, 0,
872 ifq_stage_detach_handler);
873 base.lmsg.u.ms_resultp = ifq;
875 for (cpu = 0; cpu < ncpus; ++cpu)
876 lwkt_domsg(netisr_cpuport(cpu), &base.lmsg, 0);
879 struct netmsg_if_rtdel {
880 struct netmsg_base base;
885 if_rtdel_dispatch(netmsg_t msg)
887 struct netmsg_if_rtdel *rmsg = (void *)msg;
891 for (i = 1; i <= AF_MAX; i++) {
892 struct radix_node_head *rnh;
894 if ((rnh = rt_tables[cpu][i]) == NULL)
896 rnh->rnh_walktree(rnh, if_rtdel, rmsg->ifp);
901 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
903 lwkt_replymsg(&rmsg->base.lmsg, 0);
907 * Detach an interface, removing it from the
908 * list of "active" interfaces.
911 if_detach(struct ifnet *ifp)
913 struct ifnet_array *old_ifnet_array;
914 struct netmsg_if_rtdel msg;
918 /* Announce that the interface is gone. */
919 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
920 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
921 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL);
924 * Remove this ifp from ifindex2inet, ifnet queue and ifnet
925 * array before it is whacked.
927 * Protect ifindex2ifnet, ifnet queue and ifnet array changes
928 * by ifnet lock, so that non-netisr threads could get a
934 * Remove this ifp from ifindex2ifnet and maybe decrement if_index.
936 ifindex2ifnet[ifp->if_index] = NULL;
937 while (if_index > 0 && ifindex2ifnet[if_index] == NULL)
941 * Remove this ifp from ifnet queue.
943 TAILQ_REMOVE(&ifnetlist, ifp, if_link);
946 * Remove this ifp from ifnet array.
948 /* Free old ifnet array after sync all netisrs */
949 old_ifnet_array = ifnet_array;
950 ifnet_array = ifnet_array_del(ifp, old_ifnet_array);
955 * Sync all netisrs so that the old ifnet array is no longer
956 * accessed and we can free it safely later on.
958 netmsg_service_sync();
959 ifnet_array_free(old_ifnet_array);
962 * Remove routes and flush queues.
966 if (ifp->if_flags & IFF_NPOLLING)
967 ifpoll_deregister(ifp);
971 /* Decrease the mbuf clusters/jclusters limits increased by us */
972 if (ifp->if_nmbclusters > 0)
973 mcl_inclimit(-ifp->if_nmbclusters);
974 if (ifp->if_nmbjclusters > 0)
975 mjcl_inclimit(-ifp->if_nmbjclusters);
978 if (ifq_is_enabled(&ifp->if_snd))
979 altq_disable(&ifp->if_snd);
980 if (ifq_is_attached(&ifp->if_snd))
981 altq_detach(&ifp->if_snd);
985 * Clean up all addresses.
987 ifp->if_lladdr = NULL;
989 if_purgeaddrs_nolink(ifp);
990 if (!TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
993 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
994 KASSERT(ifa->ifa_addr->sa_family == AF_LINK,
995 ("non-link ifaddr is left on if_addrheads"));
997 ifa_ifunlink(ifa, ifp);
999 KASSERT(TAILQ_EMPTY(&ifp->if_addrheads[mycpuid]),
1000 ("there are still ifaddrs left on if_addrheads"));
1005 * Remove all IPv4 kernel structures related to ifp.
1012 * Remove all IPv6 kernel structs related to ifp. This should be done
1013 * before removing routing entries below, since IPv6 interface direct
1014 * routes are expected to be removed by the IPv6-specific kernel API.
1015 * Otherwise, the kernel will detect some inconsistency and bark it.
1021 * Delete all remaining routes using this interface
1023 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
1026 rt_domsg_global(&msg.base);
1028 SLIST_FOREACH(dp, &domains, dom_next)
1029 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family])
1030 (*dp->dom_ifdetach)(ifp,
1031 ifp->if_afdata[dp->dom_family]);
1033 kfree(ifp->if_addrheads, M_IFADDR);
1035 lwkt_synchronize_ipiqs("if_detach");
1036 ifq_stage_detach(&ifp->if_snd);
1038 for (q = 0; q < ifp->if_snd.altq_subq_cnt; ++q) {
1039 struct ifaltq_subque *ifsq = &ifp->if_snd.altq_subq[q];
1041 kfree(ifsq->ifsq_ifstart_nmsg, M_LWKTMSG);
1042 kfree(ifsq->ifsq_stage, M_DEVBUF);
1044 kfree(ifp->if_snd.altq_subq, M_DEVBUF);
1046 kfree(ifp->if_data_pcpu, M_DEVBUF);
1052 * Create interface group without members
1055 if_creategroup(const char *groupname)
1057 struct ifg_group *ifg = NULL;
1059 if ((ifg = (struct ifg_group *)kmalloc(sizeof(struct ifg_group),
1060 M_TEMP, M_NOWAIT)) == NULL)
1063 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group));
1064 ifg->ifg_refcnt = 0;
1065 ifg->ifg_carp_demoted = 0;
1066 TAILQ_INIT(&ifg->ifg_members);
1068 pfi_attach_ifgroup(ifg);
1070 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next);
1076 * Add a group to an interface
1079 if_addgroup(struct ifnet *ifp, const char *groupname)
1081 struct ifg_list *ifgl;
1082 struct ifg_group *ifg = NULL;
1083 struct ifg_member *ifgm;
1085 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' &&
1086 groupname[strlen(groupname) - 1] <= '9')
1089 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1090 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1093 if ((ifgl = kmalloc(sizeof(*ifgl), M_TEMP, M_NOWAIT)) == NULL)
1096 if ((ifgm = kmalloc(sizeof(*ifgm), M_TEMP, M_NOWAIT)) == NULL) {
1097 kfree(ifgl, M_TEMP);
1101 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1102 if (!strcmp(ifg->ifg_group, groupname))
1105 if (ifg == NULL && (ifg = if_creategroup(groupname)) == NULL) {
1106 kfree(ifgl, M_TEMP);
1107 kfree(ifgm, M_TEMP);
1112 ifgl->ifgl_group = ifg;
1113 ifgm->ifgm_ifp = ifp;
1115 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next);
1116 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next);
1119 pfi_group_change(groupname);
1126 * Remove a group from an interface
1129 if_delgroup(struct ifnet *ifp, const char *groupname)
1131 struct ifg_list *ifgl;
1132 struct ifg_member *ifgm;
1134 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1135 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname))
1140 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next);
1142 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next)
1143 if (ifgm->ifgm_ifp == ifp)
1147 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next);
1148 kfree(ifgm, M_TEMP);
1151 if (--ifgl->ifgl_group->ifg_refcnt == 0) {
1152 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next);
1154 pfi_detach_ifgroup(ifgl->ifgl_group);
1156 kfree(ifgl->ifgl_group, M_TEMP);
1159 kfree(ifgl, M_TEMP);
1162 pfi_group_change(groupname);
1169 * Stores all groups from an interface in memory pointed
1173 if_getgroup(caddr_t data, struct ifnet *ifp)
1176 struct ifg_list *ifgl;
1177 struct ifg_req ifgrq, *ifgp;
1178 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1180 if (ifgr->ifgr_len == 0) {
1181 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next)
1182 ifgr->ifgr_len += sizeof(struct ifg_req);
1186 len = ifgr->ifgr_len;
1187 ifgp = ifgr->ifgr_groups;
1188 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) {
1189 if (len < sizeof(ifgrq))
1191 bzero(&ifgrq, sizeof ifgrq);
1192 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group,
1193 sizeof(ifgrq.ifgrq_group));
1194 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1195 sizeof(struct ifg_req))))
1197 len -= sizeof(ifgrq);
1205 * Stores all members of a group in memory pointed to by data
1208 if_getgroupmembers(caddr_t data)
1210 struct ifgroupreq *ifgr = (struct ifgroupreq *)data;
1211 struct ifg_group *ifg;
1212 struct ifg_member *ifgm;
1213 struct ifg_req ifgrq, *ifgp;
1216 TAILQ_FOREACH(ifg, &ifg_head, ifg_next)
1217 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name))
1222 if (ifgr->ifgr_len == 0) {
1223 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next)
1224 ifgr->ifgr_len += sizeof(ifgrq);
1228 len = ifgr->ifgr_len;
1229 ifgp = ifgr->ifgr_groups;
1230 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) {
1231 if (len < sizeof(ifgrq))
1233 bzero(&ifgrq, sizeof ifgrq);
1234 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname,
1235 sizeof(ifgrq.ifgrq_member));
1236 if ((error = copyout((caddr_t)&ifgrq, (caddr_t)ifgp,
1237 sizeof(struct ifg_req))))
1239 len -= sizeof(ifgrq);
1247 * Delete Routes for a Network Interface
1249 * Called for each routing entry via the rnh->rnh_walktree() call above
1250 * to delete all route entries referencing a detaching network interface.
1253 * rn pointer to node in the routing table
1254 * arg argument passed to rnh->rnh_walktree() - detaching interface
1258 * errno failed - reason indicated
1262 if_rtdel(struct radix_node *rn, void *arg)
1264 struct rtentry *rt = (struct rtentry *)rn;
1265 struct ifnet *ifp = arg;
1268 if (rt->rt_ifp == ifp) {
1271 * Protect (sorta) against walktree recursion problems
1272 * with cloned routes
1274 if (!(rt->rt_flags & RTF_UP))
1277 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1278 rt_mask(rt), rt->rt_flags,
1281 log(LOG_WARNING, "if_rtdel: error %d\n", err);
1288 static __inline boolean_t
1289 ifa_prefer(const struct ifaddr *cur_ifa, const struct ifaddr *old_ifa)
1291 if (old_ifa == NULL)
1294 if ((old_ifa->ifa_ifp->if_flags & IFF_UP) == 0 &&
1295 (cur_ifa->ifa_ifp->if_flags & IFF_UP))
1297 if ((old_ifa->ifa_flags & IFA_ROUTE) == 0 &&
1298 (cur_ifa->ifa_flags & IFA_ROUTE))
1304 * Locate an interface based on a complete address.
1307 ifa_ifwithaddr(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 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1318 struct ifaddr *ifa = ifac->ifa;
1320 if (ifa->ifa_addr->sa_family != addr->sa_family)
1322 if (sa_equal(addr, ifa->ifa_addr))
1324 if ((ifp->if_flags & IFF_BROADCAST) &&
1325 ifa->ifa_broadaddr &&
1326 /* IPv6 doesn't have broadcast */
1327 ifa->ifa_broadaddr->sa_len != 0 &&
1328 sa_equal(ifa->ifa_broadaddr, addr))
1336 * Locate the point to point interface with a given destination address.
1339 ifa_ifwithdstaddr(struct sockaddr *addr)
1341 const struct ifnet_array *arr;
1344 arr = ifnet_array_get();
1345 for (i = 0; i < arr->ifnet_count; ++i) {
1346 struct ifnet *ifp = arr->ifnet_arr[i];
1347 struct ifaddr_container *ifac;
1349 if (!(ifp->if_flags & IFF_POINTOPOINT))
1352 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1353 struct ifaddr *ifa = ifac->ifa;
1355 if (ifa->ifa_addr->sa_family != addr->sa_family)
1357 if (ifa->ifa_dstaddr &&
1358 sa_equal(addr, ifa->ifa_dstaddr))
1366 * Find an interface on a specific network. If many, choice
1367 * is most specific found.
1370 ifa_ifwithnet(struct sockaddr *addr)
1372 struct ifaddr *ifa_maybe = NULL;
1373 u_int af = addr->sa_family;
1374 char *addr_data = addr->sa_data, *cplim;
1375 const struct ifnet_array *arr;
1379 * AF_LINK addresses can be looked up directly by their index number,
1380 * so do that if we can.
1382 if (af == AF_LINK) {
1383 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
1385 if (sdl->sdl_index && sdl->sdl_index <= if_index)
1386 return (ifindex2ifnet[sdl->sdl_index]->if_lladdr);
1390 * Scan though each interface, looking for ones that have
1391 * addresses in this address family.
1393 arr = ifnet_array_get();
1394 for (i = 0; i < arr->ifnet_count; ++i) {
1395 struct ifnet *ifp = arr->ifnet_arr[i];
1396 struct ifaddr_container *ifac;
1398 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1399 struct ifaddr *ifa = ifac->ifa;
1400 char *cp, *cp2, *cp3;
1402 if (ifa->ifa_addr->sa_family != af)
1404 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
1406 * This is a bit broken as it doesn't
1407 * take into account that the remote end may
1408 * be a single node in the network we are
1410 * The trouble is that we don't know the
1411 * netmask for the remote end.
1413 if (ifa->ifa_dstaddr != NULL &&
1414 sa_equal(addr, ifa->ifa_dstaddr))
1418 * if we have a special address handler,
1419 * then use it instead of the generic one.
1421 if (ifa->ifa_claim_addr) {
1422 if ((*ifa->ifa_claim_addr)(ifa, addr)) {
1430 * Scan all the bits in the ifa's address.
1431 * If a bit dissagrees with what we are
1432 * looking for, mask it with the netmask
1433 * to see if it really matters.
1434 * (A byte at a time)
1436 if (ifa->ifa_netmask == 0)
1439 cp2 = ifa->ifa_addr->sa_data;
1440 cp3 = ifa->ifa_netmask->sa_data;
1441 cplim = ifa->ifa_netmask->sa_len +
1442 (char *)ifa->ifa_netmask;
1444 if ((*cp++ ^ *cp2++) & *cp3++)
1445 goto next; /* next address! */
1447 * If the netmask of what we just found
1448 * is more specific than what we had before
1449 * (if we had one) then remember the new one
1450 * before continuing to search for an even
1451 * better one. If the netmasks are equal,
1452 * we prefer the this ifa based on the result
1455 if (ifa_maybe == NULL ||
1456 rn_refines((char *)ifa->ifa_netmask,
1457 (char *)ifa_maybe->ifa_netmask) ||
1458 (sa_equal(ifa_maybe->ifa_netmask,
1459 ifa->ifa_netmask) &&
1460 ifa_prefer(ifa, ifa_maybe)))
1469 * Find an interface address specific to an interface best matching
1473 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp)
1475 struct ifaddr_container *ifac;
1476 char *cp, *cp2, *cp3;
1478 struct ifaddr *ifa_maybe = NULL;
1479 u_int af = addr->sa_family;
1483 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1484 struct ifaddr *ifa = ifac->ifa;
1486 if (ifa->ifa_addr->sa_family != af)
1488 if (ifa_maybe == NULL)
1490 if (ifa->ifa_netmask == NULL) {
1491 if (sa_equal(addr, ifa->ifa_addr) ||
1492 (ifa->ifa_dstaddr != NULL &&
1493 sa_equal(addr, ifa->ifa_dstaddr)))
1497 if (ifp->if_flags & IFF_POINTOPOINT) {
1498 if (sa_equal(addr, ifa->ifa_dstaddr))
1502 cp2 = ifa->ifa_addr->sa_data;
1503 cp3 = ifa->ifa_netmask->sa_data;
1504 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1505 for (; cp3 < cplim; cp3++)
1506 if ((*cp++ ^ *cp2++) & *cp3)
1516 * Default action when installing a route with a Link Level gateway.
1517 * Lookup an appropriate real ifa to point to.
1518 * This should be moved to /sys/net/link.c eventually.
1521 link_rtrequest(int cmd, struct rtentry *rt)
1524 struct sockaddr *dst;
1527 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL ||
1528 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_key(rt)) == NULL)
1530 ifa = ifaof_ifpforaddr(dst, ifp);
1532 IFAFREE(rt->rt_ifa);
1535 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
1536 ifa->ifa_rtrequest(cmd, rt);
1540 struct netmsg_ifroute {
1541 struct netmsg_base base;
1548 * Mark an interface down and notify protocols of the transition.
1551 if_unroute_dispatch(netmsg_t nmsg)
1553 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1554 struct ifnet *ifp = msg->ifp;
1555 int flag = msg->flag, fam = msg->fam;
1556 struct ifaddr_container *ifac;
1558 ifp->if_flags &= ~flag;
1559 getmicrotime(&ifp->if_lastchange);
1561 * The ifaddr processing in the following loop will block,
1562 * however, this function is called in netisr0, in which
1563 * ifaddr list changes happen, so we don't care about the
1564 * blockness of the ifaddr processing here.
1566 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1567 struct ifaddr *ifa = ifac->ifa;
1570 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1573 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1574 kpfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1576 ifq_purge_all(&ifp->if_snd);
1579 lwkt_replymsg(&nmsg->lmsg, 0);
1583 if_unroute(struct ifnet *ifp, int flag, int fam)
1585 struct netmsg_ifroute msg;
1587 ASSERT_CANDOMSG_NETISR0(curthread);
1589 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1590 if_unroute_dispatch);
1594 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1598 * Mark an interface up and notify protocols of the transition.
1601 if_route_dispatch(netmsg_t nmsg)
1603 struct netmsg_ifroute *msg = (struct netmsg_ifroute *)nmsg;
1604 struct ifnet *ifp = msg->ifp;
1605 int flag = msg->flag, fam = msg->fam;
1606 struct ifaddr_container *ifac;
1608 ifq_purge_all(&ifp->if_snd);
1609 ifp->if_flags |= flag;
1610 getmicrotime(&ifp->if_lastchange);
1612 * The ifaddr processing in the following loop will block,
1613 * however, this function is called in netisr0, in which
1614 * ifaddr list changes happen, so we don't care about the
1615 * blockness of the ifaddr processing here.
1617 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1618 struct ifaddr *ifa = ifac->ifa;
1621 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
1624 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1625 kpfctlinput(PRC_IFUP, ifa->ifa_addr);
1632 lwkt_replymsg(&nmsg->lmsg, 0);
1636 if_route(struct ifnet *ifp, int flag, int fam)
1638 struct netmsg_ifroute msg;
1640 ASSERT_CANDOMSG_NETISR0(curthread);
1642 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
1647 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
1651 * Mark an interface down and notify protocols of the transition. An
1652 * interface going down is also considered to be a synchronizing event.
1653 * We must ensure that all packet processing related to the interface
1654 * has completed before we return so e.g. the caller can free the ifnet
1655 * structure that the mbufs may be referencing.
1657 * NOTE: must be called at splnet or eqivalent.
1660 if_down(struct ifnet *ifp)
1662 if_unroute(ifp, IFF_UP, AF_UNSPEC);
1663 netmsg_service_sync();
1667 * Mark an interface up and notify protocols of
1669 * NOTE: must be called at splnet or eqivalent.
1672 if_up(struct ifnet *ifp)
1674 if_route(ifp, IFF_UP, AF_UNSPEC);
1678 * Process a link state change.
1679 * NOTE: must be called at splsoftnet or equivalent.
1682 if_link_state_change(struct ifnet *ifp)
1684 int link_state = ifp->if_link_state;
1687 devctl_notify("IFNET", ifp->if_xname,
1688 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL);
1692 * Handle interface watchdog timer routines. Called
1693 * from softclock, we decrement timers (if set) and
1694 * call the appropriate interface routine on expiration.
1697 if_slowtimo_dispatch(netmsg_t nmsg)
1699 struct globaldata *gd = mycpu;
1700 const struct ifnet_array *arr;
1703 ASSERT_IN_NETISR(0);
1706 lwkt_replymsg(&nmsg->lmsg, 0); /* reply ASAP */
1709 arr = ifnet_array_get();
1710 for (i = 0; i < arr->ifnet_count; ++i) {
1711 struct ifnet *ifp = arr->ifnet_arr[i];
1715 if (if_stats_compat) {
1716 IFNET_STAT_GET(ifp, ipackets, ifp->if_ipackets);
1717 IFNET_STAT_GET(ifp, ierrors, ifp->if_ierrors);
1718 IFNET_STAT_GET(ifp, opackets, ifp->if_opackets);
1719 IFNET_STAT_GET(ifp, oerrors, ifp->if_oerrors);
1720 IFNET_STAT_GET(ifp, collisions, ifp->if_collisions);
1721 IFNET_STAT_GET(ifp, ibytes, ifp->if_ibytes);
1722 IFNET_STAT_GET(ifp, obytes, ifp->if_obytes);
1723 IFNET_STAT_GET(ifp, imcasts, ifp->if_imcasts);
1724 IFNET_STAT_GET(ifp, omcasts, ifp->if_omcasts);
1725 IFNET_STAT_GET(ifp, iqdrops, ifp->if_iqdrops);
1726 IFNET_STAT_GET(ifp, noproto, ifp->if_noproto);
1727 IFNET_STAT_GET(ifp, oqdrops, ifp->if_oqdrops);
1730 if (ifp->if_timer == 0 || --ifp->if_timer) {
1734 if (ifp->if_watchdog) {
1735 if (ifnet_tryserialize_all(ifp)) {
1736 (*ifp->if_watchdog)(ifp);
1737 ifnet_deserialize_all(ifp);
1739 /* try again next timeout */
1747 callout_reset(&if_slowtimo_timer, hz / IFNET_SLOWHZ, if_slowtimo, NULL);
1751 if_slowtimo(void *arg __unused)
1753 struct lwkt_msg *lmsg = &if_slowtimo_netmsg.lmsg;
1755 KASSERT(mycpuid == 0, ("not on cpu0"));
1757 if (lmsg->ms_flags & MSGF_DONE)
1758 lwkt_sendmsg_oncpu(netisr_cpuport(0), lmsg);
1763 * Map interface name to
1764 * interface structure pointer.
1767 ifunit(const char *name)
1772 * Search all the interfaces for this name/number
1774 KASSERT(mtx_owned(&ifnet_mtx), ("ifnet is not locked"));
1776 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
1777 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1784 ifunit_netisr(const char *name)
1786 const struct ifnet_array *arr;
1790 * Search all the interfaces for this name/number
1793 arr = ifnet_array_get();
1794 for (i = 0; i < arr->ifnet_count; ++i) {
1795 struct ifnet *ifp = arr->ifnet_arr[i];
1797 if (strncmp(ifp->if_xname, name, IFNAMSIZ) == 0)
1807 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct ucred *cred)
1812 int error, do_ifup = 0;
1815 size_t namelen, onamelen;
1816 char new_name[IFNAMSIZ];
1818 struct sockaddr_dl *sdl;
1823 return (ifconf(cmd, data, cred));
1828 ifr = (struct ifreq *)data;
1833 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1835 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name),
1836 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL));
1838 if ((error = priv_check_cred(cred, PRIV_ROOT, 0)) != 0)
1840 return (if_clone_destroy(ifr->ifr_name));
1841 case SIOCIFGCLONERS:
1842 return (if_clone_list((struct if_clonereq *)data));
1848 * Nominal ioctl through interface, lookup the ifp and obtain a
1849 * lock to serialize the ifconfig ioctl operation.
1853 ifp = ifunit(ifr->ifr_name);
1862 ifr->ifr_index = ifp->if_index;
1866 ifr->ifr_flags = ifp->if_flags;
1867 ifr->ifr_flagshigh = ifp->if_flags >> 16;
1871 ifr->ifr_reqcap = ifp->if_capabilities;
1872 ifr->ifr_curcap = ifp->if_capenable;
1876 ifr->ifr_metric = ifp->if_metric;
1880 ifr->ifr_mtu = ifp->if_mtu;
1884 ifr->ifr_tsolen = ifp->if_tsolen;
1888 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data,
1889 sizeof(ifp->if_data));
1893 ifr->ifr_phys = ifp->if_physical;
1896 case SIOCGIFPOLLCPU:
1897 ifr->ifr_pollcpu = -1;
1900 case SIOCSIFPOLLCPU:
1904 error = priv_check_cred(cred, PRIV_ROOT, 0);
1907 new_flags = (ifr->ifr_flags & 0xffff) |
1908 (ifr->ifr_flagshigh << 16);
1909 if (ifp->if_flags & IFF_SMART) {
1910 /* Smart drivers twiddle their own routes */
1911 } else if (ifp->if_flags & IFF_UP &&
1912 (new_flags & IFF_UP) == 0) {
1914 } else if (new_flags & IFF_UP &&
1915 (ifp->if_flags & IFF_UP) == 0) {
1919 #ifdef IFPOLL_ENABLE
1920 if ((new_flags ^ ifp->if_flags) & IFF_NPOLLING) {
1921 if (new_flags & IFF_NPOLLING)
1922 ifpoll_register(ifp);
1924 ifpoll_deregister(ifp);
1928 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
1929 (new_flags &~ IFF_CANTCHANGE);
1930 if (new_flags & IFF_PPROMISC) {
1931 /* Permanently promiscuous mode requested */
1932 ifp->if_flags |= IFF_PROMISC;
1933 } else if (ifp->if_pcount == 0) {
1934 ifp->if_flags &= ~IFF_PROMISC;
1936 if (ifp->if_ioctl) {
1937 ifnet_serialize_all(ifp);
1938 ifp->if_ioctl(ifp, cmd, data, cred);
1939 ifnet_deserialize_all(ifp);
1943 getmicrotime(&ifp->if_lastchange);
1947 error = priv_check_cred(cred, PRIV_ROOT, 0);
1950 if (ifr->ifr_reqcap & ~ifp->if_capabilities) {
1954 ifnet_serialize_all(ifp);
1955 ifp->if_ioctl(ifp, cmd, data, cred);
1956 ifnet_deserialize_all(ifp);
1960 error = priv_check_cred(cred, PRIV_ROOT, 0);
1963 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL);
1966 if (new_name[0] == '\0') {
1970 if (ifunit(new_name) != NULL) {
1975 EVENTHANDLER_INVOKE(ifnet_detach_event, ifp);
1977 /* Announce the departure of the interface. */
1978 rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
1980 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname));
1981 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
1982 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
1983 namelen = strlen(new_name);
1984 onamelen = sdl->sdl_nlen;
1986 * Move the address if needed. This is safe because we
1987 * allocate space for a name of length IFNAMSIZ when we
1988 * create this in if_attach().
1990 if (namelen != onamelen) {
1991 bcopy(sdl->sdl_data + onamelen,
1992 sdl->sdl_data + namelen, sdl->sdl_alen);
1994 bcopy(new_name, sdl->sdl_data, namelen);
1995 sdl->sdl_nlen = namelen;
1996 sdl = (struct sockaddr_dl *)ifa->ifa_netmask;
1997 bzero(sdl->sdl_data, onamelen);
1998 while (namelen != 0)
1999 sdl->sdl_data[--namelen] = 0xff;
2001 EVENTHANDLER_INVOKE(ifnet_attach_event, ifp);
2003 /* Announce the return of the interface. */
2004 rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
2008 error = priv_check_cred(cred, PRIV_ROOT, 0);
2011 ifp->if_metric = ifr->ifr_metric;
2012 getmicrotime(&ifp->if_lastchange);
2016 error = priv_check_cred(cred, PRIV_ROOT, 0);
2019 if (ifp->if_ioctl == NULL) {
2023 ifnet_serialize_all(ifp);
2024 error = ifp->if_ioctl(ifp, cmd, data, cred);
2025 ifnet_deserialize_all(ifp);
2027 getmicrotime(&ifp->if_lastchange);
2032 u_long oldmtu = ifp->if_mtu;
2034 error = priv_check_cred(cred, PRIV_ROOT, 0);
2037 if (ifp->if_ioctl == NULL) {
2041 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) {
2045 ifnet_serialize_all(ifp);
2046 error = ifp->if_ioctl(ifp, cmd, data, cred);
2047 ifnet_deserialize_all(ifp);
2049 getmicrotime(&ifp->if_lastchange);
2053 * If the link MTU changed, do network layer specific procedure.
2055 if (ifp->if_mtu != oldmtu) {
2064 error = priv_check_cred(cred, PRIV_ROOT, 0);
2068 /* XXX need driver supplied upper limit */
2069 if (ifr->ifr_tsolen <= 0) {
2073 ifp->if_tsolen = ifr->ifr_tsolen;
2078 error = priv_check_cred(cred, PRIV_ROOT, 0);
2082 /* Don't allow group membership on non-multicast interfaces. */
2083 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2088 /* Don't let users screw up protocols' entries. */
2089 if (ifr->ifr_addr.sa_family != AF_LINK) {
2094 if (cmd == SIOCADDMULTI) {
2095 struct ifmultiaddr *ifma;
2096 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
2098 error = if_delmulti(ifp, &ifr->ifr_addr);
2101 getmicrotime(&ifp->if_lastchange);
2104 case SIOCSIFPHYADDR:
2105 case SIOCDIFPHYADDR:
2107 case SIOCSIFPHYADDR_IN6:
2109 case SIOCSLIFPHYADDR:
2111 case SIOCSIFGENERIC:
2112 error = priv_check_cred(cred, PRIV_ROOT, 0);
2115 if (ifp->if_ioctl == 0) {
2119 ifnet_serialize_all(ifp);
2120 error = ifp->if_ioctl(ifp, cmd, data, cred);
2121 ifnet_deserialize_all(ifp);
2123 getmicrotime(&ifp->if_lastchange);
2127 ifs = (struct ifstat *)data;
2128 ifs->ascii[0] = '\0';
2130 case SIOCGIFPSRCADDR:
2131 case SIOCGIFPDSTADDR:
2132 case SIOCGLIFPHYADDR:
2134 case SIOCGIFGENERIC:
2135 if (ifp->if_ioctl == NULL) {
2139 ifnet_serialize_all(ifp);
2140 error = ifp->if_ioctl(ifp, cmd, data, cred);
2141 ifnet_deserialize_all(ifp);
2145 error = priv_check_cred(cred, PRIV_ROOT, 0);
2148 error = if_setlladdr(ifp, ifr->ifr_addr.sa_data,
2149 ifr->ifr_addr.sa_len);
2150 EVENTHANDLER_INVOKE(iflladdr_event, ifp);
2154 oif_flags = ifp->if_flags;
2155 if (so->so_proto == 0) {
2159 error = so_pru_control_direct(so, cmd, data, ifp);
2161 if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
2163 DELAY(100);/* XXX: temporary workaround for fxp issue*/
2164 if (ifp->if_flags & IFF_UP) {
2179 * Set/clear promiscuous mode on interface ifp based on the truth value
2180 * of pswitch. The calls are reference counted so that only the first
2181 * "on" request actually has an effect, as does the final "off" request.
2182 * Results are undefined if the "off" and "on" requests are not matched.
2185 ifpromisc(struct ifnet *ifp, int pswitch)
2191 oldflags = ifp->if_flags;
2192 if (ifp->if_flags & IFF_PPROMISC) {
2193 /* Do nothing if device is in permanently promiscuous mode */
2194 ifp->if_pcount += pswitch ? 1 : -1;
2199 * If the device is not configured up, we cannot put it in
2202 if ((ifp->if_flags & IFF_UP) == 0)
2204 if (ifp->if_pcount++ != 0)
2206 ifp->if_flags |= IFF_PROMISC;
2207 log(LOG_INFO, "%s: promiscuous mode enabled\n",
2210 if (--ifp->if_pcount > 0)
2212 ifp->if_flags &= ~IFF_PROMISC;
2213 log(LOG_INFO, "%s: promiscuous mode disabled\n",
2216 ifr.ifr_flags = ifp->if_flags;
2217 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2218 ifnet_serialize_all(ifp);
2219 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr, NULL);
2220 ifnet_deserialize_all(ifp);
2224 ifp->if_flags = oldflags;
2229 * Return interface configuration
2230 * of system. List may be used
2231 * in later ioctl's (above) to get
2232 * other information.
2235 ifconf(u_long cmd, caddr_t data, struct ucred *cred)
2237 struct ifconf *ifc = (struct ifconf *)data;
2239 struct sockaddr *sa;
2240 struct ifreq ifr, *ifrp;
2241 int space = ifc->ifc_len, error = 0;
2243 ifrp = ifc->ifc_req;
2246 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2247 struct ifaddr_container *ifac, *ifac_mark;
2248 struct ifaddr_marker mark;
2249 struct ifaddrhead *head;
2252 if (space <= sizeof ifr)
2256 * Zero the stack declared structure first to prevent
2257 * memory disclosure.
2259 bzero(&ifr, sizeof(ifr));
2260 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name))
2261 >= sizeof(ifr.ifr_name)) {
2262 error = ENAMETOOLONG;
2267 * Add a marker, since copyout() could block and during that
2268 * period the list could be changed. Inserting the marker to
2269 * the header of the list will not cause trouble for the code
2270 * assuming that the first element of the list is AF_LINK; the
2271 * marker will be moved to the next position w/o blocking.
2273 ifa_marker_init(&mark, ifp);
2274 ifac_mark = &mark.ifac;
2275 head = &ifp->if_addrheads[mycpuid];
2278 TAILQ_INSERT_HEAD(head, ifac_mark, ifa_link);
2279 while ((ifac = TAILQ_NEXT(ifac_mark, ifa_link)) != NULL) {
2280 struct ifaddr *ifa = ifac->ifa;
2282 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2283 TAILQ_INSERT_AFTER(head, ifac, ifac_mark, ifa_link);
2286 if (ifa->ifa_addr->sa_family == AF_UNSPEC)
2289 if (space <= sizeof ifr)
2292 if (cred->cr_prison &&
2293 prison_if(cred, sa))
2297 * Keep a reference on this ifaddr, so that it will
2298 * not be destroyed when its address is copied to
2299 * the userland, which could block.
2302 if (sa->sa_len <= sizeof(*sa)) {
2304 error = copyout(&ifr, ifrp, sizeof ifr);
2307 if (space < (sizeof ifr) + sa->sa_len -
2312 space -= sa->sa_len - sizeof(*sa);
2313 error = copyout(&ifr, ifrp,
2314 sizeof ifr.ifr_name);
2316 error = copyout(sa, &ifrp->ifr_addr,
2318 ifrp = (struct ifreq *)
2319 (sa->sa_len + (caddr_t)&ifrp->ifr_addr);
2324 space -= sizeof ifr;
2326 TAILQ_REMOVE(head, ifac_mark, ifa_link);
2330 bzero(&ifr.ifr_addr, sizeof ifr.ifr_addr);
2331 error = copyout(&ifr, ifrp, sizeof ifr);
2334 space -= sizeof ifr;
2340 ifc->ifc_len -= space;
2345 * Just like if_promisc(), but for all-multicast-reception mode.
2348 if_allmulti(struct ifnet *ifp, int onswitch)
2356 if (ifp->if_amcount++ == 0) {
2357 ifp->if_flags |= IFF_ALLMULTI;
2358 ifr.ifr_flags = ifp->if_flags;
2359 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2360 ifnet_serialize_all(ifp);
2361 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2363 ifnet_deserialize_all(ifp);
2366 if (ifp->if_amcount > 1) {
2369 ifp->if_amcount = 0;
2370 ifp->if_flags &= ~IFF_ALLMULTI;
2371 ifr.ifr_flags = ifp->if_flags;
2372 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2373 ifnet_serialize_all(ifp);
2374 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2376 ifnet_deserialize_all(ifp);
2388 * Add a multicast listenership to the interface in question.
2389 * The link layer provides a routine which converts
2392 if_addmulti_serialized(struct ifnet *ifp, struct sockaddr *sa,
2393 struct ifmultiaddr **retifma)
2395 struct sockaddr *llsa, *dupsa;
2397 struct ifmultiaddr *ifma;
2399 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2402 * If the matching multicast address already exists
2403 * then don't add a new one, just add a reference
2405 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2406 if (sa_equal(sa, ifma->ifma_addr)) {
2407 ifma->ifma_refcount++;
2415 * Give the link layer a chance to accept/reject it, and also
2416 * find out which AF_LINK address this maps to, if it isn't one
2419 if (ifp->if_resolvemulti) {
2420 error = ifp->if_resolvemulti(ifp, &llsa, sa);
2427 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2428 dupsa = kmalloc(sa->sa_len, M_IFMADDR, M_INTWAIT);
2429 bcopy(sa, dupsa, sa->sa_len);
2431 ifma->ifma_addr = dupsa;
2432 ifma->ifma_lladdr = llsa;
2433 ifma->ifma_ifp = ifp;
2434 ifma->ifma_refcount = 1;
2435 ifma->ifma_protospec = NULL;
2436 rt_newmaddrmsg(RTM_NEWMADDR, ifma);
2438 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2443 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2444 if (sa_equal(ifma->ifma_addr, llsa))
2448 ifma->ifma_refcount++;
2450 ifma = kmalloc(sizeof *ifma, M_IFMADDR, M_INTWAIT);
2451 dupsa = kmalloc(llsa->sa_len, M_IFMADDR, M_INTWAIT);
2452 bcopy(llsa, dupsa, llsa->sa_len);
2453 ifma->ifma_addr = dupsa;
2454 ifma->ifma_ifp = ifp;
2455 ifma->ifma_refcount = 1;
2456 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
2460 * We are certain we have added something, so call down to the
2461 * interface to let them know about it.
2464 ifp->if_ioctl(ifp, SIOCADDMULTI, 0, NULL);
2470 if_addmulti(struct ifnet *ifp, struct sockaddr *sa,
2471 struct ifmultiaddr **retifma)
2475 ifnet_serialize_all(ifp);
2476 error = if_addmulti_serialized(ifp, sa, retifma);
2477 ifnet_deserialize_all(ifp);
2483 * Remove a reference to a multicast address on this interface. Yell
2484 * if the request does not match an existing membership.
2487 if_delmulti_serialized(struct ifnet *ifp, struct sockaddr *sa)
2489 struct ifmultiaddr *ifma;
2491 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2493 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2494 if (sa_equal(sa, ifma->ifma_addr))
2499 if (ifma->ifma_refcount > 1) {
2500 ifma->ifma_refcount--;
2504 rt_newmaddrmsg(RTM_DELMADDR, ifma);
2505 sa = ifma->ifma_lladdr;
2506 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2508 * Make sure the interface driver is notified
2509 * in the case of a link layer mcast group being left.
2511 if (ifma->ifma_addr->sa_family == AF_LINK && sa == NULL)
2512 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2513 kfree(ifma->ifma_addr, M_IFMADDR);
2514 kfree(ifma, M_IFMADDR);
2519 * Now look for the link-layer address which corresponds to
2520 * this network address. It had been squirreled away in
2521 * ifma->ifma_lladdr for this purpose (so we don't have
2522 * to call ifp->if_resolvemulti() again), and we saved that
2523 * value in sa above. If some nasty deleted the
2524 * link-layer address out from underneath us, we can deal because
2525 * the address we stored was is not the same as the one which was
2526 * in the record for the link-layer address. (So we don't complain
2529 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2530 if (sa_equal(sa, ifma->ifma_addr))
2535 if (ifma->ifma_refcount > 1) {
2536 ifma->ifma_refcount--;
2540 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
2541 ifp->if_ioctl(ifp, SIOCDELMULTI, 0, NULL);
2542 kfree(ifma->ifma_addr, M_IFMADDR);
2543 kfree(sa, M_IFMADDR);
2544 kfree(ifma, M_IFMADDR);
2550 if_delmulti(struct ifnet *ifp, struct sockaddr *sa)
2554 ifnet_serialize_all(ifp);
2555 error = if_delmulti_serialized(ifp, sa);
2556 ifnet_deserialize_all(ifp);
2562 * Delete all multicast group membership for an interface.
2563 * Should be used to quickly flush all multicast filters.
2566 if_delallmulti_serialized(struct ifnet *ifp)
2568 struct ifmultiaddr *ifma, mark;
2571 ASSERT_IFNET_SERIALIZED_ALL(ifp);
2573 bzero(&sa, sizeof(sa));
2574 sa.sa_family = AF_UNSPEC;
2575 sa.sa_len = sizeof(sa);
2577 bzero(&mark, sizeof(mark));
2578 mark.ifma_addr = &sa;
2580 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, &mark, ifma_link);
2581 while ((ifma = TAILQ_NEXT(&mark, ifma_link)) != NULL) {
2582 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2583 TAILQ_INSERT_AFTER(&ifp->if_multiaddrs, ifma, &mark,
2586 if (ifma->ifma_addr->sa_family == AF_UNSPEC)
2589 if_delmulti_serialized(ifp, ifma->ifma_addr);
2591 TAILQ_REMOVE(&ifp->if_multiaddrs, &mark, ifma_link);
2596 * Set the link layer address on an interface.
2598 * At this time we only support certain types of interfaces,
2599 * and we don't allow the length of the address to change.
2602 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
2604 struct sockaddr_dl *sdl;
2607 sdl = IF_LLSOCKADDR(ifp);
2610 if (len != sdl->sdl_alen) /* don't allow length to change */
2612 switch (ifp->if_type) {
2613 case IFT_ETHER: /* these types use struct arpcom */
2616 case IFT_IEEE8023ADLAG:
2617 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
2618 bcopy(lladdr, LLADDR(sdl), len);
2624 * If the interface is already up, we need
2625 * to re-init it in order to reprogram its
2628 ifnet_serialize_all(ifp);
2629 if ((ifp->if_flags & IFF_UP) != 0) {
2631 struct ifaddr_container *ifac;
2634 ifp->if_flags &= ~IFF_UP;
2635 ifr.ifr_flags = ifp->if_flags;
2636 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2637 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2639 ifp->if_flags |= IFF_UP;
2640 ifr.ifr_flags = ifp->if_flags;
2641 ifr.ifr_flagshigh = ifp->if_flags >> 16;
2642 ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr,
2646 * Also send gratuitous ARPs to notify other nodes about
2647 * the address change.
2649 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2650 struct ifaddr *ifa = ifac->ifa;
2652 if (ifa->ifa_addr != NULL &&
2653 ifa->ifa_addr->sa_family == AF_INET)
2654 arp_gratuitous(ifp, ifa);
2658 ifnet_deserialize_all(ifp);
2662 struct ifmultiaddr *
2663 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp)
2665 struct ifmultiaddr *ifma;
2667 /* TODO: need ifnet_serialize_main */
2668 ifnet_serialize_all(ifp);
2669 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
2670 if (sa_equal(ifma->ifma_addr, sa))
2672 ifnet_deserialize_all(ifp);
2678 * This function locates the first real ethernet MAC from a network
2679 * card and loads it into node, returning 0 on success or ENOENT if
2680 * no suitable interfaces were found. It is used by the uuid code to
2681 * generate a unique 6-byte number.
2684 if_getanyethermac(uint16_t *node, int minlen)
2687 struct sockaddr_dl *sdl;
2690 TAILQ_FOREACH(ifp, &ifnetlist, if_link) {
2691 if (ifp->if_type != IFT_ETHER)
2693 sdl = IF_LLSOCKADDR(ifp);
2694 if (sdl->sdl_alen < minlen)
2696 bcopy(((struct arpcom *)ifp->if_softc)->ac_enaddr, node,
2706 * The name argument must be a pointer to storage which will last as
2707 * long as the interface does. For physical devices, the result of
2708 * device_get_name(dev) is a good choice and for pseudo-devices a
2709 * static string works well.
2712 if_initname(struct ifnet *ifp, const char *name, int unit)
2714 ifp->if_dname = name;
2715 ifp->if_dunit = unit;
2716 if (unit != IF_DUNIT_NONE)
2717 ksnprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit);
2719 strlcpy(ifp->if_xname, name, IFNAMSIZ);
2723 if_printf(struct ifnet *ifp, const char *fmt, ...)
2728 retval = kprintf("%s: ", ifp->if_xname);
2729 __va_start(ap, fmt);
2730 retval += kvprintf(fmt, ap);
2736 if_alloc(uint8_t type)
2742 * XXX temporary hack until arpcom is setup in if_l2com
2744 if (type == IFT_ETHER)
2745 size = sizeof(struct arpcom);
2747 size = sizeof(struct ifnet);
2749 ifp = kmalloc(size, M_IFNET, M_WAITOK|M_ZERO);
2751 ifp->if_type = type;
2753 if (if_com_alloc[type] != NULL) {
2754 ifp->if_l2com = if_com_alloc[type](type, ifp);
2755 if (ifp->if_l2com == NULL) {
2756 kfree(ifp, M_IFNET);
2764 if_free(struct ifnet *ifp)
2766 kfree(ifp, M_IFNET);
2770 ifq_set_classic(struct ifaltq *ifq)
2772 ifq_set_methods(ifq, ifq->altq_ifp->if_mapsubq,
2773 ifsq_classic_enqueue, ifsq_classic_dequeue, ifsq_classic_request);
2777 ifq_set_methods(struct ifaltq *ifq, altq_mapsubq_t mapsubq,
2778 ifsq_enqueue_t enqueue, ifsq_dequeue_t dequeue, ifsq_request_t request)
2782 KASSERT(mapsubq != NULL, ("mapsubq is not specified"));
2783 KASSERT(enqueue != NULL, ("enqueue is not specified"));
2784 KASSERT(dequeue != NULL, ("dequeue is not specified"));
2785 KASSERT(request != NULL, ("request is not specified"));
2787 ifq->altq_mapsubq = mapsubq;
2788 for (q = 0; q < ifq->altq_subq_cnt; ++q) {
2789 struct ifaltq_subque *ifsq = &ifq->altq_subq[q];
2791 ifsq->ifsq_enqueue = enqueue;
2792 ifsq->ifsq_dequeue = dequeue;
2793 ifsq->ifsq_request = request;
2798 ifsq_norm_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2801 classq_add(&ifsq->ifsq_norm, m);
2802 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2806 ifsq_prio_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m)
2809 classq_add(&ifsq->ifsq_prio, m);
2810 ALTQ_SQ_CNTR_INC(ifsq, m->m_pkthdr.len);
2811 ALTQ_SQ_PRIO_CNTR_INC(ifsq, m->m_pkthdr.len);
2814 static struct mbuf *
2815 ifsq_norm_dequeue(struct ifaltq_subque *ifsq)
2819 m = classq_get(&ifsq->ifsq_norm);
2821 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2825 static struct mbuf *
2826 ifsq_prio_dequeue(struct ifaltq_subque *ifsq)
2830 m = classq_get(&ifsq->ifsq_prio);
2832 ALTQ_SQ_CNTR_DEC(ifsq, m->m_pkthdr.len);
2833 ALTQ_SQ_PRIO_CNTR_DEC(ifsq, m->m_pkthdr.len);
2839 ifsq_classic_enqueue(struct ifaltq_subque *ifsq, struct mbuf *m,
2840 struct altq_pktattr *pa __unused)
2845 if (ifsq->ifsq_len >= ifsq->ifsq_maxlen ||
2846 ifsq->ifsq_bcnt >= ifsq->ifsq_maxbcnt) {
2847 struct mbuf *m_drop;
2849 if (m->m_flags & M_PRIO) {
2851 if (ifsq->ifsq_prio_len < (ifsq->ifsq_maxlen >> 1) &&
2852 ifsq->ifsq_prio_bcnt < (ifsq->ifsq_maxbcnt >> 1)) {
2853 /* Try dropping some from normal queue. */
2854 m_drop = ifsq_norm_dequeue(ifsq);
2857 m_drop = ifsq_prio_dequeue(ifsq);
2859 m_drop = ifsq_norm_dequeue(ifsq);
2861 if (m_drop != NULL) {
2862 IFNET_STAT_INC(ifsq->ifsq_ifp, oqdrops, 1);
2867 * No old packets could be dropped!
2868 * NOTE: Caller increases oqdrops.
2873 if (m->m_flags & M_PRIO)
2874 ifsq_prio_enqueue(ifsq, m);
2876 ifsq_norm_enqueue(ifsq, m);
2882 ifsq_classic_dequeue(struct ifaltq_subque *ifsq, int op)
2888 m = classq_head(&ifsq->ifsq_prio);
2890 m = classq_head(&ifsq->ifsq_norm);
2894 m = ifsq_prio_dequeue(ifsq);
2896 m = ifsq_norm_dequeue(ifsq);
2900 panic("unsupported ALTQ dequeue op: %d", op);
2906 ifsq_classic_request(struct ifaltq_subque *ifsq, int req, void *arg)
2913 m = ifsq_classic_dequeue(ifsq, ALTDQ_REMOVE);
2921 panic("unsupported ALTQ request: %d", req);
2927 ifsq_ifstart_try(struct ifaltq_subque *ifsq, int force_sched)
2929 struct ifnet *ifp = ifsq_get_ifp(ifsq);
2930 int running = 0, need_sched;
2933 * Try to do direct ifnet.if_start on the subqueue first, if there is
2934 * contention on the subqueue hardware serializer, ifnet.if_start on
2935 * the subqueue will be scheduled on the subqueue owner CPU.
2937 if (!ifsq_tryserialize_hw(ifsq)) {
2939 * Subqueue hardware serializer contention happened,
2940 * ifnet.if_start on the subqueue is scheduled on
2941 * the subqueue owner CPU, and we keep going.
2943 ifsq_ifstart_schedule(ifsq, 1);
2947 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq)) {
2948 ifp->if_start(ifp, ifsq);
2949 if ((ifp->if_flags & IFF_RUNNING) && !ifsq_is_oactive(ifsq))
2952 need_sched = ifsq_ifstart_need_schedule(ifsq, running);
2954 ifsq_deserialize_hw(ifsq);
2958 * More data need to be transmitted, ifnet.if_start on the
2959 * subqueue is scheduled on the subqueue owner CPU, and we
2961 * NOTE: ifnet.if_start subqueue interlock is not released.
2963 ifsq_ifstart_schedule(ifsq, force_sched);
2968 * Subqeue packets staging mechanism:
2970 * The packets enqueued into the subqueue are staged to a certain amount
2971 * before the ifnet.if_start on the subqueue is called. In this way, the
2972 * driver could avoid writing to hardware registers upon every packet,
2973 * instead, hardware registers could be written when certain amount of
2974 * packets are put onto hardware TX ring. The measurement on several modern
2975 * NICs (emx(4), igb(4), bnx(4), bge(4), jme(4)) shows that the hardware
2976 * registers writing aggregation could save ~20% CPU time when 18bytes UDP
2977 * datagrams are transmitted at 1.48Mpps. The performance improvement by
2978 * hardware registers writing aggeregation is also mentioned by Luigi Rizzo's
2979 * netmap paper (http://info.iet.unipi.it/~luigi/netmap/).
2981 * Subqueue packets staging is performed for two entry points into drivers'
2982 * transmission function:
2983 * - Direct ifnet.if_start calling on the subqueue, i.e. ifsq_ifstart_try()
2984 * - ifnet.if_start scheduling on the subqueue, i.e. ifsq_ifstart_schedule()
2986 * Subqueue packets staging will be stopped upon any of the following
2988 * - If the count of packets enqueued on the current CPU is great than or
2989 * equal to ifsq_stage_cntmax. (XXX this should be per-interface)
2990 * - If the total length of packets enqueued on the current CPU is great
2991 * than or equal to the hardware's MTU - max_protohdr. max_protohdr is
2992 * cut from the hardware's MTU mainly bacause a full TCP segment's size
2993 * is usually less than hardware's MTU.
2994 * - ifsq_ifstart_schedule() is not pending on the current CPU and
2995 * ifnet.if_start subqueue interlock (ifaltq_subq.ifsq_started) is not
2997 * - The if_start_rollup(), which is registered as low priority netisr
2998 * rollup function, is called; probably because no more work is pending
3002 * Currently subqueue packet staging is only performed in netisr threads.
3005 ifq_dispatch(struct ifnet *ifp, struct mbuf *m, struct altq_pktattr *pa)
3007 struct ifaltq *ifq = &ifp->if_snd;
3008 struct ifaltq_subque *ifsq;
3009 int error, start = 0, len, mcast = 0, avoid_start = 0;
3010 struct ifsubq_stage_head *head = NULL;
3011 struct ifsubq_stage *stage = NULL;
3012 struct globaldata *gd = mycpu;
3013 struct thread *td = gd->gd_curthread;
3015 crit_enter_quick(td);
3017 ifsq = ifq_map_subq(ifq, gd->gd_cpuid);
3018 ASSERT_ALTQ_SQ_NOT_SERIALIZED_HW(ifsq);
3020 len = m->m_pkthdr.len;
3021 if (m->m_flags & M_MCAST)
3024 if (td->td_type == TD_TYPE_NETISR) {
3025 head = &ifsubq_stage_heads[mycpuid];
3026 stage = ifsq_get_stage(ifsq, mycpuid);
3029 stage->stg_len += len;
3030 if (stage->stg_cnt < ifsq_stage_cntmax &&
3031 stage->stg_len < (ifp->if_mtu - max_protohdr))
3036 error = ifsq_enqueue_locked(ifsq, m, pa);
3038 IFNET_STAT_INC(ifp, oqdrops, 1);
3039 if (!ifsq_data_ready(ifsq)) {
3040 ALTQ_SQ_UNLOCK(ifsq);
3041 crit_exit_quick(td);
3046 if (!ifsq_is_started(ifsq)) {
3048 ALTQ_SQ_UNLOCK(ifsq);
3051 if ((stage->stg_flags & IFSQ_STAGE_FLAG_QUED) == 0)
3052 ifsq_stage_insert(head, stage);
3054 IFNET_STAT_INC(ifp, obytes, len);
3056 IFNET_STAT_INC(ifp, omcasts, 1);
3057 crit_exit_quick(td);
3062 * Hold the subqueue interlock of ifnet.if_start
3064 ifsq_set_started(ifsq);
3067 ALTQ_SQ_UNLOCK(ifsq);
3070 IFNET_STAT_INC(ifp, obytes, len);
3072 IFNET_STAT_INC(ifp, omcasts, 1);
3075 if (stage != NULL) {
3076 if (!start && (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)) {
3077 KKASSERT(stage->stg_flags & IFSQ_STAGE_FLAG_QUED);
3079 ifsq_stage_remove(head, stage);
3080 ifsq_ifstart_schedule(ifsq, 1);
3082 crit_exit_quick(td);
3086 if (stage->stg_flags & IFSQ_STAGE_FLAG_QUED) {
3087 ifsq_stage_remove(head, stage);
3095 crit_exit_quick(td);
3099 ifsq_ifstart_try(ifsq, 0);
3101 crit_exit_quick(td);
3106 ifa_create(int size)
3111 KASSERT(size >= sizeof(*ifa), ("ifaddr size too small"));
3113 ifa = kmalloc(size, M_IFADDR, M_INTWAIT | M_ZERO);
3114 ifa->ifa_containers =
3115 kmalloc_cachealign(ncpus * sizeof(struct ifaddr_container),
3116 M_IFADDR, M_INTWAIT | M_ZERO);
3118 ifa->ifa_ncnt = ncpus;
3119 for (i = 0; i < ncpus; ++i) {
3120 struct ifaddr_container *ifac = &ifa->ifa_containers[i];
3122 ifac->ifa_magic = IFA_CONTAINER_MAGIC;
3124 ifac->ifa_refcnt = 1;
3127 kprintf("alloc ifa %p %d\n", ifa, size);
3133 ifac_free(struct ifaddr_container *ifac, int cpu_id)
3135 struct ifaddr *ifa = ifac->ifa;
3137 KKASSERT(ifac->ifa_magic == IFA_CONTAINER_MAGIC);
3138 KKASSERT(ifac->ifa_refcnt == 0);
3139 KASSERT(ifac->ifa_listmask == 0,
3140 ("ifa is still on %#x lists", ifac->ifa_listmask));
3142 ifac->ifa_magic = IFA_CONTAINER_DEAD;
3144 #ifdef IFADDR_DEBUG_VERBOSE
3145 kprintf("try free ifa %p cpu_id %d\n", ifac->ifa, cpu_id);
3148 KASSERT(ifa->ifa_ncnt > 0 && ifa->ifa_ncnt <= ncpus,
3149 ("invalid # of ifac, %d", ifa->ifa_ncnt));
3150 if (atomic_fetchadd_int(&ifa->ifa_ncnt, -1) == 1) {
3152 kprintf("free ifa %p\n", ifa);
3154 kfree(ifa->ifa_containers, M_IFADDR);
3155 kfree(ifa, M_IFADDR);
3160 ifa_iflink_dispatch(netmsg_t nmsg)
3162 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3163 struct ifaddr *ifa = msg->ifa;
3164 struct ifnet *ifp = msg->ifp;
3166 struct ifaddr_container *ifac;
3170 ifac = &ifa->ifa_containers[cpu];
3171 ASSERT_IFAC_VALID(ifac);
3172 KASSERT((ifac->ifa_listmask & IFA_LIST_IFADDRHEAD) == 0,
3173 ("ifaddr is on if_addrheads"));
3175 ifac->ifa_listmask |= IFA_LIST_IFADDRHEAD;
3177 TAILQ_INSERT_TAIL(&ifp->if_addrheads[cpu], ifac, ifa_link);
3179 TAILQ_INSERT_HEAD(&ifp->if_addrheads[cpu], ifac, ifa_link);
3183 netisr_forwardmsg(&nmsg->base, cpu + 1);
3187 ifa_iflink(struct ifaddr *ifa, struct ifnet *ifp, int tail)
3189 struct netmsg_ifaddr msg;
3191 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3192 0, ifa_iflink_dispatch);
3197 netisr_domsg(&msg.base, 0);
3201 ifa_ifunlink_dispatch(netmsg_t nmsg)
3203 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3204 struct ifaddr *ifa = msg->ifa;
3205 struct ifnet *ifp = msg->ifp;
3207 struct ifaddr_container *ifac;
3211 ifac = &ifa->ifa_containers[cpu];
3212 ASSERT_IFAC_VALID(ifac);
3213 KASSERT(ifac->ifa_listmask & IFA_LIST_IFADDRHEAD,
3214 ("ifaddr is not on if_addrhead"));
3216 TAILQ_REMOVE(&ifp->if_addrheads[cpu], ifac, ifa_link);
3217 ifac->ifa_listmask &= ~IFA_LIST_IFADDRHEAD;
3221 netisr_forwardmsg(&nmsg->base, cpu + 1);
3225 ifa_ifunlink(struct ifaddr *ifa, struct ifnet *ifp)
3227 struct netmsg_ifaddr msg;
3229 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3230 0, ifa_ifunlink_dispatch);
3234 netisr_domsg(&msg.base, 0);
3238 ifa_destroy_dispatch(netmsg_t nmsg)
3240 struct netmsg_ifaddr *msg = (struct netmsg_ifaddr *)nmsg;
3243 netisr_forwardmsg(&nmsg->base, mycpuid + 1);
3247 ifa_destroy(struct ifaddr *ifa)
3249 struct netmsg_ifaddr msg;
3251 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
3252 0, ifa_destroy_dispatch);
3255 netisr_domsg(&msg.base, 0);
3259 if_start_rollup(void)
3261 struct ifsubq_stage_head *head = &ifsubq_stage_heads[mycpuid];
3262 struct ifsubq_stage *stage;
3266 while ((stage = TAILQ_FIRST(&head->stg_head)) != NULL) {
3267 struct ifaltq_subque *ifsq = stage->stg_subq;
3270 if (stage->stg_flags & IFSQ_STAGE_FLAG_SCHED)
3272 ifsq_stage_remove(head, stage);
3275 ifsq_ifstart_schedule(ifsq, 1);
3280 if (!ifsq_is_started(ifsq)) {
3282 * Hold the subqueue interlock of
3285 ifsq_set_started(ifsq);
3288 ALTQ_SQ_UNLOCK(ifsq);
3291 ifsq_ifstart_try(ifsq, 1);
3293 KKASSERT((stage->stg_flags &
3294 (IFSQ_STAGE_FLAG_QUED | IFSQ_STAGE_FLAG_SCHED)) == 0);
3301 ifnetinit(void *dummy __unused)
3305 for (i = 0; i < ncpus; ++i)
3306 TAILQ_INIT(&ifsubq_stage_heads[i].stg_head);
3307 netisr_register_rollup(if_start_rollup, NETISR_ROLLUP_PRIO_IFSTART);
3311 if_register_com_alloc(u_char type,
3312 if_com_alloc_t *a, if_com_free_t *f)
3315 KASSERT(if_com_alloc[type] == NULL,
3316 ("if_register_com_alloc: %d already registered", type));
3317 KASSERT(if_com_free[type] == NULL,
3318 ("if_register_com_alloc: %d free already registered", type));
3320 if_com_alloc[type] = a;
3321 if_com_free[type] = f;
3325 if_deregister_com_alloc(u_char type)
3328 KASSERT(if_com_alloc[type] != NULL,
3329 ("if_deregister_com_alloc: %d not registered", type));
3330 KASSERT(if_com_free[type] != NULL,
3331 ("if_deregister_com_alloc: %d free not registered", type));
3332 if_com_alloc[type] = NULL;
3333 if_com_free[type] = NULL;
3337 ifq_set_maxlen(struct ifaltq *ifq, int len)
3339 ifq->altq_maxlen = len + (ncpus * ifsq_stage_cntmax);
3343 ifq_mapsubq_default(struct ifaltq *ifq __unused, int cpuid __unused)
3345 return ALTQ_SUBQ_INDEX_DEFAULT;
3349 ifq_mapsubq_modulo(struct ifaltq *ifq, int cpuid)
3352 return (cpuid % ifq->altq_subq_mappriv);
3356 ifsq_watchdog(void *arg)
3358 struct ifsubq_watchdog *wd = arg;
3361 if (__predict_true(wd->wd_timer == 0 || --wd->wd_timer))
3364 ifp = ifsq_get_ifp(wd->wd_subq);
3365 if (ifnet_tryserialize_all(ifp)) {
3366 wd->wd_watchdog(wd->wd_subq);
3367 ifnet_deserialize_all(ifp);
3369 /* try again next timeout */
3373 ifsq_watchdog_reset(wd);
3377 ifsq_watchdog_reset(struct ifsubq_watchdog *wd)
3379 callout_reset_bycpu(&wd->wd_callout, hz, ifsq_watchdog, wd,
3380 ifsq_get_cpuid(wd->wd_subq));
3384 ifsq_watchdog_init(struct ifsubq_watchdog *wd, struct ifaltq_subque *ifsq,
3385 ifsq_watchdog_t watchdog)
3387 callout_init_mp(&wd->wd_callout);
3390 wd->wd_watchdog = watchdog;
3394 ifsq_watchdog_start(struct ifsubq_watchdog *wd)
3397 ifsq_watchdog_reset(wd);
3401 ifsq_watchdog_stop(struct ifsubq_watchdog *wd)
3404 callout_stop(&wd->wd_callout);
3410 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3411 ("try holding ifnet lock in netisr"));
3412 mtx_lock(&ifnet_mtx);
3418 KASSERT(curthread->td_type != TD_TYPE_NETISR,
3419 ("try holding ifnet lock in netisr"));
3420 mtx_unlock(&ifnet_mtx);
3423 static struct ifnet_array *
3424 ifnet_array_alloc(int count)
3426 struct ifnet_array *arr;
3428 arr = kmalloc(__offsetof(struct ifnet_array, ifnet_arr[count]),
3430 arr->ifnet_count = count;
3436 ifnet_array_free(struct ifnet_array *arr)
3438 if (arr == &ifnet_array0)
3440 kfree(arr, M_IFNET);
3443 static struct ifnet_array *
3444 ifnet_array_add(struct ifnet *ifp, const struct ifnet_array *old_arr)
3446 struct ifnet_array *arr;
3449 KASSERT(old_arr->ifnet_count >= 0,
3450 ("invalid ifnet array count %d", old_arr->ifnet_count));
3451 count = old_arr->ifnet_count + 1;
3452 arr = ifnet_array_alloc(count);
3455 * Save the old ifnet array and append this ifp to the end of
3456 * the new ifnet array.
3458 for (i = 0; i < old_arr->ifnet_count; ++i) {
3459 KASSERT(old_arr->ifnet_arr[i] != ifp,
3460 ("%s is already in ifnet array", ifp->if_xname));
3461 arr->ifnet_arr[i] = old_arr->ifnet_arr[i];
3463 KASSERT(i == count - 1,
3464 ("add %s, ifnet array index mismatch, should be %d, but got %d",
3465 ifp->if_xname, count - 1, i));
3466 arr->ifnet_arr[i] = ifp;
3471 static struct ifnet_array *
3472 ifnet_array_del(struct ifnet *ifp, const struct ifnet_array *old_arr)
3474 struct ifnet_array *arr;
3475 int count, i, idx, found = 0;
3477 KASSERT(old_arr->ifnet_count > 0,
3478 ("invalid ifnet array count %d", old_arr->ifnet_count));
3479 count = old_arr->ifnet_count - 1;
3480 arr = ifnet_array_alloc(count);
3483 * Save the old ifnet array, but skip this ifp.
3486 for (i = 0; i < old_arr->ifnet_count; ++i) {
3487 if (old_arr->ifnet_arr[i] == ifp) {
3489 ("dup %s is in ifnet array", ifp->if_xname));
3493 KASSERT(idx < count,
3494 ("invalid ifnet array index %d, count %d", idx, count));
3495 arr->ifnet_arr[idx] = old_arr->ifnet_arr[i];
3498 KASSERT(found, ("%s is not in ifnet array", ifp->if_xname));
3499 KASSERT(idx == count,
3500 ("del %s, ifnet array count mismatch, should be %d, but got %d ",
3501 ifp->if_xname, count, idx));
3506 const struct ifnet_array *
3507 ifnet_array_get(void)
3509 const struct ifnet_array *ret;
3511 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3513 /* Make sure 'ret' is really used. */
3519 ifnet_array_isempty(void)
3521 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
3522 if (ifnet_array->ifnet_count == 0)
3529 ifa_marker_init(struct ifaddr_marker *mark, struct ifnet *ifp)
3533 memset(mark, 0, sizeof(*mark));
3536 mark->ifac.ifa = ifa;
3538 ifa->ifa_addr = &mark->addr;
3539 ifa->ifa_dstaddr = &mark->dstaddr;
3540 ifa->ifa_netmask = &mark->netmask;
3545 if_ringcnt_fixup(int ring_cnt, int ring_cntmax)
3548 KASSERT(ring_cntmax > 0, ("invalid ring count max %d", ring_cntmax));
3550 if (ring_cnt <= 0 || ring_cnt > ring_cntmax)
3551 ring_cnt = ring_cntmax;
3552 if (ring_cnt > netisr_ncpus)
3553 ring_cnt = netisr_ncpus;
3558 if_ringmap_set_grid(device_t dev, struct if_ringmap *rm, int grid)
3562 KASSERT(grid > 0, ("invalid if_ringmap grid %d", grid));
3563 KASSERT(grid >= rm->rm_cnt, ("invalid if_ringmap grid %d, count %d",
3567 offset = (rm->rm_grid * device_get_unit(dev)) % netisr_ncpus;
3568 for (i = 0; i < rm->rm_cnt; ++i) {
3569 rm->rm_cpumap[i] = offset + i;
3570 KASSERT(rm->rm_cpumap[i] < netisr_ncpus,
3571 ("invalid cpumap[%d] = %d, offset %d", i,
3572 rm->rm_cpumap[i], offset));
3576 static struct if_ringmap *
3577 if_ringmap_alloc_flags(device_t dev, int ring_cnt, int ring_cntmax,
3580 struct if_ringmap *rm;
3581 int i, grid = 0, prev_grid;
3583 ring_cnt = if_ringcnt_fixup(ring_cnt, ring_cntmax);
3584 rm = kmalloc(__offsetof(struct if_ringmap, rm_cpumap[ring_cnt]),
3585 M_DEVBUF, M_WAITOK | M_ZERO);
3587 rm->rm_cnt = ring_cnt;
3588 if (flags & RINGMAP_FLAG_POWEROF2)
3589 rm->rm_cnt = 1 << (fls(rm->rm_cnt) - 1);
3591 prev_grid = netisr_ncpus;
3592 for (i = 0; i < netisr_ncpus; ++i) {
3593 if (netisr_ncpus % (i + 1) != 0)
3596 grid = netisr_ncpus / (i + 1);
3597 if (rm->rm_cnt > grid) {
3602 if (rm->rm_cnt > netisr_ncpus / (i + 2))
3606 if_ringmap_set_grid(dev, rm, grid);
3612 if_ringmap_alloc(device_t dev, int ring_cnt, int ring_cntmax)
3615 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3616 RINGMAP_FLAG_NONE));
3620 if_ringmap_alloc2(device_t dev, int ring_cnt, int ring_cntmax)
3623 return (if_ringmap_alloc_flags(dev, ring_cnt, ring_cntmax,
3624 RINGMAP_FLAG_POWEROF2));
3628 if_ringmap_free(struct if_ringmap *rm)
3631 kfree(rm, M_DEVBUF);
3635 * Align the two ringmaps.
3637 * e.g. 8 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3641 * CPU 0 1 2 3 4 5 6 7
3642 * NIC_RX n0 n1 n2 n3
3647 * CPU 0 1 2 3 4 5 6 7
3648 * NIC_RX n0 n1 n2 n3
3652 if_ringmap_align(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3655 if (rm0->rm_grid > rm1->rm_grid)
3656 if_ringmap_set_grid(dev, rm1, rm0->rm_grid);
3657 else if (rm0->rm_grid < rm1->rm_grid)
3658 if_ringmap_set_grid(dev, rm0, rm1->rm_grid);
3662 if_ringmap_match(device_t dev, struct if_ringmap *rm0, struct if_ringmap *rm1)
3664 int subset_grid, cnt, divisor, mod, offset, i;
3665 struct if_ringmap *subset_rm, *rm;
3666 int old_rm0_grid, old_rm1_grid;
3668 if (rm0->rm_grid == rm1->rm_grid)
3671 /* Save grid for later use */
3672 old_rm0_grid = rm0->rm_grid;
3673 old_rm1_grid = rm1->rm_grid;
3675 if_ringmap_align(dev, rm0, rm1);
3678 * Re-shuffle rings to get more even distribution.
3680 * e.g. 12 netisrs, rm0 contains 4 rings, rm1 contains 2 rings.
3682 * CPU 0 1 2 3 4 5 6 7 8 9 10 11
3684 * NIC_RX a0 a1 a2 a3 b0 b1 b2 b3 c0 c1 c2 c3
3685 * NIC_TX A0 A1 B0 B1 C0 C1
3687 * NIC_RX d0 d1 d2 d3 e0 e1 e2 e3 f0 f1 f2 f3
3688 * NIC_TX D0 D1 E0 E1 F0 F1
3691 if (rm0->rm_cnt >= (2 * old_rm1_grid)) {
3693 subset_grid = old_rm1_grid;
3696 } else if (rm1->rm_cnt > (2 * old_rm0_grid)) {
3698 subset_grid = old_rm0_grid;
3702 /* No space to shuffle. */
3706 mod = cnt / subset_grid;
3708 divisor = netisr_ncpus / rm->rm_grid;
3709 offset = ((device_get_unit(dev) / divisor) % mod) * subset_grid;
3711 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3712 subset_rm->rm_cpumap[i] += offset;
3713 KASSERT(subset_rm->rm_cpumap[i] < netisr_ncpus,
3714 ("match: invalid cpumap[%d] = %d, offset %d",
3715 i, subset_rm->rm_cpumap[i], offset));
3718 for (i = 0; i < subset_rm->rm_cnt; ++i) {
3721 for (j = 0; j < rm->rm_cnt; ++j) {
3722 if (rm->rm_cpumap[j] == subset_rm->rm_cpumap[i])
3725 KASSERT(j < rm->rm_cnt,
3726 ("subset cpumap[%d] = %d not found in superset",
3727 i, subset_rm->rm_cpumap[i]));
3733 if_ringmap_count(const struct if_ringmap *rm)
3736 return (rm->rm_cnt);
3740 if_ringmap_cpumap(const struct if_ringmap *rm, int ring)
3743 KASSERT(ring >= 0 && ring < rm->rm_cnt, ("invalid ring %d", ring));
3744 return (rm->rm_cpumap[ring]);
3748 if_ringmap_rdrtable(const struct if_ringmap *rm, int table[], int table_nent)
3750 int i, grid_idx, grid_cnt, patch_off, patch_cnt, ncopy;
3752 KASSERT(table_nent > 0 && (table_nent & NETISR_CPUMASK) == 0,
3753 ("invalid redirect table entries %d", table_nent));
3756 for (i = 0; i < NETISR_CPUMAX; ++i) {
3757 table[i] = grid_idx++ % rm->rm_cnt;
3759 if (grid_idx == rm->rm_grid)
3764 * Make the ring distributed more evenly for the remainder
3767 * e.g. 12 netisrs, rm contains 8 rings.
3769 * Redirect table before:
3771 * 0 1 2 3 4 5 6 7 0 1 2 3 0 1 2 3
3772 * 4 5 6 7 0 1 2 3 0 1 2 3 4 5 6 7
3773 * 0 1 2 3 0 1 2 3 4 5 6 7 0 1 2 3
3776 * Redirect table after being patched (pX, patched entries):
3778 * 0 1 2 3 4 5 6 7 p0 p1 p2 p3 0 1 2 3
3779 * 4 5 6 7 p4 p5 p6 p7 0 1 2 3 4 5 6 7
3780 * p0 p1 p2 p3 0 1 2 3 4 5 6 7 p4 p5 p6 p7
3783 patch_cnt = rm->rm_grid % rm->rm_cnt;
3786 patch_off = rm->rm_grid - (rm->rm_grid % rm->rm_cnt);
3788 grid_cnt = roundup(NETISR_CPUMAX, rm->rm_grid) / rm->rm_grid;
3790 for (i = 0; i < grid_cnt; ++i) {
3793 for (j = 0; j < patch_cnt; ++j) {
3796 fix_idx = (i * rm->rm_grid) + patch_off + j;
3797 if (fix_idx >= NETISR_CPUMAX)
3799 table[fix_idx] = grid_idx++ % rm->rm_cnt;
3804 * If the device supports larger redirect table, duplicate
3805 * the first NETISR_CPUMAX entries to the rest of the table,
3806 * so that it matches upper layer's expectation:
3807 * (hash & NETISR_CPUMASK) % netisr_ncpus
3809 ncopy = table_nent / NETISR_CPUMAX;
3810 for (i = 1; i < ncopy; ++i) {
3811 memcpy(&table[i * NETISR_CPUMAX], table,
3812 NETISR_CPUMAX * sizeof(table[0]));
3814 if (if_ringmap_dumprdr) {
3815 for (i = 0; i < table_nent; ++i) {
3816 if (i != 0 && i % 16 == 0)
3818 kprintf("%03d ", table[i]);
3825 if_ringmap_cpumap_sysctl(SYSCTL_HANDLER_ARGS)
3827 struct if_ringmap *rm = arg1;
3830 for (i = 0; i < rm->rm_cnt; ++i) {
3831 int cpu = rm->rm_cpumap[i];
3833 error = SYSCTL_OUT(req, &cpu, sizeof(cpu));