Implement LISP control plane messages
[vpp.git] / vnet / test / lisp-cp / test_cp_serdes.c
index 2ca391e..2d1cf06 100644 (file)
@@ -181,8 +181,11 @@ build_map_request (lisp_cp_main_t * lcm, vlib_buffer_t * b,
   gid_address_t _seid, * seid = &_seid;
   gid_address_t _deid, * deid = &_deid;
   u8 is_smr_invoked = 1;
+  u8 rloc_probe_set = 0;
   u64 nonce = 0;
   map_request_hdr_t * h = 0;
+  memset (deid, 0, sizeof (deid[0]));
+  memset (seid, 0, sizeof (seid[0]));
 
   gid_address_type (seid) = GID_ADDR_IP_PREFIX;
   ip_address_t * ip_addr = &gid_address_ip (seid);
@@ -196,7 +199,7 @@ build_map_request (lisp_cp_main_t * lcm, vlib_buffer_t * b,
   gid_address_ippref_len (deid) = 24;
 
   h = lisp_msg_put_mreq (lcm, b, seid, deid, rlocs,
-                     is_smr_invoked, &nonce);
+                     is_smr_invoked, rloc_probe_set, &nonce);
   vec_free(rlocs);
   return h;
 }
@@ -205,6 +208,7 @@ static void
 generate_rlocs (gid_address_t **rlocs, u32 * count)
 {
   gid_address_t gid_addr_data, * gid_addr = &gid_addr_data;
+  memset (gid_addr, 0, sizeof (gid_addr[0]));
   ip_address_t * addr = &gid_address_ip (gid_addr);
 
   gid_address_type (gid_addr) = GID_ADDR_IP_PREFIX;
@@ -277,34 +281,20 @@ static clib_error_t * test_lisp_msg_put_mreq_with_lcaf ()
   clib_error_t * error = 0;
   map_request_hdr_t *h = 0;
   gid_address_t * rlocs = 0;
-  gid_address_t rloc;
 
   ip_prefix_t ippref;
   ip_prefix_version (&ippref) = IP4;
   ip4_address_t * ip = &ip_prefix_v4 (&ippref);
   ip->as_u32 = 0x11223344;
 
-  gid_address_t gid1 =
+  gid_address_t g =
     {
       .type = GID_ADDR_IP_PREFIX,
-      .ippref = ippref
+      .ippref = ippref,
+      .vni = 0x90919293,
+      .vni_mask = 0x17
     };
-
-  lcaf_t lcaf1 =
-    {
-      .type = LCAF_INSTANCE_ID,
-      .uni =
-        {
-          .vni_mask_len = 0x17,
-          .vni = 0x90919293,
-          .gid_addr = &gid1
-        }
-    };
-
-  gid_address_type (&rloc) = GID_ADDR_LCAF;
-  gid_address_lcaf (&rloc) = lcaf1;
-
-  vec_add1 (rlocs, rloc);
+  vec_add1 (rlocs, g);
 
   u8 * data = clib_mem_alloc (500);
   memset (data, 0, 500);
@@ -395,6 +385,107 @@ done:
   return error;
 }
 
