summaryrefslogtreecommitdiffstats
path: root/test/vpp_sub_interface.py
blob: 38d24041e81f06d72d04a8eb2f6684b7c3c3fb26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
from scapy.layers.l2 import Dot1Q
from abc import abstractmethod, ABCMeta
from vpp_interface import VppInterface
from vpp_pg_interface import VppPGInterface
from vpp_papi_provider import L2_VTR_OP


class VppSubInterface(VppPGInterface):
    __metaclass__ = ABCMeta

    @property
    def parent(self):
        """Parent interface for this sub-interface"""
        return self._parent

    @property
    def sub_id(self):
        """Sub-interface ID"""
        return self._sub_id

    @property
    def tag1(self):
        return self._tag1

    @property
    def tag2(self):
        return self._tag2

    @property
    def vtr(self):
        return self._vtr

    def __init__(self, test, parent, sub_id):
        VppInterface.__init__(self, test)
        self._parent = parent
        self._parent.add_sub_if(self)
        self._sub_id = sub_id
        self.set_vtr(L2_VTR_OP.L2_DISABLED)
        self.DOT1AD_TYPE = 0x88A8
        self.DOT1Q_TYPE = 0x8100

    @abstractmethod
    def create_arp_req(self):
        pass

    @abstractmethod
    def create_ndp_req(self):
        pass

    def resolve_arp(self):
        super(VppSubInterface, self).resolve_arp(self.parent)

    def resolve_ndp(self):
        super(VppSubInterface, self).resolve_ndp(self.parent)

    @abstractmethod
    def add_dot1_layer(self, pkt):
        pass

    def remove_vpp_config(self):
        self.test.vapi.delete_subif(self._sw_if_index)

    def _add_tag(self, packet, vlan, tag_type):
        payload = packet.payload
        inner_type = packet.type
        packet.remove_payload()
        packet.add_payload(Dot1Q(vlan=vlan) / payload)
        packet.payload.type = inner_type
        packet.payload.vlan = vlan
        packet.type = tag_type
        return packet

    def _remove_tag(self, packet, vlan=None, tag_type=None):
        if tag_type:
            self.test.instance().assertEqual(packet.type, tag_type)

        payload = packet.payload
        if vlan:
            self.test.instance().assertEqual(payload.vlan, vlan)
        inner_type = payload.type
        payload = payload.payload
        packet.remove_payload()
        packet.add_payload(payload)
        packet.type = inner_type
        return packet

    def add_dot1q_layer(self, packet, vlan):
        return self._add_tag(packet, vlan, self.DOT1Q_TYPE)

    def add_dot1ad_layer(self, packet, outer, inner):
        p = self._add_tag(packet, inner, self.DOT1Q_TYPE)
        return self._add_tag(p, outer, self.DOT1AD_TYPE)

    def remove_dot1q_layer(self, packet, vlan=None):
        return self._remove_tag(packet, vlan, self.DOT1Q_TYPE)

    def remove_dot1ad_layer(self, packet, outer=None, inner=None):
        p = self._remove_tag(packet, outer, self.DOT1AD_TYPE)
        return self._remove_tag(p, inner, self.DOT1Q_TYPE)

    def set_vtr(self, vtr, push1q=0, tag=None, inner=None, outer=None):
        self._tag1 = 0
        self._tag2 = 0
        self._push1q = 0

        if (vtr == L2_VTR_OP.L2_PUSH_1 or
            vtr == L2_VTR_OP.L2_TRANSLATE_1_1 or
                vtr == L2_VTR_OP.L2_TRANSLATE_2_1):
            self._tag1 = tag
            self._push1q = push1q
        if (vtr == L2_VTR_OP.L2_PUSH_2 or
            vtr == L2_VTR_OP.L2_TRANSLATE_1_2 or
                vtr == L2_VTR_OP.L2_TRANSLATE_2_2):
            self._tag1 = outer
            self._tag2 = inner
            self._push1q = push1q

        self.test.vapi.sw_interface_set_l2_tag_rewrite(
            self.sw_if_index, vtr, push=self._push1q,
            tag1=self._tag1, tag2=self._tag2)
        self._vtr = vtr


