2 *------------------------------------------------------------------
3 * map_api.c - vnet map api
5 * Copyright (c) 2016 Cisco and/or its affiliates.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
21 #include <map/map_msg_enum.h>
22 #include <vnet/ip/ip.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vlibmemory/api.h>
26 #define vl_typedefs /* define message structures */
27 #include <map/map_all_api_h.h>
30 #define vl_endianfun /* define message structures */
31 #include <map/map_all_api_h.h>
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
37 #include <map/map_all_api_h.h>
40 /* Get the API version number */
41 #define vl_api_version(n,v) static u32 api_version=(v);
42 #include <map/map_all_api_h.h>
45 #define REPLY_MSG_ID_BASE mm->msg_id_base
46 #include <vlibapi/api_helper_macros.h>
49 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
51 map_main_t *mm = &map_main;
52 vl_api_map_add_domain_reply_t *rmp;
57 if (mp->is_translation)
58 flags |= MAP_DOMAIN_TRANSLATION;
61 flags |= MAP_DOMAIN_RFC6052;
64 map_create_domain ((ip4_address_t *) & mp->ip4_prefix.prefix,
66 (ip6_address_t *) & mp->ip6_prefix.prefix,
68 (ip6_address_t *) & mp->ip6_src.prefix,
69 mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
70 mp->psid_length, &index, ntohs (mp->mtu), flags);
73 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
75 rmp->index = ntohl(index);
81 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
83 map_main_t *mm = &map_main;
84 vl_api_map_del_domain_reply_t *rmp;
87 rv = map_delete_domain (ntohl (mp->index));
89 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
93 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
95 map_main_t *mm = &map_main;
96 vl_api_map_del_domain_reply_t *rmp;
100 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
101 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
103 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
107 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
109 vl_api_map_domain_details_t *rmp;
110 map_main_t *mm = &map_main;
112 vl_api_registration_t *reg;
114 if (pool_elts (mm->domains) == 0)
117 reg = vl_api_client_index_to_registration (mp->client_index);
122 pool_foreach(d, mm->domains,
124 /* Make sure every field is initiated (or don't skip the clib_memset()) */
125 rmp = vl_msg_api_alloc (sizeof (*rmp));
126 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
127 rmp->context = mp->context;
128 rmp->domain_index = htonl(d - mm->domains);
129 clib_memcpy(&rmp->ip6_prefix.prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix.prefix));
130 clib_memcpy(&rmp->ip4_prefix.prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix.prefix));
131 clib_memcpy(&rmp->ip6_src.prefix, &d->ip6_src, sizeof(rmp->ip6_src.prefix));
132 rmp->ip6_prefix.len = d->ip6_prefix_len;
133 rmp->ip4_prefix.len = d->ip4_prefix_len;
134 rmp->ip6_src.len = d->ip6_src_len;
135 rmp->ea_bits_len = d->ea_bits_len;
136 rmp->psid_offset = d->psid_offset;
137 rmp->psid_length = d->psid_length;
138 rmp->flags = d->flags;
139 rmp->mtu = htons(d->mtu);
140 rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION); // Redundant
142 vl_api_send_msg (reg, (u8 *) rmp);
148 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
150 vl_api_registration_t *reg;
153 vl_api_map_rule_details_t *rmp;
154 map_main_t *mm = &map_main;
155 u32 domain_index = ntohl (mp->domain_index);
158 if (pool_elts (mm->domains) == 0)
161 d = pool_elt_at_index (mm->domains, domain_index);
167 reg = vl_api_client_index_to_registration (mp->client_index);
171 for (i = 0; i < (0x1 << d->psid_length); i++)
174 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
178 rmp = vl_msg_api_alloc (sizeof (*rmp));
179 clib_memset (rmp, 0, sizeof (*rmp));
180 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
181 rmp->psid = htons (i);
182 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
183 rmp->context = mp->context;
184 vl_api_send_msg (reg, (u8 *) rmp);
189 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
191 vl_api_map_summary_stats_reply_t *rmp;
192 vlib_combined_counter_main_t *cm;
195 u64 total_pkts[VLIB_N_RX_TX];
196 u64 total_bytes[VLIB_N_RX_TX];
197 map_main_t *mm = &map_main;
198 vl_api_registration_t *reg;
200 reg = vl_api_client_index_to_registration (mp->client_index);
204 rmp = vl_msg_api_alloc (sizeof (*rmp));
205 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
206 rmp->context = mp->context;
209 if (pool_elts (mm->domains) == 0)
215 clib_memset (total_pkts, 0, sizeof (total_pkts));
216 clib_memset (total_bytes, 0, sizeof (total_bytes));
218 map_domain_counter_lock (mm);
219 vec_foreach (cm, mm->domain_counters)
221 which = cm - mm->domain_counters;
223 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
225 vlib_get_combined_counter (cm, i, &v);
226 total_pkts[which] += v.packets;
227 total_bytes[which] += v.bytes;
231 map_domain_counter_unlock (mm);
233 /* Note: in network byte order! */
234 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
235 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
236 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
237 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
238 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
239 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
240 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
241 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
242 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
243 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
244 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
245 clib_host_to_net_u64 (map_error_counter_get
246 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
247 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
248 clib_host_to_net_u64 (map_error_counter_get
249 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
252 vl_api_send_msg (reg, (u8 *) rmp);
257 map_param_set_fragmentation (bool inner, bool ignore_df)
259 map_main_t *mm = &map_main;
261 mm->frag_inner = ! !inner;
262 mm->frag_ignore_df = ! !ignore_df;
268 vl_api_map_param_set_fragmentation_t_handler
269 (vl_api_map_param_set_fragmentation_t * mp)
271 map_main_t *mm = &map_main;
272 vl_api_map_param_set_fragmentation_reply_t *rmp;
275 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
277 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
282 map_param_set_icmp (ip4_address_t * icmp_src_address)
284 map_main_t *mm = &map_main;
286 if (icmp_src_address == 0)
289 mm->icmp4_src_address = *icmp_src_address;
296 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
298 map_main_t *mm = &map_main;
299 vl_api_map_param_set_icmp_reply_t *rmp;
302 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
304 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
309 map_param_set_icmp6 (u8 enable_unreachable)
311 map_main_t *mm = &map_main;
313 mm->icmp6_enabled = ! !enable_unreachable;
319 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
321 map_main_t *mm = &map_main;
322 vl_api_map_param_set_icmp6_reply_t *rmp;
325 rv = map_param_set_icmp6 (mp->enable_unreachable);
327 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
332 vl_api_map_param_add_del_pre_resolve_t_handler
333 (vl_api_map_param_add_del_pre_resolve_t * mp)
335 map_main_t *mm = &map_main;
336 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
339 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
340 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
342 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
347 map_param_set_reassembly (bool is_ipv6,
351 f64 ht_ratio, u32 * reass, u32 * packets)
353 u32 ps_reass = 0, ps_packets = 0;
354 u32 ht_reass = 0, ht_packets = 0;
358 if (pool_size != (u16) ~ 0)
360 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
361 return MAP_ERR_BAD_POOL_SIZE;
362 if (map_ip6_reass_conf_pool_size
363 (pool_size, &ps_reass, &ps_packets))
364 return MAP_ERR_BAD_POOL_SIZE;
367 if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
369 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
370 return MAP_ERR_BAD_HT_RATIO;
371 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
372 return MAP_ERR_BAD_HT_RATIO;
375 if (lifetime_ms != (u16) ~ 0)
377 if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
378 return MAP_ERR_BAD_LIFETIME;
379 if (map_ip6_reass_conf_lifetime (lifetime_ms))
380 return MAP_ERR_BAD_LIFETIME;
385 if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
386 return MAP_ERR_BAD_BUFFERS;
387 if (map_ip6_reass_conf_buffers (buffers))
388 return MAP_ERR_BAD_BUFFERS;
391 if (map_main.ip6_reass_conf_buffers >
392 map_main.ip6_reass_conf_pool_size *
393 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
395 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
400 if (pool_size != (u16) ~ 0)
402 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
403 return MAP_ERR_BAD_POOL_SIZE;
404 if (map_ip4_reass_conf_pool_size
405 (pool_size, &ps_reass, &ps_packets))
406 return MAP_ERR_BAD_POOL_SIZE;
409 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
411 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
412 return MAP_ERR_BAD_HT_RATIO;
413 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
414 return MAP_ERR_BAD_HT_RATIO;
417 if (lifetime_ms != (u16) ~ 0)
419 if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
420 return MAP_ERR_BAD_LIFETIME;
421 if (map_ip4_reass_conf_lifetime (lifetime_ms))
422 return MAP_ERR_BAD_LIFETIME;
427 if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
428 return MAP_ERR_BAD_BUFFERS;
429 if (map_ip4_reass_conf_buffers (buffers))
430 return MAP_ERR_BAD_BUFFERS;
433 if (map_main.ip4_reass_conf_buffers >
434 map_main.ip4_reass_conf_pool_size *
435 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
437 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
442 *reass = ps_reass + ht_reass;
445 *packets = ps_packets + ht_packets;
452 vl_api_map_param_set_reassembly_t_handler
453 (vl_api_map_param_set_reassembly_t * mp)
455 map_main_t *mm = &map_main;
456 vl_api_map_param_set_reassembly_reply_t *rmp;
457 u32 reass = 0, packets = 0;
461 ht_ratio = (f64) clib_net_to_host_u64 (mp->ht_ratio);
463 ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
465 rv = map_param_set_reassembly (mp->is_ip6,
466 clib_net_to_host_u16 (mp->lifetime_ms),
467 clib_net_to_host_u16 (mp->pool_size),
468 clib_net_to_host_u32 (mp->buffers),
469 ht_ratio, &reass, &packets);
472 * FIXME: Should the lost reass and packet counts be returned in the API?
475 REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
480 map_param_set_security_check (bool enable, bool fragments)
482 map_main_t *mm = &map_main;
484 mm->sec_check = ! !enable;
485 mm->sec_check_frag = ! !fragments;
491 vl_api_map_param_set_security_check_t_handler
492 (vl_api_map_param_set_security_check_t * mp)
494 map_main_t *mm = &map_main;
495 vl_api_map_param_set_security_check_reply_t *rmp;
498 rv = map_param_set_security_check (mp->enable, mp->fragments);
500 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
505 map_param_set_traffic_class (bool copy, u8 tc)
507 map_main_t *mm = &map_main;
509 mm->tc_copy = ! !copy;
516 vl_api_map_param_set_traffic_class_t_handler
517 (vl_api_map_param_set_traffic_class_t * mp)
519 map_main_t *mm = &map_main;
520 vl_api_map_param_set_traffic_class_reply_t *rmp;
523 rv = map_param_set_traffic_class (mp->copy, mp->class);
525 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
530 map_param_set_tcp (u16 tcp_mss)
532 map_main_t *mm = &map_main;
534 mm->tcp_mss = tcp_mss;
541 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
543 map_main_t *mm = &map_main;
544 vl_api_map_param_set_tcp_reply_t *rmp;
547 map_param_set_tcp (ntohs (mp->tcp_mss));
548 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
553 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
555 map_main_t *mm = &map_main;
556 vl_api_map_param_get_reply_t *rmp;
557 vl_api_registration_t *reg;
559 reg = vl_api_client_index_to_registration (mp->client_index);
563 rmp = vl_msg_api_alloc (sizeof (*rmp));
564 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
565 rmp->context = mp->context;
568 rmp->frag_inner = mm->frag_inner;
569 rmp->frag_ignore_df = mm->frag_ignore_df;
571 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
572 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
574 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
577 * FIXME: How are these addresses re-extracted from the FIB?
578 * Or should a local map_main copy be kept?
580 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
581 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
583 rmp->ip4_lifetime_ms =
584 clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
585 rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
586 rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
588 clib_net_to_host_u64 ((u64) mm->ip4_reass_conf_ht_ratio);
590 rmp->ip6_lifetime_ms =
591 clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
592 rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
593 rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
595 clib_net_to_host_u64 ((u64) mm->ip6_reass_conf_ht_ratio);
597 rmp->sec_check_enable = mm->sec_check;
598 rmp->sec_check_fragments = mm->sec_check_frag;
600 rmp->tc_copy = mm->tc_copy;
601 rmp->tc_class = mm->tc;
603 vl_api_send_msg (reg, (u8 *) rmp);
608 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
610 if (is_translation == false)
612 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
613 is_enable ? 1 : 0, 0, 0);
614 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
615 is_enable ? 1 : 0, 0, 0);
619 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
620 is_enable ? 1 : 0, 0, 0);
621 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
622 is_enable ? 1 : 0, 0, 0);
629 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
631 map_main_t *mm = &map_main;
632 vl_api_map_if_enable_disable_reply_t *rmp;
635 VALIDATE_SW_IF_INDEX (mp);
638 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
641 BAD_SW_IF_INDEX_LABEL;
642 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
646 #define foreach_map_plugin_api_msg \
647 _(MAP_ADD_DOMAIN, map_add_domain) \
648 _(MAP_DEL_DOMAIN, map_del_domain) \
649 _(MAP_ADD_DEL_RULE, map_add_del_rule) \
650 _(MAP_DOMAIN_DUMP, map_domain_dump) \
651 _(MAP_RULE_DUMP, map_rule_dump) \
652 _(MAP_IF_ENABLE_DISABLE, map_if_enable_disable) \
653 _(MAP_SUMMARY_STATS, map_summary_stats) \
654 _(MAP_PARAM_SET_FRAGMENTATION, map_param_set_fragmentation) \
655 _(MAP_PARAM_SET_ICMP, map_param_set_icmp) \
656 _(MAP_PARAM_SET_ICMP6, map_param_set_icmp6) \
657 _(MAP_PARAM_ADD_DEL_PRE_RESOLVE, map_param_add_del_pre_resolve) \
658 _(MAP_PARAM_SET_REASSEMBLY, map_param_set_reassembly) \
659 _(MAP_PARAM_SET_SECURITY_CHECK, map_param_set_security_check) \
660 _(MAP_PARAM_SET_TRAFFIC_CLASS, map_param_set_traffic_class) \
661 _(MAP_PARAM_SET_TCP, map_param_set_tcp) \
662 _(MAP_PARAM_GET, map_param_get)
664 #define vl_msg_name_crc_list
665 #include <map/map_all_api_h.h>
666 #undef vl_msg_name_crc_list
669 setup_message_id_table (map_main_t * mm, api_main_t * am)
671 #define _(id,n,crc) \
672 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
673 foreach_vl_msg_name_crc_map;
677 /* Set up the API message handling tables */
679 map_plugin_api_hookup (vlib_main_t * vm)
681 map_main_t *mm = &map_main;
682 u8 *name = format (0, "map_%08x%c", api_version, 0);
684 /* Ask for a correctly-sized block of API message decode slots */
686 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
688 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
690 vl_api_##n##_t_handler, \
692 vl_api_##n##_t_endian, \
693 vl_api_##n##_t_print, \
694 sizeof(vl_api_##n##_t), 1);
695 foreach_map_plugin_api_msg;
699 * Set up the (msg_name, crc, message-id) table
701 setup_message_id_table (mm, &api_main);
708 * fd.io coding-style-patch-verification: ON
711 * eval: (c-set-style "gnu")