/* *------------------------------------------------------------------ * one_api.c - Overlay Network Engine API * * Copyright (c) 2016-2017 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #define vl_api_one_remote_locator_t_endian vl_noop_handler #define vl_api_one_remote_locator_t_print vl_noop_handler #define vl_api_one_local_locator_t_endian vl_noop_handler #define vl_api_one_local_locator_t_print vl_noop_handler #define vl_api_one_add_del_locator_set_t_endian vl_noop_handler #define vl_api_one_add_del_locator_set_t_print vl_noop_handler #define vl_api_one_add_del_remote_mapping_t_endian vl_noop_handler #define vl_api_one_add_del_remote_mapping_t_print vl_noop_handler #define vl_api_one_add_del_locator_set_t_endian vl_noop_handler #define vl_api_one_add_del_locator_set_t_print vl_noop_handler #define vl_api_one_add_del_remote_mapping_t_endian vl_noop_handler #define vl_api_one_add_del_remote_mapping_t_print vl_noop_handler #define vl_api_one_l2_arp_entry_t_endian vl_noop_handler #define vl_api_one_l2_arp_entry_t_print vl_noop_handler #define vl_api_one_add_del_l2_arp_entry vl_noop_handler #define vl_api_one_l2_arp_bd_get vl_noop_handler #define vl_api_one_ndp_entry_t_endian vl_noop_handler #define vl_api_one_ndp_entry_t_print vl_noop_handler #define vl_api_one_ndp_entries_get_reply_t_endian vl_noop_handler #define vl_api_one_ndp_entries_get_reply_t_print vl_noop_handler #define vl_typedefs /* define message structures */ #include #undef vl_typedefs #define vl_endianfun /* define message structures */ #include #undef vl_endianfun /* instantiate all the print functions we know about */ #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) #define vl_printfun #include #undef vl_printfun #include #define REPLY_DETAILS(t, body) \ do { \ vl_api_registration_t * reg; \ rv = vl_msg_api_pd_handler (mp, rv); \ reg = vl_api_client_index_to_registration (mp->client_index); \ if (!reg) \ return; \ \ rmp = vl_msg_api_alloc (sizeof (*rmp)); \ rmp->_vl_msg_id = ntohs((t)); \ rmp->context = mp->context; \ do {body;} while (0); \ vl_api_send_msg (reg, (u8 *)&rmp); \ } while(0); #define foreach_vpe_api_msg \ _(ONE_ADD_DEL_LOCATOR_SET, one_add_del_locator_set) \ _(ONE_ADD_DEL_LOCATOR, one_add_del_locator) \ _(ONE_ADD_DEL_LOCAL_EID, one_add_del_local_eid) \ _(ONE_ADD_DEL_MAP_RESOLVER, one_add_del_map_resolver) \ _(ONE_ADD_DEL_MAP_SERVER, one_add_del_map_server) \ _(ONE_ENABLE_DISABLE, one_enable_disable) \ _(ONE_RLOC_PROBE_ENABLE_DISABLE, one_rloc_probe_enable_disable) \ _(ONE_MAP_REGISTER_ENABLE_DISABLE, one_map_register_enable_disable) \ _(ONE_MAP_REGISTER_FALLBACK_THRESHOLD, \ one_map_register_fallback_threshold) \ _(ONE_ADD_DEL_REMOTE_MAPPING, one_add_del_remote_mapping) \ _(ONE_ADD_DEL_ADJACENCY, one_add_del_adjacency) \ _(ONE_PITR_SET_LOCATOR_SET, one_pitr_set_locator_set) \ _(ONE_NSH_SET_LOCATOR_SET, one_nsh_set_locator_set) \ _(ONE_MAP_REQUEST_MODE, one_map_request_mode) \ _(ONE_EID_TABLE_ADD_DEL_MAP, one_eid_table_add_del_map) \ _(ONE_LOCATOR_SET_DUMP, one_locator_set_dump) \ _(ONE_LOCATOR_DUMP, one_locator_dump) \ _(ONE_EID_TABLE_DUMP, one_eid_table_dump) \ _(ONE_MAP_RESOLVER_DUMP, one_map_resolver_dump) \ _(ONE_MAP_SERVER_DUMP, one_map_server_dump) \ _(ONE_EID_TABLE_MAP_DUMP, one_eid_table_map_dump) \ _(ONE_EID_TABLE_VNI_DUMP, one_eid_table_vni_dump) \ _(ONE_ADJACENCIES_GET, one_adjacencies_get) \ _(ONE_MAP_REGISTER_SET_TTL, one_map_register_set_ttl) \ _(SHOW_ONE_NSH_MAPPING, show_one_nsh_mapping) \ _(SHOW_ONE_RLOC_PROBE_STATE, show_one_rloc_probe_state) \ _(SHOW_ONE_MAP_REGISTER_STATE, show_one_map_register_state) \ _(SHOW_ONE_MAP_REGISTER_TTL, show_one_map_register_ttl) \ _(SHOW_ONE_MAP_REGISTER_FALLBACK_THRESHOLD, \ show_one_map_register_fallback_threshold) \ _(SHOW_ONE_STATUS, show_one_status) \ _(ONE_ADD_DEL_MAP_REQUEST_ITR_RLOCS, \ one_add_del_map_request_itr_rlocs) \ _(ONE_GET_MAP_REQUEST_ITR_RLOCS, one_get_map_request_itr_rlocs) \ _(SHOW_ONE_PITR, show_one_pitr) \ _(SHOW_ONE_MAP_REQUEST_MODE, show_one_map_request_mode) \ _(ONE_USE_PETR, one_use_petr) \ _(SHOW_ONE_USE_PETR, show_one_use_petr) \ _(SHOW_ONE_STATS_ENABLE_DISABLE, show_one_stats_enable_disable) \ _(ONE_STATS_ENABLE_DISABLE, one_stats_enable_disable) \ _(ONE_STATS_DUMP, one_stats_dump) \ _(ONE_STATS_FLUSH, one_stats_flush) \ _(ONE_L2_ARP_BD_GET, one_l2_arp_bd_get) \ _(ONE_L2_ARP_ENTRIES_GET, one_l2_arp_entries_get) \ _(ONE_ADD_DEL_L2_ARP_ENTRY, one_add_del_l2_arp_entry) \ _(ONE_ADD_DEL_NDP_ENTRY, one_add_del_ndp_entry) \ _(ONE_NDP_BD_GET, one_ndp_bd_get) \ _(ONE_NDP_ENTRIES_GET, one_ndp_entries_get) \ _(ONE_SET_TRANSPORT_PROTOCOL, one_set_transport_protocol) \ _(ONE_GET_TRANSPORT_PROTOCOL, one_get_transport_protocol) \ _(ONE_ENABLE_DISABLE_XTR_MODE, one_enable_disable_xtr_mode) \ _(ONE_SHOW_XTR_MODE, one_show_xtr_mode) \ _(ONE_ENABLE_DISABLE_PITR_MODE, one_enable_disable_pitr_mode) \ _(ONE_SHOW_PITR_MODE, one_show_pitr_mode) \ _(ONE_ENABLE_DISABLE_PETR_MODE, one_enable_disable_petr_mode) \ _(ONE_SHOW_PETR_MODE, one_show_petr_mode) \ static locator_t * unformat_one_locs (vl_api_one_remote_locator_t * rmt_locs, u32 rloc_num) { u32 i; locator_t *locs = 0, loc; vl_api_one_remote_locator_t *r; for (i = 0; i < rloc_num; i++) { /* remote locators */ r = &rmt_locs[i]; clib_memset (&loc, 0, sizeof (loc)); gid_address_ip_set (&loc.address, &r->addr, r->is_ip4 ? IP4 : IP6); loc.priority = r->priority; loc.weight = r->weight; vec_add1 (locs, loc); } return locs; } static void vl_api_one_map_register_set_ttl_t_handler (vl_api_one_map_register_set_ttl_t * mp) { vl_api_one_map_register_set_ttl_reply_t *rmp; int rv = 0; mp->ttl = clib_net_to_host_u32 (mp->ttl); rv = vnet_lisp_map_register_set_ttl (mp->ttl); REPLY_MACRO (VL_API_ONE_MAP_REGISTER_SET_TTL_REPLY); } static void vl_api_show_one_map_register_ttl_t_handler (vl_api_show_one_map_register_ttl_t * mp) { vl_api_show_one_map_register_ttl_reply_t *rmp; int rv = 0; u32 ttl = vnet_lisp_map_register_get_ttl (); /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_SHOW_ONE_MAP_REGISTER_TTL_REPLY, ({ rmp->ttl = clib_host_to_net_u32 (ttl); })); /* *INDENT-ON* */ } static void vl_api_one_add_del_locator_set_t_handler (vl_api_one_add_del_locator_set_t * mp) { vl_api_one_add_del_locator_set_reply_t *rmp; int rv = 0; vnet_lisp_add_del_locator_set_args_t _a, *a = &_a; locator_t locator; vl_api_one_local_locator_t *ls_loc; u32 ls_index = ~0, locator_num; u8 *locator_name = NULL; int i; clib_memset (a, 0, sizeof (a[0])); mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0; locator_name = format (0, "%s", mp->locator_set_name); vec_terminate_c_string (locator_name); a->name = locator_name; a->is_add = mp->is_add; a->local = 1; locator_num = clib_net_to_host_u32 (mp->locator_num); clib_memset (&locator, 0, sizeof (locator)); for (i = 0; i < locator_num; i++) { ls_loc = &mp->locators[i]; VALIDATE_SW_IF_INDEX (ls_loc); locator.sw_if_index = htonl (ls_loc->sw_if_index); locator.priority = ls_loc->priority; locator.weight = ls_loc->weight; locator.local = 1; vec_add1 (a->locators, locator); } rv = vnet_lisp_add_del_locator_set (a, &ls_index); BAD_SW_IF_INDEX_LABEL; vec_free (locator_name); vec_free (a->locators); /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_ONE_ADD_DEL_LOCATOR_SET_REPLY, ({ rmp->ls_index = clib_host_to_net_u32 (ls_index); })); /* *INDENT-ON* */ } static void vl_api_one_add_del_locator_t_handler (vl_api_one_add_del_locator_t * mp) { vl_api_one_add_del_locator_reply_t *rmp; int rv = 0; locator_t locator, *locators = NULL; vnet_lisp_add_del_locator_set_args_t _a, *a = &_a; u32 ls_index = ~0; u8 *locator_name = NULL; clib_memset (&locator, 0, sizeof (locator)); clib_memset (a, 0, sizeof (a[0])); locator.sw_if_index = ntohl (mp->sw_if_index); locator.priority = mp->priority; locator.weight = mp->weight; locator.local = 1; vec_add1 (locators, locator); mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0; locator_name = format (0, "%s", mp->locator_set_name); vec_terminate_c_string (locator_name); a->name = locator_name; a->locators = locators; a->is_add = mp->is_add; a->local = 1; rv = vnet_lisp_add_del_locator (a, NULL, &ls_index); vec_free (locators); vec_free (locator_name); REPLY_MACRO (VL_API_ONE_ADD_DEL_LOCATOR_REPLY); } typedef struct { u32 spi; u8 si; } __attribute__ ((__packed__)) lisp_nsh_api_t; static int unformat_one_eid_api (gid_address_t * dst, u32 vni, u8 type, void *src, u8 len) { lisp_nsh_api_t *nsh; switch (type) { case 0: /* ipv4 */ gid_address_type (dst) = GID_ADDR_IP_PREFIX; gid_address_ip_set (dst, src, IP4); gid_address_ippref_len (dst) = len; ip_prefix_normalize (&gid_address_ippref (dst)); break; case 1: /* ipv6 */ gid_address_type (dst) = GID_ADDR_IP_PREFIX; gid_address_ip_set (dst, src, IP6); gid_address_ippref_len (dst) = len; ip_prefix_normalize (&gid_address_ippref (dst)); break; case 2: /* l2 mac */ gid_address_type (dst) = GID_ADDR_MAC; clib_memcpy (&gid_address_mac (dst), src, 6); break; case 3: /* NSH */ gid_address_type (dst) = GID_ADDR_NSH; nsh = src; gid_address_nsh_spi (dst) = clib_net_to_host_u32 (nsh->spi); gid_address_nsh_si (dst) = nsh->si; break; default: /* unknown type */ return VNET_API_ERROR_INVALID_VALUE; } gid_address_vni (dst) = vni; return 0; } static void vl_api_one_add_del_local_eid_t_handler (vl_api_one_add_del_local_eid_t * mp) { vl_api_one_add_del_local_eid_reply_t *rmp; lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); int rv = 0; gid_address_t _eid, *eid = &_eid; uword *p = NULL; u32 locator_set_index = ~0, map_index = ~0; vnet_lisp_add_del_mapping_args_t _a, *a = &_a; u8 *name = NULL, *key = NULL; clib_memset (a, 0, sizeof (a[0])); clib_memset (eid, 0, sizeof (eid[0])); rv = unformat_one_eid_api (eid, clib_net_to_host_u32 (mp->vni), mp->eid_type, mp->eid, mp->prefix_len); if (rv) goto out; if (gid_address_type (eid) == GID_ADDR_NSH) { rv = VNET_API_ERROR_INVALID_VALUE; goto out; } mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0; name = format (0, "%s", mp->locator_set_name); vec_terminate_c_string (name); p = hash_get_mem (lcm->locator_set_index_by_name, name); if (!p) { rv = VNET_API_ERROR_INVALID_VALUE; goto out; } locator_set_index = p[0]; if (*mp->key) key = format (0, "%s", mp->key); /* XXX treat batch configuration */ a->is_add = mp->is_add; gid_address_copy (&a->eid, eid); a->locator_set_index = locator_set_index; a->local = 1; a->key = key; a->key_id = clib_net_to_host_u16 (mp->key_id); rv = vnet_lisp_add_del_local_mapping (a, &map_index); out: vec_free (name); vec_free (key); gid_address_free (&a->eid); REPLY_MACRO (VL_API_ONE_ADD_DEL_LOCAL_EID_REPLY); } static void vl_api_one_eid_table_add_del_map_t_handler (vl_api_one_eid_table_add_del_map_t * mp) { vl_api_one_eid_table_add_del_map_reply_t *rmp; int rv = 0; rv = vnet_lisp_eid_table_map (clib_net_to_host_u32 (mp->vni), clib_net_to_host_u32 (mp->dp_table), mp->is_l2, mp->is_add); REPLY_MACRO (VL_API_ONE_EID_TABLE_ADD_DEL_MAP_REPLY)} static void vl_api_one_add_del_map_server_t_handler (vl_api_one_add_del_map_server_t * mp) { vl_api_one_add_del_map_server_reply_t *rmp; int rv = 0; ip_address_t addr; clib_memset (&addr, 0, sizeof (addr)); ip_address_set (&addr, mp->ip_address, mp->is_ipv6 ? IP6 : IP4); rv = vnet_lisp_add_del_map_server (&addr, mp->is_add); REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_SERVER_REPLY); } static void vl_api_one_add_del_map_resolver_t_handler (vl_api_one_add_del_map_resolver_t * mp) { vl_api_one_add_del_map_resolver_reply_t *rmp; int rv = 0; vnet_lisp_add_del_map_resolver_args_t _a, *a = &_a; clib_memset (a, 0, sizeof (a[0])); a->is_add = mp->is_add; ip_address_set (&a->address, mp->ip_address, mp->is_ipv6 ? IP6 : IP4); rv = vnet_lisp_add_del_map_resolver (a); REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_RESOLVER_REPLY); } static void vl_api_one_map_register_enable_disable_t_handler (vl_api_one_map_register_enable_disable_t * mp) { vl_api_one_map_register_enable_disable_reply_t *rmp; int rv = 0; vnet_lisp_map_register_enable_disable (mp->is_enabled); REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY); } static void vl_api_one_rloc_probe_enable_disable_t_handler (vl_api_one_rloc_probe_enable_disable_t * mp) { vl_api_one_rloc_probe_enable_disable_reply_t *rmp; int rv = 0; vnet_lisp_rloc_probe_enable_disable (mp->is_enabled); REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY); } static void vl_api_one_enable_disable_t_handler (vl_api_one_enable_disable_t * mp) { vl_api_one_enable_disable_reply_t *rmp; int rv = 0; vnet_lisp_enable_disable (mp->is_en); REPLY_MACRO (VL_API_ONE_ENABLE_DISABLE_REPLY); } static void vl_api_show_one_map_request_mode_t_handler (vl_api_show_one_map_request_mode_t * mp) { int rv = 0; vl_api_show_one_map_request_mode_reply_t *rmp; /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_SHOW_ONE_MAP_REQUEST_MODE_REPLY, ({ rmp->mode = vnet_lisp_get_map_request_mode (); })); /* *INDENT-ON* */ } static void vl_api_one_map_request_mode_t_handler (vl_api_one_map_request_mode_t * mp) { vl_api_one_map_request_mode_reply_t *rmp; int rv = 0; rv = vnet_lisp_set_map_request_mode (mp->mode); REPLY_MACRO (VL_API_ONE_MAP_REQUEST_MODE_REPLY); } static void vl_api_one_nsh_set_locator_set_t_handler (vl_api_one_nsh_set_locator_set_t * mp) { vl_api_one_nsh_set_locator_set_reply_t *rmp; int rv = 0; u8 *ls_name = 0; mp->ls_name[sizeof (mp->ls_name) - 1] = 0; ls_name = format (0, "%s", mp->ls_name); vec_terminate_c_string (ls_name); rv = vnet_lisp_nsh_set_locator_set (ls_name, mp->is_add); vec_free (ls_name); REPLY_MACRO (VL_API_ONE_PITR_SET_LOCATOR_SET_REPLY); } static void vl_api_one_pitr_set_locator_set_t_handler (vl_api_one_pitr_set_locator_set_t * mp) { vl_api_one_pitr_set_locator_set_reply_t *rmp; int rv = 0; u8 *ls_name = 0; mp->ls_name[sizeof (mp->ls_name) - 1] = 0; ls_name = format (0, "%s", mp->ls_name); vec_terminate_c_string (ls_name); rv = vnet_lisp_pitr_set_locator_set (ls_name, mp->is_add); vec_free (ls_name); REPLY_MACRO (VL_API_ONE_PITR_SET_LOCATOR_SET_REPLY); } static void vl_api_one_use_petr_t_handler (vl_api_one_use_petr_t * mp) { vl_api_one_use_petr_reply_t *rmp; int rv = 0; ip_address_t addr; ip_address_set (&addr, &mp->address, mp->is_ip4 ? IP4 : IP6); rv = vnet_lisp_use_petr (&addr, mp->is_add); REPLY_MACRO (VL_API_ONE_USE_PETR_REPLY); } static void vl_api_show_one_use_petr_t_handler (vl_api_show_one_use_petr_t * mp) { vl_api_show_one_use_petr_reply_t *rmp = NULL; lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); mapping_t *m; locator_set_t *ls = 0; int rv = 0; locator_t *loc = 0; u8 status = 0; gid_address_t addr; clib_memset (&addr, 0, sizeof (addr)); status = lcm->flags & LISP_FLAG_USE_PETR; if (status) { m = pool_elt_at_index (lcm->mapping_pool, lcm->petr_map_index); if (~0 != m->locator_set_index) { ls = pool_elt_at_index (lcm->locator_set_pool, m->locator_set_index); loc = pool_elt_at_index (lcm->locator_pool, ls->locator_indices[0]); gid_address_copy (&addr, &loc->address); } } /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_SHOW_ONE_USE_PETR_REPLY, { rmp->status = status; ip_address_t *ip = &gid_address_ip (&addr); switch (ip_addr_version (ip)) { case IP4: clib_memcpy (rmp->address, &ip_addr_v4 (ip), sizeof (ip_addr_v4 (ip))); break; case IP6: clib_memcpy (rmp->address, &ip_addr_v6 (ip), sizeof (ip_addr_v6 (ip))); break; default: ASSERT (0); } rmp->is_ip4 = (gid_address_ip_version (&addr) == IP4); }); /* *INDENT-ON* */ } static void vl_api_one_add_del_map_request_itr_rlocs_t_handler (vl_api_one_add_del_map_request_itr_rlocs_t * mp) { vl_api_one_add_del_map_request_itr_rlocs_reply_t *rmp; int rv = 0; u8 *locator_set_name = NULL; vnet_lisp_add_del_mreq_itr_rloc_args_t _a, *a = &_a; mp->locator_set_name[sizeof (mp->locator_set_name) - 1] = 0; locator_set_name = format (0, "%s", mp->locator_set_name); vec_terminate_c_string (locator_set_name); a->is_add = mp->is_add; a->locator_set_name = locator_set_name; rv = vnet_lisp_add_del_mreq_itr_rlocs (a); vec_free (locator_set_name); REPLY_MACRO (VL_API_ONE_ADD_DEL_MAP_REQUEST_ITR_RLOCS_REPLY); } static void vl_api_one_add_del_remote_mapping_t_handler (vl_api_one_add_del_remote_mapping_t * mp) { locator_t *rlocs = 0; vl_api_one_add_del_remote_mapping_reply_t *rmp; int rv = 0; gid_address_t _eid, *eid = &_eid; u32 rloc_num = clib_net_to_host_u32 (mp->rloc_num); clib_memset (eid, 0, sizeof (eid[0])); rv = unformat_one_eid_api (eid, clib_net_to_host_u32 (mp->vni), mp->eid_type, mp->eid, mp->eid_len); if (rv) goto send_reply; rlocs = unformat_one_locs (mp->rlocs, rloc_num); if (!mp->is_add) { vnet_lisp_add_del_adjacency_args_t _a, *a = &_a; clib_memset (a, 0, sizeof (a[0])); gid_address_copy (&a->reid, eid); a->is_add = 0; rv = vnet_lisp_add_del_adjacency (a); if (rv) { goto out; } } /* NOTE: for now this works as a static remote mapping, i.e., * not authoritative and ttl infinite. */ if (mp->is_add) { vnet_lisp_add_del_mapping_args_t _m_args, *m_args = &_m_args; clib_memset (m_args, 0, sizeof (m_args[0])); gid_address_copy (&m_args->eid, eid); m_args->action = mp->action; m_args->is_static = 1; m_args->ttl = ~0; m_args->authoritative = 0; rv = vnet_lisp_add_mapping (m_args, rlocs, NULL, NULL); } else rv = vnet_lisp_del_mapping (eid, NULL); if (mp->del_all) vnet_lisp_clear_all_remote_adjacencies (); out: vec_free (rlocs); send_reply: REPLY_MACRO (VL_API_ONE_ADD_DEL_REMOTE_MAPPING_REPLY); } static void vl_api_one_add_del_adjacency_t_handler (vl_api_one_add_del_adjacency_t * mp) { vl_api_one_add_del_adjacency_reply_t *rmp; vnet_lisp_add_del_adjacency_args_t _a, *a = &_a; int rv = 0; clib_memset (a, 0, sizeof (a[0])); rv = unformat_one_eid_api (&a->leid, clib_net_to_host_u32 (mp->vni), mp->eid_type, mp->leid, mp->leid_len); rv |= unformat_one_eid_api (&a->reid, clib_net_to_host_u32 (mp->vni), mp->eid_type, mp->reid, mp->reid_len); if (rv) goto send_reply; a->is_add = mp->is_add; rv = vnet_lisp_add_del_adjacency (a); send_reply: REPLY_MACRO (VL_API_ONE_ADD_DEL_ADJACENCY_REPLY); } static void send_one_locator_details (lisp_cp_main_t * lcm, locator_t * loc, vl_api_registration_t * reg, u32 context) { vl_api_one_locator_details_t *rmp; rmp = vl_msg_api_alloc (sizeof (*rmp)); clib_memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_ONE_LOCATOR_DETAILS); rmp->context = context; rmp->local = loc->local; if (loc->local) { rmp->sw_if_index = ntohl (loc->sw_if_index); } else { rmp->is_ipv6 = gid_address_ip_version (&loc->address); ip_address_copy_addr (rmp->ip_address, &gid_address_ip (&loc->address)); } rmp->priority = loc->priority; rmp->weight = loc->weight; vl_api_send_msg (reg, (u8 *) rmp); } static void vl_api_one_locator_dump_t_handler (vl_api_one_locator_dump_t * mp) { u8 *ls_name = 0; vl_api_registration_t *reg; lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); locator_set_t *lsit = 0; locator_t *loc = 0; u32 ls_index = ~0, *locit = 0; uword *p = 0; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; if (mp->is_index_set) ls_index = htonl (mp->ls_index); else { /* make sure we get a proper C-string */ mp->ls_name[sizeof (mp->ls_name) - 1] = 0; ls_name = format (0, "%s", mp->ls_name); vec_terminate_c_string (ls_name); p = hash_get_mem (lcm->locator_set_index_by_name, ls_name); if (!p) goto out; ls_index = p[0]; } if (pool_is_free_index (lcm->locator_set_pool, ls_index)) return; lsit = pool_elt_at_index (lcm->locator_set_pool, ls_index); vec_foreach (locit, lsit->locator_indices) { loc = pool_elt_at_index (lcm->locator_pool, locit[0]); send_one_locator_details (lcm, loc, reg, mp->context); }; out: vec_free (ls_name); } static void send_one_locator_set_details (lisp_cp_main_t * lcm, locator_set_t * lsit, vl_api_registration_t * reg, u32 context, u32 ls_index) { vl_api_one_locator_set_details_t *rmp; u8 *str = 0; rmp = vl_msg_api_alloc (sizeof (*rmp)); clib_memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_ONE_LOCATOR_SET_DETAILS); rmp->context = context; rmp->ls_index = htonl (ls_index); if (lsit->local) { ASSERT (lsit->name != NULL); strncpy ((char *) rmp->ls_name, (char *) lsit->name, vec_len (lsit->name)); } else { str = format (0, "", ls_index); strncpy ((char *) rmp->ls_name, (char *) str, vec_len (str)); vec_free (str); } vl_api_send_msg (reg, (u8 *) rmp); } static void vl_api_one_locator_set_dump_t_handler (vl_api_one_locator_set_dump_t * mp) { vl_api_registration_t *reg; lisp_cp_main_t *lcm = vnet_lisp_cp_get_main (); locator_set_t *lsit = NULL; u8 filter; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; filter = mp->filter; /* *INDENT-OFF* */ pool_foreach (lsit, lcm->locator_set_pool, ({ if (filter && !((1 == filter && lsit->local) || (2 == filter && !lsit->local))) { continue; } send_one_locator_set_details (lcm, lsit, reg, mp->context, lsit - lcm->locator_set_pool); })); /* *INDENT-ON* */ } static void one_fid_put_api (u8 * dst, fid_address_t * src, u8 * prefix_length) { ASSERT
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * @file
 * @brief L2 LISP-GPE decap code.
 *
 */
