blob: f4b26f6d931c345a3e1cf5fbc388cf07ce2d093f [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
116make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
117{
rootc9d1c5b2017-08-15 12:58:31 -0400118 make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port, t->rmt_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700119 session_type_from_proto_and_ip (t->proto, 1));
Florin Coras04e53442017-07-16 17:12:15 -0700120}
121
122always_inline void
123make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
124 u16 lcl_port, u16 rmt_port, u8 proto)
125{
126 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
127
128 key->src.as_u64[0] = lcl->as_u64[0];
129 key->src.as_u64[1] = lcl->as_u64[1];
130 key->dst.as_u64[0] = rmt->as_u64[0];
131 key->dst.as_u64[1] = rmt->as_u64[1];
132 key->src_port = lcl_port;
133 key->dst_port = rmt_port;
134 key->proto = proto;
135 key->unused = 0;
136
137 kv->value = ~0ULL;
138}
139
140always_inline void
141make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
142 u8 proto)
143{
144 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
145
146 key->src.as_u64[0] = lcl->as_u64[0];
147 key->src.as_u64[1] = lcl->as_u64[1];
148 key->dst.as_u64[0] = 0;
149 key->dst.as_u64[1] = 0;
150 key->src_port = lcl_port;
151 key->dst_port = 0;
152 key->proto = proto;
153 key->unused = 0;
154
155 kv->value = ~0ULL;
156}
157
158always_inline void
159make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
160{
rootc9d1c5b2017-08-15 12:58:31 -0400161 make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port, t->rmt_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700162 session_type_from_proto_and_ip (t->proto, 0));
Florin Coras04e53442017-07-16 17:12:15 -0700163}
164
Florin Corascea194d2017-10-02 00:18:51 -0700165static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700166session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
Florin Coras04e53442017-07-16 17:12:15 -0700167{
Florin Corascea194d2017-10-02 00:18:51 -0700168 session_table_t *st;
Florin Coras6c36f532017-11-03 18:32:34 -0700169 u32 table_index;
170 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
Florin Corascea194d2017-10-02 00:18:51 -0700171 {
172 st = session_table_alloc ();
173 table_index = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -0700174 vec_validate (fib_index_to_table_index[fib_proto], fib_index);
175 fib_index_to_table_index[fib_proto][fib_index] = table_index;
176 st->active_fib_proto = fib_proto;
Florin Corascea194d2017-10-02 00:18:51 -0700177 return st;
178 }
179 else
180 {
Florin Coras6c36f532017-11-03 18:32:34 -0700181 table_index = fib_index_to_table_index[fib_proto][fib_index];
Florin Corascea194d2017-10-02 00:18:51 -0700182 return session_table_get (table_index);
183 }
184}
185
186static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700187session_table_get_or_alloc_for_connection (transport_connection_t * tc)
188{
189 u32 fib_proto;
190 fib_proto = transport_connection_fib_proto (tc);
191 return session_table_get_or_alloc (fib_proto, tc->fib_index);
192}
193
194static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700195session_table_get_for_connection (transport_connection_t * tc)
196{
197 u32 fib_proto = transport_connection_fib_proto (tc);
198 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
199 return 0;
200 return
201 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
202}
203
204static session_table_t *
205session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
206{
207 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
208 return 0;
209 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
210}
211
212u32
213session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
214{
215 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
216 return SESSION_TABLE_INVALID_INDEX;
217 return fib_index_to_table_index[fib_proto][fib_index];
218}
219
220/**
221 * Add transport connection to a session table
222 *
223 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
224 * is added to requested session table.
225 *
226 * @param tc transport connection to be added
227 * @param value value to be stored
228 *
229 * @return non-zero if failure
230 */
231int
232session_lookup_add_connection (transport_connection_t * tc, u64 value)
233{
234 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700235 session_kv4_t kv4;
236 session_kv6_t kv6;
237
Florin Corascea194d2017-10-02 00:18:51 -0700238 st = session_table_get_or_alloc_for_connection (tc);
239 if (!st)
240 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700241 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700242 {
Florin Coras04e53442017-07-16 17:12:15 -0700243 make_v4_ss_kv_from_tc (&kv4, tc);
244 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700245 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
246 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700247 }
248 else
249 {
Florin Coras04e53442017-07-16 17:12:15 -0700250 make_v6_ss_kv_from_tc (&kv6, tc);
251 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700252 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
253 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700254 }
255}
256
Florin Coras04e53442017-07-16 17:12:15 -0700257int
Florin Corascea194d2017-10-02 00:18:51 -0700258session_lookup_add_session_endpoint (u32 table_index,
259 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700260{
Florin Corascea194d2017-10-02 00:18:51 -0700261 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700262 session_kv4_t kv4;
263 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700264
Florin Corascea194d2017-10-02 00:18:51 -0700265 st = session_table_get (table_index);
266 if (!st)
267 return -1;
268 if (sep->is_ip4)
269 {
270 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
271 sep->transport_proto);
272 kv4.value = value;
273 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
274 }
275 else
276 {
277 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
278 sep->transport_proto);
279 kv6.value = value;
280 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
281 }
282}
283
284int
285session_lookup_del_session_endpoint (u32 table_index,
286 session_endpoint_t * sep)
287{
288 session_table_t *st;
289 session_kv4_t kv4;
290 session_kv6_t kv6;
291
292 st = session_table_get (table_index);
293 if (!st)
294 return -1;
295 if (sep->is_ip4)
296 {
297 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
298 sep->transport_proto);
299 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
300 }
301 else
302 {
303 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
304 sep->transport_proto);
305 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
306 }
307}
308
309/**
310 * Delete transport connection from session table
311 *
312 * @param table_index session table index
313 * @param tc transport connection to be removed
314 *
315 * @return non-zero if failure
316 */
317int
318session_lookup_del_connection (transport_connection_t * tc)
319{
320 session_table_t *st;
321 session_kv4_t kv4;
322 session_kv6_t kv6;
323
324 st = session_table_get_for_connection (tc);
325 if (!st)
326 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700327 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700328 {
Florin Coras04e53442017-07-16 17:12:15 -0700329 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700330 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700331 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700332 }
333 else
334 {
Florin Coras04e53442017-07-16 17:12:15 -0700335 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700336 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700337 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700338 }
Florin Coras04e53442017-07-16 17:12:15 -0700339}
340
341int
Florin Corascea194d2017-10-02 00:18:51 -0700342session_lookup_del_session (stream_session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700343{
344 transport_connection_t *ts;
345 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
346 s->thread_index);
Florin Corascea194d2017-10-02 00:18:51 -0700347 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700348}
349
Florin Corasf0c1c962017-11-02 21:31:46 -0700350static u32
Florin Corasa2ff7b82017-11-08 17:55:03 -0800351session_lookup_action_to_session_index (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700352{
353 if (action_index != SESSION_RULES_TABLE_ACTION_DROP)
354 return action_index;
355 return SESSION_INVALID_INDEX;
356}
357
Florin Coras1c710452017-10-17 00:03:13 -0700358static stream_session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700359session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
360 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700361{
362 application_t *app;
363 app = application_get (app_index);
364 if (!app)
365 return 0;
366
Florin Coras7999e832017-10-31 01:51:04 -0700367 return application_first_listener (app, fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700368}
369
Florin Corasa2ff7b82017-11-08 17:55:03 -0800370static stream_session_t *
371session_lookup_action_to_session (u32 action_index, u8 fib_proto,
372 u8 transport_proto)
373{
374 u32 session_index;
375 session_index = session_lookup_action_to_session_index (action_index);
376 /* Nothing sophisticated for now, action index is app index */
377 return session_lookup_app_listen_session (session_index, fib_proto,
378 transport_proto);
379}
380
Florin Coras1c710452017-10-17 00:03:13 -0700381stream_session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800382session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
383 ip4_address_t * lcl, u16 lcl_port,
384 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700385{
Florin Corasc97a7392017-11-05 23:07:07 -0800386 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasf0c1c962017-11-02 21:31:46 -0700387 u32 action_index, session_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800388 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700389 rmt_port);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800390 session_index = session_lookup_action_to_session_index (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700391 /* Nothing sophisticated for now, action index is app index */
Florin Corasf0c1c962017-11-02 21:31:46 -0700392 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700393 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700394}
395
396stream_session_t *
Florin Corasc97a7392017-11-05 23:07:07 -0800397session_lookup_rules_table6 (session_table_t * st, u8 proto,
Florin Coras1c710452017-10-17 00:03:13 -0700398 ip6_address_t * lcl, u16 lcl_port,
399 ip6_address_t * rmt, u16 rmt_port)
400{
Florin Corasc97a7392017-11-05 23:07:07 -0800401 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasf0c1c962017-11-02 21:31:46 -0700402 u32 action_index, session_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800403 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700404 rmt_port);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800405 session_index = session_lookup_action_to_session_index (action_index);
Florin Corasf0c1c962017-11-02 21:31:46 -0700406 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700407 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700408}
409
Florin Corasa2ff7b82017-11-08 17:55:03 -0800410/**
411 * Lookup listener for session endpoint in table
412 *
413 * @param table_index table where the endpoint should be looked up
414 * @param sep session endpoint to be looked up
415 * @param use_rules flag that indicates if the session rules of the table
416 * should be used
417 * @return invalid handle if nothing is found, the handle of a valid listener
418 * or an action_index if a rule is hit
419 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700420u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800421session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
422 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700423{
Florin Corasc97a7392017-11-05 23:07:07 -0800424 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700425 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700426 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700427 int rv;
Florin Coras4e4531e2017-11-06 23:27:56 -0800428 u8 sst;
Florin Coras04e53442017-07-16 17:12:15 -0700429
Florin Coras4e4531e2017-11-06 23:27:56 -0800430 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
Florin Corascea194d2017-10-02 00:18:51 -0700431 st = session_table_get (table_index);
432 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700433 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700434 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700435 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800436 session_kv4_t kv4;
437 ip4_address_t lcl4;
438
Florin Coras4e4531e2017-11-06 23:27:56 -0800439 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, sst);
Florin Corascea194d2017-10-02 00:18:51 -0700440 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
441 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700442 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800443 if (use_rules)
444 {
445 memset (&lcl4, 0, sizeof (lcl4));
446 srt = &st->session_rules[sep->transport_proto];
447 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
448 sep->port);
449 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
450 return session_lookup_action_to_session_index (ai);
451 }
Florin Coras68810622017-07-24 17:40:28 -0700452 }
453 else
454 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800455 session_kv6_t kv6;
456 ip6_address_t lcl6;
457
Florin Coras4e4531e2017-11-06 23:27:56 -0800458 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, sst);
Florin Corascea194d2017-10-02 00:18:51 -0700459 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
460 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700461 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700462
Florin Corasa2ff7b82017-11-08 17:55:03 -0800463 if (use_rules)
464 {
465 memset (&lcl6, 0, sizeof (lcl6));
466 srt = &st->session_rules[sep->transport_proto];
467 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
468 sep->port);
469 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
470 return session_lookup_action_to_session_index (ai);
471 }
Florin Coras68810622017-07-24 17:40:28 -0700472 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700473 return SESSION_INVALID_HANDLE;
474}
475
Florin Corasa2ff7b82017-11-08 17:55:03 -0800476/**
477 * Look up endpoint in local session table
478 *
479 * The result, for now, is an application index and it may in the future
480 * be extended to a more complicated "action object". The only action we
481 * emulate now is "drop" and for that we return a special app index.
482 *
483 * Lookup logic is to check in order:
484 * - the rules in the table (connect acls)
485 * - session sub-table for a listener
486 * - session sub-table for a local listener (zeroed addr)
487 *
488 * @param table_index table where the lookup should be done
489 * @param sep session endpoint to be looked up
490 * @return index that can be interpreted as an app index or drop action.
491 */
Florin Corascea194d2017-10-02 00:18:51 -0700492u32
Florin Corasa2ff7b82017-11-08 17:55:03 -0800493session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700494{
Florin Corasc97a7392017-11-05 23:07:07 -0800495 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700496 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700497 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700498 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700499
Florin Corascea194d2017-10-02 00:18:51 -0700500 st = session_table_get (table_index);
501 if (!st)
502 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800503 ASSERT (st->is_local);
504
Florin Corascea194d2017-10-02 00:18:51 -0700505 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700506 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800507 session_kv4_t kv4;
508 ip4_address_t lcl4;
509
510 /*
511 * Check if endpoint has special rules associated
512 */
513 memset (&lcl4, 0, sizeof (lcl4));
514 srt = &st->session_rules[sep->transport_proto];
515 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
516 sep->port);
517 if (ai == SESSION_RULES_TABLE_ACTION_DROP)
518 return APP_DROP_INDEX;
519 if (ai != SESSION_RULES_TABLE_ACTION_NONE)
520 return session_lookup_action_to_session_index (ai);
521
522 /*
523 * Check if session endpoint is a listener
524 */
Florin Corascea194d2017-10-02 00:18:51 -0700525 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
526 sep->transport_proto);
527 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
528 if (rv == 0)
529 return (u32) kv4.value;
530
531 /*
532 * Zero out the ip. Logic is that connect to local ips, say
533 * 127.0.0.1:port, can match 0.0.0.0:port
534 */
535 kv4.key[0] = 0;
536 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
537 if (rv == 0)
538 return (u32) kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700539 }
540 else
541 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800542 session_kv6_t kv6;
543 ip6_address_t lcl6;
544
545 memset (&lcl6, 0, sizeof (lcl6));
546 srt = &st->session_rules[sep->transport_proto];
547 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
548 sep->port);
549 if (ai == SESSION_RULES_TABLE_ACTION_DROP)
550 return APP_DROP_INDEX;
551 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
552 return session_lookup_action_to_session_index (ai);
553
Florin Corascea194d2017-10-02 00:18:51 -0700554 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
555 sep->transport_proto);
556 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
557 if (rv == 0)
558 return (u32) kv6.value;
559
560 /*
561 * Zero out the ip. Same logic as above.
562 */
563 kv6.key[0] = kv6.key[1] = 0;
564 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
565 if (rv == 0)
566 return (u32) kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700567 }
Florin Corasa2ff7b82017-11-08 17:55:03 -0800568 return APP_INVALID_INDEX;
Florin Coras04e53442017-07-16 17:12:15 -0700569}
570
Florin Corascea194d2017-10-02 00:18:51 -0700571static stream_session_t *
572session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
573 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700574{
Florin Coras04e53442017-07-16 17:12:15 -0700575 session_kv4_t kv4;
576 int rv;
577
578 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700579 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700580 if (rv == 0)
581 return session_manager_get_listener (proto, (u32) kv4.value);
582
583 /* Zero out the lcl ip */
584 kv4.key[0] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700585 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700586 if (rv == 0)
587 return session_manager_get_listener (proto, (u32) kv4.value);
588
589 return 0;
590}
591
Florin Coras04e53442017-07-16 17:12:15 -0700592stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700593session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
594 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700595{
Florin Corascea194d2017-10-02 00:18:51 -0700596 session_table_t *st;
597 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
598 if (!st)
599 return 0;
600 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700601}
602
Florin Corascea194d2017-10-02 00:18:51 -0700603static stream_session_t *
604session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
605 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700606{
Florin Coras04e53442017-07-16 17:12:15 -0700607 session_kv6_t kv6;
608 int rv;
609
610 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700611 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700612 if (rv == 0)
613 return session_manager_get_listener (proto, (u32) kv6.value);
614
615 /* Zero out the lcl ip */
616 kv6.key[0] = kv6.key[1] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700617 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700618 if (rv == 0)
619 return session_manager_get_listener (proto, (u32) kv6.value);
620
621 return 0;
622}
623
Florin Coras04e53442017-07-16 17:12:15 -0700624stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700625session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
626 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700627{
Florin Corascea194d2017-10-02 00:18:51 -0700628 session_table_t *st;
629 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
630 if (!st)
631 return 0;
632 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700633}
634
635stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700636session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700637{
Florin Corascea194d2017-10-02 00:18:51 -0700638 session_table_t *st;
639 st = session_table_get (table_index);
640 if (!st)
641 return 0;
642 if (sep->is_ip4)
643 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
644 sep->transport_proto);
645 else
646 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
647 sep->transport_proto);
Florin Coras04e53442017-07-16 17:12:15 -0700648 return 0;
649}
650
Florin Corascea194d2017-10-02 00:18:51 -0700651int
652session_lookup_add_half_open (transport_connection_t * tc, u64 value)
653{
654 session_table_t *st;
655 session_kv4_t kv4;
656 session_kv6_t kv6;
657
658 st = session_table_get_or_alloc_for_connection (tc);
659 if (!st)
660 return 0;
661 if (tc->is_ip4)
662 {
663 make_v4_ss_kv_from_tc (&kv4, tc);
664 kv4.value = value;
665 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
666 1 /* is_add */ );
667 }
668 else
669 {
670 make_v6_ss_kv_from_tc (&kv6, tc);
671 kv6.value = value;
672 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
673 1 /* is_add */ );
674 }
675}
676
677int
678session_lookup_del_half_open (transport_connection_t * tc)
679{
680 session_table_t *st;
681 session_kv4_t kv4;
682 session_kv6_t kv6;
683
684 st = session_table_get_for_connection (tc);
685 if (!st)
686 return -1;
687 if (tc->is_ip4)
688 {
689 make_v4_ss_kv_from_tc (&kv4, tc);
690 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
691 0 /* is_add */ );
692 }
693 else
694 {
695 make_v6_ss_kv_from_tc (&kv6, tc);
696 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
697 0 /* is_add */ );
698 }
699}
700
Florin Coras04e53442017-07-16 17:12:15 -0700701u64
Florin Corascea194d2017-10-02 00:18:51 -0700702session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700703{
Florin Corascea194d2017-10-02 00:18:51 -0700704 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700705 session_kv4_t kv4;
706 session_kv6_t kv6;
707 int rv;
708
Florin Corascea194d2017-10-02 00:18:51 -0700709 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
710 tc->fib_index);
711 if (!st)
712 return HALF_OPEN_LOOKUP_INVALID_VALUE;
713 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700714 {
Florin Corascea194d2017-10-02 00:18:51 -0700715 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700716 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700717 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700718 if (rv == 0)
719 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700720 }
721 else
722 {
723 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700724 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700725 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700726 if (rv == 0)
727 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700728 }
729 return HALF_OPEN_LOOKUP_INVALID_VALUE;
730}
731
732transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700733session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700734{
Florin Corascea194d2017-10-02 00:18:51 -0700735 u32 sst;
736
Florin Coras04e53442017-07-16 17:12:15 -0700737 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700738 {
739 sst = session_type_from_proto_and_ip (proto, is_ip4);
740 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
741 }
Florin Coras04e53442017-07-16 17:12:15 -0700742 return 0;
743}
744
Florin Corascea194d2017-10-02 00:18:51 -0700745/**
746 * Lookup connection with ip4 and transport layer information
747 *
748 * This is used on the fast path so it needs to be fast. Thereby,
749 * duplication of code and 'hacks' allowed.
750 *
751 * The lookup is incremental and returns whenever something is matched. The
752 * steps are:
753 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700754 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700755 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800756 * - Try to find a fully-formed or local source wildcarded (listener bound to
757 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700758 * - return 0
759 *
760 * @param fib_index index of fib wherein the connection was received
761 * @param lcl local ip4 address
762 * @param rmt remote ip4 address
763 * @param lcl_port local port
764 * @param rmt_port remote port
765 * @param proto transport protocol (e.g., tcp, udp)
766 * @param thread_index thread index for request
767 *
768 * @return pointer to transport connection, if one is found, 0 otherwise
769 */
Florin Coras04e53442017-07-16 17:12:15 -0700770transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700771session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
772 ip4_address_t * rmt, u16 lcl_port,
773 u16 rmt_port, u8 proto, u32 thread_index)
Florin Coras04e53442017-07-16 17:12:15 -0700774{
Florin Corascea194d2017-10-02 00:18:51 -0700775 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700776 session_kv4_t kv4;
777 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800778 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700779 int rv;
780
Florin Corascea194d2017-10-02 00:18:51 -0700781 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
782 if (PREDICT_FALSE (!st))
783 return 0;
784
Florin Corasa2ff7b82017-11-08 17:55:03 -0800785 /*
786 * Lookup session amongst established ones
787 */
Florin Coras04e53442017-07-16 17:12:15 -0700788 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700789 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700790 if (rv == 0)
791 {
Florin Corascea194d2017-10-02 00:18:51 -0700792 ASSERT ((u32) (kv4.value >> 32) == thread_index);
793 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700794 return tp_vfts[s->session_type].get_connection (s->connection_index,
Florin Corascea194d2017-10-02 00:18:51 -0700795 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700796 }
797
Florin Corasa2ff7b82017-11-08 17:55:03 -0800798 /*
799 * Try half-open connections
800 */
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)
Florin Corascea194d2017-10-02 00:18:51 -0700803 {
804 u32 sst = session_type_from_proto_and_ip (proto, 1);
805 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
806 }
Florin Coras1c710452017-10-17 00:03:13 -0700807
Florin Corasa2ff7b82017-11-08 17:55:03 -0800808 /*
809 * Check the session rules table
810 */
811 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
812 rmt, lcl_port, rmt_port);
813 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
814 return 0;
815 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
816 {
817 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
818 proto);
819 if (s)
820 return tp_vfts[s->session_type].get_listener (s->connection_index);
821 }
822
823 /*
824 * If nothing is found, check if any listener is available
825 */
826 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
827 if (s)
828 return tp_vfts[s->session_type].get_listener (s->connection_index);
829
830 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700831}
832
Florin Corascea194d2017-10-02 00:18:51 -0700833/**
834 * Lookup connection with ip4 and transport layer information
835 *
836 * Not optimized. This is used on the fast path so it needs to be fast.
837 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
838 * to that of @ref session_lookup_connection_wt4
839 *
840 * @param fib_index index of the fib wherein the connection was received
841 * @param lcl local ip4 address
842 * @param rmt remote ip4 address
843 * @param lcl_port local port
844 * @param rmt_port remote port
845 * @param proto transport protocol (e.g., tcp, udp)
846 *
847 * @return pointer to transport connection, if one is found, 0 otherwise
848 */
Florin Coras04e53442017-07-16 17:12:15 -0700849transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700850session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
851 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
852 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700853{
Florin Corascea194d2017-10-02 00:18:51 -0700854 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700855 session_kv4_t kv4;
856 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800857 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700858 int rv;
859
Florin Corascea194d2017-10-02 00:18:51 -0700860 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
861 if (PREDICT_FALSE (!st))
862 return 0;
863
Florin Corasa2ff7b82017-11-08 17:55:03 -0800864 /*
865 * Lookup session amongst established ones
866 */
Florin Coras04e53442017-07-16 17:12:15 -0700867 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700868 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700869 if (rv == 0)
870 {
Florin Corascea194d2017-10-02 00:18:51 -0700871 s = session_get_from_handle (kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700872 return tp_vfts[s->session_type].get_connection (s->connection_index,
873 s->thread_index);
874 }
875
Florin Corasa2ff7b82017-11-08 17:55:03 -0800876 /*
877 * Try half-open connections
878 */
Florin Corascea194d2017-10-02 00:18:51 -0700879 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700880 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700881 {
882 u32 sst = session_type_from_proto_and_ip (proto, 1);
883 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
884 }
Florin Corasa2ff7b82017-11-08 17:55:03 -0800885
886 /*
887 * Check the session rules table
888 */
889 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
890 rmt, lcl_port, rmt_port);
891 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
892 return 0;
893 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
894 {
895 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
896 proto);
897 if (s)
898 return tp_vfts[s->session_type].get_listener (s->connection_index);
899 }
900
901 /*
902 * If nothing is found, check if any listener is available
903 */
904 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
905 if (s)
906 return tp_vfts[s->session_type].get_listener (s->connection_index);
907
908 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700909}
910
Florin Corascea194d2017-10-02 00:18:51 -0700911/**
912 * Lookup session with ip4 and transport layer information
913 *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700914 * Important note: this may look into another thread's pool table and
915 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
916 * if needed as soon as possible.
917 *
918 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
919 * this returns a session as opposed to a transport connection and it does not
920 * try to lookup half-open sessions.
921 *
922 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -0700923 */
924stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700925session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
926 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700927{
Florin Corascea194d2017-10-02 00:18:51 -0700928 session_table_t *st;
929 session_kv4_t kv4;
930 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800931 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -0700932 int rv;
933
934 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
935 if (PREDICT_FALSE (!st))
936 return 0;
937
Florin Corasa2ff7b82017-11-08 17:55:03 -0800938 /*
939 * Lookup session amongst established ones
940 */
Florin Corascea194d2017-10-02 00:18:51 -0700941 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
942 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
943 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700944 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -0700945
Florin Corasa2ff7b82017-11-08 17:55:03 -0800946 /*
947 * Check the session rules table
948 */
949 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
950 rmt, lcl_port, rmt_port);
951 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
952 return 0;
953 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
954 {
955 if ((s = session_lookup_action_to_session (action_index,
956 FIB_PROTOCOL_IP4, proto)))
957 return s;
958 }
959
960 /*
961 * If nothing is found, check if any listener is available
962 */
Florin Corascea194d2017-10-02 00:18:51 -0700963 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
964 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800965
966 return 0;
Florin Corascea194d2017-10-02 00:18:51 -0700967}
968
969/**
970 * Lookup connection with ip6 and transport layer information
971 *
972 * This is used on the fast path so it needs to be fast. Thereby,
973 * duplication of code and 'hacks' allowed.
974 *
975 * The lookup is incremental and returns whenever something is matched. The
976 * steps are:
977 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700978 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700979 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800980 * - Try to find a fully-formed or local source wildcarded (listener bound to
981 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700982 * - return 0
983 *
984 * @param fib_index index of the fib wherein the connection was received
985 * @param lcl local ip6 address
986 * @param rmt remote ip6 address
987 * @param lcl_port local port
988 * @param rmt_port remote port
989 * @param proto transport protocol (e.g., tcp, udp)
990 * @param thread_index thread index for request
991 *
992 * @return pointer to transport connection, if one is found, 0 otherwise
993 */
994transport_connection_t *
995session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
996 ip6_address_t * rmt, u16 lcl_port,
997 u16 rmt_port, u8 proto, u32 thread_index)
998{
999 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001000 stream_session_t *s;
1001 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001002 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001003 int rv;
1004
Florin Corascea194d2017-10-02 00:18:51 -07001005 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1006 if (PREDICT_FALSE (!st))
1007 return 0;
1008
Florin Coras04e53442017-07-16 17:12:15 -07001009 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001010 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001011 if (rv == 0)
1012 {
Florin Corascea194d2017-10-02 00:18:51 -07001013 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1014 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001015 return tp_vfts[s->session_type].get_connection (s->connection_index,
Florin Corascea194d2017-10-02 00:18:51 -07001016 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001017 }
1018
Florin Corasa2ff7b82017-11-08 17:55:03 -08001019 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001020 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001021 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001022 {
1023 u32 sst = session_type_from_proto_and_ip (proto, 1);
1024 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1025 }
Florin Coras04e53442017-07-16 17:12:15 -07001026
Florin Corasa2ff7b82017-11-08 17:55:03 -08001027 /* Check the session rules table */
1028 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1029 rmt, lcl_port, rmt_port);
1030 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1031 return 0;
1032 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1033 {
1034 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1035 proto);
1036 if (s)
1037 return tp_vfts[s->session_type].get_listener (s->connection_index);
1038 }
1039
1040 /* If nothing is found, check if any listener is available */
1041 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1042 if (s)
1043 return tp_vfts[s->session_type].get_listener (s->connection_index);
1044
1045 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001046}
1047
Florin Corascea194d2017-10-02 00:18:51 -07001048/**
1049 * Lookup connection with ip6 and transport layer information
1050 *
1051 * Not optimized. This is used on the fast path so it needs to be fast.
1052 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1053 * to that of @ref session_lookup_connection_wt4
1054 *
1055 * @param fib_index index of the fib wherein the connection was received
1056 * @param lcl local ip6 address
1057 * @param rmt remote ip6 address
1058 * @param lcl_port local port
1059 * @param rmt_port remote port
1060 * @param proto transport protocol (e.g., tcp, udp)
1061 *
1062 * @return pointer to transport connection, if one is found, 0 otherwise
1063 */
Florin Coras04e53442017-07-16 17:12:15 -07001064transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001065session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1066 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1067 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001068{
Florin Corascea194d2017-10-02 00:18:51 -07001069 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001070 stream_session_t *s;
1071 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001072 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001073 int rv;
1074
Florin Corascea194d2017-10-02 00:18:51 -07001075 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1076 if (PREDICT_FALSE (!st))
1077 return 0;
1078
Florin Coras04e53442017-07-16 17:12:15 -07001079 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001080 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001081 if (rv == 0)
1082 {
Florin Corascea194d2017-10-02 00:18:51 -07001083 s = session_get_from_handle (kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -07001084 return tp_vfts[s->session_type].get_connection (s->connection_index,
1085 s->thread_index);
1086 }
1087
Florin Corasa2ff7b82017-11-08 17:55:03 -08001088 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001089 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001090 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001091 {
1092 u32 sst = session_type_from_proto_and_ip (proto, 1);
1093 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1094 }
Florin Coras04e53442017-07-16 17:12:15 -07001095
Florin Corasa2ff7b82017-11-08 17:55:03 -08001096 /* Check the session rules table */
1097 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1098 rmt, lcl_port, rmt_port);
1099 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1100 return 0;
1101 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1102 {
1103 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1104 proto);
1105 if (s)
1106 return tp_vfts[s->session_type].get_listener (s->connection_index);
1107 }
1108
1109 /* If nothing is found, check if any listener is available */
1110 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1111 if (s)
1112 return tp_vfts[s->session_type].get_listener (s->connection_index);
1113
1114 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001115}
1116
Florin Corascea194d2017-10-02 00:18:51 -07001117/**
1118 * Lookup session with ip6 and transport layer information
1119 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001120 * Important note: this may look into another thread's pool table and
1121 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1122 * if needed as soon as possible.
1123 *
1124 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1125 * this returns a session as opposed to a transport connection and it does not
1126 * try to lookup half-open sessions.
1127 *
1128 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001129 */
1130stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001131session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1132 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001133{
1134 session_table_t *st;
1135 session_kv6_t kv6;
1136 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001137 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001138 int rv;
1139
1140 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1141 if (PREDICT_FALSE (!st))
1142 return 0;
1143
1144 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1145 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1146 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001147 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001148
Florin Corasa2ff7b82017-11-08 17:55:03 -08001149 /* Check the session rules table */
1150 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1151 rmt, lcl_port, rmt_port);
1152 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1153 return 0;
1154 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1155 {
1156 if ((s =
1157 session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1158 proto)))
1159 return s;
1160 }
1161
Florin Corascea194d2017-10-02 00:18:51 -07001162 /* If nothing is found, check if any listener is available */
1163 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1164 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001165 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001166}
1167
1168u64
1169session_lookup_local_listener_make_handle (session_endpoint_t * sep)
1170{
1171 return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
1172 | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
1173 | (u32) sep->is_ip4);
1174}
1175
1176u8
1177session_lookup_local_is_handle (u64 handle)
1178{
1179 if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
1180 return 1;
1181 return 0;
1182}
1183
1184int
1185session_lookup_local_listener_parse_handle (u64 handle,
1186 session_endpoint_t * sep)
1187{
1188 u32 local_table_handle;
1189 if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
1190 return -1;
1191 local_table_handle = handle & 0xFFFFFFFFULL;
1192 sep->is_ip4 = local_table_handle & 0xff;
1193 local_table_handle >>= 8;
1194 sep->transport_proto = local_table_handle & 0xff;
1195 sep->port = local_table_handle >> 8;
1196 return 0;
1197}
1198
Florin Coras1c710452017-10-17 00:03:13 -07001199clib_error_t *
1200vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1201{
1202 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001203 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001204 session_table_t *st;
1205 u32 fib_index;
1206 u8 fib_proto;
1207 clib_error_t *error;
1208
1209 if (!app_ns)
1210 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1211 "invalid app ns");
1212 if (args->scope > 3)
1213 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1214 "invalid scope");
Florin Corasc97a7392017-11-05 23:07:07 -08001215 if (args->transport_proto != TRANSPORT_PROTO_TCP
1216 && args->transport_proto != TRANSPORT_PROTO_UDP)
1217 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1218 "invalid transport proto");
Florin Coras1c710452017-10-17 00:03:13 -07001219 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1220 {
1221 fib_proto = args->table_args.rmt.fp_proto;
1222 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1223 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001224 srt = &st->session_rules[args->transport_proto];
1225 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1226 {
1227 clib_error_report (error);
1228 return error;
1229 }
Florin Coras1c710452017-10-17 00:03:13 -07001230 }
1231 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1232 {
Florin Corasa2ff7b82017-11-08 17:55:03 -08001233 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1234 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1235 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001236 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001237 srt = &st->session_rules[args->transport_proto];
1238 error = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001239 }
1240 return error;
1241}
1242
Florin Coras6c36f532017-11-03 18:32:34 -07001243/**
1244 * Mark (global) tables as pertaining to app ns
1245 */
1246void
1247session_lookup_set_tables_appns (app_namespace_t * app_ns)
1248{
1249 session_table_t *st;
1250 u32 fib_index;
1251 u8 fp;
1252
1253 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1254 {
1255 fib_index = app_namespace_get_fib_index (app_ns, fp);
1256 st = session_table_get_for_fib_index (fp, fib_index);
1257 if (st)
1258 st->appns_index = app_namespace_index (app_ns);
1259 }
1260}
1261
Florin Corascea194d2017-10-02 00:18:51 -07001262u8 *
1263format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1264{
1265 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1266 u32 is_local = va_arg (*args, u32);
1267 u8 *app_name, *str = 0;
1268 stream_session_t *session;
1269 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1270
1271 char *proto = key->proto == TRANSPORT_PROTO_TCP ? "T" : "U";
1272 if (!is_local)
1273 {
1274 session = session_get_from_handle (kvp->value);
1275 app_name = application_name_from_index (session->app_index);
1276 str = format (0, "[%s] %U:%d->%U:%d", proto, format_ip4_address,
1277 &key->src, clib_net_to_host_u16 (key->src_port),
1278 format_ip4_address, &key->dst,
1279 clib_net_to_host_u16 (key->dst_port));
1280 s = format (s, "%-40v%-30v", str, app_name);
1281 }
1282 else
1283 {
1284 app_name = application_name_from_index (kvp->value);
1285 str = format (0, "[%s] %U:%d", proto, format_ip4_address,
1286 &key->src, clib_net_to_host_u16 (key->src_port));
1287 s = format (s, "%-30v%-30v", str, app_name);
1288 }
1289 vec_free (app_name);
1290 return s;
1291}
1292
1293typedef struct _ip4_session_table_show_ctx_t
1294{
1295 vlib_main_t *vm;
1296 u8 is_local;
1297} ip4_session_table_show_ctx_t;
1298
1299static int
1300ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1301{
1302 ip4_session_table_show_ctx_t *ctx = arg;
1303 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1304 ctx->is_local);
1305 return 1;
1306}
1307
1308void
1309session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1310 u8 type, u8 is_local)
1311{
1312 ip4_session_table_show_ctx_t ctx = {
1313 .vm = vm,
1314 .is_local = is_local,
1315 };
1316 if (!is_local)
1317 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1318 else
1319 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1320 switch (type)
1321 {
1322 /* main table v4 */
1323 case 0:
1324 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1325 &ctx);
1326 break;
1327 default:
1328 clib_warning ("not supported");
1329 }
1330}
Florin Coras66b11312017-07-31 17:18:03 -07001331
Florin Coras1c710452017-10-17 00:03:13 -07001332static clib_error_t *
1333session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1334 vlib_cli_command_t * cmd)
1335{
Florin Corasc97a7392017-11-05 23:07:07 -08001336 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001337 u32 appns_index, scope = 0;
1338 ip46_address_t lcl_ip, rmt_ip;
1339 u8 is_ip4 = 1, conn_set = 0;
1340 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001341 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001342 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001343 clib_error_t *error;
Florin Coras1c710452017-10-17 00:03:13 -07001344
1345 memset (&lcl_ip, 0, sizeof (lcl_ip));
1346 memset (&rmt_ip, 0, sizeof (rmt_ip));
1347 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1348 {
1349 if (unformat (input, "del"))
1350 is_add = 0;
1351 else if (unformat (input, "add"))
1352 ;
1353 else if (unformat (input, "appns %_%v%_", &ns_id))
1354 ;
1355 else if (unformat (input, "scope global"))
1356 scope = SESSION_RULE_SCOPE_GLOBAL;
1357 else if (unformat (input, "scope local"))
1358 scope = SESSION_RULE_SCOPE_LOCAL;
1359 else if (unformat (input, "scope all"))
1360 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1361 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1362 ;
1363 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1364 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1365 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1366 &rmt_port))
1367 {
1368 is_ip4 = 1;
1369 conn_set = 1;
1370 }
1371 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1372 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1373 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1374 &rmt_port))
1375 {
1376 is_ip4 = 0;
1377 conn_set = 1;
1378 }
1379 else if (unformat (input, "action %d", &action))
1380 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001381 else if (unformat (input, "tag %_%v%_", &tag))
1382 ;
Florin Coras1c710452017-10-17 00:03:13 -07001383 else
1384 return clib_error_return (0, "unknown input `%U'",
1385 format_unformat_error, input);
1386 }
1387
Florin Corasc97a7392017-11-05 23:07:07 -08001388 if (proto == ~0)
1389 {
1390 vlib_cli_output (vm, "proto must be set");
1391 return 0;
1392 }
1393 if (is_add && !conn_set && action == ~0)
1394 {
1395 vlib_cli_output (vm, "connection and action must be set for add");
1396 return 0;
1397 }
1398 if (!is_add && !tag && !conn_set)
1399 {
1400 vlib_cli_output (vm, "connection or tag must be set for delete");
1401 return 0;
1402 }
1403 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1404 {
1405 vlib_cli_output (vm, "tag too long (max u64)");
1406 return 0;
1407 }
Florin Coras1c710452017-10-17 00:03:13 -07001408
1409 if (ns_id)
1410 {
1411 app_ns = app_namespace_get_from_id (ns_id);
1412 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001413 {
1414 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1415 return 0;
1416 }
Florin Coras1c710452017-10-17 00:03:13 -07001417 }
1418 else
1419 {
1420 app_ns = app_namespace_get_default ();
1421 }
1422 appns_index = app_namespace_index (app_ns);
1423
1424 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1425 session_rule_add_del_args_t args = {
1426 .table_args.lcl.fp_addr = lcl_ip,
1427 .table_args.lcl.fp_len = lcl_plen,
1428 .table_args.lcl.fp_proto = fib_proto,
1429 .table_args.rmt.fp_addr = rmt_ip,
1430 .table_args.rmt.fp_len = rmt_plen,
1431 .table_args.rmt.fp_proto = fib_proto,
1432 .table_args.lcl_port = lcl_port,
1433 .table_args.rmt_port = rmt_port,
1434 .table_args.action_index = action,
1435 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001436 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001437 .appns_index = appns_index,
1438 .scope = scope,
1439 };
Florin Corasc97a7392017-11-05 23:07:07 -08001440 error = vnet_session_rule_add_del (&args);
1441 vec_free (tag);
1442 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001443}
1444
1445/* *INDENT-OFF* */
1446VLIB_CLI_COMMAND (session_rule_command, static) =
1447{
1448 .path = "session rule",
1449 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1450 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1451 .function = session_rule_command_fn,
1452};
1453/* *INDENT-ON* */
1454
Florin Coras7999e832017-10-31 01:51:04 -07001455void
1456session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1457 u8 transport_proto)
1458{
1459 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001460 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001461 session_table_t *st;
1462 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001463 srt = &st->session_rules[transport_proto];
1464 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001465}
1466
1467void
1468session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1469 u8 transport_proto)
1470{
1471 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001472 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001473 session_table_t *st;
1474 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001475 srt = &st->session_rules[transport_proto];
1476 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001477}
1478
Florin Coras1c710452017-10-17 00:03:13 -07001479static clib_error_t *
1480show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1481 vlib_cli_command_t * cmd)
1482{
1483 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1484 u32 fib_index, scope = 0;
1485 ip46_address_t lcl_ip, rmt_ip;
1486 u8 is_ip4 = 1, show_one = 0;
1487 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001488 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001489 session_table_t *st;
1490 u8 *ns_id = 0, fib_proto;
1491
1492 memset (&lcl_ip, 0, sizeof (lcl_ip));
1493 memset (&rmt_ip, 0, sizeof (rmt_ip));
1494 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1495 {
1496 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1497 ;
1498 else if (unformat (input, "appns %_%v%_", &ns_id))
1499 ;
1500 else if (unformat (input, "scope global"))
1501 scope = 1;
1502 else if (unformat (input, "scope local"))
1503 scope = 2;
1504 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1505 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1506 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1507 &rmt_port))
1508 {
1509 is_ip4 = 1;
1510 show_one = 1;
1511 }
1512 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1513 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1514 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1515 &rmt_port))
1516 {
1517 is_ip4 = 0;
1518 show_one = 1;
1519 }
1520 else
1521 return clib_error_return (0, "unknown input `%U'",
1522 format_unformat_error, input);
1523 }
1524
1525 if (transport_proto == ~0)
1526 {
1527 vlib_cli_output (vm, "transport proto must be set");
1528 return 0;
1529 }
1530
1531 if (ns_id)
1532 {
1533 app_ns = app_namespace_get_from_id (ns_id);
1534 if (!app_ns)
1535 {
1536 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1537 return 0;
1538 }
1539 }
1540 else
1541 {
1542 app_ns = app_namespace_get_default ();
1543 }
1544
1545 if (scope == 1 || scope == 0)
1546 {
1547 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1548 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1549 st = session_table_get_for_fib_index (fib_proto, fib_index);
1550 }
1551 else
1552 {
1553 st = app_namespace_get_local_table (app_ns);
1554 }
1555
1556 if (show_one)
1557 {
Florin Corasc97a7392017-11-05 23:07:07 -08001558 srt = &st->session_rules[transport_proto];
1559 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1560 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001561 return 0;
1562 }
1563
Florin Corasc97a7392017-11-05 23:07:07 -08001564 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1565 transport_proto);
1566 srt = &st->session_rules[transport_proto];
1567 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1568 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001569
1570 vec_free (ns_id);
1571 return 0;
1572}
1573
1574/* *INDENT-OFF* */
1575VLIB_CLI_COMMAND (show_session_rules_command, static) =
1576{
1577 .path = "show session rules",
1578 .short_help = "show session rules [appns <id> proto <proto> <lcl-ip/plen>"
1579 " <lcl-port> <rmt-ip/plen> <rmt-port>]",
1580 .function = show_session_rules_command_fn,
1581};
1582/* *INDENT-ON* */
1583
Florin Coras04e53442017-07-16 17:12:15 -07001584void
1585session_lookup_init (void)
1586{
Florin Corascea194d2017-10-02 00:18:51 -07001587 /*
1588 * Allocate default table and map it to fib_index 0
1589 */
1590 session_table_t *st = session_table_alloc ();
1591 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1592 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001593 st->active_fib_proto = FIB_PROTOCOL_IP4;
1594 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001595 st = session_table_alloc ();
1596 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1597 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001598 st->active_fib_proto = FIB_PROTOCOL_IP6;
1599 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001600}
1601
1602/*
1603 * fd.io coding-style-patch-verification: ON
1604 *
1605 * Local Variables:
1606 * eval: (c-set-style "gnu")
1607 * End:
1608 */