lisp: Move to plugin
[vpp.git] / src / plugins / nsh / nsh-md2-ioam / md2_ioam_transit.c
1  /*
2   * Copyright (c) 2017 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 <lisp/lisp-gpe/lisp_gpe_packet.h>
22 #include <nsh/nsh.h>
23 #include <nsh/nsh_packet.h>
24 #include <nsh/nsh-md2-ioam/nsh_md2_ioam.h>
25 #include <nsh/nsh-md2-ioam/nsh_md2_ioam_util.h>
26 #include <vnet/fib/ip6_fib.h>
27 #include <vnet/fib/ip4_fib.h>
28 #include <vnet/fib/fib_entry.h>
29
30 /* Statistics (not really errors) */
31 #define foreach_nsh_md2_ioam_encap_transit_error    \
32 _(ENCAPSULATED, "good packets encapsulated")
33
34 static char *nsh_md2_ioam_encap_transit_error_strings[] = {
35 #define _(sym,string) string,
36   foreach_nsh_md2_ioam_encap_transit_error
37 #undef _
38 };
39
40 typedef enum
41 {
42 #define _(sym,str) NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_ERROR_##sym,
43   foreach_nsh_md2_ioam_encap_transit_error
44 #undef _
45     NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_ERROR,
46 } nsh_md2_ioam_encap_transit_error_t;
47
48 typedef enum
49 {
50   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT,
51   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP,
52   NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_NEXT
53 } nsh_md2_ioam_encap_transit_next_t;
54
55
56 /* *INDENT-OFF* */
57 VNET_FEATURE_INIT (nsh_md2_ioam_encap_transit, static) =
58 {
59   .arc_name = "ip4-output",
60   .node_name = "nsh-md2-ioam-encap-transit",
61 };
62 /* *INDENT-ON* */
63
64
65 static uword
66 nsh_md2_ioam_encap_transit (vlib_main_t * vm,
67                             vlib_node_runtime_t * node,
68                             vlib_frame_t * from_frame)
69 {
70   u32 n_left_from, next_index, *from, *to_next;
71
72   from = vlib_frame_vector_args (from_frame);
73   n_left_from = from_frame->n_vectors;
74
75   next_index = node->cached_next_index;
76
77   while (n_left_from > 0)
78     {
79       u32 n_left_to_next;
80
81       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
82
83
84       while (n_left_from > 0 && n_left_to_next > 0)
85         {
86           u32 bi0;
87           vlib_buffer_t *b0;
88           u32 next0 = NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT;
89
90           bi0 = from[0];
91           to_next[0] = bi0;
92           from += 1;
93           to_next += 1;
94           n_left_from -= 1;
95           n_left_to_next -= 1;
96           ip4_header_t *ip0;
97           u32 iph_offset = 0;
98
99           b0 = vlib_get_buffer (vm, bi0);
100           iph_offset = vnet_buffer (b0)->ip.save_rewrite_length;
101           ip0 = (ip4_header_t *) ((u8 *) vlib_buffer_get_current (b0)
102                                   + iph_offset);
103
104           /* just forward non ipv4 packets */
105           if (PREDICT_FALSE
106               ((ip0->ip_version_and_header_length & 0xF0) == 0x40))
107             {
108               /* ipv4 packets */
109               udp_header_t *udp_hdr0 = (udp_header_t *) (ip0 + 1);
110               if (PREDICT_FALSE
111                   ((ip0->protocol == IP_PROTOCOL_UDP) &&
112                    (clib_net_to_host_u16 (udp_hdr0->dst_port) ==
113                     UDP_DST_PORT_lisp_gpe)))
114                 {
115
116                   /* Check the iOAM header */
117                   lisp_gpe_header_t *lisp_gpe_hdr0 =
118                     (lisp_gpe_header_t *) (udp_hdr0 + 1);
119                   nsh_base_header_t *nsh_hdr =
120                     (nsh_base_header_t *) (lisp_gpe_hdr0 + 1);
121
122                   if (PREDICT_FALSE
123                       (lisp_gpe_hdr0->next_protocol ==
124                        LISP_GPE_NEXT_PROTO_NSH) && (nsh_hdr->md_type == 2))
125                     {
126                       uword *t = NULL;
127                       nsh_md2_ioam_main_t *hm = &nsh_md2_ioam_main;
128                       fib_prefix_t key4;
129                       clib_memset (&key4, 0, sizeof (key4));
130                       key4.fp_proto = FIB_PROTOCOL_IP4;
131                       key4.fp_addr.ip4.as_u32 = ip0->dst_address.as_u32;
132                       t = hash_get_mem (hm->dst_by_ip4, &key4);
133                       if (t)
134                         {
135                           vlib_buffer_advance (b0,
136                                                (word) (sizeof
137                                                        (ethernet_header_t)));
138                           nsh_md2_ioam_encap_decap_ioam_v4_one_inline (vm,
139                                                                        node,
140                                                                        b0,
141                                                                        &next0,
142                                                                        NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP,
143                                                                        1
144                                                                        /* use_adj */
145                             );
146                           vlib_buffer_advance (b0,
147                                                -(word) (sizeof
148                                                         (ethernet_header_t)));
149                         }
150                     }
151                 }
152             }
153
154           vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
155                                            n_left_to_next, bi0, next0);
156         }
157
158       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
159     }
160
161   return from_frame->n_vectors;
162 }
163
164 extern u8 *format_nsh_node_map_trace (u8 * s, va_list * args);
165 /* *INDENT-OFF* */
166 VLIB_REGISTER_NODE (nsh_md2_ioam_encap_transit_node) = {
167   .function = nsh_md2_ioam_encap_transit,
168   .name = "nsh-md2-ioam-encap-transit",
169   .vector_size = sizeof (u32),
170   .format_trace = format_nsh_node_map_trace,
171   .type = VLIB_NODE_TYPE_INTERNAL,
172
173   .n_errors = ARRAY_LEN(nsh_md2_ioam_encap_transit_error_strings),
174   .error_strings = nsh_md2_ioam_encap_transit_error_strings,
175
176   .n_next_nodes = NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_N_NEXT,
177
178   .next_nodes = {
179         [NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_OUTPUT] = "interface-output",
180         [NSH_MD2_IOAM_ENCAP_TRANSIT_IOAM_NEXT_DROP] = "error-drop",
181   },
182
183 };
184 /* *INDENT-ON* */
185
186
187 /*
188  * fd.io coding-style-patch-verification: ON
189  *
190  * Local Variables:
191  * eval: (c-set-style "gnu")
192  * End:
193  */