aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python
diff options
context:
space:
mode:
authorjuraj.linkes <juraj.linkes@pantheon.tech>2019-07-10 11:32:47 +0200
committerPeter Mikus <pmikus@cisco.com>2019-07-17 07:47:32 +0000
commit248d1a52e06622dc9eb1dfdd6ca9f6670b4c0bc3 (patch)
tree75693f1fe6a233a542d78ba64e80c4c5621ed3ac /resources/libraries/python
parent5287b5307b3e39c4e13545b0c6ea651ac4014a84 (diff)
Refactor VPP Device VM vhost tests
* replace the current VM image with kernel img * rework keyword usage to make it consistent with performance tests * remove resources/libraries/robot/shared/qemu.robot as it's not used anywhere anymore Change-Id: Ia5bc19e9e6ed9af031e4d9b5c0c89431fb49fd33 Signed-off-by: juraj.linkes <juraj.linkes@pantheon.tech>
Diffstat (limited to 'resources/libraries/python')
-rw-r--r--resources/libraries/python/Constants.py3
-rw-r--r--resources/libraries/python/CpuUtils.py13
-rw-r--r--resources/libraries/python/QemuUtils.py78
3 files changed, 58 insertions, 36 deletions
diff --git a/resources/libraries/python/Constants.py b/resources/libraries/python/Constants.py
index 877fc25012..ddff2b161c 100644
--- a/resources/libraries/python/Constants.py
+++ b/resources/libraries/python/Constants.py
@@ -61,6 +61,9 @@ class Constants(object):
# QEMU VM kernel image path
QEMU_VM_KERNEL = '/opt/boot/vmlinuz'
+ # QEMU VM kernel initrd path
+ QEMU_VM_KERNEL_INITRD = '/opt/boot/initrd.img'
+
# QEMU VM nested image path
QEMU_VM_IMAGE = '/var/lib/vm/vhost-nested.img'
diff --git a/resources/libraries/python/CpuUtils.py b/resources/libraries/python/CpuUtils.py
index 67bf312f5d..07c5032aa8 100644
--- a/resources/libraries/python/CpuUtils.py
+++ b/resources/libraries/python/CpuUtils.py
@@ -62,15 +62,20 @@ class CpuUtils(object):
return bool(count == cpu_mems_len)
@staticmethod
- def get_cpu_layout_from_all_nodes(nodes):
- """Retrieve cpu layout from all nodes, assuming all nodes
- are Linux nodes.
+ def get_cpu_info_from_all_nodes(nodes):
+ """Assuming all nodes are Linux nodes, retrieve the following
+ cpu information from all nodes:
+ - cpu architecture
+ - cpu layout
:param nodes: DICT__nodes from Topology.DICT__nodes.
:type nodes: dict
- :raises RuntimeError: If the ssh command "lscpu -p" fails.
+ :raises RuntimeError: If an ssh command retrieving cpu information
+ fails.
"""
for node in nodes.values():
+ stdout, _ = exec_cmd_no_error(node, 'uname -m')
+ node['arch'] = stdout.strip()
stdout, _ = exec_cmd_no_error(node, 'lscpu -p')
node['cpuinfo'] = list()
for line in stdout.split("\n"):
diff --git a/resources/libraries/python/QemuUtils.py b/resources/libraries/python/QemuUtils.py
index 9c021763bd..6a63798b1e 100644
--- a/resources/libraries/python/QemuUtils.py
+++ b/resources/libraries/python/QemuUtils.py
@@ -13,13 +13,13 @@
"""QEMU utilities library."""
-from time import sleep
-from string import Template
-import json
-from re import match
# Disable due to pylint bug
# pylint: disable=no-name-in-module,import-error
from distutils.version import StrictVersion
+import json
+from re import match
+from string import Template
+from time import sleep
from robot.api import logger
from resources.libraries.python.Constants import Constants
@@ -59,6 +59,11 @@ class QemuUtils(object):
"""
self._vhost_id = 0
self._node = node
+ self._arch = Topology.get_node_arch(self._node)
+ dpdk_target = 'arm64-armv8a' if self._arch == 'aarch64' \
+ else 'x86_64-native'
+ self._testpmd_path = '{path}/{dpdk_target}-linuxapp-gcc/app'\
+ .format(path=Constants.QEMU_VM_DPDK, dpdk_target=dpdk_target)
self._vm_info = {
'host': node['host'],
'type': NodeType.VM,
@@ -82,16 +87,25 @@ class QemuUtils(object):
# Temporary files.
self._temp = dict()
self._temp['pidfile'] = '/var/run/qemu_{id}.pid'.format(id=qemu_id)
- if '/var/lib/vm/' in img:
+ if img == Constants.QEMU_VM_IMAGE:
self._opt['vm_type'] = 'nestedvm'
self._temp['qmp'] = '/var/run/qmp_{id}.sock'.format(id=qemu_id)
self._temp['qga'] = '/var/run/qga_{id}.sock'.format(id=qemu_id)
- elif '/opt/boot/vmlinuz' in img:
+ elif img == Constants.QEMU_VM_KERNEL:
+ self._opt['img'], _ = exec_cmd_no_error(
+ node,
+ 'ls -1 {img}* | tail -1'.format(img=Constants.QEMU_VM_KERNEL),
+ message='Qemu Kernel VM image not found!')
self._opt['vm_type'] = 'kernelvm'
self._temp['log'] = '/tmp/serial_{id}.log'.format(id=qemu_id)
self._temp['ini'] = '/etc/vm_init_{id}.conf'.format(id=qemu_id)
+ self._opt['initrd'], _ = exec_cmd_no_error(
+ node,
+ 'ls -1 {initrd}* | tail -1'.format(
+ initrd=Constants.QEMU_VM_KERNEL_INITRD),
+ message='Qemu Kernel initrd image not found!')
else:
- raise RuntimeError('QEMU: Unknown VM image option!')
+ raise RuntimeError('QEMU: Unknown VM image option: {}'.format(img))
# Computed parameters for QEMU command line.
self._params = OptionString(prefix='-')
self.add_params()
@@ -119,8 +133,13 @@ class QemuUtils(object):
self._params.add('enable-kvm')
self._params.add_with_value('pidfile', self._temp.get('pidfile'))
self._params.add_with_value('cpu', 'host')
+
+ if self._arch == 'aarch64':
+ machine_args = 'virt,accel=kvm,usb=off,mem-merge=off,gic-version=3'
+ else:
+ machine_args = 'pc,accel=kvm,usb=off,mem-merge=off'
self._params.add_with_value(
- 'machine', 'pc,accel=kvm,usb=off,mem-merge=off')
+ 'machine', machine_args)
self._params.add_with_value(
'smp', '{smp},sockets=1,cores={smp},threads=1'.format(
smp=self._opt.get('smp')))
@@ -163,21 +182,22 @@ class QemuUtils(object):
def add_kernelvm_params(self):
"""Set KernelVM QEMU parameters."""
- self._params.add_with_value(
- 'chardev', 'file,id=char0,path={log}'.format(
- log=self._temp.get('log')))
- self._params.add_with_value('device', 'isa-serial,chardev=char0')
+ console = 'ttyAMA0' if self._arch == 'aarch64' else 'ttyS0'
+ self._params.add_with_value('serial', 'file:{log}'.format(
+ log=self._temp.get('log')))
self._params.add_with_value(
'fsdev', 'local,id=root9p,path=/,security_model=none')
self._params.add_with_value(
- 'device', 'virtio-9p-pci,fsdev=root9p,mount_tag=/dev/root')
+ 'device', 'virtio-9p-pci,fsdev=root9p,mount_tag=virtioroot')
+ self._params.add_with_value(
+ 'kernel', '{img}'.format(img=self._opt.get('img')))
self._params.add_with_value(
- 'kernel', '$(readlink -m {img}* | tail -1)'.format(
- img=self._opt.get('img')))
+ 'initrd', '{initrd}'.format(initrd=self._opt.get('initrd')))
self._params.add_with_value(
- 'append', '"ro rootfstype=9p rootflags=trans=virtio console=ttyS0'
- ' tsc=reliable hugepages=256 init={init}"'.format(
- init=self._temp.get('ini')))
+ 'append', '"ro rootfstype=9p rootflags=trans=virtio '
+ 'root=virtioroot console={console} tsc=reliable '
+ 'hugepages=256 init={init}"'.format(
+ console=console, init=self._temp.get('ini')))
def create_kernelvm_config_vpp(self, **kwargs):
"""Create QEMU VPP config files.
@@ -203,8 +223,9 @@ class QemuUtils(object):
vpp_config.add_unix_cli_listen()
vpp_config.add_unix_exec(running)
vpp_config.add_cpu_main_core('0')
- vpp_config.add_cpu_corelist_workers('1-{smp}'.
- format(smp=self._opt.get('smp')-1))
+ if self._opt.get('smp') > 1:
+ vpp_config.add_cpu_corelist_workers('1-{smp}'.format(
+ smp=self._opt.get('smp')-1))
vpp_config.add_dpdk_dev('0000:00:06.0', '0000:00:07.0')
vpp_config.add_dpdk_dev_default_rxq(kwargs['queues'])
vpp_config.add_dpdk_log_level('debug')
@@ -233,9 +254,6 @@ class QemuUtils(object):
:param kwargs: Key-value pairs to construct command line parameters.
:type kwargs: dict
"""
- testpmd_path = ('{path}/{arch}-native-linuxapp-gcc/app'.
- format(path=Constants.QEMU_VM_DPDK,
- arch=Topology.get_node_arch(self._node)))
testpmd_cmd = DpdkUtil.get_testpmd_cmdline(
eal_corelist='0-{smp}'.format(smp=self._opt.get('smp') - 1),
eal_driver=False,
@@ -249,7 +267,7 @@ class QemuUtils(object):
pmd_nb_cores=str(self._opt.get('smp') - 1))
self._opt['vnf_bin'] = ('{testpmd_path}/{testpmd_cmd}'.
- format(testpmd_path=testpmd_path,
+ format(testpmd_path=self._testpmd_path,
testpmd_cmd=testpmd_cmd))
def create_kernelvm_config_testpmd_mac(self, **kwargs):
@@ -258,9 +276,6 @@ class QemuUtils(object):
:param kwargs: Key-value pairs to construct command line parameters.
:type kwargs: dict
"""
- testpmd_path = ('{path}/{arch}-native-linuxapp-gcc/app'.
- format(path=Constants.QEMU_VM_DPDK,
- arch=Topology.get_node_arch(self._node)))
testpmd_cmd = DpdkUtil.get_testpmd_cmdline(
eal_corelist='0-{smp}'.format(smp=self._opt.get('smp') - 1),
eal_driver=False,
@@ -277,7 +292,7 @@ class QemuUtils(object):
pmd_nb_cores=str(self._opt.get('smp') - 1))
self._opt['vnf_bin'] = ('{testpmd_path}/{testpmd_cmd}'.
- format(testpmd_path=testpmd_path,
+ format(testpmd_path=self._testpmd_path,
testpmd_cmd=testpmd_cmd))
def create_kernelvm_init(self, **kwargs):
@@ -407,7 +422,7 @@ class QemuUtils(object):
format(queue_size=queue_size)) if queue_size else ''
mbuf = 'on,host_mtu=9200'
self._params.add_with_value(
- 'device', 'virtio-net-pci,netdev=vhost{vhost},mac={mac},bus=pci.0,'
+ 'device', 'virtio-net-pci,netdev=vhost{vhost},mac={mac},'
'addr={addr}.0,mq=on,vectors={vectors},csum=off,gso=off,'
'guest_tso4=off,guest_tso6=off,guest_ecn=off,mrg_rxbuf={mbuf},'
'{queue_size}'.format(
@@ -597,8 +612,7 @@ class QemuUtils(object):
"""
cmd_opts = OptionString()
cmd_opts.add('{bin_path}/qemu-system-{arch}'.format(
- bin_path=Constants.QEMU_BIN_PATH,
- arch=Topology.get_node_arch(self._node)))
+ bin_path=Constants.QEMU_BIN_PATH, arch=self._arch))
cmd_opts.extend(self._params)
message = ('QEMU: Start failed on {host}!'.
format(host=self._node['host']))
@@ -643,7 +657,7 @@ class QemuUtils(object):
"""
command = ('{bin_path}/qemu-system-{arch} --version'.format(
bin_path=Constants.QEMU_BIN_PATH,
- arch=Topology.get_node_arch(self._node)))
+ arch=self._arch))
try:
stdout, _ = exec_cmd_no_error(self._node, command, sudo=True)
ver = match(r'QEMU emulator version ([\d.]*)', stdout).group(1)
href='#n869'>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 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
/*
 * Copyright (c) 2015 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 "map.h"

#include "../ip/ip_frag.h"

#define IP6_MAP_T_DUAL_LOOP

typedef enum
{
  IP6_MAPT_NEXT_MAPT_TCP_UDP,
  IP6_MAPT_NEXT_MAPT_ICMP,
  IP6_MAPT_NEXT_MAPT_FRAGMENTED,
  IP6_MAPT_NEXT_DROP,
  IP6_MAPT_N_NEXT
} ip6_mapt_next_t;

typedef enum
{
  IP6_MAPT_ICMP_NEXT_IP4_LOOKUP,
  IP6_MAPT_ICMP_NEXT_IP4_FRAG,
  IP6_MAPT_ICMP_NEXT_DROP,
  IP6_MAPT_ICMP_N_NEXT
} ip6_mapt_icmp_next_t;

typedef enum
{
  IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP,
  IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG,
  IP6_MAPT_TCP_UDP_NEXT_DROP,
  IP6_MAPT_TCP_UDP_N_NEXT
} ip6_mapt_tcp_udp_next_t;

typedef enum
{
  IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP,
  IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG,
  IP6_MAPT_FRAGMENTED_NEXT_DROP,
  IP6_MAPT_FRAGMENTED_N_NEXT
} ip6_mapt_fragmented_next_t;

static_always_inline int
ip6_map_fragment_cache (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
			map_domain_t * d, u16 port)
{
  u32 *ignore = NULL;
  map_ip4_reass_lock ();
  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
					  ip6_map_t_embedded_address (d,
								      &ip6->
								      dst_address),
					  frag_id_6to4 (frag->identification),
					  (ip6->protocol ==
					   IP_PROTOCOL_ICMP6) ?
					  IP_PROTOCOL_ICMP : ip6->protocol,
					  &ignore);
  if (r)
    r->port = port;

  map_ip4_reass_unlock ();
  return !r;
}

/* Returns the associated port or -1 */
static_always_inline i32
ip6_map_fragment_get (ip6_header_t * ip6, ip6_frag_hdr_t * frag,
		      map_domain_t * d)
{
  u32 *ignore = NULL;
  map_ip4_reass_lock ();
  map_ip4_reass_t *r = map_ip4_reass_get (map_get_ip4 (&ip6->src_address),
					  ip6_map_t_embedded_address (d,
								      &ip6->
								      dst_address),
					  frag_id_6to4 (frag->identification),
					  (ip6->protocol ==
					   IP_PROTOCOL_ICMP6) ?
					  IP_PROTOCOL_ICMP : ip6->protocol,
					  &ignore);
  i32 ret = r ? r->port : -1;
  map_ip4_reass_unlock ();
  return ret;
}

static_always_inline u8
ip6_translate_tos (const ip6_header_t * ip6)
{
#ifdef IP6_MAP_T_OVERRIDE_TOS
  return IP6_MAP_T_OVERRIDE_TOS;
#else
  return (clib_net_to_host_u32 (ip6->ip_version_traffic_class_and_flow_label)
	  & 0x0ff00000) >> 20;
#endif
}

//TODO: Find right place in memory for that
/* *INDENT-OFF* */
static u8 icmp6_to_icmp_updater_pointer_table[] =
  { 0, 1, ~0, ~0,
    2, 2, 9, 8,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    12, 12, 12, 12,
    24, 24, 24, 24,
    24, 24, 24, 24,
    24, 24, 24, 24,
    24, 24, 24, 24
  };
/* *INDENT-ON* */

static_always_inline int
ip6_icmp_to_icmp6_in_place (icmp46_header_t * icmp, u32 icmp_len,
			    i32 * sender_port, ip6_header_t ** inner_ip6)
{
  *inner_ip6 = NULL;
  switch (icmp->type)
    {
    case ICMP6_echo_request:
      *sender_port = ((u16 *) icmp)[2];
      icmp->type = ICMP4_echo_request;
      break;
    case ICMP6_echo_reply:
      *sender_port = ((u16 *) icmp)[2];
      icmp->type = ICMP4_echo_reply;
      break;
    case ICMP6_destination_unreachable:
      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);

      switch (icmp->code)
	{
	case ICMP6_destination_unreachable_no_route_to_destination:	//0
	case ICMP6_destination_unreachable_beyond_scope_of_source_address:	//2
	case ICMP6_destination_unreachable_address_unreachable:	//3
	  icmp->type = ICMP4_destination_unreachable;
	  icmp->code =
	    ICMP4_destination_unreachable_destination_unreachable_host;
	  break;
	case ICMP6_destination_unreachable_destination_administratively_prohibited:	//1
	  icmp->type =
	    ICMP4_destination_unreachable;
	  icmp->code =
	    ICMP4_destination_unreachable_communication_administratively_prohibited;
	  break;
	case ICMP6_destination_unreachable_port_unreachable:
	  icmp->type = ICMP4_destination_unreachable;
	  icmp->code = ICMP4_destination_unreachable_port_unreachable;
	  break;
	default:
	  return -1;
	}
      break;
    case ICMP6_packet_too_big:
      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);

      icmp->type = ICMP4_destination_unreachable;
      icmp->code = 4;
      {
	u32 advertised_mtu = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
	advertised_mtu -= 20;
	//FIXME: = minimum(advertised MTU-20, MTU_of_IPv4_nexthop, (MTU_of_IPv6_nexthop)-20)
	((u16 *) (icmp))[3] = clib_host_to_net_u16 (advertised_mtu);
      }
      break;

    case ICMP6_time_exceeded:
      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);

      icmp->type = ICMP4_time_exceeded;
      break;

    case ICMP6_parameter_problem:
      *inner_ip6 = (ip6_header_t *) u8_ptr_add (icmp, 8);
      *sender_port = ip6_get_port (*inner_ip6, MAP_RECEIVER, icmp_len);

      switch (icmp->code)
	{
	case ICMP6_parameter_problem_erroneous_header_field:
	  icmp->type = ICMP4_parameter_problem;
	  icmp->code = ICMP4_parameter_problem_pointer_indicates_error;
	  u32 pointer = clib_net_to_host_u32 (*((u32 *) (icmp + 1)));
	  if (pointer >= 40)
	    return -1;

	  ((u8 *) (icmp + 1))[0] =
	    icmp6_to_icmp_updater_pointer_table[pointer];
	  break;
	case ICMP6_parameter_problem_unrecognized_next_header:
	  icmp->type = ICMP4_destination_unreachable;
	  icmp->code = ICMP4_destination_unreachable_port_unreachable;
	  break;
	case ICMP6_parameter_problem_unrecognized_option:
	default:
	  return -1;
	}
      break;
    default:
      return -1;
      break;
    }
  return 0;
}

static_always_inline void
_ip6_map_t_icmp (map_domain_t * d, vlib_buffer_t * p, u8 * error)
{
  ip6_header_t *ip6, *inner_ip6;
  ip4_header_t *ip4, *inner_ip4;
  u32 ip6_pay_len;
  icmp46_header_t *icmp;
  i32 sender_port;
  ip_csum_t csum;
  u32 ip4_sadr, inner_ip4_dadr;

  ip6 = vlib_buffer_get_current (p);
  ip6_pay_len = clib_net_to_host_u16 (ip6->payload_length);
  icmp = (icmp46_header_t *) (ip6 + 1);
  ASSERT (ip6_pay_len + sizeof (*ip6) <= p->current_length);

  if (ip6->protocol != IP_PROTOCOL_ICMP6)
    {
      //No extensions headers allowed here
      //TODO: SR header
      *error = MAP_ERROR_MALFORMED;
      return;
    }

  //There are no fragmented ICMP messages, so no extension header for now

  if (ip6_icmp_to_icmp6_in_place
      (icmp, ip6_pay_len, &sender_port, &inner_ip6))
    {
      //TODO: In case of 1:1 mapping it is not necessary to have the sender port
      *error = MAP_ERROR_ICMP;
      return;
    }

  if (sender_port < 0)
    {
      // In case of 1:1 mapping, we don't care about the port
      if (d->ea_bits_len == 0 && d->rules)
	{
	  sender_port = 0;
	}
      else
	{
	  *error = MAP_ERROR_ICMP;
	  return;
	}
    }

  //Security check
  //Note that this prevents an intermediate IPv6 router from answering the request
  ip4_sadr = map_get_ip4 (&ip6->src_address);
  if (ip6->src_address.as_u64[0] != map_get_pfx_net (d, ip4_sadr, sender_port)
      || ip6->src_address.as_u64[1] != map_get_sfx_net (d, ip4_sadr,
							sender_port))
    {
      *error = MAP_ERROR_SEC_CHECK;
      return;
    }

  if (inner_ip6)
    {
      u16 *inner_L4_checksum, inner_l4_offset, inner_frag_offset,
	inner_frag_id;
      u8 *inner_l4, inner_protocol;

      //We have two headers to translate
      //   FROM
      //   [   IPv6   ]<- ext ->[IC][   IPv6   ]<- ext ->[L4 header ...
      // Handled cases:
      //                     [   IPv6   ][IC][   IPv6   ][L4 header ...
      //                 [   IPv6   ][IC][   IPv6   ][Fr][L4 header ...
      //    TO
      //                               [ IPv4][IC][ IPv4][L4 header ...

      //TODO: This was already done deep in ip6_icmp_to_icmp6_in_place
      //We shouldn't have to do it again
      if (ip6_parse (inner_ip6, ip6_pay_len - 8,
		     &inner_protocol, &inner_l4_offset, &inner_frag_offset))
	{
	  *error = MAP_ERROR_MALFORMED;
	  return;
	}

      inner_l4 = u8_ptr_add (inner_ip6, inner_l4_offset);
      inner_ip4 =
	(ip4_header_t *) u8_ptr_add (inner_l4, -sizeof (*inner_ip4));
      if (inner_frag_offset)
	{
	  ip6_frag_hdr_t *inner_frag =
	    (ip6_frag_hdr_t *) u8_ptr_add (inner_ip6, inner_frag_offset);
	  inner_frag_id = frag_id_6to4 (inner_frag->identification);
	}
      else
	{
	  inner_frag_id = 0;
	}

      //Do the translation of the inner packet
      if (inner_protocol == IP_PROTOCOL_TCP)
	{
	  inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 16);
	}
      else if (inner_protocol == IP_PROTOCOL_UDP)
	{
	  inner_L4_checksum = (u16 *) u8_ptr_add (inner_l4, 6);
	}
      else if (inner_protocol == IP_PROTOCOL_ICMP6)
	{
	  icmp46_header_t *inner_icmp = (icmp46_header_t *) inner_l4;
	  csum = inner_icmp->checksum;
	  csum = ip_csum_sub_even (csum, *((u16 *) inner_icmp));
	  //It cannot be of a different type as ip6_icmp_to_icmp6_in_place succeeded
	  inner_icmp->type = (inner_icmp->type == ICMP6_echo_request) ?
	    ICMP4_echo_request : ICMP4_echo_reply;
	  csum = ip_csum_add_even (csum, *((u16 *) inner_icmp));
	  inner_icmp->checksum = ip_csum_fold (csum);
	  inner_protocol = IP_PROTOCOL_ICMP;	//Will be copied to ip6 later
	  inner_L4_checksum = &inner_icmp->checksum;
	}
      else
	{
	  *error = MAP_ERROR_BAD_PROTOCOL;
	  return;
	}

      csum = *inner_L4_checksum;
      csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[0]);
      csum = ip_csum_sub_even (csum, inner_ip6->src_address.as_u64[1]);
      csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[0]);
      csum = ip_csum_sub_even (csum, inner_ip6->dst_address.as_u64[1]);

      //Sanity check of the outer destination address
      if (ip6->dst_address.as_u64[0] != inner_ip6->src_address.as_u64[0] &&
	  ip6->dst_address.as_u64[1] != inner_ip6->src_address.as_u64[1])
	{
	  *error = MAP_ERROR_SEC_CHECK;
	  return;
	}

      //Security check of inner packet
      inner_ip4_dadr = map_get_ip4 (&inner_ip6->dst_address);
      if (inner_ip6->dst_address.as_u64[0] !=
	  map_get_pfx_net (d, inner_ip4_dadr, sender_port)
	  || inner_ip6->dst_address.as_u64[1] != map_get_sfx_net (d,
								  inner_ip4_dadr,
								  sender_port))
	{
	  *error = MAP_ERROR_SEC_CHECK;
	  return;
	}

      inner_ip4->dst_address.as_u32 = inner_ip4_dadr;
      inner_ip4->src_address.as_u32 =
	ip6_map_t_embedded_address (d, &inner_ip6->src_address);
      inner_ip4->ip_version_and_header_length =
	IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
      inner_ip4->tos = ip6_translate_tos (inner_ip6);
      inner_ip4->length =
	u16_net_add (inner_ip6->payload_length,
		     sizeof (*ip4) + sizeof (*ip6) - inner_l4_offset);
      inner_ip4->fragment_id = inner_frag_id;
      inner_ip4->flags_and_fragment_offset =
	clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
      inner_ip4->ttl = inner_ip6->hop_limit;
      inner_ip4->protocol = inner_protocol;
      inner_ip4->checksum = ip4_header_checksum (inner_ip4);

      if (inner_ip4->protocol == IP_PROTOCOL_ICMP)
	{
	  //Remove remainings of the pseudo-header in the csum
	  csum =
	    ip_csum_sub_even (csum, clib_host_to_net_u16 (IP_PROTOCOL_ICMP6));
	  csum =
	    ip_csum_sub_even (csum, inner_ip4->length - sizeof (*inner_ip4));
	}
      else
	{
	  //Update to new pseudo-header
	  csum = ip_csum_add_even (csum, inner_ip4->src_address.as_u32);
	  csum = ip_csum_add_even (csum, inner_ip4->dst_address.as_u32);
	}
      *inner_L4_checksum = ip_csum_fold (csum);

      //Move up icmp header
      ip4 = (ip4_header_t *) u8_ptr_add (inner_l4, -2 * sizeof (*ip4) - 8);
      clib_memcpy (u8_ptr_add (inner_l4, -sizeof (*ip4) - 8), icmp, 8);
      icmp = (icmp46_header_t *) u8_ptr_add (inner_l4, -sizeof (*ip4) - 8);
    }
  else
    {
      //Only one header to translate
      ip4 = (ip4_header_t *) u8_ptr_add (ip6, sizeof (*ip6) - sizeof (*ip4));
    }
  vlib_buffer_advance (p, (u32) (((u8 *) ip4) - ((u8 *) ip6)));

  ip4->dst_address.as_u32 = ip6_map_t_embedded_address (d, &ip6->dst_address);
  ip4->src_address.as_u32 = ip4_sadr;
  ip4->ip_version_and_header_length =
    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
  ip4->tos = ip6_translate_tos (ip6);
  ip4->fragment_id = 0;
  ip4->flags_and_fragment_offset = 0;
  ip4->ttl = ip6->hop_limit;
  ip4->protocol = IP_PROTOCOL_ICMP;
  //TODO fix the length depending on offset length
  ip4->length = u16_net_add (ip6->payload_length,
			     (inner_ip6 ==
			      NULL) ? sizeof (*ip4) : (2 * sizeof (*ip4) -
						       sizeof (*ip6)));
  ip4->checksum = ip4_header_checksum (ip4);

  //TODO: We could do an easy diff-checksum for echo requests/replies
  //Recompute ICMP checksum
  icmp->checksum = 0;
  csum =
    ip_incremental_checksum (0, icmp,
			     clib_net_to_host_u16 (ip4->length) -
			     sizeof (*ip4));
  icmp->checksum = ~ip_csum_fold (csum);
}

static uword
ip6_map_t_icmp (vlib_main_t * vm,
		vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
  vlib_node_runtime_t *error_node =
    vlib_node_get_runtime (vm, ip6_map_t_icmp_node.index);
  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;
  vlib_combined_counter_main_t *cm = map_main.domain_counters;
  u32 cpu_index = os_get_cpu_number ();

  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 pi0;
	  vlib_buffer_t *p0;
	  u8 error0;
	  ip6_mapt_icmp_next_t next0;
	  map_domain_t *d0;
	  u16 len0;

	  pi0 = to_next[0] = from[0];
	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  error0 = MAP_ERROR_NONE;
	  next0 = IP6_MAPT_ICMP_NEXT_IP4_LOOKUP;

	  p0 = vlib_get_buffer (vm, pi0);
	  len0 =
	    clib_net_to_host_u16 (((ip6_header_t *)
				   vlib_buffer_get_current
				   (p0))->payload_length);
	  d0 =
	    pool_elt_at_index (map_main.domains,
			       vnet_buffer (p0)->map_t.map_domain_index);
	  _ip6_map_t_icmp (d0, p0, &error0);

	  if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
	    {
	      //Send to fragmentation node if necessary
	      vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
	      vnet_buffer (p0)->ip_frag.header_offset = 0;
	      vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next0 = IP6_MAPT_ICMP_NEXT_IP4_FRAG;
	    }

	  if (PREDICT_TRUE (error0 == MAP_ERROR_NONE))
	    {
	      vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
					       cpu_index,
					       vnet_buffer (p0)->
					       map_t.map_domain_index, 1,
					       len0);
	    }
	  else
	    {
	      next0 = IP6_MAPT_ICMP_NEXT_DROP;
	    }

	  p0->error = error_node->errors[error0];
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next, pi0,
					   next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
  return frame->n_vectors;
}

static uword
ip6_map_t_fragmented (vlib_main_t * vm,
		      vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

#ifdef IP6_MAP_T_DUAL_LOOP
      while (n_left_from >= 4 && n_left_to_next >= 2)
	{
	  u32 pi0, pi1;
	  vlib_buffer_t *p0, *p1;
	  ip6_header_t *ip60, *ip61;
	  ip6_frag_hdr_t *frag0, *frag1;
	  ip4_header_t *ip40, *ip41;
	  u16 frag_id0, frag_offset0, frag_id1, frag_offset1;
	  u8 frag_more0, frag_more1;
	  u32 next0, next1;

	  pi0 = to_next[0] = from[0];
	  pi1 = to_next[1] = from[1];
	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
	  next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
	  p0 = vlib_get_buffer (vm, pi0);
	  p1 = vlib_get_buffer (vm, pi1);
	  ip60 = vlib_buffer_get_current (p0);
	  ip61 = vlib_buffer_get_current (p1);
	  frag0 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip60,
					   vnet_buffer (p0)->map_t.
					   v6.frag_offset);
	  frag1 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip61,
					   vnet_buffer (p1)->map_t.
					   v6.frag_offset);
	  ip40 =
	    (ip4_header_t *) u8_ptr_add (ip60,
					 vnet_buffer (p0)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  ip41 =
	    (ip4_header_t *) u8_ptr_add (ip61,
					 vnet_buffer (p1)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  vlib_buffer_advance (p0,
			       vnet_buffer (p0)->map_t.v6.l4_offset -
			       sizeof (*ip40));
	  vlib_buffer_advance (p1,
			       vnet_buffer (p1)->map_t.v6.l4_offset -
			       sizeof (*ip40));

	  frag_id0 = frag_id_6to4 (frag0->identification);
	  frag_id1 = frag_id_6to4 (frag1->identification);
	  frag_more0 = ip6_frag_hdr_more (frag0);
	  frag_more1 = ip6_frag_hdr_more (frag1);
	  frag_offset0 = ip6_frag_hdr_offset (frag0);
	  frag_offset1 = ip6_frag_hdr_offset (frag1);

	  ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
	  ip41->dst_address.as_u32 = vnet_buffer (p1)->map_t.v6.daddr;
	  ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
	  ip41->src_address.as_u32 = vnet_buffer (p1)->map_t.v6.saddr;
	  ip40->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip41->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip40->tos = ip6_translate_tos (ip60);
	  ip41->tos = ip6_translate_tos (ip61);
	  ip40->length = u16_net_add (ip60->payload_length,
				      sizeof (*ip40) -
				      vnet_buffer (p0)->map_t.v6.l4_offset +
				      sizeof (*ip60));
	  ip41->length =
	    u16_net_add (ip61->payload_length,
			 sizeof (*ip40) -
			 vnet_buffer (p1)->map_t.v6.l4_offset +
			 sizeof (*ip60));
	  ip40->fragment_id = frag_id0;
	  ip41->fragment_id = frag_id1;
	  ip40->flags_and_fragment_offset =
	    clib_host_to_net_u16 (frag_offset0 |
				  (frag_more0 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
				   : 0));
	  ip41->flags_and_fragment_offset =
	    clib_host_to_net_u16 (frag_offset1 |
				  (frag_more1 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
				   : 0));
	  ip40->ttl = ip60->hop_limit;
	  ip41->ttl = ip61->hop_limit;
	  ip40->protocol =
	    (vnet_buffer (p0)->map_t.v6.l4_protocol ==
	     IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p0)->
	    map_t.v6.l4_protocol;
	  ip41->protocol =
	    (vnet_buffer (p1)->map_t.v6.l4_protocol ==
	     IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p1)->
	    map_t.v6.l4_protocol;
	  ip40->checksum = ip4_header_checksum (ip40);
	  ip41->checksum = ip4_header_checksum (ip41);

	  if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
	    {
	      vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
	      vnet_buffer (p0)->ip_frag.header_offset = 0;
	      vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
	    }

	  if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
	    {
	      vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
	      vnet_buffer (p1)->ip_frag.header_offset = 0;
	      vnet_buffer (p1)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next1 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
	    }

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
					   to_next, n_left_to_next, pi0, pi1,
					   next0, next1);
	}
#endif

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 pi0;
	  vlib_buffer_t *p0;
	  ip6_header_t *ip60;
	  ip6_frag_hdr_t *frag0;
	  ip4_header_t *ip40;
	  u16 frag_id0;
	  u8 frag_more0;
	  u16 frag_offset0;
	  u32 next0;

	  pi0 = to_next[0] = from[0];
	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;

	  next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
	  p0 = vlib_get_buffer (vm, pi0);
	  ip60 = vlib_buffer_get_current (p0);
	  frag0 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip60,
					   vnet_buffer (p0)->map_t.
					   v6.frag_offset);
	  ip40 =
	    (ip4_header_t *) u8_ptr_add (ip60,
					 vnet_buffer (p0)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  vlib_buffer_advance (p0,
			       vnet_buffer (p0)->map_t.v6.l4_offset -
			       sizeof (*ip40));

	  frag_id0 = frag_id_6to4 (frag0->identification);
	  frag_more0 = ip6_frag_hdr_more (frag0);
	  frag_offset0 = ip6_frag_hdr_offset (frag0);

	  ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
	  ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
	  ip40->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip40->tos = ip6_translate_tos (ip60);
	  ip40->length = u16_net_add (ip60->payload_length,
				      sizeof (*ip40) -
				      vnet_buffer (p0)->map_t.v6.l4_offset +
				      sizeof (*ip60));
	  ip40->fragment_id = frag_id0;
	  ip40->flags_and_fragment_offset =
	    clib_host_to_net_u16 (frag_offset0 |
				  (frag_more0 ? IP4_HEADER_FLAG_MORE_FRAGMENTS
				   : 0));
	  ip40->ttl = ip60->hop_limit;
	  ip40->protocol =
	    (vnet_buffer (p0)->map_t.v6.l4_protocol ==
	     IP_PROTOCOL_ICMP6) ? IP_PROTOCOL_ICMP : vnet_buffer (p0)->
	    map_t.v6.l4_protocol;
	  ip40->checksum = ip4_header_checksum (ip40);

	  if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
	    {
	      //Send to fragmentation node if necessary
	      vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
	      vnet_buffer (p0)->ip_frag.header_offset = 0;
	      vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next0 = IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG;
	    }

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next, pi0,
					   next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
  return frame->n_vectors;
}

static uword
ip6_map_t_tcp_udp (vlib_main_t * vm,
		   vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;
  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

#ifdef IP6_MAP_T_DUAL_LOOP
      while (n_left_from >= 4 && n_left_to_next >= 2)
	{
	  u32 pi0, pi1;
	  vlib_buffer_t *p0, *p1;
	  ip6_header_t *ip60, *ip61;
	  ip_csum_t csum0, csum1;
	  ip4_header_t *ip40, *ip41;
	  u16 fragment_id0, flags0, *checksum0,
	    fragment_id1, flags1, *checksum1;
	  ip6_mapt_tcp_udp_next_t next0, next1;

	  pi0 = to_next[0] = from[0];
	  pi1 = to_next[1] = from[1];
	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;
	  next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;
	  next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;

	  p0 = vlib_get_buffer (vm, pi0);
	  p1 = vlib_get_buffer (vm, pi1);
	  ip60 = vlib_buffer_get_current (p0);
	  ip61 = vlib_buffer_get_current (p1);
	  ip40 =
	    (ip4_header_t *) u8_ptr_add (ip60,
					 vnet_buffer (p0)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  ip41 =
	    (ip4_header_t *) u8_ptr_add (ip61,
					 vnet_buffer (p1)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  vlib_buffer_advance (p0,
			       vnet_buffer (p0)->map_t.v6.l4_offset -
			       sizeof (*ip40));
	  vlib_buffer_advance (p1,
			       vnet_buffer (p1)->map_t.v6.l4_offset -
			       sizeof (*ip40));
	  checksum0 =
	    (u16 *) u8_ptr_add (ip60,
				vnet_buffer (p0)->map_t.checksum_offset);
	  checksum1 =
	    (u16 *) u8_ptr_add (ip61,
				vnet_buffer (p1)->map_t.checksum_offset);

	  csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
	  csum1 = ip_csum_sub_even (*checksum1, ip61->src_address.as_u64[0]);
	  csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
	  csum1 = ip_csum_sub_even (csum1, ip61->src_address.as_u64[1]);
	  csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[0]);
	  csum1 = ip_csum_sub_even (csum0, ip61->dst_address.as_u64[0]);
	  csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
	  csum1 = ip_csum_sub_even (csum1, ip61->dst_address.as_u64[1]);
	  csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.daddr);
	  csum1 = ip_csum_add_even (csum1, vnet_buffer (p1)->map_t.v6.daddr);
	  csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.saddr);
	  csum1 = ip_csum_add_even (csum1, vnet_buffer (p1)->map_t.v6.saddr);
	  *checksum0 = ip_csum_fold (csum0);
	  *checksum1 = ip_csum_fold (csum1);

	  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset))
	    {
	      ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip60,
								   vnet_buffer
								   (p0)->
								   map_t.
								   v6.frag_offset);
	      fragment_id0 = frag_id_6to4 (hdr->identification);
	      flags0 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
	    }
	  else
	    {
	      fragment_id0 = 0;
	      flags0 = 0;
	    }

	  if (PREDICT_FALSE (vnet_buffer (p1)->map_t.v6.frag_offset))
	    {
	      ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip61,
								   vnet_buffer
								   (p1)->
								   map_t.
								   v6.frag_offset);
	      fragment_id1 = frag_id_6to4 (hdr->identification);
	      flags1 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
	    }
	  else
	    {
	      fragment_id1 = 0;
	      flags1 = 0;
	    }

	  ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
	  ip41->dst_address.as_u32 = vnet_buffer (p1)->map_t.v6.daddr;
	  ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
	  ip41->src_address.as_u32 = vnet_buffer (p1)->map_t.v6.saddr;
	  ip40->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip41->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip40->tos = ip6_translate_tos (ip60);
	  ip41->tos = ip6_translate_tos (ip61);
	  ip40->length = u16_net_add (ip60->payload_length,
				      sizeof (*ip40) + sizeof (*ip60) -
				      vnet_buffer (p0)->map_t.v6.l4_offset);
	  ip41->length =
	    u16_net_add (ip61->payload_length,
			 sizeof (*ip40) + sizeof (*ip60) -
			 vnet_buffer (p1)->map_t.v6.l4_offset);
	  ip40->fragment_id = fragment_id0;
	  ip41->fragment_id = fragment_id1;
	  ip40->flags_and_fragment_offset = flags0;
	  ip41->flags_and_fragment_offset = flags1;
	  ip40->ttl = ip60->hop_limit;
	  ip41->ttl = ip61->hop_limit;
	  ip40->protocol = vnet_buffer (p0)->map_t.v6.l4_protocol;
	  ip41->protocol = vnet_buffer (p1)->map_t.v6.l4_protocol;
	  ip40->checksum = ip4_header_checksum (ip40);
	  ip41->checksum = ip4_header_checksum (ip41);

	  if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
	    {
	      vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
	      vnet_buffer (p0)->ip_frag.header_offset = 0;
	      vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
	    }

	  if (vnet_buffer (p1)->map_t.mtu < p1->current_length)
	    {
	      vnet_buffer (p1)->ip_frag.mtu = vnet_buffer (p1)->map_t.mtu;
	      vnet_buffer (p1)->ip_frag.header_offset = 0;
	      vnet_buffer (p1)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next1 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
	    }

	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
					   n_left_to_next, pi0, pi1, next0,
					   next1);
	}
#endif

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 pi0;
	  vlib_buffer_t *p0;
	  ip6_header_t *ip60;
	  u16 *checksum0;
	  ip_csum_t csum0;
	  ip4_header_t *ip40;
	  u16 fragment_id0;
	  u16 flags0;
	  ip6_mapt_tcp_udp_next_t next0;

	  pi0 = to_next[0] = from[0];
	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP;

	  p0 = vlib_get_buffer (vm, pi0);
	  ip60 = vlib_buffer_get_current (p0);
	  ip40 =
	    (ip4_header_t *) u8_ptr_add (ip60,
					 vnet_buffer (p0)->map_t.
					 v6.l4_offset - sizeof (*ip40));
	  vlib_buffer_advance (p0,
			       vnet_buffer (p0)->map_t.v6.l4_offset -
			       sizeof (*ip40));
	  checksum0 =
	    (u16 *) u8_ptr_add (ip60,
				vnet_buffer (p0)->map_t.checksum_offset);

	  //TODO: This can probably be optimized
	  csum0 = ip_csum_sub_even (*checksum0, ip60->src_address.as_u64[0]);
	  csum0 = ip_csum_sub_even (csum0, ip60->src_address.as_u64[1]);
	  csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[0]);
	  csum0 = ip_csum_sub_even (csum0, ip60->dst_address.as_u64[1]);
	  csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.daddr);
	  csum0 = ip_csum_add_even (csum0, vnet_buffer (p0)->map_t.v6.saddr);
	  *checksum0 = ip_csum_fold (csum0);

	  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset))
	    {
	      //Only the first fragment
	      ip6_frag_hdr_t *hdr = (ip6_frag_hdr_t *) u8_ptr_add (ip60,
								   vnet_buffer
								   (p0)->
								   map_t.
								   v6.frag_offset);
	      fragment_id0 = frag_id_6to4 (hdr->identification);
	      flags0 = clib_host_to_net_u16 (IP4_HEADER_FLAG_MORE_FRAGMENTS);
	    }
	  else
	    {
	      fragment_id0 = 0;
	      flags0 = 0;
	    }

	  ip40->dst_address.as_u32 = vnet_buffer (p0)->map_t.v6.daddr;
	  ip40->src_address.as_u32 = vnet_buffer (p0)->map_t.v6.saddr;
	  ip40->ip_version_and_header_length =
	    IP4_VERSION_AND_HEADER_LENGTH_NO_OPTIONS;
	  ip40->tos = ip6_translate_tos (ip60);
	  ip40->length = u16_net_add (ip60->payload_length,
				      sizeof (*ip40) + sizeof (*ip60) -
				      vnet_buffer (p0)->map_t.v6.l4_offset);
	  ip40->fragment_id = fragment_id0;
	  ip40->flags_and_fragment_offset = flags0;
	  ip40->ttl = ip60->hop_limit;
	  ip40->protocol = vnet_buffer (p0)->map_t.v6.l4_protocol;
	  ip40->checksum = ip4_header_checksum (ip40);

	  if (vnet_buffer (p0)->map_t.mtu < p0->current_length)
	    {
	      //Send to fragmentation node if necessary
	      vnet_buffer (p0)->ip_frag.mtu = vnet_buffer (p0)->map_t.mtu;
	      vnet_buffer (p0)->ip_frag.header_offset = 0;
	      vnet_buffer (p0)->ip_frag.next_index = IP4_FRAG_NEXT_IP4_LOOKUP;
	      next0 = IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG;
	    }

	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next, pi0,
					   next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
  return frame->n_vectors;
}

static_always_inline void
ip6_map_t_classify (vlib_buffer_t * p0, ip6_header_t * ip60,
		    map_domain_t * d0, i32 * src_port0,
		    u8 * error0, ip6_mapt_next_t * next0,
		    u32 l4_len0, ip6_frag_hdr_t * frag0)
{
  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
		     ip6_frag_hdr_offset (frag0)))
    {
      *next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
      if (d0->ea_bits_len == 0 && d0->rules)
	{
	  *src_port0 = 0;
	}
      else
	{
	  *src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
	  *error0 = (*src_port0 != -1) ? *error0 : MAP_ERROR_FRAGMENT_DROPPED;
	}
    }
  else
    if (PREDICT_TRUE
	(vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
    {
      *error0 =
	l4_len0 < sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : *error0;
      vnet_buffer (p0)->map_t.checksum_offset =
	vnet_buffer (p0)->map_t.v6.l4_offset + 16;
      *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
      *src_port0 =
	(i32) *
	((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
    }
  else
    if (PREDICT_TRUE
	(vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
    {
      *error0 =
	l4_len0 < sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : *error0;
      vnet_buffer (p0)->map_t.checksum_offset =
	vnet_buffer (p0)->map_t.v6.l4_offset + 6;
      *next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
      *src_port0 =
	(i32) *
	((u16 *) u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
    }
  else if (vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_ICMP6)
    {
      *error0 =
	l4_len0 < sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : *error0;
      *next0 = IP6_MAPT_NEXT_MAPT_ICMP;
      if (d0->ea_bits_len == 0 && d0->rules)
	{
	  *src_port0 = 0;
	}
      else
	if (((icmp46_header_t *)
	     u8_ptr_add (ip60,
			 vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
	    ICMP6_echo_reply
	    || ((icmp46_header_t *)
		u8_ptr_add (ip60,
			    vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
	    ICMP6_echo_request)
	{
	  *src_port0 =
	    (i32) *
	    ((u16 *)
	     u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset + 6));
	}
    }
  else
    {
      //TODO: In case of 1:1 mapping, it might be possible to do something with those packets.
      *error0 = MAP_ERROR_BAD_PROTOCOL;
    }
}

static uword
ip6_map_t (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
  vlib_node_runtime_t *error_node =
    vlib_node_get_runtime (vm, ip6_map_t_node.index);
  vlib_combined_counter_main_t *cm = map_main.domain_counters;
  u32 cpu_index = os_get_cpu_number ();

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;
  next_index = node->cached_next_index;
  while (n_left_from > 0)
    {
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

#ifdef IP6_MAP_T_DUAL_LOOP
      while (n_left_from >= 4 && n_left_to_next >= 2)
	{
	  u32 pi0, pi1;
	  vlib_buffer_t *p0, *p1;
	  ip6_header_t *ip60, *ip61;
	  u8 error0, error1;
	  ip6_mapt_next_t next0, next1;
	  u32 l4_len0, l4_len1;
	  i32 src_port0, src_port1;
	  map_domain_t *d0, *d1;
	  ip6_frag_hdr_t *frag0, *frag1;
	  u32 saddr0, saddr1;
	  next0 = next1 = 0;	//Because compiler whines

	  pi0 = to_next[0] = from[0];
	  pi1 = to_next[1] = from[1];
	  from += 2;
	  n_left_from -= 2;
	  to_next += 2;
	  n_left_to_next -= 2;

	  error0 = MAP_ERROR_NONE;
	  error1 = MAP_ERROR_NONE;

	  p0 = vlib_get_buffer (vm, pi0);
	  p1 = vlib_get_buffer (vm, pi1);
	  ip60 = vlib_buffer_get_current (p0);
	  ip61 = vlib_buffer_get_current (p1);

	  saddr0 = map_get_ip4 (&ip60->src_address);
	  saddr1 = map_get_ip4 (&ip61->src_address);
	  d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
				   (ip4_address_t *) & saddr0,
				   &vnet_buffer (p0)->map_t.map_domain_index,
				   &error0);
	  d1 =
	    ip6_map_get_domain (vnet_buffer (p1)->ip.adj_index[VLIB_TX],
				(ip4_address_t *) & saddr1,
				&vnet_buffer (p1)->map_t.map_domain_index,
				&error1);

	  vnet_buffer (p0)->map_t.v6.saddr = saddr0;
	  vnet_buffer (p1)->map_t.v6.saddr = saddr1;
	  vnet_buffer (p0)->map_t.v6.daddr =
	    ip6_map_t_embedded_address (d0, &ip60->dst_address);
	  vnet_buffer (p1)->map_t.v6.daddr =
	    ip6_map_t_embedded_address (d1, &ip61->dst_address);
	  vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;
	  vnet_buffer (p1)->map_t.mtu = d1->mtu ? d1->mtu : ~0;

	  if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
					&(vnet_buffer (p0)->map_t.
					  v6.l4_protocol),
					&(vnet_buffer (p0)->map_t.
					  v6.l4_offset),
					&(vnet_buffer (p0)->map_t.
					  v6.frag_offset))))
	    {
	      error0 = MAP_ERROR_MALFORMED;
	      next0 = IP6_MAPT_NEXT_DROP;
	    }

	  if (PREDICT_FALSE (ip6_parse (ip61, p1->current_length,
					&(vnet_buffer (p1)->map_t.
					  v6.l4_protocol),
					&(vnet_buffer (p1)->map_t.
					  v6.l4_offset),
					&(vnet_buffer (p1)->map_t.
					  v6.frag_offset))))
	    {
	      error1 = MAP_ERROR_MALFORMED;
	      next1 = IP6_MAPT_NEXT_DROP;
	    }

	  src_port0 = src_port1 = -1;
	  l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
	    sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
	  l4_len1 = (u32) clib_net_to_host_u16 (ip61->payload_length) +
	    sizeof (*ip60) - vnet_buffer (p1)->map_t.v6.l4_offset;
	  frag0 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip60,
					   vnet_buffer (p0)->map_t.
					   v6.frag_offset);
	  frag1 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip61,
					   vnet_buffer (p1)->map_t.
					   v6.frag_offset);

	  ip6_map_t_classify (p0, ip60, d0, &src_port0, &error0, &next0,
			      l4_len0, frag0);
	  ip6_map_t_classify (p1, ip61, d1, &src_port1, &error1, &next1,
			      l4_len1, frag1);

	  if (PREDICT_FALSE
	      ((src_port0 != -1)
	       && (ip60->src_address.as_u64[0] !=
		   map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
				    src_port0)
		   || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
								      vnet_buffer
								      (p0)->map_t.v6.saddr,
								      src_port0))))
	    {
	      error0 = MAP_ERROR_SEC_CHECK;
	    }

	  if (PREDICT_FALSE
	      ((src_port1 != -1)
	       && (ip61->src_address.as_u64[0] !=
		   map_get_pfx_net (d1, vnet_buffer (p1)->map_t.v6.saddr,
				    src_port1)
		   || ip61->src_address.as_u64[1] != map_get_sfx_net (d1,
								      vnet_buffer
								      (p1)->map_t.v6.saddr,
								      src_port1))))
	    {
	      error1 = MAP_ERROR_SEC_CHECK;
	    }

	  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
			     !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
						   u8_ptr_add (ip60,
							       vnet_buffer
							       (p0)->map_t.
							       v6.frag_offset)))
	      && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
	      && (error0 == MAP_ERROR_NONE))
	    {
	      ip6_map_fragment_cache (ip60,
				      (ip6_frag_hdr_t *) u8_ptr_add (ip60,
								     vnet_buffer
								     (p0)->map_t.
								     v6.frag_offset),
				      d0, src_port0);
	    }

	  if (PREDICT_FALSE (vnet_buffer (p1)->map_t.v6.frag_offset &&
			     !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
						   u8_ptr_add (ip61,
							       vnet_buffer
							       (p1)->map_t.
							       v6.frag_offset)))
	      && (src_port1 != -1) && (d1->ea_bits_len != 0 || !d1->rules)
	      && (error1 == MAP_ERROR_NONE))
	    {
	      ip6_map_fragment_cache (ip61,
				      (ip6_frag_hdr_t *) u8_ptr_add (ip61,
								     vnet_buffer
								     (p1)->map_t.
								     v6.frag_offset),
				      d1, src_port1);
	    }

	  if (PREDICT_TRUE
	      (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
	    {
	      vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
					       cpu_index,
					       vnet_buffer (p0)->
					       map_t.map_domain_index, 1,
					       clib_net_to_host_u16
					       (ip60->payload_length));
	    }

	  if (PREDICT_TRUE
	      (error1 == MAP_ERROR_NONE && next1 != IP6_MAPT_NEXT_MAPT_ICMP))
	    {
	      vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
					       cpu_index,
					       vnet_buffer (p1)->
					       map_t.map_domain_index, 1,
					       clib_net_to_host_u16
					       (ip61->payload_length));
	    }

	  next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
	  next1 = (error1 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next1;
	  p0->error = error_node->errors[error0];
	  p1->error = error_node->errors[error1];
	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
					   n_left_to_next, pi0, pi1, next0,
					   next1);
	}
#endif

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 pi0;
	  vlib_buffer_t *p0;
	  ip6_header_t *ip60;
	  u8 error0;
	  u32 l4_len0;
	  i32 src_port0;
	  map_domain_t *d0;
	  ip6_frag_hdr_t *frag0;
	  ip6_mapt_next_t next0 = 0;
	  u32 saddr;

	  pi0 = to_next[0] = from[0];
	  from += 1;
	  n_left_from -= 1;
	  to_next += 1;
	  n_left_to_next -= 1;
	  error0 = MAP_ERROR_NONE;

	  p0 = vlib_get_buffer (vm, pi0);
	  ip60 = vlib_buffer_get_current (p0);
	  //Save saddr in a different variable to not overwrite ip.adj_index
	  saddr = map_get_ip4 (&ip60->src_address);
	  d0 = ip6_map_get_domain (vnet_buffer (p0)->ip.adj_index[VLIB_TX],
				   (ip4_address_t *) & saddr,
				   &vnet_buffer (p0)->map_t.map_domain_index,
				   &error0);

	  //FIXME: What if d0 is null
	  vnet_buffer (p0)->map_t.v6.saddr = saddr;
	  vnet_buffer (p0)->map_t.v6.daddr =
	    ip6_map_t_embedded_address (d0, &ip60->dst_address);
	  vnet_buffer (p0)->map_t.mtu = d0->mtu ? d0->mtu : ~0;

	  if (PREDICT_FALSE (ip6_parse (ip60, p0->current_length,
					&(vnet_buffer (p0)->map_t.
					  v6.l4_protocol),
					&(vnet_buffer (p0)->map_t.
					  v6.l4_offset),
					&(vnet_buffer (p0)->map_t.
					  v6.frag_offset))))
	    {
	      error0 = MAP_ERROR_MALFORMED;
	      next0 = IP6_MAPT_NEXT_DROP;
	    }

	  src_port0 = -1;
	  l4_len0 = (u32) clib_net_to_host_u16 (ip60->payload_length) +
	    sizeof (*ip60) - vnet_buffer (p0)->map_t.v6.l4_offset;
	  frag0 =
	    (ip6_frag_hdr_t *) u8_ptr_add (ip60,
					   vnet_buffer (p0)->map_t.
					   v6.frag_offset);


	  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
			     ip6_frag_hdr_offset (frag0)))
	    {
	      src_port0 = ip6_map_fragment_get (ip60, frag0, d0);
	      error0 = (src_port0 != -1) ? error0 : MAP_ERROR_FRAGMENT_MEMORY;
	      next0 = IP6_MAPT_NEXT_MAPT_FRAGMENTED;
	    }
	  else
	    if (PREDICT_TRUE
		(vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_TCP))
	    {
	      error0 =
		l4_len0 <
		sizeof (tcp_header_t) ? MAP_ERROR_MALFORMED : error0;
	      vnet_buffer (p0)->map_t.checksum_offset =
		vnet_buffer (p0)->map_t.v6.l4_offset + 16;
	      next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
	      src_port0 =
		(i32) *
		((u16 *)
		 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
	    }
	  else
	    if (PREDICT_TRUE
		(vnet_buffer (p0)->map_t.v6.l4_protocol == IP_PROTOCOL_UDP))
	    {
	      error0 =
		l4_len0 <
		sizeof (udp_header_t) ? MAP_ERROR_MALFORMED : error0;
	      vnet_buffer (p0)->map_t.checksum_offset =
		vnet_buffer (p0)->map_t.v6.l4_offset + 6;
	      next0 = IP6_MAPT_NEXT_MAPT_TCP_UDP;
	      src_port0 =
		(i32) *
		((u16 *)
		 u8_ptr_add (ip60, vnet_buffer (p0)->map_t.v6.l4_offset));
	    }
	  else if (vnet_buffer (p0)->map_t.v6.l4_protocol ==
		   IP_PROTOCOL_ICMP6)
	    {
	      error0 =
		l4_len0 <
		sizeof (icmp46_header_t) ? MAP_ERROR_MALFORMED : error0;
	      next0 = IP6_MAPT_NEXT_MAPT_ICMP;
	      if (((icmp46_header_t *)
		   u8_ptr_add (ip60,
			       vnet_buffer (p0)->map_t.v6.l4_offset))->code ==
		  ICMP6_echo_reply
		  || ((icmp46_header_t *)
		      u8_ptr_add (ip60,
				  vnet_buffer (p0)->map_t.v6.
				  l4_offset))->code == ICMP6_echo_request)
		src_port0 =
		  (i32) *
		  ((u16 *)
		   u8_ptr_add (ip60,
			       vnet_buffer (p0)->map_t.v6.l4_offset + 6));
	    }
	  else
	    {
	      //TODO: In case of 1:1 mapping, it might be possible to do something with those packets.
	      error0 = MAP_ERROR_BAD_PROTOCOL;
	    }

	  //Security check
	  if (PREDICT_FALSE
	      ((src_port0 != -1)
	       && (ip60->src_address.as_u64[0] !=
		   map_get_pfx_net (d0, vnet_buffer (p0)->map_t.v6.saddr,
				    src_port0)
		   || ip60->src_address.as_u64[1] != map_get_sfx_net (d0,
								      vnet_buffer
								      (p0)->map_t.v6.saddr,
								      src_port0))))
	    {
	      //Security check when src_port0 is not zero (non-first fragment, UDP or TCP)
	      error0 = MAP_ERROR_SEC_CHECK;
	    }

	  //Fragmented first packet needs to be cached for following packets
	  if (PREDICT_FALSE (vnet_buffer (p0)->map_t.v6.frag_offset &&
			     !ip6_frag_hdr_offset ((ip6_frag_hdr_t *)
						   u8_ptr_add (ip60,
							       vnet_buffer
							       (p0)->map_t.
							       v6.frag_offset)))
	      && (src_port0 != -1) && (d0->ea_bits_len != 0 || !d0->rules)
	      && (error0 == MAP_ERROR_NONE))
	    {
	      ip6_map_fragment_cache (ip60,
				      (ip6_frag_hdr_t *) u8_ptr_add (ip60,
								     vnet_buffer
								     (p0)->map_t.
								     v6.frag_offset),
				      d0, src_port0);
	    }

	  if (PREDICT_TRUE
	      (error0 == MAP_ERROR_NONE && next0 != IP6_MAPT_NEXT_MAPT_ICMP))
	    {
	      vlib_increment_combined_counter (cm + MAP_DOMAIN_COUNTER_RX,
					       cpu_index,
					       vnet_buffer (p0)->
					       map_t.map_domain_index, 1,
					       clib_net_to_host_u16
					       (ip60->payload_length));
	    }

	  next0 = (error0 != MAP_ERROR_NONE) ? IP6_MAPT_NEXT_DROP : next0;
	  p0->error = error_node->errors[error0];
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next, pi0,
					   next0);
	}
      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }
  return frame->n_vectors;
}

static char *map_t_error_strings[] = {
#define _(sym,string) string,
  foreach_map_error
#undef _
};

/* *INDENT-OFF* */
VLIB_REGISTER_NODE(ip6_map_t_fragmented_node) = {
  .function = ip6_map_t_fragmented,
  .name = "ip6-map-t-fragmented",
  .vector_size = sizeof (u32),
  .format_trace = format_map_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = MAP_N_ERROR,
  .error_strings = map_t_error_strings,

  .n_next_nodes = IP6_MAPT_FRAGMENTED_N_NEXT,
  .next_nodes = {
      [IP6_MAPT_FRAGMENTED_NEXT_IP4_LOOKUP] = "ip4-lookup",
      [IP6_MAPT_FRAGMENTED_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
      [IP6_MAPT_FRAGMENTED_NEXT_DROP] = "error-drop",
  },
};
/* *INDENT-ON* */

/* *INDENT-OFF* */
VLIB_REGISTER_NODE(ip6_map_t_icmp_node) = {
  .function = ip6_map_t_icmp,
  .name = "ip6-map-t-icmp",
  .vector_size = sizeof (u32),
  .format_trace = format_map_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = MAP_N_ERROR,
  .error_strings = map_t_error_strings,

  .n_next_nodes = IP6_MAPT_ICMP_N_NEXT,
  .next_nodes = {
      [IP6_MAPT_ICMP_NEXT_IP4_LOOKUP] = "ip4-lookup",
      [IP6_MAPT_ICMP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
      [IP6_MAPT_ICMP_NEXT_DROP] = "error-drop",
  },
};
/* *INDENT-ON* */

/* *INDENT-OFF* */
VLIB_REGISTER_NODE(ip6_map_t_tcp_udp_node) = {
  .function = ip6_map_t_tcp_udp,
  .name = "ip6-map-t-tcp-udp",
  .vector_size = sizeof (u32),
  .format_trace = format_map_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = MAP_N_ERROR,
  .error_strings = map_t_error_strings,

  .n_next_nodes = IP6_MAPT_TCP_UDP_N_NEXT,
  .next_nodes = {
      [IP6_MAPT_TCP_UDP_NEXT_IP4_LOOKUP] = "ip4-lookup",
      [IP6_MAPT_TCP_UDP_NEXT_IP4_FRAG] = IP4_FRAG_NODE_NAME,
      [IP6_MAPT_TCP_UDP_NEXT_DROP] = "error-drop",
  },
};
/* *INDENT-ON* */

/* *INDENT-OFF* */
VLIB_REGISTER_NODE(ip6_map_t_node) = {
  .function = ip6_map_t,
  .name = "ip6-map-t",
  .vector_size = sizeof(u32),
  .format_trace = format_map_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = MAP_N_ERROR,
  .error_strings = map_t_error_strings,

  .n_next_nodes = IP6_MAPT_N_NEXT,
  .next_nodes = {
      [IP6_MAPT_NEXT_MAPT_TCP_UDP] = "ip6-map-t-tcp-udp",
      [IP6_MAPT_NEXT_MAPT_ICMP] = "ip6-map-t-icmp",
      [IP6_MAPT_NEXT_MAPT_FRAGMENTED] = "ip6-map-t-fragmented",
      [IP6_MAPT_NEXT_DROP] = "error-drop",
  },
};
/* *INDENT-ON* */

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