1 /*
2  * Copyright (c) 2016-2021 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 
29 #include <skywalk/os_skywalk_private.h>
30 #include <skywalk/nexus/flowswitch/flow/flow_var.h>
31 
32 #include <dev/random/randomdev.h>
33 
34 #define SK_FO_ZONE_MAX                  256
35 #define SK_FO_ZONE_NAME                 "flow.owner"
36 
37 unsigned int sk_fo_size;                /* size of zone element */
38 struct skmem_cache *sk_fo_cache;        /* cache for flow_owner */
39 
40 #define SK_FE_ZONE_NAME                 "flow.entry"
41 
42 unsigned int sk_fe_size;                /* size of zone element */
43 struct skmem_cache *sk_fe_cache;        /* cache for flow_entry */
44 
45 #define SK_FAB_ZONE_NAME                "flow.adv.bmap"
46 
47 unsigned int sk_fab_size;               /* size of zone element */
48 struct skmem_cache *sk_fab_cache;       /* cache for flow advisory bitmap */
49 
50 static int __flow_inited = 0;
51 uint32_t flow_seed;
52 
53 #define SKMEM_TAG_FLOW_DEMUX "com.apple.skywalk.fsw.flow_demux"
54 SKMEM_TAG_DEFINE(skmem_tag_flow_demux, SKMEM_TAG_FLOW_DEMUX);
55 
56 int
flow_init(void)57 flow_init(void)
58 {
59 	SK_LOCK_ASSERT_HELD();
60 	ASSERT(!__flow_inited);
61 
62 	do {
63 		read_random(&flow_seed, sizeof(flow_seed));
64 	} while (flow_seed == 0);
65 
66 	sk_fo_size = sizeof(struct flow_owner);
67 	if (sk_fo_cache == NULL) {
68 		sk_fo_cache = skmem_cache_create(SK_FO_ZONE_NAME, sk_fo_size,
69 		    sizeof(uint64_t), NULL, NULL, NULL, NULL, NULL, 0);
70 		if (sk_fo_cache == NULL) {
71 			panic("%s: skmem_cache create failed (%s)", __func__,
72 			    SK_FO_ZONE_NAME);
73 			/* NOTREACHED */
74 			__builtin_unreachable();
75 		}
76 	}
77 
78 	sk_fe_size = sizeof(struct flow_entry);
79 	if (sk_fe_cache == NULL) {
80 		/* request for 16-bytes alignment (due to fe_key) */
81 		sk_fe_cache = skmem_cache_create(SK_FE_ZONE_NAME, sk_fe_size,
82 		    16, NULL, NULL, NULL, NULL, NULL, 0);
83 		if (sk_fe_cache == NULL) {
84 			panic("%s: skmem_cache create failed (%s)", __func__,
85 			    SK_FE_ZONE_NAME);
86 			/* NOTREACHED */
87 			__builtin_unreachable();
88 		}
89 	}
90 
91 	/* these are initialized in skywalk_init() */
92 	VERIFY(sk_max_flows > 0 && sk_max_flows <= NX_FLOWADV_MAX);
93 	VERIFY(sk_fadv_nchunks != 0);
94 	_CASSERT(sizeof(*((struct flow_owner *)0)->fo_flowadv_bmap) ==
95 	    sizeof(bitmap_t));
96 
97 	sk_fab_size = (sk_fadv_nchunks * sizeof(bitmap_t));
98 	if (sk_fab_cache == NULL) {
99 		sk_fab_cache = skmem_cache_create(SK_FAB_ZONE_NAME, sk_fab_size,
100 		    sizeof(uint64_t), NULL, NULL, NULL, NULL, NULL, 0);
101 		if (sk_fab_cache == NULL) {
102 			panic("%s: skmem_cache create failed (%s)", __func__,
103 			    SK_FAB_ZONE_NAME);
104 			/* NOTREACHED */
105 			__builtin_unreachable();
106 		}
107 	}
108 
109 	flow_route_init();
110 	flow_stats_init();
111 
112 	__flow_inited = 1;
113 
114 	return 0;
115 }
116 
117 void
flow_fini(void)118 flow_fini(void)
119 {
120 	if (__flow_inited) {
121 		flow_stats_fini();
122 
123 		flow_route_fini();
124 
125 		if (sk_fo_cache != NULL) {
126 			skmem_cache_destroy(sk_fo_cache);
127 			sk_fo_cache = NULL;
128 		}
129 		if (sk_fe_cache != NULL) {
130 			skmem_cache_destroy(sk_fe_cache);
131 			sk_fe_cache = NULL;
132 		}
133 		if (sk_fab_cache != NULL) {
134 			skmem_cache_destroy(sk_fab_cache);
135 			sk_fab_cache = NULL;
136 		}
137 		__flow_inited = 0;
138 	}
139 }
140