LCOV - code coverage report
Current view: top level - plugins/nat/nat44-ed - nat44_ed_format.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 98 170 57.6 %
Date: 2023-10-26 01:39:38 Functions: 6 8 75.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2018 Cisco and/or its affiliates.
       3             :  * Licensed under the Apache License, Version 2.0 (the "License");
       4             :  * you may not use this file except in compliance with the License.
       5             :  * You may obtain a copy of the License at:
       6             :  *
       7             :  *     http://www.apache.org/licenses/LICENSE-2.0
       8             :  *
       9             :  * Unless required by applicable law or agreed to in writing, software
      10             :  * distributed under the License is distributed on an "AS IS" BASIS,
      11             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             :  * See the License for the specific language governing permissions and
      13             :  * limitations under the License.
      14             :  */
      15             : 
      16             : #include <nat/nat44-ed/nat44_ed.h>
      17             : #include <nat/nat44-ed/nat44_ed_inlines.h>
      18             : 
      19             : u8 *
      20        2150 : format_ed_session_kvp (u8 *s, va_list *args)
      21             : {
      22        2150 :   clib_bihash_kv_16_8_t *v = va_arg (*args, clib_bihash_kv_16_8_t *);
      23             : 
      24             :   u8 proto;
      25             :   u16 r_port, l_port;
      26             :   ip4_address_t l_addr, r_addr;
      27             :   u32 fib_index;
      28             : 
      29        2150 :   split_ed_kv (v, &l_addr, &r_addr, &proto, &fib_index, &l_port, &r_port);
      30        4300 :   s = format (s,
      31             :               "local %U:%d remote %U:%d proto %U fib %d thread-index %u "
      32             :               "session-index %u",
      33        2150 :               format_ip4_address, &l_addr, clib_net_to_host_u16 (l_port),
      34        2150 :               format_ip4_address, &r_addr, clib_net_to_host_u16 (r_port),
      35             :               format_ip_protocol, proto, fib_index,
      36             :               ed_value_get_thread_index (v), ed_value_get_session_index (v));
      37             : 
      38        2150 :   return s;
      39             : }
      40             : 
      41             : u8 *
      42       13317 : format_snat_session (u8 * s, va_list * args)
      43             : {
      44       13317 :   snat_main_t *sm = va_arg (*args, snat_main_t *);
      45       13317 :   snat_main_per_thread_data_t *tsm =
      46             :     va_arg (*args, snat_main_per_thread_data_t *);
      47       13317 :   snat_session_t *sess = va_arg (*args, snat_session_t *);
      48       13317 :   f64 now = va_arg (*args, f64);
      49             : 
      50       13317 :   if (nat44_ed_is_unk_proto (sess->proto))
      51             :     {
      52           0 :       s = format (s, "  i2o %U proto %u fib %u\n",
      53             :                   format_ip4_address, &sess->in2out.addr,
      54           0 :                   sess->in2out.port, sess->in2out.fib_index);
      55             :       s =
      56           0 :         format (s, "    o2i %U proto %u fib %u\n", format_ip4_address,
      57           0 :                 &sess->out2in.addr, sess->out2in.port, sess->out2in.fib_index);
      58             :     }
      59             :   else
      60             :     {
      61       26634 :       s = format (s, "  i2o %U proto %U port %d fib %d\n", format_ip4_address,
      62       13317 :                   &sess->in2out.addr, format_ip_protocol, sess->proto,
      63       13317 :                   clib_net_to_host_u16 (sess->in2out.port),
      64             :                   sess->in2out.fib_index);
      65       26634 :       s = format (s, "    o2i %U proto %U port %d fib %d\n",
      66             :                   format_ip4_address, &sess->out2in.addr, format_ip_protocol,
      67       13317 :                   sess->proto, clib_net_to_host_u16 (sess->out2in.port),
      68             :                   sess->out2in.fib_index);
      69             :     }
      70       13317 :   if (nat44_ed_is_twice_nat_session (sess))
      71             :     {
      72           8 :       s = format (s, "       external host o2i %U:%d i2o %U:%d\n",
      73             :                   format_ip4_address, &sess->ext_host_addr,
      74           8 :                   clib_net_to_host_u16 (sess->ext_host_port),
      75             :                   format_ip4_address, &sess->ext_host_nat_addr,
      76           8 :                   clib_net_to_host_u16 (sess->ext_host_nat_port));
      77             :     }
      78             :       else
      79             :         {
      80       13309 :           if (sess->ext_host_addr.as_u32)
      81       13309 :             s = format (s, "       external host %U:%u\n",
      82             :                         format_ip4_address, &sess->ext_host_addr,
      83       13309 :                         clib_net_to_host_u16 (sess->ext_host_port));
      84             :         }
      85       13317 :       s = format (s, "       i2o flow: %U\n", format_nat_6t_flow, &sess->i2o);
      86       13317 :       s = format (s, "       o2i flow: %U\n", format_nat_6t_flow, &sess->o2i);
      87       13317 :   s = format (s, "       index %llu\n", sess - tsm->sessions);
      88       13317 :   s = format (s, "       last heard %.2f\n", sess->last_heard);
      89       13317 :   s = format (s, "       timeout in %.2f\n",
      90       13317 :               nat44_session_get_timeout (sm, sess) - (now - sess->last_heard));
      91       13317 :   s = format (s, "       total pkts %d, total bytes %lld\n", sess->total_pkts,
      92             :               sess->total_bytes);
      93       13317 :   if (nat44_ed_is_session_static (sess))
      94           9 :     s = format (s, "       static translation\n");
      95             :   else
      96       13308 :     s = format (s, "       dynamic translation\n");
      97       13317 :   if (na44_ed_is_fwd_bypass_session (sess))
      98           2 :     s = format (s, "       forwarding-bypass\n");
      99       13317 :   if (nat44_ed_is_lb_session (sess))
     100           4 :     s = format (s, "       load-balancing\n");
     101       13317 :   if (nat44_ed_is_twice_nat_session (sess))
     102           8 :     s = format (s, "       twice-nat\n");
     103       13317 :   return s;
     104             : }
     105             : 
     106             : u8 *
     107           0 : format_snat_static_mapping (u8 * s, va_list * args)
     108             : {
     109           0 :   snat_static_mapping_t *m = va_arg (*args, snat_static_mapping_t *);
     110             :   nat44_lb_addr_port_t *local;
     111             : 
     112           0 :   if (is_sm_identity_nat (m->flags))
     113             :     {
     114           0 :       if (is_sm_addr_only (m->flags))
     115           0 :         s = format (s, "identity mapping %U",
     116             :                     format_ip4_address, &m->local_addr);
     117             :       else
     118           0 :         s = format (s, "identity mapping %U %U:%d", format_ip_protocol,
     119           0 :                     m->proto, format_ip4_address, &m->local_addr,
     120           0 :                     clib_net_to_host_u16 (m->local_port));
     121             : 
     122           0 :       pool_foreach (local, m->locals)
     123             :        {
     124           0 :         s = format (s, " vrf %d", local->vrf_id);
     125             :       }
     126             : 
     127           0 :       return s;
     128             :     }
     129             : 
     130           0 :   if (is_sm_addr_only (m->flags))
     131             :     s =
     132           0 :       format (s, "local %U external %U vrf %d %s %s", format_ip4_address,
     133             :               &m->local_addr, format_ip4_address, &m->external_addr, m->vrf_id,
     134           0 :               is_sm_twice_nat (m->flags) ?
     135             :                 "twice-nat" :
     136           0 :                 is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
     137           0 :               is_sm_out2in_only (m->flags) ? "out2in-only" : "");
     138             :   else
     139             :     {
     140           0 :       if (is_sm_lb (m->flags))
     141             :         {
     142             :           s =
     143           0 :             format (s, "%U external %U:%d %s %s", format_ip_protocol, m->proto,
     144             :                     format_ip4_address, &m->external_addr,
     145           0 :                     clib_net_to_host_u16 (m->external_port),
     146           0 :                     is_sm_twice_nat (m->flags) ?
     147             :                       "twice-nat" :
     148           0 :                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
     149           0 :                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
     150             : 
     151           0 :           pool_foreach (local, m->locals)
     152             :             {
     153           0 :               s = format (s, "\n  local %U:%d vrf %d probability %d\%",
     154             :                           format_ip4_address, &local->addr,
     155           0 :                           clib_net_to_host_u16 (local->port), local->vrf_id,
     156           0 :                           local->probability);
     157             :             }
     158             :         }
     159             :       else
     160           0 :         s = format (s, "%U local %U:%d external %U:%d vrf %d %s %s",
     161           0 :                     format_ip_protocol, m->proto, format_ip4_address,
     162           0 :                     &m->local_addr, clib_net_to_host_u16 (m->local_port),
     163             :                     format_ip4_address, &m->external_addr,
     164           0 :                     clib_net_to_host_u16 (m->external_port), m->vrf_id,
     165           0 :                     is_sm_twice_nat (m->flags) ?
     166             :                       "twice-nat" :
     167           0 :                       is_sm_self_twice_nat (m->flags) ? "self-twice-nat" : "",
     168           0 :                     is_sm_out2in_only (m->flags) ? "out2in-only" : "");
     169             :     }
     170           0 :   return s;
     171             : }
     172             : 
     173             : u8 *
     174           0 : format_snat_static_map_to_resolve (u8 * s, va_list * args)
     175             : {
     176           0 :   snat_static_mapping_resolve_t *m =
     177             :     va_arg (*args, snat_static_mapping_resolve_t *);
     178           0 :   vnet_main_t *vnm = vnet_get_main ();
     179             : 
     180           0 :   if (is_sm_addr_only (m->flags))
     181           0 :     s = format (s, "local %U external %U vrf %d",
     182             :                 format_ip4_address, &m->l_addr,
     183             :                 format_vnet_sw_if_index_name, vnm, m->sw_if_index, m->vrf_id);
     184             :   else
     185           0 :     s = format (s, "%U local %U:%d external %U:%d vrf %d", format_ip_protocol,
     186           0 :                 m->proto, format_ip4_address, &m->l_addr,
     187           0 :                 clib_net_to_host_u16 (m->l_port), format_vnet_sw_if_index_name,
     188           0 :                 vnm, m->sw_if_index, clib_net_to_host_u16 (m->e_port),
     189             :                 m->vrf_id);
     190             : 
     191           0 :   return s;
     192             : }
     193             : 
     194             : u8 *
     195        2615 : format_nat_ed_translation_error (u8 *s, va_list *args)
     196             : {
     197        2615 :   nat_translation_error_e e = va_arg (*args, nat_translation_error_e);
     198             : 
     199        2615 :   switch (e)
     200             :     {
     201        2615 :     case NAT_ED_TRNSL_ERR_SUCCESS:
     202        2615 :       s = format (s, "success");
     203        2615 :       break;
     204           0 :     case NAT_ED_TRNSL_ERR_TRANSLATION_FAILED:
     205           0 :       s = format (s, "translation-failed");
     206           0 :       break;
     207           0 :     case NAT_ED_TRNSL_ERR_FLOW_MISMATCH:
     208           0 :       s = format (s, "flow-mismatch");
     209           0 :       break;
     210           0 :     case NAT_ED_TRNSL_ERR_PACKET_TRUNCATED:
     211           0 :       s = format (s, "packet-truncated");
     212           0 :       break;
     213           0 :     case NAT_ED_TRNSL_ERR_INNER_IP_CORRUPT:
     214           0 :       s = format (s, "inner-ip-corrupted");
     215           0 :       break;
     216           0 :     case NAT_ED_TRNSL_ERR_INVALID_CSUM:
     217           0 :       s = format (s, "invalid-checksum");
     218           0 :       break;
     219             :     }
     220        2615 :   return s;
     221             : }
     222             : 
     223             : u8 *
     224       31864 : format_nat_6t_flow (u8 *s, va_list *args)
     225             : {
     226       31864 :   nat_6t_flow_t *f = va_arg (*args, nat_6t_flow_t *);
     227             : 
     228       31864 :   s = format (s, "match: %U ", format_nat_6t, &f->match);
     229       31864 :   int r = 0;
     230       31864 :   if (f->ops & NAT_FLOW_OP_SADDR_REWRITE)
     231             :     {
     232       15929 :       s = format (s, "rewrite: saddr %U ", format_ip4_address,
     233       15929 :                   f->rewrite.saddr.as_u8);
     234       15929 :       r = 1;
     235             :     }
     236       31864 :   if (f->ops & NAT_FLOW_OP_SPORT_REWRITE)
     237             :     {
     238       14756 :       if (!r)
     239             :         {
     240           0 :           s = format (s, "rewrite: ");
     241           0 :           r = 1;
     242             :         }
     243       14756 :       s = format (s, "sport %u ", clib_net_to_host_u16 (f->rewrite.sport));
     244             :     }
     245       31864 :   if (f->ops & NAT_FLOW_OP_DADDR_REWRITE)
     246             :     {
     247       31596 :       if (!r)
     248             :         {
     249       15877 :           s = format (s, "rewrite: ");
     250       15877 :           r = 1;
     251             :         }
     252       31596 :       s = format (s, "daddr %U ", format_ip4_address, f->rewrite.daddr.as_u8);
     253             :     }
     254       31864 :   if (f->ops & NAT_FLOW_OP_DPORT_REWRITE)
     255             :     {
     256       29260 :       if (!r)
     257             :         {
     258           0 :           s = format (s, "rewrite: ");
     259           0 :           r = 1;
     260             :         }
     261       29260 :       s = format (s, "dport %u ", clib_net_to_host_u16 (f->rewrite.dport));
     262             :     }
     263       31864 :   if (f->ops & NAT_FLOW_OP_ICMP_ID_REWRITE)
     264             :     {
     265        2338 :       if (!r)
     266             :         {
     267           0 :           s = format (s, "rewrite: ");
     268           0 :           r = 1;
     269             :         }
     270        2338 :       s = format (s, "icmp-id %u ", clib_net_to_host_u16 (f->rewrite.icmp_id));
     271             :     }
     272       31864 :   if (f->ops & NAT_FLOW_OP_TXFIB_REWRITE)
     273             :     {
     274       31601 :       if (!r)
     275             :         {
     276          29 :           s = format (s, "rewrite: ");
     277          29 :           r = 1;
     278             :         }
     279       31601 :       s = format (s, "txfib %u ", f->rewrite.fib_index);
     280             :     }
     281       31864 :   return s;
     282             : }
     283             : 
     284             : u8 *
     285       31864 : format_nat_6t (u8 *s, va_list *args)
     286             : {
     287       31864 :   nat_6t_t *t = va_arg (*args, nat_6t_t *);
     288             : 
     289       95592 :   s = format (s, "saddr %U sport %u daddr %U dport %u proto %U fib_idx %u",
     290       31864 :               format_ip4_address, t->saddr.as_u8,
     291       31864 :               clib_net_to_host_u16 (t->sport), format_ip4_address,
     292       31864 :               t->daddr.as_u8, clib_net_to_host_u16 (t->dport),
     293       31864 :               format_ip_protocol, t->proto, t->fib_index);
     294       31864 :   return s;
     295             : }
     296             : 
     297             : u8 *
     298         713 : format_nat44_ed_tcp_state (u8 *s, va_list *args)
     299             : {
     300         713 :   nat44_ed_tcp_state_e e = va_arg (*args, nat44_ed_tcp_state_e);
     301         713 :   switch (e)
     302             :     {
     303         658 :     case NAT44_ED_TCP_STATE_CLOSED:
     304         658 :       s = format (s, "closed");
     305         658 :       break;
     306          21 :     case NAT44_ED_TCP_STATE_ESTABLISHED:
     307          21 :       s = format (s, "established");
     308          21 :       break;
     309          34 :     case NAT44_ED_TCP_STATE_CLOSING:
     310          34 :       s = format (s, "closing");
     311          34 :       break;
     312           0 :     case NAT44_ED_TCP_N_STATE:
     313           0 :       s = format (s, "BUG! unexpected N_STATE! BUG!");
     314           0 :       break;
     315             :     }
     316         713 :   return s;
     317             : }
     318             : 
     319             : /*
     320             :  * fd.io coding-style-patch-verification: ON
     321             :  *
     322             :  * Local Variables:
     323             :  * eval: (c-set-style "gnu")
     324             :  * End:
     325             :  */

Generated by: LCOV version 1.14