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 *------------------------------------------------------------------
20 #include <vnet/ip/ip_types_api.h>
22 #include <map/map.api_enum.h>
23 #include <map/map.api_types.h>
24 #include <vnet/ip/ip.h>
25 #include <vnet/fib/fib_table.h>
26 #include <vlibmemory/api.h>
28 #define REPLY_MSG_ID_BASE mm->msg_id_base
29 #include <vlibapi/api_helper_macros.h>
32 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
34 map_main_t *mm = &map_main;
35 vl_api_map_add_domain_reply_t *rmp;
40 u8 *tag = format (0, "%s", mp->tag);
42 map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
44 (ip6_address_t *) & mp->ip6_prefix.address,
46 (ip6_address_t *) & mp->ip6_src.address,
47 mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
48 mp->psid_length, &index, ntohs (mp->mtu), flags, tag);
51 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
53 rmp->index = ntohl(index);
59 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
61 map_main_t *mm = &map_main;
62 vl_api_map_del_domain_reply_t *rmp;
65 rv = map_delete_domain (ntohl (mp->index));
67 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
71 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
73 map_main_t *mm = &map_main;
74 vl_api_map_del_domain_reply_t *rmp;
78 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
79 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
81 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
85 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
87 vl_api_map_domain_details_t *rmp;
88 map_main_t *mm = &map_main;
90 map_domain_extra_t *de;
91 vl_api_registration_t *reg;
94 if (pool_elts (mm->domains) == 0)
97 reg = vl_api_client_index_to_registration (mp->client_index);
102 pool_foreach(d, mm->domains,
104 map_domain_index = d - mm->domains;
105 de = vec_elt_at_index(mm->domain_extras, map_domain_index);
106 int tag_len = clib_min(ARRAY_LEN(rmp->tag), vec_len(de->tag) + 1);
108 /* Make sure every field is initiated (or don't skip the clib_memset()) */
109 rmp = vl_msg_api_alloc (sizeof (*rmp) + tag_len);
111 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
112 rmp->context = mp->context;
113 rmp->domain_index = htonl(map_domain_index);
114 clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address));
115 clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address));
116 clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address));
117 rmp->ip6_prefix.len = d->ip6_prefix_len;
118 rmp->ip4_prefix.len = d->ip4_prefix_len;
119 rmp->ip6_src.len = d->ip6_src_len;
120 rmp->ea_bits_len = d->ea_bits_len;
121 rmp->psid_offset = d->psid_offset;
122 rmp->psid_length = d->psid_length;
123 rmp->flags = d->flags;
124 rmp->mtu = htons(d->mtu);
125 memcpy(rmp->tag, de->tag, tag_len-1);
126 rmp->tag[tag_len-1] = '\0';
128 vl_api_send_msg (reg, (u8 *) rmp);
134 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
136 vl_api_registration_t *reg;
139 vl_api_map_rule_details_t *rmp;
140 map_main_t *mm = &map_main;
141 u32 domain_index = ntohl (mp->domain_index);
144 if (pool_elts (mm->domains) == 0)
147 d = pool_elt_at_index (mm->domains, domain_index);
153 reg = vl_api_client_index_to_registration (mp->client_index);
157 for (i = 0; i < (0x1 << d->psid_length); i++)
160 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
164 rmp = vl_msg_api_alloc (sizeof (*rmp));
165 clib_memset (rmp, 0, sizeof (*rmp));
166 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
167 rmp->psid = htons (i);
168 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
169 rmp->context = mp->context;
170 vl_api_send_msg (reg, (u8 *) rmp);
175 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
177 vl_api_map_summary_stats_reply_t *rmp;
178 vlib_combined_counter_main_t *cm;
181 u64 total_pkts[VLIB_N_RX_TX];
182 u64 total_bytes[VLIB_N_RX_TX];
183 map_main_t *mm = &map_main;
184 vl_api_registration_t *reg;
186 reg = vl_api_client_index_to_registration (mp->client_index);
190 rmp = vl_msg_api_alloc (sizeof (*rmp));
191 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
192 rmp->context = mp->context;
195 if (pool_elts (mm->domains) == 0)
201 clib_memset (total_pkts, 0, sizeof (total_pkts));
202 clib_memset (total_bytes, 0, sizeof (total_bytes));
204 map_domain_counter_lock (mm);
205 vec_foreach (cm, mm->domain_counters)
207 which = cm - mm->domain_counters;
209 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
211 vlib_get_combined_counter (cm, i, &v);
212 total_pkts[which] += v.packets;
213 total_bytes[which] += v.bytes;
217 map_domain_counter_unlock (mm);
219 /* Note: in network byte order! */
220 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
221 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
222 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
223 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
224 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
225 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
226 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
227 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
228 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
229 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
230 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
231 clib_host_to_net_u64 (map_error_counter_get
232 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
233 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
234 clib_host_to_net_u64 (map_error_counter_get
235 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
238 vl_api_send_msg (reg, (u8 *) rmp);
243 map_param_set_fragmentation (bool inner, bool ignore_df)
245 map_main_t *mm = &map_main;
247 mm->frag_inner = ! !inner;
248 mm->frag_ignore_df = ! !ignore_df;
254 vl_api_map_param_set_fragmentation_t_handler
255 (vl_api_map_param_set_fragmentation_t * mp)
257 map_main_t *mm = &map_main;
258 vl_api_map_param_set_fragmentation_reply_t *rmp;
261 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
263 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
268 map_param_set_icmp (ip4_address_t * icmp_src_address)
270 map_main_t *mm = &map_main;
272 if (icmp_src_address == 0)
275 mm->icmp4_src_address = *icmp_src_address;
282 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
284 map_main_t *mm = &map_main;
285 vl_api_map_param_set_icmp_reply_t *rmp;
288 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
290 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
295 map_param_set_icmp6 (u8 enable_unreachable)
297 map_main_t *mm = &map_main;
299 mm->icmp6_enabled = ! !enable_unreachable;
305 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
307 map_main_t *mm = &map_main;
308 vl_api_map_param_set_icmp6_reply_t *rmp;
311 rv = map_param_set_icmp6 (mp->enable_unreachable);
313 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
318 vl_api_map_param_add_del_pre_resolve_t_handler
319 (vl_api_map_param_add_del_pre_resolve_t * mp)
321 map_main_t *mm = &map_main;
322 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
325 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
326 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
328 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
333 map_param_set_reassembly (bool is_ipv6,
337 f64 ht_ratio, u32 * reass, u32 * packets)
339 u32 ps_reass = 0, ps_packets = 0;
340 u32 ht_reass = 0, ht_packets = 0;
344 if (pool_size != (u16) ~ 0)
346 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
347 return MAP_ERR_BAD_POOL_SIZE;
348 if (map_ip6_reass_conf_pool_size
349 (pool_size, &ps_reass, &ps_packets))
350 return MAP_ERR_BAD_POOL_SIZE;
353 if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
355 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
356 return MAP_ERR_BAD_HT_RATIO;
357 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
358 return MAP_ERR_BAD_HT_RATIO;
361 if (lifetime_ms != (u16) ~ 0)
363 if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
364 return MAP_ERR_BAD_LIFETIME;
365 if (map_ip6_reass_conf_lifetime (lifetime_ms))
366 return MAP_ERR_BAD_LIFETIME;
371 if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
372 return MAP_ERR_BAD_BUFFERS;
373 if (map_ip6_reass_conf_buffers (buffers))
374 return MAP_ERR_BAD_BUFFERS;
377 if (map_main.ip6_reass_conf_buffers >
378 map_main.ip6_reass_conf_pool_size *
379 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
381 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
386 if (pool_size != (u16) ~ 0)
388 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
389 return MAP_ERR_BAD_POOL_SIZE;
390 if (map_ip4_reass_conf_pool_size
391 (pool_size, &ps_reass, &ps_packets))
392 return MAP_ERR_BAD_POOL_SIZE;
395 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
397 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
398 return MAP_ERR_BAD_HT_RATIO;
399 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
400 return MAP_ERR_BAD_HT_RATIO;
403 if (lifetime_ms != (u16) ~ 0)
405 if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
406 return MAP_ERR_BAD_LIFETIME;
407 if (map_ip4_reass_conf_lifetime (lifetime_ms))
408 return MAP_ERR_BAD_LIFETIME;
413 if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
414 return MAP_ERR_BAD_BUFFERS;
415 if (map_ip4_reass_conf_buffers (buffers))
416 return MAP_ERR_BAD_BUFFERS;
419 if (map_main.ip4_reass_conf_buffers >
420 map_main.ip4_reass_conf_pool_size *
421 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
423 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
428 *reass = ps_reass + ht_reass;
431 *packets = ps_packets + ht_packets;
438 vl_api_map_param_set_reassembly_t_handler
439 (vl_api_map_param_set_reassembly_t * mp)
441 map_main_t *mm = &map_main;
442 vl_api_map_param_set_reassembly_reply_t *rmp;
443 u32 reass = 0, packets = 0;
447 ht_ratio = (f64) clib_net_to_host_f64 (mp->ht_ratio);
449 ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
451 rv = map_param_set_reassembly (mp->is_ip6,
452 clib_net_to_host_u16 (mp->lifetime_ms),
453 clib_net_to_host_u16 (mp->pool_size),
454 clib_net_to_host_u32 (mp->buffers),
455 ht_ratio, &reass, &packets);
458 * FIXME: Should the lost reass and packet counts be returned in the API?
461 REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
466 map_param_set_security_check (bool enable, bool fragments)
468 map_main_t *mm = &map_main;
470 mm->sec_check = ! !enable;
471 mm->sec_check_frag = ! !fragments;
477 vl_api_map_param_set_security_check_t_handler
478 (vl_api_map_param_set_security_check_t * mp)
480 map_main_t *mm = &map_main;
481 vl_api_map_param_set_security_check_reply_t *rmp;
484 rv = map_param_set_security_check (mp->enable, mp->fragments);
486 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
491 map_param_set_traffic_class (bool copy, u8 tc)
493 map_main_t *mm = &map_main;
495 mm->tc_copy = ! !copy;
502 vl_api_map_param_set_traffic_class_t_handler
503 (vl_api_map_param_set_traffic_class_t * mp)
505 map_main_t *mm = &map_main;
506 vl_api_map_param_set_traffic_class_reply_t *rmp;
509 rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
511 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
516 map_param_set_tcp (u16 tcp_mss)
518 map_main_t *mm = &map_main;
520 mm->tcp_mss = tcp_mss;
527 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
529 map_main_t *mm = &map_main;
530 vl_api_map_param_set_tcp_reply_t *rmp;
533 map_param_set_tcp (ntohs (mp->tcp_mss));
534 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
539 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
541 map_main_t *mm = &map_main;
542 vl_api_map_param_get_reply_t *rmp;
543 vl_api_registration_t *reg;
545 reg = vl_api_client_index_to_registration (mp->client_index);
549 rmp = vl_msg_api_alloc (sizeof (*rmp));
550 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
551 rmp->context = mp->context;
554 rmp->frag_inner = mm->frag_inner;
555 rmp->frag_ignore_df = mm->frag_ignore_df;
557 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
558 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
560 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
563 * FIXME: How are these addresses re-extracted from the FIB?
564 * Or should a local map_main copy be kept?
566 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
567 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
569 rmp->ip4_lifetime_ms =
570 clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
571 rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
572 rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
573 rmp->ip4_ht_ratio = clib_net_to_host_f64 (mm->ip4_reass_conf_ht_ratio);
575 rmp->ip6_lifetime_ms =
576 clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
577 rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
578 rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
579 rmp->ip6_ht_ratio = clib_net_to_host_f64 (mm->ip6_reass_conf_ht_ratio);
581 rmp->sec_check_enable = mm->sec_check;
582 rmp->sec_check_fragments = mm->sec_check_frag;
584 rmp->tc_copy = mm->tc_copy;
585 rmp->tc_class = mm->tc;
587 vl_api_send_msg (reg, (u8 *) rmp);
592 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
594 map_main_t *mm = &map_main;
596 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
598 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
600 is_enable = ! !is_enable;
604 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
610 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
615 if (is_translation == false)
617 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
618 is_enable ? 1 : 0, 0, 0);
619 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
620 is_enable ? 1 : 0, 0, 0);
621 mm->bm_encap_enabled_by_sw_if =
622 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
627 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
628 is_enable ? 1 : 0, 0, 0);
629 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
630 is_enable ? 1 : 0, 0, 0);
631 mm->bm_trans_enabled_by_sw_if =
632 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
641 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
643 map_main_t *mm = &map_main;
644 vl_api_map_if_enable_disable_reply_t *rmp;
647 VALIDATE_SW_IF_INDEX (mp);
650 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
653 BAD_SW_IF_INDEX_LABEL;
654 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
657 /* API definitions */
658 #include <vnet/format_fns.h>
659 #include <map/map.api.c>
661 /* Set up the API message handling tables */
663 map_plugin_api_hookup (vlib_main_t * vm)
665 map_main_t *mm = &map_main;
667 mm->msg_id_base = setup_message_id_table ();
672 * fd.io coding-style-patch-verification: ON
675 * eval: (c-set-style "gnu")