blob: ff20bc2d83530e262c15231c54bcbab8a0506563 [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/* 16 octets */
41typedef CLIB_PACKED (struct {
42 union
43 {
44 struct
45 {
46 ip4_address_t src;
47 ip4_address_t dst;
48 u16 src_port;
49 u16 dst_port;
50 /* align by making this 4 octets even though its a 1-bit field
51 * NOTE: avoid key overlap with other transports that use 5 tuples for
52 * session identification.
53 */
54 u32 proto;
55 };
56 u64 as_u64[2];
57 };
58}) v4_connection_key_t;
59
60typedef CLIB_PACKED (struct {
61 union
62 {
63 struct
64 {
65 /* 48 octets */
66 ip6_address_t src;
67 ip6_address_t dst;
68 u16 src_port;
69 u16 dst_port;
70 u32 proto;
71 u64 unused;
72 };
73 u64 as_u64[6];
74 };
75}) v6_connection_key_t;
Florin Coras04e53442017-07-16 17:12:15 -070076
77typedef clib_bihash_kv_16_8_t session_kv4_t;
78typedef clib_bihash_kv_48_8_t session_kv6_t;
79
80always_inline void
81make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
82 u16 lcl_port, u16 rmt_port, u8 proto)
83{
Florin Coras9679c812018-06-21 08:14:34 -070084 kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
85 kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070086 kv->value = ~0ULL;
87}
88
89always_inline void
90make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
91 u8 proto)
92{
Florin Coras9679c812018-06-21 08:14:34 -070093 kv->key[0] = (u64) lcl->as_u32;
94 kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
Florin Coras04e53442017-07-16 17:12:15 -070095 kv->value = ~0ULL;
96}
97
98always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -080099make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
100{
Florin Coras9679c812018-06-21 08:14:34 -0700101 kv->key[0] = (u64) lcl->as_u32;
102 kv->key[1] = (u64) proto << 32;
Florin Corasdbd44562017-11-09 19:30:17 -0800103 kv->value = ~0ULL;
104}
105
106always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800107make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700108{
Florin Coras561af9b2017-12-09 10:19:43 -0800109 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
110 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700111}
112
113always_inline void
114make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
115 u16 lcl_port, u16 rmt_port, u8 proto)
116{
Florin Coras9679c812018-06-21 08:14:34 -0700117 kv->key[0] = lcl->as_u64[0];
118 kv->key[1] = lcl->as_u64[1];
119 kv->key[2] = rmt->as_u64[0];
120 kv->key[3] = rmt->as_u64[1];
121 kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
122 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700123 kv->value = ~0ULL;
124}
125
126always_inline void
127make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
128 u8 proto)
129{
Florin Coras9679c812018-06-21 08:14:34 -0700130 kv->key[0] = lcl->as_u64[0];
131 kv->key[1] = lcl->as_u64[1];
132 kv->key[2] = 0;
133 kv->key[3] = 0;
134 kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
135 kv->key[5] = 0;
Florin Coras04e53442017-07-16 17:12:15 -0700136 kv->value = ~0ULL;
137}
138
139always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800140make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
141{
Florin Coras9679c812018-06-21 08:14:34 -0700142 kv->key[0] = lcl->as_u64[0];
143 kv->key[1] = lcl->as_u64[1];
144 kv->key[2] = 0;
145 kv->key[3] = 0;
146 kv->key[4] = (u64) proto << 32;
147 kv->key[5] = 0;
Florin Corasdbd44562017-11-09 19:30:17 -0800148 kv->value = ~0ULL;
149}
150
151always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800152make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700153{
Florin Coras561af9b2017-12-09 10:19:43 -0800154 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
155 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700156}
157
Florin Corasf9bdc032024-02-02 13:01:07 -0800158static inline u8
159session_table_alloc_needs_sync (void)
160{
161 return !vlib_thread_is_main_w_barrier () && (vlib_num_workers () > 1);
162}
163
Florin Coras5bd96b72024-03-20 16:31:38 -0700164static_always_inline u8
165session_table_is_alloced (u8 fib_proto, u32 fib_index)
166{
167 return (vec_len (fib_index_to_table_index[fib_proto]) > fib_index &&
168 fib_index_to_table_index[fib_proto][fib_index] != ~0);
169}
170
Florin Corascea194d2017-10-02 00:18:51 -0700171static session_table_t *
Andreas Schultz30a28c12020-04-23 10:41:50 +0200172session_table_get_or_alloc (u8 fib_proto, u32 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700173{
Florin Corascea194d2017-10-02 00:18:51 -0700174 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700175 u32 table_index;
Florin Coras5bd96b72024-03-20 16:31:38 -0700176
Andreas Schultz30a28c12020-04-23 10:41:50 +0200177 ASSERT (fib_index != ~0);
Florin Coras5bd96b72024-03-20 16:31:38 -0700178
179 if (session_table_is_alloced (fib_proto, fib_index))
Andreas Schultz30a28c12020-04-23 10:41:50 +0200180 {
181 table_index = fib_index_to_table_index[fib_proto][fib_index];
182 return session_table_get (table_index);
183 }
Florin Coras5bd96b72024-03-20 16:31:38 -0700184
185 u8 needs_sync = session_table_alloc_needs_sync ();
186 session_lookup_main_t *slm = &sl_main;
187
188 /* Stop workers, otherwise consumers might be affected. This is
189 * acceptable because new tables should seldom be allocated */
190 if (needs_sync)
191 {
192 vlib_workers_sync ();
193
194 /* We might have a race, only one worker allowed at once */
195 clib_spinlock_lock (&slm->st_alloc_lock);
196 }
197
198 /* Another worker just allocated this table */
199 if (session_table_is_alloced (fib_proto, fib_index))
200 {
201 table_index = fib_index_to_table_index[fib_proto][fib_index];
202 st = session_table_get (table_index);
203 }
Andreas Schultz30a28c12020-04-23 10:41:50 +0200204 else
Florin Corascea194d2017-10-02 00:18:51 -0700205 {
206 st = session_table_alloc ();
Florin Coras6c36f532017-11-03 18:32:34 -0700207 st->active_fib_proto = fib_proto;
Florin Corasb795bd02017-12-14 11:30:48 -0800208 session_table_init (st, fib_proto);
Florin Coras5bd96b72024-03-20 16:31:38 -0700209 vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index,
210 ~0);
211 table_index = session_table_index (st);
212 fib_index_to_table_index[fib_proto][fib_index] = table_index;
Florin Corascea194d2017-10-02 00:18:51 -0700213 }
Florin Coras5bd96b72024-03-20 16:31:38 -0700214
215 if (needs_sync)
216 {
217 clib_spinlock_unlock (&slm->st_alloc_lock);
218 vlib_workers_continue ();
219 }
220
221 return st;
Florin Corascea194d2017-10-02 00:18:51 -0700222}
223
224static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700225session_table_get_or_alloc_for_connection (transport_connection_t * tc)
226{
227 u32 fib_proto;
228 fib_proto = transport_connection_fib_proto (tc);
229 return session_table_get_or_alloc (fib_proto, tc->fib_index);
230}
231
232static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700233session_table_get_for_connection (transport_connection_t * tc)
234{
235 u32 fib_proto = transport_connection_fib_proto (tc);
236 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
237 return 0;
238 return
239 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
240}
241
242static session_table_t *
243session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
244{
245 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
246 return 0;
247 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
248}
249
250u32
251session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
252{
253 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
254 return SESSION_TABLE_INVALID_INDEX;
255 return fib_index_to_table_index[fib_proto][fib_index];
256}
257
Florin Corasa4d09562021-07-08 08:25:09 -0700258u32
259session_lookup_get_or_alloc_index_for_fib (u32 fib_proto, u32 fib_index)
260{
261 session_table_t *st;
262 st = session_table_get_or_alloc (fib_proto, fib_index);
263 return session_table_index (st);
264}
265
Florin Corascea194d2017-10-02 00:18:51 -0700266/**
267 * Add transport connection to a session table
268 *
269 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
270 * is added to requested session table.
271 *
272 * @param tc transport connection to be added
273 * @param value value to be stored
274 *
275 * @return non-zero if failure
276 */
277int
278session_lookup_add_connection (transport_connection_t * tc, u64 value)
279{
280 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700281 session_kv4_t kv4;
282 session_kv6_t kv6;
283
Florin Corascea194d2017-10-02 00:18:51 -0700284 st = session_table_get_or_alloc_for_connection (tc);
285 if (!st)
286 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700287 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700288 {
Florin Coras04e53442017-07-16 17:12:15 -0700289 make_v4_ss_kv_from_tc (&kv4, tc);
290 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700291 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
292 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700293 }
294 else
295 {
Florin Coras04e53442017-07-16 17:12:15 -0700296 make_v6_ss_kv_from_tc (&kv6, tc);
297 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700298 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
299 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700300 }
301}
302
Florin Coras04e53442017-07-16 17:12:15 -0700303int
Florin Corascea194d2017-10-02 00:18:51 -0700304session_lookup_add_session_endpoint (u32 table_index,
305 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700306{
Florin Corascea194d2017-10-02 00:18:51 -0700307 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700308 session_kv4_t kv4;
309 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700310
Florin Corascea194d2017-10-02 00:18:51 -0700311 st = session_table_get (table_index);
312 if (!st)
313 return -1;
314 if (sep->is_ip4)
315 {
316 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
317 sep->transport_proto);
318 kv4.value = value;
319 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
320 }
321 else
322 {
323 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
324 sep->transport_proto);
325 kv6.value = value;
326 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
327 }
328}
329
330int
331session_lookup_del_session_endpoint (u32 table_index,
332 session_endpoint_t * sep)
333{
334 session_table_t *st;
335 session_kv4_t kv4;
336 session_kv6_t kv6;
337
338 st = session_table_get (table_index);
339 if (!st)
340 return -1;
341 if (sep->is_ip4)
342 {
343 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
344 sep->transport_proto);
345 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
346 }
347 else
348 {
349 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
350 sep->transport_proto);
351 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
352 }
353}
354
Florin Corasaab06042020-02-26 02:56:14 +0000355int
356session_lookup_del_session_endpoint2 (session_endpoint_t * sep)
357{
358 fib_protocol_t fib_proto;
359 session_table_t *st;
360 session_kv4_t kv4;
361 session_kv6_t kv6;
362
363 fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
364 st = session_table_get_for_fib_index (fib_proto, sep->fib_index);
365 if (!st)
366 return -1;
367 if (sep->is_ip4)
368 {
369 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
370 sep->transport_proto);
371 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
372 }
373 else
374 {
375 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
376 sep->transport_proto);
377 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
378 }
379}
380
Florin Corascea194d2017-10-02 00:18:51 -0700381/**
382 * Delete transport connection from session table
383 *
384 * @param table_index session table index
385 * @param tc transport connection to be removed
386 *
387 * @return non-zero if failure
388 */
389int
390session_lookup_del_connection (transport_connection_t * tc)
391{
392 session_table_t *st;
393 session_kv4_t kv4;
394 session_kv6_t kv6;
395
396 st = session_table_get_for_connection (tc);
397 if (!st)
398 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700399 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700400 {
Florin Coras04e53442017-07-16 17:12:15 -0700401 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700402 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700403 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700404 }
405 else
406 {
Florin Coras04e53442017-07-16 17:12:15 -0700407 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700408 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700409 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700410 }
Florin Coras04e53442017-07-16 17:12:15 -0700411}
412
413int
Florin Coras288eaab2019-02-03 15:26:14 -0800414session_lookup_del_session (session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700415{
416 transport_connection_t *ts;
Florin Coras1ee78302019-02-05 15:51:15 -0800417 ts = transport_get_connection (session_get_transport_proto (s),
418 s->connection_index, s->thread_index);
Florin Corase1e7fb82019-09-25 07:28:34 -0700419 if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200420 return 0;
Florin Corascea194d2017-10-02 00:18:51 -0700421 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700422}
423
Florin Corasdff48db2017-11-19 18:06:58 -0800424static u8
425session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700426{
Florin Corasdff48db2017-11-19 18:06:58 -0800427 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
428 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
429 return 0;
430 return 1;
431}
432
Florin Corasf8f516a2018-02-08 15:10:09 -0800433static u64
434session_lookup_action_to_handle (u32 action_index)
Florin Corasdff48db2017-11-19 18:06:58 -0800435{
436 switch (action_index)
437 {
438 case SESSION_RULES_TABLE_ACTION_DROP:
Florin Corasf8f516a2018-02-08 15:10:09 -0800439 return SESSION_DROP_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800440 case SESSION_RULES_TABLE_ACTION_ALLOW:
441 case SESSION_RULES_TABLE_INVALID_INDEX:
Florin Corasf8f516a2018-02-08 15:10:09 -0800442 return SESSION_INVALID_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800443 default:
Florin Corasf8f516a2018-02-08 15:10:09 -0800444 /* application index */
Florin Corasdff48db2017-11-19 18:06:58 -0800445 return action_index;
446 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700447}
448
Florin Coras288eaab2019-02-03 15:26:14 -0800449static session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700450session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
451 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700452{
453 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800454 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700455 if (!app)
456 return 0;
457
Florin Coras15531972018-08-12 23:50:53 -0700458 return app_worker_first_listener (application_get_default_worker (app),
459 fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700460}
461
Florin Coras288eaab2019-02-03 15:26:14 -0800462static session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800463session_lookup_action_to_session (u32 action_index, u8 fib_proto,
464 u8 transport_proto)
465{
Florin Corasdff48db2017-11-19 18:06:58 -0800466 u32 app_index;
Florin Corasf8f516a2018-02-08 15:10:09 -0800467 app_index = session_lookup_action_to_handle (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800468 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800469 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800470 transport_proto);
471}
472
Florin Corasf8f516a2018-02-08 15:10:09 -0800473/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800474session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800475session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
476 ip4_address_t * lcl, u16 lcl_port,
477 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700478{
Florin Corasc97a7392017-11-05 23:07:07 -0800479 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800480 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800481 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700482 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800483 app_index = session_lookup_action_to_handle (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700484 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800485 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700486 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700487}
488
Florin Corasf8f516a2018-02-08 15:10:09 -0800489/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800490session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800491session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
492 ip6_address_t * lcl, u16 lcl_port,
493 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700494{
Florin Corasc97a7392017-11-05 23:07:07 -0800495 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800496 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800497 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700498 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800499 app_index = session_lookup_action_to_handle (action_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800500 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700501 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700502}
503
Florin Corasa2ff7b82017-11-08 17:55:03 -0800504/**
505 * Lookup listener for session endpoint in table
506 *
507 * @param table_index table where the endpoint should be looked up
508 * @param sep session endpoint to be looked up
509 * @param use_rules flag that indicates if the session rules of the table
510 * should be used
511 * @return invalid handle if nothing is found, the handle of a valid listener
Florin Corasf8f516a2018-02-08 15:10:09 -0800512 * or an action derived handle if a rule is hit
Florin Corasa2ff7b82017-11-08 17:55:03 -0800513 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700514u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800515session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
516 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700517{
Florin Corasc97a7392017-11-05 23:07:07 -0800518 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700519 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700520 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700521 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700522
Florin Corascea194d2017-10-02 00:18:51 -0700523 st = session_table_get (table_index);
524 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700525 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700526 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700527 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800528 session_kv4_t kv4;
529 ip4_address_t lcl4;
530
Florin Coras561af9b2017-12-09 10:19:43 -0800531 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
532 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700533 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
534 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700535 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800536 if (use_rules)
537 {
Dave Barachb7b92992018-10-17 10:38:51 -0400538 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800539 srt = &st->session_rules[sep->transport_proto];
540 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
541 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800542 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800543 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800544 }
Florin Coras68810622017-07-24 17:40:28 -0700545 }
546 else
547 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800548 session_kv6_t kv6;
549 ip6_address_t lcl6;
550
Florin Coras561af9b2017-12-09 10:19:43 -0800551 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
552 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700553 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
554 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700555 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700556
Florin Corasa2ff7b82017-11-08 17:55:03 -0800557 if (use_rules)
558 {
Dave Barachb7b92992018-10-17 10:38:51 -0400559 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800560 srt = &st->session_rules[sep->transport_proto];
561 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
562 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800563 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800564 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800565 }
Florin Coras68810622017-07-24 17:40:28 -0700566 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700567 return SESSION_INVALID_HANDLE;
568}
569
Florin Corasa2ff7b82017-11-08 17:55:03 -0800570/**
571 * Look up endpoint in local session table
572 *
573 * The result, for now, is an application index and it may in the future
574 * be extended to a more complicated "action object". The only action we
575 * emulate now is "drop" and for that we return a special app index.
576 *
577 * Lookup logic is to check in order:
578 * - the rules in the table (connect acls)
579 * - session sub-table for a listener
580 * - session sub-table for a local listener (zeroed addr)
581 *
582 * @param table_index table where the lookup should be done
583 * @param sep session endpoint to be looked up
Florin Corasf8f516a2018-02-08 15:10:09 -0800584 * @return session handle that can be interpreted as an adjacency
Florin Corasa2ff7b82017-11-08 17:55:03 -0800585 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800586u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800587session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700588{
Florin Corasc97a7392017-11-05 23:07:07 -0800589 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700590 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700591 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700592 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700593
Florin Corascea194d2017-10-02 00:18:51 -0700594 st = session_table_get (table_index);
595 if (!st)
596 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800597 ASSERT (st->is_local);
598
Florin Corascea194d2017-10-02 00:18:51 -0700599 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700600 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800601 session_kv4_t kv4;
602 ip4_address_t lcl4;
603
604 /*
605 * Check if endpoint has special rules associated
606 */
Dave Barachb7b92992018-10-17 10:38:51 -0400607 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800608 srt = &st->session_rules[sep->transport_proto];
609 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
610 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800611 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800612 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800613
614 /*
615 * Check if session endpoint is a listener
616 */
Florin Corascea194d2017-10-02 00:18:51 -0700617 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
618 sep->transport_proto);
619 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
620 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800621 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700622
623 /*
624 * Zero out the ip. Logic is that connect to local ips, say
625 * 127.0.0.1:port, can match 0.0.0.0:port
626 */
Florin Coras477e91a2018-02-27 10:05:57 -0800627 if (ip4_is_local_host (&sep->ip.ip4))
628 {
629 kv4.key[0] = 0;
630 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
631 if (rv == 0)
632 return kv4.value;
633 }
634 else
635 {
636 kv4.key[0] = 0;
637 }
Florin Corasdbd44562017-11-09 19:30:17 -0800638
639 /*
640 * Zero out the port and check if we have proxy
641 */
642 kv4.key[1] = 0;
643 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
644 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800645 return kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700646 }
647 else
648 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800649 session_kv6_t kv6;
650 ip6_address_t lcl6;
651
Dave Barachb7b92992018-10-17 10:38:51 -0400652 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800653 srt = &st->session_rules[sep->transport_proto];
654 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
655 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800656 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800657 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800658
Florin Corascea194d2017-10-02 00:18:51 -0700659 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
660 sep->transport_proto);
661 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
662 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800663 return kv6.value;
Florin Corascea194d2017-10-02 00:18:51 -0700664
665 /*
666 * Zero out the ip. Same logic as above.
667 */
Florin Coras477e91a2018-02-27 10:05:57 -0800668
669 if (ip6_is_local_host (&sep->ip.ip6))
670 {
671 kv6.key[0] = kv6.key[1] = 0;
672 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
673 if (rv == 0)
674 return kv6.value;
675 }
676 else
677 {
678 kv6.key[0] = kv6.key[1] = 0;
679 }
Florin Corasdbd44562017-11-09 19:30:17 -0800680
681 /*
682 * Zero out the port. Same logic as above.
683 */
684 kv6.key[4] = kv6.key[5] = 0;
685 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
686 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800687 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700688 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800689 return SESSION_INVALID_HANDLE;
Florin Coras04e53442017-07-16 17:12:15 -0700690}
691
Florin Coras288eaab2019-02-03 15:26:14 -0800692static inline session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700693session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800694 u16 lcl_port, u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700695{
Florin Coras04e53442017-07-16 17:12:15 -0700696 session_kv4_t kv4;
697 int rv;
698
Florin Corasdbd44562017-11-09 19:30:17 -0800699 /*
700 * First, try a fully formed listener
701 */
Florin Coras04e53442017-07-16 17:12:15 -0700702 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700703 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700704 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700705 return listen_session_get ((u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700706
Florin Corasdbd44562017-11-09 19:30:17 -0800707 /*
708 * Zero out the lcl ip and check if any 0/0 port binds have been done
709 */
Florin Coras477e91a2018-02-27 10:05:57 -0800710 if (use_wildcard)
711 {
712 kv4.key[0] = 0;
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 Coras477e91a2018-02-27 10:05:57 -0800716 }
717 else
718 {
719 kv4.key[0] = 0;
720 }
Florin Coras04e53442017-07-16 17:12:15 -0700721
Florin Corasdbd44562017-11-09 19:30:17 -0800722 /*
723 * Zero out port and check if we have a proxy set up for our ip
724 */
725 make_v4_proxy_kv (&kv4, lcl, proto);
726 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
727 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700728 return listen_session_get ((u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800729
Florin Coras04e53442017-07-16 17:12:15 -0700730 return 0;
731}
732
Florin Coras288eaab2019-02-03 15:26:14 -0800733session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700734session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800735 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700736{
Florin Corascea194d2017-10-02 00:18:51 -0700737 session_table_t *st;
738 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
739 if (!st)
740 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800741 return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700742}
743
Florin Coras288eaab2019-02-03 15:26:14 -0800744static session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700745session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800746 u16 lcl_port, u8 proto, u8 ip_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700747{
Florin Coras04e53442017-07-16 17:12:15 -0700748 session_kv6_t kv6;
749 int rv;
750
751 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700752 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700753 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700754 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700755
756 /* Zero out the lcl ip */
Florin Coras477e91a2018-02-27 10:05:57 -0800757 if (ip_wildcard)
758 {
759 kv6.key[0] = kv6.key[1] = 0;
760 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
761 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700762 return listen_session_get ((u32) kv6.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800763 }
764 else
765 {
766 kv6.key[0] = kv6.key[1] = 0;
767 }
Florin Coras04e53442017-07-16 17:12:15 -0700768
Florin Corasdbd44562017-11-09 19:30:17 -0800769 make_v6_proxy_kv (&kv6, lcl, proto);
770 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
771 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700772 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700773 return 0;
774}
775
Florin Coras288eaab2019-02-03 15:26:14 -0800776session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700777session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800778 u8 proto, u8 use_wildcard)
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_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
782 if (!st)
783 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800784 return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700785}
786
Florin Coras477e91a2018-02-27 10:05:57 -0800787/**
788 * Lookup listener, exact or proxy (inaddr_any:0) match
789 */
Florin Coras288eaab2019-02-03 15:26:14 -0800790session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700791session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700792{
Florin Corascea194d2017-10-02 00:18:51 -0700793 session_table_t *st;
794 st = session_table_get (table_index);
795 if (!st)
796 return 0;
797 if (sep->is_ip4)
798 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800799 sep->transport_proto, 0);
Florin Corascea194d2017-10-02 00:18:51 -0700800 else
801 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800802 sep->transport_proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700803 return 0;
804}
805
Florin Coras9f1a5432019-03-10 21:03:20 -0700806/**
807 * Lookup listener wildcard match
808 */
809session_t *
810session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep)
811{
812 session_table_t *st;
813 st = session_table_get (table_index);
814 if (!st)
815 return 0;
816 if (sep->is_ip4)
817 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
818 sep->transport_proto,
819 1 /* use_wildcard */ );
820 else
821 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
822 sep->transport_proto,
823 1 /* use_wildcard */ );
824 return 0;
825}
826
Florin Corascea194d2017-10-02 00:18:51 -0700827int
828session_lookup_add_half_open (transport_connection_t * tc, u64 value)
829{
830 session_table_t *st;
831 session_kv4_t kv4;
832 session_kv6_t kv6;
833
834 st = session_table_get_or_alloc_for_connection (tc);
835 if (!st)
836 return 0;
837 if (tc->is_ip4)
838 {
839 make_v4_ss_kv_from_tc (&kv4, tc);
840 kv4.value = value;
841 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
842 1 /* is_add */ );
843 }
844 else
845 {
846 make_v6_ss_kv_from_tc (&kv6, tc);
847 kv6.value = value;
848 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
849 1 /* is_add */ );
850 }
851}
852
853int
854session_lookup_del_half_open (transport_connection_t * tc)
855{
856 session_table_t *st;
857 session_kv4_t kv4;
858 session_kv6_t kv6;
859
860 st = session_table_get_for_connection (tc);
861 if (!st)
862 return -1;
863 if (tc->is_ip4)
864 {
865 make_v4_ss_kv_from_tc (&kv4, tc);
866 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
867 0 /* is_add */ );
868 }
869 else
870 {
871 make_v6_ss_kv_from_tc (&kv6, tc);
872 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
873 0 /* is_add */ );
874 }
875}
876
Florin Coras04e53442017-07-16 17:12:15 -0700877u64
Florin Corascea194d2017-10-02 00:18:51 -0700878session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700879{
Florin Corascea194d2017-10-02 00:18:51 -0700880 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700881 session_kv4_t kv4;
882 session_kv6_t kv6;
883 int rv;
884
Florin Corascea194d2017-10-02 00:18:51 -0700885 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
886 tc->fib_index);
887 if (!st)
888 return HALF_OPEN_LOOKUP_INVALID_VALUE;
889 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700890 {
Florin Corascea194d2017-10-02 00:18:51 -0700891 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700892 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700893 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700894 if (rv == 0)
895 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700896 }
897 else
898 {
899 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700900 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700901 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700902 if (rv == 0)
903 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700904 }
905 return HALF_OPEN_LOOKUP_INVALID_VALUE;
906}
907
908transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700909session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700910{
Florin Coras04e53442017-07-16 17:12:15 -0700911 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700912 {
Florin Coras1ee78302019-02-05 15:51:15 -0800913 u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
914 return transport_get_half_open (sst, handle & 0xFFFFFFFF);
Florin Corascea194d2017-10-02 00:18:51 -0700915 }
Florin Coras04e53442017-07-16 17:12:15 -0700916 return 0;
917}
918
Florin Corascea194d2017-10-02 00:18:51 -0700919/**
920 * Lookup connection with ip4 and transport layer information
921 *
922 * This is used on the fast path so it needs to be fast. Thereby,
923 * duplication of code and 'hacks' allowed.
924 *
925 * The lookup is incremental and returns whenever something is matched. The
926 * steps are:
927 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700928 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700929 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800930 * - Try to find a fully-formed or local source wildcarded (listener bound to
931 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700932 * - return 0
933 *
934 * @param fib_index index of fib wherein the connection was received
935 * @param lcl local ip4 address
936 * @param rmt remote ip4 address
937 * @param lcl_port local port
938 * @param rmt_port remote port
939 * @param proto transport protocol (e.g., tcp, udp)
940 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800941 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700942 *
943 * @return pointer to transport connection, if one is found, 0 otherwise
944 */
Florin Coras04e53442017-07-16 17:12:15 -0700945transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700946session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
947 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800948 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -0800949 u8 * result)
Florin Coras04e53442017-07-16 17:12:15 -0700950{
Florin Corascea194d2017-10-02 00:18:51 -0700951 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700952 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800953 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800954 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700955 int rv;
956
Florin Corascea194d2017-10-02 00:18:51 -0700957 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
958 if (PREDICT_FALSE (!st))
959 return 0;
960
Florin Corasa2ff7b82017-11-08 17:55:03 -0800961 /*
962 * Lookup session amongst established ones
963 */
Florin Coras04e53442017-07-16 17:12:15 -0700964 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700965 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700966 if (rv == 0)
967 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800968 if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
969 {
970 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
971 return 0;
972 }
Florin Corascea194d2017-10-02 00:18:51 -0700973 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -0800974 return transport_get_connection (proto, s->connection_index,
975 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700976 }
977
Florin Corasa2ff7b82017-11-08 17:55:03 -0800978 /*
979 * Try half-open connections
980 */
Florin Corascea194d2017-10-02 00:18:51 -0700981 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700982 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -0800983 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700984
Florin Corasa2ff7b82017-11-08 17:55:03 -0800985 /*
986 * Check the session rules table
987 */
988 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
989 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800990 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800991 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800992 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
993 {
994 *result = SESSION_LOOKUP_RESULT_FILTERED;
995 return 0;
996 }
Florin Corasdff48db2017-11-19 18:06:58 -0800997 if ((s = session_lookup_action_to_session (action_index,
998 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -0800999 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001000 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001001 }
1002
1003 /*
1004 * If nothing is found, check if any listener is available
1005 */
Florin Coras477e91a2018-02-27 10:05:57 -08001006 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001007 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001008 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001009
1010 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001011}
1012
Florin Corascea194d2017-10-02 00:18:51 -07001013/**
1014 * Lookup connection with ip4 and transport layer information
1015 *
Florin Corasb5e55a22019-01-10 12:42:47 -08001016 * Not optimized. Lookup logic is identical to that of
1017 * @ref session_lookup_connection_wt4
Florin Corascea194d2017-10-02 00:18:51 -07001018 *
1019 * @param fib_index index of the fib wherein the connection was received
1020 * @param lcl local ip4 address
1021 * @param rmt remote ip4 address
1022 * @param lcl_port local port
1023 * @param rmt_port remote port
1024 * @param proto transport protocol (e.g., tcp, udp)
1025 *
1026 * @return pointer to transport connection, if one is found, 0 otherwise
1027 */
Florin Coras04e53442017-07-16 17:12:15 -07001028transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001029session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
1030 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
1031 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001032{
Florin Corascea194d2017-10-02 00:18:51 -07001033 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001034 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001035 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001036 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001037 int rv;
1038
Florin Corascea194d2017-10-02 00:18:51 -07001039 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1040 if (PREDICT_FALSE (!st))
1041 return 0;
1042
Florin Corasa2ff7b82017-11-08 17:55:03 -08001043 /*
1044 * Lookup session amongst established ones
1045 */
Florin Coras04e53442017-07-16 17:12:15 -07001046 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001047 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001048 if (rv == 0)
1049 {
Florin Corascea194d2017-10-02 00:18:51 -07001050 s = session_get_from_handle (kv4.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001051 return transport_get_connection (proto, s->connection_index,
1052 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001053 }
1054
Florin Corasa2ff7b82017-11-08 17:55:03 -08001055 /*
1056 * Try half-open connections
1057 */
Florin Corascea194d2017-10-02 00:18:51 -07001058 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001059 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001060 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001061
1062 /*
1063 * Check the session rules table
1064 */
1065 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1066 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001067 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001068 {
Florin Corasdff48db2017-11-19 18:06:58 -08001069 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1070 return 0;
1071 if ((s = session_lookup_action_to_session (action_index,
1072 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001073 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001074 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001075 }
1076
1077 /*
1078 * If nothing is found, check if any listener is available
1079 */
Florin Coras477e91a2018-02-27 10:05:57 -08001080 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001081 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001082 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001083
1084 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001085}
1086
Florin Corascea194d2017-10-02 00:18:51 -07001087/**
1088 * Lookup session with ip4 and transport layer information
1089 *
Florin Coras6bd8d3f2022-03-14 21:17:25 -07001090 * Important note: this may look into another thread's pool table
Florin Coras3cbc04b2017-10-02 00:18:51 -07001091 *
1092 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
1093 * this returns a session as opposed to a transport connection and it does not
1094 * try to lookup half-open sessions.
1095 *
1096 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001097 */
Florin Coras288eaab2019-02-03 15:26:14 -08001098session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001099session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1100 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001101{
Florin Corascea194d2017-10-02 00:18:51 -07001102 session_table_t *st;
1103 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001104 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001105 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001106 int rv;
1107
1108 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1109 if (PREDICT_FALSE (!st))
1110 return 0;
1111
Florin Corasa2ff7b82017-11-08 17:55:03 -08001112 /*
1113 * Lookup session amongst established ones
1114 */
Florin Corascea194d2017-10-02 00:18:51 -07001115 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1116 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1117 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001118 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001119
Florin Corasa2ff7b82017-11-08 17:55:03 -08001120 /*
1121 * Check the session rules table
1122 */
1123 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1124 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001125 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001126 {
Florin Corasdff48db2017-11-19 18:06:58 -08001127 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1128 return 0;
1129 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1130 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001131 }
1132
1133 /*
1134 * If nothing is found, check if any listener is available
1135 */
Florin Coras477e91a2018-02-27 10:05:57 -08001136 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001137 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001138
1139 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001140}
1141
1142/**
1143 * Lookup connection with ip6 and transport layer information
1144 *
1145 * This is used on the fast path so it needs to be fast. Thereby,
1146 * duplication of code and 'hacks' allowed.
1147 *
1148 * The lookup is incremental and returns whenever something is matched. The
1149 * steps are:
1150 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001151 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001152 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001153 * - Try to find a fully-formed or local source wildcarded (listener bound to
1154 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001155 * - return 0
1156 *
1157 * @param fib_index index of the fib wherein the connection was received
1158 * @param lcl local ip6 address
1159 * @param rmt remote ip6 address
1160 * @param lcl_port local port
1161 * @param rmt_port remote port
1162 * @param proto transport protocol (e.g., tcp, udp)
1163 * @param thread_index thread index for request
1164 *
1165 * @return pointer to transport connection, if one is found, 0 otherwise
1166 */
1167transport_connection_t *
1168session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1169 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001170 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -08001171 u8 * result)
Florin Corascea194d2017-10-02 00:18:51 -07001172{
1173 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001174 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001175 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001176 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001177 int rv;
1178
Florin Corascea194d2017-10-02 00:18:51 -07001179 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1180 if (PREDICT_FALSE (!st))
1181 return 0;
1182
Florin Coras04e53442017-07-16 17:12:15 -07001183 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001184 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001185 if (rv == 0)
1186 {
Florin Corasb5e55a22019-01-10 12:42:47 -08001187 if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1188 {
1189 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1190 return 0;
1191 }
Florin Corascea194d2017-10-02 00:18:51 -07001192 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -08001193 return transport_get_connection (proto, s->connection_index,
1194 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001195 }
1196
Florin Corasa2ff7b82017-11-08 17:55:03 -08001197 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001198 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001199 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001200 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001201
Florin Corasa2ff7b82017-11-08 17:55:03 -08001202 /* Check the session rules table */
1203 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1204 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001205 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001206 {
Florin Corasb5e55a22019-01-10 12:42:47 -08001207 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1208 {
1209 *result = SESSION_LOOKUP_RESULT_FILTERED;
1210 return 0;
1211 }
Florin Corasdff48db2017-11-19 18:06:58 -08001212 if ((s = session_lookup_action_to_session (action_index,
1213 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001214 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001215 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001216 }
1217
1218 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001219 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001220 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001221 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001222
1223 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001224}
1225
Florin Corascea194d2017-10-02 00:18:51 -07001226/**
1227 * Lookup connection with ip6 and transport layer information
1228 *
1229 * Not optimized. This is used on the fast path so it needs to be fast.
1230 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1231 * to that of @ref session_lookup_connection_wt4
1232 *
1233 * @param fib_index index of the fib wherein the connection was received
1234 * @param lcl local ip6 address
1235 * @param rmt remote ip6 address
1236 * @param lcl_port local port
1237 * @param rmt_port remote port
1238 * @param proto transport protocol (e.g., tcp, udp)
1239 *
1240 * @return pointer to transport connection, if one is found, 0 otherwise
1241 */
Florin Coras04e53442017-07-16 17:12:15 -07001242transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001243session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1244 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1245 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001246{
Florin Corascea194d2017-10-02 00:18:51 -07001247 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001248 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001249 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001250 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001251 int rv;
1252
Florin Corascea194d2017-10-02 00:18:51 -07001253 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1254 if (PREDICT_FALSE (!st))
1255 return 0;
1256
Florin Coras04e53442017-07-16 17:12:15 -07001257 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001258 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001259 if (rv == 0)
1260 {
Florin Corascea194d2017-10-02 00:18:51 -07001261 s = session_get_from_handle (kv6.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001262 return transport_get_connection (proto, s->connection_index,
1263 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001264 }
1265
Florin Corasa2ff7b82017-11-08 17:55:03 -08001266 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001267 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001268 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001269 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001270
Florin Corasa2ff7b82017-11-08 17:55:03 -08001271 /* Check the session rules table */
1272 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1273 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001274 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001275 {
Florin Corasdff48db2017-11-19 18:06:58 -08001276 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1277 return 0;
1278 if ((s = session_lookup_action_to_session (action_index,
1279 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001280 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001281 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001282 }
1283
1284 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001285 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001286 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001287 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001288
1289 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001290}
1291
Florin Corascea194d2017-10-02 00:18:51 -07001292/**
1293 * Lookup session with ip6 and transport layer information
1294 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001295 * Important note: this may look into another thread's pool table and
1296 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1297 * if needed as soon as possible.
1298 *
1299 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1300 * this returns a session as opposed to a transport connection and it does not
1301 * try to lookup half-open sessions.
1302 *
1303 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001304 */
Florin Coras288eaab2019-02-03 15:26:14 -08001305session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001306session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1307 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001308{
1309 session_table_t *st;
1310 session_kv6_t kv6;
Florin Coras288eaab2019-02-03 15:26:14 -08001311 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001312 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001313 int rv;
1314
1315 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1316 if (PREDICT_FALSE (!st))
1317 return 0;
1318
1319 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1320 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1321 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001322 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001323
Florin Corasa2ff7b82017-11-08 17:55:03 -08001324 /* Check the session rules table */
1325 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1326 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001327 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001328 {
Florin Corasdff48db2017-11-19 18:06:58 -08001329 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1330 return 0;
1331 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1332 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001333 }
1334
Florin Corascea194d2017-10-02 00:18:51 -07001335 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001336 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001337 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001338 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001339}
1340
Florin Coras57660d92020-04-04 22:45:34 +00001341transport_connection_t *
1342session_lookup_connection (u32 fib_index, ip46_address_t * lcl,
1343 ip46_address_t * rmt, u16 lcl_port, u16 rmt_port,
1344 u8 proto, u8 is_ip4)
1345{
1346 if (is_ip4)
1347 return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4,
1348 lcl_port, rmt_port, proto);
1349 else
1350 return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6,
1351 lcl_port, rmt_port, proto);
1352}
1353
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001354session_error_t
1355vnet_session_rule_add_del (session_rule_add_del_args_t *args)
Florin Coras1c710452017-10-17 00:03:13 -07001356{
1357 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001358 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001359 session_table_t *st;
1360 u32 fib_index;
1361 u8 fib_proto;
Florin Corasc1a42652019-02-08 18:27:29 -08001362 int rv = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001363
1364 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001365 return SESSION_E_INVALID_NS;
Florin Corasc1a42652019-02-08 18:27:29 -08001366
Florin Coras1c710452017-10-17 00:03:13 -07001367 if (args->scope > 3)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001368 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001369
Florin Corasc97a7392017-11-05 23:07:07 -08001370 if (args->transport_proto != TRANSPORT_PROTO_TCP
1371 && args->transport_proto != TRANSPORT_PROTO_UDP)
Filip Tehlar0028e6f2023-06-28 10:47:32 +02001372 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -08001373
Florin Coras1c710452017-10-17 00:03:13 -07001374 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1375 {
1376 fib_proto = args->table_args.rmt.fp_proto;
1377 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1378 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001379 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001380 if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1381 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001382 }
1383 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1384 {
Dave Barachb7b92992018-10-17 10:38:51 -04001385 clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
Florin Corasa2ff7b82017-11-08 17:55:03 -08001386 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1387 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001388 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001389 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001390 rv = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001391 }
Florin Corasc1a42652019-02-08 18:27:29 -08001392 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001393}
1394
Florin Coras6c36f532017-11-03 18:32:34 -07001395/**
1396 * Mark (global) tables as pertaining to app ns
1397 */
1398void
1399session_lookup_set_tables_appns (app_namespace_t * app_ns)
1400{
1401 session_table_t *st;
1402 u32 fib_index;
1403 u8 fp;
1404
1405 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1406 {
1407 fib_index = app_namespace_get_fib_index (app_ns, fp);
Florin Coras95cd8642019-12-30 21:53:19 -08001408 st = session_table_get_or_alloc (fp, fib_index);
Florin Coras6c36f532017-11-03 18:32:34 -07001409 if (st)
1410 st->appns_index = app_namespace_index (app_ns);
1411 }
1412}
1413
Florin Corascea194d2017-10-02 00:18:51 -07001414u8 *
1415format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1416{
1417 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001418 u32 is_local = va_arg (*args, u32);
Florin Coras15531972018-08-12 23:50:53 -07001419 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
Florin Coras288eaab2019-02-03 15:26:14 -08001420 session_t *session;
Florin Coras15531972018-08-12 23:50:53 -07001421 app_worker_t *app_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -08001422 const u8 *app_name;
1423 u8 *str = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001424
Florin Corascea194d2017-10-02 00:18:51 -07001425 if (!is_local)
1426 {
1427 session = session_get_from_handle (kvp->value);
Florin Coras15531972018-08-12 23:50:53 -07001428 app_wrk = app_worker_get (session->app_wrk_index);
1429 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001430 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1431 key->proto, format_ip4_address, &key->src,
1432 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1433 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001434 s = format (s, "%-40v%-30v", str, app_name);
1435 }
1436 else
1437 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001438 session = session_get_from_handle (kvp->value);
1439 app_wrk = app_worker_get (session->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -07001440 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001441 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1442 format_ip4_address, &key->src,
1443 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001444 s = format (s, "%-30v%-30v", str, app_name);
1445 }
Florin Corascea194d2017-10-02 00:18:51 -07001446 return s;
1447}
1448
1449typedef struct _ip4_session_table_show_ctx_t
1450{
1451 vlib_main_t *vm;
1452 u8 is_local;
1453} ip4_session_table_show_ctx_t;
1454
1455static int
1456ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1457{
1458 ip4_session_table_show_ctx_t *ctx = arg;
1459 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1460 ctx->is_local);
1461 return 1;
1462}
1463
1464void
1465session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1466 u8 type, u8 is_local)
1467{
1468 ip4_session_table_show_ctx_t ctx = {
1469 .vm = vm,
1470 .is_local = is_local,
1471 };
1472 if (!is_local)
1473 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1474 else
1475 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1476 switch (type)
1477 {
1478 /* main table v4 */
1479 case 0:
1480 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1481 &ctx);
1482 break;
1483 default:
1484 clib_warning ("not supported");
1485 }
1486}
Florin Coras66b11312017-07-31 17:18:03 -07001487
Florin Coras1c710452017-10-17 00:03:13 -07001488static clib_error_t *
1489session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1490 vlib_cli_command_t * cmd)
1491{
Florin Corasc97a7392017-11-05 23:07:07 -08001492 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001493 clib_error_t *error = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001494 u32 appns_index, scope = 0;
1495 ip46_address_t lcl_ip, rmt_ip;
1496 u8 is_ip4 = 1, conn_set = 0;
1497 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001498 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001499 app_namespace_t *app_ns;
Florin Corasc1a42652019-02-08 18:27:29 -08001500 int rv;
Florin Coras1c710452017-10-17 00:03:13 -07001501
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001502 session_cli_return_if_not_enabled ();
1503
Dave Barachb7b92992018-10-17 10:38:51 -04001504 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1505 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001506 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1507 {
1508 if (unformat (input, "del"))
1509 is_add = 0;
1510 else if (unformat (input, "add"))
1511 ;
1512 else if (unformat (input, "appns %_%v%_", &ns_id))
1513 ;
1514 else if (unformat (input, "scope global"))
1515 scope = SESSION_RULE_SCOPE_GLOBAL;
1516 else if (unformat (input, "scope local"))
1517 scope = SESSION_RULE_SCOPE_LOCAL;
1518 else if (unformat (input, "scope all"))
1519 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1520 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1521 ;
1522 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1523 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1524 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1525 &rmt_port))
1526 {
1527 is_ip4 = 1;
1528 conn_set = 1;
1529 }
1530 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1531 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1532 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1533 &rmt_port))
1534 {
1535 is_ip4 = 0;
1536 conn_set = 1;
1537 }
1538 else if (unformat (input, "action %d", &action))
1539 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001540 else if (unformat (input, "tag %_%v%_", &tag))
1541 ;
Florin Coras1c710452017-10-17 00:03:13 -07001542 else
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001543 {
1544 error = clib_error_return (0, "unknown input `%U'",
1545 format_unformat_error, input);
1546 goto done;
1547 }
Florin Coras1c710452017-10-17 00:03:13 -07001548 }
1549
Florin Corasc97a7392017-11-05 23:07:07 -08001550 if (proto == ~0)
1551 {
1552 vlib_cli_output (vm, "proto must be set");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001553 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001554 }
1555 if (is_add && !conn_set && action == ~0)
1556 {
1557 vlib_cli_output (vm, "connection and action must be set for add");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001558 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001559 }
1560 if (!is_add && !tag && !conn_set)
1561 {
1562 vlib_cli_output (vm, "connection or tag must be set for delete");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001563 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001564 }
1565 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1566 {
1567 vlib_cli_output (vm, "tag too long (max u64)");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001568 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001569 }
Florin Coras1c710452017-10-17 00:03:13 -07001570
1571 if (ns_id)
1572 {
1573 app_ns = app_namespace_get_from_id (ns_id);
1574 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001575 {
1576 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001577 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001578 }
Florin Coras1c710452017-10-17 00:03:13 -07001579 }
1580 else
1581 {
1582 app_ns = app_namespace_get_default ();
1583 }
1584 appns_index = app_namespace_index (app_ns);
1585
1586 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1587 session_rule_add_del_args_t args = {
zhanglimaoa04ebb82019-03-13 10:36:54 +08001588 .transport_proto = proto,
Florin Coras1c710452017-10-17 00:03:13 -07001589 .table_args.lcl.fp_addr = lcl_ip,
1590 .table_args.lcl.fp_len = lcl_plen,
1591 .table_args.lcl.fp_proto = fib_proto,
1592 .table_args.rmt.fp_addr = rmt_ip,
1593 .table_args.rmt.fp_len = rmt_plen,
1594 .table_args.rmt.fp_proto = fib_proto,
1595 .table_args.lcl_port = lcl_port,
1596 .table_args.rmt_port = rmt_port,
1597 .table_args.action_index = action,
1598 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001599 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001600 .appns_index = appns_index,
1601 .scope = scope,
1602 };
Florin Corasc1a42652019-02-08 18:27:29 -08001603 if ((rv = vnet_session_rule_add_del (&args)))
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001604 error = clib_error_return (0, "rule add del returned %u", rv);
Florin Corasc1a42652019-02-08 18:27:29 -08001605
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001606done:
1607 vec_free (ns_id);
Florin Corasc97a7392017-11-05 23:07:07 -08001608 vec_free (tag);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001609 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001610}
1611
Florin Coras1c710452017-10-17 00:03:13 -07001612VLIB_CLI_COMMAND (session_rule_command, static) =
1613{
1614 .path = "session rule",
1615 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1616 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1617 .function = session_rule_command_fn,
1618};
Florin Coras1c710452017-10-17 00:03:13 -07001619
Florin Coras7999e832017-10-31 01:51:04 -07001620void
1621session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1622 u8 transport_proto)
1623{
1624 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001625 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001626 session_table_t *st;
1627 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001628 srt = &st->session_rules[transport_proto];
1629 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001630}
1631
1632void
1633session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1634 u8 transport_proto)
1635{
1636 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001637 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001638 session_table_t *st;
1639 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001640 srt = &st->session_rules[transport_proto];
1641 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001642}
1643
Florin Coras1c710452017-10-17 00:03:13 -07001644static clib_error_t *
1645show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1646 vlib_cli_command_t * cmd)
1647{
1648 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1649 u32 fib_index, scope = 0;
1650 ip46_address_t lcl_ip, rmt_ip;
1651 u8 is_ip4 = 1, show_one = 0;
1652 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001653 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001654 session_table_t *st;
1655 u8 *ns_id = 0, fib_proto;
1656
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001657 session_cli_return_if_not_enabled ();
1658
Dave Barachb7b92992018-10-17 10:38:51 -04001659 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1660 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001661 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1662 {
1663 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1664 ;
1665 else if (unformat (input, "appns %_%v%_", &ns_id))
1666 ;
1667 else if (unformat (input, "scope global"))
1668 scope = 1;
1669 else if (unformat (input, "scope local"))
1670 scope = 2;
1671 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1672 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1673 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1674 &rmt_port))
1675 {
1676 is_ip4 = 1;
1677 show_one = 1;
1678 }
1679 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1680 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1681 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1682 &rmt_port))
1683 {
1684 is_ip4 = 0;
1685 show_one = 1;
1686 }
1687 else
1688 return clib_error_return (0, "unknown input `%U'",
1689 format_unformat_error, input);
1690 }
1691
1692 if (transport_proto == ~0)
1693 {
1694 vlib_cli_output (vm, "transport proto must be set");
1695 return 0;
1696 }
1697
1698 if (ns_id)
1699 {
1700 app_ns = app_namespace_get_from_id (ns_id);
1701 if (!app_ns)
1702 {
1703 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1704 return 0;
1705 }
1706 }
1707 else
1708 {
1709 app_ns = app_namespace_get_default ();
1710 }
1711
1712 if (scope == 1 || scope == 0)
1713 {
1714 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1715 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1716 st = session_table_get_for_fib_index (fib_proto, fib_index);
1717 }
1718 else
1719 {
1720 st = app_namespace_get_local_table (app_ns);
1721 }
1722
1723 if (show_one)
1724 {
Florin Corasc97a7392017-11-05 23:07:07 -08001725 srt = &st->session_rules[transport_proto];
1726 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1727 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001728 return 0;
1729 }
1730
Florin Corasc97a7392017-11-05 23:07:07 -08001731 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1732 transport_proto);
1733 srt = &st->session_rules[transport_proto];
1734 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1735 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001736
1737 vec_free (ns_id);
1738 return 0;
1739}
1740
Florin Coras1c710452017-10-17 00:03:13 -07001741VLIB_CLI_COMMAND (show_session_rules_command, static) =
1742{
1743 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001744 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1745 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001746 .function = show_session_rules_command_fn,
1747};
Florin Coras1c710452017-10-17 00:03:13 -07001748
Florin Coras894d0a62023-11-17 16:35:04 -08001749u8 *
1750format_session_lookup_tables (u8 *s, va_list *args)
1751{
1752 u32 fib_proto = va_arg (*args, u32);
1753 u32 *fibs, num_fibs = 0, fib_index, indent;
1754 session_table_t *st;
1755 u64 total_mem = 0;
1756
1757 fibs = fib_index_to_table_index[fib_proto];
1758
1759 for (fib_index = 0; fib_index < vec_len (fibs); fib_index++)
1760 {
1761 if (fibs[fib_index] == ~0)
1762 continue;
1763
1764 num_fibs += 1;
1765 st = session_table_get (fibs[fib_index]);
1766 total_mem += session_table_memory_size (st);
1767 }
1768
1769 indent = format_get_indent (s);
1770 s = format (s, "active fibs:\t%u\n", num_fibs);
1771 s = format (s, "%Umax fib-index:\t%u\n", format_white_space, indent,
1772 vec_len (fibs) - 1);
1773 s = format (s, "%Utable memory:\t%U\n", format_white_space, indent,
1774 format_memory_size, total_mem);
1775 s = format (s, "%Uvec memory:\t%U\n", format_white_space, indent,
1776 format_memory_size, vec_mem_size (fibs));
1777
1778 return s;
1779}
1780
1781static clib_error_t *
1782show_session_lookup_command_fn (vlib_main_t *vm, unformat_input_t *input,
1783 vlib_cli_command_t *cmd)
1784{
1785 session_table_t *st;
1786 u32 fib_index = ~0;
1787
1788 session_cli_return_if_not_enabled ();
1789 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1790 {
1791 if (unformat (input, "table %u", &fib_index))
1792 ;
1793 else
1794 return clib_error_return (0, "unknown input `%U'",
1795 format_unformat_error, input);
1796 }
1797
1798 if (fib_index != ~0)
1799 {
1800 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1801 if (st)
1802 vlib_cli_output (vm, "%U", format_session_table, st);
1803 else
1804 vlib_cli_output (vm, "no ip4 table for fib-index %u", fib_index);
1805 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1806 if (st)
1807 vlib_cli_output (vm, "%U", format_session_table, st);
1808 else
1809 vlib_cli_output (vm, "no ip6 table for fib-index %u", fib_index);
1810 goto done;
1811 }
1812
1813 vlib_cli_output (vm, "ip4 fib lookup tables:\n %U",
1814 format_session_lookup_tables, FIB_PROTOCOL_IP4);
1815 vlib_cli_output (vm, "ip6 fib lookup tables:\n %U",
1816 format_session_lookup_tables, FIB_PROTOCOL_IP6);
1817
1818done:
1819 return 0;
1820}
1821
1822VLIB_CLI_COMMAND (show_session_lookup_command, static) = {
1823 .path = "show session lookup",
1824 .short_help = "show session lookup [table <fib-index>]",
1825 .function = show_session_lookup_command_fn,
1826};
1827
Florin Coras04e53442017-07-16 17:12:15 -07001828void
1829session_lookup_init (void)
1830{
Florin Corasf9bdc032024-02-02 13:01:07 -08001831 session_lookup_main_t *slm = &sl_main;
1832
1833 clib_spinlock_init (&slm->st_alloc_lock);
1834
Florin Corascea194d2017-10-02 00:18:51 -07001835 /*
1836 * Allocate default table and map it to fib_index 0
1837 */
1838 session_table_t *st = session_table_alloc ();
1839 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1840 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001841 st->active_fib_proto = FIB_PROTOCOL_IP4;
1842 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001843 st = session_table_alloc ();
1844 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1845 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001846 st->active_fib_proto = FIB_PROTOCOL_IP6;
1847 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001848}
1849
1850/*
1851 * fd.io coding-style-patch-verification: ON
1852 *
1853 * Local Variables:
1854 * eval: (c-set-style "gnu")
1855 * End:
1856 */