summaryrefslogtreecommitdiffstats
path: root/src/vnet/devices/netmap/netmap.c
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2016-12-19 23:05:39 +0100
committerDamjan Marion <damarion@cisco.com>2016-12-28 12:25:14 +0100
commit7cd468a3d7dee7d6c92f69a0bb7061ae208ec727 (patch)
tree5de62f8dbd3a752f5a676ca600e43d2652d1ff1a /src/vnet/devices/netmap/netmap.c
parent696f1adec0df3b8f161862566dd9c86174302658 (diff)
Reorganize source tree to use single autotools instance
Change-Id: I7b51f88292e057c6443b12224486f2d0c9f8ae23 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'src/vnet/devices/netmap/netmap.c')
-rw-r--r--src/vnet/devices/netmap/netmap.c316
1 files changed, 316 insertions, 0 deletions
diff --git a/src/vnet/devices/netmap/netmap.c b/src/vnet/devices/netmap/netmap.c
new file mode 100644
index 00000000000..3bdb442dda2
--- /dev/null
+++ b/src/vnet/devices/netmap/netmap.c
@@ -0,0 +1,316 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2016 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <stdint.h>
+#include <net/if.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <vnet/devices/netmap/net_netmap.h>
+
+#include <vlib/vlib.h>
+#include <vlib/unix/unix.h>
+#include <vnet/ethernet/ethernet.h>
+#include <vnet/devices/netmap/netmap.h>
+
+static u32
+netmap_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi,
+ u32 flags)
+{
+ /* nothing for now */
+ return 0;
+}
+
+static clib_error_t *
+netmap_fd_read_ready (unix_file_t * uf)
+{
+ vlib_main_t *vm = vlib_get_main ();
+ netmap_main_t *nm = &netmap_main;
+ u32 idx = uf->private_data;
+
+ nm->pending_input_bitmap =
+ clib_bitmap_set (nm->pending_input_bitmap, idx, 1);
+
+ /* Schedule the rx node */
+ vlib_node_set_interrupt_pending (vm, netmap_input_node.index);
+
+ return 0;
+}
+
+static void
+close_netmap_if (netmap_main_t * nm, netmap_if_t * nif)
+{
+ if (nif->unix_file_index != ~0)
+ {
+ unix_file_del (&unix_main, unix_main.file_pool + nif->unix_file_index);
+ nif->unix_file_index = ~0;
+ }
+ else if (nif->fd > -1)
+ close (nif->fd);
+
+ if (nif->mem_region)
+ {
+ netmap_mem_region_t *reg = &nm->mem_regions[nif->mem_region];
+ if (--reg->refcnt == 0)
+ {
+ munmap (reg->mem, reg->region_size);
+ reg->region_size = 0;
+ }
+ }
+
+
+ mhash_unset (&nm->if_index_by_host_if_name, nif->host_if_name,
+ &nif->if_index);
+ vec_free (nif->host_if_name);
+ vec_free (nif->req);
+
+ memset (nif, 0, sizeof (*nif));
+ pool_put (nm->interfaces, nif);
+}
+
+int
+netmap_worker_thread_enable ()
+{
+ /* if worker threads are enabled, switch to polling mode */
+ foreach_vlib_main ((
+ {
+ vlib_node_set_state (this_vlib_main,
+ netmap_input_node.index,
+ VLIB_NODE_STATE_POLLING);
+ }));
+
+ return 0;
+}
+
+int
+netmap_worker_thread_disable ()
+{
+ foreach_vlib_main ((
+ {
+ vlib_node_set_state (this_vlib_main,
+ netmap_input_node.index,
+ VLIB_NODE_STATE_INTERRUPT);
+ }));
+
+ return 0;
+}
+
+int
+netmap_create_if (vlib_main_t * vm, u8 * if_name, u8 * hw_addr_set,
+ u8 is_pipe, u8 is_master, u32 * sw_if_index)
+{
+ netmap_main_t *nm = &netmap_main;
+ int ret = 0;
+ netmap_if_t *nif = 0;
+ u8 hw_addr[6];
+ clib_error_t *error = 0;
+ vnet_sw_interface_t *sw;
+ vnet_main_t *vnm = vnet_get_main ();
+ uword *p;
+ struct nmreq *req = 0;
+ netmap_mem_region_t *reg;
+ vlib_thread_main_t *tm = vlib_get_thread_main ();
+ int fd;
+
+ p = mhash_get (&nm->if_index_by_host_if_name, if_name);
+ if (p)
+ return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
+
+ fd = open ("/dev/netmap", O_RDWR);
+ if (fd < 0)
+ return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
+
+ pool_get (nm->interfaces, nif);
+ nif->if_index = nif - nm->interfaces;
+ nif->fd = fd;
+ nif->unix_file_index = ~0;
+
+ vec_validate (req, 0);
+ nif->req = req;
+ req->nr_version = NETMAP_API;
+ req->nr_flags = NR_REG_ALL_NIC;
+
+ if (is_pipe)
+ req->nr_flags = is_master ? NR_REG_PIPE_MASTER : NR_REG_PIPE_SLAVE;
+ else
+ req->nr_flags = NR_REG_ALL_NIC;
+
+ req->nr_flags |= NR_ACCEPT_VNET_HDR;
+ snprintf (req->nr_name, IFNAMSIZ, "%s", if_name);
+ req->nr_name[IFNAMSIZ - 1] = 0;
+
+ if (ioctl (nif->fd, NIOCREGIF, req))
+ {
+ ret = VNET_API_ERROR_NOT_CONNECTED;
+ goto error;
+ }
+
+ nif->mem_region = req->nr_arg2;
+ vec_validate (nm->mem_regions, nif->mem_region);
+ reg = &nm->mem_regions[nif->mem_region];
+ if (reg->region_size == 0)
+ {
+ reg->mem = mmap (NULL, req->nr_memsize, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ clib_warning ("mem %p", reg->mem);
+ if (reg->mem == MAP_FAILED)
+ {
+ ret = VNET_API_ERROR_NOT_CONNECTED;
+ goto error;
+ }
+ reg->region_size = req->nr_memsize;
+ }
+ reg->refcnt++;
+
+ nif->nifp = NETMAP_IF (reg->mem, req->nr_offset);
+ nif->first_rx_ring = 0;
+ nif->last_rx_ring = 0;
+ nif->first_tx_ring = 0;
+ nif->last_tx_ring = 0;
+ nif->host_if_name = if_name;
+ nif->per_interface_next_index = ~0;
+
+ if (tm->n_vlib_mains > 1)
+ {
+ nif->lockp = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
+ CLIB_CACHE_LINE_BYTES);
+ memset ((void *) nif->lockp, 0, CLIB_CACHE_LINE_BYTES);
+ }
+
+ {
+ unix_file_t template = { 0 };
+ template.read_function = netmap_fd_read_ready;
+ template.file_descriptor = nif->fd;
+ template.private_data = nif->if_index;
+ nif->unix_file_index = unix_file_add (&unix_main, &template);
+ }
+
+ /*use configured or generate random MAC address */
+ if (hw_addr_set)
+ memcpy (hw_addr, hw_addr_set, 6);
+ else
+ {
+ f64 now = vlib_time_now (vm);
+ u32 rnd;
+ rnd = (u32) (now * 1e6);
+ rnd = random_u32 (&rnd);
+
+ memcpy (hw_addr + 2, &rnd, sizeof (rnd));
+ hw_addr[0] = 2;
+ hw_addr[1] = 0xfe;
+ }
+
+ error = ethernet_register_interface (vnm, netmap_device_class.index,
+ nif->if_index, hw_addr,
+ &nif->hw_if_index,
+ netmap_eth_flag_change);
+
+ if (error)
+ {
+ clib_error_report (error);
+ ret = VNET_API_ERROR_SYSCALL_ERROR_1;
+ goto error;
+ }
+
+ sw = vnet_get_hw_sw_interface (vnm, nif->hw_if_index);
+ nif->sw_if_index = sw->sw_if_index;
+
+ mhash_set_mem (&nm->if_index_by_host_if_name, if_name, &nif->if_index, 0);
+
+ if (sw_if_index)
+ *sw_if_index = nif->sw_if_index;
+
+ if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 1)
+ netmap_worker_thread_enable ();
+
+ return 0;
+
+error:
+ close_netmap_if (nm, nif);
+ return ret;
+}
+
+int
+netmap_delete_if (vlib_main_t * vm, u8 * host_if_name)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ netmap_main_t *nm = &netmap_main;
+ netmap_if_t *nif;
+ uword *p;
+ vlib_thread_main_t *tm = vlib_get_thread_main ();
+
+ p = mhash_get (&nm->if_index_by_host_if_name, host_if_name);
+ if (p == NULL)
+ {
+ clib_warning ("Host interface %s does not exist", host_if_name);
+ return VNET_API_ERROR_SYSCALL_ERROR_1;
+ }
+ nif = pool_elt_at_index (nm->interfaces, p[0]);
+
+ /* bring down the interface */
+ vnet_hw_interface_set_flags (vnm, nif->hw_if_index, 0);
+
+ ethernet_delete_interface (vnm, nif->hw_if_index);
+
+ close_netmap_if (nm, nif);
+
+ if (tm->n_vlib_mains > 1 && pool_elts (nm->interfaces) == 0)
+ netmap_worker_thread_disable ();
+
+ return 0;
+}
+
+static clib_error_t *
+netmap_init (vlib_main_t * vm)
+{
+ netmap_main_t *nm = &netmap_main;
+ vlib_thread_main_t *tm = vlib_get_thread_main ();
+ vlib_thread_registration_t *tr;
+ uword *p;
+
+ memset (nm, 0, sizeof (netmap_main_t));
+
+ nm->input_cpu_first_index = 0;
+ nm->input_cpu_count = 1;
+
+ /* find out which cpus will be used for input */
+ p = hash_get_mem (tm->thread_registrations_by_name, "workers");
+ tr = p ? (vlib_thread_registration_t *) p[0] : 0;
+
+ if (tr && tr->count > 0)
+ {
+ nm->input_cpu_first_index = tr->first_index;
+ nm->input_cpu_count = tr->count;
+ }
+
+ mhash_init_vec_string (&nm->if_index_by_host_if_name, sizeof (uword));
+
+ vec_validate_aligned (nm->rx_buffers, tm->n_vlib_mains - 1,
+ CLIB_CACHE_LINE_BYTES);
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (netmap_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
/*
 *------------------------------------------------------------------
 * api_format.c
 *
 * Copyright (c) 2014-2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */

#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>
#include <vnet/ipsec/ipsec_sa.h>
#include <plugins/ikev2/ikev2.h>
#include <vnet/ip/ip_types_api.h>

#define __plugin_msg_base ikev2_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>

/* Declare message IDs */
#include <vnet/format_fns.h>
#include <ikev2/ikev2.api_enum.h>
#include <ikev2/ikev2.api_types.h>
#include <vpp/api/vpe.api_types.h>

#define vl_endianfun		/* define message structures */
#include <plugins/ikev2/ikev2.api.h>
#undef vl_endianfun

typedef struct
{
  /* API message ID base */
  u16 msg_id_base;
  u32 ping_id;
  vat_main_t *vat_main;
} ikev2_test_main_t;

ikev2_test_main_t ikev2_test_main;

uword
unformat_ikev2_auth_method (unformat_input_t * input, va_list * args)
{
  u32 *r = va_arg (*args, u32 *);

  if (0);
#define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_AUTH_METHOD_##f;
  foreach_ikev2_auth_method
#undef _
    else
    return 0;
  return 1;
}


uword
unformat_ikev2_id_type (unformat_input_t * input, va_list * args)
{
  u32 *r = va_arg (*args, u32 *);

  if (0);
#define _(v,f,s) else if (unformat (input, s)) *r = IKEV2_ID_TYPE_##f;
  foreach_ikev2_id_type
#undef _
    else
    return 0;
  return 1;
}

#define MACRO_FORMAT(lc)                                \
u8 * format_ikev2_##lc (u8 * s, va_list * args)         \
{                                                       \
  u32 i = va_arg (*args, u32);                          \
  char * t = 0;                                         \
  switch (i) {                                          \
        foreach_ikev2_##lc                              \
      default:                                          \
        return format (s, "unknown (%u)", i);           \
    }                                                   \
  s = format (s, "%s", t);                              \
  return s;                                             \
}

#define _(v,f,str) case IKEV2_AUTH_METHOD_##f: t = str; break;
MACRO_FORMAT (auth_method)
#undef _
#define _(v,f,str) case IKEV2_ID_TYPE_##f: t = str; break;
  MACRO_FORMAT (id_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_ENCR_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_encr_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_PRF_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_prf_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_INTEG_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_integ_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_DH_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_dh_type)
#undef _
#define _(v,f,str) case IKEV2_TRANSFORM_ESN_TYPE_##f: t = str; break;
  MACRO_FORMAT (transform_esn_type)
#undef _
     u8 *format_ikev2_id_type_and_data (u8 * s, va_list * args)
{
  vl_api_ikev2_id_t *id = va_arg (*args, vl_api_ikev2_id_t *);

  if (id->type == 0)
    return format (s, "none");

  s = format (s, "%U", format_ikev2_id_type, id->type);

  switch (id->type)
    {
    case 0:
      return format (s, "none");
    case IKEV2_ID_TYPE_ID_FQDN:
      s = format (s, " %s", id->data);
      break;
    case IKEV2_ID_TYPE_ID_RFC822_ADDR:
      s = format (s, " %s", id->data);
      break;
    case IKEV2_ID_TYPE_ID_IPV4_ADDR:
      s = format (s, " %U", format_ip_address, id->data);
      break;
    case IKEV2_ID_TYPE_ID_KEY_ID:
      s = format (s, " 0x%U", format_hex_bytes, id->data, id->data_len);
      break;
    default:
      s = format (s, " %s", id->data);
    }

  return s;
}

u8 *
format_ikev2_sa_transform (u8 * s, va_list * args)
{
  vl_api_ikev2_sa_transform_t *tr =
    va_arg (*args, vl_api_ikev2_sa_transform_t *);

  if (!tr)
    return s;

  if (tr->transform_type >= IKEV2_TRANSFORM_NUM_TYPES)
    return s;

  s = format (s, "%U:", format_ikev2_transform_type, tr->transform_type);

  switch (tr->transform_type)
    {
    case IKEV2_TRANSFORM_TYPE_ENCR:
      s =
	format (s, "%U", format_ikev2_transform_encr_type, tr->transform_id);
      break;
    case IKEV2_TRANSFORM_TYPE_PRF:
      s = format (s, "%U", format_ikev2_transform_prf_type, tr->transform_id);
      break;
    case IKEV2_TRANSFORM_TYPE_INTEG:
      s =
	format (s, "%U", format_ikev2_transform_integ_type, tr->transform_id);
      break;
    case IKEV2_TRANSFORM_TYPE_DH:
      s = format (s, "%U", format_ikev2_transform_dh_type, tr->transform_id);
      break;
    case IKEV2_TRANSFORM_TYPE_ESN:
      s = format (s, "%U", format_ikev2_transform_esn_type, tr->transform_id);
      break;
    default:
      break;
    }

  if (tr->transform_type == IKEV2_TRANSFORM_TYPE_ENCR &&
      tr->transform_id == IKEV2_TRANSFORM_ENCR_TYPE_AES_CBC && tr->key_len)
    s = format (s, "-%u", tr->key_len * 8);

  return s;
}

static int
api_ikev2_profile_dump (vat_main_t * vam)
{
  ikev2_test_main_t *ik = &ikev2_test_main;
  vl_api_ikev2_profile_dump_t *mp;
  vl_api_control_ping_t *mp_ping;
  int ret;

  /* Construct the API message */
  M (IKEV2_PROFILE_DUMP, mp);

  /* send it... */
  S (mp);

  /* Use a control ping for synchronization */
  if (!ik->ping_id)
    ik->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (ik->ping_id);
  mp_ping->client_index = vam->my_client_index;

  vam->result_ready = 0;
  S (mp_ping);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void vl_api_ikev2_profile_details_t_handler
  (vl_api_ikev2_profile_details_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  vl_api_ikev2_profile_t *p = &mp->profile;

  fformat (vam->ofp, "profile %s\n", p->name);

  if (p->auth.method)
    {
      if (p->auth.hex)
	fformat (vam->ofp, "  auth-method %U auth data 0x%U\n",
		 format_ikev2_auth_method, p->auth.method,
		 format_hex_bytes, p->auth.data,
		 clib_net_to_host_u32 (p->auth.data_len));
      else
	fformat (vam->ofp, "  auth-method %U auth data %v\n",
		 format_ikev2_auth_method, p->auth.method, format (0,
								   "%s",
								   p->
								   auth.data));
    }

  if (p->loc_id.type)
    {
      fformat (vam->ofp, "  local id-type data %U\n",
	       format_ikev2_id_type_and_data, &p->loc_id);
    }

  if (p->rem_id.type)
    {
      fformat (vam->ofp, "  remote id-type data %U\n",
	       format_ikev2_id_type_and_data, &p->rem_id);
    }

  fformat (vam->ofp, "  local traffic-selector addr %U - %U port %u - %u"
	   " protocol %u\n",
	   format_ip_address, &p->loc_ts.start_addr,
	   format_ip_address, &p->loc_ts.end_addr,
	   clib_net_to_host_u16 (p->loc_ts.start_port),
	   clib_net_to_host_u16 (p->loc_ts.end_port), p->loc_ts.protocol_id);

  fformat (vam->ofp, "  remote traffic-selector addr %U - %U port %u - %u"
	   " protocol %u\n",
	   format_ip_address, &p->rem_ts.start_addr,
	   format_ip_address, &p->rem_ts.end_addr,
	   clib_net_to_host_u16 (p->rem_ts.start_port),
	   clib_net_to_host_u16 (p->rem_ts.end_port), p->rem_ts.protocol_id);
  u32 tun_itf = clib_net_to_host_u32 (p->tun_itf);
  if (~0 != tun_itf)
    fformat (vam->ofp, "  protected tunnel idx %d\n", tun_itf);

  u32 sw_if_index = clib_net_to_host_u32 (p->responder.sw_if_index);
  if (~0 != sw_if_index)
    fformat (vam->ofp, "  responder idx %d %U\n",
	     sw_if_index, format_ip_address, &p->responder.addr);

  if (p->udp_encap)
    fformat (vam->ofp, "  udp-encap\n");

  u32 ipsec_over_udp_port = clib_net_to_host_u16 (p->ipsec_over_udp_port);
  if (ipsec_over_udp_port != IPSEC_UDP_PORT_NONE)
    fformat (vam->ofp, "  ipsec-over-udp port %d\n", ipsec_over_udp_port);

  u32 crypto_key_size = clib_net_to_host_u32 (p->ike_ts.crypto_key_size);
  if (p->ike_ts.crypto_alg || p->ike_ts.integ_alg || p->ike_ts.dh_group
      || crypto_key_size)
    fformat (vam->ofp, "  ike-crypto-alg %U %u ike-integ-alg %U ike-dh %U\n",
	     format_ikev2_transform_encr_type, p->ike_ts.crypto_alg,
	     crypto_key_size, format_ikev2_transform_integ_type,
	     p->ike_ts.integ_alg, format_ikev2_transform_dh_type,
	     p->ike_ts.dh_group);

  crypto_key_size = clib_net_to_host_u32 (p->esp_ts.crypto_key_size);
  if (p->esp_ts.crypto_alg || p->esp_ts.integ_alg)
    fformat (vam->ofp, "  esp-crypto-alg %U %u esp-integ-alg %U\n",
	     format_ikev2_transform_encr_type, p->esp_ts.crypto_alg,
	     crypto_key_size,
	     format_ikev2_transform_integ_type, p->esp_ts.integ_alg);

  fformat (vam->ofp, "  lifetime %d jitter %d handover %d maxdata %d\n",
	   clib_net_to_host_u64 (p->lifetime),
	   clib_net_to_host_u32 (p->lifetime_jitter),
	   clib_net_to_host_u32 (p->handover),
	   clib_net_to_host_u64 (p->lifetime_maxdata));

  vam->result_ready = 1;
}

static int
api_ikev2_sa_dump (vat_main_t * vam)
{
  ikev2_test_main_t *im = &ikev2_test_main;
  vl_api_ikev2_sa_dump_t *mp;
  vl_api_control_ping_t *mp_ping;
  int ret;

  /* Construct the API message */
  M (IKEV2_SA_DUMP, mp);

  /* send it... */
  S (mp);

  /* Use a control ping for synchronization */
  if (!im->ping_id)
    im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (im->ping_id);
  mp_ping->client_index = vam->my_client_index;
  vam->result_ready = 0;

  S (mp_ping);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void
vl_api_ikev2_sa_details_t_handler (vl_api_ikev2_sa_details_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  vl_api_ikev2_sa_t *sa = &mp->sa;
  ip_address_t iaddr;
  ip_address_t raddr;
  vl_api_ikev2_keys_t *k = &sa->keys;
  vl_api_ikev2_sa_t_endian (sa);

  ip_address_decode2 (&sa->iaddr, &iaddr);
  ip_address_decode2 (&sa->raddr, &raddr);

  fformat (vam->ofp, "profile index %d sa index: %d\n",
	   mp->sa.profile_index, mp->sa.sa_index);
  fformat (vam->ofp, " iip %U ispi %lx rip %U rspi %lx\n", format_ip_address,
	   &iaddr, sa->ispi, format_ip_address, &raddr, sa->rspi);
  fformat (vam->ofp, " %U ", format_ikev2_sa_transform, &sa->encryption);
  fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->prf);
  fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &sa->integrity);
  fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &sa->dh);

  fformat (vam->ofp, "  SK_d    %U\n",
	   format_hex_bytes, k->sk_d, k->sk_d_len);

  fformat (vam->ofp, "  SK_a  i:%U\n        r:%U\n",
	   format_hex_bytes, k->sk_ai, k->sk_ai_len, format_hex_bytes,
	   k->sk_ar, k->sk_ar_len);

  fformat (vam->ofp, "  SK_e  i:%U\n        r:%U\n", format_hex_bytes,
	   k->sk_ei, k->sk_ei_len, format_hex_bytes, k->sk_er, k->sk_er_len);

  fformat (vam->ofp, "  SK_p  i:%U\n        r:%U\n", format_hex_bytes,
	   k->sk_pi, k->sk_pi_len, format_hex_bytes, k->sk_pr, k->sk_pr_len);

  fformat (vam->ofp, "  identifier (i) %U\n",
	   format_ikev2_id_type_and_data, &sa->i_id);
  fformat (vam->ofp, "  identifier (r) %U\n",
	   format_ikev2_id_type_and_data, &sa->r_id);

  vam->result_ready = 1;
}

static int
api_ikev2_child_sa_dump (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  ikev2_test_main_t *im = &ikev2_test_main;
  vl_api_ikev2_child_sa_dump_t *mp;
  vl_api_control_ping_t *mp_ping;
  int ret;
  u32 sa_index = ~0;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "sa_index %d", &sa_index))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (sa_index == ~0)
    return -99;

  /* Construct the API message */
  M (IKEV2_CHILD_SA_DUMP, mp);

  mp->sa_index = clib_net_to_host_u32 (sa_index);

  /* send it... */
  S (mp);

  /* Use a control ping for synchronization */
  if (!im->ping_id)
    im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (im->ping_id);
  mp_ping->client_index = vam->my_client_index;
  vam->result_ready = 0;

  S (mp_ping);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void
