aboutsummaryrefslogtreecommitdiffstats
path: root/docs
diff options
context:
space:
mode:
authorfatelei <fatelei@gmail.com>2022-06-08 12:28:23 +0800
committerDave Wallace <dwallacelf@gmail.com>2022-09-20 21:31:27 -0400
commit320272a7eb047c2d9263e19d924c489a1cade54f (patch)
tree916a4a61e2300d62021d0a9279a96088866df61a /docs
parentc119e796f535046c33430a92949addcf11dbf684 (diff)
docs: enhance install vpp docs
Type: docs Change-Id: Ic1439ce658d9d53208b29d85440a6cc225ed5b74 Signed-off-by: fatelei <fatelei@gmail.com> Signed-off-by: Dave Wallace <dwallacelf@gmail.com>
Diffstat (limited to 'docs')
-rw-r--r--docs/gettingstarted/installing/index.rst16
-rw-r--r--docs/gettingstarted/installing/ubuntu.rst68
-rw-r--r--docs/spelling_wordlist.txt1
3 files changed, 51 insertions, 34 deletions
diff --git a/docs/gettingstarted/installing/index.rst b/docs/gettingstarted/installing/index.rst
index ba67375f8c7..86ffec07d73 100644
--- a/docs/gettingstarted/installing/index.rst
+++ b/docs/gettingstarted/installing/index.rst
@@ -10,16 +10,16 @@ Downloading and Installing VPP
If you want to use VPP it can be convenient to install the binaries from
existing packages. This guide describes how to pull, install and run the VPP packages.
-This section provides directions on how to Install VPP binaries on
-Ubuntu, and Centos platforms.
+FD.io VPP packages are stored in Packagecloud.io package repositories. There is
+a package repository for the latest VPP release packages as well as a package
+repository associated with each branch in the VPP git repository. The VPP merge
+jobs which run on Jenkins (https://jenkins.fd.io) for each actively supported
+git branch uploads packages to packagecloud that are built from the vpp code in
+the branch.
-FD.io VPP is installed using Package Cloud. For a complete set of
-instructions on how to install VPP with package cloud please refer
-to `Package Cloud <https://packagecloud.io/fdio/release>`_.
-
-Installing on Ubuntu
----------------------------------
+Installing on Ubuntu / Debian OS Distros
+----------------------------------------
The following are instructions on how to install VPP on Ubuntu.
diff --git a/docs/gettingstarted/installing/ubuntu.rst b/docs/gettingstarted/installing/ubuntu.rst
index f1bdb3eecd0..1022e0c3ff1 100644
--- a/docs/gettingstarted/installing/ubuntu.rst
+++ b/docs/gettingstarted/installing/ubuntu.rst
@@ -8,53 +8,58 @@ Ubuntu - Setup the FD.io Repository
Choose one of the following releases to install.
Update the OS
------------------------
+-------------
It is a good idea to first update and upgrade the OS before starting; run the
-following command to update the OS:
+following commands to upgrade the OS and install the curl package to download
+the setup script from packagecloud.io:
.. code-block:: console
- apt-get update
+ sudo apt-get update
+ sudo apt-get dist-upgrade -y
+ sudo apt-get install curl
+Configure Apt Using the Packagecloud Setup Script
+-------------------------------------------------
-Point to the Repository
------------------------------------
+FD.io Packagecloud Repositories provides pop-up menu that provides the
+ability to copy a one-line bash command to fetch the packagecloud setup script.
+In general, start at the FD.io packagecloud URL:
-Create a file **/etc/apt/sources.list.d/99fd.io.list** with contents that point to
-the version needed. The contents needed are shown below.
+https://packagecloud.io/fdio
-.. _install_vpp:
-
-VPP latest Release
-^^^^^^^^^^^^^^^^^^^
-
-Create the file **/etc/apt/sources.list.d/99fd.io.list** that contain the following contents:
+Then choose the desired repository link (e.g. 'release') and select the "Debian"
+package icon in the section named "Quick install instructions". When the pop-up
+dialog appears, select the "Copy" button to copy the command to run the setup
+script and paste it into a terminal on your server.
-.. code-block:: console
- deb [trusted=yes] https://packagecloud.io/fdio/release/ubuntu bionic main
+.. _install_vpp:
-Get the key:
+VPP Release Repo
+^^^^^^^^^^^^^^^^^^^
-.. code-block:: console
+The URL to install the latest VPP release is
- curl -L https://packagecloud.io/fdio/release/gpgkey | sudo apt-key add -
+https://packagecloud.io/fdio/release
-VPP master Branch
-^^^^^^^^^^^^^^^^^^^^
-Create the file **/etc/apt/sources.list.d/99fd.io.list** that contain the following contents:
+VPP master Branch Repo
+^^^^^^^^^^^^^^^^^^^^^^
+The URL to install the latest VPP release is
-.. code-block:: console
+https://packagecloud.io/fdio/release
- deb [trusted=yes] https://packagecloud.io/fdio/master/ubuntu bionic main
-Get the key:
+VPP stable release Branch Repo
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-.. code-block:: console
+Stable release branches are named "stable/YYMM" (e.g. stable/2206) and the associated
+packagecloud repositories are named "YYMM" (e.g 2206). For example, the URL to
+the VPP 22.06 stable release branch package repository is:
- curl -L https://packagecloud.io/fdio/master/gpgkey | sudo apt-key add -
+https://packagecloud.io/fdio/2206
Install the Mandatory Packages
@@ -86,3 +91,14 @@ Uninstall the packages by running the following command:
.. code-block:: console
sudo apt-get remove --purge "vpp*"
+
+
+Remove FD.io Apt source lists
+=============================
+
+Remove FD.io Apt source list files created by the packagecloud apt setup script
+by running the following command:
+
+.. code-block:: console
+
+ sudo rm /etc/apt/sources.list.d/fdio*.list
diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt
index b64644b5e8b..97bd0b573e1 100644
--- a/docs/spelling_wordlist.txt
+++ b/docs/spelling_wordlist.txt
@@ -1334,6 +1334,7 @@ XXX
xyz
yaml
Yaml
+YYMM
yyy
zeroize
zfs
n419' href='#n419'>419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 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 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
/*
 * Copyright (c) 2019 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 <vnet/vnet.h>
#include <vppinfra/error.h>
#include <vnet/lisp-cp/lisp_cp_messages.h>
#include <vnet/lisp-cp/control.h>
#include <vnet/lisp-cp/lisp_msg_serdes.h>
#include <vlibapi/api.h>
#include <vnet/lisp-cp/packets.h>

#define _assert(e)                    			\
  error = CLIB_ERROR_ASSERT (e);      			\
  if (error)                 		         	\
    {							\
      fformat(stderr, "FAIL: line %d \n\n", __LINE__);	\
      goto done;					\
    }

static void
print_chunk (u8 * b, int *offset, int c, char *des)
{
  int i, n = offset[0] + c;;
  for (i = offset[0]; i < n; i++)
    {
      printf ("0x%02x, ", b[i]);
    }
  printf (" // %s\n", des);
  *offset += c;
}

void
print_map_request (map_request_hdr_t * h)
{
#define pchunk(_count, _desc) \
  print_chunk((u8 *)h, &offset, _count, _desc)

  int offset = 0;

  pchunk (4, "data");
  pchunk (8, "Nonce");
  pchunk (2, "Source-EID-AFI");
  pchunk (4, "Source EID Address");
  pchunk (2, "ITR-RLOC-AFI 1");
  pchunk (4, "ITR-RLOC Address 1");
  pchunk (2, "ITR-RLOC-AFI 2");
  pchunk (16, "ITR-RLOC Address 2");
  pchunk (1, "REC: reserved");
  pchunk (1, "REC: EID mask-len");
  pchunk (2, "REC: EID-prefix-AFI");
  pchunk (4, "REC: EID-prefix");
  printf ("\n");
}

static clib_error_t *
test_lisp_msg_push_ecm ()
{
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error = 0;
  gid_address_t la, ra;
  vlib_buffer_t *b = 0;
  u32 buff_len = 900;
  int lp = 0x15, rp = 0x14;

  b = clib_mem_alloc (buff_len);
  clib_memset ((u8 *) b, 0, buff_len);
  b->current_length = buff_len;
  b->current_data = sizeof (udp_header_t) + sizeof (ip4_header_t) +
    sizeof (ecm_hdr_t) + 1;

  la.type = GID_ADDR_IP_PREFIX;
  la.ippref.addr.ip.v4.as_u32 = 0xa1b2c3d4;
  la.ippref.addr.version = AF_IP4;

  ra.type = GID_ADDR_IP_PREFIX;
  ra.ippref.addr.ip.v4.as_u32 = 0x90817263;
  ra.ippref.addr.version = AF_IP4;

  ecm_hdr_t *lh = lisp_msg_push_ecm (vm, b, lp, rp, &la, &ra);

  u8 expected_ecm_hdr[] = {
    0x80, 0x00, 0x00, 0x00
  };
  _assert (0 == memcmp (expected_ecm_hdr, lh, sizeof (expected_ecm_hdr)));

  ip4_header_t *ih = (ip4_header_t *) (lh + 1);
  /* clear ip checksum */
  clib_memset ((u8 *) ih + 10, 0, 2);

  /* *INDENT-OFF* */
  u8 expected_ip4_hdr[] = {
    0x45,                   /* version; IHL */
    0x00,                   /* services */
    0x03, 0xa0,             /* total length */
    0x00, 0x00,             /* identification */
    0x40, 0x00,             /* flags; fragment offset*/
    0xff,                   /* TTL */
    0x11,                   /* protocol */
    0x00, 0x00,             /* header checksum */
    0xd4, 0xc3, 0xb2, 0xa1, /* src IP */
    0x63, 0x72, 0x81, 0x90, /* dst IP */
  };
  /* *INDENT-ON* */

  _assert (0 == memcmp (ih, expected_ip4_hdr, sizeof (expected_ip4_hdr)));

  udp_header_t *uh = (udp_header_t *) (ih + 1);
  /* clear udp checksum */
  clib_memset ((u8 *) uh + 6, 0, 2);

  /* *INDENT-OFF* */
  u8 expected_udp_hdr[] = {
    0x00, 0x15, /* src port */
    0x00, 0x14, /* dst port */
    0x03, 0x8c, /* length */
    0x00, 0x00, /* checksum */
  };
  /* *INDENT-ON* */

  _assert (0 == memcmp (uh, expected_udp_hdr, sizeof (expected_udp_hdr)));

done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_lisp_msg_parse_mapping_record ()
{
  clib_error_t *error = 0;
  locator_t probed;
  locator_t *locs = 0;
  vlib_buffer_t *b = 0;
  gid_address_t eid;
  u32 buff_len = 500;

  b = clib_mem_alloc (buff_len);
  clib_memset ((u8 *) b, 0, buff_len);

  /* *INDENT-OFF* */
  u8 map_reply_records[] = {
    /* 1. record */
    0x01, 0x02, 0x03, 0x04, /* record TTL */
    0x01,                   /* locator count */
    0x00, 0x00, 0x00,       /* eid-mask-len; ... */
    0x00, 0x00,             /* reserved; map-version num */
    0x00, 0x01,             /* EID-Prefix-AFI */
    0x33, 0x44, 0x55, 0x66, /* eid-prefix */
    /* loc */
    0x0a,                   /* prority */
    0x0b,                   /* weight */
    0x0c,                   /* m-prority */
    0x0d,                   /* m-weight */
    0x00, 0x00,             /* unused flags */
    0x00, 0x01,             /* Loc-AFI */
    0xaa, 0xbb, 0xcc, 0xdd, /* Loator */
  };
  /* *INDENT-ON* */

  b->current_length = buff_len;
  clib_memcpy (b->data, map_reply_records, sizeof (map_reply_records));

  lisp_msg_parse_mapping_record (b, &eid, &locs, &probed);
  _assert (vec_len (locs) == 1);
  _assert (eid.ippref.addr.ip.v4.as_u32 == 0x66554433);
  _assert (locs[0].local == 0);
  _assert (locs[0].address.ippref.addr.ip.v4.as_u32 == 0xddccbbaa);
  _assert (locs[0].address.type == GID_ADDR_IP_PREFIX);
  _assert (locs[0].priority == 0xa);
  _assert (locs[0].weight == 0xb);
  _assert (locs[0].mpriority == 0xc);
  _assert (locs[0].mweight == 0xd);

done:
  clib_mem_free (b);
  if (locs)
    vec_free (locs);
  return error;
}

static map_request_hdr_t *
build_map_request (lisp_cp_main_t * lcm, vlib_buffer_t * b,
		   gid_address_t * rlocs)
{
  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;
  clib_memset (deid, 0, sizeof (deid[0]));
  clib_memset (seid, 0, sizeof (seid[0]));

  gid_address_type (seid) = GID_ADDR_IP_PREFIX;
  ip_address_t *ip_addr = &gid_address_ip (seid);
  ip_addr_v4 (ip_addr).as_u32 = 0x12345678;
  seid->ippref.addr.version = AF_IP4;

  gid_address_type (deid) = GID_ADDR_IP_PREFIX;
  ip_address_t *ip_addr2 = &gid_address_ip (deid);
  ip_addr_v4 (ip_addr2).as_u32 = 0x9abcdef0;
  deid->ippref.addr.version = AF_IP4;
  gid_address_ippref_len (deid) = 24;

  h = lisp_msg_put_mreq (lcm, b, seid, deid, rlocs,
			 is_smr_invoked, rloc_probe_set, &nonce);
  vec_free (rlocs);
  return h;
}

static void
generate_rlocs (gid_address_t ** rlocs, u32 * count)
{
  gid_address_t gid_addr_data, *gid_addr = &gid_addr_data;
  clib_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;

  ip_addr_version (addr) = AF_IP4;
  ip_addr_v4 (addr).data_u32 = 0x10203040;
  vec_add1 (rlocs[0], gid_addr[0]);

  ip_addr_v6 (addr).as_u32[0] = 0xffeeddcc;
  ip_addr_v6 (addr).as_u32[1] = 0xbbaa9988;
  ip_addr_v6 (addr).as_u32[2] = 0x77665544;
  ip_addr_v6 (addr).as_u32[3] = 0x33221100;
  ip_addr_version (addr) = AF_IP6;
  vec_add1 (rlocs[0], gid_addr[0]);
}

static clib_error_t *
test_lisp_msg_parse ()
{
  gid_address_t eid;
  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
  map_request_hdr_t *h;
  gid_address_t gid;
  clib_error_t *error = 0;
  vlib_buffer_t *b;
  gid_address_t *rlocs_decode = 0, *rlocs = 0;
  u32 rloc_count_parse = 0;

  u8 *data = clib_mem_alloc (500);
  clib_memset (data, 0, 500);
  b = (vlib_buffer_t *) data;

  generate_rlocs (&rlocs_decode, &rloc_count_parse);
  h = build_map_request (lcm, b, rlocs_decode);

  vlib_buffer_pull (b, sizeof (*h));
  u32 len = lisp_msg_parse_addr (b, &gid);
  _assert (len == 2 + 4
	   /* Source-EID-AFI field lenght + IPv4 address length */ );
  _assert (gid.ippref.addr.ip.v4.as_u32 == 0x12345678);
  _assert (gid.ippref.addr.version == AF_IP4);

  u8 rloc_count = MREQ_ITR_RLOC_COUNT (h) + 1;
  lisp_msg_parse_itr_rlocs (b, &rlocs, rloc_count);

  _assert (vec_len (rlocs) == 2);
  _assert (rlocs[0].ippref.addr.ip.v4.as_u32 == 0x10203040);
  _assert (rlocs[0].ippref.addr.version == AF_IP4);

  _assert (rlocs[1].ippref.addr.ip.v6.as_u32[0] == 0xffeeddcc);
  _assert (rlocs[1].ippref.addr.ip.v6.as_u32[1] == 0xbbaa9988);
  _assert (rlocs[1].ippref.addr.ip.v6.as_u32[2] == 0x77665544);
  _assert (rlocs[1].ippref.addr.ip.v6.as_u32[3] == 0x33221100);
  _assert (rlocs[1].ippref.addr.version == AF_IP6);

  lisp_msg_parse_eid_rec (b, &eid);
  _assert (eid.ippref.addr.ip.v4.as_u32 == 0x9abcdef0);
  _assert (eid.ippref.addr.version == AF_IP4);
  _assert (eid.ippref.len == 24);

done:
  clib_mem_free (data);
  if (rlocs)
    vec_free (rlocs);
  return error;
}

static clib_error_t *
test_lisp_msg_put_mreq_with_lcaf ()
{
  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
  clib_error_t *error = 0;
  map_request_hdr_t *h = 0;
  gid_address_t *rlocs = 0;

  ip_prefix_t ippref;
  ip_prefix_version (&ippref) = AF_IP4;
  ip4_address_t *ip = &ip_prefix_v4 (&ippref);
  ip->as_u32 = 0x11223344;
  ippref.len = 32;

  gid_address_t g = {
    .type = GID_ADDR_IP_PREFIX,
    .ippref = ippref,
    .vni = 0x90919293,
    .vni_mask = 0x17
  };
  vec_add1 (rlocs, g);

  u8 *data = clib_mem_alloc (500);
  clib_memset (data, 0, 500);

  h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);

  /* clear Nonce to simplify comparison */
  clib_memset ((u8 *) h + 4, 0, 8);

  /* *INDENT-OFF* */
  u8 expected_data[] =
    {
      0x10, 0x40, 0x00, 0x01, /* type; flags; IRC; REC count */
      0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, /* nonce */
      0x00, 0x01,             /* Source-EID-AFI */
      0x78, 0x56, 0x34, 0x12, /* Source EID Address */

      /* RLOCs */
      0x40, 0x03,             /* AFI = LCAF*/
      /* LCAF header*/
      0x00, 0x00,             /* reserved1, flags */
      0x02,                   /* type = Instance ID */
      0x17,                   /* IID mask-len */
      0x00, 0x0a,             /* lenght */
      0x90, 0x91, 0x92, 0x93, /* IID / VNI */

      0x00, 0x01,             /* AFI = ipv4 */
      0x44, 0x33, 0x22, 0x11, /* ITR-RLOC Address 1 */

      /* record */
      0x00,                   /* reserved */
      0x18,                   /* EID mask-len */
      0x00, 0x01,             /* EID-prefix-AFI */
      0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
    };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));
