blob: 9ce0b1a22eb97b348cb798e6b958391ffc2317d6 [file] [log] [blame]
Florin Coras04e53442017-07-16 17:12:15 -07001/*
2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/** 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/**
33 * External vector of per transport virtual functions table
34 */
Florin Coras04e53442017-07-16 17:12:15 -070035extern transport_proto_vft_t *tp_vfts;
36
Florin Corascea194d2017-10-02 00:18:51 -070037/**
38 * Network namespace index (i.e., fib index) to session lookup table. We
39 * should have one per network protocol type but for now we only support IP4/6
40 */
41static u32 *fib_index_to_table_index[2];
42
Florin Coras04e53442017-07-16 17:12:15 -070043/* *INDENT-OFF* */
44/* 16 octets */
45typedef CLIB_PACKED (struct {
46 union
47 {
48 struct
49 {
50 ip4_address_t src;
51 ip4_address_t dst;
52 u16 src_port;
53 u16 dst_port;
54 /* align by making this 4 octets even though its a 1-bit field
55 * NOTE: avoid key overlap with other transports that use 5 tuples for
56 * session identification.
57 */
58 u32 proto;
59 };
60 u64 as_u64[2];
61 };
62}) v4_connection_key_t;
63
64typedef CLIB_PACKED (struct {
65 union
66 {
67 struct
68 {
69 /* 48 octets */
70 ip6_address_t src;
71 ip6_address_t dst;
72 u16 src_port;
73 u16 dst_port;
74 u32 proto;
75 u64 unused;
76 };
77 u64 as_u64[6];
78 };
79}) v6_connection_key_t;
80/* *INDENT-ON* */
81
82typedef clib_bihash_kv_16_8_t session_kv4_t;
83typedef clib_bihash_kv_48_8_t session_kv6_t;
84
85always_inline void
86make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
87 u16 lcl_port, u16 rmt_port, u8 proto)
88{
89 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
90
91 key->src.as_u32 = lcl->as_u32;
92 key->dst.as_u32 = rmt->as_u32;
93 key->src_port = lcl_port;
94 key->dst_port = rmt_port;
95 key->proto = proto;
96
97 kv->value = ~0ULL;
98}
99
100always_inline void
101make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
102 u8 proto)
103{
104 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
105
106 key->src.as_u32 = lcl->as_u32;
107 key->dst.as_u32 = 0;
108 key->src_port = lcl_port;
109 key->dst_port = 0;
110 key->proto = proto;
111
112 kv->value = ~0ULL;
113}
114
115always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800116make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
117{
118 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
119
120 key->src.as_u32 = lcl->as_u32;
121 key->dst.as_u32 = 0;
122 key->src_port = 0;
123 key->dst_port = 0;
124 key->proto = proto;
125
126 kv->value = ~0ULL;
127}
128
129always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800130make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700131{
Florin Coras561af9b2017-12-09 10:19:43 -0800132 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
133 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700134}
135
136always_inline void
137make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
138 u16 lcl_port, u16 rmt_port, u8 proto)
139{
140 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
141
142 key->src.as_u64[0] = lcl->as_u64[0];
143 key->src.as_u64[1] = lcl->as_u64[1];
144 key->dst.as_u64[0] = rmt->as_u64[0];
145 key->dst.as_u64[1] = rmt->as_u64[1];
146 key->src_port = lcl_port;
147 key->dst_port = rmt_port;
148 key->proto = proto;
149 key->unused = 0;
150
151 kv->value = ~0ULL;
152}
153
154always_inline void
155make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
156 u8 proto)
157{
158 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
159
160 key->src.as_u64[0] = lcl->as_u64[0];
161 key->src.as_u64[1] = lcl->as_u64[1];
162 key->dst.as_u64[0] = 0;
163 key->dst.as_u64[1] = 0;
164 key->src_port = lcl_port;
165 key->dst_port = 0;
166 key->proto = proto;
167 key->unused = 0;
168
169 kv->value = ~0ULL;
170}
171
172always_inline void
Florin Corasdbd44562017-11-09 19:30:17 -0800173make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
174{
175 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
176
177 key->src.as_u64[0] = lcl->as_u64[0];
178 key->src.as_u64[1] = lcl->as_u64[1];
179 key->dst.as_u64[0] = 0;
180 key->dst.as_u64[1] = 0;
181 key->src_port = 0;
182 key->dst_port = 0;
183 key->proto = proto;
184 key->unused = 0;
185
186 kv->value = ~0ULL;
187}
188
189always_inline void
Florin Coras561af9b2017-12-09 10:19:43 -0800190make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700191{
Florin Coras561af9b2017-12-09 10:19:43 -0800192 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
193 tc->rmt_port, tc->proto);
Florin Coras04e53442017-07-16 17:12:15 -0700194}
195
Florin Corascea194d2017-10-02 00:18:51 -0700196static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700197session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700198{
Florin Corascea194d2017-10-02 00:18:51 -0700199 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700200 u32 table_index;
201 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
Florin Corascea194d2017-10-02 00:18:51 -0700202 {
203 st = session_table_alloc ();
204 table_index = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -0700205 vec_validate (fib_index_to_table_index[fib_proto], fib_index);
206 fib_index_to_table_index[fib_proto][fib_index] = table_index;
207 st->active_fib_proto = fib_proto;
Florin Corasb795bd02017-12-14 11:30:48 -0800208 session_table_init (st, fib_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700209 return st;
210 }
211 else
212 {
Florin Coras6c36f532017-11-03 18:32:34 -0700213 table_index = fib_index_to_table_index[fib_proto][fib_index];
Florin Corascea194d2017-10-02 00:18:51 -0700214 return session_table_get (table_index);
215 }
216}
217
218static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700219session_table_get_or_alloc_for_connection (transport_connection_t * tc)
220{
221 u32 fib_proto;
222 fib_proto = transport_connection_fib_proto (tc);
223 return session_table_get_or_alloc (fib_proto, tc->fib_index);
224}
225
226static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700227session_table_get_for_connection (transport_connection_t * tc)
228{
229 u32 fib_proto = transport_connection_fib_proto (tc);
230 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
231 return 0;
232 return
233 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
234}
235
236static session_table_t *
237session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
238{
239 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
240 return 0;
241 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
242}
243
244u32
245session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
246{
247 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
248 return SESSION_TABLE_INVALID_INDEX;
249 return fib_index_to_table_index[fib_proto][fib_index];
250}
251
252/**
253 * Add transport connection to a session table
254 *
255 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
256 * is added to requested session table.
257 *
258 * @param tc transport connection to be added
259 * @param value value to be stored
260 *
261 * @return non-zero if failure
262 */
263int
264session_lookup_add_connection (transport_connection_t * tc, u64 value)
265{
266 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700267 session_kv4_t kv4;
268 session_kv6_t kv6;
269
Florin Corascea194d2017-10-02 00:18:51 -0700270 st = session_table_get_or_alloc_for_connection (tc);
271 if (!st)
272 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700273 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700274 {
Florin Coras04e53442017-07-16 17:12:15 -0700275 make_v4_ss_kv_from_tc (&kv4, tc);
276 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700277 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
278 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700279 }
280 else
281 {
Florin Coras04e53442017-07-16 17:12:15 -0700282 make_v6_ss_kv_from_tc (&kv6, tc);
283 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700284 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
285 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700286 }
287}
288
Florin Coras04e53442017-07-16 17:12:15 -0700289int
Florin Corascea194d2017-10-02 00:18:51 -0700290session_lookup_add_session_endpoint (u32 table_index,
291 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700292{
Florin Corascea194d2017-10-02 00:18:51 -0700293 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700294 session_kv4_t kv4;
295 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700296
Florin Corascea194d2017-10-02 00:18:51 -0700297 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 kv4.value = value;
305 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
306 }
307 else
308 {
309 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
310 sep->transport_proto);
311 kv6.value = value;
312 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
313 }
314}
315
316int
317session_lookup_del_session_endpoint (u32 table_index,
318 session_endpoint_t * sep)
319{
320 session_table_t *st;
321 session_kv4_t kv4;
322 session_kv6_t kv6;
323
324 st = session_table_get (table_index);
325 if (!st)
326 return -1;
327 if (sep->is_ip4)
328 {
329 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
330 sep->transport_proto);
331 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
332 }
333 else
334 {
335 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
336 sep->transport_proto);
337 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
338 }
339}
340
341/**
342 * Delete transport connection from session table
343 *
344 * @param table_index session table index
345 * @param tc transport connection to be removed
346 *
347 * @return non-zero if failure
348 */
349int
350session_lookup_del_connection (transport_connection_t * tc)
351{
352 session_table_t *st;
353 session_kv4_t kv4;
354 session_kv6_t kv6;
355
356 st = session_table_get_for_connection (tc);
357 if (!st)
358 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700359 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700360 {
Florin Coras04e53442017-07-16 17:12:15 -0700361 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700362 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700363 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700364 }
365 else
366 {
Florin Coras04e53442017-07-16 17:12:15 -0700367 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700368 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700369 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700370 }
Florin Coras04e53442017-07-16 17:12:15 -0700371}
372
373int
Florin Corascea194d2017-10-02 00:18:51 -0700374session_lookup_del_session (stream_session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700375{
Florin Coras561af9b2017-12-09 10:19:43 -0800376 transport_proto_t tp = session_get_transport_proto (s);
Florin Coras04e53442017-07-16 17:12:15 -0700377 transport_connection_t *ts;
Florin Coras561af9b2017-12-09 10:19:43 -0800378 ts = tp_vfts[tp].get_connection (s->connection_index, s->thread_index);
Florin Corascea194d2017-10-02 00:18:51 -0700379 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700380}
381
Florin Corasdff48db2017-11-19 18:06:58 -0800382static u8
383session_lookup_action_index_is_valid (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700384{
Florin Corasdff48db2017-11-19 18:06:58 -0800385 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
386 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
387 return 0;
388 return 1;
389}
390
Florin Corasf8f516a2018-02-08 15:10:09 -0800391static u64
392session_lookup_action_to_handle (u32 action_index)
Florin Corasdff48db2017-11-19 18:06:58 -0800393{
394 switch (action_index)
395 {
396 case SESSION_RULES_TABLE_ACTION_DROP:
Florin Corasf8f516a2018-02-08 15:10:09 -0800397 return SESSION_DROP_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800398 case SESSION_RULES_TABLE_ACTION_ALLOW:
399 case SESSION_RULES_TABLE_INVALID_INDEX:
Florin Corasf8f516a2018-02-08 15:10:09 -0800400 return SESSION_INVALID_HANDLE;
Florin Corasdff48db2017-11-19 18:06:58 -0800401 default:
Florin Corasf8f516a2018-02-08 15:10:09 -0800402 /* application index */
Florin Corasdff48db2017-11-19 18:06:58 -0800403 return action_index;
404 }
Florin Corasf0c1c962017-11-02 21:31:46 -0700405}
406
Florin Coras1c710452017-10-17 00:03:13 -0700407static stream_session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700408session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
409 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700410{
411 application_t *app;
Florin Corasdff48db2017-11-19 18:06:58 -0800412 app = application_get_if_valid (app_index);
Florin Coras1c710452017-10-17 00:03:13 -0700413 if (!app)
414 return 0;
415
Florin Coras7999e832017-10-31 01:51:04 -0700416 return application_first_listener (app, fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700417}
418
Florin Corasa2ff7b82017-11-08 17:55:03 -0800419static stream_session_t *
420session_lookup_action_to_session (u32 action_index, u8 fib_proto,
421 u8 transport_proto)
422{
Florin Corasdff48db2017-11-19 18:06:58 -0800423 u32 app_index;
Florin Corasf8f516a2018-02-08 15:10:09 -0800424 app_index = session_lookup_action_to_handle (action_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800425 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800426 return session_lookup_app_listen_session (app_index, fib_proto,
Florin Corasa2ff7b82017-11-08 17:55:03 -0800427 transport_proto);
428}
429
Florin Corasf8f516a2018-02-08 15:10:09 -0800430/** UNUSED */
Florin Coras1c710452017-10-17 00:03:13 -0700431stream_session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800432session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
433 ip4_address_t * lcl, u16 lcl_port,
434 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700435{
Florin Corasc97a7392017-11-05 23:07:07 -0800436 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800437 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800438 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700439 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800440 app_index = session_lookup_action_to_handle (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700441 /* Nothing sophisticated for now, action index is app index */
Florin Corasdff48db2017-11-19 18:06:58 -0800442 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700443 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700444}
445
Florin Corasf8f516a2018-02-08 15:10:09 -0800446/** UNUSED */
Florin Coras1c710452017-10-17 00:03:13 -0700447stream_session_t *
Florin Corasdff48db2017-11-19 18:06:58 -0800448session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
449 ip6_address_t * lcl, u16 lcl_port,
450 ip6_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700451{
Florin Corasc97a7392017-11-05 23:07:07 -0800452 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasdff48db2017-11-19 18:06:58 -0800453 u32 action_index, app_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800454 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700455 rmt_port);
Florin Corasf8f516a2018-02-08 15:10:09 -0800456 app_index = session_lookup_action_to_handle (action_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800457 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700458 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700459}
460
Florin Corasa2ff7b82017-11-08 17:55:03 -0800461/**
462 * Lookup listener for session endpoint in table
463 *
464 * @param table_index table where the endpoint should be looked up
465 * @param sep session endpoint to be looked up
466 * @param use_rules flag that indicates if the session rules of the table
467 * should be used
468 * @return invalid handle if nothing is found, the handle of a valid listener
Florin Corasf8f516a2018-02-08 15:10:09 -0800469 * or an action derived handle if a rule is hit
Florin Corasa2ff7b82017-11-08 17:55:03 -0800470 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700471u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800472session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
473 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700474{
Florin Corasc97a7392017-11-05 23:07:07 -0800475 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700476 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700477 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700478 int rv;
Florin Coras04e53442017-07-16 17:12:15 -0700479
Florin Corascea194d2017-10-02 00:18:51 -0700480 st = session_table_get (table_index);
481 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700482 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700483 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700484 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800485 session_kv4_t kv4;
486 ip4_address_t lcl4;
487
Florin Coras561af9b2017-12-09 10:19:43 -0800488 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
489 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700490 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
491 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700492 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800493 if (use_rules)
494 {
495 memset (&lcl4, 0, sizeof (lcl4));
496 srt = &st->session_rules[sep->transport_proto];
497 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
498 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800499 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800500 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800501 }
Florin Coras68810622017-07-24 17:40:28 -0700502 }
503 else
504 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800505 session_kv6_t kv6;
506 ip6_address_t lcl6;
507
Florin Coras561af9b2017-12-09 10:19:43 -0800508 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
509 sep->transport_proto);
Florin Corascea194d2017-10-02 00:18:51 -0700510 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
511 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700512 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700513
Florin Corasa2ff7b82017-11-08 17:55:03 -0800514 if (use_rules)
515 {
516 memset (&lcl6, 0, sizeof (lcl6));
517 srt = &st->session_rules[sep->transport_proto];
518 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
519 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800520 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800521 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800522 }
Florin Coras68810622017-07-24 17:40:28 -0700523 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700524 return SESSION_INVALID_HANDLE;
525}
526
Florin Corasa2ff7b82017-11-08 17:55:03 -0800527/**
528 * Look up endpoint in local session table
529 *
530 * The result, for now, is an application index and it may in the future
531 * be extended to a more complicated "action object". The only action we
532 * emulate now is "drop" and for that we return a special app index.
533 *
534 * Lookup logic is to check in order:
535 * - the rules in the table (connect acls)
536 * - session sub-table for a listener
537 * - session sub-table for a local listener (zeroed addr)
538 *
539 * @param table_index table where the lookup should be done
540 * @param sep session endpoint to be looked up
Florin Corasf8f516a2018-02-08 15:10:09 -0800541 * @return session handle that can be interpreted as an adjacency
Florin Corasa2ff7b82017-11-08 17:55:03 -0800542 */
Florin Corasf8f516a2018-02-08 15:10:09 -0800543u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800544session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700545{
Florin Corasc97a7392017-11-05 23:07:07 -0800546 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700547 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700548 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700549 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700550
Florin Corascea194d2017-10-02 00:18:51 -0700551 st = session_table_get (table_index);
552 if (!st)
553 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800554 ASSERT (st->is_local);
555
Florin Corascea194d2017-10-02 00:18:51 -0700556 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700557 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800558 session_kv4_t kv4;
559 ip4_address_t lcl4;
560
561 /*
562 * Check if endpoint has special rules associated
563 */
564 memset (&lcl4, 0, sizeof (lcl4));
565 srt = &st->session_rules[sep->transport_proto];
566 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
567 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800568 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800569 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800570
571 /*
572 * Check if session endpoint is a listener
573 */
Florin Corascea194d2017-10-02 00:18:51 -0700574 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
575 sep->transport_proto);
576 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
577 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800578 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700579
580 /*
581 * Zero out the ip. Logic is that connect to local ips, say
582 * 127.0.0.1:port, can match 0.0.0.0:port
583 */
584 kv4.key[0] = 0;
585 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
586 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800587 return kv4.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800588
589 /*
590 * Zero out the port and check if we have proxy
591 */
592 kv4.key[1] = 0;
593 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
594 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800595 return kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700596 }
597 else
598 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800599 session_kv6_t kv6;
600 ip6_address_t lcl6;
601
602 memset (&lcl6, 0, sizeof (lcl6));
603 srt = &st->session_rules[sep->transport_proto];
604 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
605 sep->port);
Florin Corasdff48db2017-11-19 18:06:58 -0800606 if (session_lookup_action_index_is_valid (ai))
Florin Corasf8f516a2018-02-08 15:10:09 -0800607 return session_lookup_action_to_handle (ai);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800608
Florin Corascea194d2017-10-02 00:18:51 -0700609 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
610 sep->transport_proto);
611 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
612 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800613 return kv6.value;
Florin Corascea194d2017-10-02 00:18:51 -0700614
615 /*
616 * Zero out the ip. Same logic as above.
617 */
618 kv6.key[0] = kv6.key[1] = 0;
619 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
620 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800621 return kv6.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800622
623 /*
624 * Zero out the port. Same logic as above.
625 */
626 kv6.key[4] = kv6.key[5] = 0;
627 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
628 if (rv == 0)
Florin Corasf8f516a2018-02-08 15:10:09 -0800629 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700630 }
Florin Corasf8f516a2018-02-08 15:10:09 -0800631 return SESSION_INVALID_HANDLE;
Florin Coras04e53442017-07-16 17:12:15 -0700632}
633
Florin Corascea194d2017-10-02 00:18:51 -0700634static stream_session_t *
635session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
636 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700637{
Florin Coras04e53442017-07-16 17:12:15 -0700638 session_kv4_t kv4;
639 int rv;
Florin Coras561af9b2017-12-09 10:19:43 -0800640 session_type_t session_type;
Florin Coras04e53442017-07-16 17:12:15 -0700641
Florin Corasdbd44562017-11-09 19:30:17 -0800642 /*
643 * First, try a fully formed listener
644 */
Florin Coras561af9b2017-12-09 10:19:43 -0800645 session_type = session_type_from_proto_and_ip (proto, 1);
Florin Coras04e53442017-07-16 17:12:15 -0700646 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700647 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700648 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800649 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700650
Florin Corasdbd44562017-11-09 19:30:17 -0800651 /*
652 * Zero out the lcl ip and check if any 0/0 port binds have been done
653 */
Florin Coras04e53442017-07-16 17:12:15 -0700654 kv4.key[0] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700655 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700656 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800657 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700658
Florin Corasdbd44562017-11-09 19:30:17 -0800659 /*
660 * Zero out port and check if we have a proxy set up for our ip
661 */
662 make_v4_proxy_kv (&kv4, lcl, proto);
663 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
664 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800665 return session_manager_get_listener (session_type, (u32) kv4.value);
Florin Corasdbd44562017-11-09 19:30:17 -0800666
Florin Coras04e53442017-07-16 17:12:15 -0700667 return 0;
668}
669
Florin Coras04e53442017-07-16 17:12:15 -0700670stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700671session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
672 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700673{
Florin Corascea194d2017-10-02 00:18:51 -0700674 session_table_t *st;
675 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
676 if (!st)
677 return 0;
678 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700679}
680
Florin Corascea194d2017-10-02 00:18:51 -0700681static stream_session_t *
682session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
683 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700684{
Florin Coras04e53442017-07-16 17:12:15 -0700685 session_kv6_t kv6;
686 int rv;
Florin Coras561af9b2017-12-09 10:19:43 -0800687 session_type_t session_type;
Florin Coras04e53442017-07-16 17:12:15 -0700688
Florin Coras561af9b2017-12-09 10:19:43 -0800689 session_type = session_type_from_proto_and_ip (proto, 0);
Florin Coras04e53442017-07-16 17:12:15 -0700690 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700691 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700692 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800693 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700694
695 /* Zero out the lcl ip */
696 kv6.key[0] = kv6.key[1] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700697 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700698 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800699 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700700
Florin Corasdbd44562017-11-09 19:30:17 -0800701 make_v6_proxy_kv (&kv6, lcl, proto);
702 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
703 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800704 return session_manager_get_listener (session_type, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700705 return 0;
706}
707
Florin Coras04e53442017-07-16 17:12:15 -0700708stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700709session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
710 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700711{
Florin Corascea194d2017-10-02 00:18:51 -0700712 session_table_t *st;
713 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
714 if (!st)
715 return 0;
716 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700717}
718
719stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700720session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700721{
Florin Corascea194d2017-10-02 00:18:51 -0700722 session_table_t *st;
723 st = session_table_get (table_index);
724 if (!st)
725 return 0;
726 if (sep->is_ip4)
727 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
728 sep->transport_proto);
729 else
730 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
731 sep->transport_proto);
Florin Coras04e53442017-07-16 17:12:15 -0700732 return 0;
733}
734
Florin Corascea194d2017-10-02 00:18:51 -0700735int
736session_lookup_add_half_open (transport_connection_t * tc, u64 value)
737{
738 session_table_t *st;
739 session_kv4_t kv4;
740 session_kv6_t kv6;
741
742 st = session_table_get_or_alloc_for_connection (tc);
743 if (!st)
744 return 0;
745 if (tc->is_ip4)
746 {
747 make_v4_ss_kv_from_tc (&kv4, tc);
748 kv4.value = value;
749 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
750 1 /* is_add */ );
751 }
752 else
753 {
754 make_v6_ss_kv_from_tc (&kv6, tc);
755 kv6.value = value;
756 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
757 1 /* is_add */ );
758 }
759}
760
761int
762session_lookup_del_half_open (transport_connection_t * tc)
763{
764 session_table_t *st;
765 session_kv4_t kv4;
766 session_kv6_t kv6;
767
768 st = session_table_get_for_connection (tc);
769 if (!st)
770 return -1;
771 if (tc->is_ip4)
772 {
773 make_v4_ss_kv_from_tc (&kv4, tc);
774 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
775 0 /* is_add */ );
776 }
777 else
778 {
779 make_v6_ss_kv_from_tc (&kv6, tc);
780 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
781 0 /* is_add */ );
782 }
783}
784
Florin Coras04e53442017-07-16 17:12:15 -0700785u64
Florin Corascea194d2017-10-02 00:18:51 -0700786session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700787{
Florin Corascea194d2017-10-02 00:18:51 -0700788 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700789 session_kv4_t kv4;
790 session_kv6_t kv6;
791 int rv;
792
Florin Corascea194d2017-10-02 00:18:51 -0700793 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
794 tc->fib_index);
795 if (!st)
796 return HALF_OPEN_LOOKUP_INVALID_VALUE;
797 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700798 {
Florin Corascea194d2017-10-02 00:18:51 -0700799 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700800 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700801 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700802 if (rv == 0)
803 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700804 }
805 else
806 {
807 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700808 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700809 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700810 if (rv == 0)
811 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700812 }
813 return HALF_OPEN_LOOKUP_INVALID_VALUE;
814}
815
816transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700817session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700818{
Florin Corascea194d2017-10-02 00:18:51 -0700819 u32 sst;
820
Florin Coras04e53442017-07-16 17:12:15 -0700821 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700822 {
823 sst = session_type_from_proto_and_ip (proto, is_ip4);
824 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
825 }
Florin Coras04e53442017-07-16 17:12:15 -0700826 return 0;
827}
828
Florin Corascea194d2017-10-02 00:18:51 -0700829/**
830 * Lookup connection with ip4 and transport layer information
831 *
832 * This is used on the fast path so it needs to be fast. Thereby,
833 * duplication of code and 'hacks' allowed.
834 *
835 * The lookup is incremental and returns whenever something is matched. The
836 * steps are:
837 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700838 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700839 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800840 * - Try to find a fully-formed or local source wildcarded (listener bound to
841 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700842 * - return 0
843 *
844 * @param fib_index index of fib wherein the connection was received
845 * @param lcl local ip4 address
846 * @param rmt remote ip4 address
847 * @param lcl_port local port
848 * @param rmt_port remote port
849 * @param proto transport protocol (e.g., tcp, udp)
850 * @param thread_index thread index for request
Florin Corasdff48db2017-11-19 18:06:58 -0800851 * @param is_filtered return flag that indicates if connection was filtered.
Florin Corascea194d2017-10-02 00:18:51 -0700852 *
853 * @return pointer to transport connection, if one is found, 0 otherwise
854 */
Florin Coras04e53442017-07-16 17:12:15 -0700855transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700856session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
857 ip4_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -0800858 u16 rmt_port, u8 proto, u32 thread_index,
859 u8 * is_filtered)
Florin Coras04e53442017-07-16 17:12:15 -0700860{
Florin Corascea194d2017-10-02 00:18:51 -0700861 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700862 session_kv4_t kv4;
863 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800864 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700865 int rv;
866
Florin Corascea194d2017-10-02 00:18:51 -0700867 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
868 if (PREDICT_FALSE (!st))
869 return 0;
870
Florin Corasa2ff7b82017-11-08 17:55:03 -0800871 /*
872 * Lookup session amongst established ones
873 */
Florin Coras04e53442017-07-16 17:12:15 -0700874 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700875 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700876 if (rv == 0)
877 {
Florin Corascea194d2017-10-02 00:18:51 -0700878 ASSERT ((u32) (kv4.value >> 32) == thread_index);
879 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras561af9b2017-12-09 10:19:43 -0800880 return tp_vfts[proto].get_connection (s->connection_index,
881 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700882 }
883
Florin Corasa2ff7b82017-11-08 17:55:03 -0800884 /*
885 * Try half-open connections
886 */
Florin Corascea194d2017-10-02 00:18:51 -0700887 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700888 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800889 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
Florin Coras1c710452017-10-17 00:03:13 -0700890
Florin Corasa2ff7b82017-11-08 17:55:03 -0800891 /*
892 * Check the session rules table
893 */
894 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
895 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800896 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800897 {
Florin Corasdff48db2017-11-19 18:06:58 -0800898 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
899 return 0;
900 if ((s = session_lookup_action_to_session (action_index,
901 FIB_PROTOCOL_IP4, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -0800902 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800903 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800904 }
905
906 /*
907 * If nothing is found, check if any listener is available
908 */
909 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
910 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -0800911 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800912
913 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700914}
915
Florin Corascea194d2017-10-02 00:18:51 -0700916/**
917 * Lookup connection with ip4 and transport layer information
918 *
919 * Not optimized. This is used on the fast path so it needs to be fast.
920 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
921 * to that of @ref session_lookup_connection_wt4
922 *
923 * @param fib_index index of the fib wherein the connection was received
924 * @param lcl local ip4 address
925 * @param rmt remote ip4 address
926 * @param lcl_port local port
927 * @param rmt_port remote port
928 * @param proto transport protocol (e.g., tcp, udp)
929 *
930 * @return pointer to transport connection, if one is found, 0 otherwise
931 */
Florin Coras04e53442017-07-16 17:12:15 -0700932transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700933session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
934 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
935 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700936{
Florin Corascea194d2017-10-02 00:18:51 -0700937 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700938 session_kv4_t kv4;
939 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800940 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700941 int rv;
942
Florin Corascea194d2017-10-02 00:18:51 -0700943 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
944 if (PREDICT_FALSE (!st))
945 return 0;
946
Florin Corasa2ff7b82017-11-08 17:55:03 -0800947 /*
948 * Lookup session amongst established ones
949 */
Florin Coras04e53442017-07-16 17:12:15 -0700950 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700951 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700952 if (rv == 0)
953 {
Florin Corascea194d2017-10-02 00:18:51 -0700954 s = session_get_from_handle (kv4.value);
Florin Coras561af9b2017-12-09 10:19:43 -0800955 return tp_vfts[proto].get_connection (s->connection_index,
956 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700957 }
958
Florin Corasa2ff7b82017-11-08 17:55:03 -0800959 /*
960 * Try half-open connections
961 */
Florin Corascea194d2017-10-02 00:18:51 -0700962 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700963 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -0800964 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800965
966 /*
967 * Check the session rules table
968 */
969 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
970 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -0800971 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -0800972 {
Florin Corasdff48db2017-11-19 18:06:58 -0800973 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
974 return 0;
975 if ((s = session_lookup_action_to_session (action_index,
976 FIB_PROTOCOL_IP4, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -0800977 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -0800978 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800979 }
980
981 /*
982 * If nothing is found, check if any listener is available
983 */
984 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
985 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -0800986 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800987
988 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700989}
990
Florin Corascea194d2017-10-02 00:18:51 -0700991/**
992 * Lookup session with ip4 and transport layer information
993 *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700994 * Important note: this may look into another thread's pool table and
995 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
996 * if needed as soon as possible.
997 *
998 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
999 * this returns a session as opposed to a transport connection and it does not
1000 * try to lookup half-open sessions.
1001 *
1002 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001003 */
1004stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001005session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1006 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001007{
Florin Corascea194d2017-10-02 00:18:51 -07001008 session_table_t *st;
1009 session_kv4_t kv4;
1010 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001011 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001012 int rv;
1013
1014 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1015 if (PREDICT_FALSE (!st))
1016 return 0;
1017
Florin Corasa2ff7b82017-11-08 17:55:03 -08001018 /*
1019 * Lookup session amongst established ones
1020 */
Florin Corascea194d2017-10-02 00:18:51 -07001021 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1022 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1023 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001024 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001025
Florin Corasa2ff7b82017-11-08 17:55:03 -08001026 /*
1027 * Check the session rules table
1028 */
1029 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1030 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001031 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001032 {
Florin Corasdff48db2017-11-19 18:06:58 -08001033 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1034 return 0;
1035 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1036 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001037 }
1038
1039 /*
1040 * If nothing is found, check if any listener is available
1041 */
Florin Corascea194d2017-10-02 00:18:51 -07001042 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
1043 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001044
1045 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001046}
1047
1048/**
1049 * Lookup connection with ip6 and transport layer information
1050 *
1051 * This is used on the fast path so it needs to be fast. Thereby,
1052 * duplication of code and 'hacks' allowed.
1053 *
1054 * The lookup is incremental and returns whenever something is matched. The
1055 * steps are:
1056 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001057 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001058 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001059 * - Try to find a fully-formed or local source wildcarded (listener bound to
1060 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001061 * - return 0
1062 *
1063 * @param fib_index index of the fib wherein the connection was received
1064 * @param lcl local ip6 address
1065 * @param rmt remote ip6 address
1066 * @param lcl_port local port
1067 * @param rmt_port remote port
1068 * @param proto transport protocol (e.g., tcp, udp)
1069 * @param thread_index thread index for request
1070 *
1071 * @return pointer to transport connection, if one is found, 0 otherwise
1072 */
1073transport_connection_t *
1074session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1075 ip6_address_t * rmt, u16 lcl_port,
Florin Corasdff48db2017-11-19 18:06:58 -08001076 u16 rmt_port, u8 proto, u32 thread_index,
1077 u8 * is_filtered)
Florin Corascea194d2017-10-02 00:18:51 -07001078{
1079 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001080 stream_session_t *s;
1081 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001082 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001083 int rv;
1084
Florin Corascea194d2017-10-02 00:18:51 -07001085 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1086 if (PREDICT_FALSE (!st))
1087 return 0;
1088
Florin Coras04e53442017-07-16 17:12:15 -07001089 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001090 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001091 if (rv == 0)
1092 {
Florin Corascea194d2017-10-02 00:18:51 -07001093 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1094 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001095 return tp_vfts[proto].get_connection (s->connection_index,
1096 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001097 }
1098
Florin Corasa2ff7b82017-11-08 17:55:03 -08001099 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001100 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001101 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -08001102 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001103
Florin Corasa2ff7b82017-11-08 17:55:03 -08001104 /* Check the session rules table */
1105 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1106 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001107 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001108 {
Florin Corasdff48db2017-11-19 18:06:58 -08001109 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
1110 return 0;
1111 if ((s = session_lookup_action_to_session (action_index,
1112 FIB_PROTOCOL_IP6, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -08001113 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001114 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001115 }
1116
1117 /* If nothing is found, check if any listener is available */
1118 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1119 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -08001120 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001121
1122 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001123}
1124
Florin Corascea194d2017-10-02 00:18:51 -07001125/**
1126 * Lookup connection with ip6 and transport layer information
1127 *
1128 * Not optimized. This is used on the fast path so it needs to be fast.
1129 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1130 * to that of @ref session_lookup_connection_wt4
1131 *
1132 * @param fib_index index of the fib wherein the connection was received
1133 * @param lcl local ip6 address
1134 * @param rmt remote ip6 address
1135 * @param lcl_port local port
1136 * @param rmt_port remote port
1137 * @param proto transport protocol (e.g., tcp, udp)
1138 *
1139 * @return pointer to transport connection, if one is found, 0 otherwise
1140 */
Florin Coras04e53442017-07-16 17:12:15 -07001141transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001142session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1143 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1144 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001145{
Florin Corascea194d2017-10-02 00:18:51 -07001146 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001147 stream_session_t *s;
1148 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001149 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001150 int rv;
1151
Florin Corascea194d2017-10-02 00:18:51 -07001152 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1153 if (PREDICT_FALSE (!st))
1154 return 0;
1155
Florin Coras04e53442017-07-16 17:12:15 -07001156 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001157 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001158 if (rv == 0)
1159 {
Florin Corascea194d2017-10-02 00:18:51 -07001160 s = session_get_from_handle (kv6.value);
Florin Coras561af9b2017-12-09 10:19:43 -08001161 return tp_vfts[proto].get_connection (s->connection_index,
1162 s->thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001163 }
1164
Florin Corasa2ff7b82017-11-08 17:55:03 -08001165 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001166 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001167 if (rv == 0)
Florin Coras561af9b2017-12-09 10:19:43 -08001168 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
Florin Coras04e53442017-07-16 17:12:15 -07001169
Florin Corasa2ff7b82017-11-08 17:55:03 -08001170 /* Check the session rules table */
1171 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1172 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001173 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001174 {
Florin Corasdff48db2017-11-19 18:06:58 -08001175 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1176 return 0;
1177 if ((s = session_lookup_action_to_session (action_index,
1178 FIB_PROTOCOL_IP6, proto)))
Florin Coras561af9b2017-12-09 10:19:43 -08001179 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasdff48db2017-11-19 18:06:58 -08001180 return 0;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001181 }
1182
1183 /* If nothing is found, check if any listener is available */
1184 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1185 if (s)
Florin Coras561af9b2017-12-09 10:19:43 -08001186 return tp_vfts[proto].get_listener (s->connection_index);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001187
1188 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001189}
1190
Florin Corascea194d2017-10-02 00:18:51 -07001191/**
1192 * Lookup session with ip6 and transport layer information
1193 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001194 * Important note: this may look into another thread's pool table and
1195 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1196 * if needed as soon as possible.
1197 *
1198 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1199 * this returns a session as opposed to a transport connection and it does not
1200 * try to lookup half-open sessions.
1201 *
1202 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001203 */
1204stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001205session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1206 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001207{
1208 session_table_t *st;
1209 session_kv6_t kv6;
1210 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001211 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001212 int rv;
1213
1214 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1215 if (PREDICT_FALSE (!st))
1216 return 0;
1217
1218 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1219 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1220 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001221 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001222
Florin Corasa2ff7b82017-11-08 17:55:03 -08001223 /* Check the session rules table */
1224 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1225 rmt, lcl_port, rmt_port);
Florin Corasdff48db2017-11-19 18:06:58 -08001226 if (session_lookup_action_index_is_valid (action_index))
Florin Corasa2ff7b82017-11-08 17:55:03 -08001227 {
Florin Corasdff48db2017-11-19 18:06:58 -08001228 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1229 return 0;
1230 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1231 proto);
Florin Corasa2ff7b82017-11-08 17:55:03 -08001232 }
1233
Florin Corascea194d2017-10-02 00:18:51 -07001234 /* If nothing is found, check if any listener is available */
1235 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1236 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001237 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001238}
1239
Florin Coras1c710452017-10-17 00:03:13 -07001240clib_error_t *
1241vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1242{
1243 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001244 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001245 session_table_t *st;
1246 u32 fib_index;
1247 u8 fib_proto;
1248 clib_error_t *error;
1249
1250 if (!app_ns)
1251 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1252 "invalid app ns");
1253 if (args->scope > 3)
1254 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1255 "invalid scope");
Florin Corasc97a7392017-11-05 23:07:07 -08001256 if (args->transport_proto != TRANSPORT_PROTO_TCP
1257 && args->transport_proto != TRANSPORT_PROTO_UDP)
1258 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1259 "invalid transport proto");
Florin Coras1c710452017-10-17 00:03:13 -07001260 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1261 {
1262 fib_proto = args->table_args.rmt.fp_proto;
1263 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1264 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001265 srt = &st->session_rules[args->transport_proto];
1266 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1267 {
1268 clib_error_report (error);
1269 return error;
1270 }
Florin Coras1c710452017-10-17 00:03:13 -07001271 }
1272 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1273 {
Florin Corasa2ff7b82017-11-08 17:55:03 -08001274 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1275 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1276 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001277 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001278 srt = &st->session_rules[args->transport_proto];
1279 error = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001280 }
1281 return error;
1282}
1283
Florin Coras6c36f532017-11-03 18:32:34 -07001284/**
1285 * Mark (global) tables as pertaining to app ns
1286 */
1287void
1288session_lookup_set_tables_appns (app_namespace_t * app_ns)
1289{
1290 session_table_t *st;
1291 u32 fib_index;
1292 u8 fp;
1293
1294 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1295 {
1296 fib_index = app_namespace_get_fib_index (app_ns, fp);
1297 st = session_table_get_for_fib_index (fp, fib_index);
1298 if (st)
1299 st->appns_index = app_namespace_index (app_ns);
1300 }
1301}
1302
Florin Corascea194d2017-10-02 00:18:51 -07001303u8 *
1304format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1305{
1306 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1307 u32 is_local = va_arg (*args, u32);
1308 u8 *app_name, *str = 0;
1309 stream_session_t *session;
1310 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1311
Florin Corascea194d2017-10-02 00:18:51 -07001312 if (!is_local)
1313 {
1314 session = session_get_from_handle (kvp->value);
1315 app_name = application_name_from_index (session->app_index);
Florin Coras561af9b2017-12-09 10:19:43 -08001316 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1317 key->proto, format_ip4_address, &key->src,
1318 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1319 &key->dst, clib_net_to_host_u16 (key->dst_port));
Florin Corascea194d2017-10-02 00:18:51 -07001320 s = format (s, "%-40v%-30v", str, app_name);
1321 }
1322 else
1323 {
1324 app_name = application_name_from_index (kvp->value);
Florin Coras561af9b2017-12-09 10:19:43 -08001325 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1326 format_ip4_address, &key->src,
1327 clib_net_to_host_u16 (key->src_port));
Florin Corascea194d2017-10-02 00:18:51 -07001328 s = format (s, "%-30v%-30v", str, app_name);
1329 }
1330 vec_free (app_name);
1331 return s;
1332}
1333
1334typedef struct _ip4_session_table_show_ctx_t
1335{
1336 vlib_main_t *vm;
1337 u8 is_local;
1338} ip4_session_table_show_ctx_t;
1339
1340static int
1341ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1342{
1343 ip4_session_table_show_ctx_t *ctx = arg;
1344 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1345 ctx->is_local);
1346 return 1;
1347}
1348
1349void
1350session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1351 u8 type, u8 is_local)
1352{
1353 ip4_session_table_show_ctx_t ctx = {
1354 .vm = vm,
1355 .is_local = is_local,
1356 };
1357 if (!is_local)
1358 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1359 else
1360 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1361 switch (type)
1362 {
1363 /* main table v4 */
1364 case 0:
1365 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1366 &ctx);
1367 break;
1368 default:
1369 clib_warning ("not supported");
1370 }
1371}
Florin Coras66b11312017-07-31 17:18:03 -07001372
Florin Coras1c710452017-10-17 00:03:13 -07001373static clib_error_t *
1374session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1375 vlib_cli_command_t * cmd)
1376{
Florin Corasc97a7392017-11-05 23:07:07 -08001377 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001378 u32 appns_index, scope = 0;
1379 ip46_address_t lcl_ip, rmt_ip;
1380 u8 is_ip4 = 1, conn_set = 0;
1381 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001382 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001383 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001384 clib_error_t *error;
Florin Coras1c710452017-10-17 00:03:13 -07001385
1386 memset (&lcl_ip, 0, sizeof (lcl_ip));
1387 memset (&rmt_ip, 0, sizeof (rmt_ip));
1388 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1389 {
1390 if (unformat (input, "del"))
1391 is_add = 0;
1392 else if (unformat (input, "add"))
1393 ;
1394 else if (unformat (input, "appns %_%v%_", &ns_id))
1395 ;
1396 else if (unformat (input, "scope global"))
1397 scope = SESSION_RULE_SCOPE_GLOBAL;
1398 else if (unformat (input, "scope local"))
1399 scope = SESSION_RULE_SCOPE_LOCAL;
1400 else if (unformat (input, "scope all"))
1401 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1402 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1403 ;
1404 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1405 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1406 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1407 &rmt_port))
1408 {
1409 is_ip4 = 1;
1410 conn_set = 1;
1411 }
1412 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1413 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1414 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1415 &rmt_port))
1416 {
1417 is_ip4 = 0;
1418 conn_set = 1;
1419 }
1420 else if (unformat (input, "action %d", &action))
1421 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001422 else if (unformat (input, "tag %_%v%_", &tag))
1423 ;
Florin Coras1c710452017-10-17 00:03:13 -07001424 else
1425 return clib_error_return (0, "unknown input `%U'",
1426 format_unformat_error, input);
1427 }
1428
Florin Corasc97a7392017-11-05 23:07:07 -08001429 if (proto == ~0)
1430 {
1431 vlib_cli_output (vm, "proto must be set");
1432 return 0;
1433 }
1434 if (is_add && !conn_set && action == ~0)
1435 {
1436 vlib_cli_output (vm, "connection and action must be set for add");
1437 return 0;
1438 }
1439 if (!is_add && !tag && !conn_set)
1440 {
1441 vlib_cli_output (vm, "connection or tag must be set for delete");
1442 return 0;
1443 }
1444 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1445 {
1446 vlib_cli_output (vm, "tag too long (max u64)");
1447 return 0;
1448 }
Florin Coras1c710452017-10-17 00:03:13 -07001449
1450 if (ns_id)
1451 {
1452 app_ns = app_namespace_get_from_id (ns_id);
1453 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001454 {
1455 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1456 return 0;
1457 }
Florin Coras1c710452017-10-17 00:03:13 -07001458 }
1459 else
1460 {
1461 app_ns = app_namespace_get_default ();
1462 }
1463 appns_index = app_namespace_index (app_ns);
1464
1465 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1466 session_rule_add_del_args_t args = {
1467 .table_args.lcl.fp_addr = lcl_ip,
1468 .table_args.lcl.fp_len = lcl_plen,
1469 .table_args.lcl.fp_proto = fib_proto,
1470 .table_args.rmt.fp_addr = rmt_ip,
1471 .table_args.rmt.fp_len = rmt_plen,
1472 .table_args.rmt.fp_proto = fib_proto,
1473 .table_args.lcl_port = lcl_port,
1474 .table_args.rmt_port = rmt_port,
1475 .table_args.action_index = action,
1476 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001477 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001478 .appns_index = appns_index,
1479 .scope = scope,
1480 };
Florin Corasc97a7392017-11-05 23:07:07 -08001481 error = vnet_session_rule_add_del (&args);
1482 vec_free (tag);
1483 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001484}
1485
1486/* *INDENT-OFF* */
1487VLIB_CLI_COMMAND (session_rule_command, static) =
1488{
1489 .path = "session rule",
1490 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1491 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1492 .function = session_rule_command_fn,
1493};
1494/* *INDENT-ON* */
1495
Florin Coras7999e832017-10-31 01:51:04 -07001496void
1497session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1498 u8 transport_proto)
1499{
1500 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001501 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001502 session_table_t *st;
1503 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001504 srt = &st->session_rules[transport_proto];
1505 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001506}
1507
1508void
1509session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1510 u8 transport_proto)
1511{
1512 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001513 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001514 session_table_t *st;
1515 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001516 srt = &st->session_rules[transport_proto];
1517 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001518}
1519
Florin Coras1c710452017-10-17 00:03:13 -07001520static clib_error_t *
1521show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1522 vlib_cli_command_t * cmd)
1523{
1524 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1525 u32 fib_index, scope = 0;
1526 ip46_address_t lcl_ip, rmt_ip;
1527 u8 is_ip4 = 1, show_one = 0;
1528 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001529 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001530 session_table_t *st;
1531 u8 *ns_id = 0, fib_proto;
1532
1533 memset (&lcl_ip, 0, sizeof (lcl_ip));
1534 memset (&rmt_ip, 0, sizeof (rmt_ip));
1535 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1536 {
1537 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1538 ;
1539 else if (unformat (input, "appns %_%v%_", &ns_id))
1540 ;
1541 else if (unformat (input, "scope global"))
1542 scope = 1;
1543 else if (unformat (input, "scope local"))
1544 scope = 2;
1545 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1546 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1547 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1548 &rmt_port))
1549 {
1550 is_ip4 = 1;
1551 show_one = 1;
1552 }
1553 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1554 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1555 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1556 &rmt_port))
1557 {
1558 is_ip4 = 0;
1559 show_one = 1;
1560 }
1561 else
1562 return clib_error_return (0, "unknown input `%U'",
1563 format_unformat_error, input);
1564 }
1565
1566 if (transport_proto == ~0)
1567 {
1568 vlib_cli_output (vm, "transport proto must be set");
1569 return 0;
1570 }
1571
1572 if (ns_id)
1573 {
1574 app_ns = app_namespace_get_from_id (ns_id);
1575 if (!app_ns)
1576 {
1577 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1578 return 0;
1579 }
1580 }
1581 else
1582 {
1583 app_ns = app_namespace_get_default ();
1584 }
1585
1586 if (scope == 1 || scope == 0)
1587 {
1588 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1589 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1590 st = session_table_get_for_fib_index (fib_proto, fib_index);
1591 }
1592 else
1593 {
1594 st = app_namespace_get_local_table (app_ns);
1595 }
1596
1597 if (show_one)
1598 {
Florin Corasc97a7392017-11-05 23:07:07 -08001599 srt = &st->session_rules[transport_proto];
1600 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1601 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001602 return 0;
1603 }
1604
Florin Corasc97a7392017-11-05 23:07:07 -08001605 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1606 transport_proto);
1607 srt = &st->session_rules[transport_proto];
1608 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1609 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001610
1611 vec_free (ns_id);
1612 return 0;
1613}
1614
1615/* *INDENT-OFF* */
1616VLIB_CLI_COMMAND (show_session_rules_command, static) =
1617{
1618 .path = "show session rules",
Florin Corasdff48db2017-11-19 18:06:58 -08001619 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1620 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
Florin Coras1c710452017-10-17 00:03:13 -07001621 .function = show_session_rules_command_fn,
1622};
1623/* *INDENT-ON* */
1624
Florin Coras04e53442017-07-16 17:12:15 -07001625void
1626session_lookup_init (void)
1627{
Florin Corascea194d2017-10-02 00:18:51 -07001628 /*
1629 * Allocate default table and map it to fib_index 0
1630 */
1631 session_table_t *st = session_table_alloc ();
1632 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1633 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001634 st->active_fib_proto = FIB_PROTOCOL_IP4;
1635 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001636 st = session_table_alloc ();
1637 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1638 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001639 st->active_fib_proto = FIB_PROTOCOL_IP6;
1640 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001641}
1642
1643/*
1644 * fd.io coding-style-patch-verification: ON
1645 *
1646 * Local Variables:
1647 * eval: (c-set-style "gnu")
1648 * End:
1649 */