Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 3 | * 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 Luong | c4b5d10 | 2024-07-30 13:44:01 -0700 | [diff] [blame] | 18 | #include <vnet/session/session_rules_table.h> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * Pool of session tables |
| 22 | */ |
| 23 | static session_table_t *lookup_tables; |
| 24 | |
| 25 | session_table_t * |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 26 | _get_session_tables (void) |
| 27 | { |
| 28 | return lookup_tables; |
| 29 | } |
| 30 | |
| 31 | session_table_t * |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 32 | session_table_alloc (void) |
| 33 | { |
| 34 | session_table_t *slt; |
| 35 | pool_get_aligned (lookup_tables, slt, CLIB_CACHE_LINE_BYTES); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 36 | clib_memset (slt, 0, sizeof (*slt)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 37 | return slt; |
| 38 | } |
| 39 | |
| 40 | u32 |
| 41 | session_table_index (session_table_t * slt) |
| 42 | { |
| 43 | return (slt - lookup_tables); |
| 44 | } |
| 45 | |
| 46 | session_table_t * |
| 47 | session_table_get (u32 table_index) |
| 48 | { |
Florin Coras | 7ce3d13 | 2018-05-29 01:03:16 -0700 | [diff] [blame] | 49 | if (pool_is_free_index (lookup_tables, table_index)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 50 | return 0; |
Florin Coras | 7ce3d13 | 2018-05-29 01:03:16 -0700 | [diff] [blame] | 51 | return pool_elt_at_index (lookup_tables, table_index); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 52 | } |
| 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 Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 64 | void |
| 65 | session_table_free (session_table_t *slt, u8 fib_proto) |
| 66 | { |
| 67 | u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 68 | |
Steven Luong | c4b5d10 | 2024-07-30 13:44:01 -0700 | [diff] [blame] | 69 | session_rules_table_free (slt, fib_proto); |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 70 | |
| 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 | |
| 82 | pool_put (lookup_tables, slt); |
| 83 | } |
| 84 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Initialize session table hash tables |
| 87 | * |
| 88 | * If vpp configured with set of table parameters it uses them, |
| 89 | * otherwise it uses defaults above. |
| 90 | */ |
| 91 | void |
Steven Luong | c4b5d10 | 2024-07-30 13:44:01 -0700 | [diff] [blame] | 92 | session_table_init (session_table_t *slt, u8 fib_proto) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 93 | { |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 94 | u8 all = fib_proto > FIB_PROTOCOL_IP6 ? 1 : 0; |
| 95 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 96 | #define _(af,table,parm,value) \ |
| 97 | u32 configured_##af##_##table##_table_##parm = value; |
| 98 | foreach_hash_table_parameter; |
| 99 | #undef _ |
| 100 | |
| 101 | #define _(af,table,parm,value) \ |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 102 | if (session_main.configured_##af##_##table##_table_##parm) \ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 103 | configured_##af##_##table##_table_##parm = \ |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 104 | session_main.configured_##af##_##table##_table_##parm; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 105 | foreach_hash_table_parameter; |
| 106 | #undef _ |
| 107 | |
Steven Luong | c4b5d10 | 2024-07-30 13:44:01 -0700 | [diff] [blame] | 108 | slt->srtg_handle = SESSION_SRTG_HANDLE_INVALID; |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 109 | if (fib_proto == FIB_PROTOCOL_IP4 || all) |
| 110 | { |
Florin Coras | 27eeb87 | 2019-09-03 13:06:11 -0700 | [diff] [blame] | 111 | clib_bihash_init2_args_16_8_t _a, *a = &_a; |
| 112 | |
| 113 | memset (a, 0, sizeof (*a)); |
| 114 | a->h = &slt->v4_session_hash; |
| 115 | a->name = "v4 session table"; |
| 116 | a->nbuckets = configured_v4_session_table_buckets; |
| 117 | a->memory_size = configured_v4_session_table_memory; |
| 118 | a->dont_add_to_all_bihash_list = 1; |
| 119 | a->instantiate_immediately = 1; |
| 120 | clib_bihash_init2_16_8 (a); |
| 121 | |
| 122 | memset (a, 0, sizeof (*a)); |
| 123 | a->h = &slt->v4_half_open_hash; |
| 124 | a->name = "v4 half-open table"; |
| 125 | a->nbuckets = configured_v4_halfopen_table_buckets; |
| 126 | a->memory_size = configured_v4_halfopen_table_memory; |
| 127 | a->dont_add_to_all_bihash_list = 1; |
| 128 | a->instantiate_immediately = 1; |
| 129 | clib_bihash_init2_16_8 (a); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 130 | } |
| 131 | if (fib_proto == FIB_PROTOCOL_IP6 || all) |
| 132 | { |
Florin Coras | 27eeb87 | 2019-09-03 13:06:11 -0700 | [diff] [blame] | 133 | clib_bihash_init2_args_48_8_t _a, *a = &_a; |
| 134 | |
| 135 | memset (a, 0, sizeof (*a)); |
| 136 | a->h = &slt->v6_session_hash; |
| 137 | a->name = "v6 session table"; |
| 138 | a->nbuckets = configured_v6_session_table_buckets; |
| 139 | a->memory_size = configured_v6_session_table_memory; |
| 140 | a->dont_add_to_all_bihash_list = 1; |
| 141 | a->instantiate_immediately = 1; |
| 142 | clib_bihash_init2_48_8 (a); |
| 143 | |
| 144 | memset (a, 0, sizeof (*a)); |
| 145 | a->h = &slt->v6_half_open_hash; |
| 146 | a->name = "v6 half-open table"; |
| 147 | a->nbuckets = configured_v6_halfopen_table_buckets; |
| 148 | a->memory_size = configured_v6_halfopen_table_memory; |
| 149 | a->dont_add_to_all_bihash_list = 1; |
| 150 | a->instantiate_immediately = 1; |
| 151 | clib_bihash_init2_48_8 (a); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 152 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | typedef struct _ip4_session_table_walk_ctx_t |
| 156 | { |
| 157 | ip4_session_table_walk_fn_t fn; |
| 158 | void *ctx; |
| 159 | } ip4_session_table_walk_ctx_t; |
| 160 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 161 | static int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 162 | ip4_session_table_walk_cb (clib_bihash_kv_16_8_t * kvp, void *arg) |
| 163 | { |
| 164 | ip4_session_table_walk_ctx_t *ctx = arg; |
| 165 | ctx->fn (kvp, ctx->ctx); |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 166 | return (BIHASH_WALK_CONTINUE); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void |
| 170 | ip4_session_table_walk (clib_bihash_16_8_t * hash, |
| 171 | ip4_session_table_walk_fn_t fn, void *arg) |
| 172 | { |
| 173 | ip4_session_table_walk_ctx_t ctx = { |
| 174 | .fn = fn, |
| 175 | .ctx = arg, |
| 176 | }; |
| 177 | clib_bihash_foreach_key_value_pair_16_8 (hash, ip4_session_table_walk_cb, |
| 178 | &ctx); |
| 179 | } |
| 180 | |
Florin Coras | 894d0a6 | 2023-11-17 16:35:04 -0800 | [diff] [blame] | 181 | u32 |
| 182 | session_table_memory_size (session_table_t *st) |
| 183 | { |
| 184 | u64 total_size = 0; |
| 185 | |
| 186 | if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash)) |
| 187 | { |
| 188 | clib_bihash_alloc_chunk_16_8_t *c = st->v4_session_hash.chunks; |
| 189 | while (c) |
| 190 | { |
| 191 | total_size += c->size; |
| 192 | c = c->next; |
| 193 | } |
| 194 | c = st->v4_half_open_hash.chunks; |
| 195 | while (c) |
| 196 | { |
| 197 | total_size += c->size; |
| 198 | c = c->next; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash)) |
| 203 | { |
| 204 | clib_bihash_alloc_chunk_48_8_t *c = st->v6_session_hash.chunks; |
| 205 | while (c) |
| 206 | { |
| 207 | total_size += c->size; |
| 208 | c = c->next; |
| 209 | } |
| 210 | c = st->v6_half_open_hash.chunks; |
| 211 | while (c) |
| 212 | { |
| 213 | total_size += c->size; |
| 214 | c = c->next; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return total_size; |
| 219 | } |
| 220 | |
| 221 | u8 * |
| 222 | format_session_table (u8 *s, va_list *args) |
| 223 | { |
| 224 | session_table_t *st = va_arg (*args, session_table_t *); |
| 225 | |
| 226 | if (clib_bihash_is_initialised_16_8 (&st->v4_session_hash)) |
| 227 | { |
| 228 | s = format (s, "%U", format_bihash_16_8, &st->v4_session_hash, 0); |
| 229 | s = format (s, "%U", format_bihash_16_8, &st->v4_half_open_hash, 0); |
| 230 | } |
| 231 | |
| 232 | if (clib_bihash_is_initialised_48_8 (&st->v6_session_hash)) |
| 233 | { |
| 234 | s = format (s, "%U", format_bihash_48_8, &st->v6_session_hash, 0); |
| 235 | s = format (s, "%U", format_bihash_48_8, &st->v6_half_open_hash, 0); |
| 236 | } |
| 237 | |
| 238 | return s; |
| 239 | } |
| 240 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 241 | /* |
| 242 | * fd.io coding-style-patch-verification: ON |
| 243 | * |
| 244 | * Local Variables: |
| 245 | * eval: (c-set-style "gnu") |
| 246 | * End: |
| 247 | */ |