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 u8 *tag = format (0, "%s", 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, tag);
68 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
70 rmp->index = ntohl(index);
76 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
78 map_main_t *mm = &map_main;
79 vl_api_map_del_domain_reply_t *rmp;
82 rv = map_delete_domain (ntohl (mp->index));
84 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
88 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
90 map_main_t *mm = &map_main;
91 vl_api_map_del_domain_reply_t *rmp;
95 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
96 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
98 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
102 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
104 vl_api_map_domain_details_t *rmp;
105 map_main_t *mm = &map_main;
107 map_domain_extra_t *de;
108 vl_api_registration_t *reg;
109 u32 map_domain_index;
111 if (pool_elts (mm->domains) == 0)
114 reg = vl_api_client_index_to_registration (mp->client_index);
119 pool_foreach(d, mm->domains,
121 map_domain_index = d - mm->domains;
122 de = vec_elt_at_index(mm->domain_extras, map_domain_index);
124 /* Make sure every field is initiated (or don't skip the clib_memset()) */
125 rmp = vl_msg_api_alloc (sizeof (*rmp) + vec_len(de->tag));
127 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
128 rmp->context = mp->context;
129 rmp->domain_index = htonl(map_domain_index);
130 clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address));
131 clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address));
132 clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address));
133 rmp->ip6_prefix.len = d->ip6_prefix_len;
134 rmp->ip4_prefix.len = d->ip4_prefix_len;
135 rmp->ip6_src.len = d->ip6_src_len;
136 rmp->ea_bits_len = d->ea_bits_len;
137 rmp->psid_offset = d->psid_offset;
138 rmp->psid_length = d->psid_length;
139 rmp->flags = d->flags;
140 rmp->mtu = htons(d->mtu);
142 strncpy ((char *) rmp->tag, (char *) de->tag, ARRAY_LEN(rmp->tag)-1);
144 vl_api_send_msg (reg, (u8 *) rmp);
150 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
152 vl_api_registration_t *reg;
155 vl_api_map_rule_details_t *rmp;
156 map_main_t *mm = &map_main;
157 u32 domain_index = ntohl (mp->domain_index);
160 if (pool_elts (mm->domains) == 0)
163 d = pool_elt_at_index (mm->domains, domain_index);
169 reg = vl_api_client_index_to_registration (mp->client_index);
173 for (i = 0; i < (0x1 << d->psid_length); i++)
176 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
180 rmp = vl_msg_api_alloc (sizeof (*rmp));
181 clib_memset (rmp, 0, sizeof (*rmp));
182 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
183 rmp->psid = htons (i);
184 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
185 rmp->context = mp->context;
186 vl_api_send_msg (reg, (u8 *) rmp);
191 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
193 vl_api_map_summary_stats_reply_t *rmp;
194 vlib_combined_counter_main_t *cm;
197 u64 total_pkts[VLIB_N_RX_TX];
198 u64 total_bytes[VLIB_N_RX_TX];
199 map_main_t *mm = &map_main;
200 vl_api_registration_t *reg;
202 reg = vl_api_client_index_to_registration (mp->client_index);
206 rmp = vl_msg_api_alloc (sizeof (*rmp));
207 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
208 rmp->context = mp->context;
211 if (pool_elts (mm->domains) == 0)
217 clib_memset (total_pkts, 0, sizeof (total_pkts));
218 clib_memset (total_bytes, 0, sizeof (total_bytes));
220 map_domain_counter_lock (mm);
221 vec_foreach (cm, mm->domain_counters)
223 which = cm - mm->domain_counters;
225 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
227 vlib_get_combined_counter (cm, i, &v);
228 total_pkts[which] += v.packets;
229 total_bytes[which] += v.bytes;
233 map_domain_counter_unlock (mm);
235 /* Note: in network byte order! */
236 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
237 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
238 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
239 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
240 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
241 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
242 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
243 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
244 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
245 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
246 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
247 clib_host_to_net_u64 (map_error_counter_get
248 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
249 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
250 clib_host_to_net_u64 (map_error_counter_get
251 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
254 vl_api_send_msg (reg, (u8 *) rmp);
259 map_param_set_fragmentation (bool inner, bool ignore_df)
261 map_main_t *mm = &map_main;
263 mm->frag_inner = ! !inner;
264 mm->frag_ignore_df = ! !ignore_df;
270 vl_api_map_param_set_fragmentation_t_handler
271 (vl_api_map_param_set_fragmentation_t * mp)
273 map_main_t *mm = &map_main;
274 vl_api_map_param_set_fragmentation_reply_t *rmp;
277 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
279 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
284 map_param_set_icmp (ip4_address_t * icmp_src_address)
286 map_main_t *mm = &map_main;
288 if (icmp_src_address == 0)
291 mm->icmp4_src_address = *icmp_src_address;
298 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
300 map_main_t *mm = &map_main;
301 vl_api_map_param_set_icmp_reply_t *rmp;
304 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
306 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
311 map_param_set_icmp6 (u8 enable_unreachable)
313 map_main_t *mm = &map_main;
315 mm->icmp6_enabled = ! !enable_unreachable;
321 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
323 map_main_t *mm = &map_main;
324 vl_api_map_param_set_icmp6_reply_t *rmp;
327 rv = map_param_set_icmp6 (mp->enable_unreachable);
329 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
334 vl_api_map_param_add_del_pre_resolve_t_handler
335 (vl_api_map_param_add_del_pre_resolve_t * mp)
337 map_main_t *mm = &map_main;
338 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
341 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
342 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
344 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
349 map_param_set_reassembly (bool is_ipv6,
353 f64 ht_ratio, u32 * reass, u32 * packets)
355 u32 ps_reass = 0, ps_packets = 0;
356 u32 ht_reass = 0, ht_packets = 0;
360 if (pool_size != (u16) ~ 0)
362 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
363 return MAP_ERR_BAD_POOL_SIZE;
364 if (map_ip6_reass_conf_pool_size
365 (pool_size, &ps_reass, &ps_packets))
366 return MAP_ERR_BAD_POOL_SIZE;
369 if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
371 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
372 return MAP_ERR_BAD_HT_RATIO;
373 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
374 return MAP_ERR_BAD_HT_RATIO;
377 if (lifetime_ms != (u16) ~ 0)
379 if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
380 return MAP_ERR_BAD_LIFETIME;
381 if (map_ip6_reass_conf_lifetime (lifetime_ms))
382 return MAP_ERR_BAD_LIFETIME;
387 if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
388 return MAP_ERR_BAD_BUFFERS;
389 if (map_ip6_reass_conf_buffers (buffers))
390 return MAP_ERR_BAD_BUFFERS;
393 if (map_main.ip6_reass_conf_buffers >
394 map_main.ip6_reass_conf_pool_size *
395 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
397 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
402 if (pool_size != (u16) ~ 0)
404 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
405 return MAP_ERR_BAD_POOL_SIZE;
406 if (map_ip4_reass_conf_pool_size
407 (pool_size, &ps_reass, &ps_packets))
408 return MAP_ERR_BAD_POOL_SIZE;
411 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
413 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
414 return MAP_ERR_BAD_HT_RATIO;
415 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
416 return MAP_ERR_BAD_HT_RATIO;
419 if (lifetime_ms != (u16) ~ 0)
421 if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
422 return MAP_ERR_BAD_LIFETIME;
423 if (map_ip4_reass_conf_lifetime (lifetime_ms))
424 return MAP_ERR_BAD_LIFETIME;
429 if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
430 return MAP_ERR_BAD_BUFFERS;
431 if (map_ip4_reass_conf_buffers (buffers))
432 return MAP_ERR_BAD_BUFFERS;
435 if (map_main.ip4_reass_conf_buffers >
436 map_main.ip4_reass_conf_pool_size *
437 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
439 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
444 *reass = ps_reass + ht_reass;
447 *packets = ps_packets + ht_packets;
454 vl_api_map_param_set_reassembly_t_handler
455 (vl_api_map_param_set_reassembly_t * mp)
457 map_main_t *mm = &map_main;
458 vl_api_map_param_set_reassembly_reply_t *rmp;
459 u32 reass = 0, packets = 0;
463 ht_ratio = (f64) clib_net_to_host_f64 (mp->ht_ratio);
465 ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
467 rv = map_param_set_reassembly (mp->is_ip6,
468 clib_net_to_host_u16 (mp->lifetime_ms),
469 clib_net_to_host_u16 (mp->pool_size),
470 clib_net_to_host_u32 (mp->buffers),
471 ht_ratio, &reass, &packets);
474 * FIXME: Should the lost reass and packet counts be returned in the API?
477 REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
482 map_param_set_security_check (bool enable, bool fragments)
484 map_main_t *mm = &map_main;
486 mm->sec_check = ! !enable;
487 mm->sec_check_frag = ! !fragments;
493 vl_api_map_param_set_security_check_t_handler
494 (vl_api_map_param_set_security_check_t * mp)
496 map_main_t *mm = &map_main;
497 vl_api_map_param_set_security_check_reply_t *rmp;
500 rv = map_param_set_security_check (mp->enable, mp->fragments);
502 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
507 map_param_set_traffic_class (bool copy, u8 tc)
509 map_main_t *mm = &map_main;
511 mm->tc_copy = ! !copy;
518 vl_api_map_param_set_traffic_class_t_handler
519 (vl_api_map_param_set_traffic_class_t * mp)
521 map_main_t *mm = &map_main;
522 vl_api_map_param_set_traffic_class_reply_t *rmp;
525 rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
527 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
532 map_param_set_tcp (u16 tcp_mss)
534 map_main_t *mm = &map_main;
536 mm->tcp_mss = tcp_mss;
543 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
545 map_main_t *mm = &map_main;
546 vl_api_map_param_set_tcp_reply_t *rmp;
549 map_param_set_tcp (ntohs (mp->tcp_mss));
550 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
555 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
557 map_main_t *mm = &map_main;
558 vl_api_map_param_get_reply_t *rmp;
559 vl_api_registration_t *reg;
561 reg = vl_api_client_index_to_registration (mp->client_index);
565 rmp = vl_msg_api_alloc (sizeof (*rmp));
566 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
567 rmp->context = mp->context;
570 rmp->frag_inner = mm->frag_inner;
571 rmp->frag_ignore_df = mm->frag_ignore_df;
573 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
574 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
576 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
579 * FIXME: How are these addresses re-extracted from the FIB?
580 * Or should a local map_main copy be kept?
582 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
583 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
585 rmp->ip4_lifetime_ms =
586 clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
587 rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
588 rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
589 rmp->ip4_ht_ratio = clib_net_to_host_f64 (mm->ip4_reass_conf_ht_ratio);
591 rmp->ip6_lifetime_ms =
592 clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
593 rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
594 rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
595 rmp->ip6_ht_ratio = clib_net_to_host_f64 (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 map_main_t *mm = &map_main;
612 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
614 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
616 is_enable = ! !is_enable;
620 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
626 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
631 if (is_translation == false)
633 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
634 is_enable ? 1 : 0, 0, 0);
635 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
636 is_enable ? 1 : 0, 0, 0);
637 mm->bm_encap_enabled_by_sw_if =
638 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
643 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
644 is_enable ? 1 : 0, 0, 0);
645 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
646 is_enable ? 1 : 0, 0, 0);
647 mm->bm_trans_enabled_by_sw_if =
648 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
657 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
659 map_main_t *mm = &map_main;
660 vl_api_map_if_enable_disable_reply_t *rmp;
663 VALIDATE_SW_IF_INDEX (mp);
666 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
669 BAD_SW_IF_INDEX_LABEL;
670 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
674 #define foreach_map_plugin_api_msg \
675 _(MAP_ADD_DOMAIN, map_add_domain) \
676 _(MAP_DEL_DOMAIN, map_del_domain) \
677 _(MAP_ADD_DEL_RULE, map_add_del_rule) \
678 _(MAP_DOMAIN_DUMP, map_domain_dump) \
679 _(MAP_RULE_DUMP, map_rule_dump) \
680 _(MAP_IF_ENABLE_DISABLE, map_if_enable_disable) \
681 _(MAP_SUMMARY_STATS, map_summary_stats) \
682 _(MAP_PARAM_SET_FRAGMENTATION, map_param_set_fragmentation) \
683 _(MAP_PARAM_SET_ICMP, map_param_set_icmp) \
684 _(MAP_PARAM_SET_ICMP6, map_param_set_icmp6) \
685 _(MAP_PARAM_ADD_DEL_PRE_RESOLVE, map_param_add_del_pre_resolve) \
686 _(MAP_PARAM_SET_REASSEMBLY, map_param_set_reassembly) \
687 _(MAP_PARAM_SET_SECURITY_CHECK, map_param_set_security_check) \
688 _(MAP_PARAM_SET_TRAFFIC_CLASS, map_param_set_traffic_class) \
689 _(MAP_PARAM_SET_TCP, map_param_set_tcp) \
690 _(MAP_PARAM_GET, map_param_get)
692 #define vl_msg_name_crc_list
693 #include <map/map_all_api_h.h>
694 #undef vl_msg_name_crc_list
697 setup_message_id_table (map_main_t * mm, api_main_t * am)
699 #define _(id,n,crc) \
700 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
701 foreach_vl_msg_name_crc_map;
705 /* Set up the API message handling tables */
707 map_plugin_api_hookup (vlib_main_t * vm)
709 map_main_t *mm = &map_main;
710 u8 *name = format (0, "map_%08x%c", api_version, 0);
712 /* Ask for a correctly-sized block of API message decode slots */
714 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
716 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
718 vl_api_##n##_t_handler, \
720 vl_api_##n##_t_endian, \
721 vl_api_##n##_t_print, \
722 sizeof(vl_api_##n##_t), 1);
723 foreach_map_plugin_api_msg;
727 * Set up the (msg_name, crc, message-id) table
729 setup_message_id_table (mm, &api_main);
736 * fd.io coding-style-patch-verification: ON
739 * eval: (c-set-style "gnu")