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.
16 #include <vnet/lisp-gpe/lisp_gpe.h>
18 lisp_gpe_main_t lisp_gpe_main;
21 lisp_gpe_rewrite (lisp_gpe_tunnel_t * t)
24 lisp_gpe_header_t * lisp0;
27 if (ip_addr_version(&t->src) == IP4)
30 ip4_udp_lisp_gpe_header_t * h0;
33 vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
35 h0 = (ip4_udp_lisp_gpe_header_t *) rw;
37 /* Fixed portion of the (outer) ip4 header */
39 ip0->ip_version_and_header_length = 0x45;
41 ip0->protocol = IP_PROTOCOL_UDP;
43 /* we fix up the ip4 header length and checksum after-the-fact */
44 ip_address_copy_addr(&ip0->src_address, &t->src);
45 ip_address_copy_addr(&ip0->dst_address, &t->dst);
46 ip0->checksum = ip4_header_checksum (ip0);
48 /* UDP header, randomize src port on something, maybe? */
49 h0->udp.src_port = clib_host_to_net_u16 (4341);
50 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
58 ip6_udp_lisp_gpe_header_t * h0;
61 vec_validate_aligned(rw, len - 1, CLIB_CACHE_LINE_BYTES);
63 h0 = (ip6_udp_lisp_gpe_header_t *) rw;
65 /* Fixed portion of the (outer) ip6 header */
67 ip0->ip_version_traffic_class_and_flow_label =
68 clib_host_to_net_u32 (0x6 << 28);
70 ip0->protocol = IP_PROTOCOL_UDP;
72 /* we fix up the ip6 header length after-the-fact */
73 ip_address_copy_addr(&ip0->src_address, &t->src);
74 ip_address_copy_addr(&ip0->dst_address, &t->dst);
76 /* UDP header, randomize src port on something, maybe? */
77 h0->udp.src_port = clib_host_to_net_u16 (4341);
78 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_lisp_gpe);
84 lisp0->flags = t->flags;
85 lisp0->ver_res = t->ver_res;
87 lisp0->next_protocol = t->next_protocol;
88 lisp0->iid = clib_host_to_net_u32 (t->vni);
94 #define foreach_copy_field \
101 add_del_ip_tunnel (vnet_lisp_gpe_add_del_fwd_entry_args_t *a,
104 lisp_gpe_main_t * lgm = &lisp_gpe_main;
105 lisp_gpe_tunnel_t *t = 0;
108 lisp_gpe_tunnel_key_t key;
110 /* prepare tunnel key */
111 memset(&key, 0, sizeof(key));
112 ip_prefix_copy(&key.eid, &gid_address_ippref(&a->deid));
113 ip_address_copy(&key.dst_loc, &a->dlocator);
114 key.iid = clib_host_to_net_u32 (a->vni);
116 p = mhash_get (&lgm->lisp_gpe_tunnel_by_key, &key);
120 /* adding a tunnel: tunnel must not already exist */
122 return VNET_API_ERROR_INVALID_VALUE;
124 if (a->decap_next_index >= LISP_GPE_INPUT_N_NEXT)
125 return VNET_API_ERROR_INVALID_DECAP_NEXT;
127 pool_get_aligned (lgm->tunnels, t, CLIB_CACHE_LINE_BYTES);
128 memset (t, 0, sizeof (*t));
130 /* copy from arg structure */
131 #define _(x) t->x = a->x;
135 ip_address_copy(&t->src, &a->slocator);
136 ip_address_copy(&t->dst, &a->dlocator);
138 t->flags |= LISP_GPE_FLAGS_P;
139 t->next_protocol = ip_prefix_version(&key.eid) == IP4 ?
140 LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
142 rv = lisp_gpe_rewrite (t);
146 pool_put(lgm->tunnels, t);
150 mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
152 /* return tunnel index */
154 tun_index_res[0] = t - lgm->tunnels;
158 /* deleting a tunnel: tunnel must exist */
161 clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
163 return VNET_API_ERROR_NO_SUCH_ENTRY;
166 t = pool_elt_at_index(lgm->tunnels, p[0]);
168 mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
170 vec_free(t->rewrite);
171 pool_put(lgm->tunnels, t);
178 add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
179 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
182 /* setup adjacency for eid */
183 memset (&adj, 0, sizeof(adj));
185 adj.explicit_fib_index = ~0;
187 ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
188 ip_prefix_t * spref = &gid_address_ippref(&a->seid);
193 /* TODO update timers? */
195 /* TODO check if route/next-hop for eid exists in fib and add
196 * more specific for the eid with the next-hop found */
197 case SEND_MAP_REQUEST:
198 /* insert tunnel that always sends map-request */
199 adj.rewrite_header.sw_if_index = ~0;
200 adj.lookup_next_index = (u32) (ip_prefix_version(dpref) == IP4) ?
201 LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP:
202 LGPE_IP6_LOOKUP_NEXT_LISP_CP_LOOKUP;
203 /* add/delete route for prefix */
204 return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
207 /* for drop fwd entries, just add route, no need to add encap tunnel */
208 adj.lookup_next_index = (u32) (ip_prefix_version(dpref) == IP4 ?
209 LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP);
211 /* add/delete route for prefix */
212 return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
220 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
223 lisp_gpe_main_t * lgm = &lisp_gpe_main;
224 ip_adjacency_t adj, * adjp;
225 u32 adj_index, rv, tun_index = ~0;
226 ip_prefix_t * dpref, * spref;
227 uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
230 /* treat negative fwd entries separately */
232 return add_del_negative_fwd_entry (lgm, a);
234 dpref = &gid_address_ippref(&a->deid);
235 spref = &gid_address_ippref(&a->seid);
236 ip_ver = ip_prefix_version(dpref);
238 /* add/del tunnel to tunnels pool and prepares rewrite */
239 rv = add_del_ip_tunnel (a, &tun_index);
243 /* setup adjacency for eid */
244 memset (&adj, 0, sizeof(adj));
247 /* fill in lookup_next_index with a 'legal' value to avoid problems */
248 adj.lookup_next_index = (ip_ver == IP4) ?
249 lgm->ip4_lookup_next_lgpe_ip4_lookup :
250 lgm->ip6_lookup_next_lgpe_ip6_lookup;
254 /* send packets that hit this adj to lisp-gpe interface output node in
256 lnip = ip_ver == IP4 ?
257 lgm->lgpe_ip4_lookup_next_index_by_table_id :
258 lgm->lgpe_ip6_lookup_next_index_by_table_id;
259 lookup_next_index = hash_get(lnip, a->table_id);
260 lgpe_sw_if_index = hash_get(lgm->lisp_gpe_hw_if_index_by_table_id,
263 /* the assumption is that the interface must've been created before
264 * programming the dp */
265 ASSERT(lookup_next_index != 0);
266 ASSERT(lgpe_sw_if_index != 0);
268 /* hijack explicit fib index to store lisp interface node index */
269 adj.explicit_fib_index = lookup_next_index[0];
270 adj.rewrite_header.node_index = tun_index;
271 adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
274 /* add/delete route for prefix */
275 rv = ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
278 /* check that everything worked */
279 if (CLIB_DEBUG && a->is_add)
281 adj_index = ip_sd_fib_get_route (lgm, dpref, spref, a->table_id);
282 ASSERT(adj_index != 0);
284 adjp = ip_get_adjacency ((ip_ver == IP4) ? lgm->lm4 : lgm->lm6,
288 ASSERT(adjp->rewrite_header.node_index == tun_index);
294 static clib_error_t *
295 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
296 unformat_input_t * input,
297 vlib_cli_command_t * cmd)
299 unformat_input_t _line_input, * line_input = &_line_input;
301 ip_address_t slocator, dlocator, *slocators = 0, *dlocators = 0;
303 gid_address_t * eids = 0, eid;
304 clib_error_t * error = 0;
307 prefp = &gid_address_ippref(&eid);
309 /* Get a line of input. */
310 if (! unformat_user (input, unformat_line_input, line_input))
313 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
315 if (unformat (line_input, "del"))
317 else if (unformat (line_input, "add"))
319 else if (unformat (line_input, "eid %U slocator %U dlocator %U",
320 unformat_ip_prefix, prefp,
321 unformat_ip_address, &slocator,
322 unformat_ip_address, &dlocator))
324 vec_add1 (eids, eid);
325 vec_add1 (slocators, slocator);
326 vec_add1 (dlocators, dlocator);
330 error = unformat_parse_error (line_input);
334 unformat_free (line_input);
336 if (vec_len (eids) + vec_len (slocators) == 0)
338 error = clib_error_return (0, "expected ip4/ip6 eids/locators.");
342 if (vec_len (eids) != vec_len (slocators))
344 error = clib_error_return (0, "number of eids not equal to that of "
349 for (i = 0; i < vec_len(eids); i++)
351 vnet_lisp_gpe_add_del_fwd_entry_args_t a;
352 memset (&a, 0, sizeof(a));
356 a.slocator = slocators[i];
357 a.dlocator = dlocators[i];
358 vnet_lisp_gpe_add_del_fwd_entry (&a, 0);
368 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
369 .path = "lisp gpe maptunnel",
370 .short_help = "lisp gpe maptunnel eid <eid> sloc <src-locator> "
371 "dloc <dst-locator> [del]",
372 .function = lisp_gpe_add_del_fwd_entry_command_fn,
376 format_decap_next (u8 * s, va_list * args)
378 u32 next_index = va_arg (*args, u32);
382 case LISP_GPE_INPUT_NEXT_DROP:
383 return format (s, "drop");
384 case LISP_GPE_INPUT_NEXT_IP4_INPUT:
385 return format (s, "ip4");
386 case LISP_GPE_INPUT_NEXT_IP6_INPUT:
387 return format (s, "ip6");
389 return format (s, "unknown %d", next_index);
395 format_lisp_gpe_tunnel (u8 * s, va_list * args)
397 lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
398 lisp_gpe_main_t * lgm = &lisp_gpe_main;
401 "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
403 format_ip_address, &t->src,
404 format_ip_address, &t->dst,
408 s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
409 s = format (s, "lisp ver %d ", (t->ver_res>>6));
411 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
412 foreach_lisp_gpe_flag_bit;
415 s = format (s, "next_protocol %d ver_res %x res %x\n",
416 t->next_protocol, t->ver_res, t->res);
418 s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
422 static clib_error_t *
423 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
424 unformat_input_t * input,
425 vlib_cli_command_t * cmd)
427 lisp_gpe_main_t * lgm = &lisp_gpe_main;
428 lisp_gpe_tunnel_t * t;
430 if (pool_elts (lgm->tunnels) == 0)
431 vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
433 pool_foreach (t, lgm->tunnels,
435 vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
441 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
442 .path = "show lisp gpe tunnel",
443 .function = show_lisp_gpe_tunnel_command_fn,
447 vnet_lisp_gpe_enable_disable_status(void)
449 lisp_gpe_main_t * lgm = &lisp_gpe_main;
455 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
457 lisp_gpe_main_t * lgm = &lisp_gpe_main;
458 vnet_main_t * vnm = lgm->vnet_main;
462 /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
463 if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
465 lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
466 vnm->vlib_main, ip4_lookup_node.index,
467 lgpe_ip4_lookup_node.index);
469 /* add lgpe_ip6_lookup as possible next_node for ip6 lookup */
470 if (lgm->ip6_lookup_next_lgpe_ip6_lookup == ~0)
472 lgm->ip6_lookup_next_lgpe_ip6_lookup = vlib_node_add_next (
473 vnm->vlib_main, ip6_lookup_node.index,
474 lgpe_ip6_lookup_node.index);
478 /* ask cp to re-add ifaces and defaults */
485 CLIB_UNUSED(uword * val);
487 u32 * table_ids = 0, * table_id;
488 lisp_gpe_tunnel_key_t * tunnels = 0, * tunnel;
489 vnet_lisp_gpe_add_del_fwd_entry_args_t _at, * at = &_at;
490 vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
492 /* remove all tunnels */
493 mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
494 vec_add1(tunnels, tunnel[0]);
497 vec_foreach(tunnel, tunnels) {
498 memset(at, 0, sizeof(at[0]));
500 gid_address_type(&at->deid) = GID_ADDR_IP_PREFIX;
501 ip_prefix_copy(&gid_address_ippref(&at->deid), &tunnel->eid);
502 ip_address_copy(&at->dlocator, &tunnel->dst_loc);
503 vnet_lisp_gpe_add_del_fwd_entry (at, 0);
507 /* disable all ifaces */
508 hash_foreach_pair(p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
509 vec_add1(table_ids, p->key);
512 vec_foreach(table_id, table_ids) {
514 ai->table_id = table_id[0];
516 /* disables interface and removes defaults */
517 vnet_lisp_gpe_add_del_iface(ai, 0);
526 static clib_error_t *
527 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
528 vlib_cli_command_t * cmd)
530 unformat_input_t _line_input, * line_input = &_line_input;
532 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
534 /* Get a line of input. */
535 if (! unformat_user (input, unformat_line_input, line_input))
538 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
540 if (unformat (line_input, "enable"))
542 else if (unformat (line_input, "disable"))
546 return clib_error_return (0, "parse error: '%U'",
547 format_unformat_error, line_input);
551 return vnet_lisp_gpe_enable_disable (a);
554 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
556 .short_help = "lisp gpe [enable|disable]",
557 .function = lisp_gpe_enable_disable_command_fn,
560 static clib_error_t *
561 lisp_show_iface_command_fn (vlib_main_t * vm,
562 unformat_input_t * input,
563 vlib_cli_command_t * cmd)
565 lisp_gpe_main_t * lgm = &lisp_gpe_main;
568 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
569 hash_foreach_pair (p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
570 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
575 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
576 .path = "show lisp gpe interface",
577 .short_help = "show lisp gpe interface",
578 .function = lisp_show_iface_command_fn,
582 lisp_gpe_init (vlib_main_t *vm)
584 lisp_gpe_main_t * lgm = &lisp_gpe_main;
585 clib_error_t * error = 0;
587 if ((error = vlib_call_init_function (vm, ip_main_init)))
590 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
593 lgm->vnet_main = vnet_get_main();
595 lgm->im4 = &ip4_main;
596 lgm->im6 = &ip6_main;
597 lgm->lm4 = &ip4_main.lookup_main;
598 lgm->lm6 = &ip6_main.lookup_main;
599 lgm->ip4_lookup_next_lgpe_ip4_lookup = ~0;
600 lgm->ip6_lookup_next_lgpe_ip6_lookup = ~0;
602 mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
603 sizeof(lisp_gpe_tunnel_key_t));
605 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
606 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */);
607 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
608 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */);
613 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
615 lisp_gpe_main_t * lgm = &lisp_gpe_main;
616 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
619 VLIB_INIT_FUNCTION(lisp_gpe_init);