xref: /freebsd/sys/net/if_private.h (revision 607f11055d2d421770963162a4d9a99cdd136152)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef	_NET_IF_PRIVATE_H_
33 #define	_NET_IF_PRIVATE_H_
34 
35 #ifdef	_KERNEL
36 /*
37  * Structure defining a network interface.
38  */
39 struct ifnet {
40 	/* General book keeping of interface lists. */
41 	CK_STAILQ_ENTRY(ifnet) if_link; 	/* all struct ifnets are chained (CK_) */
42 	LIST_ENTRY(ifnet) if_clones;	/* interfaces of a cloner */
43 	CK_STAILQ_HEAD(, ifg_list) if_groups; /* linked list of groups per if (CK_) */
44 					/* protected by if_addr_lock */
45 	u_char	if_alloctype;		/* if_type at time of allocation */
46 	uint8_t	if_numa_domain;		/* NUMA domain of device */
47 	/* Driver and protocol specific information that remains stable. */
48 	void	*if_softc;		/* pointer to driver state */
49 	void	*if_llsoftc;		/* link layer softc */
50 	void	*if_l2com;		/* pointer to protocol bits */
51 	const char *if_dname;		/* driver name */
52 	int	if_dunit;		/* unit or IF_DUNIT_NONE */
53 	u_short	if_index;		/* numeric abbreviation for this if  */
54 	u_short	if_idxgen;		/* ... and its generation count */
55 	char	if_xname[IFNAMSIZ];	/* external name (name + unit) */
56 	char	*if_description;	/* interface description */
57 
58 	/* Variable fields that are touched by the stack and drivers. */
59 	int	if_flags;		/* up/down, broadcast, etc. */
60 	int	if_drv_flags;		/* driver-managed status flags */
61 	int	if_capabilities;	/* interface features & capabilities */
62 	int	if_capabilities2;	/* part 2 */
63 	int	if_capenable;		/* enabled features & capabilities */
64 	int	if_capenable2;		/* part 2 */
65 	void	*if_linkmib;		/* link-type-specific MIB data */
66 	size_t	if_linkmiblen;		/* length of above data */
67 	u_int	if_refcount;		/* reference count */
68 	u_int	if_linux_ethno;		/* linux name id for IFT_ETHER */
69 
70 	/* These fields are shared with struct if_data. */
71 	uint8_t		if_type;	/* ethernet, tokenring, etc */
72 	uint8_t		if_addrlen;	/* media address length */
73 	uint8_t		if_hdrlen;	/* media header length */
74 	uint8_t		if_link_state;	/* current link state */
75 	uint32_t	if_mtu;		/* maximum transmission unit */
76 	uint32_t	if_metric;	/* routing metric (external only) */
77 	uint64_t	if_baudrate;	/* linespeed */
78 	uint64_t	if_hwassist;	/* HW offload capabilities, see IFCAP */
79 	time_t		if_epoch;	/* uptime at attach or stat reset */
80 	struct timeval	if_lastchange;	/* time of last administrative change */
81 
82 	struct  ifaltq if_snd;		/* output queue (includes altq) */
83 	struct	task if_linktask;	/* task for link change events */
84 	struct	task if_addmultitask;	/* task for SIOCADDMULTI */
85 
86 	/* Addresses of different protocol families assigned to this if. */
87 	struct mtx if_addr_lock;	/* lock to protect address lists */
88 		/*
89 		 * if_addrhead is the list of all addresses associated to
90 		 * an interface.
91 		 * Some code in the kernel assumes that first element
92 		 * of the list has type AF_LINK, and contains sockaddr_dl
93 		 * addresses which store the link-level address and the name
94 		 * of the interface.
95 		 * However, access to the AF_LINK address through this
96 		 * field is deprecated. Use if_addr instead.
97 		 */
98 	struct	ifaddrhead if_addrhead;	/* linked list of addresses per if */
99 	struct	ifmultihead if_multiaddrs; /* multicast addresses configured */
100 	int	if_amcount;		/* number of all-multicast requests */
101 	struct	ifaddr	*if_addr;	/* pointer to link-level address */
102 	void	*if_hw_addr;		/* hardware link-level address */
103 	const u_int8_t *if_broadcastaddr; /* linklevel broadcast bytestring */
104 	struct	mtx if_afdata_lock;
105 	void	*if_afdata[AF_MAX];
106 	int	if_afdata_initialized;
107 
108 	/* Additional features hung off the interface. */
109 	u_int	if_fib;			/* interface FIB */
110 	struct	vnet *if_vnet;		/* pointer to network stack instance */
111 	struct	vnet *if_home_vnet;	/* where this ifnet originates from */
112 	struct  ifvlantrunk *if_vlantrunk; /* pointer to 802.1q data */
113 	struct	bpf_if *if_bpf;		/* packet filter structure */
114 	int	if_pcount;		/* number of promiscuous listeners */
115 	void	*if_bridge;		/* bridge glue */
116 	void	*if_lagg;		/* lagg glue */
117 	void	*if_pf_kif;		/* pf glue */
118 	struct	carp_if *if_carp;	/* carp interface structure */
119 	struct	label *if_label;	/* interface MAC label */
120 	struct	netmap_adapter *if_netmap; /* netmap(4) softc */
121 
122 	/* Various procedures of the layer2 encapsulation and drivers. */
123 	if_output_fn_t if_output;	/* output routine (enqueue) */
124 	if_input_fn_t if_input;		/* input routine (from h/w driver) */
125 	struct mbuf *(*if_bridge_input)(struct ifnet *, struct mbuf *);
126 	int	(*if_bridge_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
127 		    struct rtentry *);
128 	void (*if_bridge_linkstate)(struct ifnet *ifp);
129 	if_start_fn_t	if_start;	/* initiate output routine */
130 	if_ioctl_fn_t	if_ioctl;	/* ioctl routine */
131 	if_init_fn_t	if_init;	/* Init routine */
132 	int	(*if_resolvemulti)	/* validate/resolve multicast */
133 		(struct ifnet *, struct sockaddr **, struct sockaddr *);
134 	if_qflush_fn_t	if_qflush;	/* flush any queue */
135 	if_transmit_fn_t if_transmit;   /* initiate output routine */
136 
137 	if_reassign_fn_t if_reassign;		/* reassign to vnet routine */
138 	if_get_counter_t if_get_counter; /* get counter values */
139 	int	(*if_requestencap)	/* make link header from request */
140 		(struct ifnet *, struct if_encap_req *);
141 
142 	const struct if_ipsec_accel_methods *if_ipsec_accel_m;
143 
144 	/* Statistics. */
145 	counter_u64_t	if_counters[IFCOUNTERS];
146 
147 	/* Stuff that's only temporary and doesn't belong here. */
148 
149 	/*
150 	 * Network adapter TSO limits:
151 	 * ===========================
152 	 *
153 	 * If the "if_hw_tsomax" field is zero the maximum segment
154 	 * length limit does not apply. If the "if_hw_tsomaxsegcount"
155 	 * or the "if_hw_tsomaxsegsize" field is zero the TSO segment
156 	 * count limit does not apply. If all three fields are zero,
157 	 * there is no TSO limit.
158 	 *
159 	 * NOTE: The TSO limits should reflect the values used in the
160 	 * BUSDMA tag a network adapter is using to load a mbuf chain
161 	 * for transmission. The TCP/IP network stack will subtract
162 	 * space for all linklevel and protocol level headers and
163 	 * ensure that the full mbuf chain passed to the network
164 	 * adapter fits within the given limits.
165 	 */
166 	u_int	if_hw_tsomax;		/* TSO maximum size in bytes */
167 	u_int	if_hw_tsomaxsegcount;	/* TSO maximum segment count */
168 	u_int	if_hw_tsomaxsegsize;	/* TSO maximum segment size in bytes */
169 
170 	/*
171 	 * Network adapter send tag support:
172 	 */
173 	if_snd_tag_alloc_t *if_snd_tag_alloc;
174 
175 	/* Ratelimit (packet pacing) */
176 	if_ratelimit_query_t *if_ratelimit_query;
177 	if_ratelimit_setup_t *if_ratelimit_setup;
178 
179 	/* Ethernet PCP */
180 	uint8_t if_pcp;
181 
182 	/*
183 	 * Debugnet (Netdump) hooks to be called while in db/panic.
184 	 */
185 	struct debugnet_methods *if_debugnet_methods;
186 	struct epoch_context	if_epoch_ctx;
187 
188 	/*
189 	 * Spare fields to be added before branching a stable branch, so
190 	 * that structure can be enhanced without changing the kernel
191 	 * binary interface.
192 	 */
193 	int	if_ispare[4];		/* general use */
194 };
195 
196 #define	IF_AFDATA_LOCK_INIT(ifp)	\
197 	mtx_init(&(ifp)->if_afdata_lock, "if_afdata", NULL, MTX_DEF)
198 
199 #define	IF_AFDATA_WLOCK(ifp)	mtx_lock(&(ifp)->if_afdata_lock)
200 #define	IF_AFDATA_WUNLOCK(ifp)	mtx_unlock(&(ifp)->if_afdata_lock)
201 #define	IF_AFDATA_LOCK(ifp)	IF_AFDATA_WLOCK(ifp)
202 #define	IF_AFDATA_UNLOCK(ifp)	IF_AFDATA_WUNLOCK(ifp)
203 #define	IF_AFDATA_TRYLOCK(ifp)	mtx_trylock(&(ifp)->if_afdata_lock)
204 #define	IF_AFDATA_DESTROY(ifp)	mtx_destroy(&(ifp)->if_afdata_lock)
205 
206 #define	IF_AFDATA_LOCK_ASSERT(ifp)	MPASS(in_epoch(net_epoch_preempt) || mtx_owned(&(ifp)->if_afdata_lock))
207 #define	IF_AFDATA_WLOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_lock, MA_OWNED)
208 #define	IF_AFDATA_UNLOCK_ASSERT(ifp)	mtx_assert(&(ifp)->if_afdata_lock, MA_NOTOWNED)
209 
210 #define IF_LLADDR(ifp)							\
211     LLADDR((struct sockaddr_dl *)((ifp)->if_addr->ifa_addr))
212 
213 #endif	/* _KERNEL */
214 
215 #endif	/* _NET_IF_PRIVATE_H_ */
216