FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
nat66_api.c
Go to the documentation of this file.
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 <vlibmemory/api.h>
18 #include <nat/nat66/nat66.h>
19 #include <nat/nat66/nat66.api_enum.h>
20 #include <nat/nat66/nat66.api_types.h>
21 #include <vnet/fib/fib_table.h>
22 
23 #define REPLY_MSG_ID_BASE nm->msg_id_base
25 
26 static void
29 {
31  vl_api_nat66_plugin_enable_disable_reply_t *rmp;
32  int rv = 0;
33 
34  if (mp->enable)
35  {
36  rv = nat66_plugin_enable (ntohl (mp->outside_vrf));
37  }
38  else
40 
41  REPLY_MACRO (VL_API_NAT66_PLUGIN_ENABLE_DISABLE_REPLY);
42 }
43 
44 static void
46  mp)
47 {
49  vl_api_nat66_add_del_interface_reply_t *rmp;
50  int rv = 0;
51 
53 
54  rv =
56  mp->flags & NAT_IS_INSIDE, mp->is_add);
57 
59 
60  REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
61 }
62 
63 static void
66 {
68  vl_api_nat66_add_del_static_mapping_reply_t *rmp;
69  ip6_address_t l_addr, e_addr;
70  int rv = 0;
71 
72  memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
73  memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
74 
75  rv =
76  nat66_static_mapping_add_del (&l_addr, &e_addr,
77  clib_net_to_host_u32 (mp->vrf_id),
78  mp->is_add);
79 
80  REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
81 }
82 
83 typedef struct nat66_api_walk_ctx_t_
84 {
88 
89 static int
91 {
95 
96  rmp = vl_msg_api_alloc (sizeof (*rmp));
97  clib_memset (rmp, 0, sizeof (*rmp));
98  rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + nm->msg_id_base);
99  rmp->sw_if_index = ntohl (i->sw_if_index);
101  rmp->flags |= NAT_IS_INSIDE;
102  rmp->context = ctx->context;
103 
104  vl_api_send_msg (ctx->rp, (u8 *) rmp);
105 
106  return 0;
107 }
108 
109 static void
111 {
114 
115  if (PREDICT_FALSE (!nm->enabled))
116  return;
117 
119  if (rp == 0)
120  return;
121 
123  .rp = rp,
124  .context = mp->context,
125  };
126 
128 }
129 
130 static int
132 {
135  nat66_api_walk_ctx_t *ctx = arg;
136  fib_table_t *fib;
137  vlib_counter_t vc;
138 
140  if (!fib)
141  return -1;
142 
143  vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
144 
145  rmp = vl_msg_api_alloc (sizeof (*rmp));
146  clib_memset (rmp, 0, sizeof (*rmp));
147  rmp->_vl_msg_id =
148  ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + nm->msg_id_base);
149  clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
150  clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
151  rmp->vrf_id = ntohl (fib->ft_table_id);
152  rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
153  rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
154  rmp->context = ctx->context;
155 
156  vl_api_send_msg (ctx->rp, (u8 *) rmp);
157 
158  return 0;
159 }
160 
161 static void
163  * mp)
164 {
167 
168  if (PREDICT_FALSE (!nm->enabled))
169  return;
170 
172  if (rp == 0)
173  return;
174 
176  .rp = rp,
177  .context = mp->context,
178  };
179 
181 }
182 
183 /* API definitions */
184 #include <vnet/format_fns.h>
185 #include <nat/nat66/nat66.api.c>
186 
187 /* Set up the API message handling tables */
188 clib_error_t *
190 {
193  return 0;
194 }
vl_api_nat66_interface_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: nat66.api:69
VALIDATE_SW_IF_INDEX
#define VALIDATE_SW_IF_INDEX(mp)
Definition: api_helper_macros.h:281
vl_api_nat66_static_mapping_dump_t::client_index
u32 client_index
Definition: nat66.api:94
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
nat66_interface_add_del
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:156
api.h
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
vl_api_nat66_add_del_interface_t::flags
vl_api_nat_config_flags_t flags
Definition: nat66.api:47
nat66_static_mapping_t::e_addr
ip6_address_t e_addr
Definition: nat66.h:29
nat66_interfaces_walk
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:202
vl_api_nat66_add_del_static_mapping_t::local_ip_address
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:84
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
nat66_api_walk_ctx_t_::context
u32 context
Definition: nat66_api.c:86
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
nat66_static_mapping_t::l_addr
ip6_address_t l_addr
Definition: nat66.h:28
nat66_plugin_enable
int nat66_plugin_enable(u32 outside_vrf)
Definition: nat66.c:86
nat66_api_walk_ctx_t
struct nat66_api_walk_ctx_t_ nat66_api_walk_ctx_t
vl_api_nat66_plugin_enable_disable_t::outside_vrf
u32 outside_vrf
Definition: nat66.api:31
fib_table.h
nat66_static_mapping_t::fib_index
u32 fib_index
Definition: nat66.h:30
nat66_api_walk_ctx_t_
Definition: nat66_api.c:83
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_nat66_add_del_static_mapping_t_handler
static void vl_api_nat66_add_del_static_mapping_t_handler(vl_api_nat66_add_del_static_mapping_t *mp)
Definition: nat66_api.c:65
vl_api_nat66_static_mapping_details_t::context
u32 context
Definition: nat66.api:107
nat66_static_mappings_walk
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:320
vl_api_nat66_interface_details_t::context
u32 context
Definition: nat66.api:67
vl_api_nat66_interface_details_t
NAT66 interface details response.
Definition: nat66.api:66
nat66_static_mapping_add_del
int nat66_static_mapping_add_del(ip6_address_t *l_addr, ip6_address_t *e_addr, u32 vrf_id, u8 is_add)
Definition: nat66.c:239
fib_table_t_
A protocol Independent FIB table.
Definition: fib_table.h:71
nat44_ei_main_s::enabled
u8 enabled
Definition: nat44_ei.h:471
fib_table_t_::ft_table_id
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
vlib_counter_t
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
vl_api_nat66_interface_dump_t::client_index
u32 client_index
Definition: nat66.api:56
vl_api_nat66_plugin_enable_disable_t
Enable/disable NAT66 plugin.
Definition: nat66.api:28
vl_api_nat66_static_mapping_details_t::vrf_id
u32 vrf_id
Definition: nat66.api:110
nat66_main_t
Definition: nat66.h:58
vlib_counter_t::packets
counter_t packets
packet counter
Definition: counter_types.h:28
vl_api_nat66_add_del_static_mapping_t::vrf_id
u32 vrf_id
Definition: nat66.api:86
nat44_ei_main_s::msg_id_base
u16 msg_id_base
Definition: nat44_ei.h:448
vl_api_nat66_static_mapping_dump_t
Dump NAT66 static mappings.
Definition: nat66.api:93
BAD_SW_IF_INDEX_LABEL
#define BAD_SW_IF_INDEX_LABEL
Definition: api_helper_macros.h:289
vlib_get_combined_counter
static void vlib_get_combined_counter(const vlib_combined_counter_main_t *cm, u32 index, vlib_counter_t *result)
Get the value of a combined counter, never called in the speed path Scrapes the entire set of per-thr...
Definition: counter.h:272
vl_api_nat66_add_del_interface_t::is_add
bool is_add
Definition: nat66.api:46
vl_api_nat66_static_mapping_details_t::external_ip_address
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:109
vl_api_nat66_interface_dump_t_handler
static void vl_api_nat66_interface_dump_t_handler(vl_api_nat66_interface_dump_t *mp)
Definition: nat66_api.c:110
vl_api_nat66_static_mapping_dump_t_handler
static void vl_api_nat66_static_mapping_dump_t_handler(vl_api_nat66_static_mapping_dump_t *mp)
Definition: nat66_api.c:162
vl_api_nat66_add_del_interface_t
Enable/disable NAT66 feature on the interface.
Definition: nat66.api:43
vl_api_nat66_interface_dump_t
Dump interfaces with NAT66 feature.
Definition: nat66.api:55
nat66_plugin_disable
int nat66_plugin_disable()
Definition: nat66.c:108
format_fns.h
fib_table_get
fib_table_t * fib_table_get(fib_node_index_t index, fib_protocol_t proto)
Get a pointer to a FIB table.
Definition: fib_table.c:29
u32
unsigned int u32
Definition: types.h:88
nat66_api_interface_walk
static int nat66_api_interface_walk(nat66_interface_t *i, void *arg)
Definition: nat66_api.c:90
vlib_counter_t::bytes
counter_t bytes
byte counter
Definition: counter_types.h:29
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
vl_api_nat66_add_del_static_mapping_t::external_ip_address
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:85
nat66_interface_is_inside
#define nat66_interface_is_inside(i)
Definition: nat66.h:55
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
api_helper_macros.h
nat66_main
nat66_main_t nat66_main
Definition: nat66.c:25
nm
nat44_ei_main_t * nm
Definition: nat44_ei_hairpinning.c:413
vl_api_nat66_add_del_interface_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: nat66.api:48
vl_api_nat66_static_mapping_details_t::local_ip_address
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:108
nat66_api_walk_ctx_t_::rp
vl_api_registration_t * rp
Definition: nat66_api.c:85
vl_api_nat66_add_del_interface_t_handler
static void vl_api_nat66_add_del_interface_t_handler(vl_api_nat66_add_del_interface_t *mp)
Definition: nat66_api.c:45
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vl_api_nat66_static_mapping_dump_t::context
u32 context
Definition: nat66.api:95
i
int i
Definition: flowhash_template.h:376
vl_api_nat66_static_mapping_details_t::total_bytes
u64 total_bytes
Definition: nat66.api:111
vl_api_nat66_plugin_enable_disable_t::enable
bool enable
Definition: nat66.api:32
vl_api_nat66_static_mapping_details_t
NAT66 static mapping details response.
Definition: nat66.api:106
rv
int __clib_unused rv
Definition: application.c:491
nat66_interface_t
Definition: nat66.h:47
vl_api_nat66_add_del_static_mapping_t::is_add
bool is_add
Definition: nat66.api:83
NAT_IS_INSIDE
@ NAT_IS_INSIDE
Definition: nat_types.api:44
nat66.h
NAT66 global declarations.
vl_api_nat66_add_del_static_mapping_t
Add/delete 1:1 NAT66.
Definition: nat66.api:80
vl_api_nat66_static_mapping_details_t::total_pkts
u64 total_pkts
Definition: nat66.api:112
vl_api_nat66_plugin_enable_disable_t_handler
static void vl_api_nat66_plugin_enable_disable_t_handler(vl_api_nat66_plugin_enable_disable_t *mp)
Definition: nat66_api.c:27
vl_api_nat66_interface_details_t::flags
vl_api_nat_config_flags_t flags
Definition: nat66.api:68
nat66_static_mapping_t
Definition: nat66.h:26
nat66_api_static_mapping_walk
static int nat66_api_static_mapping_walk(nat66_static_mapping_t *m, void *arg)
Definition: nat66_api.c:131
vl_api_nat66_interface_dump_t::context
u32 context
Definition: nat66.api:57
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
nat66_plugin_api_hookup
clib_error_t * nat66_plugin_api_hookup(vlib_main_t *vm)
Definition: nat66_api.c:189