class VppDot1QSubint(VppSubInterface):

    @property
    def vlan(self):
        """VLAN tag"""
        return self._vlan

    def __init__(self, test, parent, sub_id, vlan=None):
        if vlan is None:
            vlan = sub_id
        self._vlan = vlan
        r = test.vapi.create_vlan_subif(parent.sw_if_index, vlan)
        self._sw_if_index = r.sw_if_index
        super(VppDot1QSubint, self).__init__(test, parent, sub_id)

    def create_arp_req(self):
        packet = VppPGInterface.create_arp_req(self)
        return self.add_dot1_layer(packet)

    def create_ndp_req(self):
        packet = VppPGInterface.create_ndp_req(self)
        return self.add_dot1_layer(packet)

    # called before sending packet
    def add_dot1_layer(self, packet):
        return self.add_dot1q_layer(packet, self.vlan)

    # called on received packet to "reverse" the add call
    def remove_dot1_layer(self, packet):
        return self.remove_dot1q_layer(packet, self.vlan)


class VppDot1ADSubint(VppSubInterface):

    @property
    def outer_vlan(self):
        """Outer VLAN tag"""
        return self._outer_vlan

    @property
    def inner_vlan(self):
        """Inner VLAN tag"""
        return self._inner_vlan

    def __init__(self, test, parent, sub_id, outer_vlan, inner_vlan):
        r = test.vapi.create_subif(parent.sw_if_index, sub_id, outer_vlan,
                                   inner_vlan, dot1ad=1, two_tags=1,
                                   exact_match=1)
        self._sw_if_index = r.sw_if_index
        self._outer_vlan = outer_vlan
        self._inner_vlan = inner_vlan
        super(VppDot1ADSubint, self).__init__(test, parent, sub_id)

    def create_arp_req(self):
        packet = VppPGInterface.create_arp_req(self)
        return self.add_dot1_layer(packet)

    def create_ndp_req(self):
        packet = VppPGInterface.create_ndp_req(self)
        return self.add_dot1_layer(packet)

    def add_dot1_layer(self, packet):
        return self.add_dot1ad_layer(packet, self.outer_vlan, self.inner_vlan)

    def remove_dot1_layer(self, packet):
        return self.remove_dot1ad_layer(packet, self.outer_vlan,
                                        self.inner_vlan)


class VppP2PSubint(VppSubInterface):

    def __init__(self, test, parent, sub_id, remote_mac):
        r = test.vapi.create_p2pethernet_subif(parent.sw_if_index,
                                               remote_mac, sub_id)
        self._sw_if_index = r.sw_if_index
        self.parent_sw_if_index = parent.sw_if_index
        self.p2p_remote_mac = remote_mac
        super(VppP2PSubint, self).__init__(test, parent, sub_id)

    def add_dot1_layer(self, packet):
        return packet

    def remove_dot1_layer(self, packet):
        return packet

    def create_arp_req(self):
        packet = VppPGInterface.create_arp_req(self)
        return packet

    def create_ndp_req(self):
        packet = VppPGInterface.create_ndp_req(self)
        return packet
