LISP: add API handlers for set/get transport protocol

Change-Id: Ib675164c475edcdbe3013df7b847adf5e050c53f
Signed-off-by: Filip Tehlar <ftehlar@cisco.com>
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index 520ae4f..0230021 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -1752,6 +1752,64 @@
   vam->result_ready = 1;
 }
 
+u8 *
+format_lisp_transport_protocol (u8 * s, va_list * args)
+{
+  u32 proto = va_arg (*args, u32);
+
+  switch (proto)
+    {
+    case 1:
+      return format (s, "udp");
+    case 2:
+      return format (s, "api");
+    default:
+      return 0;
+    }
+  return 0;
+}
+
+static void vl_api_one_get_transport_protocol_reply_t_handler
+  (vl_api_one_get_transport_protocol_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  i32 retval = ntohl (mp->retval);
+  if (vam->async_mode)
+    {
+      vam->async_errors += (retval < 0);
+    }
+  else
+    {
+      u32 proto = mp->protocol;
+      print (vam->ofp, "Transport protocol: %U",
+	     format_lisp_transport_protocol, proto);
+      vam->retval = retval;
+      vam->result_ready = 1;
+    }
+}
+
+static void vl_api_one_get_transport_protocol_reply_t_handler_json
+  (vl_api_one_get_transport_protocol_reply_t * mp)
+{
+  vat_main_t *vam = &vat_main;
+  vat_json_node_t node;
+  u8 *s;
+
+  s = format (0, "%U", format_lisp_transport_protocol, mp->protocol);
+  vec_add1 (s, 0);
+
+  vat_json_init_object (&node);
+  vat_json_object_add_int (&node, "retval", ntohl (mp->retval));
+  vat_json_object_add_string_copy (&node, "transport-protocol", s);
+
+  vec_free (s);
+  vat_json_print (vam->ofp, &node);
+  vat_json_free (&node);
+
+  vam->retval = ntohl (mp->retval);
+  vam->result_ready = 1;
+}
+
 static void vl_api_one_add_del_locator_set_reply_t_handler
   (vl_api_one_add_del_locator_set_reply_t * mp)
 {
@@ -4818,6 +4876,7 @@
 _(one_rloc_probe_enable_disable_reply)                  \
 _(one_map_register_enable_disable_reply)                \
 _(one_map_register_set_ttl_reply)                       \
+_(one_set_transport_protocol_reply)                     \
 _(one_map_register_fallback_threshold_reply)            \
 _(one_pitr_set_locator_set_reply)                       \
 _(one_map_request_mode_reply)                           \
@@ -5050,6 +5109,8 @@
 _(ONE_MAP_REGISTER_ENABLE_DISABLE_REPLY,                                \
   one_map_register_enable_disable_reply)                                \
 _(ONE_MAP_REGISTER_SET_TTL_REPLY, one_map_register_set_ttl_reply)       \
+_(ONE_SET_TRANSPORT_PROTOCOL_REPLY, one_set_transport_protocol_reply)   \
+_(ONE_GET_TRANSPORT_PROTOCOL_REPLY, one_get_transport_protocol_reply)   \
 _(ONE_MAP_REGISTER_FALLBACK_THRESHOLD_REPLY,                            \
   one_map_register_fallback_threshold_reply)                            \
 _(ONE_RLOC_PROBE_ENABLE_DISABLE_REPLY,                                  \
@@ -16598,6 +16659,75 @@
   return ret;
 }
 
+uword
+unformat_lisp_transport_protocol (unformat_input_t * input, va_list * args)
+{
+  u32 *proto = va_arg (*args, u32 *);
+
+  if (unformat (input, "udp"))
+    *proto = 1;
+  else if (unformat (input, "api"))
+    *proto = 2;
+  else
+    return 0;
+
+  return 1;
+}
+
+static int
+api_one_set_transport_protocol (vat_main_t * vam)
+{
+  unformat_input_t *input = vam->input;
+  vl_api_one_set_transport_protocol_t *mp;
+  u8 is_set = 0;
+  u32 protocol = 0;
+  int ret;
+
+  /* Parse args required to build the message */
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "%U", unformat_lisp_transport_protocol, &protocol))
+	is_set = 1;
+      else
+	{
+	  clib_warning ("parse error '%U'", format_unformat_error, input);
+	  return -99;
+	}
+    }
+
+  if (!is_set)
+    {
+      errmsg ("Transport protocol missing!");
+      return -99;
+    }
+
+  M (ONE_SET_TRANSPORT_PROTOCOL, mp);
+  mp->protocol = (u8) protocol;
+
+  /* send it... */
+  S (mp);
+
+  /* Wait for a reply... */
+  W (ret);
+  return ret;
+}
+
+static int
+api_one_get_transport_protocol (vat_main_t * vam)
+{
+  vl_api_one_get_transport_protocol_t *mp;
+  int ret;
+
+  M (ONE_GET_TRANSPORT_PROTOCOL, mp);
+
+  /* send it... */
+  S (mp);
+
+  /* Wait for a reply... */
+  W (ret);
+  return ret;
+}
+
 static int
 api_one_map_register_set_ttl (vat_main_t * vam)
 {
@@ -20662,6 +20792,8 @@
 _(one_stats_flush, "")                                                  \
 _(one_get_map_request_itr_rlocs, "")                                    \
 _(one_map_register_set_ttl, "<ttl>")                                    \
+_(one_set_transport_protocol, "udp|api")                                \
+_(one_get_transport_protocol, "")                                       \
 _(show_one_nsh_mapping, "")                                             \
 _(show_one_pitr, "")                                                    \
 _(show_one_use_petr, "")                                                \
diff --git a/src/vnet/lisp-cp/control.c b/src/vnet/lisp-cp/control.c
index 42b5b8b..0d6d453 100644
--- a/src/vnet/lisp-cp/control.c
+++ b/src/vnet/lisp-cp/control.c
@@ -4351,6 +4351,7 @@
   lcm->map_register_ttl = MAP_REGISTER_DEFAULT_TTL;
   lcm->max_expired_map_registers = MAX_EXPIRED_MAP_REGISTERS_DEFAULT;
   lcm->expired_map_registers = 0;
+  lcm->transport_protocol = LISP_TRANSPORT_PROTOCOL_UDP;
   return 0;
 }
 
