session: rules tables

This introduces 5-tuple lookup tables that may be used to implement
custom session layer actions at connection establishment time (session
layer perspective).

The rules table build mask-match-action lookup trees that for a given
5-tuple key return the action for the first longest match. If rules
overlap, ordering is established by tuple longest match with the
following descending priority: remote ip, local ip, remote port, local
port.

At this time, the only match action supported is to forward packets to
the application identified by the action.

Change-Id: Icbade6fac720fa3979820d50cd7d6137f8b635c3
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/session/session_api.c b/src/vnet/session/session_api.c
index 181312c..32ef34b 100755
--- a/src/vnet/session/session_api.c
+++ b/src/vnet/session/session_api.c
@@ -16,9 +16,10 @@
 #include <vnet/vnet.h>
 #include <vlibmemory/api.h>
 #include <vnet/session/application.h>
+#include <vnet/session/application_interface.h>
+#include <vnet/session/session_rules_table.h>
 
 #include <vnet/vnet_msg_enum.h>
-#include "application_interface.h"
 
 #define vl_typedefs		/* define message structures */
 #include <vnet/vnet_all_api_h.h>
@@ -52,6 +53,7 @@
 _(CONNECT_SOCK, connect_sock)                                          	\
 _(SESSION_ENABLE_DISABLE, session_enable_disable)                   	\
 _(APP_NAMESPACE_ADD_DEL, app_namespace_add_del)				\
+_(SESSION_RULE_ADD_DEL, session_rule_add_del)				\
 
 static int
 send_add_segment_callback (u32 api_client_index, const u8 * segment_name,
@@ -778,6 +780,42 @@
   REPLY_MACRO (VL_API_APP_NAMESPACE_ADD_DEL_REPLY);
 }
 
+static void
+vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp)
+{
+  vl_api_session_rule_add_del_reply_t *rmp;
+  session_rule_add_del_args_t args;
+  session_rule_table_add_del_args_t *table_args = &args.table_args;
+  clib_error_t *error;
+  u8 fib_proto;
+  int rv = 0;
+
+  fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;
+
+  table_args->lcl.fp_len = mp->lcl_plen;
+  table_args->lcl.fp_proto = fib_proto;
+  table_args->rmt.fp_len = mp->rmt_plen;
+  table_args->rmt.fp_proto = fib_proto;
+  table_args->lcl_port = clib_net_to_host_u16 (mp->lcl_port);
+  table_args->rmt_port = clib_net_to_host_u16 (mp->rmt_port);
+  table_args->action_index = clib_net_to_host_u32 (mp->action_index);
+  table_args->is_add = mp->is_add;
+  args.appns_index = clib_net_to_host_u32 (mp->appns_index);
+  args.scope = mp->scope;
+
+  memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr));
+  memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr));
+  ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4);
+  ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4);
+  error = vnet_session_rule_add_del (&args);
+  if (error)
+    {
+      rv = clib_error_get_code (error);
+      clib_error_report (error);
+    }
+  REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY);
+}
+
 static clib_error_t *
 application_reaper_cb (u32 client_index)
 {