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