Ole Troan | f0c90e2 | 2016-12-06 12:50:08 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * map_api.c - vnet map api |
| 4 | * |
| 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: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | |
| 20 | #include <vnet/vnet.h> |
| 21 | #include <vlibmemory/api.h> |
| 22 | |
| 23 | #include "map.h" |
| 24 | #include <vnet/api_errno.h> |
| 25 | #include <vnet/ip/ip.h> |
| 26 | #include <vnet/fib/fib_table.h> |
| 27 | #include <vnet/vnet_msg_enum.h> |
| 28 | |
| 29 | #define vl_typedefs /* define message structures */ |
| 30 | #include <vnet/vnet_all_api_h.h> |
| 31 | #undef vl_typedefs |
| 32 | |
| 33 | #define vl_endianfun /* define message structures */ |
| 34 | #include <vnet/vnet_all_api_h.h> |
| 35 | #undef vl_endianfun |
| 36 | |
| 37 | /* instantiate all the print functions we know about */ |
| 38 | #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__) |
| 39 | #define vl_printfun |
| 40 | #include <vnet/vnet_all_api_h.h> |
| 41 | #undef vl_printfun |
| 42 | |
| 43 | #include <vlibapi/api_helper_macros.h> |
| 44 | |
| 45 | #define foreach_vpe_api_msg \ |
| 46 | _(MAP_ADD_DOMAIN, map_add_domain) \ |
| 47 | _(MAP_DEL_DOMAIN, map_del_domain) \ |
| 48 | _(MAP_ADD_DEL_RULE, map_add_del_rule) \ |
| 49 | _(MAP_DOMAIN_DUMP, map_domain_dump) \ |
| 50 | _(MAP_RULE_DUMP, map_rule_dump) \ |
| 51 | _(MAP_SUMMARY_STATS, map_summary_stats) |
| 52 | |
| 53 | static void |
| 54 | vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp) |
| 55 | { |
| 56 | vl_api_map_add_domain_reply_t *rmp; |
| 57 | int rv = 0; |
| 58 | u32 index; |
Ole Troan | e31d956 | 2017-11-08 11:10:54 +0100 | [diff] [blame] | 59 | u8 flags = 0; |
| 60 | |
| 61 | if (mp->is_translation) |
| 62 | flags |= MAP_DOMAIN_TRANSLATION; |
| 63 | |
| 64 | if (mp->is_rfc6052) |
| 65 | flags |= MAP_DOMAIN_RFC6052; |
| 66 | |
Ole Troan | f0c90e2 | 2016-12-06 12:50:08 +0100 | [diff] [blame] | 67 | rv = |
| 68 | map_create_domain ((ip4_address_t *) & mp->ip4_prefix, mp->ip4_prefix_len, |
| 69 | (ip6_address_t *) & mp->ip6_prefix, mp->ip6_prefix_len, |
| 70 | (ip6_address_t *) & mp->ip6_src, |
| 71 | mp->ip6_src_prefix_len, mp->ea_bits_len, |
| 72 | mp->psid_offset, mp->psid_length, &index, |
| 73 | ntohs (mp->mtu), flags); |
| 74 | |
| 75 | /* *INDENT-OFF* */ |
| 76 | REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY, |
| 77 | ({ |
| 78 | rmp->index = ntohl(index); |
| 79 | })); |
| 80 | /* *INDENT-ON* */ |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp) |
| 85 | { |
| 86 | vl_api_map_del_domain_reply_t *rmp; |
| 87 | int rv = 0; |
| 88 | |
| 89 | rv = map_delete_domain (ntohl (mp->index)); |
| 90 | |
| 91 | REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY); |
| 92 | } |
| 93 | |
| 94 | static void |
| 95 | vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp) |
| 96 | { |
| 97 | vl_api_map_del_domain_reply_t *rmp; |
| 98 | int rv = 0; |
| 99 | |
| 100 | rv = |
| 101 | map_add_del_psid (ntohl (mp->index), ntohs (mp->psid), |
| 102 | (ip6_address_t *) mp->ip6_dst, mp->is_add); |
| 103 | |
| 104 | REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp) |
| 109 | { |
| 110 | vl_api_map_domain_details_t *rmp; |
| 111 | map_main_t *mm = &map_main; |
| 112 | map_domain_t *d; |
| 113 | unix_shared_memory_queue_t *q; |
| 114 | |
| 115 | if (pool_elts (mm->domains) == 0) |
| 116 | return; |
| 117 | |
| 118 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 119 | if (q == 0) |
| 120 | { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | /* *INDENT-OFF* */ |
| 125 | pool_foreach(d, mm->domains, |
| 126 | ({ |
| 127 | /* Make sure every field is initiated (or don't skip the memset()) */ |
| 128 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 129 | rmp->_vl_msg_id = ntohs(VL_API_MAP_DOMAIN_DETAILS); |
| 130 | rmp->domain_index = htonl(d - mm->domains); |
| 131 | rmp->ea_bits_len = d->ea_bits_len; |
| 132 | rmp->psid_offset = d->psid_offset; |
| 133 | rmp->psid_length = d->psid_length; |
| 134 | clib_memcpy(rmp->ip4_prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix)); |
| 135 | rmp->ip4_prefix_len = d->ip4_prefix_len; |
| 136 | clib_memcpy(rmp->ip6_prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix)); |
| 137 | rmp->ip6_prefix_len = d->ip6_prefix_len; |
| 138 | clib_memcpy(rmp->ip6_src, &d->ip6_src, sizeof(rmp->ip6_src)); |
| 139 | rmp->ip6_src_len = d->ip6_src_len; |
| 140 | rmp->mtu = htons(d->mtu); |
| 141 | rmp->is_translation = (d->flags & MAP_DOMAIN_TRANSLATION); |
| 142 | rmp->context = mp->context; |
| 143 | |
| 144 | vl_msg_api_send_shmem (q, (u8 *)&rmp); |
| 145 | })); |
| 146 | /* *INDENT-ON* */ |
| 147 | } |
| 148 | |
| 149 | static void |
| 150 | vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp) |
| 151 | { |
| 152 | unix_shared_memory_queue_t *q; |
| 153 | u16 i; |
| 154 | ip6_address_t dst; |
| 155 | vl_api_map_rule_details_t *rmp; |
| 156 | map_main_t *mm = &map_main; |
| 157 | u32 domain_index = ntohl (mp->domain_index); |
| 158 | map_domain_t *d; |
| 159 | |
| 160 | if (pool_elts (mm->domains) == 0) |
| 161 | return; |
| 162 | |
| 163 | d = pool_elt_at_index (mm->domains, domain_index); |
| 164 | if (!d || !d->rules) |
| 165 | { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | q = vl_api_client_index_to_input_queue (mp->client_index); |
| 170 | if (q == 0) |
| 171 | { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | for (i = 0; i < (0x1 << d->psid_length); i++) |
| 176 | { |
| 177 | dst = d->rules[i]; |
| 178 | if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0) |
| 179 | { |
| 180 | continue; |
| 181 | } |
| 182 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 183 | memset (rmp, 0, sizeof (*rmp)); |
| 184 | rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS); |
| 185 | rmp->psid = htons (i); |
| 186 | clib_memcpy (rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst)); |
| 187 | rmp->context = mp->context; |
| 188 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp) |
| 194 | { |
| 195 | vl_api_map_summary_stats_reply_t *rmp; |
| 196 | vlib_combined_counter_main_t *cm; |
| 197 | vlib_counter_t v; |
| 198 | int i, which; |
| 199 | u64 total_pkts[VLIB_N_RX_TX]; |
| 200 | u64 total_bytes[VLIB_N_RX_TX]; |
| 201 | map_main_t *mm = &map_main; |
| 202 | unix_shared_memory_queue_t *q = |
| 203 | vl_api_client_index_to_input_queue (mp->client_index); |
| 204 | |
| 205 | if (!q) |
| 206 | return; |
| 207 | |
| 208 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 209 | rmp->_vl_msg_id = ntohs (VL_API_MAP_SUMMARY_STATS_REPLY); |
| 210 | rmp->context = mp->context; |
| 211 | rmp->retval = 0; |
| 212 | |
Ole Troan | c08b096 | 2017-06-26 18:12:37 +0200 | [diff] [blame] | 213 | if (pool_elts (mm->domains) == 0) |
| 214 | { |
| 215 | rmp->retval = -1; |
| 216 | goto out; |
| 217 | } |
| 218 | |
Ole Troan | f0c90e2 | 2016-12-06 12:50:08 +0100 | [diff] [blame] | 219 | memset (total_pkts, 0, sizeof (total_pkts)); |
| 220 | memset (total_bytes, 0, sizeof (total_bytes)); |
| 221 | |
| 222 | map_domain_counter_lock (mm); |
| 223 | vec_foreach (cm, mm->domain_counters) |
| 224 | { |
| 225 | which = cm - mm->domain_counters; |
| 226 | |
Neale Ranns | 1bd0109 | 2017-03-15 15:41:17 -0400 | [diff] [blame] | 227 | for (i = 0; i < vlib_combined_counter_n_counters (cm); i++) |
Ole Troan | f0c90e2 | 2016-12-06 12:50:08 +0100 | [diff] [blame] | 228 | { |
| 229 | vlib_get_combined_counter (cm, i, &v); |
| 230 | total_pkts[which] += v.packets; |
| 231 | total_bytes[which] += v.bytes; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | map_domain_counter_unlock (mm); |
| 236 | |
| 237 | /* Note: in network byte order! */ |
| 238 | rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] = |
| 239 | clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]); |
| 240 | rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] = |
| 241 | clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]); |
| 242 | rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] = |
| 243 | clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]); |
| 244 | rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] = |
| 245 | clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]); |
| 246 | rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains)); |
| 247 | rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter. |
| 248 | rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] = |
| 249 | clib_host_to_net_u64 (map_error_counter_get |
| 250 | (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK)); |
| 251 | rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] = |
| 252 | clib_host_to_net_u64 (map_error_counter_get |
| 253 | (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK)); |
| 254 | |
Ole Troan | c08b096 | 2017-06-26 18:12:37 +0200 | [diff] [blame] | 255 | out: |
Ole Troan | f0c90e2 | 2016-12-06 12:50:08 +0100 | [diff] [blame] | 256 | vl_msg_api_send_shmem (q, (u8 *) & rmp); |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * vpe_api_hookup |
| 261 | * Add vpe's API message handlers to the table. |
| 262 | * vlib has alread mapped shared memory and |
| 263 | * added the client registration handlers. |
| 264 | * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process() |
| 265 | */ |
| 266 | #define vl_msg_name_crc_list |
| 267 | #include <vnet/vnet_all_api_h.h> |
| 268 | #undef vl_msg_name_crc_list |
| 269 | |
| 270 | static void |
| 271 | setup_message_id_table (api_main_t * am) |
| 272 | { |
| 273 | #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); |
| 274 | foreach_vl_msg_name_crc_map; |
| 275 | #undef _ |
| 276 | } |
| 277 | |
| 278 | static clib_error_t * |
| 279 | map_api_hookup (vlib_main_t * vm) |
| 280 | { |
| 281 | api_main_t *am = &api_main; |
| 282 | |
| 283 | #define _(N,n) \ |
| 284 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 285 | vl_api_##n##_t_handler, \ |
| 286 | vl_noop_handler, \ |
| 287 | vl_api_##n##_t_endian, \ |
| 288 | vl_api_##n##_t_print, \ |
| 289 | sizeof(vl_api_##n##_t), 1); |
| 290 | foreach_vpe_api_msg; |
| 291 | #undef _ |
| 292 | |
| 293 | /* |
| 294 | * Set up the (msg_name, crc, message-id) table |
| 295 | */ |
| 296 | setup_message_id_table (am); |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | VLIB_API_INIT_FUNCTION (map_api_hookup); |
| 302 | |
| 303 | /* |
| 304 | * fd.io coding-style-patch-verification: ON |
| 305 | * |
| 306 | * Local Variables: |
| 307 | * eval: (c-set-style "gnu") |
| 308 | * End: |
| 309 | */ |