2 * snat_det.c - deterministic NAT
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief deterministic NAT
22 #include <snat/snat_det.h>
26 * @brief Add/delete deterministic NAT mapping.
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
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.
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)
43 snat_det_map_t *det_map;
44 static snat_det_session_t empty_snat_det_session = { 0 };
47 pool_get (sm->det_maps, det_map);
48 memset (det_map, 0, sizeof (*det_map));
49 det_map->in_addr.as_u32 = in_addr->as_u32 & ip4_main.fib_masks[in_plen];
50 det_map->in_plen = in_plen;
51 det_map->out_addr.as_u32 = out_addr->as_u32 & ip4_main.fib_masks[out_plen];
52 det_map->out_plen = out_plen;
53 det_map->sharing_ratio = (1 << (32 - in_plen)) / (1 << (32 - out_plen));
54 det_map->ports_per_host = (65535 - 1023) / det_map->sharing_ratio;
56 vec_validate_init_empty (det_map->sessions,
57 SNAT_DET_SES_PER_USER * (1 << (32 - in_plen)) - 1,
58 empty_snat_det_session);
60 /* Add/del external address range to FIB */
62 pool_foreach (i, sm->interfaces,
67 snat_add_del_addr_to_fib(out_addr, out_plen, i->sw_if_index, is_add);
75 * @brief The 'snat-det-expire-walk' process's main loop.
77 * Check expire time for active sessions.
80 snat_det_expire_walk_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
83 snat_main_t *sm = &snat_main;
85 snat_det_session_t *ses;
87 while (sm->deterministic)
89 vlib_process_wait_for_event_or_clock (vm, 10.0);
90 vlib_process_get_events (vm, NULL);
91 u32 now = (u32) vlib_time_now (vm);
93 pool_foreach (dm, sm->det_maps,
95 vec_foreach(ses, dm->sessions)
97 /* Delete if session expired */
98 if (ses->in_port && (ses->expire < now))
99 snat_det_ses_close (dm, ses);
108 static vlib_node_registration_t snat_det_expire_walk_node;
111 VLIB_REGISTER_NODE (snat_det_expire_walk_node, static) = {
112 .function = snat_det_expire_walk_fn,
113 .type = VLIB_NODE_TYPE_PROCESS,
115 "snat-det-expire-walk",
120 * fd.io coding-style-patch-verification: ON
123 * eval: (c-set-style "gnu")