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