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