vl_api_ikev2_child_sa_details_t_handler (vl_api_ikev2_child_sa_details_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  vl_api_ikev2_child_sa_t *child_sa = &mp->child_sa;
  vl_api_ikev2_keys_t *k = &child_sa->keys;
  vl_api_ikev2_child_sa_t_endian (child_sa);

  fformat (vam->ofp, "  child sa %u:\n", child_sa->child_sa_index);

  fformat (vam->ofp, "    %U ", format_ikev2_sa_transform,
	   &child_sa->encryption);
  fformat (vam->ofp, "%U ", format_ikev2_sa_transform, &child_sa->integrity);
  fformat (vam->ofp, "%U \n", format_ikev2_sa_transform, &child_sa->esn);

  fformat (vam->ofp, "    spi(i) %lx spi(r) %lx\n",
	   child_sa->i_spi, child_sa->r_spi);

  fformat (vam->ofp, "    SK_e  i:%U\n          r:%U\n",
	   format_hex_bytes, k->sk_ei, k->sk_ei_len,
	   format_hex_bytes, k->sk_er, k->sk_er_len);
  if (k->sk_ai_len)
    {
      fformat (vam->ofp, "    SK_a  i:%U\n          r:%U\n",
	       format_hex_bytes, k->sk_ai, k->sk_ai_len,
	       format_hex_bytes, k->sk_ar, k->sk_ar_len);
    }
  vam->result_ready = 1;
}

