3 from framework import VppTestCase, VppTestRunner
4 from vpp_udp_encap import *
5 from vpp_ip import DpoProto
6 from vpp_ip_route import VppIpRoute, VppRoutePath, VppIpTable
8 from scapy.packet import Raw
9 from scapy.layers.l2 import Ether, ARP
10 from scapy.layers.inet import IP, UDP
11 from scapy.layers.inet6 import IPv6
12 from scapy.contrib.mpls import MPLS
14 from vpp_object import *
15 from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
18 def find_abf_policy(test, id):
19 policies = test.vapi.abf_policy_dump()
21 if id == p.policy.policy_id:
26 def find_abf_itf_attach(test, id, sw_if_index):
27 attachs = test.vapi.abf_itf_attach_dump()
29 if id == a.attach.policy_id and \
30 sw_if_index == a.attach.sw_if_index:
35 class VppAbfPolicy(VppObject):
43 self.policy_id = policy_id
47 def encode_paths(self):
52 if type(l) == VppMplsLabel:
53 lstack.append(l.encode())
55 lstack.append({'label': l, 'ttl': 255})
56 n_labels = len(lstack)
57 while (len(lstack) < 16):
59 br_paths.append({'next_hop': p.nh_addr,
62 'sw_if_index': 0xffffffff,
64 'table_id': p.nh_table_id,
65 'next_hop_id': p.next_hop_id,
66 'is_udp_encap': p.is_udp_encap,
68 'label_stack': lstack})
71 def add_vpp_config(self):
72 self._test.vapi.abf_policy_add_del(
74 {'policy_id': self.policy_id,
75 'acl_index': self.acl.acl_index,
76 'n_paths': len(self.paths),
77 'paths': self.encode_paths()})
78 self._test.registry.register(self, self._test.logger)
80 def remove_vpp_config(self):
81 self._test.vapi.abf_policy_add_del(
83 {'policy_id': self.policy_id,
84 'acl_index': self.acl.acl_index,
85 'n_paths': len(self.paths),
86 'paths': self.encode_paths()})
88 def query_vpp_config(self):
89 return find_abf_policy(self._test, self.policy_id)
92 return self.object_id()
95 return ("abf-policy-%d" % self.policy_id)
98 class VppAbfAttach(VppObject):
107 self.policy_id = policy_id
108 self.sw_if_index = sw_if_index
109 self.priority = priority
110 self.is_ipv6 = is_ipv6
112 def add_vpp_config(self):
113 self._test.vapi.abf_itf_attach_add_del(
115 {'policy_id': self.policy_id,
116 'sw_if_index': self.sw_if_index,
117 'priority': self.priority,
118 'is_ipv6': self.is_ipv6})
119 self._test.registry.register(self, self._test.logger)
121 def remove_vpp_config(self):
122 self._test.vapi.abf_itf_attach_add_del(
124 {'policy_id': self.policy_id,
125 'sw_if_index': self.sw_if_index,
126 'priority': self.priority,
127 'is_ipv6': self.is_ipv6})
129 def query_vpp_config(self):
130 return find_abf_itf_attach(self._test,
135 return self.object_id()
138 return ("abf-attach-%d-%d" % (self.policy_id, self.sw_if_index))
141 class TestAbf(VppTestCase):
142 """ ABF Test Case """
145 super(TestAbf, self).setUp()
147 self.create_pg_interfaces(range(4))
149 for i in self.pg_interfaces:
157 for i in self.pg_interfaces:
162 super(TestAbf, self).tearDown()
165 """ IPv4 ACL Based Forwarding
169 # We are not testing the various matching capabilities
170 # of ACLs, that's done elsewhere. Here ware are testing
171 # the application of ACLs to a forwarding path to achieve
173 # So we construct just a few ACLs to ensure the ABF policies
174 # are correclty constructed and used. And a few path types
175 # to test the API path decoding.
181 rule_1 = ({'is_permit': 1,
184 'srcport_or_icmptype_first': 1234,
185 'srcport_or_icmptype_last': 1234,
186 'src_ip_prefix_len': 32,
187 'src_ip_addr': inet_pton(AF_INET, "1.1.1.1"),
188 'dstport_or_icmpcode_first': 1234,
189 'dstport_or_icmpcode_last': 1234,
190 'dst_ip_prefix_len': 32,
191 'dst_ip_addr': inet_pton(AF_INET, "1.1.1.2")})
192 acl_1 = self.vapi.acl_add_replace(acl_index=4294967295, r=[rule_1])
195 # ABF policy for ACL 1 - path via interface 1
197 abf_1 = VppAbfPolicy(self, 10, acl_1,
198 [VppRoutePath(self.pg1.remote_ip4,
199 self.pg1.sw_if_index)])
200 abf_1.add_vpp_config()
203 # Attach the policy to input interface Pg0
205 attach_1 = VppAbfAttach(self, 10, self.pg0.sw_if_index, 50)
206 attach_1.add_vpp_config()
209 # fire in packet matching the ACL src,dst. If it's forwarded
210 # then the ABF was successful, since default routing will drop it
212 p_1 = (Ether(src=self.pg0.remote_mac,
213 dst=self.pg0.local_mac) /
214 IP(src="1.1.1.1", dst="1.1.1.2") /
215 UDP(sport=1234, dport=1234) /
217 self.send_and_expect(self.pg0, p_1*65, self.pg1)
220 # Attach a 'better' priority policy to the same interface
222 abf_2 = VppAbfPolicy(self, 11, acl_1,
223 [VppRoutePath(self.pg2.remote_ip4,
224 self.pg2.sw_if_index)])
225 abf_2.add_vpp_config()
226 attach_2 = VppAbfAttach(self, 11, self.pg0.sw_if_index, 40)
227 attach_2.add_vpp_config()
229 self.send_and_expect(self.pg0, p_1*65, self.pg2)
232 # Attach a policy with priority in the middle
234 abf_3 = VppAbfPolicy(self, 12, acl_1,
235 [VppRoutePath(self.pg3.remote_ip4,
236 self.pg3.sw_if_index)])
237 abf_3.add_vpp_config()
238 attach_3 = VppAbfAttach(self, 12, self.pg0.sw_if_index, 45)
239 attach_3.add_vpp_config()
241 self.send_and_expect(self.pg0, p_1*65, self.pg2)
244 # remove the best priority
246 attach_2.remove_vpp_config()
247 self.send_and_expect(self.pg0, p_1*65, self.pg3)
250 # Attach one of the same policies to Pg1
252 attach_4 = VppAbfAttach(self, 12, self.pg1.sw_if_index, 45)
253 attach_4.add_vpp_config()
255 p_2 = (Ether(src=self.pg1.remote_mac,
256 dst=self.pg1.local_mac) /
257 IP(src="1.1.1.1", dst="1.1.1.2") /
258 UDP(sport=1234, dport=1234) /
260 self.send_and_expect(self.pg1, p_2 * 65, self.pg3)
263 # detach the policy from PG1, now expect traffic to be dropped
265 attach_4.remove_vpp_config()
267 self.send_and_assert_no_replies(self.pg1, p_2 * 65, "Detached")
270 """ IPv6 ACL Based Forwarding
274 # Simple test for matching IPv6 packets
280 rule_1 = ({'is_permit': 1,
283 'srcport_or_icmptype_first': 1234,
284 'srcport_or_icmptype_last': 1234,
285 'src_ip_prefix_len': 128,
286 'src_ip_addr': inet_pton(AF_INET6, "2001::2"),
287 'dstport_or_icmpcode_first': 1234,
288 'dstport_or_icmpcode_last': 1234,
289 'dst_ip_prefix_len': 128,
290 'dst_ip_addr': inet_pton(AF_INET6, "2001::1")})
291 acl_1 = self.vapi.acl_add_replace(acl_index=4294967295,
295 # ABF policy for ACL 1 - path via interface 1
297 abf_1 = VppAbfPolicy(self, 10, acl_1,
298 [VppRoutePath("3001::1",
300 proto=DpoProto.DPO_PROTO_IP6)])
301 abf_1.add_vpp_config()
303 attach_1 = VppAbfAttach(self, 10, self.pg0.sw_if_index,
305 attach_1.add_vpp_config()
308 # a packet matching the rule
310 p = (Ether(src=self.pg0.remote_mac,
311 dst=self.pg0.local_mac) /
312 IPv6(src="2001::2", dst="2001::1") /
313 UDP(sport=1234, dport=1234) /
317 # packets are dropped because there is no route to the policy's
320 self.send_and_assert_no_replies(self.pg1, p * 65, "no route")
323 # add a route resolving the next-hop
325 route = VppIpRoute(self, "3001::1", 32,
326 [VppRoutePath(self.pg1.remote_ip6,
327 self.pg1.sw_if_index,
328 proto=DpoProto.DPO_PROTO_IP6)],
330 route.add_vpp_config()
333 # now expect packets forwarded.
335 self.send_and_expect(self.pg0, p * 65, self.pg1)
338 if __name__ == '__main__':
339 unittest.main(testRunner=VppTestRunner)