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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 /** Generate typed init functions for multiple hash table styles... */
17 #include <vppinfra/bihash_16_8.h>
18 #include <vppinfra/bihash_template.h>
20 #include <vppinfra/bihash_template.c>
22 #undef __included_bihash_template_h__
24 #include <vppinfra/bihash_48_8.h>
25 #include <vppinfra/bihash_template.h>
27 #include <vppinfra/bihash_template.c>
28 #include <vnet/session/session_lookup.h>
29 #include <vnet/session/session.h>
30 #include <vnet/session/application.h>
33 * External vector of per transport virtual functions table
35 extern transport_proto_vft_t *tp_vfts;
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
41 static u32 *fib_index_to_table_index[2];
45 typedef CLIB_PACKED (struct {
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.
62 }) v4_connection_key_t;
64 typedef CLIB_PACKED (struct {
79 }) v6_connection_key_t;
82 typedef clib_bihash_kv_16_8_t session_kv4_t;
83 typedef clib_bihash_kv_48_8_t session_kv6_t;
86 make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
87 u16 lcl_port, u16 rmt_port, u8 proto)
89 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
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;
101 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
104 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
106 key->src.as_u32 = lcl->as_u32;
108 key->src_port = lcl_port;
116 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * t)
118 make_v4_ss_kv (kv, &t->lcl_ip.ip4, &t->rmt_ip.ip4, t->lcl_port, t->rmt_port,
119 session_type_from_proto_and_ip (t->proto, 1));
123 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
124 u16 lcl_port, u16 rmt_port, u8 proto)
126 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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;
141 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
144 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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;
159 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * t)
161 make_v6_ss_kv (kv, &t->lcl_ip.ip6, &t->rmt_ip.ip6, t->lcl_port, t->rmt_port,
162 session_type_from_proto_and_ip (t->proto, 0));
165 static session_table_t *
166 session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
170 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
172 st = session_table_alloc ();
173 table_index = session_table_index (st);
174 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;
181 table_index = fib_index_to_table_index[fib_proto][fib_index];
182 return session_table_get (table_index);
186 static session_table_t *
187 session_table_get_or_alloc_for_connection (transport_connection_t * tc)
190 fib_proto = transport_connection_fib_proto (tc);
191 return session_table_get_or_alloc (fib_proto, tc->fib_index);
194 static session_table_t *
195 session_table_get_for_connection (transport_connection_t * tc)
197 u32 fib_proto = transport_connection_fib_proto (tc);
198 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
201 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
204 static session_table_t *
205 session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
207 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
209 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
213 session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
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];
221 * Add transport connection to a session table
223 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
224 * is added to requested session table.
226 * @param tc transport connection to be added
227 * @param value value to be stored
229 * @return non-zero if failure
232 session_lookup_add_connection (transport_connection_t * tc, u64 value)
238 st = session_table_get_or_alloc_for_connection (tc);
243 make_v4_ss_kv_from_tc (&kv4, tc);
245 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
250 make_v6_ss_kv_from_tc (&kv6, tc);
252 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
258 session_lookup_add_session_endpoint (u32 table_index,
259 session_endpoint_t * sep, u64 value)
265 st = session_table_get (table_index);
270 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
271 sep->transport_proto);
273 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
277 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
278 sep->transport_proto);
280 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
285 session_lookup_del_session_endpoint (u32 table_index,
286 session_endpoint_t * sep)
292 st = session_table_get (table_index);
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);
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);
310 * Delete transport connection from session table
312 * @param table_index session table index
313 * @param tc transport connection to be removed
315 * @return non-zero if failure
318 session_lookup_del_connection (transport_connection_t * tc)
324 st = session_table_get_for_connection (tc);
329 make_v4_ss_kv_from_tc (&kv4, tc);
330 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
335 make_v6_ss_kv_from_tc (&kv6, tc);
336 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
342 session_lookup_del_session (stream_session_t * s)
344 transport_connection_t *ts;
345 ts = tp_vfts[s->session_type].get_connection (s->connection_index,
347 return session_lookup_del_connection (ts);
351 session_lookup_action_to_session_index (u32 action_index)
353 if (action_index != SESSION_RULES_TABLE_ACTION_DROP)
355 return SESSION_INVALID_INDEX;
358 static stream_session_t *
359 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
363 app = application_get (app_index);
367 return application_first_listener (app, fib_proto, transport_proto);
370 static stream_session_t *
371 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
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,
382 session_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)
386 session_rules_table_t *srt = &st->session_rules[proto];
387 u32 action_index, session_index;
388 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
390 session_index = session_lookup_action_to_session_index (action_index);
391 /* Nothing sophisticated for now, action index is app index */
392 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP4,
397 session_lookup_rules_table6 (session_table_t * st, u8 proto,
398 ip6_address_t * lcl, u16 lcl_port,
399 ip6_address_t * rmt, u16 rmt_port)
401 session_rules_table_t *srt = &st->session_rules[proto];
402 u32 action_index, session_index;
403 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
405 session_index = session_lookup_action_to_session_index (action_index);
406 return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP6,
411 * Lookup listener for session endpoint in table
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
417 * @return invalid handle if nothing is found, the handle of a valid listener
418 * or an action_index if a rule is hit
421 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
424 session_rules_table_t *srt;
430 sst = session_type_from_proto_and_ip (sep->transport_proto, sep->is_ip4);
431 st = session_table_get (table_index);
433 return SESSION_INVALID_HANDLE;
439 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port, sst);
440 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
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,
449 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
450 return session_lookup_action_to_session_index (ai);
458 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port, sst);
459 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
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,
469 if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
470 return session_lookup_action_to_session_index (ai);
473 return SESSION_INVALID_HANDLE;
477 * Look up endpoint in local session table
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.
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)
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.
493 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
495 session_rules_table_t *srt;
500 st = session_table_get (table_index);
502 return SESSION_INVALID_INDEX;
503 ASSERT (st->is_local);
511 * Check if endpoint has special rules associated
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,
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);
523 * Check if session endpoint is a listener
525 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);
529 return (u32) kv4.value;
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
536 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
538 return (u32) kv4.value;
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,
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);
554 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);
558 return (u32) kv6.value;
561 * Zero out the ip. Same logic as above.
563 kv6.key[0] = kv6.key[1] = 0;
564 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
566 return (u32) kv6.value;
568 return APP_INVALID_INDEX;
571 static stream_session_t *
572 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
573 u16 lcl_port, u8 proto)
578 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
579 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
581 return session_manager_get_listener (proto, (u32) kv4.value);
583 /* Zero out the lcl ip */
585 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
587 return session_manager_get_listener (proto, (u32) kv4.value);
593 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
597 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
600 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
603 static stream_session_t *
604 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
605 u16 lcl_port, u8 proto)
610 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
611 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
613 return session_manager_get_listener (proto, (u32) kv6.value);
615 /* Zero out the lcl ip */
616 kv6.key[0] = kv6.key[1] = 0;
617 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
619 return session_manager_get_listener (proto, (u32) kv6.value);
625 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
629 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
632 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
636 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
639 st = session_table_get (table_index);
643 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
644 sep->transport_proto);
646 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
647 sep->transport_proto);
652 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
658 st = session_table_get_or_alloc_for_connection (tc);
663 make_v4_ss_kv_from_tc (&kv4, tc);
665 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
670 make_v6_ss_kv_from_tc (&kv6, tc);
672 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
678 session_lookup_del_half_open (transport_connection_t * tc)
684 st = session_table_get_for_connection (tc);
689 make_v4_ss_kv_from_tc (&kv4, tc);
690 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
695 make_v6_ss_kv_from_tc (&kv6, tc);
696 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
702 session_lookup_half_open_handle (transport_connection_t * tc)
709 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
712 return HALF_OPEN_LOOKUP_INVALID_VALUE;
715 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
716 tc->rmt_port, tc->proto);
717 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
723 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
724 tc->rmt_port, tc->proto);
725 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
729 return HALF_OPEN_LOOKUP_INVALID_VALUE;
732 transport_connection_t *
733 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
737 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
739 sst = session_type_from_proto_and_ip (proto, is_ip4);
740 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
746 * Lookup connection with ip4 and transport layer information
748 * This is used on the fast path so it needs to be fast. Thereby,
749 * duplication of code and 'hacks' allowed.
751 * The lookup is incremental and returns whenever something is matched. The
753 * - Try to find an established session
754 * - Try to find a half-open connection
755 * - Try session rules table
756 * - Try to find a fully-formed or local source wildcarded (listener bound to
757 * all interfaces) listener session
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
768 * @return pointer to transport connection, if one is found, 0 otherwise
770 transport_connection_t *
771 session_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)
781 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
782 if (PREDICT_FALSE (!st))
786 * Lookup session amongst established ones
788 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
789 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
792 ASSERT ((u32) (kv4.value >> 32) == thread_index);
793 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
794 return tp_vfts[s->session_type].get_connection (s->connection_index,
799 * Try half-open connections
801 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
804 u32 sst = session_type_from_proto_and_ip (proto, 1);
805 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
809 * Check the session rules table
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)
815 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
817 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
820 return tp_vfts[s->session_type].get_listener (s->connection_index);
824 * If nothing is found, check if any listener is available
826 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
828 return tp_vfts[s->session_type].get_listener (s->connection_index);
834 * Lookup connection with ip4 and transport layer information
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
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)
847 * @return pointer to transport connection, if one is found, 0 otherwise
849 transport_connection_t *
850 session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
851 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
860 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
861 if (PREDICT_FALSE (!st))
865 * Lookup session amongst established ones
867 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
868 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
871 s = session_get_from_handle (kv4.value);
872 return tp_vfts[s->session_type].get_connection (s->connection_index,
877 * Try half-open connections
879 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
882 u32 sst = session_type_from_proto_and_ip (proto, 1);
883 return tp_vfts[sst].get_half_open (kv4.value & 0xFFFFFFFF);
887 * Check the session rules table
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)
893 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
895 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
898 return tp_vfts[s->session_type].get_listener (s->connection_index);
902 * If nothing is found, check if any listener is available
904 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
906 return tp_vfts[s->session_type].get_listener (s->connection_index);
912 * Lookup session with ip4 and transport layer information
914 * 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.
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.
922 * Typically used by dgram connections
925 session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
926 u16 lcl_port, u16 rmt_port, u8 proto)
934 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
935 if (PREDICT_FALSE (!st))
939 * Lookup session amongst established ones
941 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);
944 return session_get_from_handle_safe (kv4.value);
947 * Check the session rules table
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)
953 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
955 if ((s = session_lookup_action_to_session (action_index,
956 FIB_PROTOCOL_IP4, proto)))
961 * If nothing is found, check if any listener is available
963 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
970 * Lookup connection with ip6 and transport layer information
972 * This is used on the fast path so it needs to be fast. Thereby,
973 * duplication of code and 'hacks' allowed.
975 * The lookup is incremental and returns whenever something is matched. The
977 * - Try to find an established session
978 * - Try to find a half-open connection
979 * - Try session rules table
980 * - Try to find a fully-formed or local source wildcarded (listener bound to
981 * all interfaces) listener session
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
992 * @return pointer to transport connection, if one is found, 0 otherwise
994 transport_connection_t *
995 session_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)
1000 stream_session_t *s;
1005 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1006 if (PREDICT_FALSE (!st))
1009 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1010 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1013 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1014 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1015 return tp_vfts[s->session_type].get_connection (s->connection_index,
1019 /* Try half-open connections */
1020 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1023 u32 sst = session_type_from_proto_and_ip (proto, 1);
1024 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1027 /* 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)
1032 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1034 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1037 return tp_vfts[s->session_type].get_listener (s->connection_index);
1040 /* If nothing is found, check if any listener is available */
1041 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1043 return tp_vfts[s->session_type].get_listener (s->connection_index);
1049 * Lookup connection with ip6 and transport layer information
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
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)
1062 * @return pointer to transport connection, if one is found, 0 otherwise
1064 transport_connection_t *
1065 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1066 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1069 session_table_t *st;
1070 stream_session_t *s;
1075 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1076 if (PREDICT_FALSE (!st))
1079 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1080 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1083 s = session_get_from_handle (kv6.value);
1084 return tp_vfts[s->session_type].get_connection (s->connection_index,
1088 /* Try half-open connections */
1089 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1092 u32 sst = session_type_from_proto_and_ip (proto, 1);
1093 return tp_vfts[sst].get_half_open (kv6.value & 0xFFFFFFFF);
1096 /* 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)
1101 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1103 s = session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1106 return tp_vfts[s->session_type].get_listener (s->connection_index);
1109 /* If nothing is found, check if any listener is available */
1110 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1112 return tp_vfts[s->session_type].get_listener (s->connection_index);
1118 * Lookup session with ip6 and transport layer information
1120 * 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.
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.
1128 * Typically used by dgram connections
1131 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1132 u16 lcl_port, u16 rmt_port, u8 proto)
1134 session_table_t *st;
1136 stream_session_t *s;
1140 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1141 if (PREDICT_FALSE (!st))
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);
1147 return session_get_from_handle_safe (kv6.value);
1149 /* 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)
1154 if (action_index != SESSION_RULES_TABLE_ACTION_NONE)
1157 session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1162 /* If nothing is found, check if any listener is available */
1163 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1169 session_lookup_local_listener_make_handle (session_endpoint_t * sep)
1171 return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
1172 | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
1173 | (u32) sep->is_ip4);
1177 session_lookup_local_is_handle (u64 handle)
1179 if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
1185 session_lookup_local_listener_parse_handle (u64 handle,
1186 session_endpoint_t * sep)
1188 u32 local_table_handle;
1189 if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
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;
1200 vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1202 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1203 session_rules_table_t *srt;
1204 session_table_t *st;
1207 clib_error_t *error;
1210 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1212 if (args->scope > 3)
1213 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1215 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");
1219 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
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);
1224 srt = &st->session_rules[args->transport_proto];
1225 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1227 clib_error_report (error);
1231 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1233 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;
1236 st = app_namespace_get_local_table (app_ns);
1237 srt = &st->session_rules[args->transport_proto];
1238 error = session_rules_table_add_del (srt, &args->table_args);
1244 * Mark (global) tables as pertaining to app ns
1247 session_lookup_set_tables_appns (app_namespace_t * app_ns)
1249 session_table_t *st;
1253 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1255 fib_index = app_namespace_get_fib_index (app_ns, fp);
1256 st = session_table_get_for_fib_index (fp, fib_index);
1258 st->appns_index = app_namespace_index (app_ns);
1263 format_ip4_session_lookup_kvp (u8 * s, va_list * args)
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;
1271 char *proto = key->proto == TRANSPORT_PROTO_TCP ? "T" : "U";
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);
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);
1289 vec_free (app_name);
1293 typedef struct _ip4_session_table_show_ctx_t
1297 } ip4_session_table_show_ctx_t;
1300 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1302 ip4_session_table_show_ctx_t *ctx = arg;
1303 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1309 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1310 u8 type, u8 is_local)
1312 ip4_session_table_show_ctx_t ctx = {
1314 .is_local = is_local,
1317 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1319 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1324 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1328 clib_warning ("not supported");
1332 static clib_error_t *
1333 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1334 vlib_cli_command_t * cmd)
1336 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1337 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;
1342 app_namespace_t *app_ns;
1343 clib_error_t *error;
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)
1349 if (unformat (input, "del"))
1351 else if (unformat (input, "add"))
1353 else if (unformat (input, "appns %_%v%_", &ns_id))
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))
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,
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,
1379 else if (unformat (input, "action %d", &action))
1381 else if (unformat (input, "tag %_%v%_", &tag))
1384 return clib_error_return (0, "unknown input `%U'",
1385 format_unformat_error, input);
1390 vlib_cli_output (vm, "proto must be set");
1393 if (is_add && !conn_set && action == ~0)
1395 vlib_cli_output (vm, "connection and action must be set for add");
1398 if (!is_add && !tag && !conn_set)
1400 vlib_cli_output (vm, "connection or tag must be set for delete");
1403 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1405 vlib_cli_output (vm, "tag too long (max u64)");
1411 app_ns = app_namespace_get_from_id (ns_id);
1414 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1420 app_ns = app_namespace_get_default ();
1422 appns_index = app_namespace_index (app_ns);
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,
1436 .table_args.tag = tag,
1437 .appns_index = appns_index,
1440 error = vnet_session_rule_add_del (&args);
1446 VLIB_CLI_COMMAND (session_rule_command, static) =
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,
1456 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1459 vlib_main_t *vm = vlib_get_main ();
1460 session_rules_table_t *srt;
1461 session_table_t *st;
1462 st = session_table_get_for_fib_index (fib_index, fib_proto);
1463 srt = &st->session_rules[transport_proto];
1464 session_rules_table_cli_dump (vm, srt, fib_proto);
1468 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1471 vlib_main_t *vm = vlib_get_main ();
1472 session_rules_table_t *srt;
1473 session_table_t *st;
1474 st = session_table_get (table_index);
1475 srt = &st->session_rules[transport_proto];
1476 session_rules_table_cli_dump (vm, srt, fib_proto);
1479 static clib_error_t *
1480 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1481 vlib_cli_command_t * cmd)
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;
1488 session_rules_table_t *srt;
1489 session_table_t *st;
1490 u8 *ns_id = 0, fib_proto;
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)
1496 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1498 else if (unformat (input, "appns %_%v%_", &ns_id))
1500 else if (unformat (input, "scope global"))
1502 else if (unformat (input, "scope local"))
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,
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,
1521 return clib_error_return (0, "unknown input `%U'",
1522 format_unformat_error, input);
1525 if (transport_proto == ~0)
1527 vlib_cli_output (vm, "transport proto must be set");
1533 app_ns = app_namespace_get_from_id (ns_id);
1536 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1542 app_ns = app_namespace_get_default ();
1545 if (scope == 1 || scope == 0)
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);
1553 st = app_namespace_get_local_table (app_ns);
1558 srt = &st->session_rules[transport_proto];
1559 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1564 vlib_cli_output (vm, "%U rules table", format_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);
1575 VLIB_CLI_COMMAND (show_session_rules_command, static) =
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,
1585 session_lookup_init (void)
1588 * Allocate default table and map it to fib_index 0
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);
1593 st->active_fib_proto = FIB_PROTOCOL_IP4;
1594 session_table_init (st, FIB_PROTOCOL_IP4);
1595 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);
1598 st->active_fib_proto = FIB_PROTOCOL_IP6;
1599 session_table_init (st, FIB_PROTOCOL_IP6);
1603 * fd.io coding-style-patch-verification: ON
1606 * eval: (c-set-style "gnu")