2 * Copyright (c) 1982, 1986, 1989, 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 * From: @(#)if.h 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: src/sys/net/if_var.h,v 1.18.2.16 2003/04/15 18:11:19 fjoe Exp $
33 #ifndef _NET_IF_VAR_H_
34 #define _NET_IF_VAR_H_
36 #ifndef _SYS_SERIALIZE_H_
37 #include <sys/serialize.h>
43 #include <sys/mutex.h>
47 * Structures defining a network interface, providing a packet
48 * transport mechanism (ala level 0 of the PUP protocols).
50 * Each interface accepts output datagrams of a specified maximum
51 * length, and provides higher level routines with input datagrams
52 * received from its medium.
54 * Output occurs when the routine if_output is called, with four parameters:
56 * ifp->if_output(ifp, m, dst, rt)
58 * Here m is the mbuf chain to be sent and dst is the destination address.
59 * The output routine encapsulates the supplied datagram if necessary,
60 * and then transmits it on its medium.
62 * On input, each interface unwraps the data received by it, and either
63 * places it on the input queue of a internetwork datagram routine
64 * and posts the associated software interrupt, or passes the datagram to
65 * the routine if_input. It is called with four parameters:
67 * ifp->if_input(ifp, m, pi, cpuid)
69 * Here m is the mbuf chain to be received. The input routine removes the
70 * protocol dependent header if necessary. A driver may also call using
71 * custom struct pktinfo reference pi and a cpuid to take advantage of
72 * hardware supplied information. Otherwise, the defaults for pi and cpuid
75 * ifp->if_input(ifp, m, NULL, -1);
77 * Routines exist for locating interfaces by their addresses
78 * or for locating a interface on a certain network, as well as more general
79 * routing and gateway routines maintaining information used to locate
80 * interfaces. These routines live in the files if.c and route.c
84 * Forward structure declarations for function prototypes [sic].
86 struct rtentry; /* ifa_rtrequest */
89 struct lwkt_serialize;
90 struct ifaddr_container;
96 #include <sys/queue.h> /* get TAILQ macros */
98 #include <net/altq/if_altq.h>
101 #include <sys/eventhandler.h>
102 #include <sys/mbuf.h>
103 #include <sys/thread2.h>
106 #define IF_DUNIT_NONE -1
109 * we use TAILQs so that the order of instantiation is preserved in
112 TAILQ_HEAD(ifnethead, ifnet);
113 TAILQ_HEAD(ifaddrhead, ifaddr_container);
114 TAILQ_HEAD(ifmultihead, ifmultiaddr);
115 TAILQ_HEAD(ifgrouphead, ifg_group);
118 * Structure defining a mbuf queue.
121 struct mbuf *ifq_head;
122 struct mbuf *ifq_tail;
129 * Note of IFPOLL_ENABLE
130 * 1) Any file(*.c) that depends on IFPOLL_ENABLE supports in this
131 * file should include opt_ifpoll.h at its beginning.
132 * 2) When struct changes, which are conditioned by IFPOLL_ENABLE,
133 * are to be introduced, please keep the struct's size and layout
134 * same, no matter whether IFPOLL_ENABLE is defined or not.
135 * See ifnet.if_npoll and ifnet.if_npoll_unused for example.
139 * Network serialize/deserialize types
141 enum ifnet_serialize {
142 IFNET_SERIALIZE_ALL /* all serializers */
145 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
148 * Structure defining a network interface.
150 * (Would like to call this struct ``if'', but C isn't PL/1.)
154 * NB: For DragonFlyBSD, it is assumed that each NIC driver's softc starts
155 * with one of these structures, typically held within an arpcom structure.
157 * struct <foo>_softc {
159 * struct ifnet ac_if;
165 * The assumption is used in a number of places, including many
166 * files in sys/net, device drivers, and sys/dev/mii.c:miibus_attach().
168 * Unfortunately devices' softc are opaque, so we depend on this layout
169 * to locate the struct ifnet from the softc in the generic code.
175 * ifnet is protected by calling if_serialize, if_tryserialize and
176 * if_deserialize serialize functions with the ifnet_serialize parameter.
177 * Callers of if_ioctl, if_watchdog, if_init, if_resolvemulti, and if_npoll
178 * should call the ifnet serialize functions with IFNET_SERIALIZE_ALL.
180 * if_snd subqueues are protected by its own serializers. Callers of
181 * if_start should call ifsq_serialiize_hw(), ifsq_deserialize_hw() and
182 * ifsq_tryserialize_hw() to properly serialize hardware for transmission.
184 * Caller of if_output MUST NOT serialize ifnet or if_snd by calling
185 * the related serialize functions.
187 * For better transmission performance, driver should setup if_snd subqueue
188 * owner's cpuid properly using ifsq_set_cpuid() (or ifq_set_cpuid(), if not
189 * multiple transmit queue capable). Normally, the if_snd subqueue owner's
190 * cpu is the one that processing the transmission interruption. And in
191 * driver, direct call of if_start should be avoided, use ifsq_devstart() or
192 * ifsq_devstart_sched() instead (or if_devstart()/if_devstart_sched(), if
193 * not multiple transmit queue capable).
199 * if_data is no longer used to hold per interface statistics, so DO NOT use
200 * the old style ifp->if_ipackets++ to update statistics; instead IFNET_STAT_
201 * macros should be used.
205 * SINGLE SERIALIZER MODE:
207 * In this mode, driver MUST NOT setup if_serialize, if_deserialize,
208 * if_tryserialize or if_serialize_assert. Driver could supply its own
209 * serializer to be used (through the type specific attach function, e.g.
210 * ether_ifattach()) or it could depend on the default serializer. In this
211 * mode if_serializer will be setup properly.
213 * If a device driver installs the same serializer for its interrupt
214 * as for ifnet, then the driver only really needs to worry about further
215 * serialization in timeout based entry points and device_method_t entry
216 * points. All other entry points will already be serialized.
220 * MULTI SERIALIZERS MODE:
222 * In this mode, driver MUST setup if_serialize, if_deserialize,
223 * if_tryserialize and if_serialize_assert. Driver MUST NOT supply its own
224 * serializer to be used. In this mode, if_serializer will be left as NULL.
225 * And driver MUST setup if_snd subqueues' hardware serailizer properly by
226 * calling ifsq_set_hw_serialize().
230 * MULTIPLE TRANSMIT QUEUES:
232 * This should be implemented in "MULTI SERIALIZERS MODE". Legacy if_watchdog
233 * method SHOULD NOT be used.
237 * Before the type specific attach, e.g. ether_ifattach(), driver should
238 * setup the transmit queue count and cpuid to subqueue mapping method
239 * properly (assume QCOUNT is power of 2):
241 * ifq_set_subq_cnt(&ifp->if_snd, QCOUNT);
242 * ifp->if_mapsubq = ifq_mapsubq_modulo;
243 * ifq_set_subq_divisor(&ifp->if_snd, QCOUNT);
245 * After the type specific attach, driver should setup the subqueues owner
246 * cpu, serializer and watchdog properly:
248 * for (i = 0; i < QCOUNT, ++i) {
249 * struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
251 * ifsq_set_cpuid(ifsq, Q_CPUID);
252 * ifsq_set_hw_serialize(ifsq, Q_SLIZE);
253 * ifsq_watchdog_init(Q_WDOG, ifsq, Q_WDOG_FUNC);
256 * Q_CPUID, the cpu which handles the hardware transmit queue interrupt
257 * Q_SLIZE, the serializer protects the hardware transmit queue
258 * Q_WDOG, per hardware transmit queue watchdog handler, struct ifsubq_watchdog
259 * Q_WDOG_FUNC, watchdog function, probably should reset hardware
263 * Make sure per hardware transmit queue watchdog is stopped and oactive is
266 * for (i = 0; i < QCOUNT, ++i) {
267 * ifsq_clr_oactive(ifsq);
268 * ifsq_watchdog_stop(Q_WDOG);
273 * Make sure per hardware transmit queue watchdog is started and oactive is
276 * for (i = 0; i < QCOUNT, ++i) {
277 * ifsq_clr_oactive(ifsq);
278 * ifsq_watchdog_start(Q_WDOG);
283 * if_start takes subqueue as parameter, so instead of using ifq_ functions
284 * ifsq_ functions should be used. If device could not be programmed to
285 * transmit when no media link is not up, MAKE SURE to purge the subqueue:
287 * if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
294 * if (NO_FREE_DESC) {
295 * ifsq_set_oactive(ifsq);
298 * m = ifsq_dequeue(ifsq);
301 * Q_WDOG.wd_timer = WDOG_TIMEOUT;
304 * 5) Transmission done, e.g. transmit queue interrupt processing
306 * Same as if_start, ifsq_ functions should be used:
308 * DRIVER_COLLECT_DESC();
310 * ifsq_clr_oactive(ifsq);
311 * if (NO_PENDING_DESC)
312 * Q_WDOG.wd_timer = 0;
313 * if (!ifsq_is_empty(ifsq))
314 * ifsq_devstart(ifsq);
317 void *if_softc; /* pointer to driver state */
318 void *if_l2com; /* pointer to protocol bits */
319 TAILQ_ENTRY(ifnet) if_link; /* all struct ifnets are chained */
320 char if_xname[IFNAMSIZ]; /* external name (name + unit);
321 * can be renamed (SIOCSIFNAME) */
322 const char *if_dname; /* driver name */
323 int if_dunit; /* unit or IF_DUNIT_NONE */
324 void *if_vlantrunks; /* vlan trunks */
325 struct ifaddrhead *if_addrheads; /* per-cpu per-if addresses */
326 int if_pcount; /* number of promiscuous listeners */
327 void *if_carp; /* carp interfaces */
328 struct bpf_if *if_bpf; /* packet filter structure */
329 u_short if_index; /* numeric abbreviation for this if */
330 short if_timer; /* time 'til if_watchdog called */
331 int if_flags; /* up/down, broadcast, etc. */
332 int if_capabilities; /* interface capabilities */
333 int if_capenable; /* enabled features */
334 void *if_linkmib; /* link-type-specific MIB data */
335 size_t if_linkmiblen; /* length of above data */
336 struct if_data if_data; /* NOTE: stats are in if_data_pcpu */
337 struct ifmultihead if_multiaddrs; /* multicast addresses configured */
338 int if_amcount; /* number of all-multicast requests */
339 TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if;
340 * protected by 'ifgroup_lock' */
345 int (*if_output) /* output routine (enqueue) */
346 (struct ifnet *, struct mbuf *, struct sockaddr *,
348 void (*if_input) /* input routine from hardware driver */
349 (struct ifnet *, struct mbuf *,
350 const struct pktinfo *pi, int cpuid);
351 void (*if_start) /* initiate output routine */
352 (struct ifnet *, struct ifaltq_subque *);
353 int (*if_ioctl) /* ioctl routine */
354 (struct ifnet *, u_long, caddr_t, struct ucred *);
355 void (*if_watchdog) /* timer routine */
357 void (*if_init) /* init routine */
359 int (*if_resolvemulti) /* validate/resolve multicast */
360 (struct ifnet *, struct sockaddr **, struct sockaddr *);
362 int (*if_mapsubq) /* cpuid to if_snd subqueue map */
363 (struct ifaltq *, int);
367 * ifnet serialize functions
370 (struct ifnet *, enum ifnet_serialize);
371 void (*if_deserialize)
372 (struct ifnet *, enum ifnet_serialize);
373 int (*if_tryserialize)
374 (struct ifnet *, enum ifnet_serialize);
376 void (*if_serialize_assert)
377 (struct ifnet *, enum ifnet_serialize, boolean_t);
380 void (*if_serialize_unused)(void);
384 void (*if_npoll) /* polling config */
385 (struct ifnet *, struct ifpoll_info *);
388 void (*if_npoll_unused)(void);
390 int if_tsolen; /* max TSO length */
391 struct ifaltq if_snd; /* output subqueues */
392 const uint8_t *if_broadcastaddr;
393 void *if_bridge; /* bridge glue */
394 void *if_lagg; /* lagg glue */
395 void *if_afdata[AF_MAX];
396 struct ifaddr *if_lladdr;
398 /* serializer, in single serializer mode */
399 struct lwkt_serialize *if_serializer;
401 * default serializer, in single serializer mode,
402 * if driver does not supply one
404 struct lwkt_serialize if_default_serializer;
407 struct ifdata_pcpu *if_data_pcpu; /* per-cpu stats */
408 void *if_pf_kif; /* pf interface */
411 * Mbuf clusters/jclusters limits should be increased
412 * by if_nmbclusters/if_nmbjclusters. Mainly for mbuf
413 * clusters/jclusters that could sit on the device
414 * queues, e.g. reception queues, for quite some time.
419 typedef void if_init_f_t (void *);
421 #define if_mtu if_data.ifi_mtu
422 #define if_type if_data.ifi_type
423 #define if_physical if_data.ifi_physical
424 #define if_addrlen if_data.ifi_addrlen
425 #define if_hdrlen if_data.ifi_hdrlen
426 #define if_metric if_data.ifi_metric
427 #define if_link_state if_data.ifi_link_state
428 #define if_baudrate if_data.ifi_baudrate
429 #define if_hwassist if_data.ifi_hwassist
430 #define if_ipackets if_data.ifi_ipackets
431 #define if_ierrors if_data.ifi_ierrors
432 #define if_opackets if_data.ifi_opackets
433 #define if_oerrors if_data.ifi_oerrors
434 #define if_collisions if_data.ifi_collisions
435 #define if_ibytes if_data.ifi_ibytes
436 #define if_obytes if_data.ifi_obytes
437 #define if_imcasts if_data.ifi_imcasts
438 #define if_omcasts if_data.ifi_omcasts
439 #define if_iqdrops if_data.ifi_iqdrops
440 #define if_noproto if_data.ifi_noproto
441 #define if_oqdrops if_data.ifi_oqdrops
442 #define if_lastchange if_data.ifi_lastchange
443 #define if_recvquota if_data.ifi_recvquota
444 #define if_xmitquota if_data.ifi_xmitquota
445 #define if_rawoutput(if, m, sa) if_output(if, m, sa, NULL)
447 /* for compatibility with other BSDs */
448 #define if_list if_link
451 * Per-cpu interface statistics
454 u_long ifd_ipackets; /* packets received on interface */
455 u_long ifd_ierrors; /* input errors on interface */
456 u_long ifd_opackets; /* packets sent on interface */
457 u_long ifd_oerrors; /* output errors on interface */
458 u_long ifd_collisions; /* collisions on csma interfaces */
459 u_long ifd_ibytes; /* total number of octets received */
460 u_long ifd_obytes; /* total number of octets sent */
461 u_long ifd_imcasts; /* packets received via multicast */
462 u_long ifd_omcasts; /* packets sent via multicast */
463 u_long ifd_iqdrops; /* dropped on input, this interface */
464 u_long ifd_noproto; /* destined for unsupported protocol */
465 u_long ifd_oqdrops; /* dropped on output, this interface */
468 #endif /* _KERNEL || _KERNEL_STRUCTURES */
471 * ifqueue operation macros
473 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
474 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
475 #define IF_QLEN(ifq) ((ifq)->ifq_len)
476 #define IF_QEMPTY(ifq) (IF_QLEN(ifq) == 0)
478 #define IF_ENQUEUE(ifq, m) do { \
479 (m)->m_nextpkt = NULL; \
480 if ((ifq)->ifq_tail == NULL) \
481 (ifq)->ifq_head = m; \
483 (ifq)->ifq_tail->m_nextpkt = m; \
484 (ifq)->ifq_tail = m; \
488 #define IF_PREPEND(ifq, m) do { \
489 (m)->m_nextpkt = (ifq)->ifq_head; \
490 if ((ifq)->ifq_tail == NULL) \
491 (ifq)->ifq_tail = (m); \
492 (ifq)->ifq_head = (m); \
496 #define IF_DEQUEUE(ifq, m) do { \
497 (m) = (ifq)->ifq_head; \
499 if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL) \
500 (ifq)->ifq_tail = NULL; \
501 (m)->m_nextpkt = NULL; \
506 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
508 #define IF_DRAIN(ifq) do { \
511 IF_DEQUEUE(ifq, m); \
521 #define ASSERT_IFNET_SERIALIZED_ALL(ifp) \
522 (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, TRUE)
523 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp) \
524 (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, FALSE)
526 #define ASSERT_IFNET_SERIALIZED_ALL(ifp) ((void)0)
527 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp) ((void)0)
531 ifnet_serialize_all(struct ifnet *_ifp)
533 _ifp->if_serialize(_ifp, IFNET_SERIALIZE_ALL);
537 ifnet_deserialize_all(struct ifnet *_ifp)
539 _ifp->if_deserialize(_ifp, IFNET_SERIALIZE_ALL);
543 ifnet_tryserialize_all(struct ifnet *_ifp)
545 return _ifp->if_tryserialize(_ifp, IFNET_SERIALIZE_ALL);
549 * 72 was chosen below because it is the size of a TCP/IP
550 * header (40) + the minimum mss (32).
553 #define IF_MAXMTU 65535
559 struct in_ifaddr_container {
560 struct in_ifaddr *ia;
561 LIST_ENTRY(in_ifaddr_container) ia_hash;
562 /* entry in bucket of inet addresses */
563 TAILQ_ENTRY(in_ifaddr_container) ia_link;
564 /* list of internet addresses */
565 struct ifaddr_container *ia_ifac; /* parent ifaddr_container */
569 * Per-cpu ifaddr container:
570 * - per-cpu ifaddr reference count
571 * - linkage to per-cpu addresses lists
572 * - per-cpu ifaddr statistics
574 struct ifaddr_container {
575 #define IFA_CONTAINER_MAGIC 0x19810219
576 #define IFA_CONTAINER_DEAD 0xc0dedead
577 uint32_t ifa_magic; /* IFA_CONTAINER_MAGIC */
579 TAILQ_ENTRY(ifaddr_container) ifa_link; /* queue macro glue */
580 u_int ifa_refcnt; /* references to this structure */
581 uint16_t ifa_listmask; /* IFA_LIST_ */
582 uint16_t ifa_prflags; /* protocol specific flags */
584 u_long ifa_ipackets; /* packets received on addr */
585 u_long ifa_ibytes; /* bytes received on addr */
586 u_long ifa_opackets; /* packets sent on addr */
587 u_long ifa_obytes; /* bytes sent on addr */
590 * Protocol specific states
593 struct in_ifaddr_container u_in_ifac;
597 #define IFA_LIST_IFADDRHEAD 0x01 /* on ifnet.if_addrheads[cpuid] */
598 #define IFA_LIST_IN_IFADDRHEAD 0x02 /* on in_ifaddrheads[cpuid] */
599 #define IFA_LIST_IN_IFADDRHASH 0x04 /* on in_ifaddrhashtbls[cpuid] */
601 #define IFA_PRF_FLAG0 0x01
602 #define IFA_PRF_FLAG1 0x02
603 #define IFA_PRF_FLAG2 0x04
604 #define IFA_PRF_FLAG3 0x08
607 * The ifaddr structure contains information about one address
608 * of an interface. They are maintained by the different address families,
609 * are allocated and attached when an address is set, and are linked
610 * together so all addresses for an interface can be located.
613 * Statistics are no longer stored in if_data, instead, they are stored
614 * in the per-cpu ifaddr_container. So don't use the old style
615 * ifa->if_ipackets++ to update statistics, use IFA_STAT_ macros.
618 struct sockaddr *ifa_addr; /* address of interface */
619 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
620 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
621 struct sockaddr *ifa_netmask; /* used to determine subnet */
622 struct if_data if_data; /* not all members are meaningful */
623 struct ifnet *ifa_ifp; /* back-pointer to interface */
625 struct ifaddr_container *ifa_containers; /* per-cpu data */
626 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
627 (int, struct rtentry *);
628 u_short ifa_flags; /* mostly rt_flags for cloning */
629 int ifa_ncnt; /* # of valid ifaddr_container */
630 int ifa_metric; /* cost of going out this interface */
632 struct rtentry *ifa_rt; /* XXXX for ROUTETOIF ????? */
634 int (*ifa_claim_addr) /* check if an addr goes to this if */
635 (struct ifaddr *, struct sockaddr *);
638 #define IFA_ROUTE RTF_UP /* route installed */
640 /* for compatibility with other BSDs */
641 #define ifa_list ifa_link
644 * Multicast address structure. This is analogous to the ifaddr
645 * structure except that it keeps track of multicast addresses.
646 * Also, the reference count here is a count of requests for this
647 * address, not a count of pointers to this structure.
650 TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
651 struct sockaddr *ifma_addr; /* address this membership is for */
652 struct sockaddr *ifma_lladdr; /* link-layer translation, if any */
653 struct ifnet *ifma_ifp; /* back-pointer to interface */
654 u_int ifma_refcount; /* reference count */
655 void *ifma_protospec; /* protocol-specific state, if any */
660 struct ifaddr_marker {
662 struct ifaddr_container ifac;
663 struct sockaddr addr;
664 struct sockaddr netmask;
665 struct sockaddr dstaddr;
669 * ifaddr statistics update macro
671 #define IFA_STAT_INC(ifa, name, v) \
673 (ifa)->ifa_containers[mycpuid].ifa_##name += (v); \
677 * Interface (ifnet) statistics update macros
679 #define IFNET_STAT_INC(ifp, name, v) \
681 (ifp)->if_data_pcpu[mycpuid].ifd_##name += (v); \
684 #define IFNET_STAT_SET(ifp, name, v) \
687 (ifp)->if_data_pcpu[0].ifd_##name = (v); \
688 for (_cpu = 1; _cpu < ncpus; ++_cpu) \
689 (ifp)->if_data_pcpu[_cpu].ifd_##name = 0; \
692 #define IFNET_STAT_GET(ifp, name, v) \
695 (v) = (ifp)->if_data_pcpu[0].ifd_##name; \
696 for (_cpu = 1; _cpu < ncpus; ++_cpu) \
697 (v) += (ifp)->if_data_pcpu[_cpu].ifd_##name; \
707 /* interface address change event */
708 typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *,
709 enum ifaddr_event, struct ifaddr *);
710 EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
711 /* interface link layer address change event */
712 typedef void (*iflladdr_event_handler_t)(void *, struct ifnet *);
713 EVENTHANDLER_DECLARE(iflladdr_event, iflladdr_event_handler_t);
714 /* new interface attach event */
715 typedef void (*ifnet_attach_event_handler_t)(void *, struct ifnet *);
716 EVENTHANDLER_DECLARE(ifnet_attach_event, ifnet_attach_event_handler_t);
717 /* interface detach event */
718 typedef void (*ifnet_detach_event_handler_t)(void *, struct ifnet *);
719 EVENTHANDLER_DECLARE(ifnet_detach_event, ifnet_detach_event_handler_t);
720 /* Interface link state change event */
721 typedef void (*ifnet_link_event_handler_t)(void *, struct ifnet *, int);
722 EVENTHANDLER_DECLARE(ifnet_link_event, ifnet_link_event_handler_t);
723 /* Interface up/down event */
724 #define IFNET_EVENT_UP 0
725 #define IFNET_EVENT_DOWN 1
726 typedef void (*ifnet_event_fn)(void *, struct ifnet *ifp, int event);
727 EVENTHANDLER_DECLARE(ifnet_event, ifnet_event_fn);
729 /* Array of all ifnets in the system */
731 int ifnet_count; /* # of elem. in ifnet_arr */
732 int ifnet_pad; /* explicit */
733 struct ifnet *ifnet_arr[];
740 char ifg_group[IFNAMSIZ];
743 int ifg_carp_demoted;
744 TAILQ_HEAD(, ifg_member) ifg_members;
745 TAILQ_ENTRY(ifg_group) ifg_next;
749 TAILQ_ENTRY(ifg_member) ifgm_next;
750 struct ifnet *ifgm_ifp;
754 struct ifg_group *ifgl_group;
755 TAILQ_ENTRY(ifg_list) ifgl_next;
758 /* group attach event */
759 typedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
760 EVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
761 /* group detach event */
762 typedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
763 EVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
764 /* group change event */
765 typedef void (*group_change_event_handler_t)(void *, const char *);
766 EVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
770 #define ASSERT_IFAC_VALID(ifac) do { \
771 KKASSERT((ifac)->ifa_magic == IFA_CONTAINER_MAGIC); \
772 KKASSERT((ifac)->ifa_refcnt > 0); \
775 #define ASSERT_IFAC_VALID(ifac) ((void)0)
779 _IFAREF(struct ifaddr *_ifa, int _cpu_id)
781 struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
784 ASSERT_IFAC_VALID(_ifac);
790 IFAREF(struct ifaddr *_ifa)
792 _IFAREF(_ifa, mycpuid);
795 #include <sys/malloc.h>
796 #include <sys/serialize2.h>
798 MALLOC_DECLARE(M_IFADDR);
799 MALLOC_DECLARE(M_IFMADDR);
800 MALLOC_DECLARE(M_IFNET);
802 void ifac_free(struct ifaddr_container *, int);
805 _IFAFREE(struct ifaddr *_ifa, int _cpu_id)
807 struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
810 ASSERT_IFAC_VALID(_ifac);
811 if (--_ifac->ifa_refcnt == 0)
812 ifac_free(_ifac, _cpu_id);
817 IFAFREE(struct ifaddr *_ifa)
819 _IFAFREE(_ifa, mycpuid);
823 ifnet_serialize_array_enter(lwkt_serialize_t *_arr, int _arrcnt,
824 enum ifnet_serialize _slz)
826 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
827 lwkt_serialize_array_enter(_arr, _arrcnt, 0);
831 ifnet_serialize_array_exit(lwkt_serialize_t *_arr, int _arrcnt,
832 enum ifnet_serialize _slz)
834 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
835 lwkt_serialize_array_exit(_arr, _arrcnt, 0);
839 ifnet_serialize_array_try(lwkt_serialize_t *_arr, int _arrcnt,
840 enum ifnet_serialize _slz)
842 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
843 return lwkt_serialize_array_try(_arr, _arrcnt, 0);
849 ifnet_serialize_array_assert(lwkt_serialize_t *_arr, int _arrcnt,
850 enum ifnet_serialize _slz, boolean_t _serialized)
854 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
856 for (_i = 0; _i < _arrcnt; ++_i)
857 ASSERT_SERIALIZED(_arr[_i]);
859 for (_i = 0; _i < _arrcnt; ++_i)
860 ASSERT_NOT_SERIALIZED(_arr[_i]);
864 #endif /* INVARIANTS */
866 #define REINPUT_KEEPRCVIF 0x0001 /* ether_reinput_oncpu() */
867 #define REINPUT_RUNBPF 0x0002 /* ether_reinput_oncpu() */
870 * MPSAFE NOTE for ifnet queue (ifnet), ifnet array, ifunit() and
873 * - ifnet queue must only be accessed by non-netisr threads and
874 * ifnet lock must be held (by ifnet_lock()).
875 * - If accessing ifnet queue is needed in netisrs, ifnet array
876 * (obtained through ifnet_array_get()) must be used instead.
877 * There is no need to (must not, actually) hold ifnet lock for
878 * ifnet array accessing.
879 * - ifindex2ifnet could be accessed by both non-netisr threads and
880 * netisrs. Accessing ifindex2ifnet in non-netisr threads must be
881 * protected by ifnet lock (by ifnet_lock()). Accessing
882 * ifindex2ifnet in netisrs is lockless MPSAFE and ifnet lock must
883 * not be held. However, ifindex2ifnet should be saved in a stack
884 * variable to get a consistent view of ifindex2ifnet, if
885 * ifindex2ifnet is accessed multiple times from a function in
887 * - ifunit() must only be called in non-netisr threads and ifnet
888 * lock must be held before calling this function and for the
889 * accessing of the ifp returned by this function.
890 * - If ifunit() is needed in netisr, ifunit_netisr() must be used
891 * instead. There is no need to (must not, actually) hold ifnet
892 * lock for ifunit_netisr() and the returned ifp.
894 extern struct ifnethead ifnet;
895 #define ifnetlist ifnet /* easily distinguished ifnet alias */
897 extern struct ifnet **ifindex2ifnet;
900 struct ifnet *ifunit(const char *);
901 struct ifnet *ifunit_netisr(const char *);
902 const struct ifnet_array *ifnet_array_get(void);
903 int ifnet_array_isempty(void);
905 extern int ifqmaxlen;
906 extern struct ifnet *loif; /* first loopback interface */
907 extern struct ifgrouphead ifg_head;
912 void ether_ifattach(struct ifnet *, const uint8_t *,
913 struct lwkt_serialize *);
914 void ether_ifattach_bpf(struct ifnet *, const uint8_t *, u_int, u_int,
915 struct lwkt_serialize *);
916 void ether_ifdetach(struct ifnet *);
917 void ether_demux(struct mbuf *);
918 void ether_demux_oncpu(struct ifnet *, struct mbuf *);
919 void ether_reinput_oncpu(struct ifnet *, struct mbuf *, int);
920 void ether_input(struct ifnet *, struct mbuf *,
921 const struct pktinfo *, int);
922 int ether_output_frame(struct ifnet *, struct mbuf *);
923 int ether_ioctl(struct ifnet *, u_long, caddr_t);
924 u_char *kether_aton(const char *, u_char *);
925 char *kether_ntoa(const u_char *, char *);
926 struct ifnet *ether_bridge_interface(struct ifnet *ifp);
927 uint32_t ether_crc32_le(const uint8_t *, size_t);
928 uint32_t ether_crc32_be(const uint8_t *, size_t);
930 int if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
931 int if_addmulti_serialized(struct ifnet *, struct sockaddr *,
932 struct ifmultiaddr **);
933 int if_allmulti(struct ifnet *, int);
934 void if_attach(struct ifnet *, struct lwkt_serialize *);
935 int if_delmulti(struct ifnet *, struct sockaddr *);
936 void if_delallmulti_serialized(struct ifnet *ifp);
937 void if_purgeaddrs_nolink(struct ifnet *);
938 void if_detach(struct ifnet *);
939 void if_down(struct ifnet *);
940 void if_link_state_change(struct ifnet *);
941 void if_initname(struct ifnet *, const char *, int);
942 int if_getanyethermac(uint16_t *, int);
943 int if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
944 struct ifnet *if_alloc(uint8_t);
945 void if_free(struct ifnet *);
946 int if_setlladdr(struct ifnet *, const u_char *, int);
947 struct ifnet *if_bylla(const void *, unsigned char);
948 void if_up(struct ifnet *);
949 /*void ifinit(void);*/ /* declared in systm.h for main() */
950 int ifioctl(struct socket *, u_long, caddr_t, struct ucred *);
951 int ifpromisc(struct ifnet *, int);
953 int ifgroup_lockmgr(u_int flags);
954 int if_addgroup(struct ifnet *, const char *);
955 int if_delgroup(struct ifnet *, const char *);
957 struct ifaddr *ifa_ifwithaddr(struct sockaddr *);
958 struct ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
959 struct ifaddr *ifa_ifwithnet(struct sockaddr *);
960 struct ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
961 struct ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
963 typedef void *if_com_alloc_t(u_char type, struct ifnet *ifp);
964 typedef void if_com_free_t(void *com, u_char type);
965 void if_register_com_alloc(u_char, if_com_alloc_t *a, if_com_free_t *);
966 void if_deregister_com_alloc(u_char);
968 void *ifa_create(int);
969 void ifa_destroy(struct ifaddr *);
970 void ifa_iflink(struct ifaddr *, struct ifnet *, int);
971 void ifa_ifunlink(struct ifaddr *, struct ifnet *);
972 void ifa_marker_init(struct ifaddr_marker *, struct ifnet *);
974 struct ifmultiaddr *ifmaof_ifpforaddr(struct sockaddr *, struct ifnet *);
975 int if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
976 void if_devstart(struct ifnet *ifp); /* COMPAT */
977 void if_devstart_sched(struct ifnet *ifp); /* COMPAT */
979 void ifnet_lock(void);
980 void ifnet_unlock(void);
982 #define IF_LLSOCKADDR(ifp) \
983 ((struct sockaddr_dl *)(ifp)->if_lladdr->ifa_addr)
984 #define IF_LLADDR(ifp) LLADDR(IF_LLSOCKADDR(ifp))
987 int ifpoll_register(struct ifnet *);
988 int ifpoll_deregister(struct ifnet *);
989 #endif /* IFPOLL_ENABLE */
993 #endif /* !_NET_IF_VAR_H_ */