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:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 //#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>
21 #include <nat/lib/log.h>
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>
27 /* number of retries */
28 #define NAT_HA_RETRIES 3
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)
42 /* NAT HA protocol version */
43 #define NAT_HA_VERSION 0x01
45 /* NAT HA protocol flags */
46 #define NAT_HA_FLAG_ACK 0x01
48 /* NAT HA event types */
54 } nat_ha_event_type_t;
56 /* NAT HA protocol header */
67 /* thread index where events originated */
69 } __attribute__ ((packed)) nat_ha_message_header_t;
71 /* NAT HA protocol event data */
90 } __attribute__ ((packed)) nat_ha_event_t;
94 #define _(N, s, v) NAT_HA_COUNTER_##N = v,
95 foreach_nat_ha_counter
100 /* data waiting for ACK */
103 /* sequence number */
107 /* next retry time */
113 } nat_ha_resend_entry_t;
115 /* per thread data */
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;
130 /* NAT HA settings */
131 typedef struct nat_ha_main_s
134 /* local IP address and UDP port */
135 ip4_address_t src_ip_address;
137 /* failvoer IP address and UDP port */
138 ip4_address_t dst_ip_address;
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;
145 vlib_simple_counter_main_t counters[NAT_HA_N_COUNTERS];
146 vlib_main_t *vlib_main;
147 /* sequence number counter */
149 /* 1 if resync in progress */
151 /* number of remaing ACK for resync */
152 u32 resync_ack_count;
153 /* number of missed ACK for resync */
154 u32 resync_ack_missed;
156 nat_ha_resync_event_cb_t event_callback;
159 /* per thread data */
161 nat_ha_per_thread_data_t *per_thread_data;
163 u32 ha_handoff_node_index;
164 u32 ha_process_node_index;
165 u32 ha_worker_node_index;
168 /* worker handoff frame-queue index */
172 nat_ha_main_t nat_ha_main;
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)
180 nat44_ei_main_t *nm = &nat44_ei_main;
181 nat44_ei_main_per_thread_data_t *tnm = &nm->per_thread_data[thread_index];
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;
190 .fp_proto = FIB_PROTOCOL_IP4,
193 .ip4.as_u32 = eh_addr->as_u32,
197 if (!(flags & NAT44_EI_SESSION_FLAG_STATIC_MAPPING))
199 if (nat44_ei_set_outside_address_and_port (nm->addresses, thread_index,
200 *out_addr, out_port, proto))
204 u = nat44_ei_user_get_or_create (nm, in_addr, fib_index, thread_index);
208 s = nat44_ei_session_alloc_or_recycle (nm, u, thread_index, now);
212 s->out2in.addr.as_u32 = out_addr->as_u32;
213 s->out2in.port = out_port;
214 s->nat_proto = proto;
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))
223 s->out2in.fib_index = nm->outside_fib_index;
226 s->out2in.fib_index = nm->outside_fibs[0].fib_index;
229 vec_foreach (outside_fib, nm->outside_fibs)
231 fei = fib_table_lookup (outside_fib->fib_index, &pfx);
232 if (FIB_NODE_INDEX_INVALID != fei)
234 if (fib_entry_get_resolving_interface (fei) != ~0)
236 s->out2in.fib_index = outside_fib->fib_index;
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");
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");
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,
260 nat44_ei_main_t *nm = &nat44_ei_main;
261 clib_bihash_kv_8_8_t kv, value;
263 nat44_ei_session_t *s;
264 nat44_ei_main_per_thread_data_t *tnm;
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]);
271 thread_index = nm->num_workers;
272 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
274 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
275 if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
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);
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)
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;
293 tnm = vec_elt_at_index (nm->per_thread_data, thread_index);
295 init_nat_k (&kv, *out_addr, out_port, fib_index, proto);
296 if (clib_bihash_search_8_8 (&nm->out2in, &kv, &value))
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;
305 nat_ha_resync_fin (void)
307 nat44_ei_main_t *nm = &nat44_ei_main;
308 nat_ha_main_t *ha = &nat_ha_main;
310 /* if no more resync ACK remainig we are done */
311 if (ha->resync_ack_count)
315 if (ha->resync_ack_missed)
317 nat_elog_info (nm, "resync completed with result FAILED");
321 nat_elog_info (nm, "resync completed with result SUCCESS");
323 if (ha->event_callback)
324 ha->event_callback (ha->client_index, ha->pid, ha->resync_ack_missed);
327 /* cache HA NAT data waiting for ACK */
329 nat_ha_resend_queue_add (u32 seq, u8 * data, u8 data_len, u8 is_resync,
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);
337 vec_add2 (td->resend_queue, entry, 1);
338 clib_memset (entry, 0, sizeof (*entry));
339 entry->retry_timer = now + 2.0;
341 entry->is_resync = is_resync;
342 vec_add (entry->data, data, data_len);
347 static_always_inline void
348 nat_ha_ack_recv (u32 seq, u32 thread_index)
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];
355 vec_foreach_index (i, td->resend_queue)
357 if (td->resend_queue[i].seq != seq)
360 vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ACK],
362 /* ACK received remove cached data */
363 if (td->resend_queue[i].is_resync)
365 clib_atomic_fetch_sub (&ha->resync_ack_count, 1);
366 nat_ha_resync_fin ();
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));
377 /* scan non-ACKed HA NAT for retry */
379 nat_ha_resend_scan (f64 now, u32 thread_index)
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;
391 vec_foreach_index (i, td->resend_queue)
393 if (td->resend_queue[i].retry_timer > now)
396 /* maximum retry reached delete cached data */
397 if (td->resend_queue[i].retry_count >= NAT_HA_RETRIES)
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)
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 ();
407 vec_add1 (to_delete, i);
408 vlib_increment_simple_counter (&ha->counters
409 [NAT_HA_COUNTER_MISSED_COUNT],
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],
420 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
422 nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
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);
438 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
439 td->resend_queue[i].retry_timer = now + 2.0;
442 vec_foreach (del, to_delete)
444 vec_free (td->resend_queue[*del].data);
445 vec_del1 (td->resend_queue, *del);
447 vec_free (to_delete);
453 nat_ha_main_t *ha = &nat_ha_main;
460 nat_ha_main_t *ha = &nat_ha_main;
466 nat_ha_set_node_indexes (nat_ha_main_t *ha, vlib_main_t *vm)
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;
481 nat_ha_init (vlib_main_t * vm, u32 num_workers, u32 num_threads)
483 nat_ha_main_t *ha = &nat_ha_main;
484 clib_memset (ha, 0, sizeof (*ha));
486 nat_ha_set_node_indexes (ha, vm);
491 ha->num_workers = num_workers;
492 vec_validate (ha->per_thread_data, num_threads);
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
504 nat_ha_set_listener (ip4_address_t * addr, u16 port, u32 path_mtu)
506 nat44_ei_main_t *nm = &nat44_ei_main;
507 nat_ha_main_t *ha = &nat_ha_main;
509 /* unregister previously set UDP port */
511 udp_unregister_dst_port (ha->vlib_main, ha->src_port, 1);
513 ha->src_ip_address.as_u32 = addr->as_u32;
515 ha->state_sync_path_mtu = path_mtu;
519 /* if multiple worker threads first go to handoff node */
520 if (ha->num_workers > 1)
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);
529 udp_register_dst_port (ha->vlib_main, port, ha->ha_node_index, 1);
531 nat_elog_info_X1 (nm, "HA listening on port %d for state sync", "i4",
539 nat_ha_get_listener (ip4_address_t * addr, u16 * port, u32 * path_mtu)
541 nat_ha_main_t *ha = &nat_ha_main;
543 addr->as_u32 = ha->src_ip_address.as_u32;
544 *port = ha->src_port;
545 *path_mtu = ha->state_sync_path_mtu;
549 nat_ha_set_failover (ip4_address_t * addr, u16 port,
550 u32 session_refresh_interval)
552 nat_ha_main_t *ha = &nat_ha_main;
554 ha->dst_ip_address.as_u32 = addr->as_u32;
556 ha->session_refresh_interval = session_refresh_interval;
558 vlib_process_signal_event (ha->vlib_main, ha->ha_process_node_index, 1, 0);
564 nat_ha_get_failover (ip4_address_t * addr, u16 * port,
565 u32 * session_refresh_interval)
567 nat_ha_main_t *ha = &nat_ha_main;
569 addr->as_u32 = ha->dst_ip_address.as_u32;
570 *port = ha->dst_port;
571 *session_refresh_interval = ha->session_refresh_interval;
574 static_always_inline void
575 nat_ha_recv_add (nat_ha_event_t * event, f64 now, u32 thread_index)
577 nat_ha_main_t *ha = &nat_ha_main;
578 ip4_address_t in_addr, out_addr, eh_addr, ehn_addr;
582 vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_ADD],
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);
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);
597 static_always_inline void
598 nat_ha_recv_del (nat_ha_event_t * event, u32 thread_index)
600 nat_ha_main_t *ha = &nat_ha_main;
601 ip4_address_t out_addr, eh_addr;
604 vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_DEL],
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);
611 nat44_ei_ha_sdel (&out_addr, event->out_port, &eh_addr, event->eh_port,
612 event->protocol, fib_index, thread_index);
615 static_always_inline void
616 nat_ha_recv_refresh (nat_ha_event_t * event, f64 now, u32 thread_index)
618 nat_ha_main_t *ha = &nat_ha_main;
619 ip4_address_t out_addr, eh_addr;
620 u32 fib_index, total_pkts;
623 vlib_increment_simple_counter (&ha->counters[NAT_HA_COUNTER_RECV_REFRESH],
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);
632 nat44_ei_ha_sref (&out_addr, event->out_port, &eh_addr, event->eh_port,
633 event->protocol, fib_index, total_pkts, total_bytes,
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)
641 nat44_ei_main_t *nm = &nat44_ei_main;
642 switch (event->event_type)
645 nat_ha_recv_add (event, now, thread_index);
648 nat_ha_recv_del (event, thread_index);
651 nat_ha_recv_refresh (event, now, thread_index);
654 nat_elog_notice_X1 (nm, "Unsupported HA event type %d", "i4",
661 nat_ha_header_create (vlib_buffer_t * b, u32 * offset, u32 thread_index)
663 nat_ha_main_t *ha = &nat_ha_main;
664 nat_ha_message_header_t *h;
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);
680 ip->ip_version_and_header_length = 0x45;
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;
688 udp->src_port = clib_host_to_net_u16 (ha->src_port);
689 udp->dst_port = clib_host_to_net_u16 (ha->dst_port);
692 /* NAT HA protocol header */
693 h->version = NAT_HA_VERSION;
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);
701 sizeof (ip4_header_t) + sizeof (udp_header_t) +
702 sizeof (nat_ha_message_header_t);
706 nat_ha_send (vlib_frame_t * f, vlib_buffer_t * b, u8 is_resync,
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;
714 vlib_main_t *vm = vlib_mains[thread_index];
716 ip = vlib_buffer_get_current (b);
717 udp = ip4_next_header (ip);
718 h = (nat_ha_message_header_t *) (udp + 1);
720 h->count = clib_host_to_net_u16 (td->state_sync_count);
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));
726 nat_ha_resend_queue_add (h->sequence_number, (u8 *) ip, b->current_length,
727 is_resync, thread_index);
729 vlib_put_frame_to_node (vm, ip4_lookup_node.index, f);
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,
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;
745 b = td->state_sync_buffer;
747 if (PREDICT_FALSE (b == 0))
752 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
754 nat_elog_warn (nm, "HA NAT state sync can't allocate buffer");
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);
765 bi = vlib_get_buffer_index (vm, b);
766 offset = td->state_sync_next_event_offset;
769 f = td->state_sync_frame;
770 if (PREDICT_FALSE (f == 0))
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);
780 if (PREDICT_FALSE (td->state_sync_count == 0))
781 nat_ha_header_create (b, &offset, thread_index);
783 if (PREDICT_TRUE (do_flush == 0))
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);
790 switch (event->event_type)
793 vlib_increment_simple_counter (&ha->counters
794 [NAT_HA_COUNTER_SEND_ADD],
798 vlib_increment_simple_counter (&ha->counters
799 [NAT_HA_COUNTER_SEND_DEL],
803 vlib_increment_simple_counter (&ha->counters
804 [NAT_HA_COUNTER_SEND_REFRESH],
813 (do_flush || offset + (sizeof (*event)) > ha->state_sync_path_mtu))
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;
822 clib_atomic_fetch_add (&ha->resync_ack_count, 1);
823 nat_ha_resync_fin ();
827 td->state_sync_next_event_offset = offset;
830 #define skip_if_disabled() \
832 nat_ha_main_t *ha = &nat_ha_main; \
833 if (PREDICT_TRUE (!ha->dst_port)) \
838 nat_ha_flush (u8 is_resync)
841 nat_ha_event_add (0, 1, 0, is_resync);
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)
850 nat_ha_event_t event;
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);
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)
874 nat_ha_event_t event;
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);
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)
894 nat_ha_main_t *ha = &nat_ha_main;
895 nat_ha_event_t event;
899 if ((*last_refreshed + ha->session_refresh_interval) > now)
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);
916 static_always_inline u8
919 nat_ha_main_t *ha = &nat_ha_main;
923 /* per thread process waiting for interrupt */
925 nat_ha_worker_fn (vlib_main_t * vm, vlib_node_runtime_t * rt,
928 u32 thread_index = vm->thread_index;
930 if (plugin_enabled () == 0)
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);
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",
949 /* periodically send interrupt to each thread */
951 nat_ha_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
953 nat44_ei_main_t *nm = &nat44_ei_main;
954 nat_ha_main_t *ha = &nat_ha_main;
956 uword *event_data = 0;
959 vlib_process_wait_for_event (vm);
960 event_type = vlib_process_get_events (vm, &event_data);
962 nat_elog_info (nm, "nat44-ei-ha-process: bogus kickoff event received");
963 vec_reset_length (event_data);
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++)
972 if (ti >= vec_len (ha->per_thread_data))
975 vlib_node_set_interrupt_pending (vlib_mains[ti],
976 nat_ha_worker_node.index);
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",
992 nat_ha_get_resync_status (u8 * in_resync, u32 * resync_ack_missed)
994 nat_ha_main_t *ha = &nat_ha_main;
996 *in_resync = ha->in_resync;
997 *resync_ack_missed = ha->resync_ack_missed;
1007 format_nat_ha_trace (u8 * s, va_list * args)
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 *);
1013 s = format (s, "nat44-ei-ha: %u events from %U", t->event_count,
1014 format_ip4_address, &t->addr);
1021 NAT_HA_NEXT_IP4_LOOKUP,
1026 #define foreach_nat_ha_error \
1027 _(PROCESSED, "pkts-processed") \
1028 _(BAD_VERSION, "bad-version")
1032 #define _(sym, str) NAT_HA_ERROR_##sym,
1033 foreach_nat_ha_error
1038 static char *nat_ha_error_strings[] = {
1039 #define _(sym, str) str,
1040 foreach_nat_ha_error
1044 /* process received HA NAT protocol messages */
1046 nat_ha_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1047 vlib_frame_t * frame)
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;
1057 from = vlib_frame_vector_args (frame);
1058 n_left_from = frame->n_vectors;
1059 next_index = node->cached_next_index;
1061 while (n_left_from > 0)
1065 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
1067 while (n_left_from > 0 && n_left_to_next > 0)
1069 u32 bi0, next0, src_addr0, dst_addr0;;
1071 nat_ha_message_header_t *h0;
1073 u16 event_count0, src_port0, dst_port0, old_len0;
1083 n_left_to_next -= 1;
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);
1092 next0 = NAT_HA_NEXT_DROP;
1094 if (h0->version != NAT_HA_VERSION)
1096 b0->error = node->errors[NAT_HA_ERROR_BAD_VERSION];
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))
1104 nat_ha_ack_recv (h0->sequence_number, thread_index);
1105 b0->error = node->errors[NAT_HA_ERROR_PROCESSED];
1109 e0 = (nat_ha_event_t *) (h0 + 1);
1111 /* process each event */
1112 while (event_count0)
1114 nat_ha_event_process (e0, now, thread_index);
1116 e0 = (nat_ha_event_t *) ((u8 *) e0 + sizeof (nat_ha_event_t));
1119 next0 = NAT_HA_NEXT_IP4_LOOKUP;
1122 /* reply with ACK */
1123 b0->current_length = sizeof (*ip0) + sizeof (*udp0) + sizeof (*h0);
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);
1132 sum0 = ip0->checksum;
1133 sum0 = ip_csum_update (sum0, ip0->ttl, host_config_ttl,
1135 ip0->ttl = host_config_ttl;
1137 ip_csum_update (sum0, old_len0, ip0->length, ip4_header_t,
1139 ip0->checksum = ip_csum_fold (sum0);
1142 src_port0 = udp0->src_port;
1143 dst_port0 = udp0->dst_port;
1144 udp0->src_port = dst_port0;
1145 udp0->dst_port = src_port0;
1147 clib_host_to_net_u16 (b0->current_length - sizeof (*ip0));
1149 h0->flags = NAT_HA_FLAG_ACK;
1151 vlib_increment_simple_counter (&ha->counters
1152 [NAT_HA_COUNTER_SEND_ACK],
1153 thread_index, 0, 1);
1156 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1157 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
1159 nat_ha_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
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;
1166 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
1167 to_next, n_left_to_next,
1171 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
1174 vlib_node_increment_counter (vm, ha->ha_node_index, NAT_HA_ERROR_PROCESSED,
1177 return frame->n_vectors;
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,
1191 [NAT_HA_NEXT_IP4_LOOKUP] = "ip4-lookup",
1192 [NAT_HA_NEXT_DROP] = "error-drop",
1199 u32 next_worker_index;
1201 } nat_ha_handoff_trace_t;
1203 #define foreach_nat_ha_handoff_error \
1204 _(CONGESTION_DROP, "congestion drop") \
1205 _(SAME_WORKER, "same worker") \
1206 _(DO_HANDOFF, "do handoff")
1210 #define _(sym,str) NAT_HA_HANDOFF_ERROR_##sym,
1211 foreach_nat_ha_handoff_error
1213 NAT_HA_HANDOFF_N_ERROR,
1214 } nat_ha_handoff_error_t;
1216 static char *nat_ha_handoff_error_strings[] = {
1217 #define _(sym,string) string,
1218 foreach_nat_ha_handoff_error
1223 format_nat_ha_handoff_trace (u8 * s, va_list * args)
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 *);
1230 format (s, "NAT_HA_WORKER_HANDOFF: next-worker %d", t->next_worker_index);
1235 /* do worker handoff based on thread_index in NAT HA protcol header */
1237 nat_ha_handoff_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
1238 vlib_frame_t * frame)
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;
1247 from = vlib_frame_vector_args (frame);
1248 n_left_from = frame->n_vectors;
1249 vlib_get_buffers (vm, from, bufs, n_left_from);
1252 ti = thread_indices;
1254 while (n_left_from > 0)
1256 nat_ha_message_header_t *h0;
1258 h0 = vlib_buffer_get_current (b[0]);
1259 ti[0] = clib_net_to_host_u32 (h0->thread_index);
1261 if (ti[0] != thread_index)
1266 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
1267 && (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
1269 nat_ha_handoff_trace_t *t =
1270 vlib_add_trace (vm, node, b[0], sizeof (*t));
1271 t->next_worker_index = ti[0];
1280 vlib_buffer_enqueue_to_thread (vm, ha->fq_index, from, thread_indices,
1281 frame->n_vectors, 1);
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;
1295 nat_ha_resync (u32 client_index, u32 pid,
1296 nat_ha_resync_event_cb_t event_callback)
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,
1318 * fd.io coding-style-patch-verification: ON
1321 * eval: (c-set-style "gnu")