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