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 /* if vni is non-default */
141 t->flags = LISP_GPE_FLAGS_I;
145 t->flags |= LISP_GPE_FLAGS_P;
146 t->next_protocol = ip_prefix_version(&key.eid) == IP4 ?
147 LISP_GPE_NEXT_PROTO_IP4 : LISP_GPE_NEXT_PROTO_IP6;
149 rv = lisp_gpe_rewrite (t);
153 pool_put(lgm->tunnels, t);
157 mhash_set(&lgm->lisp_gpe_tunnel_by_key, &key, t - lgm->tunnels, 0);
159 /* return tunnel index */
161 tun_index_res[0] = t - lgm->tunnels;
165 /* deleting a tunnel: tunnel must exist */
168 clib_warning("Tunnel for eid %U doesn't exist!", format_gid_address,
170 return VNET_API_ERROR_NO_SUCH_ENTRY;
173 t = pool_elt_at_index(lgm->tunnels, p[0]);
175 mhash_unset(&lgm->lisp_gpe_tunnel_by_key, &key, 0);
177 vec_free(t->rewrite);
178 pool_put(lgm->tunnels, t);
185 add_del_negative_fwd_entry (lisp_gpe_main_t * lgm,
186 vnet_lisp_gpe_add_del_fwd_entry_args_t * a)
189 ip_prefix_t * dpref = &gid_address_ippref(&a->deid);
190 ip_prefix_t * spref = &gid_address_ippref(&a->seid);
192 /* setup adjacency for eid */
193 memset (&adj, 0, sizeof(adj));
196 /* fill in 'legal' data to avoid issues */
197 adj.lookup_next_index = (ip_prefix_version(dpref) == IP4) ?
198 lgm->ip4_lookup_next_lgpe_ip4_lookup :
199 lgm->ip6_lookup_next_lgpe_ip6_lookup;
201 adj.rewrite_header.sw_if_index = ~0;
202 adj.rewrite_header.next_index = ~0;
207 /* TODO update timers? */
209 /* TODO check if route/next-hop for eid exists in fib and add
210 * more specific for the eid with the next-hop found */
211 case SEND_MAP_REQUEST:
212 /* insert tunnel that always sends map-request */
213 adj.explicit_fib_index = (ip_prefix_version(dpref) == IP4) ?
214 LGPE_IP4_LOOKUP_NEXT_LISP_CP_LOOKUP:
215 LGPE_IP6_LOOKUP_NEXT_LISP_CP_LOOKUP;
216 /* add/delete route for prefix */
217 return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
220 /* for drop fwd entries, just add route, no need to add encap tunnel */
221 adj.explicit_fib_index = (ip_prefix_version(dpref) == IP4 ?
222 LGPE_IP4_LOOKUP_NEXT_DROP : LGPE_IP6_LOOKUP_NEXT_DROP);
224 /* add/delete route for prefix */
225 return ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
233 vnet_lisp_gpe_add_del_fwd_entry (vnet_lisp_gpe_add_del_fwd_entry_args_t * a,
236 lisp_gpe_main_t * lgm = &lisp_gpe_main;
237 ip_adjacency_t adj, * adjp;
238 u32 adj_index, rv, tun_index = ~0;
239 ip_prefix_t * dpref, * spref;
240 uword * lookup_next_index, * lgpe_sw_if_index, * lnip;
243 if (vnet_lisp_gpe_enable_disable_status() == 0)
245 clib_warning ("LISP is disabled!");
246 return VNET_API_ERROR_LISP_DISABLED;
249 /* treat negative fwd entries separately */
251 return add_del_negative_fwd_entry (lgm, a);
253 dpref = &gid_address_ippref(&a->deid);
254 spref = &gid_address_ippref(&a->seid);
255 ip_ver = ip_prefix_version(dpref);
257 /* add/del tunnel to tunnels pool and prepares rewrite */
258 rv = add_del_ip_tunnel (a, &tun_index);
262 /* setup adjacency for eid */
263 memset (&adj, 0, sizeof(adj));
266 /* fill in lookup_next_index with a 'legal' value to avoid problems */
267 adj.lookup_next_index = (ip_ver == IP4) ?
268 lgm->ip4_lookup_next_lgpe_ip4_lookup :
269 lgm->ip6_lookup_next_lgpe_ip6_lookup;
273 /* send packets that hit this adj to lisp-gpe interface output node in
275 lnip = (ip_ver == IP4) ?
276 lgm->lgpe_ip4_lookup_next_index_by_table_id :
277 lgm->lgpe_ip6_lookup_next_index_by_table_id;
278 lookup_next_index = hash_get(lnip, a->table_id);
279 lgpe_sw_if_index = hash_get(lgm->tunnel_term_sw_if_index_by_vni,
282 /* the assumption is that the interface must've been created before
283 * programming the dp */
284 ASSERT(lookup_next_index != 0);
285 ASSERT(lgpe_sw_if_index != 0);
287 /* hijack explicit fib index to store lisp interface node index and
288 * if_address_index for the tunnel index */
289 adj.explicit_fib_index = lookup_next_index[0];
290 adj.if_address_index = tun_index;
291 adj.rewrite_header.sw_if_index = lgpe_sw_if_index[0];
294 /* add/delete route for prefix */
295 rv = ip_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
298 /* check that everything worked */
299 if (CLIB_DEBUG && a->is_add)
301 adj_index = ip_sd_fib_get_route (lgm, dpref, spref, a->table_id);
302 ASSERT(adj_index != 0);
304 adjp = ip_get_adjacency ((ip_ver == IP4) ? lgm->lm4 : lgm->lm6,
308 ASSERT(adjp->if_address_index == tun_index);
314 static clib_error_t *
315 lisp_gpe_add_del_fwd_entry_command_fn (vlib_main_t * vm,
316 unformat_input_t * input,
317 vlib_cli_command_t * cmd)
319 unformat_input_t _line_input, * line_input = &_line_input;
321 ip_address_t lloc, rloc, *llocs = 0, *rlocs = 0;
322 clib_error_t * error = 0;
323 gid_address_t _reid, * reid = &_reid, _leid, * leid = &_leid;
324 u8 reid_set = 0, leid_set = 0, is_negative = 0, vrf_set = 0, vni_set = 0;
325 u32 vni, vrf, action = ~0;
328 /* Get a line of input. */
329 if (! unformat_user (input, unformat_line_input, line_input))
332 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
334 if (unformat (line_input, "del"))
336 else if (unformat (line_input, "add"))
338 else if (unformat (line_input, "leid %U",
339 unformat_gid_address, leid))
343 else if (unformat (line_input, "reid %U",
344 unformat_gid_address, reid))
348 else if (unformat (line_input, "vni %u", &vni))
350 gid_address_vni (leid) = vni;
351 gid_address_vni (reid) = vni;
354 else if (unformat (line_input, "vrf %u", &vrf))
358 else if (unformat (line_input, "negative action %U",
359 unformat_negative_mapping_action, &action))
363 else if (unformat (line_input, "lloc %U rloc %U",
364 unformat_ip_address, &lloc,
365 unformat_ip_address, &rloc))
367 /* TODO: support p and w */
368 vec_add1 (llocs, lloc);
369 vec_add1 (rlocs, rloc);
373 error = unformat_parse_error (line_input);
377 unformat_free (line_input);
379 if (!vni_set || !vrf_set)
381 error = clib_error_return(0, "vni and vrf must be set!");
387 error = clib_error_return(0, "remote eid must be set!");
395 error = clib_error_return(0, "no action set for negative tunnel!");
401 if (vec_len (llocs) == 0)
403 error = clib_error_return (0, "expected ip4/ip6 locators.");
407 if (vec_len (llocs) != 1)
409 error = clib_error_return (0, "multihoming not supported for now!");
418 /* if leid not set, make sure it's the same AFI like reid */
419 gid_address_type(leid) = gid_address_type(reid);
420 if (GID_ADDR_IP_PREFIX == gid_address_type (reid))
421 gid_address_ip_version(leid) = gid_address_ip_version(reid);
425 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
426 memset (a, 0, sizeof(a[0]));
431 gid_address_copy(&a->seid, leid);
432 gid_address_copy(&a->deid, reid);
436 a->slocator = llocs[0];
437 a->dlocator = rlocs[0];
440 rv = vnet_lisp_gpe_add_del_fwd_entry (a, 0);
443 error = clib_error_return(0, "failed to %s gpe tunnel!",
444 is_add ? "add" : "delete");
453 VLIB_CLI_COMMAND (lisp_gpe_add_del_fwd_entry_command, static) = {
454 .path = "lisp gpe tunnel",
455 .short_help = "lisp gpe tunnel add/del vni <vni> vrf <vrf> [leid <leid>]"
456 "reid <reid> [lloc <sloc> rloc <rloc>] [negative action <action>]",
457 .function = lisp_gpe_add_del_fwd_entry_command_fn,
461 format_decap_next (u8 * s, va_list * args)
463 u32 next_index = va_arg (*args, u32);
467 case LISP_GPE_INPUT_NEXT_DROP:
468 return format (s, "drop");
469 case LISP_GPE_INPUT_NEXT_IP4_INPUT:
470 return format (s, "ip4");
471 case LISP_GPE_INPUT_NEXT_IP6_INPUT:
472 return format (s, "ip6");
474 return format (s, "unknown %d", next_index);
480 format_lisp_gpe_tunnel (u8 * s, va_list * args)
482 lisp_gpe_tunnel_t * t = va_arg (*args, lisp_gpe_tunnel_t *);
483 lisp_gpe_main_t * lgm = &lisp_gpe_main;
486 "[%d] %U (src) %U (dst) fibs: encap %d, decap %d",
488 format_ip_address, &t->src,
489 format_ip_address, &t->dst,
493 s = format (s, " decap next %U\n", format_decap_next, t->decap_next_index);
494 s = format (s, "lisp ver %d ", (t->ver_res>>6));
496 #define _(n,v) if (t->flags & v) s = format (s, "%s-bit ", #n);
497 foreach_lisp_gpe_flag_bit;
500 s = format (s, "next_protocol %d ver_res %x res %x\n",
501 t->next_protocol, t->ver_res, t->res);
503 s = format (s, "iid %d (0x%x)\n", t->vni, t->vni);
507 static clib_error_t *
508 show_lisp_gpe_tunnel_command_fn (vlib_main_t * vm,
509 unformat_input_t * input,
510 vlib_cli_command_t * cmd)
512 lisp_gpe_main_t * lgm = &lisp_gpe_main;
513 lisp_gpe_tunnel_t * t;
515 if (pool_elts (lgm->tunnels) == 0)
516 vlib_cli_output (vm, "No lisp-gpe tunnels configured...");
518 pool_foreach (t, lgm->tunnels,
520 vlib_cli_output (vm, "%U", format_lisp_gpe_tunnel, t);
526 VLIB_CLI_COMMAND (show_lisp_gpe_tunnel_command, static) = {
527 .path = "show lisp gpe tunnel",
528 .function = show_lisp_gpe_tunnel_command_fn,
532 vnet_lisp_gpe_enable_disable_status(void)
534 lisp_gpe_main_t * lgm = &lisp_gpe_main;
540 vnet_lisp_gpe_enable_disable (vnet_lisp_gpe_enable_disable_args_t * a)
542 lisp_gpe_main_t * lgm = &lisp_gpe_main;
543 vnet_main_t * vnm = lgm->vnet_main;
547 /* add lgpe_ip4_lookup as possible next_node for ip4 lookup */
548 if (lgm->ip4_lookup_next_lgpe_ip4_lookup == ~0)
550 lgm->ip4_lookup_next_lgpe_ip4_lookup = vlib_node_add_next (
551 vnm->vlib_main, ip4_lookup_node.index,
552 lgpe_ip4_lookup_node.index);
554 /* add lgpe_ip6_lookup as possible next_node for ip6 lookup */
555 if (lgm->ip6_lookup_next_lgpe_ip6_lookup == ~0)
557 lgm->ip6_lookup_next_lgpe_ip6_lookup = vlib_node_add_next (
558 vnm->vlib_main, ip6_lookup_node.index,
559 lgpe_ip6_lookup_node.index);
563 /* ask cp to re-add ifaces and defaults */
570 CLIB_UNUSED(uword * val);
572 u32 * table_ids = 0, * table_id;
573 lisp_gpe_tunnel_key_t * tunnels = 0, * tunnel;
574 vnet_lisp_gpe_add_del_fwd_entry_args_t _at, * at = &_at;
575 vnet_lisp_gpe_add_del_iface_args_t _ai, * ai= &_ai;
577 /* remove all tunnels */
578 mhash_foreach(tunnel, val, &lgm->lisp_gpe_tunnel_by_key, ({
579 vec_add1(tunnels, tunnel[0]);
582 vec_foreach(tunnel, tunnels) {
583 memset(at, 0, sizeof(at[0]));
585 gid_address_type(&at->deid) = GID_ADDR_IP_PREFIX;
586 ip_prefix_copy(&gid_address_ippref(&at->deid), &tunnel->eid);
587 ip_address_copy(&at->dlocator, &tunnel->dst_loc);
588 vnet_lisp_gpe_add_del_fwd_entry (at, 0);
592 /* disable all ifaces */
593 hash_foreach_pair(p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
594 vec_add1(table_ids, p->key);
597 vec_foreach(table_id, table_ids) {
599 ai->table_id = table_id[0];
601 /* disables interface and removes defaults */
602 vnet_lisp_gpe_add_del_iface(ai, 0);
611 static clib_error_t *
612 lisp_gpe_enable_disable_command_fn (vlib_main_t * vm, unformat_input_t * input,
613 vlib_cli_command_t * cmd)
615 unformat_input_t _line_input, * line_input = &_line_input;
617 vnet_lisp_gpe_enable_disable_args_t _a, * a = &_a;
619 /* Get a line of input. */
620 if (! unformat_user (input, unformat_line_input, line_input))
623 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
625 if (unformat (line_input, "enable"))
627 else if (unformat (line_input, "disable"))
631 return clib_error_return (0, "parse error: '%U'",
632 format_unformat_error, line_input);
636 return vnet_lisp_gpe_enable_disable (a);
639 VLIB_CLI_COMMAND (enable_disable_lisp_gpe_command, static) = {
641 .short_help = "lisp gpe [enable|disable]",
642 .function = lisp_gpe_enable_disable_command_fn,
645 static clib_error_t *
646 lisp_show_iface_command_fn (vlib_main_t * vm,
647 unformat_input_t * input,
648 vlib_cli_command_t * cmd)
650 lisp_gpe_main_t * lgm = &lisp_gpe_main;
653 vlib_cli_output (vm, "%=10s%=12s", "vrf", "hw_if_index");
654 hash_foreach_pair (p, lgm->lisp_gpe_hw_if_index_by_table_id, ({
655 vlib_cli_output (vm, "%=10d%=10d", p->key, p->value[0]);
660 VLIB_CLI_COMMAND (lisp_show_iface_command) = {
661 .path = "show lisp gpe interface",
662 .short_help = "show lisp gpe interface",
663 .function = lisp_show_iface_command_fn,
667 lisp_gpe_init (vlib_main_t *vm)
669 lisp_gpe_main_t * lgm = &lisp_gpe_main;
670 clib_error_t * error = 0;
672 if ((error = vlib_call_init_function (vm, ip_main_init)))
675 if ((error = vlib_call_init_function (vm, ip4_lookup_init)))
678 lgm->vnet_main = vnet_get_main();
680 lgm->im4 = &ip4_main;
681 lgm->im6 = &ip6_main;
682 lgm->lm4 = &ip4_main.lookup_main;
683 lgm->lm6 = &ip6_main.lookup_main;
684 lgm->ip4_lookup_next_lgpe_ip4_lookup = ~0;
685 lgm->ip6_lookup_next_lgpe_ip6_lookup = ~0;
687 mhash_init (&lgm->lisp_gpe_tunnel_by_key, sizeof(uword),
688 sizeof(lisp_gpe_tunnel_key_t));
690 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe,
691 lisp_gpe_ip4_input_node.index, 1 /* is_ip4 */);
692 udp_register_dst_port (vm, UDP_DST_PORT_lisp_gpe6,
693 lisp_gpe_ip6_input_node.index, 0 /* is_ip4 */);
698 format_vnet_lisp_gpe_status (u8 * s, va_list * args)
700 lisp_gpe_main_t * lgm = &lisp_gpe_main;
701 return format (s, "%s", lgm->is_en ? "enabled" : "disabled");
704 VLIB_INIT_FUNCTION(lisp_gpe_init);