xref: /xnu-8792.61.2/bsd/net/flowadv.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions /*
2*42e22086SApple OSS Distributions  * Copyright (c) 2012-2021 Apple Inc. All rights reserved.
3*42e22086SApple OSS Distributions  *
4*42e22086SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*42e22086SApple OSS Distributions  *
6*42e22086SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*42e22086SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*42e22086SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*42e22086SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*42e22086SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*42e22086SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*42e22086SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*42e22086SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*42e22086SApple OSS Distributions  *
15*42e22086SApple OSS Distributions  * Please obtain a copy of the License at
16*42e22086SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*42e22086SApple OSS Distributions  *
18*42e22086SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*42e22086SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*42e22086SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*42e22086SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*42e22086SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*42e22086SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*42e22086SApple OSS Distributions  * limitations under the License.
25*42e22086SApple OSS Distributions  *
26*42e22086SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*42e22086SApple OSS Distributions  */
28*42e22086SApple OSS Distributions 
29*42e22086SApple OSS Distributions /*
30*42e22086SApple OSS Distributions  * Flow Control and Feedback Advisory
31*42e22086SApple OSS Distributions  *
32*42e22086SApple OSS Distributions  * Each mbuf that is being sent out through an interface is tagged with a
33*42e22086SApple OSS Distributions  * unique 32-bit ID which will help to identify all the packets that belong
34*42e22086SApple OSS Distributions  * to a particular flow at the interface layer.  Packets carrying such ID
35*42e22086SApple OSS Distributions  * would need to be marked with PKTF_FLOW_ID.  Normally, this ID is computed
36*42e22086SApple OSS Distributions  * by the module that generates the flow.  There are 3 kinds of flow sources
37*42e22086SApple OSS Distributions  * that are currently recognized:
38*42e22086SApple OSS Distributions  *
39*42e22086SApple OSS Distributions  *	a. INPCB (INET/INET6 Protocol Control Block).  When a socket is
40*42e22086SApple OSS Distributions  *	   connected, the flow hash for the socket is computed and stored in
41*42e22086SApple OSS Distributions  *	   the PCB.  Further transmissions on the socket will cause the hash
42*42e22086SApple OSS Distributions  *	   value to be carried within the mbuf as the flow ID.
43*42e22086SApple OSS Distributions  *
44*42e22086SApple OSS Distributions  *	b. Interface.  When an interface is attached, the flow hash for the
45*42e22086SApple OSS Distributions  *	   interface is computed and stored in the ifnet.  This value is
46*42e22086SApple OSS Distributions  *	   normally ignored for most network drivers, except for those that
47*42e22086SApple OSS Distributions  *	   reside atop another driver, e.g. a virtual interface performing
48*42e22086SApple OSS Distributions  *	   encapsulation/encryption on the original packet and sending the
49*42e22086SApple OSS Distributions  *	   newly-generated packet to another interface.  Such interface needs
50*42e22086SApple OSS Distributions  *	   to associate all generated packets with the interface flow hash
51*42e22086SApple OSS Distributions  *	   value as the flow ID.
52*42e22086SApple OSS Distributions  *
53*42e22086SApple OSS Distributions  *	c. PF (Packet Filter).  When a packet goes through PF and it is not
54*42e22086SApple OSS Distributions  *	   already associated with a flow ID, PF will compute a flow hash and
55*42e22086SApple OSS Distributions  *	   store it in the packet as flow ID.  When the packet is associated
56*42e22086SApple OSS Distributions  *	   with a PF state, the state record will have the flow ID stored
57*42e22086SApple OSS Distributions  *	   within, in order to avoid recalculating the flow hash.  Although PF
58*42e22086SApple OSS Distributions  *	   is capable of generating flow IDs, it does not participate in flow
59*42e22086SApple OSS Distributions  *	   advisory, and therefore packets whose IDs are computed by PF will
60*42e22086SApple OSS Distributions  *	   not have their PKTF_FLOW_ADV packet flag set.
61*42e22086SApple OSS Distributions  *
62*42e22086SApple OSS Distributions  * Activation of flow advisory mechanism is done by setting the PKTF_FLOW_ADV
63*42e22086SApple OSS Distributions  * packet flag; because a flow ID is required, the mechanism will not take
64*42e22086SApple OSS Distributions  * place unless PKTF_FLOW_ID is set as well.  The packet must also carry one
65*42e22086SApple OSS Distributions  * of the flow source types FLOWSRC_{INPCB,IFNET} in order to identify where
66*42e22086SApple OSS Distributions  * the flow advisory notification should be delivered to.  As noted above,
67*42e22086SApple OSS Distributions  * FLOWSRC_PF does not participate in this mechanism.
68*42e22086SApple OSS Distributions  *
69*42e22086SApple OSS Distributions  * The classq module configured on the interface is responsible for exerting
70*42e22086SApple OSS Distributions  * flow control to the upper layers.  This occurs when the number of packets
71*42e22086SApple OSS Distributions  * queued for a flow reaches a limit.  The module generating the flow will
72*42e22086SApple OSS Distributions  * cease transmission until further flow advisory notice, and the flow will
73*42e22086SApple OSS Distributions  * be inserted into the classq's flow control list.
74*42e22086SApple OSS Distributions  *
75*42e22086SApple OSS Distributions  * When packets are dequeued from the classq and the number of packets for
76*42e22086SApple OSS Distributions  * a flow goes below a limit, the classq will transfer its flow control list
77*42e22086SApple OSS Distributions  * to the global fadv_list.  This will then trigger the flow advisory thread
78*42e22086SApple OSS Distributions  * to run, which will cause the flow source modules to be notified that data
79*42e22086SApple OSS Distributions  * can now be generated for those previously flow-controlled flows.
80*42e22086SApple OSS Distributions  */
81*42e22086SApple OSS Distributions 
82*42e22086SApple OSS Distributions #include <sys/param.h>
83*42e22086SApple OSS Distributions #include <sys/systm.h>
84*42e22086SApple OSS Distributions #include <sys/kernel.h>
85*42e22086SApple OSS Distributions #include <sys/mcache.h>
86*42e22086SApple OSS Distributions #include <sys/mbuf.h>
87*42e22086SApple OSS Distributions #include <sys/proc_internal.h>
88*42e22086SApple OSS Distributions #include <sys/socketvar.h>
89*42e22086SApple OSS Distributions 
90*42e22086SApple OSS Distributions #include <kern/assert.h>
91*42e22086SApple OSS Distributions #include <kern/thread.h>
92*42e22086SApple OSS Distributions #include <kern/locks.h>
93*42e22086SApple OSS Distributions #include <kern/zalloc.h>
94*42e22086SApple OSS Distributions 
95*42e22086SApple OSS Distributions #include <netinet/in_pcb.h>
96*42e22086SApple OSS Distributions #include <net/flowadv.h>
97*42e22086SApple OSS Distributions #if SKYWALK
98*42e22086SApple OSS Distributions #include <skywalk/os_channel.h>
99*42e22086SApple OSS Distributions #endif /* SKYWALK */
100*42e22086SApple OSS Distributions 
101*42e22086SApple OSS Distributions /* Lock group and attribute for fadv_lock */
102*42e22086SApple OSS Distributions static LCK_GRP_DECLARE(fadv_lock_grp, "fadv_lock");
103*42e22086SApple OSS Distributions static LCK_MTX_DECLARE(fadv_lock, &fadv_lock_grp);
104*42e22086SApple OSS Distributions 
105*42e22086SApple OSS Distributions /* protected by fadv_lock */
106*42e22086SApple OSS Distributions static STAILQ_HEAD(fadv_head, flowadv_fcentry) fadv_list =
107*42e22086SApple OSS Distributions     STAILQ_HEAD_INITIALIZER(fadv_list);
108*42e22086SApple OSS Distributions static thread_t fadv_thread = THREAD_NULL;
109*42e22086SApple OSS Distributions static uint32_t fadv_active;
110*42e22086SApple OSS Distributions 
111*42e22086SApple OSS Distributions static const unsigned int fadv_size = sizeof(struct flowadv_fcentry);
112*42e22086SApple OSS Distributions static struct mcache *fadv_cache;               /* mcache for flowadv_fcentry */
113*42e22086SApple OSS Distributions 
114*42e22086SApple OSS Distributions #define FADV_CACHE_NAME  "flowadv"              /* cache name */
115*42e22086SApple OSS Distributions 
116*42e22086SApple OSS Distributions static int flowadv_thread_cont(int);
117*42e22086SApple OSS Distributions static void flowadv_thread_func(void *, wait_result_t);
118*42e22086SApple OSS Distributions 
119*42e22086SApple OSS Distributions void
flowadv_init(void)120*42e22086SApple OSS Distributions flowadv_init(void)
121*42e22086SApple OSS Distributions {
122*42e22086SApple OSS Distributions 	fadv_cache = mcache_create(FADV_CACHE_NAME, fadv_size,
123*42e22086SApple OSS Distributions 	    sizeof(uint64_t), 0, MCR_SLEEP);
124*42e22086SApple OSS Distributions 
125*42e22086SApple OSS Distributions 	if (kernel_thread_start(flowadv_thread_func, NULL, &fadv_thread) !=
126*42e22086SApple OSS Distributions 	    KERN_SUCCESS) {
127*42e22086SApple OSS Distributions 		panic("%s: couldn't create flow event advisory thread",
128*42e22086SApple OSS Distributions 		    __func__);
129*42e22086SApple OSS Distributions 		/* NOTREACHED */
130*42e22086SApple OSS Distributions 	}
131*42e22086SApple OSS Distributions 	thread_deallocate(fadv_thread);
132*42e22086SApple OSS Distributions }
133*42e22086SApple OSS Distributions 
134*42e22086SApple OSS Distributions struct flowadv_fcentry *
flowadv_alloc_entry(int how)135*42e22086SApple OSS Distributions flowadv_alloc_entry(int how)
136*42e22086SApple OSS Distributions {
137*42e22086SApple OSS Distributions 	struct flowadv_fcentry *fce;
138*42e22086SApple OSS Distributions 
139*42e22086SApple OSS Distributions 	if ((fce = mcache_alloc(fadv_cache, (how == M_WAITOK) ?
140*42e22086SApple OSS Distributions 	    MCR_SLEEP : MCR_NOSLEEP)) != NULL) {
141*42e22086SApple OSS Distributions 		bzero(fce, fadv_size);
142*42e22086SApple OSS Distributions 	}
143*42e22086SApple OSS Distributions 
144*42e22086SApple OSS Distributions 	return fce;
145*42e22086SApple OSS Distributions }
146*42e22086SApple OSS Distributions 
147*42e22086SApple OSS Distributions void
flowadv_free_entry(struct flowadv_fcentry * fce)148*42e22086SApple OSS Distributions flowadv_free_entry(struct flowadv_fcentry *fce)
149*42e22086SApple OSS Distributions {
150*42e22086SApple OSS Distributions 	mcache_free(fadv_cache, fce);
151*42e22086SApple OSS Distributions }
152*42e22086SApple OSS Distributions 
153*42e22086SApple OSS Distributions void
flowadv_add(struct flowadv_fclist * fcl)154*42e22086SApple OSS Distributions flowadv_add(struct flowadv_fclist *fcl)
155*42e22086SApple OSS Distributions {
156*42e22086SApple OSS Distributions 	if (STAILQ_EMPTY(fcl)) {
157*42e22086SApple OSS Distributions 		return;
158*42e22086SApple OSS Distributions 	}
159*42e22086SApple OSS Distributions 
160*42e22086SApple OSS Distributions 	lck_mtx_lock_spin(&fadv_lock);
161*42e22086SApple OSS Distributions 
162*42e22086SApple OSS Distributions 	STAILQ_CONCAT(&fadv_list, fcl);
163*42e22086SApple OSS Distributions 	VERIFY(!STAILQ_EMPTY(&fadv_list));
164*42e22086SApple OSS Distributions 
165*42e22086SApple OSS Distributions 	if (!fadv_active && fadv_thread != THREAD_NULL) {
166*42e22086SApple OSS Distributions 		wakeup_one((caddr_t)&fadv_list);
167*42e22086SApple OSS Distributions 	}
168*42e22086SApple OSS Distributions 
169*42e22086SApple OSS Distributions 	lck_mtx_unlock(&fadv_lock);
170*42e22086SApple OSS Distributions }
171*42e22086SApple OSS Distributions 
172*42e22086SApple OSS Distributions void
flowadv_add_entry(struct flowadv_fcentry * fce)173*42e22086SApple OSS Distributions flowadv_add_entry(struct flowadv_fcentry *fce)
174*42e22086SApple OSS Distributions {
175*42e22086SApple OSS Distributions 	lck_mtx_lock_spin(&fadv_lock);
176*42e22086SApple OSS Distributions 	STAILQ_INSERT_HEAD(&fadv_list, fce, fce_link);
177*42e22086SApple OSS Distributions 	VERIFY(!STAILQ_EMPTY(&fadv_list));
178*42e22086SApple OSS Distributions 
179*42e22086SApple OSS Distributions 	if (!fadv_active && fadv_thread != THREAD_NULL) {
180*42e22086SApple OSS Distributions 		wakeup_one((caddr_t)&fadv_list);
181*42e22086SApple OSS Distributions 	}
182*42e22086SApple OSS Distributions 
183*42e22086SApple OSS Distributions 	lck_mtx_unlock(&fadv_lock);
184*42e22086SApple OSS Distributions }
185*42e22086SApple OSS Distributions 
186*42e22086SApple OSS Distributions static int
flowadv_thread_cont(int err)187*42e22086SApple OSS Distributions flowadv_thread_cont(int err)
188*42e22086SApple OSS Distributions {
189*42e22086SApple OSS Distributions #pragma unused(err)
190*42e22086SApple OSS Distributions 	for (;;) {
191*42e22086SApple OSS Distributions 		LCK_MTX_ASSERT(&fadv_lock, LCK_MTX_ASSERT_OWNED);
192*42e22086SApple OSS Distributions 		while (STAILQ_EMPTY(&fadv_list)) {
193*42e22086SApple OSS Distributions 			VERIFY(!fadv_active);
194*42e22086SApple OSS Distributions 			(void) msleep0(&fadv_list, &fadv_lock, (PSOCK | PSPIN),
195*42e22086SApple OSS Distributions 			    "flowadv_cont", 0, flowadv_thread_cont);
196*42e22086SApple OSS Distributions 			/* NOTREACHED */
197*42e22086SApple OSS Distributions 		}
198*42e22086SApple OSS Distributions 
199*42e22086SApple OSS Distributions 		fadv_active = 1;
200*42e22086SApple OSS Distributions 		for (;;) {
201*42e22086SApple OSS Distributions 			struct flowadv_fcentry *fce;
202*42e22086SApple OSS Distributions 
203*42e22086SApple OSS Distributions 			VERIFY(!STAILQ_EMPTY(&fadv_list));
204*42e22086SApple OSS Distributions 			fce = STAILQ_FIRST(&fadv_list);
205*42e22086SApple OSS Distributions 			STAILQ_REMOVE(&fadv_list, fce,
206*42e22086SApple OSS Distributions 			    flowadv_fcentry, fce_link);
207*42e22086SApple OSS Distributions 			STAILQ_NEXT(fce, fce_link) = NULL;
208*42e22086SApple OSS Distributions 
209*42e22086SApple OSS Distributions 			lck_mtx_unlock(&fadv_lock);
210*42e22086SApple OSS Distributions 			switch (fce->fce_flowsrc_type) {
211*42e22086SApple OSS Distributions 			case FLOWSRC_INPCB:
212*42e22086SApple OSS Distributions 				inp_flowadv(fce->fce_flowid);
213*42e22086SApple OSS Distributions 				break;
214*42e22086SApple OSS Distributions 
215*42e22086SApple OSS Distributions 			case FLOWSRC_IFNET:
216*42e22086SApple OSS Distributions #if SKYWALK
217*42e22086SApple OSS Distributions 				/*
218*42e22086SApple OSS Distributions 				 * when using the flowID allocator, IPSec
219*42e22086SApple OSS Distributions 				 * driver uses the "pkt_flowid" field in mbuf
220*42e22086SApple OSS Distributions 				 * packet header for the globally unique flowID
221*42e22086SApple OSS Distributions 				 * and the "pkt_mpriv_srcid" field carries the
222*42e22086SApple OSS Distributions 				 * interface flow control id (if_flowhash).
223*42e22086SApple OSS Distributions 				 * For IPSec flows, it is the IPSec driver
224*42e22086SApple OSS Distributions 				 * network interface which is flow controlled,
225*42e22086SApple OSS Distributions 				 * instead of the IPSec SA flow.
226*42e22086SApple OSS Distributions 				 */
227*42e22086SApple OSS Distributions 				ifnet_flowadv(fce->fce_flowsrc_token);
228*42e22086SApple OSS Distributions #else /* !SKYWALK */
229*42e22086SApple OSS Distributions 				ifnet_flowadv(fce->fce_flowid);
230*42e22086SApple OSS Distributions #endif /* !SKYWALK */
231*42e22086SApple OSS Distributions 				break;
232*42e22086SApple OSS Distributions 
233*42e22086SApple OSS Distributions #if SKYWALK
234*42e22086SApple OSS Distributions 			case FLOWSRC_CHANNEL:
235*42e22086SApple OSS Distributions 				kern_channel_flowadv_clear(fce);
236*42e22086SApple OSS Distributions 				break;
237*42e22086SApple OSS Distributions #endif /* SKYWALK */
238*42e22086SApple OSS Distributions 
239*42e22086SApple OSS Distributions 			case FLOWSRC_PF:
240*42e22086SApple OSS Distributions 			default:
241*42e22086SApple OSS Distributions 				break;
242*42e22086SApple OSS Distributions 			}
243*42e22086SApple OSS Distributions 			flowadv_free_entry(fce);
244*42e22086SApple OSS Distributions 			lck_mtx_lock_spin(&fadv_lock);
245*42e22086SApple OSS Distributions 
246*42e22086SApple OSS Distributions 			/* if there's no pending request, we're done */
247*42e22086SApple OSS Distributions 			if (STAILQ_EMPTY(&fadv_list)) {
248*42e22086SApple OSS Distributions 				break;
249*42e22086SApple OSS Distributions 			}
250*42e22086SApple OSS Distributions 		}
251*42e22086SApple OSS Distributions 		fadv_active = 0;
252*42e22086SApple OSS Distributions 	}
253*42e22086SApple OSS Distributions }
254*42e22086SApple OSS Distributions 
255*42e22086SApple OSS Distributions __dead2
256*42e22086SApple OSS Distributions static void
flowadv_thread_func(void * v,wait_result_t w)257*42e22086SApple OSS Distributions flowadv_thread_func(void *v, wait_result_t w)
258*42e22086SApple OSS Distributions {
259*42e22086SApple OSS Distributions #pragma unused(v, w)
260*42e22086SApple OSS Distributions 	lck_mtx_lock(&fadv_lock);
261*42e22086SApple OSS Distributions 	(void) msleep0(&fadv_list, &fadv_lock, (PSOCK | PSPIN),
262*42e22086SApple OSS Distributions 	    "flowadv", 0, flowadv_thread_cont);
263*42e22086SApple OSS Distributions 	/*
264*42e22086SApple OSS Distributions 	 * msleep0() shouldn't have returned as PCATCH was not set;
265*42e22086SApple OSS Distributions 	 * therefore assert in this case.
266*42e22086SApple OSS Distributions 	 */
267*42e22086SApple OSS Distributions 	lck_mtx_unlock(&fadv_lock);
268*42e22086SApple OSS Distributions 	VERIFY(0);
269*42e22086SApple OSS Distributions }
270*42e22086SApple OSS Distributions 
271*42e22086SApple OSS Distributions void
flowadv_reap_caches(boolean_t purge)272*42e22086SApple OSS Distributions flowadv_reap_caches(boolean_t purge)
273*42e22086SApple OSS Distributions {
274*42e22086SApple OSS Distributions 	mcache_reap_now(fadv_cache, purge);
275*42e22086SApple OSS Distributions }
276