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