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