blob: 255cfffd827a46a481830562aae37055a9b9e5a1 [file] [log] [blame]
Eyal Bari284293a2017-06-06 14:18:55 +03001from scapy.layers.l2 import Dot1Q
Klement Sekeraf62ae122016-10-11 11:47:09 +02002from abc import abstractmethod, ABCMeta
Matej Klotton0178d522016-11-04 11:11:44 +01003from vpp_pg_interface import VppPGInterface
Eyal Bari284293a2017-06-06 14:18:55 +03004from vpp_papi_provider import L2_VTR_OP
Klement Sekerabeaded52018-06-24 10:30:37 +02005from vpp_interface import VppInterface
Klement Sekeraf62ae122016-10-11 11:47:09 +02006
7
Matej Klotton0178d522016-11-04 11:11:44 +01008class VppSubInterface(VppPGInterface):
Klement Sekeraf62ae122016-10-11 11:47:09 +02009 __metaclass__ = ABCMeta
10
11 @property
12 def parent(self):
13 """Parent interface for this sub-interface"""
14 return self._parent
15
16 @property
17 def sub_id(self):
18 """Sub-interface ID"""
19 return self._sub_id
20
Eyal Bari284293a2017-06-06 14:18:55 +030021 @property
22 def tag1(self):
23 return self._tag1
24
25 @property
26 def tag2(self):
27 return self._tag2
28
29 @property
30 def vtr(self):
31 return self._vtr
32
Klement Sekeraf62ae122016-10-11 11:47:09 +020033 def __init__(self, test, parent, sub_id):
Matej Klottonc5bf07f2016-11-23 15:27:17 +010034 VppInterface.__init__(self, test)
Klement Sekeraf62ae122016-10-11 11:47:09 +020035 self._parent = parent
36 self._parent.add_sub_if(self)
37 self._sub_id = sub_id
Eyal Bari284293a2017-06-06 14:18:55 +030038 self.DOT1AD_TYPE = 0x88A8
39 self.DOT1Q_TYPE = 0x8100
Klement Sekeraf62ae122016-10-11 11:47:09 +020040
Klement Sekerab9ef2732018-06-24 22:49:33 +020041 def set_sw_if_index(self, sw_if_index):
42 super(VppSubInterface, self).set_sw_if_index(sw_if_index)
43 self.set_vtr(L2_VTR_OP.L2_DISABLED)
44
Klement Sekeraf62ae122016-10-11 11:47:09 +020045 @abstractmethod
46 def create_arp_req(self):
47 pass
48
49 @abstractmethod
50 def create_ndp_req(self):
51 pass
52
Klement Sekerabeaded52018-06-24 10:30:37 +020053 def resolve_arp(self):
54 super(VppSubInterface, self).resolve_arp(self.parent)
55
56 def resolve_ndp(self):
57 super(VppSubInterface, self).resolve_ndp(self.parent)
58
Klement Sekeraf62ae122016-10-11 11:47:09 +020059 @abstractmethod
60 def add_dot1_layer(self, pkt):
61 pass
62
Neale Ranns177bbdc2016-11-15 09:46:51 +000063 def remove_vpp_config(self):
Klement Sekerab9ef2732018-06-24 22:49:33 +020064 self.test.vapi.delete_subif(self.sw_if_index)
Neale Ranns177bbdc2016-11-15 09:46:51 +000065
Eyal Bari284293a2017-06-06 14:18:55 +030066 def _add_tag(self, packet, vlan, tag_type):
67 payload = packet.payload
68 inner_type = packet.type
69 packet.remove_payload()
70 packet.add_payload(Dot1Q(vlan=vlan) / payload)
71 packet.payload.type = inner_type
72 packet.payload.vlan = vlan
73 packet.type = tag_type
74 return packet
75
76 def _remove_tag(self, packet, vlan=None, tag_type=None):
77 if tag_type:
78 self.test.instance().assertEqual(packet.type, tag_type)
79
80 payload = packet.payload
81 if vlan:
82 self.test.instance().assertEqual(payload.vlan, vlan)
83 inner_type = payload.type
84 payload = payload.payload
85 packet.remove_payload()
86 packet.add_payload(payload)
87 packet.type = inner_type
88 return packet
89
90 def add_dot1q_layer(self, packet, vlan):
91 return self._add_tag(packet, vlan, self.DOT1Q_TYPE)
92
93 def add_dot1ad_layer(self, packet, outer, inner):
94 p = self._add_tag(packet, inner, self.DOT1Q_TYPE)
95 return self._add_tag(p, outer, self.DOT1AD_TYPE)
96
97 def remove_dot1q_layer(self, packet, vlan=None):
98 return self._remove_tag(packet, vlan, self.DOT1Q_TYPE)
99
100 def remove_dot1ad_layer(self, packet, outer=None, inner=None):
101 p = self._remove_tag(packet, outer, self.DOT1AD_TYPE)
102 return self._remove_tag(p, inner, self.DOT1Q_TYPE)
103
104 def set_vtr(self, vtr, push1q=0, tag=None, inner=None, outer=None):
105 self._tag1 = 0
106 self._tag2 = 0
107 self._push1q = 0
108
109 if (vtr == L2_VTR_OP.L2_PUSH_1 or
110 vtr == L2_VTR_OP.L2_TRANSLATE_1_1 or
111 vtr == L2_VTR_OP.L2_TRANSLATE_2_1):
112 self._tag1 = tag
113 self._push1q = push1q
114 if (vtr == L2_VTR_OP.L2_PUSH_2 or
115 vtr == L2_VTR_OP.L2_TRANSLATE_1_2 or
116 vtr == L2_VTR_OP.L2_TRANSLATE_2_2):
117 self._tag1 = outer
118 self._tag2 = inner
119 self._push1q = push1q
120
121 self.test.vapi.sw_interface_set_l2_tag_rewrite(
122 self.sw_if_index, vtr, push=self._push1q,
123 tag1=self._tag1, tag2=self._tag2)
124 self._vtr = vtr
125
Klement Sekeraf62ae122016-10-11 11:47:09 +0200126
127class VppDot1QSubint(VppSubInterface):
128
129 @property
130 def vlan(self):
131 """VLAN tag"""
132 return self._vlan
133
134 def __init__(self, test, parent, sub_id, vlan=None):
Klement Sekerab9ef2732018-06-24 22:49:33 +0200135 super(VppDot1QSubint, self).__init__(test, parent, sub_id)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200136 if vlan is None:
137 vlan = sub_id
Klement Sekeraf62ae122016-10-11 11:47:09 +0200138 self._vlan = vlan
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100139 r = test.vapi.create_vlan_subif(parent.sw_if_index, vlan)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200140 self.set_sw_if_index(r.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200141
142 def create_arp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100143 packet = VppPGInterface.create_arp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200144 return self.add_dot1_layer(packet)
145
146 def create_ndp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100147 packet = VppPGInterface.create_ndp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200148 return self.add_dot1_layer(packet)
149
Eyal Bari284293a2017-06-06 14:18:55 +0300150 # called before sending packet
Klement Sekeraf62ae122016-10-11 11:47:09 +0200151 def add_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300152 return self.add_dot1q_layer(packet, self.vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200153
Eyal Bari284293a2017-06-06 14:18:55 +0300154 # called on received packet to "reverse" the add call
Klement Sekeraf62ae122016-10-11 11:47:09 +0200155 def remove_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300156 return self.remove_dot1q_layer(packet, self.vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200157
158
159class VppDot1ADSubint(VppSubInterface):
160
161 @property
162 def outer_vlan(self):
163 """Outer VLAN tag"""
164 return self._outer_vlan
165
166 @property
167 def inner_vlan(self):
168 """Inner VLAN tag"""
169 return self._inner_vlan
170
171 def __init__(self, test, parent, sub_id, outer_vlan, inner_vlan):
Klement Sekerab9ef2732018-06-24 22:49:33 +0200172 super(VppDot1ADSubint, self).__init__(test, parent, sub_id)
Matej Klottonc5bf07f2016-11-23 15:27:17 +0100173 r = test.vapi.create_subif(parent.sw_if_index, sub_id, outer_vlan,
174 inner_vlan, dot1ad=1, two_tags=1,
175 exact_match=1)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200176 self.set_sw_if_index(r.sw_if_index)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200177 self._outer_vlan = outer_vlan
178 self._inner_vlan = inner_vlan
Klement Sekeraf62ae122016-10-11 11:47:09 +0200179
180 def create_arp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100181 packet = VppPGInterface.create_arp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200182 return self.add_dot1_layer(packet)
183
184 def create_ndp_req(self):
Matej Klotton0178d522016-11-04 11:11:44 +0100185 packet = VppPGInterface.create_ndp_req(self)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200186 return self.add_dot1_layer(packet)
187
188 def add_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300189 return self.add_dot1ad_layer(packet, self.outer_vlan, self.inner_vlan)
Klement Sekeraf62ae122016-10-11 11:47:09 +0200190
191 def remove_dot1_layer(self, packet):
Eyal Bari284293a2017-06-06 14:18:55 +0300192 return self.remove_dot1ad_layer(packet, self.outer_vlan,
193 self.inner_vlan)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200194
195
196class VppP2PSubint(VppSubInterface):
197
198 def __init__(self, test, parent, sub_id, remote_mac):
Klement Sekerab9ef2732018-06-24 22:49:33 +0200199 super(VppP2PSubint, self).__init__(test, parent, sub_id)
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200200 r = test.vapi.create_p2pethernet_subif(parent.sw_if_index,
201 remote_mac, sub_id)
Klement Sekerab9ef2732018-06-24 22:49:33 +0200202 self.set_sw_if_index(r.sw_if_index)
Neale Ranns2ae2bc52018-03-16 03:22:39 -0700203 self.parent_sw_if_index = parent.sw_if_index
204 self.p2p_remote_mac = remote_mac
Pavel Kotucek15ac81c2017-06-20 14:00:26 +0200205
206 def add_dot1_layer(self, packet):
207 return packet
208
209 def remove_dot1_layer(self, packet):
210 return packet
211
212 def create_arp_req(self):
213 packet = VppPGInterface.create_arp_req(self)
214 return packet
215
216 def create_ndp_req(self):
217 packet = VppPGInterface.create_ndp_req(self)
218 return packet