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 vtag = vl_api_from_api_to_vec (&mp->tag);
59 map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
61 (ip6_address_t *) & mp->ip6_prefix.address,
63 (ip6_address_t *) & mp->ip6_src.address,
64 mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
65 mp->psid_length, &index, ntohs (mp->mtu), flags, vtag);
69 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
71 rmp->index = ntohl(index);
77 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
79 map_main_t *mm = &map_main;
80 vl_api_map_del_domain_reply_t *rmp;
83 rv = map_delete_domain (ntohl (mp->index));
85 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
89 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
91 map_main_t *mm = &map_main;
92 vl_api_map_del_domain_reply_t *rmp;
96 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
97 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
99 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
103 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
105 vl_api_map_domain_details_t *rmp;
106 map_main_t *mm = &map_main;
108 map_domain_extra_t *de;
109 vl_api_registration_t *reg;
110 u32 map_domain_index;
112 if (pool_elts (mm->domains) == 0)
115 reg = vl_api_client_index_to_registration (mp->client_index);
120 pool_foreach(d, mm->domains,
122 map_domain_index = d - mm->domains;
123 de = vec_elt_at_index(mm->domain_extras, map_domain_index);
125 /* Make sure every field is initiated (or don't skip the clib_memset()) */
126 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len(de->tag));
128 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
129 rmp->context = mp->context;
130 rmp->domain_index = htonl(map_domain_index);
131 clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address));
132 clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address));
133 clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address));
134 rmp->ip6_prefix.len = d->ip6_prefix_len;
135 rmp->ip4_prefix.len = d->ip4_prefix_len;
136 rmp->ip6_src.len = d->ip6_src_len;
137 rmp->ea_bits_len = d->ea_bits_len;
138 rmp->psid_offset = d->psid_offset;
139 rmp->psid_length = d->psid_length;
140 rmp->flags = d->flags;
141 rmp->mtu = htons(d->mtu);
143 vl_api_vec_to_api_string (de->tag, &rmp->tag );
145 vl_api_send_msg (reg, (u8 *) rmp);
151 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
153 vl_api_registration_t *reg;
156 vl_api_map_rule_details_t *rmp;
157 map_main_t *mm = &map_main;
158 u32 domain_index = ntohl (mp->domain_index);
161 if (pool_elts (mm->domains) == 0)
164 d = pool_elt_at_index (mm->domains, domain_index);
170 reg = vl_api_client_index_to_registration (mp->client_index);
174 for (i = 0; i < (0x1 << d->psid_length); i++)
177 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
181 rmp = vl_msg_api_alloc (sizeof (*rmp));
182 clib_memset (rmp, 0, sizeof (*rmp));
183 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
184 rmp->psid = htons (i);
185 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
186 rmp->context = mp->context;
187 vl_api_send_msg (reg, (u8 *) rmp);
192 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
194 vl_api_map_summary_stats_reply_t *rmp;
195 vlib_combined_counter_main_t *cm;
198 u64 total_pkts[VLIB_N_RX_TX];
199 u64 total_bytes[VLIB_N_RX_TX];
200 map_main_t *mm = &map_main;
201 vl_api_registration_t *reg;
203 reg = vl_api_client_index_to_registration (mp->client_index);
207 rmp = vl_msg_api_alloc (sizeof (*rmp));
208 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
209 rmp->context = mp->context;
212 if (pool_elts (mm->domains) == 0)
218 clib_memset (total_pkts, 0, sizeof (total_pkts));
219 clib_memset (total_bytes, 0, sizeof (total_bytes));
221 map_domain_counter_lock (mm);
222 vec_foreach (cm, mm->domain_counters)
224 which = cm - mm->domain_counters;
226 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
228 vlib_get_combined_counter (cm, i, &v);
229 total_pkts[which] += v.packets;
230 total_bytes[which] += v.bytes;
234 map_domain_counter_unlock (mm);
236 /* Note: in network byte order! */
237 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
238 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
239 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
240 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
241 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
242 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
243 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
244 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
245 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
246 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
247 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
248 clib_host_to_net_u64 (map_error_counter_get
249 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
250 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
251 clib_host_to_net_u64 (map_error_counter_get
252 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
255 vl_api_send_msg (reg, (u8 *) rmp);
260 map_param_set_fragmentation (bool inner, bool ignore_df)
262 map_main_t *mm = &map_main;
264 mm->frag_inner = ! !inner;
265 mm->frag_ignore_df = ! !ignore_df;
271 vl_api_map_param_set_fragmentation_t_handler
272 (vl_api_map_param_set_fragmentation_t * mp)
274 map_main_t *mm = &map_main;
275 vl_api_map_param_set_fragmentation_reply_t *rmp;
278 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
280 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
285 map_param_set_icmp (ip4_address_t * icmp_src_address)
287 map_main_t *mm = &map_main;
289 if (icmp_src_address == 0)
292 mm->icmp4_src_address = *icmp_src_address;
299 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
301 map_main_t *mm = &map_main;
302 vl_api_map_param_set_icmp_reply_t *rmp;
305 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
307 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
312 map_param_set_icmp6 (u8 enable_unreachable)
314 map_main_t *mm = &map_main;
316 mm->icmp6_enabled = ! !enable_unreachable;
322 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
324 map_main_t *mm = &map_main;
325 vl_api_map_param_set_icmp6_reply_t *rmp;
328 rv = map_param_set_icmp6 (mp->enable_unreachable);
330 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
335 vl_api_map_param_add_del_pre_resolve_t_handler
336 (vl_api_map_param_add_del_pre_resolve_t * mp)
338 map_main_t *mm = &map_main;
339 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
342 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
343 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
345 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
350 map_param_set_reassembly (bool is_ipv6,
354 f64 ht_ratio, u32 * reass, u32 * packets)
356 u32 ps_reass = 0, ps_packets = 0;
357 u32 ht_reass = 0, ht_packets = 0;
361 if (pool_size != (u16) ~ 0)
363 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
364 return MAP_ERR_BAD_POOL_SIZE;
365 if (map_ip6_reass_conf_pool_size
366 (pool_size, &ps_reass, &ps_packets))
367 return MAP_ERR_BAD_POOL_SIZE;
370 if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
372 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
373 return MAP_ERR_BAD_HT_RATIO;
374 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
375 return MAP_ERR_BAD_HT_RATIO;
378 if (lifetime_ms != (u16) ~ 0)
380 if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
381 return MAP_ERR_BAD_LIFETIME;
382 if (map_ip6_reass_conf_lifetime (lifetime_ms))
383 return MAP_ERR_BAD_LIFETIME;
388 if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
389 return MAP_ERR_BAD_BUFFERS;
390 if (map_ip6_reass_conf_buffers (buffers))
391 return MAP_ERR_BAD_BUFFERS;
394 if (map_main.ip6_reass_conf_buffers >
395 map_main.ip6_reass_conf_pool_size *
396 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
398 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
403 if (pool_size != (u16) ~ 0)
405 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
406 return MAP_ERR_BAD_POOL_SIZE;
407 if (map_ip4_reass_conf_pool_size
408 (pool_size, &ps_reass, &ps_packets))
409 return MAP_ERR_BAD_POOL_SIZE;
412 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
414 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
415 return MAP_ERR_BAD_HT_RATIO;
416 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
417 return MAP_ERR_BAD_HT_RATIO;
420 if (lifetime_ms != (u16) ~ 0)
422 if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
423 return MAP_ERR_BAD_LIFETIME;
424 if (map_ip4_reass_conf_lifetime (lifetime_ms))
425 return MAP_ERR_BAD_LIFETIME;
430 if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
431 return MAP_ERR_BAD_BUFFERS;
432 if (map_ip4_reass_conf_buffers (buffers))
433 return MAP_ERR_BAD_BUFFERS;
436 if (map_main.ip4_reass_conf_buffers >
437 map_main.ip4_reass_conf_pool_size *
438 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
440 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
445 *reass = ps_reass + ht_reass;
448 *packets = ps_packets + ht_packets;
455 vl_api_map_param_set_reassembly_t_handler
456 (vl_api_map_param_set_reassembly_t * mp)
458 map_main_t *mm = &map_main;
459 vl_api_map_param_set_reassembly_reply_t *rmp;
460 u32 reass = 0, packets = 0;
464 ht_ratio = (f64) clib_net_to_host_f64 (mp->ht_ratio);
466 ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
468 rv = map_param_set_reassembly (mp->is_ip6,
469 clib_net_to_host_u16 (mp->lifetime_ms),
470 clib_net_to_host_u16 (mp->pool_size),
471 clib_net_to_host_u32 (mp->buffers),
472 ht_ratio, &reass, &packets);
475 * FIXME: Should the lost reass and packet counts be returned in the API?
478 REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
483 map_param_set_security_check (bool enable, bool fragments)
485 map_main_t *mm = &map_main;
487 mm->sec_check = ! !enable;
488 mm->sec_check_frag = ! !fragments;
494 vl_api_map_param_set_security_check_t_handler
495 (vl_api_map_param_set_security_check_t * mp)
497 map_main_t *mm = &map_main;
498 vl_api_map_param_set_security_check_reply_t *rmp;
501 rv = map_param_set_security_check (mp->enable, mp->fragments);
503 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
508 map_param_set_traffic_class (bool copy, u8 tc)
510 map_main_t *mm = &map_main;
512 mm->tc_copy = ! !copy;
519 vl_api_map_param_set_traffic_class_t_handler
520 (vl_api_map_param_set_traffic_class_t * mp)
522 map_main_t *mm = &map_main;
523 vl_api_map_param_set_traffic_class_reply_t *rmp;
526 rv = map_param_set_traffic_class (mp->copy, mp->class);
528 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
533 map_param_set_tcp (u16 tcp_mss)
535 map_main_t *mm = &map_main;
537 mm->tcp_mss = tcp_mss;
544 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
546 map_main_t *mm = &map_main;
547 vl_api_map_param_set_tcp_reply_t *rmp;
550 map_param_set_tcp (ntohs (mp->tcp_mss));
551 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
556 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
558 map_main_t *mm = &map_main;
559 vl_api_map_param_get_reply_t *rmp;
560 vl_api_registration_t *reg;
562 reg = vl_api_client_index_to_registration (mp->client_index);
566 rmp = vl_msg_api_alloc (sizeof (*rmp));
567 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
568 rmp->context = mp->context;
571 rmp->frag_inner = mm->frag_inner;
572 rmp->frag_ignore_df = mm->frag_ignore_df;
574 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
575 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
577 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
580 * FIXME: How are these addresses re-extracted from the FIB?
581 * Or should a local map_main copy be kept?
583 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
584 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
586 rmp->ip4_lifetime_ms =
587 clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
588 rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
589 rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
590 rmp->ip4_ht_ratio = clib_net_to_host_f64 (mm->ip4_reass_conf_ht_ratio);
592 rmp->ip6_lifetime_ms =
593 clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
594 rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
595 rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
596 rmp->ip6_ht_ratio = clib_net_to_host_f64 (mm->ip6_reass_conf_ht_ratio);
598 rmp->sec_check_enable = mm->sec_check;
599 rmp->sec_check_fragments = mm->sec_check_frag;
601 rmp->tc_copy = mm->tc_copy;
602 rmp->tc_class = mm->tc;
604 vl_api_send_msg (reg, (u8 *) rmp);
609 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
611 map_main_t *mm = &map_main;
613 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
615 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
617 is_enable = ! !is_enable;
621 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
627 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
632 if (is_translation == false)
634 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
635 is_enable ? 1 : 0, 0, 0);
636 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
637 is_enable ? 1 : 0, 0, 0);
638 mm->bm_encap_enabled_by_sw_if =
639 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
644 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
645 is_enable ? 1 : 0, 0, 0);
646 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
647 is_enable ? 1 : 0, 0, 0);
648 mm->bm_trans_enabled_by_sw_if =
649 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
658 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
660 map_main_t *mm = &map_main;
661 vl_api_map_if_enable_disable_reply_t *rmp;
664 VALIDATE_SW_IF_INDEX (mp);
667 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
670 BAD_SW_IF_INDEX_LABEL;
671 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
675 #define foreach_map_plugin_api_msg \
676 _(MAP_ADD_DOMAIN, map_add_domain) \
677 _(MAP_DEL_DOMAIN, map_del_domain) \
678 _(MAP_ADD_DEL_RULE, map_add_del_rule) \
679 _(MAP_DOMAIN_DUMP, map_domain_dump) \
680 _(MAP_RULE_DUMP, map_rule_dump) \
681 _(MAP_IF_ENABLE_DISABLE, map_if_enable_disable) \
682 _(MAP_SUMMARY_STATS, map_summary_stats) \
683 _(MAP_PARAM_SET_FRAGMENTATION, map_param_set_fragmentation) \
684 _(MAP_PARAM_SET_ICMP, map_param_set_icmp) \
685 _(MAP_PARAM_SET_ICMP6, map_param_set_icmp6) \
686 _(MAP_PARAM_ADD_DEL_PRE_RESOLVE, map_param_add_del_pre_resolve) \
687 _(MAP_PARAM_SET_REASSEMBLY, map_param_set_reassembly) \
688 _(MAP_PARAM_SET_SECURITY_CHECK, map_param_set_security_check) \
689 _(MAP_PARAM_SET_TRAFFIC_CLASS, map_param_set_traffic_class) \
690 _(MAP_PARAM_SET_TCP, map_param_set_tcp) \
691 _(MAP_PARAM_GET, map_param_get)
693 #define vl_msg_name_crc_list
694 #include <map/map_all_api_h.h>
695 #undef vl_msg_name_crc_list
698 setup_message_id_table (map_main_t * mm, api_main_t * am)
700 #define _(id,n,crc) \
701 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
702 foreach_vl_msg_name_crc_map;
706 /* Set up the API message handling tables */
708 map_plugin_api_hookup (vlib_main_t * vm)
710 map_main_t *mm = &map_main;
711 u8 *name = format (0, "map_%08x%c", api_version, 0);
713 /* Ask for a correctly-sized block of API message decode slots */
715 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
717 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
719 vl_api_##n##_t_handler, \
721 vl_api_##n##_t_endian, \
722 vl_api_##n##_t_print, \
723 sizeof(vl_api_##n##_t), 1);
724 foreach_map_plugin_api_msg;
728 * Set up the (msg_name, crc, message-id) table
730 setup_message_id_table (mm, &api_main);
737 * fd.io coding-style-patch-verification: ON
740 * eval: (c-set-style "gnu")