blob: e78d45c902049c51248534073f4729e64094893f [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Florin Corase127a7e2016-02-18 22:20:01 +01002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
Florin Corasff0bf132016-09-05 19:30:35 +020015/**
16 * @file
17 * @brief Common utility functions for IPv4, IPv6 and L2 LISP-GPE tunnels.
18 *
19 */
Florin Corase127a7e2016-02-18 22:20:01 +010020
Ed Warnickecb9cada2015-12-08 15:45:58 -070021#include <vnet/lisp-gpe/lisp_gpe.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010022#include <vnet/lisp-gpe/lisp_gpe_fwd_entry.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010023#include <vnet/lisp-gpe/lisp_gpe_adjacency.h>
Neale Ranns5e575b12016-10-03 09:40:25 +010024#include <vnet/lisp-gpe/lisp_gpe_tenant.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070025
Florin Corasff0bf132016-09-05 19:30:35 +020026/** LISP-GPE global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -070027lisp_gpe_main_t lisp_gpe_main;
28
Florin Coras1a1adc72016-07-22 01:45:30 +020029
Florin Corasff0bf132016-09-05 19:30:35 +020030/** CLI command to add/del forwarding entry. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070031static clib_error_t *
Florin Corase127a7e2016-02-18 22:20:01 +010032lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
Florin Coras220beac2016-08-16 23:04:00 +020033 unformat_input_t * input,
34 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070035{
Florin Coras220beac2016-08-16 23:04:00 +020036 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070037 u8 is_add = 1;
Florin Corasbb5c22f2016-08-02 02:31:03 +020038 ip_address_t lloc, rloc;
Florin Coras220beac2016-08-16 23:04:00 +020039 clib_error_t *error = 0;
40 gid_address_t _reid, *reid = &_reid, _leid, *leid = &_leid;
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010041 u8 reid_set = 0, leid_set = 0, is_negative = 0, dp_table_set = 0,
42 vni_set = 0;
43 u32 vni = 0, dp_table = 0, action = ~0, w;
Florin Coras220beac2016-08-16 23:04:00 +020044 locator_pair_t pair, *pairs = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +020045 int rv;
Florin Corase127a7e2016-02-18 22:20:01 +010046
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010047 memset (leid, 0, sizeof (*leid));
48 memset (reid, 0, sizeof (*reid));
49
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 /* Get a line of input. */
Florin Coras220beac2016-08-16 23:04:00 +020051 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070052 return 0;
53
Florin Corase127a7e2016-02-18 22:20:01 +010054 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
55 {
56 if (unformat (line_input, "del"))
Florin Coras220beac2016-08-16 23:04:00 +020057 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +010058 else if (unformat (line_input, "add"))
Florin Coras220beac2016-08-16 23:04:00 +020059 is_add = 1;
60 else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
61 {
62 leid_set = 1;
63 }
64 else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
65 {
66 reid_set = 1;
67 }
Florin Coras03c4f992016-07-19 15:27:58 +020068 else if (unformat (line_input, "vni %u", &vni))
Florin Coras220beac2016-08-16 23:04:00 +020069 {
70 gid_address_vni (leid) = vni;
71 gid_address_vni (reid) = vni;
72 vni_set = 1;
73 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010074 else if (unformat (line_input, "vrf %u", &dp_table))
Florin Coras220beac2016-08-16 23:04:00 +020075 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010076 dp_table_set = 1;
Florin Coras220beac2016-08-16 23:04:00 +020077 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010078 else if (unformat (line_input, "bd %u", &dp_table))
Neale Ranns5e575b12016-10-03 09:40:25 +010079 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010080 dp_table_set = 1;
Neale Ranns5e575b12016-10-03 09:40:25 +010081 }
Florin Coras03c4f992016-07-19 15:27:58 +020082 else if (unformat (line_input, "negative action %U",
Florin Coras220beac2016-08-16 23:04:00 +020083 unformat_negative_mapping_action, &action))
84 {
85 is_negative = 1;
86 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010087 else if (unformat (line_input, "loc-pair %U %U w %d",
Florin Coras220beac2016-08-16 23:04:00 +020088 unformat_ip_address, &lloc,
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010089 unformat_ip_address, &rloc, &w))
Florin Coras220beac2016-08-16 23:04:00 +020090 {
91 pair.lcl_loc = lloc;
92 pair.rmt_loc = rloc;
Florin Coras220beac2016-08-16 23:04:00 +020093 pair.weight = w;
Florin Coras42e480d2017-01-16 00:57:02 -080094 pair.priority = 0;
Florin Coras220beac2016-08-16 23:04:00 +020095 vec_add1 (pairs, pair);
96 }
Florin Corase127a7e2016-02-18 22:20:01 +010097 else
Florin Coras220beac2016-08-16 23:04:00 +020098 {
99 error = unformat_parse_error (line_input);
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100100 vlib_cli_output (vm, "parse error: '%U'",
101 format_unformat_error, line_input);
Florin Coras220beac2016-08-16 23:04:00 +0200102 goto done;
103 }
Florin Corase127a7e2016-02-18 22:20:01 +0100104 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100106 if (!vni_set || !dp_table_set)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100108 vlib_cli_output (vm, "vni and vrf/bd must be set!");
Florin Corase127a7e2016-02-18 22:20:01 +0100109 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110 }
111
Florin Coras03c4f992016-07-19 15:27:58 +0200112 if (!reid_set)
Florin Corase127a7e2016-02-18 22:20:01 +0100113 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100114 vlib_cli_output (vm, "remote eid must be set!");
Florin Corase127a7e2016-02-18 22:20:01 +0100115 goto done;
116 }
117
Florin Coras03c4f992016-07-19 15:27:58 +0200118 if (is_negative)
Florin Corase127a7e2016-02-18 22:20:01 +0100119 {
Florin Coras03c4f992016-07-19 15:27:58 +0200120 if (~0 == action)
Florin Coras220beac2016-08-16 23:04:00 +0200121 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100122 vlib_cli_output (vm, "no action set for negative tunnel!");
Florin Coras220beac2016-08-16 23:04:00 +0200123 goto done;
124 }
Florin Coras03c4f992016-07-19 15:27:58 +0200125 }
126 else
127 {
Florin Corasbb5c22f2016-08-02 02:31:03 +0200128 if (vec_len (pairs) == 0)
Florin Coras220beac2016-08-16 23:04:00 +0200129 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100130 vlib_cli_output (vm, "expected ip4/ip6 locators");
Florin Coras220beac2016-08-16 23:04:00 +0200131 goto done;
132 }
Florin Corase127a7e2016-02-18 22:20:01 +0100133 }
134
Florin Coras03c4f992016-07-19 15:27:58 +0200135 if (!leid_set)
136 {
137 /* if leid not set, make sure it's the same AFI like reid */
Florin Coras220beac2016-08-16 23:04:00 +0200138 gid_address_type (leid) = gid_address_type (reid);
Florin Coras03c4f992016-07-19 15:27:58 +0200139 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
Florin Coras220beac2016-08-16 23:04:00 +0200140 gid_address_ip_version (leid) = gid_address_ip_version (reid);
Florin Coras03c4f992016-07-19 15:27:58 +0200141 }
142
143 /* add fwd entry */
Florin Coras220beac2016-08-16 23:04:00 +0200144 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
145 memset (a, 0, sizeof (a[0]));
Florin Coras03c4f992016-07-19 15:27:58 +0200146
147 a->is_add = is_add;
Florin Coras82bf7cd2016-09-26 18:59:44 +0300148 a->is_negative = is_negative;
Florin Coras03c4f992016-07-19 15:27:58 +0200149 a->vni = vni;
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100150 a->table_id = dp_table;
Florin Coras220beac2016-08-16 23:04:00 +0200151 gid_address_copy (&a->lcl_eid, leid);
152 gid_address_copy (&a->rmt_eid, reid);
Florin Corasbb5c22f2016-08-02 02:31:03 +0200153 a->locator_pairs = pairs;
Florin Coras03c4f992016-07-19 15:27:58 +0200154
155 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
156 if (0 != rv)
157 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100158 vlib_cli_output (vm, "failed to %s gpe tunnel!",
159 is_add ? "add" : "delete");
Florin Coras03c4f992016-07-19 15:27:58 +0200160 }
161
Florin Coras220beac2016-08-16 23:04:00 +0200162done:
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100163 unformat_free (line_input);
Florin Coras220beac2016-08-16 23:04:00 +0200164 vec_free (pairs);
Florin Corase127a7e2016-02-18 22:20:01 +0100165 return error;
166}
167
Florin Coras220beac2016-08-16 23:04:00 +0200168/* *INDENT-OFF* */
Florin Corased09a052016-05-06 14:22:40 +0200169VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100170 .path = "lisp gpe entry",
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100171 .short_help = "lisp gpe entry add/del vni <vni> vrf/bd <id> [leid <leid>]"
172 "reid <reid> [loc-pair <lloc> <rloc> w <weight>] "
Florin Corasff0bf132016-09-05 19:30:35 +0200173 "[negative action <action>]",
Florin Corase127a7e2016-02-18 22:20:01 +0100174 .function = lisp_gpe_add_del_fwd_entry_command_fn,
175};
Florin Coras220beac2016-08-16 23:04:00 +0200176/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100177
Florin Corasff0bf132016-09-05 19:30:35 +0200178/** Check if LISP-GPE is enabled. */
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200179u8
Florin Coras220beac2016-08-16 23:04:00 +0200180vnet_lisp_gpe_enable_disable_status (void)
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200181{
Florin Coras220beac2016-08-16 23:04:00 +0200182 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200183
184 return lgm->is_en;
185}
186
Florin Corasff0bf132016-09-05 19:30:35 +0200187/** Enable/disable LISP-GPE. */
Florin Coras577c3552016-04-21 00:45:40 +0200188clib_error_t *
189vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +0100190{
Florin Coras220beac2016-08-16 23:04:00 +0200191 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Florin Corase127a7e2016-02-18 22:20:01 +0100192
Florin Coras577c3552016-04-21 00:45:40 +0200193 if (a->is_en)
Florin Corase127a7e2016-02-18 22:20:01 +0100194 {
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200195 lgm->is_en = 1;
Florin Corase127a7e2016-02-18 22:20:01 +0100196 }
197 else
198 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100199 /* remove all entries */
Neale Ranns5e575b12016-10-03 09:40:25 +0100200 vnet_lisp_gpe_fwd_entry_flush ();
Florin Coras577c3552016-04-21 00:45:40 +0200201
Florin Coras1a1adc72016-07-22 01:45:30 +0200202 /* disable all l3 ifaces */
Neale Ranns5e575b12016-10-03 09:40:25 +0100203 lisp_gpe_tenant_flush ();
Florin Coras220beac2016-08-16 23:04:00 +0200204
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200205 lgm->is_en = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100206 }
Florin Coras577c3552016-04-21 00:45:40 +0200207
208 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100209}
210
Florin Corasff0bf132016-09-05 19:30:35 +0200211/** CLI command to enable/disable LISP-GPE. */
Florin Corase127a7e2016-02-18 22:20:01 +0100212static clib_error_t *
Florin Coras220beac2016-08-16 23:04:00 +0200213lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
214 unformat_input_t * input,
215 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +0100216{
Florin Coras220beac2016-08-16 23:04:00 +0200217 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras577c3552016-04-21 00:45:40 +0200218 u8 is_en = 1;
Florin Coras220beac2016-08-16 23:04:00 +0200219 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Florin Corase127a7e2016-02-18 22:20:01 +0100220
221 /* Get a line of input. */
Florin Coras220beac2016-08-16 23:04:00 +0200222 if (!unformat_user (input, unformat_line_input, line_input))
Florin Corase127a7e2016-02-18 22:20:01 +0100223 return 0;
224
225 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
226 {
Florin Coras577c3552016-04-21 00:45:40 +0200227 if (unformat (line_input, "enable"))
Florin Coras220beac2016-08-16 23:04:00 +0200228 is_en = 1;
Florin Coras577c3552016-04-21 00:45:40 +0200229 else if (unformat (line_input, "disable"))
Florin Coras220beac2016-08-16 23:04:00 +0200230 is_en = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100231 else
Florin Coras220beac2016-08-16 23:04:00 +0200232 {
233 return clib_error_return (0, "parse error: '%U'",
234 format_unformat_error, line_input);
235 }
Florin Corase127a7e2016-02-18 22:20:01 +0100236 }
Florin Coras577c3552016-04-21 00:45:40 +0200237 a->is_en = is_en;
238 return vnet_lisp_gpe_enable_disable (a);
Florin Corase127a7e2016-02-18 22:20:01 +0100239}
240
Florin Coras220beac2016-08-16 23:04:00 +0200241/* *INDENT-OFF* */
Florin Coras577c3552016-04-21 00:45:40 +0200242VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
243 .path = "lisp gpe",
244 .short_help = "lisp gpe [enable|disable]",
245 .function = lisp_gpe_enable_disable_command_fn,
Florin Corase127a7e2016-02-18 22:20:01 +0100246};
Florin Coras220beac2016-08-16 23:04:00 +0200247/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100248
Florin Corasff0bf132016-09-05 19:30:35 +0200249/** CLI command to show LISP-GPE interfaces. */
Filip Tehlar53f09e32016-05-19 14:25:44 +0200250static clib_error_t *
251lisp_show_iface_command_fn (vlib_main_t * vm,
Florin Coras220beac2016-08-16 23:04:00 +0200252 unformat_input_t * input,
253 vlib_cli_command_t * cmd)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200254{
Florin Coras220beac2016-08-16 23:04:00 +0200255 lisp_gpe_main_t *lgm = &lisp_gpe_main;
256 hash_pair_t *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200257
258 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
Florin Coras220beac2016-08-16 23:04:00 +0200259
260 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200261 hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
Filip Tehlar53f09e32016-05-19 14:25:44 +0200262 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
263 }));
Florin Coras220beac2016-08-16 23:04:00 +0200264 /* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200265
266 if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
267 {
268 vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
Florin Coras220beac2016-08-16 23:04:00 +0200269 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200270 hash_foreach_pair (p, lgm->l2_ifaces.hw_if_index_by_dp_table, ({
271 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
272 }));
Florin Coras220beac2016-08-16 23:04:00 +0200273 /* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200274 }
Filip Tehlar53f09e32016-05-19 14:25:44 +0200275 return 0;
276}
277
Florin Coras220beac2016-08-16 23:04:00 +0200278/* *INDENT-OFF* */
Filip Tehlar53f09e32016-05-19 14:25:44 +0200279VLIB_CLI_COMMAND (lisp_show_iface_command) = {
280 .path = "show lisp gpe interface",
281 .short_help = "show lisp gpe interface",
282 .function = lisp_show_iface_command_fn,
283};
Florin Coras220beac2016-08-16 23:04:00 +0200284/* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200285
Florin Corasff0bf132016-09-05 19:30:35 +0200286/** Format LISP-GPE status. */
Florin Coras1a1adc72016-07-22 01:45:30 +0200287u8 *
288format_vnet_lisp_gpe_status (u8 * s, va_list * args)
289{
Florin Coras220beac2016-08-16 23:04:00 +0200290 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Florin Coras1a1adc72016-07-22 01:45:30 +0200291 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
292}
293
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100294
Florin Corasff0bf132016-09-05 19:30:35 +0200295/** LISP-GPE init function. */
Florin Corase127a7e2016-02-18 22:20:01 +0100296clib_error_t *
Florin Coras220beac2016-08-16 23:04:00 +0200297lisp_gpe_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +0100298{
Florin Coras220beac2016-08-16 23:04:00 +0200299 lisp_gpe_main_t *lgm = &lisp_gpe_main;
300 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100301
302 if ((error = vlib_call_init_function (vm, ip_main_init)))
303 return error;
304
305 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
306 return error;
307
Florin Coras220beac2016-08-16 23:04:00 +0200308 lgm->vnet_main = vnet_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +0100309 lgm->vlib_main = vm;
310 lgm->im4 = &ip4_main;
Florin Coras02655bd2016-04-26 00:17:24 +0200311 lgm->im6 = &ip6_main;
312 lgm->lm4 = &ip4_main.lookup_main;
313 lgm->lm6 = &ip6_main.lookup_main;
Florin Coras577c3552016-04-21 00:45:40 +0200314
Neale Ranns5e575b12016-10-03 09:40:25 +0100315 lgm->lisp_gpe_fwd_entries =
316 hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
Florin Coras1a1adc72016-07-22 01:45:30 +0200317
Florin Coras220beac2016-08-16 23:04:00 +0200318 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
319 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200320 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
Florin Coras220beac2016-08-16 23:04:00 +0200321 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 return 0;
323}
324
Florin Coras220beac2016-08-16 23:04:00 +0200325VLIB_INIT_FUNCTION (lisp_gpe_init);
326
327/*
328 * fd.io coding-style-patch-verification: ON
329 *
330 * Local Variables:
331 * eval: (c-set-style "gnu")
332 * End:
333 */