done:
  clib_mem_free (data);
  return error;
}

static clib_error_t *
test_lisp_msg_put_mreq ()
{
  lisp_cp_main_t *lcm = vnet_lisp_cp_get_main ();
  clib_error_t *error = 0;
  map_request_hdr_t *h;
  gid_address_t *rlocs = 0;
  u32 rloc_count = 0;

  u8 *data = clib_mem_alloc (500);
  clib_memset (data, 0, 500);

  generate_rlocs (&rlocs, &rloc_count);
  h = build_map_request (lcm, (vlib_buffer_t *) data, rlocs);

  /* clear Nonce to simplify comparison */
  clib_memset ((u8 *) h + 4, 0, 8);

  print_map_request (h);

  /* *INDENT-OFF* */
  u8 expected_data[50] = {
    0x10, 0x40, 0x01, 0x01, /* type; flags; IRC; REC count */
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, /* nonce */
    0x00, 0x01,             /* Source-EID-AFI */
    0x78, 0x56, 0x34, 0x12, /* Source EID Address */

    /* RLOCs */
    0x00, 0x01,             /* ITR-RLOC-AFI 1 */
    0x40, 0x30, 0x20, 0x10, /* ITR-RLOC Address 1 */
    0x00, 0x02,             /* ITR-RLOC-AFI 2 */
    0xcc, 0xdd, 0xee, 0xff,
    0x88, 0x99, 0xaa, 0xbb,
    0x44, 0x55, 0x66, 0x77,
    0x00, 0x11, 0x22, 0x33, /* ITR-RLOC Address 2 */

    /* record */
    0x00,                   /* reserved */
    0x18,                   /* EID mask-len */
    0x00, 0x01,             /* EID-prefix-AFI */
    0xf0, 0xde, 0xbc, 0x9a, /* EID-prefix */
  };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_data, (u8 *) h, sizeof (expected_data)));

