Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 1 | """ |
| 2 | L2/BD Types |
| 3 | |
| 4 | """ |
| 5 | |
Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 6 | from vpp_object import VppObject |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 7 | from vpp_lo_interface import VppLoInterface |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 8 | from vpp_papi import MACAddress |
Paul Vinciguerra | 95c0ca4 | 2019-03-28 13:07:00 -0700 | [diff] [blame] | 9 | from vpp_sub_interface import L2_VTR_OP |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 10 | |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 11 | try: |
| 12 | text_type = unicode |
| 13 | except NameError: |
| 14 | text_type = str |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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 |
Mohsin Kazmi | 5e6f734 | 2019-04-05 17:40:20 +0200 | [diff] [blame] | 30 | ARP_UFWD = 32 |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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... |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 51 | if mac == str(arp.entry.mac) and ip == str(arp.entry.ip): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 52 | return True |
| 53 | return False |
| 54 | |
| 55 | |
| 56 | def find_l2_fib_entry(test, bd_id, mac, sw_if_index): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 57 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 58 | lfs = test.vapi.l2_fib_table_dump(bd_id) |
| 59 | for lf in lfs: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 60 | if vmac.packed == lf.mac and sw_if_index == lf.sw_if_index: |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 61 | return True |
| 62 | return False |
| 63 | |
| 64 | |
| 65 | class VppBridgeDomain(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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 |
Mohsin Kazmi | 5e6f734 | 2019-04-05 17:40:20 +0200 | [diff] [blame] | 84 | self.arp_ufwd = arp_ufwd |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 85 | |
| 86 | def add_vpp_config(self): |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 87 | self._test.vapi.bridge_domain_add_del_v2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 97 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 61aad0a | 2021-10-25 09:13:00 +0000 | [diff] [blame] | 98 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 99 | |
| 100 | def remove_vpp_config(self): |
Laszlo Kiraly | 0f8f435 | 2022-09-16 13:20:07 +0200 | [diff] [blame] | 101 | self._test.vapi.bridge_domain_add_del_v2(bd_id=self.bd_id, is_add=0) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 102 | |
| 103 | def query_vpp_config(self): |
| 104 | return find_bridge_domain(self._test, self.bd_id) |
| 105 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 106 | def object_id(self): |
| 107 | return "bridge-domain-%d" % (self.bd_id) |
| 108 | |
| 109 | |
| 110 | class VppBridgeDomainPort(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 111 | def __init__(self, test, bd, itf, port_type=L2_PORT_TYPE.NORMAL): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 124 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 61aad0a | 2021-10-25 09:13:00 +0000 | [diff] [blame] | 125 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 126 | |
| 127 | def remove_vpp_config(self): |
| 128 | self._test.vapi.sw_interface_set_l2_bridge( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 134 | |
| 135 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 136 | return find_bridge_domain_port(self._test, self.bd.bd_id, self.itf.sw_if_index) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 137 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 138 | def object_id(self): |
| 139 | return "BD-Port-%s-%s" % (self.bd, self.itf) |
| 140 | |
| 141 | |
| 142 | class VppBridgeDomainArpEntry(VppObject): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 143 | def __init__(self, test, bd, mac, ip): |
| 144 | self._test = test |
| 145 | self.bd = bd |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 146 | self.mac = mac |
| 147 | self.ip = ip |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 148 | |
| 149 | def add_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 153 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 61aad0a | 2021-10-25 09:13:00 +0000 | [diff] [blame] | 154 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 155 | |
| 156 | def remove_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 160 | |
| 161 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 162 | return find_bridge_domain_arp_entry( |
| 163 | self._test, self.bd.bd_id, self.mac, self.ip |
| 164 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 165 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 166 | def object_id(self): |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 167 | return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 168 | |
| 169 | |
| 170 | class VppL2FibEntry(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 171 | def __init__(self, test, bd, mac, itf, static_mac=0, filter_mac=0, bvi_mac=-1): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 172 | self._test = test |
| 173 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 174 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 185 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 191 | bvi_mac=self.bvi_mac, |
| 192 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 193 | self._test.registry.register(self, self._test.logger) |
Neale Ranns | 61aad0a | 2021-10-25 09:13:00 +0000 | [diff] [blame] | 194 | return self |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 195 | |
| 196 | def remove_vpp_config(self): |
| 197 | self._test.vapi.l2fib_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 198 | self.mac.packed, self.bd.bd_id, self.itf.sw_if_index, is_add=0 |
| 199 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 200 | |
| 201 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 202 | return find_l2_fib_entry( |
| 203 | self._test, self.bd.bd_id, self.mac.packed, self.itf.sw_if_index |
| 204 | ) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 205 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 206 | def object_id(self): |
| 207 | return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf) |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | class VppL2Vtr(VppObject): |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 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) |
Neale Ranns | 61aad0a | 2021-10-25 09:13:00 +0000 | [diff] [blame] | 219 | return self |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 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: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 229 | return d.vtr_op == self.op |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 230 | return False |
| 231 | |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 232 | def object_id(self): |
| 233 | return "L2-vtr-%s-%d" % (str(self.itf), self.op) |