/* *------------------------------------------------------------------ * map_api.c - vnet map api * * 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. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #define REPLY_MSG_ID_BASE mm->msg_id_base #include static void vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp) { map_main_t *mm = &map_main; vl_api_map_add_domain_reply_t *rmp; int rv = 0; u32 index; u8 flags = 0; mp->tag[ARRAY_LEN (mp->tag) - 1] = '\0'; rv = map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address, mp->ip4_prefix.len, (ip6_address_t *) & mp->ip6_prefix.address, mp->ip6_prefix.len, (ip6_address_t *) & mp->ip6_src.address, mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset, mp->psid_length, &index, ntohs (mp->mtu), flags, mp->tag); /* *INDENT-OFF* */ REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY, ({ rmp->index = ntohl(index); })); /* *INDENT-ON* */ } static void vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp) { map_main_t *mm = &map_main; vl_api_map_del_domain_reply_t *rmp; int rv = 0; rv = map_delete_domain (ntohl (mp->index)); REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY); } static void vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp) { map_main_t *mm = &map_main; vl_api_map_del_domain_reply_t *rmp; int rv = 0; rv = map_add_del_psid (ntohl (mp->index), ntohs (mp->psid), (ip6_address_t *) & mp->ip6_dst, mp->is_add); REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY); } static void vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp) { vl_api_map_domain_details_t *rmp; map_main_t *mm = &map_main; map_domain_t *d; map_domain_extra_t *de; vl_api_registration_t *reg; u32 map_domain_index; if (pool_elts (mm->domains) == 0) return; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* *INDENT-OFF* */ pool_foreach(d, mm->domains, ({ map_domain_index = d - mm->domains; de = vec_elt_at_index(mm->domain_extras, map_domain_index); int tag_len = clib_min(ARRAY_LEN(rmp->tag), vec_len(de->tag) + 1); /* Make sure every field is initiated (or don't skip the clib_memset()) */ rmp = vl_msg_api_alloc (sizeof (*rmp) + tag_len); rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base); rmp->context = mp->context; rmp->domain_index = htonl(map_domain_index); clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address)); clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address)); clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address)); rmp->ip6_prefix.len = d->ip6_prefix_len; rmp->ip4_prefix.len = d->ip4_prefix_len; rmp->ip6_src.len = d->ip6_src_len; rmp->ea_bits_len = d->ea_bits_len; rmp->psid_offset = d->psid_offset; rmp->psid_length = d->psid_length; rmp->flags = d->flags; rmp->mtu = htons(d->mtu); memcpy(rmp->tag, de->tag, tag_len-1); rmp->tag[tag_len-1] = '\0'; vl_api_send_msg (reg, (u8 *) rmp); })); /* *INDENT-ON* */ } static void vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp) { vl_api_registration_t *reg; u16 i; ip6_address_t dst; vl_api_map_rule_details_t *rmp; map_main_t *mm = &map_main; u32 domain_index = ntohl (mp->domain_index); map_domain_t *d; if (pool_elts (mm->domains) == 0) return; d = pool_elt_at_index (mm->domains, domain_index); if (!d || !d->rules) { return; } reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; for (i = 0; i < (0x1 << d->psid_length); i++) { dst = d->rules[i]; if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) { continue; } rmp = vl_msg_api_alloc (sizeof (*rmp)); clib_memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base); rmp->psid = htons (i); clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst)); rmp->context = mp->context; vl_api_send_msg (reg, (u8 *) rmp); } } static void vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp) { vl_api_map_summary_stats_reply_t *rmp; vlib_combined_counter_main_t *cm; vlib_counter_t v; int i, which; u64 total_pkts[VLIB_N_RX_TX]; u64 total_bytes[VLIB_N_RX_TX]; map_main_t *mm = &map_main; vl_api_registration_t *reg; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; rmp = vl_msg_api_alloc (sizeof (*rmp)); rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base); rmp->context = mp->context; rmp->retval = 0; if (pool_elts (mm->domains) == 0) { rmp->retval = -1; goto out; } clib_memset (total_pkts, 0, sizeof (total_pkts)); clib_memset (total_bytes, 0, sizeof (total_bytes)); map_domain_counter_lock (mm); vec_foreach (cm, mm->domain_counters) { which = cm - mm->domain_counters; for (i = 0; i < vlib_combined_counter_n_counters (cm); i++) { vlib_get_combined_counter (cm, i, &v); total_pkts[which] += v.packets; total_bytes[which] += v.bytes; } } map_domain_counter_unlock (mm); /* Note: in network byte order! */ rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]); rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]); rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]); rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]); rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains)); rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter. rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = clib_host_to_net_u64 (map_error_counter_get (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK)); rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = clib_host_to_net_u64 (map_error_counter_get (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK)); out: vl_api_send_msg (reg, (u8 *) rmp); } int map_param_set