done:
  clib_mem_free (data);
  return error;
}

/* generate a vector of eid records */
static mapping_t *
build_test_map_records ()
{
  mapping_t *records = 0;

  /* *INDENT-OFF* */
  mapping_t r = {
    .ttl = MAP_REGISTER_DEFAULT_TTL,
    .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 = AF_IP4
        }
      }
    }
  };
  /* *INDENT-ON* */

  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);
  clib_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 */
  clib_memset ((u8 *) b->data + 4, 0, 8);

  /* clear authentication data */
  clib_memset ((u8 *) b->data + 16, 0, 20);

  /* *INDENT-OFF* */
  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 */
    /* 0x00, 0x00, 0x03, 0x84, */ /* default ttl (15 minues) */
    0x00, 0x01, 0x51, 0x80, /* default ttl (24h = 86400s) */
    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 */
  };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_data, b->data, sizeof (expected_data)));
done:
  clib_mem_free (data);
  return error;
}

static vlib_buffer_t *
create_buffer (u8 * data, u32 data_len)
{
  vlib_buffer_t *b;

  u8 *buf_data = clib_mem_alloc (500);
  clib_memset (buf_data, 0, 500);
  b = (vlib_buffer_t *) buf_data;

  u8 *p = vlib_buffer_put_uninit (b, data_len);
  clib_memcpy (p, data, data_len);

  return b;
}

