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/ip/reass/ip4_sv_reass.h>
26 #include <vnet/ip/reass/ip6_sv_reass.h>
27 #include <vnet/fib/fib_table.h>
28 #include <vlibmemory/api.h>
30 #define REPLY_MSG_ID_BASE mm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
34 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
36 map_main_t *mm = &map_main;
37 vl_api_map_add_domain_reply_t *rmp;
42 u8 *tag = format (0, "%s", mp->tag);
44 map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
46 (ip6_address_t *) & mp->ip6_prefix.address,
48 (ip6_address_t *) & mp->ip6_src.address,
49 mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
50 mp->psid_length, &index, ntohs (mp->mtu), flags, tag);
53 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
55 rmp->index = ntohl(index);
61 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
63 map_main_t *mm = &map_main;
64 vl_api_map_del_domain_reply_t *rmp;
67 rv = map_delete_domain (ntohl (mp->index));
69 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
73 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
75 map_main_t *mm = &map_main;
76 vl_api_map_del_domain_reply_t *rmp;
80 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
81 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
83 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
87 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
89 vl_api_map_domain_details_t *rmp;
90 map_main_t *mm = &map_main;
92 map_domain_extra_t *de;
93 vl_api_registration_t *reg;
96 if (pool_elts (mm->domains) == 0)
99 reg = vl_api_client_index_to_registration (mp->client_index);
104 pool_foreach(d, mm->domains,
106 map_domain_index = d - mm->domains;
107 de = vec_elt_at_index(mm->domain_extras, map_domain_index);
108 int tag_len = clib_min(ARRAY_LEN(rmp->tag), vec_len(de->tag) + 1);
110 /* Make sure every field is initiated (or don't skip the clib_memset()) */
111 rmp = vl_msg_api_alloc (sizeof (*rmp) + tag_len);
113 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
114 rmp->context = mp->context;
115 rmp->domain_index = htonl(map_domain_index);
116 clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address));
117 clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address));
118 clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address));
119 rmp->ip6_prefix.len = d->ip6_prefix_len;
120 rmp->ip4_prefix.len = d->ip4_prefix_len;
121 rmp->ip6_src.len = d->ip6_src_len;
122 rmp->ea_bits_len = d->ea_bits_len;
123 rmp->psid_offset = d->psid_offset;
124 rmp->psid_length = d->psid_length;
125 rmp->flags = d->flags;
126 rmp->mtu = htons(d->mtu);
127 memcpy(rmp->tag, de->tag, tag_len-1);
128 rmp->tag[tag_len-1] = '\0';
130 vl_api_send_msg (reg, (u8 *) rmp);
136 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
138 vl_api_registration_t *reg;
141 vl_api_map_rule_details_t *rmp;
142 map_main_t *mm = &map_main;
143 u32 domain_index = ntohl (mp->domain_index);
146 if (pool_elts (mm->domains) == 0)
149 d = pool_elt_at_index (mm->domains, domain_index);
155 reg = vl_api_client_index_to_registration (mp->client_index);
159 for (i = 0; i < (0x1 << d->psid_length); i++)
162 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
166 rmp = vl_msg_api_alloc (sizeof (*rmp));
167 clib_memset (rmp, 0, sizeof (*rmp));
168 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
169 rmp->psid = htons (i);
170 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
171 rmp->context = mp->context;
172 vl_api_send_msg (reg, (u8 *) rmp);
177 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
179 vl_api_map_summary_stats_reply_t *rmp;
180 vlib_combined_counter_main_t *cm;
183 u64 total_pkts[VLIB_N_RX_TX];
184 u64 total_bytes[VLIB_N_RX_TX];
185 map_main_t *mm = &map_main;
186 vl_api_registration_t *reg;
188 reg = vl_api_client_index_to_registration (mp->client_index);
192 rmp = vl_msg_api_alloc (sizeof (*rmp));
193 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
194 rmp->context = mp->context;
197 if (pool_elts (mm->domains) == 0)
203 clib_memset (total_pkts, 0, sizeof (total_pkts));
204 clib_memset (total_bytes, 0, sizeof (total_bytes));
206 map_domain_counter_lock (mm);
207 vec_foreach (cm, mm->domain_counters)
209 which = cm - mm->domain_counters;
211 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
213 vlib_get_combined_counter (cm, i, &v);
214 total_pkts[which] += v.packets;
215 total_bytes[which] += v.bytes;
219 map_domain_counter_unlock (mm);
221 /* Note: in network byte order! */
222 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
223 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
224 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
225 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
226 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
227 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
228 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
229 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
230 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
231 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
232 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
233 clib_host_to_net_u64 (map_error_counter_get
234 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
235 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
236 clib_host_to_net_u64 (map_error_counter_get
237 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
240 vl_api_send_msg (reg, (u8 *) rmp);
245 map_param_set_fragmentation (bool inner, bool ignore_df)
247 map_main_t *mm = &map_main;
249 mm->frag_inner = ! !inner;
250 mm->frag_ignore_df = ! !ignore_df;
256 vl_api_map_param_set_fragmentation_t_handler
257 (vl_api_map_param_set_fragmentation_t * mp)
259 map_main_t *mm = &map_main;
260 vl_api_map_param_set_fragmentation_reply_t *rmp;
263 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
265 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
270 map_param_set_icmp (ip4_address_t * icmp_src_address)
272 map_main_t *mm = &map_main;
274 if (icmp_src_address == 0)
277 mm->icmp4_src_address = *icmp_src_address;
284 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
286 map_main_t *mm = &map_main;
287 vl_api_map_param_set_icmp_reply_t *rmp;
290 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
292 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
297 map_param_set_icmp6 (u8 enable_unreachable)
299 map_main_t *mm = &map_main;
301 mm->icmp6_enabled = ! !enable_unreachable;
307 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
309 map_main_t *mm = &map_main;
310 vl_api_map_param_set_icmp6_reply_t *rmp;
313 rv = map_param_set_icmp6 (mp->enable_unreachable);
315 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
320 vl_api_map_param_add_del_pre_resolve_t_handler
321 (vl_api_map_param_add_del_pre_resolve_t * mp)
323 map_main_t *mm = &map_main;
324 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
327 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
328 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
330 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
335 map_param_set_reassembly (bool is_ipv6,
339 f64 ht_ratio, u32 * reass, u32 * packets)
341 u32 ps_reass = 0, ps_packets = 0;
342 u32 ht_reass = 0, ht_packets = 0;
346 if (pool_size != (u16) ~ 0)
348 if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
349 return MAP_ERR_BAD_POOL_SIZE;
350 if (map_ip6_reass_conf_pool_size
351 (pool_size, &ps_reass, &ps_packets))
352 return MAP_ERR_BAD_POOL_SIZE;
355 if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
357 if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
358 return MAP_ERR_BAD_HT_RATIO;
359 if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
360 return MAP_ERR_BAD_HT_RATIO;
363 if (lifetime_ms != (u16) ~ 0)
365 if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
366 return MAP_ERR_BAD_LIFETIME;
367 if (map_ip6_reass_conf_lifetime (lifetime_ms))
368 return MAP_ERR_BAD_LIFETIME;
373 if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
374 return MAP_ERR_BAD_BUFFERS;
375 if (map_ip6_reass_conf_buffers (buffers))
376 return MAP_ERR_BAD_BUFFERS;
379 if (map_main.ip6_reass_conf_buffers >
380 map_main.ip6_reass_conf_pool_size *
381 MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
383 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
388 if (pool_size != (u16) ~ 0)
390 if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
391 return MAP_ERR_BAD_POOL_SIZE;
392 if (map_ip4_reass_conf_pool_size
393 (pool_size, &ps_reass, &ps_packets))
394 return MAP_ERR_BAD_POOL_SIZE;
397 if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
399 if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
400 return MAP_ERR_BAD_HT_RATIO;
401 if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
402 return MAP_ERR_BAD_HT_RATIO;
405 if (lifetime_ms != (u16) ~ 0)
407 if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
408 return MAP_ERR_BAD_LIFETIME;
409 if (map_ip4_reass_conf_lifetime (lifetime_ms))
410 return MAP_ERR_BAD_LIFETIME;
415 if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
416 return MAP_ERR_BAD_BUFFERS;
417 if (map_ip4_reass_conf_buffers (buffers))
418 return MAP_ERR_BAD_BUFFERS;
421 if (map_main.ip4_reass_conf_buffers >
422 map_main.ip4_reass_conf_pool_size *
423 MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
425 return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
430 *reass = ps_reass + ht_reass;
433 *packets = ps_packets + ht_packets;
440 vl_api_map_param_set_reassembly_t_handler
441 (vl_api_map_param_set_reassembly_t * mp)
443 map_main_t *mm = &map_main;
444 vl_api_map_param_set_reassembly_reply_t *rmp;
445 u32 reass = 0, packets = 0;
449 ht_ratio = (f64) clib_net_to_host_f64 (mp->ht_ratio);
451 ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
453 rv = map_param_set_reassembly (mp->is_ip6,
454 clib_net_to_host_u16 (mp->lifetime_ms),
455 clib_net_to_host_u16 (mp->pool_size),
456 clib_net_to_host_u32 (mp->buffers),
457 ht_ratio, &reass, &packets);
460 * FIXME: Should the lost reass and packet counts be returned in the API?
463 REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
468 map_param_set_security_check (bool enable, bool fragments)
470 map_main_t *mm = &map_main;
472 mm->sec_check = ! !enable;
473 mm->sec_check_frag = ! !fragments;
479 vl_api_map_param_set_security_check_t_handler
480 (vl_api_map_param_set_security_check_t * mp)
482 map_main_t *mm = &map_main;
483 vl_api_map_param_set_security_check_reply_t *rmp;
486 rv = map_param_set_security_check (mp->enable, mp->fragments);
488 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
493 map_param_set_traffic_class (bool copy, u8 tc)
495 map_main_t *mm = &map_main;
497 mm->tc_copy = ! !copy;
504 vl_api_map_param_set_traffic_class_t_handler
505 (vl_api_map_param_set_traffic_class_t * mp)
507 map_main_t *mm = &map_main;
508 vl_api_map_param_set_traffic_class_reply_t *rmp;
511 rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
513 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
518 map_param_set_tcp (u16 tcp_mss)
520 map_main_t *mm = &map_main;
522 mm->tcp_mss = tcp_mss;
529 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
531 map_main_t *mm = &map_main;
532 vl_api_map_param_set_tcp_reply_t *rmp;
535 map_param_set_tcp (ntohs (mp->tcp_mss));
536 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
541 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
543 map_main_t *mm = &map_main;
544 vl_api_map_param_get_reply_t *rmp;
545 vl_api_registration_t *reg;
547 reg = vl_api_client_index_to_registration (mp->client_index);
551 rmp = vl_msg_api_alloc (sizeof (*rmp));
552 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
553 rmp->context = mp->context;
556 rmp->frag_inner = mm->frag_inner;
557 rmp->frag_ignore_df = mm->frag_ignore_df;
559 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
560 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
562 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
565 * FIXME: How are these addresses re-extracted from the FIB?
566 * Or should a local map_main copy be kept?
568 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
569 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
571 rmp->ip4_lifetime_ms =
572 clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
573 rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
574 rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
575 rmp->ip4_ht_ratio = clib_net_to_host_f64 (mm->ip4_reass_conf_ht_ratio);
577 rmp->ip6_lifetime_ms =
578 clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
579 rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
580 rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
581 rmp->ip6_ht_ratio = clib_net_to_host_f64 (mm->ip6_reass_conf_ht_ratio);
583 rmp->sec_check_enable = mm->sec_check;
584 rmp->sec_check_fragments = mm->sec_check_frag;
586 rmp->tc_copy = mm->tc_copy;
587 rmp->tc_class = mm->tc;
589 vl_api_send_msg (reg, (u8 *) rmp);
594 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
596 map_main_t *mm = &map_main;
598 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
600 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
602 is_enable = ! !is_enable;
606 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
612 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
617 if (is_translation == false)
619 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
620 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
621 is_enable ? 1 : 0, 0, 0);
622 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
623 is_enable ? 1 : 0, 0, 0);
624 mm->bm_encap_enabled_by_sw_if =
625 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
630 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
631 is_enable ? 1 : 0, 0, 0);
632 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
633 is_enable ? 1 : 0, 0, 0);
634 mm->bm_trans_enabled_by_sw_if =
635 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
644 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
646 map_main_t *mm = &map_main;
647 vl_api_map_if_enable_disable_reply_t *rmp;
650 VALIDATE_SW_IF_INDEX (mp);
653 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
656 BAD_SW_IF_INDEX_LABEL;
657 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
660 /* API definitions */
661 #include <vnet/format_fns.h>
662 #include <map/map.api.c>
664 /* Set up the API message handling tables */
666 map_plugin_api_hookup (vlib_main_t * vm)
668 map_main_t *mm = &map_main;
670 mm->msg_id_base = setup_message_id_table ();
675 * fd.io coding-style-patch-verification: ON
678 * eval: (c-set-style "gnu")