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_handle (u32 action_index)
394 switch (action_index)
396 case SESSION_RULES_TABLE_ACTION_DROP:
397 return SESSION_DROP_HANDLE;
398 case SESSION_RULES_TABLE_ACTION_ALLOW:
399 case SESSION_RULES_TABLE_INVALID_INDEX:
400 return SESSION_INVALID_HANDLE;
402 /* application index */
407 static stream_session_t *
408 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
412 app = application_get_if_valid (app_index);
416 return application_first_listener (app, fib_proto, transport_proto);
419 static stream_session_t *
420 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
424 app_index = session_lookup_action_to_handle (action_index);
425 /* Nothing sophisticated for now, action index is app index */
426 return session_lookup_app_listen_session (app_index, fib_proto,
432 session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
433 ip4_address_t * lcl, u16 lcl_port,
434 ip4_address_t * rmt, u16 rmt_port)
436 session_rules_table_t *srt = &st->session_rules[proto];
437 u32 action_index, app_index;
438 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
440 app_index = session_lookup_action_to_handle (action_index);
441 /* Nothing sophisticated for now, action index is app index */
442 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
448 session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
449 ip6_address_t * lcl, u16 lcl_port,
450 ip6_address_t * rmt, u16 rmt_port)
452 session_rules_table_t *srt = &st->session_rules[proto];
453 u32 action_index, app_index;
454 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
456 app_index = session_lookup_action_to_handle (action_index);
457 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
462 * Lookup listener for session endpoint in table
464 * @param table_index table where the endpoint should be looked up
465 * @param sep session endpoint to be looked up
466 * @param use_rules flag that indicates if the session rules of the table
468 * @return invalid handle if nothing is found, the handle of a valid listener
469 * or an action derived handle if a rule is hit
472 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
475 session_rules_table_t *srt;
480 st = session_table_get (table_index);
482 return SESSION_INVALID_HANDLE;
488 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
489 sep->transport_proto);
490 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
495 memset (&lcl4, 0, sizeof (lcl4));
496 srt = &st->session_rules[sep->transport_proto];
497 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
499 if (session_lookup_action_index_is_valid (ai))
500 return session_lookup_action_to_handle (ai);
508 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
509 sep->transport_proto);
510 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
516 memset (&lcl6, 0, sizeof (lcl6));
517 srt = &st->session_rules[sep->transport_proto];
518 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
520 if (session_lookup_action_index_is_valid (ai))
521 return session_lookup_action_to_handle (ai);
524 return SESSION_INVALID_HANDLE;
528 * Look up endpoint in local session table
530 * The result, for now, is an application index and it may in the future
531 * be extended to a more complicated "action object". The only action we
532 * emulate now is "drop" and for that we return a special app index.
534 * Lookup logic is to check in order:
535 * - the rules in the table (connect acls)
536 * - session sub-table for a listener
537 * - session sub-table for a local listener (zeroed addr)
539 * @param table_index table where the lookup should be done
540 * @param sep session endpoint to be looked up
541 * @return session handle that can be interpreted as an adjacency
544 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
546 session_rules_table_t *srt;
551 st = session_table_get (table_index);
553 return SESSION_INVALID_INDEX;
554 ASSERT (st->is_local);
562 * Check if endpoint has special rules associated
564 memset (&lcl4, 0, sizeof (lcl4));
565 srt = &st->session_rules[sep->transport_proto];
566 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
568 if (session_lookup_action_index_is_valid (ai))
569 return session_lookup_action_to_handle (ai);
572 * Check if session endpoint is a listener
574 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
575 sep->transport_proto);
576 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
581 * Zero out the ip. Logic is that connect to local ips, say
582 * 127.0.0.1:port, can match 0.0.0.0:port
585 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
590 * Zero out the port and check if we have proxy
593 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
602 memset (&lcl6, 0, sizeof (lcl6));
603 srt = &st->session_rules[sep->transport_proto];
604 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
606 if (session_lookup_action_index_is_valid (ai))
607 return session_lookup_action_to_handle (ai);
609 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
610 sep->transport_proto);
611 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
616 * Zero out the ip. Same logic as above.
618 kv6.key[0] = kv6.key[1] = 0;
619 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
624 * Zero out the port. Same logic as above.
626 kv6.key[4] = kv6.key[5] = 0;
627 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
631 return SESSION_INVALID_HANDLE;
634 static stream_session_t *
635 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
636 u16 lcl_port, u8 proto)
640 session_type_t session_type;
643 * First, try a fully formed listener
645 session_type = session_type_from_proto_and_ip (proto, 1);
646 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
647 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
649 return session_manager_get_listener (session_type, (u32) kv4.value);
652 * Zero out the lcl ip and check if any 0/0 port binds have been done
655 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
657 return session_manager_get_listener (session_type, (u32) kv4.value);
660 * Zero out port and check if we have a proxy set up for our ip
662 make_v4_proxy_kv (&kv4, lcl, proto);
663 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
665 return session_manager_get_listener (session_type, (u32) kv4.value);
671 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
675 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
678 return session_lookup_listener4_i (st, lcl, lcl_port, proto);
681 static stream_session_t *
682 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
683 u16 lcl_port, u8 proto)
687 session_type_t session_type;
689 session_type = session_type_from_proto_and_ip (proto, 0);
690 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
691 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
693 return session_manager_get_listener (session_type, (u32) kv6.value);
695 /* Zero out the lcl ip */
696 kv6.key[0] = kv6.key[1] = 0;
697 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
699 return session_manager_get_listener (session_type, (u32) kv6.value);
701 make_v6_proxy_kv (&kv6, lcl, proto);
702 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
704 return session_manager_get_listener (session_type, (u32) kv6.value);
709 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
713 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
716 return session_lookup_listener6_i (st, lcl, lcl_port, proto);
720 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
723 st = session_table_get (table_index);
727 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
728 sep->transport_proto);
730 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
731 sep->transport_proto);
736 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
742 st = session_table_get_or_alloc_for_connection (tc);
747 make_v4_ss_kv_from_tc (&kv4, tc);
749 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
754 make_v6_ss_kv_from_tc (&kv6, tc);
756 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
762 session_lookup_del_half_open (transport_connection_t * tc)
768 st = session_table_get_for_connection (tc);
773 make_v4_ss_kv_from_tc (&kv4, tc);
774 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
779 make_v6_ss_kv_from_tc (&kv6, tc);
780 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
786 session_lookup_half_open_handle (transport_connection_t * tc)
793 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
796 return HALF_OPEN_LOOKUP_INVALID_VALUE;
799 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
800 tc->rmt_port, tc->proto);
801 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
807 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
808 tc->rmt_port, tc->proto);
809 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
813 return HALF_OPEN_LOOKUP_INVALID_VALUE;
816 transport_connection_t *
817 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
821 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
823 sst = session_type_from_proto_and_ip (proto, is_ip4);
824 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
830 * Lookup connection with ip4 and transport layer information
832 * This is used on the fast path so it needs to be fast. Thereby,
833 * duplication of code and 'hacks' allowed.
835 * The lookup is incremental and returns whenever something is matched. The
837 * - Try to find an established session
838 * - Try to find a half-open connection
839 * - Try session rules table
840 * - Try to find a fully-formed or local source wildcarded (listener bound to
841 * all interfaces) listener session
844 * @param fib_index index of fib wherein the connection was received
845 * @param lcl local ip4 address
846 * @param rmt remote ip4 address
847 * @param lcl_port local port
848 * @param rmt_port remote port
849 * @param proto transport protocol (e.g., tcp, udp)
850 * @param thread_index thread index for request
851 * @param is_filtered return flag that indicates if connection was filtered.
853 * @return pointer to transport connection, if one is found, 0 otherwise
855 transport_connection_t *
856 session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
857 ip4_address_t * rmt, u16 lcl_port,
858 u16 rmt_port, u8 proto, u32 thread_index,
867 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
868 if (PREDICT_FALSE (!st))
872 * Lookup session amongst established ones
874 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
875 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
878 ASSERT ((u32) (kv4.value >> 32) == thread_index);
879 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
880 return tp_vfts[proto].get_connection (s->connection_index,
885 * Try half-open connections
887 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
889 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
892 * Check the session rules table
894 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
895 rmt, lcl_port, rmt_port);
896 if (session_lookup_action_index_is_valid (action_index))
898 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
900 if ((s = session_lookup_action_to_session (action_index,
901 FIB_PROTOCOL_IP4, proto)))
902 return tp_vfts[proto].get_listener (s->connection_index);
907 * If nothing is found, check if any listener is available
909 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
911 return tp_vfts[proto].get_listener (s->connection_index);
917 * Lookup connection with ip4 and transport layer information
919 * Not optimized. This is used on the fast path so it needs to be fast.
920 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
921 * to that of @ref session_lookup_connection_wt4
923 * @param fib_index index of the fib wherein the connection was received
924 * @param lcl local ip4 address
925 * @param rmt remote ip4 address
926 * @param lcl_port local port
927 * @param rmt_port remote port
928 * @param proto transport protocol (e.g., tcp, udp)
930 * @return pointer to transport connection, if one is found, 0 otherwise
932 transport_connection_t *
933 session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
934 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
943 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
944 if (PREDICT_FALSE (!st))
948 * Lookup session amongst established ones
950 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
951 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
954 s = session_get_from_handle (kv4.value);
955 return tp_vfts[proto].get_connection (s->connection_index,
960 * Try half-open connections
962 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
964 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
967 * Check the session rules table
969 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
970 rmt, lcl_port, rmt_port);
971 if (session_lookup_action_index_is_valid (action_index))
973 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
975 if ((s = session_lookup_action_to_session (action_index,
976 FIB_PROTOCOL_IP4, proto)))
977 return tp_vfts[proto].get_listener (s->connection_index);
982 * If nothing is found, check if any listener is available
984 s = session_lookup_listener4_i (st, lcl, lcl_port, proto);
986 return tp_vfts[proto].get_listener (s->connection_index);
992 * Lookup session with ip4 and transport layer information
994 * Important note: this may look into another thread's pool table and
995 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
996 * if needed as soon as possible.
998 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
999 * this returns a session as opposed to a transport connection and it does not
1000 * try to lookup half-open sessions.
1002 * Typically used by dgram connections
1005 session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1006 u16 lcl_port, u16 rmt_port, u8 proto)
1008 session_table_t *st;
1010 stream_session_t *s;
1014 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1015 if (PREDICT_FALSE (!st))
1019 * Lookup session amongst established ones
1021 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1022 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1024 return session_get_from_handle_safe (kv4.value);
1027 * Check the session rules table
1029 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1030 rmt, lcl_port, rmt_port);
1031 if (session_lookup_action_index_is_valid (action_index))
1033 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1035 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1040 * If nothing is found, check if any listener is available
1042 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto)))
1049 * Lookup connection with ip6 and transport layer information
1051 * This is used on the fast path so it needs to be fast. Thereby,
1052 * duplication of code and 'hacks' allowed.
1054 * The lookup is incremental and returns whenever something is matched. The
1056 * - Try to find an established session
1057 * - Try to find a half-open connection
1058 * - Try session rules table
1059 * - Try to find a fully-formed or local source wildcarded (listener bound to
1060 * all interfaces) listener session
1063 * @param fib_index index of the fib wherein the connection was received
1064 * @param lcl local ip6 address
1065 * @param rmt remote ip6 address
1066 * @param lcl_port local port
1067 * @param rmt_port remote port
1068 * @param proto transport protocol (e.g., tcp, udp)
1069 * @param thread_index thread index for request
1071 * @return pointer to transport connection, if one is found, 0 otherwise
1073 transport_connection_t *
1074 session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1075 ip6_address_t * rmt, u16 lcl_port,
1076 u16 rmt_port, u8 proto, u32 thread_index,
1079 session_table_t *st;
1080 stream_session_t *s;
1085 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1086 if (PREDICT_FALSE (!st))
1089 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1090 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1093 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1094 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1095 return tp_vfts[proto].get_connection (s->connection_index,
1099 /* Try half-open connections */
1100 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1102 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1104 /* Check the session rules table */
1105 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1106 rmt, lcl_port, rmt_port);
1107 if (session_lookup_action_index_is_valid (action_index))
1109 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
1111 if ((s = session_lookup_action_to_session (action_index,
1112 FIB_PROTOCOL_IP6, proto)))
1113 return tp_vfts[proto].get_listener (s->connection_index);
1117 /* If nothing is found, check if any listener is available */
1118 s = session_lookup_listener6_i (st, lcl, lcl_port, proto);
1120 return tp_vfts[proto].get_listener (s->connection_index);
1126 * Lookup connection with ip6 and transport layer information
1128 * Not optimized. This is used on the fast path so it needs to be fast.
1129 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1130 * to that of @ref session_lookup_connection_wt4
1132 * @param fib_index index of the fib wherein the connection was received
1133 * @param lcl local ip6 address
1134 * @param rmt remote ip6 address
1135 * @param lcl_port local port
1136 * @param rmt_port remote port
1137 * @param proto transport protocol (e.g., tcp, udp)
1139 * @return pointer to transport connection, if one is found, 0 otherwise
1141 transport_connection_t *
1142 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1143 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1146 session_table_t *st;
1147 stream_session_t *s;
1152 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1153 if (PREDICT_FALSE (!st))
1156 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1157 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1160 s = session_get_from_handle (kv6.value);
1161 return tp_vfts[proto].get_connection (s->connection_index,
1165 /* Try half-open connections */
1166 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1168 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1170 /* Check the session rules table */
1171 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1172 rmt, lcl_port, rmt_port);
1173 if (session_lookup_action_index_is_valid (action_index))
1175 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1177 if ((s = session_lookup_action_to_session (action_index,
1178 FIB_PROTOCOL_IP6, proto)))
1179 return tp_vfts[proto].get_listener (s->connection_index);
1183 /* If nothing is found, check if any listener is available */
1184 s = session_lookup_listener6 (fib_index, lcl, lcl_port, proto);
1186 return tp_vfts[proto].get_listener (s->connection_index);
1192 * Lookup session with ip6 and transport layer information
1194 * Important note: this may look into another thread's pool table and
1195 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1196 * if needed as soon as possible.
1198 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1199 * this returns a session as opposed to a transport connection and it does not
1200 * try to lookup half-open sessions.
1202 * Typically used by dgram connections
1205 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1206 u16 lcl_port, u16 rmt_port, u8 proto)
1208 session_table_t *st;
1210 stream_session_t *s;
1214 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1215 if (PREDICT_FALSE (!st))
1218 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1219 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1221 return session_get_from_handle_safe (kv6.value);
1223 /* Check the session rules table */
1224 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1225 rmt, lcl_port, rmt_port);
1226 if (session_lookup_action_index_is_valid (action_index))
1228 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1230 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1234 /* If nothing is found, check if any listener is available */
1235 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto)))
1241 vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1243 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1244 session_rules_table_t *srt;
1245 session_table_t *st;
1248 clib_error_t *error;
1251 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1253 if (args->scope > 3)
1254 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1256 if (args->transport_proto != TRANSPORT_PROTO_TCP
1257 && args->transport_proto != TRANSPORT_PROTO_UDP)
1258 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1259 "invalid transport proto");
1260 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1262 fib_proto = args->table_args.rmt.fp_proto;
1263 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1264 st = session_table_get_for_fib_index (fib_proto, fib_index);
1265 srt = &st->session_rules[args->transport_proto];
1266 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1268 clib_error_report (error);
1272 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1274 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1275 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1276 args->table_args.lcl_port = 0;
1277 st = app_namespace_get_local_table (app_ns);
1278 srt = &st->session_rules[args->transport_proto];
1279 error = session_rules_table_add_del (srt, &args->table_args);
1285 * Mark (global) tables as pertaining to app ns
1288 session_lookup_set_tables_appns (app_namespace_t * app_ns)
1290 session_table_t *st;
1294 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1296 fib_index = app_namespace_get_fib_index (app_ns, fp);
1297 st = session_table_get_for_fib_index (fp, fib_index);
1299 st->appns_index = app_namespace_index (app_ns);
1304 format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1306 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1307 u32 is_local = va_arg (*args, u32);
1308 u8 *app_name, *str = 0;
1309 stream_session_t *session;
1310 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1314 session = session_get_from_handle (kvp->value);
1315 app_name = application_name_from_index (session->app_index);
1316 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1317 key->proto, format_ip4_address, &key->src,
1318 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1319 &key->dst, clib_net_to_host_u16 (key->dst_port));
1320 s = format (s, "%-40v%-30v", str, app_name);
1324 app_name = application_name_from_index (kvp->value);
1325 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1326 format_ip4_address, &key->src,
1327 clib_net_to_host_u16 (key->src_port));
1328 s = format (s, "%-30v%-30v", str, app_name);
1330 vec_free (app_name);
1334 typedef struct _ip4_session_table_show_ctx_t
1338 } ip4_session_table_show_ctx_t;
1341 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1343 ip4_session_table_show_ctx_t *ctx = arg;
1344 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1350 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1351 u8 type, u8 is_local)
1353 ip4_session_table_show_ctx_t ctx = {
1355 .is_local = is_local,
1358 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1360 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1365 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1369 clib_warning ("not supported");
1373 static clib_error_t *
1374 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1375 vlib_cli_command_t * cmd)
1377 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1378 u32 appns_index, scope = 0;
1379 ip46_address_t lcl_ip, rmt_ip;
1380 u8 is_ip4 = 1, conn_set = 0;
1381 u8 fib_proto, is_add = 1, *ns_id = 0;
1383 app_namespace_t *app_ns;
1384 clib_error_t *error;
1386 memset (&lcl_ip, 0, sizeof (lcl_ip));
1387 memset (&rmt_ip, 0, sizeof (rmt_ip));
1388 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1390 if (unformat (input, "del"))
1392 else if (unformat (input, "add"))
1394 else if (unformat (input, "appns %_%v%_", &ns_id))
1396 else if (unformat (input, "scope global"))
1397 scope = SESSION_RULE_SCOPE_GLOBAL;
1398 else if (unformat (input, "scope local"))
1399 scope = SESSION_RULE_SCOPE_LOCAL;
1400 else if (unformat (input, "scope all"))
1401 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1402 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1404 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1405 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1406 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1412 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1413 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1414 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1420 else if (unformat (input, "action %d", &action))
1422 else if (unformat (input, "tag %_%v%_", &tag))
1425 return clib_error_return (0, "unknown input `%U'",
1426 format_unformat_error, input);
1431 vlib_cli_output (vm, "proto must be set");
1434 if (is_add && !conn_set && action == ~0)
1436 vlib_cli_output (vm, "connection and action must be set for add");
1439 if (!is_add && !tag && !conn_set)
1441 vlib_cli_output (vm, "connection or tag must be set for delete");
1444 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1446 vlib_cli_output (vm, "tag too long (max u64)");
1452 app_ns = app_namespace_get_from_id (ns_id);
1455 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1461 app_ns = app_namespace_get_default ();
1463 appns_index = app_namespace_index (app_ns);
1465 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1466 session_rule_add_del_args_t args = {
1467 .table_args.lcl.fp_addr = lcl_ip,
1468 .table_args.lcl.fp_len = lcl_plen,
1469 .table_args.lcl.fp_proto = fib_proto,
1470 .table_args.rmt.fp_addr = rmt_ip,
1471 .table_args.rmt.fp_len = rmt_plen,
1472 .table_args.rmt.fp_proto = fib_proto,
1473 .table_args.lcl_port = lcl_port,
1474 .table_args.rmt_port = rmt_port,
1475 .table_args.action_index = action,
1476 .table_args.is_add = is_add,
1477 .table_args.tag = tag,
1478 .appns_index = appns_index,
1481 error = vnet_session_rule_add_del (&args);
1487 VLIB_CLI_COMMAND (session_rule_command, static) =
1489 .path = "session rule",
1490 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1491 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1492 .function = session_rule_command_fn,
1497 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1500 vlib_main_t *vm = vlib_get_main ();
1501 session_rules_table_t *srt;
1502 session_table_t *st;
1503 st = session_table_get_for_fib_index (fib_index, fib_proto);
1504 srt = &st->session_rules[transport_proto];
1505 session_rules_table_cli_dump (vm, srt, fib_proto);
1509 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1512 vlib_main_t *vm = vlib_get_main ();
1513 session_rules_table_t *srt;
1514 session_table_t *st;
1515 st = session_table_get (table_index);
1516 srt = &st->session_rules[transport_proto];
1517 session_rules_table_cli_dump (vm, srt, fib_proto);
1520 static clib_error_t *
1521 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1522 vlib_cli_command_t * cmd)
1524 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1525 u32 fib_index, scope = 0;
1526 ip46_address_t lcl_ip, rmt_ip;
1527 u8 is_ip4 = 1, show_one = 0;
1528 app_namespace_t *app_ns;
1529 session_rules_table_t *srt;
1530 session_table_t *st;
1531 u8 *ns_id = 0, fib_proto;
1533 memset (&lcl_ip, 0, sizeof (lcl_ip));
1534 memset (&rmt_ip, 0, sizeof (rmt_ip));
1535 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1537 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1539 else if (unformat (input, "appns %_%v%_", &ns_id))
1541 else if (unformat (input, "scope global"))
1543 else if (unformat (input, "scope local"))
1545 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1546 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1547 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1553 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1554 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1555 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1562 return clib_error_return (0, "unknown input `%U'",
1563 format_unformat_error, input);
1566 if (transport_proto == ~0)
1568 vlib_cli_output (vm, "transport proto must be set");
1574 app_ns = app_namespace_get_from_id (ns_id);
1577 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1583 app_ns = app_namespace_get_default ();
1586 if (scope == 1 || scope == 0)
1588 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1589 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1590 st = session_table_get_for_fib_index (fib_proto, fib_index);
1594 st = app_namespace_get_local_table (app_ns);
1599 srt = &st->session_rules[transport_proto];
1600 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1605 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1607 srt = &st->session_rules[transport_proto];
1608 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1609 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
1616 VLIB_CLI_COMMAND (show_session_rules_command, static) =
1618 .path = "show session rules",
1619 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1620 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
1621 .function = show_session_rules_command_fn,
1626 session_lookup_init (void)
1629 * Allocate default table and map it to fib_index 0
1631 session_table_t *st = session_table_alloc ();
1632 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1633 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
1634 st->active_fib_proto = FIB_PROTOCOL_IP4;
1635 session_table_init (st, FIB_PROTOCOL_IP4);
1636 st = session_table_alloc ();
1637 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1638 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
1639 st->active_fib_proto = FIB_PROTOCOL_IP6;
1640 session_table_init (st, FIB_PROTOCOL_IP6);
1644 * fd.io coding-style-patch-verification: ON
1647 * eval: (c-set-style "gnu")