FD.io VPP  v21.06-1-gbb7418cf9
Vector Packet Processing
nat44_ei_ha.c
Go to the documentation of this file.
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>
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 {
55 
56 /* NAT HA protocol header */
57 typedef struct
58 {
59  /* version */
61  /* flags */
63  /* event count */
65  /* sequence number */
67  /* thread index where events originated */
69 } __attribute__ ((packed)) nat_ha_message_header_t;
70 
71 /* NAT HA protocol event data */
72 typedef struct
73 {
74  /* event type */
76  /* session data */
90 } __attribute__ ((packed)) nat_ha_event_t;
91 
92 typedef enum
93 {
94 #define _(N, s, v) NAT_HA_COUNTER_##N = v,
96 #undef _
99 
100 /* data waiting for ACK */
101 typedef struct
102 {
103  /* sequence number */
105  /* retry count */
107  /* next retry time */
109  /* 1 if HA resync */
111  /* packet data */
114 
115 /* per thread data */
116 typedef struct
117 {
118  /* buffer under construction */
120  /* frame containing NAT HA buffers */
122  /* number of events */
124  /* next event offset */
126  /* data waiting for ACK */
129 
130 /* NAT HA settings */
131 typedef struct nat_ha_main_s
132 {
134  /* local IP address and UDP port */
137  /* failvoer IP address and UDP port */
140  /* path MTU between local and failover */
142  /* number of seconds after which to send session counters refresh */
144  /* counters */
146  /* sequence number counter */
148  /* 1 if resync in progress */
150  /* number of remaing ACK for resync */
152  /* number of missed ACK for resync */
154  /* resync data */
158  /* per thread data */
161 
166 
167  /* worker handoff frame-queue index */
169 } nat_ha_main_t;
170 
172 
174 nat44_ei_ha_sadd (ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr,
175  u16 out_port, ip4_address_t *eh_addr, u16 eh_port,
176  ip4_address_t *ehn_addr, u16 ehn_port, u8 proto,
177  u32 fib_index, u16 flags, u32 thread_index)
178 {
181  nat44_ei_user_t *u;
182  nat44_ei_session_t *s;
185  f64 now = vlib_time_now (vm);
186  nat44_ei_outside_fib_t *outside_fib;
188  fib_prefix_t pfx = {
190  .fp_len = 32,
191  .fp_addr = {
192  .ip4.as_u32 = eh_addr->as_u32,
193  },
194  };
195 
197  {
198  if (nat44_ei_set_outside_address_and_port (nm->addresses, thread_index,
199  *out_addr, out_port, proto))
200  return;
201  }
202 
203  u = nat44_ei_user_get_or_create (nm, in_addr, fib_index, thread_index);
204  if (!u)
205  return;
206 
207  s = nat44_ei_session_alloc_or_recycle (nm, u, thread_index, now);
208  if (!s)
209  return;
210 
211  s->out2in.addr.as_u32 = out_addr->as_u32;
212  s->out2in.port = out_port;
213  s->nat_proto = proto;
214  s->last_heard = now;
215  s->flags = flags;
216  s->ext_host_addr.as_u32 = eh_addr->as_u32;
217  s->ext_host_port = eh_port;
219  switch (vec_len (nm->outside_fibs))
220  {
221  case 0:
222  s->out2in.fib_index = nm->outside_fib_index;
223  break;
224  case 1:
225  s->out2in.fib_index = nm->outside_fibs[0].fib_index;
226  break;
227  default:
228  vec_foreach (outside_fib, nm->outside_fibs)
229  {
230  fei = fib_table_lookup (outside_fib->fib_index, &pfx);
231  if (FIB_NODE_INDEX_INVALID != fei)
232  {
233  if (fib_entry_get_resolving_interface (fei) != ~0)
234  {
235  s->out2in.fib_index = outside_fib->fib_index;
236  break;
237  }
238  }
239  }
240  break;
241  }
242  init_nat_o2i_kv (&kv, s, thread_index, s - tnm->sessions);
243  if (clib_bihash_add_del_8_8 (&nm->out2in, &kv, 1))
244  nat_elog_warn (nm, "out2in key add failed");
245 
246  s->in2out.addr.as_u32 = in_addr->as_u32;
247  s->in2out.port = in_port;
248  s->in2out.fib_index = fib_index;
249  init_nat_i2o_kv (&kv, s, thread_index, s - tnm->sessions);
250  if (clib_bihash_add_del_8_8 (&nm->in2out, &kv, 1))
251  nat_elog_warn (nm, "in2out key add failed");
252 }
253 
255 nat44_ei_ha_sdel (ip4_address_t *out_addr, u16 out_port,
256  ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
258 {
261  nat44_ei_session_t *s;
263 
264  init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
265  if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
266  return;
267 
268  ASSERT (thread_index == nat_value_get_thread_index (&value));
269  tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
271  nat44_ei_free_session_data_v2 (nm, s, thread_index, 1);
272  nat44_ei_delete_session (nm, s, thread_index);
273 }
274 
276 nat44_ei_ha_sref (ip4_address_t *out_addr, u16 out_port,
277  ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index,
278  u32 total_pkts, u64 total_bytes, u32 thread_index)
279 {
282  nat44_ei_session_t *s;
284 
285  tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
286 
287  init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
288  if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
289  return;
290 
292  s->total_pkts = total_pkts;
293  s->total_bytes = total_bytes;
294 }
295 
296 static void
298 {
300  nat_ha_main_t *ha = &nat_ha_main;
301 
302  /* if no more resync ACK remainig we are done */
303  if (ha->resync_ack_count)
304  return;
305 
306  ha->in_resync = 0;
307  if (ha->resync_ack_missed)
308  {
309  nat_elog_info (nm, "resync completed with result FAILED");
310  }
311  else
312  {
313  nat_elog_info (nm, "resync completed with result SUCCESS");
314  }
315  if (ha->event_callback)
316  ha->event_callback (ha->client_index, ha->pid, ha->resync_ack_missed);
317 }
318 
319 /* cache HA NAT data waiting for ACK */
320 static int
322  u8 is_resync, u32 vlib_thread_index)
323 {
324  nat_ha_main_t *ha = &nat_ha_main;
325  nat_ha_per_thread_data_t *td = &ha->per_thread_data[vlib_thread_index];
326  nat_ha_resend_entry_t *entry;
327  f64 now = vlib_time_now (vm);
328 
329  vec_add2 (td->resend_queue, entry, 1);
330  clib_memset (entry, 0, sizeof (*entry));
331  entry->retry_timer = now + 2.0;
332  entry->seq = seq;
333  entry->is_resync = is_resync;
334  vec_add (entry->data, data, data_len);
335 
336  return 0;
337 }
338 
341 {
343  nat_ha_main_t *ha = &nat_ha_main;
345  u32 i;
346 
348  {
349  if (td->resend_queue[i].seq != seq)
350  continue;
351 
352  vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ACK],
353  thread_index, 0, 1);
354  /* ACK received remove cached data */
355  if (td->resend_queue[i].is_resync)
356  {
359  }
360  vec_free (td->resend_queue[i].data);
361  vec_del1 (td->resend_queue, i);
362  nat_elog_debug_X1 (nm, "ACK for seq %d received", "i4",
363  clib_net_to_host_u32 (seq));
364 
365  return;
366  }
367 }
368 
369 /* scan non-ACKed HA NAT for retry */
370 static void
372 {
374  nat_ha_main_t *ha = &nat_ha_main;
376  u32 i, *del, *to_delete = 0;
377  vlib_buffer_t *b = 0;
378  vlib_frame_t *f;
379  u32 bi, *to_next;
380  ip4_header_t *ip;
381  f64 now = vlib_time_now (vm);
382 
384  {
385  if (td->resend_queue[i].retry_timer > now)
386  continue;
387 
388  /* maximum retry reached delete cached data */
390  {
391  nat_elog_notice_X1 (nm, "seq %d missed", "i4",
392  clib_net_to_host_u32 (td->resend_queue[i].seq));
393  if (td->resend_queue[i].is_resync)
394  {
398  }
399  vec_add1 (to_delete, i);
401  [NAT_HA_COUNTER_MISSED_COUNT],
402  thread_index, 0, 1);
403  continue;
404  }
405 
406  /* retry to send non-ACKed data */
407  nat_elog_debug_X1 (nm, "state sync seq %d resend", "i4",
408  clib_net_to_host_u32 (td->resend_queue[i].seq));
409  td->resend_queue[i].retry_count++;
410  vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RETRY_COUNT],
411  thread_index, 0, 1);
412  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
413  {
414  nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
415  return;
416  }
417  b = vlib_get_buffer (vm, bi);
418  b->current_length = vec_len (td->resend_queue[i].data);
419  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
420  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
421  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
422  vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
423  ip = vlib_buffer_get_current (b);
424  clib_memcpy (ip, td->resend_queue[i].data,
425  vec_len (td->resend_queue[i].data));
426  f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
427  to_next = vlib_frame_vector_args (f);
428  to_next[0] = bi;
429  f->n_vectors = 1;
431  td->resend_queue[i].retry_timer = now + 2.0;
432  }
433 
434  vec_foreach (del, to_delete)
435  {
436  vec_free (td->resend_queue[*del].data);
437  vec_del1 (td->resend_queue, *del);
438  }
439  vec_free (to_delete);
440 }
441 
442 void
444 {
445  nat_ha_main_t *ha = &nat_ha_main;
446  ha->enabled = 1;
447 }
448 
449 void
451 {
452  nat_ha_main_t *ha = &nat_ha_main;
453  ha->dst_port = 0;
454  ha->enabled = 0;
455 }
456 
457 void
459 {
460  vlib_node_t *node;
461 
462  node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-handoff");
463  ha->ha_handoff_node_index = node->index;
464  node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-process");
465  ha->ha_process_node_index = node->index;
466  node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha-worker");
467  ha->ha_worker_node_index = node->index;
468  node = vlib_get_node_by_name (vm, (u8 *) "nat44-ei-ha");
469  ha->ha_node_index = node->index;
470 }
471 
472 void
474 {
475  nat_ha_main_t *ha = &nat_ha_main;
476  clib_memset (ha, 0, sizeof (*ha));
477 
478  nat_ha_set_node_indexes (ha, vm);
479 
480  ha->fq_index = ~0;
481 
482  ha->num_workers = num_workers;
483  vec_validate (ha->per_thread_data, num_threads);
484 
485 #define _(N, s, v) \
486  ha->counters[v].name = s; \
487  ha->counters[v].stat_segment_name = "/nat44-ei/ha/" s; \
488  vlib_validate_simple_counter (&ha->counters[v], 0); \
489  vlib_zero_simple_counter (&ha->counters[v], 0);
491 #undef _
492 }
493 
494 int
496  u32 path_mtu)
497 {
499  nat_ha_main_t *ha = &nat_ha_main;
500 
501  /* unregister previously set UDP port */
502  if (ha->src_port)
503  udp_unregister_dst_port (vm, ha->src_port, 1);
504 
505  ha->src_ip_address.as_u32 = addr->as_u32;
506  ha->src_port = port;
508 
509  if (port)
510  {
511  /* if multiple worker threads first go to handoff node */
512  if (ha->num_workers > 1)
513  {
514  if (ha->fq_index == ~0)
516  udp_register_dst_port (vm, port, ha->ha_handoff_node_index, 1);
517  }
518  else
519  {
520  udp_register_dst_port (vm, port, ha->ha_node_index, 1);
521  }
522  nat_elog_info_X1 (nm, "HA listening on port %d for state sync", "i4",
523  port);
524  }
525 
526  return 0;
527 }
528 
529 void
531 {
532  nat_ha_main_t *ha = &nat_ha_main;
533 
534  addr->as_u32 = ha->src_ip_address.as_u32;
535  *port = ha->src_port;
536  *path_mtu = ha->state_sync_path_mtu;
537 }
538 
539 int
542 {
543  nat_ha_main_t *ha = &nat_ha_main;
544 
545  ha->dst_ip_address.as_u32 = addr->as_u32;
546  ha->dst_port = port;
548 
550 
551  return 0;
552 }
553 
554 void
557 {
558  nat_ha_main_t *ha = &nat_ha_main;
559 
560  addr->as_u32 = ha->dst_ip_address.as_u32;
561  *port = ha->dst_port;
562  *session_refresh_interval = ha->session_refresh_interval;
563 }
564 
567 {
568  nat_ha_main_t *ha = &nat_ha_main;
569  ip4_address_t in_addr, out_addr, eh_addr, ehn_addr;
570  u32 fib_index;
571  u16 flags;
572 
573  vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ADD],
574  thread_index, 0, 1);
575 
576  in_addr.as_u32 = event->in_addr;
577  out_addr.as_u32 = event->out_addr;
578  eh_addr.as_u32 = event->eh_addr;
579  ehn_addr.as_u32 = event->ehn_addr;
580  fib_index = clib_net_to_host_u32 (event->fib_index);
581  flags = clib_net_to_host_u16 (event->flags);
582 
583  nat44_ei_ha_sadd (&in_addr, event->in_port, &out_addr, event->out_port,
584  &eh_addr, event->eh_port, &ehn_addr, event->ehn_port,
585  event->protocol, fib_index, flags, thread_index);
586 }
587 
590 {
591  nat_ha_main_t *ha = &nat_ha_main;
592  ip4_address_t out_addr, eh_addr;
593  u32 fib_index;
594 
595  vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_DEL],
596  thread_index, 0, 1);
597 
598  out_addr.as_u32 = event->out_addr;
599  eh_addr.as_u32 = event->eh_addr;
600  fib_index = clib_net_to_host_u32 (event->fib_index);
601 
602  nat44_ei_ha_sdel (&out_addr, event->out_port, &eh_addr, event->eh_port,
603  event->protocol, fib_index, thread_index);
604 }
605 
608 {
609  nat_ha_main_t *ha = &nat_ha_main;
610  ip4_address_t out_addr, eh_addr;
611  u32 fib_index, total_pkts;
612  u64 total_bytes;
613 
614  vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_REFRESH],
615  thread_index, 0, 1);
616 
617  out_addr.as_u32 = event->out_addr;
618  eh_addr.as_u32 = event->eh_addr;
619  fib_index = clib_net_to_host_u32 (event->fib_index);
620  total_pkts = clib_net_to_host_u32 (event->total_pkts);
621  total_bytes = clib_net_to_host_u64 (event->total_bytes);
622 
623  nat44_ei_ha_sref (&out_addr, event->out_port, &eh_addr, event->eh_port,
624  event->protocol, fib_index, total_pkts, total_bytes,
625  thread_index);
626 }
627 
628 /* process received NAT HA event */
631 {
633  switch (event->event_type)
634  {
635  case NAT_HA_ADD:
636  nat_ha_recv_add (event, now, thread_index);
637  break;
638  case NAT_HA_DEL:
639  nat_ha_recv_del (event, thread_index);
640  break;
641  case NAT_HA_REFRESH:
642  nat_ha_recv_refresh (event, now, thread_index);
643  break;
644  default:
645  nat_elog_notice_X1 (nm, "Unsupported HA event type %d", "i4",
646  event->event_type);
647  break;
648  }
649 }
650 
651 static inline void
653 {
654  nat_ha_main_t *ha = &nat_ha_main;
656  ip4_header_t *ip;
657  udp_header_t *udp;
659 
660  b->current_data = 0;
661  b->current_length = sizeof (*ip) + sizeof (*udp) + sizeof (*h);
662  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
663  b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
664  vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
665  vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
666  ip = vlib_buffer_get_current (b);
667  udp = (udp_header_t *) (ip + 1);
668  h = (nat_ha_message_header_t *) (udp + 1);
669 
670  /* IP header */
671  ip->ip_version_and_header_length = 0x45;
672  ip->ttl = 254;
673  ip->protocol = IP_PROTOCOL_UDP;
675  clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
678  /* UDP header */
679  udp->src_port = clib_host_to_net_u16 (ha->src_port);
680  udp->dst_port = clib_host_to_net_u16 (ha->dst_port);
681  udp->checksum = 0;
682 
683  /* NAT HA protocol header */
684  h->version = NAT_HA_VERSION;
685  h->flags = 0;
686  h->count = 0;
687  h->thread_index = clib_host_to_net_u32 (thread_index);
688  sequence_number = clib_atomic_fetch_add (&ha->sequence_number, 1);
689  h->sequence_number = clib_host_to_net_u32 (sequence_number);
690 
691  *offset =
692  sizeof (ip4_header_t) + sizeof (udp_header_t) +
693  sizeof (nat_ha_message_header_t);
694 }
695 
696 static inline void
698  u32 vlib_thread_index)
699 {
700  nat_ha_main_t *ha = &nat_ha_main;
701  nat_ha_per_thread_data_t *td = &ha->per_thread_data[vlib_thread_index];
703  ip4_header_t *ip;
704  udp_header_t *udp;
705  vlib_main_t *vm = vlib_get_main_by_index (vlib_thread_index);
706 
707  ip = vlib_buffer_get_current (b);
708  udp = ip4_next_header (ip);
709  h = (nat_ha_message_header_t *) (udp + 1);
710 
711  h->count = clib_host_to_net_u16 (td->state_sync_count);
712 
713  ip->length = clib_host_to_net_u16 (b->current_length);
714  ip->checksum = ip4_header_checksum (ip);
715  udp->length = clib_host_to_net_u16 (b->current_length - sizeof (*ip));
716 
718  b->current_length, is_resync, vlib_thread_index);
719 
721 }
722 
723 /* add NAT HA protocol event */
725 nat_ha_event_add (nat_ha_event_t *event, u8 do_flush, u32 session_thread_index,
726  u8 is_resync)
727 {
729  nat_ha_main_t *ha = &nat_ha_main;
730  u32 vlib_thread_index = vlib_get_thread_index ();
731  nat_ha_per_thread_data_t *td = &ha->per_thread_data[vlib_thread_index];
732  vlib_main_t *vm = vlib_get_main_by_index (vlib_thread_index);
733  vlib_buffer_t *b = 0;
734  vlib_frame_t *f;
735  u32 bi = ~0, offset;
736 
737  b = td->state_sync_buffer;
738 
739  if (PREDICT_FALSE (b == 0))
740  {
741  if (do_flush)
742  return;
743 
744  if (vlib_buffer_alloc (vm, &bi, 1) != 1)
745  {
746  nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
747  return;
748  }
749 
750  b = td->state_sync_buffer = vlib_get_buffer (vm, bi);
751  clib_memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
752  offset = 0;
753  }
754  else
755  {
756  bi = vlib_get_buffer_index (vm, b);
758  }
759 
760  f = td->state_sync_frame;
761  if (PREDICT_FALSE (f == 0))
762  {
763  u32 *to_next;
764  f = vlib_get_frame_to_node (vm, ip4_lookup_node.index);
765  td->state_sync_frame = f;
766  to_next = vlib_frame_vector_args (f);
767  to_next[0] = bi;
768  f->n_vectors = 1;
769  }
770 
771  if (PREDICT_FALSE (td->state_sync_count == 0))
772  nat_ha_header_create (b, &offset, session_thread_index);
773 
774  if (PREDICT_TRUE (do_flush == 0))
775  {
776  clib_memcpy_fast (b->data + offset, event, sizeof (*event));
777  offset += sizeof (*event);
778  td->state_sync_count++;
779  b->current_length += sizeof (*event);
780 
781  switch (event->event_type)
782  {
783  case NAT_HA_ADD:
785  &ha->counters[NAT_HA_COUNTER_SEND_ADD], vlib_thread_index, 0, 1);
786  break;
787  case NAT_HA_DEL:
789  &ha->counters[NAT_HA_COUNTER_SEND_DEL], vlib_thread_index, 0, 1);
790  break;
791  case NAT_HA_REFRESH:
793  &ha->counters[NAT_HA_COUNTER_SEND_REFRESH], vlib_thread_index, 0,
794  1);
795  break;
796  default:
797  break;
798  }
799  }
800 
801  if (PREDICT_FALSE
802  (do_flush || offset + (sizeof (*event)) > ha->state_sync_path_mtu))
803  {
804  nat_ha_send (f, b, is_resync, vlib_thread_index);
805  td->state_sync_buffer = 0;
806  td->state_sync_frame = 0;
807  td->state_sync_count = 0;
808  offset = 0;
809  if (is_resync)
810  {
813  }
814  }
815 
817 }
818 
819 #define skip_if_disabled() \
820 do { \
821  nat_ha_main_t *ha = &nat_ha_main; \
822  if (PREDICT_TRUE (!ha->dst_port)) \
823  return; \
824 } while (0)
825 
826 void
827 nat_ha_flush (u8 is_resync)
828 {
829  skip_if_disabled ();
830  nat_ha_event_add (0, 1, 0, is_resync);
831 }
832 
833 void
834 nat_ha_sadd (ip4_address_t * in_addr, u16 in_port, ip4_address_t * out_addr,
835  u16 out_port, ip4_address_t * eh_addr, u16 eh_port,
836  ip4_address_t * ehn_addr, u16 ehn_port, u8 proto, u32 fib_index,
837  u16 flags, u32 thread_index, u8 is_resync)
838 {
839  nat_ha_event_t event;
840 
841  skip_if_disabled ();
842 
843  clib_memset (&event, 0, sizeof (event));
844  event.event_type = NAT_HA_ADD;
845  event.flags = clib_host_to_net_u16 (flags);
846  event.in_addr = in_addr->as_u32;
847  event.in_port = in_port;
848  event.out_addr = out_addr->as_u32;
849  event.out_port = out_port;
850  event.eh_addr = eh_addr->as_u32;
851  event.eh_port = eh_port;
852  event.ehn_addr = ehn_addr->as_u32;
853  event.ehn_port = ehn_port;
854  event.fib_index = clib_host_to_net_u32 (fib_index);
855  event.protocol = proto;
856  nat_ha_event_add (&event, 0, thread_index, is_resync);
857 }
858 
859 void
860 nat_ha_sdel (ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr,
861  u16 eh_port, u8 proto, u32 fib_index, u32 session_thread_index)
862 {
863  nat_ha_event_t event;
864 
865  skip_if_disabled ();
866 
867  clib_memset (&event, 0, sizeof (event));
868  event.event_type = NAT_HA_DEL;
869  event.out_addr = out_addr->as_u32;
870  event.out_port = out_port;
871  event.eh_addr = eh_addr->as_u32;
872  event.eh_port = eh_port;
873  event.fib_index = clib_host_to_net_u32 (fib_index);
874  event.protocol = proto;
875  nat_ha_event_add (&event, 0, session_thread_index, 0);
876 }
877 
878 void
879 nat_ha_sref (ip4_address_t * out_addr, u16 out_port, ip4_address_t * eh_addr,
880  u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts,
881  u64 total_bytes, u32 thread_index, f64 * last_refreshed, f64 now)
882 {
883  nat_ha_main_t *ha = &nat_ha_main;
884  nat_ha_event_t event;
885 
886  skip_if_disabled ();
887 
888  if ((*last_refreshed + ha->session_refresh_interval) > now)
889  return;
890 
891  *last_refreshed = now;
892  clib_memset (&event, 0, sizeof (event));
893  event.event_type = NAT_HA_REFRESH;
894  event.out_addr = out_addr->as_u32;
895  event.out_port = out_port;
896  event.eh_addr = eh_addr->as_u32;
897  event.eh_port = eh_port;
898  event.fib_index = clib_host_to_net_u32 (fib_index);
899  event.protocol = proto;
900  event.total_pkts = clib_host_to_net_u32 (total_pkts);
901  event.total_bytes = clib_host_to_net_u64 (total_bytes);
902  nat_ha_event_add (&event, 0, thread_index, 0);
903 }
904 
907 {
908  nat_ha_main_t *ha = &nat_ha_main;
909  return ha->enabled;
910 }
911 
912 /* per thread process waiting for interrupt */
913 static uword
915  vlib_frame_t * f)
916 {
918 
919  if (plugin_enabled () == 0)
920  return 0;
921 
922  /* flush HA NAT data under construction */
923  nat_ha_event_add (0, 1, thread_index, 0);
924  /* scan if we need to resend some non-ACKed data */
925  nat_ha_resend_scan (vm, thread_index);
926  return 0;
927 }
928 
929 /* *INDENT-OFF* */
930 VLIB_REGISTER_NODE (nat_ha_worker_node) = {
931  .function = nat_ha_worker_fn,
932  .type = VLIB_NODE_TYPE_INPUT,
933  .state = VLIB_NODE_STATE_INTERRUPT,
934  .name = "nat44-ei-ha-worker",
935 };
936 /* *INDENT-ON* */
937 
938 /* periodically send interrupt to each thread */
939 static uword
941 {
943  nat_ha_main_t *ha = &nat_ha_main;
944  uword event_type;
945  uword *event_data = 0;
946  u32 ti;
947 
949  event_type = vlib_process_get_events (vm, &event_data);
950  if (event_type)
951  nat_elog_info (nm, "nat44-ei-ha-process: bogus kickoff event received");
952  vec_reset_length (event_data);
953 
954  while (1)
955  {
957  event_type = vlib_process_get_events (vm, &event_data);
958  vec_reset_length (event_data);
959  for (ti = 0; ti < vlib_get_n_threads (); ti++)
960  {
961  if (ti >= vec_len (ha->per_thread_data))
962  continue;
963 
965  nat_ha_worker_node.index);
966  }
967  }
968 
969  return 0;
970 }
971 
972 /* *INDENT-OFF* */
973 VLIB_REGISTER_NODE (nat_ha_process_node) = {
974  .function = nat_ha_process,
975  .type = VLIB_NODE_TYPE_PROCESS,
976  .name = "nat44-ei-ha-process",
977 };
978 /* *INDENT-ON* */
979 
980 void
982 {
983  nat_ha_main_t *ha = &nat_ha_main;
984 
985  *in_resync = ha->in_resync;
986  *resync_ack_missed = ha->resync_ack_missed;
987 }
988 
989 typedef struct
990 {
994 
995 static u8 *
996 format_nat_ha_trace (u8 * s, va_list * args)
997 {
998  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
999  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1000  nat_ha_trace_t *t = va_arg (*args, nat_ha_trace_t *);
1001 
1002  s = format (s, "nat44-ei-ha: %u events from %U", t->event_count,
1003  format_ip4_address, &t->addr);
1004 
1005  return s;
1006 }
1007 
1008 typedef enum
1009 {
1013 } nat_ha_next_t;
1014 
1015 #define foreach_nat_ha_error \
1016 _(PROCESSED, "pkts-processed") \
1017 _(BAD_VERSION, "bad-version")
1018 
1019 typedef enum
1020 {
1021 #define _(sym, str) NAT_HA_ERROR_##sym,
1023 #undef _
1025 } nat_ha_error_t;
1026 
1027 static char *nat_ha_error_strings[] = {
1028 #define _(sym, str) str,
1030 #undef _
1031 };
1032 
1033 /* process received HA NAT protocol messages */
1034 static uword
1036  vlib_frame_t * frame)
1037 {
1038  u32 n_left_from, *from, next_index, *to_next;
1039  f64 now = vlib_time_now (vm);
1041  u32 pkts_processed = 0;
1042  ip4_main_t *i4m = &ip4_main;
1043  u8 host_config_ttl = i4m->host_config.ttl;
1044  nat_ha_main_t *ha = &nat_ha_main;
1045 
1046  from = vlib_frame_vector_args (frame);
1047  n_left_from = frame->n_vectors;
1048  next_index = node->cached_next_index;
1049 
1050  while (n_left_from > 0)
1051  {
1052  u32 n_left_to_next;
1053 
1054  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1055 
1056  while (n_left_from > 0 && n_left_to_next > 0)
1057  {
1058  u32 bi0, next0, src_addr0, dst_addr0;;
1059  vlib_buffer_t *b0;
1061  nat_ha_event_t *e0;
1062  u16 event_count0, src_port0, dst_port0, old_len0;
1063  ip4_header_t *ip0;
1064  udp_header_t *udp0;
1065  ip_csum_t sum0;
1066 
1067  bi0 = from[0];
1068  to_next[0] = bi0;
1069  from += 1;
1070  to_next += 1;
1071  n_left_from -= 1;
1072  n_left_to_next -= 1;
1073 
1074  b0 = vlib_get_buffer (vm, bi0);
1075  h0 = vlib_buffer_get_current (b0);
1076  vlib_buffer_advance (b0, -sizeof (*udp0));
1077  udp0 = vlib_buffer_get_current (b0);
1078  vlib_buffer_advance (b0, -sizeof (*ip0));
1079  ip0 = vlib_buffer_get_current (b0);
1080 
1081  next0 = NAT_HA_NEXT_DROP;
1082 
1083  if (h0->version != NAT_HA_VERSION)
1084  {
1085  b0->error = node->errors[NAT_HA_ERROR_BAD_VERSION];
1086  goto done0;
1087  }
1088 
1089  event_count0 = clib_net_to_host_u16 (h0->count);
1090  /* ACK for previously send data */
1091  if (!event_count0 && (h0->flags & NAT_HA_FLAG_ACK))
1092  {
1093  nat_ha_ack_recv (h0->sequence_number, thread_index);
1094  b0->error = node->errors[NAT_HA_ERROR_PROCESSED];
1095  goto done0;
1096  }
1097 
1098  e0 = (nat_ha_event_t *) (h0 + 1);
1099 
1100  /* process each event */
1101  while (event_count0)
1102  {
1103  nat_ha_event_process (e0, now, thread_index);
1104  event_count0--;
1105  e0 = (nat_ha_event_t *) ((u8 *) e0 + sizeof (nat_ha_event_t));
1106  }
1107 
1108  next0 = NAT_HA_NEXT_IP4_LOOKUP;
1109  pkts_processed++;
1110 
1111  /* reply with ACK */
1112  b0->current_length = sizeof (*ip0) + sizeof (*udp0) + sizeof (*h0);
1113 
1114  src_addr0 = ip0->src_address.data_u32;
1115  dst_addr0 = ip0->dst_address.data_u32;
1116  ip0->src_address.data_u32 = dst_addr0;
1117  ip0->dst_address.data_u32 = src_addr0;
1118  old_len0 = ip0->length;
1119  ip0->length = clib_host_to_net_u16 (b0->current_length);
1120 
1121  sum0 = ip0->checksum;
1122  sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
1123  ip4_header_t, ttl);
1124  ip0->ttl = host_config_ttl;
1125  sum0 =
1126  ip_csum_update (sum0, old_len0, ip0->length, ip4_header_t,
1127  length);
1128  ip0->checksum = ip_csum_fold (sum0);
1129 
1130  udp0->checksum = 0;
1131  src_port0 = udp0->src_port;
1132  dst_port0 = udp0->dst_port;
1133  udp0->src_port = dst_port0;
1134  udp0->dst_port = src_port0;
1135  udp0->length =
1136  clib_host_to_net_u16 (b0->current_length - sizeof (*ip0));
1137 
1138  h0->flags = NAT_HA_FLAG_ACK;
1139  h0->count = 0;
1141  [NAT_HA_COUNTER_SEND_ACK],
1142  thread_index, 0, 1);
1143 
1144  done0:
1146  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1147  {
1148  nat_ha_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
1149  ip4_header_t *ip =
1150  (void *) (b0->data + vnet_buffer (b0)->l3_hdr_offset);
1151  t->event_count = clib_net_to_host_u16 (h0->count);
1152  t->addr.as_u32 = ip->src_address.data_u32;
1153  }
1154 
1155  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1156  to_next, n_left_to_next,
1157  bi0, next0);
1158  }
1159 
1160  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1161  }
1162 
1163  vlib_node_increment_counter (vm, ha->ha_node_index, NAT_HA_ERROR_PROCESSED,
1164  pkts_processed);
1165 
1166  return frame->n_vectors;
1167 }
1168 
1169 /* *INDENT-OFF* */
1170 VLIB_REGISTER_NODE (nat_ha_node) = {
1171  .function = nat_ha_node_fn,
1172  .name = "nat44-ei-ha",
1173  .vector_size = sizeof (u32),
1174  .format_trace = format_nat_ha_trace,
1176  .n_errors = ARRAY_LEN (nat_ha_error_strings),
1177  .error_strings = nat_ha_error_strings,
1178  .n_next_nodes = NAT_HA_N_NEXT,
1179  .next_nodes = {
1180  [NAT_HA_NEXT_IP4_LOOKUP] = "ip4-lookup",
1181  [NAT_HA_NEXT_DROP] = "error-drop",
1182  },
1183 };
1184 /* *INDENT-ON* */
1185 
1186 typedef struct
1187 {
1191 
1192 #define foreach_nat_ha_handoff_error \
1193 _(CONGESTION_DROP, "congestion drop") \
1194 _(SAME_WORKER, "same worker") \
1195 _(DO_HANDOFF, "do handoff")
1196 
1197 typedef enum
1198 {
1199 #define _(sym,str) NAT_HA_HANDOFF_ERROR_##sym,
1201 #undef _
1204 
1205 static char *nat_ha_handoff_error_strings[] = {
1206 #define _(sym,string) string,
1208 #undef _
1209 };
1210 
1211 static u8 *
1212 format_nat_ha_handoff_trace (u8 * s, va_list * args)
1213 {
1214  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1215  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1216  nat_ha_handoff_trace_t *t = va_arg (*args, nat_ha_handoff_trace_t *);
1217 
1218  s =
1219  format (s, "NAT_HA_WORKER_HANDOFF: next-worker %d", t->next_worker_index);
1220 
1221  return s;
1222 }
1223 
1224 /* do worker handoff based on thread_index in NAT HA protcol header */
1225 static uword
1227  vlib_frame_t * frame)
1228 {
1229  nat_ha_main_t *ha = &nat_ha_main;
1231  u32 n_enq, n_left_from, *from;
1232  u16 thread_indices[VLIB_FRAME_SIZE], *ti;
1234  u32 do_handoff = 0, same_worker = 0;
1235 
1236  from = vlib_frame_vector_args (frame);
1237  n_left_from = frame->n_vectors;
1238  vlib_get_buffers (vm, from, bufs, n_left_from);
1239 
1240  b = bufs;
1241  ti = thread_indices;
1242 
1243  while (n_left_from > 0)
1244  {
1246 
1247  h0 = vlib_buffer_get_current (b[0]);
1248  ti[0] = clib_net_to_host_u32 (h0->thread_index);
1249 
1250  if (ti[0] != thread_index)
1251  do_handoff++;
1252  else
1253  same_worker++;
1254 
1256  && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1257  {
1259  vlib_add_trace (vm, node, b[0], sizeof (*t));
1260  t->next_worker_index = ti[0];
1261  }
1262 
1263  n_left_from -= 1;
1264  ti += 1;
1265  b += 1;
1266  }
1267 
1268  n_enq = vlib_buffer_enqueue_to_thread (vm, node, ha->fq_index, from,
1269  thread_indices, frame->n_vectors, 1);
1270 
1271  if (n_enq < frame->n_vectors)
1273  NAT_HA_HANDOFF_ERROR_CONGESTION_DROP,
1274  frame->n_vectors - n_enq);
1276  NAT_HA_HANDOFF_ERROR_SAME_WORKER, same_worker);
1278  NAT_HA_HANDOFF_ERROR_DO_HANDOFF, do_handoff);
1279  return frame->n_vectors;
1280 }
1281 
1282 int
1285 {
1286  return 0;
1287 }
1288 
1289 /* *INDENT-OFF* */
1290 VLIB_REGISTER_NODE (nat_ha_handoff_node) = {
1291  .function = nat_ha_handoff_node_fn,
1292  .name = "nat44-ei-ha-handoff",
1293  .vector_size = sizeof (u32),
1294  .format_trace = format_nat_ha_handoff_trace,
1296  .n_errors = ARRAY_LEN(nat_ha_handoff_error_strings),
1297  .error_strings = nat_ha_handoff_error_strings,
1298  .n_next_nodes = 1,
1299  .next_nodes = {
1300  [0] = "error-drop",
1301  },
1302 };
1303 /* *INDENT-ON* */
1304 
1305 /*
1306  * fd.io coding-style-patch-verification: ON
1307  *
1308  * Local Variables:
1309  * eval: (c-set-style "gnu")
1310  * End:
1311  */
void udp_unregister_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u8 is_ip4)
Definition: udp_local.c:506
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:524
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:133
fib_protocol_t fp_proto
protocol type
Definition: fib_types.h:211
static uword nat_ha_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: nat44_ei_ha.c:1035
#define vec_foreach_index(var, v)
Iterate over vector indices.
int nat_ha_set_failover(vlib_main_t *vm, ip4_address_t *addr, u16 port, u32 session_refresh_interval)
Set HA failover (remote settings)
Definition: nat44_ei_ha.c:540
vnet_interface_output_runtime_t * rt
u32 ha_handoff_node_index
Definition: nat44_ei_ha.c:162
Definition: nat44_ei_ha.c:101
#define CLIB_UNUSED(x)
Definition: clib.h:90
void nat44_ei_free_session_data_v2(nat44_ei_main_t *nm, nat44_ei_session_t *s, u32 thread_index, u8 is_ha)
Definition: nat44_ei.c:1079
static uword nat_ha_worker_fn(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: nat44_ei_ha.c:914
#define NAT_HA_FLAG_ACK
Definition: nat44_ei_ha.c:46
#define nat_elog_info(_pm, nat_elog_str)
Definition: log.h:183
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:755
#define foreach_nat_ha_handoff_error
Definition: nat44_ei_ha.c:1192
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105
static uword * vlib_process_wait_for_event(vlib_main_t *vm)
Definition: node_funcs.h:660
ip4_address_t src_address
Definition: ip4_packet.h:125
u8 is_resync
Definition: nat44_ei_ha.c:110
u32 thread_index
void nat_ha_set_node_indexes(nat_ha_main_t *ha, vlib_main_t *vm)
Definition: nat44_ei_ha.c:458
#define PREDICT_TRUE(x)
Definition: clib.h:125
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:119
unsigned long u64
Definition: types.h:89
static void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
Definition: node_funcs.h:249
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 index
Definition: node.h:270
static_always_inline void nat44_ei_ha_sref(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index)
Definition: nat44_ei_ha.c:276
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
u32 outside_fib_index
Definition: nat44_ei.h:353
u32 resync_ack_count
Definition: nat44_ei_ha.c:151
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1572
u32 thread_index
Definition: main.h:213
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:122
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:607
NAT44 endpoint independent plugin declarations.
nat44_ei_session_t * sessions
Definition: nat44_ei.h:283
#define clib_atomic_fetch_sub(a, b)
Definition: atomics.h:24
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:645
void udp_register_dst_port(vlib_main_t *vm, udp_dst_port_t dst_port, u32 node_index, u8 is_ip4)
Definition: udp_local.c:468
static void init_nat_i2o_kv(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s, u32 thread_index, u32 session_index)
static int nat_ha_resend_queue_add(vlib_main_t *vm, u32 seq, u8 *data, u8 data_len, u8 is_resync, u32 vlib_thread_index)
Definition: nat44_ei_ha.c:321
uword ip_csum_t
Definition: ip_packet.h:245
static char * nat_ha_handoff_error_strings[]
Definition: nat44_ei_ha.c:1205
static void vlib_increment_simple_counter(vlib_simple_counter_main_t *cm, u32 thread_index, u32 index, u64 increment)
Increment a simple counter.
Definition: counter.h:74
u16 flags_and_fragment_offset
Definition: ip4_packet.h:106
static u32 nat_value_get_thread_index(clib_bihash_kv_8_8_t *value)
u8 * data
Definition: nat44_ei_ha.c:112
static void nat_ha_resync_fin(void)
Definition: nat44_ei_ha.c:297
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:461
vhost_vring_addr_t addr
Definition: vhost_user.h:130
static_always_inline void nat_ha_event_add(nat_ha_event_t *event, u8 do_flush, u32 session_thread_index, u8 is_resync)
Definition: nat44_ei_ha.c:725
int nat44_ei_set_outside_address_and_port(nat44_ei_address_t *addresses, u32 thread_index, ip4_address_t addr, u16 port, nat_protocol_t protocol)
Definition: nat44_ei.c:983
unsigned char u8
Definition: types.h:56
vlib_buffer_t ** b
u8 data[128]
Definition: ipsec_types.api:92
int nat_ha_set_listener(vlib_main_t *vm, ip4_address_t *addr, u16 port, u32 path_mtu)
Set HA listener (local settings)
Definition: nat44_ei_ha.c:495
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:104
unsigned int u32
Definition: types.h:88
#define clib_memcpy(d, s, n)
Definition: string.h:197
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
Definition: vec.h:689
vlib_frame_t * f
static void init_nat_o2i_kv(clib_bihash_kv_8_8_t *kv, nat44_ei_session_t *s, u32 thread_index, u32 session_index)
format_function_t format_ip4_address
Definition: format.h:73
ip4_address_t dst_ip_address
Definition: nat44_ei_ha.c:138
#define static_always_inline
Definition: clib.h:112
u32 retry_count
Definition: nat44_ei_ha.c:106
static void nat44_ei_user_session_increment(nat44_ei_main_t *nm, nat44_ei_user_t *u, u8 is_static)
nat_ha_main_t nat_ha_main
Definition: nat44_ei_ha.c:171
nat44_ei_main_per_thread_data_t * tnm
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:583
nat44_ei_address_t * addresses
Definition: nat44_ei.h:369
vlib_get_buffers(vm, from, b, n_left_from)
ip4_address_t dst_address
Definition: ip4_packet.h:125
return frame n_vectors
#define nat_elog_notice_X1(_pm, nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
Definition: log.h:186
#define foreach_nat_ha_error
Definition: nat44_ei_ha.c:1015
description fragment has unexpected format
Definition: map.api:433
#define nat_elog_debug_X1(_pm, nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
Definition: log.h:198
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Aggregate type for a prefix.
Definition: fib_types.h:202
#define nat_elog_warn(_pm, nat_elog_str)
Definition: log.h:177
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:184
static u32 nat_value_get_session_index(clib_bihash_kv_8_8_t *value)
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
static u8 * format_nat_ha_handoff_trace(u8 *s, va_list *args)
Definition: nat44_ei_ha.c:1212
A collection of simple counters.
Definition: counter.h:57
#define nat44_ei_is_session_static(sp)
Definition: nat44_ei.h:666
#define VLIB_FRAME_SIZE
Definition: node.h:369
fib_node_index_t fib_table_lookup(u32 fib_index, const fib_prefix_t *prefix)
Perfom a longest prefix match in the non-forwarding table.
Definition: fib_table.c:68
static u32 vlib_get_buffer_index(vlib_main_t *vm, void *p)
Translate buffer pointer into buffer index.
Definition: buffer_funcs.h:324
static u8 * format_nat_ha_trace(u8 *s, va_list *args)
Definition: nat44_ei_ha.c:996
static void nat_ha_resend_scan(vlib_main_t *vm, u32 thread_index)
Definition: nat44_ei_ha.c:371
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
#define NAT44_EI_SESSION_FLAG_STATIC_MAPPING
Definition: nat44_ei.h:60
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:553
void nat_ha_sref(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 total_pkts, u64 total_bytes, u32 thread_index, f64 *last_refreshed, f64 now)
Create session refresh HA event.
Definition: nat44_ei_ha.c:879
f64 retry_timer
Definition: nat44_ei_ha.c:108
static_always_inline void nat_ha_ack_recv(u32 seq, u32 thread_index)
Definition: nat44_ei_ha.c:340
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:1019
vl_api_ip_proto_t proto
Definition: acl_types.api:51
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:708
u32 ha_process_node_index
Definition: nat44_ei_ha.c:163
unsigned short u16
Definition: types.h:57
#define NAT_HA_RETRIES
Definition: nat44_ei_ha.c:28
u8 data_len
Definition: ikev2_types.api:24
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:218
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
#define PREDICT_FALSE(x)
Definition: clib.h:124
#define vec_del1(v, i)
Delete the element at index I.
Definition: vec.h:897
#define NAT_HA_VERSION
Definition: nat44_ei_ha.c:43
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
u32 node_index
Node index.
Definition: node.h:479
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:224
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:395
void nat_ha_enable()
Enable NAT HA.
Definition: nat44_ei_ha.c:443
ip4_address_t addr
Definition: nat44_ei_ha.c:991
nat_ha_per_thread_data_t * per_thread_data
Definition: nat44_ei_ha.c:160
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
u32 fib_entry_get_resolving_interface(fib_node_index_t entry_index)
Definition: fib_entry.c:1474
struct nat_ha_main_s nat_ha_main_t
static void nat_ha_send(vlib_frame_t *f, vlib_buffer_t *b, u8 is_resync, u32 vlib_thread_index)
Definition: nat44_ei_ha.c:697
nat_ha_counter_t
Definition: nat44_ei_ha.c:92
void nat_ha_init(vlib_main_t *vm, u32 num_workers, u32 num_threads)
Initialize NAT HA.
Definition: nat44_ei_ha.c:473
#define skip_if_disabled()
Definition: nat44_ei_ha.c:819
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
nat44_ei_main_per_thread_data_t * per_thread_data
Definition: nat44_ei.h:366
u16 n_vectors
Definition: node.h:388
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:208
u32 ti
static_always_inline void nat_ha_recv_add(nat_ha_event_t *event, f64 now, u32 thread_index)
Definition: nat44_ei_ha.c:566
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
void(* nat_ha_resync_event_cb_t)(u32 client_index, u32 pid, u32 missed_count)
Definition: nat44_ei_ha.h:155
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:395
u8 ttl
Definition: fib_types.api:26
static void nat_ha_header_create(vlib_buffer_t *b, u32 *offset, u32 thread_index)
Definition: nat44_ei_ha.c:652
u8 data[]
Packet data.
Definition: buffer.h:204
static_always_inline u8 plugin_enabled()
Definition: nat44_ei_ha.c:906
static char * nat_ha_error_strings[]
Definition: nat44_ei_ha.c:1027
nat_ha_resend_entry_t * resend_queue
Definition: nat44_ei_ha.c:127
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
8 octet key, 8 octet key value pair
Definition: bihash_8_8.h:41
#define ARRAY_LEN(x)
Definition: clib.h:70
#define nat_elog_info_X1(_pm, nat_elog_fmt_str, nat_elog_fmt_arg, nat_elog_val1)
Definition: log.h:202
u32 seq
Definition: nat44_ei_ha.c:104
static_always_inline void nat_ha_recv_refresh(nat_ha_event_t *event, f64 now, u32 thread_index)
Definition: nat44_ei_ha.c:607
NAT active-passive HA.
vlib_frame_t * state_sync_frame
Definition: nat44_ei_ha.c:121
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:498
static_always_inline void nat_ha_recv_del(nat_ha_event_t *event, u32 thread_index)
Definition: nat44_ei_ha.c:589
u8 value
Definition: qos.api:54
#define ASSERT(truth)
clib_bihash_8_8_t out2in
Definition: nat44_ei.h:362
IPv4 main type.
Definition: ip4.h:107
static_always_inline void nat_ha_event_process(nat_ha_event_t *event, f64 now, u32 thread_index)
Definition: nat44_ei_ha.c:630
vlib_simple_counter_main_t counters[NAT_HA_N_COUNTERS]
Definition: nat44_ei_ha.c:145
void nat_ha_get_failover(ip4_address_t *addr, u16 *port, u32 *session_refresh_interval)
Get HA failover/remote settings.
Definition: nat44_ei_ha.c:555
u16 path_mtu
Definition: ip.api:783
vlib_put_next_frame(vm, node, next_index, 0)
int nat_ha_resync(u32 client_index, u32 pid, nat_ha_resync_event_cb_t event_callback)
Resync HA (resend existing sessions to new failover)
Definition: nat44_ei_ha.c:1283
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:276
char const int length
Definition: cJSON.h:163
static u32 vlib_get_n_threads()
Definition: global_funcs.h:23
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
nat44_ei_hairpin_src_next_t next_index
nat_ha_next_t
Definition: nat44_ei_ha.c:1008
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
void nat_ha_flush(u8 is_resync)
Flush the current HA data (for testing)
Definition: nat44_ei_ha.c:827
#define clib_atomic_fetch_add(a, b)
Definition: atomics.h:23
void nat44_ei_delete_session(nat44_ei_main_t *nm, nat44_ei_session_t *ses, u32 thread_index)
Definition: nat44_ei.c:1753
template key/value backing page structure
Definition: bihash_doc.h:44
Definition: defs.h:47
u32 resync_ack_missed
Definition: nat44_ei_ha.c:153
void nat_ha_sadd(ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, ip4_address_t *ehn_addr, u16 ehn_port, u8 proto, u32 fib_index, u16 flags, u32 thread_index, u8 is_resync)
Create session add HA event.
Definition: nat44_ei_ha.c:834
static vlib_main_t * vlib_get_main_by_index(u32 thread_index)
Definition: global_funcs.h:29
vl_api_address_t ip
Definition: l2.api:558
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
static uword nat_ha_handoff_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: nat44_ei_ha.c:1226
#define foreach_nat_ha_counter
Definition: nat44_ei_ha.c:30
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
vlib_buffer_t * state_sync_buffer
Definition: nat44_ei_ha.c:119
nat_ha_handoff_error_t
Definition: nat44_ei_ha.c:1197
VLIB buffer representation.
Definition: buffer.h:111
nat44_ei_outside_fib_t * outside_fibs
Definition: nat44_ei.h:386
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:295
nat44_ei_main_t nat44_ei_main
Definition: nat44_ei.c:40
void nat_ha_get_resync_status(u8 *in_resync, u32 *resync_ack_missed)
Get resync status.
Definition: nat44_ei_ha.c:981
u16 port
Definition: lb_types.api:73
nat44_ei_session_t * nat44_ei_session_alloc_or_recycle(nat44_ei_main_t *nm, nat44_ei_user_t *u, u32 thread_index, f64 now)
Definition: nat44_ei.c:1180
struct clib_bihash_value offset
template key/value backing page structure
#define vnet_buffer(b)
Definition: buffer.h:437
static uword nat_ha_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: nat44_ei_ha.c:940
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1105
nat44_ei_main_t * nm
NAT port/address allocation lib.
static_always_inline void nat44_ei_ha_sdel(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 thread_index)
Definition: nat44_ei_ha.c:255
clib_bihash_8_8_t in2out
Definition: nat44_ei.h:363
f64 now
static_always_inline void nat44_ei_ha_sadd(ip4_address_t *in_addr, u16 in_port, ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, ip4_address_t *ehn_addr, u16 ehn_port, u8 proto, u32 fib_index, u16 flags, u32 thread_index)
Definition: nat44_ei_ha.c:174
nat_ha_event_type_t
Definition: nat44_ei_ha.c:49
#define vec_foreach(var, vec)
Vector iterator.
#define IP4_HEADER_FLAG_DONT_FRAGMENT
Definition: ip4_packet.h:108
u16 flags
Copy of main node flags.
Definition: node.h:492
void nat_ha_sdel(ip4_address_t *out_addr, u16 out_port, ip4_address_t *eh_addr, u16 eh_port, u8 proto, u32 fib_index, u32 session_thread_index)
Create session delete HA event.
Definition: nat44_ei_ha.c:860
void nat_ha_get_listener(ip4_address_t *addr, u16 *port, u32 *path_mtu)
Get HA listener/local configuration.
Definition: nat44_ei_ha.c:530
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
nat_ha_error_t
Definition: nat44_ei_ha.c:1019
u8 ip_version_and_header_length
Definition: ip4_packet.h:93
u32 session_refresh_interval
Definition: nat44_ei_ha.c:143
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:292
nat_ha_resync_event_cb_t event_callback
Definition: nat44_ei_ha.c:155
u32 ha_worker_node_index
Definition: nat44_ei_ha.c:164
u8 ttl
TTL to use for host generated packets.
Definition: ip4.h:160
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:358
static void init_nat_k(clib_bihash_kv_8_8_t *kv, ip4_address_t addr, u16 port, u32 fib_index, nat_protocol_t proto)
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:111
void nat_ha_disable()
Disable NAT HA.
Definition: nat44_ei_ha.c:450
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
Definition: defs.h:46
nat44_ei_user_t * nat44_ei_user_get_or_create(nat44_ei_main_t *nm, ip4_address_t *addr, u32 fib_index, u32 thread_index)
Definition: nat44_ei.c:1121
u32 state_sync_path_mtu
Definition: nat44_ei_ha.c:141
ip4_address_t src_ip_address
Definition: nat44_ei_ha.c:135
struct ip4_main_t::@372 host_config
Template information for VPP generated packets.