static int
api_ikev2_traffic_selector_dump (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  ikev2_test_main_t *im = &ikev2_test_main;
  vl_api_ikev2_traffic_selector_dump_t *mp;
  vl_api_control_ping_t *mp_ping;
  int is_initiator = ~0;
  int sa_index = ~0;
  int child_sa_index = ~0;
  int ret;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "initiator"))
	is_initiator = 1;
      else if (unformat (i, "responder"))
	is_initiator = 0;
      else if (unformat (i, "sa_index %d", &sa_index))
	;
      else if (unformat (i, "child_sa_index %d", &child_sa_index))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (child_sa_index == ~0 || sa_index == ~0 || is_initiator == ~0)
    return -99;

  /* Construct the API message */
  M (IKEV2_TRAFFIC_SELECTOR_DUMP, mp);

  mp->is_initiator = is_initiator;
  mp->sa_index = clib_host_to_net_u32 (sa_index);
  mp->child_sa_index = clib_host_to_net_u32 (child_sa_index);

  /* send it... */
  S (mp);

  /* Use a control ping for synchronization */
  if (!im->ping_id)
    im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (im->ping_id);
  mp_ping->client_index = vam->my_client_index;
  vam->result_ready = 0;

  S (mp_ping);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void
  vl_api_ikev2_traffic_selector_details_t_handler
  (vl_api_ikev2_traffic_selector_details_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  vl_api_ikev2_ts_t *ts = &mp->ts;
  ip_address_t start_addr, end_addr;
  vl_api_ikev2_ts_t_endian (ts);

  ip_address_decode2 (&ts->start_addr, &start_addr);
  ip_address_decode2 (&ts->end_addr, &end_addr);

  fformat (vam->ofp, " %s protocol_id %u addr "
	   "%U - %U port %u - %u\n",
	   ts->is_local, ts->protocol_id,
	   format_ip_address, &start_addr,
	   format_ip_address, &end_addr, ts->start_port, ts->end_port);
  vam->result_ready = 1;
}

