session: segment manager refactor
[vpp.git] / src / vnet / session / session_test.c
index bf3cea2..febe1b7 100644 (file)
@@ -98,12 +98,49 @@ static session_cb_vft_t dummy_session_cbs = {
 };
 /* *INDENT-ON* */
 
+static int
+session_create_lookpback (u32 table_id, u32 * sw_if_index,
+                         ip4_address_t * intf_addr)
+{
+  u8 intf_mac[6];
+
+  memset (intf_mac, 0, sizeof (intf_mac));
+
+  if (vnet_create_loopback_interface (sw_if_index, intf_mac, 0, 0))
+    {
+      clib_warning ("couldn't create loopback. stopping the test!");
+      return -1;
+    }
+
+  if (table_id != 0)
+    ip_table_bind (FIB_PROTOCOL_IP4, *sw_if_index, table_id, 0);
+
+  vnet_sw_interface_set_flags (vnet_get_main (), *sw_if_index,
+                              VNET_SW_INTERFACE_FLAG_ADMIN_UP);
+
+  if (ip4_add_del_interface_address (vlib_get_main (), *sw_if_index,
+                                    intf_addr, 24, 0))
+    {
+      clib_warning ("couldn't assign loopback ip %U", format_ip4_address,
+                   intf_addr);
+      return -1;
+    }
+
+  return 0;
+}
+
+static void
+session_delete_loopback (u32 sw_if_index)
+{
+  /* fails spectacularly  */
+  /* vnet_delete_loopback_interface (sw_if_index); */
+}
+
 static int
 session_test_basic (vlib_main_t * vm, unformat_input_t * input)
 {
   session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
-  u64 options[SESSION_OPTIONS_N_OPTIONS], bind4_handle, bind6_handle;
-  u8 segment_name[128];
+  u64 options[APP_OPTIONS_N_OPTIONS], bind4_handle, bind6_handle;
   clib_error_t *error = 0;
   u32 server_index;
 
@@ -117,7 +154,6 @@ session_test_basic (vlib_main_t * vm, unformat_input_t * input)
     .options = options,
     .namespace_id = 0,
     .session_cb_vft = &dummy_session_cbs,
-    .segment_name = segment_name,
   };
 
   error = vnet_application_attach (&attach_args);
@@ -167,7 +203,7 @@ session_test_basic (vlib_main_t * vm, unformat_input_t * input)
 static int
 session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
 {
-  u64 options[SESSION_OPTIONS_N_OPTIONS], dummy_secret = 1234;
+  u64 options[APP_OPTIONS_N_OPTIONS], dummy_secret = 1234;
   u32 server_index, server_st_index, server_local_st_index;
   u32 dummy_port = 1234, local_listener, client_index;
   u32 dummy_api_context = 4321, dummy_client_api_index = 1234;
@@ -176,9 +212,8 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   session_endpoint_t client_sep = SESSION_ENDPOINT_NULL;
   session_endpoint_t intf_sep = SESSION_ENDPOINT_NULL;
   clib_error_t *error = 0;
-  u8 *ns_id = format (0, "appns1"), intf_mac[6];
+  u8 *ns_id = format (0, "appns1");
   app_namespace_t *app_ns;
-  u8 segment_name[128];
   application_t *server;
   stream_session_t *s;
   int code;
@@ -188,7 +223,6 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   client_sep.is_ip4 = 1;
   client_sep.port = dummy_port;
   memset (options, 0, sizeof (options));
-  memset (intf_mac, 0, sizeof (intf_mac));
 
   options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
@@ -197,7 +231,6 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
     .options = options,
     .namespace_id = 0,
     .session_cb_vft = &dummy_session_cbs,
-    .segment_name = segment_name,
   };
 
   vnet_bind_args_t bind_args = {
@@ -444,15 +477,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
   /*
    * Create loopback interface
    */
-  if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0))
-    {
-      clib_warning ("couldn't create loopback. stopping the test!");
-      return 0;
-    }
-  vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
-                              VNET_SW_INTERFACE_FLAG_ADMIN_UP);
-  ip4_add_del_interface_address (vlib_get_main (), sw_if_index, &intf_addr,
-                                24, 0);
+  session_create_lookpback (0, &sw_if_index, &intf_addr);
 
   /*
    * Update namespace
@@ -496,8 +521,7 @@ session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
    * Cleanup
    */
   vec_free (ns_id);
-  /* fails in multi core scenarions .. */
-  /* vnet_delete_loopback_interface (sw_if_index); */
+  session_delete_loopback (sw_if_index);
   return 0;
 }
 
