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, dp_table_set = 0,
43 u32 vni = 0, dp_table = 0, action = ~0, w;
44 locator_pair_t pair, *pairs = 0;
47 memset (leid, 0, sizeof (*leid));
48 memset (reid, 0, sizeof (*reid));
50 /* Get a line of input. */
51 if (!unformat_user (input, unformat_line_input, line_input))
54 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
56 if (unformat (line_input, "del"))
58 else if (unformat (line_input, "add"))
60 else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
64 else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
68 else if (unformat (line_input, "vni %u", &vni))
70 gid_address_vni (leid) = vni;
71 gid_address_vni (reid) = vni;
74 else if (unformat (line_input, "vrf %u", &dp_table))
78 else if (unformat (line_input, "bd %u", &dp_table))
82 else if (unformat (line_input, "negative action %U",
83 unformat_negative_mapping_action, &action))
87 else if (unformat (line_input, "loc-pair %U %U w %d",
88 unformat_ip_address, &lloc,
89 unformat_ip_address, &rloc, &w))
95 vec_add1 (pairs, pair);
99 error = unformat_parse_error (line_input);
100 vlib_cli_output (vm, "parse error: '%U'",
101 format_unformat_error, line_input);
108 vlib_cli_output (vm, "remote eid must be set!");
112 if (gid_address_type (reid) != GID_ADDR_NSH && (!vni_set || !dp_table_set))
114 vlib_cli_output (vm, "vni and vrf/bd must be set!");
122 vlib_cli_output (vm, "no action set for negative tunnel!");
128 if (vec_len (pairs) == 0)
130 vlib_cli_output (vm, "expected ip4/ip6 locators");
137 /* if leid not set, make sure it's the same AFI like reid */
138 gid_address_type (leid) = gid_address_type (reid);
139 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
140 gid_address_ip_version (leid) = gid_address_ip_version (reid);
144 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
145 memset (a, 0, sizeof (a[0]));
148 a->is_negative = is_negative;
150 a->table_id = dp_table;
151 gid_address_copy (&a->lcl_eid, leid);
152 gid_address_copy (&a->rmt_eid, reid);
153 a->locator_pairs = pairs;
156 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
159 vlib_cli_output (vm, "failed to %s gpe tunnel!",
160 is_add ? "add" : "delete");
164 unformat_free (line_input);
170 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
172 .short_help = "gpe entry add/del vni <vni> vrf/bd <id> [leid <leid>]"
173 "reid <reid> [loc-pair <lloc> <rloc> w <weight>] "
174 "[negative action <action>]",
175 .function = lisp_gpe_add_del_fwd_entry_command_fn,
179 /** Check if LISP-GPE is enabled. */
181 vnet_lisp_gpe_enable_disable_status (void)
183 lisp_gpe_main_t *lgm = &lisp_gpe_main;
188 /** Enable/disable LISP-GPE. */
190 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
192 lisp_gpe_main_t *lgm = &lisp_gpe_main;
200 /* remove all entries */
201 vnet_lisp_gpe_fwd_entry_flush ();
203 /* disable all l3 ifaces */
204 lisp_gpe_tenant_flush ();
212 /** Set GPE encapsulation mode. */
214 vnet_gpe_set_encap_mode (gpe_encap_mode_t mode)
216 lisp_gpe_main_t *lgm = &lisp_gpe_main;
218 if (mode >= GPE_ENCAP_COUNT)
219 return VNET_API_ERROR_INVALID_GPE_MODE;
221 if (pool_elts (lgm->lisp_fwd_entry_pool) != 0)
222 return VNET_API_ERROR_LISP_GPE_ENTRIES_PRESENT;
224 lgm->encap_mode = mode;
228 /** CLI command to set GPE encap */
229 static clib_error_t *
230 gpe_set_encap_mode_command_fn (vlib_main_t * vm,
231 unformat_input_t * input,
232 vlib_cli_command_t * cmd)
234 unformat_input_t _line_input, *line_input = &_line_input;
235 gpe_encap_mode_t mode = GPE_ENCAP_COUNT;
238 /* Get a line of input. */
239 if (!unformat_user (input, unformat_line_input, line_input))
242 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
244 if (unformat (line_input, "lisp"))
245 mode = GPE_ENCAP_LISP;
246 else if (unformat (line_input, "vxlan"))
247 mode = GPE_ENCAP_VXLAN;
250 return clib_error_return (0, "parse error: '%U'",
251 format_unformat_error, line_input);
254 rv = vnet_gpe_set_encap_mode (mode);
257 return clib_error_return (0,
258 "Error: invalid mode or GPE entries are present!");
265 VLIB_CLI_COMMAND (gpe_set_encap_mode_command, static) = {
267 .short_help = "gpe encap [lisp|vxlan]",
268 .function = gpe_set_encap_mode_command_fn,
272 /** Format GPE encap mode. */
274 format_vnet_gpe_encap_mode (u8 * s, va_list * args)
276 lisp_gpe_main_t *lgm = &lisp_gpe_main;
278 switch (lgm->encap_mode)
281 return format (s, "lisp");
282 case GPE_ENCAP_VXLAN:
283 return format (s, "vxlan");
290 /** CLI command to show GPE encap */
291 static clib_error_t *
292 gpe_show_encap_mode_command_fn (vlib_main_t * vm,
293 unformat_input_t * input,
294 vlib_cli_command_t * cmd)
296 vlib_cli_output (vm, "encap mode: %U", format_vnet_gpe_encap_mode);
301 VLIB_CLI_COMMAND (gpe_show_encap_mode_command, static) = {
302 .path = "show gpe encap",
303 .short_help = "show GPE encapulation mode",
304 .function = gpe_show_encap_mode_command_fn,
308 /** CLI command to enable/disable LISP-GPE. */
309 static clib_error_t *
310 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
311 unformat_input_t * input,
312 vlib_cli_command_t * cmd)
314 unformat_input_t _line_input, *line_input = &_line_input;
316 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
317 clib_error_t *error = NULL;
319 /* Get a line of input. */
320 if (!unformat_user (input, unformat_line_input, line_input))
323 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
325 if (unformat (line_input, "enable"))
327 else if (unformat (line_input, "disable"))
331 error = clib_error_return (0, "parse error: '%U'",
332 format_unformat_error, line_input);
337 error = vnet_lisp_gpe_enable_disable (a);
340 unformat_free (line_input);
346 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
348 .short_help = "gpe [enable|disable]",
349 .function = lisp_gpe_enable_disable_command_fn,
353 /** CLI command to show LISP-GPE interfaces. */
354 static clib_error_t *
355 lisp_show_iface_command_fn (vlib_main_t * vm,
356 unformat_input_t * input,
357 vlib_cli_command_t * cmd)
359 lisp_gpe_main_t *lgm = &lisp_gpe_main;
362 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
365 hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
366 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
370 if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
372 vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
374 hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
375 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
383 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
384 .path = "show gpe interface",
385 .short_help = "show gpe interface",
386 .function = lisp_show_iface_command_fn,
390 /** Format LISP-GPE status. */
392 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
394 lisp_gpe_main_t *lgm = &lisp_gpe_main;
395 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
398 /** LISP-GPE init function. */
400 lisp_gpe_init (vlib_main_t * vm)
402 lisp_gpe_main_t *lgm = &lisp_gpe_main;
403 clib_error_t *error = 0;
405 if ((error = vlib_call_init_function (vm, ip_main_init)))
408 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
411 lgm->vnet_main = vnet_get_main ();
413 lgm->im4 = &ip4_main;
414 lgm->im6 = &ip6_main;
415 lgm->lm4 = &ip4_main.lookup_main;
416 lgm->lm6 = &ip6_main.lookup_main;
417 lgm->encap_mode = GPE_ENCAP_LISP;
419 lgm->lisp_gpe_fwd_entries =
420 hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
422 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
423 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
424 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
425 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
431 vnet_gpe_get_encap_mode (void)
433 lisp_gpe_main_t *lgm = &lisp_gpe_main;
434 return lgm->encap_mode;
437 VLIB_INIT_FUNCTION (lisp_gpe_init);
440 * fd.io coding-style-patch-verification: ON
443 * eval: (c-set-style "gnu")