a6b43efe14c07041f6864c2409a0a581fa4fe77a
[vpp.git] / test / vpp_l2.py
1 """
2   L2/BD Types
3
4 """
5
6 from vpp_object import *
7 from util import mactobinary
8 from vpp_ip import VppIpAddress
9 from vpp_mac import VppMacAddress
10 from vpp_lo_interface import VppLoInterface
11
12
13 class L2_PORT_TYPE:
14     NORMAL = 0
15     BVI = 1
16     UU_FWD = 2
17
18
19 class BRIDGE_FLAGS:
20     NONE = 0
21     LEARN = 1
22     FWD = 2
23     FLOOD = 4
24     UU_FLOOD = 8
25     ARP_TERM = 16
26
27
28 def find_bridge_domain(test, bd_id):
29     bds = test.vapi.bridge_domain_dump(bd_id)
30     return len(bds) == 1
31
32
33 def find_bridge_domain_port(test, bd_id, sw_if_index):
34     bds = test.vapi.bridge_domain_dump(bd_id)
35     for bd in bds:
36         for p in bd.sw_if_details:
37             if p.sw_if_index == sw_if_index:
38                 return True
39     return False
40
41
42 def find_bridge_domain_arp_entry(test, bd_id, mac, ip):
43     vmac = VppMacAddress(mac)
44     vip = VppIpAddress(ip)
45
46     if vip.version == 4:
47         n = 4
48     else:
49         n = 16
50
51     arps = test.vapi.bd_ip_mac_dump(bd_id)
52     for arp in arps:
53         # do IP addr comparison too once .api is fixed...
54         if vmac.bytes == arp.mac_address and \
55            vip.bytes == arp.ip_address[:n]:
56             return True
57     return False
58
59
60 def find_l2_fib_entry(test, bd_id, mac, sw_if_index):
61     vmac = VppMacAddress(mac)
62     lfs = test.vapi.l2_fib_table_dump(bd_id)
63     for lf in lfs:
64         if vmac.bytes == lf.mac and sw_if_index == lf.sw_if_index:
65             return True
66     return False
67
68
69 class VppBridgeDomain(VppObject):
70
71     def __init__(self, test, bd_id,
72                  flood=1, uu_flood=1, forward=1,
73                  learn=1, arp_term=1):
74         self._test = test
75         self.bd_id = bd_id
76         self.flood = flood
77         self.uu_flood = uu_flood
78         self.forward = forward
79         self.learn = learn
80         self.arp_term = arp_term
81
82     def add_vpp_config(self):
83         self._test.vapi.bridge_domain_add_del(
84             self.bd_id,
85             is_add=1,
86             flood=self.flood,
87             uu_flood=self.uu_flood,
88             forward=self.forward,
89             learn=self.learn,
90             arp_term=self.arp_term)
91         self._test.registry.register(self, self._test.logger)
92
93     def remove_vpp_config(self):
94         self._test.vapi.bridge_domain_add_del(self.bd_id, is_add=0)
95
96     def query_vpp_config(self):
97         return find_bridge_domain(self._test, self.bd_id)
98
99     def __str__(self):
100         return self.object_id()
101
102     def object_id(self):
103         return "bridge-domain-%d" % (self.bd_id)
104
105
106 class VppBridgeDomainPort(VppObject):
107
108     def __init__(self, test, bd, itf,
109                  port_type=L2_PORT_TYPE.NORMAL):
110         self._test = test
111         self.bd = bd
112         self.itf = itf
113         self.port_type = port_type
114
115     def add_vpp_config(self):
116         self._test.vapi.sw_interface_set_l2_bridge(
117             self.itf.sw_if_index,
118             self.bd.bd_id,
119             port_type=self.port_type,
120             enable=1)
121         self._test.registry.register(self, self._test.logger)
122
123     def remove_vpp_config(self):
124         self._test.vapi.sw_interface_set_l2_bridge(
125             self.itf.sw_if_index,
126             self.bd.bd_id,
127             port_type=self.port_type,
128             enable=0)
129
130     def query_vpp_config(self):
131         return find_bridge_domain_port(self._test,
132                                        self.bd.bd_id,
133                                        self.itf.sw_if_index)
134
135     def __str__(self):
136         return self.object_id()
137
138     def object_id(self):
139         return "BD-Port-%s-%s" % (self.bd, self.itf)
140
141
142 class VppBridgeDomainArpEntry(VppObject):
143
144     def __init__(self, test, bd, mac, ip):
145         self._test = test
146         self.bd = bd
147         self.mac = VppMacAddress(mac)
148         self.ip = VppIpAddress(ip)
149
150     def add_vpp_config(self):
151         self._test.vapi.bd_ip_mac_add_del(
152             self.bd.bd_id,
153             self.mac.encode(),
154             self.ip.encode(),
155             is_add=1)
156         self._test.registry.register(self, self._test.logger)
157
158     def remove_vpp_config(self):
159         self._test.vapi.bd_ip_mac_add_del(
160             self.bd.bd_id,
161             self.mac.encode(),
162             self.ip.encode(),
163             is_add=0)
164
165     def query_vpp_config(self):
166         return find_bridge_domain_arp_entry(self._test,
167                                             self.bd.bd_id,
168                                             self.mac.address,
169                                             self.ip.address)
170
171     def __str__(self):
172         return self.object_id()
173
174     def object_id(self):
175         return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address)
176
177
178 class VppL2FibEntry(VppObject):
179
180     def __init__(self, test, bd, mac, itf,
181                  static_mac=0, filter_mac=0, bvi_mac=-1):
182         self._test = test
183         self.bd = bd
184         self.mac = VppMacAddress(mac)
185         self.itf = itf
186         self.static_mac = static_mac
187         self.filter_mac = filter_mac
188         if bvi_mac == -1:
189             self.bvi_mac = isinstance(self.itf, VppLoInterface)
190         else:
191             self.bvi_mac = bvi_mac
192
193     def add_vpp_config(self):
194         self._test.vapi.l2fib_add_del(
195             self.mac.address,
196             self.bd.bd_id,
197             self.itf.sw_if_index,
198             is_add=1,
199             static_mac=self.static_mac,
200             filter_mac=self.filter_mac,
201             bvi_mac=self.bvi_mac)
202         self._test.registry.register(self, self._test.logger)
203
204     def remove_vpp_config(self):
205         self._test.vapi.l2fib_add_del(
206             self.mac.address,
207             self.bd.bd_id,
208             self.itf.sw_if_index,
209             is_add=0)
210
211     def query_vpp_config(self):
212         return find_l2_fib_entry(self._test,
213                                  self.bd.bd_id,
214                                  self.mac.address,
215                                  self.itf.sw_if_index)
216
217     def __str__(self):
218         return self.object_id()
219
220     def object_id(self):
221         return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf)