nat: report time between current vpp time and last_heard
[vpp.git] / src / plugins / nat / nat44-ei / nat44_ei_api.c
1 /*
2  * Copyright (c) 2020 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <vnet/ip/ip_types_api.h>
18 #include <vlibmemory/api.h>
19
20 #include <vnet/fib/fib_table.h>
21
22 #include <nat/lib/nat_inlines.h>
23 #include <nat/lib/ipfix_logging.h>
24
25 #include <nat/nat44-ei/nat44_ei.api_enum.h>
26 #include <nat/nat44-ei/nat44_ei.api_types.h>
27
28 #include <nat/nat44-ei/nat44_ei_ha.h>
29 #include <nat/nat44-ei/nat44_ei.h>
30
31 #define REPLY_MSG_ID_BASE nm->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
33
34 static void
35 vl_api_nat44_ei_show_running_config_t_handler (
36   vl_api_nat44_ei_show_running_config_t *mp)
37 {
38   vl_api_nat44_ei_show_running_config_reply_t *rmp;
39   nat44_ei_main_t *nm = &nat44_ei_main;
40   nat44_ei_config_t *rc = &nm->rconfig;
41   int rv = 0;
42
43   REPLY_MACRO2_ZERO (
44     VL_API_NAT44_EI_SHOW_RUNNING_CONFIG_REPLY, ({
45       rmp->inside_vrf = htonl (rc->inside_vrf);
46       rmp->outside_vrf = htonl (rc->outside_vrf);
47       rmp->users = htonl (rc->users);
48       rmp->sessions = htonl (rc->sessions);
49       rmp->user_sessions = htonl (rc->user_sessions);
50
51       rmp->user_buckets = htonl (nm->user_buckets);
52       rmp->translation_buckets = htonl (nm->translation_buckets);
53
54       rmp->timeouts.udp = htonl (nm->timeouts.udp);
55       rmp->timeouts.tcp_established = htonl (nm->timeouts.tcp.established);
56       rmp->timeouts.tcp_transitory = htonl (nm->timeouts.tcp.transitory);
57       rmp->timeouts.icmp = htonl (nm->timeouts.icmp);
58
59       rmp->forwarding_enabled = nm->forwarding_enabled == 1;
60       // consider how to split functionality between subplugins
61       rmp->ipfix_logging_enabled = nat_ipfix_logging_enabled ();
62
63       if (rc->static_mapping_only)
64         rmp->flags |= NAT44_EI_STATIC_MAPPING_ONLY;
65       if (rc->connection_tracking)
66         rmp->flags |= NAT44_EI_CONNECTION_TRACKING;
67       if (rc->out2in_dpo)
68         rmp->flags |= NAT44_EI_OUT2IN_DPO;
69     }));
70 }
71
72 static void
73 vl_api_nat44_ei_set_workers_t_handler (vl_api_nat44_ei_set_workers_t *mp)
74 {
75   nat44_ei_main_t *nm = &nat44_ei_main;
76   vl_api_nat44_ei_set_workers_reply_t *rmp;
77   int rv = 0;
78   uword *bitmap = 0;
79   u64 mask;
80
81   mask = clib_net_to_host_u64 (mp->worker_mask);
82
83   if (nm->num_workers < 2)
84     {
85       rv = VNET_API_ERROR_FEATURE_DISABLED;
86       goto send_reply;
87     }
88
89   bitmap = clib_bitmap_set_multiple (bitmap, 0, mask, BITS (mask));
90   rv = nat44_ei_set_workers (bitmap);
91   clib_bitmap_free (bitmap);
92
93 send_reply:
94   REPLY_MACRO (VL_API_NAT44_EI_SET_WORKERS_REPLY);
95 }
96
97 static void
98 send_nat_worker_details (u32 worker_index, vl_api_registration_t *reg,
99                          u32 context)
100 {
101   vl_api_nat44_ei_worker_details_t *rmp;
102   nat44_ei_main_t *nm = &nat44_ei_main;
103   vlib_worker_thread_t *w =
104     vlib_worker_threads + worker_index + nm->first_worker_index;
105
106   rmp = vl_msg_api_alloc (sizeof (*rmp));
107   clib_memset (rmp, 0, sizeof (*rmp));
108   rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_WORKER_DETAILS + nm->msg_id_base);
109   rmp->context = context;
110   rmp->worker_index = htonl (worker_index);
111   rmp->lcore_id = htonl (w->cpu_id);
112   strncpy ((char *) rmp->name, (char *) w->name, ARRAY_LEN (rmp->name) - 1);
113
114   vl_api_send_msg (reg, (u8 *) rmp);
115 }
116
117 static void
118 vl_api_nat44_ei_worker_dump_t_handler (vl_api_nat44_ei_worker_dump_t *mp)
119 {
120   vl_api_registration_t *reg;
121   nat44_ei_main_t *nm = &nat44_ei_main;
122   u32 *worker_index;
123
124   reg = vl_api_client_index_to_registration (mp->client_index);
125   if (!reg)
126     return;
127
128   vec_foreach (worker_index, nm->workers)
129     {
130       send_nat_worker_details (*worker_index, reg, mp->context);
131     }
132 }
133
134 static void
135 vl_api_nat44_ei_set_log_level_t_handler (vl_api_nat44_ei_set_log_level_t *mp)
136 {
137   nat44_ei_main_t *nm = &nat44_ei_main;
138   vl_api_nat44_ei_set_log_level_reply_t *rmp;
139   int rv = 0;
140
141   if (nm->log_level > NAT_LOG_DEBUG)
142     rv = VNET_API_ERROR_UNSUPPORTED;
143   else
144     nm->log_level = mp->log_level;
145
146   REPLY_MACRO (VL_API_NAT44_EI_SET_LOG_LEVEL_REPLY);
147 }
148
149 static void
150 vl_api_nat44_ei_plugin_enable_disable_t_handler (
151   vl_api_nat44_ei_plugin_enable_disable_t *mp)
152 {
153   nat44_ei_main_t *nm = &nat44_ei_main;
154   nat44_ei_config_t c = { 0 };
155   vl_api_nat44_ei_plugin_enable_disable_reply_t *rmp;
156   int rv = 0;
157
158   if (mp->enable)
159     {
160       c.static_mapping_only = mp->flags & NAT44_EI_STATIC_MAPPING_ONLY;
161       c.connection_tracking = mp->flags & NAT44_EI_CONNECTION_TRACKING;
162       c.out2in_dpo = mp->flags & NAT44_EI_OUT2IN_DPO;
163
164       c.inside_vrf = ntohl (mp->inside_vrf);
165       c.outside_vrf = ntohl (mp->outside_vrf);
166
167       c.users = ntohl (mp->users);
168
169       c.sessions = ntohl (mp->sessions);
170
171       c.user_sessions = ntohl (mp->user_sessions);
172
173       rv = nat44_ei_plugin_enable (c);
174     }
175   else
176     rv = nat44_ei_plugin_disable ();
177
178   REPLY_MACRO (VL_API_NAT44_EI_PLUGIN_ENABLE_DISABLE_REPLY);
179 }
180
181 static void
182 vl_api_nat44_ei_ipfix_enable_disable_t_handler (
183   vl_api_nat44_ei_ipfix_enable_disable_t *mp)
184 {
185   nat44_ei_main_t *nm = &nat44_ei_main;
186   vl_api_nat44_ei_ipfix_enable_disable_reply_t *rmp;
187   int rv = 0;
188
189   rv = nat_ipfix_logging_enable_disable (mp->enable,
190                                          clib_host_to_net_u32 (mp->domain_id),
191                                          clib_host_to_net_u16 (mp->src_port));
192
193   REPLY_MACRO (VL_API_NAT44_EI_IPFIX_ENABLE_DISABLE_REPLY);
194 }
195
196 static void
197 vl_api_nat44_ei_set_timeouts_t_handler (vl_api_nat44_ei_set_timeouts_t *mp)
198 {
199   nat44_ei_main_t *nm = &nat44_ei_main;
200   vl_api_nat44_ei_set_timeouts_reply_t *rmp;
201   int rv = 0;
202
203   nm->timeouts.udp = ntohl (mp->udp);
204   nm->timeouts.tcp.established = ntohl (mp->tcp_established);
205   nm->timeouts.tcp.transitory = ntohl (mp->tcp_transitory);
206   nm->timeouts.icmp = ntohl (mp->icmp);
207
208   REPLY_MACRO (VL_API_NAT44_EI_SET_TIMEOUTS_REPLY);
209 }
210
211 static void
212 vl_api_nat44_ei_set_addr_and_port_alloc_alg_t_handler (
213   vl_api_nat44_ei_set_addr_and_port_alloc_alg_t *mp)
214 {
215   nat44_ei_main_t *nm = &nat44_ei_main;
216   vl_api_nat44_ei_set_addr_and_port_alloc_alg_reply_t *rmp;
217   int rv = 0;
218   u16 port_start, port_end;
219
220   switch (mp->alg)
221     {
222     case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_DEFAULT:
223       nat44_ei_set_alloc_default ();
224       break;
225     case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_MAPE:
226       nat44_ei_set_alloc_mape (ntohs (mp->psid), mp->psid_offset,
227                                mp->psid_length);
228       break;
229     case NAT44_EI_ADDR_AND_PORT_ALLOC_ALG_RANGE:
230       port_start = ntohs (mp->start_port);
231       port_end = ntohs (mp->end_port);
232       if (port_end <= port_start)
233         {
234           rv = VNET_API_ERROR_INVALID_VALUE;
235           goto send_reply;
236         }
237       nat44_ei_set_alloc_range (port_start, port_end);
238       break;
239     default:
240       rv = VNET_API_ERROR_INVALID_VALUE;
241       break;
242     }
243
244 send_reply:
245   REPLY_MACRO (VL_API_NAT44_EI_SET_ADDR_AND_PORT_ALLOC_ALG_REPLY);
246 }
247
248 static void
249 vl_api_nat44_ei_get_addr_and_port_alloc_alg_t_handler (
250   vl_api_nat44_ei_get_addr_and_port_alloc_alg_t *mp)
251 {
252   nat44_ei_main_t *nm = &nat44_ei_main;
253   vl_api_nat44_ei_get_addr_and_port_alloc_alg_reply_t *rmp;
254   int rv = 0;
255
256   REPLY_MACRO2 (VL_API_NAT44_EI_GET_ADDR_AND_PORT_ALLOC_ALG_REPLY, ({
257                   rmp->alg = nm->addr_and_port_alloc_alg;
258                   rmp->psid_offset = nm->psid_offset;
259                   rmp->psid_length = nm->psid_length;
260                   rmp->psid = htons (nm->psid);
261                   rmp->start_port = htons (nm->start_port);
262                   rmp->end_port = htons (nm->end_port);
263                 }))
264 }
265
266 static void
267 vl_api_nat44_ei_set_mss_clamping_t_handler (
268   vl_api_nat44_ei_set_mss_clamping_t *mp)
269 {
270   nat44_ei_main_t *nm = &nat44_ei_main;
271   vl_api_nat44_ei_set_mss_clamping_reply_t *rmp;
272   int rv = 0;
273
274   if (mp->enable)
275     nm->mss_clamping = ntohs (mp->mss_value);
276   else
277     nm->mss_clamping = 0;
278
279   REPLY_MACRO (VL_API_NAT44_EI_SET_MSS_CLAMPING_REPLY);
280 }
281
282 static void
283 vl_api_nat44_ei_get_mss_clamping_t_handler (
284   vl_api_nat44_ei_get_mss_clamping_t *mp)
285 {
286   nat44_ei_main_t *nm = &nat44_ei_main;
287   vl_api_nat44_ei_get_mss_clamping_reply_t *rmp;
288   int rv = 0;
289
290   REPLY_MACRO2 (VL_API_NAT44_EI_GET_MSS_CLAMPING_REPLY, ({
291                   rmp->enable = nm->mss_clamping ? 1 : 0;
292                   rmp->mss_value = htons (nm->mss_clamping);
293                 }))
294 }
295
296 static void
297 vl_api_nat44_ei_ha_set_listener_t_handler (
298   vl_api_nat44_ei_ha_set_listener_t *mp)
299 {
300   nat44_ei_main_t *nm = &nat44_ei_main;
301   vl_api_nat44_ei_ha_set_listener_reply_t *rmp;
302   ip4_address_t addr;
303   int rv;
304
305   memcpy (&addr, &mp->ip_address, sizeof (addr));
306   rv = nat_ha_set_listener (vlib_get_main (), &addr,
307                             clib_net_to_host_u16 (mp->port),
308                             clib_net_to_host_u32 (mp->path_mtu));
309
310   REPLY_MACRO (VL_API_NAT44_EI_HA_SET_LISTENER_REPLY);
311 }
312
313 static void
314 vl_api_nat44_ei_ha_get_listener_t_handler (
315   vl_api_nat44_ei_ha_get_listener_t *mp)
316 {
317   nat44_ei_main_t *nm = &nat44_ei_main;
318   vl_api_nat44_ei_ha_get_listener_reply_t *rmp;
319   int rv = 0;
320   ip4_address_t addr;
321   u16 port;
322   u32 path_mtu;
323
324   nat_ha_get_listener (&addr, &port, &path_mtu);
325
326   REPLY_MACRO2 (VL_API_NAT44_EI_HA_GET_LISTENER_REPLY, ({
327                   clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
328                   rmp->port = clib_host_to_net_u16 (port);
329                   rmp->path_mtu = clib_host_to_net_u32 (path_mtu);
330                 }))
331 }
332
333 static void
334 vl_api_nat44_ei_ha_set_failover_t_handler (
335   vl_api_nat44_ei_ha_set_failover_t *mp)
336 {
337   nat44_ei_main_t *nm = &nat44_ei_main;
338   vl_api_nat44_ei_ha_set_failover_reply_t *rmp;
339   ip4_address_t addr;
340   int rv;
341
342   memcpy (&addr, &mp->ip_address, sizeof (addr));
343   rv = nat_ha_set_failover (
344     vlib_get_main (), &addr, clib_net_to_host_u16 (mp->port),
345     clib_net_to_host_u32 (mp->session_refresh_interval));
346
347   REPLY_MACRO (VL_API_NAT44_EI_HA_SET_FAILOVER_REPLY);
348 }
349
350 static void
351 vl_api_nat44_ei_ha_get_failover_t_handler (
352   vl_api_nat44_ei_ha_get_failover_t *mp)
353 {
354   nat44_ei_main_t *nm = &nat44_ei_main;
355   vl_api_nat44_ei_ha_get_failover_reply_t *rmp;
356   int rv = 0;
357   ip4_address_t addr;
358   u16 port;
359   u32 session_refresh_interval;
360
361   nat_ha_get_failover (&addr, &port, &session_refresh_interval);
362
363   REPLY_MACRO2 (VL_API_NAT44_EI_HA_GET_FAILOVER_REPLY, ({
364                   clib_memcpy (rmp->ip_address, &addr, sizeof (ip4_address_t));
365                   rmp->port = clib_host_to_net_u16 (port);
366                   rmp->session_refresh_interval =
367                     clib_host_to_net_u32 (session_refresh_interval);
368                 }))
369 }
370
371 static void
372 vl_api_nat44_ei_ha_flush_t_handler (vl_api_nat44_ei_ha_flush_t *mp)
373 {
374   nat44_ei_main_t *nm = &nat44_ei_main;
375   vl_api_nat44_ei_ha_flush_reply_t *rmp;
376   int rv = 0;
377
378   nat_ha_flush (0);
379
380   REPLY_MACRO (VL_API_NAT44_EI_HA_FLUSH_REPLY);
381 }
382
383 static void
384 nat_ha_resync_completed_event_cb (u32 client_index, u32 pid, u32 missed_count)
385 {
386   nat44_ei_main_t *nm = &nat44_ei_main;
387   vl_api_registration_t *reg;
388   vl_api_nat44_ei_ha_resync_completed_event_t *mp;
389
390   reg = vl_api_client_index_to_registration (client_index);
391   if (!reg)
392     return;
393
394   mp = vl_msg_api_alloc (sizeof (*mp));
395   clib_memset (mp, 0, sizeof (*mp));
396   mp->client_index = client_index;
397   mp->pid = pid;
398   mp->missed_count = clib_host_to_net_u32 (missed_count);
399   mp->_vl_msg_id =
400     ntohs (VL_API_NAT44_EI_HA_RESYNC_COMPLETED_EVENT + nm->msg_id_base);
401
402   vl_api_send_msg (reg, (u8 *) mp);
403 }
404
405 static void
406 vl_api_nat44_ei_ha_resync_t_handler (vl_api_nat44_ei_ha_resync_t *mp)
407 {
408   nat44_ei_main_t *nm = &nat44_ei_main;
409   vl_api_nat44_ei_ha_resync_reply_t *rmp;
410   int rv;
411
412   rv = nat_ha_resync (
413     mp->client_index, mp->pid,
414     mp->want_resync_event ? nat_ha_resync_completed_event_cb : NULL);
415
416   REPLY_MACRO (VL_API_NAT44_EI_HA_RESYNC_REPLY);
417 }
418
419 static void
420 vl_api_nat44_ei_del_user_t_handler (vl_api_nat44_ei_del_user_t *mp)
421 {
422   nat44_ei_main_t *nm = &nat44_ei_main;
423   vl_api_nat44_ei_del_user_reply_t *rmp;
424   ip4_address_t addr;
425   int rv;
426   memcpy (&addr.as_u8, mp->ip_address, 4);
427   rv = nat44_ei_user_del (&addr, ntohl (mp->fib_index));
428   REPLY_MACRO (VL_API_NAT44_EI_DEL_USER_REPLY);
429 }
430
431 static void
432 vl_api_nat44_ei_add_del_address_range_t_handler (
433   vl_api_nat44_ei_add_del_address_range_t *mp)
434 {
435   nat44_ei_main_t *nm = &nat44_ei_main;
436   vl_api_nat44_ei_add_del_address_range_reply_t *rmp;
437   ip4_address_t this_addr;
438   u8 is_add;
439   u32 start_host_order, end_host_order;
440   u32 vrf_id;
441   int i, count;
442   int rv = 0;
443   u32 *tmp;
444
445   if (nm->static_mapping_only)
446     {
447       rv = VNET_API_ERROR_FEATURE_DISABLED;
448       goto send_reply;
449     }
450
451   is_add = mp->is_add;
452
453   tmp = (u32 *) mp->first_ip_address;
454   start_host_order = clib_host_to_net_u32 (tmp[0]);
455   tmp = (u32 *) mp->last_ip_address;
456   end_host_order = clib_host_to_net_u32 (tmp[0]);
457
458   count = (end_host_order - start_host_order) + 1;
459
460   vrf_id = clib_host_to_net_u32 (mp->vrf_id);
461
462   if (count > 1024)
463     nat44_ei_log_info ("%U - %U, %d addresses...", format_ip4_address,
464                        mp->first_ip_address, format_ip4_address,
465                        mp->last_ip_address, count);
466
467   memcpy (&this_addr.as_u8, mp->first_ip_address, 4);
468
469   for (i = 0; i < count; i++)
470     {
471       if (is_add)
472         rv = nat44_ei_add_address (&this_addr, vrf_id);
473       else
474         rv = nat44_ei_del_address (this_addr, 0);
475
476       if (rv)
477         goto send_reply;
478
479       if (nm->out2in_dpo)
480         nat44_ei_add_del_address_dpo (this_addr, is_add);
481
482       increment_v4_address (&this_addr);
483     }
484
485 send_reply:
486   REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_ADDRESS_RANGE_REPLY);
487 }
488
489 static void
490 send_nat44_ei_address_details (nat44_ei_address_t *a,
491                                vl_api_registration_t *reg, u32 context)
492 {
493   vl_api_nat44_ei_address_details_t *rmp;
494   nat44_ei_main_t *nm = &nat44_ei_main;
495
496   rmp = vl_msg_api_alloc (sizeof (*rmp));
497   clib_memset (rmp, 0, sizeof (*rmp));
498   rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_ADDRESS_DETAILS + nm->msg_id_base);
499   clib_memcpy (rmp->ip_address, &(a->addr), 4);
500   if (a->fib_index != ~0)
501     {
502       fib_table_t *fib = fib_table_get (a->fib_index, FIB_PROTOCOL_IP4);
503       rmp->vrf_id = ntohl (fib->ft_table_id);
504     }
505   else
506     rmp->vrf_id = ~0;
507   rmp->context = context;
508
509   vl_api_send_msg (reg, (u8 *) rmp);
510 }
511
512 static void
513 vl_api_nat44_ei_address_dump_t_handler (vl_api_nat44_ei_address_dump_t *mp)
514 {
515   vl_api_registration_t *reg;
516   nat44_ei_main_t *nm = &nat44_ei_main;
517   nat44_ei_address_t *a;
518
519   reg = vl_api_client_index_to_registration (mp->client_index);
520   if (!reg)
521     return;
522
523   vec_foreach (a, nm->addresses)
524     {
525       send_nat44_ei_address_details (a, reg, mp->context);
526     }
527 }
528
529 static void
530 vl_api_nat44_ei_interface_add_del_feature_t_handler (
531   vl_api_nat44_ei_interface_add_del_feature_t *mp)
532 {
533   nat44_ei_main_t *nm = &nat44_ei_main;
534   vl_api_nat44_ei_interface_add_del_feature_reply_t *rmp;
535   u32 sw_if_index = ntohl (mp->sw_if_index);
536   int rv = 0;
537
538   VALIDATE_SW_IF_INDEX (mp);
539
540   if (mp->is_add)
541     {
542       rv =
543         nat44_ei_add_interface (sw_if_index, mp->flags & NAT44_EI_IF_INSIDE);
544     }
545   else
546     {
547       rv =
548         nat44_ei_del_interface (sw_if_index, mp->flags & NAT44_EI_IF_INSIDE);
549     }
550
551   BAD_SW_IF_INDEX_LABEL;
552   REPLY_MACRO (VL_API_NAT44_EI_INTERFACE_ADD_DEL_FEATURE_REPLY);
553 }
554
555 static void
556 send_nat44_ei_interface_details (nat44_ei_interface_t *i,
557                                  vl_api_registration_t *reg, u32 context)
558 {
559   vl_api_nat44_ei_interface_details_t *rmp;
560   nat44_ei_main_t *nm = &nat44_ei_main;
561
562   rmp = vl_msg_api_alloc (sizeof (*rmp));
563   clib_memset (rmp, 0, sizeof (*rmp));
564   rmp->_vl_msg_id =
565     ntohs (VL_API_NAT44_EI_INTERFACE_DETAILS + nm->msg_id_base);
566   rmp->sw_if_index = ntohl (i->sw_if_index);
567
568   if (nat44_ei_interface_is_inside (i))
569     rmp->flags |= NAT44_EI_IF_INSIDE;
570   if (nat44_ei_interface_is_outside (i))
571     rmp->flags |= NAT44_EI_IF_OUTSIDE;
572
573   rmp->context = context;
574
575   vl_api_send_msg (reg, (u8 *) rmp);
576 }
577
578 static void
579 vl_api_nat44_ei_interface_dump_t_handler (vl_api_nat44_ei_interface_dump_t *mp)
580 {
581   vl_api_registration_t *reg;
582   nat44_ei_main_t *nm = &nat44_ei_main;
583   nat44_ei_interface_t *i;
584
585   reg = vl_api_client_index_to_registration (mp->client_index);
586   if (!reg)
587     return;
588
589   pool_foreach (i, nm->interfaces)
590     {
591       send_nat44_ei_interface_details (i, reg, mp->context);
592     }
593 }
594
595 static_always_inline int
596 add_del_dummy_output_interface (u32 sw_if_index, u8 is_inside, u8 is_add)
597 {
598   nat44_ei_main_t *nm = &nat44_ei_main;
599   nat44_ei_interface_t *i;
600   int rv = 1;
601
602   pool_foreach (i, nm->output_feature_dummy_interfaces)
603     {
604       if (i->sw_if_index == sw_if_index)
605         {
606           if (!is_add)
607             {
608               pool_put (nm->output_feature_dummy_interfaces, i);
609               rv = 0;
610             }
611           goto done;
612         }
613     }
614
615   if (is_add)
616     {
617       pool_get (nm->output_feature_dummy_interfaces, i);
618       i->sw_if_index = sw_if_index;
619
620       if (is_inside)
621         {
622           i->flags |= NAT44_EI_INTERFACE_FLAG_IS_INSIDE;
623         }
624       else
625         {
626           i->flags |= NAT44_EI_INTERFACE_FLAG_IS_OUTSIDE;
627         }
628
629       rv = 0;
630     }
631
632 done:
633   return rv;
634 }
635
636 static void
637 vl_api_nat44_ei_interface_add_del_output_feature_t_handler (
638   vl_api_nat44_ei_interface_add_del_output_feature_t *mp)
639 {
640   vl_api_nat44_ei_interface_add_del_output_feature_reply_t *rmp;
641   nat44_ei_main_t *nm = &nat44_ei_main;
642   u32 sw_if_index;
643   int rv = 0;
644
645   VALIDATE_SW_IF_INDEX (mp);
646
647   sw_if_index = ntohl (mp->sw_if_index);
648
649   // register all interfaces in the dummy structure
650   rv = add_del_dummy_output_interface (
651     sw_if_index, mp->flags & NAT44_EI_IF_INSIDE, mp->is_add);
652
653   if (!(mp->flags & NAT44_EI_IF_INSIDE))
654     {
655       if (mp->is_add)
656         {
657           rv = nat44_ei_add_output_interface (sw_if_index);
658         }
659       else
660         {
661           rv = nat44_ei_del_output_interface (sw_if_index);
662         }
663     }
664
665   BAD_SW_IF_INDEX_LABEL;
666   REPLY_MACRO (VL_API_NAT44_EI_INTERFACE_ADD_DEL_OUTPUT_FEATURE_REPLY);
667 }
668
669 static void
670 send_nat44_ei_interface_output_feature_details (nat44_ei_interface_t *i,
671                                                 vl_api_registration_t *reg,
672                                                 u32 context)
673 {
674   vl_api_nat44_ei_interface_output_feature_details_t *rmp;
675   nat44_ei_main_t *nm = &nat44_ei_main;
676
677   rmp = vl_msg_api_alloc (sizeof (*rmp));
678   clib_memset (rmp, 0, sizeof (*rmp));
679   rmp->_vl_msg_id =
680     ntohs (VL_API_NAT44_EI_INTERFACE_OUTPUT_FEATURE_DETAILS + nm->msg_id_base);
681   rmp->sw_if_index = ntohl (i->sw_if_index);
682   rmp->context = context;
683
684   if (nat44_ei_interface_is_inside (i))
685     {
686       rmp->flags |= NAT44_EI_IF_INSIDE;
687     }
688
689   vl_api_send_msg (reg, (u8 *) rmp);
690 }
691
692 static void
693 vl_api_nat44_ei_interface_output_feature_dump_t_handler (
694   vl_api_nat44_ei_interface_output_feature_dump_t *mp)
695 {
696   vl_api_registration_t *reg;
697   nat44_ei_main_t *nm = &nat44_ei_main;
698   nat44_ei_interface_t *i;
699
700   reg = vl_api_client_index_to_registration (mp->client_index);
701   if (!reg)
702     return;
703
704   pool_foreach (i, nm->output_feature_dummy_interfaces)
705     {
706       send_nat44_ei_interface_output_feature_details (i, reg, mp->context);
707     }
708 }
709
710 static void
711 vl_api_nat44_ei_add_del_output_interface_t_handler (
712   vl_api_nat44_ei_add_del_output_interface_t *mp)
713 {
714   vl_api_nat44_ei_add_del_output_interface_reply_t *rmp;
715   nat44_ei_main_t *nm = &nat44_ei_main;
716   int rv = 0;
717
718   VALIDATE_SW_IF_INDEX_END (mp);
719
720   if (mp->is_add)
721     {
722       rv = nat44_ei_add_output_interface (mp->sw_if_index);
723     }
724   else
725     {
726       rv = nat44_ei_del_output_interface (mp->sw_if_index);
727     }
728
729 bad_sw_if_index:
730   REPLY_MACRO_END (VL_API_NAT44_EI_ADD_DEL_OUTPUT_INTERFACE_REPLY);
731 }
732
733 #define vl_endianfun
734 #include <nat/nat44-ei/nat44_ei.api.h>
735 #undef vl_endianfun
736 static void
737 send_nat44_ei_output_interface_details (u32 index, vl_api_registration_t *rp,
738                                         u32 context)
739 {
740   nat44_ei_main_t *nm = &nat44_ei_main;
741   vl_api_nat44_ei_output_interface_details_t *rmp;
742   nat44_ei_interface_t *i =
743     pool_elt_at_index (nm->output_feature_interfaces, index);
744
745   /* Make sure every field is initiated (or don't skip the clib_memset()) */
746   REPLY_MACRO_DETAILS4 (
747     VL_API_NAT44_EI_OUTPUT_INTERFACE_DETAILS, rp, context, ({
748       rmp->sw_if_index = i->sw_if_index;
749
750       /* Endian hack until apigen registers _details
751        * endian functions */
752       vl_api_nat44_ei_output_interface_details_t_endian (rmp);
753       rmp->_vl_msg_id = htons (rmp->_vl_msg_id);
754       rmp->context = htonl (rmp->context);
755     }));
756 }
757
758 static void
759 vl_api_nat44_ei_output_interface_get_t_handler (
760   vl_api_nat44_ei_output_interface_get_t *mp)
761 {
762   vl_api_nat44_ei_output_interface_get_reply_t *rmp;
763   nat44_ei_main_t *nm = &nat44_ei_main;
764   i32 rv = 0;
765
766   if (pool_elts (nm->output_feature_interfaces) == 0)
767     {
768       REPLY_MACRO (VL_API_NAT44_EI_OUTPUT_INTERFACE_GET_REPLY);
769       return;
770     }
771
772   REPLY_AND_DETAILS_MACRO (
773     VL_API_NAT44_EI_OUTPUT_INTERFACE_GET_REPLY, nm->output_feature_interfaces,
774     ({ send_nat44_ei_output_interface_details (cursor, rp, mp->context); }));
775 }
776
777 static void
778 vl_api_nat44_ei_add_del_static_mapping_t_handler (
779   vl_api_nat44_ei_add_del_static_mapping_t *mp)
780 {
781   vl_api_nat44_ei_add_del_static_mapping_reply_t *rmp;
782
783   nat44_ei_main_t *nm = &nat44_ei_main;
784   int rv = 0;
785
786   ip4_address_t l_addr, e_addr, pool_addr = { 0 };
787   u32 sw_if_index, flags = 0, vrf_id;
788   u16 l_port = 0, e_port = 0;
789   nat_protocol_t proto = 0;
790   u8 *tag = 0;
791
792   memcpy (&l_addr.as_u8, mp->local_ip_address, 4);
793
794   if (mp->flags & NAT44_EI_ADDR_ONLY_MAPPING)
795     {
796       flags |= NAT44_EI_SM_FLAG_ADDR_ONLY;
797     }
798   else
799     {
800       l_port = mp->local_port;
801       e_port = mp->external_port;
802       proto = ip_proto_to_nat_proto (mp->protocol);
803     }
804
805   sw_if_index = clib_net_to_host_u32 (mp->external_sw_if_index);
806   if (sw_if_index != ~0)
807     {
808       flags |= NAT44_EI_SM_FLAG_SWITCH_ADDRESS;
809     }
810   else
811     {
812       memcpy (&e_addr.as_u8, mp->external_ip_address, 4);
813     }
814
815   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
816
817   if (mp->is_add)
818     {
819       mp->tag[sizeof (mp->tag) - 1] = 0;
820       tag = format (0, "%s", mp->tag);
821       vec_terminate_c_string (tag);
822
823       rv = nat44_ei_add_static_mapping (l_addr, e_addr, l_port, e_port, proto,
824                                         vrf_id, sw_if_index, flags, pool_addr,
825                                         tag);
826       vec_free (tag);
827     }
828   else
829     {
830       rv = nat44_ei_del_static_mapping (l_addr, e_addr, l_port, e_port, proto,
831                                         vrf_id, sw_if_index, flags);
832     }
833   REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_STATIC_MAPPING_REPLY);
834 }
835
836 static void
837 send_nat44_ei_static_mapping_details (nat44_ei_static_mapping_t *m,
838                                       vl_api_registration_t *reg, u32 context)
839 {
840   vl_api_nat44_ei_static_mapping_details_t *rmp;
841   nat44_ei_main_t *nm = &nat44_ei_main;
842   u32 len = sizeof (*rmp);
843
844   rmp = vl_msg_api_alloc (len);
845   clib_memset (rmp, 0, len);
846   rmp->_vl_msg_id =
847     ntohs (VL_API_NAT44_EI_STATIC_MAPPING_DETAILS + nm->msg_id_base);
848
849   clib_memcpy (rmp->local_ip_address, &(m->local_addr), 4);
850   clib_memcpy (rmp->external_ip_address, &(m->external_addr), 4);
851   rmp->external_sw_if_index = ~0;
852   rmp->vrf_id = htonl (m->vrf_id);
853   rmp->context = context;
854
855   if (is_sm_addr_only (m->flags))
856     {
857       rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
858     }
859   else
860     {
861       rmp->protocol = nat_proto_to_ip_proto (m->proto);
862       rmp->external_port = m->external_port;
863       rmp->local_port = m->local_port;
864     }
865
866   if (m->tag)
867     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
868
869   vl_api_send_msg (reg, (u8 *) rmp);
870 }
871
872 static void
873 send_nat44_ei_static_map_resolve_details (nat44_ei_static_map_resolve_t *m,
874                                           vl_api_registration_t *reg,
875                                           u32 context)
876 {
877   vl_api_nat44_ei_static_mapping_details_t *rmp;
878   nat44_ei_main_t *nm = &nat44_ei_main;
879
880   rmp = vl_msg_api_alloc (sizeof (*rmp));
881   clib_memset (rmp, 0, sizeof (*rmp));
882   rmp->_vl_msg_id =
883     ntohs (VL_API_NAT44_EI_STATIC_MAPPING_DETAILS + nm->msg_id_base);
884   clib_memcpy (rmp->local_ip_address, &(m->l_addr), 4);
885   rmp->external_sw_if_index = htonl (m->sw_if_index);
886   rmp->vrf_id = htonl (m->vrf_id);
887   rmp->context = context;
888
889   if (is_sm_addr_only (m->flags))
890     {
891       rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
892     }
893   else
894     {
895       rmp->protocol = nat_proto_to_ip_proto (m->proto);
896       rmp->external_port = m->e_port;
897       rmp->local_port = m->l_port;
898     }
899   if (m->tag)
900     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
901
902   vl_api_send_msg (reg, (u8 *) rmp);
903 }
904
905 static void
906 vl_api_nat44_ei_static_mapping_dump_t_handler (
907   vl_api_nat44_ei_static_mapping_dump_t *mp)
908 {
909   vl_api_registration_t *reg;
910   nat44_ei_main_t *nm = &nat44_ei_main;
911   nat44_ei_static_mapping_t *m;
912   nat44_ei_static_map_resolve_t *rp;
913   int j;
914
915   reg = vl_api_client_index_to_registration (mp->client_index);
916   if (!reg)
917     return;
918
919   pool_foreach (m, nm->static_mappings)
920     {
921       if (!is_sm_identity_nat (m->flags))
922         send_nat44_ei_static_mapping_details (m, reg, mp->context);
923     }
924
925   for (j = 0; j < vec_len (nm->to_resolve); j++)
926     {
927       rp = nm->to_resolve + j;
928       if (!is_sm_identity_nat (rp->flags))
929         send_nat44_ei_static_map_resolve_details (rp, reg, mp->context);
930     }
931 }
932
933 static void
934 vl_api_nat44_ei_add_del_identity_mapping_t_handler (
935   vl_api_nat44_ei_add_del_identity_mapping_t *mp)
936 {
937   vl_api_nat44_ei_add_del_identity_mapping_reply_t *rmp;
938
939   nat44_ei_main_t *nm = &nat44_ei_main;
940   int rv = 0;
941
942   ip4_address_t addr, pool_addr = { 0 };
943   u32 sw_if_index, flags, vrf_id;
944   nat_protocol_t proto = 0;
945   u16 port = 0;
946   u8 *tag = 0;
947
948   flags = NAT44_EI_SM_FLAG_IDENTITY_NAT;
949
950   if (mp->flags & NAT44_EI_ADDR_ONLY_MAPPING)
951     {
952       flags |= NAT44_EI_SM_FLAG_ADDR_ONLY;
953     }
954   else
955     {
956       port = mp->port;
957       proto = ip_proto_to_nat_proto (mp->protocol);
958     }
959
960   sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
961   if (sw_if_index != ~0)
962     {
963       flags |= NAT44_EI_SM_FLAG_SWITCH_ADDRESS;
964     }
965   else
966     {
967       memcpy (&addr.as_u8, mp->ip_address, 4);
968     }
969
970   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
971
972   if (mp->is_add)
973     {
974       mp->tag[sizeof (mp->tag) - 1] = 0;
975       tag = format (0, "%s", mp->tag);
976       vec_terminate_c_string (tag);
977
978       rv = nat44_ei_add_static_mapping (addr, addr, port, port, proto, vrf_id,
979                                         sw_if_index, flags, pool_addr, tag);
980       vec_free (tag);
981     }
982   else
983     {
984       rv = nat44_ei_del_static_mapping (addr, addr, port, port, proto, vrf_id,
985                                         sw_if_index, flags);
986     }
987
988   REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_IDENTITY_MAPPING_REPLY);
989 }
990
991 static void
992 send_nat44_ei_identity_mapping_details (nat44_ei_static_mapping_t *m,
993                                         int index, vl_api_registration_t *reg,
994                                         u32 context)
995 {
996   vl_api_nat44_ei_identity_mapping_details_t *rmp;
997   nat44_ei_main_t *nm = &nat44_ei_main;
998   nat44_ei_lb_addr_port_t *local = pool_elt_at_index (m->locals, index);
999
1000   rmp = vl_msg_api_alloc (sizeof (*rmp));
1001   clib_memset (rmp, 0, sizeof (*rmp));
1002   rmp->_vl_msg_id =
1003     ntohs (VL_API_NAT44_EI_IDENTITY_MAPPING_DETAILS + nm->msg_id_base);
1004
1005   if (is_sm_addr_only (m->flags))
1006     rmp->flags |= NAT44_EI_ADDR_ONLY_MAPPING;
1007
1008   clib_memcpy (rmp->ip_address, &(m->local_addr), 4);
1009   rmp->port = m->local_port;
1010   rmp->sw_if_index = ~0;
1011   rmp->vrf_id = htonl (local->vrf_id);
1012   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1013   rmp->context = context;
1014   if (m->tag)
1015     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1016
1017   vl_api_send_msg (reg, (u8 *) rmp);
1018 }
1019
1020 static void
1021 send_nat44_ei_identity_map_resolve_details (nat44_ei_static_map_resolve_t *m,
1022                                             vl_api_registration_t *reg,
1023                                             u32 context)
1024 {
1025   vl_api_nat44_ei_identity_mapping_details_t *rmp;
1026   nat44_ei_main_t *nm = &nat44_ei_main;
1027   rmp = vl_msg_api_alloc (sizeof (*rmp));
1028   clib_memset (rmp, 0, sizeof (*rmp));
1029   rmp->_vl_msg_id =
1030     ntohs (VL_API_NAT44_EI_IDENTITY_MAPPING_DETAILS + nm->msg_id_base);
1031
1032   if (is_sm_addr_only (m->flags))
1033     rmp->flags = (vl_api_nat44_ei_config_flags_t) NAT44_EI_ADDR_ONLY_MAPPING;
1034
1035   rmp->port = m->l_port;
1036   rmp->sw_if_index = htonl (m->sw_if_index);
1037   rmp->vrf_id = htonl (m->vrf_id);
1038   rmp->protocol = nat_proto_to_ip_proto (m->proto);
1039   rmp->context = context;
1040   if (m->tag)
1041     strncpy ((char *) rmp->tag, (char *) m->tag, vec_len (m->tag));
1042
1043   vl_api_send_msg (reg, (u8 *) rmp);
1044 }
1045
1046 static void
1047 vl_api_nat44_ei_identity_mapping_dump_t_handler (
1048   vl_api_nat44_ei_identity_mapping_dump_t *mp)
1049 {
1050   vl_api_registration_t *reg;
1051   nat44_ei_main_t *nm = &nat44_ei_main;
1052   nat44_ei_static_mapping_t *m;
1053   nat44_ei_static_map_resolve_t *rp;
1054   int j;
1055
1056   reg = vl_api_client_index_to_registration (mp->client_index);
1057   if (!reg)
1058     return;
1059
1060   pool_foreach (m, nm->static_mappings)
1061     {
1062       if (is_sm_identity_nat (m->flags))
1063         {
1064           pool_foreach_index (j, m->locals)
1065             {
1066               send_nat44_ei_identity_mapping_details (m, j, reg, mp->context);
1067             }
1068         }
1069     }
1070
1071   for (j = 0; j < vec_len (nm->to_resolve); j++)
1072     {
1073       rp = nm->to_resolve + j;
1074       if (is_sm_identity_nat (rp->flags))
1075         send_nat44_ei_identity_map_resolve_details (rp, reg, mp->context);
1076     }
1077 }
1078
1079 static void
1080 vl_api_nat44_ei_add_del_interface_addr_t_handler (
1081   vl_api_nat44_ei_add_del_interface_addr_t *mp)
1082 {
1083   nat44_ei_main_t *nm = &nat44_ei_main;
1084   vl_api_nat44_ei_add_del_interface_addr_reply_t *rmp;
1085   u32 sw_if_index = ntohl (mp->sw_if_index);
1086   int rv = 0;
1087
1088   VALIDATE_SW_IF_INDEX (mp);
1089
1090   if (mp->is_add)
1091     {
1092       rv = nat44_ei_add_interface_address (sw_if_index);
1093     }
1094   else
1095     {
1096       rv = nat44_ei_del_interface_address (sw_if_index);
1097     }
1098
1099   BAD_SW_IF_INDEX_LABEL;
1100   REPLY_MACRO (VL_API_NAT44_EI_ADD_DEL_INTERFACE_ADDR_REPLY);
1101 }
1102
1103 static void
1104 send_nat44_ei_interface_addr_details (u32 sw_if_index,
1105                                       vl_api_registration_t *reg, u32 context)
1106 {
1107   vl_api_nat44_ei_interface_addr_details_t *rmp;
1108   nat44_ei_main_t *nm = &nat44_ei_main;
1109
1110   rmp = vl_msg_api_alloc (sizeof (*rmp));
1111   clib_memset (rmp, 0, sizeof (*rmp));
1112   rmp->_vl_msg_id =
1113     ntohs (VL_API_NAT44_EI_INTERFACE_ADDR_DETAILS + nm->msg_id_base);
1114   rmp->sw_if_index = ntohl (sw_if_index);
1115   rmp->context = context;
1116
1117   vl_api_send_msg (reg, (u8 *) rmp);
1118 }
1119
1120 static void
1121 vl_api_nat44_ei_interface_addr_dump_t_handler (
1122   vl_api_nat44_ei_interface_addr_dump_t *mp)
1123 {
1124   vl_api_registration_t *reg;
1125   nat44_ei_main_t *nm = &nat44_ei_main;
1126   u32 *i;
1127
1128   reg = vl_api_client_index_to_registration (mp->client_index);
1129   if (!reg)
1130     return;
1131
1132   vec_foreach (i, nm->auto_add_sw_if_indices)
1133     send_nat44_ei_interface_addr_details (*i, reg, mp->context);
1134 }
1135
1136 static void
1137 send_nat44_ei_user_details (nat44_ei_user_t *u, vl_api_registration_t *reg,
1138                             u32 context)
1139 {
1140   vl_api_nat44_ei_user_details_t *rmp;
1141   nat44_ei_main_t *nm = &nat44_ei_main;
1142   ip4_main_t *im = &ip4_main;
1143
1144   rmp = vl_msg_api_alloc (sizeof (*rmp));
1145   clib_memset (rmp, 0, sizeof (*rmp));
1146   rmp->_vl_msg_id = ntohs (VL_API_NAT44_EI_USER_DETAILS + nm->msg_id_base);
1147
1148   if (!pool_is_free_index (im->fibs, u->fib_index))
1149     {
1150       fib_table_t *fib = fib_table_get (u->fib_index, FIB_PROTOCOL_IP4);
1151       rmp->vrf_id = ntohl (fib->ft_table_id);
1152     }
1153
1154   clib_memcpy (rmp->ip_address, &(u->addr), 4);
1155   rmp->nsessions = ntohl (u->nsessions);
1156   rmp->nstaticsessions = ntohl (u->nstaticsessions);
1157   rmp->context = context;
1158
1159   vl_api_send_msg (reg, (u8 *) rmp);
1160 }
1161
1162 static void
1163 vl_api_nat44_ei_user_dump_t_handler (vl_api_nat44_ei_user_dump_t *mp)
1164 {
1165   vl_api_registration_t *reg;
1166   nat44_ei_main_t *nm = &nat44_ei_main;
1167   nat44_ei_main_per_thread_data_t *tnm;
1168   nat44_ei_user_t *u;
1169
1170   reg = vl_api_client_index_to_registration (mp->client_index);
1171   if (!reg)
1172     return;
1173
1174   vec_foreach (tnm, nm->per_thread_data)
1175     {
1176       pool_foreach (u, tnm->users)
1177         {
1178           send_nat44_ei_user_details (u, reg, mp->context);
1179         }
1180     }
1181 }
1182
1183 static void
1184 send_nat44_ei_user_session_details (nat44_ei_session_t *s,
1185                                     vl_api_registration_t *reg, u32 context)
1186 {
1187   vl_api_nat44_ei_user_session_details_t *rmp;
1188   nat44_ei_main_t *nm = &nat44_ei_main;
1189
1190   rmp = vl_msg_api_alloc (sizeof (*rmp));
1191   clib_memset (rmp, 0, sizeof (*rmp));
1192   rmp->_vl_msg_id =
1193     ntohs (VL_API_NAT44_EI_USER_SESSION_DETAILS + nm->msg_id_base);
1194   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1195   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1196
1197   if (nat44_ei_is_session_static (s))
1198     rmp->flags |= NAT44_EI_STATIC_MAPPING;
1199
1200   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1201   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1202   rmp->total_pkts = ntohl (s->total_pkts);
1203   rmp->context = context;
1204   if (nat44_ei_is_unk_proto_session (s))
1205     {
1206       rmp->outside_port = 0;
1207       rmp->inside_port = 0;
1208       rmp->protocol = ntohs (s->in2out.port);
1209     }
1210   else
1211     {
1212       rmp->outside_port = s->out2in.port;
1213       rmp->inside_port = s->in2out.port;
1214       rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1215     }
1216   vl_api_send_msg (reg, (u8 *) rmp);
1217 }
1218
1219 static void
1220 send_nat44_ei_user_session_v2_details (nat44_ei_session_t *s,
1221                                        vl_api_registration_t *reg, u32 context)
1222 {
1223   vl_api_nat44_ei_user_session_v2_details_t *rmp;
1224   nat44_ei_main_t *nm = &nat44_ei_main;
1225
1226   rmp = vl_msg_api_alloc (sizeof (*rmp));
1227   clib_memset (rmp, 0, sizeof (*rmp));
1228   rmp->_vl_msg_id =
1229     ntohs (VL_API_NAT44_EI_USER_SESSION_V2_DETAILS + nm->msg_id_base);
1230   clib_memcpy (rmp->outside_ip_address, (&s->out2in.addr), 4);
1231   clib_memcpy (rmp->inside_ip_address, (&s->in2out.addr), 4);
1232
1233   if (nat44_ei_is_session_static (s))
1234     rmp->flags |= NAT44_EI_STATIC_MAPPING;
1235
1236   rmp->last_heard = clib_host_to_net_u64 ((u64) s->last_heard);
1237   rmp->time_since_last_heard = clib_host_to_net_u64 (
1238     (u64) (vlib_time_now (vlib_get_main ()) - s->last_heard));
1239   rmp->total_bytes = clib_host_to_net_u64 (s->total_bytes);
1240   rmp->total_pkts = ntohl (s->total_pkts);
1241   rmp->context = context;
1242   if (nat44_ei_is_unk_proto_session (s))
1243     {
1244       rmp->outside_port = 0;
1245       rmp->inside_port = 0;
1246       rmp->protocol = ntohs (s->in2out.port);
1247     }
1248   else
1249     {
1250       rmp->outside_port = s->out2in.port;
1251       rmp->inside_port = s->in2out.port;
1252       rmp->protocol = ntohs (nat_proto_to_ip_proto (s->nat_proto));
1253     }
1254   vl_api_send_msg (reg, (u8 *) rmp);
1255 }
1256
1257 static void
1258 vl_api_nat44_ei_user_session_dump_t_handler (
1259   vl_api_nat44_ei_user_session_dump_t *mp)
1260 {
1261   vl_api_registration_t *reg;
1262   nat44_ei_main_t *nm = &nat44_ei_main;
1263   nat44_ei_main_per_thread_data_t *tnm;
1264   nat44_ei_session_t *s;
1265   clib_bihash_kv_8_8_t key, value;
1266   nat44_ei_user_key_t ukey;
1267   nat44_ei_user_t *u;
1268   u32 session_index, head_index, elt_index;
1269   dlist_elt_t *head, *elt;
1270   ip4_header_t ip;
1271
1272   reg = vl_api_client_index_to_registration (mp->client_index);
1273   if (!reg)
1274     return;
1275
1276   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1277   ip.src_address.as_u32 = ukey.addr.as_u32;
1278   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1279   key.key = ukey.as_u64;
1280   if (nm->num_workers > 1)
1281     tnm = vec_elt_at_index (
1282       nm->per_thread_data,
1283       nat44_ei_get_in2out_worker_index (&ip, ukey.fib_index, 0));
1284   else
1285     tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1286
1287   if (clib_bihash_search_8_8 (&tnm->user_hash, &key, &value))
1288     return;
1289   u = pool_elt_at_index (tnm->users, value.value);
1290   if (!u->nsessions && !u->nstaticsessions)
1291     return;
1292
1293   head_index = u->sessions_per_user_list_head_index;
1294   head = pool_elt_at_index (tnm->list_pool, head_index);
1295   elt_index = head->next;
1296   elt = pool_elt_at_index (tnm->list_pool, elt_index);
1297   session_index = elt->value;
1298   while (session_index != ~0)
1299     {
1300       s = pool_elt_at_index (tnm->sessions, session_index);
1301
1302       send_nat44_ei_user_session_details (s, reg, mp->context);
1303
1304       elt_index = elt->next;
1305       elt = pool_elt_at_index (tnm->list_pool, elt_index);
1306       session_index = elt->value;
1307     }
1308 }
1309
1310 static void
1311 vl_api_nat44_ei_user_session_v2_dump_t_handler (
1312   vl_api_nat44_ei_user_session_dump_t *mp)
1313 {
1314   vl_api_registration_t *reg;
1315   nat44_ei_main_t *nm = &nat44_ei_main;
1316   nat44_ei_main_per_thread_data_t *tnm;
1317   nat44_ei_session_t *s;
1318   clib_bihash_kv_8_8_t key, value;
1319   nat44_ei_user_key_t ukey;
1320   nat44_ei_user_t *u;
1321   u32 session_index, head_index, elt_index;
1322   dlist_elt_t *head, *elt;
1323   ip4_header_t ip;
1324
1325   reg = vl_api_client_index_to_registration (mp->client_index);
1326   if (!reg)
1327     return;
1328
1329   clib_memcpy (&ukey.addr, mp->ip_address, 4);
1330   ip.src_address.as_u32 = ukey.addr.as_u32;
1331   ukey.fib_index = fib_table_find (FIB_PROTOCOL_IP4, ntohl (mp->vrf_id));
1332   key.key = ukey.as_u64;
1333   if (nm->num_workers > 1)
1334     tnm = vec_elt_at_index (
1335       nm->per_thread_data,
1336       nat44_ei_get_in2out_worker_index (&ip, ukey.fib_index, 0));
1337   else
1338     tnm = vec_elt_at_index (nm->per_thread_data, nm->num_workers);
1339
1340   if (clib_bihash_search_8_8 (&tnm->user_hash, &key, &value))
1341     return;
1342   u = pool_elt_at_index (tnm->users, value.value);
1343   if (!u->nsessions && !u->nstaticsessions)
1344     return;
1345
1346   head_index = u->sessions_per_user_list_head_index;
1347   head = pool_elt_at_index (tnm->list_pool, head_index);
1348   elt_index = head->next;
1349   elt = pool_elt_at_index (tnm->list_pool, elt_index);
1350   session_index = elt->value;
1351   while (session_index != ~0)
1352     {
1353       s = pool_elt_at_index (tnm->sessions, session_index);
1354
1355       send_nat44_ei_user_session_v2_details (s, reg, mp->context);
1356
1357       elt_index = elt->next;
1358       elt = pool_elt_at_index (tnm->list_pool, elt_index);
1359       session_index = elt->value;
1360     }
1361 }
1362
1363 static void
1364 vl_api_nat44_ei_del_session_t_handler (vl_api_nat44_ei_del_session_t *mp)
1365 {
1366   nat44_ei_main_t *nm = &nat44_ei_main;
1367   vl_api_nat44_ei_del_session_reply_t *rmp;
1368   ip4_address_t addr, eh_addr;
1369   u16 port;
1370   u32 vrf_id;
1371   int rv = 0;
1372   u8 is_in;
1373   nat_protocol_t proto;
1374
1375   memcpy (&addr.as_u8, mp->address, 4);
1376   port = mp->port;
1377   vrf_id = clib_net_to_host_u32 (mp->vrf_id);
1378   proto = ip_proto_to_nat_proto (mp->protocol);
1379   memcpy (&eh_addr.as_u8, mp->ext_host_address, 4);
1380
1381   // is session inside ?
1382   is_in = mp->flags & NAT44_EI_IF_INSIDE;
1383
1384   rv = nat44_ei_del_session (nm, &addr, port, proto, vrf_id, is_in);
1385
1386   REPLY_MACRO (VL_API_NAT44_EI_DEL_SESSION_REPLY);
1387 }
1388
1389 static void
1390 vl_api_nat44_ei_forwarding_enable_disable_t_handler (
1391   vl_api_nat44_ei_forwarding_enable_disable_t *mp)
1392 {
1393   nat44_ei_main_t *nm = &nat44_ei_main;
1394   vl_api_nat44_ei_forwarding_enable_disable_reply_t *rmp;
1395   int rv = 0;
1396   u32 *ses_to_be_removed = 0, *ses_index;
1397   nat44_ei_main_per_thread_data_t *tnm;
1398   nat44_ei_session_t *s;
1399
1400   nm->forwarding_enabled = mp->enable != 0;
1401
1402   if (mp->enable == 0)
1403     {
1404       vec_foreach (tnm, nm->per_thread_data)
1405         {
1406           vec_foreach (ses_index, ses_to_be_removed)
1407             {
1408               s = pool_elt_at_index (tnm->sessions, ses_index[0]);
1409               nat44_ei_free_session_data (nm, s, tnm - nm->per_thread_data, 0);
1410               nat44_ei_delete_session (nm, s, tnm - nm->per_thread_data);
1411             }
1412
1413           vec_free (ses_to_be_removed);
1414         }
1415     }
1416
1417   REPLY_MACRO (VL_API_NAT44_EI_FORWARDING_ENABLE_DISABLE_REPLY);
1418 }
1419
1420 static void
1421 vl_api_nat44_ei_set_fq_options_t_handler (vl_api_nat44_ei_set_fq_options_t *mp)
1422 {
1423   nat44_ei_main_t *nm = &nat44_ei_main;
1424   vl_api_nat44_ei_set_fq_options_reply_t *rmp;
1425   int rv = 0;
1426   u32 frame_queue_nelts = ntohl (mp->frame_queue_nelts);
1427   rv = nat44_ei_set_frame_queue_nelts (frame_queue_nelts);
1428   REPLY_MACRO (VL_API_NAT44_EI_SET_FQ_OPTIONS_REPLY);
1429 }
1430
1431 static void
1432 vl_api_nat44_ei_show_fq_options_t_handler (
1433   vl_api_nat44_ei_show_fq_options_t *mp)
1434 {
1435   nat44_ei_main_t *nm = &nat44_ei_main;
1436   vl_api_nat44_ei_show_fq_options_reply_t *rmp;
1437   int rv = 0;
1438   /* clang-format off */
1439   REPLY_MACRO2_ZERO (VL_API_NAT44_EI_SHOW_FQ_OPTIONS_REPLY,
1440   ({
1441     rmp->frame_queue_nelts = htonl (nm->frame_queue_nelts);
1442   }));
1443   /* clang-format on */
1444 }
1445
1446 /* API definitions */
1447 #include <vnet/format_fns.h>
1448 #include <nat/nat44-ei/nat44_ei.api.c>
1449
1450 /* Set up the API message handling tables */
1451 clib_error_t *
1452 nat44_ei_api_hookup (vlib_main_t *vm)
1453 {
1454   nat44_ei_main_t *nm = &nat44_ei_main;
1455   nm->msg_id_base = setup_message_id_table ();
1456   return 0;
1457 }
1458
1459 /*
1460  * fd.io coding-style-patch-verification: ON
1461  *
1462  * Local Variables:
1463  * eval: (c-set-style "gnu")
1464  * End:
1465  */