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-cp/control.h>
17 #include <vnet/lisp-cp/packets.h>
18 #include <vnet/lisp-cp/lisp_msg_serdes.h>
19 #include <vnet/lisp-gpe/lisp_gpe.h>
21 /* Adds mapping to map-cache but does NOT program LISP forwarding */
23 vnet_lisp_add_del_mapping (vnet_lisp_add_del_mapping_args_t * a,
24 u32 * map_index_result)
26 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
27 u32 mi, * map_indexp, map_index, i;
31 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &a->deid);
34 /* TODO check if overwriting and take appropriate actions */
35 if (mi != GID_LOOKUP_MISS) {
36 clib_warning("eid %U found in the eid-table", format_ip_address,
38 return VNET_API_ERROR_VALUE_EXIST;
41 pool_get(lcm->mapping_pool, m);
43 m->locator_set_index = a->locator_set_index;
47 map_index = m - lcm->mapping_pool;
48 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, map_index,
51 if (pool_is_free_index(lcm->locator_set_pool, a->locator_set_index))
53 clib_warning("Locator set with index %d doesn't exist",
54 a->locator_set_index);
55 return VNET_API_ERROR_INVALID_VALUE;
58 /* add eid to list of eids supported by locator-set */
59 vec_validate (lcm->locator_set_to_eids, a->locator_set_index);
60 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
61 a->locator_set_index);
62 vec_add1(eid_indexes[0], map_index);
67 vec_add1(lcm->local_mappings_indexes, map_index);
69 /* XXX do something else? */
71 map_index_result[0] = map_index;
75 if (mi == GID_LOOKUP_MISS) {
76 clib_warning("eid %U not found in the eid-table", format_ip_address,
78 return VNET_API_ERROR_INVALID_VALUE;
81 /* clear locator-set to eids binding */
82 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids,
83 a->locator_set_index);
84 for (i = 0; i < vec_len(eid_indexes[0]); i++)
86 map_indexp = vec_elt_at_index(eid_indexes[0], i);
87 if (map_indexp[0] == mi)
90 vec_del1(eid_indexes[0], i);
92 /* remove local mark if needed */
93 m = pool_elt_at_index(lcm->mapping_pool, mi);
97 for (k = 0; k < vec_len(lcm->local_mappings_indexes); k++)
99 lm_indexp = vec_elt_at_index(lcm->local_mappings_indexes, k);
100 if (lm_indexp[0] == mi)
103 vec_del1(lcm->local_mappings_indexes, k);
107 /* remove tunnel ??? */
110 /* remove mapping from dictionary */
111 gid_dictionary_add_del (&lcm->mapping_index_by_gid, &a->deid, 0, 0);
112 pool_put_index (lcm->mapping_pool, mi);
118 static clib_error_t *
119 lisp_add_del_local_eid_command_fn (vlib_main_t * vm, unformat_input_t * input,
120 vlib_cli_command_t * cmd)
122 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
123 unformat_input_t _line_input, * line_input = &_line_input;
126 ip_prefix_t * prefp = &gid_address_ippref(&eid);
127 gid_address_t * eids = 0;
128 clib_error_t * error = 0;
129 u8 * locator_set_name;
130 u32 locator_set_index = 0, map_index = 0;
132 vnet_lisp_add_del_mapping_args_t _a, * a = &_a;
134 gid_address_type (&eid) = IP_PREFIX;
136 /* Get a line of input. */
137 if (! unformat_user (input, unformat_line_input, line_input))
140 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
142 if (unformat (line_input, "add"))
144 else if (unformat (line_input, "del"))
146 else if (unformat (line_input, "eid %U", unformat_ip_prefix, prefp))
150 else if (unformat (line_input, "locator-set %_%v%_", &locator_set_name))
152 p = hash_get_mem(lcm->locator_set_index_by_name, locator_set_name);
155 error = clib_error_return(0, "locator-set %s doesn't exist",
159 locator_set_index = p[0];
163 error = unformat_parse_error(line_input);
168 /* XXX treat batch configuration */
171 a->locator_set_index = locator_set_index;
174 vnet_lisp_add_del_mapping (a, &map_index);
180 VLIB_CLI_COMMAND (lisp_add_del_local_eid_command) = {
181 .path = "lisp eid-table",
182 .short_help = "lisp eid-table add/del eid <eid> locator-set <locator-set>",
183 .function = lisp_add_del_local_eid_command_fn,
186 static clib_error_t *
187 lisp_show_local_eid_table_command_fn (vlib_main_t * vm,
188 unformat_input_t * input,
189 vlib_cli_command_t * cmd)
191 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
194 vlib_cli_output (vm, "%=20s%=16s", "EID", "Locator");
195 pool_foreach (mapit, lcm->mapping_pool,
198 locator_set_t * ls = pool_elt_at_index (lcm->locator_set_pool,
199 mapit->locator_set_index);
200 vlib_cli_output (vm, "%-16U%16v", format_gid_address, &mapit->eid,
208 VLIB_CLI_COMMAND (lisp_cp_show_local_eid_table_command) = {
209 .path = "show lisp eid-table",
210 .short_help = "Shows local EID table",
211 .function = lisp_show_local_eid_table_command_fn,
214 /* cleans locator to locator-set data and removes locators not part of
217 clean_locator_to_locator_set (lisp_cp_main_t * lcm, u32 lsi)
219 u32 i, j, *loc_indexp, *ls_indexp, **ls_indexes;
220 locator_set_t * ls = pool_elt_at_index(lcm->locator_set_pool, lsi);
221 for (i = 0; i < vec_len(ls->locator_indices); i++)
223 loc_indexp = vec_elt_at_index(ls->locator_indices, i);
224 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
226 for (j = 0; j < vec_len(ls_indexes[0]); j++)
228 ls_indexp = vec_elt_at_index(ls_indexes[0], j);
229 if (ls_indexp[0] == lsi)
233 /* delete index for removed locator-set*/
234 vec_del1(ls_indexes[0], j);
236 /* delete locator if it's part of no locator-set */
237 if (vec_len (ls_indexes[0]) == 0)
238 pool_put_index(lcm->locator_pool, loc_indexp[0]);
242 vnet_lisp_add_del_locator_set (vnet_lisp_add_del_locator_set_args_t * a,
245 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
247 locator_t * loc, * itloc;
248 uword _p = (u32)~0, * p = &_p;
249 u32 loc_index, ls_index, ** ls_indexes;
254 /* check if overwrite */
256 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
261 if (p && p[0] != (u32)~0)
263 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
266 clib_warning("locator-set %d to be overwritten doesn't exist!",
271 /* clean locator to locator-set vectors and remove locators if
272 * they're not part of another locator-set */
273 clean_locator_to_locator_set (lcm, p[0]);
275 /* remove locator indices from locator set */
276 vec_free(ls->locator_indices);
283 /* new locator-set */
286 pool_get(lcm->locator_set_pool, ls);
287 ls_index = ls - lcm->locator_set_pool;
291 ls->name = vec_dup(a->name);
293 if (!lcm->locator_set_index_by_name)
294 lcm->locator_set_index_by_name = hash_create_vec(
295 /* size */0, sizeof(ls->name[0]), sizeof(uword));
296 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
298 /* mark as local locator-set */
299 vec_add1(lcm->local_locator_set_indexes, ls_index);
301 ls->local = a->local;
303 ls_result[0] = ls_index;
306 /* allocate locators */
307 vec_foreach (itloc, a->locators)
309 pool_get(lcm->locator_pool, loc);
311 loc_index = loc - lcm->locator_pool;
313 vec_add1(ls->locator_indices, loc_index);
315 vec_validate (lcm->locator_to_locator_sets, loc_index);
316 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
318 vec_add1(ls_indexes[0], ls_index);
323 /* find locator-set */
326 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
329 clib_warning("locator-set %v doesn't exists", a->name);
336 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
339 clib_warning("locator-set with index %d doesn't exists", p[0]);
342 // /* XXX what happens when a mapping is configured to use the loc-set ? */
343 // if (vec_len (vec_elt_at_index(lcm->locator_set_to_eids, p[0])) != 0)
345 // clib_warning ("Can't delete a locator that supports a mapping!");
349 if (vec_len(lcm->locator_set_to_eids) != 0)
351 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
352 if (vec_len(eid_indexes[0]) != 0)
354 clib_warning ("Can't delete a locator that supports a mapping!");
359 /* clean locator to locator-sets data */
360 clean_locator_to_locator_set (lcm, p[0]);
366 vec_foreach_index(it, lcm->local_locator_set_indexes)
368 lsi = vec_elt(lcm->local_locator_set_indexes, it);
371 vec_del1(lcm->local_locator_set_indexes, it);
375 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
378 pool_put(lcm->locator_set_pool, ls);
384 uword *vnet_lisp_get_locator(vnet_lisp_add_del_locator_set_args_t * a,
387 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
392 /* find locator-set */
395 p = hash_get_mem(lcm->locator_set_index_by_name, a->name);
406 vnet_lisp_add_del_locator_set_name (vnet_lisp_add_del_locator_set_args_t * a,
409 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
411 uword _p = (u32)~0, * p = &_p;
413 u32 **eid_indexes = NULL;
416 ASSERT(ls_result != NULL);
418 p = vnet_lisp_get_locator(a, p);
423 if (p && p[0] != (u32)~0)
425 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
428 clib_warning("locator-set %d to be overwritten doesn't exist!",
430 return VNET_API_ERROR_UNSPECIFIED;
433 /* clean locator to locator-set vectors and remove locators if
434 * they're not part of another locator-set */
435 clean_locator_to_locator_set (lcm, p[0]);
437 /* remove locator indices from locator set */
438 vec_free(ls->locator_indices);
445 /* new locator-set */
448 pool_get(lcm->locator_set_pool, ls);
449 ls_index = ls - lcm->locator_set_pool;
453 ls->name = vec_dup(a->name);
455 if (!lcm->locator_set_index_by_name)
456 lcm->locator_set_index_by_name = hash_create_vec(
457 /* size */0, sizeof(ls->name[0]), sizeof(uword));
458 hash_set_mem(lcm->locator_set_index_by_name, ls->name, ls_index);
460 /* mark as local locator-set */
461 vec_add1(lcm->local_locator_set_indexes, ls_index);
463 ls->local = a->local;
464 ls->locator_indices = NULL;
466 ls_result[0] = ls_index;
473 clib_warning("locator-set %v doesn't exists", a->name);
474 return VNET_API_ERROR_INVALID_ARGUMENT;
477 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
480 clib_warning("locator-set with index %d doesn't exists", p[0]);
481 return VNET_API_ERROR_INVALID_ARGUMENT;
484 if (vec_len(lcm->locator_set_to_eids) != 0)
486 eid_indexes = vec_elt_at_index(lcm->locator_set_to_eids, p[0]);
487 if (vec_len(eid_indexes[0]) != 0)
489 clib_warning ("Can't delete a locator that supports a mapping!");
494 /* clean locator to locator-sets data */
495 clean_locator_to_locator_set (lcm, p[0]);
501 vec_foreach_index(it, lcm->local_locator_set_indexes)
503 lsi = vec_elt(lcm->local_locator_set_indexes, it);
506 vec_del1(lcm->local_locator_set_indexes, it);
510 hash_unset_mem(lcm->locator_set_index_by_name, ls->name);
513 pool_put(lcm->locator_set_pool, ls);
519 vnet_lisp_add_del_locator (vnet_lisp_add_del_locator_set_args_t *a,
522 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
523 locator_set_t *ls = NULL;
524 locator_t *loc = NULL, *itloc = NULL;
525 uword _p = (u32)~0, * p = &_p;
526 u32 loc_index = ~0, ls_index = ~0, *locit = NULL, **ls_indexes = NULL;
530 ASSERT(ls_result != NULL);
532 p = vnet_lisp_get_locator(a, p);
534 clib_warning("locator-set %v doesn't exists", a->name);
535 return VNET_API_ERROR_INVALID_ARGUMENT;
542 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
545 clib_warning("locator-set %d to be overwritten doesn't exist!",
547 return VNET_API_ERROR_INVALID_ARGUMENT;
553 /* allocate locators */
555 pool_get(lcm->locator_pool, loc);
557 loc_index = loc - lcm->locator_pool;
559 vec_add1(ls->locator_indices, loc_index);
561 vec_validate (lcm->locator_to_locator_sets, loc_index);
562 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
564 vec_add1(ls_indexes[0], ls_index);
568 ls = pool_elt_at_index(lcm->locator_set_pool, p[0]);
571 clib_warning("locator-set with index %d doesn't exists", p[0]);
572 return VNET_API_ERROR_INVALID_ARGUMENT;
579 vec_foreach (locit, ls->locator_indices)
581 loc = pool_elt_at_index(lcm->locator_pool, locit[0]);
582 if (loc->local && loc->sw_if_index == itloc->sw_if_index)
584 ls_indexes = vec_elt_at_index(lcm->locator_to_locator_sets,
586 pool_put_index(lcm->locator_pool, locit[0]);
587 vec_del1(ls->locator_indices, i);
588 vec_del1(ls_indexes[0], ls_index);
597 static clib_error_t *
598 lisp_add_del_locator_set_command_fn (vlib_main_t * vm, unformat_input_t * input,
599 vlib_cli_command_t * cmd)
601 lisp_gpe_main_t * lgm = &lisp_gpe_main;
602 vnet_main_t * vnm = lgm->vnet_main;
603 unformat_input_t _line_input, * line_input = &_line_input;
605 clib_error_t * error = 0;
606 u8 * locator_set_name = 0;
607 locator_t locator, * locators = 0;
608 vnet_lisp_add_del_locator_set_args_t _a, * a = &_a;
611 memset(&locator, 0, sizeof(locator));
612 memset(a, 0, sizeof(a[0]));
614 /* Get a line of input. */
615 if (! unformat_user (input, unformat_line_input, line_input))
618 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
620 if (unformat (line_input, "add %_%v%_", &locator_set_name))
622 else if (unformat (line_input, "del %_%v%_", &locator_set_name))
624 else if (unformat (line_input, "iface %U p %d w %d",
625 unformat_vnet_sw_interface, vnm, &locator.sw_if_index,
626 &locator.priority, &locator.weight))
629 vec_add1(locators, locator);
633 error = unformat_parse_error(line_input);
638 a->name = locator_set_name;
639 a->locators = locators;
643 vnet_lisp_add_del_locator_set(a, &ls_index);
647 vec_free(locator_set_name);
651 VLIB_CLI_COMMAND (lisp_cp_add_del_locator_set_command) = {
652 .path = "lisp locator-set",
653 .short_help = "lisp locator-set add/del <name> <iface-name> <priority> <weight>",
654 .function = lisp_add_del_locator_set_command_fn,
657 static clib_error_t *
658 lisp_cp_show_locator_sets_command_fn (vlib_main_t * vm,
659 unformat_input_t * input,
660 vlib_cli_command_t * cmd)
662 locator_set_t * lsit;
665 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
667 vlib_cli_output (vm, "%=20s%=16s%=16s%=16s", "Locator-set", "Locator",
668 "Priority", "Weight");
669 pool_foreach (lsit, lcm->locator_set_pool,
672 msg = format (msg, "%-16v", lsit->name);
673 vec_foreach (locit, lsit->locator_indices)
675 loc = pool_elt_at_index (lcm->locator_pool, locit[0]);
677 msg = format (msg, "%16d%16d%16d", loc->sw_if_index, loc->priority,
680 msg = format (msg, "%16U%16d%16d", format_gid_address, &loc->address,
681 loc->priority, loc->weight);
683 vlib_cli_output (vm, "%v", msg);
689 VLIB_CLI_COMMAND (lisp_cp_show_locator_sets_command) = {
690 .path = "show lisp locator-set",
691 .short_help = "Shows locator-sets",
692 .function = lisp_cp_show_locator_sets_command_fn,
696 vnet_lisp_add_del_map_resolver (vnet_lisp_add_del_map_resolver_args_t * a)
698 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
704 vec_foreach(addr, lcm->map_resolvers)
706 if (!ip_address_cmp (addr, &a->address))
708 clib_warning("map-resolver %U already exists!", format_ip_address,
713 vec_add1(lcm->map_resolvers, a->address);
717 for (i = 0; i < vec_len(lcm->map_resolvers); i++)
719 addr = vec_elt_at_index(lcm->map_resolvers, i);
720 if (!ip_address_cmp (addr, &a->address))
722 vec_delete(lcm->map_resolvers, 1, i);
730 static clib_error_t *
731 lisp_add_del_map_resolver_command_fn (vlib_main_t * vm,
732 unformat_input_t * input,
733 vlib_cli_command_t * cmd)
735 unformat_input_t _line_input, * line_input = &_line_input;
737 ip_address_t ip_addr;
738 clib_error_t * error = 0;
739 vnet_lisp_add_del_map_resolver_args_t _a, * a = &_a;
741 /* Get a line of input. */
742 if (! unformat_user (input, unformat_line_input, line_input))
745 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
747 if (unformat (line_input, "add"))
749 else if (unformat (line_input, "del"))
751 else if (unformat (line_input, "%U", unformat_ip_address, &ip_addr))
755 error = unformat_parse_error(line_input);
760 a->address = ip_addr;
761 vnet_lisp_add_del_map_resolver (a);
767 VLIB_CLI_COMMAND (lisp_add_del_map_resolver_command) = {
768 .path = "lisp map-resolver",
769 .short_help = "lisp map-resolver add/del <ip_address>",
770 .function = lisp_add_del_map_resolver_command_fn,
773 /* Statistics (not really errors) */
774 #define foreach_lisp_cp_lookup_error \
776 _(MAP_REQUESTS_SENT, "map-request sent")
778 static char * lisp_cp_lookup_error_strings[] = {
779 #define _(sym,string) string,
780 foreach_lisp_cp_lookup_error
786 #define _(sym,str) LISP_CP_LOOKUP_ERROR_##sym,
787 foreach_lisp_cp_lookup_error
789 LISP_CP_LOOKUP_N_ERROR,
790 } lisp_cp_lookup_error_t;
794 LISP_CP_LOOKUP_NEXT_DROP,
795 LISP_CP_LOOKUP_NEXT_IP4_LOOKUP,
796 LISP_CP_LOOKUP_NEXT_IP6_LOOKUP,
797 LISP_CP_LOOKUP_N_NEXT,
798 } lisp_cp_lookup_next_t;
802 gid_address_t dst_eid;
803 ip4_address_t map_resolver_ip;
804 } lisp_cp_lookup_trace_t;
807 format_lisp_cp_lookup_trace (u8 * s, va_list * args)
809 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
810 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
811 lisp_cp_lookup_trace_t * t = va_arg (*args, lisp_cp_lookup_trace_t *);
813 s = format (s, "LISP-CP-LOOKUP: map-resolver: %U destination eid %U",
814 format_ip4_address, &t->map_resolver_ip, format_gid_address,
820 ip_fib_lookup_with_table (lisp_cp_main_t * lcm, u32 fib_index,
823 if (ip_addr_version (dst) == IP4)
824 return ip4_fib_lookup_with_table (lcm->im4, fib_index, &ip_addr_v4(dst),
827 return ip6_fib_lookup_with_table (lcm->im6, fib_index, &ip_addr_v6(dst));
831 get_local_iface_ip_for_dst (lisp_cp_main_t *lcm, ip_address_t * dst,
835 ip_adjacency_t * adj;
836 ip_interface_address_t * ia = 0;
837 ip_lookup_main_t * lm = &lcm->im4->lookup_main;
838 ip4_address_t * l4 = 0;
839 ip6_address_t * l6 = 0;
841 adj_index = ip_fib_lookup_with_table (lcm, 0, dst);
842 adj = ip_get_adjacency (lm, adj_index);
844 if (adj->lookup_next_index == IP_LOOKUP_NEXT_ARP)
846 ia = pool_elt_at_index(lm->if_address_pool, adj->if_address_index);
847 if (ip_addr_version(dst) == IP4)
849 l4 = ip_interface_address_get_address (lm, ia);
853 l6 = ip_interface_address_get_address (lm, ia);
856 else if (adj->lookup_next_index == IP_LOOKUP_NEXT_REWRITE)
858 /* find sw_if_index in rewrite header */
859 u32 sw_if_index = adj->rewrite_header.sw_if_index;
861 /* find suitable address */
862 if (ip_addr_version(dst) == IP4)
864 /* find the first ip address */
865 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
866 sw_if_index, 1 /* unnumbered */,
868 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
874 /* find the first ip address */
875 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
876 sw_if_index, 1 /* unnumbered */,
878 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
885 clib_warning("Can't find local local interface ip for dst %U",
886 format_ip_address, dst);
892 ip_addr_v4(sloc).as_u32 = l4->as_u32;
893 ip_addr_version(sloc) = IP4;
897 memcpy (&ip_addr_v6(sloc), l6, sizeof(*l6));
898 ip_addr_version(sloc) = IP6;
902 clib_warning("Can't find local interface addr for dst %U",
903 format_ip_address, dst);
908 static ip_address_t *
909 build_itr_rloc_list (lisp_cp_main_t * lcm, locator_set_t * loc_set)
916 ip_interface_address_t * ia = 0;
917 ip_address_t * rlocs = 0;
918 ip_address_t _rloc, * rloc = &_rloc;
920 for (i = 0; i < vec_len(loc_set->locator_indices); i++)
922 loc_indexp = vec_elt_at_index(loc_set->locator_indices, i);
923 loc = pool_elt_at_index (lcm->locator_pool, loc_indexp[0]);
925 ip_addr_version(rloc) = IP4;
926 /* Add ipv4 locators first TODO sort them */
927 foreach_ip_interface_address (&lcm->im4->lookup_main, ia,
928 loc->sw_if_index, 1 /* unnumbered */,
930 l4 = ip_interface_address_get_address (&lcm->im4->lookup_main, ia);
931 ip_addr_v4(rloc) = l4[0];
932 vec_add1(rlocs, rloc[0]);
935 ip_addr_version(rloc) = IP6;
936 /* Add ipv6 locators */
937 foreach_ip_interface_address (&lcm->im6->lookup_main, ia,
938 loc->sw_if_index, 1 /* unnumbered */,
940 l6 = ip_interface_address_get_address (&lcm->im6->lookup_main, ia);
941 ip_addr_v6(rloc) = l6[0];
942 vec_add1(rlocs, rloc[0]);
948 static vlib_buffer_t *
949 build_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
950 gid_address_t * seid, gid_address_t * deid,
951 locator_set_t * loc_set, u8 is_smr_invoked,
952 u64 *nonce_res, u32 * bi_res)
956 ip_address_t * mr_ip, sloc;
957 ip_address_t * rlocs = 0;
959 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
961 clib_warning ("Can't allocate buffer for Map-Request!");
965 b = vlib_get_buffer (vm, bi);
967 /* leave some space for the encap headers */
968 vlib_buffer_make_headroom (b, MAX_LISP_MSG_ENCAP_LEN);
971 rlocs = build_itr_rloc_list (lcm, loc_set);
974 lisp_msg_put_mreq (lcm, b, seid, deid, rlocs, is_smr_invoked, nonce_res);
976 /* push ecm: udp-ip-lisp */
977 lisp_msg_push_ecm (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, seid, deid);
979 /* get map-resolver ip XXX use first*/
980 mr_ip = vec_elt_at_index(lcm->map_resolvers, 0);
982 /* get local iface ip to use in map-request XXX fib 0 for now*/
983 get_local_iface_ip_for_dst (lcm, mr_ip, &sloc);
985 /* push outer ip header */
986 pkt_push_udp_and_ip (vm, b, LISP_CONTROL_PORT, LISP_CONTROL_PORT, &sloc,
997 send_encapsulated_map_request (vlib_main_t * vm, lisp_cp_main_t *lcm,
998 gid_address_t * seid, gid_address_t * deid,
1001 u32 next_index, bi = 0, * to_next, map_index;
1005 locator_set_t * loc_set;
1007 pending_map_request_t * pmr;
1009 /* get locator-set for seid */
1010 map_index = gid_dictionary_lookup (&lcm->mapping_index_by_gid, seid);
1011 if (map_index == ~0)
1013 clib_warning("No local mapping found in eid-table for %U!",
1014 format_gid_address, seid);
1018 map = pool_elt_at_index (lcm->mapping_pool, map_index);
1022 clib_warning("Mapping found for src eid %U is not marked as local!",
1023 format_gid_address, seid);
1026 loc_set = pool_elt_at_index (lcm->locator_set_pool, map->locator_set_index);
1028 /* build the encapsulated map request */
1029 b = build_encapsulated_map_request (vm, lcm, seid, deid, loc_set,
1030 is_smr_invoked, &nonce, &bi);
1035 vnet_buffer(b)->sw_if_index[VLIB_TX] = ~0;
1036 next_index = (ip_prefix_version(&gid_address_ippref(seid)) == IP4) ?
1037 ip4_lookup_node.index : ip6_lookup_node.index;
1039 f = vlib_get_frame_to_node (vm, next_index);
1041 /* Enqueue the packet */
1042 to_next = vlib_frame_vector_args (f);
1045 vlib_put_frame_to_node (vm, next_index, f);
1047 /* add map-request to pending requests table */
1048 pool_get(lcm->pending_map_requests_pool, pmr);
1049 gid_address_copy (&pmr->src, seid);
1050 gid_address_copy (&pmr->dst, deid);
1051 pmr->src_mapping_index = map_index;
1052 hash_set(lcm->pending_map_requests_by_nonce, nonce,
1053 pmr - lcm->pending_map_requests_pool);
1057 get_src_and_dst (void *hdr, ip_address_t * src, ip_address_t *dst)
1059 ip4_header_t * ip4 = hdr;
1062 if ((ip4->ip_version_and_header_length & 0xF0) == 0x40)
1064 ip_addr_v4(src).as_u32 = ip4->src_address.as_u32;
1065 ip_addr_version(src) = IP4;
1066 ip_addr_v4(dst).as_u32 = ip4->dst_address.as_u32;
1067 ip_addr_version(dst) = IP4;
1072 memcpy (&ip_addr_v6(src), &ip6->src_address, sizeof(ip6->src_address));
1073 ip_addr_version(src) = IP6;
1074 memcpy (&ip_addr_v6(dst), &ip6->dst_address, sizeof(ip6->dst_address));
1075 ip_addr_version(dst) = IP6;
1080 lisp_cp_lookup (vlib_main_t * vm, vlib_node_runtime_t * node,
1081 vlib_frame_t * from_frame)
1083 u32 * from, * to_next_drop;
1084 lisp_cp_main_t *lcm = vnet_lisp_cp_get_main();
1085 u32 pkts_mapped = 0;
1086 uword n_left_from, n_left_to_next_drop;
1088 from = vlib_frame_vector_args (from_frame);
1089 n_left_from = from_frame->n_vectors;
1091 while (n_left_from > 0)
1093 vlib_get_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP,
1094 to_next_drop, n_left_to_next_drop);
1096 while (n_left_from > 0 && n_left_to_next_drop > 0)
1101 gid_address_t src, dst;
1102 ip_prefix_t * spref, * dpref;
1104 gid_address_type (&src) = IP_PREFIX;
1105 spref = &gid_address_ippref(&src);
1106 gid_address_type (&dst) = IP_PREFIX;
1107 dpref = &gid_address_ippref(&dst);
1112 to_next_drop[0] = pi0;
1114 n_left_to_next_drop -= 1;
1116 p0 = vlib_get_buffer (vm, pi0);
1117 p0->error = node->errors[LISP_CP_LOOKUP_ERROR_DROP];
1119 /* src/dst eid pair */
1120 ip0 = vlib_buffer_get_current (p0);
1121 get_src_and_dst (ip0, &ip_prefix_addr(spref), &ip_prefix_addr(dpref));
1122 ip_prefix_len(spref) = ip_address_max_len (ip_prefix_version(spref));
1123 ip_prefix_len(dpref) = ip_address_max_len (ip_prefix_version(dpref));
1125 /* send map-request */
1126 send_encapsulated_map_request (vm, lcm, &src, &dst, 0);
1130 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
1132 lisp_cp_lookup_trace_t *tr = vlib_add_trace (vm, node, p0,
1134 gid_address_copy (&tr->dst_eid, &dst);
1135 memcpy (&tr->map_resolver_ip,
1136 vec_elt_at_index(lcm->map_resolvers, 0),
1137 sizeof(ip_address_t));
1141 vlib_put_next_frame (vm, node, LISP_CP_LOOKUP_NEXT_DROP, n_left_to_next_drop);
1143 vlib_node_increment_counter (vm, node->node_index,
1144 LISP_CP_LOOKUP_ERROR_MAP_REQUESTS_SENT,
1146 return from_frame->n_vectors;
1149 VLIB_REGISTER_NODE (lisp_cp_lookup_node) = {
1150 .function = lisp_cp_lookup,
1151 .name = "lisp-cp-lookup",
1152 .vector_size = sizeof (u32),
1153 .format_trace = format_lisp_cp_lookup_trace,
1154 .type = VLIB_NODE_TYPE_INTERNAL,
1156 .n_errors = LISP_CP_LOOKUP_N_ERROR,
1157 .error_strings = lisp_cp_lookup_error_strings,
1159 .n_next_nodes = LISP_CP_LOOKUP_N_NEXT,
1162 [LISP_CP_LOOKUP_NEXT_DROP] = "error-drop",
1163 [LISP_CP_LOOKUP_NEXT_IP4_LOOKUP] = "ip4-lookup",
1164 [LISP_CP_LOOKUP_NEXT_IP6_LOOKUP] = "ip6-lookup",
1168 /* lisp_cp_input statistics */
1169 #define foreach_lisp_cp_input_error \
1171 _(MAP_REPLIES_RECEIVED, "map-replies received")
1173 static char * lisp_cp_input_error_strings[] = {
1174 #define _(sym,string) string,
1175 foreach_lisp_cp_input_error
1181 #define _(sym,str) LISP_CP_INPUT_ERROR_##sym,
1182 foreach_lisp_cp_input_error
1184 LISP_CP_INPUT_N_ERROR,
1185 } lisp_cp_input_error_t;
1189 LISP_CP_INPUT_NEXT_DROP,
1190 LISP_CP_INPUT_N_NEXT,
1191 } lisp_cp_input_next_t;
1195 gid_address_t dst_eid;
1196 ip4_address_t map_resolver_ip;
1197 } lisp_cp_input_trace_t;
1200 format_lisp_cp_input_trace (u8 * s, va_list * args)
1202 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1203 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1204 CLIB_UNUSED(lisp_cp_input_trace_t * t) = va_arg (*args, lisp_cp_input_trace_t *);
1206 s = format (s, "LISP-CP-INPUT: TODO");
1210 ip_interface_address_t *
1211 ip_interface_get_first_interface_address (ip_lookup_main_t *lm, u32 sw_if_index,
1214 vnet_main_t *vnm = vnet_get_main ();
1215 vnet_sw_interface_t * swif = vnet_get_sw_interface (vnm, sw_if_index);
1216 if (loop && swif->flags & VNET_SW_INTERFACE_FLAG_UNNUMBERED)
1217 sw_if_index = swif->unnumbered_sw_if_index;
1219 (vec_len((lm)->if_address_pool_index_by_sw_if_index) > (sw_if_index)) ?
1220 vec_elt((lm)->if_address_pool_index_by_sw_if_index, (sw_if_index)) :
1222 return pool_elt_at_index((lm)->if_address_pool, ia);
1226 ip_interface_get_first_ip_addres (ip_lookup_main_t *lm, u32 sw_if_index,
1229 ip_interface_address_t * ia = ip_interface_get_first_interface_address (
1230 lm, sw_if_index, loop);
1231 return ip_interface_address_get_address (lm, ia);
1235 del_fwd_entry (lisp_cp_main_t * lcm, u32 src_map_index,
1238 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1239 fwd_entry_t * fe = 0;
1241 memset(a, 0, sizeof(*a));
1243 feip = hash_get(lcm->fwd_entry_by_mapping_index, dst_map_index);
1247 fe = pool_elt_at_index(lcm->fwd_entry_pool, feip[0]);
1249 /* delete dp fwd entry */
1252 a->dlocator = fe->dst_loc;
1253 a->slocator = fe->src_loc;
1254 a->iid = 0; // XXX should be part of mapping/eid
1255 gid_address_copy(&a->deid, &fe->deid);
1257 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
1259 /* delete entry in fwd table */
1260 hash_unset(lcm->fwd_entry_by_mapping_index, dst_map_index);
1261 pool_put(lcm->fwd_entry_pool, fe);
1265 add_fwd_entry (lisp_cp_main_t* lcm, u32 src_map_index, u32 dst_map_index)
1267 mapping_t * src_map, * dst_map;
1268 locator_set_t * dst_ls, * src_ls;
1272 vnet_lisp_gpe_add_del_fwd_entry_args_t _a, * a = &_a;
1273 memset (a, 0, sizeof(*a));
1275 /* remove entry if it already exists */
1276 feip = hash_get (lcm->fwd_entry_by_mapping_index, dst_map_index);
1278 del_fwd_entry (lcm, src_map_index, dst_map_index);
1280 src_map = pool_elt_at_index (lcm->mapping_pool, src_map_index);
1281 dst_map = pool_elt_at_index (lcm->mapping_pool, dst_map_index);
1283 /* XXX simple forwarding policy: first lowest (value) priority locator */
1284 dst_ls = pool_elt_at_index (lcm->locator_set_pool,
1285 dst_map->locator_set_index);
1286 for (i = 0; i < vec_len (dst_ls->locator_indices); i++)
1288 u32 li = vec_elt (dst_ls->locator_indices, i);
1289 locator_t * l = pool_elt_at_index (lcm->locator_pool, li);
1290 if (l->priority < minp && gid_address_type(&l->address) == IP_PREFIX)
1298 src_ls = pool_elt_at_index (lcm->locator_set_pool,
1299 src_map->locator_set_index);
1300 for (i = 0; i < vec_len (src_ls->locator_indices); i++)
1302 u32 li = vec_elt (src_ls->locator_indices, i);
1303 locator_t * sl = pool_elt_at_index (lcm->locator_pool, li);
1305 if (ip_addr_version(&gid_address_ip(&dl->address)) == IP4)
1308 l4 = ip_interface_get_first_ip_addres (&lcm->im4->lookup_main,
1310 1 /* unnumbered */);
1311 ip_addr_v4(&a->slocator) = *l4;
1312 ip_addr_version(&a->slocator) = IP4;
1317 l6 = ip_interface_get_first_ip_addres (&lcm->im6->lookup_main,
1319 1 /* unnumbered */);
1320 ip_addr_v6(&a->slocator) = *l6;
1321 ip_addr_version(&a->slocator) = IP6;
1325 /* insert data plane forwarding entry */
1329 a->dlocator = gid_address_ip(&dl->address);
1333 a->action = dst_map->action;
1336 gid_address_copy (&a->deid, &dst_map->eid);
1337 a->iid = 0; // XXX should be part of mapping/eid
1338 u8 ipver = ip_prefix_version(&gid_address_ippref(&a->deid));
1339 a->decap_next_index = (ipver == IP4) ?
1340 LISP_GPE_INPUT_NEXT_IP4_INPUT : LISP_GPE_INPUT_NEXT_IP6_INPUT;
1341 /* XXX tunnels work only with IP4 now */
1342 vnet_lisp_gpe_add_del_fwd_entry (a, &sw_if_index);
1344 /* add tunnel to fwd entry table XXX check return value from DP insertion */
1346 pool_get (lcm->fwd_entry_pool, fe);
1347 fe->dst_loc = a->dlocator;
1348 fe->src_loc = a->slocator;
1349 gid_address_copy (&fe->deid, &a->deid);
1350 hash_set (lcm->fwd_entry_by_mapping_index, dst_map_index,
1351 fe - lcm->fwd_entry_pool);
1354 /* return 0 if the two locator sets are identical 1 otherwise */
1356 compare_locators (lisp_cp_main_t *lcm, u32 * old_ls_indexes,
1357 locator_t * new_locators)
1360 locator_t * old_loc, * new_loc;
1362 if (vec_len (old_ls_indexes) != vec_len(new_locators))
1365 for (i = 0; i < vec_len(new_locators); i++)
1367 old_li = vec_elt(old_ls_indexes, i);
1368 old_loc = pool_elt_at_index(lcm->locator_pool, old_li);
1370 new_loc = vec_elt_at_index(new_locators, i);
1372 if (locator_cmp (old_loc, new_loc))
1379 process_map_reply (lisp_cp_main_t * lcm, vlib_buffer_t * b)
1381 u32 len = 0, i, ls_index = 0;
1383 vnet_lisp_add_del_locator_set_args_t _ls_arg, * ls_arg = &_ls_arg;
1384 vnet_lisp_add_del_mapping_args_t _m_args, * m_args = &_m_args;
1385 pending_map_request_t * pmr;
1387 map_reply_hdr_t * mrep_hdr;
1389 u32 dst_map_index, mi;
1392 mrep_hdr = vlib_buffer_get_current (b);
1394 /* Check pending requests table and nonce */
1395 nonce = MREP_NONCE(mrep_hdr);
1396 pmr_index = hash_get(lcm->pending_map_requests_by_nonce, nonce);
1399 clib_warning("No pending map-request entry with nonce %lu!", nonce);
1402 pmr = pool_elt_at_index(lcm->pending_map_requests_pool, pmr_index[0]);
1404 vlib_buffer_pull (b, sizeof(*mrep_hdr));
1406 for (i = 0; i < MREP_REC_COUNT(mrep_hdr); i++)
1408 memset (ls_arg, 0, sizeof(*ls_arg));
1409 memset (m_args, 0, sizeof(*m_args));
1411 h = vlib_buffer_get_current (b);
1412 m_args->ttl = clib_net_to_host_u32 (MAP_REC_TTL(h));
1413 m_args->action = MAP_REC_ACTION(h);
1414 m_args->authoritative = MAP_REC_AUTH(h);
1416 len = lisp_msg_parse_mapping_record (b, &m_args->deid, &ls_arg->locators,
1420 clib_warning ("Failed to parse mapping record!");
1421 vec_free(ls_arg->locators);
1425 mi = gid_dictionary_lookup (&lcm->mapping_index_by_gid, &m_args->deid);
1427 /* if mapping already exists, decide if locators (and forwarding) should
1428 * be updated and be done */
1431 mapping_t * old_map;
1432 locator_set_t * old_ls;
1433 old_map = pool_elt_at_index(lcm->mapping_pool, mi);
1435 /* update mapping attributes */
1436 old_map->action = m_args->action;
1437 old_map->authoritative = m_args->authoritative;
1438 old_map->ttl = m_args->ttl;
1440 old_ls = pool_elt_at_index(lcm->locator_set_pool,
1441 old_map->locator_set_index);
1442 /* if the two locators are not equal, update them and forwarding
1443 * otherwise there's nothing to be done */
1444 if (compare_locators (lcm, old_ls->locator_indices, ls_arg->locators))
1446 /* set locator-set index to overwrite */
1448 ls_arg->index = old_map->locator_set_index;
1449 vnet_lisp_add_del_locator_set (ls_arg, 0);
1450 add_fwd_entry (lcm, pmr->src_mapping_index, mi);
1456 /* add locator-set */
1459 vnet_lisp_add_del_locator_set (ls_arg, &ls_index);
1463 m_args->locator_set_index = ls_index;
1464 vnet_lisp_add_del_mapping (m_args, &dst_map_index);
1466 /* add forwarding tunnel */
1467 add_fwd_entry (lcm, pmr->src_mapping_index, dst_map_index);
1469 vec_free(ls_arg->locators);
1472 /* remove pending map request entry */
1473 hash_unset(lcm->pending_map_requests_by_nonce, nonce);
1474 pool_put(lcm->pending_map_requests_pool, pmr);
1478 process_map_request (vlib_main_t * vm, lisp_cp_main_t * lcm, vlib_buffer_t * b)
1480 map_request_hdr_t * mreq_hdr;
1481 gid_address_t src, dst;
1484 gid_address_t * itr_rlocs = 0;
1486 mreq_hdr = vlib_buffer_get_current (b);
1487 vlib_buffer_pull (b, sizeof(*mreq_hdr));
1489 // nonce = MREQ_NONCE(mreq_hdr);
1491 if (!MREQ_SMR(mreq_hdr)) {
1492 clib_warning("Only SMR Map-Requests supported for now!");
1497 len = lisp_msg_parse_addr (b, &src);
1501 /* for now we don't do anything with the itr's rlocs */
1502 len = lisp_msg_parse_itr_rlocs (b, &itr_rlocs, MREQ_ITR_RLOC_COUNT(mreq_hdr) + 1);
1506 /* parse eid records and send SMR-invoked map-requests */
1507 for (i = 0; i < MREQ_REC_COUNT(mreq_hdr); i++)
1509 memset(&dst, 0, sizeof(dst));
1510 len = lisp_msg_parse_eid_rec (b, &dst);
1513 clib_warning("Can't parse map-request EID-record");
1516 /* send SMR-invoked map-requests */
1517 send_encapsulated_map_request (vm, lcm, &dst, &src, /* invoked */ 1);
1522 lisp_cp_input (vlib_main_t * vm, vlib_node_runtime_t * node,
1523 vlib_frame_t * from_frame)
1525 u32 n_left_from, * from, * to_next_drop;
1526 lisp_msg_type_e type;
1527 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1529 from = vlib_frame_vector_args (from_frame);
1530 n_left_from = from_frame->n_vectors;
1533 while (n_left_from > 0)
1535 u32 n_left_to_next_drop;
1537 vlib_get_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP,
1538 to_next_drop, n_left_to_next_drop);
1539 while (n_left_from > 0 && n_left_to_next_drop > 0)
1547 to_next_drop[0] = bi0;
1549 n_left_to_next_drop -= 1;
1551 b0 = vlib_get_buffer (vm, bi0);
1553 type = lisp_msg_type(vlib_buffer_get_current (b0));
1556 case LISP_MAP_REPLY:
1557 process_map_reply (lcm, b0);
1559 case LISP_MAP_REQUEST:
1560 process_map_request(vm, lcm, b0);
1563 clib_warning("Unsupported LISP message type %d", type);
1567 b0->error = node->errors[LISP_CP_INPUT_ERROR_DROP];
1569 if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
1575 vlib_put_next_frame (vm, node, LISP_CP_INPUT_NEXT_DROP, n_left_to_next_drop);
1577 return from_frame->n_vectors;
1580 VLIB_REGISTER_NODE (lisp_cp_input_node) = {
1581 .function = lisp_cp_input,
1582 .name = "lisp-cp-input",
1583 .vector_size = sizeof (u32),
1584 .format_trace = format_lisp_cp_input_trace,
1585 .type = VLIB_NODE_TYPE_INTERNAL,
1587 .n_errors = LISP_CP_INPUT_N_ERROR,
1588 .error_strings = lisp_cp_input_error_strings,
1590 .n_next_nodes = LISP_CP_INPUT_N_NEXT,
1593 [LISP_CP_INPUT_NEXT_DROP] = "error-drop",
1598 lisp_cp_init (vlib_main_t *vm)
1600 lisp_cp_main_t * lcm = vnet_lisp_cp_get_main();
1601 clib_error_t * error = 0;
1603 if ((error = vlib_call_init_function (vm, lisp_gpe_init)))
1606 lcm->im4 = &ip4_main;
1607 lcm->im6 = &ip6_main;
1608 lcm->vlib_main = vm;
1609 lcm->vnet_main = vnet_get_main();
1611 gid_dictionary_init (&lcm->mapping_index_by_gid);
1612 gid_dictionary_init (&lcm->mapping_index_by_gid);
1614 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp,
1615 lisp_cp_input_node.index, 1 /* is_ip4 */);
1616 udp_register_dst_port (vm, UDP_DST_PORT_lisp_cp6,
1617 lisp_cp_input_node.index, 0 /* is_ip4 */);
1622 VLIB_INIT_FUNCTION(lisp_cp_init);