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_ip import VppIpAddress |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 8 | from vpp_lo_interface import VppLoInterface |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 9 | from vpp_papi import MACAddress |
Paul Vinciguerra | 95c0ca4 | 2019-03-28 13:07:00 -0700 | [diff] [blame] | 10 | from vpp_sub_interface import L2_VTR_OP |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 43 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 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... |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 54 | if vmac.packed == arp.mac_address and \ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 55 | vip.bytes == arp.ip_address[:n]: |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 56 | return True |
| 57 | return False |
| 58 | |
| 59 | |
| 60 | def find_l2_fib_entry(test, bd_id, mac, sw_if_index): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 61 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 62 | lfs = test.vapi.l2_fib_table_dump(bd_id) |
| 63 | for lf in lfs: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 64 | 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] | 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): |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 83 | self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, |
| 84 | flood=self.flood, |
| 85 | uu_flood=self.uu_flood, |
| 86 | forward=self.forward, |
| 87 | learn=self.learn, |
| 88 | arp_term=self.arp_term, is_add=1) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 89 | self._test.registry.register(self, self._test.logger) |
| 90 | |
| 91 | def remove_vpp_config(self): |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 92 | self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, is_add=0) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 93 | |
| 94 | def query_vpp_config(self): |
| 95 | return find_bridge_domain(self._test, self.bd_id) |
| 96 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 97 | def object_id(self): |
| 98 | return "bridge-domain-%d" % (self.bd_id) |
| 99 | |
| 100 | |
| 101 | class VppBridgeDomainPort(VppObject): |
| 102 | |
| 103 | def __init__(self, test, bd, itf, |
| 104 | port_type=L2_PORT_TYPE.NORMAL): |
| 105 | self._test = test |
| 106 | self.bd = bd |
| 107 | self.itf = itf |
| 108 | self.port_type = port_type |
| 109 | |
| 110 | def add_vpp_config(self): |
| 111 | self._test.vapi.sw_interface_set_l2_bridge( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 112 | rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id, |
| 113 | port_type=self.port_type, enable=1) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 114 | self._test.registry.register(self, self._test.logger) |
| 115 | |
| 116 | def remove_vpp_config(self): |
| 117 | self._test.vapi.sw_interface_set_l2_bridge( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 118 | rx_sw_if_index=self.itf.sw_if_index, bd_id=self.bd.bd_id, |
| 119 | port_type=self.port_type, enable=0) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 120 | |
| 121 | def query_vpp_config(self): |
| 122 | return find_bridge_domain_port(self._test, |
| 123 | self.bd.bd_id, |
| 124 | self.itf.sw_if_index) |
| 125 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 126 | def object_id(self): |
| 127 | return "BD-Port-%s-%s" % (self.bd, self.itf) |
| 128 | |
| 129 | |
| 130 | class VppBridgeDomainArpEntry(VppObject): |
| 131 | |
| 132 | def __init__(self, test, bd, mac, ip): |
| 133 | self._test = test |
| 134 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 135 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 136 | self.ip = VppIpAddress(ip) |
| 137 | |
| 138 | def add_vpp_config(self): |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 139 | self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1, |
| 140 | ip=self.ip.encode(), |
| 141 | mac=self.mac.packed) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 142 | self._test.registry.register(self, self._test.logger) |
| 143 | |
| 144 | def remove_vpp_config(self): |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame] | 145 | self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0, |
| 146 | ip=self.ip.encode(), |
| 147 | mac=self.mac.packed) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 148 | |
| 149 | def query_vpp_config(self): |
| 150 | return find_bridge_domain_arp_entry(self._test, |
| 151 | self.bd.bd_id, |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 152 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 153 | self.ip.address) |
| 154 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 155 | def object_id(self): |
| 156 | return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address) |
| 157 | |
| 158 | |
| 159 | class VppL2FibEntry(VppObject): |
| 160 | |
| 161 | def __init__(self, test, bd, mac, itf, |
| 162 | static_mac=0, filter_mac=0, bvi_mac=-1): |
| 163 | self._test = test |
| 164 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 165 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 166 | self.itf = itf |
| 167 | self.static_mac = static_mac |
| 168 | self.filter_mac = filter_mac |
| 169 | if bvi_mac == -1: |
| 170 | self.bvi_mac = isinstance(self.itf, VppLoInterface) |
| 171 | else: |
| 172 | self.bvi_mac = bvi_mac |
| 173 | |
| 174 | def add_vpp_config(self): |
| 175 | self._test.vapi.l2fib_add_del( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 176 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 177 | self.bd.bd_id, |
| 178 | self.itf.sw_if_index, |
| 179 | is_add=1, |
| 180 | static_mac=self.static_mac, |
| 181 | filter_mac=self.filter_mac, |
| 182 | bvi_mac=self.bvi_mac) |
| 183 | self._test.registry.register(self, self._test.logger) |
| 184 | |
| 185 | def remove_vpp_config(self): |
| 186 | self._test.vapi.l2fib_add_del( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 187 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 188 | self.bd.bd_id, |
| 189 | self.itf.sw_if_index, |
| 190 | is_add=0) |
| 191 | |
| 192 | def query_vpp_config(self): |
| 193 | return find_l2_fib_entry(self._test, |
| 194 | self.bd.bd_id, |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 195 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 196 | self.itf.sw_if_index) |
| 197 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 198 | def object_id(self): |
| 199 | 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] | 200 | |
| 201 | |
| 202 | class VppL2Vtr(VppObject): |
| 203 | |
| 204 | def __init__(self, test, itf, op): |
| 205 | self._test = test |
| 206 | self.itf = itf |
| 207 | self.op = op |
| 208 | |
| 209 | def add_vpp_config(self): |
| 210 | self.itf.set_vtr(self.op) |
| 211 | self._test.registry.register(self, self._test.logger) |
| 212 | |
| 213 | def remove_vpp_config(self): |
| 214 | self.itf.set_vtr(L2_VTR_OP.L2_DISABLED) |
| 215 | |
| 216 | def query_vpp_config(self): |
| 217 | ds = self._test.vapi.sw_interface_dump() |
| 218 | d = self.itf.get_interface_config_from_dump(ds) |
| 219 | |
| 220 | if d is not None: |
| 221 | return (d.vtr_op == self.op) |
| 222 | return False |
| 223 | |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 224 | def object_id(self): |
| 225 | return "L2-vtr-%s-%d" % (str(self.itf), self.op) |