Allow the provider of a midchain adjacency to pass context data that is returned...
[vpp.git] / src / vnet / adj / adj_nsh.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
16 #include <vnet/vnet.h>
17 #include <vnet/adj/adj_nsh.h>
18 #include <vnet/ip/ip.h>
19
20 nsh_main_dummy_t nsh_main_dummy;
21
22 /**
23  * @brief Trace data for a NSH Midchain
24  */
25 typedef struct adj_nsh_trace_t_ {
26     /** Adjacency index taken. */
27     u32 adj_index;
28 } adj_nsh_trace_t;
29
30 static u8 *
31 format_adj_nsh_trace (u8 * s, va_list * args)
32 {
33     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
34     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
35     adj_nsh_trace_t * t = va_arg (*args, adj_nsh_trace_t *);
36
37     s = format (s, "adj-idx %d : %U",
38                 t->adj_index,
39                 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE);
40     return s;
41 }
42
43 typedef enum adj_nsh_rewrite_next_t_
44 {
45     ADJ_NSH_REWRITE_NEXT_DROP,
46 } adj_gpe_rewrite_next_t;
47
48 always_inline uword
49 adj_nsh_rewrite_inline (vlib_main_t * vm,
50                        vlib_node_runtime_t * node,
51                        vlib_frame_t * frame,
52                        int is_midchain)
53 {
54     u32 * from = vlib_frame_vector_args (frame);
55     u32 n_left_from, n_left_to_next, * to_next, next_index;
56     u32 thread_index = vlib_get_thread_index();
57
58     n_left_from = frame->n_vectors;
59     next_index = node->cached_next_index;
60
61     while (n_left_from > 0)
62     {
63         vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
64
65         while (n_left_from > 0 && n_left_to_next > 0)
66         {
67             ip_adjacency_t * adj0;
68             vlib_buffer_t * p0;
69             char *h0;
70             u32 pi0, rw_len0, adj_index0, next0 = 0;
71             u32 tx_sw_if_index0;
72
73             pi0 = to_next[0] = from[0];
74             from += 1;
75             n_left_from -= 1;
76             to_next += 1;
77             n_left_to_next -= 1;
78
79             p0 = vlib_get_buffer (vm, pi0);
80             h0 = vlib_buffer_get_current (p0);
81
82             adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
83
84             /* We should never rewrite a pkt using the MISS adjacency */
85             ASSERT(adj_index0);
86
87             adj0 = adj_get (adj_index0);
88
89             /* Guess we are only writing on simple IP4 header. */
90             vnet_rewrite_one_header(adj0[0], h0, sizeof(ip4_header_t));
91
92             /* Update packet buffer attributes/set output interface. */
93             rw_len0 = adj0[0].rewrite_header.data_bytes;
94             vnet_buffer(p0)->ip.save_rewrite_length = rw_len0;
95
96             vlib_increment_combined_counter(&adjacency_counters,
97                                             thread_index,
98                                             adj_index0,
99                                             /* packet increment */ 0,
100                                             /* byte increment */ rw_len0);
101
102             /* Check MTU of outgoing interface. */
103             if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0)  <=
104                               adj0[0].rewrite_header.max_l3_packet_bytes)))
105             {
106                 /* Don't adjust the buffer for ttl issue; icmp-error node wants
107                  * to see the IP headerr */
108                 p0->current_data -= rw_len0;
109                 p0->current_length += rw_len0;
110                 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
111
112                 if (is_midchain)
113                 {
114                     adj0->sub_type.midchain.fixup_func(
115                         vm, adj0, p0,
116                         adj0->sub_type.midchain.fixup_data);
117                 }
118
119                 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
120
121                 /*
122                  * Follow the feature ARC. this will result eventually in
123                  * the midchain-tx node
124                  */
125                 vnet_feature_arc_start (nsh_main_dummy.output_feature_arc_index,
126                                         tx_sw_if_index0, &next0, p0);
127             }
128             else
129             {
130                 /* can't fragment NSH */
131                 next0 = ADJ_NSH_REWRITE_NEXT_DROP;
132             }
133
134             if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
135             {
136                 adj_nsh_trace_t *tr = vlib_add_trace (vm, node,
137                                                      p0, sizeof (*tr));
138                 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
139             }
140
141             vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
142                                              to_next, n_left_to_next,
143                                              pi0, next0);
144         }
145
146         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
147     }
148
149     return frame->n_vectors;
150 }
151
152 static uword
153 adj_nsh_rewrite (vlib_main_t * vm,
154                 vlib_node_runtime_t * node,
155                 vlib_frame_t * frame)
156 {
157     return adj_nsh_rewrite_inline (vm, node, frame, 0);
158 }
159
160 static uword
161 adj_nsh_midchain (vlib_main_t * vm,
162                  vlib_node_runtime_t * node,
163                  vlib_frame_t * frame)
164 {
165     return adj_nsh_rewrite_inline (vm, node, frame, 1);
166 }
167
168 VLIB_REGISTER_NODE (adj_nsh_rewrite_node) = {
169     .function = adj_nsh_rewrite,
170     .name = "adj-nsh-rewrite",
171     .vector_size = sizeof (u32),
172
173     .format_trace = format_adj_nsh_trace,
174
175     .n_next_nodes = 1,
176     .next_nodes = {
177         [ADJ_NSH_REWRITE_NEXT_DROP] = "error-drop",
178     },
179 };
180
181 VLIB_NODE_FUNCTION_MULTIARCH (adj_nsh_rewrite_node, adj_nsh_rewrite)
182
183 VLIB_REGISTER_NODE (adj_nsh_midchain_node) = {
184     .function = adj_nsh_midchain,
185     .name = "adj-nsh-midchain",
186     .vector_size = sizeof (u32),
187
188     .format_trace = format_adj_nsh_trace,
189
190     .n_next_nodes = 1,
191     .next_nodes = {
192         [ADJ_NSH_REWRITE_NEXT_DROP] = "error-drop",
193     },
194 };
195
196 VLIB_NODE_FUNCTION_MULTIARCH (adj_nsh_midchain_node, adj_nsh_midchain)
197
198 /* Built-in ip4 tx feature path definition */
199 /* *INDENT-OFF* */
200 VNET_FEATURE_ARC_INIT (nsh_output, static) =
201 {
202   .arc_name  = "nsh-output",
203   .start_nodes = VNET_FEATURES ("adj-nsh-midchain"),
204   .arc_index_ptr = &nsh_main_dummy.output_feature_arc_index,
205 };
206
207 VNET_FEATURE_INIT (nsh_tx_drop, static) =
208 {
209   .arc_name = "nsh-output",
210   .node_name = "error-drop",
211   .runs_before = 0,     /* not before any other features */
212 };
213 /* *INDENT-ON* */