static int
api_ikev2_nonce_get (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  ikev2_test_main_t *im = &ikev2_test_main;
  vl_api_ikev2_nonce_get_t *mp;
  vl_api_control_ping_t *mp_ping;
  u32 is_initiator = ~0;
  u32 sa_index = ~0;
  int ret;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "initiator"))
	is_initiator = 1;
      else if (unformat (i, "responder"))
	is_initiator = 0;
      else if (unformat (i, "sa_index %d", &sa_index))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (sa_index == ~0 || is_initiator == ~0)
    return -99;

  /* Construct the API message */
  M (IKEV2_NONCE_GET, mp);

  mp->is_initiator = is_initiator;
  mp->sa_index = clib_host_to_net_u32 (sa_index);

  /* send it... */
  S (mp);

  /* Use a control ping for synchronization */
  if (!im->ping_id)
    im->ping_id = vl_msg_api_get_msg_index ((u8 *) (VL_API_CONTROL_PING_CRC));
  mp_ping = vl_msg_api_alloc_as_if_client (sizeof (*mp_ping));
  mp_ping->_vl_msg_id = htons (im->ping_id);
  mp_ping->client_index = vam->my_client_index;
  vam->result_ready = 0;

  S (mp_ping);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void
vl_api_ikev2_nonce_get_reply_t_handler (vl_api_ikev2_nonce_get_reply_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  mp->data_len = clib_net_to_host_u32 (mp->data_len);

  fformat (vam->ofp, "  nonce:%U\n",
	   format_hex_bytes, mp->nonce, mp->data_len);

  vam->result_ready = 1;
}

