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 <vppinfra/error.h>
17 #include <vppinfra/hash.h>
18 #include <vnet/vnet.h>
19 #include <vnet/ip/ip.h>
20 #include <vnet/ip/udp.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/lisp-gpe/lisp_gpe.h>
24 #define foreach_lisp_gpe_tx_next \
25 _(DROP, "error-drop") \
26 _(IP4_LOOKUP, "ip4-lookup")
30 #define _(sym,str) LISP_GPE_TX_NEXT_##sym,
31 foreach_lisp_gpe_tx_next
39 } lisp_gpe_tx_trace_t;
42 format_lisp_gpe_tx_trace (u8 * s, va_list * args)
44 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
45 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
46 lisp_gpe_tx_trace_t * t = va_arg (*args, lisp_gpe_tx_trace_t *);
48 s = format (s, "LISP-GPE-TX: tunnel %d", t->tunnel_index);
53 lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
54 vlib_frame_t * from_frame)
56 u32 n_left_from, next_index, * from, * to_next;
57 lisp_gpe_main_t * lgm = &lisp_gpe_main;
58 u32 pkts_encapsulated = 0;
60 from = vlib_frame_vector_args (from_frame);
61 n_left_from = from_frame->n_vectors;
63 next_index = node->cached_next_index;
65 while (n_left_from > 0)
69 vlib_get_next_frame (vm, node, next_index,
70 to_next, n_left_to_next);
72 while (n_left_from >= 4 && n_left_to_next >= 2)
75 vlib_buffer_t * b0, * b1;
77 u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
78 ip_adjacency_t * adj0, * adj1;
79 lisp_gpe_tunnel_t * t0, * t1;
81 next0 = next1 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
83 /* Prefetch next iteration. */
85 vlib_buffer_t * p2, *p3;
87 p2 = vlib_get_buffer (vm, from[2]);
88 p3 = vlib_get_buffer (vm, from[3]);
90 vlib_prefetch_buffer_header(p2, LOAD);
91 vlib_prefetch_buffer_header(p3, LOAD);
93 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
94 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
106 b0 = vlib_get_buffer (vm, bi0);
107 b1 = vlib_get_buffer (vm, bi1);
109 /* Get adjacency and from it the tunnel_index */
110 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
111 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
113 adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
114 adj1 = ip_get_adjacency (lgm->lookup_main, adj_index1);
116 tunnel_index0 = adj0->rewrite_header.node_index;
117 tunnel_index1 = adj1->rewrite_header.node_index;
119 t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
120 t1 = pool_elt_at_index (lgm->tunnels, tunnel_index1);
125 ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
126 ip4_udp_encap_two (vm, b0, b1, t0->rewrite, t1->rewrite, 36);
128 /* Reset to look up tunnel partner in the configured FIB */
129 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
130 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
132 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
134 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
136 tr->tunnel_index = t0 - lgm->tunnels;
138 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
140 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
142 tr->tunnel_index = t1 - lgm->tunnels;
145 pkts_encapsulated += 2;
147 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
148 n_left_to_next, bi0, bi1, next0,
152 while (n_left_from > 0 && n_left_to_next > 0)
155 u32 bi0, adj_index0, tunnel_index0;
156 u32 next0 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
157 lisp_gpe_tunnel_t * t0 = 0;
158 ip_adjacency_t * adj0;
167 b0 = vlib_get_buffer (vm, bi0);
169 /* Get adjacency and from it the tunnel_index */
170 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
171 adj0 = ip_get_adjacency (lgm->lookup_main, adj_index0);
173 tunnel_index0 = adj0->rewrite_header.node_index;
174 t0 = pool_elt_at_index (lgm->tunnels, tunnel_index0);
178 ASSERT (sizeof(ip4_udp_lisp_gpe_header_t) == 36);
179 ip4_udp_encap_one (vm, b0, t0->rewrite, 36);
181 /* Reset to look up tunnel partner in the configured FIB */
182 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
186 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
188 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
190 tr->tunnel_index = t0 - lgm->tunnels;
192 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
193 n_left_to_next, bi0, next0);
196 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
198 vlib_node_increment_counter (vm, node->node_index,
199 LISP_GPE_ERROR_ENCAPSULATED, pkts_encapsulated);
200 return from_frame->n_vectors;
204 format_lisp_gpe_name (u8 * s, va_list * args)
206 u32 dev_instance = va_arg (*args, u32);
207 return format (s, "lisp_gpe%d", dev_instance);
210 VNET_DEVICE_CLASS (lisp_gpe_device_class,static) = {
212 .format_device_name = format_lisp_gpe_name,
213 .format_tx_trace = format_lisp_gpe_tx_trace,
214 .tx_function = lisp_gpe_interface_tx,
215 .no_flatten_output_chains = 1,
219 dummy_set_rewrite (vnet_main_t * vnm, u32 sw_if_index, u32 l3_type,
220 void * dst_address, void * rewrite, uword max_rewrite_bytes)
226 format_lisp_gpe_header_with_length (u8 * s, va_list * args)
228 lisp_gpe_header_t * h = va_arg (*args, lisp_gpe_header_t *);
229 u32 max_header_bytes = va_arg (*args, u32);
232 header_bytes = sizeof (h[0]);
233 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
234 return format (s, "lisp-gpe header truncated");
236 s = format (s, "flags: ");
237 #define _(n,v) if (h->flags & v) s = format (s, "%s ", #n);
238 foreach_lisp_gpe_flag_bit;
241 s = format (s, "\n ver_res %d res %d next_protocol %d iid %d(%x)",
242 h->ver_res, h->res, h->next_protocol,
243 clib_net_to_host_u32 (h->iid),
244 clib_net_to_host_u32 (h->iid));
248 VNET_HW_INTERFACE_CLASS (lisp_gpe_hw_class) = {
250 .format_header = format_lisp_gpe_header_with_length,
251 .set_rewrite = dummy_set_rewrite,
255 add_del_ip_prefix_route (ip_prefix_t * dst_prefix, u32 table_id,
256 ip_adjacency_t * add_adj, u8 is_add, u32 * adj_index)
260 if (ip_prefix_version(dst_prefix) == IP4)
262 ip4_main_t * im4 = &ip4_main;
263 ip4_add_del_route_args_t a;
264 ip4_address_t addr = ip_prefix_v4(dst_prefix);
266 memset(&a, 0, sizeof(a));
267 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
268 a.table_index_or_table_id = table_id;
270 a.dst_address_length = ip_prefix_len(dst_prefix);
271 a.dst_address = addr;
272 a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
275 ip4_add_del_route (im4, &a);
279 p = ip4_get_route (im4, table_id, 0, addr.as_u8,
280 ip_prefix_len(dst_prefix));
283 clib_warning("Failed to insert route for eid %U!",
284 format_ip4_address_and_length, addr.as_u8,
285 ip_prefix_len(dst_prefix));
293 ip6_main_t * im6 = &ip6_main;
294 ip6_add_del_route_args_t a;
295 ip6_address_t addr = ip_prefix_v6(dst_prefix);
297 memset(&a, 0, sizeof(a));
298 a.flags = IP6_ROUTE_FLAG_TABLE_ID;
299 a.table_index_or_table_id = table_id;
301 a.dst_address_length = ip_prefix_len(dst_prefix);
302 a.dst_address = addr;
303 a.flags |= is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL;
307 ip6_add_del_route (im6, &a);
311 adj_index[0] = ip6_get_route (im6, table_id, 0, &addr,
312 ip_prefix_len(dst_prefix));
313 if (adj_index[0] == 0)
315 clib_warning("Failed to insert route for eid %U!",
316 format_ip6_address_and_length, addr.as_u8,
317 ip_prefix_len(dst_prefix));
326 add_del_lisp_gpe_default_route (u32 table_id, u8 is_v4, u8 is_add)
328 lisp_gpe_main_t * lgm = &lisp_gpe_main;
333 /* setup adjacency */
334 memset (&adj, 0, sizeof(adj));
337 adj.explicit_fib_index = ~0;
338 adj.lookup_next_index = lgm->ip4_lookup_next_lgpe_ip4_lookup;
339 /* default route has tunnel_index ~0 */
340 adj.rewrite_header.sw_if_index = ~0;
342 /* set prefix to 0/0 */
343 memset(&prefix, 0, sizeof(prefix));
344 ip_prefix_version(&prefix) = is_v4 ? IP4 : IP6;
346 /* add/delete route for prefix */
347 add_del_ip_prefix_route (&prefix, table_id, &adj, is_add, &adj_index);
351 lisp_gpe_iface_set_table (u32 sw_if_index, u32 table_id, u8 is_ip4)
355 ip4_main_t * im4 = &ip4_main;
357 fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
358 IP4_ROUTE_FLAG_TABLE_ID);
360 /* fib's created if it doesn't exist */
363 vec_validate(im4->fib_index_by_sw_if_index, sw_if_index);
364 im4->fib_index_by_sw_if_index[sw_if_index] = fib->index;
368 ip6_main_t * im6 = &ip6_main;
370 fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
371 IP6_ROUTE_FLAG_TABLE_ID);
373 /* fib's created if it doesn't exist */
376 vec_validate(im6->fib_index_by_sw_if_index, sw_if_index);
377 im6->fib_index_by_sw_if_index[sw_if_index] = fib->index;
382 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
385 lisp_gpe_main_t * lgm = &lisp_gpe_main;
386 vnet_main_t * vnm = lgm->vnet_main;
387 vnet_hw_interface_t * hi;
388 u32 hw_if_index = ~0, lookup_next_index, flen;
391 hip = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id);
397 clib_warning ("Interface for vrf %d already exists", a->table_id);
401 /* create hw lisp_gpeX iface if needed, otherwise reuse existing */
402 flen = vec_len(lgm->free_lisp_gpe_tunnel_hw_if_indices);
405 hw_if_index = lgm->free_lisp_gpe_tunnel_hw_if_indices[flen - 1];
406 _vec_len(lgm->free_lisp_gpe_tunnel_hw_if_indices) -= 1;
410 hw_if_index = vnet_register_interface (vnm,
411 lisp_gpe_device_class.index,
413 lisp_gpe_hw_class.index, 0);
416 hi = vnet_get_hw_interface (vnm, hw_if_index);
417 hash_set(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id, hw_if_index);
419 /* set tunnel termination: post decap, packets are tagged as having been
420 * originated by lisp-gpe interface */
421 hash_set(lgm->tunnel_term_sw_if_index_by_vni, a->vni, hi->sw_if_index);
422 hash_set(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index, a->vni);
424 /* set ingress arc from lgpe_ip4_lookup */
425 lookup_next_index = vlib_node_add_next (lgm->vlib_main,
426 lgpe_ip4_lookup_node.index,
427 hi->output_node_index);
428 hash_set(lgm->lgpe_ip4_lookup_next_index_by_table_id, a->table_id,
431 /* insert default routes that point to lgpe-ipx-lookup */
432 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 1);
433 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 1);
435 /* set egress arcs */
436 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
437 hi->tx_node_index, str, LISP_GPE_TX_NEXT_##sym);
438 foreach_lisp_gpe_tx_next
441 /* set interface in appropriate v4 and v6 FIBs */
442 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 1);
443 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 0);
445 /* enable interface */
446 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
447 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
448 vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
449 VNET_HW_INTERFACE_FLAG_LINK_UP);
455 clib_warning("The interface for vrf %d doesn't exist", a->table_id);
458 hi = vnet_get_hw_interface (vnm, hip[0]);
460 /* disable interface */
461 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0/* down */);
462 vnet_hw_interface_set_flags (vnm, hi->hw_if_index, 0/* down */);
463 hash_unset(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id);
464 vec_add1(lgm->free_lisp_gpe_tunnel_hw_if_indices, hi->hw_if_index);
466 /* clean tunnel termination and vni to sw_if_index binding */
467 vni = hash_get(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index);
468 hash_unset(lgm->tunnel_term_sw_if_index_by_vni, vni[0]);
469 hash_unset(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index);
471 /* unset default routes */
472 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 0);
473 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 0);
477 static clib_error_t *
478 lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
479 vlib_cli_command_t * cmd)
481 unformat_input_t _line_input, * line_input = &_line_input;
485 vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
487 /* Get a line of input. */
488 if (! unformat_user (input, unformat_line_input, line_input))
491 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
493 if (unformat (line_input, "add"))
495 else if (unformat (line_input, "del"))
497 else if (unformat (line_input, "vrf %d", &table_id))
501 return clib_error_return (0, "parse error: '%U'",
502 format_unformat_error, line_input);
507 a->table_id = table_id;
508 vnet_lisp_gpe_add_del_iface (a, 0);
512 VLIB_CLI_COMMAND (add_del_lisp_gpe_iface_command, static) = {
513 .path = "lisp gpe iface",
514 .short_help = "lisp gpe iface add/del table-index <table_index> vrf <vrf>",
515 .function = lisp_gpe_add_del_iface_command_fn,