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 if (mp->is_translation)
58 flags |= MAP_DOMAIN_TRANSLATION;
61 flags |= MAP_DOMAIN_RFC6052;
64 map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len,
65 (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len,
66 (ip6_address_t *) & mp->ip6_src,
67 mp->ip6_src_prefix_len, mp->ea_bits_len,
68 mp->psid_offset, mp->psid_length, &index,
69 ntohs (mp->mtu), flags);
72 REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
74 rmp->index = ntohl(index);
80 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
82 map_main_t *mm = &map_main;
83 vl_api_map_del_domain_reply_t *rmp;
86 rv = map_delete_domain (ntohl (mp->index));
88 REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
92 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
94 map_main_t *mm = &map_main;
95 vl_api_map_del_domain_reply_t *rmp;
99 map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
100 (ip6_address_t *) mp->ip6_dst, mp->is_add);
102 REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
106 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
108 vl_api_map_domain_details_t *rmp;
109 map_main_t *mm = &map_main;
111 vl_api_registration_t *reg;
113 if (pool_elts (mm->domains) == 0)
116 reg = vl_api_client_index_to_registration (mp->client_index);
121 pool_foreach(d, mm->domains,
123 /* Make sure every field is initiated (or don't skip the clib_memset()) */
124 rmp = vl_msg_api_alloc (sizeof (*rmp));
125 rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
126 rmp->context = mp->context;
127 rmp->domain_index = htonl(d - mm->domains);
128 clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix));
129 clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix));
130 clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src));
131 rmp->ip6_prefix_len = d->ip6_prefix_len;
132 rmp->ip4_prefix_len = d->ip4_prefix_len;
133 rmp->ip6_src_len = d->ip6_src_len;
134 rmp->ea_bits_len = d->ea_bits_len;
135 rmp->psid_offset = d->psid_offset;
136 rmp->psid_length = d->psid_length;
137 rmp->flags = d->flags;
138 rmp->mtu = htons(d->mtu);
139 rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION); // Redundant
141 vl_api_send_msg (reg, (u8 *) rmp);
147 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
149 vl_api_registration_t *reg;
152 vl_api_map_rule_details_t *rmp;
153 map_main_t *mm = &map_main;
154 u32 domain_index = ntohl (mp->domain_index);
157 if (pool_elts (mm->domains) == 0)
160 d = pool_elt_at_index (mm->domains, domain_index);
166 reg = vl_api_client_index_to_registration (mp->client_index);
170 for (i = 0; i < (0x1 << d->psid_length); i++)
173 if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
177 rmp = vl_msg_api_alloc (sizeof (*rmp));
178 clib_memset (rmp, 0, sizeof (*rmp));
179 rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
180 rmp->psid = htons (i);
181 clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
182 rmp->context = mp->context;
183 vl_api_send_msg (reg, (u8 *) rmp);
188 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
190 vl_api_map_summary_stats_reply_t *rmp;
191 vlib_combined_counter_main_t *cm;
194 u64 total_pkts[VLIB_N_RX_TX];
195 u64 total_bytes[VLIB_N_RX_TX];
196 map_main_t *mm = &map_main;
197 vl_api_registration_t *reg;
199 reg = vl_api_client_index_to_registration (mp->client_index);
203 rmp = vl_msg_api_alloc (sizeof (*rmp));
204 rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
205 rmp->context = mp->context;
208 if (pool_elts (mm->domains) == 0)
214 clib_memset (total_pkts, 0, sizeof (total_pkts));
215 clib_memset (total_bytes, 0, sizeof (total_bytes));
217 map_domain_counter_lock (mm);
218 vec_foreach (cm, mm->domain_counters)
220 which = cm - mm->domain_counters;
222 for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
224 vlib_get_combined_counter (cm, i, &v);
225 total_pkts[which] += v.packets;
226 total_bytes[which] += v.bytes;
230 map_domain_counter_unlock (mm);
232 /* Note: in network byte order! */
233 rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
234 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
235 rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
236 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
237 rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
238 clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
239 rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
240 clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
241 rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
242 rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
243 rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
244 clib_host_to_net_u64 (map_error_counter_get
245 (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
246 rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
247 clib_host_to_net_u64 (map_error_counter_get
248 (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
251 vl_api_send_msg (reg, (u8 *) rmp);
254 #define foreach_map_plugin_api_msg \
255 _(MAP_ADD_DOMAIN, map_add_domain) \
256 _(MAP_DEL_DOMAIN, map_del_domain) \
257 _(MAP_ADD_DEL_RULE, map_add_del_rule) \
258 _(MAP_DOMAIN_DUMP, map_domain_dump) \
259 _(MAP_RULE_DUMP, map_rule_dump) \
260 _(MAP_SUMMARY_STATS, map_summary_stats)
262 #define vl_msg_name_crc_list
263 #include <map/map_all_api_h.h>
264 #undef vl_msg_name_crc_list
267 setup_message_id_table (map_main_t * mm, api_main_t * am)
269 #define _(id,n,crc) \
270 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
271 foreach_vl_msg_name_crc_map;
275 /* Set up the API message handling tables */
277 map_plugin_api_hookup (vlib_main_t * vm)
279 map_main_t *mm = &map_main;
280 u8 *name = format (0, "map_%08x%c", api_version, 0);
282 /* Ask for a correctly-sized block of API message decode slots */
284 vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
286 vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base), \
288 vl_api_##n##_t_handler, \
290 vl_api_##n##_t_endian, \
291 vl_api_##n##_t_print, \
292 sizeof(vl_api_##n##_t), 1);
293 foreach_map_plugin_api_msg;
297 * Set up the (msg_name, crc, message-id) table
299 setup_message_id_table (mm, &api_main);
306 * fd.io coding-style-patch-verification: ON
309 * eval: (c-set-style "gnu")