static int
api_ikev2_plugin_get_version (vat_main_t * vam)
{
  ikev2_test_main_t *sm = &ikev2_test_main;
  vl_api_ikev2_plugin_get_version_t *mp;
  u32 msg_size = sizeof (*mp);
  int ret;

  vam->result_ready = 0;
  mp = vl_msg_api_alloc_as_if_client (msg_size);
  clib_memset (mp, 0, msg_size);
  mp->_vl_msg_id = ntohs (VL_API_IKEV2_PLUGIN_GET_VERSION + sm->msg_id_base);
  mp->client_index = vam->my_client_index;

  /* send it... */
  S (mp);

  /* Wait for a reply... */
  W (ret);
  return ret;
}

static void vl_api_ikev2_plugin_get_version_reply_t_handler
  (vl_api_ikev2_plugin_get_version_reply_t * mp)
{
  vat_main_t *vam = ikev2_test_main.vat_main;
  clib_warning ("IKEv2 plugin version: %d.%d", ntohl (mp->major),
		ntohl (mp->minor));
  vam->result_ready = 1;
}

static int
api_ikev2_profile_set_ipsec_udp_port (vat_main_t * vam)
{
  return 0;
}

static int
api_ikev2_profile_set_liveness (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_profile_set_liveness_t *mp;
  u32 period = 0, max_retries = 0;
  int ret;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (!unformat (i, "period %d max-retries %d", &period, &max_retries))
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  M (IKEV2_PROFILE_SET_LIVENESS, mp);

  mp->period = clib_host_to_net_u32 (period);
  mp->max_retries = clib_host_to_net_u32 (max_retries);

  S (mp);
  W (ret);

  return ret;
}

