map: use SVR for MAP-T
[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 <vnet/ip/ip_types_api.h>
21 #include <map/map.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/fib/fib_table.h>
28 #include <vlibmemory/api.h>
29
30 #define REPLY_MSG_ID_BASE mm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
32
33 static void
34 vl_api_map_add_domain_t_handler (vl_api_map_add_domain_t * mp)
35 {
36   map_main_t *mm = &map_main;
37   vl_api_map_add_domain_reply_t *rmp;
38   int rv = 0;
39   u32 index;
40   u8 flags = 0;
41
42   u8 *tag = format (0, "%s", mp->tag);
43   rv =
44     map_create_domain ((ip4_address_t *) & mp->ip4_prefix.address,
45                        mp->ip4_prefix.len,
46                        (ip6_address_t *) & mp->ip6_prefix.address,
47                        mp->ip6_prefix.len,
48                        (ip6_address_t *) & mp->ip6_src.address,
49                        mp->ip6_src.len, mp->ea_bits_len, mp->psid_offset,
50                        mp->psid_length, &index, ntohs (mp->mtu), flags, tag);
51   vec_free (tag);
52   /* *INDENT-OFF* */
53   REPLY_MACRO2(VL_API_MAP_ADD_DOMAIN_REPLY,
54   ({
55     rmp->index = ntohl(index);
56   }));
57   /* *INDENT-ON* */
58 }
59
60 static void
61 vl_api_map_del_domain_t_handler (vl_api_map_del_domain_t * mp)
62 {
63   map_main_t *mm = &map_main;
64   vl_api_map_del_domain_reply_t *rmp;
65   int rv = 0;
66
67   rv = map_delete_domain (ntohl (mp->index));
68
69   REPLY_MACRO (VL_API_MAP_DEL_DOMAIN_REPLY);
70 }
71
72 static void
73 vl_api_map_add_del_rule_t_handler (vl_api_map_add_del_rule_t * mp)
74 {
75   map_main_t *mm = &map_main;
76   vl_api_map_del_domain_reply_t *rmp;
77   int rv = 0;
78
79   rv =
80     map_add_del_psid (ntohl (mp->index), ntohs (mp->psid),
81                       (ip6_address_t *) & mp->ip6_dst, mp->is_add);
82
83   REPLY_MACRO (VL_API_MAP_ADD_DEL_RULE_REPLY);
84 }
85
86 static void
87 vl_api_map_domain_dump_t_handler (vl_api_map_domain_dump_t * mp)
88 {
89   vl_api_map_domain_details_t *rmp;
90   map_main_t *mm = &map_main;
91   map_domain_t *d;
92   map_domain_extra_t *de;
93   vl_api_registration_t *reg;
94   u32 map_domain_index;
95
96   if (pool_elts (mm->domains) == 0)
97     return;
98
99   reg = vl_api_client_index_to_registration (mp->client_index);
100   if (!reg)
101     return;
102
103   /* *INDENT-OFF* */
104   pool_foreach(d, mm->domains,
105   ({
106     map_domain_index = d - mm->domains;
107     de = vec_elt_at_index(mm->domain_extras, map_domain_index);
108     int tag_len = clib_min(ARRAY_LEN(rmp->tag), vec_len(de->tag) + 1);
109
110     /* Make sure every field is initiated (or don't skip the clib_memset()) */
111     rmp = vl_msg_api_alloc (sizeof (*rmp) + tag_len);
112
113     rmp->_vl_msg_id = htons(VL_API_MAP_DOMAIN_DETAILS + mm->msg_id_base);
114     rmp->context = mp->context;
115     rmp->domain_index = htonl(map_domain_index);
116     clib_memcpy(&rmp->ip6_prefix.address, &d->ip6_prefix, sizeof(rmp->ip6_prefix.address));
117     clib_memcpy(&rmp->ip4_prefix.address, &d->ip4_prefix, sizeof(rmp->ip4_prefix.address));
118     clib_memcpy(&rmp->ip6_src.address, &d->ip6_src, sizeof(rmp->ip6_src.address));
119     rmp->ip6_prefix.len = d->ip6_prefix_len;
120     rmp->ip4_prefix.len = d->ip4_prefix_len;
121     rmp->ip6_src.len = d->ip6_src_len;
122     rmp->ea_bits_len = d->ea_bits_len;
123     rmp->psid_offset = d->psid_offset;
124     rmp->psid_length = d->psid_length;
125     rmp->flags = d->flags;
126     rmp->mtu = htons(d->mtu);
127     memcpy(rmp->tag, de->tag, tag_len-1);
128     rmp->tag[tag_len-1] = '\0';
129
130     vl_api_send_msg (reg, (u8 *) rmp);
131   }));
132   /* *INDENT-ON* */
133 }
134
135 static void
136 vl_api_map_rule_dump_t_handler (vl_api_map_rule_dump_t * mp)
137 {
138   vl_api_registration_t *reg;
139   u16 i;
140   ip6_address_t dst;
141   vl_api_map_rule_details_t *rmp;
142   map_main_t *mm = &map_main;
143   u32 domain_index = ntohl (mp->domain_index);
144   map_domain_t *d;
145
146   if (pool_elts (mm->domains) == 0)
147     return;
148
149   d = pool_elt_at_index (mm->domains, domain_index);
150   if (!d || !d->rules)
151     {
152       return;
153     }
154
155   reg = vl_api_client_index_to_registration (mp->client_index);
156   if (!reg)
157     return;
158
159   for (i = 0; i < (0x1 << d->psid_length); i++)
160     {
161       dst = d->rules[i];
162       if (dst.as_u64[0] == 0 && dst.as_u64[1] == 0)
163         {
164           continue;
165         }
166       rmp = vl_msg_api_alloc (sizeof (*rmp));
167       clib_memset (rmp, 0, sizeof (*rmp));
168       rmp->_vl_msg_id = ntohs (VL_API_MAP_RULE_DETAILS + mm->msg_id_base);
169       rmp->psid = htons (i);
170       clib_memcpy (&rmp->ip6_dst, &dst, sizeof (rmp->ip6_dst));
171       rmp->context = mp->context;
172       vl_api_send_msg (reg, (u8 *) rmp);
173     }
174 }
175
176 static void
177 vl_api_map_summary_stats_t_handler (vl_api_map_summary_stats_t * mp)
178 {
179   vl_api_map_summary_stats_reply_t *rmp;
180   vlib_combined_counter_main_t *cm;
181   vlib_counter_t v;
182   int i, which;
183   u64 total_pkts[VLIB_N_RX_TX];
184   u64 total_bytes[VLIB_N_RX_TX];
185   map_main_t *mm = &map_main;
186   vl_api_registration_t *reg;
187
188   reg = vl_api_client_index_to_registration (mp->client_index);
189   if (!reg)
190     return;
191
192   rmp = vl_msg_api_alloc (sizeof (*rmp));
193   rmp->_vl_msg_id = htons (VL_API_MAP_SUMMARY_STATS_REPLY + mm->msg_id_base);
194   rmp->context = mp->context;
195   rmp->retval = 0;
196
197   if (pool_elts (mm->domains) == 0)
198     {
199       rmp->retval = -1;
200       goto out;
201     }
202
203   clib_memset (total_pkts, 0, sizeof (total_pkts));
204   clib_memset (total_bytes, 0, sizeof (total_bytes));
205
206   map_domain_counter_lock (mm);
207   vec_foreach (cm, mm->domain_counters)
208   {
209     which = cm - mm->domain_counters;
210
211     for (i = 0; i < vlib_combined_counter_n_counters (cm); i++)
212       {
213         vlib_get_combined_counter (cm, i, &v);
214         total_pkts[which] += v.packets;
215         total_bytes[which] += v.bytes;
216       }
217   }
218
219   map_domain_counter_unlock (mm);
220
221   /* Note: in network byte order! */
222   rmp->total_pkts[MAP_DOMAIN_COUNTER_RX] =
223     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_RX]);
224   rmp->total_bytes[MAP_DOMAIN_COUNTER_RX] =
225     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_RX]);
226   rmp->total_pkts[MAP_DOMAIN_COUNTER_TX] =
227     clib_host_to_net_u64 (total_pkts[MAP_DOMAIN_COUNTER_TX]);
228   rmp->total_bytes[MAP_DOMAIN_COUNTER_TX] =
229     clib_host_to_net_u64 (total_bytes[MAP_DOMAIN_COUNTER_TX]);
230   rmp->total_bindings = clib_host_to_net_u64 (pool_elts (mm->domains));
231   rmp->total_ip4_fragments = 0; // Not yet implemented. Should be a simple counter.
232   rmp->total_security_check[MAP_DOMAIN_COUNTER_TX] =
233     clib_host_to_net_u64 (map_error_counter_get
234                           (ip4_map_node.index, MAP_ERROR_ENCAP_SEC_CHECK));
235   rmp->total_security_check[MAP_DOMAIN_COUNTER_RX] =
236     clib_host_to_net_u64 (map_error_counter_get
237                           (ip4_map_node.index, MAP_ERROR_DECAP_SEC_CHECK));
238
239 out:
240   vl_api_send_msg (reg, (u8 *) rmp);
241 }
242
243
244 int
245 map_param_set_fragmentation (bool inner, bool ignore_df)
246 {
247   map_main_t *mm = &map_main;
248
249   mm->frag_inner = ! !inner;
250   mm->frag_ignore_df = ! !ignore_df;
251
252   return 0;
253 }
254
255 static void
256   vl_api_map_param_set_fragmentation_t_handler
257   (vl_api_map_param_set_fragmentation_t * mp)
258 {
259   map_main_t *mm = &map_main;
260   vl_api_map_param_set_fragmentation_reply_t *rmp;
261   int rv = 0;
262
263   rv = map_param_set_fragmentation (mp->inner, mp->ignore_df);
264
265   REPLY_MACRO (VL_API_MAP_PARAM_SET_FRAGMENTATION_REPLY);
266 }
267
268
269 int
270 map_param_set_icmp (ip4_address_t * icmp_src_address)
271 {
272   map_main_t *mm = &map_main;
273
274   if (icmp_src_address == 0)
275     return -1;
276
277   mm->icmp4_src_address = *icmp_src_address;
278
279   return 0;
280 }
281
282
283 static void
284 vl_api_map_param_set_icmp_t_handler (vl_api_map_param_set_icmp_t * mp)
285 {
286   map_main_t *mm = &map_main;
287   vl_api_map_param_set_icmp_reply_t *rmp;
288   int rv;
289
290   rv = map_param_set_icmp ((ip4_address_t *) & mp->ip4_err_relay_src);
291
292   REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP_REPLY);
293 }
294
295
296 int
297 map_param_set_icmp6 (u8 enable_unreachable)
298 {
299   map_main_t *mm = &map_main;
300
301   mm->icmp6_enabled = ! !enable_unreachable;
302
303   return 0;
304 }
305
306 static void
307 vl_api_map_param_set_icmp6_t_handler (vl_api_map_param_set_icmp6_t * mp)
308 {
309   map_main_t *mm = &map_main;
310   vl_api_map_param_set_icmp6_reply_t *rmp;
311   int rv;
312
313   rv = map_param_set_icmp6 (mp->enable_unreachable);
314
315   REPLY_MACRO (VL_API_MAP_PARAM_SET_ICMP6_REPLY);
316 }
317
318
319 static void
320   vl_api_map_param_add_del_pre_resolve_t_handler
321   (vl_api_map_param_add_del_pre_resolve_t * mp)
322 {
323   map_main_t *mm = &map_main;
324   vl_api_map_param_add_del_pre_resolve_reply_t *rmp;
325   int rv = 0;
326
327   map_pre_resolve ((ip4_address_t *) & mp->ip4_nh_address,
328                    (ip6_address_t *) & mp->ip6_nh_address, !mp->is_add);
329
330   REPLY_MACRO (VL_API_MAP_PARAM_ADD_DEL_PRE_RESOLVE_REPLY);
331 }
332
333
334 int
335 map_param_set_reassembly (bool is_ipv6,
336                           u16 lifetime_ms,
337                           u16 pool_size,
338                           u32 buffers,
339                           f64 ht_ratio, u32 * reass, u32 * packets)
340 {
341   u32 ps_reass = 0, ps_packets = 0;
342   u32 ht_reass = 0, ht_packets = 0;
343
344   if (is_ipv6)
345     {
346       if (pool_size != (u16) ~ 0)
347         {
348           if (pool_size > MAP_IP6_REASS_CONF_POOL_SIZE_MAX)
349             return MAP_ERR_BAD_POOL_SIZE;
350           if (map_ip6_reass_conf_pool_size
351               (pool_size, &ps_reass, &ps_packets))
352             return MAP_ERR_BAD_POOL_SIZE;
353         }
354
355       if (ht_ratio != (MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1))
356         {
357           if (ht_ratio > MAP_IP6_REASS_CONF_HT_RATIO_MAX)
358             return MAP_ERR_BAD_HT_RATIO;
359           if (map_ip6_reass_conf_ht_ratio (ht_ratio, &ht_reass, &ht_packets))
360             return MAP_ERR_BAD_HT_RATIO;
361         }
362
363       if (lifetime_ms != (u16) ~ 0)
364         {
365           if (lifetime_ms > MAP_IP6_REASS_CONF_LIFETIME_MAX)
366             return MAP_ERR_BAD_LIFETIME;
367           if (map_ip6_reass_conf_lifetime (lifetime_ms))
368             return MAP_ERR_BAD_LIFETIME;
369         }
370
371       if (buffers != ~0)
372         {
373           if (buffers > MAP_IP6_REASS_CONF_BUFFERS_MAX)
374             return MAP_ERR_BAD_BUFFERS;
375           if (map_ip6_reass_conf_buffers (buffers))
376             return MAP_ERR_BAD_BUFFERS;
377         }
378
379       if (map_main.ip6_reass_conf_buffers >
380           map_main.ip6_reass_conf_pool_size *
381           MAP_IP6_REASS_MAX_FRAGMENTS_PER_REASSEMBLY)
382         {
383           return MAP_ERR_BAD_BUFFERS_TOO_LARGE;
384         }
385     }
386   else
387     {
388       return MAP_ERR_UNSUPPORTED;
389     }
390
391   if (reass)
392     *reass = ps_reass + ht_reass;
393
394   if (packets)
395     *packets = ps_packets + ht_packets;
396
397   return 0;
398 }
399
400
401 static void
402   vl_api_map_param_set_reassembly_t_handler
403   (vl_api_map_param_set_reassembly_t * mp)
404 {
405   map_main_t *mm = &map_main;
406   vl_api_map_param_set_reassembly_reply_t *rmp;
407   u32 reass = 0, packets = 0;
408   int rv;
409   f64 ht_ratio;
410
411   ht_ratio = (f64) clib_net_to_host_f64 (mp->ht_ratio);
412   if (ht_ratio == ~0)
413     ht_ratio = MAP_IP6_REASS_CONF_HT_RATIO_MAX + 1;
414
415   rv = map_param_set_reassembly (mp->is_ip6,
416                                  clib_net_to_host_u16 (mp->lifetime_ms),
417                                  clib_net_to_host_u16 (mp->pool_size),
418                                  clib_net_to_host_u32 (mp->buffers),
419                                  ht_ratio, &reass, &packets);
420
421   /*
422    * FIXME: Should the lost reass and packet counts be returned in the API?
423    */
424
425   REPLY_MACRO (VL_API_MAP_PARAM_SET_REASSEMBLY_REPLY);
426 }
427
428
429 int
430 map_param_set_security_check (bool enable, bool fragments)
431 {
432   map_main_t *mm = &map_main;
433
434   mm->sec_check = ! !enable;
435   mm->sec_check_frag = ! !fragments;
436
437   return 0;
438 }
439
440 static void
441   vl_api_map_param_set_security_check_t_handler
442   (vl_api_map_param_set_security_check_t * mp)
443 {
444   map_main_t *mm = &map_main;
445   vl_api_map_param_set_security_check_reply_t *rmp;
446   int rv;
447
448   rv = map_param_set_security_check (mp->enable, mp->fragments);
449
450   REPLY_MACRO (VL_API_MAP_PARAM_SET_SECURITY_CHECK_REPLY);
451 }
452
453
454 int
455 map_param_set_traffic_class (bool copy, u8 tc)
456 {
457   map_main_t *mm = &map_main;
458
459   mm->tc_copy = ! !copy;
460   mm->tc = tc;
461
462   return 0;
463 }
464
465 static void
466   vl_api_map_param_set_traffic_class_t_handler
467   (vl_api_map_param_set_traffic_class_t * mp)
468 {
469   map_main_t *mm = &map_main;
470   vl_api_map_param_set_traffic_class_reply_t *rmp;
471   int rv;
472
473   rv = map_param_set_traffic_class (mp->copy, mp->tc_class);
474
475   REPLY_MACRO (VL_API_MAP_PARAM_SET_TRAFFIC_CLASS_REPLY);
476 }
477
478
479 int
480 map_param_set_tcp (u16 tcp_mss)
481 {
482   map_main_t *mm = &map_main;
483
484   mm->tcp_mss = tcp_mss;
485
486   return 0;
487 }
488
489
490 static void
491 vl_api_map_param_set_tcp_t_handler (vl_api_map_param_set_tcp_t * mp)
492 {
493   map_main_t *mm = &map_main;
494   vl_api_map_param_set_tcp_reply_t *rmp;
495   int rv = 0;
496
497   map_param_set_tcp (ntohs (mp->tcp_mss));
498   REPLY_MACRO (VL_API_MAP_PARAM_SET_TCP_REPLY);
499 }
500
501
502 static void
503 vl_api_map_param_get_t_handler (vl_api_map_param_get_t * mp)
504 {
505   map_main_t *mm = &map_main;
506   vl_api_map_param_get_reply_t *rmp;
507   vl_api_registration_t *reg;
508
509   reg = vl_api_client_index_to_registration (mp->client_index);
510   if (!reg)
511     return;
512
513   rmp = vl_msg_api_alloc (sizeof (*rmp));
514   rmp->_vl_msg_id = htons (VL_API_MAP_PARAM_GET_REPLY + mm->msg_id_base);
515   rmp->context = mp->context;
516   rmp->retval = 0;
517
518   rmp->frag_inner = mm->frag_inner;
519   rmp->frag_ignore_df = mm->frag_ignore_df;
520
521   clib_memcpy (&rmp->icmp_ip4_err_relay_src,
522                &mm->icmp4_src_address, sizeof (rmp->icmp_ip4_err_relay_src));
523
524   rmp->icmp6_enable_unreachable = mm->icmp6_enabled;
525
526   /*
527    * FIXME: How are these addresses re-extracted from the FIB?
528    * Or should a local map_main copy be kept?
529    */
530   clib_memset (&rmp->ip4_nh_address, 0, sizeof (rmp->ip4_nh_address));
531   clib_memset (&rmp->ip6_nh_address, 0, sizeof (rmp->ip6_nh_address));
532
533   rmp->ip6_lifetime_ms =
534     clib_net_to_host_u16 (mm->ip6_reass_conf_lifetime_ms);
535   rmp->ip6_pool_size = clib_net_to_host_u16 (mm->ip6_reass_conf_pool_size);
536   rmp->ip6_buffers = clib_net_to_host_u32 (mm->ip6_reass_conf_buffers);
537   rmp->ip6_ht_ratio = clib_net_to_host_f64 (mm->ip6_reass_conf_ht_ratio);
538
539   rmp->sec_check_enable = mm->sec_check;
540   rmp->sec_check_fragments = mm->sec_check_frag;
541
542   rmp->tc_copy = mm->tc_copy;
543   rmp->tc_class = mm->tc;
544
545   vl_api_send_msg (reg, (u8 *) rmp);
546 }
547
548
549 int
550 map_if_enable_disable (bool is_enable, u32 sw_if_index, bool is_translation)
551 {
552   map_main_t *mm = &map_main;
553
554   if (pool_is_free_index (mm->vnet_main->interface_main.sw_interfaces,
555                           sw_if_index))
556     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
557
558   is_enable = ! !is_enable;
559
560   if (is_translation)
561     {
562       if (clib_bitmap_get (mm->bm_trans_enabled_by_sw_if, sw_if_index)
563           == is_enable)
564         return 0;
565     }
566   else
567     {
568       if (clib_bitmap_get (mm->bm_encap_enabled_by_sw_if, sw_if_index)
569           == is_enable)
570         return 0;
571     }
572
573   if (is_translation == false)
574     {
575       ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
576       vnet_feature_enable_disable ("ip4-unicast", "ip4-map", sw_if_index,
577                                    is_enable ? 1 : 0, 0, 0);
578       vnet_feature_enable_disable ("ip6-unicast", "ip6-map", sw_if_index,
579                                    is_enable ? 1 : 0, 0, 0);
580       mm->bm_encap_enabled_by_sw_if =
581         clib_bitmap_set (mm->bm_encap_enabled_by_sw_if, sw_if_index,
582                          is_enable);
583     }
584   else
585     {
586       ip4_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
587       ip6_sv_reass_enable_disable_with_refcnt (sw_if_index, is_enable);
588       vnet_feature_enable_disable ("ip4-unicast", "ip4-map-t", sw_if_index,
589                                    is_enable ? 1 : 0, 0, 0);
590       vnet_feature_enable_disable ("ip6-unicast", "ip6-map-t", sw_if_index,
591                                    is_enable ? 1 : 0, 0, 0);
592       mm->bm_trans_enabled_by_sw_if =
593         clib_bitmap_set (mm->bm_trans_enabled_by_sw_if, sw_if_index,
594                          is_enable);
595     }
596
597   return 0;
598 }
599
600
601 static void
602 vl_api_map_if_enable_disable_t_handler (vl_api_map_if_enable_disable_t * mp)
603 {
604   map_main_t *mm = &map_main;
605   vl_api_map_if_enable_disable_reply_t *rmp;
606   int rv = 0;
607
608   VALIDATE_SW_IF_INDEX (mp);
609
610   rv =
611     map_if_enable_disable (mp->is_enable, htonl (mp->sw_if_index),
612                            mp->is_translation);
613
614   BAD_SW_IF_INDEX_LABEL;
615   REPLY_MACRO (VL_API_MAP_IF_ENABLE_DISABLE_REPLY);
616 }
617
618 /* API definitions */
619 #include <vnet/format_fns.h>
620 #include <map/map.api.c>
621
622 /* Set up the API message handling tables */
623 clib_error_t *
624 map_plugin_api_hookup (vlib_main_t * vm)
625 {
626   map_main_t *mm = &map_main;
627
628   mm->msg_id_base = setup_message_id_table ();
629   return 0;
630 }
631
632 /*
633  * fd.io coding-style-patch-verification: ON
634  *
635  * Local Variables:
636  * eval: (c-set-style "gnu")
637  * End:
638  */