+/* generate a vector of eid records */
+static mapping_t *
+build_test_map_records ()
+{
+  mapping_t * records = 0;
+
+  mapping_t r = {
+    .ttl = 0x44332211,
+    .eid = {
+      .type = GID_ADDR_MAC,
+      .mac = {1, 2, 3, 4, 5, 6},
+      .vni = 0x0
+    }
+  };
+
+  locator_t loc = {
+    .weight = 1,
+    .priority = 2,
+    .local = 1,
+    .address = {
+      .type = GID_ADDR_IP_PREFIX,
+      .ippref = {
+        .addr = {
+          .ip.v4.as_u32 = 0x99887766,
+          .version = IP4
+        }
+      }
+    }
+  };
+  vec_add1 (r.locators, loc);
+  vec_add1 (records, r);
+
+  return records;
+}
+
+static void
+free_test_map_records (mapping_t * maps)
+{
+  mapping_t * map;
+  vec_foreach (map, maps)
+    {
+      vec_free (map->locators);
+    }
+  vec_free (maps);
+}
+
+static clib_error_t *
+test_lisp_map_register ()
+{
+  vlib_buffer_t *b;
+  clib_error_t * error = 0;
+  u64 nonce;
+  u32 msg_len = 0;
+  mapping_t * records = build_test_map_records ();
+
+  u8 * data = clib_mem_alloc(500);
+  memset(data, 0, 500);
+  b = (vlib_buffer_t *) data;
+
+  lisp_msg_put_map_register (b, records, 1 /* want map notify */,
+                            20 /* length of HMAC_SHA_1_96 */,
+                            &nonce, &msg_len);
+  free_test_map_records (records);
+
+  /* clear Nonce to simplify comparison */
+  memset((u8 *)b->data + 4, 0, 8);
+
+  /* clear authentication data */
+  memset ((u8 *)b->data + 16, 0, 20);
+
+  u8 expected_data[] = {
+    0x30, 0x00, 0x01, 0x01, /* type; rsvd; want notify; REC count */
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, /* nonce */
+    0x00, 0x00, 0x00, 0x00, /* key id, auth data length:
+                              both are zeroes because those are set in another
+                              function (see auth_data_len_by_key_id())*/
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00,
+    0x00, 0x00, 0x00, 0x00, /* auth data */
+
+    /* first record */
+    0x44, 0x33, 0x22, 0x11, /* ttl */
+    0x01, 0x00, 0x00, 0x00, /* loc count, eid len, ACT, A */
+    0x00, 0x00, 0x40, 0x05, /* rsvd, map ver num, AFI = MAC */
+    0x01, 0x02, 0x03, 0x04,
+    0x05, 0x06,             /* MAC EID */
+
+    /* locator 1 */
+    0x02, 0x01, 0x00, 0x00, /* prio, weight, mprio, mweight */
+    0x00, 0x04, 0x00, 0x01, /* flags, AFI = ipv4 */
+    0x66, 0x77, 0x88, 0x99, /* ipv4 locator address */
+  };
+  _assert (0 == memcmp (expected_data, b->data, sizeof (expected_data)));
+done:
+  clib_mem_free (data);
+  return error;
+}
+
 static clib_error_t *
 test_lisp_parse_lcaf ()
 {
@@ -482,16 +573,9 @@ test_lisp_parse_lcaf ()
   _assert (locs[0].mpriority == 0xc);
   _assert (locs[0].mweight == 0xd);
 
-  /* check LCAF header data */
-  lcaf_t * lcaf = &gid_address_lcaf (&locs[0].address);
-  _assert (gid_address_type (&locs[0].address) == GID_ADDR_LCAF);
-  _assert (lcaf_type (lcaf) == LCAF_INSTANCE_ID);
-  vni_t * v = (vni_t *) lcaf;
-  _assert (vni_vni (v) == 0x09);
-
-  gid_address_t * nested_gid = vni_gid (v);
-  _assert (GID_ADDR_IP_PREFIX == gid_address_type (nested_gid));
-  ip_prefix_t * ip_pref = &gid_address_ippref (nested_gid);
+  _assert (gid_address_type (&locs[0].address) == GID_ADDR_IP_PREFIX);
+  _assert (gid_address_vni (&locs[0].address) == 0x09);
+  ip_prefix_t * ip_pref = &gid_address_ippref (&locs[0].address);
   _assert (IP4 == ip_prefix_version (ip_pref));
 
   /* 2nd locator - LCAF entry with ipv6 address */
@@ -501,16 +585,9 @@ test_lisp_parse_lcaf ()
   _assert (locs[1].mpriority == 0x5);
   _assert (locs[1].mweight == 0x4);
 
-  /* LCAF header */
-  _assert (gid_address_type (&locs[1].address) == GID_ADDR_LCAF);
-  lcaf = &gid_address_lcaf (&locs[1].address);
-  _assert (lcaf_type (lcaf) == LCAF_INSTANCE_ID);
-  v = (vni_t *) lcaf;
-  _assert (vni_vni (v) == 0x22446688);
-
-  nested_gid = vni_gid (v);
-  _assert (GID_ADDR_IP_PREFIX == gid_address_type (nested_gid));
-  ip_pref = &gid_address_ippref (nested_gid);
+  _assert (gid_address_type (&locs[1].address) == GID_ADDR_IP_PREFIX);
+  _assert (0x22446688 == gid_address_vni (&locs[1].address));
+  ip_pref = &gid_address_ippref (&locs[1].address);
   _assert (IP6 == ip_prefix_version (ip_pref));
 
   /* 3rd locator - simple ipv4 address */
@@ -530,7 +607,8 @@ done:
   _(lisp_msg_push_ecm)                    \
   _(lisp_msg_parse)                       \
   _(lisp_msg_parse_mapping_record)        \
-  _(lisp_parse_lcaf)
+  _(lisp_parse_lcaf)                      \
+  _(lisp_map_register)
 
 int run_tests (void)
 {