1 from scapy.layers.l2 import Dot1Q
4 from vpp_pg_interface import VppPGInterface
5 from vpp_interface import VppInterface
6 from vpp_papi import VppEnum
21 @six.add_metaclass(abc.ABCMeta)
22 class VppSubInterface(VppPGInterface):
26 """Parent interface for this sub-interface"""
31 """Sub-interface ID"""
46 def __init__(self, test, parent, sub_id):
47 VppInterface.__init__(self, test)
49 self._parent.add_sub_if(self)
51 self.DOT1AD_TYPE = 0x88A8
52 self.DOT1Q_TYPE = 0x8100
54 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)
59 def create_arp_req(self):
63 def create_ndp_req(self):
66 def resolve_arp(self):
67 super(VppSubInterface, self).resolve_arp(self.parent)
69 def resolve_ndp(self):
70 super(VppSubInterface, self).resolve_ndp(self.parent)
73 def add_dot1_layer(self, pkt):
76 def remove_vpp_config(self):
77 self.test.vapi.delete_subif(self.sw_if_index)
79 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
89 def _remove_tag(self, packet, vlan=None, tag_type=None):
91 self.test.instance().assertEqual(packet.type, tag_type)
93 payload = packet.payload
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
103 def add_dot1q_layer(self, packet, vlan):
104 return self._add_tag(packet, vlan, self.DOT1Q_TYPE)
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)
110 def remove_dot1q_layer(self, packet, vlan=None):
111 return self._remove_tag(packet, vlan, self.DOT1Q_TYPE)
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)
117 def set_vtr(self, vtr, push1q=0, tag=None, inner=None, outer=None):
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):
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):
132 self._push1q = push1q
134 self.test.vapi.l2_interface_vlan_tag_rewrite(
135 sw_if_index=self.sw_if_index, vtr_op=vtr, push_dot1q=self._push1q,
136 tag1=self._tag1, tag2=self._tag2)
140 class VppDot1QSubint(VppSubInterface):
147 def __init__(self, test, parent, sub_id, vlan=None):
148 super(VppDot1QSubint, self).__init__(test, parent, sub_id)
152 r = test.vapi.create_vlan_subif(parent.sw_if_index, vlan)
153 self.set_sw_if_index(r.sw_if_index)
155 def create_arp_req(self):
156 packet = VppPGInterface.create_arp_req(self)
157 return self.add_dot1_layer(packet)
159 def create_ndp_req(self):
160 packet = VppPGInterface.create_ndp_req(self)
161 return self.add_dot1_layer(packet)
163 # called before sending packet
164 def add_dot1_layer(self, packet):
165 return self.add_dot1q_layer(packet, self.vlan)
167 # called on received packet to "reverse" the add call
168 def remove_dot1_layer(self, packet):
169 return self.remove_dot1q_layer(packet, self.vlan)
172 class VppDot1ADSubint(VppSubInterface):
175 def outer_vlan(self):
177 return self._outer_vlan
180 def inner_vlan(self):
182 return self._inner_vlan
184 def __init__(self, test, parent, sub_id, outer_vlan, inner_vlan):
185 super(VppDot1ADSubint, self).__init__(test, parent, sub_id)
186 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)
189 r = test.vapi.create_subif(sw_if_index=parent.sw_if_index,
190 sub_id=sub_id, outer_vlan_id=outer_vlan,
191 inner_vlan_id=inner_vlan,
193 self.set_sw_if_index(r.sw_if_index)
194 self._outer_vlan = outer_vlan
195 self._inner_vlan = inner_vlan
197 def create_arp_req(self):
198 packet = VppPGInterface.create_arp_req(self)
199 return self.add_dot1_layer(packet)
201 def create_ndp_req(self):
202 packet = VppPGInterface.create_ndp_req(self)
203 return self.add_dot1_layer(packet)
205 def add_dot1_layer(self, packet):
206 return self.add_dot1ad_layer(packet, self.outer_vlan, self.inner_vlan)
208 def remove_dot1_layer(self, packet):
209 return self.remove_dot1ad_layer(packet, self.outer_vlan,
213 class VppP2PSubint(VppSubInterface):
215 def __init__(self, test, parent, sub_id, remote_mac):
216 super(VppP2PSubint, self).__init__(test, parent, sub_id)
217 r = test.vapi.p2p_ethernet_add(parent.sw_if_index,
219 self.set_sw_if_index(r.sw_if_index)
220 self.parent_sw_if_index = parent.sw_if_index
221 self.p2p_remote_mac = remote_mac
223 def add_dot1_layer(self, packet):
226 def remove_dot1_layer(self, packet):
229 def create_arp_req(self):
230 packet = VppPGInterface.create_arp_req(self)
233 def create_ndp_req(self):
234 packet = VppPGInterface.create_ndp_req(self)