L2 over LISP and GRE (VPP-457)
[vpp.git] / vnet / vnet / adj / adj_l2.c
1 /*
2  * Copyright (c) 2016 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_l2.h>
18 #include <vnet/ethernet/ethernet.h>
19 #include <vnet/ip/ip.h>
20
21 /**
22  * @brief Trace data for a L2 Midchain
23  */
24 typedef struct adj_l2_trace_t_ {
25     /** Adjacency index taken. */
26     u32 adj_index;
27 } adj_l2_trace_t;
28
29 static u8 *
30 format_adj_l2_trace (u8 * s, va_list * args)
31 {
32     CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33     CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34     adj_l2_trace_t * t = va_arg (*args, adj_l2_trace_t *);
35     vnet_main_t * vnm = vnet_get_main();
36
37     s = format (s, "adj-idx %d : %U",
38                 t->adj_index,
39                 format_ip_adjacency, vnm, t->adj_index, FORMAT_IP_ADJACENCY_NONE);
40     return s;
41 }
42
43 typedef enum adj_l2_rewrite_next_t_
44 {
45     ADJ_L2_REWRITE_NEXT_DROP,
46 } adj_l2_rewrite_next_t;
47
48 always_inline uword
49 adj_l2_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 cpu_index = os_get_cpu_number();
57     ip_config_main_t * cm = &ethernet_main.feature_config_mains[VNET_IP_TX_FEAT];
58
59     n_left_from = frame->n_vectors;
60     next_index = node->cached_next_index;
61
62     while (n_left_from > 0)
63     {
64         vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
65
66         while (n_left_from > 0 && n_left_to_next > 0)
67         {
68             ip_adjacency_t * adj0;
69             vlib_buffer_t * p0;
70             char *h0;
71             u32 pi0, rw_len0, adj_index0, next0;
72             u32 tx_sw_if_index0;
73
74             pi0 = to_next[0] = from[0];
75             from += 1;
76             n_left_from -= 1;
77             to_next += 1;
78             n_left_to_next -= 1;
79
80             p0 = vlib_get_buffer (vm, pi0);
81             h0 = vlib_buffer_get_current (p0);
82
83             adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
84
85             /* We should never rewrite a pkt using the MISS adjacency */
86             ASSERT(adj_index0);
87
88             adj0 = adj_get (adj_index0);
89
90             /* Guess we are only writing on simple Ethernet header. */
91             vnet_rewrite_one_header (adj0[0], h0,
92                                      sizeof (ethernet_header_t));
93
94             /* Update packet buffer attributes/set output interface. */
95             rw_len0 = adj0[0].rewrite_header.data_bytes;
96             vnet_buffer(p0)->ip.save_rewrite_length = rw_len0;
97
98             vlib_increment_combined_counter
99                 (&adjacency_counters,
100                  cpu_index, adj_index0,
101                  /* packet increment */ 0,
102                  /* byte increment */ rw_len0-sizeof(ethernet_header_t));
103
104             /* Check MTU of outgoing interface. */
105             if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0)  <=
106                               adj0[0].rewrite_header.max_l3_packet_bytes)))
107             {
108                 /* Don't adjust the buffer for ttl issue; icmp-error node wants
109                  * to see the IP headerr */
110                 p0->current_data -= rw_len0;
111                 p0->current_length += rw_len0;
112                 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
113
114                 if (is_midchain)
115                 {
116                     adj0->sub_type.midchain.fixup_func(vm, adj0, p0);
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                 p0->current_config_index = vec_elt(cm->config_index_by_sw_if_index,
126                                                    tx_sw_if_index0);
127                 vnet_get_config_data (&cm->config_main,
128                                       &p0->current_config_index,
129                                       &next0,
130                                       /* # bytes of config data */ 0);
131             }
132             else
133             {
134                 /* can't fragment L2 */
135                 next0 = ADJ_L2_REWRITE_NEXT_DROP;
136             }
137
138             if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
139             {
140                 adj_l2_trace_t *tr = vlib_add_trace (vm, node,
141                                                      p0, sizeof (*tr));
142                 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
143             }
144
145             vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
146                                              to_next, n_left_to_next,
147                                              pi0, next0);
148         }
149
150         vlib_put_next_frame (vm, node, next_index, n_left_to_next);
151     }
152
153     return frame->n_vectors;
154 }
155
156 static uword
157 adj_l2_rewrite (vlib_main_t * vm,
158                 vlib_node_runtime_t * node,
159                 vlib_frame_t * frame)
160 {
161     return adj_l2_rewrite_inline (vm, node, frame, 0);
162 }
163
164 static uword
165 adj_l2_midchain (vlib_main_t * vm,
166                  vlib_node_runtime_t * node,
167                  vlib_frame_t * frame)
168 {
169     return adj_l2_rewrite_inline (vm, node, frame, 1);
170 }
171
172 VLIB_REGISTER_NODE (adj_l2_rewrite_node) = {
173     .function = adj_l2_rewrite,
174     .name = "adj-l2-rewrite",
175     .vector_size = sizeof (u32),
176
177     .format_trace = format_adj_l2_trace,
178
179     .n_next_nodes = 1,
180     .next_nodes = {
181         [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop",
182     },
183 };
184
185 VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_rewrite_node, adj_l2_rewrite)
186
187 VLIB_REGISTER_NODE (adj_l2_midchain_node) = {
188     .function = adj_l2_midchain,
189     .name = "adj-l2-midchain",
190     .vector_size = sizeof (u32),
191
192     .format_trace = format_adj_l2_trace,
193
194     .n_next_nodes = 1,
195     .next_nodes = {
196         [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop",
197     },
198 };
199
200 VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_midchain_node, adj_l2_midchain)