@@ -774,14 +798,14 @@ static int
 session_test_rules (vlib_main_t * vm, unformat_input_t * input)
 {
   session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
-  u64 options[SESSION_OPTIONS_N_OPTIONS];
+  u64 options[APP_OPTIONS_N_OPTIONS];
   u16 lcl_port = 1234, rmt_port = 4321;
   u32 server_index, server_index2, app_index;
   u32 dummy_server_api_index = ~0;
   transport_connection_t *tc;
   u32 dummy_port = 1111;
   clib_error_t *error = 0;
-  u8 segment_name[128], is_filtered = 0, *ns_id = format (0, "appns1");
+  u8 is_filtered = 0, *ns_id = format (0, "appns1");
   stream_session_t *listener, *s;
   app_namespace_t *default_ns = app_namespace_get_default ();
   u32 local_ns_index = default_ns->local_table_index;
@@ -809,7 +833,6 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
     .options = options,
     .namespace_id = 0,
     .session_cb_vft = &dummy_session_cbs,
-    .segment_name = segment_name,
   };
 
   vnet_bind_args_t bind_args = {
@@ -1019,8 +1042,6 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
 
   sep.ip.ip4.as_u32 -= 1 << 24;
 
-
-
   /*
    * Delete masking rule: 1.2.3.4/32 1234 5.6.7.8/32 4321 allow
    */
@@ -1136,7 +1157,7 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
                " not work (no-rule)");
 
   /*
-   * Test tags. Add/del rule with tag
+   * Test tags. Add/overwrite/del rule with tag
    */
   args.table_args.is_add = 1;
   args.table_args.lcl_port = 1234;
@@ -1164,9 +1185,28 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
   SESSION_TEST ((tc->c_index == listener->connection_index),
                "lookup 1.2.3.4/32 1234 5.6.7.8/16 4321 should work");
 
+  vec_free (args.table_args.tag);
+  args.table_args.lcl_port = 1234;
+  args.table_args.lcl.fp_addr.ip4 = lcl_ip;
+  args.table_args.lcl.fp_len = 16;
+  args.table_args.tag = format (0, "test_rule_overwrite");
+  error = vnet_session_rule_add_del (&args);
+  SESSION_TEST ((error == 0),
+               "Overwrite 1.2.3.4/16 1234 5.6.7.8/16 4321 deny tag test_rule"
+               " should work");
+  if (verbose)
+    {
+      session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4,
+                                      TRANSPORT_PROTO_TCP);
+      session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4,
+                                            TRANSPORT_PROTO_TCP);
+    }
+
   args.table_args.is_add = 0;
   args.table_args.lcl_port += 1;
   error = vnet_session_rule_add_del (&args);
+  SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 deny "
+               "tag %v", args.table_args.tag);
   if (verbose)
     {
       session_lookup_dump_rules_table (0, FIB_PROTOCOL_IP4,
@@ -1174,8 +1214,6 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
       session_lookup_dump_local_rules_table (local_ns_index, FIB_PROTOCOL_IP4,
                                             TRANSPORT_PROTO_TCP);
     }
-  SESSION_TEST ((error == 0), "Del 1.2.3.4/32 1234 5.6.7.8/32 4321 deny "
-               "tag test_rule");
   tc = session_lookup_connection_wt4 (0, &lcl_pref.fp_addr.ip4,
                                      &rmt_pref.fp_addr.ip4, lcl_port,
                                      rmt_port, TRANSPORT_PROTO_TCP, 0,
@@ -1318,14 +1356,14 @@ session_test_rules (vlib_main_t * vm, unformat_input_t * input)
 static int
 session_test_proxy (vlib_main_t * vm, unformat_input_t * input)
 {
-  u64 options[SESSION_OPTIONS_N_OPTIONS];
+  u64 options[APP_OPTIONS_N_OPTIONS];
   char *show_listeners = "sh session listeners tcp verbose";
   char *show_local_listeners = "sh app ns table default";
   unformat_input_t tmp_input;
   u32 server_index, app_index;
   u32 dummy_server_api_index = ~0, sw_if_index = 0;
   clib_error_t *error = 0;
-  u8 segment_name[128], intf_mac[6], sst, is_filtered = 0;
+  u8 sst, is_filtered = 0;
   stream_session_t *s;
   transport_connection_t *tc;
   u16 lcl_port = 1234, rmt_port = 4321;
@@ -1365,21 +1403,13 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input)
   /*
    * Create loopback interface
    */
-  memset (intf_mac, 0, sizeof (intf_mac));
-  if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0))
-    {
-      clib_warning ("couldn't create loopback. stopping the test!");
-      return 0;
-    }
-  vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
-                              VNET_SW_INTERFACE_FLAG_ADMIN_UP);
-  ip4_add_del_interface_address (vlib_get_main (), sw_if_index, &lcl_ip,
-                                24, 0);
+  session_create_lookpback (0, &sw_if_index, &lcl_ip);
 
   app_ns = app_namespace_get_default ();
   app_ns->sw_if_index = sw_if_index;
 
   memset (options, 0, sizeof (options));
+  options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN;
   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_IS_PROXY;
   options[APP_OPTIONS_PROXY_TRANSPORT] = 1 << TRANSPORT_PROTO_TCP;
@@ -1390,7 +1420,6 @@ session_test_proxy (vlib_main_t * vm, unformat_input_t * input)
     .options = options,
     .namespace_id = 0,
     .session_cb_vft = &dummy_session_cbs,
-    .segment_name = segment_name,
   };
 
   attach_args.api_client_index = dummy_server_api_index;