#include <vlib/vlib.h>
#include <vnet/pg/pg.h>
#include <vnet/lisp-gpe/lisp_gpe.h>

typedef struct
{
  u32 next_index;
  u32 tunnel_index;
  u32 error;
  lisp_gpe_header_t h;
} lisp_gpe_rx_trace_t;

static u8 *
format_lisp_gpe_rx_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  lisp_gpe_rx_trace_t *t = va_arg (*args, lisp_gpe_rx_trace_t *);

  if (t->tunnel_index != ~0)
    {
      s = format (s, "LISP-GPE: tunnel %d next %d error %d", t->tunnel_index,
		  t->next_index, t->error);
    }
  else
    {
      s = format (s, "LISP-GPE: no tunnel next %d error %d\n", t->next_index,
		  t->error);
    }
  s = format (s, "\n  %U", format_lisp_gpe_header_with_length, &t->h,
	      (u32) sizeof (t->h) /* max size */ );
  return s;
}

static u32 next_proto_to_next_index[LISP_GPE_NEXT_PROTOS] = {
  LISP_GPE_INPUT_NEXT_DROP,
  LISP_GPE_INPUT_NEXT_IP4_INPUT,
  LISP_GPE_INPUT_NEXT_IP6_INPUT,
  LISP_GPE_INPUT_NEXT_L2_INPUT,
  LISP_GPE_INPUT_NEXT_DROP
};

