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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * From: @(#)if.h 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_var.h,v 1.18.2.16 2003/04/15 18:11:19 fjoe Exp $
37 #ifndef _NET_IF_VAR_H_
38 #define _NET_IF_VAR_H_
40 #ifndef _SYS_SERIALIZE_H_
41 #include <sys/serialize.h>
47 #include <sys/mutex.h>
51 * Structures defining a network interface, providing a packet
52 * transport mechanism (ala level 0 of the PUP protocols).
54 * Each interface accepts output datagrams of a specified maximum
55 * length, and provides higher level routines with input datagrams
56 * received from its medium.
58 * Output occurs when the routine if_output is called, with four parameters:
60 * ifp->if_output(ifp, m, dst, rt)
62 * Here m is the mbuf chain to be sent and dst is the destination address.
63 * The output routine encapsulates the supplied datagram if necessary,
64 * and then transmits it on its medium.
66 * On input, each interface unwraps the data received by it, and either
67 * places it on the input queue of a internetwork datagram routine
68 * and posts the associated software interrupt, or passes the datagram to
69 * the routine if_input. It is called with four parameters:
71 * ifp->if_input(ifp, m, pi, cpuid)
73 * Here m is the mbuf chain to be received. The input routine removes the
74 * protocol dependent header if necessary. A driver may also call using
75 * custom struct pktinfo reference pi and a cpuid to take advantage of
76 * hardware supplied information. Otherwise, the defaults for pi and cpuid
79 * ifp->if_input(ifp, m, NULL, -1);
81 * Routines exist for locating interfaces by their addresses
82 * or for locating a interface on a certain network, as well as more general
83 * routing and gateway routines maintaining information used to locate
84 * interfaces. These routines live in the files if.c and route.c
88 * Forward structure declarations for function prototypes [sic].
97 struct lwkt_serialize;
98 struct ifaddr_container;
107 #include <sys/queue.h> /* get TAILQ macros */
109 #include <net/altq/if_altq.h>
112 #include <sys/eventhandler.h>
113 #include <sys/mbuf.h>
114 #include <sys/systm.h> /* XXX */
115 #include <sys/thread2.h>
118 #define IF_DUNIT_NONE -1
120 TAILQ_HEAD(ifnethead, ifnet); /* we use TAILQs so that the order of */
121 TAILQ_HEAD(ifaddrhead, ifaddr_container); /* instantiation is preserved in the list */
122 TAILQ_HEAD(ifprefixhead, ifprefix);
123 TAILQ_HEAD(ifmultihead, ifmultiaddr);
126 * Structure defining a mbuf queue.
129 struct mbuf *ifq_head;
130 struct mbuf *ifq_tail;
137 * Note of IFPOLL_ENABLE
138 * 1) Any file(*.c) that depends on IFPOLL_ENABLE supports in this
139 * file should include opt_ifpoll.h at its beginning.
140 * 2) When struct changes, which are conditioned by IFPOLL_ENABLE,
141 * are to be introduced, please keep the struct's size and layout
142 * same, no matter whether IFPOLL_ENABLE is defined or not.
143 * See ifnet.if_npoll and ifnet.if_npoll_unused for example.
147 * Network serialize/deserialize types
149 enum ifnet_serialize {
150 IFNET_SERIALIZE_ALL /* all serializers */
153 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
156 * Structure defining a network interface.
158 * (Would like to call this struct ``if'', but C isn't PL/1.)
162 * NB: For DragonFlyBSD, it is assumed that each NIC driver's softc starts
163 * with one of these structures, typically held within an arpcom structure.
165 * struct <foo>_softc {
167 * struct ifnet ac_if;
173 * The assumption is used in a number of places, including many
174 * files in sys/net, device drivers, and sys/dev/mii.c:miibus_attach().
176 * Unfortunately devices' softc are opaque, so we depend on this layout
177 * to locate the struct ifnet from the softc in the generic code.
183 * ifnet is protected by calling if_serialize, if_tryserialize and
184 * if_deserialize serialize functions with the ifnet_serialize parameter.
185 * Callers of if_ioctl, if_watchdog, if_init, if_resolvemulti, and if_npoll
186 * should call the ifnet serialize functions with IFNET_SERIALIZE_ALL.
188 * if_snd subqueues are protected by its own serializers. Callers of
189 * if_start should call ifsq_serialiize_hw(), ifsq_deserialize_hw() and
190 * ifsq_tryserialize_hw() to properly serialize hardware for transmission.
192 * Caller of if_output MUST NOT serialize ifnet or if_snd by calling
193 * the related serialize functions.
195 * For better tranmission performance, driver should setup if_snd subqueue
196 * owner cpuid properly using ifsq_set_cpuid() (or ifq_set_cpuid(), if not
197 * multiple transmit queue capable). Normally, the if_snd subqueue owner
198 * cpu is the one that processing the transmission interrupt. And in driver,
199 * direct call of if_start should be avoided, use ifsq_devstart() or
200 * ifsq_devstart_sched() instead (or if_devstart()/if_devstart_sched(), if
201 * not multiple transmit queue capable).
207 * if_data is no longer used to hold per interface statistics, so DO NOT use
208 * the old style ifp->if_ipackets++ to update statistics; instead IFNET_STAT_
209 * macros should be used.
213 * SINGLE SERIALIZER MODE:
215 * In this mode, driver MUST NOT setup if_serialize, if_deserialize,
216 * if_tryserialize or if_serialize_assert. Driver could supply its own
217 * serializer to be used (through the type specific attach function, e.g.
218 * ether_ifattach()) or it could depend on the default serializer. In this
219 * mode if_serializer will be setup properly.
221 * If a device driver installs the same serializer for its interrupt
222 * as for ifnet, then the driver only really needs to worry about further
223 * serialization in timeout based entry points and device_method_t entry
224 * points. All other entry points will already be serialized.
228 * MULTI SERIALIZERS MODE:
230 * In this mode, driver MUST setup if_serialize, if_deserialize,
231 * if_tryserialize and if_serialize_assert. Driver MUST NOT supply its own
232 * serializer to be used. In this mode, if_serializer will be left as NULL.
233 * And driver MUST setup if_snd subqueues' hardware serailizer properly by
234 * calling ifsq_set_hw_serialize().
238 * MULTIPLE TRANSMIT QUEUES:
240 * This should be implemented in "MULTI SERIALIZERS MODE". Legacy if_watchdog
241 * method SHOULD NOT be used.
245 * Before the type specific attach, e.g. ether_ifattach(), driver should
246 * setup the transmit queue count and cpuid to subqueue mapping method
247 * properly (assume QCOUNT is power of 2):
249 * ifq_set_subq_cnt(&ifp->if_snd, QCOUNT);
250 * ifp->if_mapsubq = ifq_mapsubq_mask;
251 * ifq_set_subq_mask(&ifp->if_snd, QCOUNT - 1);
253 * After the type specific attach, driver should setup the subqueues owner
254 * cpu, serializer and watchdog properly:
256 * for (i = 0; i < QCOUNT, ++i) {
257 * struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
259 * ifsq_set_cpuid(ifsq, Q_CPUID);
260 * ifsq_set_hw_serialize(ifsq, Q_SLIZE);
261 * ifsq_watchdog_init(Q_WDOG, ifsq, Q_WDOG_FUNC);
264 * Q_CPUID, the cpu which handles the hardware transmit queue interrupt
265 * Q_SLIZE, the serializer protects the hardware transmit queue
266 * Q_WDOG, per hardware transmit queue watchdog handler, struct ifsubq_watchdog
267 * Q_WDOG_FUNC, watchdog function, probably should reset hardware
271 * Make sure per hardware transmit queue watchdog is stopped and oactive is
274 * for (i = 0; i < QCOUNT, ++i) {
275 * ifsq_clr_oactive(ifsq);
276 * ifsq_watchdog_stop(Q_WDOG);
281 * Make sure per hardware transmit queue watchdog is started and oactive is
284 * for (i = 0; i < QCOUNT, ++i) {
285 * ifsq_clr_oactive(ifsq);
286 * ifsq_watchdog_start(Q_WDOG);
291 * if_start takes subqueue as parameter, so instead of using ifq_ functions
292 * ifsq_ functions should be used. If device could not be programmed to
293 * transmit when no media link is not up, MAKE SURE to purge the subqueue:
295 * if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
302 * if (NO_FREE_DESC) {
303 * ifsq_set_oactive(ifsq);
306 * m = ifsq_dequeue(ifsq);
309 * Q_WDOG.wd_timer = WDOG_TIMEOUT;
312 * 5) Transmission done, e.g. transmit queue interrupt processing
314 * Same as if_start, ifsq_ functions should be used:
316 * DRIVER_COLLECT_DESC();
318 * ifsq_clr_oactive(ifsq);
319 * if (NO_PENDING_DESC)
320 * Q_WDOG.wd_timer = 0;
321 * if (!ifsq_is_empty(ifsq))
322 * ifsq_devstart(ifsq);
325 void *if_softc; /* pointer to driver state */
326 void *if_l2com; /* pointer to protocol bits */
327 TAILQ_ENTRY(ifnet) if_link; /* all struct ifnets are chained */
328 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
329 const char *if_dname; /* driver name */
330 int if_dunit; /* unit or IF_DUNIT_NONE */
331 void *if_vlantrunks; /* vlan trunks */
332 struct ifaddrhead *if_addrheads; /* per-cpu per-if addresses */
333 int if_pcount; /* number of promiscuous listeners */
334 void *if_carp; /* carp interfaces */
335 struct bpf_if *if_bpf; /* packet filter structure */
336 u_short if_index; /* numeric abbreviation for this if */
337 short if_timer; /* time 'til if_watchdog called */
338 int if_flags; /* up/down, broadcast, etc. */
339 int if_capabilities; /* interface capabilities */
340 int if_capenable; /* enabled features */
341 void *if_linkmib; /* link-type-specific MIB data */
342 size_t if_linkmiblen; /* length of above data */
343 struct if_data if_data; /* NOTE: stats are in if_data_pcpu */
344 struct ifmultihead if_multiaddrs; /* multicast addresses configured */
345 int if_amcount; /* number of all-multicast requests */
346 /* procedure handles */
347 int (*if_output) /* output routine (enqueue) */
348 (struct ifnet *, struct mbuf *, struct sockaddr *,
350 void (*if_input) /* input routine from hardware driver */
351 (struct ifnet *, struct mbuf *,
352 const struct pktinfo *pi, int cpuid);
353 void (*if_start) /* initiate output routine */
354 (struct ifnet *, struct ifaltq_subque *);
355 int (*if_ioctl) /* ioctl routine */
356 (struct ifnet *, u_long, caddr_t, struct ucred *);
357 void (*if_watchdog) /* timer routine */
359 void (*if_init) /* init routine */
361 int (*if_resolvemulti) /* validate/resolve multicast */
362 (struct ifnet *, struct sockaddr **, struct sockaddr *);
364 TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */
365 int (*if_mapsubq) /* cpuid to if_snd subqueue map */
366 (struct ifaltq *, int);
370 * ifnet serialize functions
373 (struct ifnet *, enum ifnet_serialize);
374 void (*if_deserialize)
375 (struct ifnet *, enum ifnet_serialize);
376 int (*if_tryserialize)
377 (struct ifnet *, enum ifnet_serialize);
379 void (*if_serialize_assert)
380 (struct ifnet *, enum ifnet_serialize, boolean_t);
383 void (*if_serialize_unused)(void);
387 void (*if_npoll) /* polling config */
388 (struct ifnet *, struct ifpoll_info *);
391 void (*if_npoll_unused)(void);
393 int if_tsolen; /* max TSO length */
394 struct ifaltq if_snd; /* output subqueues */
395 struct ifprefixhead if_prefixhead; /* list of prefixes per if */
396 const uint8_t *if_broadcastaddr;
397 void *if_bridge; /* bridge glue */
398 void *if_lagg; /* lagg glue */
399 void *if_afdata[AF_MAX];
400 struct ifaddr *if_lladdr;
402 /* serializer, in single serializer mode */
403 struct lwkt_serialize *if_serializer;
405 * default serializer, in single serializer mode,
406 * if driver does not supply one
408 struct lwkt_serialize if_default_serializer;
410 struct mtx if_ioctl_mtx; /* high-level ioctl mutex */
412 struct ifdata_pcpu *if_data_pcpu; /* per-cpu stats */
413 void *if_pf_kif; /* pf interface */
416 typedef void if_init_f_t (void *);
418 #define if_mtu if_data.ifi_mtu
419 #define if_type if_data.ifi_type
420 #define if_physical if_data.ifi_physical
421 #define if_addrlen if_data.ifi_addrlen
422 #define if_hdrlen if_data.ifi_hdrlen
423 #define if_metric if_data.ifi_metric
424 #define if_link_state if_data.ifi_link_state
425 #define if_baudrate if_data.ifi_baudrate
426 #define if_hwassist if_data.ifi_hwassist
427 #define if_ipackets if_data.ifi_ipackets
428 #define if_ierrors if_data.ifi_ierrors
429 #define if_opackets if_data.ifi_opackets
430 #define if_oerrors if_data.ifi_oerrors
431 #define if_collisions if_data.ifi_collisions
432 #define if_ibytes if_data.ifi_ibytes
433 #define if_obytes if_data.ifi_obytes
434 #define if_imcasts if_data.ifi_imcasts
435 #define if_omcasts if_data.ifi_omcasts
436 #define if_iqdrops if_data.ifi_iqdrops
437 #define if_noproto if_data.ifi_noproto
438 #define if_lastchange if_data.ifi_lastchange
439 #define if_recvquota if_data.ifi_recvquota
440 #define if_xmitquota if_data.ifi_xmitquota
441 #define if_rawoutput(if, m, sa) if_output(if, m, sa, NULL)
443 /* for compatibility with other BSDs */
444 #define if_list if_link
447 * Per-cpu interface statistics
450 u_long ifd_ipackets; /* packets received on interface */
451 u_long ifd_ierrors; /* input errors on interface */
452 u_long ifd_opackets; /* packets sent on interface */
453 u_long ifd_oerrors; /* output errors on interface */
454 u_long ifd_collisions; /* collisions on csma interfaces */
455 u_long ifd_ibytes; /* total number of octets received */
456 u_long ifd_obytes; /* total number of octets sent */
457 u_long ifd_imcasts; /* packets received via multicast */
458 u_long ifd_omcasts; /* packets sent via multicast */
459 u_long ifd_iqdrops; /* dropped on input, this interface */
460 u_long ifd_noproto; /* destined for unsupported protocol */
463 #endif /* _KERNEL || _KERNEL_STRUCTURES */
466 * ifqueue operation macros
468 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen)
469 #define IF_DROP(ifq) ((ifq)->ifq_drops++)
470 #define IF_QLEN(ifq) ((ifq)->ifq_len)
471 #define IF_QEMPTY(ifq) (IF_QLEN(ifq) == 0)
473 #define IF_ENQUEUE(ifq, m) do { \
474 (m)->m_nextpkt = NULL; \
475 if ((ifq)->ifq_tail == NULL) \
476 (ifq)->ifq_head = m; \
478 (ifq)->ifq_tail->m_nextpkt = m; \
479 (ifq)->ifq_tail = m; \
483 #define IF_PREPEND(ifq, m) do { \
484 (m)->m_nextpkt = (ifq)->ifq_head; \
485 if ((ifq)->ifq_tail == NULL) \
486 (ifq)->ifq_tail = (m); \
487 (ifq)->ifq_head = (m); \
491 #define IF_DEQUEUE(ifq, m) do { \
492 (m) = (ifq)->ifq_head; \
494 if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL) \
495 (ifq)->ifq_tail = NULL; \
496 (m)->m_nextpkt = NULL; \
501 #define IF_POLL(ifq, m) ((m) = (ifq)->ifq_head)
503 #define IF_DRAIN(ifq) do { \
506 IF_DEQUEUE(ifq, m); \
515 /* interface link layer address change event */
516 typedef void (*iflladdr_event_handler_t)(void *, struct ifnet *);
517 EVENTHANDLER_DECLARE(iflladdr_event, iflladdr_event_handler_t);
520 #define ASSERT_IFNET_SERIALIZED_ALL(ifp) \
521 (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, TRUE)
522 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp) \
523 (ifp)->if_serialize_assert((ifp), IFNET_SERIALIZE_ALL, FALSE)
525 #define ASSERT_IFNET_SERIALIZED_ALL(ifp) ((void)0)
526 #define ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp) ((void)0)
530 ifnet_serialize_all(struct ifnet *_ifp)
532 _ifp->if_serialize(_ifp, IFNET_SERIALIZE_ALL);
536 ifnet_deserialize_all(struct ifnet *_ifp)
538 _ifp->if_deserialize(_ifp, IFNET_SERIALIZE_ALL);
542 ifnet_tryserialize_all(struct ifnet *_ifp)
544 return _ifp->if_tryserialize(_ifp, IFNET_SERIALIZE_ALL);
548 * 72 was chosen below because it is the size of a TCP/IP
549 * header (40) + the minimum mss (32).
552 #define IF_MAXMTU 65535
558 struct in_ifaddr_container {
559 struct in_ifaddr *ia;
560 LIST_ENTRY(in_ifaddr_container) ia_hash;
561 /* entry in bucket of inet addresses */
562 TAILQ_ENTRY(in_ifaddr_container) ia_link;
563 /* list of internet addresses */
564 struct ifaddr_container *ia_ifac; /* parent ifaddr_container */
568 * Per-cpu ifaddr container:
569 * - per-cpu ifaddr reference count
570 * - linkage to per-cpu addresses lists
571 * - per-cpu ifaddr statistics
573 struct ifaddr_container {
574 #define IFA_CONTAINER_MAGIC 0x19810219
575 #define IFA_CONTAINER_DEAD 0xc0dedead
576 uint32_t ifa_magic; /* IFA_CONTAINER_MAGIC */
578 TAILQ_ENTRY(ifaddr_container) ifa_link; /* queue macro glue */
579 u_int ifa_refcnt; /* references to this structure */
580 uint16_t ifa_listmask; /* IFA_LIST_ */
581 uint16_t ifa_prflags; /* protocol specific flags */
583 u_long ifa_ipackets; /* packets received on addr */
584 u_long ifa_ibytes; /* bytes received on addr */
585 u_long ifa_opackets; /* packets sent on addr */
586 u_long ifa_obytes; /* bytes sent on addr */
589 * Protocol specific states
592 struct in_ifaddr_container u_in_ifac;
596 #define IFA_LIST_IFADDRHEAD 0x01 /* on ifnet.if_addrheads[cpuid] */
597 #define IFA_LIST_IN_IFADDRHEAD 0x02 /* on in_ifaddrheads[cpuid] */
598 #define IFA_LIST_IN_IFADDRHASH 0x04 /* on in_ifaddrhashtbls[cpuid] */
600 #define IFA_PRF_FLAG0 0x01
601 #define IFA_PRF_FLAG1 0x02
602 #define IFA_PRF_FLAG2 0x04
603 #define IFA_PRF_FLAG3 0x08
606 * The ifaddr structure contains information about one address
607 * of an interface. They are maintained by the different address families,
608 * are allocated and attached when an address is set, and are linked
609 * together so all addresses for an interface can be located.
612 * Statistics are no longer stored in if_data, instead, they are stored
613 * in the per-cpu ifaddr_container. So don't use the old style
614 * ifa->if_ipackets++ to update statistics, use IFA_STAT_ macros.
617 struct sockaddr *ifa_addr; /* address of interface */
618 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */
619 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */
620 struct sockaddr *ifa_netmask; /* used to determine subnet */
621 struct if_data if_data; /* not all members are meaningful */
622 struct ifnet *ifa_ifp; /* back-pointer to interface */
624 struct ifaddr_container *ifa_containers; /* per-cpu data */
625 void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */
626 (int, struct rtentry *);
627 u_short ifa_flags; /* mostly rt_flags for cloning */
628 int ifa_ncnt; /* # of valid ifaddr_container */
629 int ifa_metric; /* cost of going out this interface */
631 struct rtentry *ifa_rt; /* XXXX for ROUTETOIF ????? */
633 int (*ifa_claim_addr) /* check if an addr goes to this if */
634 (struct ifaddr *, struct sockaddr *);
637 #define IFA_ROUTE RTF_UP /* route installed */
639 /* for compatibility with other BSDs */
640 #define ifa_list ifa_link
643 * The prefix structure contains information about one prefix
644 * of an interface. They are maintained by the different address families,
645 * are allocated and attached when an prefix or an address is set,
646 * and are linked together so all prefixes for an interface can be located.
649 struct sockaddr *ifpr_prefix; /* prefix of interface */
650 struct ifnet *ifpr_ifp; /* back-pointer to interface */
651 TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */
652 u_char ifpr_plen; /* prefix length in bits */
653 u_char ifpr_type; /* protocol dependent prefix type */
657 * Multicast address structure. This is analogous to the ifaddr
658 * structure except that it keeps track of multicast addresses.
659 * Also, the reference count here is a count of requests for this
660 * address, not a count of pointers to this structure.
663 TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */
664 struct sockaddr *ifma_addr; /* address this membership is for */
665 struct sockaddr *ifma_lladdr; /* link-layer translation, if any */
666 struct ifnet *ifma_ifp; /* back-pointer to interface */
667 u_int ifma_refcount; /* reference count */
668 void *ifma_protospec; /* protocol-specific state, if any */
674 * ifaddr statistics update macro
676 #define IFA_STAT_INC(ifa, name, v) \
678 (ifa)->ifa_containers[mycpuid].ifa_##name += (v); \
682 * Interface (ifnet) statistics update macros
684 #define IFNET_STAT_INC(ifp, name, v) \
686 (ifp)->if_data_pcpu[mycpuid].ifd_##name += (v); \
689 #define IFNET_STAT_SET(ifp, name, v) \
692 (ifp)->if_data_pcpu[0].ifd_##name = (v); \
693 for (_cpu = 1; _cpu < ncpus; ++_cpu) \
694 (ifp)->if_data_pcpu[_cpu].ifd_##name = 0; \
697 #define IFNET_STAT_GET(ifp, name, v) \
700 (v) = (ifp)->if_data_pcpu[0].ifd_##name; \
701 for (_cpu = 1; _cpu < ncpus; ++_cpu) \
702 (v) += (ifp)->if_data_pcpu[_cpu].ifd_##name; \
705 #ifndef _SYS_SERIALIZE2_H_
706 #include <sys/serialize2.h>
715 /* interface address change event */
716 typedef void (*ifaddr_event_handler_t)(void *, struct ifnet *,
717 enum ifaddr_event, struct ifaddr *);
718 EVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t);
719 /* new interface attach event */
720 typedef void (*ifnet_attach_event_handler_t)(void *, struct ifnet *);
721 EVENTHANDLER_DECLARE(ifnet_attach_event, ifnet_attach_event_handler_t);
722 /* interface detach event */
723 typedef void (*ifnet_detach_event_handler_t)(void *, struct ifnet *);
724 EVENTHANDLER_DECLARE(ifnet_detach_event, ifnet_detach_event_handler_t);
730 char ifg_group[IFNAMSIZ];
733 int ifg_carp_demoted;
734 TAILQ_HEAD(, ifg_member) ifg_members;
735 TAILQ_ENTRY(ifg_group) ifg_next;
739 TAILQ_ENTRY(ifg_member) ifgm_next;
740 struct ifnet *ifgm_ifp;
744 struct ifg_group *ifgl_group;
745 TAILQ_ENTRY(ifg_list) ifgl_next;
748 /* group attach event */
749 typedef void (*group_attach_event_handler_t)(void *, struct ifg_group *);
750 EVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t);
751 /* group detach event */
752 typedef void (*group_detach_event_handler_t)(void *, struct ifg_group *);
753 EVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t);
754 /* group change event */
755 typedef void (*group_change_event_handler_t)(void *, const char *);
756 EVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t);
760 #define ASSERT_IFAC_VALID(ifac) do { \
761 KKASSERT((ifac)->ifa_magic == IFA_CONTAINER_MAGIC); \
762 KKASSERT((ifac)->ifa_refcnt > 0); \
765 #define ASSERT_IFAC_VALID(ifac) ((void)0)
769 _IFAREF(struct ifaddr *_ifa, int _cpu_id)
771 struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
774 ASSERT_IFAC_VALID(_ifac);
780 IFAREF(struct ifaddr *_ifa)
782 _IFAREF(_ifa, mycpuid);
785 #include <sys/malloc.h>
787 MALLOC_DECLARE(M_IFADDR);
788 MALLOC_DECLARE(M_IFMADDR);
789 MALLOC_DECLARE(M_IFNET);
791 void ifac_free(struct ifaddr_container *, int);
794 _IFAFREE(struct ifaddr *_ifa, int _cpu_id)
796 struct ifaddr_container *_ifac = &_ifa->ifa_containers[_cpu_id];
799 ASSERT_IFAC_VALID(_ifac);
800 if (--_ifac->ifa_refcnt == 0)
801 ifac_free(_ifac, _cpu_id);
806 IFAFREE(struct ifaddr *_ifa)
808 _IFAFREE(_ifa, mycpuid);
811 struct lwkt_port *ifnet_portfn(int);
812 int ifnet_domsg(struct lwkt_msg *, int);
813 void ifnet_sendmsg(struct lwkt_msg *, int);
814 void ifnet_forwardmsg(struct lwkt_msg *, int);
815 struct ifnet *ifnet_byindex(unsigned short);
818 ifa_domsg(struct lwkt_msg *_lmsg, int _cpu)
820 return ifnet_domsg(_lmsg, _cpu);
824 ifa_sendmsg(struct lwkt_msg *_lmsg, int _cpu)
826 ifnet_sendmsg(_lmsg, _cpu);
830 ifa_forwardmsg(struct lwkt_msg *_lmsg, int _nextcpu)
832 ifnet_forwardmsg(_lmsg, _nextcpu);
836 ifnet_serialize_array_enter(lwkt_serialize_t *_arr, int _arrcnt,
837 enum ifnet_serialize _slz)
839 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
840 lwkt_serialize_array_enter(_arr, _arrcnt, 0);
844 ifnet_serialize_array_exit(lwkt_serialize_t *_arr, int _arrcnt,
845 enum ifnet_serialize _slz)
847 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
848 lwkt_serialize_array_exit(_arr, _arrcnt, 0);
852 ifnet_serialize_array_try(lwkt_serialize_t *_arr, int _arrcnt,
853 enum ifnet_serialize _slz)
855 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
856 return lwkt_serialize_array_try(_arr, _arrcnt, 0);
862 ifnet_serialize_array_assert(lwkt_serialize_t *_arr, int _arrcnt,
863 enum ifnet_serialize _slz, boolean_t _serialized)
867 KKASSERT(_slz == IFNET_SERIALIZE_ALL);
869 for (_i = 0; _i < _arrcnt; ++_i)
870 ASSERT_SERIALIZED(_arr[_i]);
872 for (_i = 0; _i < _arrcnt; ++_i)
873 ASSERT_NOT_SERIALIZED(_arr[_i]);
877 #endif /* INVARIANTS */
879 #define REINPUT_KEEPRCVIF 0x0001 /* ether_reinput_oncpu() */
880 #define REINPUT_RUNBPF 0x0002 /* ether_reinput_oncpu() */
882 extern struct ifnethead ifnet;
883 extern struct ifnet **ifindex2ifnet;
884 extern int ifqmaxlen;
885 extern struct ifnet loif[];
891 void ether_ifattach(struct ifnet *, const uint8_t *,
892 struct lwkt_serialize *);
893 void ether_ifattach_bpf(struct ifnet *, const uint8_t *, u_int, u_int,
894 struct lwkt_serialize *);
895 void ether_ifdetach(struct ifnet *);
896 void ether_demux(struct mbuf *);
897 void ether_demux_oncpu(struct ifnet *, struct mbuf *);
898 void ether_reinput_oncpu(struct ifnet *, struct mbuf *, int);
899 void ether_input(struct ifnet *, struct mbuf *,
900 const struct pktinfo *, int);
901 int ether_output_frame(struct ifnet *, struct mbuf *);
902 int ether_ioctl(struct ifnet *, u_long, caddr_t);
903 u_char *kether_aton(const char *, u_char *);
904 char *kether_ntoa(const u_char *, char *);
905 struct ifnet *ether_bridge_interface(struct ifnet *ifp);
906 uint32_t ether_crc32_le(const uint8_t *, size_t);
907 uint32_t ether_crc32_be(const uint8_t *, size_t);
909 int if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **);
910 int if_addmulti_serialized(struct ifnet *, struct sockaddr *,
911 struct ifmultiaddr **);
912 int if_allmulti(struct ifnet *, int);
913 void if_attach(struct ifnet *, struct lwkt_serialize *);
914 int if_delmulti(struct ifnet *, struct sockaddr *);
915 void if_delallmulti_serialized(struct ifnet *ifp);
916 void if_purgeaddrs_nolink(struct ifnet *);
917 void if_detach(struct ifnet *);
918 void if_down(struct ifnet *);
919 void if_link_state_change(struct ifnet *);
920 void if_initname(struct ifnet *, const char *, int);
921 int if_getanyethermac(uint16_t *, int);
922 int if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
923 struct ifnet *if_alloc(uint8_t);
924 void if_free(struct ifnet *);
925 void if_route(struct ifnet *, int flag, int fam);
926 int if_setlladdr(struct ifnet *, const u_char *, int);
927 void if_unroute(struct ifnet *, int flag, int fam);
928 void if_up(struct ifnet *);
929 /*void ifinit(void);*/ /* declared in systm.h for main() */
930 int ifioctl(struct socket *, u_long, caddr_t, struct ucred *);
931 int ifpromisc(struct ifnet *, int);
932 struct ifnet *ifunit(const char *);
933 struct ifnet *if_withname(struct sockaddr *);
935 struct ifg_group *if_creategroup(const char *);
936 int if_addgroup(struct ifnet *, const char *);
937 int if_delgroup(struct ifnet *, const char *);
938 int if_getgroup(caddr_t, struct ifnet *);
939 int if_getgroupmembers(caddr_t);
941 struct ifaddr *ifa_ifwithaddr(struct sockaddr *);
942 struct ifaddr *ifa_ifwithdstaddr(struct sockaddr *);
943 struct ifaddr *ifa_ifwithnet(struct sockaddr *);
944 struct ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *);
945 struct ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *);
947 typedef void *if_com_alloc_t(u_char type, struct ifnet *ifp);
948 typedef void if_com_free_t(void *com, u_char type);
949 void if_register_com_alloc(u_char, if_com_alloc_t *a, if_com_free_t *);
950 void if_deregister_com_alloc(u_char);
952 void *ifa_create(int, int);
953 void ifa_destroy(struct ifaddr *);
954 void ifa_iflink(struct ifaddr *, struct ifnet *, int);
955 void ifa_ifunlink(struct ifaddr *, struct ifnet *);
957 struct ifaddr *ifaddr_byindex(unsigned short);
959 struct ifmultiaddr *ifmaof_ifpforaddr(struct sockaddr *, struct ifnet *);
960 int if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen);
961 void if_devstart(struct ifnet *ifp); /* COMPAT */
962 void if_devstart_sched(struct ifnet *ifp); /* COMPAT */
963 int if_ring_count2(int cnt, int cnt_max);
965 #define IF_LLSOCKADDR(ifp) \
966 ((struct sockaddr_dl *)(ifp)->if_lladdr->ifa_addr)
967 #define IF_LLADDR(ifp) LLADDR(IF_LLSOCKADDR(ifp))
970 int ifpoll_register(struct ifnet *);
971 int ifpoll_deregister(struct ifnet *);
972 #endif /* IFPOLL_ENABLE */
976 #endif /* !_NET_IF_VAR_H_ */