2 * Copyright (c) 2020 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #ifndef __included_nat44_ei_inlines_h__
17 #define __included_nat44_ei_inlines_h__
19 #include <vppinfra/clib.h>
21 #include <nat/nat44-ei/nat44_ei.h>
22 #include <nat/nat44-ei/nat44_ei_ha.h>
23 #include <nat/lib/nat_proto.h>
26 calc_nat_key (ip4_address_t addr, u16 port, u32 fib_index, u8 proto)
28 ASSERT (fib_index <= (1 << 14) - 1);
29 ASSERT (proto <= (1 << 3) - 1);
30 return (u64) addr.as_u32 << 32 | (u64) port << 16 | fib_index << 3 |
35 split_nat_key (u64 key, ip4_address_t *addr, u16 *port, u32 *fib_index,
36 nat_protocol_t *proto)
40 addr->as_u32 = key >> 32;
44 *port = (key >> 16) & (u16) ~0;
48 *fib_index = key >> 3 & ((1 << 13) - 1);
57 init_nat_k (clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port,
58 u32 fib_index, nat_protocol_t proto)
60 kv->key = calc_nat_key (addr, port, fib_index, proto);
65 init_nat_kv (clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port,
66 u32 fib_index, nat_protocol_t proto, u32 thread_index,
69 init_nat_k (kv, addr, port, fib_index, proto);
70 kv->value = (u64) thread_index << 32 | session_index;
74 init_nat_i2o_k (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
76 return init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
81 init_nat_i2o_kv (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s,
82 u32 thread_index, u32 session_index)
84 init_nat_k (kv, s->in2out.addr, s->in2out.port, s->in2out.fib_index,
86 kv->value = (u64) thread_index << 32 | session_index;
90 init_nat_o2i_k (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s)
92 return init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
97 init_nat_o2i_kv (clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s,
98 u32 thread_index, u32 session_index)
100 init_nat_k (kv, s->out2in.addr, s->out2in.port, s->out2in.fib_index,
102 kv->value = (u64) thread_index << 32 | session_index;
106 nat_value_get_thread_index (clib_bihash_kv_8_8_t *value)
108 return value->value >> 32;
112 nat_value_get_session_index (clib_bihash_kv_8_8_t *value)
114 return value->value & ~(u32) 0;
118 nat44_ei_is_interface_addr (ip4_main_t *im, vlib_node_runtime_t *node,
119 u32 sw_if_index0, u32 ip4_addr)
121 nat44_ei_runtime_t *rt = (nat44_ei_runtime_t *) node->runtime_data;
124 if (PREDICT_FALSE (rt->cached_sw_if_index != sw_if_index0))
126 ip_lookup_main_t *lm = &im->lookup_main;
127 ip_interface_address_t *ia;
130 rt->cached_sw_if_index = ~0;
131 hash_free (rt->cached_presence_by_ip4_address);
133 foreach_ip_interface_address (
134 lm, ia, sw_if_index0, 1 /* honor unnumbered */, ({
135 a = ip_interface_address_get_address (lm, ia);
136 hash_set (rt->cached_presence_by_ip4_address, a->as_u32, 1);
137 rt->cached_sw_if_index = sw_if_index0;
140 if (rt->cached_sw_if_index == ~0)
144 ip4_addr_exists = !!hash_get (rt->cached_presence_by_ip4_address, ip4_addr);
145 if (PREDICT_FALSE (ip4_addr_exists))
151 /** \brief Per-user LRU list maintenance */
153 nat44_ei_session_update_lru (nat44_ei_main_t *nm, nat44_ei_session_t *s,
156 /* don't update too often - timeout is in magnitude of seconds anyway */
157 if (s->last_heard > s->last_lru_update + 1)
159 clib_dlist_remove (nm->per_thread_data[thread_index].list_pool,
161 clib_dlist_addtail (nm->per_thread_data[thread_index].list_pool,
162 s->per_user_list_head_index, s->per_user_index);
163 s->last_lru_update = s->last_heard;
168 nat44_ei_user_session_increment (nat44_ei_main_t *nm, nat44_ei_user_t *u,
171 if (u->nsessions + u->nstaticsessions < nm->max_translations_per_user)
174 u->nstaticsessions++;
181 nat44_ei_delete_user_with_no_session (nat44_ei_main_t *nm, nat44_ei_user_t *u,
184 clib_bihash_kv_8_8_t kv;
185 nat44_ei_user_key_t u_key;
186 nat44_ei_main_per_thread_data_t *tnm =
187 vec_elt_at_index (nm->per_thread_data, thread_index);
189 if (u->nstaticsessions == 0 && u->nsessions == 0)
191 u_key.addr.as_u32 = u->addr.as_u32;
192 u_key.fib_index = u->fib_index;
193 kv.key = u_key.as_u64;
194 pool_put_index (tnm->list_pool, u->sessions_per_user_list_head_index);
195 pool_put (tnm->users, u);
196 clib_bihash_add_del_8_8 (&tnm->user_hash, &kv, 0);
197 vlib_set_simple_counter (&nm->total_users, thread_index, 0,
198 pool_elts (tnm->users));
202 static_always_inline u8
203 nat44_ei_maximum_sessions_exceeded (nat44_ei_main_t *nm, u32 thread_index)
205 if (pool_elts (nm->per_thread_data[thread_index].sessions) >=
206 nm->max_translations_per_thread)
212 nat44_ei_session_update_counters (nat44_ei_session_t *s, f64 now, uword bytes,
217 s->total_bytes += bytes;
218 nat_ha_sref (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
219 s->ext_host_port, s->nat_proto, s->out2in.fib_index,
220 s->total_pkts, s->total_bytes, thread_index,
221 &s->ha_last_refreshed, now);
224 static_always_inline u32
225 nat_session_get_timeout (nat_timeouts_t *timeouts, nat_protocol_t proto,
230 case NAT_PROTOCOL_ICMP:
231 return timeouts->icmp;
232 case NAT_PROTOCOL_UDP:
233 return timeouts->udp;
234 case NAT_PROTOCOL_TCP:
237 return timeouts->tcp.transitory;
239 return timeouts->tcp.established;
242 return timeouts->udp;
247 #endif /* __included_nat44_ei_inlines_h__ */
250 * fd.io coding-style-patch-verification: ON
253 * eval: (c-set-style "gnu")