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