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 | 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... |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 51 | if mac == str(arp.entry.mac) and \ |
| 52 | ip == str(arp.entry.ip): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 53 | return True |
| 54 | return False |
| 55 | |
| 56 | |
| 57 | def find_l2_fib_entry(test, bd_id, mac, sw_if_index): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 58 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 59 | lfs = test.vapi.l2_fib_table_dump(bd_id) |
| 60 | for lf in lfs: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 61 | 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] | 62 | return True |
| 63 | return False |
| 64 | |
| 65 | |
| 66 | class VppBridgeDomain(VppObject): |
| 67 | |
| 68 | def __init__(self, test, bd_id, |
| 69 | flood=1, uu_flood=1, forward=1, |
Mohsin Kazmi | 5e6f734 | 2019-04-05 17:40:20 +0200 | [diff] [blame] | 70 | learn=1, arp_term=1, arp_ufwd=0): |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 71 | self._test = test |
| 72 | self.bd_id = bd_id |
| 73 | self.flood = flood |
| 74 | self.uu_flood = uu_flood |
| 75 | self.forward = forward |
| 76 | self.learn = learn |
| 77 | self.arp_term = arp_term |
Mohsin Kazmi | 5e6f734 | 2019-04-05 17:40:20 +0200 | [diff] [blame] | 78 | self.arp_ufwd = arp_ufwd |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 79 | |
| 80 | def add_vpp_config(self): |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 81 | self._test.vapi.bridge_domain_add_del(bd_id=self.bd_id, |
| 82 | flood=self.flood, |
| 83 | uu_flood=self.uu_flood, |
| 84 | forward=self.forward, |
| 85 | learn=self.learn, |
Mohsin Kazmi | 5e6f734 | 2019-04-05 17:40:20 +0200 | [diff] [blame] | 86 | arp_term=self.arp_term, |
| 87 | arp_ufwd=self.arp_ufwd, |
| 88 | 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 |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 135 | self.mac = mac |
| 136 | self.ip = ip |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 137 | |
| 138 | def add_vpp_config(self): |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 139 | self._test.vapi.bd_ip_mac_add_del(is_add=1, |
| 140 | entry={ |
| 141 | 'bd_id': self.bd.bd_id, |
| 142 | 'ip': self.ip, |
| 143 | 'mac': self.mac}) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 144 | self._test.registry.register(self, self._test.logger) |
| 145 | |
| 146 | def remove_vpp_config(self): |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 147 | self._test.vapi.bd_ip_mac_add_del(is_add=0, |
| 148 | entry={ |
| 149 | 'bd_id': self.bd.bd_id, |
| 150 | 'ip': self.ip, |
| 151 | 'mac': self.mac}) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 152 | |
| 153 | def query_vpp_config(self): |
| 154 | return find_bridge_domain_arp_entry(self._test, |
| 155 | self.bd.bd_id, |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 156 | self.mac, |
| 157 | self.ip) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 158 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 159 | def object_id(self): |
Neale Ranns | bc764c8 | 2019-06-19 07:07:13 -0700 | [diff] [blame] | 160 | 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] | 161 | |
| 162 | |
| 163 | class VppL2FibEntry(VppObject): |
| 164 | |
| 165 | def __init__(self, test, bd, mac, itf, |
| 166 | static_mac=0, filter_mac=0, bvi_mac=-1): |
| 167 | self._test = test |
| 168 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 169 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 170 | self.itf = itf |
| 171 | self.static_mac = static_mac |
| 172 | self.filter_mac = filter_mac |
| 173 | if bvi_mac == -1: |
| 174 | self.bvi_mac = isinstance(self.itf, VppLoInterface) |
| 175 | else: |
| 176 | self.bvi_mac = bvi_mac |
| 177 | |
| 178 | def add_vpp_config(self): |
| 179 | self._test.vapi.l2fib_add_del( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 180 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 181 | self.bd.bd_id, |
| 182 | self.itf.sw_if_index, |
| 183 | is_add=1, |
| 184 | static_mac=self.static_mac, |
| 185 | filter_mac=self.filter_mac, |
| 186 | bvi_mac=self.bvi_mac) |
| 187 | self._test.registry.register(self, self._test.logger) |
| 188 | |
| 189 | def remove_vpp_config(self): |
| 190 | self._test.vapi.l2fib_add_del( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 191 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 192 | self.bd.bd_id, |
| 193 | self.itf.sw_if_index, |
| 194 | is_add=0) |
| 195 | |
| 196 | def query_vpp_config(self): |
| 197 | return find_l2_fib_entry(self._test, |
| 198 | self.bd.bd_id, |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 199 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 200 | self.itf.sw_if_index) |
| 201 | |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 202 | def object_id(self): |
| 203 | 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] | 204 | |
| 205 | |
| 206 | class VppL2Vtr(VppObject): |
| 207 | |
| 208 | def __init__(self, test, itf, op): |
| 209 | self._test = test |
| 210 | self.itf = itf |
| 211 | self.op = op |
| 212 | |
| 213 | def add_vpp_config(self): |
| 214 | self.itf.set_vtr(self.op) |
| 215 | self._test.registry.register(self, self._test.logger) |
| 216 | |
| 217 | def remove_vpp_config(self): |
| 218 | self.itf.set_vtr(L2_VTR_OP.L2_DISABLED) |
| 219 | |
| 220 | def query_vpp_config(self): |
| 221 | ds = self._test.vapi.sw_interface_dump() |
| 222 | d = self.itf.get_interface_config_from_dump(ds) |
| 223 | |
| 224 | if d is not None: |
| 225 | return (d.vtr_op == self.op) |
| 226 | return False |
| 227 | |
Neale Ranns | 36abbf1 | 2019-03-12 02:34:07 -0700 | [diff] [blame] | 228 | def object_id(self): |
| 229 | return "L2-vtr-%s-%d" % (str(self.itf), self.op) |