nat: improve perf - long read after short write
[vpp.git] / src / plugins / nat / out2in_ed.c
1 /*
2  * Copyright (c) 2018 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /**
16  * @file
17  * @brief NAT44 endpoint-dependent outside to inside network translation
18  */
19
20 #include <vlib/vlib.h>
21 #include <vnet/vnet.h>
22 #include <vnet/pg/pg.h>
23 #include <vnet/ip/ip.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/ip4_fib.h>
26 #include <vnet/udp/udp.h>
27 #include <vppinfra/error.h>
28 #include <nat/nat.h>
29 #include <nat/nat_ipfix_logging.h>
30 #include <nat/nat_inlines.h>
31 #include <nat/nat44/inlines.h>
32 #include <nat/nat_syslog.h>
33 #include <nat/nat_ha.h>
34
35 static char *nat_out2in_ed_error_strings[] = {
36 #define _(sym,string) string,
37   foreach_nat_out2in_ed_error
38 #undef _
39 };
40
41 typedef struct
42 {
43   u32 sw_if_index;
44   u32 next_index;
45   u32 session_index;
46   u32 is_slow_path;
47 } nat44_ed_out2in_trace_t;
48
49 static u8 *
50 format_nat44_ed_out2in_trace (u8 * s, va_list * args)
51 {
52   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54   nat44_ed_out2in_trace_t *t = va_arg (*args, nat44_ed_out2in_trace_t *);
55   char *tag;
56
57   tag =
58     t->is_slow_path ? "NAT44_OUT2IN_ED_SLOW_PATH" :
59     "NAT44_OUT2IN_ED_FAST_PATH";
60
61   s = format (s, "%s: sw_if_index %d, next index %d, session %d", tag,
62               t->sw_if_index, t->next_index, t->session_index);
63
64   return s;
65 }
66
67 static inline u32
68 icmp_out2in_ed_slow_path (snat_main_t * sm, vlib_buffer_t * b0,
69                           ip4_header_t * ip0, icmp46_header_t * icmp0,
70                           u32 sw_if_index0, u32 rx_fib_index0,
71                           vlib_node_runtime_t * node, u32 next0, f64 now,
72                           u32 thread_index, snat_session_t ** p_s0)
73 {
74   next0 = icmp_out2in (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
75                        next0, thread_index, p_s0, 0);
76   snat_session_t *s0 = *p_s0;
77   if (PREDICT_TRUE (next0 != NAT_NEXT_DROP && s0))
78     {
79       /* Accounting */
80       nat44_session_update_counters (s0, now,
81                                      vlib_buffer_length_in_chain
82                                      (sm->vlib_main, b0), thread_index);
83       /* Per-user LRU list maintenance */
84       nat44_session_update_lru (sm, s0, thread_index);
85     }
86   return next0;
87 }
88
89 #ifndef CLIB_MARCH_VARIANT
90 int
91 nat44_o2i_ed_is_idle_session_cb (clib_bihash_kv_16_8_t * kv, void *arg)
92 {
93   snat_main_t *sm = &snat_main;
94   nat44_is_idle_session_ctx_t *ctx = arg;
95   snat_session_t *s;
96   u64 sess_timeout_time;
97   u8 proto;
98   u16 r_port, l_port;
99   ip4_address_t *l_addr, *r_addr;
100   u32 fib_index;
101   clib_bihash_kv_16_8_t ed_kv;
102   int i;
103   snat_address_t *a;
104   snat_session_key_t key;
105   snat_main_per_thread_data_t *tsm = vec_elt_at_index (sm->per_thread_data,
106                                                        ctx->thread_index);
107
108   s = pool_elt_at_index (tsm->sessions, kv->value);
109   sess_timeout_time = s->last_heard + (f64) nat44_session_get_timeout (sm, s);
110   if (ctx->now >= sess_timeout_time)
111     {
112       l_addr = &s->in2out.addr;
113       r_addr = &s->ext_host_addr;
114       fib_index = s->in2out.fib_index;
115       if (snat_is_unk_proto_session (s))
116         {
117           proto = s->in2out.port;
118           r_port = 0;
119           l_port = 0;
120         }
121       else
122         {
123           proto = snat_proto_to_ip_proto (s->in2out.protocol);
124           l_port = s->in2out.port;
125           r_port = s->ext_host_port;
126         }
127       if (is_twice_nat_session (s))
128         {
129           r_addr = &s->ext_host_nat_addr;
130           r_port = s->ext_host_nat_port;
131         }
132       make_ed_kv (l_addr, r_addr, proto, fib_index, l_port, r_port, ~0ULL,
133                   &ed_kv);
134       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &ed_kv, 0))
135         nat_elog_warn ("in2out_ed key del failed");
136
137       if (snat_is_unk_proto_session (s))
138         goto delete;
139
140       snat_ipfix_logging_nat44_ses_delete (ctx->thread_index,
141                                            s->in2out.addr.as_u32,
142                                            s->out2in.addr.as_u32,
143                                            s->in2out.protocol,
144                                            s->in2out.port,
145                                            s->out2in.port,
146                                            s->in2out.fib_index);
147
148       nat_syslog_nat44_sdel (s->user_index, s->in2out.fib_index,
149                              &s->in2out.addr, s->in2out.port,
150                              &s->ext_host_nat_addr, s->ext_host_nat_port,
151                              &s->out2in.addr, s->out2in.port,
152                              &s->ext_host_addr, s->ext_host_port,
153                              s->in2out.protocol, is_twice_nat_session (s));
154
155       nat_ha_sdel (&s->out2in.addr, s->out2in.port, &s->ext_host_addr,
156                    s->ext_host_port, s->out2in.protocol, s->out2in.fib_index,
157                    ctx->thread_index);
158
159       if (is_twice_nat_session (s))
160         {
161           for (i = 0; i < vec_len (sm->twice_nat_addresses); i++)
162             {
163               key.protocol = s->in2out.protocol;
164               key.port = s->ext_host_nat_port;
165               a = sm->twice_nat_addresses + i;
166               if (a->addr.as_u32 == s->ext_host_nat_addr.as_u32)
167                 {
168                   snat_free_outside_address_and_port (sm->twice_nat_addresses,
169                                                       ctx->thread_index,
170                                                       &key);
171                   break;
172                 }
173             }
174         }
175
176       if (snat_is_session_static (s))
177         goto delete;
178
179       snat_free_outside_address_and_port (sm->addresses, ctx->thread_index,
180                                           &s->out2in);
181     delete:
182       nat44_ed_delete_session (sm, s, ctx->thread_index, 1);
183       return 1;
184     }
185
186   return 0;
187 }
188 #endif
189
190 static snat_session_t *
191 create_session_for_static_mapping_ed (snat_main_t * sm,
192                                       vlib_buffer_t * b,
193                                       snat_session_key_t l_key,
194                                       snat_session_key_t e_key,
195                                       vlib_node_runtime_t * node,
196                                       u32 thread_index,
197                                       twice_nat_type_t twice_nat,
198                                       lb_nat_type_t lb_nat, f64 now)
199 {
200   snat_session_t *s;
201   ip4_header_t *ip;
202   udp_header_t *udp;
203   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
204   clib_bihash_kv_16_8_t kv;
205   snat_session_key_t eh_key;
206   nat44_is_idle_session_ctx_t ctx;
207
208   if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
209     {
210       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
211       nat_elog_notice ("maximum sessions exceeded");
212       return 0;
213     }
214
215   s = nat_ed_session_alloc (sm, thread_index, now);
216   if (!s)
217     {
218       b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
219       nat_elog_warn ("create NAT session failed");
220       return 0;
221     }
222
223   ip = vlib_buffer_get_current (b);
224   udp = ip4_next_header (ip);
225
226   s->ext_host_addr.as_u32 = ip->src_address.as_u32;
227   s->ext_host_port = e_key.protocol == SNAT_PROTOCOL_ICMP ? 0 : udp->src_port;
228   s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
229   if (lb_nat)
230     s->flags |= SNAT_SESSION_FLAG_LOAD_BALANCING;
231   if (lb_nat == AFFINITY_LB_NAT)
232     s->flags |= SNAT_SESSION_FLAG_AFFINITY;
233   s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
234   s->out2in = e_key;
235   s->in2out = l_key;
236   s->in2out.protocol = s->out2in.protocol;
237
238   /* Add to lookup tables */
239   make_ed_kv (&e_key.addr, &s->ext_host_addr, ip->protocol,
240               e_key.fib_index, e_key.port, s->ext_host_port,
241               s - tsm->sessions, &kv);
242   ctx.now = now;
243   ctx.thread_index = thread_index;
244   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->out2in_ed, &kv,
245                                                nat44_o2i_ed_is_idle_session_cb,
246                                                &ctx))
247     nat_elog_notice ("out2in-ed key add failed");
248
249   if (twice_nat == TWICE_NAT || (twice_nat == TWICE_NAT_SELF &&
250                                  ip->src_address.as_u32 == l_key.addr.as_u32))
251     {
252       eh_key.protocol = e_key.protocol;
253       if (snat_alloc_outside_address_and_port (sm->twice_nat_addresses, 0,
254                                                thread_index, &eh_key,
255                                                sm->port_per_thread,
256                                                tsm->snat_thread_index))
257         {
258           b->error = node->errors[NAT_OUT2IN_ED_ERROR_OUT_OF_PORTS];
259           nat44_ed_delete_session (sm, s, thread_index, 1);
260           if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &kv, 0))
261             nat_elog_notice ("out2in-ed key del failed");
262           return 0;
263         }
264       s->ext_host_nat_addr.as_u32 = eh_key.addr.as_u32;
265       s->ext_host_nat_port = eh_key.port;
266       s->flags |= SNAT_SESSION_FLAG_TWICE_NAT;
267       make_ed_kv (&l_key.addr, &s->ext_host_nat_addr, ip->protocol,
268                   l_key.fib_index, l_key.port, s->ext_host_nat_port,
269                   s - tsm->sessions, &kv);
270     }
271   else
272     {
273       make_ed_kv (&l_key.addr, &s->ext_host_addr, ip->protocol,
274                   l_key.fib_index, l_key.port, s->ext_host_port,
275                   s - tsm->sessions, &kv);
276     }
277   if (clib_bihash_add_or_overwrite_stale_16_8 (&tsm->in2out_ed, &kv,
278                                                nat44_i2o_ed_is_idle_session_cb,
279                                                &ctx))
280     nat_elog_notice ("in2out-ed key add failed");
281
282   snat_ipfix_logging_nat44_ses_create (thread_index,
283                                        s->in2out.addr.as_u32,
284                                        s->out2in.addr.as_u32,
285                                        s->in2out.protocol,
286                                        s->in2out.port,
287                                        s->out2in.port, s->in2out.fib_index);
288
289   nat_syslog_nat44_sadd (s->user_index, s->in2out.fib_index,
290                          &s->in2out.addr, s->in2out.port,
291                          &s->ext_host_nat_addr, s->ext_host_nat_port,
292                          &s->out2in.addr, s->out2in.port,
293                          &s->ext_host_addr, s->ext_host_port,
294                          s->in2out.protocol, is_twice_nat_session (s));
295
296   nat_ha_sadd (&s->in2out.addr, s->in2out.port, &s->out2in.addr,
297                s->out2in.port, &s->ext_host_addr, s->ext_host_port,
298                &s->ext_host_nat_addr, s->ext_host_nat_port,
299                s->in2out.protocol, s->in2out.fib_index, s->flags,
300                thread_index, 0);
301
302   return s;
303 }
304
305 static int
306 next_src_nat (snat_main_t * sm, ip4_header_t * ip, u16 src_port,
307               u16 dst_port, u32 thread_index, u32 rx_fib_index)
308 {
309   clib_bihash_kv_16_8_t kv, value;
310   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
311
312   make_ed_kv (&ip->src_address, &ip->dst_address, ip->protocol,
313               rx_fib_index, src_port, dst_port, ~0ULL, &kv);
314   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
315     return 1;
316
317   return 0;
318 }
319
320 static void
321 create_bypass_for_fwd (snat_main_t * sm, vlib_buffer_t * b, ip4_header_t * ip,
322                        u32 rx_fib_index, u32 thread_index)
323 {
324   clib_bihash_kv_16_8_t kv, value;
325   udp_header_t *udp;
326   snat_session_t *s = 0;
327   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
328   f64 now = vlib_time_now (sm->vlib_main);
329   u16 l_port, r_port;
330
331   if (ip->protocol == IP_PROTOCOL_ICMP)
332     {
333       if (get_icmp_o2i_ed_key
334           (b, ip, rx_fib_index, ~0ULL, 0, &l_port, &r_port, &kv))
335         return;
336     }
337   else
338     {
339       if (ip->protocol == IP_PROTOCOL_UDP || ip->protocol == IP_PROTOCOL_TCP)
340         {
341           udp = ip4_next_header (ip);
342           l_port = udp->dst_port;
343           r_port = udp->src_port;
344         }
345       else
346         {
347           l_port = 0;
348           r_port = 0;
349         }
350       make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
351                   rx_fib_index, l_port, r_port, ~0ULL, &kv);
352     }
353
354   if (!clib_bihash_search_16_8 (&tsm->in2out_ed, &kv, &value))
355     {
356       s = pool_elt_at_index (tsm->sessions, value.value);
357     }
358   else
359     {
360       u32 proto;
361
362       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
363         return;
364
365       s = nat_ed_session_alloc (sm, thread_index, now);
366       if (!s)
367         {
368           nat_elog_warn ("create NAT session failed");
369           return;
370         }
371
372       proto = ip_proto_to_snat_proto (ip->protocol);
373
374       s->ext_host_addr = ip->src_address;
375       s->ext_host_port = r_port;
376       s->flags |= SNAT_SESSION_FLAG_FWD_BYPASS;
377       s->out2in.addr = ip->dst_address;
378       s->out2in.port = l_port;
379       s->out2in.protocol = proto;
380       if (proto == ~0)
381         {
382           s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
383           s->out2in.port = ip->protocol;
384         }
385       s->out2in.fib_index = 0;
386       s->in2out = s->out2in;
387
388       kv.value = s - tsm->sessions;
389       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &kv, 1))
390         nat_elog_notice ("in2out_ed key add failed");
391     }
392
393   if (ip->protocol == IP_PROTOCOL_TCP)
394     {
395       tcp_header_t *tcp = ip4_next_header (ip);
396       if (nat44_set_tcp_session_state_o2i
397           (sm, now, s, tcp->flags, tcp->ack_number, tcp->seq_number,
398            thread_index))
399         return;
400     }
401
402   /* Accounting */
403   nat44_session_update_counters (s, now, 0, thread_index);
404   /* Per-user LRU list maintenance */
405   nat44_session_update_lru (sm, s, thread_index);
406 }
407
408 static inline void
409 create_bypass_for_fwd_worker (snat_main_t * sm, vlib_buffer_t * b,
410                               ip4_header_t * ip, u32 rx_fib_index)
411 {
412   ip4_header_t ip_wkr = {
413     .src_address = ip->dst_address,
414   };
415   u32 thread_index = sm->worker_in2out_cb (&ip_wkr, rx_fib_index, 0);
416
417   create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
418 }
419
420 #ifndef CLIB_MARCH_VARIANT
421 u32
422 icmp_match_out2in_ed (snat_main_t * sm, vlib_node_runtime_t * node,
423                       u32 thread_index, vlib_buffer_t * b, ip4_header_t * ip,
424                       u8 * p_proto, snat_session_key_t * p_value,
425                       u8 * p_dont_translate, void *d, void *e)
426 {
427   u32 next = ~0, sw_if_index, rx_fib_index;
428   clib_bihash_kv_16_8_t kv, value;
429   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
430   snat_session_t *s = 0;
431   u8 dont_translate = 0, is_addr_only, identity_nat;
432   snat_session_key_t e_key, l_key;
433   u16 l_port, r_port;
434
435   sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
436   rx_fib_index = ip4_fib_table_get_index_for_sw_if_index (sw_if_index);
437
438   if (get_icmp_o2i_ed_key
439       (b, ip, rx_fib_index, ~0ULL, p_proto, &l_port, &r_port, &kv))
440     {
441       b->error = node->errors[NAT_OUT2IN_ED_ERROR_UNSUPPORTED_PROTOCOL];
442       next = NAT_NEXT_DROP;
443       goto out;
444     }
445
446   if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv, &value))
447     {
448       /* Try to match static mapping */
449       e_key.addr = ip->dst_address;
450       e_key.port = l_port;
451       e_key.protocol = ip_proto_to_snat_proto (ip->protocol);
452       e_key.fib_index = rx_fib_index;
453       if (snat_static_mapping_match
454           (sm, e_key, &l_key, 1, &is_addr_only, 0, 0, 0, &identity_nat))
455         {
456           if (!sm->forwarding_enabled)
457             {
458               /* Don't NAT packet aimed at the intfc address */
459               if (PREDICT_FALSE (is_interface_addr (sm, node, sw_if_index,
460                                                     ip->dst_address.as_u32)))
461                 {
462                   dont_translate = 1;
463                   goto out;
464                 }
465               b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
466               next = NAT_NEXT_DROP;
467               goto out;
468             }
469           else
470             {
471               dont_translate = 1;
472               if (next_src_nat (sm, ip, l_port, r_port,
473                                 thread_index, rx_fib_index))
474                 {
475                   next = NAT_NEXT_IN2OUT_ED_FAST_PATH;
476                   goto out;
477                 }
478               if (sm->num_workers > 1)
479                 create_bypass_for_fwd_worker (sm, b, ip, rx_fib_index);
480               else
481                 create_bypass_for_fwd (sm, b, ip, rx_fib_index, thread_index);
482               goto out;
483             }
484         }
485
486       if (PREDICT_FALSE
487           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
488            ICMP4_echo_reply
489            && (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
490                ICMP4_echo_request || !is_addr_only)))
491         {
492           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
493           next = NAT_NEXT_DROP;
494           goto out;
495         }
496
497       if (PREDICT_FALSE (identity_nat))
498         {
499           dont_translate = 1;
500           goto out;
501         }
502
503       /* Create session initiated by host from external network */
504       s = create_session_for_static_mapping_ed (sm, b, l_key, e_key, node,
505                                                 thread_index, 0, 0,
506                                                 vlib_time_now
507                                                 (sm->vlib_main));
508
509       if (!s)
510         {
511           next = NAT_NEXT_DROP;
512           goto out;
513         }
514     }
515   else
516     {
517       if (PREDICT_FALSE
518           (vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
519            ICMP4_echo_reply
520            && vnet_buffer (b)->ip.reass.icmp_type_or_tcp_flags !=
521            ICMP4_echo_request
522            && !icmp_type_is_error_message (vnet_buffer (b)->ip.
523                                            reass.icmp_type_or_tcp_flags)))
524         {
525           b->error = node->errors[NAT_OUT2IN_ED_ERROR_BAD_ICMP_TYPE];
526           next = NAT_NEXT_DROP;
527           goto out;
528         }
529
530       s = pool_elt_at_index (tsm->sessions, value.value);
531     }
532 out:
533   if (s)
534     *p_value = s->in2out;
535   *p_dont_translate = dont_translate;
536   if (d)
537     *(snat_session_t **) d = s;
538   return next;
539 }
540 #endif
541
542 static snat_session_t *
543 nat44_ed_out2in_unknown_proto (snat_main_t * sm,
544                                vlib_buffer_t * b,
545                                ip4_header_t * ip,
546                                u32 rx_fib_index,
547                                u32 thread_index,
548                                f64 now,
549                                vlib_main_t * vm, vlib_node_runtime_t * node)
550 {
551   clib_bihash_kv_8_8_t kv, value;
552   clib_bihash_kv_16_8_t s_kv, s_value;
553   snat_static_mapping_t *m;
554   u32 old_addr, new_addr;
555   ip_csum_t sum;
556   snat_session_t *s;
557   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
558
559   old_addr = ip->dst_address.as_u32;
560
561   make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol, rx_fib_index,
562               0, 0, ~0ULL, &s_kv);
563
564   if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &s_kv, &s_value))
565     {
566       s = pool_elt_at_index (tsm->sessions, s_value.value);
567       new_addr = ip->dst_address.as_u32 = s->in2out.addr.as_u32;
568     }
569   else
570     {
571       if (PREDICT_FALSE (nat44_maximum_sessions_exceeded (sm, thread_index)))
572         {
573           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_SESSIONS_EXCEEDED];
574           nat_elog_notice ("maximum sessions exceeded");
575           return 0;
576         }
577
578       make_sm_kv (&kv, &ip->dst_address, 0, 0, 0);
579       if (clib_bihash_search_8_8
580           (&sm->static_mapping_by_external, &kv, &value))
581         {
582           b->error = node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
583           return 0;
584         }
585
586       m = pool_elt_at_index (sm->static_mappings, value.value);
587
588       new_addr = ip->dst_address.as_u32 = m->local_addr.as_u32;
589
590       /* Create a new session */
591       s = nat_ed_session_alloc (sm, thread_index, now);
592       if (!s)
593         {
594           b->error = node->errors[NAT_OUT2IN_ED_ERROR_MAX_USER_SESS_EXCEEDED];
595           nat_elog_warn ("create NAT session failed");
596           return 0;
597         }
598
599       s->ext_host_addr.as_u32 = ip->src_address.as_u32;
600       s->flags |= SNAT_SESSION_FLAG_UNKNOWN_PROTO;
601       s->flags |= SNAT_SESSION_FLAG_STATIC_MAPPING;
602       s->flags |= SNAT_SESSION_FLAG_ENDPOINT_DEPENDENT;
603       s->out2in.addr.as_u32 = old_addr;
604       s->out2in.fib_index = rx_fib_index;
605       s->in2out.addr.as_u32 = new_addr;
606       s->in2out.fib_index = m->fib_index;
607       s->in2out.port = s->out2in.port = ip->protocol;
608
609       /* Add to lookup tables */
610       s_kv.value = s - tsm->sessions;
611       if (clib_bihash_add_del_16_8 (&tsm->out2in_ed, &s_kv, 1))
612         nat_elog_notice ("out2in key add failed");
613
614       make_ed_kv (&ip->dst_address, &ip->src_address, ip->protocol,
615                   m->fib_index, 0, 0, s - tsm->sessions, &s_kv);
616       if (clib_bihash_add_del_16_8 (&tsm->in2out_ed, &s_kv, 1))
617         nat_elog_notice ("in2out key add failed");
618     }
619
620   /* Update IP checksum */
621   sum = ip->checksum;
622   sum = ip_csum_update (sum, old_addr, new_addr, ip4_header_t, dst_address);
623   ip->checksum = ip_csum_fold (sum);
624
625   vnet_buffer (b)->sw_if_index[VLIB_TX] = s->in2out.fib_index;
626
627   /* Accounting */
628   nat44_session_update_counters (s, now, vlib_buffer_length_in_chain (vm, b),
629                                  thread_index);
630   /* Per-user LRU list maintenance */
631   nat44_session_update_lru (sm, s, thread_index);
632
633   return s;
634 }
635
636 static inline uword
637 nat44_ed_out2in_fast_path_node_fn_inline (vlib_main_t * vm,
638                                           vlib_node_runtime_t * node,
639                                           vlib_frame_t * frame)
640 {
641   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
642   nat_next_t next_index;
643   snat_main_t *sm = &snat_main;
644   f64 now = vlib_time_now (vm);
645   u32 thread_index = vm->thread_index;
646   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
647   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
648     0, fragments = 0;
649
650   stats_node_index = sm->ed_out2in_node_index;
651
652   from = vlib_frame_vector_args (frame);
653   n_left_from = frame->n_vectors;
654   next_index = node->cached_next_index;
655
656   while (n_left_from > 0)
657     {
658       u32 n_left_to_next;
659
660       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
661
662       while (n_left_from > 0 && n_left_to_next > 0)
663         {
664           u32 bi0;
665           vlib_buffer_t *b0;
666           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
667             new_addr0;
668           u16 old_port0, new_port0;
669           ip4_header_t *ip0;
670           udp_header_t *udp0;
671           tcp_header_t *tcp0;
672           snat_session_t *s0 = 0;
673           clib_bihash_kv_16_8_t kv0, value0;
674           ip_csum_t sum0;
675
676           /* speculatively enqueue b0 to the current next frame */
677           bi0 = from[0];
678           to_next[0] = bi0;
679           from += 1;
680           to_next += 1;
681           n_left_from -= 1;
682           n_left_to_next -= 1;
683
684           b0 = vlib_get_buffer (vm, bi0);
685           next0 = vnet_buffer2 (b0)->nat.arc_next;
686
687           vnet_buffer (b0)->snat.flags = 0;
688           ip0 = vlib_buffer_get_current (b0);
689
690           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
691           rx_fib_index0 =
692             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
693                                                  sw_if_index0);
694
695           if (PREDICT_FALSE (ip0->ttl == 1))
696             {
697               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
698               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
699                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
700                                            0);
701               next0 = NAT_NEXT_ICMP_ERROR;
702               goto trace0;
703             }
704
705           udp0 = ip4_next_header (ip0);
706           tcp0 = (tcp_header_t *) udp0;
707           proto0 = ip_proto_to_snat_proto (ip0->protocol);
708
709           if (PREDICT_FALSE (proto0 == ~0))
710             {
711               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
712               goto trace0;
713             }
714
715           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
716             {
717               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
718               goto trace0;
719             }
720
721           make_ed_kv (&ip0->dst_address, &ip0->src_address,
722                       ip0->protocol, rx_fib_index0,
723                       vnet_buffer (b0)->ip.reass.l4_dst_port,
724                       vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
725
726           if (clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
727             {
728               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
729               goto trace0;
730             }
731           s0 = pool_elt_at_index (tsm->sessions, value0.value);
732
733           if (s0->tcp_close_timestamp)
734             {
735               if (now >= s0->tcp_close_timestamp)
736                 {
737                   // session is closed, go slow path
738                   next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
739                 }
740               else
741                 {
742                   // session in transitory timeout, drop
743                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_TCP_CLOSED];
744                   next0 = NAT_NEXT_DROP;
745                 }
746               goto trace0;
747             }
748
749           // drop if session expired
750           u64 sess_timeout_time;
751           sess_timeout_time = s0->last_heard +
752             (f64) nat44_session_get_timeout (sm, s0);
753           if (now >= sess_timeout_time)
754             {
755               // session is closed, go slow path
756               nat_free_session_data (sm, s0, thread_index, 0);
757               nat44_ed_delete_session (sm, s0, thread_index, 1);
758               next0 = NAT_NEXT_OUT2IN_ED_SLOW_PATH;
759               goto trace0;
760             }
761           //
762
763           old_addr0 = ip0->dst_address.as_u32;
764           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
765           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
766
767           sum0 = ip0->checksum;
768           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
769                                  dst_address);
770           if (PREDICT_FALSE (is_twice_nat_session (s0)))
771             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
772                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
773                                    src_address);
774           ip0->checksum = ip_csum_fold (sum0);
775
776           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
777
778           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
779             {
780               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
781                 {
782                   new_port0 = udp0->dst_port = s0->in2out.port;
783                   sum0 = tcp0->checksum;
784                   sum0 =
785                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
786                                     dst_address);
787                   sum0 =
788                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
789                                     length);
790                   if (is_twice_nat_session (s0))
791                     {
792                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
793                                              s0->ext_host_nat_addr.as_u32,
794                                              ip4_header_t, dst_address);
795                       sum0 =
796                         ip_csum_update (sum0,
797                                         vnet_buffer (b0)->ip.
798                                         reass.l4_src_port,
799                                         s0->ext_host_nat_port, ip4_header_t,
800                                         length);
801                       tcp0->src_port = s0->ext_host_nat_port;
802                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
803                     }
804                   tcp0->checksum = ip_csum_fold (sum0);
805                 }
806               tcp_packets++;
807               if (nat44_set_tcp_session_state_o2i
808                   (sm, now, s0,
809                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
810                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
811                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
812                 goto trace0;
813             }
814           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
815                    && udp0->checksum)
816             {
817               new_port0 = udp0->dst_port = s0->in2out.port;
818               sum0 = udp0->checksum;
819               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
820                                      dst_address);
821               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
822                                      length);
823               if (PREDICT_FALSE (is_twice_nat_session (s0)))
824                 {
825                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
826                                          s0->ext_host_nat_addr.as_u32,
827                                          ip4_header_t, dst_address);
828                   sum0 =
829                     ip_csum_update (sum0,
830                                     vnet_buffer (b0)->ip.reass.l4_src_port,
831                                     s0->ext_host_nat_port, ip4_header_t,
832                                     length);
833                   udp0->src_port = s0->ext_host_nat_port;
834                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
835                 }
836               udp0->checksum = ip_csum_fold (sum0);
837               udp_packets++;
838             }
839           else
840             {
841               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
842                 {
843                   new_port0 = udp0->dst_port = s0->in2out.port;
844                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
845                     {
846                       udp0->src_port = s0->ext_host_nat_port;
847                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
848                     }
849                 }
850               udp_packets++;
851             }
852
853           /* Accounting */
854           nat44_session_update_counters (s0, now,
855                                          vlib_buffer_length_in_chain (vm, b0),
856                                          thread_index);
857           /* Per-user LRU list maintenance */
858           nat44_session_update_lru (sm, s0, thread_index);
859
860         trace0:
861           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
862                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
863             {
864               nat44_ed_out2in_trace_t *t =
865                 vlib_add_trace (vm, node, b0, sizeof (*t));
866               t->sw_if_index = sw_if_index0;
867               t->next_index = next0;
868               t->is_slow_path = 0;
869
870               if (s0)
871                 t->session_index = s0 - tsm->sessions;
872               else
873                 t->session_index = ~0;
874             }
875
876           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
877           /* verify speculative enqueue, maybe switch current next frame */
878           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
879                                            to_next, n_left_to_next,
880                                            bi0, next0);
881         }
882
883       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
884     }
885
886   vlib_node_increment_counter (vm, stats_node_index,
887                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
888                                pkts_processed);
889   vlib_node_increment_counter (vm, stats_node_index,
890                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
891   vlib_node_increment_counter (vm, stats_node_index,
892                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
893   vlib_node_increment_counter (vm, stats_node_index,
894                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
895                                icmp_packets);
896   vlib_node_increment_counter (vm, stats_node_index,
897                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
898                                other_packets);
899   vlib_node_increment_counter (vm, stats_node_index,
900                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
901   return frame->n_vectors;
902 }
903
904 static inline uword
905 nat44_ed_out2in_slow_path_node_fn_inline (vlib_main_t * vm,
906                                           vlib_node_runtime_t * node,
907                                           vlib_frame_t * frame)
908 {
909   u32 n_left_from, *from, *to_next, pkts_processed = 0, stats_node_index;
910   nat_next_t next_index;
911   snat_main_t *sm = &snat_main;
912   f64 now = vlib_time_now (vm);
913   u32 thread_index = vm->thread_index;
914   snat_main_per_thread_data_t *tsm = &sm->per_thread_data[thread_index];
915   u32 tcp_packets = 0, udp_packets = 0, icmp_packets = 0, other_packets =
916     0, fragments = 0;
917
918   stats_node_index = sm->ed_out2in_slowpath_node_index;
919
920   from = vlib_frame_vector_args (frame);
921   n_left_from = frame->n_vectors;
922   next_index = node->cached_next_index;
923
924   while (n_left_from > 0)
925     {
926       u32 n_left_to_next;
927
928       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
929
930       while (n_left_from > 0 && n_left_to_next > 0)
931         {
932           u32 bi0;
933           vlib_buffer_t *b0;
934           u32 next0, sw_if_index0, rx_fib_index0, proto0, old_addr0,
935             new_addr0;
936           u16 old_port0, new_port0;
937           ip4_header_t *ip0;
938           udp_header_t *udp0;
939           tcp_header_t *tcp0;
940           icmp46_header_t *icmp0;
941           snat_session_t *s0 = 0;
942           clib_bihash_kv_16_8_t kv0, value0;
943           ip_csum_t sum0;
944           snat_session_key_t e_key0, l_key0;
945           lb_nat_type_t lb_nat0;
946           twice_nat_type_t twice_nat0;
947           u8 identity_nat0;
948
949           /* speculatively enqueue b0 to the current next frame */
950           bi0 = from[0];
951           to_next[0] = bi0;
952           from += 1;
953           to_next += 1;
954           n_left_from -= 1;
955           n_left_to_next -= 1;
956
957           b0 = vlib_get_buffer (vm, bi0);
958           next0 = vnet_buffer2 (b0)->nat.arc_next;
959
960           vnet_buffer (b0)->snat.flags = 0;
961           ip0 = vlib_buffer_get_current (b0);
962
963           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
964           rx_fib_index0 =
965             fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4,
966                                                  sw_if_index0);
967
968           if (PREDICT_FALSE (ip0->ttl == 1))
969             {
970               vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
971               icmp4_error_set_vnet_buffer (b0, ICMP4_time_exceeded,
972                                            ICMP4_time_exceeded_ttl_exceeded_in_transit,
973                                            0);
974               next0 = NAT_NEXT_ICMP_ERROR;
975               goto trace0;
976             }
977
978           udp0 = ip4_next_header (ip0);
979           tcp0 = (tcp_header_t *) udp0;
980           icmp0 = (icmp46_header_t *) udp0;
981           proto0 = ip_proto_to_snat_proto (ip0->protocol);
982
983           if (PREDICT_FALSE (proto0 == ~0))
984             {
985               s0 =
986                 nat44_ed_out2in_unknown_proto (sm, b0, ip0, rx_fib_index0,
987                                                thread_index, now, vm, node);
988               if (!sm->forwarding_enabled)
989                 {
990                   if (!s0)
991                     next0 = NAT_NEXT_DROP;
992                 }
993               other_packets++;
994               goto trace0;
995             }
996
997           if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_ICMP))
998             {
999               next0 = icmp_out2in_ed_slow_path
1000                 (sm, b0, ip0, icmp0, sw_if_index0, rx_fib_index0, node,
1001                  next0, now, thread_index, &s0);
1002               icmp_packets++;
1003               goto trace0;
1004             }
1005
1006           make_ed_kv (&ip0->dst_address, &ip0->src_address,
1007                       ip0->protocol, rx_fib_index0,
1008                       vnet_buffer (b0)->ip.reass.l4_dst_port,
1009                       vnet_buffer (b0)->ip.reass.l4_src_port, ~0ULL, &kv0);
1010
1011           s0 = NULL;
1012           if (!clib_bihash_search_16_8 (&tsm->out2in_ed, &kv0, &value0))
1013             {
1014               s0 = pool_elt_at_index (tsm->sessions, value0.value);
1015
1016               if (s0->tcp_close_timestamp && now >= s0->tcp_close_timestamp)
1017                 {
1018                   nat_free_session_data (sm, s0, thread_index, 0);
1019                   nat44_ed_delete_session (sm, s0, thread_index, 1);
1020                   s0 = NULL;
1021                 }
1022             }
1023
1024           if (!s0)
1025             {
1026               /* Try to match static mapping by external address and port,
1027                  destination address and port in packet */
1028               e_key0.addr = ip0->dst_address;
1029               e_key0.port = vnet_buffer (b0)->ip.reass.l4_dst_port;
1030               e_key0.protocol = proto0;
1031               e_key0.fib_index = rx_fib_index0;
1032
1033               if (snat_static_mapping_match (sm, e_key0, &l_key0, 1, 0,
1034                                              &twice_nat0, &lb_nat0,
1035                                              &ip0->src_address,
1036                                              &identity_nat0))
1037                 {
1038                   /*
1039                    * Send DHCP packets to the ipv4 stack, or we won't
1040                    * be able to use dhcp client on the outside interface
1041                    */
1042                   if (PREDICT_FALSE (proto0 == SNAT_PROTOCOL_UDP
1043                                      && (vnet_buffer (b0)->ip.
1044                                          reass.l4_dst_port ==
1045                                          clib_host_to_net_u16
1046                                          (UDP_DST_PORT_dhcp_to_client))))
1047                     {
1048                       goto trace0;
1049                     }
1050
1051                   if (!sm->forwarding_enabled)
1052                     {
1053                       b0->error =
1054                         node->errors[NAT_OUT2IN_ED_ERROR_NO_TRANSLATION];
1055                       next0 = NAT_NEXT_DROP;
1056                     }
1057                   else
1058                     {
1059                       if (next_src_nat
1060                           (sm, ip0, vnet_buffer (b0)->ip.reass.l4_src_port,
1061                            vnet_buffer (b0)->ip.reass.l4_dst_port,
1062                            thread_index, rx_fib_index0))
1063                         {
1064                           next0 = NAT_NEXT_IN2OUT_ED_FAST_PATH;
1065                           goto trace0;
1066                         }
1067                       if (sm->num_workers > 1)
1068                         create_bypass_for_fwd_worker (sm, b0, ip0,
1069                                                       rx_fib_index0);
1070                       else
1071                         create_bypass_for_fwd (sm, b0, ip0, rx_fib_index0,
1072                                                thread_index);
1073                     }
1074                   goto trace0;
1075                 }
1076
1077               if (PREDICT_FALSE (identity_nat0))
1078                 goto trace0;
1079
1080               if ((proto0 == SNAT_PROTOCOL_TCP)
1081                   && !tcp_flags_is_init (vnet_buffer (b0)->ip.
1082                                          reass.icmp_type_or_tcp_flags))
1083                 {
1084                   b0->error = node->errors[NAT_OUT2IN_ED_ERROR_NON_SYN];
1085                   next0 = NAT_NEXT_DROP;
1086                   goto trace0;
1087                 }
1088
1089               /* Create session initiated by host from external network */
1090               s0 = create_session_for_static_mapping_ed (sm, b0, l_key0,
1091                                                          e_key0, node,
1092                                                          thread_index,
1093                                                          twice_nat0,
1094                                                          lb_nat0, now);
1095               if (!s0)
1096                 {
1097                   next0 = NAT_NEXT_DROP;
1098                   goto trace0;
1099                 }
1100             }
1101
1102           old_addr0 = ip0->dst_address.as_u32;
1103           new_addr0 = ip0->dst_address.as_u32 = s0->in2out.addr.as_u32;
1104           vnet_buffer (b0)->sw_if_index[VLIB_TX] = s0->in2out.fib_index;
1105
1106           sum0 = ip0->checksum;
1107           sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1108                                  dst_address);
1109           if (PREDICT_FALSE (is_twice_nat_session (s0)))
1110             sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1111                                    s0->ext_host_nat_addr.as_u32, ip4_header_t,
1112                                    src_address);
1113           ip0->checksum = ip_csum_fold (sum0);
1114
1115           old_port0 = vnet_buffer (b0)->ip.reass.l4_dst_port;
1116
1117           if (PREDICT_TRUE (proto0 == SNAT_PROTOCOL_TCP))
1118             {
1119               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1120                 {
1121                   new_port0 = udp0->dst_port = s0->in2out.port;
1122                   sum0 = tcp0->checksum;
1123                   sum0 =
1124                     ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1125                                     dst_address);
1126                   sum0 =
1127                     ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1128                                     length);
1129                   if (is_twice_nat_session (s0))
1130                     {
1131                       sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1132                                              s0->ext_host_nat_addr.as_u32,
1133                                              ip4_header_t, dst_address);
1134                       sum0 =
1135                         ip_csum_update (sum0,
1136                                         vnet_buffer (b0)->ip.
1137                                         reass.l4_src_port,
1138                                         s0->ext_host_nat_port, ip4_header_t,
1139                                         length);
1140                       tcp0->src_port = s0->ext_host_nat_port;
1141                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1142                     }
1143                   tcp0->checksum = ip_csum_fold (sum0);
1144                 }
1145               tcp_packets++;
1146               if (nat44_set_tcp_session_state_o2i
1147                   (sm, now, s0,
1148                    vnet_buffer (b0)->ip.reass.icmp_type_or_tcp_flags,
1149                    vnet_buffer (b0)->ip.reass.tcp_ack_number,
1150                    vnet_buffer (b0)->ip.reass.tcp_seq_number, thread_index))
1151                 goto trace0;
1152             }
1153           else if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment
1154                    && udp0->checksum)
1155             {
1156               new_port0 = udp0->dst_port = s0->in2out.port;
1157               sum0 = udp0->checksum;
1158               sum0 = ip_csum_update (sum0, old_addr0, new_addr0, ip4_header_t,
1159                                      dst_address);
1160               sum0 = ip_csum_update (sum0, old_port0, new_port0, ip4_header_t,
1161                                      length);
1162               if (PREDICT_FALSE (is_twice_nat_session (s0)))
1163                 {
1164                   sum0 = ip_csum_update (sum0, ip0->src_address.as_u32,
1165                                          s0->ext_host_nat_addr.as_u32,
1166                                          ip4_header_t, dst_address);
1167                   sum0 =
1168                     ip_csum_update (sum0,
1169                                     vnet_buffer (b0)->ip.reass.l4_src_port,
1170                                     s0->ext_host_nat_port, ip4_header_t,
1171                                     length);
1172                   udp0->src_port = s0->ext_host_nat_port;
1173                   ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1174                 }
1175               udp0->checksum = ip_csum_fold (sum0);
1176               udp_packets++;
1177             }
1178           else
1179             {
1180               if (!vnet_buffer (b0)->ip.reass.is_non_first_fragment)
1181                 {
1182                   new_port0 = udp0->dst_port = s0->in2out.port;
1183                   if (PREDICT_FALSE (is_twice_nat_session (s0)))
1184                     {
1185                       udp0->src_port = s0->ext_host_nat_port;
1186                       ip0->src_address.as_u32 = s0->ext_host_nat_addr.as_u32;
1187                     }
1188                 }
1189               udp_packets++;
1190             }
1191
1192           /* Accounting */
1193           nat44_session_update_counters (s0, now,
1194                                          vlib_buffer_length_in_chain (vm, b0),
1195                                          thread_index);
1196           /* Per-user LRU list maintenance */
1197           nat44_session_update_lru (sm, s0, thread_index);
1198
1199         trace0:
1200           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1201                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1202             {
1203               nat44_ed_out2in_trace_t *t =
1204                 vlib_add_trace (vm, node, b0, sizeof (*t));
1205               t->sw_if_index = sw_if_index0;
1206               t->next_index = next0;
1207               t->is_slow_path = 1;
1208
1209               if (s0)
1210                 t->session_index = s0 - tsm->sessions;
1211               else
1212                 t->session_index = ~0;
1213             }
1214
1215           pkts_processed += next0 == vnet_buffer2 (b0)->nat.arc_next;
1216           /* verify speculative enqueue, maybe switch current next frame */
1217           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1218                                            to_next, n_left_to_next,
1219                                            bi0, next0);
1220         }
1221
1222       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1223     }
1224
1225   vlib_node_increment_counter (vm, stats_node_index,
1226                                NAT_OUT2IN_ED_ERROR_OUT2IN_PACKETS,
1227                                pkts_processed);
1228   vlib_node_increment_counter (vm, stats_node_index,
1229                                NAT_OUT2IN_ED_ERROR_TCP_PACKETS, tcp_packets);
1230   vlib_node_increment_counter (vm, stats_node_index,
1231                                NAT_OUT2IN_ED_ERROR_UDP_PACKETS, udp_packets);
1232   vlib_node_increment_counter (vm, stats_node_index,
1233                                NAT_OUT2IN_ED_ERROR_ICMP_PACKETS,
1234                                icmp_packets);
1235   vlib_node_increment_counter (vm, stats_node_index,
1236                                NAT_OUT2IN_ED_ERROR_OTHER_PACKETS,
1237                                other_packets);
1238   vlib_node_increment_counter (vm, stats_node_index,
1239                                NAT_OUT2IN_ED_ERROR_FRAGMENTS, fragments);
1240   return frame->n_vectors;
1241 }
1242
1243 VLIB_NODE_FN (nat44_ed_out2in_node) (vlib_main_t * vm,
1244                                      vlib_node_runtime_t * node,
1245                                      vlib_frame_t * frame)
1246 {
1247   return nat44_ed_out2in_fast_path_node_fn_inline (vm, node, frame);
1248 }
1249
1250 /* *INDENT-OFF* */
1251 VLIB_REGISTER_NODE (nat44_ed_out2in_node) = {
1252   .name = "nat44-ed-out2in",
1253   .vector_size = sizeof (u32),
1254   .sibling_of = "nat-default",
1255   .format_trace = format_nat44_ed_out2in_trace,
1256   .type = VLIB_NODE_TYPE_INTERNAL,
1257   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1258   .error_strings = nat_out2in_ed_error_strings,
1259   .runtime_data_bytes = sizeof (snat_runtime_t),
1260 };
1261 /* *INDENT-ON* */
1262
1263 VLIB_NODE_FN (nat44_ed_out2in_slowpath_node) (vlib_main_t * vm,
1264                                               vlib_node_runtime_t * node,
1265                                               vlib_frame_t * frame)
1266 {
1267   return nat44_ed_out2in_slow_path_node_fn_inline (vm, node, frame);
1268 }
1269
1270 /* *INDENT-OFF* */
1271 VLIB_REGISTER_NODE (nat44_ed_out2in_slowpath_node) = {
1272   .name = "nat44-ed-out2in-slowpath",
1273   .vector_size = sizeof (u32),
1274   .sibling_of = "nat-default",
1275   .format_trace = format_nat44_ed_out2in_trace,
1276   .type = VLIB_NODE_TYPE_INTERNAL,
1277   .n_errors = ARRAY_LEN(nat_out2in_ed_error_strings),
1278   .error_strings = nat_out2in_ed_error_strings,
1279   .runtime_data_bytes = sizeof (snat_runtime_t),
1280 };
1281 /* *INDENT-ON* */
1282
1283 static u8 *
1284 format_nat_pre_trace (u8 * s, va_list * args)
1285 {
1286   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1287   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1288   nat_pre_trace_t *t = va_arg (*args, nat_pre_trace_t *);
1289   return format (s, "out2in next_index %d", t->next_index);
1290 }
1291
1292 VLIB_NODE_FN (nat_pre_out2in_node) (vlib_main_t * vm,
1293                                     vlib_node_runtime_t * node,
1294                                     vlib_frame_t * frame)
1295 {
1296   return nat_pre_node_fn_inline (vm, node, frame,
1297                                  NAT_NEXT_OUT2IN_ED_FAST_PATH);
1298 }
1299
1300 /* *INDENT-OFF* */
1301 VLIB_REGISTER_NODE (nat_pre_out2in_node) = {
1302   .name = "nat-pre-out2in",
1303   .vector_size = sizeof (u32),
1304   .sibling_of = "nat-default",
1305   .format_trace = format_nat_pre_trace,
1306   .type = VLIB_NODE_TYPE_INTERNAL,
1307   .n_errors = 0,
1308  };
1309 /* *INDENT-ON* */
1310
1311 /*
1312  * fd.io coding-style-patch-verification: ON
1313  *
1314  * Local Variables:
1315  * eval: (c-set-style "gnu")
1316  * End:
1317  */