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>
26 /** LISP-GPE global state */
27 lisp_gpe_main_t lisp_gpe_main;
30 /** CLI command to add/del forwarding entry. */
32 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
33 unformat_input_t * input,
34 vlib_cli_command_t * cmd)
36 unformat_input_t _line_input, *line_input = &_line_input;
38 ip_address_t lloc, rloc;
39 clib_error_t *error = 0;
40 gid_address_t _reid, *reid = &_reid, _leid, *leid = &_leid;
41 u8 reid_set = 0, leid_set = 0, is_negative = 0, vrf_set = 0, vni_set = 0;
42 u32 vni, vrf, action = ~0, p, w;
43 locator_pair_t pair, *pairs = 0;
46 /* Get a line of input. */
47 if (!unformat_user (input, unformat_line_input, line_input))
50 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
52 if (unformat (line_input, "del"))
54 else if (unformat (line_input, "add"))
56 else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
60 else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
64 else if (unformat (line_input, "vni %u", &vni))
66 gid_address_vni (leid) = vni;
67 gid_address_vni (reid) = vni;
70 else if (unformat (line_input, "vrf %u", &vrf))
74 else if (unformat (line_input, "bd %u", &vrf))
78 else if (unformat (line_input, "negative action %U",
79 unformat_negative_mapping_action, &action))
83 else if (unformat (line_input, "loc-pair %U %U p %d w %d",
84 unformat_ip_address, &lloc,
85 unformat_ip_address, &rloc, &p, &w))
91 vec_add1 (pairs, pair);
95 error = unformat_parse_error (line_input);
99 unformat_free (line_input);
101 if (!vni_set || !vrf_set)
103 error = clib_error_return (0, "vni and vrf must be set!");
109 error = clib_error_return (0, "remote eid must be set!");
117 error = clib_error_return (0, "no action set for negative tunnel!");
123 if (vec_len (pairs) == 0)
125 error = clib_error_return (0, "expected ip4/ip6 locators.");
132 /* if leid not set, make sure it's the same AFI like reid */
133 gid_address_type (leid) = gid_address_type (reid);
134 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
135 gid_address_ip_version (leid) = gid_address_ip_version (reid);
139 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
140 memset (a, 0, sizeof (a[0]));
143 a->is_negative = is_negative;
146 gid_address_copy (&a->lcl_eid, leid);
147 gid_address_copy (&a->rmt_eid, reid);
148 a->locator_pairs = pairs;
150 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
153 error = clib_error_return (0, "failed to %s gpe tunnel!",
154 is_add ? "add" : "delete");
163 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
164 .path = "lisp gpe entry",
165 .short_help = "lisp gpe entry add/del vni <vni> vrf <vrf> [leid <leid>]"
166 "reid <reid> [loc-pair <lloc> <rloc> p <priority> w <weight>] "
167 "[negative action <action>]",
168 .function = lisp_gpe_add_del_fwd_entry_command_fn,
172 /** Check if LISP-GPE is enabled. */
174 vnet_lisp_gpe_enable_disable_status (void)
176 lisp_gpe_main_t *lgm = &lisp_gpe_main;
181 /** Enable/disable LISP-GPE. */
183 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
185 lisp_gpe_main_t *lgm = &lisp_gpe_main;
193 /* remove all entries */
194 vnet_lisp_gpe_fwd_entry_flush ();
196 /* disable all l3 ifaces */
197 lisp_gpe_tenant_flush ();
205 /** CLI command to enable/disable LISP-GPE. */
206 static clib_error_t *
207 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
208 unformat_input_t * input,
209 vlib_cli_command_t * cmd)
211 unformat_input_t _line_input, *line_input = &_line_input;
213 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
215 /* Get a line of input. */
216 if (!unformat_user (input, unformat_line_input, line_input))
219 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
221 if (unformat (line_input, "enable"))
223 else if (unformat (line_input, "disable"))
227 return clib_error_return (0, "parse error: '%U'",
228 format_unformat_error, line_input);
232 return vnet_lisp_gpe_enable_disable (a);
236 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
238 .short_help = "lisp gpe [enable|disable]",
239 .function = lisp_gpe_enable_disable_command_fn,
243 /** CLI command to show LISP-GPE interfaces. */
244 static clib_error_t *
245 lisp_show_iface_command_fn (vlib_main_t * vm,
246 unformat_input_t * input,
247 vlib_cli_command_t * cmd)
249 lisp_gpe_main_t *lgm = &lisp_gpe_main;
252 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
255 hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
256 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
260 if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
262 vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
264 hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
265 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
273 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
274 .path = "show lisp gpe interface",
275 .short_help = "show lisp gpe interface",
276 .function = lisp_show_iface_command_fn,
280 /** Format LISP-GPE status. */
282 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
284 lisp_gpe_main_t *lgm = &lisp_gpe_main;
285 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
289 /** LISP-GPE init function. */
291 lisp_gpe_init (vlib_main_t * vm)
293 lisp_gpe_main_t *lgm = &lisp_gpe_main;
294 clib_error_t *error = 0;
296 if ((error = vlib_call_init_function (vm, ip_main_init)))
299 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
302 lgm->vnet_main = vnet_get_main ();
304 lgm->im4 = &ip4_main;
305 lgm->im6 = &ip6_main;
306 lgm->lm4 = &ip4_main.lookup_main;
307 lgm->lm6 = &ip6_main.lookup_main;
309 lgm->lisp_gpe_fwd_entries =
310 hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
312 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
313 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
314 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
315 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
319 VLIB_INIT_FUNCTION (lisp_gpe_init);
322 * fd.io coding-style-patch-verification: ON
325 * eval: (c-set-style "gnu")