static int
api_ikev2_profile_add_del (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_profile_add_del_t *mp;
  u8 is_add = 1;
  u8 *name = 0;
  int ret;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "del"))
	is_add = 0;
      else if (unformat (i, "name %U", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_PROFILE_ADD_DEL, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  mp->is_add = is_add;
  vec_free (name);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_profile_set_auth (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_profile_set_auth_t *mp;
  u8 *name = 0;
  u8 *data = 0;
  u32 auth_method = 0;
  u8 is_hex = 0;
  int ret;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "name %U", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else if (unformat (i, "auth_method %U",
			 unformat_ikev2_auth_method, &auth_method))
	;
      else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data))
	is_hex = 1;
      else if (unformat (i, "auth_data %v", &data))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  if (!vec_len (data))
    {
      errmsg ("auth_data must be specified");
      return -99;
    }

  if (!auth_method)
    {
      errmsg ("auth_method must be specified");
      return -99;
    }

  M (IKEV2_PROFILE_SET_AUTH, mp);

  mp->is_hex = is_hex;
  mp->auth_method = (u8) auth_method;
  mp->data_len = vec_len (data);
  clib_memcpy (mp->name, name, vec_len (name));
  clib_memcpy (mp->data, data, vec_len (data));
  vec_free (name);
  vec_free (data);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_profile_set_id (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_profile_set_id_t *mp;
  u8 *name = 0;
  u8 *data = 0;
  u8 is_local = 0;
  u32 id_type = 0;
  ip_address_t ip;
  int ret;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "name %U", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type))
	;
      else if (unformat (i, "id_data %U", unformat_ip_address, &ip))
	{
	  data = vec_new (u8, ip_address_size (&ip));
	  clib_memcpy (data, ip_addr_bytes (&ip), ip_address_size (&ip));
	}
      else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data))
	;
      else if (unformat (i, "id_data %v", &data))
	;
      else if (unformat (i, "local"))
	is_local = 1;
      else if (unformat (i, "remote"))
	is_local = 0;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  if (!vec_len (data))
    {
      errmsg ("id_data must be specified");
      return -99;
    }

  if (!id_type)
    {
      errmsg ("id_type must be specified");
      return -99;
    }

  M (IKEV2_PROFILE_SET_ID, mp);

  mp->is_local = is_local;
  mp->id_type = (u8) id_type;
  mp->data_len = vec_len (data);
  clib_memcpy (mp->name, name, vec_len (name));
  clib_memcpy (mp->data, data, vec_len (data));
  vec_free (name);
  vec_free (data);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_profile_set_ts (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_profile_set_ts_t *mp;
  u8 *name = 0;
  u8 is_local = 0;
  u32 proto = 0, start_port = 0, end_port = (u32) ~ 0;
  ip_address_t start_addr, end_addr;
  u8 start_addr_set = 0, end_addr_set = 0;

  const char *valid_chars = "a-zA-Z0-9_";
  int ret;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "name %U", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else if (unformat (i, "protocol %d", &proto))
	;
      else if (unformat (i, "start_port %d", &start_port))
	;
      else if (unformat (i, "end_port %d", &end_port))
	;
      else
	if (unformat (i, "start_addr %U", unformat_ip_address, &start_addr))
	start_addr_set = 1;
      else if (unformat (i, "end_addr %U", unformat_ip_address, &end_addr))
	end_addr_set = 1;
      else if (unformat (i, "local"))
	is_local = 1;
      else if (unformat (i, "remote"))
	is_local = 0;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!start_addr_set || !end_addr_set)
    {
      errmsg ("missing start or end address");
      return -99;
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_PROFILE_SET_TS, mp);

  mp->ts.is_local = is_local;
  mp->ts.protocol_id = (u8) proto;
  mp->ts.start_port = clib_host_to_net_u16 ((u16) start_port);
  mp->ts.end_port = clib_host_to_net_u16 ((u16) end_port);
  ip_address_encode2 (&start_addr, &mp->ts.start_addr);
  ip_address_encode2 (&end_addr, &mp->ts.end_addr);
  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_set_local_key (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_local_key_t *mp;
  u8 *file = 0;
  int ret;

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "file %v", &file))
	vec_add1 (file, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (file))
    {
      errmsg ("RSA key file must be specified");
      return -99;
    }

  if (vec_len (file) > 256)
    {
      errmsg ("file name too long");
      return -99;
    }

  M (IKEV2_SET_LOCAL_KEY, mp);

  clib_memcpy (mp->key_file, file, vec_len (file));
  vec_free (file);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_profile_set_udp_encap (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_responder_t *mp;
  int ret;
  u8 *name = 0;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%U udp-encap", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_PROFILE_SET_UDP_ENCAP, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_set_tunnel_interface (vat_main_t * vam)
{
  return (0);
}

static int
api_ikev2_set_responder (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_responder_t *mp;
  int ret;
  u8 *name = 0;
  u32 sw_if_index = ~0;
  ip_address_t address;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat
	  (i, "%U interface %d address %U", unformat_token, valid_chars,
	   &name, &sw_if_index, unformat_ip_address, &address))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_SET_RESPONDER, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);

  mp->responder.sw_if_index = clib_host_to_net_u32 (sw_if_index);
  ip_address_encode2 (&address, &mp->responder.addr);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_set_ike_transforms (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_ike_transforms_t *mp;
  int ret;
  u8 *name = 0;
  u32 crypto_alg, crypto_key_size, integ_alg, dh_group;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name,
		    &crypto_alg, &crypto_key_size, &integ_alg, &dh_group))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_SET_IKE_TRANSFORMS, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);
  mp->tr.crypto_alg = crypto_alg;
  mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
  mp->tr.integ_alg = integ_alg;
  mp->tr.dh_group = dh_group;

  S (mp);
  W (ret);
  return ret;
}