word) _vec_len (t1->rewrite)); if (is_ip4) { u8 ip4_geneve_base_header_len = sizeof (ip4_header_t) + sizeof (udp_header_t) + GENEVE_BASE_HEADER_LENGTH; u8 ip4_geneve_header_total_len0 = ip4_geneve_base_header_len; u8 ip4_geneve_header_total_len1 = ip4_geneve_base_header_len; #if SUPPORT_OPTIONS_HEADER==1 ip4_geneve_header_total_len0 += t0->options_len; ip4_geneve_header_total_len1 += t1->options_len; #endif ASSERT (vec_len (t0->rewrite) == ip4_geneve_header_total_len0); ASSERT (vec_len (t1->rewrite) == ip4_geneve_header_total_len1); ip4_0 = vlib_buffer_get_current (b[0]); ip4_1 = vlib_buffer_get_current (b[1]); /* Copy the fixed header */ copy_dst0 = (u64 *) ip4_0; copy_src0 = (u64 *) t0->rewrite; copy_dst1 = (u64 *) ip4_1; copy_src1 = (u64 *) t1->rewrite; /* Copy first 32 octets 8-bytes at a time */ #define _(offs) copy_dst0[offs] = copy_src0[offs]; foreach_fixed_header4_offset; #undef _ #define _(offs) copy_dst1[offs] = copy_src1[offs]; foreach_fixed_header4_offset; #undef _ /* Last 4 octets. Hopefully gcc will be our friend */ copy_dst_last0 = (u32 *) (&copy_dst0[4]); copy_src_last0 = (u32 *) (&copy_src0[4]); copy_dst_last0[0] = copy_src_last0[0]; copy_dst_last1 = (u32 *) (&copy_dst1[4]); copy_src_last1 = (u32 *) (&copy_src1[4]); copy_dst_last1[0] = copy_src_last1[0]; /* Fix the IP4 checksum and length */ sum0 = ip4_0->checksum; new_l0 = /* old_l0 always 0, see the rewrite setup */ clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, length /* changed member */ ); ip4_0->checksum = ip_csum_fold (sum0); ip4_0->length = new_l0; sum1 = ip4_1->checksum; new_l1 = /* old_l1 always 0, see the rewrite setup */ clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1])); sum1 = ip_csum_update (sum1, old_l1, new_l1, ip4_header_t, length /* changed member */ ); ip4_1->checksum = ip_csum_fold (sum1); ip4_1->length = new_l1; /* Fix UDP length and set source port */ udp0 = (udp_header_t *) (ip4_0 + 1); new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - sizeof (*ip4_0)); udp0->length = new_l0; udp0->src_port = flow_hash0; udp1 = (udp_header_t *) (ip4_1 + 1); new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1]) - sizeof (*ip4_1)); udp1->length = new_l1; udp1->src_port = flow_hash1; } else /* ipv6 */ { int bogus = 0; u8 ip6_geneve_base_header_len = sizeof (ip6_header_t) + sizeof (udp_header_t) + GENEVE_BASE_HEADER_LENGTH; u8 ip6_geneve_header_total_len0 = ip6_geneve_base_header_len; u8 ip6_geneve_header_total_len1 = ip6_geneve_base_header_len; #if SUPPORT_OPTIONS_HEADER==1 ip6_geneve_header_total_len0 += t0->options_len; ip6_geneve_header_total_len1 += t1->options_len; #endif ASSERT (vec_len (t0->rewrite) == ip6_geneve_header_total_len0); ASSERT (vec_len (t1->rewrite) == ip6_geneve_header_total_len1); ip6_0 = vlib_buffer_get_current (b[0]); ip6_1 = vlib_buffer_get_current (b[1]); /* Copy the fixed header */ copy_dst0 = (u64 *) ip6_0; copy_src0 = (u64 *) t0->rewrite; copy_dst1 = (u64 *) ip6_1; copy_src1 = (u64 *) t1->rewrite; /* Copy first 56 (ip6) octets 8-bytes at a time */ #define _(offs) copy_dst0[offs] = copy_src0[offs]; foreach_fixed_header6_offset; #undef _ #define _(offs) copy_dst1[offs] = copy_src1[offs]; foreach_fixed_header6_offset; #undef _ /* Fix IP6 payload length */ new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - sizeof (*ip6_0)); ip6_0->payload_length = new_l0; new_l1 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[1]) - sizeof (*ip6_1)); ip6_1->payload_length = new_l1; /* Fix UDP length and set source port */ udp0 = (udp_header_t *) (ip6_0 + 1); udp0->length = new_l0; udp0->src_port = flow_hash0; udp1 = (udp_header_t *) (ip6_1 + 1); udp1->length = new_l1; udp1->src_port = flow_hash1; /* IPv6 UDP checksum is mandatory */ udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[0], ip6_0, &bogus); ASSERT (bogus == 0); if (udp0->checksum == 0) udp0->checksum = 0xffff; udp1->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[1], ip6_1, &bogus); ASSERT (bogus == 0); if (udp1->checksum == 0) udp1->checksum = 0xffff; } pkts_encapsulated += 2; len0 = vlib_buffer_length_in_chain (vm, b[0]); len1 = vlib_buffer_length_in_chain (vm, b[1]); stats_n_packets += 2; stats_n_bytes += len0 + len1; /* save inner packet flow_hash for load-balance node */ vnet_buffer (b[0])->ip.flow_hash = flow_hash0; vnet_buffer (b[1])->ip.flow_hash = flow_hash1; /* Batch stats increment on the same geneve tunnel so counter is not incremented per packet. Note stats are still incremented for deleted and admin-down tunnel where packets are dropped. It is not worthwhile to check for this rare case and affect normal path performance. */ if (PREDICT_FALSE ((sw_if_index0 != stats_sw_if_index) || (sw_if_index1 != stats_sw_if_index))) { stats_n_packets -= 2; stats_n_bytes -= len0 + len1; if (sw_if_index0 == sw_if_index1) { if (stats_n_packets) vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); stats_sw_if_index = sw_if_index0; stats_n_packets = 2; stats_n_bytes = len0 + len1; } else { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, sw_if_index0, 1, len0); vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, sw_if_index1, 1, len1); } } if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) { geneve_encap_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr)); tr->tunnel_index = t0 - vxm->tunnels; tr->vni = t0->vni; } if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED)) { geneve_encap_trace_t *tr = vlib_add_trace (vm, node, b[1], sizeof (*tr)); tr->tunnel_index = t1 - vxm->tunnels; tr->vni = t1->vni; } b += 2; vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1); } while (n_left_from > 0 && n_left_to_next > 0) { u32 bi0; u32 flow_hash0; u32 len0; ip4_header_t *ip4_0; ip6_header_t *ip6_0; udp_header_t *udp0; u64 *copy_src0, *copy_dst0; u32 *copy_src_last0, *copy_dst_last0; u16 new_l0; ip_csum_t sum0; bi0 = from[0]; to_next[0] = bi0; from += 1; to_next += 1; n_left_from -= 1; n_left_to_next -= 1; flow_hash0 = vnet_l2_compute_flow_hash (b[0]); /* Get next node index and adj index from tunnel next_dpo */ if (sw_if_index0 != vnet_buffer (b[0])->sw_if_index[VLIB_TX]) { sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_TX]; hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0); t0 = &vxm->tunnels[hi0->dev_instance]; /* Note: change to always set next0 if it may be set to drop */ next0 = t0->next_dpo.dpoi_next_node; } ALWAYS_ASSERT (t0 != NULL); vnet_buffer (b[0])->ip.adj_index[VLIB_TX] = t0->next_dpo.dpoi_index; /* Apply the rewrite string. $$$$ vnet_rewrite? */ vlib_buffer_advance (b[0], -(word) _vec_len (t0->rewrite)); if (is_ip4) { u8 ip4_geneve_base_header_len = sizeof (ip4_header_t) + sizeof (udp_header_t) + GENEVE_BASE_HEADER_LENGTH; u8 ip4_geneve_header_total_len0 = ip4_geneve_base_header_len; #if SUPPORT_OPTIONS_HEADER==1 ip4_geneve_header_total_len0 += t0->options_len; #endif ASSERT (vec_len (t0->rewrite) == ip4_geneve_header_total_len0); ip4_0 = vlib_buffer_get_current (b[0]); /* Copy the fixed header */ copy_dst0 = (u64 *) ip4_0; copy_src0 = (u64 *) t0->rewrite; /* Copy first 32 octets 8-bytes at a time */ #define _(offs) copy_dst0[offs] = copy_src0[offs]; foreach_fixed_header4_offset; #undef _ /* Last 4 octets. Hopefully gcc will be our friend */ copy_dst_last0 = (u32 *) (&copy_dst0[4]); copy_src_last0 = (u32 *) (&copy_src0[4]); copy_dst_last0[0] = copy_src_last0[0]; /* Fix the IP4 checksum and length */ sum0 = ip4_0->checksum; new_l0 = /* old_l0 always 0, see the rewrite setup */ clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0])); sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t, length /* changed member */ ); ip4_0->checksum = ip_csum_fold (sum0); ip4_0->length = new_l0; /* Fix UDP length and set source port */ udp0 = (udp_header_t *) (ip4_0 + 1); new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - sizeof (*ip4_0)); udp0->length = new_l0; udp0->src_port = flow_hash0; } else /* ip6 path */ { int bogus = 0; u8 ip6_geneve_base_header_len = sizeof (ip6_header_t) + sizeof (udp_header_t) + GENEVE_BASE_HEADER_LENGTH; u8 ip6_geneve_header_total_len0 = ip6_geneve_base_header_len; #if SUPPORT_OPTIONS_HEADER==1 ip6_geneve_header_total_len0 += t0->options_len; #endif ASSERT (vec_len (t0->rewrite) == ip6_geneve_header_total_len0); ip6_0 = vlib_buffer_get_current (b[0]); /* Copy the fixed header */ copy_dst0 = (u64 *) ip6_0; copy_src0 = (u64 *) t0->rewrite; /* Copy first 56 (ip6) octets 8-bytes at a time */ #define _(offs) copy_dst0[offs] = copy_src0[offs]; foreach_fixed_header6_offset; #undef _ /* Fix IP6 payload length */ new_l0 = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b[0]) - sizeof (*ip6_0)); ip6_0->payload_length = new_l0; /* Fix UDP length and set source port */ udp0 = (udp_header_t *) (ip6_0 + 1); udp0->length = new_l0; udp0->src_port = flow_hash0; /* IPv6 UDP checksum is mandatory */ udp0->checksum = ip6_tcp_udp_icmp_compute_checksum (vm, b[0], ip6_0, &bogus); ASSERT (bogus == 0); if (udp0->checksum == 0) udp0->checksum = 0xffff; } pkts_encapsulated++; len0 = vlib_buffer_length_in_chain (vm, b[0]); stats_n_packets += 1; stats_n_bytes += len0; /* save inner packet flow_hash for load-balance node */ vnet_buffer (b[0])->ip.flow_hash = flow_hash0; /* Batch stats increment on the same geneve tunnel so counter is not incremented per packet. Note stats are still incremented for deleted and admin-down tunnel where packets are dropped. It is not worthwhile to check for this rare case and affect normal path performance. */ if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) { stats_n_packets -= 1; stats_n_bytes -= len0; if (stats_n_packets) vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); stats_n_packets = 1; stats_n_bytes = len0; stats_sw_if_index = sw_if_index0; } if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED)) { geneve_encap_trace_t *tr = vlib_add_trace (vm, node, b[0], sizeof (*tr)); tr->tunnel_index = t0 - vxm->tunnels; tr->vni = t0->vni; } b += 1; vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left_to_next, bi0, next0); } vlib_put_next_frame (vm, node, next_index, n_left_to_next); } /* Do we still need this now that tunnel tx stats is kept? */ vlib_node_increment_counter (vm, node->node_index, GENEVE_ENCAP_ERROR_ENCAPSULATED, pkts_encapsulated); /* Increment any remaining batch stats */ if (stats_n_packets) { vlib_increment_combined_counter (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_TX, thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); node->runtime_data[0] = stats_sw_if_index; } return from_frame->n_vectors; } VLIB_NODE_FN (geneve4_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * from_frame) { return geneve_encap_inline (vm, node, from_frame, /* is_ip4 */ 1); } VLIB_NODE_FN (geneve6_encap_node) (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * from_frame) { return geneve_encap_inline (vm, node, from_frame, /* is_ip4 */ 0); } /* *INDENT-OFF* */ VLIB_REGISTER_NODE (geneve4_encap_node) = { .name = "geneve4-encap", .vector_size = sizeof (u32), .format_trace = format_geneve_encap_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = ARRAY_LEN (geneve_encap_error_strings), .error_strings = geneve_encap_error_strings, .n_next_nodes = GENEVE_ENCAP_N_NEXT, .next_nodes = { [GENEVE_ENCAP_NEXT_DROP] = "error-drop", }, }; VLIB_REGISTER_NODE (geneve6_encap_node) = { .name = "geneve6-encap", .vector_size = sizeof (u32), .format_trace = format_geneve_encap_trace, .type = VLIB_NODE_TYPE_INTERNAL, .n_errors = ARRAY_LEN (geneve_encap_error_strings), .error_strings = geneve_encap_error_strings, .n_next_nodes = GENEVE_ENCAP_N_NEXT, .next_nodes = { [GENEVE_ENCAP_NEXT_DROP] = "error-drop", }, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */