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_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
118 v4_connection_key_t *key = (v4_connection_key_t *) kv->key;
120 key->src.as_u32 = lcl->as_u32;
130 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
132 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
133 tc->rmt_port, tc->proto);
137 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
138 u16 lcl_port, u16 rmt_port, u8 proto)
140 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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;
155 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
158 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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;
173 make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
175 v6_connection_key_t *key = (v6_connection_key_t *) kv->key;
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;
190 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
192 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
193 tc->rmt_port, tc->proto);
196 static session_table_t *
197 session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
201 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
203 st = session_table_alloc ();
204 table_index = session_table_index (st);
205 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;
208 session_table_init (st, fib_proto);
213 table_index = fib_index_to_table_index[fib_proto][fib_index];
214 return session_table_get (table_index);
218 static session_table_t *
219 session_table_get_or_alloc_for_connection (transport_connection_t * tc)
222 fib_proto = transport_connection_fib_proto (tc);
223 return session_table_get_or_alloc (fib_proto, tc->fib_index);
226 static session_table_t *
227 session_table_get_for_connection (transport_connection_t * tc)
229 u32 fib_proto = transport_connection_fib_proto (tc);
230 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
233 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
236 static session_table_t *
237 session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
239 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
241 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
245 session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
247 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
248 return SESSION_TABLE_INVALID_INDEX;
249 return fib_index_to_table_index[fib_proto][fib_index];
253 * Add transport connection to a session table
255 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
256 * is added to requested session table.
258 * @param tc transport connection to be added
259 * @param value value to be stored
261 * @return non-zero if failure
264 session_lookup_add_connection (transport_connection_t * tc, u64 value)
270 st = session_table_get_or_alloc_for_connection (tc);
275 make_v4_ss_kv_from_tc (&kv4, tc);
277 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
282 make_v6_ss_kv_from_tc (&kv6, tc);
284 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
290 session_lookup_add_session_endpoint (u32 table_index,
291 session_endpoint_t * sep, u64 value)
297 st = session_table_get (table_index);
302 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
303 sep->transport_proto);
305 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
309 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
310 sep->transport_proto);
312 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
317 session_lookup_del_session_endpoint (u32 table_index,
318 session_endpoint_t * sep)
324 st = session_table_get (table_index);
329 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
330 sep->transport_proto);
331 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
335 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
336 sep->transport_proto);
337 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
342 * Delete transport connection from session table
344 * @param table_index session table index
345 * @param tc transport connection to be removed
347 * @return non-zero if failure
350 session_lookup_del_connection (transport_connection_t * tc)
356 st = session_table_get_for_connection (tc);
361 make_v4_ss_kv_from_tc (&kv4, tc);
362 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
367 make_v6_ss_kv_from_tc (&kv6, tc);
368 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
374 session_lookup_del_session (stream_session_t * s)
376 transport_proto_t tp = session_get_transport_proto (s);
377 transport_connection_t *ts;
378 ts = tp_vfts[tp].get_connection (s->connection_index, s->thread_index);
379 return session_lookup_del_connection (ts);
383 session_lookup_action_index_is_valid (u32 action_index)
385 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
386 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
392 session_lookup_action_to_app_index (u32 action_index)
394 switch (action_index)
396 case SESSION_RULES_TABLE_ACTION_DROP:
397 return APP_DROP_INDEX;
398 case SESSION_RULES_TABLE_ACTION_ALLOW:
399 case SESSION_RULES_TABLE_INVALID_INDEX:
400 return APP_INVALID_INDEX;
406 static stream_session_t *
407 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
411 app = application_get_if_valid (app_index);
415 return application_first_listener (app, fib_proto, transport_proto);
418 static stream_session_t *
419 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
423 app_index = session_lookup_action_to_app_index (action_index);
424 /* Nothing sophisticated for now, action index is app index */
425 return session_lookup_app_listen_session (app_index, fib_proto,
430 session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
431 ip4_address_t * lcl, u16 lcl_port,
432 ip4_address_t * rmt, u16 rmt_port)
434 session_rules_table_t *srt = &st->session_rules[proto];
435 u32 action_index, app_index;
436 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
438 app_index = session_lookup_action_to_app_index (action_index);
439 /* Nothing sophisticated for now, action index is app index */
440 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
445 session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
446 ip6_address_t * lcl, u16 lcl_port,
447 ip6_address_t * rmt, u16 rmt_port)
449 session_rules_table_t *srt = &st->session_rules[proto];
450 u32 action_index, app_index;
451 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
453 app_index = session_lookup_action_to_app_index (action_index);
454 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
459 * Lookup listener for session endpoint in table
461 * @param table_index table where the endpoint should be looked up
462 * @param sep session endpoint to be looked up
463 * @param use_rules flag that indicates if the session rules of the table
465 * @return invalid handle if nothing is found, the handle of a valid listener
466 * or an action_index if a rule is hit
469 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
472 session_rules_table_t *srt;
477 st = session_table_get (table_index);
479 return SESSION_INVALID_HANDLE;
485 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
486 sep->transport_proto);
487 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
492 memset (&lcl4, 0, sizeof (lcl4));
493 srt = &st->session_rules[sep->transport_proto];
494 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
496 if (session_lookup_action_index_is_valid (ai))
497 return session_lookup_action_to_app_index (ai);
505 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
506 sep->transport_proto);
507 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
513 memset (&lcl6, 0, sizeof (lcl6));
514 srt = &st->session_rules[sep->transport_proto];
515 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
517 if (session_lookup_action_index_is_valid (ai))
518 return session_lookup_action_to_app_index (ai);
521 return SESSION_INVALID_HANDLE;
525 * Look up endpoint in local session table
527 * The result, for now, is an application index and it may in the future
528 * be extended to a more complicated "action object". The only action we
529 * emulate now is "drop" and for that we return a special app index.
531 * Lookup logic is to check in order:
532 * - the rules in the table (connect acls)
533 * - session sub-table for a listener
534 * - session sub-table for a local listener (zeroed addr)
536 * @param table_index table where the lookup should be done
537 * @param sep session endpoint to be looked up
538 * @return index that can be interpreted as an app index or drop action.
541 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
543 session_rules_table_t *srt;
548 st = session_table_get (table_index);
550 return SESSION_INVALID_INDEX;
551 ASSERT (st->is_local);
559 * Check if endpoint has special rules associated
561 memset (&lcl4, 0, sizeof (lcl4));
562 srt = &st->session_rules[sep->transport_proto];
563 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
565 if (session_lookup_action_index_is_valid (ai))
566 return session_lookup_action_to_app_index (ai);
569 * Check if session endpoint is a listener
571 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
572 sep->transport_proto);
573 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
575 return (u32) kv4.value;
578 * Zero out the ip. Logic is that connect to local ips, say
579 * 127.0.0.1:port, can match 0.0.0.0:port
582 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
584 return (u32) kv4.value;
587 * Zero out the port and check if we have proxy
590 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
592 return (u32) kv4.value;
599 memset (&lcl6, 0, sizeof (lcl6));
600 srt = &st->session_rules[sep->transport_proto];
601 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
603 if (session_lookup_action_index_is_valid (ai))
604 return session_lookup_action_to_app_index (ai);
606 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
607 sep->transport_proto);
608 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
610 return (u32) kv6.value;
613 * Zero out the ip. Same logic as above.
615 kv6.key[0] = kv6.key[1] = 0;
616 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
618 return (u32) kv6.value;
621 * Zero out the port. Same logic as above.
623 kv6.key[4] = kv6.key[5] = 0;
624 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
626 return (u32) kv6.value;
628 return APP_INVALID_INDEX;
631 static stream_session_t *
632 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
633 u16 lcl_port, u8 proto)
637 session_type_t session_type;
640 * First, try a fully formed listener
642 session_type = session_type_from_proto_and_ip (proto, 1);
643 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
644 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
646 return session_manager_get_listener (session_type, (u32) kv4.value);
649 * Zero out the lcl ip and check if any 0/0 port binds have been done
652 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
654 return session_manager_get_listener (session_type, (u32) kv4.value);
657 * Zero out port and check if we have a proxy set up for our ip
659 make_v4_proxy_kv (&kv4, lcl, proto);
660 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
662 return session_manager_get_listener (session_type, (u32) kv4.value);
668 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
672 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
675 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
678 static stream_session_t *
679 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
680 u16 lcl_port, u8 proto)
684 session_type_t session_type;
686 session_type = session_type_from_proto_and_ip (proto, 0);
687 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
688 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
690 return session_manager_get_listener (session_type, (u32) kv6.value);
692 /* Zero out the lcl ip */
693 kv6.key[0] = kv6.key[1] = 0;
694 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
696 return session_manager_get_listener (session_type, (u32) kv6.value);
698 make_v6_proxy_kv (&kv6, lcl, proto);
699 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
701 return session_manager_get_listener (session_type, (u32) kv6.value);
706 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
710 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
713 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
717 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
720 st = session_table_get (table_index);
724 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
725 sep->transport_proto);
727 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
728 sep->transport_proto);
733 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
739 st = session_table_get_or_alloc_for_connection (tc);
744 make_v4_ss_kv_from_tc (&kv4, tc);
746 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
751 make_v6_ss_kv_from_tc (&kv6, tc);
753 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
759 session_lookup_del_half_open (transport_connection_t * tc)
765 st = session_table_get_for_connection (tc);
770 make_v4_ss_kv_from_tc (&kv4, tc);
771 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
776 make_v6_ss_kv_from_tc (&kv6, tc);
777 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
783 session_lookup_half_open_handle (transport_connection_t * tc)
790 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
793 return HALF_OPEN_LOOKUP_INVALID_VALUE;
796 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
797 tc->rmt_port, tc->proto);
798 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
804 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
805 tc->rmt_port, tc->proto);
806 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
810 return HALF_OPEN_LOOKUP_INVALID_VALUE;
813 transport_connection_t *
814 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
818 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
820 sst = session_type_from_proto_and_ip (proto, is_ip4);
821 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
827 * Lookup connection with ip4 and transport layer information
829 * This is used on the fast path so it needs to be fast. Thereby,
830 * duplication of code and 'hacks' allowed.
832 * The lookup is incremental and returns whenever something is matched. The
834 * - Try to find an established session
835 * - Try to find a half-open connection
836 * - Try session rules table
837 * - Try to find a fully-formed or local source wildcarded (listener bound to
838 * all interfaces) listener session
841 * @param fib_index index of fib wherein the connection was received
842 * @param lcl local ip4 address
843 * @param rmt remote ip4 address
844 * @param lcl_port local port
845 * @param rmt_port remote port
846 * @param proto transport protocol (e.g., tcp, udp)
847 * @param thread_index thread index for request
848 * @param is_filtered return flag that indicates if connection was filtered.
850 * @return pointer to transport connection, if one is found, 0 otherwise
852 transport_connection_t *
853 session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
854 ip4_address_t * rmt, u16 lcl_port,
855 u16 rmt_port, u8 proto, u32 thread_index,
864 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
865 if (PREDICT_FALSE (!st))
869 * Lookup session amongst established ones
871 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
872 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
875 ASSERT ((u32) (kv4.value >> 32) == thread_index);
876 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
877 return tp_vfts[proto].get_connection (s->connection_index,
882 * Try half-open connections
884 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
886 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
889 * Check the session rules table
891 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
892 rmt, lcl_port, rmt_port);
893 if (session_lookup_action_index_is_valid (action_index))
895 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
897 if ((s = session_lookup_action_to_session (action_index,
898 FIB_PROTOCOL_IP4, proto)))
899 return tp_vfts[proto].get_listener (s->connection_index);
904 * If nothing is found, check if any listener is available
906 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
908 return tp_vfts[proto].get_listener (s->connection_index);
914 * Lookup connection with ip4 and transport layer information
916 * Not optimized. This is used on the fast path so it needs to be fast.
917 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
918 * to that of @ref session_lookup_connection_wt4
920 * @param fib_index index of the fib wherein the connection was received
921 * @param lcl local ip4 address
922 * @param rmt remote ip4 address
923 * @param lcl_port local port
924 * @param rmt_port remote port
925 * @param proto transport protocol (e.g., tcp, udp)
927 * @return pointer to transport connection, if one is found, 0 otherwise
929 transport_connection_t *
930 session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
931 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
940 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
941 if (PREDICT_FALSE (!st))
945 * Lookup session amongst established ones
947 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
948 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
951 s = session_get_from_handle (kv4.value);
952 return tp_vfts[proto].get_connection (s->connection_index,
957 * Try half-open connections
959 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
961 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
964 * Check the session rules table
966 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
967 rmt, lcl_port, rmt_port);
968 if (session_lookup_action_index_is_valid (action_index))
970 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
972 if ((s = session_lookup_action_to_session (action_index,
973 FIB_PROTOCOL_IP4, proto)))
974 return tp_vfts[proto].get_listener (s->connection_index);
979 * If nothing is found, check if any listener is available
981 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
983 return tp_vfts[proto].get_listener (s->connection_index);
989 * Lookup session with ip4 and transport layer information
991 * Important note: this may look into another thread's pool table and
992 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
993 * if needed as soon as possible.
995 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
996 * this returns a session as opposed to a transport connection and it does not
997 * try to lookup half-open sessions.
999 * Typically used by dgram connections
1002 session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1003 u16 lcl_port, u16 rmt_port, u8 proto)
1005 session_table_t *st;
1007 stream_session_t *s;
1011 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1012 if (PREDICT_FALSE (!st))
1016 * Lookup session amongst established ones
1018 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1019 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1021 return session_get_from_handle_safe (kv4.value);
1024 * Check the session rules table
1026 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1027 rmt, lcl_port, rmt_port);
1028 if (session_lookup_action_index_is_valid (action_index))
1030 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1032 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1037 * If nothing is found, check if any listener is available
1039 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
1046 * Lookup connection with ip6 and transport layer information
1048 * This is used on the fast path so it needs to be fast. Thereby,
1049 * duplication of code and 'hacks' allowed.
1051 * The lookup is incremental and returns whenever something is matched. The
1053 * - Try to find an established session
1054 * - Try to find a half-open connection
1055 * - Try session rules table
1056 * - Try to find a fully-formed or local source wildcarded (listener bound to
1057 * all interfaces) listener session
1060 * @param fib_index index of the fib wherein the connection was received
1061 * @param lcl local ip6 address
1062 * @param rmt remote ip6 address
1063 * @param lcl_port local port
1064 * @param rmt_port remote port
1065 * @param proto transport protocol (e.g., tcp, udp)
1066 * @param thread_index thread index for request
1068 * @return pointer to transport connection, if one is found, 0 otherwise
1070 transport_connection_t *
1071 session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1072 ip6_address_t * rmt, u16 lcl_port,
1073 u16 rmt_port, u8 proto, u32 thread_index,
1076 session_table_t *st;
1077 stream_session_t *s;
1082 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1083 if (PREDICT_FALSE (!st))
1086 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1087 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1090 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1091 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1092 return tp_vfts[proto].get_connection (s->connection_index,
1096 /* Try half-open connections */
1097 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1099 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1101 /* Check the session rules table */
1102 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1103 rmt, lcl_port, rmt_port);
1104 if (session_lookup_action_index_is_valid (action_index))
1106 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
1108 if ((s = session_lookup_action_to_session (action_index,
1109 FIB_PROTOCOL_IP6, proto)))
1110 return tp_vfts[proto].get_listener (s->connection_index);
1114 /* If nothing is found, check if any listener is available */
1115 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1117 return tp_vfts[proto].get_listener (s->connection_index);
1123 * Lookup connection with ip6 and transport layer information
1125 * Not optimized. This is used on the fast path so it needs to be fast.
1126 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1127 * to that of @ref session_lookup_connection_wt4
1129 * @param fib_index index of the fib wherein the connection was received
1130 * @param lcl local ip6 address
1131 * @param rmt remote ip6 address
1132 * @param lcl_port local port
1133 * @param rmt_port remote port
1134 * @param proto transport protocol (e.g., tcp, udp)
1136 * @return pointer to transport connection, if one is found, 0 otherwise
1138 transport_connection_t *
1139 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1140 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1143 session_table_t *st;
1144 stream_session_t *s;
1149 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1150 if (PREDICT_FALSE (!st))
1153 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1154 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1157 s = session_get_from_handle (kv6.value);
1158 return tp_vfts[proto].get_connection (s->connection_index,
1162 /* Try half-open connections */
1163 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1165 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1167 /* Check the session rules table */
1168 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1169 rmt, lcl_port, rmt_port);
1170 if (session_lookup_action_index_is_valid (action_index))
1172 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1174 if ((s = session_lookup_action_to_session (action_index,
1175 FIB_PROTOCOL_IP6, proto)))
1176 return tp_vfts[proto].get_listener (s->connection_index);
1180 /* If nothing is found, check if any listener is available */
1181 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1183 return tp_vfts[proto].get_listener (s->connection_index);
1189 * Lookup session with ip6 and transport layer information
1191 * Important note: this may look into another thread's pool table and
1192 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1193 * if needed as soon as possible.
1195 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1196 * this returns a session as opposed to a transport connection and it does not
1197 * try to lookup half-open sessions.
1199 * Typically used by dgram connections
1202 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1203 u16 lcl_port, u16 rmt_port, u8 proto)
1205 session_table_t *st;
1207 stream_session_t *s;
1211 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1212 if (PREDICT_FALSE (!st))
1215 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1216 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1218 return session_get_from_handle_safe (kv6.value);
1220 /* Check the session rules table */
1221 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1222 rmt, lcl_port, rmt_port);
1223 if (session_lookup_action_index_is_valid (action_index))
1225 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1227 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1231 /* If nothing is found, check if any listener is available */
1232 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1238 session_lookup_local_listener_make_handle (session_endpoint_t * sep)
1240 return ((u64) SESSION_LOCAL_TABLE_PREFIX << 32
1241 | (u32) sep->port << 16 | (u32) sep->transport_proto << 8
1242 | (u32) sep->is_ip4);
1246 session_lookup_local_is_handle (u64 handle)
1248 if (handle >> 32 == SESSION_LOCAL_TABLE_PREFIX)
1254 session_lookup_local_listener_parse_handle (u64 handle,
1255 session_endpoint_t * sep)
1257 u32 local_table_handle;
1258 if (handle >> 32 != SESSION_LOCAL_TABLE_PREFIX)
1260 local_table_handle = handle & 0xFFFFFFFFULL;
1261 sep->is_ip4 = local_table_handle & 0xff;
1262 local_table_handle >>= 8;
1263 sep->transport_proto = local_table_handle & 0xff;
1264 sep->port = local_table_handle >> 8;
1269 vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1271 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1272 session_rules_table_t *srt;
1273 session_table_t *st;
1276 clib_error_t *error;
1279 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1281 if (args->scope > 3)
1282 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1284 if (args->transport_proto != TRANSPORT_PROTO_TCP
1285 && args->transport_proto != TRANSPORT_PROTO_UDP)
1286 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1287 "invalid transport proto");
1288 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1290 fib_proto = args->table_args.rmt.fp_proto;
1291 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1292 st = session_table_get_for_fib_index (fib_proto, fib_index);
1293 srt = &st->session_rules[args->transport_proto];
1294 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1296 clib_error_report (error);
1300 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1302 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1303 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1304 args->table_args.lcl_port = 0;
1305 st = app_namespace_get_local_table (app_ns);
1306 srt = &st->session_rules[args->transport_proto];
1307 error = session_rules_table_add_del (srt, &args->table_args);
1313 * Mark (global) tables as pertaining to app ns
1316 session_lookup_set_tables_appns (app_namespace_t * app_ns)
1318 session_table_t *st;
1322 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1324 fib_index = app_namespace_get_fib_index (app_ns, fp);
1325 st = session_table_get_for_fib_index (fp, fib_index);
1327 st->appns_index = app_namespace_index (app_ns);
1332 format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1334 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1335 u32 is_local = va_arg (*args, u32);
1336 u8 *app_name, *str = 0;
1337 stream_session_t *session;
1338 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1342 session = session_get_from_handle (kvp->value);
1343 app_name = application_name_from_index (session->app_index);
1344 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1345 key->proto, format_ip4_address, &key->src,
1346 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1347 &key->dst, clib_net_to_host_u16 (key->dst_port));
1348 s = format (s, "%-40v%-30v", str, app_name);
1352 app_name = application_name_from_index (kvp->value);
1353 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1354 format_ip4_address, &key->src,
1355 clib_net_to_host_u16 (key->src_port));
1356 s = format (s, "%-30v%-30v", str, app_name);
1358 vec_free (app_name);
1362 typedef struct _ip4_session_table_show_ctx_t
1366 } ip4_session_table_show_ctx_t;
1369 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1371 ip4_session_table_show_ctx_t *ctx = arg;
1372 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1378 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1379 u8 type, u8 is_local)
1381 ip4_session_table_show_ctx_t ctx = {
1383 .is_local = is_local,
1386 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1388 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1393 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1397 clib_warning ("not supported");
1401 static clib_error_t *
1402 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1403 vlib_cli_command_t * cmd)
1405 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1406 u32 appns_index, scope = 0;
1407 ip46_address_t lcl_ip, rmt_ip;
1408 u8 is_ip4 = 1, conn_set = 0;
1409 u8 fib_proto, is_add = 1, *ns_id = 0;
1411 app_namespace_t *app_ns;
1412 clib_error_t *error;
1414 memset (&lcl_ip, 0, sizeof (lcl_ip));
1415 memset (&rmt_ip, 0, sizeof (rmt_ip));
1416 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1418 if (unformat (input, "del"))
1420 else if (unformat (input, "add"))
1422 else if (unformat (input, "appns %_%v%_", &ns_id))
1424 else if (unformat (input, "scope global"))
1425 scope = SESSION_RULE_SCOPE_GLOBAL;
1426 else if (unformat (input, "scope local"))
1427 scope = SESSION_RULE_SCOPE_LOCAL;
1428 else if (unformat (input, "scope all"))
1429 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1430 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1432 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1433 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1434 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1440 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1441 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1442 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1448 else if (unformat (input, "action %d", &action))
1450 else if (unformat (input, "tag %_%v%_", &tag))
1453 return clib_error_return (0, "unknown input `%U'",
1454 format_unformat_error, input);
1459 vlib_cli_output (vm, "proto must be set");
1462 if (is_add && !conn_set && action == ~0)
1464 vlib_cli_output (vm, "connection and action must be set for add");
1467 if (!is_add && !tag && !conn_set)
1469 vlib_cli_output (vm, "connection or tag must be set for delete");
1472 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1474 vlib_cli_output (vm, "tag too long (max u64)");
1480 app_ns = app_namespace_get_from_id (ns_id);
1483 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1489 app_ns = app_namespace_get_default ();
1491 appns_index = app_namespace_index (app_ns);
1493 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1494 session_rule_add_del_args_t args = {
1495 .table_args.lcl.fp_addr = lcl_ip,
1496 .table_args.lcl.fp_len = lcl_plen,
1497 .table_args.lcl.fp_proto = fib_proto,
1498 .table_args.rmt.fp_addr = rmt_ip,
1499 .table_args.rmt.fp_len = rmt_plen,
1500 .table_args.rmt.fp_proto = fib_proto,
1501 .table_args.lcl_port = lcl_port,
1502 .table_args.rmt_port = rmt_port,
1503 .table_args.action_index = action,
1504 .table_args.is_add = is_add,
1505 .table_args.tag = tag,
1506 .appns_index = appns_index,
1509 error = vnet_session_rule_add_del (&args);
1515 VLIB_CLI_COMMAND (session_rule_command, static) =
1517 .path = "session rule",
1518 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1519 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1520 .function = session_rule_command_fn,
1525 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1528 vlib_main_t *vm = vlib_get_main ();
1529 session_rules_table_t *srt;
1530 session_table_t *st;
1531 st = session_table_get_for_fib_index (fib_index, fib_proto);
1532 srt = &st->session_rules[transport_proto];
1533 session_rules_table_cli_dump (vm, srt, fib_proto);
1537 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1540 vlib_main_t *vm = vlib_get_main ();
1541 session_rules_table_t *srt;
1542 session_table_t *st;
1543 st = session_table_get (table_index);
1544 srt = &st->session_rules[transport_proto];
1545 session_rules_table_cli_dump (vm, srt, fib_proto);
1548 static clib_error_t *
1549 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1550 vlib_cli_command_t * cmd)
1552 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1553 u32 fib_index, scope = 0;
1554 ip46_address_t lcl_ip, rmt_ip;
1555 u8 is_ip4 = 1, show_one = 0;
1556 app_namespace_t *app_ns;
1557 session_rules_table_t *srt;
1558 session_table_t *st;
1559 u8 *ns_id = 0, fib_proto;
1561 memset (&lcl_ip, 0, sizeof (lcl_ip));
1562 memset (&rmt_ip, 0, sizeof (rmt_ip));
1563 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1565 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1567 else if (unformat (input, "appns %_%v%_", &ns_id))
1569 else if (unformat (input, "scope global"))
1571 else if (unformat (input, "scope local"))
1573 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1574 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1575 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1581 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1582 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1583 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1590 return clib_error_return (0, "unknown input `%U'",
1591 format_unformat_error, input);
1594 if (transport_proto == ~0)
1596 vlib_cli_output (vm, "transport proto must be set");
1602 app_ns = app_namespace_get_from_id (ns_id);
1605 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1611 app_ns = app_namespace_get_default ();
1614 if (scope == 1 || scope == 0)
1616 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1617 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1618 st = session_table_get_for_fib_index (fib_proto, fib_index);
1622 st = app_namespace_get_local_table (app_ns);
1627 srt = &st->session_rules[transport_proto];
1628 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1633 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1635 srt = &st->session_rules[transport_proto];
1636 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1637 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
1644 VLIB_CLI_COMMAND (show_session_rules_command, static) =
1646 .path = "show session rules",
1647 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1648 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
1649 .function = show_session_rules_command_fn,
1654 session_lookup_init (void)
1657 * Allocate default table and map it to fib_index 0
1659 session_table_t *st = session_table_alloc ();
1660 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1661 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
1662 st->active_fib_proto = FIB_PROTOCOL_IP4;
1663 session_table_init (st, FIB_PROTOCOL_IP4);
1664 st = session_table_alloc ();
1665 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1666 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
1667 st->active_fib_proto = FIB_PROTOCOL_IP6;
1668 session_table_init (st, FIB_PROTOCOL_IP6);
1672 * fd.io coding-style-patch-verification: ON
1675 * eval: (c-set-style "gnu")