blob: f3ec1f90bf6f72cdbd0c7799b8f843d026f3fa4e [file] [log] [blame]
Florin Corascea194d2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Corascea194d2017-10-02 00:18:51 -07003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/session/session_table.h>
17#include <vnet/session/session.h>
Steven Luongc4b5d102024-07-30 13:44:01 -070018#include <vnet/session/session_rules_table.h>
Florin Corascea194d2017-10-02 00:18:51 -070019
20/**
21 * Pool of session tables
22 */
23static session_table_t *lookup_tables;
24
25session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -070026_get_session_tables (void)
27{
28 return lookup_tables;
29}
30
31session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -070032session_table_alloc (void)
33{
34 session_table_t *slt;
35 pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES);
Dave Barachb7b92992018-10-17 10:38:51 -040036 clib_memset (slt, 0, sizeof (*slt));
Florin Corascea194d2017-10-02 00:18:51 -070037 return slt;
38}
39
40u32
41session_table_index (session_table_t * slt)
42{
43 return (slt - lookup_tables);
44}
45
46session_table_t *
47session_table_get (u32 table_index)
48{
Florin Coras7ce3d132018-05-29 01:03:16 -070049 if (pool_is_free_index (lookup_tables, table_index))
Florin Corascea194d2017-10-02 00:18:51 -070050 return 0;
Florin Coras7ce3d132018-05-29 01:03:16 -070051 return pool_elt_at_index (lookup_tables, table_index);
Florin Corascea194d2017-10-02 00:18:51 -070052}
53
54#define foreach_hash_table_parameter \
55 _(v4,session,buckets,20000) \
56 _(v4,session,memory,(64<<20)) \
57 _(v6,session,buckets,20000) \
58 _(v6,session,memory,(64<<20)) \
59 _(v4,halfopen,buckets,20000) \
60 _(v4,halfopen,memory,(64<<20)) \
61 _(v6,halfopen,buckets,20000) \
62 _(v6,halfopen,memory,(64<<20))
63
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020064void
65session_table_free (session_table_t *slt, u8 fib_proto)
66{
67 u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020068
Steven Luongc4b5d102024-07-30 13:44:01 -070069 session_rules_table_free (slt, fib_proto);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020070
71 if (fib_proto == FIB_PROTOCOL_IP4 || all)
72 {
73 clib_bihash_free_16_8 (&slt->v4_session_hash);
74 clib_bihash_free_16_8 (&slt->v4_half_open_hash);
75 }
76 if (fib_proto == FIB_PROTOCOL_IP6 || all)
77 {
78 clib_bihash_free_48_8 (&slt->v6_session_hash);
79 clib_bihash_free_48_8 (&slt->v6_half_open_hash);
80 }
81
Steven Luonge0c4e6e2024-10-22 10:44:07 -070082 vec_free (slt->appns_index);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020083 pool_put (lookup_tables, slt);
84}
85
Florin Corascea194d2017-10-02 00:18:51 -070086/**
87 * Initialize session table hash tables
88 *
89 * If vpp configured with set of table parameters it uses them,
90 * otherwise it uses defaults above.
91 */
92void
Steven Luongc4b5d102024-07-30 13:44:01 -070093session_table_init (session_table_t *slt, u8 fib_proto)
Florin Corascea194d2017-10-02 00:18:51 -070094{
Florin Coras6c36f532017-11-03 18:32:34 -070095 u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0;
96
Florin Corascea194d2017-10-02 00:18:51 -070097#define _(af,table,parm,value) \
98 u32 configured_##af##_##table##_table_##parm = value;
99 foreach_hash_table_parameter;
100#undef _
101
102#define _(af,table,parm,value) \
Florin Coras31c99552019-03-01 13:00:58 -0800103 if (session_main.configured_##af##_##table##_table_##parm) \
Florin Corascea194d2017-10-02 00:18:51 -0700104 configured_##af##_##table##_table_##parm = \
Florin Coras31c99552019-03-01 13:00:58 -0800105 session_main.configured_##af##_##table##_table_##parm;
Florin Corascea194d2017-10-02 00:18:51 -0700106 foreach_hash_table_parameter;
107#undef _
108
Steven Luongc4b5d102024-07-30 13:44:01 -0700109 slt->srtg_handle = SESSION_SRTG_HANDLE_INVALID;
Florin Coras6c36f532017-11-03 18:32:34 -0700110 if (fib_proto == FIB_PROTOCOL_IP4 || all)
111 {
Florin Coras27eeb872019-09-03 13:06:11 -0700112 clib_bihash_init2_args_16_8_t _a, *a = &_a;
113
114 memset (a, 0, sizeof (*a));
115 a->h = &slt->v4_session_hash;
116 a->name = "v4 session table";
117 a->nbuckets = configured_v4_session_table_buckets;
118 a->memory_size = configured_v4_session_table_memory;
119 a->dont_add_to_all_bihash_list = 1;
120 a->instantiate_immediately = 1;
121 clib_bihash_init2_16_8 (a);
122
123 memset (a, 0, sizeof (*a));
124 a->h = &slt->v4_half_open_hash;
125 a->name = "v4 half-open table";
126 a->nbuckets = configured_v4_halfopen_table_buckets;
127 a->memory_size = configured_v4_halfopen_table_memory;
128 a->dont_add_to_all_bihash_list = 1;
129 a->instantiate_immediately = 1;
130 clib_bihash_init2_16_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700131 }
132 if (fib_proto == FIB_PROTOCOL_IP6 || all)
133 {
Florin Coras27eeb872019-09-03 13:06:11 -0700134 clib_bihash_init2_args_48_8_t _a, *a = &_a;
135
136 memset (a, 0, sizeof (*a));
137 a->h = &slt->v6_session_hash;
138 a->name = "v6 session table";
139 a->nbuckets = configured_v6_session_table_buckets;
140 a->memory_size = configured_v6_session_table_memory;
141 a->dont_add_to_all_bihash_list = 1;
142 a->instantiate_immediately = 1;
143 clib_bihash_init2_48_8 (a);
144
145 memset (a, 0, sizeof (*a));
146 a->h = &slt->v6_half_open_hash;
147 a->name = "v6 half-open table";
148 a->nbuckets = configured_v6_halfopen_table_buckets;
149 a->memory_size = configured_v6_halfopen_table_memory;
150 a->dont_add_to_all_bihash_list = 1;
151 a->instantiate_immediately = 1;
152 clib_bihash_init2_48_8 (a);
Florin Coras6c36f532017-11-03 18:32:34 -0700153 }
Florin Corascea194d2017-10-02 00:18:51 -0700154}
155
156typedef struct _ip4_session_table_walk_ctx_t
157{
158 ip4_session_table_walk_fn_t fn;
159 void *ctx;
160} ip4_session_table_walk_ctx_t;
161
Neale Rannsf50bac12019-12-06 05:53:17 +0000162static int
Florin Corascea194d2017-10-02 00:18:51 -0700163ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg)
164{
165 ip4_session_table_walk_ctx_t *ctx = arg;
166 ctx->fn (kvp, ctx->ctx);
Neale Rannsf50bac12019-12-06 05:53:17 +0000167 return (BIHASH_WALK_CONTINUE);
Florin Corascea194d2017-10-02 00:18:51 -0700168}
169
170void
171ip4_session_table_walk (clib_bihash_16_8_t * hash,
172 ip4_session_table_walk_fn_t fn, void *arg)
173{
174 ip4_session_table_walk_ctx_t ctx = {
175 .fn = fn,
176 .ctx = arg,
177 };
178 clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb,
179 &ctx);
180}
181
Florin Coras894d0a62023-11-17 16:35:04 -0800182u32
183session_table_memory_size (session_table_t *st)
184{
185 u64 total_size = 0;
186
187 if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash))
188 {
189 clib_bihash_alloc_chunk_16_8_t *c = st->v4_session_hash.chunks;
190 while (c)
191 {
192 total_size += c->size;
193 c = c->next;
194 }
195 c = st->v4_half_open_hash.chunks;
196 while (c)
197 {
198 total_size += c->size;
199 c = c->next;
200 }
201 }
202
203 if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash))
204 {
205 clib_bihash_alloc_chunk_48_8_t *c = st->v6_session_hash.chunks;
206 while (c)
207 {
208 total_size += c->size;
209 c = c->next;
210 }
211 c = st->v6_half_open_hash.chunks;
212 while (c)
213 {
214 total_size += c->size;
215 c = c->next;
216 }
217 }
218
219 return total_size;
220}
221
222u8 *
223format_session_table (u8 *s, va_list *args)
224{
225 session_table_t *st = va_arg (*args, session_table_t *);
Steven Luonge0c4e6e2024-10-22 10:44:07 -0700226 u32 appns_index, i;
Florin Coras894d0a62023-11-17 16:35:04 -0800227
Steven Luonge0c4e6e2024-10-22 10:44:07 -0700228 s = format (s, "appns index: ");
229 vec_foreach_index (i, st->appns_index)
230 {
231 appns_index = *vec_elt_at_index (st->appns_index, i);
232 if (i > 0)
233 s = format (s, ", ");
234 s = format (s, "%d", appns_index);
235 }
236 s = format (s, "\n");
Florin Coras894d0a62023-11-17 16:35:04 -0800237 if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash))
238 {
239 s = format (s, "%U", format_bihash_16_8, &st->v4_session_hash, 0);
240 s = format (s, "%U", format_bihash_16_8, &st->v4_half_open_hash, 0);
241 }
242
243 if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash))
244 {
245 s = format (s, "%U", format_bihash_48_8, &st->v6_session_hash, 0);
246 s = format (s, "%U", format_bihash_48_8, &st->v6_half_open_hash, 0);
247 }
248
249 return s;
250}
251
Florin Corascea194d2017-10-02 00:18:51 -0700252/*
253 * fd.io coding-style-patch-verification: ON
254 *
255 * Local Variables:
256 * eval: (c-set-style "gnu")
257 * End:
258 */