always_inline u32
next_protocol_to_next_index (lisp_gpe_header_t * lgh, u8 * next_header)
{
  lisp_gpe_main_t *lgm = vnet_lisp_gpe_get_main ();

  /* lisp-gpe router */
  if (PREDICT_TRUE ((lgh->flags & LISP_GPE_FLAGS_P)
		    || GPE_ENCAP_VXLAN == lgm->encap_mode))
    {
      if (PREDICT_FALSE (lgh->next_protocol >= LISP_GPE_NEXT_PROTOS))
	return LISP_GPE_INPUT_NEXT_DROP;

      return next_proto_to_next_index[lgh->next_protocol];
    }
  /* legacy lisp router */
  else if ((lgh->flags & LISP_GPE_FLAGS_P) == 0)
    {
      ip4_header_t *iph = (ip4_header_t *) next_header;
      if ((iph->ip_version_and_header_length & 0xF0) == 0x40)
	return LISP_GPE_INPUT_NEXT_IP4_INPUT;
      else if ((iph->ip_version_and_header_length & 0xF0) == 0x60)
	return LISP_GPE_INPUT_NEXT_IP6_INPUT;
      else
	return LISP_GPE_INPUT_NEXT_DROP;
    }
  else
    return LISP_GPE_INPUT_NEXT_DROP;
}

