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