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);
53 REPLY_MACRO2_END(VL_API_MAP_ADD_DOMAIN_REPLY,
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 send_domain_details (u32 map_domain_index, vl_api_registration_t * rp,
90 map_main_t *mm = &map_main;
91 vl_api_map_domain_details_t *rmp;
92 map_domain_t *d = pool_elt_at_index (mm->domains, map_domain_index);
94 /* Make sure every field is initiated (or don't skip the clib_memset()) */
95 map_domain_extra_t *de =
96 vec_elt_at_index (mm->domain_extras, map_domain_index);
97 int tag_len = clib_min (ARRAY_LEN (rmp->tag), vec_len (de->tag) + 1);
99 REPLY_MACRO_DETAILS4(VL_API_MAP_DOMAIN_DETAILS, rp, context,
101 rmp->domain_index = htonl (map_domain_index);
102 clib_memcpy (&rmp->ip6_prefix.address, &d->ip6_prefix,
103 sizeof (rmp->ip6_prefix.address));
104 clib_memcpy (&rmp->ip4_prefix.address, &d->ip4_prefix,
105 sizeof (rmp->ip4_prefix.address));
106 clib_memcpy (&rmp->ip6_src.address, &d->ip6_src,
107 sizeof (rmp->ip6_src.address));
108 rmp->ip6_prefix.len = d->ip6_prefix_len;
109 rmp->ip4_prefix.len = d->ip4_prefix_len;
110 rmp->ip6_src.len = d->ip6_src_len;
111 rmp->ea_bits_len = d->ea_bits_len;
112 rmp->psid_offset = d->psid_offset;
113 rmp->psid_length = d->psid_length;
114 rmp->flags = d->flags;
115 rmp->mtu = htons (d->mtu);
116 memcpy (rmp->tag, de->tag, tag_len - 1);
117 rmp->tag[tag_len - 1] = '\0';
122 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
124 map_main_t *mm = &map_main;
126 vl_api_registration_t *reg;
128 if (pool_elts (mm->domains) == 0)
131 reg = vl_api_client_index_to_registration (mp->client_index);
135 pool_foreach_index (i, mm->domains)
137 send_domain_details(i, reg, mp->context);
142 vl_api_map_domains_get_t_handler (vl_api_map_domains_get_t * mp)
144 map_main_t *mm = &map_main;
145 vl_api_map_domains_get_reply_t *rmp;
149 REPLY_AND_DETAILS_MACRO (VL_API_MAP_DOMAINS_GET_REPLY, mm->domains,
151 send_domain_details (cursor, rp, mp->context);
156 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
158 vl_api_registration_t *reg;
161 vl_api_map_rule_details_t *rmp;
162 map_main_t *mm = &map_main;
163 u32 domain_index = ntohl (mp->domain_index);
166 if (pool_elts (mm->domains) == 0)
169 d = pool_elt_at_index (mm->domains, domain_index);
175 reg = vl_api_client_index_to_registration (mp->client_index);
179 for (i = 0; i < (0x1 << d->psid_length); i++)
182 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
186 rmp = vl_msg_api_alloc (sizeof (*rmp));
187 clib_memset (rmp, 0, sizeof (*rmp));
188 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
189 rmp->psid = htons (i);
190 clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
191 rmp->context = mp->context;
192 vl_api_send_msg (reg, (u8 *) rmp);
197 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
199 vl_api_map_summary_stats_reply_t *rmp;
200 vlib_combined_counter_main_t *cm;
203 u64 total_pkts[VLIB_N_RX_TX];
204 u64 total_bytes[VLIB_N_RX_TX];
205 map_main_t *mm = &map_main;
206 vl_api_registration_t *reg;
208 reg = vl_api_client_index_to_registration (mp->client_index);
212 rmp = vl_msg_api_alloc (sizeof (*rmp));
213 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
214 rmp->context = mp->context;
217 if (pool_elts (mm->domains) == 0)
223 clib_memset (total_pkts, 0, sizeof (total_pkts));
224 clib_memset (total_bytes, 0, sizeof (total_bytes));
226 map_domain_counter_lock (mm);
227 vec_foreach (cm, mm->domain_counters)
229 which = cm - mm->domain_counters;
231 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
233 vlib_get_combined_counter (cm, i, &v);
234 total_pkts[which] += v.packets;
235 total_bytes[which] += v.bytes;
239 map_domain_counter_unlock (mm);
241 /* Note: in network byte order! */
242 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
243 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
244 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
245 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
246 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
247 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
248 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
249 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
250 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
251 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
252 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
253 clib_host_to_net_u64 (map_error_counter_get
254 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
255 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
256 clib_host_to_net_u64 (map_error_counter_get
257 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
260 vl_api_send_msg (reg, (u8 *) rmp);
265 map_param_set_fragmentation (bool inner, bool ignore_df)
267 map_main_t *mm = &map_main;
269 mm->frag_inner = ! !inner;
270 mm->frag_ignore_df = ! !ignore_df;
276 vl_api_map_param_set_fragmentation_t_handler
277 (vl_api_map_param_set_fragmentation_t * mp)
279 map_main_t *mm = &map_main;
280 vl_api_map_param_set_fragmentation_reply_t *rmp;
283 rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
285 REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
290 map_param_set_icmp (ip4_address_t * icmp_src_address)
292 map_main_t *mm = &map_main;
294 if (icmp_src_address == 0)
297 mm->icmp4_src_address = *icmp_src_address;
304 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
306 map_main_t *mm = &map_main;
307 vl_api_map_param_set_icmp_reply_t *rmp;
310 rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
312 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
317 map_param_set_icmp6 (u8 enable_unreachable)
319 map_main_t *mm = &map_main;
321 mm->icmp6_enabled = ! !enable_unreachable;
327 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
329 map_main_t *mm = &map_main;
330 vl_api_map_param_set_icmp6_reply_t *rmp;
333 rv = map_param_set_icmp6 (mp->enable_unreachable);
335 REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
340 vl_api_map_param_add_del_pre_resolve_t_handler
341 (vl_api_map_param_add_del_pre_resolve_t * mp)
343 map_main_t *mm = &map_main;
344 vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
347 map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
348 (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
350 REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
354 map_param_set_security_check (bool enable, bool fragments)
356 map_main_t *mm = &map_main;
358 mm->sec_check = ! !enable;
359 mm->sec_check_frag = ! !fragments;
365 vl_api_map_param_set_security_check_t_handler
366 (vl_api_map_param_set_security_check_t * mp)
368 map_main_t *mm = &map_main;
369 vl_api_map_param_set_security_check_reply_t *rmp;
372 rv = map_param_set_security_check (mp->enable, mp->fragments);
374 REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
379 map_param_set_traffic_class (bool copy, u8 tc)
381 map_main_t *mm = &map_main;
383 mm->tc_copy = ! !copy;
390 vl_api_map_param_set_traffic_class_t_handler
391 (vl_api_map_param_set_traffic_class_t * mp)
393 map_main_t *mm = &map_main;
394 vl_api_map_param_set_traffic_class_reply_t *rmp;
397 rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
399 REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
404 map_param_set_tcp (u16 tcp_mss)
406 map_main_t *mm = &map_main;
408 mm->tcp_mss = tcp_mss;
415 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
417 map_main_t *mm = &map_main;
418 vl_api_map_param_set_tcp_reply_t *rmp;
421 map_param_set_tcp (ntohs (mp->tcp_mss));
422 REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
427 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
429 map_main_t *mm = &map_main;
430 vl_api_map_param_get_reply_t *rmp;
431 vl_api_registration_t *reg;
433 reg = vl_api_client_index_to_registration (mp->client_index);
437 rmp = vl_msg_api_alloc (sizeof (*rmp));
438 rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
439 rmp->context = mp->context;
442 rmp->frag_inner = mm->frag_inner;
443 rmp->frag_ignore_df = mm->frag_ignore_df;
445 clib_memcpy (&rmp->icmp_ip4_err_relay_src,
446 &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
448 rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
451 * FIXME: How are these addresses re-extracted from the FIB?
452 * Or should a local map_main copy be kept?
454 clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
455 clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
457 rmp->sec_check_enable = mm->sec_check;
458 rmp->sec_check_fragments = mm->sec_check_frag;
460 rmp->tc_copy = mm->tc_copy;
461 rmp->tc_class = mm->tc;
463 vl_api_send_msg (reg, (u8 *) rmp);
468 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
470 map_main_t *mm = &map_main;
472 if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
474 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
476 is_enable = ! !is_enable;
480 if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
486 if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
491 if (is_translation == false)
493 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
494 ip6_full_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
495 vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
496 is_enable ? 1 : 0, 0, 0);
497 vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
498 is_enable ? 1 : 0, 0, 0);
499 mm->bm_encap_enabled_by_sw_if =
500 clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
505 ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
506 ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
507 vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
508 is_enable ? 1 : 0, 0, 0);
509 vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
510 is_enable ? 1 : 0, 0, 0);
511 mm->bm_trans_enabled_by_sw_if =
512 clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
521 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
523 map_main_t *mm = &map_main;
524 vl_api_map_if_enable_disable_reply_t *rmp;
527 VALIDATE_SW_IF_INDEX (mp);
530 map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
533 BAD_SW_IF_INDEX_LABEL;
534 REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
537 /* API definitions */
538 #include <vnet/format_fns.h>
539 #include <map/map.api.c>
541 /* Set up the API message handling tables */
543 map_plugin_api_hookup (vlib_main_t * vm)
545 map_main_t *mm = &map_main;
547 mm->msg_id_base = setup_message_id_table ();
553 * fd.io coding-style-patch-verification: ON
556 * eval: (c-set-style "gnu")