Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 1 | from scapy.layers.l2 import Dot1Q |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 2 | import abc |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 3 | from vpp_pg_interface import VppPGInterface |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 4 | from vpp_interface import VppInterface |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 5 | from vpp_papi import VppEnum |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 6 | |
| 7 | |
Paul Vinciguerra | 95c0ca4 | 2019-03-28 13:07:00 -0700 | [diff] [blame] | 8 | class L2_VTR_OP: |
| 9 | L2_DISABLED = 0 |
| 10 | L2_PUSH_1 = 1 |
| 11 | L2_PUSH_2 = 2 |
| 12 | L2_POP_1 = 3 |
| 13 | L2_POP_2 = 4 |
| 14 | L2_TRANSLATE_1_1 = 5 |
| 15 | L2_TRANSLATE_1_2 = 6 |
| 16 | L2_TRANSLATE_2_1 = 7 |
| 17 | L2_TRANSLATE_2_2 = 8 |
| 18 | |
| 19 | |
Paul Vinciguerra | 090096b | 2020-12-03 00:42:46 -0500 | [diff] [blame] | 20 | class VppSubInterface(VppPGInterface, metaclass=abc.ABCMeta): |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 21 | |
| 22 | @property |
| 23 | def parent(self): |
| 24 | """Parent interface for this sub-interface""" |
| 25 | return self._parent |
| 26 | |
| 27 | @property |
| 28 | def sub_id(self): |
| 29 | """Sub-interface ID""" |
| 30 | return self._sub_id |
| 31 | |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 32 | @property |
| 33 | def tag1(self): |
| 34 | return self._tag1 |
| 35 | |
| 36 | @property |
| 37 | def tag2(self): |
| 38 | return self._tag2 |
| 39 | |
| 40 | @property |
| 41 | def vtr(self): |
| 42 | return self._vtr |
| 43 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 44 | def __init__(self, test, parent, sub_id): |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 45 | VppInterface.__init__(self, test) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 46 | self._parent = parent |
| 47 | self._parent.add_sub_if(self) |
| 48 | self._sub_id = sub_id |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 49 | self.DOT1AD_TYPE = 0x88A8 |
| 50 | self.DOT1Q_TYPE = 0x8100 |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 51 | |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 52 | def set_sw_if_index(self, sw_if_index): |
| 53 | super(VppSubInterface, self).set_sw_if_index(sw_if_index) |
| 54 | self.set_vtr(L2_VTR_OP.L2_DISABLED) |
| 55 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 56 | @abc.abstractmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 57 | def create_arp_req(self): |
| 58 | pass |
| 59 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 60 | @abc.abstractmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 61 | def create_ndp_req(self): |
| 62 | pass |
| 63 | |
Klement Sekera | beaded5 | 2018-06-24 10:30:37 +0200 | [diff] [blame] | 64 | def resolve_arp(self): |
| 65 | super(VppSubInterface, self).resolve_arp(self.parent) |
| 66 | |
| 67 | def resolve_ndp(self): |
| 68 | super(VppSubInterface, self).resolve_ndp(self.parent) |
| 69 | |
Paul Vinciguerra | 3bce8eb | 2018-11-24 21:46:05 -0800 | [diff] [blame] | 70 | @abc.abstractmethod |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 71 | def add_dot1_layer(self, pkt): |
| 72 | pass |
| 73 | |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 74 | def remove_vpp_config(self): |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 75 | self.test.vapi.delete_subif(self.sw_if_index) |
Neale Ranns | 177bbdc | 2016-11-15 09:46:51 +0000 | [diff] [blame] | 76 | |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 77 | def _add_tag(self, packet, vlan, tag_type): |
| 78 | payload = packet.payload |
| 79 | inner_type = packet.type |
| 80 | packet.remove_payload() |
| 81 | packet.add_payload(Dot1Q(vlan=vlan) / payload) |
| 82 | packet.payload.type = inner_type |
| 83 | packet.payload.vlan = vlan |
| 84 | packet.type = tag_type |
| 85 | return packet |
| 86 | |
| 87 | def _remove_tag(self, packet, vlan=None, tag_type=None): |
| 88 | if tag_type: |
| 89 | self.test.instance().assertEqual(packet.type, tag_type) |
| 90 | |
| 91 | payload = packet.payload |
| 92 | if vlan: |
| 93 | self.test.instance().assertEqual(payload.vlan, vlan) |
| 94 | inner_type = payload.type |
| 95 | payload = payload.payload |
| 96 | packet.remove_payload() |
| 97 | packet.add_payload(payload) |
| 98 | packet.type = inner_type |
| 99 | return packet |
| 100 | |
| 101 | def add_dot1q_layer(self, packet, vlan): |
| 102 | return self._add_tag(packet, vlan, self.DOT1Q_TYPE) |
| 103 | |
| 104 | def add_dot1ad_layer(self, packet, outer, inner): |
| 105 | p = self._add_tag(packet, inner, self.DOT1Q_TYPE) |
| 106 | return self._add_tag(p, outer, self.DOT1AD_TYPE) |
| 107 | |
| 108 | def remove_dot1q_layer(self, packet, vlan=None): |
| 109 | return self._remove_tag(packet, vlan, self.DOT1Q_TYPE) |
| 110 | |
| 111 | def remove_dot1ad_layer(self, packet, outer=None, inner=None): |
| 112 | p = self._remove_tag(packet, outer, self.DOT1AD_TYPE) |
| 113 | return self._remove_tag(p, inner, self.DOT1Q_TYPE) |
| 114 | |
| 115 | def set_vtr(self, vtr, push1q=0, tag=None, inner=None, outer=None): |
| 116 | self._tag1 = 0 |
| 117 | self._tag2 = 0 |
| 118 | self._push1q = 0 |
| 119 | |
| 120 | if (vtr == L2_VTR_OP.L2_PUSH_1 or |
| 121 | vtr == L2_VTR_OP.L2_TRANSLATE_1_1 or |
| 122 | vtr == L2_VTR_OP.L2_TRANSLATE_2_1): |
| 123 | self._tag1 = tag |
| 124 | self._push1q = push1q |
| 125 | if (vtr == L2_VTR_OP.L2_PUSH_2 or |
| 126 | vtr == L2_VTR_OP.L2_TRANSLATE_1_2 or |
| 127 | vtr == L2_VTR_OP.L2_TRANSLATE_2_2): |
| 128 | self._tag1 = outer |
| 129 | self._tag2 = inner |
| 130 | self._push1q = push1q |
| 131 | |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 132 | self.test.vapi.l2_interface_vlan_tag_rewrite( |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 133 | sw_if_index=self.sw_if_index, vtr_op=vtr, push_dot1q=self._push1q, |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 134 | tag1=self._tag1, tag2=self._tag2) |
| 135 | self._vtr = vtr |
| 136 | |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 137 | |
| 138 | class VppDot1QSubint(VppSubInterface): |
| 139 | |
| 140 | @property |
| 141 | def vlan(self): |
| 142 | """VLAN tag""" |
| 143 | return self._vlan |
| 144 | |
| 145 | def __init__(self, test, parent, sub_id, vlan=None): |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 146 | super(VppDot1QSubint, self).__init__(test, parent, sub_id) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 147 | if vlan is None: |
| 148 | vlan = sub_id |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 149 | self._vlan = vlan |
Matej Klotton | c5bf07f | 2016-11-23 15:27:17 +0100 | [diff] [blame] | 150 | r = test.vapi.create_vlan_subif(parent.sw_if_index, vlan) |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 151 | self.set_sw_if_index(r.sw_if_index) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 152 | |
| 153 | def create_arp_req(self): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 154 | packet = VppPGInterface.create_arp_req(self) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 155 | return self.add_dot1_layer(packet) |
| 156 | |
| 157 | def create_ndp_req(self): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 158 | packet = VppPGInterface.create_ndp_req(self) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 159 | return self.add_dot1_layer(packet) |
| 160 | |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 161 | # called before sending packet |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 162 | def add_dot1_layer(self, packet): |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 163 | return self.add_dot1q_layer(packet, self.vlan) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 164 | |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 165 | # called on received packet to "reverse" the add call |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 166 | def remove_dot1_layer(self, packet): |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 167 | return self.remove_dot1q_layer(packet, self.vlan) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 168 | |
| 169 | |
| 170 | class VppDot1ADSubint(VppSubInterface): |
| 171 | |
| 172 | @property |
| 173 | def outer_vlan(self): |
| 174 | """Outer VLAN tag""" |
| 175 | return self._outer_vlan |
| 176 | |
| 177 | @property |
| 178 | def inner_vlan(self): |
| 179 | """Inner VLAN tag""" |
| 180 | return self._inner_vlan |
| 181 | |
| 182 | def __init__(self, test, parent, sub_id, outer_vlan, inner_vlan): |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 183 | super(VppDot1ADSubint, self).__init__(test, parent, sub_id) |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 184 | flags = (VppEnum.vl_api_sub_if_flags_t.SUB_IF_API_FLAG_DOT1AD | |
| 185 | VppEnum.vl_api_sub_if_flags_t.SUB_IF_API_FLAG_TWO_TAGS | |
| 186 | VppEnum.vl_api_sub_if_flags_t.SUB_IF_API_FLAG_EXACT_MATCH) |
Ole Troan | a5b2eec | 2019-03-11 19:23:25 +0100 | [diff] [blame] | 187 | r = test.vapi.create_subif(sw_if_index=parent.sw_if_index, |
| 188 | sub_id=sub_id, outer_vlan_id=outer_vlan, |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 189 | inner_vlan_id=inner_vlan, |
| 190 | sub_if_flags=flags) |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 191 | self.set_sw_if_index(r.sw_if_index) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 192 | self._outer_vlan = outer_vlan |
| 193 | self._inner_vlan = inner_vlan |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 194 | |
| 195 | def create_arp_req(self): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 196 | packet = VppPGInterface.create_arp_req(self) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 197 | return self.add_dot1_layer(packet) |
| 198 | |
| 199 | def create_ndp_req(self): |
Matej Klotton | 0178d52 | 2016-11-04 11:11:44 +0100 | [diff] [blame] | 200 | packet = VppPGInterface.create_ndp_req(self) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 201 | return self.add_dot1_layer(packet) |
| 202 | |
| 203 | def add_dot1_layer(self, packet): |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 204 | return self.add_dot1ad_layer(packet, self.outer_vlan, self.inner_vlan) |
Klement Sekera | f62ae12 | 2016-10-11 11:47:09 +0200 | [diff] [blame] | 205 | |
| 206 | def remove_dot1_layer(self, packet): |
Eyal Bari | 284293a | 2017-06-06 14:18:55 +0300 | [diff] [blame] | 207 | return self.remove_dot1ad_layer(packet, self.outer_vlan, |
| 208 | self.inner_vlan) |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 209 | |
| 210 | |
| 211 | class VppP2PSubint(VppSubInterface): |
| 212 | |
| 213 | def __init__(self, test, parent, sub_id, remote_mac): |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 214 | super(VppP2PSubint, self).__init__(test, parent, sub_id) |
Ole Troan | e1ade68 | 2019-03-04 23:55:43 +0100 | [diff] [blame] | 215 | r = test.vapi.p2p_ethernet_add(parent.sw_if_index, |
| 216 | remote_mac, sub_id) |
Klement Sekera | b9ef273 | 2018-06-24 22:49:33 +0200 | [diff] [blame] | 217 | self.set_sw_if_index(r.sw_if_index) |
Neale Ranns | 2ae2bc5 | 2018-03-16 03:22:39 -0700 | [diff] [blame] | 218 | self.parent_sw_if_index = parent.sw_if_index |
| 219 | self.p2p_remote_mac = remote_mac |
Pavel Kotucek | 15ac81c | 2017-06-20 14:00:26 +0200 | [diff] [blame] | 220 | |
| 221 | def add_dot1_layer(self, packet): |
| 222 | return packet |
| 223 | |
| 224 | def remove_dot1_layer(self, packet): |
| 225 | return packet |
| 226 | |
| 227 | def create_arp_req(self): |
| 228 | packet = VppPGInterface.create_arp_req(self) |
| 229 | return packet |
| 230 | |
| 231 | def create_ndp_req(self): |
| 232 | packet = VppPGInterface.create_ndp_req(self) |
| 233 | return packet |