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