blob: 8f422d8a0501b3fae063bdea9277557a44b7d199 [file] [log] [blame]
Eyal Bari284293a2017-06-06 14:18:55 +03001from scapy.layers.l2 import Dot1Q
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -08002import abc
3import six
Matej Klotton0178d522016-11-04 11:11:44 +01004from vpp_pg_interface import VppPGInterface
Klement Sekerabeaded52018-06-24 10:30:37 +02005from vpp_interface import VppInterface
Jakub Grajciar053204a2019-03-18 13:17:53 +01006from vpp_papi import VppEnum
Klement Sekeraf62ae122016-10-11 11:47:09 +02007
8
Paul Vinciguerra95c0ca42019-03-28 13:07:00 -07009class 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 Vinciguerra3bce8eb2018-11-24 21:46:05 -080021@six.add_metaclass(abc.ABCMeta)
Matej Klotton0178d522016-11-04 11:11:44 +010022class VppSubInterface(VppPGInterface):
Klement Sekeraf62ae122016-10-11 11:47:09 +020023
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 Bari284293a2017-06-06 14:18:55 +030034 @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 Sekeraf62ae122016-10-11 11:47:09 +020046 def __init__(self, test, parent, sub_id):
Matej Klottonc5bf07f2016-11-23 15:27:17 +010047 VppInterface.__init__(self, test)
Klement Sekeraf62ae122016-10-11 11:47:09 +020048 self._parent = parent
49 self._parent.add_sub_if(self)
50 self._sub_id = sub_id
Eyal Bari284293a2017-06-06 14:18:55 +030051 self.DOT1AD_TYPE = 0x88A8
52 self.DOT1Q_TYPE = 0x8100
Klement Sekeraf62ae122016-10-11 11:47:09 +020053
Klement Sekerab9ef2732018-06-24 22:49:33 +020054 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 Vinciguerra3bce8eb2018-11-24 21:46:05 -080058 @abc.abstractmethod
Klement Sekeraf62ae122016-10-11 11:47:09 +020059 def create_arp_req(self):
60 pass
61
Paul Vinciguerra3bce8eb2018-11-24 21:46:05 -080062 @abc.abstractmethod
Klement Sekeraf62ae122016-10-11 11:47:09 +020063 def create_ndp_req(self):
64 pass
65
Klement Sekerabeaded52018-06-24 10:30:37 +020066 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 Vinciguerra3bce8eb2018-11-24 21:46:05 -080072 @abc.abstractmethod
Klement Sekeraf62ae122016-10-11 11:47:09 +020073 def add_dot1_layer(self, pkt):
74 pass
75
Neale Ranns177bbdc2016-11-15 09:46:51 +000076 def remove_vpp_config(self):
Klement Sekerab9ef2732018-06-24 22:49:33 +020077 self.test.vapi.delete_subif(self.sw_if_index)
Neale Ranns177bbdc2016-11-15 09:46:51 +000078
Eyal Bari284293a2017-06-06 14:18:55 +030079 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 Troane1ade682019-03-04 23:55:43 +0100134 self.test.vapi.l2_interface_vlan_tag_rewrite(
Ole Troana5b2eec2019-03-11 19:23:25 +0100135 sw_if_index=self.sw_if_index, vtr_op=vtr, push_dot1q=self._push1q,
Eyal Bari284293a2017-06-06 14:18:55 +0300136 tag1=self._tag1, tag2=self._tag2)
137 self._vtr = vtr
138
Klement Sekeraf62ae122016-10-11 11:47:09 +0200139
140class 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 Sekerab9ef2732018-06-24 22:49:33 +0200148 super(VppDot1QSubint, self).__init__(test, parent, sub_id)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200149 if vlan is None:
150 vlan = sub_id
Klement Sekeraf62ae122016-10-11 11:47:09 +0200151 self._vlan = vlan
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100152 r = test.vapi.create_vlan_subif(parent.sw_if_index, vlan)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200153 self.set_sw_if_index(r.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200154
155 def create_arp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100156 packet = VppPGInterface.create_arp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200157 return self.add_dot1_layer(packet)
158
159 def create_ndp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100160 packet = VppPGInterface.create_ndp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200161 return self.add_dot1_layer(packet)
162
Eyal Bari284293a2017-06-06 14:18:55 +0300163 # called before sending packet
Klement Sekeraf62ae122016-10-11 11:47:09 +0200164 def add_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300165 return self.add_dot1q_layer(packet, self.vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200166
Eyal Bari284293a2017-06-06 14:18:55 +0300167 # called on received packet to "reverse" the add call
Klement Sekeraf62ae122016-10-11 11:47:09 +0200168 def remove_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300169 return self.remove_dot1q_layer(packet, self.vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200170
171
172class 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 Sekerab9ef2732018-06-24 22:49:33 +0200185 super(VppDot1ADSubint, self).__init__(test, parent, sub_id)
Jakub Grajciar053204a2019-03-18 13:17:53 +0100186 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 Troana5b2eec2019-03-11 19:23:25 +0100189 r = test.vapi.create_subif(sw_if_index=parent.sw_if_index,
190 sub_id=sub_id, outer_vlan_id=outer_vlan,
Jakub Grajciar053204a2019-03-18 13:17:53 +0100191 inner_vlan_id=inner_vlan,
192 sub_if_flags=flags)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200193 self.set_sw_if_index(r.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200194 self._outer_vlan = outer_vlan
195 self._inner_vlan = inner_vlan
Klement Sekeraf62ae122016-10-11 11:47:09 +0200196
197 def create_arp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100198 packet = VppPGInterface.create_arp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200199 return self.add_dot1_layer(packet)
200
201 def create_ndp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100202 packet = VppPGInterface.create_ndp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200203 return self.add_dot1_layer(packet)
204
205 def add_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300206 return self.add_dot1ad_layer(packet, self.outer_vlan, self.inner_vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200207
208 def remove_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300209 return self.remove_dot1ad_layer(packet, self.outer_vlan,
210 self.inner_vlan)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200211
212
213class VppP2PSubint(VppSubInterface):
214
215 def __init__(self, test, parent, sub_id, remote_mac):
Klement Sekerab9ef2732018-06-24 22:49:33 +0200216 super(VppP2PSubint, self).__init__(test, parent, sub_id)
Ole Troane1ade682019-03-04 23:55:43 +0100217 r = test.vapi.p2p_ethernet_add(parent.sw_if_index,
218 remote_mac, sub_id)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200219 self.set_sw_if_index(r.sw_if_index)
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700220 self.parent_sw_if_index = parent.sw_if_index
221 self.p2p_remote_mac = remote_mac
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200222
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