nat: Final NAT44 EI/ED split patch
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei_ha.c
1 /*
2  * Copyright (c) 2019 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 //#include <vnet/fib/fib_source.h>
17 #include <vnet/fib/fib_table.h>
18 #include <vnet/udp/udp_local.h>
19 #include <vppinfra/atomics.h>
20
21 #include <nat/lib/log.h>
22
23 #include <nat/nat44-ei/nat44_ei.h>
24 #include <nat/nat44-ei/nat44_ei_ha.h>
25 #include <nat/nat44-ei/nat44_ei_inlines.h>
26
27 /* number of retries */
28 #define NAT_HA_RETRIES 3
29
30 #define foreach_nat_ha_counter           \
31 _(RECV_ADD, "add-event-recv", 0)         \
32 _(RECV_DEL, "del-event-recv", 1)         \
33 _(RECV_REFRESH, "refresh-event-recv", 2) \
34 _(SEND_ADD, "add-event-send", 3)         \
35 _(SEND_DEL, "del-event-send", 4)         \
36 _(SEND_REFRESH, "refresh-event-send", 5) \
37 _(RECV_ACK, "ack-recv", 6)               \
38 _(SEND_ACK, "ack-send", 7)               \
39 _(RETRY_COUNT, "retry-count", 8)         \
40 _(MISSED_COUNT, "missed-count", 9)
41
42 /* NAT HA protocol version */
43 #define NAT_HA_VERSION 0x01
44
45 /* NAT HA protocol flags */
46 #define NAT_HA_FLAG_ACK 0x01
47
48 /* NAT HA event types */
49 typedef enum
50 {
51   NAT_HA_ADD = 1,
52   NAT_HA_DEL,
53   NAT_HA_REFRESH,
54 } nat_ha_event_type_t;
55
56 /* NAT HA protocol header */
57 typedef struct
58 {
59   /* version */
60   u8 version;
61   /* flags */
62   u8 flags;
63   /* event count */
64   u16 count;
65   /* sequence number */
66   u32 sequence_number;
67   /* thread index where events originated */
68   u32 thread_index;
69 } __attribute__ ((packed)) nat_ha_message_header_t;
70
71 /* NAT HA protocol event data */
72 typedef struct
73 {
74   /* event type */
75   u8 event_type;
76   /* session data */
77   u8 protocol;
78   u16 flags;
79   u32 in_addr;
80   u32 out_addr;
81   u16 in_port;
82   u16 out_port;
83   u32 eh_addr;
84   u32 ehn_addr;
85   u16 eh_port;
86   u16 ehn_port;
87   u32 fib_index;
88   u32 total_pkts;
89   u64 total_bytes;
90 } __attribute__ ((packed)) nat_ha_event_t;
91
92 typedef enum
93 {
94 #define _(N, s, v) NAT_HA_COUNTER_##N = v,
95   foreach_nat_ha_counter
96 #undef _
97   NAT_HA_N_COUNTERS
98 } nat_ha_counter_t;
99
100 /* data waiting for ACK */
101 typedef struct
102 {
103   /* sequence number */
104   u32 seq;
105   /* retry count */
106   u32 retry_count;
107   /* next retry time */
108   f64 retry_timer;
109   /* 1 if HA resync */
110   u8 is_resync;
111   /* packet data */
112   u8 *data;
113 } nat_ha_resend_entry_t;
114
115 /* per thread data */
116 typedef struct
117 {
118   /* buffer under construction */
119   vlib_buffer_t *state_sync_buffer;
120   /* frame containing NAT HA buffers */
121   vlib_frame_t *state_sync_frame;
122   /* number of events */
123   u16 state_sync_count;
124   /* next event offset */
125   u32 state_sync_next_event_offset;
126   /* data waiting for ACK */
127   nat_ha_resend_entry_t *resend_queue;
128 } nat_ha_per_thread_data_t;
129
130 /* NAT HA settings */
131 typedef struct nat_ha_main_s
132 {
133   u8 enabled;
134   /* local IP address and UDP port */
135   ip4_address_t src_ip_address;
136   u16 src_port;
137   /* failvoer IP address and UDP port */
138   ip4_address_t dst_ip_address;
139   u16 dst_port;
140   /* path MTU between local and failover */
141   u32 state_sync_path_mtu;
142   /* number of seconds after which to send session counters refresh */
143   u32 session_refresh_interval;
144   /* counters */
145   vlib_simple_counter_main_t counters[NAT_HA_N_COUNTERS];
146   vlib_main_t *vlib_main;
147   /* sequence number counter */
148   u32 sequence_number;
149   /* 1 if resync in progress */
150   u8 in_resync;
151   /* number of remaing ACK for resync */
152   u32 resync_ack_count;
153   /* number of missed ACK for resync */
154   u32 resync_ack_missed;
155   /* resync data */
156   nat_ha_resync_event_cb_t event_callback;
157   u32 client_index;
158   u32 pid;
159   /* per thread data */
160   u32 num_workers;
161   nat_ha_per_thread_data_t *per_thread_data;
162
163   u32 ha_handoff_node_index;
164   u32 ha_process_node_index;
165   u32 ha_worker_node_index;
166   u32 ha_node_index;
167
168   /* worker handoff frame-queue index */
169   u32 fq_index;
170 } nat_ha_main_t;
171
172 nat_ha_main_t nat_ha_main;
173
174 static_always_inline void
175 nat44_ei_ha_sadd (ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr,
176                   u16 out_port, ip4_address_t *eh_addr, u16 eh_port,
177                   ip4_address_t *ehn_addr, u16 ehn_port, u8 proto,
178                   u32 fib_index, u16 flags, u32 thread_index)
179 {
180   nat44_ei_main_t *nm = &nat44_ei_main;
181   nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
182   nat44_ei_user_t *u;
183   nat44_ei_session_t *s;
184   clib_bihash_kv_8_8_t kv;
185   vlib_main_t *vm = vlib_get_main ();
186   f64 now = vlib_time_now (vm);
187   nat44_ei_outside_fib_t *outside_fib;
188   fib_node_index_t fei = FIB_NODE_INDEX_INVALID;
189   fib_prefix_t pfx = {
190     .fp_proto = FIB_PROTOCOL_IP4,
191     .fp_len = 32,
192     .fp_addr = {
193                 .ip4.as_u32 = eh_addr->as_u32,
194                 },
195   };
196
197   if (!(flags & NAT44_EI_SESSION_FLAG_STATIC_MAPPING))
198     {
199       if (nat44_ei_set_outside_address_and_port (nm->addresses, thread_index,
200                                                  *out_addr, out_port, proto))
201         return;
202     }
203
204   u = nat44_ei_user_get_or_create (nm, in_addr, fib_index, thread_index);
205   if (!u)
206     return;
207
208   s = nat44_ei_session_alloc_or_recycle (nm, u, thread_index, now);
209   if (!s)
210     return;
211
212   s->out2in.addr.as_u32 = out_addr->as_u32;
213   s->out2in.port = out_port;
214   s->nat_proto = proto;
215   s->last_heard = now;
216   s->flags = flags;
217   s->ext_host_addr.as_u32 = eh_addr->as_u32;
218   s->ext_host_port = eh_port;
219   nat44_ei_user_session_increment (nm, u, nat44_ei_is_session_static (s));
220   switch (vec_len (nm->outside_fibs))
221     {
222     case 0:
223       s->out2in.fib_index = nm->outside_fib_index;
224       break;
225     case 1:
226       s->out2in.fib_index = nm->outside_fibs[0].fib_index;
227       break;
228     default:
229       vec_foreach (outside_fib, nm->outside_fibs)
230         {
231           fei = fib_table_lookup (outside_fib->fib_index, &pfx);
232           if (FIB_NODE_INDEX_INVALID != fei)
233             {
234               if (fib_entry_get_resolving_interface (fei) != ~0)
235                 {
236                   s->out2in.fib_index = outside_fib->fib_index;
237                   break;
238                 }
239             }
240         }
241       break;
242     }
243   init_nat_o2i_kv (&kv, s, thread_index, s - tnm->sessions);
244   if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 1))
245     nat_elog_warn (nm, "out2in key add failed");
246
247   s->in2out.addr.as_u32 = in_addr->as_u32;
248   s->in2out.port = in_port;
249   s->in2out.fib_index = fib_index;
250   init_nat_i2o_kv (&kv, s, thread_index, s - tnm->sessions);
251   if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 1))
252     nat_elog_warn (nm, "in2out key add failed");
253 }
254
255 static_always_inline void
256 nat44_ei_ha_sdel (ip4_address_t *out_addr, u16 out_port,
257                   ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
258                   u32 ti)
259 {
260   nat44_ei_main_t *nm = &nat44_ei_main;
261   clib_bihash_kv_8_8_t kv, value;
262   u32 thread_index;
263   nat44_ei_session_t *s;
264   nat44_ei_main_per_thread_data_t *tnm;
265
266   if (nm->num_workers > 1)
267     thread_index = nm->first_worker_index +
268                    (nm->workers[(clib_net_to_host_u16 (out_port) - 1024) /
269                                 nm->port_per_thread]);
270   else
271     thread_index = nm->num_workers;
272   tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
273
274   init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
275   if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
276     return;
277
278   s = pool_elt_at_index (tnm->sessions, nat_value_get_session_index (&value));
279   nat44_ei_free_session_data_v2 (nm, s, thread_index, 1);
280   nat44_ei_delete_session (nm, s, thread_index);
281 }
282
283 static_always_inline void
284 nat44_ei_ha_sref (ip4_address_t *out_addr, u16 out_port,
285                   ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
286                   u32 total_pkts, u64 total_bytes, u32 thread_index)
287 {
288   nat44_ei_main_t *nm = &nat44_ei_main;
289   clib_bihash_kv_8_8_t kv, value;
290   nat44_ei_session_t *s;
291   nat44_ei_main_per_thread_data_t *tnm;
292
293   tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
294
295   init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
296   if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
297     return;
298
299   s = pool_elt_at_index (tnm->sessions, nat_value_get_session_index (&value));
300   s->total_pkts = total_pkts;
301   s->total_bytes = total_bytes;
302 }
303
304 static void
305 nat_ha_resync_fin (void)
306 {
307   nat44_ei_main_t *nm = &nat44_ei_main;
308   nat_ha_main_t *ha = &nat_ha_main;
309
310   /* if no more resync ACK remainig we are done */
311   if (ha->resync_ack_count)
312     return;
313
314   ha->in_resync = 0;
315   if (ha->resync_ack_missed)
316     {
317       nat_elog_info (nm, "resync completed with result FAILED");
318     }
319   else
320     {
321       nat_elog_info (nm, "resync completed with result SUCCESS");
322     }
323   if (ha->event_callback)
324     ha->event_callback (ha->client_index, ha->pid, ha->resync_ack_missed);
325 }
326
327 /* cache HA NAT data waiting for ACK */
328 static int
329 nat_ha_resend_queue_add (u32 seq, u8 * data, u8 data_len, u8 is_resync,
330                          u32 thread_index)
331 {
332   nat_ha_main_t *ha = &nat_ha_main;
333   nat_ha_per_thread_data_t *td = &ha->per_thread_data[thread_index];
334   nat_ha_resend_entry_t *entry;
335   f64 now = vlib_time_now (ha->vlib_main);
336
337   vec_add2 (td->resend_queue, entry, 1);
338   clib_memset (entry, 0, sizeof (*entry));
339   entry->retry_timer = now + 2.0;
340   entry->seq = seq;
341   entry->is_resync = is_resync;
342   vec_add (entry->data, data, data_len);
343
344   return 0;
345 }
346
347 static_always_inline void
348 nat_ha_ack_recv (u32 seq, u32 thread_index)
349 {
350   nat44_ei_main_t *nm = &nat44_ei_main;
351   nat_ha_main_t *ha = &nat_ha_main;
352   nat_ha_per_thread_data_t *td = &ha->per_thread_data[thread_index];
353   u32 i;
354
355   vec_foreach_index (i, td->resend_queue)
356   {
357     if (td->resend_queue[i].seq != seq)
358       continue;
359
360     vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ACK],
361                                    thread_index, 0, 1);
362     /* ACK received remove cached data */
363     if (td->resend_queue[i].is_resync)
364       {
365         clib_atomic_fetch_sub (&ha->resync_ack_count, 1);
366         nat_ha_resync_fin ();
367       }
368     vec_free (td->resend_queue[i].data);
369     vec_del1 (td->resend_queue, i);
370     nat_elog_debug_X1 (nm, "ACK for seq %d received", "i4",
371                        clib_net_to_host_u32 (seq));
372
373     return;
374   }
375 }
376
377 /* scan non-ACKed HA NAT for retry */
378 static void
379 nat_ha_resend_scan (f64 now, u32 thread_index)
380 {
381   nat44_ei_main_t *nm = &nat44_ei_main;
382   nat_ha_main_t *ha = &nat_ha_main;
383   nat_ha_per_thread_data_t *td = &ha->per_thread_data[thread_index];
384   u32 i, *del, *to_delete = 0;
385   vlib_main_t *vm = ha->vlib_main;
386   vlib_buffer_t *b = 0;
387   vlib_frame_t *f;
388   u32 bi, *to_next;
389   ip4_header_t *ip;
390
391   vec_foreach_index (i, td->resend_queue)
392   {
393     if (td->resend_queue[i].retry_timer > now)
394       continue;
395
396     /* maximum retry reached delete cached data */
397     if (td->resend_queue[i].retry_count >= NAT_HA_RETRIES)
398       {
399         nat_elog_notice_X1 (nm, "seq %d missed", "i4",
400                             clib_net_to_host_u32 (td->resend_queue[i].seq));
401         if (td->resend_queue[i].is_resync)
402           {
403             clib_atomic_fetch_add (&ha->resync_ack_missed, 1);
404             clib_atomic_fetch_sub (&ha->resync_ack_count, 1);
405             nat_ha_resync_fin ();
406           }
407         vec_add1 (to_delete, i);
408         vlib_increment_simple_counter (&ha->counters
409                                        [NAT_HA_COUNTER_MISSED_COUNT],
410                                        thread_index, 0, 1);
411         continue;
412       }
413
414     /* retry to send non-ACKed data */
415     nat_elog_debug_X1 (nm, "state sync seq %d resend", "i4",
416                        clib_net_to_host_u32 (td->resend_queue[i].seq));
417     td->resend_queue[i].retry_count++;
418     vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RETRY_COUNT],
419                                    thread_index, 0, 1);
420     if (vlib_buffer_alloc (vm, &bi, 1) != 1)
421       {
422         nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
423         return;
424       }
425     b = vlib_get_buffer (vm, bi);
426     b->current_length = vec_len (td->resend_queue[i].data);
427     b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
428     b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
429     vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
430     vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
431     ip = vlib_buffer_get_current (b);
432     clib_memcpy (ip, td->resend_queue[i].data,
433                  vec_len (td->resend_queue[i].data));
434     f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
435     to_next = vlib_frame_vector_args (f);
436     to_next[0] = bi;
437     f->n_vectors = 1;
438     vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
439     td->resend_queue[i].retry_timer = now + 2.0;
440   }
441
442   vec_foreach (del, to_delete)
443   {
444     vec_free (td->resend_queue[*del].data);
445     vec_del1 (td->resend_queue, *del);
446   }
447   vec_free (to_delete);
448 }
449
450 void
451 nat_ha_enable ()
452 {
453   nat_ha_main_t *ha = &nat_ha_main;
454   ha->enabled = 1;
455 }
456
457 void
458 nat_ha_disable ()
459 {
460   nat_ha_main_t *ha = &nat_ha_main;
461   ha->dst_port = 0;
462   ha->enabled = 0;
463 }
464
465 void
466 nat_ha_set_node_indexes (nat_ha_main_t *ha, vlib_main_t *vm)
467 {
468   vlib_node_t *node;
469
470   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-handoff");
471   ha->ha_handoff_node_index = node->index;
472   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-process");
473   ha->ha_process_node_index = node->index;
474   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-worker");
475   ha->ha_worker_node_index = node->index;
476   node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha");
477   ha->ha_node_index = node->index;
478 }
479
480 void
481 nat_ha_init (vlib_main_t * vm, u32 num_workers, u32 num_threads)
482 {
483   nat_ha_main_t *ha = &nat_ha_main;
484   clib_memset (ha, 0, sizeof (*ha));
485
486   nat_ha_set_node_indexes (ha, vm);
487
488   ha->vlib_main = vm;
489   ha->fq_index = ~0;
490
491   ha->num_workers = num_workers;
492   vec_validate (ha->per_thread_data, num_threads);
493
494 #define _(N, s, v)                                                            \
495   ha->counters[v].name = s;                                                   \
496   ha->counters[v].stat_segment_name = "/nat44-ei/ha/" s;                      \
497   vlib_validate_simple_counter (&ha->counters[v], 0);                         \
498   vlib_zero_simple_counter (&ha->counters[v], 0);
499   foreach_nat_ha_counter
500 #undef _
501 }
502
503 int
504 nat_ha_set_listener (ip4_address_t * addr, u16 port, u32 path_mtu)
505 {
506   nat44_ei_main_t *nm = &nat44_ei_main;
507   nat_ha_main_t *ha = &nat_ha_main;
508
509   /* unregister previously set UDP port */
510   if (ha->src_port)
511     udp_unregister_dst_port (ha->vlib_main, ha->src_port, 1);
512
513   ha->src_ip_address.as_u32 = addr->as_u32;
514   ha->src_port = port;
515   ha->state_sync_path_mtu = path_mtu;
516
517   if (port)
518     {
519       /* if multiple worker threads first go to handoff node */
520       if (ha->num_workers > 1)
521         {
522           if (ha->fq_index == ~0)
523             ha->fq_index = vlib_frame_queue_main_init (ha->ha_node_index, 0);
524           udp_register_dst_port (ha->vlib_main, port,
525                                  ha->ha_handoff_node_index, 1);
526         }
527       else
528         {
529           udp_register_dst_port (ha->vlib_main, port, ha->ha_node_index, 1);
530         }
531       nat_elog_info_X1 (nm, "HA listening on port %d for state sync", "i4",
532                         port);
533     }
534
535   return 0;
536 }
537
538 void
539 nat_ha_get_listener (ip4_address_t * addr, u16 * port, u32 * path_mtu)
540 {
541   nat_ha_main_t *ha = &nat_ha_main;
542
543   addr->as_u32 = ha->src_ip_address.as_u32;
544   *port = ha->src_port;
545   *path_mtu = ha->state_sync_path_mtu;
546 }
547
548 int
549 nat_ha_set_failover (ip4_address_t * addr, u16 port,
550                      u32 session_refresh_interval)
551 {
552   nat_ha_main_t *ha = &nat_ha_main;
553
554   ha->dst_ip_address.as_u32 = addr->as_u32;
555   ha->dst_port = port;
556   ha->session_refresh_interval = session_refresh_interval;
557
558   vlib_process_signal_event (ha->vlib_main, ha->ha_process_node_index, 1, 0);
559
560   return 0;
561 }
562
563 void
564 nat_ha_get_failover (ip4_address_t * addr, u16 * port,
565                      u32 * session_refresh_interval)
566 {
567   nat_ha_main_t *ha = &nat_ha_main;
568
569   addr->as_u32 = ha->dst_ip_address.as_u32;
570   *port = ha->dst_port;
571   *session_refresh_interval = ha->session_refresh_interval;
572 }
573
574 static_always_inline void
575 nat_ha_recv_add (nat_ha_event_t * event, f64 now, u32 thread_index)
576 {
577   nat_ha_main_t *ha = &nat_ha_main;
578   ip4_address_t in_addr, out_addr, eh_addr, ehn_addr;
579   u32 fib_index;
580   u16 flags;
581
582   vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ADD],
583                                  thread_index, 0, 1);
584
585   in_addr.as_u32 = event->in_addr;
586   out_addr.as_u32 = event->out_addr;
587   eh_addr.as_u32 = event->eh_addr;
588   ehn_addr.as_u32 = event->ehn_addr;
589   fib_index = clib_net_to_host_u32 (event->fib_index);
590   flags = clib_net_to_host_u16 (event->flags);
591
592   nat44_ei_ha_sadd (&in_addr, event->in_port, &out_addr, event->out_port,
593                     &eh_addr, event->eh_port, &ehn_addr, event->ehn_port,
594                     event->protocol, fib_index, flags, thread_index);
595 }
596
597 static_always_inline void
598 nat_ha_recv_del (nat_ha_event_t * event, u32 thread_index)
599 {
600   nat_ha_main_t *ha = &nat_ha_main;
601   ip4_address_t out_addr, eh_addr;
602   u32 fib_index;
603
604   vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_DEL],
605                                  thread_index, 0, 1);
606
607   out_addr.as_u32 = event->out_addr;
608   eh_addr.as_u32 = event->eh_addr;
609   fib_index = clib_net_to_host_u32 (event->fib_index);
610
611   nat44_ei_ha_sdel (&out_addr, event->out_port, &eh_addr, event->eh_port,
612                     event->protocol, fib_index, thread_index);
613 }
614
615 static_always_inline void
616 nat_ha_recv_refresh (nat_ha_event_t * event, f64 now, u32 thread_index)
617 {
618   nat_ha_main_t *ha = &nat_ha_main;
619   ip4_address_t out_addr, eh_addr;
620   u32 fib_index, total_pkts;
621   u64 total_bytes;
622
623   vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_REFRESH],
624                                  thread_index, 0, 1);
625
626   out_addr.as_u32 = event->out_addr;
627   eh_addr.as_u32 = event->eh_addr;
628   fib_index = clib_net_to_host_u32 (event->fib_index);
629   total_pkts = clib_net_to_host_u32 (event->total_pkts);
630   total_bytes = clib_net_to_host_u64 (event->total_bytes);
631
632   nat44_ei_ha_sref (&out_addr, event->out_port, &eh_addr, event->eh_port,
633                     event->protocol, fib_index, total_pkts, total_bytes,
634                     thread_index);
635 }
636
637 /* process received NAT HA event */
638 static_always_inline void
639 nat_ha_event_process (nat_ha_event_t * event, f64 now, u32 thread_index)
640 {
641   nat44_ei_main_t *nm = &nat44_ei_main;
642   switch (event->event_type)
643     {
644     case NAT_HA_ADD:
645       nat_ha_recv_add (event, now, thread_index);
646       break;
647     case NAT_HA_DEL:
648       nat_ha_recv_del (event, thread_index);
649       break;
650     case NAT_HA_REFRESH:
651       nat_ha_recv_refresh (event, now, thread_index);
652       break;
653     default:
654       nat_elog_notice_X1 (nm, "Unsupported HA event type %d", "i4",
655                           event->event_type);
656       break;
657     }
658 }
659
660 static inline void
661 nat_ha_header_create (vlib_buffer_t * b, u32 * offset, u32 thread_index)
662 {
663   nat_ha_main_t *ha = &nat_ha_main;
664   nat_ha_message_header_t *h;
665   ip4_header_t *ip;
666   udp_header_t *udp;
667   u32 sequence_number;
668
669   b->current_data = 0;
670   b->current_length = sizeof (*ip) + sizeof (*udp) + sizeof (*h);
671   b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
672   b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
673   vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
674   vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
675   ip = vlib_buffer_get_current (b);
676   udp = (udp_header_t *) (ip + 1);
677   h = (nat_ha_message_header_t *) (udp + 1);
678
679   /* IP header */
680   ip->ip_version_and_header_length = 0x45;
681   ip->ttl = 254;
682   ip->protocol = IP_PROTOCOL_UDP;
683   ip->flags_and_fragment_offset =
684     clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
685   ip->src_address.as_u32 = ha->src_ip_address.as_u32;
686   ip->dst_address.as_u32 = ha->dst_ip_address.as_u32;
687   /* UDP header */
688   udp->src_port = clib_host_to_net_u16 (ha->src_port);
689   udp->dst_port = clib_host_to_net_u16 (ha->dst_port);
690   udp->checksum = 0;
691
692   /* NAT HA protocol header */
693   h->version = NAT_HA_VERSION;
694   h->flags = 0;
695   h->count = 0;
696   h->thread_index = clib_host_to_net_u32 (thread_index);
697   sequence_number = clib_atomic_fetch_add (&ha->sequence_number, 1);
698   h->sequence_number = clib_host_to_net_u32 (sequence_number);
699
700   *offset =
701     sizeof (ip4_header_t) + sizeof (udp_header_t) +
702     sizeof (nat_ha_message_header_t);
703 }
704
705 static inline void
706 nat_ha_send (vlib_frame_t * f, vlib_buffer_t * b, u8 is_resync,
707              u32 thread_index)
708 {
709   nat_ha_main_t *ha = &nat_ha_main;
710   nat_ha_per_thread_data_t *td = &ha->per_thread_data[thread_index];
711   nat_ha_message_header_t *h;
712   ip4_header_t *ip;
713   udp_header_t *udp;
714   vlib_main_t *vm = vlib_mains[thread_index];
715
716   ip = vlib_buffer_get_current (b);
717   udp = ip4_next_header (ip);
718   h = (nat_ha_message_header_t *) (udp + 1);
719
720   h->count = clib_host_to_net_u16 (td->state_sync_count);
721
722   ip->length = clib_host_to_net_u16 (b->current_length);
723   ip->checksum = ip4_header_checksum (ip);
724   udp->length = clib_host_to_net_u16 (b->current_length - sizeof (*ip));
725
726   nat_ha_resend_queue_add (h->sequence_number, (u8 *) ip, b->current_length,
727                            is_resync, thread_index);
728
729   vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
730 }
731
732 /* add NAT HA protocol event */
733 static_always_inline void
734 nat_ha_event_add (nat_ha_event_t * event, u8 do_flush, u32 thread_index,
735                   u8 is_resync)
736 {
737   nat44_ei_main_t *nm = &nat44_ei_main;
738   nat_ha_main_t *ha = &nat_ha_main;
739   nat_ha_per_thread_data_t *td = &ha->per_thread_data[thread_index];
740   vlib_main_t *vm = vlib_mains[thread_index];
741   vlib_buffer_t *b = 0;
742   vlib_frame_t *f;
743   u32 bi = ~0, offset;
744
745   b = td->state_sync_buffer;
746
747   if (PREDICT_FALSE (b == 0))
748     {
749       if (do_flush)
750         return;
751
752       if (vlib_buffer_alloc (vm, &bi, 1) != 1)
753         {
754           nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
755           return;
756         }
757
758       b = td->state_sync_buffer = vlib_get_buffer (vm, bi);
759       clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
760       VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
761       offset = 0;
762     }
763   else
764     {
765       bi = vlib_get_buffer_index (vm, b);
766       offset = td->state_sync_next_event_offset;
767     }
768
769   f = td->state_sync_frame;
770   if (PREDICT_FALSE (f == 0))
771     {
772       u32 *to_next;
773       f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
774       td->state_sync_frame = f;
775       to_next = vlib_frame_vector_args (f);
776       to_next[0] = bi;
777       f->n_vectors = 1;
778     }
779
780   if (PREDICT_FALSE (td->state_sync_count == 0))
781     nat_ha_header_create (b, &offset, thread_index);
782
783   if (PREDICT_TRUE (do_flush == 0))
784     {
785       clib_memcpy_fast (b->data + offset, event, sizeof (*event));
786       offset += sizeof (*event);
787       td->state_sync_count++;
788       b->current_length += sizeof (*event);
789
790       switch (event->event_type)
791         {
792         case NAT_HA_ADD:
793           vlib_increment_simple_counter (&ha->counters
794                                          [NAT_HA_COUNTER_SEND_ADD],
795                                          thread_index, 0, 1);
796           break;
797         case NAT_HA_DEL:
798           vlib_increment_simple_counter (&ha->counters
799                                          [NAT_HA_COUNTER_SEND_DEL],
800                                          thread_index, 0, 1);
801           break;
802         case NAT_HA_REFRESH:
803           vlib_increment_simple_counter (&ha->counters
804                                          [NAT_HA_COUNTER_SEND_REFRESH],
805                                          thread_index, 0, 1);
806           break;
807         default:
808           break;
809         }
810     }
811
812   if (PREDICT_FALSE
813       (do_flush || offset + (sizeof (*event)) > ha->state_sync_path_mtu))
814     {
815       nat_ha_send (f, b, is_resync, thread_index);
816       td->state_sync_buffer = 0;
817       td->state_sync_frame = 0;
818       td->state_sync_count = 0;
819       offset = 0;
820       if (is_resync)
821         {
822           clib_atomic_fetch_add (&ha->resync_ack_count, 1);
823           nat_ha_resync_fin ();
824         }
825     }
826
827   td->state_sync_next_event_offset = offset;
828 }
829
830 #define skip_if_disabled()          \
831 do {                                \
832   nat_ha_main_t *ha = &nat_ha_main; \
833   if (PREDICT_TRUE (!ha->dst_port)) \
834     return;                         \
835 } while (0)
836
837 void
838 nat_ha_flush (u8 is_resync)
839 {
840   skip_if_disabled ();
841   nat_ha_event_add (0, 1, 0, is_resync);
842 }
843
844 void
845 nat_ha_sadd (ip4_address_t * in_addr, u16 in_port, ip4_address_t * out_addr,
846              u16 out_port, ip4_address_t * eh_addr, u16 eh_port,
847              ip4_address_t * ehn_addr, u16 ehn_port, u8 proto, u32 fib_index,
848              u16 flags, u32 thread_index, u8 is_resync)
849 {
850   nat_ha_event_t event;
851
852   skip_if_disabled ();
853
854   clib_memset (&event, 0, sizeof (event));
855   event.event_type = NAT_HA_ADD;
856   event.flags = clib_host_to_net_u16 (flags);
857   event.in_addr = in_addr->as_u32;
858   event.in_port = in_port;
859   event.out_addr = out_addr->as_u32;
860   event.out_port = out_port;
861   event.eh_addr = eh_addr->as_u32;
862   event.eh_port = eh_port;
863   event.ehn_addr = ehn_addr->as_u32;
864   event.ehn_port = ehn_port;
865   event.fib_index = clib_host_to_net_u32 (fib_index);
866   event.protocol = proto;
867   nat_ha_event_add (&event, 0, thread_index, is_resync);
868 }
869
870 void
871 nat_ha_sdel (ip4_address_t * out_addr, u16 out_port, ip4_address_t * eh_addr,
872              u16 eh_port, u8 proto, u32 fib_index, u32 thread_index)
873 {
874   nat_ha_event_t event;
875
876   skip_if_disabled ();
877
878   clib_memset (&event, 0, sizeof (event));
879   event.event_type = NAT_HA_DEL;
880   event.out_addr = out_addr->as_u32;
881   event.out_port = out_port;
882   event.eh_addr = eh_addr->as_u32;
883   event.eh_port = eh_port;
884   event.fib_index = clib_host_to_net_u32 (fib_index);
885   event.protocol = proto;
886   nat_ha_event_add (&event, 0, thread_index, 0);
887 }
888
889 void
890 nat_ha_sref (ip4_address_t * out_addr, u16 out_port, ip4_address_t * eh_addr,
891              u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts,
892              u64 total_bytes, u32 thread_index, f64 * last_refreshed, f64 now)
893 {
894   nat_ha_main_t *ha = &nat_ha_main;
895   nat_ha_event_t event;
896
897   skip_if_disabled ();
898
899   if ((*last_refreshed + ha->session_refresh_interval) > now)
900     return;
901
902   *last_refreshed = now;
903   clib_memset (&event, 0, sizeof (event));
904   event.event_type = NAT_HA_REFRESH;
905   event.out_addr = out_addr->as_u32;
906   event.out_port = out_port;
907   event.eh_addr = eh_addr->as_u32;
908   event.eh_port = eh_port;
909   event.fib_index = clib_host_to_net_u32 (fib_index);
910   event.protocol = proto;
911   event.total_pkts = clib_host_to_net_u32 (total_pkts);
912   event.total_bytes = clib_host_to_net_u64 (total_bytes);
913   nat_ha_event_add (&event, 0, thread_index, 0);
914 }
915
916 static_always_inline u8
917 plugin_enabled ()
918 {
919   nat_ha_main_t *ha = &nat_ha_main;
920   return ha->enabled;
921 }
922
923 /* per thread process waiting for interrupt */
924 static uword
925 nat_ha_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
926                   vlib_frame_t * f)
927 {
928   u32 thread_index = vm->thread_index;
929
930   if (plugin_enabled () == 0)
931     return 0;
932
933   /* flush HA NAT data under construction */
934   nat_ha_event_add (0, 1, thread_index, 0);
935   /* scan if we need to resend some non-ACKed data */
936   nat_ha_resend_scan (vlib_time_now (vm), thread_index);
937   return 0;
938 }
939
940 /* *INDENT-OFF* */
941 VLIB_REGISTER_NODE (nat_ha_worker_node) = {
942   .function = nat_ha_worker_fn,
943   .type = VLIB_NODE_TYPE_INPUT,
944   .state = VLIB_NODE_STATE_INTERRUPT,
945   .name = "nat44-ei-ha-worker",
946 };
947 /* *INDENT-ON* */
948
949 /* periodically send interrupt to each thread */
950 static uword
951 nat_ha_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
952 {
953   nat44_ei_main_t *nm = &nat44_ei_main;
954   nat_ha_main_t *ha = &nat_ha_main;
955   uword event_type;
956   uword *event_data = 0;
957   u32 ti;
958
959   vlib_process_wait_for_event (vm);
960   event_type = vlib_process_get_events (vm, &event_data);
961   if (event_type)
962     nat_elog_info (nm, "nat44-ei-ha-process: bogus kickoff event received");
963   vec_reset_length (event_data);
964
965   while (1)
966     {
967       vlib_process_wait_for_event_or_clock (vm, 1.0);
968       event_type = vlib_process_get_events (vm, &event_data);
969       vec_reset_length (event_data);
970       for (ti = 0; ti < vec_len (vlib_mains); ti++)
971         {
972           if (ti >= vec_len (ha->per_thread_data))
973             continue;
974
975           vlib_node_set_interrupt_pending (vlib_mains[ti],
976                                            nat_ha_worker_node.index);
977         }
978     }
979
980   return 0;
981 }
982
983 /* *INDENT-OFF* */
984 VLIB_REGISTER_NODE (nat_ha_process_node) = {
985   .function = nat_ha_process,
986   .type = VLIB_NODE_TYPE_PROCESS,
987   .name = "nat44-ei-ha-process",
988 };
989 /* *INDENT-ON* */
990
991 void
992 nat_ha_get_resync_status (u8 * in_resync, u32 * resync_ack_missed)
993 {
994   nat_ha_main_t *ha = &nat_ha_main;
995
996   *in_resync = ha->in_resync;
997   *resync_ack_missed = ha->resync_ack_missed;
998 }
999
1000 typedef struct
1001 {
1002   ip4_address_t addr;
1003   u32 event_count;
1004 } nat_ha_trace_t;
1005
1006 static u8 *
1007 format_nat_ha_trace (u8 * s, va_list * args)
1008 {
1009   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1010   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1011   nat_ha_trace_t *t = va_arg (*args, nat_ha_trace_t *);
1012
1013   s = format (s, "nat44-ei-ha: %u events from %U", t->event_count,
1014               format_ip4_address, &t->addr);
1015
1016   return s;
1017 }
1018
1019 typedef enum
1020 {
1021   NAT_HA_NEXT_IP4_LOOKUP,
1022   NAT_HA_NEXT_DROP,
1023   NAT_HA_N_NEXT,
1024 } nat_ha_next_t;
1025
1026 #define foreach_nat_ha_error   \
1027 _(PROCESSED, "pkts-processed") \
1028 _(BAD_VERSION, "bad-version")
1029
1030 typedef enum
1031 {
1032 #define _(sym, str) NAT_HA_ERROR_##sym,
1033   foreach_nat_ha_error
1034 #undef _
1035     NAT_HA_N_ERROR,
1036 } nat_ha_error_t;
1037
1038 static char *nat_ha_error_strings[] = {
1039 #define _(sym, str) str,
1040   foreach_nat_ha_error
1041 #undef _
1042 };
1043
1044 /* process received HA NAT protocol messages */
1045 static uword
1046 nat_ha_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1047                 vlib_frame_t * frame)
1048 {
1049   u32 n_left_from, *from, next_index, *to_next;
1050   f64 now = vlib_time_now (vm);
1051   u32 thread_index = vm->thread_index;
1052   u32 pkts_processed = 0;
1053   ip4_main_t *i4m = &ip4_main;
1054   u8 host_config_ttl = i4m->host_config.ttl;
1055   nat_ha_main_t *ha = &nat_ha_main;
1056
1057   from = vlib_frame_vector_args (frame);
1058   n_left_from = frame->n_vectors;
1059   next_index = node->cached_next_index;
1060
1061   while (n_left_from > 0)
1062     {
1063       u32 n_left_to_next;
1064
1065       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1066
1067       while (n_left_from > 0 && n_left_to_next > 0)
1068         {
1069           u32 bi0, next0, src_addr0, dst_addr0;;
1070           vlib_buffer_t *b0;
1071           nat_ha_message_header_t *h0;
1072           nat_ha_event_t *e0;
1073           u16 event_count0, src_port0, dst_port0, old_len0;
1074           ip4_header_t *ip0;
1075           udp_header_t *udp0;
1076           ip_csum_t sum0;
1077
1078           bi0 = from[0];
1079           to_next[0] = bi0;
1080           from += 1;
1081           to_next += 1;
1082           n_left_from -= 1;
1083           n_left_to_next -= 1;
1084
1085           b0 = vlib_get_buffer (vm, bi0);
1086           h0 = vlib_buffer_get_current (b0);
1087           vlib_buffer_advance (b0, -sizeof (*udp0));
1088           udp0 = vlib_buffer_get_current (b0);
1089           vlib_buffer_advance (b0, -sizeof (*ip0));
1090           ip0 = vlib_buffer_get_current (b0);
1091
1092           next0 = NAT_HA_NEXT_DROP;
1093
1094           if (h0->version != NAT_HA_VERSION)
1095             {
1096               b0->error = node->errors[NAT_HA_ERROR_BAD_VERSION];
1097               goto done0;
1098             }
1099
1100           event_count0 = clib_net_to_host_u16 (h0->count);
1101           /* ACK for previously send data */
1102           if (!event_count0 && (h0->flags & NAT_HA_FLAG_ACK))
1103             {
1104               nat_ha_ack_recv (h0->sequence_number, thread_index);
1105               b0->error = node->errors[NAT_HA_ERROR_PROCESSED];
1106               goto done0;
1107             }
1108
1109           e0 = (nat_ha_event_t *) (h0 + 1);
1110
1111           /* process each event */
1112           while (event_count0)
1113             {
1114               nat_ha_event_process (e0, now, thread_index);
1115               event_count0--;
1116               e0 = (nat_ha_event_t *) ((u8 *) e0 + sizeof (nat_ha_event_t));
1117             }
1118
1119           next0 = NAT_HA_NEXT_IP4_LOOKUP;
1120           pkts_processed++;
1121
1122           /* reply with ACK */
1123           b0->current_length = sizeof (*ip0) + sizeof (*udp0) + sizeof (*h0);
1124
1125           src_addr0 = ip0->src_address.data_u32;
1126           dst_addr0 = ip0->dst_address.data_u32;
1127           ip0->src_address.data_u32 = dst_addr0;
1128           ip0->dst_address.data_u32 = src_addr0;
1129           old_len0 = ip0->length;
1130           ip0->length = clib_host_to_net_u16 (b0->current_length);
1131
1132           sum0 = ip0->checksum;
1133           sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
1134                                  ip4_header_t, ttl);
1135           ip0->ttl = host_config_ttl;
1136           sum0 =
1137             ip_csum_update (sum0, old_len0, ip0->length, ip4_header_t,
1138                             length);
1139           ip0->checksum = ip_csum_fold (sum0);
1140
1141           udp0->checksum = 0;
1142           src_port0 = udp0->src_port;
1143           dst_port0 = udp0->dst_port;
1144           udp0->src_port = dst_port0;
1145           udp0->dst_port = src_port0;
1146           udp0->length =
1147             clib_host_to_net_u16 (b0->current_length - sizeof (*ip0));
1148
1149           h0->flags = NAT_HA_FLAG_ACK;
1150           h0->count = 0;
1151           vlib_increment_simple_counter (&ha->counters
1152                                          [NAT_HA_COUNTER_SEND_ACK],
1153                                          thread_index, 0, 1);
1154
1155         done0:
1156           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1157                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1158             {
1159               nat_ha_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1160               ip4_header_t *ip =
1161                 (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
1162               t->event_count = clib_net_to_host_u16 (h0->count);
1163               t->addr.as_u32 = ip->src_address.data_u32;
1164             }
1165
1166           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1167                                            to_next, n_left_to_next,
1168                                            bi0, next0);
1169         }
1170
1171       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1172     }
1173
1174   vlib_node_increment_counter (vm, ha->ha_node_index, NAT_HA_ERROR_PROCESSED,
1175                                pkts_processed);
1176
1177   return frame->n_vectors;
1178 }
1179
1180 /* *INDENT-OFF* */
1181 VLIB_REGISTER_NODE (nat_ha_node) = {
1182   .function = nat_ha_node_fn,
1183   .name = "nat44-ei-ha",
1184   .vector_size = sizeof (u32),
1185   .format_trace = format_nat_ha_trace,
1186   .type = VLIB_NODE_TYPE_INTERNAL,
1187   .n_errors = ARRAY_LEN (nat_ha_error_strings),
1188   .error_strings = nat_ha_error_strings,
1189   .n_next_nodes = NAT_HA_N_NEXT,
1190   .next_nodes = {
1191      [NAT_HA_NEXT_IP4_LOOKUP] = "ip4-lookup",
1192      [NAT_HA_NEXT_DROP] = "error-drop",
1193   },
1194 };
1195 /* *INDENT-ON* */
1196
1197 typedef struct
1198 {
1199   u32 next_worker_index;
1200   u8 in2out;
1201 } nat_ha_handoff_trace_t;
1202
1203 #define foreach_nat_ha_handoff_error  \
1204 _(CONGESTION_DROP, "congestion drop") \
1205 _(SAME_WORKER, "same worker")         \
1206 _(DO_HANDOFF, "do handoff")
1207
1208 typedef enum
1209 {
1210 #define _(sym,str) NAT_HA_HANDOFF_ERROR_##sym,
1211   foreach_nat_ha_handoff_error
1212 #undef _
1213     NAT_HA_HANDOFF_N_ERROR,
1214 } nat_ha_handoff_error_t;
1215
1216 static char *nat_ha_handoff_error_strings[] = {
1217 #define _(sym,string) string,
1218   foreach_nat_ha_handoff_error
1219 #undef _
1220 };
1221
1222 static u8 *
1223 format_nat_ha_handoff_trace (u8 * s, va_list * args)
1224 {
1225   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1226   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1227   nat_ha_handoff_trace_t *t = va_arg (*args, nat_ha_handoff_trace_t *);
1228
1229   s =
1230     format (s, "NAT_HA_WORKER_HANDOFF: next-worker %d", t->next_worker_index);
1231
1232   return s;
1233 }
1234
1235 /* do worker handoff based on thread_index in NAT HA protcol header */
1236 static uword
1237 nat_ha_handoff_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1238                         vlib_frame_t * frame)
1239 {
1240   nat_ha_main_t *ha = &nat_ha_main;
1241   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
1242   u32 n_enq, n_left_from, *from;
1243   u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1244   u32 thread_index = vm->thread_index;
1245   u32 do_handoff = 0, same_worker = 0;
1246
1247   from = vlib_frame_vector_args (frame);
1248   n_left_from = frame->n_vectors;
1249   vlib_get_buffers (vm, from, bufs, n_left_from);
1250
1251   b = bufs;
1252   ti = thread_indices;
1253
1254   while (n_left_from > 0)
1255     {
1256       nat_ha_message_header_t *h0;
1257
1258       h0 = vlib_buffer_get_current (b[0]);
1259       ti[0] = clib_net_to_host_u32 (h0->thread_index);
1260
1261       if (ti[0] != thread_index)
1262         do_handoff++;
1263       else
1264         same_worker++;
1265
1266       if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1267                          && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1268         {
1269           nat_ha_handoff_trace_t *t =
1270             vlib_add_trace (vm, node, b[0], sizeof (*t));
1271           t->next_worker_index = ti[0];
1272         }
1273
1274       n_left_from -= 1;
1275       ti += 1;
1276       b += 1;
1277     }
1278
1279   n_enq =
1280     vlib_buffer_enqueue_to_thread (vm, ha->fq_index, from, thread_indices,
1281                                    frame->n_vectors, 1);
1282
1283   if (n_enq < frame->n_vectors)
1284     vlib_node_increment_counter (vm, node->node_index,
1285                                  NAT_HA_HANDOFF_ERROR_CONGESTION_DROP,
1286                                  frame->n_vectors - n_enq);
1287   vlib_node_increment_counter (vm, node->node_index,
1288                                NAT_HA_HANDOFF_ERROR_SAME_WORKER, same_worker);
1289   vlib_node_increment_counter (vm, node->node_index,
1290                                NAT_HA_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
1291   return frame->n_vectors;
1292 }
1293
1294 int
1295 nat_ha_resync (u32 client_index, u32 pid,
1296                nat_ha_resync_event_cb_t event_callback)
1297 {
1298   return 0;
1299 }
1300
1301 /* *INDENT-OFF* */
1302 VLIB_REGISTER_NODE (nat_ha_handoff_node) = {
1303   .function = nat_ha_handoff_node_fn,
1304   .name = "nat44-ei-ha-handoff",
1305   .vector_size = sizeof (u32),
1306   .format_trace = format_nat_ha_handoff_trace,
1307   .type = VLIB_NODE_TYPE_INTERNAL,
1308   .n_errors = ARRAY_LEN(nat_ha_handoff_error_strings),
1309   .error_strings = nat_ha_handoff_error_strings,
1310   .n_next_nodes = 1,
1311   .next_nodes = {
1312     [0] = "error-drop",
1313   },
1314 };
1315 /* *INDENT-ON* */
1316
1317 /*
1318  * fd.io coding-style-patch-verification: ON
1319  *
1320  * Local Variables:
1321  * eval: (c-set-style "gnu")
1322  * End:
1323  */