map: Use vl_api_string macros.
[vpp.git] / src / plugins / map / map_api.c
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 <map/map.h>
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>
25
26 #define vl_typedefs             /* define message structures */
27 #include <map/map_all_api_h.h>
28 #undef vl_typedefs
29
30 #define vl_endianfun            /* define message structures */
31 #include <map/map_all_api_h.h>
32 #undef vl_endianfun
33
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36 #define vl_printfun
37 #include <map/map_all_api_h.h>
38 #undef vl_printfun
39
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>
43 #undef vl_api_version
44
45 #define REPLY_MSG_ID_BASE mm->msg_id_base
46 #include <vlibapi/api_helper_macros.h>
47
48 static void
49 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
50 {
51   map_main_t *mm = &map_main;
52   vl_api_map_add_domain_reply_t *rmp;
53   int rv = 0;
54   u32 index;
55   u8 flags = 0;
56
57   rv =
58     map_create_domain ((ip4_address_t *) & mp->ip4_prefix.prefix,
59                        mp->ip4_prefix.len,
60                        (ip6_address_t *) & mp->ip6_prefix.prefix,
61                        mp->ip6_prefix.len,
62                        (ip6_address_t *) & mp->ip6_src.prefix,
63                        mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
64                        mp->psid_length, &index, ntohs (mp->mtu), flags,
65                        vl_api_from_api_string_c (&mp->tag));
66
67   /* *INDENT-OFF* */
68   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
69   ({
70     rmp->index = ntohl(index);
71   }));
72   /* *INDENT-ON* */
73 }
74
75 static void
76 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
77 {
78   map_main_t *mm = &map_main;
79   vl_api_map_del_domain_reply_t *rmp;
80   int rv = 0;
81
82   rv = map_delete_domain (ntohl (mp->index));
83
84   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
85 }
86
87 static void
88 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
89 {
90   map_main_t *mm = &map_main;
91   vl_api_map_del_domain_reply_t *rmp;
92   int rv = 0;
93
94   rv =
95     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
96                       (ip6_address_t *) & mp->ip6_dst, mp->is_add);
97
98   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
99 }
100
101 static void
102 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
103 {
104   vl_api_map_domain_details_t *rmp;
105   map_main_t *mm = &map_main;
106   map_domain_t *d;
107   map_domain_extra_t *de;
108   vl_api_registration_t *reg;
109   u32 map_domain_index;
110
111   if (pool_elts (mm->domains) == 0)
112     return;
113
114   reg = vl_api_client_index_to_registration (mp->client_index);
115   if (!reg)
116     return;
117
118   /* *INDENT-OFF* */
119   pool_foreach(d, mm->domains,
120   ({
121     u32 len;
122
123     map_domain_index = d - mm->domains;
124     de = vec_elt_at_index(mm->domain_extras, map_domain_index);
125
126     len = strnlen_s(de->tag, 64);
127
128     /* Make sure every field is initiated (or don't skip the clib_memset()) */
129     rmp = vl_msg_api_alloc (sizeof (*rmp) + len);
130
131     rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
132     rmp->context = mp->context;
133     rmp->domain_index = htonl(map_domain_index);
134     clib_memcpy(&rmp->ip6_prefix.prefix, &d->ip6_prefix, sizeof(rmp->ip6_prefix.prefix));
135     clib_memcpy(&rmp->ip4_prefix.prefix, &d->ip4_prefix, sizeof(rmp->ip4_prefix.prefix));
136     clib_memcpy(&rmp->ip6_src.prefix, &d->ip6_src, sizeof(rmp->ip6_src.prefix));
137     rmp->ip6_prefix.len = d->ip6_prefix_len;
138     rmp->ip4_prefix.len = d->ip4_prefix_len;
139     rmp->ip6_src.len = d->ip6_src_len;
140     rmp->ea_bits_len = d->ea_bits_len;
141     rmp->psid_offset = d->psid_offset;
142     rmp->psid_length = d->psid_length;
143     rmp->flags = d->flags;
144     rmp->mtu = htons(d->mtu);
145
146         vl_api_to_api_string (len, de->tag, &rmp->tag );
147
148     vl_api_send_msg (reg, (u8 *) rmp);
149   }));
150   /* *INDENT-ON* */
151 }
152
153 static void
154 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
155 {
156   vl_api_registration_t *reg;
157   u16 i;
158   ip6_address_t dst;
159   vl_api_map_rule_details_t *rmp;
160   map_main_t *mm = &map_main;
161   u32 domain_index = ntohl (mp->domain_index);
162   map_domain_t *d;
163
164   if (pool_elts (mm->domains) == 0)
165     return;
166
167   d = pool_elt_at_index (mm->domains, domain_index);
168   if (!d || !d->rules)
169     {
170       return;
171     }
172
173   reg = vl_api_client_index_to_registration (mp->client_index);
174   if (!reg)
175     return;
176
177   for (i = 0; i < (0x1 << d->psid_length); i++)
178     {
179       dst = d->rules[i];
180       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
181         {
182           continue;
183         }
184       rmp = vl_msg_api_alloc (sizeof (*rmp));
185       clib_memset (rmp, 0, sizeof (*rmp));
186       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
187       rmp->psid = htons (i);
188       clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
189       rmp->context = mp->context;
190       vl_api_send_msg (reg, (u8 *) rmp);
191     }
192 }
193
194 static void
195 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
196 {
197   vl_api_map_summary_stats_reply_t *rmp;
198   vlib_combined_counter_main_t *cm;
199   vlib_counter_t v;
200   int i, which;
201   u64 total_pkts[VLIB_N_RX_TX];
202   u64 total_bytes[VLIB_N_RX_TX];
203   map_main_t *mm = &map_main;
204   vl_api_registration_t *reg;
205
206   reg = vl_api_client_index_to_registration (mp->client_index);
207   if (!reg)
208     return;
209
210   rmp = vl_msg_api_alloc (sizeof (*rmp));
211   rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
212   rmp->context = mp->context;
213   rmp->retval = 0;
214
215   if (pool_elts (mm->domains) == 0)
216     {
217       rmp->retval = -1;
218       goto out;
219     }
220
221   clib_memset (total_pkts, 0, sizeof (total_pkts));
222   clib_memset (total_bytes, 0, sizeof (total_bytes));
223
224   map_domain_counter_lock (mm);
225   vec_foreach (cm, mm->domain_counters)
226   {
227     which = cm - mm->domain_counters;
228
229     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
230       {
231         vlib_get_combined_counter (cm, i, &v);
232         total_pkts[which] += v.packets;
233         total_bytes[which] += v.bytes;
234       }
235   }
236
237   map_domain_counter_unlock (mm);
238
239   /* Note: in network byte order! */
240   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
241     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
242   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
243     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
244   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
245     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
246   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
247     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
248   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
249   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
250   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
251     clib_host_to_net_u64 (map_error_counter_get
252                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
253   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
254     clib_host_to_net_u64 (map_error_counter_get
255                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
256
257 out:
258   vl_api_send_msg (reg, (u8 *) rmp);
259 }
260
261
262 int
263 map_param_set_fragmentation (bool inner, bool ignore_df)
264 {
265   map_main_t *mm = &map_main;
266
267   mm->frag_inner = ! !inner;
268   mm->frag_ignore_df = ! !ignore_df;
269
270   return 0;
271 }
272
273 static void
274   vl_api_map_param_set_fragmentation_t_handler
275   (vl_api_map_param_set_fragmentation_t * mp)
276 {
277   map_main_t *mm = &map_main;
278   vl_api_map_param_set_fragmentation_reply_t *rmp;
279   int rv = 0;
280
281   rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
282
283   REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
284 }
285
286
287 int
288 map_param_set_icmp (ip4_address_t * icmp_src_address)
289 {
290   map_main_t *mm = &map_main;
291
292   if (icmp_src_address == 0)
293     return -1;
294
295   mm->icmp4_src_address = *icmp_src_address;
296
297   return 0;
298 }
299
300
301 static void
302 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
303 {
304   map_main_t *mm = &map_main;
305   vl_api_map_param_set_icmp_reply_t *rmp;
306   int rv;
307
308   rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
309
310   REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
311 }
312
313
314 int
315 map_param_set_icmp6 (u8 enable_unreachable)
316 {
317   map_main_t *mm = &map_main;
318
319   mm->icmp6_enabled = ! !enable_unreachable;
320
321   return 0;
322 }
323
324 static void
325 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
326 {
327   map_main_t *mm = &map_main;
328   vl_api_map_param_set_icmp6_reply_t *rmp;
329   int rv;
330
331   rv = map_param_set_icmp6 (mp->enable_unreachable);
332
333   REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
334 }
335
336
337 static void
338   vl_api_map_param_add_del_pre_resolve_t_handler
339   (vl_api_map_param_add_del_pre_resolve_t * mp)
340 {
341   map_main_t *mm = &map_main;
342   vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
343   int rv = 0;
344
345   map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
346                    (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
347
348   REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
349 }
350
351
352 int
353 map_param_set_reassembly (bool is_ipv6,
354                           u16 lifetime_ms,
355                           u16 pool_size,
356                           u32 buffers,
357                           f64 ht_ratio, u32 * reass, u32 * packets)
358 {
359   u32 ps_reass = 0, ps_packets = 0;
360   u32 ht_reass = 0, ht_packets = 0;
361
362   if (is_ipv6)
363     {
364       if (pool_size != (u16) ~ 0)
365         {
366           if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
367             return MAP_ERR_BAD_POOL_SIZE;
368           if (map_ip6_reass_conf_pool_size
369               (pool_size, &ps_reass, &ps_packets))
370             return MAP_ERR_BAD_POOL_SIZE;
371         }
372
373       if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
374         {
375           if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
376             return MAP_ERR_BAD_HT_RATIO;
377           if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
378             return MAP_ERR_BAD_HT_RATIO;
379         }
380
381       if (lifetime_ms != (u16) ~ 0)
382         {
383           if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
384             return MAP_ERR_BAD_LIFETIME;
385           if (map_ip6_reass_conf_lifetime (lifetime_ms))
386             return MAP_ERR_BAD_LIFETIME;
387         }
388
389       if (buffers != ~0)
390         {
391           if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
392             return MAP_ERR_BAD_BUFFERS;
393           if (map_ip6_reass_conf_buffers (buffers))
394             return MAP_ERR_BAD_BUFFERS;
395         }
396
397       if (map_main.ip6_reass_conf_buffers >
398           map_main.ip6_reass_conf_pool_size *
399           MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
400         {
401           return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
402         }
403     }
404   else
405     {
406       if (pool_size != (u16) ~ 0)
407         {
408           if (pool_size > MAP_IP4_REASS_CONF_POOL_SIZE_MAX)
409             return MAP_ERR_BAD_POOL_SIZE;
410           if (map_ip4_reass_conf_pool_size
411               (pool_size, &ps_reass, &ps_packets))
412             return MAP_ERR_BAD_POOL_SIZE;
413         }
414
415       if (ht_ratio != (MAP_IP4_REASS_CONF_HT_RATIO_MAX + 1))
416         {
417           if (ht_ratio > MAP_IP4_REASS_CONF_HT_RATIO_MAX)
418             return MAP_ERR_BAD_HT_RATIO;
419           if (map_ip4_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
420             return MAP_ERR_BAD_HT_RATIO;
421         }
422
423       if (lifetime_ms != (u16) ~ 0)
424         {
425           if (lifetime_ms > MAP_IP4_REASS_CONF_LIFETIME_MAX)
426             return MAP_ERR_BAD_LIFETIME;
427           if (map_ip4_reass_conf_lifetime (lifetime_ms))
428             return MAP_ERR_BAD_LIFETIME;
429         }
430
431       if (buffers != ~0)
432         {
433           if (buffers > MAP_IP4_REASS_CONF_BUFFERS_MAX)
434             return MAP_ERR_BAD_BUFFERS;
435           if (map_ip4_reass_conf_buffers (buffers))
436             return MAP_ERR_BAD_BUFFERS;
437         }
438
439       if (map_main.ip4_reass_conf_buffers >
440           map_main.ip4_reass_conf_pool_size *
441           MAP_IP4_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
442         {
443           return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
444         }
445     }
446
447   if (reass)
448     *reass = ps_reass + ht_reass;
449
450   if (packets)
451     *packets = ps_packets + ht_packets;
452
453   return 0;
454 }
455
456
457 static void
458   vl_api_map_param_set_reassembly_t_handler
459   (vl_api_map_param_set_reassembly_t * mp)
460 {
461   map_main_t *mm = &map_main;
462   vl_api_map_param_set_reassembly_reply_t *rmp;
463   u32 reass = 0, packets = 0;
464   int rv;
465   f64 ht_ratio;
466
467   ht_ratio = (f64) clib_net_to_host_u64 (mp->ht_ratio);
468   if (ht_ratio == ~0)
469     ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
470
471   rv = map_param_set_reassembly (mp->is_ip6,
472                                  clib_net_to_host_u16 (mp->lifetime_ms),
473                                  clib_net_to_host_u16 (mp->pool_size),
474                                  clib_net_to_host_u32 (mp->buffers),
475                                  ht_ratio, &reass, &packets);
476
477   /*
478    * FIXME: Should the lost reass and packet counts be returned in the API?
479    */
480
481   REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
482 }
483
484
485 int
486 map_param_set_security_check (bool enable, bool fragments)
487 {
488   map_main_t *mm = &map_main;
489
490   mm->sec_check = ! !enable;
491   mm->sec_check_frag = ! !fragments;
492
493   return 0;
494 }
495
496 static void
497   vl_api_map_param_set_security_check_t_handler
498   (vl_api_map_param_set_security_check_t * mp)
499 {
500   map_main_t *mm = &map_main;
501   vl_api_map_param_set_security_check_reply_t *rmp;
502   int rv;
503
504   rv = map_param_set_security_check (mp->enable, mp->fragments);
505
506   REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
507 }
508
509
510 int
511 map_param_set_traffic_class (bool copy, u8 tc)
512 {
513   map_main_t *mm = &map_main;
514
515   mm->tc_copy = ! !copy;
516   mm->tc = tc;
517
518   return 0;
519 }
520
521 static void
522   vl_api_map_param_set_traffic_class_t_handler
523   (vl_api_map_param_set_traffic_class_t * mp)
524 {
525   map_main_t *mm = &map_main;
526   vl_api_map_param_set_traffic_class_reply_t *rmp;
527   int rv;
528
529   rv = map_param_set_traffic_class (mp->copy, mp->class);
530
531   REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
532 }
533
534
535 int
536 map_param_set_tcp (u16 tcp_mss)
537 {
538   map_main_t *mm = &map_main;
539
540   mm->tcp_mss = tcp_mss;
541
542   return 0;
543 }
544
545
546 static void
547 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
548 {
549   map_main_t *mm = &map_main;
550   vl_api_map_param_set_tcp_reply_t *rmp;
551   int rv = 0;
552
553   map_param_set_tcp (ntohs (mp->tcp_mss));
554   REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
555 }
556
557
558 static void
559 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
560 {
561   map_main_t *mm = &map_main;
562   vl_api_map_param_get_reply_t *rmp;
563   vl_api_registration_t *reg;
564
565   reg = vl_api_client_index_to_registration (mp->client_index);
566   if (!reg)
567     return;
568
569   rmp = vl_msg_api_alloc (sizeof (*rmp));
570   rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
571   rmp->context = mp->context;
572   rmp->retval = 0;
573
574   rmp->frag_inner = mm->frag_inner;
575   rmp->frag_ignore_df = mm->frag_ignore_df;
576
577   clib_memcpy (&rmp->icmp_ip4_err_relay_src,
578                &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
579
580   rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
581
582   /*
583    * FIXME: How are these addresses re-extracted from the FIB?
584    * Or should a local map_main copy be kept?
585    */
586   clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
587   clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
588
589   rmp->ip4_lifetime_ms =
590     clib_net_to_host_u16 (mm->ip4_reass_conf_lifetime_ms);
591   rmp->ip4_pool_size = clib_net_to_host_u16 (mm->ip4_reass_conf_pool_size);
592   rmp->ip4_buffers = clib_net_to_host_u32 (mm->ip4_reass_conf_buffers);
593   rmp->ip4_ht_ratio =
594     clib_net_to_host_u64 ((u64) mm->ip4_reass_conf_ht_ratio);
595
596   rmp->ip6_lifetime_ms =
597     clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
598   rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
599   rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
600   rmp->ip6_ht_ratio =
601     clib_net_to_host_u64 ((u64) mm->ip6_reass_conf_ht_ratio);
602
603   rmp->sec_check_enable = mm->sec_check;
604   rmp->sec_check_fragments = mm->sec_check_frag;
605
606   rmp->tc_copy = mm->tc_copy;
607   rmp->tc_class = mm->tc;
608
609   vl_api_send_msg (reg, (u8 *) rmp);
610 }
611
612
613 int
614 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
615 {
616   map_main_t *mm = &map_main;
617
618   if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
619                           sw_if_index))
620     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
621
622   is_enable = ! !is_enable;
623
624   if (is_translation)
625     {
626       if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
627           == is_enable)
628         return 0;
629     }
630   else
631     {
632       if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
633           == is_enable)
634         return 0;
635     }
636
637   if (is_translation == false)
638     {
639       vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
640                                    is_enable ? 1 : 0, 0, 0);
641       vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
642                                    is_enable ? 1 : 0, 0, 0);
643       mm->bm_encap_enabled_by_sw_if =
644         clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
645                          is_enable);
646     }
647   else
648     {
649       vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
650                                    is_enable ? 1 : 0, 0, 0);
651       vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
652                                    is_enable ? 1 : 0, 0, 0);
653       mm->bm_trans_enabled_by_sw_if =
654         clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
655                          is_enable);
656     }
657
658   return 0;
659 }
660
661
662 static void
663 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
664 {
665   map_main_t *mm = &map_main;
666   vl_api_map_if_enable_disable_reply_t *rmp;
667   int rv = 0;
668
669   VALIDATE_SW_IF_INDEX (mp);
670
671   rv =
672     map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
673                            mp->is_translation);
674
675   BAD_SW_IF_INDEX_LABEL;
676   REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
677 }
678
679
680 #define foreach_map_plugin_api_msg              \
681 _(MAP_ADD_DOMAIN, map_add_domain)               \
682 _(MAP_DEL_DOMAIN, map_del_domain)               \
683 _(MAP_ADD_DEL_RULE, map_add_del_rule)           \
684 _(MAP_DOMAIN_DUMP, map_domain_dump)             \
685 _(MAP_RULE_DUMP, map_rule_dump)                 \
686 _(MAP_IF_ENABLE_DISABLE, map_if_enable_disable) \
687 _(MAP_SUMMARY_STATS, map_summary_stats)         \
688 _(MAP_PARAM_SET_FRAGMENTATION, map_param_set_fragmentation)     \
689 _(MAP_PARAM_SET_ICMP, map_param_set_icmp)       \
690 _(MAP_PARAM_SET_ICMP6, map_param_set_icmp6)     \
691 _(MAP_PARAM_ADD_DEL_PRE_RESOLVE, map_param_add_del_pre_resolve) \
692 _(MAP_PARAM_SET_REASSEMBLY, map_param_set_reassembly)           \
693 _(MAP_PARAM_SET_SECURITY_CHECK, map_param_set_security_check)   \
694 _(MAP_PARAM_SET_TRAFFIC_CLASS, map_param_set_traffic_class)     \
695 _(MAP_PARAM_SET_TCP, map_param_set_tcp) \
696 _(MAP_PARAM_GET, map_param_get)
697
698 #define vl_msg_name_crc_list
699 #include <map/map_all_api_h.h>
700 #undef vl_msg_name_crc_list
701
702 static void
703 setup_message_id_table (map_main_t * mm, api_main_t * am)
704 {
705 #define _(id,n,crc)                                                     \
706   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + mm->msg_id_base);
707   foreach_vl_msg_name_crc_map;
708 #undef _
709 }
710
711 /* Set up the API message handling tables */
712 clib_error_t *
713 map_plugin_api_hookup (vlib_main_t * vm)
714 {
715   map_main_t *mm = &map_main;
716   u8 *name = format (0, "map_%08x%c", api_version, 0);
717
718   /* Ask for a correctly-sized block of API message decode slots */
719   mm->msg_id_base =
720     vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE);
721 #define _(N,n)                                                  \
722     vl_msg_api_set_handlers((VL_API_##N + mm->msg_id_base),     \
723                            #n,                                  \
724                            vl_api_##n##_t_handler,              \
725                            vl_noop_handler,                     \
726                            vl_api_##n##_t_endian,               \
727                            vl_api_##n##_t_print,                \
728                            sizeof(vl_api_##n##_t), 1);
729   foreach_map_plugin_api_msg;
730 #undef _
731
732   /*
733    * Set up the (msg_name, crc, message-id) table
734    */
735   setup_message_id_table (mm, &api_main);
736
737   vec_free (name);
738   return 0;
739 }
740
741 /*
742  * fd.io coding-style-patch-verification: ON
743  *
744  * Local Variables:
745  * eval: (c-set-style "gnu")
746  * End:
747  */