xref: /xnu-11215.61.5/bsd/net/dlil.h (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1 /*
2  * Copyright (c) 1999-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #ifndef DLIL_H
29 #define DLIL_H
30 #ifdef KERNEL
31 
32 #include <sys/kernel_types.h>
33 #include <net/kpi_interface.h>
34 
35 enum {
36 	BPF_TAP_DISABLE,
37 	BPF_TAP_INPUT,
38 	BPF_TAP_OUTPUT,
39 	BPF_TAP_INPUT_OUTPUT
40 };
41 
42 /*
43  * DLIL_DESC_ETYPE2 - native_type must point to 2 byte ethernet raw protocol,
44  *                    variants.native_type_length must be set to 2
45  * DLIL_DESC_SAP - native_type must point to 3 byte SAP protocol
46  *                 variants.native_type_length must be set to 3
47  * DLIL_DESC_SNAP - native_type must point to 5 byte SNAP protocol
48  *                  variants.native_type_length must be set to 5
49  *
50  * All protocols must be in Network byte order.
51  *
52  * Future interface families may define more protocol types they know about.
53  * The type implies the offset and context of the protocol data at native_type.
54  * The length of the protocol data specified at native_type must be set in
55  * variants.native_type_length.
56  */
57 /* Ethernet specific types */
58 #define DLIL_DESC_ETYPE2        4
59 #define DLIL_DESC_SAP           5
60 #define DLIL_DESC_SNAP          6
61 
62 #ifdef KERNEL_PRIVATE
63 #include <net/if.h>
64 #include <net/if_var.h>
65 #include <net/classq/classq.h>
66 #include <net/flowadv.h>
67 #include <sys/kern_event.h>
68 #include <kern/thread.h>
69 #include <kern/locks.h>
70 
71 #ifdef BSD_KERNEL_PRIVATE
72 /* Operations on timespecs. */
73 #define net_timerclear(tvp)     (tvp)->tv_sec = (tvp)->tv_nsec = 0
74 
75 #define net_timerisset(tvp)     ((tvp)->tv_sec || (tvp)->tv_nsec)
76 
77 #define net_timercmp(tvp, uvp, cmp)                                     \
78 	(((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
79 	((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                           \
80 	((tvp)->tv_sec cmp (uvp)->tv_sec))
81 
82 #define net_timeradd(tvp, uvp, vvp) do {                                \
83 	(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;                  \
84 	(vvp)->tv_nsec = (tvp)->tv_nsec + (uvp)->tv_nsec;               \
85 	if ((vvp)->tv_nsec >= (long)NSEC_PER_SEC) {                     \
86 	        (vvp)->tv_sec++;                                        \
87 	        (vvp)->tv_nsec -= NSEC_PER_SEC;                         \
88 	}                                                               \
89 } while (0)
90 
91 #define net_timersub(tvp, uvp, vvp) do {                                \
92 	(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;                  \
93 	(vvp)->tv_nsec = (tvp)->tv_nsec - (uvp)->tv_nsec;               \
94 	if ((vvp)->tv_nsec < 0) {                                       \
95 	        (vvp)->tv_sec--;                                        \
96 	        (vvp)->tv_nsec += NSEC_PER_SEC;                         \
97 	}                                                               \
98 } while (0)
99 
100 #define net_timerusec(tvp, nsp) do {                                    \
101 	*(nsp) = (tvp)->tv_nsec / NSEC_PER_USEC;                        \
102 	if ((tvp)->tv_sec > 0)                                          \
103 	        *(nsp) += ((tvp)->tv_sec * USEC_PER_SEC);               \
104 } while (0)
105 
106 #define net_timernsec(tvp, nsp) do {                                    \
107 	*(nsp) = (tvp)->tv_nsec;                                        \
108 	if ((tvp)->tv_sec > 0)                                          \
109 	        *(nsp) += ((tvp)->tv_sec * NSEC_PER_SEC);               \
110 } while (0)
111 
112 #if defined(__x86_64__) || defined(__arm64__)
113 #define net_nsectimer(nsp, tvp) do {                                    \
114 	u_int64_t __nsp = *(nsp);                                       \
115 	net_timerclear(tvp);                                            \
116 	uint64_t __sec = __nsp / NSEC_PER_SEC;                          \
117 	(tvp)->tv_sec = (__darwin_time_t)__sec;                         \
118 	(tvp)->tv_nsec = (long)(__nsp - __sec * NSEC_PER_SEC);          \
119 } while (0)
120 #else /* 32 bit */
121 /*
122  * NSEC needs to be < 2^31*10^9 to be representable in a struct timespec
123  * because __darwin_time_t is 32 bit on 32-bit platforms. This bound
124  * is < 2^61. We get a first approximation to convert into seconds using
125  * the following values.
126  * a = floor(NSEC / 2^29)
127  * inv = floor(2^61 / 10^9)
128  *
129  * The approximation of seconds is correct or too low by 1 unit.
130  * So we fix it by computing the remainder.
131  */
132 #define net_nsectimer(nsp, tvp) do {                                    \
133 	u_int64_t __nsp = *(nsp);                                       \
134 	net_timerclear(tvp);                                            \
135 	uint32_t __a = (uint32_t)(__nsp >> 29);                         \
136 	const uint32_t __inv = 0x89705F41;                              \
137 	uint32_t __sec = (uint32_t)(((uint64_t)__a * __inv) >> 32);     \
138 	uint32_t __rem = (uint32_t)(__nsp - __sec * NSEC_PER_SEC);      \
139 	__sec += ((__rem >= NSEC_PER_SEC) ? 1 : 0);                     \
140 	(tvp)->tv_sec = (__darwin_time_t)__sec;                         \
141 	(tvp)->tv_nsec =                                                \
142 	    (long)((__rem >= NSEC_PER_SEC) ? (__rem - NSEC_PER_SEC) : __rem);   \
143 } while(0)
144 #endif /* 32 bit */
145 
146 struct ifnet;
147 struct mbuf;
148 struct ether_header;
149 struct sockaddr_dl;
150 struct iff_filter;
151 
152 #define DLIL_THREADNAME_LEN     32
153 
154 /*
155  * DLIL threading info
156  */
157 struct dlil_threading_info {
158 	decl_lck_mtx_data(, dlth_lock);
159 	class_queue_t   dlth_pkts;      /* queue of pkts */
160 	struct ifnet    *dlth_ifp;      /* pointer to interface */
161 	struct ifnet_stat_increment_param dlth_stats; /* incremental stats */
162 	uint32_t       dlth_flags;      /* thread flags (see below) */
163 	uint32_t       dlth_wtot;       /* # of wakeup requests */
164 
165 	/* strategy (sync or async) */
166 	errno_t (*dlth_strategy)(struct dlil_threading_info *,
167 	    struct ifnet *, struct mbuf *, struct mbuf *,
168 	    const struct ifnet_stat_increment_param *, boolean_t,
169 	    struct thread *);
170 
171 	/*
172 	 * Thread affinity (workloop and DLIL threads).
173 	 */
174 	boolean_t       dlth_affinity;          /* affinity set is available */
175 	uint32_t        dlth_affinity_tag;      /* affinity tag */
176 	struct thread   *dlth_thread;           /* DLIL worker thread */
177 	struct thread   *dlth_driver_thread;    /* driver/workloop thread */
178 	struct thread   *dlth_poller_thread;    /* poll thread */
179 
180 	lck_grp_t       *dlth_lock_grp; /* lock group (for lock stats) */
181 	char            dlth_name[DLIL_THREADNAME_LEN]; /* name storage */
182 
183 	/* Accounting for trimming of input queues that exceed their limits */
184 	uint32_t        dlth_trim_cnt;          /* # of trim events */
185 	uint32_t        dlth_trim_pkts_dropped; /* # of packets dropped
186 	                                         * when trimming */
187 #if IFNET_INPUT_SANITY_CHK
188 	/*
189 	 * For debugging.
190 	 */
191 	uint64_t        dlth_pkts_cnt;          /* total # of packets */
192 #endif
193 };
194 
195 /*
196  * DLIL input thread info (for main/loopback input thread)
197  */
198 struct dlil_main_threading_info {
199 	struct dlil_threading_info      inp;
200 	class_queue_t                   lo_rcvq_pkts; /* queue of lo0 pkts */
201 };
202 
203 /*
204  * Valid values for dlth_flags.
205  *
206  * The following are shared with kpi_protocol.c so that it may wakeup
207  * the input thread to run through packets queued for protocol input.
208  */
209 #define DLIL_INPUT_RUNNING              0x80000000
210 #define DLIL_INPUT_WAITING              0x40000000
211 #define DLIL_PROTO_REGISTER             0x20000000
212 #define DLIL_PROTO_WAITING              0x10000000
213 #define DLIL_INPUT_TERMINATE            0x08000000
214 #define DLIL_INPUT_TERMINATE_COMPLETE   0x04000000
215 #define DLIL_INPUT_EMBRYONIC            0x00000001
216 
217 /*
218  * Flags for dlil_attach_filter()
219  */
220 #define DLIL_IFF_TSO            0x01    /* Interface filter supports TSO */
221 #define DLIL_IFF_INTERNAL       0x02    /* Apple internal -- do not count towards stats */
222 #define DLIL_IFF_BRIDGE         0x04    /* bridge interface filter */
223 
224 extern int dlil_verbose;
225 extern uint32_t hwcksum_dbg;
226 extern uint32_t hwcksum_tx;
227 extern uint32_t hwcksum_rx;
228 extern struct dlil_threading_info *dlil_main_input_thread;
229 extern unsigned int net_rxpoll;
230 extern uint32_t if_rxpoll;
231 extern uint32_t if_rxpoll_decay;
232 extern uint32_t if_rxpoll_interval_pkts;
233 extern uint32_t if_rcvq_maxlen;
234 
235 extern void dlil_init(void);
236 
237 extern errno_t ifp_if_ioctl(struct ifnet *, unsigned long, void *);
238 extern errno_t ifp_if_output(struct ifnet *, struct mbuf *);
239 extern void ifp_if_start(struct ifnet *);
240 
241 extern errno_t dlil_set_bpf_tap(ifnet_t, bpf_tap_mode, bpf_packet_func);
242 
243 /*
244  * Send arp internal bypasses the check for IPv4LL.
245  */
246 extern errno_t dlil_send_arp_internal(ifnet_t, u_int16_t,
247     const struct sockaddr_dl *, const struct sockaddr *,
248     const struct sockaddr_dl *, const struct sockaddr *);
249 
250 /*
251  * The following constants are used with the net_thread_mark_apply and
252  * net_thread_is_unmarked functions to control the bits in the uu_network_marks
253  * field of the uthread structure.
254  */
255 #define NET_THREAD_HELD_PF      0x1     /* thread is holding PF lock */
256 #define NET_THREAD_HELD_DOMAIN  0x2     /* thread is holding domain_proto_mtx */
257 #define NET_THREAD_CKREQ_LLADDR 0x4     /* thread reqs MACF check for LLADDR */
258 #if SKYWALK
259 #define NET_THREAD_CHANNEL_SYNC 0x10000 /* thread is doing channel sync */
260 #define NET_THREAD_CACHE_UPDATE 0x20000 /* thread is doing cache update */
261 #define NET_THREAD_REGION_UPDATE 0x40000 /* thread is doing region update */
262 #define NET_THREAD_RX_NOTIFY    0x80000 /* thread is doing RX notify */
263 #define NET_THREAD_TX_NOTIFY    0x100000 /* thread is doing TX notify */
264 #define NET_THREAD_AYSYNC_TX    0x200000 /* require use of starter thread */
265 #define NET_THREAD_SYNC_RX      0x400000 /* request synchronous Rx handler */
266 #endif /* SKYWALK */
267 
268 /*
269  * net_thread_marks_t is a pointer to a phantom structure type used for
270  * manipulating the uthread:uu_network_marks field.  As an example...
271  *
272  *   static const u_int32_t bits = NET_THREAD_CKREQ_LLADDR;
273  *   struct uthread *uth = current_uthread();
274  *
275  *   net_thread_marks_t marks = net_thread_marks_push(bits);
276  *   VERIFY((uth->uu_network_marks & NET_THREAD_CKREQ_LLADDR) != 0);
277  *   net_thread_marks_pop(marks);
278  *
279  * The net_thread_marks_push() function returns an encoding of the bits
280  * that were changed from zero to one in the uu_network_marks field. When
281  * the net_thread_marks_pop() function later processes that value, it
282  * resets the bits to their previous value.
283  *
284  * The net_thread_unmarks_push() and net_thread_unmarks_pop() functions
285  * are similar to net_thread_marks_push() and net_thread_marks_pop() except
286  * they clear the marks bits in the guarded section rather than set them.
287  *
288  * The net_thread_is_marked() and net_thread_is_unmarked() functions return
289  * the subset of the bits that are currently set or cleared (respectively)
290  * in the uthread:uu_network_marks field.
291  *
292  * Finally, the value of the net_thread_marks_none constant is provided for
293  * comparing for equality with the value returned when no bits in the marks
294  * field are changed by the push.
295  *
296  * It is not significant that a value of type net_thread_marks_t may
297  * compare as equal to the NULL pointer.
298  */
299 struct net_thread_marks;
300 typedef const struct net_thread_marks *__single net_thread_marks_t;
301 
302 extern const net_thread_marks_t net_thread_marks_none;
303 
304 extern net_thread_marks_t net_thread_marks_push(u_int32_t);
305 extern net_thread_marks_t net_thread_unmarks_push(u_int32_t);
306 extern void net_thread_marks_pop(net_thread_marks_t);
307 extern void net_thread_unmarks_pop(net_thread_marks_t);
308 extern u_int32_t net_thread_is_marked(u_int32_t);
309 extern u_int32_t net_thread_is_unmarked(u_int32_t);
310 
311 #define DLIL_OUTPUT_FLAGS_NONE                  0x0
312 #define DLIL_OUTPUT_FLAGS_RAW                   0x1
313 #define DLIL_OUTPUT_FLAGS_SKIP_IF_FILTERS       0x2
314 
315 extern int dlil_output(ifnet_t, protocol_family_t, mbuf_t, void *,
316     const struct sockaddr *, int, struct flowadv *);
317 
318 extern void dlil_input_packet_list(struct ifnet *, struct mbuf *);
319 extern void dlil_input_packet_list_extended(struct ifnet *, struct mbuf *,
320     u_int32_t, ifnet_model_t);
321 
322 extern errno_t dlil_resolve_multi(struct ifnet *,
323     const struct sockaddr *, struct sockaddr *, size_t);
324 
325 extern errno_t dlil_send_arp(ifnet_t, u_int16_t, const struct sockaddr_dl *,
326     const struct sockaddr *, const struct sockaddr_dl *,
327     const struct sockaddr *, u_int32_t);
328 
329 extern int dlil_attach_filter(ifnet_t, const struct iff_filter *,
330     interface_filter_t *, u_int32_t);
331 extern void dlil_detach_filter(interface_filter_t);
332 extern boolean_t dlil_has_ip_filter(void);
333 extern boolean_t dlil_has_if_filter(struct ifnet *);
334 
335 extern void dlil_proto_unplumb_all(ifnet_t);
336 
337 extern int dlil_post_msg(struct ifnet *, u_int32_t, u_int32_t,
338     struct net_event_data *, u_int32_t, boolean_t);
339 
340 extern void dlil_post_sifflags_msg(struct ifnet *);
341 
342 extern int dlil_post_complete_msg(struct ifnet *, struct kev_msg *);
343 
344 extern int dlil_alloc_local_stats(struct ifnet *);
345 
346 extern void ifnet_filter_update_tso(struct ifnet *, boolean_t filter_enable);
347 extern errno_t dlil_rxpoll_validate_params(struct ifnet_poll_params *);
348 extern void dlil_rxpoll_update_params(struct ifnet *,
349     struct ifnet_poll_params *);
350 extern void ifnet_poll(struct ifnet *);
351 extern errno_t ifnet_input_poll(struct ifnet *, struct mbuf *,
352     struct mbuf *, const struct ifnet_stat_increment_param *);
353 
354 #if SKYWALK
355 extern boolean_t ifnet_needs_fsw_transport_netagent(ifnet_t ifp);
356 extern boolean_t ifnet_needs_fsw_ip_netagent(ifnet_t ifp);
357 extern boolean_t ifnet_needs_netif_netagent(ifnet_t ifp);
358 extern boolean_t ifnet_needs_compat(ifnet_t ifp);
359 extern boolean_t ifnet_nx_noauto(ifnet_t ifp);
360 extern boolean_t ifnet_nx_noauto_flowswitch(ifnet_t ifp);
361 extern boolean_t ifnet_is_low_latency(ifnet_t ifp);
362 extern boolean_t ifnet_attach_flowswitch_nexus(ifnet_t ifp);
363 extern boolean_t ifnet_detach_flowswitch_nexus(ifnet_t ifp);
364 extern boolean_t ifnet_add_netagent(ifnet_t ifp);
365 extern boolean_t ifnet_remove_netagent(ifnet_t ifp);
366 extern void      ifnet_attach_native_flowswitch(ifnet_t ifp);
367 extern void      ifnet_start_ignore_delay(ifnet_t interface);
368 extern int       ifnet_set_flowswitch_rx_callback(ifnet_t ifp, ifnet_fsw_rx_cb_t cb, void *arg);
369 extern int       ifnet_get_flowswitch_rx_callback(ifnet_t ifp, ifnet_fsw_rx_cb_t *cbp, void **argp);
370 extern void      ifnet_release_flowswitch_rx_callback(ifnet_t ifp);
371 extern int       ifnet_set_delegate_parent(ifnet_t difp, ifnet_t parent);
372 extern int       ifnet_get_delegate_parent(ifnet_t difp, ifnet_t *parent);
373 extern void      ifnet_release_delegate_parent(ifnet_t difp);
374 extern void      ifnet_set_detach_notify_locked(ifnet_t ifp,
375     ifnet_detach_notify_cb_t cb, void *arg);
376 extern void      ifnet_get_detach_notify_locked(ifnet_t ifp,
377     ifnet_detach_notify_cb_t *cbp, void **argp);
378 extern void      ifnet_set_detach_notify(ifnet_t ifp,
379     ifnet_detach_notify_cb_t cb, void *arg);
380 extern void      ifnet_get_detach_notify(ifnet_t ifp,
381     ifnet_detach_notify_cb_t *cbp, void **argp);
382 
383 #endif /* SKYWALK */
384 
385 /*
386  * dlil_if_acquire is obsolete. Use ifnet_allocate.
387  */
388 extern int dlil_if_acquire(u_int32_t, const void *, size_t, const char *, struct ifnet **);
389 /*
390  * dlil_if_release is obsolete. The equivalent is called automatically when
391  * an interface is detached.
392  */
393 extern void dlil_if_release(struct ifnet *ifp);
394 
395 extern errno_t dlil_if_ref(struct ifnet *);
396 extern errno_t dlil_if_free(struct ifnet *);
397 
398 extern int dlil_node_present(struct ifnet *, struct sockaddr *, int32_t, int,
399     int, u_int8_t[48]);
400 extern void dlil_node_absent(struct ifnet *, struct sockaddr *);
401 extern int dlil_node_present_v2(struct ifnet *, struct sockaddr *, struct sockaddr_dl *, int32_t, int,
402     int, u_int8_t[48]);
403 
404 extern const void *dlil_ifaddr_bytes(const struct sockaddr_dl *, size_t *,
405     kauth_cred_t *);
406 
407 static inline const void *__header_indexable
408 __attribute__((always_inline))
dlil_ifaddr_bytes_indexable(const struct sockaddr_dl * sdl,size_t * sizep,kauth_cred_t * cred)409 dlil_ifaddr_bytes_indexable(const struct sockaddr_dl * sdl, size_t *sizep, kauth_cred_t *cred)
410 {
411 	const void *__unsafe_indexable raw_bytes = dlil_ifaddr_bytes(sdl, sizep, cred);
412 	return __unsafe_forge_bidi_indexable(const void*, raw_bytes, *sizep);
413 }
414 
415 extern void dlil_report_issues(struct ifnet *, u_int8_t[DLIL_MODIDLEN],
416     u_int8_t[DLIL_MODARGLEN]);
417 
418 #define PROTO_HASH_SLOTS       4
419 
420 extern int proto_hash_value(u_int32_t);
421 
422 extern const char *dlil_kev_dl_code_str(u_int32_t);
423 
424 extern errno_t dlil_rxpoll_set_params(struct ifnet *,
425     struct ifnet_poll_params *, boolean_t);
426 extern errno_t dlil_rxpoll_get_params(struct ifnet *,
427     struct ifnet_poll_params *);
428 
429 extern errno_t dlil_output_handler(struct ifnet *, struct mbuf *);
430 extern errno_t dlil_input_handler(struct ifnet *, struct mbuf *,
431     struct mbuf *, const struct ifnet_stat_increment_param *,
432     boolean_t, struct thread *);
433 extern void dlil_ifclassq_setup(struct ifnet *, struct ifclassq *);
434 
435 #if SKYWALK
436 extern errno_t dlil_set_input_handler(struct ifnet *ifp, dlil_input_func fn);
437 extern errno_t dlil_set_output_handler(struct ifnet *ifp, dlil_output_func fn);
438 extern void dlil_reset_input_handler(struct ifnet *ifp);
439 extern void dlil_reset_output_handler(struct ifnet *ifp);
440 #endif /* SKYWALK */
441 
442 /*
443  * This is mostly called from the context of the DLIL input thread;
444  * because of that there is no need for atomic operations.
445  */
446 __attribute__((always_inline))
447 static inline void
ifp_inc_traffic_class_in(struct ifnet * ifp,struct mbuf * m)448 ifp_inc_traffic_class_in(struct ifnet *ifp, struct mbuf *m)
449 {
450 	if (!(m->m_flags & M_PKTHDR)) {
451 		return;
452 	}
453 
454 	switch (m_get_traffic_class(m)) {
455 	case MBUF_TC_BE:
456 		ifp->if_tc.ifi_ibepackets++;
457 		ifp->if_tc.ifi_ibebytes += (u_int64_t)m->m_pkthdr.len;
458 		break;
459 	case MBUF_TC_BK:
460 		ifp->if_tc.ifi_ibkpackets++;
461 		ifp->if_tc.ifi_ibkbytes += (u_int64_t)m->m_pkthdr.len;
462 		break;
463 	case MBUF_TC_VI:
464 		ifp->if_tc.ifi_ivipackets++;
465 		ifp->if_tc.ifi_ivibytes += (u_int64_t)m->m_pkthdr.len;
466 		break;
467 	case MBUF_TC_VO:
468 		ifp->if_tc.ifi_ivopackets++;
469 		ifp->if_tc.ifi_ivobytes += (u_int64_t)m->m_pkthdr.len;
470 		break;
471 	default:
472 		break;
473 	}
474 
475 	if (mbuf_is_traffic_class_privileged(m)) {
476 		ifp->if_tc.ifi_ipvpackets++;
477 		ifp->if_tc.ifi_ipvbytes += (u_int64_t)m->m_pkthdr.len;
478 	}
479 }
480 
481 /*
482  * This is called from DLIL output, hence multiple threads could end
483  * up modifying the statistics.  We trade off acccuracy for performance
484  * by not using atomic operations here.
485  */
486 __attribute__((always_inline))
487 static inline void
ifp_inc_traffic_class_out(struct ifnet * ifp,struct mbuf * m)488 ifp_inc_traffic_class_out(struct ifnet *ifp, struct mbuf *m)
489 {
490 	if (!(m->m_flags & M_PKTHDR)) {
491 		return;
492 	}
493 
494 	switch (m_get_traffic_class(m)) {
495 	case MBUF_TC_BE:
496 		ifp->if_tc.ifi_obepackets++;
497 		ifp->if_tc.ifi_obebytes += (u_int64_t)m->m_pkthdr.len;
498 		break;
499 	case MBUF_TC_BK:
500 		ifp->if_tc.ifi_obkpackets++;
501 		ifp->if_tc.ifi_obkbytes += (u_int64_t)m->m_pkthdr.len;
502 		break;
503 	case MBUF_TC_VI:
504 		ifp->if_tc.ifi_ovipackets++;
505 		ifp->if_tc.ifi_ovibytes += (u_int64_t)m->m_pkthdr.len;
506 		break;
507 	case MBUF_TC_VO:
508 		ifp->if_tc.ifi_ovopackets++;
509 		ifp->if_tc.ifi_ovobytes += (u_int64_t)m->m_pkthdr.len;
510 		break;
511 	default:
512 		break;
513 	}
514 
515 	if (mbuf_is_traffic_class_privileged(m)) {
516 		ifp->if_tc.ifi_opvpackets++;
517 		ifp->if_tc.ifi_opvbytes += (u_int64_t)m->m_pkthdr.len;
518 	}
519 }
520 
521 extern void ifnet_ioctl_async(struct ifnet *, u_long);
522 
523 extern int bridge_enable_early_input;
524 extern mbuf_t bridge_early_input(struct ifnet *ifp, mbuf_t m, u_int32_t cnt);
525 
526 #define DLIL_MAX_FRAME_TYPE_SIZE 4 /* LONGWORDS */
527 #define DLIL_MAX_FRAME_TYPE_BUFFER_SIZE (DLIL_MAX_FRAME_TYPE_SIZE * 4)
528 
529 /*
530  * Helper function to safely expand the frame type
531  * parameter to indexable pointer.
532  */
533 static inline uint8_t * __header_indexable
__dlil_frame_type_buffer(char * frame_type)534 __dlil_frame_type_buffer(char *frame_type)
535 {
536 	return __unsafe_forge_bidi_indexable(uint8_t *,
537 	           frame_type, DLIL_MAX_FRAME_TYPE_BUFFER_SIZE);
538 }
539 
540 static inline const uint8_t * __header_indexable
__dlil_frame_type_cbuffer(const char * frame_type)541 __dlil_frame_type_cbuffer(const char *frame_type)
542 {
543 	return __unsafe_forge_bidi_indexable(const uint8_t *,
544 	           frame_type, DLIL_MAX_FRAME_TYPE_BUFFER_SIZE);
545 }
546 
547 #define dlil_frame_type(x) (_Generic((x),           \
548 	char       * : __dlil_frame_type_buffer,        \
549 	const char * : __dlil_frame_type_cbuffer)((x)))
550 
551 #define DLIL_MAX_LINKADDR        4 /* LONGWORDS */
552 #define DLIL_MAX_LINKADDR_BUFFER_SIZE (DLIL_MAX_LINKADDR * 4)
553 
554 /*
555  * Helper function to safely expand the link address
556  * parameter to indexable pointer.
557  */
558 static inline uint8_t * __header_indexable
__dlil_link_addr_buffer(char * link_addr)559 __dlil_link_addr_buffer(char *link_addr)
560 {
561 	return __unsafe_forge_bidi_indexable(uint8_t *,
562 	           link_addr, DLIL_MAX_LINKADDR_BUFFER_SIZE);
563 }
564 
565 static inline const uint8_t * __header_indexable
__dlil_link_addr_cbuffer(const char * link_addr)566 __dlil_link_addr_cbuffer(const char *link_addr)
567 {
568 	return __unsafe_forge_bidi_indexable(const uint8_t *,
569 	           link_addr, DLIL_MAX_LINKADDR_BUFFER_SIZE);
570 }
571 
572 #define dlil_link_addr(x) (_Generic((x),           \
573 	char       * : __dlil_link_addr_buffer,        \
574 	const char * : __dlil_link_addr_cbuffer)((x)))
575 
576 #endif /* BSD_KERNEL_PRIVATE */
577 #endif /* KERNEL_PRIVATE */
578 #endif /* KERNEL */
579 #endif /* DLIL_H */
580