blob: 5cd1712f1951f7949779bc3f179aa2b3657299aa [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 Corascea194d2017-10-02 00:18:51 -070032/**
Florin Corascea194d2017-10-02 00:18:51 -070033 * Network namespace index (i.e., fib index) to session lookup table. We
34 * should have one per network protocol type but for now we only support IP4/6
35 */
36static u32 *fib_index_to_table_index[2];
37
Florin Coras04e53442017-07-16 17:12:15 -070038/* *INDENT-OFF* */
39/* 16 octets */
40typedef CLIB_PACKED (struct {
41 union
42 {
43 struct
44 {
45 ip4_address_t src;
46 ip4_address_t dst;
47 u16 src_port;
48 u16 dst_port;
49 /* align by making this 4 octets even though its a 1-bit field
50 * NOTE: avoid key overlap with other transports that use 5 tuples for
51 * session identification.
52 */
53 u32 proto;
54 };
55 u64 as_u64[2];
56 };
57}) v4_connection_key_t;
58
59typedef CLIB_PACKED (struct {
60 union
61 {
62 struct
63 {
64 /* 48 octets */
65 ip6_address_t src;
66 ip6_address_t dst;
67 u16 src_port;
68 u16 dst_port;
69 u32 proto;
70 u64 unused;
71 };
72 u64 as_u64[6];
73 };
74}) v6_connection_key_t;
75/* *INDENT-ON* */
76
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 Corascea194d2017-10-02 00:18:51 -0700158static session_table_t *
Andreas Schultz30a28c12020-04-23 10:41:50 +0200159session_table_get_or_alloc (u8 fib_proto, u32 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700160{
Florin Corascea194d2017-10-02 00:18:51 -0700161 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700162 u32 table_index;
Andreas Schultz30a28c12020-04-23 10:41:50 +0200163 ASSERT (fib_index != ~0);
164 if (vec_len (fib_index_to_table_index[fib_proto]) > fib_index &&
165 fib_index_to_table_index[fib_proto][fib_index] != ~0)
166 {
167 table_index = fib_index_to_table_index[fib_proto][fib_index];
168 return session_table_get (table_index);
169 }
170 else
Florin Corascea194d2017-10-02 00:18:51 -0700171 {
172 st = session_table_alloc ();
173 table_index = session_table_index (st);
Andreas Schultz30a28c12020-04-23 10:41:50 +0200174 vec_validate_init_empty (fib_index_to_table_index[fib_proto], fib_index,
175 ~0);
Florin Coras6c36f532017-11-03 18:32:34 -0700176 fib_index_to_table_index[fib_proto][fib_index] = table_index;
177 st->active_fib_proto = fib_proto;
Florin Corasb795bd02017-12-14 11:30:48 -0800178 session_table_init (st, fib_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700179 return st;
180 }
Florin Corascea194d2017-10-02 00:18:51 -0700181}
182
183static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700184session_table_get_or_alloc_for_connection (transport_connection_t * tc)
185{
186 u32 fib_proto;
187 fib_proto = transport_connection_fib_proto (tc);
188 return session_table_get_or_alloc (fib_proto, tc->fib_index);
189}
190
191static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700192session_table_get_for_connection (transport_connection_t * tc)
193{
194 u32 fib_proto = transport_connection_fib_proto (tc);
195 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
196 return 0;
197 return
198 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
199}
200
201static session_table_t *
202session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
203{
204 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
205 return 0;
206 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
207}
208
209u32
210session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
211{
212 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
213 return SESSION_TABLE_INVALID_INDEX;
214 return fib_index_to_table_index[fib_proto][fib_index];
215}
216
Florin Corasa4d09562021-07-08 08:25:09 -0700217u32
218session_lookup_get_or_alloc_index_for_fib (u32 fib_proto, u32 fib_index)
219{
220 session_table_t *st;
221 st = session_table_get_or_alloc (fib_proto, fib_index);
222 return session_table_index (st);
223}
224
Florin Corascea194d2017-10-02 00:18:51 -0700225/**
226 * Add transport connection to a session table
227 *
228 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
229 * is added to requested session table.
230 *
231 * @param tc transport connection to be added
232 * @param value value to be stored
233 *
234 * @return non-zero if failure
235 */
236int
237session_lookup_add_connection (transport_connection_t * tc, u64 value)
238{
239 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700240 session_kv4_t kv4;
241 session_kv6_t kv6;
242
Florin Corascea194d2017-10-02 00:18:51 -0700243 st = session_table_get_or_alloc_for_connection (tc);
244 if (!st)
245 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700246 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700247 {
Florin Coras04e53442017-07-16 17:12:15 -0700248 make_v4_ss_kv_from_tc (&kv4, tc);
249 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700250 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
251 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700252 }
253 else
254 {
Florin Coras04e53442017-07-16 17:12:15 -0700255 make_v6_ss_kv_from_tc (&kv6, tc);
256 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700257 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
258 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700259 }
260}
261
Florin Coras04e53442017-07-16 17:12:15 -0700262int
Florin Corascea194d2017-10-02 00:18:51 -0700263session_lookup_add_session_endpoint (u32 table_index,
264 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700265{
Florin Corascea194d2017-10-02 00:18:51 -0700266 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700267 session_kv4_t kv4;
268 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700269
Florin Corascea194d2017-10-02 00:18:51 -0700270 st = session_table_get (table_index);
271 if (!st)
272 return -1;
273 if (sep->is_ip4)
274 {
275 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
276 sep->transport_proto);
277 kv4.value = value;
278 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
279 }
280 else
281 {
282 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
283 sep->transport_proto);
284 kv6.value = value;
285 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
286 }
287}
288
289int
290session_lookup_del_session_endpoint (u32 table_index,
291 session_endpoint_t * sep)
292{
293 session_table_t *st;
294 session_kv4_t kv4;
295 session_kv6_t kv6;
296
297 st = session_table_get (table_index);
298 if (!st)
299 return -1;
300 if (sep->is_ip4)
301 {
302 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
303 sep->transport_proto);
304 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
305 }
306 else
307 {
308 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
309 sep->transport_proto);
310 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
311 }
312}
313
Florin Corasaab06042020-02-26 02:56:14 +0000314int
315session_lookup_del_session_endpoint2 (session_endpoint_t * sep)
316{
317 fib_protocol_t fib_proto;
318 session_table_t *st;
319 session_kv4_t kv4;
320 session_kv6_t kv6;
321
322 fib_proto = sep->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
323 st = session_table_get_for_fib_index (fib_proto, sep->fib_index);
324 if (!st)
325 return -1;
326 if (sep->is_ip4)
327 {
328 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
329 sep->transport_proto);
330 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
331 }
332 else
333 {
334 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
335 sep->transport_proto);
336 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
337 }
338}
339
Florin Corascea194d2017-10-02 00:18:51 -0700340/**
341 * Delete transport connection from session table
342 *
343 * @param table_index session table index
344 * @param tc transport connection to be removed
345 *
346 * @return non-zero if failure
347 */
348int
349session_lookup_del_connection (transport_connection_t * tc)
350{
351 session_table_t *st;
352 session_kv4_t kv4;
353 session_kv6_t kv6;
354
355 st = session_table_get_for_connection (tc);
356 if (!st)
357 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700358 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700359 {
Florin Coras04e53442017-07-16 17:12:15 -0700360 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700361 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700362 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700363 }
364 else
365 {
Florin Coras04e53442017-07-16 17:12:15 -0700366 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700367 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700368 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700369 }
Florin Coras04e53442017-07-16 17:12:15 -0700370}
371
372int
Florin Coras288eaab2019-02-03 15:26:14 -0800373session_lookup_del_session (session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700374{
375 transport_connection_t *ts;
Florin Coras1ee78302019-02-05 15:51:15 -0800376 ts = transport_get_connection (session_get_transport_proto (s),
377 s->connection_index, s->thread_index);
Florin Corase1e7fb82019-09-25 07:28:34 -0700378 if (!ts || (ts->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP))
Nathan Skrzypczak2eed1a12019-07-04 14:26:21 +0200379 return 0;
Florin Corascea194d2017-10-02 00:18:51 -0700380 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700381}
382
Florin Corasdff48db2017-11-19 18:06:58 -0800383static u8
384session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700385{
Florin Corasdff48db2017-11-19 18:06:58 -0800386 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
387 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
388 return 0;
389 return 1;
390}
391
Florin Corasf8f516a2018-02-08 15:10:09 -0800392static u64
393session_lookup_action_to_handle (u32 action_index)
Florin Corasdff48db2017-11-19 18:06:58 -0800394{
395 switch (action_index)
396 {
397 case SESSION_RULES_TABLE_ACTION_DROP:
Florin Corasf8f516a2018-02-08 15:10:09 -0800398 return SESSION_DROP_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800399 case SESSION_RULES_TABLE_ACTION_ALLOW:
400 case SESSION_RULES_TABLE_INVALID_INDEX:
Florin Corasf8f516a2018-02-08 15:10:09 -0800401 return SESSION_INVALID_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800402 default:
Florin Corasf8f516a2018-02-08 15:10:09 -0800403 /* application index */
Florin Corasdff48db2017-11-19 18:06:58 -0800404 return action_index;
405 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700406}
407
Florin Coras288eaab2019-02-03 15:26:14 -0800408static session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700409session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
410 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700411{
412 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800413 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700414 if (!app)
415 return 0;
416
Florin Coras15531972018-08-12 23:50:53 -0700417 return app_worker_first_listener (application_get_default_worker (app),
418 fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700419}
420
Florin Coras288eaab2019-02-03 15:26:14 -0800421static session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800422session_lookup_action_to_session (u32 action_index, u8 fib_proto,
423 u8 transport_proto)
424{
Florin Corasdff48db2017-11-19 18:06:58 -0800425 u32 app_index;
Florin Corasf8f516a2018-02-08 15:10:09 -0800426 app_index = session_lookup_action_to_handle (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800427 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800428 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800429 transport_proto);
430}
431
Florin Corasf8f516a2018-02-08 15:10:09 -0800432/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800433session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800434session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
435 ip4_address_t * lcl, u16 lcl_port,
436 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700437{
Florin Corasc97a7392017-11-05 23:07:07 -0800438 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800439 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800440 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700441 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800442 app_index = session_lookup_action_to_handle (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700443 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800444 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700445 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700446}
447
Florin Corasf8f516a2018-02-08 15:10:09 -0800448/** UNUSED */
Florin Coras288eaab2019-02-03 15:26:14 -0800449session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800450session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
451 ip6_address_t * lcl, u16 lcl_port,
452 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700453{
Florin Corasc97a7392017-11-05 23:07:07 -0800454 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800455 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800456 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700457 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800458 app_index = session_lookup_action_to_handle (action_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800459 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700460 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700461}
462
Florin Corasa2ff7b82017-11-08 17:55:03 -0800463/**
464 * Lookup listener for session endpoint in table
465 *
466 * @param table_index table where the endpoint should be looked up
467 * @param sep session endpoint to be looked up
468 * @param use_rules flag that indicates if the session rules of the table
469 * should be used
470 * @return invalid handle if nothing is found, the handle of a valid listener
Florin Corasf8f516a2018-02-08 15:10:09 -0800471 * or an action derived handle if a rule is hit
Florin Corasa2ff7b82017-11-08 17:55:03 -0800472 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700473u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800474session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
475 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700476{
Florin Corasc97a7392017-11-05 23:07:07 -0800477 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700478 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700479 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700480 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700481
Florin Corascea194d2017-10-02 00:18:51 -0700482 st = session_table_get (table_index);
483 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700484 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700485 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700486 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800487 session_kv4_t kv4;
488 ip4_address_t lcl4;
489
Florin Coras561af9b2017-12-09 10:19:43 -0800490 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
491 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700492 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
493 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700494 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800495 if (use_rules)
496 {
Dave Barachb7b92992018-10-17 10:38:51 -0400497 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800498 srt = &st->session_rules[sep->transport_proto];
499 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
500 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800501 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800502 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800503 }
Florin Coras68810622017-07-24 17:40:28 -0700504 }
505 else
506 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800507 session_kv6_t kv6;
508 ip6_address_t lcl6;
509
Florin Coras561af9b2017-12-09 10:19:43 -0800510 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
511 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700512 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
513 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700514 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700515
Florin Corasa2ff7b82017-11-08 17:55:03 -0800516 if (use_rules)
517 {
Dave Barachb7b92992018-10-17 10:38:51 -0400518 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800519 srt = &st->session_rules[sep->transport_proto];
520 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
521 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800522 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800523 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800524 }
Florin Coras68810622017-07-24 17:40:28 -0700525 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700526 return SESSION_INVALID_HANDLE;
527}
528
Florin Corasa2ff7b82017-11-08 17:55:03 -0800529/**
530 * Look up endpoint in local session table
531 *
532 * The result, for now, is an application index and it may in the future
533 * be extended to a more complicated "action object". The only action we
534 * emulate now is "drop" and for that we return a special app index.
535 *
536 * Lookup logic is to check in order:
537 * - the rules in the table (connect acls)
538 * - session sub-table for a listener
539 * - session sub-table for a local listener (zeroed addr)
540 *
541 * @param table_index table where the lookup should be done
542 * @param sep session endpoint to be looked up
Florin Corasf8f516a2018-02-08 15:10:09 -0800543 * @return session handle that can be interpreted as an adjacency
Florin Corasa2ff7b82017-11-08 17:55:03 -0800544 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800545u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800546session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700547{
Florin Corasc97a7392017-11-05 23:07:07 -0800548 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700549 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700550 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700551 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700552
Florin Corascea194d2017-10-02 00:18:51 -0700553 st = session_table_get (table_index);
554 if (!st)
555 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800556 ASSERT (st->is_local);
557
Florin Corascea194d2017-10-02 00:18:51 -0700558 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700559 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800560 session_kv4_t kv4;
561 ip4_address_t lcl4;
562
563 /*
564 * Check if endpoint has special rules associated
565 */
Dave Barachb7b92992018-10-17 10:38:51 -0400566 clib_memset (&lcl4, 0, sizeof (lcl4));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800567 srt = &st->session_rules[sep->transport_proto];
568 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
569 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800570 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800571 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800572
573 /*
574 * Check if session endpoint is a listener
575 */
Florin Corascea194d2017-10-02 00:18:51 -0700576 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
577 sep->transport_proto);
578 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
579 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800580 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700581
582 /*
583 * Zero out the ip. Logic is that connect to local ips, say
584 * 127.0.0.1:port, can match 0.0.0.0:port
585 */
Florin Coras477e91a2018-02-27 10:05:57 -0800586 if (ip4_is_local_host (&sep->ip.ip4))
587 {
588 kv4.key[0] = 0;
589 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
590 if (rv == 0)
591 return kv4.value;
592 }
593 else
594 {
595 kv4.key[0] = 0;
596 }
Florin Corasdbd44562017-11-09 19:30:17 -0800597
598 /*
599 * Zero out the port and check if we have proxy
600 */
601 kv4.key[1] = 0;
602 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
603 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800604 return kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700605 }
606 else
607 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800608 session_kv6_t kv6;
609 ip6_address_t lcl6;
610
Dave Barachb7b92992018-10-17 10:38:51 -0400611 clib_memset (&lcl6, 0, sizeof (lcl6));
Florin Corasa2ff7b82017-11-08 17:55:03 -0800612 srt = &st->session_rules[sep->transport_proto];
613 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
614 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800615 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800616 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800617
Florin Corascea194d2017-10-02 00:18:51 -0700618 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
619 sep->transport_proto);
620 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
621 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800622 return kv6.value;
Florin Corascea194d2017-10-02 00:18:51 -0700623
624 /*
625 * Zero out the ip. Same logic as above.
626 */
Florin Coras477e91a2018-02-27 10:05:57 -0800627
628 if (ip6_is_local_host (&sep->ip.ip6))
629 {
630 kv6.key[0] = kv6.key[1] = 0;
631 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
632 if (rv == 0)
633 return kv6.value;
634 }
635 else
636 {
637 kv6.key[0] = kv6.key[1] = 0;
638 }
Florin Corasdbd44562017-11-09 19:30:17 -0800639
640 /*
641 * Zero out the port. Same logic as above.
642 */
643 kv6.key[4] = kv6.key[5] = 0;
644 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
645 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800646 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700647 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800648 return SESSION_INVALID_HANDLE;
Florin Coras04e53442017-07-16 17:12:15 -0700649}
650
Florin Coras288eaab2019-02-03 15:26:14 -0800651static inline session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700652session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800653 u16 lcl_port, u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700654{
Florin Coras04e53442017-07-16 17:12:15 -0700655 session_kv4_t kv4;
656 int rv;
657
Florin Corasdbd44562017-11-09 19:30:17 -0800658 /*
659 * First, try a fully formed listener
660 */
Florin Coras04e53442017-07-16 17:12:15 -0700661 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700662 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700663 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700664 return listen_session_get ((u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700665
Florin Corasdbd44562017-11-09 19:30:17 -0800666 /*
667 * Zero out the lcl ip and check if any 0/0 port binds have been done
668 */
Florin Coras477e91a2018-02-27 10:05:57 -0800669 if (use_wildcard)
670 {
671 kv4.key[0] = 0;
672 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
673 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700674 return listen_session_get ((u32) kv4.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800675 }
676 else
677 {
678 kv4.key[0] = 0;
679 }
Florin Coras04e53442017-07-16 17:12:15 -0700680
Florin Corasdbd44562017-11-09 19:30:17 -0800681 /*
682 * Zero out port and check if we have a proxy set up for our ip
683 */
684 make_v4_proxy_kv (&kv4, lcl, proto);
685 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
686 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700687 return listen_session_get ((u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800688
Florin Coras04e53442017-07-16 17:12:15 -0700689 return 0;
690}
691
Florin Coras288eaab2019-02-03 15:26:14 -0800692session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700693session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800694 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700695{
Florin Corascea194d2017-10-02 00:18:51 -0700696 session_table_t *st;
697 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
698 if (!st)
699 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800700 return session_lookup_listener4_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700701}
702
Florin Coras288eaab2019-02-03 15:26:14 -0800703static session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700704session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
Florin Coras477e91a2018-02-27 10:05:57 -0800705 u16 lcl_port, u8 proto, u8 ip_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700706{
Florin Coras04e53442017-07-16 17:12:15 -0700707 session_kv6_t kv6;
708 int rv;
709
710 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700711 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700712 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700713 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700714
715 /* Zero out the lcl ip */
Florin Coras477e91a2018-02-27 10:05:57 -0800716 if (ip_wildcard)
717 {
718 kv6.key[0] = kv6.key[1] = 0;
719 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
720 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700721 return listen_session_get ((u32) kv6.value);
Florin Coras477e91a2018-02-27 10:05:57 -0800722 }
723 else
724 {
725 kv6.key[0] = kv6.key[1] = 0;
726 }
Florin Coras04e53442017-07-16 17:12:15 -0700727
Florin Corasdbd44562017-11-09 19:30:17 -0800728 make_v6_proxy_kv (&kv6, lcl, proto);
729 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
730 if (rv == 0)
Florin Coras5c9083d2018-04-13 06:39:07 -0700731 return listen_session_get ((u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700732 return 0;
733}
734
Florin Coras288eaab2019-02-03 15:26:14 -0800735session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700736session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
Yu Pingb092b772019-12-27 04:04:33 +0800737 u8 proto, u8 use_wildcard)
Florin Coras04e53442017-07-16 17:12:15 -0700738{
Florin Corascea194d2017-10-02 00:18:51 -0700739 session_table_t *st;
740 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
741 if (!st)
742 return 0;
Yu Pingb092b772019-12-27 04:04:33 +0800743 return session_lookup_listener6_i (st, lcl, lcl_port, proto, use_wildcard);
Florin Coras04e53442017-07-16 17:12:15 -0700744}
745
Florin Coras477e91a2018-02-27 10:05:57 -0800746/**
747 * Lookup listener, exact or proxy (inaddr_any:0) match
748 */
Florin Coras288eaab2019-02-03 15:26:14 -0800749session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700750session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700751{
Florin Corascea194d2017-10-02 00:18:51 -0700752 session_table_t *st;
753 st = session_table_get (table_index);
754 if (!st)
755 return 0;
756 if (sep->is_ip4)
757 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800758 sep->transport_proto, 0);
Florin Corascea194d2017-10-02 00:18:51 -0700759 else
760 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
Florin Coras477e91a2018-02-27 10:05:57 -0800761 sep->transport_proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700762 return 0;
763}
764
Florin Coras9f1a5432019-03-10 21:03:20 -0700765/**
766 * Lookup listener wildcard match
767 */
768session_t *
769session_lookup_listener_wildcard (u32 table_index, session_endpoint_t * sep)
770{
771 session_table_t *st;
772 st = session_table_get (table_index);
773 if (!st)
774 return 0;
775 if (sep->is_ip4)
776 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
777 sep->transport_proto,
778 1 /* use_wildcard */ );
779 else
780 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
781 sep->transport_proto,
782 1 /* use_wildcard */ );
783 return 0;
784}
785
Florin Corascea194d2017-10-02 00:18:51 -0700786int
787session_lookup_add_half_open (transport_connection_t * tc, u64 value)
788{
789 session_table_t *st;
790 session_kv4_t kv4;
791 session_kv6_t kv6;
792
793 st = session_table_get_or_alloc_for_connection (tc);
794 if (!st)
795 return 0;
796 if (tc->is_ip4)
797 {
798 make_v4_ss_kv_from_tc (&kv4, tc);
799 kv4.value = value;
800 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
801 1 /* is_add */ );
802 }
803 else
804 {
805 make_v6_ss_kv_from_tc (&kv6, tc);
806 kv6.value = value;
807 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
808 1 /* is_add */ );
809 }
810}
811
812int
813session_lookup_del_half_open (transport_connection_t * tc)
814{
815 session_table_t *st;
816 session_kv4_t kv4;
817 session_kv6_t kv6;
818
819 st = session_table_get_for_connection (tc);
820 if (!st)
821 return -1;
822 if (tc->is_ip4)
823 {
824 make_v4_ss_kv_from_tc (&kv4, tc);
825 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
826 0 /* is_add */ );
827 }
828 else
829 {
830 make_v6_ss_kv_from_tc (&kv6, tc);
831 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
832 0 /* is_add */ );
833 }
834}
835
Florin Coras04e53442017-07-16 17:12:15 -0700836u64
Florin Corascea194d2017-10-02 00:18:51 -0700837session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700838{
Florin Corascea194d2017-10-02 00:18:51 -0700839 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700840 session_kv4_t kv4;
841 session_kv6_t kv6;
842 int rv;
843
Florin Corascea194d2017-10-02 00:18:51 -0700844 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
845 tc->fib_index);
846 if (!st)
847 return HALF_OPEN_LOOKUP_INVALID_VALUE;
848 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700849 {
Florin Corascea194d2017-10-02 00:18:51 -0700850 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700851 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700852 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700853 if (rv == 0)
854 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700855 }
856 else
857 {
858 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700859 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700860 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700861 if (rv == 0)
862 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700863 }
864 return HALF_OPEN_LOOKUP_INVALID_VALUE;
865}
866
867transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700868session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700869{
Florin Coras04e53442017-07-16 17:12:15 -0700870 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700871 {
Florin Coras1ee78302019-02-05 15:51:15 -0800872 u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
873 return transport_get_half_open (sst, handle & 0xFFFFFFFF);
Florin Corascea194d2017-10-02 00:18:51 -0700874 }
Florin Coras04e53442017-07-16 17:12:15 -0700875 return 0;
876}
877
Florin Corascea194d2017-10-02 00:18:51 -0700878/**
879 * Lookup connection with ip4 and transport layer information
880 *
881 * This is used on the fast path so it needs to be fast. Thereby,
882 * duplication of code and 'hacks' allowed.
883 *
884 * The lookup is incremental and returns whenever something is matched. The
885 * steps are:
886 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700887 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700888 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800889 * - Try to find a fully-formed or local source wildcarded (listener bound to
890 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700891 * - return 0
892 *
893 * @param fib_index index of fib wherein the connection was received
894 * @param lcl local ip4 address
895 * @param rmt remote ip4 address
896 * @param lcl_port local port
897 * @param rmt_port remote port
898 * @param proto transport protocol (e.g., tcp, udp)
899 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800900 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700901 *
902 * @return pointer to transport connection, if one is found, 0 otherwise
903 */
Florin Coras04e53442017-07-16 17:12:15 -0700904transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700905session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
906 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800907 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -0800908 u8 * result)
Florin Coras04e53442017-07-16 17:12:15 -0700909{
Florin Corascea194d2017-10-02 00:18:51 -0700910 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700911 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800912 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800913 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700914 int rv;
915
Florin Corascea194d2017-10-02 00:18:51 -0700916 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
917 if (PREDICT_FALSE (!st))
918 return 0;
919
Florin Corasa2ff7b82017-11-08 17:55:03 -0800920 /*
921 * Lookup session amongst established ones
922 */
Florin Coras04e53442017-07-16 17:12:15 -0700923 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700924 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700925 if (rv == 0)
926 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800927 if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
928 {
929 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
930 return 0;
931 }
Florin Corascea194d2017-10-02 00:18:51 -0700932 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -0800933 return transport_get_connection (proto, s->connection_index,
934 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700935 }
936
Florin Corasa2ff7b82017-11-08 17:55:03 -0800937 /*
938 * Try half-open connections
939 */
Florin Corascea194d2017-10-02 00:18:51 -0700940 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700941 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -0800942 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700943
Florin Corasa2ff7b82017-11-08 17:55:03 -0800944 /*
945 * Check the session rules table
946 */
947 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
948 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800949 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800950 {
Florin Corasb5e55a22019-01-10 12:42:47 -0800951 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
952 {
953 *result = SESSION_LOOKUP_RESULT_FILTERED;
954 return 0;
955 }
Florin Corasdff48db2017-11-19 18:06:58 -0800956 if ((s = session_lookup_action_to_session (action_index,
957 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -0800958 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800959 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800960 }
961
962 /*
963 * If nothing is found, check if any listener is available
964 */
Florin Coras477e91a2018-02-27 10:05:57 -0800965 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800966 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -0800967 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800968
969 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700970}
971
Florin Corascea194d2017-10-02 00:18:51 -0700972/**
973 * Lookup connection with ip4 and transport layer information
974 *
Florin Corasb5e55a22019-01-10 12:42:47 -0800975 * Not optimized. Lookup logic is identical to that of
976 * @ref session_lookup_connection_wt4
Florin Corascea194d2017-10-02 00:18:51 -0700977 *
978 * @param fib_index index of the fib wherein the connection was received
979 * @param lcl local ip4 address
980 * @param rmt remote ip4 address
981 * @param lcl_port local port
982 * @param rmt_port remote port
983 * @param proto transport protocol (e.g., tcp, udp)
984 *
985 * @return pointer to transport connection, if one is found, 0 otherwise
986 */
Florin Coras04e53442017-07-16 17:12:15 -0700987transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700988session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
989 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
990 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700991{
Florin Corascea194d2017-10-02 00:18:51 -0700992 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700993 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -0800994 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800995 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700996 int rv;
997
Florin Corascea194d2017-10-02 00:18:51 -0700998 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
999 if (PREDICT_FALSE (!st))
1000 return 0;
1001
Florin Corasa2ff7b82017-11-08 17:55:03 -08001002 /*
1003 * Lookup session amongst established ones
1004 */
Florin Coras04e53442017-07-16 17:12:15 -07001005 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001006 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001007 if (rv == 0)
1008 {
Florin Corascea194d2017-10-02 00:18:51 -07001009 s = session_get_from_handle (kv4.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001010 return transport_get_connection (proto, s->connection_index,
1011 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001012 }
1013
Florin Corasa2ff7b82017-11-08 17:55:03 -08001014 /*
1015 * Try half-open connections
1016 */
Florin Corascea194d2017-10-02 00:18:51 -07001017 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -07001018 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001019 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001020
1021 /*
1022 * Check the session rules table
1023 */
1024 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1025 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001026 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001027 {
Florin Corasdff48db2017-11-19 18:06:58 -08001028 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1029 return 0;
1030 if ((s = session_lookup_action_to_session (action_index,
1031 FIB_PROTOCOL_IP4, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001032 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001033 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001034 }
1035
1036 /*
1037 * If nothing is found, check if any listener is available
1038 */
Florin Coras477e91a2018-02-27 10:05:57 -08001039 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001040 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001041 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001042
1043 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001044}
1045
Florin Corascea194d2017-10-02 00:18:51 -07001046/**
1047 * Lookup session with ip4 and transport layer information
1048 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001049 * Important note: this may look into another thread's pool table and
1050 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1051 * if needed as soon as possible.
1052 *
1053 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
1054 * this returns a session as opposed to a transport connection and it does not
1055 * try to lookup half-open sessions.
1056 *
1057 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001058 */
Florin Coras288eaab2019-02-03 15:26:14 -08001059session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001060session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1061 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001062{
Florin Corascea194d2017-10-02 00:18:51 -07001063 session_table_t *st;
1064 session_kv4_t kv4;
Florin Coras288eaab2019-02-03 15:26:14 -08001065 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001066 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001067 int rv;
1068
1069 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1070 if (PREDICT_FALSE (!st))
1071 return 0;
1072
Florin Corasa2ff7b82017-11-08 17:55:03 -08001073 /*
1074 * Lookup session amongst established ones
1075 */
Florin Corascea194d2017-10-02 00:18:51 -07001076 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1077 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1078 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001079 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001080
Florin Corasa2ff7b82017-11-08 17:55:03 -08001081 /*
1082 * Check the session rules table
1083 */
1084 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1085 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001086 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001087 {
Florin Corasdff48db2017-11-19 18:06:58 -08001088 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1089 return 0;
1090 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1091 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001092 }
1093
1094 /*
1095 * If nothing is found, check if any listener is available
1096 */
Florin Coras477e91a2018-02-27 10:05:57 -08001097 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001098 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001099
1100 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001101}
1102
1103/**
1104 * Lookup connection with ip6 and transport layer information
1105 *
1106 * This is used on the fast path so it needs to be fast. Thereby,
1107 * duplication of code and 'hacks' allowed.
1108 *
1109 * The lookup is incremental and returns whenever something is matched. The
1110 * steps are:
1111 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001112 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001113 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001114 * - Try to find a fully-formed or local source wildcarded (listener bound to
1115 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001116 * - return 0
1117 *
1118 * @param fib_index index of the fib wherein the connection was received
1119 * @param lcl local ip6 address
1120 * @param rmt remote ip6 address
1121 * @param lcl_port local port
1122 * @param rmt_port remote port
1123 * @param proto transport protocol (e.g., tcp, udp)
1124 * @param thread_index thread index for request
1125 *
1126 * @return pointer to transport connection, if one is found, 0 otherwise
1127 */
1128transport_connection_t *
1129session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1130 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001131 u16 rmt_port, u8 proto, u32 thread_index,
Florin Corasb5e55a22019-01-10 12:42:47 -08001132 u8 * result)
Florin Corascea194d2017-10-02 00:18:51 -07001133{
1134 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001135 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001136 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001137 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001138 int rv;
1139
Florin Corascea194d2017-10-02 00:18:51 -07001140 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1141 if (PREDICT_FALSE (!st))
1142 return 0;
1143
Florin Coras04e53442017-07-16 17:12:15 -07001144 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001145 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001146 if (rv == 0)
1147 {
Florin Corascea194d2017-10-02 00:18:51 -07001148 ASSERT ((u32) (kv6.value >> 32) == thread_index);
Florin Corasb5e55a22019-01-10 12:42:47 -08001149 if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1150 {
1151 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1152 return 0;
1153 }
Florin Corascea194d2017-10-02 00:18:51 -07001154 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras1ee78302019-02-05 15:51:15 -08001155 return transport_get_connection (proto, s->connection_index,
1156 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001157 }
1158
Florin Corasa2ff7b82017-11-08 17:55:03 -08001159 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001160 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001161 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001162 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001163
Florin Corasa2ff7b82017-11-08 17:55:03 -08001164 /* Check the session rules table */
1165 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1166 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001167 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001168 {
Florin Corasb5e55a22019-01-10 12:42:47 -08001169 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1170 {
1171 *result = SESSION_LOOKUP_RESULT_FILTERED;
1172 return 0;
1173 }
Florin Corasdff48db2017-11-19 18:06:58 -08001174 if ((s = session_lookup_action_to_session (action_index,
1175 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001176 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001177 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001178 }
1179
1180 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001181 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001182 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001183 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001184
1185 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001186}
1187
Florin Corascea194d2017-10-02 00:18:51 -07001188/**
1189 * Lookup connection with ip6 and transport layer information
1190 *
1191 * Not optimized. This is used on the fast path so it needs to be fast.
1192 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1193 * to that of @ref session_lookup_connection_wt4
1194 *
1195 * @param fib_index index of the fib wherein the connection was received
1196 * @param lcl local ip6 address
1197 * @param rmt remote ip6 address
1198 * @param lcl_port local port
1199 * @param rmt_port remote port
1200 * @param proto transport protocol (e.g., tcp, udp)
1201 *
1202 * @return pointer to transport connection, if one is found, 0 otherwise
1203 */
Florin Coras04e53442017-07-16 17:12:15 -07001204transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001205session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1206 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1207 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001208{
Florin Corascea194d2017-10-02 00:18:51 -07001209 session_table_t *st;
Florin Coras288eaab2019-02-03 15:26:14 -08001210 session_t *s;
Florin Coras04e53442017-07-16 17:12:15 -07001211 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001212 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001213 int rv;
1214
Florin Corascea194d2017-10-02 00:18:51 -07001215 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1216 if (PREDICT_FALSE (!st))
1217 return 0;
1218
Florin Coras04e53442017-07-16 17:12:15 -07001219 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001220 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001221 if (rv == 0)
1222 {
Florin Corascea194d2017-10-02 00:18:51 -07001223 s = session_get_from_handle (kv6.value);
Florin Coras1ee78302019-02-05 15:51:15 -08001224 return transport_get_connection (proto, s->connection_index,
1225 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001226 }
1227
Florin Corasa2ff7b82017-11-08 17:55:03 -08001228 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001229 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001230 if (rv == 0)
Florin Coras1ee78302019-02-05 15:51:15 -08001231 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001232
Florin Corasa2ff7b82017-11-08 17:55:03 -08001233 /* Check the session rules table */
1234 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1235 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001236 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001237 {
Florin Corasdff48db2017-11-19 18:06:58 -08001238 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1239 return 0;
1240 if ((s = session_lookup_action_to_session (action_index,
1241 FIB_PROTOCOL_IP6, proto)))
Florin Coras1ee78302019-02-05 15:51:15 -08001242 return transport_get_listener (proto, s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001243 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001244 }
1245
1246 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001247 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001248 if (s)
Florin Coras1ee78302019-02-05 15:51:15 -08001249 return transport_get_listener (proto, s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001250
1251 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001252}
1253
Florin Corascea194d2017-10-02 00:18:51 -07001254/**
1255 * Lookup session with ip6 and transport layer information
1256 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001257 * Important note: this may look into another thread's pool table and
1258 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1259 * if needed as soon as possible.
1260 *
1261 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1262 * this returns a session as opposed to a transport connection and it does not
1263 * try to lookup half-open sessions.
1264 *
1265 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001266 */
Florin Coras288eaab2019-02-03 15:26:14 -08001267session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001268session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1269 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001270{
1271 session_table_t *st;
1272 session_kv6_t kv6;
Florin Coras288eaab2019-02-03 15:26:14 -08001273 session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001274 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001275 int rv;
1276
1277 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1278 if (PREDICT_FALSE (!st))
1279 return 0;
1280
1281 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1282 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1283 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001284 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001285
Florin Corasa2ff7b82017-11-08 17:55:03 -08001286 /* Check the session rules table */
1287 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1288 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001289 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001290 {
Florin Corasdff48db2017-11-19 18:06:58 -08001291 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1292 return 0;
1293 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1294 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001295 }
1296
Florin Corascea194d2017-10-02 00:18:51 -07001297 /* If nothing is found, check if any listener is available */
Florin Coras477e91a2018-02-27 10:05:57 -08001298 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
Florin Corascea194d2017-10-02 00:18:51 -07001299 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001300 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001301}
1302
Florin Coras57660d92020-04-04 22:45:34 +00001303transport_connection_t *
1304session_lookup_connection (u32 fib_index, ip46_address_t * lcl,
1305 ip46_address_t * rmt, u16 lcl_port, u16 rmt_port,
1306 u8 proto, u8 is_ip4)
1307{
1308 if (is_ip4)
1309 return session_lookup_connection4 (fib_index, &lcl->ip4, &rmt->ip4,
1310 lcl_port, rmt_port, proto);
1311 else
1312 return session_lookup_connection6 (fib_index, &lcl->ip6, &rmt->ip6,
1313 lcl_port, rmt_port, proto);
1314}
1315
Florin Corasc1a42652019-02-08 18:27:29 -08001316int
Florin Coras1c710452017-10-17 00:03:13 -07001317vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1318{
1319 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001320 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001321 session_table_t *st;
1322 u32 fib_index;
1323 u8 fib_proto;
Florin Corasc1a42652019-02-08 18:27:29 -08001324 int rv = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001325
1326 if (!app_ns)
Florin Corasc1a42652019-02-08 18:27:29 -08001327 return VNET_API_ERROR_APP_INVALID_NS;
1328
Florin Coras1c710452017-10-17 00:03:13 -07001329 if (args->scope > 3)
Florin Corasc1a42652019-02-08 18:27:29 -08001330 return VNET_API_ERROR_INVALID_VALUE;
1331
Florin Corasc97a7392017-11-05 23:07:07 -08001332 if (args->transport_proto != TRANSPORT_PROTO_TCP
1333 && args->transport_proto != TRANSPORT_PROTO_UDP)
Florin Corasc1a42652019-02-08 18:27:29 -08001334 return VNET_API_ERROR_INVALID_VALUE;
1335
Florin Coras1c710452017-10-17 00:03:13 -07001336 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1337 {
1338 fib_proto = args->table_args.rmt.fp_proto;
1339 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1340 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001341 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001342 if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1343 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001344 }
1345 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1346 {
Dave Barachb7b92992018-10-17 10:38:51 -04001347 clib_memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
Florin Corasa2ff7b82017-11-08 17:55:03 -08001348 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1349 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001350 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001351 srt = &st->session_rules[args->transport_proto];
Florin Corasc1a42652019-02-08 18:27:29 -08001352 rv = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001353 }
Florin Corasc1a42652019-02-08 18:27:29 -08001354 return rv;
Florin Coras1c710452017-10-17 00:03:13 -07001355}
1356
Florin Coras6c36f532017-11-03 18:32:34 -07001357/**
1358 * Mark (global) tables as pertaining to app ns
1359 */
1360void
1361session_lookup_set_tables_appns (app_namespace_t * app_ns)
1362{
1363 session_table_t *st;
1364 u32 fib_index;
1365 u8 fp;
1366
1367 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1368 {
1369 fib_index = app_namespace_get_fib_index (app_ns, fp);
Florin Coras95cd8642019-12-30 21:53:19 -08001370 st = session_table_get_or_alloc (fp, fib_index);
Florin Coras6c36f532017-11-03 18:32:34 -07001371 if (st)
1372 st->appns_index = app_namespace_index (app_ns);
1373 }
1374}
1375
Florin Corascea194d2017-10-02 00:18:51 -07001376u8 *
1377format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1378{
1379 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
Florin Coras2b81e3c2019-02-27 07:55:46 -08001380 u32 is_local = va_arg (*args, u32);
Florin Coras15531972018-08-12 23:50:53 -07001381 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
Florin Coras288eaab2019-02-03 15:26:14 -08001382 session_t *session;
Florin Coras15531972018-08-12 23:50:53 -07001383 app_worker_t *app_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -08001384 const u8 *app_name;
1385 u8 *str = 0;
Florin Corascea194d2017-10-02 00:18:51 -07001386
Florin Corascea194d2017-10-02 00:18:51 -07001387 if (!is_local)
1388 {
1389 session = session_get_from_handle (kvp->value);
Florin Coras15531972018-08-12 23:50:53 -07001390 app_wrk = app_worker_get (session->app_wrk_index);
1391 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001392 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1393 key->proto, format_ip4_address, &key->src,
1394 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1395 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001396 s = format (s, "%-40v%-30v", str, app_name);
1397 }
1398 else
1399 {
Florin Coras2b81e3c2019-02-27 07:55:46 -08001400 session = session_get_from_handle (kvp->value);
1401 app_wrk = app_worker_get (session->app_wrk_index);
Florin Coras15531972018-08-12 23:50:53 -07001402 app_name = application_name_from_index (app_wrk->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001403 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1404 format_ip4_address, &key->src,
1405 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001406 s = format (s, "%-30v%-30v", str, app_name);
1407 }
Florin Corascea194d2017-10-02 00:18:51 -07001408 return s;
1409}
1410
1411typedef struct _ip4_session_table_show_ctx_t
1412{
1413 vlib_main_t *vm;
1414 u8 is_local;
1415} ip4_session_table_show_ctx_t;
1416
1417static int
1418ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1419{
1420 ip4_session_table_show_ctx_t *ctx = arg;
1421 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1422 ctx->is_local);
1423 return 1;
1424}
1425
1426void
1427session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1428 u8 type, u8 is_local)
1429{
1430 ip4_session_table_show_ctx_t ctx = {
1431 .vm = vm,
1432 .is_local = is_local,
1433 };
1434 if (!is_local)
1435 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1436 else
1437 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1438 switch (type)
1439 {
1440 /* main table v4 */
1441 case 0:
1442 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1443 &ctx);
1444 break;
1445 default:
1446 clib_warning ("not supported");
1447 }
1448}
Florin Coras66b11312017-07-31 17:18:03 -07001449
Florin Coras1c710452017-10-17 00:03:13 -07001450static clib_error_t *
1451session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1452 vlib_cli_command_t * cmd)
1453{
Florin Corasc97a7392017-11-05 23:07:07 -08001454 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001455 clib_error_t *error = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001456 u32 appns_index, scope = 0;
1457 ip46_address_t lcl_ip, rmt_ip;
1458 u8 is_ip4 = 1, conn_set = 0;
1459 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001460 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001461 app_namespace_t *app_ns;
Florin Corasc1a42652019-02-08 18:27:29 -08001462 int rv;
Florin Coras1c710452017-10-17 00:03:13 -07001463
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001464 session_cli_return_if_not_enabled ();
1465
Dave Barachb7b92992018-10-17 10:38:51 -04001466 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1467 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001468 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1469 {
1470 if (unformat (input, "del"))
1471 is_add = 0;
1472 else if (unformat (input, "add"))
1473 ;
1474 else if (unformat (input, "appns %_%v%_", &ns_id))
1475 ;
1476 else if (unformat (input, "scope global"))
1477 scope = SESSION_RULE_SCOPE_GLOBAL;
1478 else if (unformat (input, "scope local"))
1479 scope = SESSION_RULE_SCOPE_LOCAL;
1480 else if (unformat (input, "scope all"))
1481 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1482 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1483 ;
1484 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1485 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1486 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1487 &rmt_port))
1488 {
1489 is_ip4 = 1;
1490 conn_set = 1;
1491 }
1492 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1493 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1494 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1495 &rmt_port))
1496 {
1497 is_ip4 = 0;
1498 conn_set = 1;
1499 }
1500 else if (unformat (input, "action %d", &action))
1501 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001502 else if (unformat (input, "tag %_%v%_", &tag))
1503 ;
Florin Coras1c710452017-10-17 00:03:13 -07001504 else
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001505 {
1506 error = clib_error_return (0, "unknown input `%U'",
1507 format_unformat_error, input);
1508 goto done;
1509 }
Florin Coras1c710452017-10-17 00:03:13 -07001510 }
1511
Florin Corasc97a7392017-11-05 23:07:07 -08001512 if (proto == ~0)
1513 {
1514 vlib_cli_output (vm, "proto must be set");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001515 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001516 }
1517 if (is_add && !conn_set && action == ~0)
1518 {
1519 vlib_cli_output (vm, "connection and action must be set for add");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001520 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001521 }
1522 if (!is_add && !tag && !conn_set)
1523 {
1524 vlib_cli_output (vm, "connection or tag must be set for delete");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001525 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001526 }
1527 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1528 {
1529 vlib_cli_output (vm, "tag too long (max u64)");
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001530 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001531 }
Florin Coras1c710452017-10-17 00:03:13 -07001532
1533 if (ns_id)
1534 {
1535 app_ns = app_namespace_get_from_id (ns_id);
1536 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001537 {
1538 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001539 goto done;
Florin Corasc97a7392017-11-05 23:07:07 -08001540 }
Florin Coras1c710452017-10-17 00:03:13 -07001541 }
1542 else
1543 {
1544 app_ns = app_namespace_get_default ();
1545 }
1546 appns_index = app_namespace_index (app_ns);
1547
1548 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1549 session_rule_add_del_args_t args = {
zhanglimaoa04ebb82019-03-13 10:36:54 +08001550 .transport_proto = proto,
Florin Coras1c710452017-10-17 00:03:13 -07001551 .table_args.lcl.fp_addr = lcl_ip,
1552 .table_args.lcl.fp_len = lcl_plen,
1553 .table_args.lcl.fp_proto = fib_proto,
1554 .table_args.rmt.fp_addr = rmt_ip,
1555 .table_args.rmt.fp_len = rmt_plen,
1556 .table_args.rmt.fp_proto = fib_proto,
1557 .table_args.lcl_port = lcl_port,
1558 .table_args.rmt_port = rmt_port,
1559 .table_args.action_index = action,
1560 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001561 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001562 .appns_index = appns_index,
1563 .scope = scope,
1564 };
Florin Corasc1a42652019-02-08 18:27:29 -08001565 if ((rv = vnet_session_rule_add_del (&args)))
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001566 error = clib_error_return (0, "rule add del returned %u", rv);
Florin Corasc1a42652019-02-08 18:27:29 -08001567
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001568done:
1569 vec_free (ns_id);
Florin Corasc97a7392017-11-05 23:07:07 -08001570 vec_free (tag);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +02001571 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001572}
1573
1574/* *INDENT-OFF* */
1575VLIB_CLI_COMMAND (session_rule_command, static) =
1576{
1577 .path = "session rule",
1578 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1579 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1580 .function = session_rule_command_fn,
1581};
1582/* *INDENT-ON* */
1583
Florin Coras7999e832017-10-31 01:51:04 -07001584void
1585session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1586 u8 transport_proto)
1587{
1588 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001589 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001590 session_table_t *st;
1591 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001592 srt = &st->session_rules[transport_proto];
1593 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001594}
1595
1596void
1597session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1598 u8 transport_proto)
1599{
1600 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001601 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001602 session_table_t *st;
1603 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001604 srt = &st->session_rules[transport_proto];
1605 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001606}
1607
Florin Coras1c710452017-10-17 00:03:13 -07001608static clib_error_t *
1609show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1610 vlib_cli_command_t * cmd)
1611{
1612 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1613 u32 fib_index, scope = 0;
1614 ip46_address_t lcl_ip, rmt_ip;
1615 u8 is_ip4 = 1, show_one = 0;
1616 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001617 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001618 session_table_t *st;
1619 u8 *ns_id = 0, fib_proto;
1620
Guanghua Zhangfcd5e122019-08-24 10:52:19 +08001621 session_cli_return_if_not_enabled ();
1622
Dave Barachb7b92992018-10-17 10:38:51 -04001623 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1624 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
Florin Coras1c710452017-10-17 00:03:13 -07001625 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1626 {
1627 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1628 ;
1629 else if (unformat (input, "appns %_%v%_", &ns_id))
1630 ;
1631 else if (unformat (input, "scope global"))
1632 scope = 1;
1633 else if (unformat (input, "scope local"))
1634 scope = 2;
1635 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1636 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1637 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1638 &rmt_port))
1639 {
1640 is_ip4 = 1;
1641 show_one = 1;
1642 }
1643 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1644 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1645 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1646 &rmt_port))
1647 {
1648 is_ip4 = 0;
1649 show_one = 1;
1650 }
1651 else
1652 return clib_error_return (0, "unknown input `%U'",
1653 format_unformat_error, input);
1654 }
1655
1656 if (transport_proto == ~0)
1657 {
1658 vlib_cli_output (vm, "transport proto must be set");
1659 return 0;
1660 }
1661
1662 if (ns_id)
1663 {
1664 app_ns = app_namespace_get_from_id (ns_id);
1665 if (!app_ns)
1666 {
1667 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1668 return 0;
1669 }
1670 }
1671 else
1672 {
1673 app_ns = app_namespace_get_default ();
1674 }
1675
1676 if (scope == 1 || scope == 0)
1677 {
1678 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1679 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1680 st = session_table_get_for_fib_index (fib_proto, fib_index);
1681 }
1682 else
1683 {
1684 st = app_namespace_get_local_table (app_ns);
1685 }
1686
1687 if (show_one)
1688 {
Florin Corasc97a7392017-11-05 23:07:07 -08001689 srt = &st->session_rules[transport_proto];
1690 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1691 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001692 return 0;
1693 }
1694
Florin Corasc97a7392017-11-05 23:07:07 -08001695 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1696 transport_proto);
1697 srt = &st->session_rules[transport_proto];
1698 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1699 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001700
1701 vec_free (ns_id);
1702 return 0;
1703}
1704
1705/* *INDENT-OFF* */
1706VLIB_CLI_COMMAND (show_session_rules_command, static) =
1707{
1708 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001709 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1710 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001711 .function = show_session_rules_command_fn,
1712};
1713/* *INDENT-ON* */
1714
Florin Coras04e53442017-07-16 17:12:15 -07001715void
1716session_lookup_init (void)
1717{
Florin Corascea194d2017-10-02 00:18:51 -07001718 /*
1719 * Allocate default table and map it to fib_index 0
1720 */
1721 session_table_t *st = session_table_alloc ();
1722 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1723 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001724 st->active_fib_proto = FIB_PROTOCOL_IP4;
1725 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001726 st = session_table_alloc ();
1727 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1728 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001729 st->active_fib_proto = FIB_PROTOCOL_IP6;
1730 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001731}
1732
1733/*
1734 * fd.io coding-style-patch-verification: ON
1735 *
1736 * Local Variables:
1737 * eval: (c-set-style "gnu")
1738 * End:
1739 */