summaryrefslogtreecommitdiffstats
path: root/test/test_acl_plugin.py
blob: 036a5dbc0c20bb70d87f4d82a0ea890e0a06ea2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Copyright (c) 2021 Cisco and/or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
#
# Licensed under the Apache License 2.0 or
# GNU General Public License v2.0 or later;  you may not use this file
# except in compliance with one of these Licenses. You
# may obtain a copy of the Licenses at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
#
# Note: If this file is linked with Scapy, which is GPLv2+, your use of it
# must be under GPLv2+.  If at any point in the future it is no longer linked
# with Scapy (or other GPLv2+ licensed software), you are free to choose
# Apache 2.
#
# 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.

"""Stream profile for T-rex traffic generator.

Stream profile:
 - Two streams sent in directions 0 --> 1 and 1 --> 0 at the same time.
 - Packet: ETH / IP /
 - Direction 0 --> 1:
   - Source IP address range:      10.0.0.1
   - Destination IP address range: 20.0.0.0 - 20.15.66.63
 - Direction 1 --> 0:
   - Source IP address range:      20.0.0.1
   - Destination IP address range: 10.0.0.0 - 10.15.66.63
"""

from trex.stl.api import *
from profile_trex_stateless_base_class import TrafficStreamsBaseClass


class TrafficStreams(TrafficStreamsBaseClass):
    """Stream profile."""

    def __init__(self):
        """Initialization and setting of streams' parameters."""

        super(TrafficStreamsBaseClass, self).__init__()

        # IPs used in packet headers.
        self.p1_src_start_ip = u"10.0.0.1"
        self.p1_dst_start_ip = u"20.0.0.0"
        self.p1_dst_end_ip = u"20.15.66.63"

        self.p2_src_start_ip = u"20.0.0.1"
        self.p2_dst_start_ip = u"10.0.0.0"
        self.p2_dst_end_ip = u"10.15.66.63"

    def define_packets(self):
        """Defines the packets to be sent from the traffic generator.

        Packet definition: | ETH | IP |

        :returns: Packets to be sent from the traffic generator.
        :rtype: tuple
        """

        # Direction 0 --> 1
        base_pkt_a = (
            Ether() /
            IP(
                src=self.p1_src_start_ip,
                dst=self.p1_dst_start_ip,
                proto=61
            )
        )
        # Direction 1 --> 0
        base_pkt_b = (
            Ether() /
            IP(
                src=self.p2_src_start_ip,
                dst=self.p2_dst_start_ip,
                proto=61
            )
        )

        # Direction 0 --> 1
        vm1 = STLScVmRaw(
            [
                STLVmFlowVar(
                    name=u"dst",
                    min_value=self.p1_dst_start_ip,
                    max_value=self.p1_dst_end_ip,
                    size=4,
                    op=u"inc"
                ),
                STLVmWrFlowVar(
                    fv_name=u"dst",
                    pkt_offset=u"IP.dst"
                ),
                STLVmFixIpv4(
                    offset=u"IP"
                )
            ]
        )
        # Direction 1 --> 0
        vm2 = STLScVmRaw(
            [
                STLVmFlowVar(
                    name=u"dst",
                    min_value=self.p2_dst_start_ip,
                    max_value=self.p2_dst_end_ip,
                    size=4,
                    op=u"inc"
                ),
                STLVmWrFlowVar(
                    fv_name=u"dst",
                    pkt_offset=u"IP.dst"
                ),
                STLVmFixIpv4(
                    offset=u"IP"
                )
            ]
        )

        return base_pkt_a, base_pkt_b, vm1, vm2


def register():
    """Register this traffic profile to T-rex.

    Do not change this function.

    :return: Traffic streams.
    :rtype: Object
    """
    return TrafficStreams()
id='n975' href='#n975'>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 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
#!/usr/bin/env python3
"""ACL plugin Test Case HLD:
"""

import unittest
import random

from scapy.packet import Raw
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP, UDP, ICMP
from scapy.layers.inet6 import IPv6, ICMPv6EchoRequest
from scapy.layers.inet6 import IPv6ExtHdrFragment
from framework import VppTestCase, VppTestRunner
from framework import tag_fixme_vpp_workers
from util import Host, ppp
from ipaddress import IPv4Network, IPv6Network

from vpp_lo_interface import VppLoInterface
from vpp_acl import AclRule, VppAcl, VppAclInterface, VppEtypeWhitelist
from vpp_ip import INVALID_INDEX


