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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/pg/pg.h>
18 #include <vnet/lisp-gpe/lisp_gpe.h>
26 } lisp_gpe_rx_trace_t;
29 format_lisp_gpe_rx_trace (u8 * s, va_list * args)
31 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
32 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 lisp_gpe_rx_trace_t * t = va_arg (*args, lisp_gpe_rx_trace_t *);
35 if (t->tunnel_index != ~0)
37 s = format (s, "LISP-GPE: tunnel %d next %d error %d", t->tunnel_index,
38 t->next_index, t->error);
42 s = format (s, "LISP-GPE: no tunnel next %d error %d\n", t->next_index,
45 s = format (s, "\n %U", format_lisp_gpe_header_with_length, &t->h,
46 (u32) sizeof (t->h) /* max size */);
51 next_proto_to_next_index[LISP_GPE_NEXT_PROTOS] ={
52 LISP_GPE_INPUT_NEXT_DROP,
53 LISP_GPE_INPUT_NEXT_IP4_INPUT,
54 LISP_GPE_INPUT_NEXT_IP6_INPUT,
55 LISP_GPE_INPUT_NEXT_DROP,
56 LISP_GPE_INPUT_NEXT_DROP
60 next_protocol_to_next_index (lisp_gpe_header_t * lgh, u8 * next_header)
62 /* legay lisp router */
63 if (PREDICT_FALSE((lgh->flags & LISP_GPE_FLAGS_P) == 0))
65 ip4_header_t * iph = (ip4_header_t *) next_header;
66 if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
67 return LISP_GPE_INPUT_NEXT_IP4_INPUT;
68 else if ((iph->ip_version_and_header_length & 0xF0) == 0x60)
69 return LISP_GPE_INPUT_NEXT_IP6_INPUT;
71 return LISP_GPE_INPUT_NEXT_DROP;
74 else if ((lgh->flags & LISP_GPE_FLAGS_P)
75 && lgh->next_protocol < LISP_GPE_NEXT_PROTOS)
76 return next_proto_to_next_index[lgh->next_protocol];
78 return LISP_GPE_INPUT_NEXT_DROP;
82 lisp_gpe_input (vlib_main_t * vm, vlib_node_runtime_t * node,
83 vlib_frame_t * from_frame)
85 u32 n_left_from, next_index, * from, * to_next;
86 lisp_gpe_tunnel_key_t last_key;
87 u32 pkts_decapsulated = 0;
89 memset (&last_key, 0xff, sizeof (last_key));
91 from = vlib_frame_vector_args (from_frame);
92 n_left_from = from_frame->n_vectors;
94 next_index = node->cached_next_index;
96 while (n_left_from > 0)
100 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
102 while (n_left_from >= 4 && n_left_to_next >= 2)
105 vlib_buffer_t * b0, * b1;
106 ip4_udp_lisp_gpe_header_t * iul0, * iul1;
110 next0 = next1 = LISP_GPE_INPUT_NEXT_IP4_INPUT;
112 /* Prefetch next iteration. */
114 vlib_buffer_t * p2, * p3;
116 p2 = vlib_get_buffer (vm, from[2]);
117 p3 = vlib_get_buffer (vm, from[3]);
119 vlib_prefetch_buffer_header (p2, LOAD);
120 vlib_prefetch_buffer_header (p3, LOAD);
122 CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
123 CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
135 b0 = vlib_get_buffer (vm, bi0);
136 b1 = vlib_get_buffer (vm, bi1);
138 /* udp leaves current_data pointing at the lisp header */
139 vlib_buffer_advance (b0, - IP_UDP_HDR_LEN);
140 vlib_buffer_advance (b1, - IP_UDP_HDR_LEN);
142 iul0 = vlib_buffer_get_current (b0);
143 iul1 = vlib_buffer_get_current (b1);
145 /* pop (ip, udp, lisp-gpe) */
146 vlib_buffer_advance (b0, sizeof (*iul0));
147 vlib_buffer_advance (b1, sizeof (*iul1));
149 /* determine next_index from lisp-gpe header */
150 next0 = next_protocol_to_next_index (&iul0->lisp,
151 vlib_buffer_get_current (b0));
152 next1 = next_protocol_to_next_index (&iul1->lisp,
153 vlib_buffer_get_current (b1));
155 /* Required to make the l2 tag push / pop code work on l2 subifs */
156 vnet_update_l2_len (b0);
157 vnet_update_l2_len (b1);
159 /* TODO hash to map iid to fib */
160 vnet_buffer(b0)->sw_if_index[VLIB_TX] = iul0->lisp.iid;
161 vnet_buffer(b1)->sw_if_index[VLIB_TX] = iul1->lisp.iid;
163 pkts_decapsulated += 2;
165 /* TODO error handling if security is implemented */
167 b0->error = error0 ? node->errors[error0] : 0;
168 b1->error = error1 ? node->errors[error1] : 0;
170 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
172 lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b0,
174 tr->next_index = next0;
179 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
181 lisp_gpe_rx_trace_t *tr = vlib_add_trace (vm, node, b1,
183 tr->next_index = next1;
188 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
189 n_left_to_next, bi0, bi1, next0,
193 while (n_left_from > 0 && n_left_to_next > 0)
198 ip4_udp_lisp_gpe_header_t * iul0;
208 b0 = vlib_get_buffer (vm, bi0);
210 /* udp leaves current_data pointing at the lisp header */
211 vlib_buffer_advance (b0, - IP_UDP_HDR_LEN);
213 iul0 = vlib_buffer_get_current (b0);
215 /* pop (ip, udp, lisp-gpe) */
216 vlib_buffer_advance (b0, sizeof (*iul0));
218 /* TODO if security is to be implemented, something similar to RPF,
219 * probably we'd like to check that the peer is allowed to send us
220 * packets. For this, we should use the tunnel table OR check that
221 * we have a mapping for the source eid and that the outer source of
222 * the packet is one of its locators */
224 /* determine next_index from lisp-gpe header */
225 next0 = next_protocol_to_next_index (&iul0->lisp,
226 vlib_buffer_get_current (b0));
228 /* Required to make the l2 tag push / pop code work on l2 subifs */
229 vnet_update_l2_len (b0);
231 /* TODO hash to map iid to fib */
232 vnet_buffer(b0)->sw_if_index[VLIB_TX] = iul0->lisp.iid;
233 pkts_decapsulated ++;
235 /* TODO error handling if security is implemented */
237 b0->error = error0 ? node->errors[error0] : 0;
239 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
241 lisp_gpe_rx_trace_t *tr
242 = vlib_add_trace (vm, node, b0, sizeof (*tr));
243 tr->next_index = next0;
248 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
249 n_left_to_next, bi0, next0);
252 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254 vlib_node_increment_counter (vm, lisp_gpe_input_node.index,
255 LISP_GPE_ERROR_DECAPSULATED,
257 return from_frame->n_vectors;
260 static char * lisp_gpe_error_strings[] = {
261 #define lisp_gpe_error(n,s) s,
262 #include <vnet/lisp-gpe/lisp_gpe_error.def>
263 #undef lisp_gpe_error
267 VLIB_REGISTER_NODE (lisp_gpe_input_node) = {
268 .function = lisp_gpe_input,
269 .name = "lisp-gpe-input",
270 /* Takes a vector of packets. */
271 .vector_size = sizeof (u32),
273 .n_errors = LISP_GPE_N_ERROR,
274 .error_strings = lisp_gpe_error_strings,
276 .n_next_nodes = LISP_GPE_INPUT_N_NEXT,
278 #define _(s,n) [LISP_GPE_INPUT_NEXT_##s] = n,
279 foreach_lisp_gpe_input_next
283 .format_buffer = format_lisp_gpe_header_with_length,
284 .format_trace = format_lisp_gpe_rx_trace,
285 // $$$$ .unformat_buffer = unformat_lisp_gpe_header,