nat: remove non-error error counters
[vpp.git] / src / plugins / nat / nat_det.c
1 /*
2  * snat_det.c - deterministic NAT
3  *
4  * Copyright (c) 2017 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 /**
18  * @file
19  * @brief deterministic NAT
20  */
21
22 #include <nat/nat_det.h>
23
24
25 /**
26  * @brief Add/delete deterministic NAT mapping.
27  *
28  * Create bijective mapping of inside address to outside address and port range
29  * pairs, with the purpose of enabling deterministic NAT to reduce logging in
30  * CGN deployments.
31  *
32  * @param sm       SNAT main.
33  * @param in_addr  Inside network address.
34  * @param in_plen  Inside network prefix length.
35  * @param out_addr Outside network address.
36  * @param out_plen Outside network prefix length.
37  * @param is_add   If 0 delete, otherwise add.
38  */
39 int
40 snat_det_add_map (snat_main_t * sm, ip4_address_t * in_addr, u8 in_plen,
41                   ip4_address_t * out_addr, u8 out_plen, int is_add)
42 {
43   snat_det_map_t *det_map;
44   static snat_det_session_t empty_snat_det_session = { 0 };
45   snat_interface_t *i;
46   ip4_address_t in_cmp, out_cmp;
47   u8 found = 0;
48
49   in_cmp.as_u32 = in_addr->as_u32 & ip4_main.fib_masks[in_plen];
50   out_cmp.as_u32 = out_addr->as_u32 & ip4_main.fib_masks[out_plen];
51   vec_foreach (det_map, sm->det_maps)
52   {
53     /* Checking for overlapping addresses to be added here */
54     if (det_map->in_addr.as_u32 == in_cmp.as_u32 &&
55         det_map->in_plen == in_plen &&
56         det_map->out_addr.as_u32 == out_cmp.as_u32 &&
57         det_map->out_plen == out_plen)
58       {
59         found = 1;
60         break;
61       }
62   }
63
64   /* If found, don't add again */
65   if (found && is_add)
66     return VNET_API_ERROR_VALUE_EXIST;
67
68   /* If not found, don't delete */
69   if (!found && !is_add)
70     return VNET_API_ERROR_NO_SUCH_ENTRY;
71
72   if (is_add)
73     {
74       u32 num_sessions = (1 << (32 - in_plen));
75       if (num_sessions > UINT32_MAX / 1000)
76         {
77           // don't let it overflow
78           return VNET_API_ERROR_INVALID_VALUE;
79         }
80       else
81         {
82           num_sessions = num_sessions * 1000 - 1;
83         }
84
85       u32 sharing_ratio = (1 << (32 - in_plen)) / (1 << (32 - out_plen));
86       if (!sharing_ratio)
87         {
88           // avoid division by zero
89           return VNET_API_ERROR_INVALID_VALUE;
90         }
91
92       pool_get (sm->det_maps, det_map);
93       clib_memset (det_map, 0, sizeof (*det_map));
94       det_map->in_addr.as_u32 = in_cmp.as_u32;
95       det_map->in_plen = in_plen;
96       det_map->out_addr.as_u32 = out_cmp.as_u32;
97       det_map->out_plen = out_plen;
98       det_map->sharing_ratio = sharing_ratio;
99       det_map->ports_per_host = (65535 - 1023) / det_map->sharing_ratio;
100
101       vec_validate_init_empty (det_map->sessions, num_sessions,
102                                empty_snat_det_session);
103     }
104   else
105     {
106       vec_free (det_map->sessions);
107       vec_del1 (sm->det_maps, det_map - sm->det_maps);
108     }
109
110   /* Add/del external address range to FIB */
111   /* *INDENT-OFF* */
112   pool_foreach (i, sm->interfaces,
113   ({
114     if (nat_interface_is_inside(i))
115       continue;
116
117     snat_add_del_addr_to_fib(out_addr, out_plen, i->sw_if_index, is_add);
118     break;
119   }));
120   /* *INDENT-ON* */
121   return 0;
122 }
123
124 /**
125  * @brief The 'nat-det-expire-walk' process's main loop.
126  *
127  * Check expire time for active sessions.
128  */
129 static uword
130 snat_det_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
131                          vlib_frame_t * f)
132 {
133   snat_main_t *sm = &snat_main;
134   snat_det_map_t *dm;
135   snat_det_session_t *ses;
136
137   while (sm->deterministic)
138     {
139       vlib_process_wait_for_event_or_clock (vm, 10.0);
140       vlib_process_get_events (vm, NULL);
141       u32 now = (u32) vlib_time_now (vm);
142       /* *INDENT-OFF* */
143       pool_foreach (dm, sm->det_maps,
144       ({
145         vec_foreach(ses, dm->sessions)
146           {
147             /* Delete if session expired */
148             if (ses->in_port && (ses->expire < now))
149               snat_det_ses_close (dm, ses);
150           }
151       }));
152       /* *INDENT-ON* */
153     }
154
155   return 0;
156 }
157
158 static vlib_node_registration_t snat_det_expire_walk_node;
159
160 /* *INDENT-OFF* */
161 VLIB_REGISTER_NODE (snat_det_expire_walk_node, static) = {
162     .function = snat_det_expire_walk_fn,
163     .type = VLIB_NODE_TYPE_PROCESS,
164     .name =
165     "nat-det-expire-walk",
166 };
167 /* *INDENT-ON* */
168
169 /*
170  * fd.io coding-style-patch-verification: ON
171  *
172  * Local Variables:
173  * eval: (c-set-style "gnu")
174  * End:
175  */