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 kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
90 kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
95 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
98 kv->key[0] = (u64) lcl->as_u32;
99 kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
104 make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
106 kv->key[0] = (u64) lcl->as_u32;
107 kv->key[1] = (u64) proto << 32;
112 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
114 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
115 tc->rmt_port, tc->proto);
119 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
120 u16 lcl_port, u16 rmt_port, u8 proto)
122 kv->key[0] = lcl->as_u64[0];
123 kv->key[1] = lcl->as_u64[1];
124 kv->key[2] = rmt->as_u64[0];
125 kv->key[3] = rmt->as_u64[1];
126 kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
132 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
135 kv->key[0] = lcl->as_u64[0];
136 kv->key[1] = lcl->as_u64[1];
139 kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
145 make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
147 kv->key[0] = lcl->as_u64[0];
148 kv->key[1] = lcl->as_u64[1];
151 kv->key[4] = (u64) proto << 32;
157 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
159 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
160 tc->rmt_port, tc->proto);
163 static session_table_t *
164 session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
168 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
170 st = session_table_alloc ();
171 table_index = session_table_index (st);
172 vec_validate (fib_index_to_table_index[fib_proto], fib_index);
173 fib_index_to_table_index[fib_proto][fib_index] = table_index;
174 st->active_fib_proto = fib_proto;
175 session_table_init (st, fib_proto);
180 table_index = fib_index_to_table_index[fib_proto][fib_index];
181 return session_table_get (table_index);
185 static session_table_t *
186 session_table_get_or_alloc_for_connection (transport_connection_t * tc)
189 fib_proto = transport_connection_fib_proto (tc);
190 return session_table_get_or_alloc (fib_proto, tc->fib_index);
193 static session_table_t *
194 session_table_get_for_connection (transport_connection_t * tc)
196 u32 fib_proto = transport_connection_fib_proto (tc);
197 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
200 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
203 static session_table_t *
204 session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
206 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
208 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
212 session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
214 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
215 return SESSION_TABLE_INVALID_INDEX;
216 return fib_index_to_table_index[fib_proto][fib_index];
220 * Add transport connection to a session table
222 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
223 * is added to requested session table.
225 * @param tc transport connection to be added
226 * @param value value to be stored
228 * @return non-zero if failure
231 session_lookup_add_connection (transport_connection_t * tc, u64 value)
237 st = session_table_get_or_alloc_for_connection (tc);
242 make_v4_ss_kv_from_tc (&kv4, tc);
244 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
249 make_v6_ss_kv_from_tc (&kv6, tc);
251 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
257 session_lookup_add_session_endpoint (u32 table_index,
258 session_endpoint_t * sep, u64 value)
264 st = session_table_get (table_index);
269 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
270 sep->transport_proto);
272 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
276 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
277 sep->transport_proto);
279 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
284 session_lookup_del_session_endpoint (u32 table_index,
285 session_endpoint_t * sep)
291 st = session_table_get (table_index);
296 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
297 sep->transport_proto);
298 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
302 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
303 sep->transport_proto);
304 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
309 * Delete transport connection from session table
311 * @param table_index session table index
312 * @param tc transport connection to be removed
314 * @return non-zero if failure
317 session_lookup_del_connection (transport_connection_t * tc)
323 st = session_table_get_for_connection (tc);
328 make_v4_ss_kv_from_tc (&kv4, tc);
329 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
334 make_v6_ss_kv_from_tc (&kv6, tc);
335 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
341 session_lookup_del_session (stream_session_t * s)
343 transport_proto_t tp = session_get_transport_proto (s);
344 transport_connection_t *ts;
345 ts = tp_vfts[tp].get_connection (s->connection_index, s->thread_index);
346 return session_lookup_del_connection (ts);
350 session_lookup_action_index_is_valid (u32 action_index)
352 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
353 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
359 session_lookup_action_to_handle (u32 action_index)
361 switch (action_index)
363 case SESSION_RULES_TABLE_ACTION_DROP:
364 return SESSION_DROP_HANDLE;
365 case SESSION_RULES_TABLE_ACTION_ALLOW:
366 case SESSION_RULES_TABLE_INVALID_INDEX:
367 return SESSION_INVALID_HANDLE;
369 /* application index */
374 static stream_session_t *
375 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
379 app = application_get_if_valid (app_index);
383 return application_first_listener (app, fib_proto, transport_proto);
386 static stream_session_t *
387 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
391 app_index = session_lookup_action_to_handle (action_index);
392 /* Nothing sophisticated for now, action index is app index */
393 return session_lookup_app_listen_session (app_index, fib_proto,
399 session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
400 ip4_address_t * lcl, u16 lcl_port,
401 ip4_address_t * rmt, u16 rmt_port)
403 session_rules_table_t *srt = &st->session_rules[proto];
404 u32 action_index, app_index;
405 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
407 app_index = session_lookup_action_to_handle (action_index);
408 /* Nothing sophisticated for now, action index is app index */
409 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
415 session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
416 ip6_address_t * lcl, u16 lcl_port,
417 ip6_address_t * rmt, u16 rmt_port)
419 session_rules_table_t *srt = &st->session_rules[proto];
420 u32 action_index, app_index;
421 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
423 app_index = session_lookup_action_to_handle (action_index);
424 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
429 * Lookup listener for session endpoint in table
431 * @param table_index table where the endpoint should be looked up
432 * @param sep session endpoint to be looked up
433 * @param use_rules flag that indicates if the session rules of the table
435 * @return invalid handle if nothing is found, the handle of a valid listener
436 * or an action derived handle if a rule is hit
439 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
442 session_rules_table_t *srt;
447 st = session_table_get (table_index);
449 return SESSION_INVALID_HANDLE;
455 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
456 sep->transport_proto);
457 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
462 memset (&lcl4, 0, sizeof (lcl4));
463 srt = &st->session_rules[sep->transport_proto];
464 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
466 if (session_lookup_action_index_is_valid (ai))
467 return session_lookup_action_to_handle (ai);
475 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
476 sep->transport_proto);
477 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
483 memset (&lcl6, 0, sizeof (lcl6));
484 srt = &st->session_rules[sep->transport_proto];
485 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
487 if (session_lookup_action_index_is_valid (ai))
488 return session_lookup_action_to_handle (ai);
491 return SESSION_INVALID_HANDLE;
495 * Look up endpoint in local session table
497 * The result, for now, is an application index and it may in the future
498 * be extended to a more complicated "action object". The only action we
499 * emulate now is "drop" and for that we return a special app index.
501 * Lookup logic is to check in order:
502 * - the rules in the table (connect acls)
503 * - session sub-table for a listener
504 * - session sub-table for a local listener (zeroed addr)
506 * @param table_index table where the lookup should be done
507 * @param sep session endpoint to be looked up
508 * @return session handle that can be interpreted as an adjacency
511 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
513 session_rules_table_t *srt;
518 st = session_table_get (table_index);
520 return SESSION_INVALID_INDEX;
521 ASSERT (st->is_local);
529 * Check if endpoint has special rules associated
531 memset (&lcl4, 0, sizeof (lcl4));
532 srt = &st->session_rules[sep->transport_proto];
533 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
535 if (session_lookup_action_index_is_valid (ai))
536 return session_lookup_action_to_handle (ai);
539 * Check if session endpoint is a listener
541 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
542 sep->transport_proto);
543 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
548 * Zero out the ip. Logic is that connect to local ips, say
549 * 127.0.0.1:port, can match 0.0.0.0:port
551 if (ip4_is_local_host (&sep->ip.ip4))
554 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
564 * Zero out the port and check if we have proxy
567 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
576 memset (&lcl6, 0, sizeof (lcl6));
577 srt = &st->session_rules[sep->transport_proto];
578 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
580 if (session_lookup_action_index_is_valid (ai))
581 return session_lookup_action_to_handle (ai);
583 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
584 sep->transport_proto);
585 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
590 * Zero out the ip. Same logic as above.
593 if (ip6_is_local_host (&sep->ip.ip6))
595 kv6.key[0] = kv6.key[1] = 0;
596 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
602 kv6.key[0] = kv6.key[1] = 0;
606 * Zero out the port. Same logic as above.
608 kv6.key[4] = kv6.key[5] = 0;
609 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
613 return SESSION_INVALID_HANDLE;
616 static inline stream_session_t *
617 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
618 u16 lcl_port, u8 proto, u8 use_wildcard)
624 * First, try a fully formed listener
626 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
627 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
629 return listen_session_get ((u32) kv4.value);
632 * Zero out the lcl ip and check if any 0/0 port binds have been done
637 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
639 return listen_session_get ((u32) kv4.value);
647 * Zero out port and check if we have a proxy set up for our ip
649 make_v4_proxy_kv (&kv4, lcl, proto);
650 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
652 return listen_session_get ((u32) kv4.value);
658 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
662 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
665 return session_lookup_listener4_i (st, lcl, lcl_port, proto, 0);
668 static stream_session_t *
669 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
670 u16 lcl_port, u8 proto, u8 ip_wildcard)
675 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
676 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
678 return listen_session_get ((u32) kv6.value);
680 /* Zero out the lcl ip */
683 kv6.key[0] = kv6.key[1] = 0;
684 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
686 return listen_session_get ((u32) kv6.value);
690 kv6.key[0] = kv6.key[1] = 0;
693 make_v6_proxy_kv (&kv6, lcl, proto);
694 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
696 return listen_session_get ((u32) kv6.value);
701 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
705 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
708 return session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
712 * Lookup listener, exact or proxy (inaddr_any:0) match
715 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
718 st = session_table_get (table_index);
722 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
723 sep->transport_proto, 0);
725 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
726 sep->transport_proto, 0);
731 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
737 st = session_table_get_or_alloc_for_connection (tc);
742 make_v4_ss_kv_from_tc (&kv4, tc);
744 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
749 make_v6_ss_kv_from_tc (&kv6, tc);
751 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
757 session_lookup_del_half_open (transport_connection_t * tc)
763 st = session_table_get_for_connection (tc);
768 make_v4_ss_kv_from_tc (&kv4, tc);
769 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
774 make_v6_ss_kv_from_tc (&kv6, tc);
775 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
781 session_lookup_half_open_handle (transport_connection_t * tc)
788 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
791 return HALF_OPEN_LOOKUP_INVALID_VALUE;
794 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
795 tc->rmt_port, tc->proto);
796 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
802 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
803 tc->rmt_port, tc->proto);
804 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
808 return HALF_OPEN_LOOKUP_INVALID_VALUE;
811 transport_connection_t *
812 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
816 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
818 sst = session_type_from_proto_and_ip (proto, is_ip4);
819 return tp_vfts[sst].get_half_open (handle & 0xFFFFFFFF);
825 * Lookup connection with ip4 and transport layer information
827 * This is used on the fast path so it needs to be fast. Thereby,
828 * duplication of code and 'hacks' allowed.
830 * The lookup is incremental and returns whenever something is matched. The
832 * - Try to find an established session
833 * - Try to find a half-open connection
834 * - Try session rules table
835 * - Try to find a fully-formed or local source wildcarded (listener bound to
836 * all interfaces) listener session
839 * @param fib_index index of fib wherein the connection was received
840 * @param lcl local ip4 address
841 * @param rmt remote ip4 address
842 * @param lcl_port local port
843 * @param rmt_port remote port
844 * @param proto transport protocol (e.g., tcp, udp)
845 * @param thread_index thread index for request
846 * @param is_filtered return flag that indicates if connection was filtered.
848 * @return pointer to transport connection, if one is found, 0 otherwise
850 transport_connection_t *
851 session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
852 ip4_address_t * rmt, u16 lcl_port,
853 u16 rmt_port, u8 proto, u32 thread_index,
862 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
863 if (PREDICT_FALSE (!st))
867 * Lookup session amongst established ones
869 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
870 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
873 ASSERT ((u32) (kv4.value >> 32) == thread_index);
874 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
875 return tp_vfts[proto].get_connection (s->connection_index,
880 * Try half-open connections
882 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
884 return tp_vfts[proto].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 (session_lookup_action_index_is_valid (action_index))
893 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
895 if ((s = session_lookup_action_to_session (action_index,
896 FIB_PROTOCOL_IP4, proto)))
897 return tp_vfts[proto].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, 1);
906 return tp_vfts[proto].get_listener (s->connection_index);
912 * Lookup connection with ip4 and transport layer information
914 * Not optimized. This is used on the fast path so it needs to be fast.
915 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
916 * to that of @ref session_lookup_connection_wt4
918 * @param fib_index index of the fib wherein the connection was received
919 * @param lcl local ip4 address
920 * @param rmt remote ip4 address
921 * @param lcl_port local port
922 * @param rmt_port remote port
923 * @param proto transport protocol (e.g., tcp, udp)
925 * @return pointer to transport connection, if one is found, 0 otherwise
927 transport_connection_t *
928 session_lookup_connection4 (u32 fib_index, ip4_address_t * lcl,
929 ip4_address_t * rmt, u16 lcl_port, u16 rmt_port,
938 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
939 if (PREDICT_FALSE (!st))
943 * Lookup session amongst established ones
945 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
946 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
949 s = session_get_from_handle (kv4.value);
950 return tp_vfts[proto].get_connection (s->connection_index,
955 * Try half-open connections
957 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
959 return tp_vfts[proto].get_half_open (kv4.value & 0xFFFFFFFF);
962 * Check the session rules table
964 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
965 rmt, lcl_port, rmt_port);
966 if (session_lookup_action_index_is_valid (action_index))
968 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
970 if ((s = session_lookup_action_to_session (action_index,
971 FIB_PROTOCOL_IP4, proto)))
972 return tp_vfts[proto].get_listener (s->connection_index);
977 * If nothing is found, check if any listener is available
979 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
981 return tp_vfts[proto].get_listener (s->connection_index);
987 * Lookup session with ip4 and transport layer information
989 * Important note: this may look into another thread's pool table and
990 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
991 * if needed as soon as possible.
993 * Lookup logic is similar to that of @ref session_lookup_connection_wt4 but
994 * this returns a session as opposed to a transport connection and it does not
995 * try to lookup half-open sessions.
997 * Typically used by dgram connections
1000 session_lookup_safe4 (u32 fib_index, ip4_address_t * lcl, ip4_address_t * rmt,
1001 u16 lcl_port, u16 rmt_port, u8 proto)
1003 session_table_t *st;
1005 stream_session_t *s;
1009 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
1010 if (PREDICT_FALSE (!st))
1014 * Lookup session amongst established ones
1016 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
1017 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
1019 return session_get_from_handle_safe (kv4.value);
1022 * Check the session rules table
1024 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
1025 rmt, lcl_port, rmt_port);
1026 if (session_lookup_action_index_is_valid (action_index))
1028 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1030 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP4,
1035 * If nothing is found, check if any listener is available
1037 if ((s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1)))
1044 * Lookup connection with ip6 and transport layer information
1046 * This is used on the fast path so it needs to be fast. Thereby,
1047 * duplication of code and 'hacks' allowed.
1049 * The lookup is incremental and returns whenever something is matched. The
1051 * - Try to find an established session
1052 * - Try to find a half-open connection
1053 * - Try session rules table
1054 * - Try to find a fully-formed or local source wildcarded (listener bound to
1055 * all interfaces) listener session
1058 * @param fib_index index of the fib wherein the connection was received
1059 * @param lcl local ip6 address
1060 * @param rmt remote ip6 address
1061 * @param lcl_port local port
1062 * @param rmt_port remote port
1063 * @param proto transport protocol (e.g., tcp, udp)
1064 * @param thread_index thread index for request
1066 * @return pointer to transport connection, if one is found, 0 otherwise
1068 transport_connection_t *
1069 session_lookup_connection_wt6 (u32 fib_index, ip6_address_t * lcl,
1070 ip6_address_t * rmt, u16 lcl_port,
1071 u16 rmt_port, u8 proto, u32 thread_index,
1074 session_table_t *st;
1075 stream_session_t *s;
1080 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1081 if (PREDICT_FALSE (!st))
1084 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1085 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1088 ASSERT ((u32) (kv6.value >> 32) == thread_index);
1089 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1090 return tp_vfts[proto].get_connection (s->connection_index,
1094 /* Try half-open connections */
1095 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1097 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1099 /* Check the session rules table */
1100 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1101 rmt, lcl_port, rmt_port);
1102 if (session_lookup_action_index_is_valid (action_index))
1104 if ((*is_filtered = (action_index == SESSION_RULES_TABLE_ACTION_DROP)))
1106 if ((s = session_lookup_action_to_session (action_index,
1107 FIB_PROTOCOL_IP6, proto)))
1108 return tp_vfts[proto].get_listener (s->connection_index);
1112 /* If nothing is found, check if any listener is available */
1113 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1115 return tp_vfts[proto].get_listener (s->connection_index);
1121 * Lookup connection with ip6 and transport layer information
1123 * Not optimized. This is used on the fast path so it needs to be fast.
1124 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1125 * to that of @ref session_lookup_connection_wt4
1127 * @param fib_index index of the fib wherein the connection was received
1128 * @param lcl local ip6 address
1129 * @param rmt remote ip6 address
1130 * @param lcl_port local port
1131 * @param rmt_port remote port
1132 * @param proto transport protocol (e.g., tcp, udp)
1134 * @return pointer to transport connection, if one is found, 0 otherwise
1136 transport_connection_t *
1137 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1138 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1141 session_table_t *st;
1142 stream_session_t *s;
1147 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1148 if (PREDICT_FALSE (!st))
1151 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1152 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1155 s = session_get_from_handle (kv6.value);
1156 return tp_vfts[proto].get_connection (s->connection_index,
1160 /* Try half-open connections */
1161 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1163 return tp_vfts[proto].get_half_open (kv6.value & 0xFFFFFFFF);
1165 /* Check the session rules table */
1166 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1167 rmt, lcl_port, rmt_port);
1168 if (session_lookup_action_index_is_valid (action_index))
1170 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1172 if ((s = session_lookup_action_to_session (action_index,
1173 FIB_PROTOCOL_IP6, proto)))
1174 return tp_vfts[proto].get_listener (s->connection_index);
1178 /* If nothing is found, check if any listener is available */
1179 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1181 return tp_vfts[proto].get_listener (s->connection_index);
1187 * Lookup session with ip6 and transport layer information
1189 * Important note: this may look into another thread's pool table and
1190 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1191 * if needed as soon as possible.
1193 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1194 * this returns a session as opposed to a transport connection and it does not
1195 * try to lookup half-open sessions.
1197 * Typically used by dgram connections
1200 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1201 u16 lcl_port, u16 rmt_port, u8 proto)
1203 session_table_t *st;
1205 stream_session_t *s;
1209 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1210 if (PREDICT_FALSE (!st))
1213 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1214 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1216 return session_get_from_handle_safe (kv6.value);
1218 /* Check the session rules table */
1219 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1220 rmt, lcl_port, rmt_port);
1221 if (session_lookup_action_index_is_valid (action_index))
1223 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1225 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1229 /* If nothing is found, check if any listener is available */
1230 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
1236 vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1238 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1239 session_rules_table_t *srt;
1240 session_table_t *st;
1243 clib_error_t *error;
1246 return clib_error_return_code (0, VNET_API_ERROR_APP_INVALID_NS, 0,
1248 if (args->scope > 3)
1249 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1251 if (args->transport_proto != TRANSPORT_PROTO_TCP
1252 && args->transport_proto != TRANSPORT_PROTO_UDP)
1253 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
1254 "invalid transport proto");
1255 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1257 fib_proto = args->table_args.rmt.fp_proto;
1258 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1259 st = session_table_get_for_fib_index (fib_proto, fib_index);
1260 srt = &st->session_rules[args->transport_proto];
1261 if ((error = session_rules_table_add_del (srt, &args->table_args)))
1263 clib_error_report (error);
1267 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1269 memset (&args->table_args.lcl, 0, sizeof (args->table_args.lcl));
1270 args->table_args.lcl.fp_proto = args->table_args.rmt.fp_proto;
1271 args->table_args.lcl_port = 0;
1272 st = app_namespace_get_local_table (app_ns);
1273 srt = &st->session_rules[args->transport_proto];
1274 error = session_rules_table_add_del (srt, &args->table_args);
1280 * Mark (global) tables as pertaining to app ns
1283 session_lookup_set_tables_appns (app_namespace_t * app_ns)
1285 session_table_t *st;
1289 for (fp = 0; fp < ARRAY_LEN (fib_index_to_table_index); fp++)
1291 fib_index = app_namespace_get_fib_index (app_ns, fp);
1292 st = session_table_get_for_fib_index (fp, fib_index);
1294 st->appns_index = app_namespace_index (app_ns);
1299 format_ip4_session_lookup_kvp (u8 * s, va_list * args)
1301 clib_bihash_kv_16_8_t *kvp = va_arg (*args, clib_bihash_kv_16_8_t *);
1302 u32 is_local = va_arg (*args, u32);
1303 u8 *app_name, *str = 0;
1304 stream_session_t *session;
1305 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1309 session = session_get_from_handle (kvp->value);
1310 app_name = application_name_from_index (session->app_index);
1311 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1312 key->proto, format_ip4_address, &key->src,
1313 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1314 &key->dst, clib_net_to_host_u16 (key->dst_port));
1315 s = format (s, "%-40v%-30v", str, app_name);
1319 app_name = application_name_from_index (kvp->value);
1320 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1321 format_ip4_address, &key->src,
1322 clib_net_to_host_u16 (key->src_port));
1323 s = format (s, "%-30v%-30v", str, app_name);
1325 vec_free (app_name);
1329 typedef struct _ip4_session_table_show_ctx_t
1333 } ip4_session_table_show_ctx_t;
1336 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1338 ip4_session_table_show_ctx_t *ctx = arg;
1339 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1345 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1346 u8 type, u8 is_local)
1348 ip4_session_table_show_ctx_t ctx = {
1350 .is_local = is_local,
1353 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1355 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1360 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1364 clib_warning ("not supported");
1368 static clib_error_t *
1369 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1370 vlib_cli_command_t * cmd)
1372 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1373 u32 appns_index, scope = 0;
1374 ip46_address_t lcl_ip, rmt_ip;
1375 u8 is_ip4 = 1, conn_set = 0;
1376 u8 fib_proto, is_add = 1, *ns_id = 0;
1378 app_namespace_t *app_ns;
1379 clib_error_t *error;
1381 memset (&lcl_ip, 0, sizeof (lcl_ip));
1382 memset (&rmt_ip, 0, sizeof (rmt_ip));
1383 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1385 if (unformat (input, "del"))
1387 else if (unformat (input, "add"))
1389 else if (unformat (input, "appns %_%v%_", &ns_id))
1391 else if (unformat (input, "scope global"))
1392 scope = SESSION_RULE_SCOPE_GLOBAL;
1393 else if (unformat (input, "scope local"))
1394 scope = SESSION_RULE_SCOPE_LOCAL;
1395 else if (unformat (input, "scope all"))
1396 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1397 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1399 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1400 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1401 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1407 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1408 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1409 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1415 else if (unformat (input, "action %d", &action))
1417 else if (unformat (input, "tag %_%v%_", &tag))
1420 return clib_error_return (0, "unknown input `%U'",
1421 format_unformat_error, input);
1426 vlib_cli_output (vm, "proto must be set");
1429 if (is_add && !conn_set && action == ~0)
1431 vlib_cli_output (vm, "connection and action must be set for add");
1434 if (!is_add && !tag && !conn_set)
1436 vlib_cli_output (vm, "connection or tag must be set for delete");
1439 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1441 vlib_cli_output (vm, "tag too long (max u64)");
1447 app_ns = app_namespace_get_from_id (ns_id);
1450 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1456 app_ns = app_namespace_get_default ();
1458 appns_index = app_namespace_index (app_ns);
1460 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1461 session_rule_add_del_args_t args = {
1462 .table_args.lcl.fp_addr = lcl_ip,
1463 .table_args.lcl.fp_len = lcl_plen,
1464 .table_args.lcl.fp_proto = fib_proto,
1465 .table_args.rmt.fp_addr = rmt_ip,
1466 .table_args.rmt.fp_len = rmt_plen,
1467 .table_args.rmt.fp_proto = fib_proto,
1468 .table_args.lcl_port = lcl_port,
1469 .table_args.rmt_port = rmt_port,
1470 .table_args.action_index = action,
1471 .table_args.is_add = is_add,
1472 .table_args.tag = tag,
1473 .appns_index = appns_index,
1476 error = vnet_session_rule_add_del (&args);
1482 VLIB_CLI_COMMAND (session_rule_command, static) =
1484 .path = "session rule",
1485 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1486 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1487 .function = session_rule_command_fn,
1492 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1495 vlib_main_t *vm = vlib_get_main ();
1496 session_rules_table_t *srt;
1497 session_table_t *st;
1498 st = session_table_get_for_fib_index (fib_index, fib_proto);
1499 srt = &st->session_rules[transport_proto];
1500 session_rules_table_cli_dump (vm, srt, fib_proto);
1504 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1507 vlib_main_t *vm = vlib_get_main ();
1508 session_rules_table_t *srt;
1509 session_table_t *st;
1510 st = session_table_get (table_index);
1511 srt = &st->session_rules[transport_proto];
1512 session_rules_table_cli_dump (vm, srt, fib_proto);
1515 static clib_error_t *
1516 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1517 vlib_cli_command_t * cmd)
1519 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1520 u32 fib_index, scope = 0;
1521 ip46_address_t lcl_ip, rmt_ip;
1522 u8 is_ip4 = 1, show_one = 0;
1523 app_namespace_t *app_ns;
1524 session_rules_table_t *srt;
1525 session_table_t *st;
1526 u8 *ns_id = 0, fib_proto;
1528 memset (&lcl_ip, 0, sizeof (lcl_ip));
1529 memset (&rmt_ip, 0, sizeof (rmt_ip));
1530 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1532 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1534 else if (unformat (input, "appns %_%v%_", &ns_id))
1536 else if (unformat (input, "scope global"))
1538 else if (unformat (input, "scope local"))
1540 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1541 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1542 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1548 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1549 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1550 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1557 return clib_error_return (0, "unknown input `%U'",
1558 format_unformat_error, input);
1561 if (transport_proto == ~0)
1563 vlib_cli_output (vm, "transport proto must be set");
1569 app_ns = app_namespace_get_from_id (ns_id);
1572 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1578 app_ns = app_namespace_get_default ();
1581 if (scope == 1 || scope == 0)
1583 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1584 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1585 st = session_table_get_for_fib_index (fib_proto, fib_index);
1589 st = app_namespace_get_local_table (app_ns);
1594 srt = &st->session_rules[transport_proto];
1595 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1600 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1602 srt = &st->session_rules[transport_proto];
1603 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1604 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
1611 VLIB_CLI_COMMAND (show_session_rules_command, static) =
1613 .path = "show session rules",
1614 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1615 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
1616 .function = show_session_rules_command_fn,
1621 session_lookup_init (void)
1624 * Allocate default table and map it to fib_index 0
1626 session_table_t *st = session_table_alloc ();
1627 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1628 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
1629 st->active_fib_proto = FIB_PROTOCOL_IP4;
1630 session_table_init (st, FIB_PROTOCOL_IP4);
1631 st = session_table_alloc ();
1632 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1633 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
1634 st->active_fib_proto = FIB_PROTOCOL_IP6;
1635 session_table_init (st, FIB_PROTOCOL_IP6);
1639 * fd.io coding-style-patch-verification: ON
1642 * eval: (c-set-style "gnu")