blob: 354fab53bc3e31fc7a9030d0c960b03f97778ba3 [file] [log] [blame]
Florin Coras04e53442017-07-16 17:12:15 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Coras04e53442017-07-16 17:12:15 -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/** Generate typed init functions for multiple hash table styles... */
17#include <vppinfra/bihash_16_8.h>
18#include <vppinfra/bihash_template.h>
19
20#include <vppinfra/bihash_template.c>
21
22#undef __included_bihash_template_h__
23
24#include <vppinfra/bihash_48_8.h>
25#include <vppinfra/bihash_template.h>
26
27#include <vppinfra/bihash_template.c>
28#include <vnet/session/session_lookup.h>
29#include <vnet/session/session.h>
Florin Corascea194d2017-10-02 00:18:51 -070030#include <vnet/session/application.h>
Florin Coras04e53442017-07-16 17:12:15 -070031
Florin Corasf9bdc032024-02-02 13:01:07 -080032static session_lookup_main_t sl_main;
33
Florin Corascea194d2017-10-02 00:18:51 -070034/**
Florin Corascea194d2017-10-02 00:18:51 -070035 * Network namespace index (i.e., fib index) to session lookup table. We
36 * should have one per network protocol type but for now we only support IP4/6
37 */
38static u32 *fib_index_to_table_index[2];
39
Florin Coras04e53442017-07-16 17:12:15 -070040/* *INDENT-OFF* */
41/* 16 octets */
42typedef CLIB_PACKED (struct {
43 union
44 {
45 struct
46 {
47 ip4_address_t src;
48 ip4_address_t dst;
49 u16 src_port;
50 u16 dst_port;
51 /* align by making this 4 octets even though its a 1-bit field
52 * NOTE: avoid key overlap with other transports that use 5 tuples for
53 * session identification.
54 */
55 u32 proto;
56 };
57 u64 as_u64[2];
58 };
59}) v4_connection_key_t;
60
61typedef CLIB_PACKED (struct {
62 union
63 {
64 struct
65 {
66 /* 48 octets */
67 ip6_address_t src;
68 ip6_address_t dst;
69 u16 src_port;
70 u16 dst_port;
71 u32 proto;
72 u64 unused;
73 };
74 u64 as_u64[6];
75 };
76}) v6_connection_key_t;
77/* *INDENT-ON* */
78
79typedef clib_bihash_kv_16_8_t session_kv4_t;
80typedef clib_bihash_kv_48_8_t session_kv6_t;
81
82always_inline void
83make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
84 u16 lcl_port, u16 rmt_port, u8 proto)
85{
Florin Coras9679c812018-06-21 08:14:34 -070086 kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
87 kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070088 kv->value = ~0ULL;
89}
90
91always_inline void
92make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
93 u8 proto)
94{
Florin Coras9679c812018-06-21 08:14:34 -070095 kv->key[0] = (u64) lcl->as_u32;
96 kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070097 kv->value = ~0ULL;
98}
99
100always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800101make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
102{
Florin Coras9679c812018-06-21 08:14:34 -0700103 kv->key[0] = (u64) lcl->as_u32;
104 kv->key[1] = (u64) proto << 32;
Florin Corasdbd44562017-11-09 19:30:17 -0800105 kv->value = ~0ULL;
106}
107
108always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800109make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700110{
Florin Coras561af9b2017-12-09 10:19:43 -0800111 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
112 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700113}
114
115always_inline void
116make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
117 u16 lcl_port, u16 rmt_port, u8 proto)
118{
Florin Coras9679c812018-06-21 08:14:34 -0700119 kv->key[0] = lcl->as_u64[0];
120 kv->key[1] = lcl->as_u64[1];
121 kv->key[2] = rmt->as_u64[0];
122 kv->key[3] = rmt->as_u64[1];
123 kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
124 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700125 kv->value = ~0ULL;
126}
127
128always_inline void
129make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
130 u8 proto)
131{
Florin Coras9679c812018-06-21 08:14:34 -0700132 kv->key[0] = lcl->as_u64[0];
133 kv->key[1] = lcl->as_u64[1];
134 kv->key[2] = 0;
135 kv->key[3] = 0;
136 kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
137 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700138 kv->value = ~0ULL;
139}
140
141always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800142make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
143{
Florin Coras9679c812018-06-21 08:14:34 -0700144 kv->key[0] = lcl->as_u64[0];
145 kv->key[1] = lcl->as_u64[1];
146 kv->key[2] = 0;
147 kv->key[3] = 0;
148 kv->key[4] = (u64) proto << 32;
149 kv->key[5] = 0;
Florin Corasdbd44562017-11-09 19:30:17 -0800150 kv->value = ~0ULL;
151}
152
153always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800154make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700155{
Florin Coras561af9b2017-12-09 10:19:43 -0800156 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
157 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700158}
159
Florin Corasf9bdc032024-02-02 13:01:07 -0800160static inline u8
161session_table_alloc_needs_sync (void)
162{
163 return !vlib_thread_is_main_w_barrier () && (vlib_num_workers () > 1);
164}
165
Florin Corascea194d2017-10-02 00:18:51 -0700166static session_table_t *
Andreas Schultz30a28c12020-04-23 10:41:50 +0200167session_table_get_or_alloc (u8 fib_proto, u32 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700168{
Florin Corascea194d2017-10-02 00:18:51 -0700169 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700170 u32 table_index;
Andreas Schultz30a28c12020-04-23 10:41:50 +0200171 ASSERT (fib_index != ~0);
172 if (vec_len (fib_index_to_table_index[fib_proto]) > fib_index &&
173 fib_index_to_table_index[fib_proto][fib_index] != ~0)
174 {
175 table_index = fib_index_to_table_index[fib_proto][fib_index];
176 return session_table_get (table_index);
177 }
178 else
Florin Corascea194d2017-10-02 00:18:51 -0700179 {
Florin Corasf9bdc032024-02-02 13:01:07 -0800180 u8 needs_sync = session_table_alloc_needs_sync ();
181 session_lookup_main_t *slm = &sl_main;
182
183 /* Stop workers, otherwise consumers might be affected. This is
184 * acceptable because new tables should seldom be allocated */
185 if (needs_sync)
186 {
187 vlib_workers_sync ();
188
189 /* We might have a race, only one worker allowed at once */
190 clib_spinlock_lock (&slm->st_alloc_lock);
191 }
192
Florin Corascea194d2017-10-02 00:18:51 -0700193 st = session_table_alloc ();
194 table_index = session_table_index (st);
Andreas Schultz30a28c12020-04-23 10:41:50 +0200195 vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index,
196 ~0);
Florin Coras6c36f532017-11-03 18:32:34 -0700197 fib_index_to_table_index[fib_proto][fib_index] = table_index;
198 st->active_fib_proto = fib_proto;
Florin Corasb795bd02017-12-14 11:30:48 -0800199 session_table_init (st, fib_proto);
Florin Corasf9bdc032024-02-02 13:01:07 -0800200
201 if (needs_sync)
202 {
203 clib_spinlock_unlock (&slm->st_alloc_lock);
204 vlib_workers_continue ();
205 }
206
Florin Corascea194d2017-10-02 00:18:51 -0700207 return st;
208 }
Florin Corascea194d2017-10-02 00:18:51 -0700209}
210
211static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700212session_table_get_or_alloc_for_connection (transport_connection_t * tc)
213{
214 u32 fib_proto;
215 fib_proto = transport_connection_fib_proto (tc);
216 return session_table_get_or_alloc (fib_proto, tc->fib_index);
217}
218
219static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700220session_table_get_for_connection (transport_connection_t * tc)
221{
222 u32 fib_proto = transport_connection_fib_proto (tc);
223 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
224 return 0;
225 return
226 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
227}
228
229static session_table_t *
230session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
231{
232 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
233 return 0;
234 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
235}
236
237u32
238session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
239{
240 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
241 return SESSION_TABLE_INVALID_INDEX;
242 return fib_index_to_table_index[fib_proto][fib_index];
243}
244
Florin Corasa4d09562021-07-08 08:25:09 -0700245u32
246session_lookup_get_or_alloc_index_for_fib (u32 fib_proto, u32 fib_index)
247{
248 session_table_t *st;
249 st = session_table_get_or_alloc (fib_proto, fib_index);
250 return session_table_index (st);
251}
252
Florin Corascea194d2017-10-02 00:18:51 -0700253/**
254 * Add transport connection to a session table
255 *
256 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
257 * is added to requested session table.
258 *
259 * @param tc transport connection to be added
260 * @param value value to be stored
261 *
262 * @return non-zero if failure
263 */
264int
265session_lookup_add_connection (transport_connection_t * tc, u64 value)
266{
267 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700268 session_kv4_t kv4;
269 session_kv6_t kv6;
270
Florin Corascea194d2017-10-02 00:18:51 -0700271 st = session_table_get_or_alloc_for_connection (tc);
272 if (!st)
273 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700274 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700275 {
Florin Coras04e53442017-07-16 17:12:15 -0700276 make_v4_ss_kv_from_tc (&kv4, tc);
277 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700278 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
279 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700280 }
281 else
282 {
Florin Coras04e53442017-07-16 17:12:15 -0700283 make_v6_ss_kv_from_tc (&kv6, tc);
284 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700285 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
286 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700287 }
288}
289
Florin Coras04e53442017-07-16 17:12:15 -0700290int
Florin Corascea194d2017-10-02 00:18:51 -0700291session_lookup_add_session_endpoint (u32 table_index,
292 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700293{
Florin Corascea194d2017-10-02 00:18:51 -0700294 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700295 session_kv4_t kv4;
296 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700297
Florin Corascea194d2017-10-02 00:18:51 -0700298 st = session_table_get (table_index);
299 if (!st)
300 return -1;
301 if (sep->is_ip4)
302 {
303 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
304 sep->transport_proto);
305 kv4.value = value;
306 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
307 }
308 else
309 {
310 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
311 sep->transport_proto);
312 kv6.value = value;
313 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
314 }
315}
316
317int
318session_lookup_del_session_endpoint (u32 table_index,
319 session_endpoint_t * sep)
320{
321 session_table_t *st;
322 session_kv4_t kv4;
323 session_kv6_t kv6;
324
325 st = session_table_get (table_index);
326 if (!st)
327 return -1;
328 if (sep->is_ip4)
329 {
330 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
331 sep->transport_proto);
332 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
333 }
334 else
335 {
336 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
337 sep->transport_proto);
338 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
339 }
340}
341
Florin Corasaab06042020-02-26 02:56:14 +0000342int
343session_lookup_del_session_endpoint2 (session_endpoint_t * sep)
344{
345 fib_protocol_t fib_proto;
346 session_table_t *st;
347 session_kv4_t kv4;
348 session_kv6_t kv6;
349
350 fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
351 st = session_table_get_for_fib_index (fib_proto, sep->fib_index);
352 if (!st)
353 return -1;
354 if (sep->is_ip4)
355 {
356 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
357 sep->transport_proto);
358 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
359 }
360 else
361 {
362 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
363 sep->transport_proto);
364 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
365 }
366}
367
Florin Corascea194d2017-10-02 00:18:51 -0700368/**
369 * Delete transport connection from session table
370 *
371 * @param table_index session table index
372 * @param tc transport connection to be removed
373 *
374 * @return non-zero if failure
375 */
376int
377session_lookup_del_connection (transport_connection_t * tc)
378{
379 session_table_t *st;
380 session_kv4_t kv4;
381 session_kv6_t kv6;
382
383 st = session_table_get_for_connection (tc);
384 if (!st)
385 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700386 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700387 {
Florin Coras04e53442017-07-16 17:12:15 -0700388 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700389 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700390 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700391 }
392 else
393 {
Florin Coras04e53442017-07-16 17:12:15 -0700394 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700395 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700396 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700397 }
Florin Coras04e53442017-07-16 17:12:15 -0700398}
399
400int
Florin Coras288eaab2019-02-03 15:26:14 -0800401session_lookup_del_session (session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700402{
403 transport_connection_t *ts;
Florin Coras1ee78302019-02-05 15:51:15 -0800404 ts = transport_get_connection (session_get_transport_proto (s),
405 s->connection_index, s->thread_index);
Florin Corase1e7fb82019-09-25 07:28:34 -0700406 if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200407 return 0;
Florin Corascea194d2017-10-02 00:18:51 -0700408 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700409}
410
Florin Corasdff48db2017-11-19 18:06:58 -0800411static u8
412session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700413{
Florin Corasdff48db2017-11-19 18:06:58 -0800414 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
415 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
416 return 0;
417 return 1;
418}
419
Florin Corasf8f516a2018-02-08 15:10:09 -0800420static u64
421session_lookup_action_to_handle (u32 action_index)
Florin Corasdff48db2017-11-19 18:06:58 -0800422{
423 switch (action_index)
424 {
425 case SESSION_RULES_TABLE_ACTION_DROP:
Florin Corasf8f516a2018-02-08 15:10:09 -0800426 return SESSION_DROP_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800427 case SESSION_RULES_TABLE_ACTION_ALLOW:
428 case SESSION_RULES_TABLE_INVALID_INDEX:
Florin Corasf8f516a2018-02-08 15:10:09 -0800429 return SESSION_INVALID_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800430 default:
Florin Corasf8f516a2018-02-08 15:10:09 -0800431 /* application index */
Florin Corasdff48db2017-11-19 18:06:58 -0800432 return action_index;
433 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700434}
435
Florin Coras288eaab2019-02-03 15:26:14 -0800436static session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700437session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
438 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700439{
440 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800441 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700442 if (!app)
443 return 0;
444
Florin Coras15531972018-08-12 23:50:53 -0700445 return app_worker_first_listener (application_get_default_worker (app),
446 fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700447}
448
Florin Coras288eaab2019-02-03 15:26:14 -0800449static session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800450session_lookup_action_to_session (u32 action_index, u8 fib_proto,
451 u8 transport_proto)
452{
Florin Corasdff48db2017-11-19 18:06:58 -0800453 u32 app_index;
Florin Corasf8f516a2018-02-08 15:10:09 -0800454 app_index = session_lookup_action_to_handle (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800455 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800456 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800457 transport_proto);
458}
459
Florin Corasf8f516a2018-02-08 15:10:09 -0800460/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800461session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800462session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
463 ip4_address_t * lcl, u16 lcl_port,
464 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700465{
Florin Corasc97a7392017-11-05 23:07:07 -0800466 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800467 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800468 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700469 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800470 app_index = session_lookup_action_to_handle (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700471 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800472 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700473 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700474}
475
Florin Corasf8f516a2018-02-08 15:10:09 -0800476/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800477session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800478session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
479 ip6_address_t * lcl, u16 lcl_port,
480 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700481{
Florin Corasc97a7392017-11-05 23:07:07 -0800482 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800483 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800484 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700485 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800486 app_index = session_lookup_action_to_handle (action_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800487 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700488 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700489}
490
Florin Corasa2ff7b82017-11-08 17:55:03 -0800491/**
492 * Lookup listener for session endpoint in table
493 *
494 * @param table_index table where the endpoint should be looked up
495 * @param sep session endpoint to be looked up
496 * @param use_rules flag that indicates if the session rules of the table
497 * should be used
498 * @return invalid handle if nothing is found, the handle of a valid listener
Florin Corasf8f516a2018-02-08 15:10:09 -0800499 * or an action derived handle if a rule is hit
Florin Corasa2ff7b82017-11-08 17:55:03 -0800500 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700501u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800502session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
503 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700504{
Florin Corasc97a7392017-11-05 23:07:07 -0800505 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700506 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700507 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700508 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700509
Florin Corascea194d2017-10-02 00:18:51 -0700510 st = session_table_get (table_index);
511 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700512 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700513 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700514 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800515 session_kv4_t kv4;
516 ip4_address_t lcl4;
517
Florin Coras561af9b2017-12-09 10:19:43 -0800518 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
519 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700520 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
521 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700522 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800523 if (use_rules)
524 {
Dave Barachb7b92992018-10-17 10:38:51 -0400525 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800526 srt = &st->session_rules[sep->transport_proto];
527 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
528 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800529 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800530 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800531 }
Florin Coras68810622017-07-24 17:40:28 -0700532 }
533 else
534 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800535 session_kv6_t kv6;
536 ip6_address_t lcl6;
537
Florin Coras561af9b2017-12-09 10:19:43 -0800538 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
539 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700540 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
541 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700542 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700543
Florin Corasa2ff7b82017-11-08 17:55:03 -0800544 if (use_rules)
545 {
Dave Barachb7b92992018-10-17 10:38:51 -0400546 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800547 srt = &st->session_rules[sep->transport_proto];
548 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
549 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800550 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800551 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800552 }
Florin Coras68810622017-07-24 17:40:28 -0700553 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700554 return SESSION_INVALID_HANDLE;
555}
556
Florin Corasa2ff7b82017-11-08 17:55:03 -0800557/**
558 * Look up endpoint in local session table
559 *
560 * The result, for now, is an application index and it may in the future
561 * be extended to a more complicated "action object". The only action we
562 * emulate now is "drop" and for that we return a special app index.
563 *
564 * Lookup logic is to check in order:
565 * - the rules in the table (connect acls)
566 * - session sub-table for a listener
567 * - session sub-table for a local listener (zeroed addr)
568 *
569 * @param table_index table where the lookup should be done
570 * @param sep session endpoint to be looked up
Florin Corasf8f516a2018-02-08 15:10:09 -0800571 * @return session handle that can be interpreted as an adjacency
Florin Corasa2ff7b82017-11-08 17:55:03 -0800572 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800573u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800574session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700575{
Florin Corasc97a7392017-11-05 23:07:07 -0800576 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700577 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700578 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700579 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700580
Florin Corascea194d2017-10-02 00:18:51 -0700581 st = session_table_get (table_index);
582 if (!st)
583 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800584 ASSERT (st->is_local);
585
Florin Corascea194d2017-10-02 00:18:51 -0700586 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700587 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800588 session_kv4_t kv4;
589 ip4_address_t lcl4;
590
591 /*
592 * Check if endpoint has special rules associated
593 */
Dave Barachb7b92992018-10-17 10:38:51 -0400594 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800595 srt = &st->session_rules[sep->transport_proto];
596 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
597 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800598 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800599 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800600
601 /*
602 * Check if session endpoint is a listener
603 */
Florin Corascea194d2017-10-02 00:18:51 -0700604 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
605 sep->transport_proto);
606 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
607 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800608 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700609
610 /*
611 * Zero out the ip. Logic is that connect to local ips, say
612 * 127.0.0.1:port, can match 0.0.0.0:port
613 */
Florin Coras477e91a2018-02-27 10:05:57 -0800614 if (ip4_is_local_host (&sep->ip.ip4))
615 {
616 kv4.key[0] = 0;
617 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
618 if (rv == 0)
619 return kv4.value;
620 }
621 else
622 {
623 kv4.key[0] = 0;
624 }
Florin Corasdbd44562017-11-09 19:30:17 -0800625
626 /*
627 * Zero out the port and check if we have proxy
628 */
629 kv4.key[1] = 0;
630 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
631 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800632 return kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700633 }
634 else
635 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800636 session_kv6_t kv6;
637 ip6_address_t lcl6;
638
Dave Barachb7b92992018-10-17 10:38:51 -0400639 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800640 srt = &st->session_rules[sep->transport_proto];
641 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
642 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800643 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800644 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800645
Florin Corascea194d2017-10-02 00:18:51 -0700646 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
647 sep->transport_proto);
648 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
649 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800650 return kv6.value;
Florin Corascea194d2017-10-02 00:18:51 -0700651
652 /*
653 * Zero out the ip. Same logic as above.
654 */
Florin Coras477e91a2018-02-27 10:05:57 -0800655
656 if (ip6_is_local_host (&sep->ip.ip6))
657 {
658 kv6.key[0] = kv6.key[1] = 0;
659 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
660 if (rv == 0)
661 return kv6.value;
662 }
663 else
664 {
665 kv6.key[0] = kv6.key[1] = 0;
666 }
Florin Corasdbd44562017-11-09 19:30:17 -0800667
668 /*
669 * Zero out the port. Same logic as above.
670 */
671 kv6.key[4] = kv6.key[5] = 0;
672 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
673 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800674 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700675 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800676 return SESSION_INVALID_HANDLE;
Florin Coras04e53442017-07-16 17:12:15 -0700677}
678
Florin Coras288eaab2019-02-03 15:26:14 -0800679static inline session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700680session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800681 u16 lcl_port, u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700682{
Florin Coras04e53442017-07-16 17:12:15 -0700683 session_kv4_t kv4;
684 int rv;
685
Florin Corasdbd44562017-11-09 19:30:17 -0800686 /*
687 * First, try a fully formed listener
688 */
Florin Coras04e53442017-07-16 17:12:15 -0700689 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700690 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700691 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700692 return listen_session_get ((u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700693
Florin Corasdbd44562017-11-09 19:30:17 -0800694 /*
695 * Zero out the lcl ip and check if any 0/0 port binds have been done
696 */
Florin Coras477e91a2018-02-27 10:05:57 -0800697 if (use_wildcard)
698 {
699 kv4.key[0] = 0;
700 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
701 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700702 return listen_session_get ((u32) kv4.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800703 }
704 else
705 {
706 kv4.key[0] = 0;
707 }
Florin Coras04e53442017-07-16 17:12:15 -0700708
Florin Corasdbd44562017-11-09 19:30:17 -0800709 /*
710 * Zero out port and check if we have a proxy set up for our ip
711 */
712 make_v4_proxy_kv (&kv4, lcl, proto);
713 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
714 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700715 return listen_session_get ((u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800716
Florin Coras04e53442017-07-16 17:12:15 -0700717 return 0;
718}
719
Florin Coras288eaab2019-02-03 15:26:14 -0800720session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700721session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800722 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700723{
Florin Corascea194d2017-10-02 00:18:51 -0700724 session_table_t *st;
725 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
726 if (!st)
727 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800728 return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700729}
730
Florin Coras288eaab2019-02-03 15:26:14 -0800731static session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700732session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800733 u16 lcl_port, u8 proto, u8 ip_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700734{
Florin Coras04e53442017-07-16 17:12:15 -0700735 session_kv6_t kv6;
736 int rv;
737
738 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700739 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700740 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700741 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700742
743 /* Zero out the lcl ip */
Florin Coras477e91a2018-02-27 10:05:57 -0800744 if (ip_wildcard)
745 {
746 kv6.key[0] = kv6.key[1] = 0;
747 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
748 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700749 return listen_session_get ((u32) kv6.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800750 }
751 else
752 {
753 kv6.key[0] = kv6.key[1] = 0;
754 }
Florin Coras04e53442017-07-16 17:12:15 -0700755
Florin Corasdbd44562017-11-09 19:30:17 -0800756 make_v6_proxy_kv (&kv6, lcl, proto);
757 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
758 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700759 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700760 return 0;
761}
762
Florin Coras288eaab2019-02-03 15:26:14 -0800763session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700764session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800765 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700766{
Florin Corascea194d2017-10-02 00:18:51 -0700767 session_table_t *st;
768 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
769 if (!st)
770 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800771 return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700772}
773
Florin Coras477e91a2018-02-27 10:05:57 -0800774/**
775 * Lookup listener, exact or proxy (inaddr_any:0) match
776 */
Florin Coras288eaab2019-02-03 15:26:14 -0800777session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700778session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700779{
Florin Corascea194d2017-10-02 00:18:51 -0700780 session_table_t *st;
781 st = session_table_get (table_index);
782 if (!st)
783 return 0;
784 if (sep->is_ip4)
785 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800786 sep->transport_proto, 0);
Florin Corascea194d2017-10-02 00:18:51 -0700787 else
788 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800789 sep->transport_proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700790 return 0;
791}
792
Florin Coras9f1a5432019-03-10 21:03:20 -0700793/**
794 * Lookup listener wildcard match
795 */
796session_t *
797session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep)
798{
799 session_table_t *st;
800 st = session_table_get (table_index);
801 if (!st)
802 return 0;
803 if (sep->is_ip4)
804 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
805 sep->transport_proto,
806 1 /* use_wildcard */ );
807 else
808 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
809 sep->transport_proto,
810 1 /* use_wildcard */ );
811 return 0;
812}
813
Florin Corascea194d2017-10-02 00:18:51 -0700814int
815session_lookup_add_half_open (transport_connection_t * tc, u64 value)
816{
817 session_table_t *st;
818 session_kv4_t kv4;
819 session_kv6_t kv6;
820
821 st = session_table_get_or_alloc_for_connection (tc);
822 if (!st)
823 return 0;
824 if (tc->is_ip4)
825 {
826 make_v4_ss_kv_from_tc (&kv4, tc);
827 kv4.value = value;
828 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
829 1 /* is_add */ );
830 }
831 else
832 {
833 make_v6_ss_kv_from_tc (&kv6, tc);
834 kv6.value = value;
835 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
836 1 /* is_add */ );
837 }
838}
839
840int
841session_lookup_del_half_open (transport_connection_t * tc)
842{
843 session_table_t *st;
844 session_kv4_t kv4;
845 session_kv6_t kv6;
846
847 st = session_table_get_for_connection (tc);
848 if (!st)
849 return -1;
850 if (tc->is_ip4)
851 {
852 make_v4_ss_kv_from_tc (&kv4, tc);
853 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
854 0 /* is_add */ );
855 }
856 else
857 {
858 make_v6_ss_kv_from_tc (&kv6, tc);
859 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
860 0 /* is_add */ );
861 }
862}
863
Florin Coras04e53442017-07-16 17:12:15 -0700864u64
Florin Corascea194d2017-10-02 00:18:51 -0700865session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700866{
Florin Corascea194d2017-10-02 00:18:51 -0700867 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700868 session_kv4_t kv4;
869 session_kv6_t kv6;
870 int rv;
871
Florin Corascea194d2017-10-02 00:18:51 -0700872 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
873 tc->fib_index);
874 if (!st)
875 return HALF_OPEN_LOOKUP_INVALID_VALUE;
876 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700877 {
Florin Corascea194d2017-10-02 00:18:51 -0700878 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700879 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700880 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700881 if (rv == 0)
882 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700883 }
884 else
885 {
886 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700887 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700888 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700889 if (rv == 0)
890 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700891 }
892 return HALF_OPEN_LOOKUP_INVALID_VALUE;
893}
894
895transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700896session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700897{
Florin Coras04e53442017-07-16 17:12:15 -0700898 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700899 {
Florin Coras1ee78302019-02-05 15:51:15 -0800900 u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
901 return transport_get_half_open (sst, handle & 0xFFFFFFFF);
Florin Corascea194d2017-10-02 00:18:51 -0700902 }
Florin Coras04e53442017-07-16 17:12:15 -0700903 return 0;
904}
905
Florin Corascea194d2017-10-02 00:18:51 -0700906/**
907 * Lookup connection with ip4 and transport layer information
908 *
909 * This is used on the fast path so it needs to be fast. Thereby,
910 * duplication of code and 'hacks' allowed.
911 *
912 * The lookup is incremental and returns whenever something is matched. The
913 * steps are:
914 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700915 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700916 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800917 * - Try to find a fully-formed or local source wildcarded (listener bound to
918 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700919 * - return 0
920 *
921 * @param fib_index index of fib wherein the connection was received
922 * @param lcl local ip4 address
923 * @param rmt remote ip4 address
924 * @param lcl_port local port
925 * @param rmt_port remote port
926 * @param proto transport protocol (e.g., tcp, udp)
927 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800928 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700929 *
930 * @return pointer to transport connection, if one is found, 0 otherwise
931 */
Florin Coras04e53442017-07-16 17:12:15 -0700932transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700933session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
934 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800935 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -0800936 u8 * result)
Florin Coras04e53442017-07-16 17:12:15 -0700937{
Florin Corascea194d2017-10-02 00:18:51 -0700938 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700939 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800940 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800941 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700942 int rv;
943
Florin Corascea194d2017-10-02 00:18:51 -0700944 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
945 if (PREDICT_FALSE (!st))
946 return 0;
947
Florin Corasa2ff7b82017-11-08 17:55:03 -0800948 /*
949 * Lookup session amongst established ones
950 */
Florin Coras04e53442017-07-16 17:12:15 -0700951 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700952 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700953 if (rv == 0)
954 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800955 if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
956 {
957 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
958 return 0;
959 }
Florin Corascea194d2017-10-02 00:18:51 -0700960 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -0800961 return transport_get_connection (proto, s->connection_index,
962 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700963 }
964
Florin Corasa2ff7b82017-11-08 17:55:03 -0800965 /*
966 * Try half-open connections
967 */
Florin Corascea194d2017-10-02 00:18:51 -0700968 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700969 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -0800970 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700971
Florin Corasa2ff7b82017-11-08 17:55:03 -0800972 /*
973 * Check the session rules table
974 */
975 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
976 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800977 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800978 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800979 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
980 {
981 *result = SESSION_LOOKUP_RESULT_FILTERED;
982 return 0;
983 }
Florin Corasdff48db2017-11-19 18:06:58 -0800984 if ((s = session_lookup_action_to_session (action_index,
985 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -0800986 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800987 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800988 }
989
990 /*
991 * If nothing is found, check if any listener is available
992 */
Florin Coras477e91a2018-02-27 10:05:57 -0800993 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800994 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -0800995 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800996
997 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700998}
999
Florin Corascea194d2017-10-02 00:18:51 -07001000/**
1001 * Lookup connection with ip4 and transport layer information
1002 *
Florin Corasb5e55a22019-01-10 12:42:47 -08001003 * Not optimized. Lookup logic is identical to that of
1004 * @ref session_lookup_connection_wt4
Florin Corascea194d2017-10-02 00:18:51 -07001005 *
1006 * @param fib_index index of the fib wherein the connection was received
1007 * @param lcl local ip4 address
1008 * @param rmt remote ip4 address
1009 * @param lcl_port local port
1010 * @param rmt_port remote port
1011 * @param proto transport protocol (e.g., tcp, udp)
1012 *
1013 * @return pointer to transport connection, if one is found, 0 otherwise
1014 */
Florin Coras04e53442017-07-16 17:12:15 -07001015transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001016session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
1017 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
1018 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001019{
Florin Corascea194d2017-10-02 00:18:51 -07001020 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001021 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001022 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001023 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001024 int rv;
1025
Florin Corascea194d2017-10-02 00:18:51 -07001026 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1027 if (PREDICT_FALSE (!st))
1028 return 0;
1029
Florin Corasa2ff7b82017-11-08 17:55:03 -08001030 /*
1031 * Lookup session amongst established ones
1032 */
Florin Coras04e53442017-07-16 17:12:15 -07001033 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001034 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001035 if (rv == 0)
1036 {
Florin Corascea194d2017-10-02 00:18:51 -07001037 s = session_get_from_handle (kv4.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001038 return transport_get_connection (proto, s->connection_index,
1039 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001040 }
1041
Florin Corasa2ff7b82017-11-08 17:55:03 -08001042 /*
1043 * Try half-open connections
1044 */
Florin Corascea194d2017-10-02 00:18:51 -07001045 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001046 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001047 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001048
1049 /*
1050 * Check the session rules table
1051 */
1052 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1053 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001054 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001055 {
Florin Corasdff48db2017-11-19 18:06:58 -08001056 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1057 return 0;
1058 if ((s = session_lookup_action_to_session (action_index,
1059 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001060 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001061 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001062 }
1063
1064 /*
1065 * If nothing is found, check if any listener is available
1066 */
Florin Coras477e91a2018-02-27 10:05:57 -08001067 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001068 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001069 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001070
1071 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001072}
1073
Florin Corascea194d2017-10-02 00:18:51 -07001074/**
1075 * Lookup session with ip4 and transport layer information
1076 *
Florin Coras6bd8d3f2022-03-14 21:17:25 -07001077 * Important note: this may look into another thread's pool table
Florin Coras3cbc04b2017-10-02 00:18:51 -07001078 *
1079 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
1080 * this returns a session as opposed to a transport connection and it does not
1081 * try to lookup half-open sessions.
1082 *
1083 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001084 */
Florin Coras288eaab2019-02-03 15:26:14 -08001085session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001086session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1087 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001088{
Florin Corascea194d2017-10-02 00:18:51 -07001089 session_table_t *st;
1090 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001091 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001092 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001093 int rv;
1094
1095 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1096 if (PREDICT_FALSE (!st))
1097 return 0;
1098
Florin Corasa2ff7b82017-11-08 17:55:03 -08001099 /*
1100 * Lookup session amongst established ones
1101 */
Florin Corascea194d2017-10-02 00:18:51 -07001102 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1103 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1104 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001105 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001106
Florin Corasa2ff7b82017-11-08 17:55:03 -08001107 /*
1108 * Check the session rules table
1109 */
1110 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1111 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001112 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001113 {
Florin Corasdff48db2017-11-19 18:06:58 -08001114 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1115 return 0;
1116 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1117 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001118 }
1119
1120 /*
1121 * If nothing is found, check if any listener is available
1122 */
Florin Coras477e91a2018-02-27 10:05:57 -08001123 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001124 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001125
1126 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001127}
1128
1129/**
1130 * Lookup connection with ip6 and transport layer information
1131 *
1132 * This is used on the fast path so it needs to be fast. Thereby,
1133 * duplication of code and 'hacks' allowed.
1134 *
1135 * The lookup is incremental and returns whenever something is matched. The
1136 * steps are:
1137 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001138 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001139 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001140 * - Try to find a fully-formed or local source wildcarded (listener bound to
1141 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001142 * - return 0
1143 *
1144 * @param fib_index index of the fib wherein the connection was received
1145 * @param lcl local ip6 address
1146 * @param rmt remote ip6 address
1147 * @param lcl_port local port
1148 * @param rmt_port remote port
1149 * @param proto transport protocol (e.g., tcp, udp)
1150 * @param thread_index thread index for request
1151 *
1152 * @return pointer to transport connection, if one is found, 0 otherwise
1153 */
1154transport_connection_t *
1155session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1156 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001157 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -08001158 u8 * result)
Florin Corascea194d2017-10-02 00:18:51 -07001159{
1160 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001161 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001162 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001163 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001164 int rv;
1165
Florin Corascea194d2017-10-02 00:18:51 -07001166 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1167 if (PREDICT_FALSE (!st))
1168 return 0;
1169
Florin Coras04e53442017-07-16 17:12:15 -07001170 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001171 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001172 if (rv == 0)
1173 {
Florin Corascea194d2017-10-02 00:18:51 -07001174 ASSERT ((u32) (kv6.value >> 32) == thread_index);
Florin Corasb5e55a22019-01-10 12:42:47 -08001175 if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1176 {
1177 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1178 return 0;
1179 }
Florin Corascea194d2017-10-02 00:18:51 -07001180 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -08001181 return transport_get_connection (proto, s->connection_index,
1182 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001183 }
1184
Florin Corasa2ff7b82017-11-08 17:55:03 -08001185 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001186 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001187 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001188 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001189
Florin Corasa2ff7b82017-11-08 17:55:03 -08001190 /* Check the session rules table */
1191 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1192 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001193 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001194 {
Florin Corasb5e55a22019-01-10 12:42:47 -08001195 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1196 {
1197 *result = SESSION_LOOKUP_RESULT_FILTERED;
1198 return 0;
1199 }
Florin Corasdff48db2017-11-19 18:06:58 -08001200 if ((s = session_lookup_action_to_session (action_index,
1201 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001202 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001203 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001204 }
1205
1206 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001207 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001208 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001209 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001210
1211 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001212}
1213
Florin Corascea194d2017-10-02 00:18:51 -07001214/**
1215 * Lookup connection with ip6 and transport layer information
1216 *
1217 * Not optimized. This is used on the fast path so it needs to be fast.
1218 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1219 * to that of @ref session_lookup_connection_wt4
1220 *
1221 * @param fib_index index of the fib wherein the connection was received
1222 * @param lcl local ip6 address
1223 * @param rmt remote ip6 address
1224 * @param lcl_port local port
1225 * @param rmt_port remote port
1226 * @param proto transport protocol (e.g., tcp, udp)
1227 *
1228 * @return pointer to transport connection, if one is found, 0 otherwise
1229 */
Florin Coras04e53442017-07-16 17:12:15 -07001230transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001231session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1232 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1233 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001234{
Florin Corascea194d2017-10-02 00:18:51 -07001235 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001236 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001237 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001238 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001239 int rv;
1240
Florin Corascea194d2017-10-02 00:18:51 -07001241 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1242 if (PREDICT_FALSE (!st))
1243 return 0;
1244
Florin Coras04e53442017-07-16 17:12:15 -07001245 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001246 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001247 if (rv == 0)
1248 {
Florin Corascea194d2017-10-02 00:18:51 -07001249 s = session_get_from_handle (kv6.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001250 return transport_get_connection (proto, s->connection_index,
1251 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001252 }
1253
Florin Corasa2ff7b82017-11-08 17:55:03 -08001254 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001255 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001256 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001257 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001258
Florin Corasa2ff7b82017-11-08 17:55:03 -08001259 /* Check the session rules table */
1260 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1261 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001262 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001263 {
Florin Corasdff48db2017-11-19 18:06:58 -08001264 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1265 return 0;
1266 if ((s = session_lookup_action_to_session (action_index,
1267 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001268 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001269 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001270 }
1271
1272 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001273 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001274 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001275 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001276
1277 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001278}
1279
Florin Corascea194d2017-10-02 00:18:51 -07001280/**
1281 * Lookup session with ip6 and transport layer information
1282 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001283 * Important note: this may look into another thread's pool table and
1284 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1285 * if needed as soon as possible.
1286 *
1287 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1288 * this returns a session as opposed to a transport connection and it does not
1289 * try to lookup half-open sessions.
1290 *
1291 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001292 */
Florin Coras288eaab2019-02-03 15:26:14 -08001293session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001294session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1295 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001296{
1297 session_table_t *st;
1298 session_kv6_t kv6;
Florin Coras288eaab2019-02-03 15:26:14 -08001299 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001300 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001301 int rv;
1302
1303 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1304 if (PREDICT_FALSE (!st))
1305 return 0;
1306
1307 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1308 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1309 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001310 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001311
Florin Corasa2ff7b82017-11-08 17:55:03 -08001312 /* Check the session rules table */
1313 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1314 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001315 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001316 {
Florin Corasdff48db2017-11-19 18:06:58 -08001317 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1318 return 0;
1319 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1320 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001321 }
1322
Florin Corascea194d2017-10-02 00:18:51 -07001323 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001324 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001325 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001326 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001327}
1328
Florin Coras57660d92020-04-04 22:45:34 +00001329transport_connection_t *
1330session_lookup_connection (u32 fib_index, ip46_address_t * lcl,
1331 ip46_address_t * rmt, u16 lcl_port, u16 rmt_port,
1332 u8 proto, u8 is_ip4)
1333{
1334 if (is_ip4)
1335 return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4,
1336 lcl_port, rmt_port, proto);
1337 else
1338 return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6,
1339 lcl_port, rmt_port, proto);
1340}
1341
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001342session_error_t
1343vnet_session_rule_add_del (session_rule_add_del_args_t *args)
Florin Coras1c710452017-10-17 00:03:13 -07001344{
1345 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001346 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001347 session_table_t *st;
1348 u32 fib_index;
1349 u8 fib_proto;
Florin Corasc1a42652019-02-08 18:27:29 -08001350 int rv = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001351
1352 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001353 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001354
Florin Coras1c710452017-10-17 00:03:13 -07001355 if (args->scope > 3)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001356 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001357
Florin Corasc97a7392017-11-05 23:07:07 -08001358 if (args->transport_proto != TRANSPORT_PROTO_TCP
1359 && args->transport_proto != TRANSPORT_PROTO_UDP)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001360 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001361
Florin Coras1c710452017-10-17 00:03:13 -07001362 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1363 {
1364 fib_proto = args->table_args.rmt.fp_proto;
1365 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1366 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001367 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001368 if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1369 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001370 }
1371 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1372 {
Dave Barachb7b92992018-10-17 10:38:51 -04001373 clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
Florin Corasa2ff7b82017-11-08 17:55:03 -08001374 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1375 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001376 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001377 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001378 rv = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001379 }
Florin Corasc1a42652019-02-08 18:27:29 -08001380 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001381}
1382
Florin Coras6c36f532017-11-03 18:32:34 -07001383/**
1384 * Mark (global) tables as pertaining to app ns
1385 */
1386void
1387session_lookup_set_tables_appns (app_namespace_t * app_ns)
1388{
1389 session_table_t *st;
1390 u32 fib_index;
1391 u8 fp;
1392
1393 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1394 {
1395 fib_index = app_namespace_get_fib_index (app_ns, fp);
Florin Coras95cd8642019-12-30 21:53:19 -08001396 st = session_table_get_or_alloc (fp, fib_index);
Florin Coras6c36f532017-11-03 18:32:34 -07001397 if (st)
1398 st->appns_index = app_namespace_index (app_ns);
1399 }
1400}
1401
Florin Corascea194d2017-10-02 00:18:51 -07001402u8 *
1403format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1404{
1405 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001406 u32 is_local = va_arg (*args, u32);
Florin Coras15531972018-08-12 23:50:53 -07001407 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
Florin Coras288eaab2019-02-03 15:26:14 -08001408 session_t *session;
Florin Coras15531972018-08-12 23:50:53 -07001409 app_worker_t *app_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -08001410 const u8 *app_name;
1411 u8 *str = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001412
Florin Corascea194d2017-10-02 00:18:51 -07001413 if (!is_local)
1414 {
1415 session = session_get_from_handle (kvp->value);
Florin Coras15531972018-08-12 23:50:53 -07001416 app_wrk = app_worker_get (session->app_wrk_index);
1417 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001418 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1419 key->proto, format_ip4_address, &key->src,
1420 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1421 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001422 s = format (s, "%-40v%-30v", str, app_name);
1423 }
1424 else
1425 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001426 session = session_get_from_handle (kvp->value);
1427 app_wrk = app_worker_get (session->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -07001428 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001429 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1430 format_ip4_address, &key->src,
1431 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001432 s = format (s, "%-30v%-30v", str, app_name);
1433 }
Florin Corascea194d2017-10-02 00:18:51 -07001434 return s;
1435}
1436
1437typedef struct _ip4_session_table_show_ctx_t
1438{
1439 vlib_main_t *vm;
1440 u8 is_local;
1441} ip4_session_table_show_ctx_t;
1442
1443static int
1444ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1445{
1446 ip4_session_table_show_ctx_t *ctx = arg;
1447 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1448 ctx->is_local);
1449 return 1;
1450}
1451
1452void
1453session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1454 u8 type, u8 is_local)
1455{
1456 ip4_session_table_show_ctx_t ctx = {
1457 .vm = vm,
1458 .is_local = is_local,
1459 };
1460 if (!is_local)
1461 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1462 else
1463 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1464 switch (type)
1465 {
1466 /* main table v4 */
1467 case 0:
1468 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1469 &ctx);
1470 break;
1471 default:
1472 clib_warning ("not supported");
1473 }
1474}
Florin Coras66b11312017-07-31 17:18:03 -07001475
Florin Coras1c710452017-10-17 00:03:13 -07001476static clib_error_t *
1477session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1478 vlib_cli_command_t * cmd)
1479{
Florin Corasc97a7392017-11-05 23:07:07 -08001480 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001481 clib_error_t *error = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001482 u32 appns_index, scope = 0;
1483 ip46_address_t lcl_ip, rmt_ip;
1484 u8 is_ip4 = 1, conn_set = 0;
1485 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001486 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001487 app_namespace_t *app_ns;
Florin Corasc1a42652019-02-08 18:27:29 -08001488 int rv;
Florin Coras1c710452017-10-17 00:03:13 -07001489
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001490 session_cli_return_if_not_enabled ();
1491
Dave Barachb7b92992018-10-17 10:38:51 -04001492 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1493 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1495 {
1496 if (unformat (input, "del"))
1497 is_add = 0;
1498 else if (unformat (input, "add"))
1499 ;
1500 else if (unformat (input, "appns %_%v%_", &ns_id))
1501 ;
1502 else if (unformat (input, "scope global"))
1503 scope = SESSION_RULE_SCOPE_GLOBAL;
1504 else if (unformat (input, "scope local"))
1505 scope = SESSION_RULE_SCOPE_LOCAL;
1506 else if (unformat (input, "scope all"))
1507 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1508 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1509 ;
1510 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1511 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1512 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1513 &rmt_port))
1514 {
1515 is_ip4 = 1;
1516 conn_set = 1;
1517 }
1518 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1519 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1520 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1521 &rmt_port))
1522 {
1523 is_ip4 = 0;
1524 conn_set = 1;
1525 }
1526 else if (unformat (input, "action %d", &action))
1527 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001528 else if (unformat (input, "tag %_%v%_", &tag))
1529 ;
Florin Coras1c710452017-10-17 00:03:13 -07001530 else
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001531 {
1532 error = clib_error_return (0, "unknown input `%U'",
1533 format_unformat_error, input);
1534 goto done;
1535 }
Florin Coras1c710452017-10-17 00:03:13 -07001536 }
1537
Florin Corasc97a7392017-11-05 23:07:07 -08001538 if (proto == ~0)
1539 {
1540 vlib_cli_output (vm, "proto must be set");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001541 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001542 }
1543 if (is_add && !conn_set && action == ~0)
1544 {
1545 vlib_cli_output (vm, "connection and action must be set for add");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001546 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001547 }
1548 if (!is_add && !tag && !conn_set)
1549 {
1550 vlib_cli_output (vm, "connection or tag must be set for delete");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001551 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001552 }
1553 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1554 {
1555 vlib_cli_output (vm, "tag too long (max u64)");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001556 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001557 }
Florin Coras1c710452017-10-17 00:03:13 -07001558
1559 if (ns_id)
1560 {
1561 app_ns = app_namespace_get_from_id (ns_id);
1562 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001563 {
1564 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001565 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001566 }
Florin Coras1c710452017-10-17 00:03:13 -07001567 }
1568 else
1569 {
1570 app_ns = app_namespace_get_default ();
1571 }
1572 appns_index = app_namespace_index (app_ns);
1573
1574 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1575 session_rule_add_del_args_t args = {
zhanglimaoa04ebb82019-03-13 10:36:54 +08001576 .transport_proto = proto,
Florin Coras1c710452017-10-17 00:03:13 -07001577 .table_args.lcl.fp_addr = lcl_ip,
1578 .table_args.lcl.fp_len = lcl_plen,
1579 .table_args.lcl.fp_proto = fib_proto,
1580 .table_args.rmt.fp_addr = rmt_ip,
1581 .table_args.rmt.fp_len = rmt_plen,
1582 .table_args.rmt.fp_proto = fib_proto,
1583 .table_args.lcl_port = lcl_port,
1584 .table_args.rmt_port = rmt_port,
1585 .table_args.action_index = action,
1586 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001587 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001588 .appns_index = appns_index,
1589 .scope = scope,
1590 };
Florin Corasc1a42652019-02-08 18:27:29 -08001591 if ((rv = vnet_session_rule_add_del (&args)))
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001592 error = clib_error_return (0, "rule add del returned %u", rv);
Florin Corasc1a42652019-02-08 18:27:29 -08001593
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001594done:
1595 vec_free (ns_id);
Florin Corasc97a7392017-11-05 23:07:07 -08001596 vec_free (tag);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001597 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001598}
1599
1600/* *INDENT-OFF* */
1601VLIB_CLI_COMMAND (session_rule_command, static) =
1602{
1603 .path = "session rule",
1604 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1605 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1606 .function = session_rule_command_fn,
1607};
1608/* *INDENT-ON* */
1609
Florin Coras7999e832017-10-31 01:51:04 -07001610void
1611session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1612 u8 transport_proto)
1613{
1614 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001615 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001616 session_table_t *st;
1617 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001618 srt = &st->session_rules[transport_proto];
1619 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001620}
1621
1622void
1623session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1624 u8 transport_proto)
1625{
1626 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001627 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001628 session_table_t *st;
1629 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001630 srt = &st->session_rules[transport_proto];
1631 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001632}
1633
Florin Coras1c710452017-10-17 00:03:13 -07001634static clib_error_t *
1635show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1636 vlib_cli_command_t * cmd)
1637{
1638 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1639 u32 fib_index, scope = 0;
1640 ip46_address_t lcl_ip, rmt_ip;
1641 u8 is_ip4 = 1, show_one = 0;
1642 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001643 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001644 session_table_t *st;
1645 u8 *ns_id = 0, fib_proto;
1646
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001647 session_cli_return_if_not_enabled ();
1648
Dave Barachb7b92992018-10-17 10:38:51 -04001649 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1650 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001651 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1652 {
1653 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1654 ;
1655 else if (unformat (input, "appns %_%v%_", &ns_id))
1656 ;
1657 else if (unformat (input, "scope global"))
1658 scope = 1;
1659 else if (unformat (input, "scope local"))
1660 scope = 2;
1661 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1662 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1663 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1664 &rmt_port))
1665 {
1666 is_ip4 = 1;
1667 show_one = 1;
1668 }
1669 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1670 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1671 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1672 &rmt_port))
1673 {
1674 is_ip4 = 0;
1675 show_one = 1;
1676 }
1677 else
1678 return clib_error_return (0, "unknown input `%U'",
1679 format_unformat_error, input);
1680 }
1681
1682 if (transport_proto == ~0)
1683 {
1684 vlib_cli_output (vm, "transport proto must be set");
1685 return 0;
1686 }
1687
1688 if (ns_id)
1689 {
1690 app_ns = app_namespace_get_from_id (ns_id);
1691 if (!app_ns)
1692 {
1693 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1694 return 0;
1695 }
1696 }
1697 else
1698 {
1699 app_ns = app_namespace_get_default ();
1700 }
1701
1702 if (scope == 1 || scope == 0)
1703 {
1704 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1705 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1706 st = session_table_get_for_fib_index (fib_proto, fib_index);
1707 }
1708 else
1709 {
1710 st = app_namespace_get_local_table (app_ns);
1711 }
1712
1713 if (show_one)
1714 {
Florin Corasc97a7392017-11-05 23:07:07 -08001715 srt = &st->session_rules[transport_proto];
1716 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1717 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001718 return 0;
1719 }
1720
Florin Corasc97a7392017-11-05 23:07:07 -08001721 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1722 transport_proto);
1723 srt = &st->session_rules[transport_proto];
1724 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1725 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001726
1727 vec_free (ns_id);
1728 return 0;
1729}
1730
1731/* *INDENT-OFF* */
1732VLIB_CLI_COMMAND (show_session_rules_command, static) =
1733{
1734 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001735 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1736 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001737 .function = show_session_rules_command_fn,
1738};
1739/* *INDENT-ON* */
1740
Florin Coras894d0a62023-11-17 16:35:04 -08001741u8 *
1742format_session_lookup_tables (u8 *s, va_list *args)
1743{
1744 u32 fib_proto = va_arg (*args, u32);
1745 u32 *fibs, num_fibs = 0, fib_index, indent;
1746 session_table_t *st;
1747 u64 total_mem = 0;
1748
1749 fibs = fib_index_to_table_index[fib_proto];
1750
1751 for (fib_index = 0; fib_index < vec_len (fibs); fib_index++)
1752 {
1753 if (fibs[fib_index] == ~0)
1754 continue;
1755
1756 num_fibs += 1;
1757 st = session_table_get (fibs[fib_index]);
1758 total_mem += session_table_memory_size (st);
1759 }
1760
1761 indent = format_get_indent (s);
1762 s = format (s, "active fibs:\t%u\n", num_fibs);
1763 s = format (s, "%Umax fib-index:\t%u\n", format_white_space, indent,
1764 vec_len (fibs) - 1);
1765 s = format (s, "%Utable memory:\t%U\n", format_white_space, indent,
1766 format_memory_size, total_mem);
1767 s = format (s, "%Uvec memory:\t%U\n", format_white_space, indent,
1768 format_memory_size, vec_mem_size (fibs));
1769
1770 return s;
1771}
1772
1773static clib_error_t *
1774show_session_lookup_command_fn (vlib_main_t *vm, unformat_input_t *input,
1775 vlib_cli_command_t *cmd)
1776{
1777 session_table_t *st;
1778 u32 fib_index = ~0;
1779
1780 session_cli_return_if_not_enabled ();
1781 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1782 {
1783 if (unformat (input, "table %u", &fib_index))
1784 ;
1785 else
1786 return clib_error_return (0, "unknown input `%U'",
1787 format_unformat_error, input);
1788 }
1789
1790 if (fib_index != ~0)
1791 {
1792 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1793 if (st)
1794 vlib_cli_output (vm, "%U", format_session_table, st);
1795 else
1796 vlib_cli_output (vm, "no ip4 table for fib-index %u", fib_index);
1797 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1798 if (st)
1799 vlib_cli_output (vm, "%U", format_session_table, st);
1800 else
1801 vlib_cli_output (vm, "no ip6 table for fib-index %u", fib_index);
1802 goto done;
1803 }
1804
1805 vlib_cli_output (vm, "ip4 fib lookup tables:\n %U",
1806 format_session_lookup_tables, FIB_PROTOCOL_IP4);
1807 vlib_cli_output (vm, "ip6 fib lookup tables:\n %U",
1808 format_session_lookup_tables, FIB_PROTOCOL_IP6);
1809
1810done:
1811 return 0;
1812}
1813
1814VLIB_CLI_COMMAND (show_session_lookup_command, static) = {
1815 .path = "show session lookup",
1816 .short_help = "show session lookup [table <fib-index>]",
1817 .function = show_session_lookup_command_fn,
1818};
1819
Florin Coras04e53442017-07-16 17:12:15 -07001820void
1821session_lookup_init (void)
1822{
Florin Corasf9bdc032024-02-02 13:01:07 -08001823 session_lookup_main_t *slm = &sl_main;
1824
1825 clib_spinlock_init (&slm->st_alloc_lock);
1826
Florin Corascea194d2017-10-02 00:18:51 -07001827 /*
1828 * Allocate default table and map it to fib_index 0
1829 */
1830 session_table_t *st = session_table_alloc ();
1831 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1832 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001833 st->active_fib_proto = FIB_PROTOCOL_IP4;
1834 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001835 st = session_table_alloc ();
1836 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1837 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001838 st->active_fib_proto = FIB_PROTOCOL_IP6;
1839 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001840}
1841
1842/*
1843 * fd.io coding-style-patch-verification: ON
1844 *
1845 * Local Variables:
1846 * eval: (c-set-style "gnu")
1847 * End:
1848 */