2 * Copyright (c) 2017-2019 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 * Network namespace index (i.e., fib index) to session lookup table. We
34 * should have one per network protocol type but for now we only support IP4/6
36 static u32 *fib_index_to_table_index[2];
40 typedef CLIB_PACKED (struct {
49 /* align by making this 4 octets even though its a 1-bit field
50 * NOTE: avoid key overlap with other transports that use 5 tuples for
51 * session identification.
57 }) v4_connection_key_t;
59 typedef CLIB_PACKED (struct {
74 }) v6_connection_key_t;
77 typedef clib_bihash_kv_16_8_t session_kv4_t;
78 typedef clib_bihash_kv_48_8_t session_kv6_t;
81 make_v4_ss_kv (session_kv4_t * kv, ip4_address_t * lcl, ip4_address_t * rmt,
82 u16 lcl_port, u16 rmt_port, u8 proto)
84 kv->key[0] = (u64) rmt->as_u32 << 32 | (u64) lcl->as_u32;
85 kv->key[1] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
90 make_v4_listener_kv (session_kv4_t * kv, ip4_address_t * lcl, u16 lcl_port,
93 kv->key[0] = (u64) lcl->as_u32;
94 kv->key[1] = (u64) proto << 32 | (u64) lcl_port;
99 make_v4_proxy_kv (session_kv4_t * kv, ip4_address_t * lcl, u8 proto)
101 kv->key[0] = (u64) lcl->as_u32;
102 kv->key[1] = (u64) proto << 32;
107 make_v4_ss_kv_from_tc (session_kv4_t * kv, transport_connection_t * tc)
109 make_v4_ss_kv (kv, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
110 tc->rmt_port, tc->proto);
114 make_v6_ss_kv (session_kv6_t * kv, ip6_address_t * lcl, ip6_address_t * rmt,
115 u16 lcl_port, u16 rmt_port, u8 proto)
117 kv->key[0] = lcl->as_u64[0];
118 kv->key[1] = lcl->as_u64[1];
119 kv->key[2] = rmt->as_u64[0];
120 kv->key[3] = rmt->as_u64[1];
121 kv->key[4] = (u64) proto << 32 | (u64) rmt_port << 16 | (u64) lcl_port;
127 make_v6_listener_kv (session_kv6_t * kv, ip6_address_t * lcl, u16 lcl_port,
130 kv->key[0] = lcl->as_u64[0];
131 kv->key[1] = lcl->as_u64[1];
134 kv->key[4] = (u64) proto << 32 | (u64) lcl_port;
140 make_v6_proxy_kv (session_kv6_t * kv, ip6_address_t * lcl, u8 proto)
142 kv->key[0] = lcl->as_u64[0];
143 kv->key[1] = lcl->as_u64[1];
146 kv->key[4] = (u64) proto << 32;
152 make_v6_ss_kv_from_tc (session_kv6_t * kv, transport_connection_t * tc)
154 make_v6_ss_kv (kv, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
155 tc->rmt_port, tc->proto);
158 static session_table_t *
159 session_table_get_or_alloc (u8 fib_proto, u8 fib_index)
163 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
165 st = session_table_alloc ();
166 table_index = session_table_index (st);
167 vec_validate (fib_index_to_table_index[fib_proto], fib_index);
168 fib_index_to_table_index[fib_proto][fib_index] = table_index;
169 st->active_fib_proto = fib_proto;
170 session_table_init (st, fib_proto);
175 table_index = fib_index_to_table_index[fib_proto][fib_index];
176 return session_table_get (table_index);
180 static session_table_t *
181 session_table_get_or_alloc_for_connection (transport_connection_t * tc)
184 fib_proto = transport_connection_fib_proto (tc);
185 return session_table_get_or_alloc (fib_proto, tc->fib_index);
188 static session_table_t *
189 session_table_get_for_connection (transport_connection_t * tc)
191 u32 fib_proto = transport_connection_fib_proto (tc);
192 if (vec_len (fib_index_to_table_index[fib_proto]) <= tc->fib_index)
195 session_table_get (fib_index_to_table_index[fib_proto][tc->fib_index]);
198 static session_table_t *
199 session_table_get_for_fib_index (u32 fib_proto, u32 fib_index)
201 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
203 return session_table_get (fib_index_to_table_index[fib_proto][fib_index]);
207 session_lookup_get_index_for_fib (u32 fib_proto, u32 fib_index)
209 if (vec_len (fib_index_to_table_index[fib_proto]) <= fib_index)
210 return SESSION_TABLE_INVALID_INDEX;
211 return fib_index_to_table_index[fib_proto][fib_index];
215 * Add transport connection to a session table
217 * Session lookup 5-tuple (src-ip, dst-ip, src-port, dst-port, session-type)
218 * is added to requested session table.
220 * @param tc transport connection to be added
221 * @param value value to be stored
223 * @return non-zero if failure
226 session_lookup_add_connection (transport_connection_t * tc, u64 value)
232 st = session_table_get_or_alloc_for_connection (tc);
237 make_v4_ss_kv_from_tc (&kv4, tc);
239 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
244 make_v6_ss_kv_from_tc (&kv6, tc);
246 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
252 session_lookup_add_session_endpoint (u32 table_index,
253 session_endpoint_t * sep, u64 value)
259 st = session_table_get (table_index);
264 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
265 sep->transport_proto);
267 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 1);
271 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
272 sep->transport_proto);
274 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 1);
279 session_lookup_del_session_endpoint (u32 table_index,
280 session_endpoint_t * sep)
286 st = session_table_get (table_index);
291 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
292 sep->transport_proto);
293 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4, 0);
297 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
298 sep->transport_proto);
299 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6, 0);
304 * Delete transport connection from session table
306 * @param table_index session table index
307 * @param tc transport connection to be removed
309 * @return non-zero if failure
312 session_lookup_del_connection (transport_connection_t * tc)
318 st = session_table_get_for_connection (tc);
323 make_v4_ss_kv_from_tc (&kv4, tc);
324 return clib_bihash_add_del_16_8 (&st->v4_session_hash, &kv4,
329 make_v6_ss_kv_from_tc (&kv6, tc);
330 return clib_bihash_add_del_48_8 (&st->v6_session_hash, &kv6,
336 session_lookup_del_session (session_t * s)
338 transport_connection_t *ts;
339 ts = transport_get_connection (session_get_transport_proto (s),
340 s->connection_index, s->thread_index);
341 return session_lookup_del_connection (ts);
345 session_lookup_action_index_is_valid (u32 action_index)
347 if (action_index == SESSION_RULES_TABLE_ACTION_ALLOW
348 || action_index == SESSION_RULES_TABLE_INVALID_INDEX)
354 session_lookup_action_to_handle (u32 action_index)
356 switch (action_index)
358 case SESSION_RULES_TABLE_ACTION_DROP:
359 return SESSION_DROP_HANDLE;
360 case SESSION_RULES_TABLE_ACTION_ALLOW:
361 case SESSION_RULES_TABLE_INVALID_INDEX:
362 return SESSION_INVALID_HANDLE;
364 /* application index */
370 session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
374 app = application_get_if_valid (app_index);
378 return app_worker_first_listener (application_get_default_worker (app),
379 fib_proto, transport_proto);
383 session_lookup_action_to_session (u32 action_index, u8 fib_proto,
387 app_index = session_lookup_action_to_handle (action_index);
388 /* Nothing sophisticated for now, action index is app index */
389 return session_lookup_app_listen_session (app_index, fib_proto,
395 session_lookup_rules_table_session4 (session_table_t * st, u8 proto,
396 ip4_address_t * lcl, u16 lcl_port,
397 ip4_address_t * rmt, u16 rmt_port)
399 session_rules_table_t *srt = &st->session_rules[proto];
400 u32 action_index, app_index;
401 action_index = session_rules_table_lookup4 (srt, lcl, rmt, lcl_port,
403 app_index = session_lookup_action_to_handle (action_index);
404 /* Nothing sophisticated for now, action index is app index */
405 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP4,
411 session_lookup_rules_table_session6 (session_table_t * st, u8 proto,
412 ip6_address_t * lcl, u16 lcl_port,
413 ip6_address_t * rmt, u16 rmt_port)
415 session_rules_table_t *srt = &st->session_rules[proto];
416 u32 action_index, app_index;
417 action_index = session_rules_table_lookup6 (srt, lcl, rmt, lcl_port,
419 app_index = session_lookup_action_to_handle (action_index);
420 return session_lookup_app_listen_session (app_index, FIB_PROTOCOL_IP6,
425 * Lookup listener for session endpoint in table
427 * @param table_index table where the endpoint should be looked up
428 * @param sep session endpoint to be looked up
429 * @param use_rules flag that indicates if the session rules of the table
431 * @return invalid handle if nothing is found, the handle of a valid listener
432 * or an action derived handle if a rule is hit
435 session_lookup_endpoint_listener (u32 table_index, session_endpoint_t * sep,
438 session_rules_table_t *srt;
443 st = session_table_get (table_index);
445 return SESSION_INVALID_HANDLE;
451 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
452 sep->transport_proto);
453 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
458 clib_memset (&lcl4, 0, sizeof (lcl4));
459 srt = &st->session_rules[sep->transport_proto];
460 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
462 if (session_lookup_action_index_is_valid (ai))
463 return session_lookup_action_to_handle (ai);
471 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
472 sep->transport_proto);
473 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
479 clib_memset (&lcl6, 0, sizeof (lcl6));
480 srt = &st->session_rules[sep->transport_proto];
481 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
483 if (session_lookup_action_index_is_valid (ai))
484 return session_lookup_action_to_handle (ai);
487 return SESSION_INVALID_HANDLE;
491 * Look up endpoint in local session table
493 * The result, for now, is an application index and it may in the future
494 * be extended to a more complicated "action object". The only action we
495 * emulate now is "drop" and for that we return a special app index.
497 * Lookup logic is to check in order:
498 * - the rules in the table (connect acls)
499 * - session sub-table for a listener
500 * - session sub-table for a local listener (zeroed addr)
502 * @param table_index table where the lookup should be done
503 * @param sep session endpoint to be looked up
504 * @return session handle that can be interpreted as an adjacency
507 session_lookup_local_endpoint (u32 table_index, session_endpoint_t * sep)
509 session_rules_table_t *srt;
514 st = session_table_get (table_index);
516 return SESSION_INVALID_INDEX;
517 ASSERT (st->is_local);
525 * Check if endpoint has special rules associated
527 clib_memset (&lcl4, 0, sizeof (lcl4));
528 srt = &st->session_rules[sep->transport_proto];
529 ai = session_rules_table_lookup4 (srt, &lcl4, &sep->ip.ip4, 0,
531 if (session_lookup_action_index_is_valid (ai))
532 return session_lookup_action_to_handle (ai);
535 * Check if session endpoint is a listener
537 make_v4_listener_kv (&kv4, &sep->ip.ip4, sep->port,
538 sep->transport_proto);
539 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
544 * Zero out the ip. Logic is that connect to local ips, say
545 * 127.0.0.1:port, can match 0.0.0.0:port
547 if (ip4_is_local_host (&sep->ip.ip4))
550 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
560 * Zero out the port and check if we have proxy
563 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
572 clib_memset (&lcl6, 0, sizeof (lcl6));
573 srt = &st->session_rules[sep->transport_proto];
574 ai = session_rules_table_lookup6 (srt, &lcl6, &sep->ip.ip6, 0,
576 if (session_lookup_action_index_is_valid (ai))
577 return session_lookup_action_to_handle (ai);
579 make_v6_listener_kv (&kv6, &sep->ip.ip6, sep->port,
580 sep->transport_proto);
581 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
586 * Zero out the ip. Same logic as above.
589 if (ip6_is_local_host (&sep->ip.ip6))
591 kv6.key[0] = kv6.key[1] = 0;
592 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
598 kv6.key[0] = kv6.key[1] = 0;
602 * Zero out the port. Same logic as above.
604 kv6.key[4] = kv6.key[5] = 0;
605 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
609 return SESSION_INVALID_HANDLE;
612 static inline session_t *
613 session_lookup_listener4_i (session_table_t * st, ip4_address_t * lcl,
614 u16 lcl_port, u8 proto, u8 use_wildcard)
620 * First, try a fully formed listener
622 make_v4_listener_kv (&kv4, lcl, lcl_port, proto);
623 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
625 return listen_session_get ((u32) kv4.value);
628 * Zero out the lcl ip and check if any 0/0 port binds have been done
633 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
635 return listen_session_get ((u32) kv4.value);
643 * Zero out port and check if we have a proxy set up for our ip
645 make_v4_proxy_kv (&kv4, lcl, proto);
646 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
648 return listen_session_get ((u32) kv4.value);
654 session_lookup_listener4 (u32 fib_index, ip4_address_t * lcl, u16 lcl_port,
658 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
661 return session_lookup_listener4_i (st, lcl, lcl_port, proto, 0);
665 session_lookup_listener6_i (session_table_t * st, ip6_address_t * lcl,
666 u16 lcl_port, u8 proto, u8 ip_wildcard)
671 make_v6_listener_kv (&kv6, lcl, lcl_port, proto);
672 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
674 return listen_session_get ((u32) kv6.value);
676 /* Zero out the lcl ip */
679 kv6.key[0] = kv6.key[1] = 0;
680 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
682 return listen_session_get ((u32) kv6.value);
686 kv6.key[0] = kv6.key[1] = 0;
689 make_v6_proxy_kv (&kv6, lcl, proto);
690 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
692 return listen_session_get ((u32) kv6.value);
697 session_lookup_listener6 (u32 fib_index, ip6_address_t * lcl, u16 lcl_port,
701 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
704 return session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
708 * Lookup listener, exact or proxy (inaddr_any:0) match
711 session_lookup_listener (u32 table_index, session_endpoint_t * sep)
714 st = session_table_get (table_index);
718 return session_lookup_listener4_i (st, &sep->ip.ip4, sep->port,
719 sep->transport_proto, 0);
721 return session_lookup_listener6_i (st, &sep->ip.ip6, sep->port,
722 sep->transport_proto, 0);
727 session_lookup_add_half_open (transport_connection_t * tc, u64 value)
733 st = session_table_get_or_alloc_for_connection (tc);
738 make_v4_ss_kv_from_tc (&kv4, tc);
740 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
745 make_v6_ss_kv_from_tc (&kv6, tc);
747 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
753 session_lookup_del_half_open (transport_connection_t * tc)
759 st = session_table_get_for_connection (tc);
764 make_v4_ss_kv_from_tc (&kv4, tc);
765 return clib_bihash_add_del_16_8 (&st->v4_half_open_hash, &kv4,
770 make_v6_ss_kv_from_tc (&kv6, tc);
771 return clib_bihash_add_del_48_8 (&st->v6_half_open_hash, &kv6,
777 session_lookup_half_open_handle (transport_connection_t * tc)
784 st = session_table_get_for_fib_index (transport_connection_fib_proto (tc),
787 return HALF_OPEN_LOOKUP_INVALID_VALUE;
790 make_v4_ss_kv (&kv4, &tc->lcl_ip.ip4, &tc->rmt_ip.ip4, tc->lcl_port,
791 tc->rmt_port, tc->proto);
792 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
798 make_v6_ss_kv (&kv6, &tc->lcl_ip.ip6, &tc->rmt_ip.ip6, tc->lcl_port,
799 tc->rmt_port, tc->proto);
800 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
804 return HALF_OPEN_LOOKUP_INVALID_VALUE;
807 transport_connection_t *
808 session_lookup_half_open_connection (u64 handle, u8 proto, u8 is_ip4)
810 if (handle != HALF_OPEN_LOOKUP_INVALID_VALUE)
812 u32 sst = session_type_from_proto_and_ip (proto, is_ip4);
813 return transport_get_half_open (sst, handle & 0xFFFFFFFF);
819 * Lookup connection with ip4 and transport layer information
821 * This is used on the fast path so it needs to be fast. Thereby,
822 * duplication of code and 'hacks' allowed.
824 * The lookup is incremental and returns whenever something is matched. The
826 * - Try to find an established session
827 * - Try to find a half-open connection
828 * - Try session rules table
829 * - Try to find a fully-formed or local source wildcarded (listener bound to
830 * all interfaces) listener session
833 * @param fib_index index of fib wherein the connection was received
834 * @param lcl local ip4 address
835 * @param rmt remote ip4 address
836 * @param lcl_port local port
837 * @param rmt_port remote port
838 * @param proto transport protocol (e.g., tcp, udp)
839 * @param thread_index thread index for request
840 * @param is_filtered return flag that indicates if connection was filtered.
842 * @return pointer to transport connection, if one is found, 0 otherwise
844 transport_connection_t *
845 session_lookup_connection_wt4 (u32 fib_index, ip4_address_t * lcl,
846 ip4_address_t * rmt, u16 lcl_port,
847 u16 rmt_port, u8 proto, u32 thread_index,
856 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP4, fib_index);
857 if (PREDICT_FALSE (!st))
861 * Lookup session amongst established ones
863 make_v4_ss_kv (&kv4, lcl, rmt, lcl_port, rmt_port, proto);
864 rv = clib_bihash_search_inline_16_8 (&st->v4_session_hash, &kv4);
867 if (PREDICT_FALSE ((u32) (kv4.value >> 32) != thread_index))
869 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
872 s = session_get (kv4.value & 0xFFFFFFFFULL, thread_index);
873 return transport_get_connection (proto, s->connection_index,
878 * Try half-open connections
880 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
882 return transport_get_half_open (proto, kv4.value & 0xFFFFFFFF);
885 * Check the session rules table
887 action_index = session_rules_table_lookup4 (&st->session_rules[proto], lcl,
888 rmt, lcl_port, rmt_port);
889 if (session_lookup_action_index_is_valid (action_index))
891 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
893 *result = SESSION_LOOKUP_RESULT_FILTERED;
896 if ((s = session_lookup_action_to_session (action_index,
897 FIB_PROTOCOL_IP4, proto)))
898 return transport_get_listener (proto, s->connection_index);
903 * If nothing is found, check if any listener is available
905 s = session_lookup_listener4_i (st, lcl, lcl_port, proto, 1);
907 return transport_get_listener (proto, s->connection_index);
913 * Lookup connection with ip4 and transport layer information
915 * Not optimized. Lookup logic is identical to that of
916 * @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 transport_get_connection (proto, s->connection_index,
955 * Try half-open connections
957 rv = clib_bihash_search_inline_16_8 (&st->v4_half_open_hash, &kv4);
959 return transport_get_half_open (proto, 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 transport_get_listener (proto, 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 transport_get_listener (proto, 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;
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;
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 if (PREDICT_FALSE ((u32) (kv6.value >> 32) != thread_index))
1091 *result = SESSION_LOOKUP_RESULT_WRONG_THREAD;
1094 s = session_get (kv6.value & 0xFFFFFFFFULL, thread_index);
1095 return transport_get_connection (proto, s->connection_index,
1099 /* Try half-open connections */
1100 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1102 return transport_get_half_open (proto, 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 (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1111 *result = SESSION_LOOKUP_RESULT_FILTERED;
1114 if ((s = session_lookup_action_to_session (action_index,
1115 FIB_PROTOCOL_IP6, proto)))
1116 return transport_get_listener (proto, s->connection_index);
1120 /* If nothing is found, check if any listener is available */
1121 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1123 return transport_get_listener (proto, s->connection_index);
1129 * Lookup connection with ip6 and transport layer information
1131 * Not optimized. This is used on the fast path so it needs to be fast.
1132 * Thereby, duplication of code and 'hacks' allowed. Lookup logic is identical
1133 * to that of @ref session_lookup_connection_wt4
1135 * @param fib_index index of the fib wherein the connection was received
1136 * @param lcl local ip6 address
1137 * @param rmt remote ip6 address
1138 * @param lcl_port local port
1139 * @param rmt_port remote port
1140 * @param proto transport protocol (e.g., tcp, udp)
1142 * @return pointer to transport connection, if one is found, 0 otherwise
1144 transport_connection_t *
1145 session_lookup_connection6 (u32 fib_index, ip6_address_t * lcl,
1146 ip6_address_t * rmt, u16 lcl_port, u16 rmt_port,
1149 session_table_t *st;
1155 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1156 if (PREDICT_FALSE (!st))
1159 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1160 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1163 s = session_get_from_handle (kv6.value);
1164 return transport_get_connection (proto, s->connection_index,
1168 /* Try half-open connections */
1169 rv = clib_bihash_search_inline_48_8 (&st->v6_half_open_hash, &kv6);
1171 return transport_get_half_open (proto, kv6.value & 0xFFFFFFFF);
1173 /* Check the session rules table */
1174 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1175 rmt, lcl_port, rmt_port);
1176 if (session_lookup_action_index_is_valid (action_index))
1178 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1180 if ((s = session_lookup_action_to_session (action_index,
1181 FIB_PROTOCOL_IP6, proto)))
1182 return transport_get_listener (proto, s->connection_index);
1186 /* If nothing is found, check if any listener is available */
1187 s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1);
1189 return transport_get_listener (proto, s->connection_index);
1195 * Lookup session with ip6 and transport layer information
1197 * Important note: this may look into another thread's pool table and
1198 * register as 'peeker'. Caller should call @ref session_pool_remove_peeker as
1199 * if needed as soon as possible.
1201 * Lookup logic is similar to that of @ref session_lookup_connection_wt6 but
1202 * this returns a session as opposed to a transport connection and it does not
1203 * try to lookup half-open sessions.
1205 * Typically used by dgram connections
1208 session_lookup_safe6 (u32 fib_index, ip6_address_t * lcl, ip6_address_t * rmt,
1209 u16 lcl_port, u16 rmt_port, u8 proto)
1211 session_table_t *st;
1217 st = session_table_get_for_fib_index (FIB_PROTOCOL_IP6, fib_index);
1218 if (PREDICT_FALSE (!st))
1221 make_v6_ss_kv (&kv6, lcl, rmt, lcl_port, rmt_port, proto);
1222 rv = clib_bihash_search_inline_48_8 (&st->v6_session_hash, &kv6);
1224 return session_get_from_handle_safe (kv6.value);
1226 /* Check the session rules table */
1227 action_index = session_rules_table_lookup6 (&st->session_rules[proto], lcl,
1228 rmt, lcl_port, rmt_port);
1229 if (session_lookup_action_index_is_valid (action_index))
1231 if (action_index == SESSION_RULES_TABLE_ACTION_DROP)
1233 return session_lookup_action_to_session (action_index, FIB_PROTOCOL_IP6,
1237 /* If nothing is found, check if any listener is available */
1238 if ((s = session_lookup_listener6_i (st, lcl, lcl_port, proto, 1)))
1244 vnet_session_rule_add_del (session_rule_add_del_args_t * args)
1246 app_namespace_t *app_ns = app_namespace_get (args->appns_index);
1247 session_rules_table_t *srt;
1248 session_table_t *st;
1254 return VNET_API_ERROR_APP_INVALID_NS;
1256 if (args->scope > 3)
1257 return VNET_API_ERROR_INVALID_VALUE;
1259 if (args->transport_proto != TRANSPORT_PROTO_TCP
1260 && args->transport_proto != TRANSPORT_PROTO_UDP)
1261 return VNET_API_ERROR_INVALID_VALUE;
1263 if ((args->scope & SESSION_RULE_SCOPE_GLOBAL) || args->scope == 0)
1265 fib_proto = args->table_args.rmt.fp_proto;
1266 fib_index = app_namespace_get_fib_index (app_ns, fib_proto);
1267 st = session_table_get_for_fib_index (fib_proto, fib_index);
1268 srt = &st->session_rules[args->transport_proto];
1269 if ((rv = session_rules_table_add_del (srt, &args->table_args)))
1272 if (args->scope & SESSION_RULE_SCOPE_LOCAL)
1274 clib_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 rv = 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 v4_connection_key_t *key = (v4_connection_key_t *) kvp->key;
1310 app_worker_t *app_wrk;
1316 session = session_get_from_handle (kvp->value);
1317 app_wrk = app_worker_get (session->app_wrk_index);
1318 app_name = application_name_from_index (app_wrk->app_index);
1319 str = format (0, "[%U] %U:%d->%U:%d", format_transport_proto_short,
1320 key->proto, format_ip4_address, &key->src,
1321 clib_net_to_host_u16 (key->src_port), format_ip4_address,
1322 &key->dst, clib_net_to_host_u16 (key->dst_port));
1323 s = format (s, "%-40v%-30v", str, app_name);
1327 session = session_get_from_handle (kvp->value);
1328 app_wrk = app_worker_get (session->app_wrk_index);
1329 app_name = application_name_from_index (app_wrk->app_index);
1330 str = format (0, "[%U] %U:%d", format_transport_proto_short, key->proto,
1331 format_ip4_address, &key->src,
1332 clib_net_to_host_u16 (key->src_port));
1333 s = format (s, "%-30v%-30v", str, app_name);
1338 typedef struct _ip4_session_table_show_ctx_t
1342 } ip4_session_table_show_ctx_t;
1345 ip4_session_table_show (clib_bihash_kv_16_8_t * kvp, void *arg)
1347 ip4_session_table_show_ctx_t *ctx = arg;
1348 vlib_cli_output (ctx->vm, "%U", format_ip4_session_lookup_kvp, kvp,
1354 session_lookup_show_table_entries (vlib_main_t * vm, session_table_t * table,
1355 u8 type, u8 is_local)
1357 ip4_session_table_show_ctx_t ctx = {
1359 .is_local = is_local,
1362 vlib_cli_output (vm, "%-40s%-30s", "Session", "Application");
1364 vlib_cli_output (vm, "%-30s%-30s", "Listener", "Application");
1369 ip4_session_table_walk (&table->v4_session_hash, ip4_session_table_show,
1373 clib_warning ("not supported");
1377 static clib_error_t *
1378 session_rule_command_fn (vlib_main_t * vm, unformat_input_t * input,
1379 vlib_cli_command_t * cmd)
1381 u32 proto = ~0, lcl_port, rmt_port, action = 0, lcl_plen = 0, rmt_plen = 0;
1382 u32 appns_index, scope = 0;
1383 ip46_address_t lcl_ip, rmt_ip;
1384 u8 is_ip4 = 1, conn_set = 0;
1385 u8 fib_proto, is_add = 1, *ns_id = 0;
1387 app_namespace_t *app_ns;
1390 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1391 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
1392 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1394 if (unformat (input, "del"))
1396 else if (unformat (input, "add"))
1398 else if (unformat (input, "appns %_%v%_", &ns_id))
1400 else if (unformat (input, "scope global"))
1401 scope = SESSION_RULE_SCOPE_GLOBAL;
1402 else if (unformat (input, "scope local"))
1403 scope = SESSION_RULE_SCOPE_LOCAL;
1404 else if (unformat (input, "scope all"))
1405 scope = SESSION_RULE_SCOPE_LOCAL | SESSION_RULE_SCOPE_GLOBAL;
1406 else if (unformat (input, "proto %U", unformat_transport_proto, &proto))
1408 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1409 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1410 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1416 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1417 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1418 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1424 else if (unformat (input, "action %d", &action))
1426 else if (unformat (input, "tag %_%v%_", &tag))
1429 return clib_error_return (0, "unknown input `%U'",
1430 format_unformat_error, input);
1435 vlib_cli_output (vm, "proto must be set");
1438 if (is_add && !conn_set && action == ~0)
1440 vlib_cli_output (vm, "connection and action must be set for add");
1443 if (!is_add && !tag && !conn_set)
1445 vlib_cli_output (vm, "connection or tag must be set for delete");
1448 if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
1450 vlib_cli_output (vm, "tag too long (max u64)");
1456 app_ns = app_namespace_get_from_id (ns_id);
1459 vlib_cli_output (vm, "namespace %v does not exist", ns_id);
1465 app_ns = app_namespace_get_default ();
1467 appns_index = app_namespace_index (app_ns);
1469 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1470 session_rule_add_del_args_t args = {
1471 .table_args.lcl.fp_addr = lcl_ip,
1472 .table_args.lcl.fp_len = lcl_plen,
1473 .table_args.lcl.fp_proto = fib_proto,
1474 .table_args.rmt.fp_addr = rmt_ip,
1475 .table_args.rmt.fp_len = rmt_plen,
1476 .table_args.rmt.fp_proto = fib_proto,
1477 .table_args.lcl_port = lcl_port,
1478 .table_args.rmt_port = rmt_port,
1479 .table_args.action_index = action,
1480 .table_args.is_add = is_add,
1481 .table_args.tag = tag,
1482 .appns_index = appns_index,
1485 if ((rv = vnet_session_rule_add_del (&args)))
1486 return clib_error_return (0, "rule add del returned %u", rv);
1493 VLIB_CLI_COMMAND (session_rule_command, static) =
1495 .path = "session rule",
1496 .short_help = "session rule [add|del] appns <ns_id> proto <proto> "
1497 "<lcl-ip/plen> <lcl-port> <rmt-ip/plen> <rmt-port> action <action>",
1498 .function = session_rule_command_fn,
1503 session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
1506 vlib_main_t *vm = vlib_get_main ();
1507 session_rules_table_t *srt;
1508 session_table_t *st;
1509 st = session_table_get_for_fib_index (fib_index, fib_proto);
1510 srt = &st->session_rules[transport_proto];
1511 session_rules_table_cli_dump (vm, srt, fib_proto);
1515 session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
1518 vlib_main_t *vm = vlib_get_main ();
1519 session_rules_table_t *srt;
1520 session_table_t *st;
1521 st = session_table_get (table_index);
1522 srt = &st->session_rules[transport_proto];
1523 session_rules_table_cli_dump (vm, srt, fib_proto);
1526 static clib_error_t *
1527 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
1528 vlib_cli_command_t * cmd)
1530 u32 transport_proto = ~0, lcl_port, rmt_port, lcl_plen, rmt_plen;
1531 u32 fib_index, scope = 0;
1532 ip46_address_t lcl_ip, rmt_ip;
1533 u8 is_ip4 = 1, show_one = 0;
1534 app_namespace_t *app_ns;
1535 session_rules_table_t *srt;
1536 session_table_t *st;
1537 u8 *ns_id = 0, fib_proto;
1539 clib_memset (&lcl_ip, 0, sizeof (lcl_ip));
1540 clib_memset (&rmt_ip, 0, sizeof (rmt_ip));
1541 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1543 if (unformat (input, "%U", unformat_transport_proto, &transport_proto))
1545 else if (unformat (input, "appns %_%v%_", &ns_id))
1547 else if (unformat (input, "scope global"))
1549 else if (unformat (input, "scope local"))
1551 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip4_address,
1552 &lcl_ip.ip4, &lcl_plen, &lcl_port,
1553 unformat_ip4_address, &rmt_ip.ip4, &rmt_plen,
1559 else if (unformat (input, "%U/%d %d %U/%d %d", unformat_ip6_address,
1560 &lcl_ip.ip6, &lcl_plen, &lcl_port,
1561 unformat_ip6_address, &rmt_ip.ip6, &rmt_plen,
1568 return clib_error_return (0, "unknown input `%U'",
1569 format_unformat_error, input);
1572 if (transport_proto == ~0)
1574 vlib_cli_output (vm, "transport proto must be set");
1580 app_ns = app_namespace_get_from_id (ns_id);
1583 vlib_cli_output (vm, "appns %v doesn't exist", ns_id);
1589 app_ns = app_namespace_get_default ();
1592 if (scope == 1 || scope == 0)
1594 fib_proto = is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
1595 fib_index = is_ip4 ? app_ns->ip4_fib_index : app_ns->ip6_fib_index;
1596 st = session_table_get_for_fib_index (fib_proto, fib_index);
1600 st = app_namespace_get_local_table (app_ns);
1605 srt = &st->session_rules[transport_proto];
1606 session_rules_table_show_rule (vm, srt, &lcl_ip, lcl_port, &rmt_ip,
1611 vlib_cli_output (vm, "%U rules table", format_transport_proto,
1613 srt = &st->session_rules[transport_proto];
1614 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP4);
1615 session_rules_table_cli_dump (vm, srt, FIB_PROTOCOL_IP6);
1622 VLIB_CLI_COMMAND (show_session_rules_command, static) =
1624 .path = "show session rules",
1625 .short_help = "show session rules [<proto> appns <id> <lcl-ip/plen> "
1626 "<lcl-port> <rmt-ip/plen> <rmt-port> scope <scope>]",
1627 .function = show_session_rules_command_fn,
1632 session_lookup_init (void)
1635 * Allocate default table and map it to fib_index 0
1637 session_table_t *st = session_table_alloc ();
1638 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP4], 0);
1639 fib_index_to_table_index[FIB_PROTOCOL_IP4][0] = session_table_index (st);
1640 st->active_fib_proto = FIB_PROTOCOL_IP4;
1641 session_table_init (st, FIB_PROTOCOL_IP4);
1642 st = session_table_alloc ();
1643 vec_validate (fib_index_to_table_index[FIB_PROTOCOL_IP6], 0);
1644 fib_index_to_table_index[FIB_PROTOCOL_IP6][0] = session_table_index (st);
1645 st->active_fib_proto = FIB_PROTOCOL_IP6;
1646 session_table_init (st, FIB_PROTOCOL_IP6);
1650 * fd.io coding-style-patch-verification: ON
1653 * eval: (c-set-style "gnu")