ifq.h revision 191418
1139823Simp/*- 221259Swollman * Copyright (c) 1982, 1986, 1989, 1993 321259Swollman * The Regents of the University of California. All rights reserved. 421259Swollman * 521259Swollman * Redistribution and use in source and binary forms, with or without 621259Swollman * modification, are permitted provided that the following conditions 721259Swollman * are met: 821259Swollman * 1. Redistributions of source code must retain the above copyright 921259Swollman * notice, this list of conditions and the following disclaimer. 1021259Swollman * 2. Redistributions in binary form must reproduce the above copyright 1121259Swollman * notice, this list of conditions and the following disclaimer in the 1221259Swollman * documentation and/or other materials provided with the distribution. 1321259Swollman * 4. Neither the name of the University nor the names of its contributors 1421259Swollman * may be used to endorse or promote products derived from this software 1521259Swollman * without specific prior written permission. 1621259Swollman * 1721259Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1821259Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1921259Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2021259Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2121259Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2221259Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2321259Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2421259Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2521259Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2621259Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2721259Swollman * SUCH DAMAGE. 2821259Swollman * 2921259Swollman * From: @(#)if.h 8.1 (Berkeley) 6/10/93 3050477Speter * $FreeBSD: head/sys/net/if_var.h 191418 2009-04-23 11:51:53Z rwatson $ 3121259Swollman */ 3221259Swollman 3321259Swollman#ifndef _NET_IF_VAR_H_ 3421259Swollman#define _NET_IF_VAR_H_ 3521259Swollman 3621259Swollman/* 3721259Swollman * Structures defining a network interface, providing a packet 3821259Swollman * transport mechanism (ala level 0 of the PUP protocols). 3921259Swollman * 4021259Swollman * Each interface accepts output datagrams of a specified maximum 4121259Swollman * length, and provides higher level routines with input datagrams 4221259Swollman * received from its medium. 4321259Swollman * 4421259Swollman * Output occurs when the routine if_output is called, with three parameters: 4521259Swollman * (*ifp->if_output)(ifp, m, dst, rt) 4621259Swollman * Here m is the mbuf chain to be sent and dst is the destination address. 4721259Swollman * The output routine encapsulates the supplied datagram if necessary, 4821259Swollman * and then transmits it on its medium. 4921259Swollman * 5021259Swollman * On input, each interface unwraps the data received by it, and either 51108533Sschweikh * places it on the input queue of an internetwork datagram routine 5221259Swollman * and posts the associated software interrupt, or passes the datagram to a raw 5321259Swollman * packet input routine. 5421259Swollman * 5521259Swollman * Routines exist for locating interfaces by their addresses 56108533Sschweikh * or for locating an interface on a certain network, as well as more general 5721259Swollman * routing and gateway routines maintaining information used to locate 5821259Swollman * interfaces. These routines live in the files if.c and route.c 5921259Swollman */ 6021259Swollman 6121259Swollman#ifdef __STDC__ 6221259Swollman/* 6321259Swollman * Forward structure declarations for function prototypes [sic]. 6421259Swollman */ 6521259Swollmanstruct mbuf; 6683366Sjulianstruct thread; 6721259Swollmanstruct rtentry; 6885074Srustruct rt_addrinfo; 6921259Swollmanstruct socket; 7021259Swollmanstruct ether_header; 71142215Sglebiusstruct carp_if; 72155051Sglebiusstruct ifvlantrunk; 73191148Skmacystruct route; 7421259Swollman#endif 7521259Swollman 7621259Swollman#include <sys/queue.h> /* get TAILQ macros */ 7721259Swollman 7869224Sjlemon#ifdef _KERNEL 7969152Sjlemon#include <sys/mbuf.h> 80126264Smlaier#include <sys/eventhandler.h> 81186207Skmacy#include <sys/buf_ring.h> 8269224Sjlemon#endif /* _KERNEL */ 8374914Sjhb#include <sys/lock.h> /* XXX */ 8474914Sjhb#include <sys/mutex.h> /* XXX */ 85186199Skmacy#include <sys/rwlock.h> /* XXX */ 8683130Sjlemon#include <sys/event.h> /* XXX */ 87132712Srwatson#include <sys/_task.h> 8869152Sjlemon 89121816Sbrooks#define IF_DUNIT_NONE -1 90121816Sbrooks 91130416Smlaier#include <altq/if_altq.h> 92130416Smlaier 9360938SjakeTAILQ_HEAD(ifnethead, ifnet); /* we use TAILQs so that the order of */ 9460938SjakeTAILQ_HEAD(ifaddrhead, ifaddr); /* instantiation is preserved in the list */ 9560938SjakeTAILQ_HEAD(ifprefixhead, ifprefix); 9672084SphkTAILQ_HEAD(ifmultihead, ifmultiaddr); 97159781SmlaierTAILQ_HEAD(ifgrouphead, ifg_group); 9821259Swollman 9921259Swollman/* 10021259Swollman * Structure defining a queue for a network interface. 10121259Swollman */ 10221259Swollmanstruct ifqueue { 10321259Swollman struct mbuf *ifq_head; 10421259Swollman struct mbuf *ifq_tail; 10521259Swollman int ifq_len; 10621259Swollman int ifq_maxlen; 10721259Swollman int ifq_drops; 10869152Sjlemon struct mtx ifq_mtx; 10921259Swollman}; 11021259Swollman 11121259Swollman/* 11221259Swollman * Structure defining a network interface. 11321259Swollman * 11421259Swollman * (Would like to call this struct ``if'', but C isn't PL/1.) 11521259Swollman */ 11684380Smjacob 11721259Swollmanstruct ifnet { 11821259Swollman void *if_softc; /* pointer to driver state */ 119147256Sbrooks void *if_l2com; /* pointer to protocol bits */ 12060938Sjake TAILQ_ENTRY(ifnet) if_link; /* all struct ifnets are chained */ 121121816Sbrooks char if_xname[IFNAMSIZ]; /* external name (name + unit) */ 122121816Sbrooks const char *if_dname; /* driver name */ 123121816Sbrooks int if_dunit; /* unit or IF_DUNIT_NONE */ 124191367Srwatson u_int if_refcount; /* reference count */ 12521259Swollman struct ifaddrhead if_addrhead; /* linked list of addresses per if */ 126128291Sluigi /* 127128291Sluigi * if_addrhead is the list of all addresses associated to 128128315Sluigi * an interface. 129128315Sluigi * Some code in the kernel assumes that first element 130128315Sluigi * of the list has type AF_LINK, and contains sockaddr_dl 131128315Sluigi * addresses which store the link-level address and the name 132128291Sluigi * of the interface. 133128315Sluigi * However, access to the AF_LINK address through this 134152315Sru * field is deprecated. Use if_addr or ifaddr_byindex() instead. 135128291Sluigi */ 136133741Sjmg struct knlist if_klist; /* events attached to this if */ 13783130Sjlemon int if_pcount; /* number of promiscuous listeners */ 138142901Sglebius struct carp_if *if_carp; /* carp interface structure */ 13921259Swollman struct bpf_if *if_bpf; /* packet filter structure */ 14021259Swollman u_short if_index; /* numeric abbreviation for this if */ 14121259Swollman short if_timer; /* time 'til if_watchdog called */ 142155051Sglebius struct ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */ 143102052Ssobomax int if_flags; /* up/down, broadcast, etc. */ 144162070Sandre int if_capabilities; /* interface features & capabilities */ 145162070Sandre int if_capenable; /* enabled features & capabilities */ 14621259Swollman void *if_linkmib; /* link-type-specific MIB data */ 14721259Swollman size_t if_linkmiblen; /* length of above data */ 14821259Swollman struct if_data if_data; 14921404Swollman struct ifmultihead if_multiaddrs; /* multicast addresses configured */ 15021404Swollman int if_amcount; /* number of all-multicast requests */ 15121259Swollman/* procedure handles */ 15221259Swollman int (*if_output) /* output routine (enqueue) */ 15392725Salfred (struct ifnet *, struct mbuf *, struct sockaddr *, 154191148Skmacy struct route *); 155106931Ssam void (*if_input) /* input routine (from h/w driver) */ 156106931Ssam (struct ifnet *, struct mbuf *); 15721259Swollman void (*if_start) /* initiate output routine */ 15892725Salfred (struct ifnet *); 15921259Swollman int (*if_ioctl) /* ioctl routine */ 16092725Salfred (struct ifnet *, u_long, caddr_t); 16121259Swollman void (*if_watchdog) /* timer routine */ 16292725Salfred (struct ifnet *); 16321259Swollman void (*if_init) /* Init routine */ 16492725Salfred (void *); 16521404Swollman int (*if_resolvemulti) /* validate/resolve multicast */ 16692725Salfred (struct ifnet *, struct sockaddr **, struct sockaddr *); 167189230Srwatson void (*if_qflush) /* flush any queues */ 168189230Srwatson (struct ifnet *); 169189230Srwatson int (*if_transmit) /* initiate output routine */ 170189230Srwatson (struct ifnet *, struct mbuf *); 171152315Sru struct ifaddr *if_addr; /* pointer to link-level address */ 172174388Skmacy void *if_llsoftc; /* link layer softc */ 173148265Srwatson int if_drv_flags; /* driver-managed status flags */ 174130416Smlaier struct ifaltq if_snd; /* output queue (includes altq) */ 175123220Simp const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */ 176127828Sluigi 177146986Sthompsa void *if_bridge; /* bridge glue */ 178146986Sthompsa 179122524Srwatson struct label *if_label; /* interface MAC label */ 180121161Sume 181127828Sluigi /* these are only used by IPv6 */ 182127828Sluigi struct ifprefixhead if_prefixhead; /* list of prefixes per if */ 183121161Sume void *if_afdata[AF_MAX]; 184121470Sume int if_afdata_initialized; 185186199Skmacy struct rwlock if_afdata_lock; 186145320Sglebius struct task if_linktask; /* task for link change events */ 187148640Srwatson struct mtx if_addr_mtx; /* mutex to protect address lists */ 188186119Sqingli 189152209Sthompsa LIST_ENTRY(ifnet) if_clones; /* interfaces of a cloner */ 190159781Smlaier TAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if */ 191159781Smlaier /* protected by if_addr_mtx */ 192159781Smlaier void *if_pf_kif; 193168793Sthompsa void *if_lagg; /* lagg glue */ 194191367Srwatson u_char if_alloctype; /* if_type at time of allocation */ 195189230Srwatson 196189230Srwatson /* 197189230Srwatson * Spare fields are added so that we can modify sensitive data 198189230Srwatson * structures without changing the kernel binary interface, and must 199189230Srwatson * be used with care where binary compatibility is required. 200189230Srwatson */ 201191367Srwatson char if_cspare[3]; 202189230Srwatson void *if_pspare[8]; 203189230Srwatson int if_ispare[4]; 20421259Swollman}; 20569152Sjlemon 20692725Salfredtypedef void if_init_f_t(void *); 20721259Swollman 208128376Sluigi/* 209128376Sluigi * XXX These aliases are terribly dangerous because they could apply 210128376Sluigi * to anything. 211128376Sluigi */ 21221259Swollman#define if_mtu if_data.ifi_mtu 21321259Swollman#define if_type if_data.ifi_type 21421259Swollman#define if_physical if_data.ifi_physical 21521259Swollman#define if_addrlen if_data.ifi_addrlen 21621259Swollman#define if_hdrlen if_data.ifi_hdrlen 21721259Swollman#define if_metric if_data.ifi_metric 218128871Sandre#define if_link_state if_data.ifi_link_state 21921259Swollman#define if_baudrate if_data.ifi_baudrate 22058698Sjlemon#define if_hwassist if_data.ifi_hwassist 22121259Swollman#define if_ipackets if_data.ifi_ipackets 22221259Swollman#define if_ierrors if_data.ifi_ierrors 22321259Swollman#define if_opackets if_data.ifi_opackets 22421259Swollman#define if_oerrors if_data.ifi_oerrors 22521259Swollman#define if_collisions if_data.ifi_collisions 22621259Swollman#define if_ibytes if_data.ifi_ibytes 22721259Swollman#define if_obytes if_data.ifi_obytes 22821259Swollman#define if_imcasts if_data.ifi_imcasts 22921259Swollman#define if_omcasts if_data.ifi_omcasts 23021259Swollman#define if_iqdrops if_data.ifi_iqdrops 23121259Swollman#define if_noproto if_data.ifi_noproto 23221259Swollman#define if_lastchange if_data.ifi_lastchange 233136950Sjmg#define if_rawoutput(if, m, sa) if_output(if, m, sa, (struct rtentry *)NULL) 23421259Swollman 23553541Sshin/* for compatibility with other BSDs */ 23653541Sshin#define if_addrlist if_addrhead 23753541Sshin#define if_list if_link 238160981Sbrooks#define if_name(ifp) ((ifp)->if_xname) 23953541Sshin 24021259Swollman/* 241148640Srwatson * Locks for address lists on the network interface. 242148640Srwatson */ 243148640Srwatson#define IF_ADDR_LOCK_INIT(if) mtx_init(&(if)->if_addr_mtx, \ 244148640Srwatson "if_addr_mtx", NULL, MTX_DEF) 245148640Srwatson#define IF_ADDR_LOCK_DESTROY(if) mtx_destroy(&(if)->if_addr_mtx) 246148640Srwatson#define IF_ADDR_LOCK(if) mtx_lock(&(if)->if_addr_mtx) 247148640Srwatson#define IF_ADDR_UNLOCK(if) mtx_unlock(&(if)->if_addr_mtx) 248148640Srwatson#define IF_ADDR_LOCK_ASSERT(if) mtx_assert(&(if)->if_addr_mtx, MA_OWNED) 249148640Srwatson 250148640Srwatson/* 25121259Swollman * Output queues (ifp->if_snd) and slow device input queues (*ifp->if_slowq) 25221259Swollman * are queues of messages stored on ifqueue structures 25321259Swollman * (defined above). Entries are added to and deleted from these structures 25421259Swollman * by these macros, which should be called with ipl raised to splimp(). 25521259Swollman */ 25672200Sbmilekic#define IF_LOCK(ifq) mtx_lock(&(ifq)->ifq_mtx) 25772200Sbmilekic#define IF_UNLOCK(ifq) mtx_unlock(&(ifq)->ifq_mtx) 258130416Smlaier#define IF_LOCK_ASSERT(ifq) mtx_assert(&(ifq)->ifq_mtx, MA_OWNED) 25969152Sjlemon#define _IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 26069152Sjlemon#define _IF_DROP(ifq) ((ifq)->ifq_drops++) 26169152Sjlemon#define _IF_QLEN(ifq) ((ifq)->ifq_len) 26221259Swollman 26369152Sjlemon#define _IF_ENQUEUE(ifq, m) do { \ 26469152Sjlemon (m)->m_nextpkt = NULL; \ 26569152Sjlemon if ((ifq)->ifq_tail == NULL) \ 26669152Sjlemon (ifq)->ifq_head = m; \ 26769152Sjlemon else \ 26869152Sjlemon (ifq)->ifq_tail->m_nextpkt = m; \ 26969152Sjlemon (ifq)->ifq_tail = m; \ 27069152Sjlemon (ifq)->ifq_len++; \ 27169152Sjlemon} while (0) 27269152Sjlemon 27369152Sjlemon#define IF_ENQUEUE(ifq, m) do { \ 27469152Sjlemon IF_LOCK(ifq); \ 27569152Sjlemon _IF_ENQUEUE(ifq, m); \ 27669152Sjlemon IF_UNLOCK(ifq); \ 27769152Sjlemon} while (0) 27869152Sjlemon 27969152Sjlemon#define _IF_PREPEND(ifq, m) do { \ 28069152Sjlemon (m)->m_nextpkt = (ifq)->ifq_head; \ 28169152Sjlemon if ((ifq)->ifq_tail == NULL) \ 28269152Sjlemon (ifq)->ifq_tail = (m); \ 28369152Sjlemon (ifq)->ifq_head = (m); \ 28469152Sjlemon (ifq)->ifq_len++; \ 28569152Sjlemon} while (0) 28669152Sjlemon 28769152Sjlemon#define IF_PREPEND(ifq, m) do { \ 28869152Sjlemon IF_LOCK(ifq); \ 28969152Sjlemon _IF_PREPEND(ifq, m); \ 29069152Sjlemon IF_UNLOCK(ifq); \ 29169152Sjlemon} while (0) 29269152Sjlemon 29369152Sjlemon#define _IF_DEQUEUE(ifq, m) do { \ 29469152Sjlemon (m) = (ifq)->ifq_head; \ 29569152Sjlemon if (m) { \ 296136950Sjmg if (((ifq)->ifq_head = (m)->m_nextpkt) == NULL) \ 29769152Sjlemon (ifq)->ifq_tail = NULL; \ 29869152Sjlemon (m)->m_nextpkt = NULL; \ 29969152Sjlemon (ifq)->ifq_len--; \ 30069152Sjlemon } \ 30169152Sjlemon} while (0) 30269152Sjlemon 30369152Sjlemon#define IF_DEQUEUE(ifq, m) do { \ 30469152Sjlemon IF_LOCK(ifq); \ 30569152Sjlemon _IF_DEQUEUE(ifq, m); \ 30669152Sjlemon IF_UNLOCK(ifq); \ 30769152Sjlemon} while (0) 30869152Sjlemon 309130416Smlaier#define _IF_POLL(ifq, m) ((m) = (ifq)->ifq_head) 310130416Smlaier#define IF_POLL(ifq, m) _IF_POLL(ifq, m) 311130416Smlaier 312130416Smlaier#define _IF_DRAIN(ifq) do { \ 31369152Sjlemon struct mbuf *m; \ 31469152Sjlemon for (;;) { \ 31569152Sjlemon _IF_DEQUEUE(ifq, m); \ 31669152Sjlemon if (m == NULL) \ 31769152Sjlemon break; \ 31869152Sjlemon m_freem(m); \ 31969152Sjlemon } \ 32069152Sjlemon} while (0) 32169152Sjlemon 322130416Smlaier#define IF_DRAIN(ifq) do { \ 323130416Smlaier IF_LOCK(ifq); \ 324130416Smlaier _IF_DRAIN(ifq); \ 325130416Smlaier IF_UNLOCK(ifq); \ 326130416Smlaier} while(0) 327130416Smlaier 32855205Speter#ifdef _KERNEL 329126264Smlaier/* interface address change event */ 330126264Smlaiertypedef void (*ifaddr_event_handler_t)(void *, struct ifnet *); 331126264SmlaierEVENTHANDLER_DECLARE(ifaddr_event, ifaddr_event_handler_t); 332126264Smlaier/* new interface arrival event */ 333126264Smlaiertypedef void (*ifnet_arrival_event_handler_t)(void *, struct ifnet *); 334126264SmlaierEVENTHANDLER_DECLARE(ifnet_arrival_event, ifnet_arrival_event_handler_t); 335126264Smlaier/* interface departure event */ 336126264Smlaiertypedef void (*ifnet_departure_event_handler_t)(void *, struct ifnet *); 337126264SmlaierEVENTHANDLER_DECLARE(ifnet_departure_event, ifnet_departure_event_handler_t); 338126264Smlaier 339159781Smlaier/* 340159781Smlaier * interface groups 341159781Smlaier */ 342159781Smlaierstruct ifg_group { 343159781Smlaier char ifg_group[IFNAMSIZ]; 344159781Smlaier u_int ifg_refcnt; 345159781Smlaier void *ifg_pf_kif; 346159781Smlaier TAILQ_HEAD(, ifg_member) ifg_members; 347159781Smlaier TAILQ_ENTRY(ifg_group) ifg_next; 348159781Smlaier}; 349159781Smlaier 350159781Smlaierstruct ifg_member { 351159781Smlaier TAILQ_ENTRY(ifg_member) ifgm_next; 352159781Smlaier struct ifnet *ifgm_ifp; 353159781Smlaier}; 354159781Smlaier 355159781Smlaierstruct ifg_list { 356159781Smlaier struct ifg_group *ifgl_group; 357159781Smlaier TAILQ_ENTRY(ifg_list) ifgl_next; 358159781Smlaier}; 359159781Smlaier 360159781Smlaier/* group attach event */ 361159781Smlaiertypedef void (*group_attach_event_handler_t)(void *, struct ifg_group *); 362159781SmlaierEVENTHANDLER_DECLARE(group_attach_event, group_attach_event_handler_t); 363159781Smlaier/* group detach event */ 364159781Smlaiertypedef void (*group_detach_event_handler_t)(void *, struct ifg_group *); 365159781SmlaierEVENTHANDLER_DECLARE(group_detach_event, group_detach_event_handler_t); 366159781Smlaier/* group change event */ 367159781Smlaiertypedef void (*group_change_event_handler_t)(void *, const char *); 368159781SmlaierEVENTHANDLER_DECLARE(group_change_event, group_change_event_handler_t); 369159781Smlaier 370121470Sume#define IF_AFDATA_LOCK_INIT(ifp) \ 371186199Skmacy rw_init(&(ifp)->if_afdata_lock, "if_afdata") 372121470Sume 373186199Skmacy#define IF_AFDATA_WLOCK(ifp) rw_wlock(&(ifp)->if_afdata_lock) 374186199Skmacy#define IF_AFDATA_RLOCK(ifp) rw_rlock(&(ifp)->if_afdata_lock) 375186199Skmacy#define IF_AFDATA_WUNLOCK(ifp) rw_wunlock(&(ifp)->if_afdata_lock) 376186199Skmacy#define IF_AFDATA_RUNLOCK(ifp) rw_runlock(&(ifp)->if_afdata_lock) 377186199Skmacy#define IF_AFDATA_LOCK(ifp) IF_AFDATA_WLOCK(ifp) 378186199Skmacy#define IF_AFDATA_UNLOCK(ifp) IF_AFDATA_WUNLOCK(ifp) 379186199Skmacy#define IF_AFDATA_TRYLOCK(ifp) rw_try_wlock(&(ifp)->if_afdata_lock) 380186199Skmacy#define IF_AFDATA_DESTROY(ifp) rw_destroy(&(ifp)->if_afdata_lock) 381186119Sqingli 382186199Skmacy#define IF_AFDATA_LOCK_ASSERT(ifp) rw_assert(&(ifp)->if_afdata_lock, RA_LOCKED) 383186199Skmacy#define IF_AFDATA_UNLOCK_ASSERT(ifp) rw_assert(&(ifp)->if_afdata_lock, RA_UNLOCKED) 384186199Skmacy 385137065Srwatsonint if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, 386137065Srwatson int adjust); 387130416Smlaier#define IF_HANDOFF(ifq, m, ifp) \ 388130416Smlaier if_handoff((struct ifqueue *)ifq, m, ifp, 0) 389130416Smlaier#define IF_HANDOFF_ADJ(ifq, m, ifp, adj) \ 390130416Smlaier if_handoff((struct ifqueue *)ifq, m, ifp, adj) 39121259Swollman 392132712Srwatsonvoid if_start(struct ifnet *); 393132712Srwatson 394130416Smlaier#define IFQ_ENQUEUE(ifq, m, err) \ 395130416Smlaierdo { \ 396130416Smlaier IF_LOCK(ifq); \ 397130416Smlaier if (ALTQ_IS_ENABLED(ifq)) \ 398130416Smlaier ALTQ_ENQUEUE(ifq, m, NULL, err); \ 399130416Smlaier else { \ 400130416Smlaier if (_IF_QFULL(ifq)) { \ 401130416Smlaier m_freem(m); \ 402130416Smlaier (err) = ENOBUFS; \ 403130416Smlaier } else { \ 404130416Smlaier _IF_ENQUEUE(ifq, m); \ 405130416Smlaier (err) = 0; \ 406130416Smlaier } \ 407130416Smlaier } \ 408130416Smlaier if (err) \ 409130416Smlaier (ifq)->ifq_drops++; \ 410130416Smlaier IF_UNLOCK(ifq); \ 411130416Smlaier} while (0) 41221259Swollman 413130416Smlaier#define IFQ_DEQUEUE_NOLOCK(ifq, m) \ 414130416Smlaierdo { \ 415130416Smlaier if (TBR_IS_ENABLED(ifq)) \ 416130508Smlaier (m) = tbr_dequeue_ptr(ifq, ALTDQ_REMOVE); \ 417130416Smlaier else if (ALTQ_IS_ENABLED(ifq)) \ 418130416Smlaier ALTQ_DEQUEUE(ifq, m); \ 419130416Smlaier else \ 420130416Smlaier _IF_DEQUEUE(ifq, m); \ 421130416Smlaier} while (0) 422130416Smlaier 423130416Smlaier#define IFQ_DEQUEUE(ifq, m) \ 424130416Smlaierdo { \ 425130416Smlaier IF_LOCK(ifq); \ 426130416Smlaier IFQ_DEQUEUE_NOLOCK(ifq, m); \ 427130416Smlaier IF_UNLOCK(ifq); \ 428130416Smlaier} while (0) 429130416Smlaier 430130416Smlaier#define IFQ_POLL_NOLOCK(ifq, m) \ 431130416Smlaierdo { \ 432130416Smlaier if (TBR_IS_ENABLED(ifq)) \ 433130508Smlaier (m) = tbr_dequeue_ptr(ifq, ALTDQ_POLL); \ 434130416Smlaier else if (ALTQ_IS_ENABLED(ifq)) \ 435130416Smlaier ALTQ_POLL(ifq, m); \ 436130416Smlaier else \ 437130416Smlaier _IF_POLL(ifq, m); \ 438130416Smlaier} while (0) 439130416Smlaier 440130416Smlaier#define IFQ_POLL(ifq, m) \ 441130416Smlaierdo { \ 442130416Smlaier IF_LOCK(ifq); \ 443130416Smlaier IFQ_POLL_NOLOCK(ifq, m); \ 444130416Smlaier IF_UNLOCK(ifq); \ 445130416Smlaier} while (0) 446130416Smlaier 447130416Smlaier#define IFQ_PURGE_NOLOCK(ifq) \ 448130416Smlaierdo { \ 449130416Smlaier if (ALTQ_IS_ENABLED(ifq)) { \ 450130416Smlaier ALTQ_PURGE(ifq); \ 451130416Smlaier } else \ 452130416Smlaier _IF_DRAIN(ifq); \ 453130416Smlaier} while (0) 454130416Smlaier 455130416Smlaier#define IFQ_PURGE(ifq) \ 456130416Smlaierdo { \ 457130416Smlaier IF_LOCK(ifq); \ 458130416Smlaier IFQ_PURGE_NOLOCK(ifq); \ 459130416Smlaier IF_UNLOCK(ifq); \ 460130416Smlaier} while (0) 461130416Smlaier 462130416Smlaier#define IFQ_SET_READY(ifq) \ 463130416Smlaier do { ((ifq)->altq_flags |= ALTQF_READY); } while (0) 464130416Smlaier 465130416Smlaier#define IFQ_LOCK(ifq) IF_LOCK(ifq) 466130416Smlaier#define IFQ_UNLOCK(ifq) IF_UNLOCK(ifq) 467130416Smlaier#define IFQ_LOCK_ASSERT(ifq) IF_LOCK_ASSERT(ifq) 468130416Smlaier#define IFQ_IS_EMPTY(ifq) ((ifq)->ifq_len == 0) 469130416Smlaier#define IFQ_INC_LEN(ifq) ((ifq)->ifq_len++) 470130416Smlaier#define IFQ_DEC_LEN(ifq) (--(ifq)->ifq_len) 471130416Smlaier#define IFQ_INC_DROPS(ifq) ((ifq)->ifq_drops++) 472130416Smlaier#define IFQ_SET_MAXLEN(ifq, len) ((ifq)->ifq_maxlen = (len)) 473130416Smlaier 474148886Srwatson/* 475148886Srwatson * The IFF_DRV_OACTIVE test should really occur in the device driver, not in 476148886Srwatson * the handoff logic, as that flag is locked by the device driver. 477148886Srwatson */ 478130416Smlaier#define IFQ_HANDOFF_ADJ(ifp, m, adj, err) \ 479130416Smlaierdo { \ 480130416Smlaier int len; \ 481130416Smlaier short mflags; \ 482130416Smlaier \ 483130416Smlaier len = (m)->m_pkthdr.len; \ 484130416Smlaier mflags = (m)->m_flags; \ 485130416Smlaier IFQ_ENQUEUE(&(ifp)->if_snd, m, err); \ 486130416Smlaier if ((err) == 0) { \ 487130416Smlaier (ifp)->if_obytes += len + (adj); \ 488130416Smlaier if (mflags & M_MCAST) \ 489130416Smlaier (ifp)->if_omcasts++; \ 490148886Srwatson if (((ifp)->if_drv_flags & IFF_DRV_OACTIVE) == 0) \ 491132712Srwatson if_start(ifp); \ 492130416Smlaier } \ 493130416Smlaier} while (0) 494130416Smlaier 495130416Smlaier#define IFQ_HANDOFF(ifp, m, err) \ 496130512Smlaier IFQ_HANDOFF_ADJ(ifp, m, 0, err) 497130416Smlaier 498130416Smlaier#define IFQ_DRV_DEQUEUE(ifq, m) \ 499130416Smlaierdo { \ 500130416Smlaier (m) = (ifq)->ifq_drv_head; \ 501130416Smlaier if (m) { \ 502130416Smlaier if (((ifq)->ifq_drv_head = (m)->m_nextpkt) == NULL) \ 503130416Smlaier (ifq)->ifq_drv_tail = NULL; \ 504130416Smlaier (m)->m_nextpkt = NULL; \ 505130416Smlaier (ifq)->ifq_drv_len--; \ 506130416Smlaier } else { \ 507130416Smlaier IFQ_LOCK(ifq); \ 508130416Smlaier IFQ_DEQUEUE_NOLOCK(ifq, m); \ 509130416Smlaier while ((ifq)->ifq_drv_len < (ifq)->ifq_drv_maxlen) { \ 510130416Smlaier struct mbuf *m0; \ 511130416Smlaier IFQ_DEQUEUE_NOLOCK(ifq, m0); \ 512130416Smlaier if (m0 == NULL) \ 513130416Smlaier break; \ 514130416Smlaier m0->m_nextpkt = NULL; \ 515130416Smlaier if ((ifq)->ifq_drv_tail == NULL) \ 516130416Smlaier (ifq)->ifq_drv_head = m0; \ 517130416Smlaier else \ 518130416Smlaier (ifq)->ifq_drv_tail->m_nextpkt = m0; \ 519130416Smlaier (ifq)->ifq_drv_tail = m0; \ 520130416Smlaier (ifq)->ifq_drv_len++; \ 521130416Smlaier } \ 522130416Smlaier IFQ_UNLOCK(ifq); \ 523130416Smlaier } \ 524130416Smlaier} while (0) 525130416Smlaier 526130416Smlaier#define IFQ_DRV_PREPEND(ifq, m) \ 527130416Smlaierdo { \ 528130416Smlaier (m)->m_nextpkt = (ifq)->ifq_drv_head; \ 529132152Smlaier if ((ifq)->ifq_drv_tail == NULL) \ 530132152Smlaier (ifq)->ifq_drv_tail = (m); \ 531130416Smlaier (ifq)->ifq_drv_head = (m); \ 532130416Smlaier (ifq)->ifq_drv_len++; \ 533130416Smlaier} while (0) 534130416Smlaier 535130416Smlaier#define IFQ_DRV_IS_EMPTY(ifq) \ 536130416Smlaier (((ifq)->ifq_drv_len == 0) && ((ifq)->ifq_len == 0)) 537130416Smlaier 538130416Smlaier#define IFQ_DRV_PURGE(ifq) \ 539130416Smlaierdo { \ 540132152Smlaier struct mbuf *m, *n = (ifq)->ifq_drv_head; \ 541132152Smlaier while((m = n) != NULL) { \ 542132152Smlaier n = m->m_nextpkt; \ 543130416Smlaier m_freem(m); \ 544130416Smlaier } \ 545130416Smlaier (ifq)->ifq_drv_head = (ifq)->ifq_drv_tail = NULL; \ 546130416Smlaier (ifq)->ifq_drv_len = 0; \ 547130416Smlaier IFQ_PURGE(ifq); \ 548130416Smlaier} while (0) 549130416Smlaier 550186207Skmacy#ifdef _KERNEL 551186213Skmacystatic __inline void 552186213Skmacydrbr_stats_update(struct ifnet *ifp, int len, int mflags) 553186213Skmacy{ 554186213Skmacy 555186213Skmacy ifp->if_obytes += len; 556186213Skmacy if (mflags & M_MCAST) 557186213Skmacy ifp->if_omcasts++; 558186213Skmacy} 559186213Skmacy 560186207Skmacystatic __inline int 561186213Skmacydrbr_enqueue(struct ifnet *ifp, struct buf_ring *br, struct mbuf *m) 562186207Skmacy{ 563186207Skmacy int error = 0; 564186213Skmacy int len = m->m_pkthdr.len; 565186213Skmacy int mflags = m->m_flags; 566186207Skmacy 567191033Skmacy#ifdef ALTQ 568191033Skmacy if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 569191033Skmacy IFQ_ENQUEUE(&ifp->if_snd, m, error); 570191033Skmacy return (error); 571191033Skmacy } 572191033Skmacy#endif 573186207Skmacy if ((error = buf_ring_enqueue(br, m)) == ENOBUFS) { 574186207Skmacy br->br_drops++; 575186213Skmacy _IF_DROP(&ifp->if_snd); 576186207Skmacy m_freem(m); 577186213Skmacy } else 578186213Skmacy drbr_stats_update(ifp, len, mflags); 579186213Skmacy 580186207Skmacy return (error); 581186207Skmacy} 582186207Skmacy 583186207Skmacystatic __inline void 584186207Skmacydrbr_free(struct buf_ring *br, struct malloc_type *type) 585186207Skmacy{ 586186207Skmacy struct mbuf *m; 587186207Skmacy 588186207Skmacy while ((m = buf_ring_dequeue_sc(br)) != NULL) 589186207Skmacy m_freem(m); 590186207Skmacy 591186207Skmacy buf_ring_free(br, type); 592186207Skmacy} 593191033Skmacy 594191033Skmacystatic __inline struct mbuf * 595191033Skmacydrbr_dequeue(struct ifnet *ifp, struct buf_ring *br) 596191033Skmacy{ 597191033Skmacy#ifdef ALTQ 598191033Skmacy struct mbuf *m; 599191033Skmacy 600191033Skmacy if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 601191033Skmacy IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 602191033Skmacy return (m); 603191033Skmacy } 604186207Skmacy#endif 605191033Skmacy return (buf_ring_dequeue_sc(br)); 606191033Skmacy} 607186207Skmacy 608191033Skmacystatic __inline int 609191033Skmacydrbr_empty(struct ifnet *ifp, struct buf_ring *br) 610191033Skmacy{ 611191033Skmacy#ifdef ALTQ 612191033Skmacy if (ALTQ_IS_ENABLED(&ifp->if_snd)) 613191033Skmacy return (IFQ_DRV_IS_EMPTY(&ifp->if_snd)); 614191033Skmacy#endif 615191033Skmacy return (buf_ring_empty(br)); 616191033Skmacy} 617191033Skmacy#endif 61849459Sbrian/* 61949459Sbrian * 72 was chosen below because it is the size of a TCP/IP 62049459Sbrian * header (40) + the minimum mss (32). 62149459Sbrian */ 62249459Sbrian#define IF_MINMTU 72 62349459Sbrian#define IF_MAXMTU 65535 62449459Sbrian 62555205Speter#endif /* _KERNEL */ 62621259Swollman 62721259Swollman/* 62821259Swollman * The ifaddr structure contains information about one address 62921259Swollman * of an interface. They are maintained by the different address families, 63021259Swollman * are allocated and attached when an address is set, and are linked 63121259Swollman * together so all addresses for an interface can be located. 632128291Sluigi * 633128291Sluigi * NOTE: a 'struct ifaddr' is always at the beginning of a larger 634128291Sluigi * chunk of malloc'ed memory, where we store the three addresses 635128291Sluigi * (ifa_addr, ifa_dstaddr and ifa_netmask) referenced here. 63621259Swollman */ 63721259Swollmanstruct ifaddr { 63821259Swollman struct sockaddr *ifa_addr; /* address of interface */ 63921259Swollman struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 64021259Swollman#define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 64121259Swollman struct sockaddr *ifa_netmask; /* used to determine subnet */ 64267334Sjoe struct if_data if_data; /* not all members are meaningful */ 64321259Swollman struct ifnet *ifa_ifp; /* back-pointer to interface */ 64460938Sjake TAILQ_ENTRY(ifaddr) ifa_link; /* queue macro glue */ 64521259Swollman void (*ifa_rtrequest) /* check or clean routes (+ or -)'d */ 64692725Salfred (int, struct rtentry *, struct rt_addrinfo *); 64721259Swollman u_short ifa_flags; /* mostly rt_flags for cloning */ 64847254Spb u_int ifa_refcnt; /* references to this structure */ 64921259Swollman int ifa_metric; /* cost of going out this interface */ 65028845Sjulian int (*ifa_claim_addr) /* check if an addr goes to this if */ 65192725Salfred (struct ifaddr *, struct sockaddr *); 652108033Shsu struct mtx ifa_mtx; 65321259Swollman}; 65421259Swollman#define IFA_ROUTE RTF_UP /* route installed */ 65521259Swollman 65653541Sshin/* for compatibility with other BSDs */ 65753541Sshin#define ifa_list ifa_link 65853541Sshin 659108033Shsu#define IFA_LOCK_INIT(ifa) \ 660108033Shsu mtx_init(&(ifa)->ifa_mtx, "ifaddr", NULL, MTX_DEF) 661108033Shsu#define IFA_LOCK(ifa) mtx_lock(&(ifa)->ifa_mtx) 662108033Shsu#define IFA_UNLOCK(ifa) mtx_unlock(&(ifa)->ifa_mtx) 663108033Shsu#define IFA_DESTROY(ifa) mtx_destroy(&(ifa)->ifa_mtx) 664108033Shsu 66521404Swollman/* 66652904Sshin * The prefix structure contains information about one prefix 66752904Sshin * of an interface. They are maintained by the different address families, 668108470Sschweikh * are allocated and attached when a prefix or an address is set, 66953541Sshin * and are linked together so all prefixes for an interface can be located. 67052904Sshin */ 67152904Sshinstruct ifprefix { 67252904Sshin struct sockaddr *ifpr_prefix; /* prefix of interface */ 67352904Sshin struct ifnet *ifpr_ifp; /* back-pointer to interface */ 67460938Sjake TAILQ_ENTRY(ifprefix) ifpr_list; /* queue macro glue */ 67552904Sshin u_char ifpr_plen; /* prefix length in bits */ 67652904Sshin u_char ifpr_type; /* protocol dependent prefix type */ 67752904Sshin}; 67852904Sshin 67952904Sshin/* 68021404Swollman * Multicast address structure. This is analogous to the ifaddr 68121404Swollman * structure except that it keeps track of multicast addresses. 68221404Swollman */ 68321404Swollmanstruct ifmultiaddr { 68472084Sphk TAILQ_ENTRY(ifmultiaddr) ifma_link; /* queue macro glue */ 68521434Swollman struct sockaddr *ifma_addr; /* address this membership is for */ 68621434Swollman struct sockaddr *ifma_lladdr; /* link-layer translation, if any */ 68721434Swollman struct ifnet *ifma_ifp; /* back-pointer to interface */ 68821434Swollman u_int ifma_refcount; /* reference count */ 68921434Swollman void *ifma_protospec; /* protocol-specific state, if any */ 690167729Sbms struct ifmultiaddr *ifma_llifma; /* pointer to ifma for ifma_lladdr */ 69121404Swollman}; 69221404Swollman 69355205Speter#ifdef _KERNEL 694108036Shsu#define IFAFREE(ifa) \ 695108036Shsu do { \ 696108036Shsu IFA_LOCK(ifa); \ 697108036Shsu KASSERT((ifa)->ifa_refcnt > 0, \ 698108036Shsu ("ifa %p !(ifa_refcnt > 0)", ifa)); \ 699108036Shsu if (--(ifa)->ifa_refcnt == 0) { \ 700108036Shsu IFA_DESTROY(ifa); \ 701108036Shsu free(ifa, M_IFADDR); \ 702108036Shsu } else \ 703108036Shsu IFA_UNLOCK(ifa); \ 70446568Speter } while (0) 70521259Swollman 706108036Shsu#define IFAREF(ifa) \ 707108036Shsu do { \ 708108036Shsu IFA_LOCK(ifa); \ 709108036Shsu ++(ifa)->ifa_refcnt; \ 710108036Shsu IFA_UNLOCK(ifa); \ 711108033Shsu } while (0) 712108033Shsu 713186199Skmacyextern struct rwlock ifnet_lock; 714108298Shsu#define IFNET_LOCK_INIT() \ 715186199Skmacy rw_init_flags(&ifnet_lock, "ifnet", RW_RECURSE) 716186199Skmacy#define IFNET_WLOCK() rw_wlock(&ifnet_lock) 717186199Skmacy#define IFNET_WUNLOCK() rw_wunlock(&ifnet_lock) 718186199Skmacy#define IFNET_WLOCK_ASSERT() rw_assert(&ifnet_lock, RA_LOCKED) 719186199Skmacy#define IFNET_RLOCK() rw_rlock(&ifnet_lock) 720186199Skmacy#define IFNET_RUNLOCK() rw_runlock(&ifnet_lock) 721108172Shsu 72283130Sjlemonstruct ifindex_entry { 72387914Sjlemon struct ifnet *ife_ifnet; 724130585Sphk struct cdev *ife_dev; 72583130Sjlemon}; 72683130Sjlemon 727191367Srwatson/* 728191367Srwatson * Look up an ifnet given its index; the _ref variant also acquires a 729191367Srwatson * reference that must be freed using if_rele(). It is almost always a bug 730191367Srwatson * to call ifnet_byindex() instead if ifnet_byindex_ref(). 731191367Srwatson */ 732180042Srwatsonstruct ifnet *ifnet_byindex(u_short idx); 733191367Srwatsonstruct ifnet *ifnet_byindex_ref(u_short idx); 734181892Skmacy 735128315Sluigi/* 736128315Sluigi * Given the index, ifaddr_byindex() returns the one and only 737128315Sluigi * link-level ifaddr for the interface. You are not supposed to use 738128315Sluigi * it to traverse the list of addresses associated to the interface. 739128315Sluigi */ 740180042Srwatsonstruct ifaddr *ifaddr_byindex(u_short idx); 741180042Srwatsonstruct cdev *ifdev_byindex(u_short idx); 74283130Sjlemon 743186048Sbz#ifdef VIMAGE_GLOBALS 74421259Swollmanextern struct ifnethead ifnet; 74571791Speterextern struct ifnet *loif; /* first loopback interface */ 74621259Swollmanextern int if_index; 747186048Sbz#endif 748186048Sbzextern int ifqmaxlen; 74921259Swollman 750159781Smlaierint if_addgroup(struct ifnet *, const char *); 751159781Smlaierint if_delgroup(struct ifnet *, const char *); 75292725Salfredint if_addmulti(struct ifnet *, struct sockaddr *, struct ifmultiaddr **); 75392725Salfredint if_allmulti(struct ifnet *, int); 754147256Sbrooksstruct ifnet* if_alloc(u_char); 75592725Salfredvoid if_attach(struct ifnet *); 756191418Srwatsonvoid if_dead(struct ifnet *); 75792725Salfredint if_delmulti(struct ifnet *, struct sockaddr *); 758167729Sbmsvoid if_delmulti_ifma(struct ifmultiaddr *); 75992725Salfredvoid if_detach(struct ifnet *); 760146620Speadarvoid if_purgeaddrs(struct ifnet *); 761177617Ssamvoid if_purgemaddrs(struct ifnet *); 76292725Salfredvoid if_down(struct ifnet *); 763167732Sbmsstruct ifmultiaddr * 764167732Sbms if_findmulti(struct ifnet *, struct sockaddr *); 765147256Sbrooksvoid if_free(struct ifnet *); 766147256Sbrooksvoid if_free_type(struct ifnet *, u_char); 767121816Sbrooksvoid if_initname(struct ifnet *, const char *, int); 768138542Ssamvoid if_link_state_change(struct ifnet *, int); 769103900Sbrooksint if_printf(struct ifnet *, const char *, ...) __printflike(2, 3); 770191161Skmacyvoid if_qflush(struct ifnet *); 771191367Srwatsonvoid if_ref(struct ifnet *); 772191367Srwatsonvoid if_rele(struct ifnet *); 77392725Salfredint if_setlladdr(struct ifnet *, const u_char *, int); 77492725Salfredvoid if_up(struct ifnet *); 77592725Salfred/*void ifinit(void);*/ /* declared in systm.h for main() */ 77692725Salfredint ifioctl(struct socket *, u_long, caddr_t, struct thread *); 77792725Salfredint ifpromisc(struct ifnet *, int); 77892725Salfredstruct ifnet *ifunit(const char *); 77921259Swollman 780185162Skmacyvoid ifq_attach(struct ifaltq *, struct ifnet *ifp); 781185162Skmacyvoid ifq_detach(struct ifaltq *); 782185162Skmacy 78392725Salfredstruct ifaddr *ifa_ifwithaddr(struct sockaddr *); 784162068Sandrestruct ifaddr *ifa_ifwithbroadaddr(struct sockaddr *); 78592725Salfredstruct ifaddr *ifa_ifwithdstaddr(struct sockaddr *); 78692725Salfredstruct ifaddr *ifa_ifwithnet(struct sockaddr *); 78792725Salfredstruct ifaddr *ifa_ifwithroute(int, struct sockaddr *, struct sockaddr *); 788178888Sjulianstruct ifaddr *ifa_ifwithroute_fib(int, struct sockaddr *, struct sockaddr *, u_int); 789178888Sjulian 79092725Salfredstruct ifaddr *ifaof_ifpforaddr(struct sockaddr *, struct ifnet *); 79121259Swollman 79292725Salfredint if_simloop(struct ifnet *ifp, struct mbuf *m, int af, int hlen); 79321434Swollman 794147256Sbrookstypedef void *if_com_alloc_t(u_char type, struct ifnet *ifp); 795147256Sbrookstypedef void if_com_free_t(void *com, u_char type); 796147256Sbrooksvoid if_register_com_alloc(u_char type, if_com_alloc_t *a, if_com_free_t *f); 797147256Sbrooksvoid if_deregister_com_alloc(u_char type); 798147256Sbrooks 79984931Sfjoe#define IF_LLADDR(ifp) \ 800152315Sru LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr)) 80184931Sfjoe 80287902Sluigi#ifdef DEVICE_POLLING 803150789Sglebiusenum poll_cmd { POLL_ONLY, POLL_AND_CHECK_STATUS }; 80487902Sluigi 80592725Salfredtypedef void poll_handler_t(struct ifnet *ifp, enum poll_cmd cmd, int count); 80692725Salfredint ether_poll_register(poll_handler_t *h, struct ifnet *ifp); 80792725Salfredint ether_poll_deregister(struct ifnet *ifp); 80887902Sluigi#endif /* DEVICE_POLLING */ 80987902Sluigi 81055205Speter#endif /* _KERNEL */ 81121259Swollman 81221259Swollman#endif /* !_NET_IF_VAR_H_ */ 813