blob: 30e39132d3a9aea44de072e2190452b50855af87 [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 Coras04e53442017-07-16 17:12:15 -0700130make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
131{
rootc9d1c5b2017-08-15 12:58:31 -0400132 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 -0700133 session_type_from_proto_and_ip (t->proto, 1));
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 Coras04e53442017-07-16 17:12:15 -0700190make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
191{
rootc9d1c5b2017-08-15 12:58:31 -0400192 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 -0700193 session_type_from_proto_and_ip (t->proto, 0));
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 Corascea194d2017-10-02 00:18:51 -0700208 return st;
209 }
210 else
211 {
Florin Coras6c36f532017-11-03 18:32:34 -0700212 table_index = fib_index_to_table_index[fib_proto][fib_index];
Florin Corascea194d2017-10-02 00:18:51 -0700213 return session_table_get (table_index);
214 }
215}
216
217static session_table_t *
Florin Coras6c36f532017-11-03 18:32:34 -0700218session_table_get_or_alloc_for_connection (transport_connection_t * tc)
219{
220 u32 fib_proto;
221 fib_proto = transport_connection_fib_proto (tc);
222 return session_table_get_or_alloc (fib_proto, tc->fib_index);
223}
224
225static session_table_t *
Florin Corascea194d2017-10-02 00:18:51 -0700226session_table_get_for_connection (transport_connection_t * tc)
227{
228 u32 fib_proto = transport_connection_fib_proto (tc);
229 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
230 return 0;
231 return
232 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
233}
234
235static session_table_t *
236session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
237{
238 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
239 return 0;
240 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
241}
242
243u32
244session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
245{
246 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
247 return SESSION_TABLE_INVALID_INDEX;
248 return fib_index_to_table_index[fib_proto][fib_index];
249}
250
251/**
252 * Add transport connection to a session table
253 *
254 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
255 * is added to requested session table.
256 *
257 * @param tc transport connection to be added
258 * @param value value to be stored
259 *
260 * @return non-zero if failure
261 */
262int
263session_lookup_add_connection (transport_connection_t * tc, u64 value)
264{
265 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700266 session_kv4_t kv4;
267 session_kv6_t kv6;
268
Florin Corascea194d2017-10-02 00:18:51 -0700269 st = session_table_get_or_alloc_for_connection (tc);
270 if (!st)
271 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700272 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700273 {
Florin Coras04e53442017-07-16 17:12:15 -0700274 make_v4_ss_kv_from_tc (&kv4, tc);
275 kv4.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700276 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
277 1 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700278 }
279 else
280 {
Florin Coras04e53442017-07-16 17:12:15 -0700281 make_v6_ss_kv_from_tc (&kv6, tc);
282 kv6.value = value;
Florin Corascea194d2017-10-02 00:18:51 -0700283 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
284 1 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700285 }
286}
287
Florin Coras04e53442017-07-16 17:12:15 -0700288int
Florin Corascea194d2017-10-02 00:18:51 -0700289session_lookup_add_session_endpoint (u32 table_index,
290 session_endpoint_t * sep, u64 value)
Florin Coras04e53442017-07-16 17:12:15 -0700291{
Florin Corascea194d2017-10-02 00:18:51 -0700292 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700293 session_kv4_t kv4;
294 session_kv6_t kv6;
Florin Coras68810622017-07-24 17:40:28 -0700295
Florin Corascea194d2017-10-02 00:18:51 -0700296 st = session_table_get (table_index);
297 if (!st)
298 return -1;
299 if (sep->is_ip4)
300 {
301 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
302 sep->transport_proto);
303 kv4.value = value;
304 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
305 }
306 else
307 {
308 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
309 sep->transport_proto);
310 kv6.value = value;
311 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
312 }
313}
314
315int
316session_lookup_del_session_endpoint (u32 table_index,
317 session_endpoint_t * sep)
318{
319 session_table_t *st;
320 session_kv4_t kv4;
321 session_kv6_t kv6;
322
323 st = session_table_get (table_index);
324 if (!st)
325 return -1;
326 if (sep->is_ip4)
327 {
328 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
329 sep->transport_proto);
330 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
331 }
332 else
333 {
334 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
335 sep->transport_proto);
336 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
337 }
338}
339
340/**
341 * Delete transport connection from session table
342 *
343 * @param table_index session table index
344 * @param tc transport connection to be removed
345 *
346 * @return non-zero if failure
347 */
348int
349session_lookup_del_connection (transport_connection_t * tc)
350{
351 session_table_t *st;
352 session_kv4_t kv4;
353 session_kv6_t kv6;
354
355 st = session_table_get_for_connection (tc);
356 if (!st)
357 return -1;
Florin Coras68810622017-07-24 17:40:28 -0700358 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700359 {
Florin Coras04e53442017-07-16 17:12:15 -0700360 make_v4_ss_kv_from_tc (&kv4, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700361 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
Florin Coras04e53442017-07-16 17:12:15 -0700362 0 /* is_add */ );
Florin Coras68810622017-07-24 17:40:28 -0700363 }
364 else
365 {
Florin Coras04e53442017-07-16 17:12:15 -0700366 make_v6_ss_kv_from_tc (&kv6, tc);
Florin Corascea194d2017-10-02 00:18:51 -0700367 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
Florin Coras04e53442017-07-16 17:12:15 -0700368 0 /* is_add */ );
Florin Coras04e53442017-07-16 17:12:15 -0700369 }
Florin Coras04e53442017-07-16 17:12:15 -0700370}
371
372int
Florin Corascea194d2017-10-02 00:18:51 -0700373session_lookup_del_session (stream_session_t * s)
Florin Coras04e53442017-07-16 17:12:15 -0700374{
375 transport_connection_t *ts;
376 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
377 s->thread_index);
Florin Corascea194d2017-10-02 00:18:51 -0700378 return session_lookup_del_connection (ts);
Florin Coras04e53442017-07-16 17:12:15 -0700379}
380
Florin Corasf0c1c962017-11-02 21:31:46 -0700381static u32
Florin Corasa2ff7b82017-11-08 17:55:03 -0800382session_lookup_action_to_session_index (u32 action_index)
Florin Corasf0c1c962017-11-02 21:31:46 -0700383{
384 if (action_index != SESSION_RULES_TABLE_ACTION_DROP)
385 return action_index;
386 return SESSION_INVALID_INDEX;
387}
388
Florin Coras1c710452017-10-17 00:03:13 -0700389static stream_session_t *
Florin Coras7999e832017-10-31 01:51:04 -0700390session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
391 u8 transport_proto)
Florin Coras1c710452017-10-17 00:03:13 -0700392{
393 application_t *app;
394 app = application_get (app_index);
395 if (!app)
396 return 0;
397
Florin Coras7999e832017-10-31 01:51:04 -0700398 return application_first_listener (app, fib_proto, transport_proto);
Florin Coras1c710452017-10-17 00:03:13 -0700399}
400
Florin Corasa2ff7b82017-11-08 17:55:03 -0800401static stream_session_t *
402session_lookup_action_to_session (u32 action_index, u8 fib_proto,
403 u8 transport_proto)
404{
405 u32 session_index;
406 session_index = session_lookup_action_to_session_index (action_index);
407 /* Nothing sophisticated for now, action index is app index */
408 return session_lookup_app_listen_session (session_index, fib_proto,
409 transport_proto);
410}
411
Florin Coras1c710452017-10-17 00:03:13 -0700412stream_session_t *
Florin Corasa2ff7b82017-11-08 17:55:03 -0800413session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
414 ip4_address_t * lcl, u16 lcl_port,
415 ip4_address_t * rmt, u16 rmt_port)
Florin Coras1c710452017-10-17 00:03:13 -0700416{
Florin Corasc97a7392017-11-05 23:07:07 -0800417 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasf0c1c962017-11-02 21:31:46 -0700418 u32 action_index, session_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800419 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700420 rmt_port);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800421 session_index = session_lookup_action_to_session_index (action_index);
Florin Coras1c710452017-10-17 00:03:13 -0700422 /* Nothing sophisticated for now, action index is app index */
Florin Corasf0c1c962017-11-02 21:31:46 -0700423 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP4,
Florin Coras7999e832017-10-31 01:51:04 -0700424 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700425}
426
427stream_session_t *
Florin Corasc97a7392017-11-05 23:07:07 -0800428session_lookup_rules_table6 (session_table_t * st, u8 proto,
Florin Coras1c710452017-10-17 00:03:13 -0700429 ip6_address_t * lcl, u16 lcl_port,
430 ip6_address_t * rmt, u16 rmt_port)
431{
Florin Corasc97a7392017-11-05 23:07:07 -0800432 session_rules_table_t *srt = &st->session_rules[proto];
Florin Corasf0c1c962017-11-02 21:31:46 -0700433 u32 action_index, session_index;
Florin Corasc97a7392017-11-05 23:07:07 -0800434 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
Florin Coras1c710452017-10-17 00:03:13 -0700435 rmt_port);
Florin Corasa2ff7b82017-11-08 17:55:03 -0800436 session_index = session_lookup_action_to_session_index (action_index);
Florin Corasf0c1c962017-11-02 21:31:46 -0700437 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP6,
Florin Coras7999e832017-10-31 01:51:04 -0700438 proto);
Florin Coras1c710452017-10-17 00:03:13 -0700439}
440
Florin Corasa2ff7b82017-11-08 17:55:03 -0800441/**
442 * Lookup listener for session endpoint in table
443 *
444 * @param table_index table where the endpoint should be looked up
445 * @param sep session endpoint to be looked up
446 * @param use_rules flag that indicates if the session rules of the table
447 * should be used
448 * @return invalid handle if nothing is found, the handle of a valid listener
449 * or an action_index if a rule is hit
450 */
Florin Coras3cbc04b2017-10-02 00:18:51 -0700451u64
Florin Corasa2ff7b82017-11-08 17:55:03 -0800452session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
453 u8 use_rules)
Florin Coras04e53442017-07-16 17:12:15 -0700454{
Florin Corasc97a7392017-11-05 23:07:07 -0800455 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700456 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700457 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700458 int rv;
Florin Coras4e4531e2017-11-06 23:27:56 -0800459 u8 sst;
Florin Coras04e53442017-07-16 17:12:15 -0700460
Florin Coras4e4531e2017-11-06 23:27:56 -0800461 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
Florin Corascea194d2017-10-02 00:18:51 -0700462 st = session_table_get (table_index);
463 if (!st)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700464 return SESSION_INVALID_HANDLE;
Florin Corascea194d2017-10-02 00:18:51 -0700465 if (sep->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700466 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800467 session_kv4_t kv4;
468 ip4_address_t lcl4;
469
Florin Coras4e4531e2017-11-06 23:27:56 -0800470 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, sst);
Florin Corascea194d2017-10-02 00:18:51 -0700471 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
472 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700473 return kv4.value;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800474 if (use_rules)
475 {
476 memset (&lcl4, 0, sizeof (lcl4));
477 srt = &st->session_rules[sep->transport_proto];
478 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
479 sep->port);
480 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
481 return session_lookup_action_to_session_index (ai);
482 }
Florin Coras68810622017-07-24 17:40:28 -0700483 }
484 else
485 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800486 session_kv6_t kv6;
487 ip6_address_t lcl6;
488
Florin Coras4e4531e2017-11-06 23:27:56 -0800489 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, sst);
Florin Corascea194d2017-10-02 00:18:51 -0700490 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
491 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -0700492 return kv6.value;
Florin Coras1c710452017-10-17 00:03:13 -0700493
Florin Corasa2ff7b82017-11-08 17:55:03 -0800494 if (use_rules)
495 {
496 memset (&lcl6, 0, sizeof (lcl6));
497 srt = &st->session_rules[sep->transport_proto];
498 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
499 sep->port);
500 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
501 return session_lookup_action_to_session_index (ai);
502 }
Florin Coras68810622017-07-24 17:40:28 -0700503 }
Florin Coras3cbc04b2017-10-02 00:18:51 -0700504 return SESSION_INVALID_HANDLE;
505}
506
Florin Corasa2ff7b82017-11-08 17:55:03 -0800507/**
508 * Look up endpoint in local session table
509 *
510 * The result, for now, is an application index and it may in the future
511 * be extended to a more complicated "action object". The only action we
512 * emulate now is "drop" and for that we return a special app index.
513 *
514 * Lookup logic is to check in order:
515 * - the rules in the table (connect acls)
516 * - session sub-table for a listener
517 * - session sub-table for a local listener (zeroed addr)
518 *
519 * @param table_index table where the lookup should be done
520 * @param sep session endpoint to be looked up
521 * @return index that can be interpreted as an app index or drop action.
522 */
Florin Corascea194d2017-10-02 00:18:51 -0700523u32
Florin Corasa2ff7b82017-11-08 17:55:03 -0800524session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
Florin Coras68810622017-07-24 17:40:28 -0700525{
Florin Corasc97a7392017-11-05 23:07:07 -0800526 session_rules_table_t *srt;
Florin Corascea194d2017-10-02 00:18:51 -0700527 session_table_t *st;
Florin Corasf0c1c962017-11-02 21:31:46 -0700528 u32 ai;
Florin Corascea194d2017-10-02 00:18:51 -0700529 int rv;
Florin Coras68810622017-07-24 17:40:28 -0700530
Florin Corascea194d2017-10-02 00:18:51 -0700531 st = session_table_get (table_index);
532 if (!st)
533 return SESSION_INVALID_INDEX;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800534 ASSERT (st->is_local);
535
Florin Corascea194d2017-10-02 00:18:51 -0700536 if (sep->is_ip4)
Florin Coras68810622017-07-24 17:40:28 -0700537 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800538 session_kv4_t kv4;
539 ip4_address_t lcl4;
540
541 /*
542 * Check if endpoint has special rules associated
543 */
544 memset (&lcl4, 0, sizeof (lcl4));
545 srt = &st->session_rules[sep->transport_proto];
546 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
547 sep->port);
548 if (ai == SESSION_RULES_TABLE_ACTION_DROP)
549 return APP_DROP_INDEX;
550 if (ai != SESSION_RULES_TABLE_ACTION_NONE)
551 return session_lookup_action_to_session_index (ai);
552
553 /*
554 * Check if session endpoint is a listener
555 */
Florin Corascea194d2017-10-02 00:18:51 -0700556 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
557 sep->transport_proto);
558 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
559 if (rv == 0)
560 return (u32) kv4.value;
561
562 /*
563 * Zero out the ip. Logic is that connect to local ips, say
564 * 127.0.0.1:port, can match 0.0.0.0:port
565 */
566 kv4.key[0] = 0;
567 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
568 if (rv == 0)
569 return (u32) kv4.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800570
571 /*
572 * Zero out the port and check if we have proxy
573 */
574 kv4.key[1] = 0;
575 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
576 if (rv == 0)
577 return (u32) kv4.value;
Florin Coras68810622017-07-24 17:40:28 -0700578 }
579 else
580 {
Florin Corasa2ff7b82017-11-08 17:55:03 -0800581 session_kv6_t kv6;
582 ip6_address_t lcl6;
583
584 memset (&lcl6, 0, sizeof (lcl6));
585 srt = &st->session_rules[sep->transport_proto];
586 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
587 sep->port);
588 if (ai == SESSION_RULES_TABLE_ACTION_DROP)
589 return APP_DROP_INDEX;
590 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
591 return session_lookup_action_to_session_index (ai);
592
Florin Corascea194d2017-10-02 00:18:51 -0700593 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
594 sep->transport_proto);
595 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
596 if (rv == 0)
597 return (u32) kv6.value;
598
599 /*
600 * Zero out the ip. Same logic as above.
601 */
602 kv6.key[0] = kv6.key[1] = 0;
603 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
604 if (rv == 0)
605 return (u32) kv6.value;
Florin Corasdbd44562017-11-09 19:30:17 -0800606
607 /*
608 * Zero out the port. Same logic as above.
609 */
610 kv6.key[4] = kv6.key[5] = 0;
611 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
612 if (rv == 0)
613 return (u32) kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700614 }
Florin Corasa2ff7b82017-11-08 17:55:03 -0800615 return APP_INVALID_INDEX;
Florin Coras04e53442017-07-16 17:12:15 -0700616}
617
Florin Corascea194d2017-10-02 00:18:51 -0700618static stream_session_t *
619session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
620 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700621{
Florin Coras04e53442017-07-16 17:12:15 -0700622 session_kv4_t kv4;
623 int rv;
624
Florin Corasdbd44562017-11-09 19:30:17 -0800625 /*
626 * First, try a fully formed listener
627 */
Florin Coras04e53442017-07-16 17:12:15 -0700628 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700629 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700630 if (rv == 0)
631 return session_manager_get_listener (proto, (u32) kv4.value);
632
Florin Corasdbd44562017-11-09 19:30:17 -0800633 /*
634 * Zero out the lcl ip and check if any 0/0 port binds have been done
635 */
Florin Coras04e53442017-07-16 17:12:15 -0700636 kv4.key[0] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700637 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700638 if (rv == 0)
639 return session_manager_get_listener (proto, (u32) kv4.value);
640
Florin Corasdbd44562017-11-09 19:30:17 -0800641 /*
642 * Zero out port and check if we have a proxy set up for our ip
643 */
644 make_v4_proxy_kv (&kv4, lcl, proto);
645 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
646 if (rv == 0)
647 return session_manager_get_listener (proto, (u32) kv4.value);
648
Florin Coras04e53442017-07-16 17:12:15 -0700649 return 0;
650}
651
Florin Coras04e53442017-07-16 17:12:15 -0700652stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700653session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
654 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700655{
Florin Corascea194d2017-10-02 00:18:51 -0700656 session_table_t *st;
657 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
658 if (!st)
659 return 0;
660 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700661}
662
Florin Corascea194d2017-10-02 00:18:51 -0700663static stream_session_t *
664session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
665 u16 lcl_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700666{
Florin Coras04e53442017-07-16 17:12:15 -0700667 session_kv6_t kv6;
668 int rv;
669
670 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700671 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700672 if (rv == 0)
673 return session_manager_get_listener (proto, (u32) kv6.value);
674
675 /* Zero out the lcl ip */
676 kv6.key[0] = kv6.key[1] = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700677 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700678 if (rv == 0)
679 return session_manager_get_listener (proto, (u32) kv6.value);
680
Florin Corasdbd44562017-11-09 19:30:17 -0800681 make_v6_proxy_kv (&kv6, lcl, proto);
682 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
683 if (rv == 0)
684 return session_manager_get_listener (proto, (u32) kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -0700685 return 0;
686}
687
Florin Coras04e53442017-07-16 17:12:15 -0700688stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700689session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
690 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700691{
Florin Corascea194d2017-10-02 00:18:51 -0700692 session_table_t *st;
693 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
694 if (!st)
695 return 0;
696 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
Florin Coras04e53442017-07-16 17:12:15 -0700697}
698
699stream_session_t *
Florin Corascea194d2017-10-02 00:18:51 -0700700session_lookup_listener (u32 table_index, session_endpoint_t * sep)
Florin Coras04e53442017-07-16 17:12:15 -0700701{
Florin Corascea194d2017-10-02 00:18:51 -0700702 session_table_t *st;
703 st = session_table_get (table_index);
704 if (!st)
705 return 0;
706 if (sep->is_ip4)
707 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
708 sep->transport_proto);
709 else
710 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
711 sep->transport_proto);
Florin Coras04e53442017-07-16 17:12:15 -0700712 return 0;
713}
714
Florin Corascea194d2017-10-02 00:18:51 -0700715int
716session_lookup_add_half_open (transport_connection_t * tc, u64 value)
717{
718 session_table_t *st;
719 session_kv4_t kv4;
720 session_kv6_t kv6;
721
722 st = session_table_get_or_alloc_for_connection (tc);
723 if (!st)
724 return 0;
725 if (tc->is_ip4)
726 {
727 make_v4_ss_kv_from_tc (&kv4, tc);
728 kv4.value = value;
729 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
730 1 /* is_add */ );
731 }
732 else
733 {
734 make_v6_ss_kv_from_tc (&kv6, tc);
735 kv6.value = value;
736 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
737 1 /* is_add */ );
738 }
739}
740
741int
742session_lookup_del_half_open (transport_connection_t * tc)
743{
744 session_table_t *st;
745 session_kv4_t kv4;
746 session_kv6_t kv6;
747
748 st = session_table_get_for_connection (tc);
749 if (!st)
750 return -1;
751 if (tc->is_ip4)
752 {
753 make_v4_ss_kv_from_tc (&kv4, tc);
754 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
755 0 /* is_add */ );
756 }
757 else
758 {
759 make_v6_ss_kv_from_tc (&kv6, tc);
760 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
761 0 /* is_add */ );
762 }
763}
764
Florin Coras04e53442017-07-16 17:12:15 -0700765u64
Florin Corascea194d2017-10-02 00:18:51 -0700766session_lookup_half_open_handle (transport_connection_t * tc)
Florin Coras04e53442017-07-16 17:12:15 -0700767{
Florin Corascea194d2017-10-02 00:18:51 -0700768 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700769 session_kv4_t kv4;
770 session_kv6_t kv6;
771 int rv;
772
Florin Corascea194d2017-10-02 00:18:51 -0700773 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
774 tc->fib_index);
775 if (!st)
776 return HALF_OPEN_LOOKUP_INVALID_VALUE;
777 if (tc->is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700778 {
Florin Corascea194d2017-10-02 00:18:51 -0700779 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700780 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700781 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700782 if (rv == 0)
783 return kv4.value;
Florin Corascea194d2017-10-02 00:18:51 -0700784 }
785 else
786 {
787 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
Florin Coras3cbc04b2017-10-02 00:18:51 -0700788 tc->rmt_port, tc->proto);
Florin Corascea194d2017-10-02 00:18:51 -0700789 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -0700790 if (rv == 0)
791 return kv6.value;
Florin Coras04e53442017-07-16 17:12:15 -0700792 }
793 return HALF_OPEN_LOOKUP_INVALID_VALUE;
794}
795
796transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700797session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
Florin Coras04e53442017-07-16 17:12:15 -0700798{
Florin Corascea194d2017-10-02 00:18:51 -0700799 u32 sst;
800
Florin Coras04e53442017-07-16 17:12:15 -0700801 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
Florin Corascea194d2017-10-02 00:18:51 -0700802 {
803 sst = session_type_from_proto_and_ip (proto, is_ip4);
804 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
805 }
Florin Coras04e53442017-07-16 17:12:15 -0700806 return 0;
807}
808
Florin Corascea194d2017-10-02 00:18:51 -0700809/**
810 * Lookup connection with ip4 and transport layer information
811 *
812 * This is used on the fast path so it needs to be fast. Thereby,
813 * duplication of code and 'hacks' allowed.
814 *
815 * The lookup is incremental and returns whenever something is matched. The
816 * steps are:
817 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -0700818 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -0700819 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -0800820 * - Try to find a fully-formed or local source wildcarded (listener bound to
821 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -0700822 * - return 0
823 *
824 * @param fib_index index of fib wherein the connection was received
825 * @param lcl local ip4 address
826 * @param rmt remote ip4 address
827 * @param lcl_port local port
828 * @param rmt_port remote port
829 * @param proto transport protocol (e.g., tcp, udp)
830 * @param thread_index thread index for request
831 *
832 * @return pointer to transport connection, if one is found, 0 otherwise
833 */
Florin Coras04e53442017-07-16 17:12:15 -0700834transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700835session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
836 ip4_address_t * rmt, u16 lcl_port,
837 u16 rmt_port, u8 proto, u32 thread_index)
Florin Coras04e53442017-07-16 17:12:15 -0700838{
Florin Corascea194d2017-10-02 00:18:51 -0700839 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700840 session_kv4_t kv4;
841 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800842 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700843 int rv;
844
Florin Corascea194d2017-10-02 00:18:51 -0700845 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
846 if (PREDICT_FALSE (!st))
847 return 0;
848
Florin Corasa2ff7b82017-11-08 17:55:03 -0800849 /*
850 * Lookup session amongst established ones
851 */
Florin Coras04e53442017-07-16 17:12:15 -0700852 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700853 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700854 if (rv == 0)
855 {
Florin Corascea194d2017-10-02 00:18:51 -0700856 ASSERT ((u32) (kv4.value >> 32) == thread_index);
857 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700858 return tp_vfts[s->session_type].get_connection (s->connection_index,
Florin Corascea194d2017-10-02 00:18:51 -0700859 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -0700860 }
861
Florin Corasa2ff7b82017-11-08 17:55:03 -0800862 /*
863 * Try half-open connections
864 */
Florin Corascea194d2017-10-02 00:18:51 -0700865 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700866 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700867 {
868 u32 sst = session_type_from_proto_and_ip (proto, 1);
869 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
870 }
Florin Coras1c710452017-10-17 00:03:13 -0700871
Florin Corasa2ff7b82017-11-08 17:55:03 -0800872 /*
873 * Check the session rules table
874 */
875 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
876 rmt, lcl_port, rmt_port);
877 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
878 return 0;
879 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
880 {
881 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
882 proto);
883 if (s)
884 return tp_vfts[s->session_type].get_listener (s->connection_index);
885 }
886
887 /*
888 * If nothing is found, check if any listener is available
889 */
890 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
891 if (s)
892 return tp_vfts[s->session_type].get_listener (s->connection_index);
893
894 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700895}
896
Florin Corascea194d2017-10-02 00:18:51 -0700897/**
898 * Lookup connection with ip4 and transport layer information
899 *
900 * Not optimized. This is used on the fast path so it needs to be fast.
901 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
902 * to that of @ref session_lookup_connection_wt4
903 *
904 * @param fib_index index of the fib wherein the connection was received
905 * @param lcl local ip4 address
906 * @param rmt remote ip4 address
907 * @param lcl_port local port
908 * @param rmt_port remote port
909 * @param proto transport protocol (e.g., tcp, udp)
910 *
911 * @return pointer to transport connection, if one is found, 0 otherwise
912 */
Florin Coras04e53442017-07-16 17:12:15 -0700913transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -0700914session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
915 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
916 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700917{
Florin Corascea194d2017-10-02 00:18:51 -0700918 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -0700919 session_kv4_t kv4;
920 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800921 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -0700922 int rv;
923
Florin Corascea194d2017-10-02 00:18:51 -0700924 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
925 if (PREDICT_FALSE (!st))
926 return 0;
927
Florin Corasa2ff7b82017-11-08 17:55:03 -0800928 /*
929 * Lookup session amongst established ones
930 */
Florin Coras04e53442017-07-16 17:12:15 -0700931 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -0700932 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700933 if (rv == 0)
934 {
Florin Corascea194d2017-10-02 00:18:51 -0700935 s = session_get_from_handle (kv4.value);
Florin Coras04e53442017-07-16 17:12:15 -0700936 return tp_vfts[s->session_type].get_connection (s->connection_index,
937 s->thread_index);
938 }
939
Florin Corasa2ff7b82017-11-08 17:55:03 -0800940 /*
941 * Try half-open connections
942 */
Florin Corascea194d2017-10-02 00:18:51 -0700943 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
Florin Coras04e53442017-07-16 17:12:15 -0700944 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -0700945 {
946 u32 sst = session_type_from_proto_and_ip (proto, 1);
947 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
948 }
Florin Corasa2ff7b82017-11-08 17:55:03 -0800949
950 /*
951 * Check the session rules table
952 */
953 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
954 rmt, lcl_port, rmt_port);
955 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
956 return 0;
957 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
958 {
959 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
960 proto);
961 if (s)
962 return tp_vfts[s->session_type].get_listener (s->connection_index);
963 }
964
965 /*
966 * If nothing is found, check if any listener is available
967 */
968 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
969 if (s)
970 return tp_vfts[s->session_type].get_listener (s->connection_index);
971
972 return 0;
Florin Coras04e53442017-07-16 17:12:15 -0700973}
974
Florin Corascea194d2017-10-02 00:18:51 -0700975/**
976 * Lookup session with ip4 and transport layer information
977 *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700978 * Important note: this may look into another thread's pool table and
979 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
980 * if needed as soon as possible.
981 *
982 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
983 * this returns a session as opposed to a transport connection and it does not
984 * try to lookup half-open sessions.
985 *
986 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -0700987 */
988stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -0700989session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
990 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -0700991{
Florin Corascea194d2017-10-02 00:18:51 -0700992 session_table_t *st;
993 session_kv4_t kv4;
994 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -0800995 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -0700996 int rv;
997
998 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
999 if (PREDICT_FALSE (!st))
1000 return 0;
1001
Florin Corasa2ff7b82017-11-08 17:55:03 -08001002 /*
1003 * Lookup session amongst established ones
1004 */
Florin Corascea194d2017-10-02 00:18:51 -07001005 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1006 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1007 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001008 return session_get_from_handle_safe (kv4.value);
Florin Corascea194d2017-10-02 00:18:51 -07001009
Florin Corasa2ff7b82017-11-08 17:55:03 -08001010 /*
1011 * Check the session rules table
1012 */
1013 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1014 rmt, lcl_port, rmt_port);
1015 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1016 return 0;
1017 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1018 {
1019 if ((s = session_lookup_action_to_session (action_index,
1020 FIB_PROTOCOL_IP4, proto)))
1021 return s;
1022 }
1023
1024 /*
1025 * If nothing is found, check if any listener is available
1026 */
Florin Corascea194d2017-10-02 00:18:51 -07001027 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
1028 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001029
1030 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001031}
1032
1033/**
1034 * Lookup connection with ip6 and transport layer information
1035 *
1036 * This is used on the fast path so it needs to be fast. Thereby,
1037 * duplication of code and 'hacks' allowed.
1038 *
1039 * The lookup is incremental and returns whenever something is matched. The
1040 * steps are:
1041 * - Try to find an established session
Florin Corascea194d2017-10-02 00:18:51 -07001042 * - Try to find a half-open connection
Florin Coras1c710452017-10-17 00:03:13 -07001043 * - Try session rules table
Florin Corasa2ff7b82017-11-08 17:55:03 -08001044 * - Try to find a fully-formed or local source wildcarded (listener bound to
1045 * all interfaces) listener session
Florin Corascea194d2017-10-02 00:18:51 -07001046 * - return 0
1047 *
1048 * @param fib_index index of the fib wherein the connection was received
1049 * @param lcl local ip6 address
1050 * @param rmt remote ip6 address
1051 * @param lcl_port local port
1052 * @param rmt_port remote port
1053 * @param proto transport protocol (e.g., tcp, udp)
1054 * @param thread_index thread index for request
1055 *
1056 * @return pointer to transport connection, if one is found, 0 otherwise
1057 */
1058transport_connection_t *
1059session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1060 ip6_address_t * rmt, u16 lcl_port,
1061 u16 rmt_port, u8 proto, u32 thread_index)
1062{
1063 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001064 stream_session_t *s;
1065 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001066 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001067 int rv;
1068
Florin Corascea194d2017-10-02 00:18:51 -07001069 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1070 if (PREDICT_FALSE (!st))
1071 return 0;
1072
Florin Coras04e53442017-07-16 17:12:15 -07001073 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001074 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001075 if (rv == 0)
1076 {
Florin Corascea194d2017-10-02 00:18:51 -07001077 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1078 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001079 return tp_vfts[s->session_type].get_connection (s->connection_index,
Florin Corascea194d2017-10-02 00:18:51 -07001080 thread_index);
Florin Coras04e53442017-07-16 17:12:15 -07001081 }
1082
Florin Corasa2ff7b82017-11-08 17:55:03 -08001083 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001084 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001085 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001086 {
1087 u32 sst = session_type_from_proto_and_ip (proto, 1);
1088 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1089 }
Florin Coras04e53442017-07-16 17:12:15 -07001090
Florin Corasa2ff7b82017-11-08 17:55:03 -08001091 /* Check the session rules table */
1092 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1093 rmt, lcl_port, rmt_port);
1094 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1095 return 0;
1096 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1097 {
1098 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1099 proto);
1100 if (s)
1101 return tp_vfts[s->session_type].get_listener (s->connection_index);
1102 }
1103
1104 /* If nothing is found, check if any listener is available */
1105 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1106 if (s)
1107 return tp_vfts[s->session_type].get_listener (s->connection_index);
1108
1109 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001110}
1111
Florin Corascea194d2017-10-02 00:18:51 -07001112/**
1113 * Lookup connection with ip6 and transport layer information
1114 *
1115 * Not optimized. This is used on the fast path so it needs to be fast.
1116 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1117 * to that of @ref session_lookup_connection_wt4
1118 *
1119 * @param fib_index index of the fib wherein the connection was received
1120 * @param lcl local ip6 address
1121 * @param rmt remote ip6 address
1122 * @param lcl_port local port
1123 * @param rmt_port remote port
1124 * @param proto transport protocol (e.g., tcp, udp)
1125 *
1126 * @return pointer to transport connection, if one is found, 0 otherwise
1127 */
Florin Coras04e53442017-07-16 17:12:15 -07001128transport_connection_t *
Florin Corascea194d2017-10-02 00:18:51 -07001129session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1130 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1131 u8 proto)
Florin Coras04e53442017-07-16 17:12:15 -07001132{
Florin Corascea194d2017-10-02 00:18:51 -07001133 session_table_t *st;
Florin Coras04e53442017-07-16 17:12:15 -07001134 stream_session_t *s;
1135 session_kv6_t kv6;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001136 u32 action_index;
Florin Coras04e53442017-07-16 17:12:15 -07001137 int rv;
1138
Florin Corascea194d2017-10-02 00:18:51 -07001139 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1140 if (PREDICT_FALSE (!st))
1141 return 0;
1142
Florin Coras04e53442017-07-16 17:12:15 -07001143 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
Florin Corascea194d2017-10-02 00:18:51 -07001144 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001145 if (rv == 0)
1146 {
Florin Corascea194d2017-10-02 00:18:51 -07001147 s = session_get_from_handle (kv6.value);
Florin Coras04e53442017-07-16 17:12:15 -07001148 return tp_vfts[s->session_type].get_connection (s->connection_index,
1149 s->thread_index);
1150 }
1151
Florin Corasa2ff7b82017-11-08 17:55:03 -08001152 /* Try half-open connections */
Florin Corascea194d2017-10-02 00:18:51 -07001153 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
Florin Coras04e53442017-07-16 17:12:15 -07001154 if (rv == 0)
Florin Corascea194d2017-10-02 00:18:51 -07001155 {
1156 u32 sst = session_type_from_proto_and_ip (proto, 1);
1157 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1158 }
Florin Coras04e53442017-07-16 17:12:15 -07001159
Florin Corasa2ff7b82017-11-08 17:55:03 -08001160 /* Check the session rules table */
1161 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1162 rmt, lcl_port, rmt_port);
1163 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1164 return 0;
1165 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1166 {
1167 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1168 proto);
1169 if (s)
1170 return tp_vfts[s->session_type].get_listener (s->connection_index);
1171 }
1172
1173 /* If nothing is found, check if any listener is available */
1174 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1175 if (s)
1176 return tp_vfts[s->session_type].get_listener (s->connection_index);
1177
1178 return 0;
Florin Coras04e53442017-07-16 17:12:15 -07001179}
1180
Florin Corascea194d2017-10-02 00:18:51 -07001181/**
1182 * Lookup session with ip6 and transport layer information
1183 *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001184 * Important note: this may look into another thread's pool table and
1185 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1186 * if needed as soon as possible.
1187 *
1188 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1189 * this returns a session as opposed to a transport connection and it does not
1190 * try to lookup half-open sessions.
1191 *
1192 * Typically used by dgram connections
Florin Corascea194d2017-10-02 00:18:51 -07001193 */
1194stream_session_t *
Florin Coras3cbc04b2017-10-02 00:18:51 -07001195session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1196 u16 lcl_port, u16 rmt_port, u8 proto)
Florin Corascea194d2017-10-02 00:18:51 -07001197{
1198 session_table_t *st;
1199 session_kv6_t kv6;
1200 stream_session_t *s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001201 u32 action_index;
Florin Corascea194d2017-10-02 00:18:51 -07001202 int rv;
1203
1204 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1205 if (PREDICT_FALSE (!st))
1206 return 0;
1207
1208 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1209 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1210 if (rv == 0)
Florin Coras3cbc04b2017-10-02 00:18:51 -07001211 return session_get_from_handle_safe (kv6.value);
Florin Corascea194d2017-10-02 00:18:51 -07001212
Florin Corasa2ff7b82017-11-08 17:55:03 -08001213 /* Check the session rules table */
1214 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1215 rmt, lcl_port, rmt_port);
1216 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1217 return 0;
1218 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1219 {
1220 if ((s =
1221 session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1222 proto)))
1223 return s;
1224 }
1225
Florin Corascea194d2017-10-02 00:18:51 -07001226 /* If nothing is found, check if any listener is available */
1227 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1228 return s;
Florin Corasa2ff7b82017-11-08 17:55:03 -08001229 return 0;
Florin Corascea194d2017-10-02 00:18:51 -07001230}
1231
1232u64
1233session_lookup_local_listener_make_handle (session_endpoint_t * sep)
1234{
1235 return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
1236 | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
1237 | (u32) sep->is_ip4);
1238}
1239
1240u8
1241session_lookup_local_is_handle (u64 handle)
1242{
1243 if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
1244 return 1;
1245 return 0;
1246}
1247
1248int
1249session_lookup_local_listener_parse_handle (u64 handle,
1250 session_endpoint_t * sep)
1251{
1252 u32 local_table_handle;
1253 if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
1254 return -1;
1255 local_table_handle = handle & 0xFFFFFFFFULL;
1256 sep->is_ip4 = local_table_handle & 0xff;
1257 local_table_handle >>= 8;
1258 sep->transport_proto = local_table_handle & 0xff;
1259 sep->port = local_table_handle >> 8;
1260 return 0;
1261}
1262
Florin Coras1c710452017-10-17 00:03:13 -07001263clib_error_t *
1264vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1265{
1266 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001267 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001268 session_table_t *st;
1269 u32 fib_index;
1270 u8 fib_proto;
1271 clib_error_t *error;
1272
1273 if (!app_ns)
1274 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1275 "invalid app ns");
1276 if (args->scope > 3)
1277 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1278 "invalid scope");
Florin Corasc97a7392017-11-05 23:07:07 -08001279 if (args->transport_proto != TRANSPORT_PROTO_TCP
1280 && args->transport_proto != TRANSPORT_PROTO_UDP)
1281 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1282 "invalid transport proto");
Florin Coras1c710452017-10-17 00:03:13 -07001283 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1284 {
1285 fib_proto = args->table_args.rmt.fp_proto;
1286 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1287 st = session_table_get_for_fib_index (fib_proto, fib_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001288 srt = &st->session_rules[args->transport_proto];
1289 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1290 {
1291 clib_error_report (error);
1292 return error;
1293 }
Florin Coras1c710452017-10-17 00:03:13 -07001294 }
1295 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1296 {
Florin Corasa2ff7b82017-11-08 17:55:03 -08001297 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1298 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1299 args->table_args.lcl_port = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001300 st = app_namespace_get_local_table (app_ns);
Florin Corasc97a7392017-11-05 23:07:07 -08001301 srt = &st->session_rules[args->transport_proto];
1302 error = session_rules_table_add_del (srt, &args->table_args);
Florin Coras1c710452017-10-17 00:03:13 -07001303 }
1304 return error;
1305}
1306
Florin Coras6c36f532017-11-03 18:32:34 -07001307/**
1308 * Mark (global) tables as pertaining to app ns
1309 */
1310void
1311session_lookup_set_tables_appns (app_namespace_t * app_ns)
1312{
1313 session_table_t *st;
1314 u32 fib_index;
1315 u8 fp;
1316
1317 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1318 {
1319 fib_index = app_namespace_get_fib_index (app_ns, fp);
1320 st = session_table_get_for_fib_index (fp, fib_index);
1321 if (st)
1322 st->appns_index = app_namespace_index (app_ns);
1323 }
1324}
1325
Florin Corascea194d2017-10-02 00:18:51 -07001326u8 *
1327format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1328{
1329 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1330 u32 is_local = va_arg (*args, u32);
1331 u8 *app_name, *str = 0;
1332 stream_session_t *session;
1333 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1334
1335 char *proto = key->proto == TRANSPORT_PROTO_TCP ? "T" : "U";
1336 if (!is_local)
1337 {
1338 session = session_get_from_handle (kvp->value);
1339 app_name = application_name_from_index (session->app_index);
1340 str = format (0, "[%s] %U:%d->%U:%d", proto, format_ip4_address,
1341 &key->src, clib_net_to_host_u16 (key->src_port),
1342 format_ip4_address, &key->dst,
1343 clib_net_to_host_u16 (key->dst_port));
1344 s = format (s, "%-40v%-30v", str, app_name);
1345 }
1346 else
1347 {
1348 app_name = application_name_from_index (kvp->value);
1349 str = format (0, "[%s] %U:%d", proto, format_ip4_address,
1350 &key->src, clib_net_to_host_u16 (key->src_port));
1351 s = format (s, "%-30v%-30v", str, app_name);
1352 }
1353 vec_free (app_name);
1354 return s;
1355}
1356
1357typedef struct _ip4_session_table_show_ctx_t
1358{
1359 vlib_main_t *vm;
1360 u8 is_local;
1361} ip4_session_table_show_ctx_t;
1362
1363static int
1364ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1365{
1366 ip4_session_table_show_ctx_t *ctx = arg;
1367 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1368 ctx->is_local);
1369 return 1;
1370}
1371
1372void
1373session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1374 u8 type, u8 is_local)
1375{
1376 ip4_session_table_show_ctx_t ctx = {
1377 .vm = vm,
1378 .is_local = is_local,
1379 };
1380 if (!is_local)
1381 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1382 else
1383 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1384 switch (type)
1385 {
1386 /* main table v4 */
1387 case 0:
1388 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1389 &ctx);
1390 break;
1391 default:
1392 clib_warning ("not supported");
1393 }
1394}
Florin Coras66b11312017-07-31 17:18:03 -07001395
Florin Coras1c710452017-10-17 00:03:13 -07001396static clib_error_t *
1397session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1398 vlib_cli_command_t * cmd)
1399{
Florin Corasc97a7392017-11-05 23:07:07 -08001400 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001401 u32 appns_index, scope = 0;
1402 ip46_address_t lcl_ip, rmt_ip;
1403 u8 is_ip4 = 1, conn_set = 0;
1404 u8 fib_proto, is_add = 1, *ns_id = 0;
Florin Corasc97a7392017-11-05 23:07:07 -08001405 u8 *tag = 0;
Florin Coras1c710452017-10-17 00:03:13 -07001406 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001407 clib_error_t *error;
Florin Coras1c710452017-10-17 00:03:13 -07001408
1409 memset (&lcl_ip, 0, sizeof (lcl_ip));
1410 memset (&rmt_ip, 0, sizeof (rmt_ip));
1411 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1412 {
1413 if (unformat (input, "del"))
1414 is_add = 0;
1415 else if (unformat (input, "add"))
1416 ;
1417 else if (unformat (input, "appns %_%v%_", &ns_id))
1418 ;
1419 else if (unformat (input, "scope global"))
1420 scope = SESSION_RULE_SCOPE_GLOBAL;
1421 else if (unformat (input, "scope local"))
1422 scope = SESSION_RULE_SCOPE_LOCAL;
1423 else if (unformat (input, "scope all"))
1424 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1425 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1426 ;
1427 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1428 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1429 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1430 &rmt_port))
1431 {
1432 is_ip4 = 1;
1433 conn_set = 1;
1434 }
1435 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1436 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1437 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1438 &rmt_port))
1439 {
1440 is_ip4 = 0;
1441 conn_set = 1;
1442 }
1443 else if (unformat (input, "action %d", &action))
1444 ;
Florin Corasc97a7392017-11-05 23:07:07 -08001445 else if (unformat (input, "tag %_%v%_", &tag))
1446 ;
Florin Coras1c710452017-10-17 00:03:13 -07001447 else
1448 return clib_error_return (0, "unknown input `%U'",
1449 format_unformat_error, input);
1450 }
1451
Florin Corasc97a7392017-11-05 23:07:07 -08001452 if (proto == ~0)
1453 {
1454 vlib_cli_output (vm, "proto must be set");
1455 return 0;
1456 }
1457 if (is_add && !conn_set && action == ~0)
1458 {
1459 vlib_cli_output (vm, "connection and action must be set for add");
1460 return 0;
1461 }
1462 if (!is_add && !tag && !conn_set)
1463 {
1464 vlib_cli_output (vm, "connection or tag must be set for delete");
1465 return 0;
1466 }
1467 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1468 {
1469 vlib_cli_output (vm, "tag too long (max u64)");
1470 return 0;
1471 }
Florin Coras1c710452017-10-17 00:03:13 -07001472
1473 if (ns_id)
1474 {
1475 app_ns = app_namespace_get_from_id (ns_id);
1476 if (!app_ns)
Florin Corasc97a7392017-11-05 23:07:07 -08001477 {
1478 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1479 return 0;
1480 }
Florin Coras1c710452017-10-17 00:03:13 -07001481 }
1482 else
1483 {
1484 app_ns = app_namespace_get_default ();
1485 }
1486 appns_index = app_namespace_index (app_ns);
1487
1488 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1489 session_rule_add_del_args_t args = {
1490 .table_args.lcl.fp_addr = lcl_ip,
1491 .table_args.lcl.fp_len = lcl_plen,
1492 .table_args.lcl.fp_proto = fib_proto,
1493 .table_args.rmt.fp_addr = rmt_ip,
1494 .table_args.rmt.fp_len = rmt_plen,
1495 .table_args.rmt.fp_proto = fib_proto,
1496 .table_args.lcl_port = lcl_port,
1497 .table_args.rmt_port = rmt_port,
1498 .table_args.action_index = action,
1499 .table_args.is_add = is_add,
Florin Corasc97a7392017-11-05 23:07:07 -08001500 .table_args.tag = tag,
Florin Coras1c710452017-10-17 00:03:13 -07001501 .appns_index = appns_index,
1502 .scope = scope,
1503 };
Florin Corasc97a7392017-11-05 23:07:07 -08001504 error = vnet_session_rule_add_del (&args);
1505 vec_free (tag);
1506 return error;
Florin Coras1c710452017-10-17 00:03:13 -07001507}
1508
1509/* *INDENT-OFF* */
1510VLIB_CLI_COMMAND (session_rule_command, static) =
1511{
1512 .path = "session rule",
1513 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1514 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1515 .function = session_rule_command_fn,
1516};
1517/* *INDENT-ON* */
1518
Florin Coras7999e832017-10-31 01:51:04 -07001519void
1520session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1521 u8 transport_proto)
1522{
1523 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001524 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001525 session_table_t *st;
1526 st = session_table_get_for_fib_index (fib_index, fib_proto);
Florin Corasc97a7392017-11-05 23:07:07 -08001527 srt = &st->session_rules[transport_proto];
1528 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001529}
1530
1531void
1532session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1533 u8 transport_proto)
1534{
1535 vlib_main_t *vm = vlib_get_main ();
Florin Corasc97a7392017-11-05 23:07:07 -08001536 session_rules_table_t *srt;
Florin Coras7999e832017-10-31 01:51:04 -07001537 session_table_t *st;
1538 st = session_table_get (table_index);
Florin Corasc97a7392017-11-05 23:07:07 -08001539 srt = &st->session_rules[transport_proto];
1540 session_rules_table_cli_dump (vm, srt, fib_proto);
Florin Coras7999e832017-10-31 01:51:04 -07001541}
1542
Florin Coras1c710452017-10-17 00:03:13 -07001543static clib_error_t *
1544show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1545 vlib_cli_command_t * cmd)
1546{
1547 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1548 u32 fib_index, scope = 0;
1549 ip46_address_t lcl_ip, rmt_ip;
1550 u8 is_ip4 = 1, show_one = 0;
1551 app_namespace_t *app_ns;
Florin Corasc97a7392017-11-05 23:07:07 -08001552 session_rules_table_t *srt;
Florin Coras1c710452017-10-17 00:03:13 -07001553 session_table_t *st;
1554 u8 *ns_id = 0, fib_proto;
1555
1556 memset (&lcl_ip, 0, sizeof (lcl_ip));
1557 memset (&rmt_ip, 0, sizeof (rmt_ip));
1558 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1559 {
1560 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1561 ;
1562 else if (unformat (input, "appns %_%v%_", &ns_id))
1563 ;
1564 else if (unformat (input, "scope global"))
1565 scope = 1;
1566 else if (unformat (input, "scope local"))
1567 scope = 2;
1568 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1569 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1570 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1571 &rmt_port))
1572 {
1573 is_ip4 = 1;
1574 show_one = 1;
1575 }
1576 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1577 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1578 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1579 &rmt_port))
1580 {
1581 is_ip4 = 0;
1582 show_one = 1;
1583 }
1584 else
1585 return clib_error_return (0, "unknown input `%U'",
1586 format_unformat_error, input);
1587 }
1588
1589 if (transport_proto == ~0)
1590 {
1591 vlib_cli_output (vm, "transport proto must be set");
1592 return 0;
1593 }
1594
1595 if (ns_id)
1596 {
1597 app_ns = app_namespace_get_from_id (ns_id);
1598 if (!app_ns)
1599 {
1600 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1601 return 0;
1602 }
1603 }
1604 else
1605 {
1606 app_ns = app_namespace_get_default ();
1607 }
1608
1609 if (scope == 1 || scope == 0)
1610 {
1611 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1612 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1613 st = session_table_get_for_fib_index (fib_proto, fib_index);
1614 }
1615 else
1616 {
1617 st = app_namespace_get_local_table (app_ns);
1618 }
1619
1620 if (show_one)
1621 {
Florin Corasc97a7392017-11-05 23:07:07 -08001622 srt = &st->session_rules[transport_proto];
1623 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1624 rmt_port, is_ip4);
Florin Coras1c710452017-10-17 00:03:13 -07001625 return 0;
1626 }
1627
Florin Corasc97a7392017-11-05 23:07:07 -08001628 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1629 transport_proto);
1630 srt = &st->session_rules[transport_proto];
1631 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1632 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
Florin Coras1c710452017-10-17 00:03:13 -07001633
1634 vec_free (ns_id);
1635 return 0;
1636}
1637
1638/* *INDENT-OFF* */
1639VLIB_CLI_COMMAND (show_session_rules_command, static) =
1640{
1641 .path = "show session rules",
1642 .short_help = "show session rules [appns <id> proto <proto> <lcl-ip/plen>"
1643 " <lcl-port> <rmt-ip/plen> <rmt-port>]",
1644 .function = show_session_rules_command_fn,
1645};
1646/* *INDENT-ON* */
1647
Florin Coras04e53442017-07-16 17:12:15 -07001648void
1649session_lookup_init (void)
1650{
Florin Corascea194d2017-10-02 00:18:51 -07001651 /*
1652 * Allocate default table and map it to fib_index 0
1653 */
1654 session_table_t *st = session_table_alloc ();
1655 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1656 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001657 st->active_fib_proto = FIB_PROTOCOL_IP4;
1658 session_table_init (st, FIB_PROTOCOL_IP4);
Florin Corascea194d2017-10-02 00:18:51 -07001659 st = session_table_alloc ();
1660 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1661 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
Florin Coras6c36f532017-11-03 18:32:34 -07001662 st->active_fib_proto = FIB_PROTOCOL_IP6;
1663 session_table_init (st, FIB_PROTOCOL_IP6);
Florin Coras04e53442017-07-16 17:12:15 -07001664}
1665
1666/*
1667 * fd.io coding-style-patch-verification: ON
1668 *
1669 * Local Variables:
1670 * eval: (c-set-style "gnu")
1671 * End:
1672 */