60eabc22019cb6dc9075d58187463744708a410f
[vpp.git] / src / plugins / ioam / lib-vxlan-gpe / ioam_transit.c
1  /*
2   * Copyright (c) 2015 Cisco and/or its affiliates.
3   * Licensed under the Apache License, Version 2.0 (the "License");
4   * you may not use this file except in compliance with the License.
5   * You may obtain a copy of the License at:
6   *
7   *     http://www.apache.org/licenses/LICENSE-2.0
8   *
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15 #include <vppinfra/error.h>
16 #include <vppinfra/hash.h>
17 #include <vnet/vnet.h>
18 #include <vnet/ip/ip.h>
19 #include <vnet/udp/udp.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/vxlan-gpe/vxlan_gpe.h>
22 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam_packet.h>
23 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
24 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam_util.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/fib/fib_entry.h>
27
28 /* Statistics (not really errors) */
29 #define foreach_vxlan_gpe_transit_ioam_error    \
30 _(ENCAPSULATED, "good packets encapsulated")
31
32 static char *vxlan_gpe_transit_ioam_error_strings[] = {
33 #define _(sym,string) string,
34   foreach_vxlan_gpe_transit_ioam_error
35 #undef _
36 };
37
38 typedef enum
39 {
40 #define _(sym,str) VXLAN_GPE_TRANSIT_IOAM_ERROR_##sym,
41   foreach_vxlan_gpe_transit_ioam_error
42 #undef _
43     VXLAN_GPE_TRANSIT_IOAM_N_ERROR,
44 } vxlan_gpe_transit_ioam_error_t;
45
46 typedef enum
47 {
48   VXLAN_GPE_TRANSIT_IOAM_NEXT_OUTPUT,
49   VXLAN_GPE_TRANSIT_IOAM_NEXT_DROP,
50   VXLAN_GPE_TRANSIT_IOAM_N_NEXT
51 } vxlan_gpe_transit_ioam_next_t;
52
53
54 /* *INDENT-OFF* */
55 VNET_FEATURE_INIT (vxlan_gpe_transit_ioam, static) =
56 {
57   .arc_name = "ip4-output",
58   .node_name = "vxlan-gpe-transit-ioam",
59   .runs_before = VNET_FEATURES ("interface-output"),
60 };
61 /* *INDENT-ON* */
62
63 static uword
64 vxlan_gpe_transit_ioam (vlib_main_t * vm,
65                         vlib_node_runtime_t * node, vlib_frame_t * from_frame)
66 {
67   u32 n_left_from, next_index, *from, *to_next;
68
69   from = vlib_frame_vector_args (from_frame);
70   n_left_from = from_frame->n_vectors;
71
72   next_index = node->cached_next_index;
73
74   while (n_left_from > 0)
75     {
76       u32 n_left_to_next;
77
78       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
79
80
81       while (n_left_from > 0 && n_left_to_next > 0)
82         {
83           u32 bi0;
84           vlib_buffer_t *b0;
85           u32 next0 = VXLAN_GPE_TRANSIT_IOAM_NEXT_OUTPUT;
86
87           bi0 = from[0];
88           to_next[0] = bi0;
89           from += 1;
90           to_next += 1;
91           n_left_from -= 1;
92           n_left_to_next -= 1;
93           ip4_header_t *ip0;
94           u32 iph_offset = 0;
95
96           b0 = vlib_get_buffer (vm, bi0);
97           iph_offset = vnet_buffer (b0)->ip.save_rewrite_length;
98           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0)
99                                   + iph_offset);
100
101           /* just forward non ipv4 packets */
102           if (PREDICT_FALSE
103               ((ip0->ip_version_and_header_length & 0xF0) == 0x40))
104             {
105               /* ipv4 packets */
106               udp_header_t *udp_hdr0 = (udp_header_t *) (ip0 + 1);
107               if (PREDICT_FALSE
108                   ((ip0->protocol == IP_PROTOCOL_UDP) &&
109                    (clib_net_to_host_u16 (udp_hdr0->dst_port) ==
110                     UDP_DST_PORT_VXLAN_GPE)))
111                 {
112
113                   /* Check the iOAM header */
114                   vxlan_gpe_header_t *gpe_hdr0 =
115                     (vxlan_gpe_header_t *) (udp_hdr0 + 1);
116
117                   if (PREDICT_FALSE
118                       (gpe_hdr0->protocol == VXLAN_GPE_PROTOCOL_IOAM))
119                     {
120                       uword *t = NULL;
121                       vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
122                       fib_prefix_t key4;
123                       memset (&key4, 0, sizeof (key4));
124                       key4.fp_proto = FIB_PROTOCOL_IP4;
125                       key4.fp_addr.ip4.as_u32 = ip0->dst_address.as_u32;
126                       t = hash_get_mem (hm->dst_by_ip4, &key4);
127                       if (t)
128                         {
129
130
131                           vlib_buffer_advance (b0,
132                                                (word) (sizeof
133                                                        (ethernet_header_t)));
134                           vxlan_gpe_encap_decap_ioam_v4_one_inline (vm, node,
135                                                                     b0,
136                                                                     &next0,
137                                                                     VXLAN_GPE_TRANSIT_IOAM_NEXT_DROP,
138                                                                     1
139                                                                     /* use_adj */
140                             );
141                           vlib_buffer_advance (b0,
142                                                -(word) (sizeof
143                                                         (ethernet_header_t)));
144                         }
145                     }
146                 }
147             }
148
149           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
150                                            n_left_to_next, bi0, next0);
151         }
152
153       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
154     }
155
156   return from_frame->n_vectors;
157 }
158
159 /* *INDENT-OFF* */
160 VLIB_REGISTER_NODE (vxlan_gpe_transit_ioam_node) = {
161   .function = vxlan_gpe_transit_ioam,
162   .name = "vxlan-gpe-transit-ioam",
163   .vector_size = sizeof (u32),
164   .format_trace = format_vxlan_gpe_ioam_v4_trace,
165   .type = VLIB_NODE_TYPE_INTERNAL,
166
167   .n_errors = ARRAY_LEN(vxlan_gpe_transit_ioam_error_strings),
168   .error_strings = vxlan_gpe_transit_ioam_error_strings,
169
170   .n_next_nodes = VXLAN_GPE_TRANSIT_IOAM_N_NEXT,
171
172   .next_nodes = {
173         [VXLAN_GPE_TRANSIT_IOAM_NEXT_OUTPUT] = "interface-output",
174         [VXLAN_GPE_TRANSIT_IOAM_NEXT_DROP] = "error-drop",
175   },
176
177 };
178 /* *INDENT-ON* */
179
180
181 /*
182  * fd.io coding-style-patch-verification: ON
183  *
184  * Local Variables:
185  * eval: (c-set-style "gnu")
186  * End:
187  */