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