session: support drop action in rules table
[vpp.git] / src / vnet / session / session_lookup.c
index 2168c61..c55712e 100644 (file)
@@ -339,22 +339,24 @@ session_lookup_del_session (stream_session_t * s)
   return session_lookup_del_connection (ts);
 }
 
+static u32
+session_lookup_action_to_session (u32 action_index)
+{
+  if (action_index != SESSION_RULES_TABLE_ACTION_DROP)
+    return action_index;
+  return SESSION_INVALID_INDEX;
+}
+
 static stream_session_t *
-session_lookup_app_listen_session (u32 app_index)
+session_lookup_app_listen_session (u32 app_index, u8 fib_proto,
+                                  u8 transport_proto)
 {
   application_t *app;
   app = application_get (app_index);
   if (!app)
     return 0;
 
-  if (application_n_listeners (app) != 1)
-    {
-      clib_warning ("there should be one and only one listener %d",
-                   hash_elts (app->listeners_table));
-      return 0;
-    }
-
-  return application_first_listener (app);
+  return application_first_listener (app, fib_proto, transport_proto);
 }
 
 stream_session_t *
@@ -362,11 +364,13 @@ session_lookup_rules_table4 (session_rules_table_t * srt, u8 proto,
                             ip4_address_t * lcl, u16 lcl_port,
                             ip4_address_t * rmt, u16 rmt_port)
 {
-  u32 action_index;
+  u32 action_index, session_index;
   action_index = session_rules_table_lookup4 (srt, proto, lcl, rmt, lcl_port,
                                              rmt_port);
+  session_index = session_lookup_action_to_session (action_index);
   /* Nothing sophisticated for now, action index is app index */
-  return session_lookup_app_listen_session (action_index);
+  return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP4,
+                                           proto);
 }
 
 stream_session_t *
@@ -374,10 +378,12 @@ session_lookup_rules_table6 (session_rules_table_t * srt, u8 proto,
                             ip6_address_t * lcl, u16 lcl_port,
                             ip6_address_t * rmt, u16 rmt_port)
 {
-  u32 action_index;
+  u32 action_index, session_index;
   action_index = session_rules_table_lookup6 (srt, proto, lcl, rmt, lcl_port,
                                              rmt_port);
-  return session_lookup_app_listen_session (action_index);
+  session_index = session_lookup_action_to_session (action_index);
+  return session_lookup_app_listen_session (session_index, FIB_PROTOCOL_IP6,
+                                           proto);
 }
 
 u64
@@ -388,7 +394,7 @@ session_lookup_session_endpoint (u32 table_index, session_endpoint_t * sep)
   session_kv6_t kv6;
   ip4_address_t lcl4;
   ip6_address_t lcl6;
-  u32 si;
+  u32 ai;
   int rv;
 
   st = session_table_get (table_index);
@@ -403,11 +409,11 @@ session_lookup_session_endpoint (u32 table_index, session_endpoint_t * sep)
        return kv4.value;
 
       memset (&lcl4, 0, sizeof (lcl4));
-      si =
-       session_rules_table_lookup4 (&st->session_rules, sep->transport_proto,
-                                    &lcl4, &sep->ip.ip4, 0, sep->port);
-      if (si != SESSION_RULES_TABLE_INVALID_INDEX)
-       return si;
+      ai = session_rules_table_lookup4 (&st->session_rules,
+                                       sep->transport_proto, &lcl4,
+                                       &sep->ip.ip4, 0, sep->port);
+      if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
+       return session_lookup_action_to_session (ai);
     }
   else
     {
@@ -418,11 +424,11 @@ session_lookup_session_endpoint (u32 table_index, session_endpoint_t * sep)
        return kv6.value;
 
       memset (&lcl6, 0, sizeof (lcl6));
-      si =
-       session_rules_table_lookup6 (&st->session_rules, sep->transport_proto,
-                                    &lcl6, &sep->ip.ip6, 0, sep->port);
-      if (si != SESSION_RULES_TABLE_INVALID_INDEX)
-       return si;
+      ai = session_rules_table_lookup6 (&st->session_rules,
+                                       sep->transport_proto, &lcl6,
+                                       &sep->ip.ip6, 0, sep->port);
+      if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
+       return session_lookup_action_to_session (ai);
     }
   return SESSION_INVALID_HANDLE;
 }
@@ -479,7 +485,7 @@ session_lookup_local_session_endpoint (u32 table_index,
   session_kv6_t kv6;
   ip4_address_t lcl4;
   ip6_address_t lcl6;
-  u32 si;
+  u32 ai;
   int rv;
 
   st = session_table_get (table_index);
@@ -503,11 +509,11 @@ session_lookup_local_session_endpoint (u32 table_index,
        return (u32) kv4.value;
 
       memset (&lcl4, 0, sizeof (lcl4));
-      si =
-       session_rules_table_lookup4 (&st->session_rules, sep->transport_proto,
-                                    &lcl4, &sep->ip.ip4, 0, sep->port);
-      if (si != SESSION_RULES_TABLE_INVALID_INDEX)
-       return si;
+      ai = session_rules_table_lookup4 (&st->session_rules,
+                                       sep->transport_proto, &lcl4,
+                                       &sep->ip.ip4, 0, sep->port);
+      if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
+       return session_lookup_action_to_session (ai);
     }
   else
     {
@@ -526,11 +532,11 @@ session_lookup_local_session_endpoint (u32 table_index,
        return (u32) kv6.value;
 
       memset (&lcl6, 0, sizeof (lcl6));
-      si =
-       session_rules_table_lookup6 (&st->session_rules, sep->transport_proto,
-                                    &lcl6, &sep->ip.ip6, 0, sep->port);
-      if (si != SESSION_RULES_TABLE_INVALID_INDEX)
-       return si;
+      ai = session_rules_table_lookup6 (&st->session_rules,
+                                       sep->transport_proto, &lcl6,
+                                       &sep->ip.ip6, 0, sep->port);
+      if (ai != SESSION_RULES_TABLE_INVALID_INDEX)
+       return session_lookup_action_to_session (ai);
     }
   return SESSION_INVALID_INDEX;
 }
@@ -1119,8 +1125,8 @@ vnet_session_rule_add_del (session_rule_add_del_args_t * args)
   if (args->scope & SESSION_RULE_SCOPE_LOCAL)
     {
       st = app_namespace_get_local_table (app_ns);
-      error =
-       session_rules_table_add_del (&st->session_rules, &args->table_args);
+      error = session_rules_table_add_del (&st->session_rules,
+                                          &args->table_args);
     }
   return error;
 }
@@ -1290,6 +1296,28 @@ VLIB_CLI_COMMAND (session_rule_command, static) =
 };
 /* *INDENT-ON* */
 
+void
+session_lookup_dump_rules_table (u32 fib_index, u8 fib_proto,
+                                u8 transport_proto)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  session_table_t *st;
+  st = session_table_get_for_fib_index (fib_index, fib_proto);
+  session_rules_table_cli_dump (vm, &st->session_rules, fib_proto,
+                               transport_proto);
+}
+
+void
+session_lookup_dump_local_rules_table (u32 table_index, u8 fib_proto,
+                                      u8 transport_proto)
+{
+  vlib_main_t *vm = vlib_get_main ();
+  session_table_t *st;
+  st = session_table_get (table_index);
+  session_rules_table_cli_dump (vm, &st->session_rules, fib_proto,
+                               transport_proto);
+}
+
 static clib_error_t *
 show_session_rules_command_fn (vlib_main_t * vm, unformat_input_t * input,
                               vlib_cli_command_t * cmd)