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 |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class L2_PORT_TYPE: |
| 13 | NORMAL = 0 |
| 14 | BVI = 1 |
| 15 | UU_FWD = 2 |
| 16 | |
| 17 | |
| 18 | class BRIDGE_FLAGS: |
| 19 | NONE = 0 |
| 20 | LEARN = 1 |
| 21 | FWD = 2 |
| 22 | FLOOD = 4 |
| 23 | UU_FLOOD = 8 |
| 24 | ARP_TERM = 16 |
| 25 | |
| 26 | |
| 27 | def find_bridge_domain(test, bd_id): |
| 28 | bds = test.vapi.bridge_domain_dump(bd_id) |
| 29 | return len(bds) == 1 |
| 30 | |
| 31 | |
| 32 | def find_bridge_domain_port(test, bd_id, sw_if_index): |
| 33 | bds = test.vapi.bridge_domain_dump(bd_id) |
| 34 | for bd in bds: |
| 35 | for p in bd.sw_if_details: |
| 36 | if p.sw_if_index == sw_if_index: |
| 37 | return True |
| 38 | return False |
| 39 | |
| 40 | |
| 41 | def find_bridge_domain_arp_entry(test, bd_id, mac, ip): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 42 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 43 | vip = VppIpAddress(ip) |
| 44 | |
| 45 | if vip.version == 4: |
| 46 | n = 4 |
| 47 | else: |
| 48 | n = 16 |
| 49 | |
| 50 | arps = test.vapi.bd_ip_mac_dump(bd_id) |
| 51 | for arp in arps: |
| 52 | # do IP addr comparison too once .api is fixed... |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 53 | if vmac.packed == arp.mac_address and \ |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame^] | 54 | vip.bytes == arp.ip_address[:n]: |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 55 | return True |
| 56 | return False |
| 57 | |
| 58 | |
| 59 | def find_l2_fib_entry(test, bd_id, mac, sw_if_index): |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 60 | vmac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 61 | lfs = test.vapi.l2_fib_table_dump(bd_id) |
| 62 | for lf in lfs: |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 63 | 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] | 64 | return True |
| 65 | return False |
| 66 | |
| 67 | |
| 68 | class VppBridgeDomain(VppObject): |
| 69 | |
| 70 | def __init__(self, test, bd_id, |
| 71 | flood=1, uu_flood=1, forward=1, |
| 72 | learn=1, arp_term=1): |
| 73 | self._test = test |
| 74 | self.bd_id = bd_id |
| 75 | self.flood = flood |
| 76 | self.uu_flood = uu_flood |
| 77 | self.forward = forward |
| 78 | self.learn = learn |
| 79 | self.arp_term = arp_term |
| 80 | |
| 81 | def add_vpp_config(self): |
| 82 | self._test.vapi.bridge_domain_add_del( |
| 83 | self.bd_id, |
| 84 | is_add=1, |
| 85 | flood=self.flood, |
| 86 | uu_flood=self.uu_flood, |
| 87 | forward=self.forward, |
| 88 | learn=self.learn, |
| 89 | arp_term=self.arp_term) |
| 90 | self._test.registry.register(self, self._test.logger) |
| 91 | |
| 92 | def remove_vpp_config(self): |
| 93 | self._test.vapi.bridge_domain_add_del(self.bd_id, is_add=0) |
| 94 | |
| 95 | def query_vpp_config(self): |
| 96 | return find_bridge_domain(self._test, self.bd_id) |
| 97 | |
| 98 | def __str__(self): |
| 99 | return self.object_id() |
| 100 | |
| 101 | def object_id(self): |
| 102 | return "bridge-domain-%d" % (self.bd_id) |
| 103 | |
| 104 | |
| 105 | class VppBridgeDomainPort(VppObject): |
| 106 | |
| 107 | def __init__(self, test, bd, itf, |
| 108 | port_type=L2_PORT_TYPE.NORMAL): |
| 109 | self._test = test |
| 110 | self.bd = bd |
| 111 | self.itf = itf |
| 112 | self.port_type = port_type |
| 113 | |
| 114 | def add_vpp_config(self): |
| 115 | self._test.vapi.sw_interface_set_l2_bridge( |
| 116 | self.itf.sw_if_index, |
| 117 | self.bd.bd_id, |
| 118 | port_type=self.port_type, |
| 119 | enable=1) |
| 120 | self._test.registry.register(self, self._test.logger) |
| 121 | |
| 122 | def remove_vpp_config(self): |
| 123 | self._test.vapi.sw_interface_set_l2_bridge( |
| 124 | self.itf.sw_if_index, |
| 125 | self.bd.bd_id, |
| 126 | port_type=self.port_type, |
| 127 | enable=0) |
| 128 | |
| 129 | def query_vpp_config(self): |
| 130 | return find_bridge_domain_port(self._test, |
| 131 | self.bd.bd_id, |
| 132 | self.itf.sw_if_index) |
| 133 | |
| 134 | def __str__(self): |
| 135 | return self.object_id() |
| 136 | |
| 137 | def object_id(self): |
| 138 | return "BD-Port-%s-%s" % (self.bd, self.itf) |
| 139 | |
| 140 | |
| 141 | class VppBridgeDomainArpEntry(VppObject): |
| 142 | |
| 143 | def __init__(self, test, bd, mac, ip): |
| 144 | self._test = test |
| 145 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 146 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 147 | self.ip = VppIpAddress(ip) |
| 148 | |
| 149 | def add_vpp_config(self): |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame^] | 150 | self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=1, |
| 151 | ip=self.ip.encode(), |
| 152 | mac=self.mac.packed) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 153 | self._test.registry.register(self, self._test.logger) |
| 154 | |
| 155 | def remove_vpp_config(self): |
Ole Troan | 9a47537 | 2019-03-05 16:58:24 +0100 | [diff] [blame^] | 156 | self._test.vapi.bd_ip_mac_add_del(bd_id=self.bd.bd_id, is_add=0, |
| 157 | ip=self.ip.encode(), |
| 158 | mac=self.mac.packed) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 159 | |
| 160 | def query_vpp_config(self): |
| 161 | return find_bridge_domain_arp_entry(self._test, |
| 162 | self.bd.bd_id, |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 163 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 164 | self.ip.address) |
| 165 | |
| 166 | def __str__(self): |
| 167 | return self.object_id() |
| 168 | |
| 169 | def object_id(self): |
| 170 | return "BD-Arp-Entry-%s-%s-%s" % (self.bd, self.mac, self.ip.address) |
| 171 | |
| 172 | |
| 173 | class VppL2FibEntry(VppObject): |
| 174 | |
| 175 | def __init__(self, test, bd, mac, itf, |
| 176 | static_mac=0, filter_mac=0, bvi_mac=-1): |
| 177 | self._test = test |
| 178 | self.bd = bd |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 179 | self.mac = MACAddress(mac) |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 180 | self.itf = itf |
| 181 | self.static_mac = static_mac |
| 182 | self.filter_mac = filter_mac |
| 183 | if bvi_mac == -1: |
| 184 | self.bvi_mac = isinstance(self.itf, VppLoInterface) |
| 185 | else: |
| 186 | self.bvi_mac = bvi_mac |
| 187 | |
| 188 | def add_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=1, |
| 194 | static_mac=self.static_mac, |
| 195 | filter_mac=self.filter_mac, |
| 196 | bvi_mac=self.bvi_mac) |
| 197 | self._test.registry.register(self, self._test.logger) |
| 198 | |
| 199 | def remove_vpp_config(self): |
| 200 | self._test.vapi.l2fib_add_del( |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 201 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 202 | self.bd.bd_id, |
| 203 | self.itf.sw_if_index, |
| 204 | is_add=0) |
| 205 | |
| 206 | def query_vpp_config(self): |
| 207 | return find_l2_fib_entry(self._test, |
| 208 | self.bd.bd_id, |
Ole Troan | 8006c6a | 2018-12-17 12:02:26 +0100 | [diff] [blame] | 209 | self.mac.packed, |
Neale Ranns | 93cc3ee | 2018-10-10 07:22:51 -0700 | [diff] [blame] | 210 | self.itf.sw_if_index) |
| 211 | |
| 212 | def __str__(self): |
| 213 | return self.object_id() |
| 214 | |
| 215 | def object_id(self): |
| 216 | return "L2-Fib-Entry-%s-%s-%s" % (self.bd, self.mac, self.itf) |