@tag_fixme_vpp_workers
class TestACLplugin(VppTestCase):
    """ACL plugin Test Case"""

    # traffic types
    IP = 0
    ICMP = 1

    # IP version
    IPRANDOM = -1
    IPV4 = 0
    IPV6 = 1

    # rule types
    DENY = 0
    PERMIT = 1

    # supported protocols
    proto = [[6, 17], [1, 58]]
    proto_map = {1: "ICMP", 58: "ICMPv6EchoRequest", 6: "TCP", 17: "UDP"}
    ICMPv4 = 0
    ICMPv6 = 1
    TCP = 0
    UDP = 1
    PROTO_ALL = 0

    # port ranges
    PORTS_ALL = -1
    PORTS_RANGE = 0
    PORTS_RANGE_2 = 1
    udp_sport_from = 10
    udp_sport_to = udp_sport_from + 5
    udp_dport_from = 20000
    udp_dport_to = udp_dport_from + 5000
    tcp_sport_from = 30
    tcp_sport_to = tcp_sport_from + 5
    tcp_dport_from = 40000
    tcp_dport_to = tcp_dport_from + 5000

    udp_sport_from_2 = 90
    udp_sport_to_2 = udp_sport_from_2 + 5
    udp_dport_from_2 = 30000
    udp_dport_to_2 = udp_dport_from_2 + 5000
    tcp_sport_from_2 = 130
    tcp_sport_to_2 = tcp_sport_from_2 + 5
    tcp_dport_from_2 = 20000
    tcp_dport_to_2 = tcp_dport_from_2 + 5000

    icmp4_type = 8  # echo request
    icmp4_code = 3
    icmp6_type = 128  # echo request
    icmp6_code = 3

    icmp4_type_2 = 8
    icmp4_code_from_2 = 5
    icmp4_code_to_2 = 20
    icmp6_type_2 = 128
    icmp6_code_from_2 = 8
    icmp6_code_to_2 = 42

    # Test variables
    bd_id = 1

    @classmethod
    def setUpClass(cls):
        """
        Perform standard class setup (defined by class method setUpClass in
        class VppTestCase) before running the test case, set test case related
        variables and configure VPP.
        """
        super(TestACLplugin, cls).setUpClass()

        try:
            # Create 2 pg interfaces
            cls.create_pg_interfaces(range(2))

            # Packet flows mapping pg0 -> pg1, pg2 etc.
            cls.flows = dict()
            cls.flows[cls.pg0] = [cls.pg1]

            # Packet sizes
            cls.pg_if_packet_sizes = [64, 512, 1518, 9018]

            # Create BD with MAC learning and unknown unicast flooding disabled
            # and put interfaces to this BD
            cls.vapi.bridge_domain_add_del_v2(
                bd_id=cls.bd_id, uu_flood=1, learn=1, flood=1, forward=1, is_add=1
            )
            for pg_if in cls.pg_interfaces:
                cls.vapi.sw_interface_set_l2_bridge(
                    rx_sw_if_index=pg_if.sw_if_index, bd_id=cls.bd_id
                )

            # Set up all interfaces
            for i in cls.pg_interfaces:
                i.admin_up()

            # Mapping between packet-generator index and lists of test hosts
            cls.hosts_by_pg_idx = dict()
            for pg_if in cls.pg_interfaces:
                cls.hosts_by_pg_idx[pg_if.sw_if_index] = []

            # Create list of deleted hosts
            cls.deleted_hosts_by_pg_idx = dict()
            for pg_if in cls.pg_interfaces:
                cls.deleted_hosts_by_pg_idx[pg_if.sw_if_index] = []

            # warm-up the mac address tables
            # self.warmup_test()
            count = 16
            start = 0
            n_int = len(cls.pg_interfaces)
            macs_per_if = count // n_int
            i = -1
            for pg_if in cls.pg_interfaces:
                i += 1
                start_nr = macs_per_if * i + start
                end_nr = (
                    count + start if i == (n_int - 1) else macs_per_if * (i + 1) + start
                )
                hosts = cls.hosts_by_pg_idx[pg_if.sw_if_index]
                for j in range(int(start_nr), int(end_nr)):
                    host = Host(
                        "00:00:00:ff:%02x:%02x" % (pg_if.sw_if_index, j),
                        "172.17.1%02x.%u" % (pg_if.sw_if_index, j),
                        "2017:dead:%02x::%u" % (pg_if.sw_if_index, j),
                    )
                    hosts.append(host)

        except Exception:
            super(TestACLplugin, cls).tearDownClass()
            raise

    @classmethod
    def tearDownClass(cls):
        super(TestACLplugin, cls).tearDownClass()

    def setUp(self):
        super(TestACLplugin, self).setUp()
        self.reset_packet_infos()

    def tearDown(self):
        """
        Show various debug prints after each test.
        """
        super(TestACLplugin, self).tearDown()

    def show_commands_at_teardown(self):
        cli = "show vlib graph l2-input-feat-arc"
        self.logger.info(self.vapi.ppcli(cli))
        cli = "show vlib graph l2-input-feat-arc-end"
        self.logger.info(self.vapi.ppcli(cli))
        cli = "show vlib graph l2-output-feat-arc"
        self.logger.info(self.vapi.ppcli(cli))
        cli = "show vlib graph l2-output-feat-arc-end"
        self.logger.info(self.vapi.ppcli(cli))
        self.logger.info(self.vapi.ppcli("show l2fib verbose"))
        self.logger.info(self.vapi.ppcli("show acl-plugin acl"))
        self.logger.info(self.vapi.ppcli("show acl-plugin interface"))
        self.logger.info(self.vapi.ppcli("show acl-plugin tables"))
        self.logger.info(self.vapi.ppcli("show bridge-domain %s detail" % self.bd_id))

    def create_rule(
        self,
        ip=0,
        permit_deny=0,
        ports=PORTS_ALL,
        proto=-1,
        s_prefix=0,
        s_ip=0,
        d_prefix=0,
        d_ip=0,
    ):
        if ip:
            src_prefix = IPv6Network((s_ip, s_prefix))
            dst_prefix = IPv6Network((d_ip, d_prefix))
        else:
            src_prefix = IPv4Network((s_ip, s_prefix))
            dst_prefix = IPv4Network((d_ip, d_prefix))
        return AclRule(
            is_permit=permit_deny,
            ports=ports,
            proto=proto,
            src_prefix=src_prefix,
            dst_prefix=dst_prefix,
        )

    def apply_rules(self, rules, tag=None):
        acl = VppAcl(self, rules, tag=tag)
        acl.add_vpp_config()
        self.logger.info("Dumped ACL: " + str(acl.dump()))
        # Apply a ACL on the interface as inbound
        for i in self.pg_interfaces:
            acl_if = VppAclInterface(
                self, sw_if_index=i.sw_if_index, n_input=1, acls=[acl]
            )
            acl_if.add_vpp_config()
        return acl.acl_index

    def apply_rules_to(self, rules, tag=None, sw_if_index=INVALID_INDEX):
        acl = VppAcl(self, rules, tag=tag)
        acl.add_vpp_config()
        self.logger.info("Dumped ACL: " + str(acl.dump()))
        # Apply a ACL on the interface as inbound
        acl_if = VppAclInterface(self, sw_if_index=sw_if_index, n_input=1, acls=[acl])
        return acl.acl_index

    def etype_whitelist(self, whitelist, n_input, add=True):
        # Apply whitelists on all the interfaces
        if add:
            self._wl = []
            for i in self.pg_interfaces:
                self._wl.append(
                    VppEtypeWhitelist(
                        self,
                        sw_if_index=i.sw_if_index,
                        whitelist=whitelist,
                        n_input=n_input,
                    ).add_vpp_config()
                )
        else:
            if hasattr(self, "_wl"):
                for wl in self._wl:
                    wl.remove_vpp_config()

    def create_upper_layer(self, packet_index, proto, ports=0):
        p = self.proto_map[proto]
        if p == "UDP":
            if ports == 0:
                return UDP(
                    sport=random.randint(self.udp_sport_from, self.udp_sport_to),
                    dport=random.randint(self.udp_dport_from, self.udp_dport_to),
                )
            else:
                return UDP(sport=ports, dport=ports)
        elif p == "TCP":
            if ports == 0:
                return TCP(
                    sport=random.randint(self.tcp_sport_from, self.tcp_sport_to),
                    dport=random.randint(self.tcp_dport_from, self.tcp_dport_to),
                )
            else:
                return TCP(sport=ports, dport=ports)
        return ""

    def create_stream(
        self,
        src_if,
        packet_sizes,
        traffic_type=0,
        ipv6=0,
        proto=-1,
        ports=0,
        fragments=False,
        pkt_raw=True,
        etype=-1,
    ):
        """
        Create input packet stream for defined interface using hosts or
        deleted_hosts list.

        :param object src_if: Interface to create packet stream for.
        :param list packet_sizes: List of required packet sizes.
        :param traffic_type: 1: ICMP packet, 2: IPv6 with EH, 0: otherwise.
        :return: Stream of packets.
        """
        pkts = []
        if self.flows.__contains__(src_if):
            src_hosts = self.hosts_by_pg_idx[src_if.sw_if_index]
            for dst_if in self.flows[src_if]:
                dst_hosts = self.hosts_by_pg_idx[dst_if.sw_if_index]
                n_int = len(dst_hosts) * len(src_hosts)
                for i in range(0, n_int):
                    dst_host = dst_hosts[int(i / len(src_hosts))]
                    src_host = src_hosts[i % len(src_hosts)]
                    pkt_info = self.create_packet_info(src_if, dst_if)
                    if ipv6 == 1:
                        pkt_info.ip = 1
                    elif ipv6 == 0:
                        pkt_info.ip = 0
                    else:
                        pkt_info.ip = random.choice([0, 1])
                    if proto == -1:
                        pkt_info.proto = random.choice(self.proto[self.IP])
                    else:
                        pkt_info.proto = proto
                    payload = self.info_to_payload(pkt_info)
                    p = Ether(dst=dst_host.mac, src=src_host.mac)
                    if etype > 0:
                        p = Ether(dst=dst_host.mac, src=src_host.mac, type=etype)
                    if pkt_info.ip:
                        p /= IPv6(dst=dst_host.ip6, src=src_host.ip6)
                        if fragments:
                            p /= IPv6ExtHdrFragment(offset=64, m=1)
                    else:
                        if fragments:
                            p /= IP(
                                src=src_host.ip4, dst=dst_host.ip4, flags=1, frag=64
                            )
                        else:
                            p /= IP(src=src_host.ip4, dst=dst_host.ip4)
                    if traffic_type == self.ICMP:
                        if pkt_info.ip:
                            p /= ICMPv6EchoRequest(
                                type=self.icmp6_type, code=self.icmp6_code
                            )
                        else:
                            p /= ICMP(type=self.icmp4_type, code=self.icmp4_code)
                    else:
                        p /= self.create_upper_layer(i, pkt_info.proto, ports)
                    if pkt_raw:
                        p /= Raw(payload)
                        pkt_info.data = p.copy()
                    if pkt_raw:
                        size = random.choice(packet_sizes)
                        self.extend_packet(p, size)
                    pkts.append(p)
        return pkts

    def verify_capture(self, pg_if, capture, traffic_type=0, ip_type=0, etype=-1):
        """
        Verify captured input packet stream for defined interface.

        :param object pg_if: Interface to verify captured packet stream for.
        :param list capture: Captured packet stream.
        :param traffic_type: 1: ICMP packet, 2: IPv6 with EH, 0: otherwise.
        """
        last_info = dict()
        for i in self.pg_interfaces:
            last_info[i.sw_if_index] = None
        dst_sw_if_index = pg_if.sw_if_index
        for packet in capture:
            if etype > 0:
                if packet[Ether].type != etype:
                    self.logger.error(ppp("Unexpected ethertype in packet:", packet))
                else:
                    continue
            try:
                # Raw data for ICMPv6 are stored in ICMPv6EchoRequest.data
                if traffic_type == self.ICMP and ip_type == self.IPV6:
                    payload_info = self.payload_to_info(
                        packet[ICMPv6EchoRequest], "data"
                    )
                    payload = packet[ICMPv6EchoRequest]
                else:
                    payload_info = self.payload_to_info(packet[Raw])
                    payload = packet[self.proto_map[payload_info.proto]]
            except:
                self.logger.error(
                    ppp("Unexpected or invalid packet (outside network):", packet)
                )
                raise

            if ip_type != 0:
                self.assertEqual(payload_info.ip, ip_type)
            if traffic_type == self.ICMP:
                try:
                    if payload_info.ip == 0:
                        self.assertEqual(payload.type, self.icmp4_type)
                        self.assertEqual(payload.code, self.icmp4_code)
                    else:
                        self.assertEqual(payload.type, self.icmp6_type)
                        self.assertEqual(payload.code, self.icmp6_code)
                except:
                    self.logger.error(
                        ppp("Unexpected or invalid packet (outside network):", packet)
                    )
                    raise
            else:
                try:
                    ip_version = IPv6 if payload_info.ip == 1 else IP

                    ip = packet[ip_version]
                    packet_index = payload_info.index

                    self.assertEqual(payload_info.dst, dst_sw_if_index)
                    self.logger.debug(
                        "Got packet on port %s: src=%u (id=%u)"
                        % (pg_if.name, payload_info.src, packet_index)
                    )
                    next_info = self.get_next_packet_info_for_interface2(
                        payload_info.src, dst_sw_if_index, last_info[payload_info.src]
                    )
                    last_info[payload_info.src] = next_info
                    self.assertTrue(next_info is not None)
                    self.assertEqual(packet_index, next_info.index)
                    saved_packet = next_info.data
                    # Check standard fields
                    self.assertEqual(ip.src, saved_packet[ip_version].src)
                    self.assertEqual(ip.dst, saved_packet[ip_version].dst)
                    p = self.proto_map[payload_info.proto]
                    if p == "TCP":
                        tcp = packet[TCP]
                        self.assertEqual(tcp.sport, saved_packet[TCP].sport)
                        self.assertEqual(tcp.dport, saved_packet[TCP].dport)
                    elif p == "UDP":
                        udp = packet[UDP]
                        self.assertEqual(udp.sport, saved_packet[UDP].sport)
                        self.assertEqual(udp.dport, saved_packet[UDP].dport)
                except:
                    self.logger.error(ppp("Unexpected or invalid packet:", packet))
                    raise
        for i in self.pg_interfaces:
            remaining_packet = self.get_next_packet_info_for_interface2(
                i, dst_sw_if_index, last_info[i.sw_if_index]
            )
            self.assertTrue(
                remaining_packet is None,
                "Port %u: Packet expected from source %u didn't arrive"
                % (dst_sw_if_index, i.sw_if_index),
            )

    def run_traffic_no_check(self):
        # Test
        # Create incoming packet streams for packet-generator interfaces
        for i in self.pg_interfaces:
            if self.flows.__contains__(i):
                pkts = self.create_stream(i, self.pg_if_packet_sizes)
                if len(pkts) > 0:
                    i.add_stream(pkts)

        # Enable packet capture and start packet sending
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

    def run_verify_test(
        self,
        traffic_type=0,
        ip_type=0,
        proto=-1,
        ports=0,
        frags=False,
        pkt_raw=True,
        etype=-1,
    ):
        # Test
        # Create incoming packet streams for packet-generator interfaces
        pkts_cnt = 0
        for i in self.pg_interfaces:
            if self.flows.__contains__(i):
                pkts = self.create_stream(
                    i,
                    self.pg_if_packet_sizes,
                    traffic_type,
                    ip_type,
                    proto,
                    ports,
                    frags,
                    pkt_raw,
                    etype,
                )
                if len(pkts) > 0:
                    i.add_stream(pkts)
                    pkts_cnt += len(pkts)

        # Enable packet capture and start packet sendingself.IPV
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.logger.info("sent packets count: %d" % pkts_cnt)

        # Verify
        # Verify outgoing packet streams per packet-generator interface
        for src_if in self.pg_interfaces:
            if self.flows.__contains__(src_if):
                for dst_if in self.flows[src_if]:
                    capture = dst_if.get_capture(pkts_cnt)
                    self.logger.info("Verifying capture on interface %s" % dst_if.name)
                    self.verify_capture(dst_if, capture, traffic_type, ip_type, etype)

    def run_verify_negat_test(
        self, traffic_type=0, ip_type=0, proto=-1, ports=0, frags=False, etype=-1
    ):
        # Test
        pkts_cnt = 0
        self.reset_packet_infos()
        for i in self.pg_interfaces:
            if self.flows.__contains__(i):
                pkts = self.create_stream(
                    i,
                    self.pg_if_packet_sizes,
                    traffic_type,
                    ip_type,
                    proto,
                    ports,
                    frags,
                    True,
                    etype,
                )
                if len(pkts) > 0:
                    i.add_stream(pkts)
                    pkts_cnt += len(pkts)

        # Enable packet capture and start packet sending
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()
        self.logger.info("sent packets count: %d" % pkts_cnt)

        # Verify
        # Verify outgoing packet streams per packet-generator interface
        for src_if in self.pg_interfaces:
            if self.flows.__contains__(src_if):
                for dst_if in self.flows[src_if]:
                    self.logger.info("Verifying capture on interface %s" % dst_if.name)
                    capture = dst_if.get_capture(0)
                    self.assertEqual(len(capture), 0)

    def test_0000_warmup_test(self):
        """ACL plugin version check; learn MACs"""
        reply = self.vapi.papi.acl_plugin_get_version()
        self.assertEqual(reply.major, 1)
        self.logger.info(
            "Working with ACL plugin version: %d.%d" % (reply.major, reply.minor)
        )
        # minor version changes are non breaking
        # self.assertEqual(reply.minor, 0)

    def test_0001_acl_create(self):
        """ACL create/delete test"""

        self.logger.info("ACLP_TEST_START_0001")
        # Create a permit-1234 ACL
        r = [AclRule(is_permit=1, proto=17, ports=1234, sport_to=1235)]
        # Test 1: add a new ACL
        first_acl = VppAcl(self, rules=r, tag="permit 1234")
        first_acl.add_vpp_config()
        self.assertTrue(first_acl.query_vpp_config())
        # The very first ACL gets #0
        self.assertEqual(first_acl.acl_index, 0)
        rr = first_acl.dump()
        self.logger.info("Dumped ACL: " + str(rr))
        self.assertEqual(len(rr), 1)
        # We should have the same number of ACL entries as we had asked
        self.assertEqual(len(rr[0].r), len(r))
        # The rules should be the same. But because the submitted and returned
        # are different types, we need to iterate over rules and keys to get
        # to basic values.
        for i_rule in range(0, len(r) - 1):
            encoded_rule = r[i_rule].encode()
            for rule_key in encoded_rule:
                self.assertEqual(rr[0].r[i_rule][rule_key], encoded_rule[rule_key])

        # Create a deny-1234 ACL
        r_deny = [
            AclRule(is_permit=0, proto=17, ports=1234, sport_to=1235),
            AclRule(is_permit=1, proto=17, ports=0),
        ]
        second_acl = VppAcl(self, rules=r_deny, tag="deny 1234;permit all")
        second_acl.add_vpp_config()
        self.assertTrue(second_acl.query_vpp_config())
        # The second ACL gets #1
        self.assertEqual(second_acl.acl_index, 1)

        # Test 2: try to modify a nonexistent ACL
        invalid_acl = VppAcl(self, acl_index=432, rules=r, tag="FFFF:FFFF")
        reply = invalid_acl.add_vpp_config(expect_error=True)

        # apply an ACL on an interface inbound, try to delete ACL, must fail
        acl_if_list = VppAclInterface(
            self, sw_if_index=self.pg0.sw_if_index, n_input=1, acls=[first_acl]
        )
        acl_if_list.add_vpp_config()
        first_acl.remove_vpp_config(expect_error=True)
        # Unapply an ACL and then try to delete it - must be ok
        acl_if_list.remove_vpp_config()
        first_acl.remove_vpp_config()

        # apply an ACL on an interface inbound, try to delete ACL, must fail
        acl_if_list = VppAclInterface(
            self, sw_if_index=self.pg0.sw_if_index, n_input=0, acls=[second_acl]
        )
        acl_if_list.add_vpp_config()
        second_acl.remove_vpp_config(expect_error=True)
        # Unapply an ACL and then try to delete it - must be ok
        acl_if_list.remove_vpp_config()
        second_acl.remove_vpp_config()

        # try to apply a nonexistent ACL - must fail
        acl_if_list = VppAclInterface(
            self, sw_if_index=self.pg0.sw_if_index, n_input=0, acls=[invalid_acl]
        )
        acl_if_list.add_vpp_config(expect_error=True)

        self.logger.info("ACLP_TEST_FINISH_0001")

    def test_0002_acl_permit_apply(self):
        """permit ACL apply test"""
        self.logger.info("ACLP_TEST_START_0002")

        rules = []
        rules.append(
            self.create_rule(self.IPV4, self.PERMIT, 0, self.proto[self.IP][self.UDP])
        )
        rules.append(
            self.create_rule(self.IPV4, self.PERMIT, 0, self.proto[self.IP][self.TCP])
        )

        # Apply rules
        acl_idx = self.apply_rules(rules, "permit per-flow")

        # enable counters
        reply = self.vapi.papi.acl_stats_intf_counters_enable(enable=1)

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, -1)

        matches = self.statistics.get_counter("/acl/%d/matches" % acl_idx)
        self.logger.info("stat segment counters: %s" % repr(matches))
        cli = "show acl-plugin acl"
        self.logger.info(self.vapi.ppcli(cli))
        cli = "show acl-plugin tables"
        self.logger.info(self.vapi.ppcli(cli))

        total_hits = matches[0][0]["packets"] + matches[0][1]["packets"]
        self.assertEqual(total_hits, 64)

        # disable counters
        reply = self.vapi.papi.acl_stats_intf_counters_enable(enable=0)

        self.logger.info("ACLP_TEST_FINISH_0002")

    def test_0003_acl_deny_apply(self):
        """deny ACL apply test"""
        self.logger.info("ACLP_TEST_START_0003")
        # Add a deny-flows ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_ALL, self.proto[self.IP][self.UDP]
            )
        )
        # Permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        acl_idx = self.apply_rules(rules, "deny per-flow;permit all")

        # enable counters
        reply = self.vapi.papi.acl_stats_intf_counters_enable(enable=1)

        # Traffic should not pass
        self.run_verify_negat_test(self.IP, self.IPV4, self.proto[self.IP][self.UDP])

        matches = self.statistics.get_counter("/acl/%d/matches" % acl_idx)
        self.logger.info("stat segment counters: %s" % repr(matches))
        cli = "show acl-plugin acl"
        self.logger.info(self.vapi.ppcli(cli))
        cli = "show acl-plugin tables"
        self.logger.info(self.vapi.ppcli(cli))
        self.assertEqual(matches[0][0]["packets"], 64)
        # disable counters
        reply = self.vapi.papi.acl_stats_intf_counters_enable(enable=0)
        self.logger.info("ACLP_TEST_FINISH_0003")
        # self.assertEqual(, 0)

    def test_0004_vpp624_permit_icmpv4(self):
        """VPP_624 permit ICMPv4"""
        self.logger.info("ACLP_TEST_START_0004")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4,
                self.PERMIT,
                self.PORTS_RANGE,
                self.proto[self.ICMP][self.ICMPv4],
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit icmpv4")

        # Traffic should still pass
        self.run_verify_test(self.ICMP, self.IPV4, self.proto[self.ICMP][self.ICMPv4])

        self.logger.info("ACLP_TEST_FINISH_0004")

    def test_0005_vpp624_permit_icmpv6(self):
        """VPP_624 permit ICMPv6"""
        self.logger.info("ACLP_TEST_START_0005")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6,
                self.PERMIT,
                self.PORTS_RANGE,
                self.proto[self.ICMP][self.ICMPv6],
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit icmpv6")

        # Traffic should still pass
        self.run_verify_test(self.ICMP, self.IPV6, self.proto[self.ICMP][self.ICMPv6])

        self.logger.info("ACLP_TEST_FINISH_0005")

    def test_0006_vpp624_deny_icmpv4(self):
        """VPP_624 deny ICMPv4"""
        self.logger.info("ACLP_TEST_START_0006")
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4,
                self.DENY,
                self.PORTS_RANGE,
                self.proto[self.ICMP][self.ICMPv4],
            )
        )
        # permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny icmpv4")

        # Traffic should not pass
        self.run_verify_negat_test(self.ICMP, self.IPV4, 0)

        self.logger.info("ACLP_TEST_FINISH_0006")

    def test_0007_vpp624_deny_icmpv6(self):
        """VPP_624 deny ICMPv6"""
        self.logger.info("ACLP_TEST_START_0007")
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6,
                self.DENY,
                self.PORTS_RANGE,
                self.proto[self.ICMP][self.ICMPv6],
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny icmpv6")

        # Traffic should not pass
        self.run_verify_negat_test(self.ICMP, self.IPV6, 0)

        self.logger.info("ACLP_TEST_FINISH_0007")

    def test_0008_tcp_permit_v4(self):
        """permit TCPv4"""
        self.logger.info("ACLP_TEST_START_0008")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.TCP])

        self.logger.info("ACLP_TEST_FINISH_0008")

    def test_0009_tcp_permit_v6(self):
        """permit TCPv6"""
        self.logger.info("ACLP_TEST_START_0009")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip6 tcp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.TCP])

        self.logger.info("ACLP_TEST_FINISH_0008")

    def test_0010_udp_permit_v4(self):
        """permit UDPv4"""
        self.logger.info("ACLP_TEST_START_0010")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv udp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.UDP])

        self.logger.info("ACLP_TEST_FINISH_0010")

    def test_0011_udp_permit_v6(self):
        """permit UDPv6"""
        self.logger.info("ACLP_TEST_START_0011")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip6 udp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.UDP])

        self.logger.info("ACLP_TEST_FINISH_0011")

    def test_0012_tcp_deny(self):
        """deny TCPv4/v6"""
        self.logger.info("ACLP_TEST_START_0012")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 tcp")

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.TCP]
        )

        self.logger.info("ACLP_TEST_FINISH_0012")

    def test_0013_udp_deny(self):
        """deny UDPv4/v6"""
        self.logger.info("ACLP_TEST_START_0013")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 udp")

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.UDP]
        )

        self.logger.info("ACLP_TEST_FINISH_0013")

    def test_0014_acl_dump(self):
        """verify add/dump acls"""
        self.logger.info("ACLP_TEST_START_0014")

        r = [
            [self.IPV4, self.PERMIT, 1234, self.proto[self.IP][self.TCP]],
            [self.IPV4, self.PERMIT, 2345, self.proto[self.IP][self.UDP]],
            [self.IPV4, self.PERMIT, 0, self.proto[self.IP][self.TCP]],
            [self.IPV4, self.PERMIT, 0, self.proto[self.IP][self.UDP]],
            [self.IPV4, self.PERMIT, 5, self.proto[self.ICMP][self.ICMPv4]],
            [self.IPV6, self.PERMIT, 4321, self.proto[self.IP][self.TCP]],
            [self.IPV6, self.PERMIT, 5432, self.proto[self.IP][self.UDP]],
            [self.IPV6, self.PERMIT, 0, self.proto[self.IP][self.TCP]],
            [self.IPV6, self.PERMIT, 0, self.proto[self.IP][self.UDP]],
            [self.IPV6, self.PERMIT, 6, self.proto[self.ICMP][self.ICMPv6]],
            [self.IPV4, self.DENY, self.PORTS_ALL, 0],
            [self.IPV4, self.DENY, 1234, self.proto[self.IP][self.TCP]],
            [self.IPV4, self.DENY, 2345, self.proto[self.IP][self.UDP]],
            [self.IPV4, self.DENY, 5, self.proto[self.ICMP][self.ICMPv4]],
            [self.IPV6, self.DENY, 4321, self.proto[self.IP][self.TCP]],
            [self.IPV6, self.DENY, 5432, self.proto[self.IP][self.UDP]],
            [self.IPV6, self.DENY, 6, self.proto[self.ICMP][self.ICMPv6]],
            [self.IPV6, self.DENY, self.PORTS_ALL, 0],
        ]

        # Add and verify new ACLs
        rules = []
        for i in range(len(r)):
            rules.append(self.create_rule(r[i][0], r[i][1], r[i][2], r[i][3]))

        acl = VppAcl(self, rules=rules)
        acl.add_vpp_config()
        result = acl.dump()

        i = 0
        for drules in result:
            for dr in drules.r:
                self.assertEqual(dr.is_permit, r[i][1])
                self.assertEqual(dr.proto, r[i][3])

                if r[i][2] > 0:
                    self.assertEqual(dr.srcport_or_icmptype_first, r[i][2])
                else:
                    if r[i][2] < 0:
                        self.assertEqual(dr.srcport_or_icmptype_first, 0)
                        self.assertEqual(dr.srcport_or_icmptype_last, 65535)
                    else:
                        if dr.proto == self.proto[self.IP][self.TCP]:
                            self.assertGreater(
                                dr.srcport_or_icmptype_first, self.tcp_sport_from - 1
                            )
                            self.assertLess(
                                dr.srcport_or_icmptype_first, self.tcp_sport_to + 1
                            )
                            self.assertGreater(
                                dr.dstport_or_icmpcode_last, self.tcp_dport_from - 1
                            )
                            self.assertLess(
                                dr.dstport_or_icmpcode_last, self.tcp_dport_to + 1
                            )
                        elif dr.proto == self.proto[self.IP][self.UDP]:
                            self.assertGreater(
                                dr.srcport_or_icmptype_first, self.udp_sport_from - 1
                            )
                            self.assertLess(
                                dr.srcport_or_icmptype_first, self.udp_sport_to + 1
                            )
                            self.assertGreater(
                                dr.dstport_or_icmpcode_last, self.udp_dport_from - 1
                            )
                            self.assertLess(
                                dr.dstport_or_icmpcode_last, self.udp_dport_to + 1
                            )
                i += 1

        self.logger.info("ACLP_TEST_FINISH_0014")

    def test_0015_tcp_permit_port_v4(self):
        """permit single TCPv4"""
        self.logger.info("ACLP_TEST_START_0015")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, port, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip4 tcp %d" % port)

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.TCP], port)

        self.logger.info("ACLP_TEST_FINISH_0015")

    def test_0016_udp_permit_port_v4(self):
        """permit single UDPv4"""
        self.logger.info("ACLP_TEST_START_0016")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, port, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip4 tcp %d" % port)

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.UDP], port)

        self.logger.info("ACLP_TEST_FINISH_0016")

    def test_0017_tcp_permit_port_v6(self):
        """permit single TCPv6"""
        self.logger.info("ACLP_TEST_START_0017")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, port, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip4 tcp %d" % port)

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.TCP], port)

        self.logger.info("ACLP_TEST_FINISH_0017")

    def test_0018_udp_permit_port_v6(self):
        """permit single UDPv6"""
        self.logger.info("ACLP_TEST_START_0018")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, port, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip4 tcp %d" % port)

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.UDP], port)

        self.logger.info("ACLP_TEST_FINISH_0018")

    def test_0019_udp_deny_port(self):
        """deny single TCPv4/v6"""
        self.logger.info("ACLP_TEST_START_0019")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(self.IPV4, self.DENY, port, self.proto[self.IP][self.TCP])
        )
        rules.append(
            self.create_rule(self.IPV6, self.DENY, port, self.proto[self.IP][self.TCP])
        )
        # Permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 udp %d" % port)

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.TCP], port
        )

        self.logger.info("ACLP_TEST_FINISH_0019")

    def test_0020_udp_deny_port(self):
        """deny single UDPv4/v6"""
        self.logger.info("ACLP_TEST_START_0020")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(self.IPV4, self.DENY, port, self.proto[self.IP][self.UDP])
        )
        rules.append(
            self.create_rule(self.IPV6, self.DENY, port, self.proto[self.IP][self.UDP])
        )
        # Permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 udp %d" % port)

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.UDP], port
        )

        self.logger.info("ACLP_TEST_FINISH_0020")

    def test_0021_udp_deny_port_verify_fragment_deny(self):
        """deny single UDPv4/v6, permit ip any, verify non-initial fragment
        blocked
        """
        self.logger.info("ACLP_TEST_START_0021")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(self.IPV4, self.DENY, port, self.proto[self.IP][self.UDP])
        )
        rules.append(
            self.create_rule(self.IPV6, self.DENY, port, self.proto[self.IP][self.UDP])
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 udp %d" % port)

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.UDP], port, True
        )

        self.logger.info("ACLP_TEST_FINISH_0021")

    def test_0022_zero_length_udp_ipv4(self):
        """VPP-687 zero length udp ipv4 packet"""
        self.logger.info("ACLP_TEST_START_0022")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, port, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit empty udp ip4 %d" % port)

        # Traffic should still pass
        # Create incoming packet streams for packet-generator interfaces
        pkts_cnt = 0
        pkts = self.create_stream(
            self.pg0,
            self.pg_if_packet_sizes,
            self.IP,
            self.IPV4,
            self.proto[self.IP][self.UDP],
            port,
            False,
            False,
        )
        if len(pkts) > 0:
            self.pg0.add_stream(pkts)
            pkts_cnt += len(pkts)

        # Enable packet capture and start packet sendingself.IPV
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        self.pg1.get_capture(pkts_cnt)

        self.logger.info("ACLP_TEST_FINISH_0022")

    def test_0023_zero_length_udp_ipv6(self):
        """VPP-687 zero length udp ipv6 packet"""
        self.logger.info("ACLP_TEST_START_0023")

        port = random.randint(16384, 65535)
        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, port, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit empty udp ip6 %d" % port)

        # Traffic should still pass
        # Create incoming packet streams for packet-generator interfaces
        pkts_cnt = 0
        pkts = self.create_stream(
            self.pg0,
            self.pg_if_packet_sizes,
            self.IP,
            self.IPV6,
            self.proto[self.IP][self.UDP],
            port,
            False,
            False,
        )
        if len(pkts) > 0:
            self.pg0.add_stream(pkts)
            pkts_cnt += len(pkts)

        # Enable packet capture and start packet sendingself.IPV
        self.pg_enable_capture(self.pg_interfaces)
        self.pg_start()

        # Verify outgoing packet streams per packet-generator interface
        self.pg1.get_capture(pkts_cnt)

        self.logger.info("ACLP_TEST_FINISH_0023")

    def test_0108_tcp_permit_v4(self):
        """permit TCPv4 + non-match range"""
        self.logger.info("ACLP_TEST_START_0108")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.TCP])

        self.logger.info("ACLP_TEST_FINISH_0108")

    def test_0109_tcp_permit_v6(self):
        """permit TCPv6 + non-match range"""
        self.logger.info("ACLP_TEST_START_0109")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip6 tcp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.TCP])

        self.logger.info("ACLP_TEST_FINISH_0109")

    def test_0110_udp_permit_v4(self):
        """permit UDPv4 + non-match range"""
        self.logger.info("ACLP_TEST_START_0110")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.UDP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 udp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV4, self.proto[self.IP][self.UDP])

        self.logger.info("ACLP_TEST_FINISH_0110")

    def test_0111_udp_permit_v6(self):
        """permit UDPv6 + non-match range"""
        self.logger.info("ACLP_TEST_START_0111")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.UDP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV6, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ip6 udp")

        # Traffic should still pass
        self.run_verify_test(self.IP, self.IPV6, self.proto[self.IP][self.UDP])

        self.logger.info("ACLP_TEST_FINISH_0111")

    def test_0112_tcp_deny(self):
        """deny TCPv4/v6 + non-match range"""
        self.logger.info("ACLP_TEST_START_0112")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4,
                self.PERMIT,
                self.PORTS_RANGE_2,
                self.proto[self.IP][self.TCP],
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6,
                self.PERMIT,
                self.PORTS_RANGE_2,
                self.proto[self.IP][self.TCP],
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 tcp")

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.TCP]
        )

        self.logger.info("ACLP_TEST_FINISH_0112")

    def test_0113_udp_deny(self):
        """deny UDPv4/v6 + non-match range"""
        self.logger.info("ACLP_TEST_START_0113")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4,
                self.PERMIT,
                self.PORTS_RANGE_2,
                self.proto[self.IP][self.UDP],
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6,
                self.PERMIT,
                self.PORTS_RANGE_2,
                self.proto[self.IP][self.UDP],
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV6, self.DENY, self.PORTS_RANGE, self.proto[self.IP][self.UDP]
            )
        )
        # permit ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.PERMIT, self.PORTS_ALL, 0))
        rules.append(self.create_rule(self.IPV6, self.PERMIT, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "deny ip4/ip6 udp")

        # Traffic should not pass
        self.run_verify_negat_test(
            self.IP, self.IPRANDOM, self.proto[self.IP][self.UDP]
        )

        self.logger.info("ACLP_TEST_FINISH_0113")

    def test_0300_tcp_permit_v4_etype_aaaa(self):
        """permit TCPv4, send 0xAAAA etype"""
        self.logger.info("ACLP_TEST_START_0300")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")

        # Traffic should still pass also for an odd ethertype
        self.run_verify_test(
            self.IP, self.IPV4, self.proto[self.IP][self.TCP], 0, False, True, 0xAAAA
        )
        self.logger.info("ACLP_TEST_FINISH_0300")

    def test_0305_tcp_permit_v4_etype_blacklist_aaaa(self):
        """permit TCPv4, whitelist 0x0BBB ethertype, send 0xAAAA-blocked"""
        self.logger.info("ACLP_TEST_START_0305")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")
        # whitelist the 0xbbbb etype - so the 0xaaaa should be blocked
        self.etype_whitelist([0xBBB], 1)

        # The oddball ethertype should be blocked
        self.run_verify_negat_test(
            self.IP, self.IPV4, self.proto[self.IP][self.TCP], 0, False, 0xAAAA
        )

        # remove the whitelist
        self.etype_whitelist([], 0, add=False)

        self.logger.info("ACLP_TEST_FINISH_0305")

    def test_0306_tcp_permit_v4_etype_blacklist_aaaa(self):
        """permit TCPv4, whitelist 0x0BBB ethertype, send 0x0BBB - pass"""
        self.logger.info("ACLP_TEST_START_0306")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")
        # whitelist the 0xbbbb etype - so the 0xaaaa should be blocked
        self.etype_whitelist([0xBBB], 1)

        # The whitelisted traffic, should pass
        self.run_verify_test(
            self.IP, self.IPV4, self.proto[self.IP][self.TCP], 0, False, True, 0x0BBB
        )

        # remove the whitelist, the previously blocked 0xAAAA should pass now
        self.etype_whitelist([], 0, add=False)

        self.logger.info("ACLP_TEST_FINISH_0306")

    def test_0307_tcp_permit_v4_etype_blacklist_aaaa(self):
        """permit TCPv4, whitelist 0x0BBB, remove, send 0xAAAA - pass"""
        self.logger.info("ACLP_TEST_START_0307")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # Apply rules
        self.apply_rules(rules, "permit ipv4 tcp")

        # whitelist the 0xbbbb etype - so the 0xaaaa should be blocked
        self.etype_whitelist([0xBBB], 1)
        # remove the whitelist, the previously blocked 0xAAAA should pass now
        self.etype_whitelist([], 0, add=False)

        # The whitelisted traffic, should pass
        self.run_verify_test(
            self.IP, self.IPV4, self.proto[self.IP][self.TCP], 0, False, True, 0xAAAA
        )

        self.logger.info("ACLP_TEST_FINISH_0306")

    def test_0315_del_intf(self):
        """apply an acl and delete the interface"""
        self.logger.info("ACLP_TEST_START_0315")

        # Add an ACL
        rules = []
        rules.append(
            self.create_rule(
                self.IPV4, self.DENY, self.PORTS_RANGE_2, self.proto[self.IP][self.TCP]
            )
        )
        rules.append(
            self.create_rule(
                self.IPV4, self.PERMIT, self.PORTS_RANGE, self.proto[self.IP][self.TCP]
            )
        )
        # deny ip any any in the end
        rules.append(self.create_rule(self.IPV4, self.DENY, self.PORTS_ALL, 0))

        # create an interface
        intf = []
        intf.append(VppLoInterface(self))

        # Apply rules
        self.apply_rules_to(rules, "permit ipv4 tcp", intf[0].sw_if_index)

        # Remove the interface
        intf[0].remove_vpp_config()

        self.logger.info("ACLP_TEST_FINISH_0315")


if __name__ == "__main__":
    unittest.main(testRunner=VppTestRunner)