always_inline tunnel_lookup_t *
next_index_to_iface (lisp_gpe_main_t * lgm, u32 next_index)
{
  if (LISP_GPE_INPUT_NEXT_IP4_INPUT == next_index
      || LISP_GPE_INPUT_NEXT_IP6_INPUT == next_index)
    return &lgm->l3_ifaces;
  else if (LISP_GPE_INPUT_NEXT_L2_INPUT == next_index)
    return &lgm->l2_ifaces;
  else if (LISP_GPE_INPUT_NEXT_NSH_INPUT == next_index)
    return &lgm->nsh_ifaces;
  clib_warning ("next_index not associated to an interface!");
  return 0;
}

static_always_inline void
incr_decap_stats (vnet_main_t * vnm, u32 thread_index, u32 length,
		  u32 sw_if_index, u32 * last_sw_if_index, u32 * n_packets,
		  u32 * n_bytes)
{
  vnet_interface_main_t *im;

  if (PREDICT_TRUE (sw_if_index == *last_sw_if_index))
    {
      *n_packets += 1;
      *n_bytes += length;
    }
  else
    {
      if (PREDICT_TRUE (*last_sw_if_index != ~0))
	{
	  im = &vnm->interface_main;

	  vlib_increment_combined_counter (im->combined_sw_if_counters +
					   VNET_INTERFACE_COUNTER_RX,
					   thread_index, *last_sw_if_index,
					   *n_packets, *n_bytes);
	}
      *last_sw_if_index = sw_if_index;
      *n_packets = 1;
      *n_bytes = length;
    }
}

/**
 * @brief LISP-GPE decap dispatcher.
 * @node lisp_gpe_input_inline
 *
 * LISP-GPE decap dispatcher.
 *
 * Decaps IP-UDP-LISP-GPE header and based on the next protocol and in the
 * GPE header and the vni decides the next node to forward the packet to.
 *
 * @param[in]   vm      vlib_main_t corresponding to current thread.
 * @param[in]   node    vlib_node_runtime_t data for this node.
 * @param[in]   frame   vlib_frame_t whose contents should be dispatched.
 *
 * @return number of vectors in frame.
 */
static uword
lisp_gpe_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,<