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.
17 * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE tunnels.
21 #include <vnet/lisp-gpe/lisp_gpe.h>
22 #include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
23 #include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
24 #include <vnet/lisp-gpe/lisp_gpe_tenant.h>
25 #include <vnet/fib/fib_path_list.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/fib/fib_internal.h>
29 /** LISP-GPE global state */
30 lisp_gpe_main_t lisp_gpe_main;
33 /** CLI command to add/del forwarding entry. */
35 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
36 unformat_input_t * input,
37 vlib_cli_command_t * cmd)
39 unformat_input_t _line_input, *line_input = &_line_input;
41 ip_address_t lloc, rloc;
42 clib_error_t *error = 0;
43 gid_address_t _reid, *reid = &_reid, _leid, *leid = &_leid;
44 u8 reid_set = 0, leid_set = 0, is_negative = 0, dp_table_set = 0,
46 u32 vni = 0, dp_table = 0, action = ~0, w;
47 locator_pair_t pair, *pairs = 0;
50 memset (leid, 0, sizeof (*leid));
51 memset (reid, 0, sizeof (*reid));
53 /* Get a line of input. */
54 if (!unformat_user (input, unformat_line_input, line_input))
57 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
59 if (unformat (line_input, "del"))
61 else if (unformat (line_input, "add"))
63 else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
67 else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
71 else if (unformat (line_input, "vni %u", &vni))
73 gid_address_vni (leid) = vni;
74 gid_address_vni (reid) = vni;
77 else if (unformat (line_input, "vrf %u", &dp_table))
81 else if (unformat (line_input, "bd %u", &dp_table))
85 else if (unformat (line_input, "negative action %U",
86 unformat_negative_mapping_action, &action))
90 else if (unformat (line_input, "loc-pair %U %U w %d",
91 unformat_ip_address, &lloc,
92 unformat_ip_address, &rloc, &w))
94 ip_address_copy (&pair.lcl_loc, &lloc);
95 ip_address_copy (&pair.rmt_loc, &rloc);
98 vec_add1 (pairs, pair);
102 error = unformat_parse_error (line_input);
103 vlib_cli_output (vm, "parse error: '%U'",
104 format_unformat_error, line_input);
111 vlib_cli_output (vm, "remote eid must be set!");
115 if (gid_address_type (reid) != GID_ADDR_NSH && (!vni_set || !dp_table_set))
117 vlib_cli_output (vm, "vni and vrf/bd must be set!");
125 vlib_cli_output (vm, "no action set for negative tunnel!");
131 if (vec_len (pairs) == 0)
133 vlib_cli_output (vm, "expected ip4/ip6 locators");
140 /* if leid not set, make sure it's the same AFI like reid */
141 gid_address_type (leid) = gid_address_type (reid);
142 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
143 gid_address_ip_version (leid) = gid_address_ip_version (reid);
147 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
148 memset (a, 0, sizeof (a[0]));
151 a->is_negative = is_negative;
153 a->table_id = dp_table;
154 gid_address_copy (&a->lcl_eid, leid);
155 gid_address_copy (&a->rmt_eid, reid);
156 a->locator_pairs = pairs;
159 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
162 vlib_cli_output (vm, "failed to %s gpe tunnel!",
163 is_add ? "add" : "delete");
167 unformat_free (line_input);
173 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
175 .short_help = "gpe entry add/del vni <vni> vrf/bd <id> [leid <leid>]"
176 "reid <reid> [loc-pair <lloc> <rloc> w <weight>] "
177 "[negative action <action>]",
178 .function = lisp_gpe_add_del_fwd_entry_command_fn,
182 /** Check if LISP-GPE is enabled. */
184 vnet_lisp_gpe_enable_disable_status (void)
186 lisp_gpe_main_t *lgm = &lisp_gpe_main;
191 /** Enable/disable LISP-GPE. */
193 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
195 lisp_gpe_main_t *lgm = &lisp_gpe_main;
203 /* remove all entries */
204 vnet_lisp_gpe_fwd_entry_flush ();
206 /* disable all l3 ifaces */
207 lisp_gpe_tenant_flush ();
215 /** Set GPE encapsulation mode. */
217 vnet_gpe_set_encap_mode (gpe_encap_mode_t mode)
219 lisp_gpe_main_t *lgm = &lisp_gpe_main;
221 if (mode >= GPE_ENCAP_COUNT)
222 return VNET_API_ERROR_INVALID_GPE_MODE;
224 if (pool_elts (lgm->lisp_fwd_entry_pool) != 0)
225 return VNET_API_ERROR_LISP_GPE_ENTRIES_PRESENT;
227 lgm->encap_mode = mode;
231 /** CLI command to set GPE encap */
232 static clib_error_t *
233 gpe_set_encap_mode_command_fn (vlib_main_t * vm,
234 unformat_input_t * input,
235 vlib_cli_command_t * cmd)
237 unformat_input_t _line_input, *line_input = &_line_input;
238 gpe_encap_mode_t mode = GPE_ENCAP_COUNT;
241 /* Get a line of input. */
242 if (!unformat_user (input, unformat_line_input, line_input))
245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
247 if (unformat (line_input, "lisp"))
248 mode = GPE_ENCAP_LISP;
249 else if (unformat (line_input, "vxlan"))
250 mode = GPE_ENCAP_VXLAN;
253 return clib_error_return (0, "parse error: '%U'",
254 format_unformat_error, line_input);
257 rv = vnet_gpe_set_encap_mode (mode);
260 return clib_error_return (0,
261 "Error: invalid mode or GPE entries are present!");
268 VLIB_CLI_COMMAND (gpe_set_encap_mode_command, static) = {
270 .short_help = "gpe encap [lisp|vxlan]",
271 .function = gpe_set_encap_mode_command_fn,
275 /** Format GPE encap mode. */
277 format_vnet_gpe_encap_mode (u8 * s, va_list * args)
279 lisp_gpe_main_t *lgm = &lisp_gpe_main;
281 switch (lgm->encap_mode)
284 return format (s, "lisp");
285 case GPE_ENCAP_VXLAN:
286 return format (s, "vxlan");
293 /** CLI command to show GPE encap */
294 static clib_error_t *
295 gpe_show_encap_mode_command_fn (vlib_main_t * vm,
296 unformat_input_t * input,
297 vlib_cli_command_t * cmd)
299 vlib_cli_output (vm, "encap mode: %U", format_vnet_gpe_encap_mode);
304 VLIB_CLI_COMMAND (gpe_show_encap_mode_command, static) = {
305 .path = "show gpe encap",
306 .short_help = "show GPE encapulation mode",
307 .function = gpe_show_encap_mode_command_fn,
311 /** CLI command to enable/disable LISP-GPE. */
312 static clib_error_t *
313 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
314 unformat_input_t * input,
315 vlib_cli_command_t * cmd)
317 unformat_input_t _line_input, *line_input = &_line_input;
319 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
320 clib_error_t *error = NULL;
322 /* Get a line of input. */
323 if (!unformat_user (input, unformat_line_input, line_input))
326 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
328 if (unformat (line_input, "enable"))
330 else if (unformat (line_input, "disable"))
334 error = clib_error_return (0, "parse error: '%U'",
335 format_unformat_error, line_input);
340 error = vnet_lisp_gpe_enable_disable (a);
343 unformat_free (line_input);
349 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
351 .short_help = "gpe [enable|disable]",
352 .function = lisp_gpe_enable_disable_command_fn,
356 /** CLI command to show LISP-GPE interfaces. */
357 static clib_error_t *
358 lisp_show_iface_command_fn (vlib_main_t * vm,
359 unformat_input_t * input,
360 vlib_cli_command_t * cmd)
362 lisp_gpe_main_t *lgm = &lisp_gpe_main;
365 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
368 hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
369 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
373 if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
375 vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
377 hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
378 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
386 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
387 .path = "show gpe interface",
388 .short_help = "show gpe interface",
389 .function = lisp_show_iface_command_fn,
393 /** CLI command to show GPE fwd native route path. */
394 static clib_error_t *
395 gpe_show_native_fwd_rpath_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
399 lisp_gpe_main_t *lgm = &lisp_gpe_main;
400 fib_route_path_t *rpath;
402 if (vec_len (lgm->native_fwd_rpath[IP4]))
404 vec_foreach (rpath, lgm->native_fwd_rpath[IP4])
406 vlib_cli_output (vm, "nh: %U fib_index %u sw_if_index %u",
407 format_ip46_address, &rpath->frp_addr,
408 IP46_TYPE_IP4, rpath->frp_fib_index,
409 rpath->frp_sw_if_index);
412 if (vec_len (lgm->native_fwd_rpath[IP6]))
414 vec_foreach (rpath, lgm->native_fwd_rpath[IP6])
416 vlib_cli_output (vm, "nh: %U fib_index %u sw_if_index %u",
417 format_ip46_address, &rpath->frp_addr, IP46_TYPE_IP6,
418 rpath->frp_fib_index, rpath->frp_sw_if_index);
425 VLIB_CLI_COMMAND (gpe_show_native_fwd_rpath_command) = {
426 .path = "show gpe native-forward",
427 .short_help = "show gpe native-forward",
428 .function = gpe_show_native_fwd_rpath_command_fn,
433 gpe_update_native_fwd_path (u8 ip_version)
435 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
436 lisp_gpe_fwd_entry_t *lfe;
437 fib_prefix_t fib_prefix;
440 vec_foreach (lfei, lgm->native_fwd_lfes[ip_version])
442 lfe = pool_elt_at_index (lgm->lisp_fwd_entry_pool, lfei[0]);
443 ip_prefix_to_fib_prefix (&lfe->key->rmt.ippref, &fib_prefix);
444 fib_table_entry_update (lfe->eid_fib_index, &fib_prefix, FIB_SOURCE_LISP,
446 lgm->native_fwd_rpath[ip_version]);
451 vnet_gpe_add_del_native_fwd_rpath (vnet_gpe_native_fwd_rpath_args_t * a)
453 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
454 fib_route_path_t *rpath;
457 ip_version = a->rpath.frp_proto == DPO_PROTO_IP4 ? IP4 : IP6;
461 vec_add1 (lgm->native_fwd_rpath[ip_version], a->rpath);
465 vec_foreach (rpath, lgm->native_fwd_rpath[ip_version])
467 if (!fib_route_path_cmp (rpath, &a->rpath))
469 vec_del1 (lgm->native_fwd_rpath[ip_version],
470 rpath - lgm->native_fwd_rpath[ip_version]);
475 gpe_update_native_fwd_path (ip_version);
480 * CLI command to add action for native forward.
482 static clib_error_t *
483 gpe_native_forward_command_fn (vlib_main_t * vm, unformat_input_t * input,
484 vlib_cli_command_t * cmd)
486 vnet_main_t *vnm = vnet_get_main ();
487 unformat_input_t _line_input, *line_input = &_line_input;
489 fib_route_path_t rpath;
491 vnet_gpe_native_fwd_rpath_args_t _a, *a = &_a;
493 clib_error_t *error = 0;
495 /* Get a line of input. */
496 if (!unformat_user (input, unformat_line_input, line_input))
499 memset (&rpath, 0, sizeof (rpath));
501 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
503 if (unformat (line_input, "table %d", &table_id))
505 else if (unformat (line_input, "del"))
507 else if (unformat (line_input, "via %U %U",
508 unformat_ip4_address,
510 unformat_vnet_sw_interface, vnm,
511 &rpath.frp_sw_if_index))
513 rpath.frp_weight = 1;
514 rpath.frp_proto = DPO_PROTO_IP4;
516 else if (unformat (line_input, "via %U %U",
517 unformat_ip6_address,
519 unformat_vnet_sw_interface, vnm,
520 &rpath.frp_sw_if_index))
522 rpath.frp_weight = 1;
523 rpath.frp_proto = DPO_PROTO_IP6;
525 else if (unformat (line_input, "via %U",
526 unformat_ip4_address, &rpath.frp_addr.ip4))
528 rpath.frp_weight = 1;
529 rpath.frp_sw_if_index = ~0;
530 rpath.frp_proto = DPO_PROTO_IP4;
532 else if (unformat (line_input, "via %U",
533 unformat_ip6_address, &rpath.frp_addr.ip6))
535 rpath.frp_weight = 1;
536 rpath.frp_sw_if_index = ~0;
537 rpath.frp_proto = DPO_PROTO_IP6;
541 return clib_error_return (0, "parse error: '%U'",
542 format_unformat_error, line_input);
546 if ((u32) ~ 0 == table_id)
548 rpath.frp_fib_index = 0;
552 rpath.frp_fib_index =
553 fib_table_find (dpo_proto_to_fib (rpath.frp_proto), table_id);
554 if ((u32) ~ 0 == rpath.frp_fib_index)
556 error = clib_error_return (0, "Nonexistent table id %d", table_id);
564 rv = vnet_gpe_add_del_native_fwd_rpath (a);
567 return clib_error_return (0, "Error: couldn't add path!");
575 VLIB_CLI_COMMAND (gpe_native_forward_command) = {
576 .path = "gpe native-forward",
577 .short_help = "gpe native-forward [del] via <nh-ip-addr> [iface] "
579 .function = gpe_native_forward_command_fn,
583 /** Format LISP-GPE status. */
585 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
587 lisp_gpe_main_t *lgm = &lisp_gpe_main;
588 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
591 /** LISP-GPE init function. */
593 lisp_gpe_init (vlib_main_t * vm)
595 lisp_gpe_main_t *lgm = &lisp_gpe_main;
596 clib_error_t *error = 0;
598 if ((error = vlib_call_init_function (vm, ip_main_init)))
601 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
604 lgm->vnet_main = vnet_get_main ();
606 lgm->im4 = &ip4_main;
607 lgm->im6 = &ip6_main;
608 lgm->lm4 = &ip4_main.lookup_main;
609 lgm->lm6 = &ip6_main.lookup_main;
610 lgm->encap_mode = GPE_ENCAP_LISP;
612 lgm->lisp_gpe_fwd_entries =
613 hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
615 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
616 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
617 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
618 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
620 lgm->lisp_stats_index_by_key =
621 hash_create_mem (0, sizeof (lisp_stats_key_t), sizeof (uword));
622 memset (&lgm->counters, 0, sizeof (lgm->counters));
623 lgm->counters.name = "LISP counters";
629 vnet_gpe_get_encap_mode (void)
631 lisp_gpe_main_t *lgm = &lisp_gpe_main;
632 return lgm->encap_mode;
635 static clib_error_t *
636 lisp_gpe_test_send_nsh_packet (u8 * file_name)
640 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
642 clib_error_t *error = 0;
645 return clib_error_create ("no pcap file specified!");
647 memset (&pm, 0, sizeof (pm));
648 pm.file_name = (char *) file_name;
649 error = pcap_read (&pm);
654 if (vlib_buffer_alloc (lgm->vlib_main, &bi, 1) != 1)
655 return clib_error_create ("cannot allocate memory!");
657 b = vlib_get_buffer (lgm->vlib_main, bi);
658 tunnel_lookup_t *nsh_ifaces = &lgm->nsh_ifaces;
660 vnet_hw_interface_t *hi;
662 hip = hash_get (nsh_ifaces->hw_if_index_by_dp_table, 0);
664 return clib_error_create ("The NSH 0 interface doesn't exist");
666 hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
668 vnet_buffer (b)->sw_if_index[VLIB_TX] = hi->sw_if_index;
669 u8 *p = vlib_buffer_put_uninit (b, vec_len (pm.packets_read[0]));
670 clib_memcpy (p, pm.packets_read[0], vec_len (pm.packets_read[0]));
671 vlib_buffer_pull (b, sizeof (ethernet_header_t));
673 vlib_node_t *n = vlib_get_node_by_name (lgm->vlib_main,
674 (u8 *) "interface-tx");
675 f = vlib_get_frame_to_node (lgm->vlib_main, n->index);
676 u32 *to_next = vlib_frame_vector_args (f);
679 vlib_put_frame_to_node (lgm->vlib_main, n->index, f);
684 static clib_error_t *
685 lisp_test_nsh_command_fn (vlib_main_t * vm, unformat_input_t * input,
686 vlib_cli_command_t * cmd)
688 clib_error_t *error = 0;
691 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
693 if (unformat (input, "pcap %v", &file_name))
695 error = lisp_gpe_test_send_nsh_packet (file_name);
700 error = clib_error_create ("unknown input `%U'",
701 format_unformat_error, input);
711 VLIB_CLI_COMMAND (lisp_test_nsh_command, static) = {
712 .path = "test one nsh",
713 .short_help = "test gpe nsh pcap <path-to-pcap-file>",
714 .function = lisp_test_nsh_command_fn,
718 VLIB_INIT_FUNCTION (lisp_gpe_init);
721 * fd.io coding-style-patch-verification: ON
724 * eval: (c-set-style "gnu")