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/ip/reass/ip6_full_reass.h>
28 #include <vnet/fib/fib_table.h>
29 #include <vlibmemory/api.h>
31 #define REPLY_MSG_ID_BASE mm->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
35 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
37 map_main_t *mm = &map_main;
38 vl_api_map_add_domain_reply_t *rmp;
43 mp->tag[ARRAY_LEN (mp->tag) - 1] = '\0';
45 map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
47 (ip6_address_t *) & mp->ip6_prefix.address,
49 (ip6_address_t *) & mp->ip6_src.address,
50 mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
51 mp->psid_length, &index, mp->mtu, flags, mp->tag);
54 REPLY_MACRO2_END(VL_API_MAP_ADD_DOMAIN_REPLY,
63 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
65 map_main_t *mm = &map_main;
66 vl_api_map_del_domain_reply_t *rmp;
69 rv = map_delete_domain (ntohl (mp->index));
71 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
75 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
77 map_main_t *mm = &map_main;
78 vl_api_map_del_domain_reply_t *rmp;
82 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
83 (ip6_address_t *) & mp->ip6_dst, mp->is_add);
85 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
89 send_domain_details (u32 map_domain_index, vl_api_registration_t * rp,
92 map_main_t *mm = &map_main;
93 vl_api_map_domain_details_t *rmp;
94 map_domain_t *d = pool_elt_at_index (mm->domains, map_domain_index);
96 /* Make sure every field is initiated (or don't skip the clib_memset()) */
97 map_domain_extra_t *de =
98 vec_elt_at_index (mm->domain_extras, map_domain_index);
99 int tag_len = clib_min (ARRAY_LEN (rmp->tag), vec_len (de->tag) + 1);
102 REPLY_MACRO_DETAILS4(VL_API_MAP_DOMAIN_DETAILS, rp, context,
104 rmp->domain_index = htonl (map_domain_index);
105 clib_memcpy (&rmp->ip6_prefix.address, &d->ip6_prefix,
106 sizeof (rmp->ip6_prefix.address));
107 clib_memcpy (&rmp->ip4_prefix.address, &d->ip4_prefix,
108 sizeof (rmp->ip4_prefix.address));
109 clib_memcpy (&rmp->ip6_src.address, &d->ip6_src,
110 sizeof (rmp->ip6_src.address));
111 rmp->ip6_prefix.len = d->ip6_prefix_len;
112 rmp->ip4_prefix.len = d->ip4_prefix_len;
113 rmp->ip6_src.len = d->ip6_src_len;
114 rmp->ea_bits_len = d->ea_bits_len;
115 rmp->psid_offset = d->psid_offset;
116 rmp->psid_length = d->psid_length;
117 rmp->flags = d->flags;
118 rmp->mtu = htons (d->mtu);
119 memcpy (rmp->tag, de->tag, tag_len - 1);
120 rmp->tag[tag_len - 1] = '\0';
126 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
128 map_main_t *mm = &map_main;
130 vl_api_registration_t *reg;
132 if (pool_elts (mm->domains) == 0)
135 reg = vl_api_client_index_to_registration (mp->client_index);
140 pool_foreach_index(i, mm->domains,
142 send_domain_details(i, reg, mp->context);
148 vl_api_map_domains_get_t_handler (vl_api_map_domains_get_t * mp)
150 map_main_t *mm = &map_main;
151 vl_api_map_domains_get_reply_t *rmp;
155 if (pool_elts (mm->domains) == 0)
159 REPLY_AND_DETAILS_MACRO (VL_API_MAP_DOMAINS_GET_REPLY, mm->domains,
161 send_domain_details (cursor, rp, mp->context);
167 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
169 vl_api_registration_t *reg;
172 vl_api_map_rule_details_t *rmp;
173 map_main_t *mm = &map_main;
174 u32 domain_index = ntohl (mp->domain_index);
177 if (pool_elts (mm->domains) == 0)
180 d = pool_elt_at_index (mm->domains, domain_index);
186 reg = vl_api_client_index_to_registration (mp->client_index);
190 for (i = 0; i < (0x1 << d->psid_length); i++)
193 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
197 rmp = vl_msg_api_alloc (sizeof (*rmp));
198 clib_memset (rmp, 0, sizeof (*rmp));
199 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
200 rmp->psid = htons (i);
201 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
202 rmp->context = mp->context;
203 vl_api_send_msg (reg, (u8 *) rmp);
208 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
210 vl_api_map_summary_stats_reply_t *rmp;
211 vlib_combined_counter_main_t *cm;
214 u64 total_pkts[VLIB_N_RX_TX];
215 u64 total_bytes[VLIB_N_RX_TX];
216 map_main_t *mm = &map_main;
217 vl_api_registration_t *reg;
219 reg = vl_api_client_index_to_registration (mp->client_index);
223 rmp = vl_msg_api_alloc (sizeof (*rmp));
224 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
225 rmp->context = mp->context;
228 if (pool_elts (mm->domains) == 0)
234 clib_memset (total_pkts, 0, sizeof (total_pkts));
235 clib_memset (total_bytes, 0, sizeof (total_bytes));
237 map_domain_counter_lock (mm);
238 vec_foreach (cm, mm->domain_counters)
240 which = cm - mm->domain_counters;
242 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
244 vlib_get_combined_counter (cm, i, &v);
245 total_pkts[which] += v.packets;
246 total_bytes[which] += v.bytes;
250 map_domain_counter_unlock (mm);
252 /* Note: in network byte order! */
253 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
254 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
255 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
256 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
257 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
258 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
259 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
260 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
261 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
262 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
263 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
264 clib_host_to_net_u64 (map_error_counter_get
265 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
266 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
267 clib_host_to_net_u64 (map_error_counter_get
268 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
271 vl_api_send_msg (reg, (u8 *) rmp);
276 map_param_set_fragmentation (bool inner, bool ignore_df)
278 map_main_t *mm = &map_main;
280 mm->frag_inner = ! !inner;
281 mm->frag_ignore_df = ! !ignore_df;
287 vl_api_map_param_set_fragmentation_t_handler
288 (vl_api_map_param_set_fragmentation_t * mp)
290 map_main_t *mm = &map_main;
291 vl_api_map_param_set_fragmentation_reply_t *rmp;
294 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
296 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
301 map_param_set_icmp (ip4_address_t * icmp_src_address)
303 map_main_t *mm = &map_main;
305 if (icmp_src_address == 0)
308 mm->icmp4_src_address = *icmp_src_address;
315 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
317 map_main_t *mm = &map_main;
318 vl_api_map_param_set_icmp_reply_t *rmp;
321 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
323 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
328 map_param_set_icmp6 (u8 enable_unreachable)
330 map_main_t *mm = &map_main;
332 mm->icmp6_enabled = ! !enable_unreachable;
338 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
340 map_main_t *mm = &map_main;
341 vl_api_map_param_set_icmp6_reply_t *rmp;
344 rv = map_param_set_icmp6 (mp->enable_unreachable);
346 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
351 vl_api_map_param_add_del_pre_resolve_t_handler
352 (vl_api_map_param_add_del_pre_resolve_t * mp)
354 map_main_t *mm = &map_main;
355 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
358 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
359 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
361 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
365 map_param_set_security_check (bool enable, bool fragments)
367 map_main_t *mm = &map_main;
369 mm->sec_check = ! !enable;
370 mm->sec_check_frag = ! !fragments;
376 vl_api_map_param_set_security_check_t_handler
377 (vl_api_map_param_set_security_check_t * mp)
379 map_main_t *mm = &map_main;
380 vl_api_map_param_set_security_check_reply_t *rmp;
383 rv = map_param_set_security_check (mp->enable, mp->fragments);
385 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
390 map_param_set_traffic_class (bool copy, u8 tc)
392 map_main_t *mm = &map_main;
394 mm->tc_copy = ! !copy;
401 vl_api_map_param_set_traffic_class_t_handler
402 (vl_api_map_param_set_traffic_class_t * mp)
404 map_main_t *mm = &map_main;
405 vl_api_map_param_set_traffic_class_reply_t *rmp;
408 rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
410 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
415 map_param_set_tcp (u16 tcp_mss)
417 map_main_t *mm = &map_main;
419 mm->tcp_mss = tcp_mss;
426 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
428 map_main_t *mm = &map_main;
429 vl_api_map_param_set_tcp_reply_t *rmp;
432 map_param_set_tcp (ntohs (mp->tcp_mss));
433 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
438 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
440 map_main_t *mm = &map_main;
441 vl_api_map_param_get_reply_t *rmp;
442 vl_api_registration_t *reg;
444 reg = vl_api_client_index_to_registration (mp->client_index);
448 rmp = vl_msg_api_alloc (sizeof (*rmp));
449 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
450 rmp->context = mp->context;
453 rmp->frag_inner = mm->frag_inner;
454 rmp->frag_ignore_df = mm->frag_ignore_df;
456 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
457 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
459 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
462 * FIXME: How are these addresses re-extracted from the FIB?
463 * Or should a local map_main copy be kept?
465 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
466 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
468 rmp->sec_check_enable = mm->sec_check;
469 rmp->sec_check_fragments = mm->sec_check_frag;
471 rmp->tc_copy = mm->tc_copy;
472 rmp->tc_class = mm->tc;
474 vl_api_send_msg (reg, (u8 *) rmp);
479 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
481 map_main_t *mm = &map_main;
483 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
485 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
487 is_enable = ! !is_enable;
491 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
497 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
502 if (is_translation == false)
504 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
505 ip6_full_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
506 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
507 is_enable ? 1 : 0, 0, 0);
508 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
509 is_enable ? 1 : 0, 0, 0);
510 mm->bm_encap_enabled_by_sw_if =
511 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
516 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
517 ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
518 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
519 is_enable ? 1 : 0, 0, 0);
520 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
521 is_enable ? 1 : 0, 0, 0);
522 mm->bm_trans_enabled_by_sw_if =
523 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
532 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
534 map_main_t *mm = &map_main;
535 vl_api_map_if_enable_disable_reply_t *rmp;
538 VALIDATE_SW_IF_INDEX (mp);
541 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
544 BAD_SW_IF_INDEX_LABEL;
545 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
548 /* API definitions */
549 #include <vnet/format_fns.h>
550 #include <map/map.api.c>
552 /* Set up the API message handling tables */
554 map_plugin_api_hookup (vlib_main_t * vm)
556 map_main_t *mm = &map_main;
558 mm->msg_id_base = setup_message_id_table ();
560 api_main_t *am = vlibapi_get_main ();
561 am->is_autoendian[mm->msg_id_base + VL_API_MAP_ADD_DOMAIN] = 1;
566 * fd.io coding-style-patch-verification: ON
569 * eval: (c-set-style "gnu")