static clib_error_t *
test_lisp_parse_map_reply ()
{
  clib_error_t *error = 0;

  /* *INDENT-OFF* */
  u8 map_reply_data[] =
    {
      0x00, 0x00, 0x00, 0x01, /* type; rsvd; mapping count */
      0x00, 0x00, 0x00, 0x00,
    };
  /* *INDENT-ON* */

  vlib_buffer_t *b = create_buffer (map_reply_data, sizeof (map_reply_data));
  map_records_arg_t *mrecs = parse_map_reply (b);
  _assert (0 == mrecs);
  clib_mem_free (b);

  /* *INDENT-OFF* */
  u8 map_reply_data2[] =
    {
      0x00, 0x00, 0x00, 0x01, /* type; rsvd */
      0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, /* nonce */

      /* 1. record  - incomplete */
      0x01, 0x02, 0x03, 0x04, /* record TTL */
      0x01,                   /* locator count */
    };
  /* *INDENT-ON* */

  b = create_buffer (map_reply_data2, sizeof (map_reply_data2));
  mrecs = parse_map_reply (b);
  _assert (0 == mrecs);
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_lisp_parse_lcaf ()
{
  int i;
  clib_error_t *error = 0;
  gid_address_t eid;
  locator_t *locs = 0;
  locator_t probed;
  vlib_buffer_t *b = 0;
  u32 buff_len = 500;

  b = clib_mem_alloc (buff_len);
  clib_memset ((u8 *) b, 0, buff_len);

  /* *INDENT-OFF* */
  u8 map_reply_records[] =
    {
      /* 1. record */
      0x01, 0x02, 0x03, 0x04, /* record TTL */
      0x03,                   /* locator count */
      0x00, 0x00, 0x00,       /* eid-mask-len; ... */
      0x00, 0x00,             /* reserved; map-version num */
      0x00, 0x01,             /* EID-Prefix-AFI */
      0x33, 0x44, 0x55, 0x66, /* eid-prefix */

      /* 1st locator */
      0x0a,                   /* prority */
      0x0b,                   /* weight */
      0x0c,                   /* m-prority */
      0x0d,                   /* m-weight */
      0x00, 0x00,             /* unused flags */
      0x40, 0x03,             /* Loc-AFI = LCAF*/

      /* LCAF header*/
      0x00, 0x00,             /* reserved1, flags */
      0x02,                   /* type = Instance ID */
      0x18,                   /* IID mask-len */
      0x00, 0x0a,             /* lenght */
      /* LCAF Instance ID */
      0x00, 0x00, 0x00, 0x09, /* iid */
      0x00, 0x01,             /* AFI = ipv4 */
      0x10, 0xbb, 0xcc, 0xdd, /* ipv4 loator address */

      /* 2nd locator */
      0x07,                   /* prority */
      0x06,                   /* weight */
      0x05,                   /* m-prority */
      0x04,                   /* m-weight */
      0x00, 0x00,             /* unused flags */
      0x40, 0x03,             /* Loc-AFI = LCAF*/

      /* LCAF header*/
      0x00, 0x00,             /* reserved1, flags */
      0x02,                   /* type = Instance ID */
      0x18,                   /* IID mask-len */
      0x00, 0x16,             /* iid length + next AFI lenght */
      /* LCAF Instance ID */
      0x22, 0x44, 0x66, 0x88, /* iid */
      0x00, 0x02,             /* AFI = ipv6 */
      0xcc, 0xdd, 0xee, 0xff,
      0x88, 0x99, 0xaa, 0xbb,
      0x44, 0x55, 0x66, 0x77,
      0x00, 0x11, 0x22, 0x33, /* ipv6 locator address */

      /* 3rd locator */
      0x0a,                   /* prority */
      0x0b,                   /* weight */
      0x0c,                   /* m-prority */
      0x0d,                   /* m-weight */
      0x00, 0x00,             /* unused flags */
      0x00, 0x01,             /* Loc-AFI */
      0xaa, 0xbb, 0xcc, 0xdd, /* Loator */
    };
  /* *INDENT-ON* */

  b->current_length = buff_len;
  memcpy (b->data, map_reply_records, sizeof (map_reply_records));

  lisp_msg_parse_mapping_record (b, &eid, &locs, &probed);
  _assert (vec_len (locs) == 3);
  _assert (eid.ippref.addr.ip.v4.as_u32 == 0x66554433);

  /* check 1st locator - an LCAF with ipv4 */
  _assert (locs[0].local == 0);
  _assert (locs[0].priority == 0xa);
  _assert (locs[0].weight == 0xb);
  _assert (locs[0].mpriority == 0xc);
  _assert (locs[0].mweight == 0xd);

  _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 (AF_IP4 == ip_prefix_version (ip_pref));

  /* 2nd locator - LCAF entry with ipv6 address */
  _assert (locs[1].local == 0);
  _assert (locs[1].priority == 0x7);
  _assert (locs[1].weight == 0x6);
  _assert (locs[1].mpriority == 0x5);
  _assert (locs[1].mweight == 0x4);

  _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 (AF_IP6 == ip_prefix_version (ip_pref));

  /* 3rd locator - simple ipv4 address */
  _assert (gid_address_type (&locs[2].address) == GID_ADDR_IP_PREFIX);
done:
  clib_mem_free (b);

  for (i = 0; i < 3; i++)
    locator_free (&locs[i]);
  vec_free (locs);
  return error;
}

#define foreach_test_case                 \
  _(lisp_msg_put_mreq)                    \
  _(lisp_msg_put_mreq_with_lcaf)          \
  _(lisp_msg_push_ecm)                    \
  _(lisp_msg_parse)                       \
  _(lisp_msg_parse_mapping_record)        \
  _(lisp_parse_map_reply)                 \
  _(lisp_parse_lcaf)                      \
  _(lisp_map_register)

static int
lisp_cp_serdes_tests (vlib_main_t * vm, unformat_input_t * input)
{
  clib_error_t *error;

#define _(_test_name)                   		\
  error = test_ ## _test_name ();       		\
  if (error)                            		\
    {                                   		\
      fformat (stderr, "FAIL: test_" #_test_name "\n");	\
      return -1;                        		\
    }							\
  else							\
      fformat (stderr, "PASS: test_" #_test_name "\n");	\

  foreach_test_case
#undef _
    return 0;
}

static clib_error_t *
test_locator_type (void)
{
  clib_error_t *error = 0;
  gid_address_t _gid_addr, *gid = &_gid_addr;
  ip_prefix_t *ippref;
  gid_address_type (gid) = GID_ADDR_IP_PREFIX;
  gid_address_ippref_len (gid) = 24;
  ippref = &gid_address_ippref (gid);
  ip_prefix_version (ippref) = AF_IP4;
  ip_prefix_len (ippref) = 0;
  ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
  ip4->as_u32 = 0x20304050;

  /* local locator */
  locator_t loc1, loc2 = {
    .local = 1,
    .state = 2,
    .sw_if_index = 8,
    .priority = 3,
    .weight = 100,
    .mpriority = 4,
    .mweight = 101
  };
  locator_copy (&loc1, &loc2);
  _assert (0 == locator_cmp (&loc1, &loc2));

  /* remote locator */
  loc2.local = 0;

  ip_prefix_t nested_ippref;
  ip_prefix_version (&nested_ippref) = AF_IP4;
  ip_prefix_len (&nested_ippref) = 0;
  ip4 = &ip_prefix_v4 (&nested_ippref);
  ip4->as_u32 = 0x33882299;
  gid_address_t nested_gid = {
    .type = GID_ADDR_IP_PREFIX,
    .ippref = nested_ippref
  };

  lcaf_t lcaf = {
    .type = LCAF_INSTANCE_ID,
    .uni = {
	    .vni_mask_len = 5,
	    .vni = 0xa1b2c3d4,
	    .gid_addr = &nested_gid}
  };
  gid_address_type (gid) = GID_ADDR_LCAF;
  gid_address_lcaf (gid) = lcaf;

  loc2.address = gid[0];
  locator_copy (&loc1, &loc2);

  _assert (0 == locator_cmp (&loc1, &loc2));

done:
  locator_free (&loc1);
  return error;
}

static clib_error_t *
test_gid_parse_ip_pref ()
{
  clib_error_t *error = 0;
  gid_address_t _gid_addr, *gid_addr = &_gid_addr;
  gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;

  /* *INDENT-OFF* */
  u8 data[] =
    {
      0x00, 0x01,             /* AFI = IPv4 */
      0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
    };
  /* *INDENT-ON* */

  u32 len = gid_address_parse (data, gid_addr);
  _assert (6 == len);
  gid_address_copy (copy, gid_addr);
  _assert (0 == gid_address_cmp (copy, gid_addr));
done:
  return error;
}

static clib_error_t *
test_gid_parse_mac ()
{
  clib_error_t *error = 0;
  gid_address_t _gid, *gid = &_gid;
  gid_address_t _gid_copy, *gid_copy = &_gid_copy;

  /* *INDENT-OFF* */
  u8 data[] =
    {
      0x40, 0x05,             /* AFI = MAC address */
      0x10, 0xbb, 0xcc, 0xdd, /* MAC */
      0x77, 0x99,
    };
  /* *INDENT-ON* */

  u32 len = gid_address_parse (data, gid);
  _assert (8 == len);
  _assert (GID_ADDR_MAC == gid_address_type (gid));
  gid_address_copy (gid_copy, gid);
  _assert (0 == gid_address_cmp (gid_copy, gid));
done:
  return error;
}

static clib_error_t *
test_gid_write_nsh (void)
{
  clib_error_t *error = 0;

  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  gid_address_t g = {
    .vni = 0,
    .nsh.spi = 0x112233,
    .nsh.si = 0x42,
    .type = GID_ADDR_NSH,
  };

  u16 len = gid_address_put (b, &g);

  /* *INDENT-OFF* */
  u8 expected[] =
    {
      0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
      0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */

      /* Service Path ID, Service index */
      0x11, 0x22, 0x33, 0x42, /* SPI, SI */
    };
  /* *INDENT-ON* */

  _assert (sizeof (expected) == len);
  _assert (0 == memcmp (expected, b, len));
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_gid_parse_nsh ()
{
  clib_error_t *error = 0;
  gid_address_t _gid_addr, *gid_addr = &_gid_addr;
  gid_address_t _gid_addr_copy, *copy = &_gid_addr_copy;

  clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
  clib_memset (copy, 0, sizeof (copy[0]));

  /* *INDENT-OFF* */
  u8 data[] =
    {
      0x40, 0x03, 0x00, 0x00, /* AFI = LCAF*/
      0x11, 0x00, 0x00, 0x04, /* type = SPI LCAF, length = 4 */

      /* Service Path ID, Service index */
      0x55, 0x99, 0x42, 0x09, /* SPI, SI */
    };
  /* *INDENT-ON* */

  u32 len = gid_address_parse (data, gid_addr);
  _assert (sizeof (data) == len);
  gid_address_copy (copy, gid_addr);
  _assert (0 == gid_address_cmp (gid_addr, copy));
  _assert (GID_ADDR_NSH == gid_address_type (copy));
  _assert (0 == gid_address_vni (copy));
  _assert (gid_address_nsh_spi (copy) == 0x559942);
  _assert (gid_address_nsh_si (copy) == 0x09);

done:
  gid_address_free (copy);
  gid_address_free (gid_addr);
  return error;
}

static clib_error_t *
test_gid_parse_lcaf ()
{
  clib_error_t *error = 0;
  gid_address_t _gid_addr, *gid_addr = &_gid_addr;
  gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;

  clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
  clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));

  /* *INDENT-OFF* */
  u8 data[] =
    {
      0x40, 0x03,             /* AFI = LCAF*/

      /* LCAF header*/
      0x00, 0x00,             /* reserved1, flags */
      0x02,                   /* type = Instance ID */
      0x18,                   /* IID mask-len */
      0x00, 0x0a,             /* iid length + next AFI lenght */
      /* LCAF Instance ID */
      0x00, 0x00, 0x00, 0x09, /* iid */
      0x00, 0x01,             /* AFI = ipv4 */
      0x10, 0xbb, 0xcc, 0xdd, /* ipv4 address */
    };
  /* *INDENT-ON* */

  u32 len = gid_address_parse (data, gid_addr);
  _assert (18 == len);
  gid_address_copy (gid_addr_copy, gid_addr);
  _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
  _assert (GID_ADDR_IP_PREFIX == gid_address_type (gid_addr));
  _assert (9 == gid_address_vni (gid_addr));
  _assert (0x18 == gid_address_vni_mask (gid_addr));
  _assert (0xddccbb10 == gid_addr->ippref.addr.ip.v4.as_u32);

done:
  gid_address_free (gid_addr);
  gid_address_free (gid_addr_copy);
  return error;
}

/* recursive LCAFs are not supported */
#if 0
static clib_error_t *
test_gid_parse_lcaf_complex ()
{
  clib_error_t *error = 0;
  gid_address_t _gid_addr, *gid_addr = &_gid_addr;
  gid_address_t _gid_addr_copy, *gid_addr_copy = &_gid_addr_copy;

  clib_memset (gid_addr, 0, sizeof (gid_addr[0]));
  clib_memset (gid_addr_copy, 0, sizeof (gid_addr_copy[0]));

  /* *INDENT-OFF* */
  u8 data[] = {
    0x40, 0x03,			/* AFI = LCAF */

    /* LCAF header */
    0x00, 0x00,			/* reserved1, flags */
    0x02,			/* type = Instance ID */
    0x18,			/* IID mask-len */
    0x00, 0x0a,			/* iid length + next AFI lenght */
    /* LCAF Instance ID */
    0x00, 0x00, 0x00, 0x0b,	/* iid */

    0x40, 0x03,			/* AFI = LCAF */
    /* LCAF header */
    0x00, 0x00,			/* reserved1, flags */
    0x02,			/* type = Instance ID */
    0x17,			/* IID mask-len */
    0x00, 0x0a,			/* iid length + next AFI lenght */
    /* LCAF Instance ID */
    0x00, 0x00, 0x00, 0x0c,	/* iid */

    0x40, 0x03,			/* AFI = LCAF */
    /* LCAF header */
    0x00, 0x00,			/* reserved1, flags */
    0x02,			/* type = Instance ID */
    0x16,			/* IID mask-len */
    0x00, 0x16,			/* iid length + next AFI lenght */
    /* LCAF Instance ID */
    0x00, 0x00, 0x00, 0x0d,	/* iid */

    0x00, 0x02,			/* AFI = IPv6 */

    0x10, 0xbb, 0xcc, 0xdd,
    0x10, 0xbb, 0xcc, 0xdd,
    0x10, 0xbb, 0xcc, 0xdd,
    0x10, 0xbb, 0xcc, 0xdd,	/* ipv6 address */
  };
  /* *INDENT-ON* */

  u32 len = gid_address_parse (data, gid_addr);
  _assert (54 == len);
  _assert (gid_addr->type == GID_ADDR_LCAF);
  gid_address_copy (gid_addr_copy, gid_addr);
  _assert (0 == gid_address_cmp (gid_addr_copy, gid_addr));
  _assert (gid_addr_copy->type == GID_ADDR_LCAF);

  lcaf_t *lcaf = &gid_address_lcaf (gid_addr_copy);
  _assert (lcaf->type == LCAF_INSTANCE_ID);
  vni_t *v = (vni_t *) lcaf;
  _assert (v->vni == 0x0b);
  _assert (v->vni_mask_len == 0x18);

  gid_address_t *tmp = vni_gid (v);
  _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
  lcaf = &gid_address_lcaf (tmp);
  _assert (lcaf->type == LCAF_INSTANCE_ID);

  v = (vni_t *) lcaf;
  _assert (v->vni == 0x0c);
  _assert (v->vni_mask_len == 0x17);

  tmp = vni_gid (v);
  _assert (gid_address_type (tmp) == GID_ADDR_LCAF);
  lcaf = &gid_address_lcaf (tmp);

  _assert (lcaf->type == LCAF_INSTANCE_ID);
  v = (vni_t *) lcaf;
  _assert (v->vni == 0x0d);
  _assert (v->vni_mask_len == 0x16);

  tmp = vni_gid (v);
  _assert (gid_address_type (tmp) == GID_ADDR_IP_PREFIX);

  ip_prefix_t *ip_pref = &gid_address_ippref (tmp);
  ip6_address_t *ip6 = &ip_prefix_v6 (ip_pref);
  _assert (ip6->as_u32[0] == 0xddccbb10);
  _assert (ip6->as_u32[1] == 0xddccbb10);
  _assert (ip6->as_u32[2] == 0xddccbb10);
  _assert (ip6->as_u32[3] == 0xddccbb10);
  _assert (ip_prefix_version (ip_pref) == AF_IP6);

done:
  gid_address_free (gid_addr);
  gid_address_free (gid_addr_copy);
  return error;
}
#endif

static clib_error_t *
test_write_mac_in_lcaf (void)
{
  clib_error_t *error = 0;

  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  gid_address_t g = {
    .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
    .vni = 0x01020304,
    .vni_mask = 0x10,
    .type = GID_ADDR_MAC,
  };

  u16 len = gid_address_put (b, &g);

  /* *INDENT-OFF* */
  u8 expected[] =
    {
      0x40, 0x03,             /* AFI = LCAF */
      0x00,                   /* reserved1 */
      0x00,                   /* flags */
      0x02,                   /* LCAF type = Instance ID */
      0x10,                   /* IID/IID mask len */
      0x00, 0x0c,             /* length */
      0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */

      0x40, 0x05,             /* AFI = MAC */
      0x01, 0x02, 0x03, 0x04,
      0x05, 0x06              /* MAC */
    };
  /* *INDENT-ON* */

  _assert (sizeof (expected) == len);
  _assert (0 == memcmp (expected, b, len));
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_mac_address_write (void)
{
  clib_error_t *error = 0;

  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  gid_address_t g = {
    .mac = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
    .type = GID_ADDR_MAC,
  };

  u16 len = gid_address_put (b, &g);
  _assert (8 == len);

  /* *INDENT-OFF* */
  u8 expected[] =
    {
      0x40, 0x05,             /* AFI = MAC */
      0x01, 0x02, 0x03, 0x04,
      0x05, 0x06              /* MAC */
    };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected, b, len));
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_src_dst_with_vni_serdes (void)
{
  clib_error_t *error = 0;
  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  /* *INDENT-OFF* */
  fid_address_t src =
    {
      .type = FID_ADDR_IP_PREF,
      .ippref =
        {
          .len = 24,
          .addr =
            {
              .version = AF_IP4,
              .ip.v4.data = { 0x1, 0x2, 0x3, 0x0 }
            }
        }
    };

  fid_address_t dst =
    {
      .type = FID_ADDR_IP_PREF,
      .ippref =
        {
          .len = 16,
          .addr =
            {
              .version = AF_IP4,
              .ip.v4.data = { 0x9, 0x8, 0x0, 0x0 }
            }
        }
    };

  source_dest_t sd =
    {
      .src = src,
      .dst = dst
    };

  gid_address_t g =
    {
      .sd = sd,
      .type = GID_ADDR_SRC_DST,
      .vni = 0x12345678,
      .vni_mask = 0x9
    };

  /* *INDENT-ON* */

  u16 size_to_put = gid_address_size_to_put (&g);
  _assert (36 == size_to_put);
  _assert (0 == gid_address_len (&g));

  u16 write_len = gid_address_put (b, &g);
  _assert (size_to_put == write_len);

  /* *INDENT-OFF* */
  u8 expected_data[] =
    {
      0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
      0x02, 0x09, 0x00, 0x1c,  /* LCAF type = IID, IID mask-len, length */
      0x12, 0x34, 0x56, 0x78,  /* reserved; source-ML, Dest-ML */

      0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
      0x0c, 0x00, 0x00, 0x10,  /* LCAF type = source/dest key, rsvd, length */
      0x00, 0x00, 0x18, 0x10,  /* reserved; source-ML, Dest-ML */

      0x00, 0x01,              /* AFI = ip4 */
      0x01, 0x02, 0x03, 0x00,  /* source */

      0x00, 0x01,              /* AFI = ip4 */
      0x09, 0x08, 0x00, 0x00,  /* destination */
    };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));

  gid_address_t p;
  clib_memset (&p, 0, sizeof (p));
  _assert (write_len == gid_address_parse (b, &p));
  _assert (0 == gid_address_cmp (&g, &p));
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_src_dst_deser_bad_afi (void)
{
  clib_error_t *error = 0;

  /* *INDENT-OFF* */
  u8 expected_data[] =
    {
      0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
      0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
      0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */

      0xde, 0xad,              /* AFI = bad value */
      0x11, 0x22, 0x33, 0x44,
      0x55, 0x66,              /* source */

      0x40, 0x05,              /* AFI = MAC */
      0x10, 0x21, 0x32, 0x43,
      0x54, 0x65,              /* destination */
    };
  /* *INDENT-ON* */

  gid_address_t p;
  _assert (~0 == gid_address_parse (expected_data, &p));
done:
  return error;
}

static clib_error_t *
test_src_dst_serdes (void)
{
  clib_error_t *error = 0;

  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  fid_address_t src = {
    .type = FID_ADDR_MAC,
    .mac = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}
  };

  fid_address_t dst = {
    .type = FID_ADDR_MAC,
    .mac = {0x10, 0x21, 0x32, 0x43, 0x54, 0x65}
  };

  source_dest_t sd = {
    .src = src,
    .dst = dst
  };

  gid_address_t g = {
    .sd = sd,
    .type = GID_ADDR_SRC_DST,
    .vni = 0x0,
    .vni_mask = 0x0
  };

  u16 size_to_put = gid_address_size_to_put (&g);
  _assert (28 == size_to_put);
  _assert (0 == gid_address_len (&g));

  u16 write_len = gid_address_put (b, &g);
  _assert (size_to_put == write_len);

  /* *INDENT-OFF* */
  u8 expected_data[] =
    {
      0x40, 0x03, 0x00, 0x00,  /* AFI = LCAF, reserved1, flags */
      0x0c, 0x00, 0x00, 0x14,  /* LCAF type = source/dest key, rsvd, length */
      0x00, 0x00, 0x00, 0x00,  /* reserved; source-ML, Dest-ML */

      0x40, 0x05,              /* AFI = MAC */
      0x11, 0x22, 0x33, 0x44,
      0x55, 0x66,              /* source */

      0x40, 0x05,              /* AFI = MAC */
      0x10, 0x21, 0x32, 0x43,
      0x54, 0x65,              /* destination */
    };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_data, b, sizeof (expected_data)));

  gid_address_t p;
  clib_memset (&p, 0, sizeof (p));
  _assert (write_len == gid_address_parse (b, &p));
  _assert (0 == gid_address_cmp (&g, &p));