static int
api_ikev2_set_esp_transforms (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_esp_transforms_t *mp;
  int ret;
  u8 *name = 0;
  u32 crypto_alg, crypto_key_size, integ_alg;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%U %d %d %d", unformat_token, valid_chars, &name,
		    &crypto_alg, &crypto_key_size, &integ_alg))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_SET_ESP_TRANSFORMS, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);
  mp->tr.crypto_alg = crypto_alg;
  mp->tr.crypto_key_size = clib_host_to_net_u32 (crypto_key_size);
  mp->tr.integ_alg = integ_alg;

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_set_sa_lifetime (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_set_sa_lifetime_t *mp;
  int ret;
  u8 *name = 0;
  u64 lifetime, lifetime_maxdata;
  u32 lifetime_jitter, handover;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name,
		    &lifetime, &lifetime_jitter, &handover,
		    &lifetime_maxdata))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_SET_SA_LIFETIME, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);
  mp->lifetime = lifetime;
  mp->lifetime_jitter = lifetime_jitter;
  mp->handover = handover;
  mp->lifetime_maxdata = lifetime_maxdata;

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_initiate_sa_init (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_initiate_sa_init_t *mp;
  int ret;
  u8 *name = 0;

  const char *valid_chars = "a-zA-Z0-9_";

  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%U", unformat_token, valid_chars, &name))
	vec_add1 (name, 0);
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  if (!vec_len (name))
    {
      errmsg ("profile name must be specified");
      return -99;
    }

  if (vec_len (name) > 64)
    {
      errmsg ("profile name too long");
      return -99;
    }

  M (IKEV2_INITIATE_SA_INIT, mp);

  clib_memcpy (mp->name, name, vec_len (name));
  vec_free (name);

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_initiate_del_ike_sa (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_initiate_del_ike_sa_t *mp;
  int ret;
  u64 ispi;


  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%lx", &ispi))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  M (IKEV2_INITIATE_DEL_IKE_SA, mp);

  mp->ispi = ispi;

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_initiate_del_child_sa (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_initiate_del_child_sa_t *mp;
  int ret;
  u32 ispi;


  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%x", &ispi))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  M (IKEV2_INITIATE_DEL_CHILD_SA, mp);

  mp->ispi = ispi;

  S (mp);
  W (ret);
  return ret;
}

static int
api_ikev2_initiate_rekey_child_sa (vat_main_t * vam)
{
  unformat_input_t *i = vam->input;
  vl_api_ikev2_initiate_rekey_child_sa_t *mp;
  int ret;
  u32 ispi;


  while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (i, "%x", &ispi))
	;
      else
	{
	  errmsg ("parse error '%U'", format_unformat_error, i);
	  return -99;
	}
    }

  M (IKEV2_INITIATE_REKEY_CHILD_SA, mp);

  mp->ispi = ispi;

  S (mp);
  W (ret);
  return ret;
}

#include <ikev2/ikev2.api_test.c>

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */