blob: c7b3d887cfa80187d27d72e19c4d7db447820386 [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>
Florin Corasa4e63e52017-06-07 21:50:57 -070025#include <vnet/fib/fib_path_list.h>
26#include <vnet/fib/fib_table.h>
27#include <vnet/fib/fib_internal.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
Florin Corasff0bf132016-09-05 19:30:35 +020029/** LISP-GPE global state */
Ed Warnickecb9cada2015-12-08 15:45:58 -070030lisp_gpe_main_t lisp_gpe_main;
31
Florin Coras1a1adc72016-07-22 01:45:30 +020032
Florin Corasff0bf132016-09-05 19:30:35 +020033/** CLI command to add/del forwarding entry. */
Ed Warnickecb9cada2015-12-08 15:45:58 -070034static clib_error_t *
Florin Corase127a7e2016-02-18 22:20:01 +010035lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
Florin Coras220beac2016-08-16 23:04:00 +020036 unformat_input_t * input,
37 vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -070038{
Florin Coras220beac2016-08-16 23:04:00 +020039 unformat_input_t _line_input, *line_input = &_line_input;
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 u8 is_add = 1;
Florin Corasbb5c22f2016-08-02 02:31:03 +020041 ip_address_t lloc, rloc;
Florin Coras220beac2016-08-16 23:04:00 +020042 clib_error_t *error = 0;
43 gid_address_t _reid, *reid = &_reid, _leid, *leid = &_leid;
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010044 u8 reid_set = 0, leid_set = 0, is_negative = 0, dp_table_set = 0,
45 vni_set = 0;
46 u32 vni = 0, dp_table = 0, action = ~0, w;
Florin Coras220beac2016-08-16 23:04:00 +020047 locator_pair_t pair, *pairs = 0;
Andrej Kozemcak8ebb2a12016-06-07 12:25:20 +020048 int rv;
Florin Corase127a7e2016-02-18 22:20:01 +010049
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010050 memset (leid, 0, sizeof (*leid));
51 memset (reid, 0, sizeof (*reid));
52
Ed Warnickecb9cada2015-12-08 15:45:58 -070053 /* Get a line of input. */
Florin Coras220beac2016-08-16 23:04:00 +020054 if (!unformat_user (input, unformat_line_input, line_input))
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 return 0;
56
Florin Corase127a7e2016-02-18 22:20:01 +010057 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
58 {
59 if (unformat (line_input, "del"))
Florin Coras220beac2016-08-16 23:04:00 +020060 is_add = 0;
Florin Corase127a7e2016-02-18 22:20:01 +010061 else if (unformat (line_input, "add"))
Florin Coras220beac2016-08-16 23:04:00 +020062 is_add = 1;
63 else if (unformat (line_input, "leid %U", unformat_gid_address, leid))
64 {
65 leid_set = 1;
66 }
67 else if (unformat (line_input, "reid %U", unformat_gid_address, reid))
68 {
69 reid_set = 1;
70 }
Florin Coras03c4f992016-07-19 15:27:58 +020071 else if (unformat (line_input, "vni %u", &vni))
Florin Coras220beac2016-08-16 23:04:00 +020072 {
73 gid_address_vni (leid) = vni;
74 gid_address_vni (reid) = vni;
75 vni_set = 1;
76 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010077 else if (unformat (line_input, "vrf %u", &dp_table))
Florin Coras220beac2016-08-16 23:04:00 +020078 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010079 dp_table_set = 1;
Florin Coras220beac2016-08-16 23:04:00 +020080 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010081 else if (unformat (line_input, "bd %u", &dp_table))
Neale Ranns5e575b12016-10-03 09:40:25 +010082 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010083 dp_table_set = 1;
Neale Ranns5e575b12016-10-03 09:40:25 +010084 }
Florin Coras03c4f992016-07-19 15:27:58 +020085 else if (unformat (line_input, "negative action %U",
Florin Coras220beac2016-08-16 23:04:00 +020086 unformat_negative_mapping_action, &action))
87 {
88 is_negative = 1;
89 }
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010090 else if (unformat (line_input, "loc-pair %U %U w %d",
Florin Coras220beac2016-08-16 23:04:00 +020091 unformat_ip_address, &lloc,
Filip Tehlarc3af7bf2017-01-13 14:13:09 +010092 unformat_ip_address, &rloc, &w))
Florin Coras220beac2016-08-16 23:04:00 +020093 {
Florin Corasaf8c8e52017-08-17 15:57:20 -070094 ip_address_copy (&pair.lcl_loc, &lloc);
95 ip_address_copy (&pair.rmt_loc, &rloc);
Florin Coras220beac2016-08-16 23:04:00 +020096 pair.weight = w;
Florin Coras42e480d2017-01-16 00:57:02 -080097 pair.priority = 0;
Florin Coras220beac2016-08-16 23:04:00 +020098 vec_add1 (pairs, pair);
99 }
Florin Corase127a7e2016-02-18 22:20:01 +0100100 else
Florin Coras220beac2016-08-16 23:04:00 +0200101 {
102 error = unformat_parse_error (line_input);
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100103 vlib_cli_output (vm, "parse error: '%U'",
104 format_unformat_error, line_input);
Florin Coras220beac2016-08-16 23:04:00 +0200105 goto done;
106 }
Florin Corase127a7e2016-02-18 22:20:01 +0100107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
Florin Coras03c4f992016-07-19 15:27:58 +0200109 if (!reid_set)
Florin Corase127a7e2016-02-18 22:20:01 +0100110 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100111 vlib_cli_output (vm, "remote eid must be set!");
Florin Corase127a7e2016-02-18 22:20:01 +0100112 goto done;
113 }
114
Florin Corasb69111e2017-02-13 23:55:27 -0800115 if (gid_address_type (reid) != GID_ADDR_NSH && (!vni_set || !dp_table_set))
116 {
117 vlib_cli_output (vm, "vni and vrf/bd must be set!");
118 goto done;
119 }
120
Florin Coras03c4f992016-07-19 15:27:58 +0200121 if (is_negative)
Florin Corase127a7e2016-02-18 22:20:01 +0100122 {
Florin Coras03c4f992016-07-19 15:27:58 +0200123 if (~0 == action)
Florin Coras220beac2016-08-16 23:04:00 +0200124 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100125 vlib_cli_output (vm, "no action set for negative tunnel!");
Florin Coras220beac2016-08-16 23:04:00 +0200126 goto done;
127 }
Florin Coras03c4f992016-07-19 15:27:58 +0200128 }
129 else
130 {
Florin Corasbb5c22f2016-08-02 02:31:03 +0200131 if (vec_len (pairs) == 0)
Florin Coras220beac2016-08-16 23:04:00 +0200132 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100133 vlib_cli_output (vm, "expected ip4/ip6 locators");
Florin Coras220beac2016-08-16 23:04:00 +0200134 goto done;
135 }
Florin Corase127a7e2016-02-18 22:20:01 +0100136 }
137
Florin Coras03c4f992016-07-19 15:27:58 +0200138 if (!leid_set)
139 {
140 /* if leid not set, make sure it's the same AFI like reid */
Florin Coras220beac2016-08-16 23:04:00 +0200141 gid_address_type (leid) = gid_address_type (reid);
Florin Coras03c4f992016-07-19 15:27:58 +0200142 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
Florin Coras220beac2016-08-16 23:04:00 +0200143 gid_address_ip_version (leid) = gid_address_ip_version (reid);
Florin Coras03c4f992016-07-19 15:27:58 +0200144 }
145
146 /* add fwd entry */
Florin Coras220beac2016-08-16 23:04:00 +0200147 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, *a = &_a;
148 memset (a, 0, sizeof (a[0]));
Florin Coras03c4f992016-07-19 15:27:58 +0200149
150 a->is_add = is_add;
Florin Coras82bf7cd2016-09-26 18:59:44 +0300151 a->is_negative = is_negative;
Florin Coras03c4f992016-07-19 15:27:58 +0200152 a->vni = vni;
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100153 a->table_id = dp_table;
Florin Coras220beac2016-08-16 23:04:00 +0200154 gid_address_copy (&a->lcl_eid, leid);
155 gid_address_copy (&a->rmt_eid, reid);
Florin Corasbb5c22f2016-08-02 02:31:03 +0200156 a->locator_pairs = pairs;
Florin Corasce1b4c72017-01-26 14:25:34 -0800157 a->action = action;
Florin Coras03c4f992016-07-19 15:27:58 +0200158
159 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
160 if (0 != rv)
161 {
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100162 vlib_cli_output (vm, "failed to %s gpe tunnel!",
163 is_add ? "add" : "delete");
Florin Coras03c4f992016-07-19 15:27:58 +0200164 }
165
Florin Coras220beac2016-08-16 23:04:00 +0200166done:
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100167 unformat_free (line_input);
Florin Coras220beac2016-08-16 23:04:00 +0200168 vec_free (pairs);
Florin Corase127a7e2016-02-18 22:20:01 +0100169 return error;
170}
171
Florin Coras220beac2016-08-16 23:04:00 +0200172/* *INDENT-OFF* */
Florin Corased09a052016-05-06 14:22:40 +0200173VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
Filip Tehlar82786c42017-02-20 15:20:37 +0100174 .path = "gpe entry",
175 .short_help = "gpe entry add/del vni <vni> vrf/bd <id> [leid <leid>]"
Filip Tehlarc3af7bf2017-01-13 14:13:09 +0100176 "reid <reid> [loc-pair <lloc> <rloc> w <weight>] "
Florin Corasff0bf132016-09-05 19:30:35 +0200177 "[negative action <action>]",
Florin Corase127a7e2016-02-18 22:20:01 +0100178 .function = lisp_gpe_add_del_fwd_entry_command_fn,
179};
Florin Coras220beac2016-08-16 23:04:00 +0200180/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100181
Florin Corasff0bf132016-09-05 19:30:35 +0200182/** Check if LISP-GPE is enabled. */
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200183u8
Florin Coras220beac2016-08-16 23:04:00 +0200184vnet_lisp_gpe_enable_disable_status (void)
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200185{
Florin Coras220beac2016-08-16 23:04:00 +0200186 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200187
188 return lgm->is_en;
189}
190
Florin Corasff0bf132016-09-05 19:30:35 +0200191/** Enable/disable LISP-GPE. */
Florin Coras577c3552016-04-21 00:45:40 +0200192clib_error_t *
193vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
Florin Corase127a7e2016-02-18 22:20:01 +0100194{
Florin Coras220beac2016-08-16 23:04:00 +0200195 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Florin Corase127a7e2016-02-18 22:20:01 +0100196
Florin Coras577c3552016-04-21 00:45:40 +0200197 if (a->is_en)
Florin Corase127a7e2016-02-18 22:20:01 +0100198 {
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200199 lgm->is_en = 1;
Florin Corase127a7e2016-02-18 22:20:01 +0100200 }
201 else
202 {
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100203 /* remove all entries */
Neale Ranns5e575b12016-10-03 09:40:25 +0100204 vnet_lisp_gpe_fwd_entry_flush ();
Florin Coras577c3552016-04-21 00:45:40 +0200205
Florin Coras1a1adc72016-07-22 01:45:30 +0200206 /* disable all l3 ifaces */
Neale Ranns5e575b12016-10-03 09:40:25 +0100207 lisp_gpe_tenant_flush ();
Florin Coras220beac2016-08-16 23:04:00 +0200208
Andrej Kozemcaka9edd852016-05-02 12:14:33 +0200209 lgm->is_en = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100210 }
Florin Coras577c3552016-04-21 00:45:40 +0200211
212 return 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100213}
214
Filip Tehlar3e7b56932017-02-21 18:28:34 +0100215/** Set GPE encapsulation mode. */
216int
217vnet_gpe_set_encap_mode (gpe_encap_mode_t mode)
218{
219 lisp_gpe_main_t *lgm = &lisp_gpe_main;
220
221 if (mode >= GPE_ENCAP_COUNT)
222 return VNET_API_ERROR_INVALID_GPE_MODE;
223
224 if (pool_elts (lgm->lisp_fwd_entry_pool) != 0)
225 return VNET_API_ERROR_LISP_GPE_ENTRIES_PRESENT;
226
227 lgm->encap_mode = mode;
228 return 0;
229}
230
231/** CLI command to set GPE encap */
232static clib_error_t *
233gpe_set_encap_mode_command_fn (vlib_main_t * vm,
234 unformat_input_t * input,
235 vlib_cli_command_t * cmd)
236{
237 unformat_input_t _line_input, *line_input = &_line_input;
238 gpe_encap_mode_t mode = GPE_ENCAP_COUNT;
239 vnet_api_error_t rv;
240
241 /* Get a line of input. */
242 if (!unformat_user (input, unformat_line_input, line_input))
243 return 0;
244
245 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
246 {
247 if (unformat (line_input, "lisp"))
248 mode = GPE_ENCAP_LISP;
249 else if (unformat (line_input, "vxlan"))
250 mode = GPE_ENCAP_VXLAN;
251 else
252 {
253 return clib_error_return (0, "parse error: '%U'",
254 format_unformat_error, line_input);
255 }
256 }
257 rv = vnet_gpe_set_encap_mode (mode);
258 if (rv)
259 {
260 return clib_error_return (0,
261 "Error: invalid mode or GPE entries are present!");
262 }
263
264 return 0;
265}
266
267/* *INDENT-OFF* */
268VLIB_CLI_COMMAND (gpe_set_encap_mode_command, static) = {
269 .path = "gpe encap",
270 .short_help = "gpe encap [lisp|vxlan]",
271 .function = gpe_set_encap_mode_command_fn,
272};
273/* *INDENT-ON* */
274
275/** Format GPE encap mode. */
276u8 *
277format_vnet_gpe_encap_mode (u8 * s, va_list * args)
278{
279 lisp_gpe_main_t *lgm = &lisp_gpe_main;
280
281 switch (lgm->encap_mode)
282 {
283 case GPE_ENCAP_LISP:
284 return format (s, "lisp");
285 case GPE_ENCAP_VXLAN:
286 return format (s, "vxlan");
287 default:
288 return 0;
289 }
290 return 0;
291}
292
293/** CLI command to show GPE encap */
294static clib_error_t *
295gpe_show_encap_mode_command_fn (vlib_main_t * vm,
296 unformat_input_t * input,
297 vlib_cli_command_t * cmd)
298{
299 vlib_cli_output (vm, "encap mode: %U", format_vnet_gpe_encap_mode);
300 return 0;
301}
302
303/* *INDENT-OFF* */
304VLIB_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,
308};
309/* *INDENT-ON* */
310
Florin Corasff0bf132016-09-05 19:30:35 +0200311/** CLI command to enable/disable LISP-GPE. */
Florin Corase127a7e2016-02-18 22:20:01 +0100312static clib_error_t *
Florin Coras220beac2016-08-16 23:04:00 +0200313lisp_gpe_enable_disable_command_fn (vlib_main_t * vm,
314 unformat_input_t * input,
315 vlib_cli_command_t * cmd)
Florin Corase127a7e2016-02-18 22:20:01 +0100316{
Florin Coras220beac2016-08-16 23:04:00 +0200317 unformat_input_t _line_input, *line_input = &_line_input;
Florin Coras577c3552016-04-21 00:45:40 +0200318 u8 is_en = 1;
Florin Coras220beac2016-08-16 23:04:00 +0200319 vnet_lisp_gpe_enable_disable_args_t _a, *a = &_a;
Billy McFalla9a20e72017-02-15 11:39:12 -0500320 clib_error_t *error = NULL;
Florin Corase127a7e2016-02-18 22:20:01 +0100321
322 /* Get a line of input. */
Florin Coras220beac2016-08-16 23:04:00 +0200323 if (!unformat_user (input, unformat_line_input, line_input))
Florin Coras9fa15812017-12-01 04:54:06 -0800324 return clib_error_return (0, "expected enable | disable");
Florin Corase127a7e2016-02-18 22:20:01 +0100325
326 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
327 {
Florin Coras577c3552016-04-21 00:45:40 +0200328 if (unformat (line_input, "enable"))
Florin Coras220beac2016-08-16 23:04:00 +0200329 is_en = 1;
Florin Coras577c3552016-04-21 00:45:40 +0200330 else if (unformat (line_input, "disable"))
Florin Coras220beac2016-08-16 23:04:00 +0200331 is_en = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100332 else
Florin Coras220beac2016-08-16 23:04:00 +0200333 {
Billy McFalla9a20e72017-02-15 11:39:12 -0500334 error = clib_error_return (0, "parse error: '%U'",
335 format_unformat_error, line_input);
336 goto done;
Florin Coras220beac2016-08-16 23:04:00 +0200337 }
Florin Corase127a7e2016-02-18 22:20:01 +0100338 }
Florin Coras577c3552016-04-21 00:45:40 +0200339 a->is_en = is_en;
Billy McFalla9a20e72017-02-15 11:39:12 -0500340 error = vnet_lisp_gpe_enable_disable (a);
341
342done:
343 unformat_free (line_input);
344
345 return error;
Florin Corase127a7e2016-02-18 22:20:01 +0100346}
347
Florin Coras220beac2016-08-16 23:04:00 +0200348/* *INDENT-OFF* */
Florin Coras577c3552016-04-21 00:45:40 +0200349VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
Filip Tehlar82786c42017-02-20 15:20:37 +0100350 .path = "gpe",
351 .short_help = "gpe [enable|disable]",
Florin Coras577c3552016-04-21 00:45:40 +0200352 .function = lisp_gpe_enable_disable_command_fn,
Florin Corase127a7e2016-02-18 22:20:01 +0100353};
Florin Coras220beac2016-08-16 23:04:00 +0200354/* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100355
Florin Corasff0bf132016-09-05 19:30:35 +0200356/** CLI command to show LISP-GPE interfaces. */
Filip Tehlar53f09e32016-05-19 14:25:44 +0200357static clib_error_t *
358lisp_show_iface_command_fn (vlib_main_t * vm,
Florin Coras220beac2016-08-16 23:04:00 +0200359 unformat_input_t * input,
360 vlib_cli_command_t * cmd)
Filip Tehlar53f09e32016-05-19 14:25:44 +0200361{
Florin Coras220beac2016-08-16 23:04:00 +0200362 lisp_gpe_main_t *lgm = &lisp_gpe_main;
363 hash_pair_t *p;
Filip Tehlar53f09e32016-05-19 14:25:44 +0200364
365 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
Florin Coras220beac2016-08-16 23:04:00 +0200366
367 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200368 hash_foreach_pair (p, lgm->l3_ifaces.hw_if_index_by_dp_table, ({
Filip Tehlar53f09e32016-05-19 14:25:44 +0200369 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
370 }));
Florin Coras220beac2016-08-16 23:04:00 +0200371 /* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200372
373 if (0 != lgm->l2_ifaces.hw_if_index_by_dp_table)
374 {
375 vlib_cli_output (vm, "%=10s%=12s", "bd_id", "hw_if_index");
Florin Coras220beac2016-08-16 23:04:00 +0200376 /* *INDENT-OFF* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200377 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]);
379 }));
Florin Coras220beac2016-08-16 23:04:00 +0200380 /* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200381 }
Filip Tehlar53f09e32016-05-19 14:25:44 +0200382 return 0;
383}
384
Florin Coras220beac2016-08-16 23:04:00 +0200385/* *INDENT-OFF* */
Filip Tehlar53f09e32016-05-19 14:25:44 +0200386VLIB_CLI_COMMAND (lisp_show_iface_command) = {
Filip Tehlar82786c42017-02-20 15:20:37 +0100387 .path = "show gpe interface",
388 .short_help = "show gpe interface",
Filip Tehlar53f09e32016-05-19 14:25:44 +0200389 .function = lisp_show_iface_command_fn,
390};
Florin Coras220beac2016-08-16 23:04:00 +0200391/* *INDENT-ON* */
Florin Coras1a1adc72016-07-22 01:45:30 +0200392
Florin Corasa4e63e52017-06-07 21:50:57 -0700393/** CLI command to show GPE fwd native route path. */
394static clib_error_t *
395gpe_show_native_fwd_rpath_command_fn (vlib_main_t * vm,
396 unformat_input_t * input,
397 vlib_cli_command_t * cmd)
398{
399 lisp_gpe_main_t *lgm = &lisp_gpe_main;
400 fib_route_path_t *rpath;
401
402 if (vec_len (lgm->native_fwd_rpath[IP4]))
403 {
404 vec_foreach (rpath, lgm->native_fwd_rpath[IP4])
405 {
406 vlib_cli_output (vm, "nh: %U fib_index %u sw_if_index %u",
Filip Tehlarb4243aa2017-06-14 14:39:42 +0200407 format_ip46_address, &rpath->frp_addr,
408 IP46_TYPE_IP4, rpath->frp_fib_index,
409 rpath->frp_sw_if_index);
Florin Corasa4e63e52017-06-07 21:50:57 -0700410 }
411 }
412 if (vec_len (lgm->native_fwd_rpath[IP6]))
413 {
414 vec_foreach (rpath, lgm->native_fwd_rpath[IP6])
415 {
416 vlib_cli_output (vm, "nh: %U fib_index %u sw_if_index %u",
Filip Tehlarb4243aa2017-06-14 14:39:42 +0200417 format_ip46_address, &rpath->frp_addr, IP46_TYPE_IP6,
Florin Corasa4e63e52017-06-07 21:50:57 -0700418 rpath->frp_fib_index, rpath->frp_sw_if_index);
419 }
420 }
421 return 0;
422}
423
424/* *INDENT-OFF* */
425VLIB_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,
429};
430/* *INDENT-ON* */
431
432void
433gpe_update_native_fwd_path (u8 ip_version)
434{
435 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
436 lisp_gpe_fwd_entry_t *lfe;
437 fib_prefix_t fib_prefix;
438 u32 *lfei;
439
440 vec_foreach (lfei, lgm->native_fwd_lfes[ip_version])
441 {
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,
445 FIB_ENTRY_FLAG_NONE,
446 lgm->native_fwd_rpath[ip_version]);
447 }
448}
449
450int
451vnet_gpe_add_del_native_fwd_rpath (vnet_gpe_native_fwd_rpath_args_t * a)
452{
453 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
454 fib_route_path_t *rpath;
455 u8 ip_version;
456
Neale Rannsda78f952017-05-24 09:15:43 -0700457 ip_version = a->rpath.frp_proto == DPO_PROTO_IP4 ? IP4 : IP6;
Florin Corasa4e63e52017-06-07 21:50:57 -0700458
459 if (a->is_add)
460 {
461 vec_add1 (lgm->native_fwd_rpath[ip_version], a->rpath);
462 }
463 else
464 {
465 vec_foreach (rpath, lgm->native_fwd_rpath[ip_version])
466 {
467 if (!fib_route_path_cmp (rpath, &a->rpath))
468 {
469 vec_del1 (lgm->native_fwd_rpath[ip_version],
470 rpath - lgm->native_fwd_rpath[ip_version]);
471 break;
472 }
473 }
474 }
475 gpe_update_native_fwd_path (ip_version);
476 return 0;
477}
478
479/**
480 * CLI command to add action for native forward.
481 */
482static clib_error_t *
483gpe_native_forward_command_fn (vlib_main_t * vm, unformat_input_t * input,
484 vlib_cli_command_t * cmd)
485{
486 vnet_main_t *vnm = vnet_get_main ();
487 unformat_input_t _line_input, *line_input = &_line_input;
488 vnet_api_error_t rv;
489 fib_route_path_t rpath;
490 u32 table_id = ~0;
491 vnet_gpe_native_fwd_rpath_args_t _a, *a = &_a;
492 u8 is_add = 1;
493 clib_error_t *error = 0;
494
495 /* Get a line of input. */
496 if (!unformat_user (input, unformat_line_input, line_input))
497 return 0;
498
499 memset (&rpath, 0, sizeof (rpath));
500
501 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
502 {
503 if (unformat (line_input, "table %d", &table_id))
504 ;
505 else if (unformat (line_input, "del"))
506 is_add = 0;
507 else if (unformat (line_input, "via %U %U",
508 unformat_ip4_address,
509 &rpath.frp_addr.ip4,
510 unformat_vnet_sw_interface, vnm,
511 &rpath.frp_sw_if_index))
512 {
513 rpath.frp_weight = 1;
Neale Rannsda78f952017-05-24 09:15:43 -0700514 rpath.frp_proto = DPO_PROTO_IP4;
Florin Corasa4e63e52017-06-07 21:50:57 -0700515 }
516 else if (unformat (line_input, "via %U %U",
517 unformat_ip6_address,
518 &rpath.frp_addr.ip6,
519 unformat_vnet_sw_interface, vnm,
520 &rpath.frp_sw_if_index))
521 {
522 rpath.frp_weight = 1;
Neale Rannsda78f952017-05-24 09:15:43 -0700523 rpath.frp_proto = DPO_PROTO_IP6;
Florin Corasa4e63e52017-06-07 21:50:57 -0700524 }
525 else if (unformat (line_input, "via %U",
526 unformat_ip4_address, &rpath.frp_addr.ip4))
527 {
528 rpath.frp_weight = 1;
529 rpath.frp_sw_if_index = ~0;
Neale Rannsda78f952017-05-24 09:15:43 -0700530 rpath.frp_proto = DPO_PROTO_IP4;
Florin Corasa4e63e52017-06-07 21:50:57 -0700531 }
532 else if (unformat (line_input, "via %U",
533 unformat_ip6_address, &rpath.frp_addr.ip6))
534 {
535 rpath.frp_weight = 1;
536 rpath.frp_sw_if_index = ~0;
Neale Rannsda78f952017-05-24 09:15:43 -0700537 rpath.frp_proto = DPO_PROTO_IP6;
Florin Corasa4e63e52017-06-07 21:50:57 -0700538 }
539 else
540 {
541 return clib_error_return (0, "parse error: '%U'",
542 format_unformat_error, line_input);
543 }
544 }
545
546 if ((u32) ~ 0 == table_id)
547 {
548 rpath.frp_fib_index = 0;
549 }
550 else
551 {
Neale Rannsda78f952017-05-24 09:15:43 -0700552 rpath.frp_fib_index =
553 fib_table_find (dpo_proto_to_fib (rpath.frp_proto), table_id);
Florin Corasa4e63e52017-06-07 21:50:57 -0700554 if ((u32) ~ 0 == rpath.frp_fib_index)
555 {
556 error = clib_error_return (0, "Nonexistent table id %d", table_id);
557 goto done;
558 }
559 }
560
561 a->rpath = rpath;
562 a->is_add = is_add;
563
564 rv = vnet_gpe_add_del_native_fwd_rpath (a);
565 if (rv)
566 {
567 return clib_error_return (0, "Error: couldn't add path!");
568 }
569
570done:
571 return error;
572}
573
574/* *INDENT-OFF* */
575VLIB_CLI_COMMAND (gpe_native_forward_command) = {
576 .path = "gpe native-forward",
577 .short_help = "gpe native-forward [del] via <nh-ip-addr> [iface] "
578 "[table <table>]",
579 .function = gpe_native_forward_command_fn,
580};
581/* *INDENT-ON* */
582
Florin Corasff0bf132016-09-05 19:30:35 +0200583/** Format LISP-GPE status. */
Florin Coras1a1adc72016-07-22 01:45:30 +0200584u8 *
585format_vnet_lisp_gpe_status (u8 * s, va_list * args)
586{
Florin Coras220beac2016-08-16 23:04:00 +0200587 lisp_gpe_main_t *lgm = &lisp_gpe_main;
Florin Coras1a1adc72016-07-22 01:45:30 +0200588 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
589}
590
Florin Corasff0bf132016-09-05 19:30:35 +0200591/** LISP-GPE init function. */
Florin Corase127a7e2016-02-18 22:20:01 +0100592clib_error_t *
Florin Coras220beac2016-08-16 23:04:00 +0200593lisp_gpe_init (vlib_main_t * vm)
Florin Corase127a7e2016-02-18 22:20:01 +0100594{
Florin Coras220beac2016-08-16 23:04:00 +0200595 lisp_gpe_main_t *lgm = &lisp_gpe_main;
596 clib_error_t *error = 0;
Florin Corase127a7e2016-02-18 22:20:01 +0100597
598 if ((error = vlib_call_init_function (vm, ip_main_init)))
599 return error;
600
601 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
602 return error;
603
Florin Coras220beac2016-08-16 23:04:00 +0200604 lgm->vnet_main = vnet_get_main ();
Florin Corase127a7e2016-02-18 22:20:01 +0100605 lgm->vlib_main = vm;
606 lgm->im4 = &ip4_main;
Florin Coras02655bd2016-04-26 00:17:24 +0200607 lgm->im6 = &ip6_main;
608 lgm->lm4 = &ip4_main.lookup_main;
609 lgm->lm6 = &ip6_main.lookup_main;
Filip Tehlar3e7b56932017-02-21 18:28:34 +0100610 lgm->encap_mode = GPE_ENCAP_LISP;
Florin Coras577c3552016-04-21 00:45:40 +0200611
Neale Ranns5e575b12016-10-03 09:40:25 +0100612 lgm->lisp_gpe_fwd_entries =
613 hash_create_mem (0, sizeof (lisp_gpe_fwd_entry_key_t), sizeof (uword));
Florin Coras1a1adc72016-07-22 01:45:30 +0200614
Florin Coras220beac2016-08-16 23:04:00 +0200615 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
616 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */ );
Florin Coras02655bd2016-04-26 00:17:24 +0200617 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
Florin Coras220beac2016-08-16 23:04:00 +0200618 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */ );
Florin Coras263440e2017-02-22 23:38:08 -0800619
Filip Tehlar4868ff62017-03-09 16:48:39 +0100620 lgm->lisp_stats_index_by_key =
621 hash_create_mem (0, sizeof (lisp_stats_key_t), sizeof (uword));
Filip Tehlar21511912017-04-07 10:41:42 +0200622 memset (&lgm->counters, 0, sizeof (lgm->counters));
623 lgm->counters.name = "LISP counters";
624
Ed Warnickecb9cada2015-12-08 15:45:58 -0700625 return 0;
626}
627
Filip Tehlar3e7b56932017-02-21 18:28:34 +0100628gpe_encap_mode_t
629vnet_gpe_get_encap_mode (void)
630{
631 lisp_gpe_main_t *lgm = &lisp_gpe_main;
632 return lgm->encap_mode;
633}
634
Filip Tehlarf89d1852017-06-12 13:36:02 +0200635static clib_error_t *
636lisp_gpe_test_send_nsh_packet (u8 * file_name)
637{
638 vlib_frame_t *f;
639 vlib_buffer_t *b;
640 lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();
641 pcap_main_t pm;
642 clib_error_t *error = 0;
643
644 if (!file_name)
645 return clib_error_create ("no pcap file specified!");
646
647 memset (&pm, 0, sizeof (pm));
648 pm.file_name = (char *) file_name;
649 error = pcap_read (&pm);
650 if (error)
651 return error;
652
653 u32 bi;
654 if (vlib_buffer_alloc (lgm->vlib_main, &bi, 1) != 1)
655 return clib_error_create ("cannot allocate memory!");
656
657 b = vlib_get_buffer (lgm->vlib_main, bi);
658 tunnel_lookup_t *nsh_ifaces = &lgm->nsh_ifaces;
659 uword *hip;
660 vnet_hw_interface_t *hi;
661
662 hip = hash_get (nsh_ifaces->hw_if_index_by_dp_table, 0);
663 if (hip == 0)
664 return clib_error_create ("The NSH 0 interface doesn't exist");
665
666 hi = vnet_get_hw_interface (lgm->vnet_main, hip[0]);
667
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));
672
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);
677 to_next[0] = bi;
678 f->n_vectors = 1;
679 vlib_put_frame_to_node (lgm->vlib_main, n->index, f);
680
681 return error;
682}
683
684static clib_error_t *
685lisp_test_nsh_command_fn (vlib_main_t * vm, unformat_input_t * input,
686 vlib_cli_command_t * cmd)
687{
688 clib_error_t *error = 0;
689 u8 *file_name = 0;
690
691 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
692 {
693 if (unformat (input, "pcap %v", &file_name))
694 {
695 error = lisp_gpe_test_send_nsh_packet (file_name);
696 goto done;
697 }
698 else
699 {
700 error = clib_error_create ("unknown input `%U'",
701 format_unformat_error, input);
702 goto done;
703 }
704 }
705
706done:
707 return error;
708}
709
710/* *INDENT-OFF* */
711VLIB_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,
715};
716/* *INDENT-ON* */
717
Florin Coras220beac2016-08-16 23:04:00 +0200718VLIB_INIT_FUNCTION (lisp_gpe_init);
719
720/*
721 * fd.io coding-style-patch-verification: ON
722 *
723 * Local Variables:
724 * eval: (c-set-style "gnu")
725 * End:
726 */