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