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") \
27 _(IP6_LOOKUP, "ip6-lookup")
31 #define _(sym,str) LISP_GPE_TX_NEXT_##sym,
32 foreach_lisp_gpe_tx_next
40 } lisp_gpe_tx_trace_t;
43 format_lisp_gpe_tx_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 lisp_gpe_tx_trace_t * t = va_arg (*args, lisp_gpe_tx_trace_t *);
49 s = format (s, "LISP-GPE-TX: tunnel %d", t->tunnel_index);
54 get_one_tunnel_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
55 lisp_gpe_tunnel_t ** t0, u8 is_v4)
57 u32 adj_index0, tunnel_index0;
58 ip_adjacency_t * adj0;
60 /* Get adjacency and from it the tunnel_index */
61 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
64 adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
66 adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
68 tunnel_index0 = adj0->if_address_index;
69 t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
75 encap_one_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
76 lisp_gpe_tunnel_t * t0, u32 * next0, u8 is_v4)
78 ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
79 ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
83 ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 36, 1);
84 next0[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
89 ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 56, 0);
90 next0[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
95 get_two_tunnels_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0,
96 vlib_buffer_t * b1, lisp_gpe_tunnel_t ** t0,
97 lisp_gpe_tunnel_t ** t1, u8 is_v4)
99 u32 adj_index0, adj_index1, tunnel_index0, tunnel_index1;
100 ip_adjacency_t * adj0, * adj1;
102 /* Get adjacency and from it the tunnel_index */
103 adj_index0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
104 adj_index1 = vnet_buffer(b1)->ip.adj_index[VLIB_TX];
108 adj0 = ip_get_adjacency (lgm->lm4, adj_index0);
109 adj1 = ip_get_adjacency (lgm->lm4, adj_index1);
113 adj0 = ip_get_adjacency (lgm->lm6, adj_index0);
114 adj1 = ip_get_adjacency (lgm->lm6, adj_index1);
117 tunnel_index0 = adj0->if_address_index;
118 tunnel_index1 = adj1->if_address_index;
120 t0[0] = pool_elt_at_index(lgm->tunnels, tunnel_index0);
121 t1[0] = pool_elt_at_index(lgm->tunnels, tunnel_index1);
128 encap_two_inline (lisp_gpe_main_t * lgm, vlib_buffer_t * b0, vlib_buffer_t * b1,
129 lisp_gpe_tunnel_t * t0, lisp_gpe_tunnel_t * t1, u32 * next0,
130 u32 * next1, u8 is_v4)
132 ASSERT(sizeof(ip4_udp_lisp_gpe_header_t) == 36);
133 ASSERT(sizeof(ip6_udp_lisp_gpe_header_t) == 56);
137 ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 36, 1);
138 ip_udp_encap_one (lgm->vlib_main, b1, t1->rewrite, 36, 1);
139 next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP4_LOOKUP;
143 ip_udp_encap_one (lgm->vlib_main, b0, t0->rewrite, 56, 0);
144 ip_udp_encap_one (lgm->vlib_main, b1, t1->rewrite, 56, 0);
145 next0[0] = next1[0] = LISP_GPE_TX_NEXT_IP6_LOOKUP;
149 #define is_v4_packet(_h) ((*(u8*) _h) & 0xF0) == 0x40
152 lisp_gpe_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node,
153 vlib_frame_t * from_frame)
155 u32 n_left_from, next_index, * from, * to_next;
156 lisp_gpe_main_t * lgm = &lisp_gpe_main;
158 from = vlib_frame_vector_args (from_frame);
159 n_left_from = from_frame->n_vectors;
161 next_index = node->cached_next_index;
163 while (n_left_from > 0)
167 vlib_get_next_frame (vm, node, next_index,
168 to_next, n_left_to_next);
170 while (n_left_from >= 4 && n_left_to_next >= 2)
173 vlib_buffer_t * b0, * b1;
175 lisp_gpe_tunnel_t * t0 = 0, * t1 = 0;
176 u8 is_v4_eid0, is_v4_eid1;
178 next0 = next1 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
180 /* Prefetch next iteration. */
182 vlib_buffer_t * p2, *p3;
184 p2 = vlib_get_buffer (vm, from[2]);
185 p3 = vlib_get_buffer (vm, from[3]);
187 vlib_prefetch_buffer_header(p2, LOAD);
188 vlib_prefetch_buffer_header(p3, LOAD);
190 CLIB_PREFETCH(p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
191 CLIB_PREFETCH(p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD);
203 b0 = vlib_get_buffer (vm, bi0);
204 b1 = vlib_get_buffer (vm, bi1);
206 is_v4_eid0 = is_v4_packet(vlib_buffer_get_current (b0));
207 is_v4_eid1 = is_v4_packet(vlib_buffer_get_current (b1));
209 if (PREDICT_TRUE(is_v4_eid0 == is_v4_eid1))
211 get_two_tunnels_inline (lgm, b0, b1, &t0, &t1,
216 get_one_tunnel_inline (lgm, b0, &t0, is_v4_eid0 ? 1 : 0);
217 get_one_tunnel_inline (lgm, b1, &t1, is_v4_eid1 ? 1 : 0);
221 ip_addr_version(&t0->dst) == ip_addr_version(&t1->dst)))
223 encap_two_inline (lgm, b0, b1, t0, t1, &next0, &next1,
224 ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
228 encap_one_inline (lgm, b0, t0, &next0,
229 ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
230 encap_one_inline (lgm, b1, t1, &next1,
231 ip_addr_version(&t1->dst) == IP4 ? 1 : 0);
234 /* Reset to look up tunnel partner in the configured FIB */
235 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
236 vnet_buffer(b1)->sw_if_index[VLIB_TX] = t1->encap_fib_index;
238 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
240 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
242 tr->tunnel_index = t0 - lgm->tunnels;
244 if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED))
246 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b1,
248 tr->tunnel_index = t1 - lgm->tunnels;
251 vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next,
252 n_left_to_next, bi0, bi1, next0,
256 while (n_left_from > 0 && n_left_to_next > 0)
259 u32 bi0, next0 = LISP_GPE_TX_NEXT_IP4_LOOKUP;
260 lisp_gpe_tunnel_t * t0 = 0;
270 b0 = vlib_get_buffer (vm, bi0);
272 is_v4_0 = is_v4_packet(vlib_buffer_get_current (b0));
273 get_one_tunnel_inline (lgm, b0, &t0, is_v4_0 ? 1 : 0);
275 encap_one_inline (lgm, b0, t0, &next0,
276 ip_addr_version(&t0->dst) == IP4 ? 1 : 0);
278 /* Reset to look up tunnel partner in the configured FIB */
279 vnet_buffer(b0)->sw_if_index[VLIB_TX] = t0->encap_fib_index;
281 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
283 lisp_gpe_tx_trace_t *tr = vlib_add_trace (vm, node, b0,
285 tr->tunnel_index = t0 - lgm->tunnels;
287 vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
288 n_left_to_next, bi0, next0);
291 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
294 return from_frame->n_vectors;
298 format_lisp_gpe_name (u8 * s, va_list * args)
300 u32 dev_instance = va_arg (*args, u32);
301 return format (s, "lisp_gpe%d", dev_instance);
304 VNET_DEVICE_CLASS (lisp_gpe_device_class,static) = {
306 .format_device_name = format_lisp_gpe_name,
307 .format_tx_trace = format_lisp_gpe_tx_trace,
308 .tx_function = lisp_gpe_interface_tx,
309 .no_flatten_output_chains = 1,
313 dummy_set_rewrite (vnet_main_t * vnm, u32 sw_if_index, u32 l3_type,
314 void * dst_address, void * rewrite, uword max_rewrite_bytes)
320 format_lisp_gpe_header_with_length (u8 * s, va_list * args)
322 lisp_gpe_header_t * h = va_arg (*args, lisp_gpe_header_t *);
323 u32 max_header_bytes = va_arg (*args, u32);
326 header_bytes = sizeof (h[0]);
327 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
328 return format (s, "lisp-gpe header truncated");
330 s = format (s, "flags: ");
331 #define _(n,v) if (h->flags & v) s = format (s, "%s ", #n);
332 foreach_lisp_gpe_flag_bit;
335 s = format (s, "\n ver_res %d res %d next_protocol %d iid %d(%x)",
336 h->ver_res, h->res, h->next_protocol,
337 clib_net_to_host_u32 (h->iid),
338 clib_net_to_host_u32 (h->iid));
342 VNET_HW_INTERFACE_CLASS (lisp_gpe_hw_class) = {
344 .format_header = format_lisp_gpe_header_with_length,
345 .set_rewrite = dummy_set_rewrite,
349 add_del_ip_prefix_route (ip_prefix_t * dst_prefix, u32 table_id,
350 ip_adjacency_t * add_adj, u8 is_add, u32 * adj_index)
354 if (ip_prefix_version(dst_prefix) == IP4)
356 ip4_main_t * im4 = &ip4_main;
357 ip4_add_del_route_args_t a;
358 ip4_address_t addr = ip_prefix_v4(dst_prefix);
360 memset(&a, 0, sizeof(a));
361 a.flags = IP4_ROUTE_FLAG_TABLE_ID;
362 a.table_index_or_table_id = table_id;
364 a.dst_address_length = ip_prefix_len(dst_prefix);
365 a.dst_address = addr;
366 a.flags |= is_add ? IP4_ROUTE_FLAG_ADD : IP4_ROUTE_FLAG_DEL;
368 a.n_add_adj = is_add ? 1 : 0;
370 ip4_add_del_route (im4, &a);
374 p = ip4_get_route (im4, table_id, 0, addr.as_u8,
375 ip_prefix_len(dst_prefix));
378 clib_warning("Failed to insert route for eid %U!",
379 format_ip4_address_and_length, addr.as_u8,
380 ip_prefix_len(dst_prefix));
388 ip6_main_t * im6 = &ip6_main;
389 ip6_add_del_route_args_t a;
390 ip6_address_t addr = ip_prefix_v6(dst_prefix);
392 memset(&a, 0, sizeof(a));
393 a.flags = IP6_ROUTE_FLAG_TABLE_ID;
394 a.table_index_or_table_id = table_id;
396 a.dst_address_length = ip_prefix_len(dst_prefix);
397 a.dst_address = addr;
398 a.flags |= is_add ? IP6_ROUTE_FLAG_ADD : IP6_ROUTE_FLAG_DEL;
400 a.n_add_adj = is_add ? 1 : 0;
402 ip6_add_del_route (im6, &a);
406 adj_index[0] = ip6_get_route (im6, table_id, 0, &addr,
407 ip_prefix_len(dst_prefix));
408 if (adj_index[0] == 0)
410 clib_warning("Failed to insert route for eid %U!",
411 format_ip6_address_and_length, addr.as_u8,
412 ip_prefix_len(dst_prefix));
421 add_del_lisp_gpe_default_route (u32 table_id, u8 is_v4, u8 is_add)
423 lisp_gpe_main_t * lgm = &lisp_gpe_main;
428 /* setup adjacency */
429 memset (&adj, 0, sizeof(adj));
432 adj.explicit_fib_index = ~0;
433 adj.lookup_next_index = is_v4 ? lgm->ip4_lookup_next_lgpe_ip4_lookup :
434 lgm->ip6_lookup_next_lgpe_ip6_lookup;
435 /* default route has tunnel_index ~0 */
436 adj.rewrite_header.sw_if_index = ~0;
438 /* set prefix to 0/0 */
439 memset(&prefix, 0, sizeof(prefix));
440 ip_prefix_version(&prefix) = is_v4 ? IP4 : IP6;
442 /* add/delete route for prefix */
443 add_del_ip_prefix_route (&prefix, table_id, &adj, is_add, &adj_index);
447 lisp_gpe_iface_set_table (u32 sw_if_index, u32 table_id, u8 is_ip4)
451 ip4_main_t * im4 = &ip4_main;
453 fib = find_ip4_fib_by_table_index_or_id (im4, table_id,
454 IP4_ROUTE_FLAG_TABLE_ID);
456 /* fib's created if it doesn't exist */
459 vec_validate(im4->fib_index_by_sw_if_index, sw_if_index);
460 im4->fib_index_by_sw_if_index[sw_if_index] = fib->index;
464 ip6_main_t * im6 = &ip6_main;
466 fib = find_ip6_fib_by_table_index_or_id (im6, table_id,
467 IP6_ROUTE_FLAG_TABLE_ID);
469 /* fib's created if it doesn't exist */
472 vec_validate(im6->fib_index_by_sw_if_index, sw_if_index);
473 im6->fib_index_by_sw_if_index[sw_if_index] = fib->index;
478 vnet_lisp_gpe_add_del_iface (vnet_lisp_gpe_add_del_iface_args_t * a,
481 lisp_gpe_main_t * lgm = &lisp_gpe_main;
482 vnet_main_t * vnm = lgm->vnet_main;
483 vnet_hw_interface_t * hi;
484 u32 hw_if_index = ~0, lookup_next_index4, lookup_next_index6, flen;
485 uword * hip, * vni, * si;
488 if (vnet_lisp_gpe_enable_disable_status() == 0)
490 clib_warning ("LISP is disabled!");
491 return VNET_API_ERROR_LISP_DISABLED;
494 hip = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id);
500 clib_warning ("vrf %d already mapped to a vni", a->table_id);
504 si = hash_get(lgm->tunnel_term_sw_if_index_by_vni, a->vni);
507 clib_warning ("Interface for vni %d already exists", a->vni);
511 /* create hw lisp_gpeX iface if needed, otherwise reuse existing */
512 flen = vec_len(lgm->free_lisp_gpe_tunnel_hw_if_indices);
515 hw_if_index = lgm->free_lisp_gpe_tunnel_hw_if_indices[flen - 1];
516 _vec_len(lgm->free_lisp_gpe_tunnel_hw_if_indices) -= 1;
518 hi = vnet_get_hw_interface (vnm, hw_if_index);
520 /* rename interface */
521 new_name = format (0, "%U", lisp_gpe_device_class.format_device_name,
523 vec_add1(new_name, 0);
524 vnet_rename_interface (vnm, hw_if_index, (char *) new_name);
527 /* clear old stats of freed interface before reuse */
528 vnet_interface_main_t * im = &vnm->interface_main;
529 vnet_interface_counter_lock (im);
530 vlib_zero_combined_counter (
531 &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
533 vlib_zero_combined_counter (
534 &im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_RX],
536 vlib_zero_simple_counter (
537 &im->sw_if_counters[VNET_INTERFACE_COUNTER_DROP],
539 vnet_interface_counter_unlock (im);
543 hw_if_index = vnet_register_interface (vnm,
544 lisp_gpe_device_class.index,
546 lisp_gpe_hw_class.index, 0);
547 hi = vnet_get_hw_interface (vnm, hw_if_index);
550 hash_set(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id, hw_if_index);
552 /* set tunnel termination: post decap, packets are tagged as having been
553 * originated by lisp-gpe interface */
554 hash_set(lgm->tunnel_term_sw_if_index_by_vni, a->vni, hi->sw_if_index);
555 hash_set(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index, a->vni);
557 /* set ingress arc from lgpe_ipX_lookup */
558 lookup_next_index4 = vlib_node_add_next (lgm->vlib_main,
559 lgpe_ip4_lookup_node.index,
560 hi->output_node_index);
561 lookup_next_index6 = vlib_node_add_next (lgm->vlib_main,
562 lgpe_ip6_lookup_node.index,
563 hi->output_node_index);
564 hash_set(lgm->lgpe_ip4_lookup_next_index_by_table_id, a->table_id,
566 hash_set(lgm->lgpe_ip6_lookup_next_index_by_table_id, a->table_id,
569 /* insert default routes that point to lgpe-ipx-lookup */
570 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 1);
571 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 1);
573 /* set egress arcs */
574 #define _(sym,str) vlib_node_add_named_next_with_slot (vnm->vlib_main, \
575 hi->tx_node_index, str, LISP_GPE_TX_NEXT_##sym);
576 foreach_lisp_gpe_tx_next
579 /* set interface in appropriate v4 and v6 FIBs */
580 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 1);
581 lisp_gpe_iface_set_table (hi->sw_if_index, a->table_id, 0);
583 /* enable interface */
584 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
585 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
586 vnet_hw_interface_set_flags (vnm, hi->hw_if_index,
587 VNET_HW_INTERFACE_FLAG_LINK_UP);
593 clib_warning("The interface for vrf %d doesn't exist", a->table_id);
596 hi = vnet_get_hw_interface (vnm, hip[0]);
598 /* disable interface */
599 vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 0/* down */);
600 vnet_hw_interface_set_flags (vnm, hi->hw_if_index, 0/* down */);
601 hash_unset(lgm->lisp_gpe_hw_if_index_by_table_id, a->table_id);
602 vec_add1(lgm->free_lisp_gpe_tunnel_hw_if_indices, hi->hw_if_index);
604 /* clean tunnel termination and vni to sw_if_index binding */
605 vni = hash_get(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index);
606 hash_unset(lgm->tunnel_term_sw_if_index_by_vni, vni[0]);
607 hash_unset(lgm->vni_by_tunnel_term_sw_if_index, hi->sw_if_index);
609 /* unset default routes */
610 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */1, 0);
611 add_del_lisp_gpe_default_route (a->table_id, /* is_v4 */0, 0);
617 static clib_error_t *
618 lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
619 vlib_cli_command_t * cmd)
621 unformat_input_t _line_input, * line_input = &_line_input;
623 clib_error_t * error = 0;
626 u8 vni_is_set = 0, vrf_is_set = 0;
628 vnet_lisp_gpe_add_del_iface_args_t _a, * a = &_a;
630 /* Get a line of input. */
631 if (! unformat_user (input, unformat_line_input, line_input))
634 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
636 if (unformat (line_input, "add"))
638 else if (unformat (line_input, "del"))
640 else if (unformat (line_input, "vrf %d", &table_id))
644 else if (unformat (line_input, "vni %d", &vni))
650 return clib_error_return (0, "parse error: '%U'",
651 format_unformat_error, line_input);
655 if (!vni_is_set || !vrf_is_set)
657 return clib_error_return (0, "Both vni and vrf must be set!");
661 a->table_id = table_id;
663 rv = vnet_lisp_gpe_add_del_iface (a, 0);
666 error = clib_error_return(0, "failed to %s gpe iface!",
667 is_add ? "add" : "delete");
673 VLIB_CLI_COMMAND (add_del_lisp_gpe_iface_command, static) = {
674 .path = "lisp gpe iface",
675 .short_help = "lisp gpe iface add/del vni <vni> vrf <vrf>",
676 .function = lisp_gpe_add_del_iface_command_fn,