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