done:
  clib_mem_free (b);
  return error;
}

static clib_error_t *
test_gid_address_write (void)
{
  clib_error_t *error = 0;
  ip_prefix_t ippref_data, *ippref = &ippref_data;

  u8 *b = clib_mem_alloc (500);
  clib_memset (b, 0, 500);

  ip_prefix_version (ippref) = AF_IP4;
  ip_prefix_len (ippref) = 9;
  ip4_address_t *ip4 = &ip_prefix_v4 (ippref);
  ip4->as_u32 = 0xaabbccdd;

  gid_address_t g = {
    .ippref = ippref[0],
    .type = GID_ADDR_IP_PREFIX,
    .vni = 0x01020304,
    .vni_mask = 0x18
  };

  _assert (18 == gid_address_size_to_put (&g));
  _assert (gid_address_len (&g) == 9);

  u16 write_len = gid_address_put (b, &g);
  _assert (18 == write_len);

  /* *INDENT-OFF* */
  u8 expected_gid_data[] =
    {
      0x40, 0x03,             /* AFI = LCAF */
      0x00,                   /* reserved1 */
      0x00,                   /* flags */
      0x02,                   /* LCAF type = Instance ID */
      0x18,                   /* IID/VNI mask len */
      0x00, 0x0a,             /* length */
      0x01, 0x02, 0x03, 0x04, /* Instance ID / VNI */

      0x00, 0x01,             /* AFI = IPv4 */
      0xdd, 0xcc, 0xbb, 0xaa, /* ipv4 addr */
    };
  /* *INDENT-ON* */

  _assert (0 == memcmp (expected_gid_data, b, sizeof (expected_gid_data)));
done:
  clib_mem_free (b);
  return error;
}

#undef foreach_test_case

#define foreach_test_case                 \
  _(locator_type)                         \
  _(gid_parse_ip_pref)                    \
  _(gid_parse_mac)                        \
  _(gid_parse_lcaf)                       \
  _(gid_parse_nsh)                        \
  _(gid_write_nsh)                        \
  _(mac_address_write)                    \
  _(gid_address_write)                    \
  _(src_dst_serdes)                       \
  _(write_mac_in_lcaf)                    \
  _(src_dst_deser_bad_afi)                \
  _(src_dst_with_vni_serdes)

static int
lisp_cp_types_tests (vlib_main_t * vm, unformat_input_t * input)
{
  clib_error_t *error;

#define _(_test_name)                   		\
  error = test_ ## _test_name ();       		\
  if (error)                            		\
    {                                   		\
      fformat (stderr, "FAIL: test_" #_test_name "\n");	\
      return -1;                        		\
    }							\
  else							\
      fformat (stderr, "PASS: test_" #_test_name "\n");	\

  foreach_test_case
#undef _
    return 0;
}

#undef _assert

static clib_error_t *
lisp_cp_test (vlib_main_t * vm, unformat_input_t * input,
	      vlib_cli_command_t * cmd_arg)
{
  int res = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "serdes"))
	{
	  res = lisp_cp_serdes_tests (vm, input);
	}
      if (unformat (input, "types"))
	{
	  res = lisp_cp_types_tests (vm, input);
	}
      else if (unformat (input, "all"))
	{
	  if ((res = lisp_cp_serdes_tests (vm, input)))
	    goto done;
	  if ((res = lisp_cp_types_tests (vm, input)))
	    goto done;
	}
      else
	break;
    }

done:
  if (res)
    return clib_error_return (0, "rbtree unit test failed");
  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (lisp_cp_command, static) =
{
  .path = "test lisp cp",
  .short_help = "lisp cp internal unit tests",
  .function = lisp_cp_test,
};
/* *INDENT-ON* */


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