@@ -4720,6 +4721,26 @@
 };
 /* *INDENT-ON* */
 
+u32
+vnet_lisp_set_transport_protocol (u8 protocol)
+{
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+
+  if (protocol < LISP_TRANSPORT_PROTOCOL_UDP ||
+      protocol > LISP_TRANSPORT_PROTOCOL_API)
+    return VNET_API_ERROR_INVALID_ARGUMENT;
+
+  lcm->transport_protocol = protocol;
+  return 0;
+}
+
+lisp_transport_protocol_t
+vnet_lisp_get_transport_protocol (void)
+{
+  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
+  return lcm->transport_protocol;
+}
+
 VLIB_INIT_FUNCTION (lisp_cp_init);
 
 /*
diff --git a/src/vnet/lisp-cp/control.h b/src/vnet/lisp-cp/control.h
index 0df18f4..a6da818 100644
--- a/src/vnet/lisp-cp/control.h
+++ b/src/vnet/lisp-cp/control.h
@@ -140,6 +140,12 @@
   u32 bd;
 } lisp_l2_arp_key_t;
 
+typedef enum
+{
+  LISP_TRANSPORT_PROTOCOL_UDP = 1,
+  LISP_TRANSPORT_PROTOCOL_API
+} lisp_transport_protocol_t;
+
 typedef struct
 {
   u64 nonce;
@@ -271,6 +277,9 @@
   u32 max_expired_map_registers;
   u32 expired_map_registers;
 
+  /** either UDP based or binary API. Default is UDP */
+  lisp_transport_protocol_t transport_protocol;
+
   /* commodity */
   ip4_main_t *im4;
   ip6_main_t *im6;
@@ -404,6 +413,8 @@
 u32 vnet_lisp_map_register_fallback_threshold_get (void);
 u32 *vnet_lisp_ndp_bds_get (void);
 lisp_api_ndp_entry_t *vnet_lisp_ndp_entries_get_by_bd (u32 bd);
+u32 vnet_lisp_set_transport_protocol (u8 protocol);
+lisp_transport_protocol_t vnet_lisp_get_transport_protocol (void);
 
 map_records_arg_t *parse_map_reply (vlib_buffer_t * b);
 
diff --git a/src/vnet/lisp-cp/one_api.c b/src/vnet/lisp-cp/one_api.c
index ae52381..e3a2afe 100644
--- a/src/vnet/lisp-cp/one_api.c
+++ b/src/vnet/lisp-cp/one_api.c
@@ -133,6 +133,8 @@
 _(ONE_ADD_DEL_NDP_ENTRY, one_add_del_ndp_entry)                         \
 _(ONE_NDP_BD_GET, one_ndp_bd_get)                                       \
 _(ONE_NDP_ENTRIES_GET, one_ndp_entries_get)                             \
+_(ONE_SET_TRANSPORT_PROTOCOL, one_set_transport_protocol)               \
+_(ONE_GET_TRANSPORT_PROTOCOL, one_get_transport_protocol)
 
 static locator_t *
 unformat_one_locs (vl_api_one_remote_locator_t * rmt_locs, u32 rloc_num)
@@ -1698,6 +1700,34 @@
 }
 
 static void
+  vl_api_one_set_transport_protocol_t_handler
+  (vl_api_one_set_transport_protocol_t * mp)
+{
+  vl_api_one_set_transport_protocol_reply_t *rmp;
+  int rv = 0;
+
+  rv = vnet_lisp_set_transport_protocol (mp->protocol);
+
+  REPLY_MACRO (VL_API_ONE_SET_TRANSPORT_PROTOCOL_REPLY);
+}
+
+static void
+  vl_api_one_get_transport_protocol_t_handler
+  (vl_api_one_get_transport_protocol_t * mp)
+{
+  vl_api_one_get_transport_protocol_reply_t *rmp;
+  int rv = 0;
+  u8 proto = (u8) vnet_lisp_get_transport_protocol ();
+
+  /* *INDENT-OFF* */
+  REPLY_MACRO2 (VL_API_ONE_GET_TRANSPORT_PROTOCOL_REPLY,
+  ({
+    rmp->protocol = proto;
+  }));
+  /* *INDENT-ON* */
+}
+
+static void
 vl_api_one_ndp_entries_get_t_handler (vl_api_one_ndp_entries_get_t * mp)
 {
   vl_api_one_ndp_entries_get_reply_t *rmp = 0;