FD.io VPP  v20.09-64-g4f7b92f0a
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 /*************/
27 /*** NAT66 ***/
28 /*************/
29 
30 static void
32  mp)
33 {
34  nat66_main_t *nm = &nat66_main;
35  vl_api_nat66_add_del_interface_reply_t *rmp;
36  int rv = 0;
37 
39 
40  rv =
42  mp->flags & NAT_IS_INSIDE, mp->is_add);
43 
45 
46  REPLY_MACRO (VL_API_NAT66_ADD_DEL_INTERFACE_REPLY);
47 }
48 
49 static void
52 {
53  nat66_main_t *nm = &nat66_main;
54  vl_api_nat66_add_del_static_mapping_reply_t *rmp;
55  ip6_address_t l_addr, e_addr;
56  int rv = 0;
57 
58  memcpy (&l_addr.as_u8, mp->local_ip_address, 16);
59  memcpy (&e_addr.as_u8, mp->external_ip_address, 16);
60 
61  rv =
62  nat66_static_mapping_add_del (&l_addr, &e_addr,
63  clib_net_to_host_u32 (mp->vrf_id),
64  mp->is_add);
65 
66  REPLY_MACRO (VL_API_NAT66_ADD_DEL_STATIC_MAPPING_REPLY);
67 }
68 
69 typedef struct nat66_api_walk_ctx_t_
70 {
74 
75 static int
77 {
79  nat66_main_t *nm = &nat66_main;
81 
82  rmp = vl_msg_api_alloc (sizeof (*rmp));
83  clib_memset (rmp, 0, sizeof (*rmp));
84  rmp->_vl_msg_id = ntohs (VL_API_NAT66_INTERFACE_DETAILS + nm->msg_id_base);
85  rmp->sw_if_index = ntohl (i->sw_if_index);
87  rmp->flags |= NAT_IS_INSIDE;
88  rmp->context = ctx->context;
89 
90  vl_api_send_msg (ctx->rp, (u8 *) rmp);
91 
92  return 0;
93 }
94 
95 static void
97 {
99 
101  if (rp == 0)
102  return;
103 
105  .rp = rp,
106  .context = mp->context,
107  };
108 
110 }
111 
112 static int
114 {
116  nat66_main_t *nm = &nat66_main;
117  nat66_api_walk_ctx_t *ctx = arg;
118  fib_table_t *fib;
119  vlib_counter_t vc;
120 
122  if (!fib)
123  return -1;
124 
125  vlib_get_combined_counter (&nm->session_counters, m - nm->sm, &vc);
126 
127  rmp = vl_msg_api_alloc (sizeof (*rmp));
128  clib_memset (rmp, 0, sizeof (*rmp));
129  rmp->_vl_msg_id =
130  ntohs (VL_API_NAT66_STATIC_MAPPING_DETAILS + nm->msg_id_base);
131  clib_memcpy (rmp->local_ip_address, &m->l_addr, 16);
132  clib_memcpy (rmp->external_ip_address, &m->e_addr, 16);
133  rmp->vrf_id = ntohl (fib->ft_table_id);
134  rmp->total_bytes = clib_host_to_net_u64 (vc.bytes);
135  rmp->total_pkts = clib_host_to_net_u64 (vc.packets);
136  rmp->context = ctx->context;
137 
138  vl_api_send_msg (ctx->rp, (u8 *) rmp);
139 
140  return 0;
141 }
142 
143 static void
145  * mp)
146 {
148 
150  if (rp == 0)
151  return;
152 
154  .rp = rp,
155  .context = mp->context,
156  };
157 
159 }
160 
161 /* API definitions */
162 #include <vnet/format_fns.h>
163 #include <nat/nat66/nat66.api.c>
164 
165 /* Set up the API message handling tables */
166 clib_error_t *
168 {
169  nat66_main_t *nm = &nat66_main;
170 
172 
173  return 0;
174 }
vl_api_nat_config_flags_t flags
Definition: nat66.api:54
#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:76
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:95
Combined counter to hold both packets and byte differences.
Definition: counter_types.h:26
Dump NAT66 static mappings.
Definition: nat66.api:79
vlib_main_t * vm
Definition: in2out_ed.c:1582
void * vl_msg_api_alloc(int nbytes)
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
#define nat66_interface_is_inside(i)
Definition: nat66.h:53
nat66_main_t nat66_main
Definition: nat66.c:26
NAT66 interface details response.
Definition: nat66.api:52
u32 sw_if_index
Definition: nat66.h:48
vl_api_registration_t * rp
Definition: nat66_api.c:71
clib_error_t * nat66_plugin_api_hookup(vlib_main_t *vm)
Definition: nat66_api.c:167
unsigned int u32
Definition: types.h:88
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:70
static void vl_api_nat66_add_del_interface_t_handler(vl_api_nat66_add_del_interface_t *mp)
Definition: nat66_api.c:31
void nat66_static_mappings_walk(nat66_static_mapping_walk_fn_t fn, void *ctx)
Definition: nat66.c:254
counter_t packets
packet counter
Definition: counter_types.h:28
long ctx[MAX_CONNS]
Definition: main.c:144
#define REPLY_MACRO(t)
Enable/disable NAT66 feature on the interface.
Definition: nat66.api:29
vl_api_ip6_address_t external_ip_address
Definition: nat66.api:71
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
#define BAD_SW_IF_INDEX_LABEL
vl_api_interface_index_t sw_if_index
Definition: nat66.api:34
int nat66_interface_add_del(u32 sw_if_index, u8 is_inside, u8 is_add)
Definition: nat66.c:90
u32 ft_table_id
Table ID (hash key) for this FIB.
Definition: fib_table.h:92
ip6_address_t e_addr
Definition: nat66.h:28
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:259
struct nat66_api_walk_ctx_t_ nat66_api_walk_ctx_t
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
NAT66 static mapping details response.
Definition: nat66.api:92
vl_api_nat_config_flags_t flags
Definition: nat66.api:33
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:51
static void vl_api_nat66_static_mapping_dump_t_handler(vl_api_nat66_static_mapping_dump_t *mp)
Definition: nat66_api.c:144
counter_t bytes
byte counter
Definition: counter_types.h:29
ip6_address_t l_addr
Definition: nat66.h:27
vlib_combined_counter_main_t session_counters
Session counters.
Definition: nat66.h:67
static int nat66_api_static_mapping_walk(nat66_static_mapping_t *m, void *arg)
Definition: nat66_api.c:113
void nat66_interfaces_walk(nat66_interface_walk_fn_t fn, void *ctx)
Definition: nat66.c:136
Dump interfaces with NAT66 feature.
Definition: nat66.api:41
Add/delete 1:1 NAT66.
Definition: nat66.api:66
vl_api_interface_index_t sw_if_index
Definition: nat66.api:55
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:175
static void setup_message_id_table(snat_main_t *sm, api_main_t *am)
Definition: nat_api.c:2804
nat66_static_mapping_t * sm
Static mapping pool.
Definition: nat66.h:61
static void vl_api_nat66_interface_dump_t_handler(vl_api_nat66_interface_dump_t *mp)
Definition: nat66_api.c:96
NAT66 global declarations.
vl_api_ip6_address_t local_ip_address
Definition: nat66.api:94
#define VALIDATE_SW_IF_INDEX(mp)
A protocol Independent FIB table.
Definition: fib_table.h:71