FD.io VPP  v21.06-1-gbb7418cf9
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
39  rv = nat66_plugin_disable ();
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_nat_config_flags_t flags
Definition: nat66.api:68
#define ntohs(x)
Definition: af_xdp.bpf.c:29
u16 msg_id_base
Definition: nat66.h:75
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
static int nat66_api_interface_walk(nat66_interface_t *i, void *arg)
Definition: nat66_api.c:90
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:109
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
Dump NAT66 static mappings.
Definition: nat66.api:93
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
unsigned int u32
Definition: types.h:88
#define clib_memcpy(d, s, n)
Definition: string.h:197
#define nat66_interface_is_inside(i)
Definition: nat66.h:55
nat66_main_t nat66_main
Definition: nat66.c:25
NAT66 interface details response.
Definition: nat66.api:66
u32 sw_if_index
Definition: nat66.h:49
vl_api_registration_t * rp
Definition: nat66_api.c:85
u8 enabled
Definition: nat66.h:78
clib_error_t * nat66_plugin_api_hookup(vlib_main_t *vm)
Definition: nat66_api.c:189
int __clib_unused rv
Definition: application.c:491
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:84
int nat66_plugin_disable()
Definition: nat66.c:108
static void vl_api_nat66_add_del_interface_t_handler(vl_api_nat66_add_del_interface_t *mp)
Definition: nat66_api.c:45
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:320
counter_t packets
packet counter
Definition: counter_types.h:28
long ctx[MAX_CONNS]
Definition: main.c:144
#define PREDICT_FALSE(x)
Definition: clib.h:124
#define REPLY_MACRO(t)
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
static void vl_api_nat66_plugin_enable_disable_t_handler(vl_api_nat66_plugin_enable_disable_t *mp)
Definition: nat66_api.c:27
Enable/disable NAT66 feature on the interface.
Definition: nat66.api:43
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:85
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
static void setup_message_id_table(api_main_t *am)
Definition: bfd_api.c:451
#define BAD_SW_IF_INDEX_LABEL
vl_api_interface_index_t sw_if_index
Definition: nat66.api:48
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:156
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
ip6_address_t e_addr
Definition: nat66.h:29
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
struct nat66_api_walk_ctx_t_ nat66_api_walk_ctx_t
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
NAT66 static mapping details response.
Definition: nat66.api:106
vl_api_nat_config_flags_t flags
Definition: nat66.api:47
int nat66_plugin_enable(u32 outside_vrf)
Definition: nat66.c:86
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
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
static void vl_api_nat66_static_mapping_dump_t_handler(vl_api_nat66_static_mapping_dump_t *mp)
Definition: nat66_api.c:162
Enable/disable NAT66 plugin.
Definition: nat66.api:28
counter_t bytes
byte counter
Definition: counter_types.h:29
ip6_address_t l_addr
Definition: nat66.h:28
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:69
static int nat66_api_static_mapping_walk(nat66_static_mapping_t *m, void *arg)
Definition: nat66_api.c:131
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:202
Dump interfaces with NAT66 feature.
Definition: nat66.api:55
Add/delete 1:1 NAT66.
Definition: nat66.api:80
vl_api_interface_index_t sw_if_index
Definition: nat66.api:69
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
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
nat44_ei_main_t * nm
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:63
static void vl_api_nat66_interface_dump_t_handler(vl_api_nat66_interface_dump_t *mp)
Definition: nat66_api.c:110
NAT66 global declarations.
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:108
#define VALIDATE_SW_IF_INDEX(mp)
A protocol Independent FIB table.
Definition: fib_table.h:71