FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
pnat_api.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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 #include "pnat.h"
16 #include <pnat/pnat.api_enum.h>
17 #include <pnat/pnat.api_types.h>
18 #include <vlibmemory/api.h>
19 #include <vnet/fib/fib_table.h>
20 #include <vnet/ip/ip.h>
21 #include <vnet/ip/ip_types_api.h>
25 #include <vpp/app/version.h>
26 
27 /*
28  * This file contains the API handlers for the pnat.api
29  */
30 
31 #define REPLY_MSG_ID_BASE pm->msg_id_base
33 
35  pnat_main_t *pm = &pnat_main;
37  u32 binding_index;
38  int rv = pnat_binding_add(&mp->match, &mp->rewrite, &binding_index);
39  REPLY_MACRO2_END(VL_API_PNAT_BINDING_ADD_REPLY,
40  ({ rmp->binding_index = binding_index; }));
41 }
42 
43 static void
45  pnat_main_t *pm = &pnat_main;
46  vl_api_pnat_binding_attach_reply_t *rmp;
47  int rv;
48 
49  /* Ensure that the interface exists */
51  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
52  goto bad_sw_if_index;
53  }
54 
55  rv =
57 
58 bad_sw_if_index:
59  REPLY_MACRO_END(VL_API_PNAT_BINDING_ATTACH_REPLY);
60 }
61 
62 static void
64  pnat_main_t *pm = &pnat_main;
65  vl_api_pnat_binding_detach_reply_t *rmp;
66  int rv;
67 
68  /* Ensure that the interface exists */
70  rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
71  goto bad_sw_if_index;
72  }
73 
74  rv =
76 
77 bad_sw_if_index:
78  REPLY_MACRO_END(VL_API_PNAT_BINDING_DETACH_REPLY);
79 }
80 
82  pnat_main_t *pm = &pnat_main;
83  vl_api_pnat_binding_del_reply_t *rmp;
85  REPLY_MACRO_END(VL_API_PNAT_BINDING_DEL_REPLY);
86 }
87 
88 /*
89  * Workaround for a bug in vppapigen that doesn't register the endian handler
90  * for _details messages. When that's fixed it should be possible to use
91  * REPLY_MACRO_DETAILS4_END and not have to care about endian-ness in the
92  * handler itself.
93  */
94 #define vl_endianfun
95 #include <pnat/pnat.api.h>
96 #undef vl_endianfun
98  u32 context) {
99  pnat_main_t *pm = &pnat_main;
102 
103  /* Make sure every field is initiated (or don't skip the clib_memset()) */
104 
105  REPLY_MACRO_DETAILS4(VL_API_PNAT_BINDINGS_DETAILS, rp, context, ({
106  rmp->match = t->match;
107  rmp->rewrite = t->rewrite;
108 
109  /* Endian hack until apigen registers _details
110  * endian functions */
111  vl_api_pnat_bindings_details_t_endian(rmp);
112  rmp->_vl_msg_id = htons(rmp->_vl_msg_id);
113  rmp->context = htonl(rmp->context);
114  }));
115 }
116 
118  pnat_main_t *pm = &pnat_main;
120 
121  i32 rv = 0;
122 
123  if (pool_elts(pm->translations) == 0) {
124  REPLY_MACRO(VL_API_PNAT_BINDINGS_GET_REPLY);
125  return;
126  }
127 
128  /*
129  * "cursor" comes from the get call, and allows client to continue a dump
130  */
131  REPLY_AND_DETAILS_MACRO(VL_API_PNAT_BINDINGS_GET_REPLY, pm->translations, ({
132  send_bindings_details(cursor, rp, mp->context);
133  }));
134 }
135 
137  u32 context) {
138  pnat_main_t *pm = &pnat_main;
141 
142  /* Make sure every field is initiated (or don't skip the clib_memset()) */
143 
145  VL_API_PNAT_INTERFACES_DETAILS, rp, context, ({
146  rmp->sw_if_index = i->sw_if_index;
147  clib_memcpy(rmp->enabled, i->enabled, sizeof(rmp->enabled));
148  clib_memcpy(rmp->lookup_mask, i->lookup_mask,
149  sizeof(rmp->lookup_mask));
150 
151  /* Endian hack until apigen registers _details
152  * endian functions */
153  vl_api_pnat_interfaces_details_t_endian(rmp);
154  rmp->_vl_msg_id = htons(rmp->_vl_msg_id);
155  rmp->context = htonl(rmp->context);
156  }));
157 }
158 
159 static void
161  pnat_main_t *pm = &pnat_main;
163 
164  i32 rv = 0;
165 
166  if (pool_elts(pm->interfaces) == 0) {
167  REPLY_MACRO(VL_API_PNAT_INTERFACES_GET_REPLY);
168  return;
169  }
170 
171  /*
172  * "cursor" comes from the get call, and allows client to continue a dump
173  */
175  VL_API_PNAT_INTERFACES_GET_REPLY, pm->interfaces,
176  ({ send_interfaces_details(cursor, rp, mp->context); }));
177 }
178 
179 /* API definitions */
180 #include <vnet/format_fns.h>
181 #include <pnat/pnat.api.c>
182 
183 /* Set up the API message handling tables */
185  pnat_main_t *pm = &pnat_main;
186 
188 
189  return 0;
190 }
191 
192 /*
193  * Register the plugin and hook up the API
194  */
195 #include <vnet/plugin/plugin.h>
197  .version = VPP_BUILD_VER,
198  .description = "Policy 1:1 NAT",
199 };
200 
202  pnat_main_t *pm = &pnat_main;
203  memset(pm, 0, sizeof(*pm));
204 
205  return pnat_plugin_api_hookup(vm);
206 }
207 
vl_api_pnat_bindings_details_t::context
u32 context
Definition: pnat.api:123
api.h
REPLY_MACRO_END
#define REPLY_MACRO_END(t)
Definition: api_helper_macros.h:46
ip6_full_reass.h
IPv6 Reassembly.
REPLY_MACRO2_END
#define REPLY_MACRO2_END(t, body)
Definition: api_helper_macros.h:81
pnat_binding_attach
int pnat_binding_attach(u32 sw_if_index, pnat_attachment_point_t attachment, u32 binding_index)
Definition: pnat.c:329
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
pnat_binding_del
int pnat_binding_del(u32 index)
Definition: pnat.c:392
vl_api_pnat_binding_detach_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: pnat.api:95
pool_elt_at_index
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:549
vl_api_pnat_bindings_details_t
Definition: pnat.api:121
vl_api_pnat_binding_add_t_handler
static void vl_api_pnat_binding_add_t_handler(vl_api_pnat_binding_add_t *mp)
Definition: pnat_api.c:34
vl_api_pnat_interfaces_get_reply_t
Definition: pnat.api:135
pnat_translation_t::rewrite
pnat_rewrite_tuple_t rewrite
Definition: pnat.h:66
vl_api_pnat_binding_attach_t_handler
static void vl_api_pnat_binding_attach_t_handler(vl_api_pnat_binding_attach_t *mp)
Definition: pnat_api.c:44
vl_api_pnat_bindings_get_t
Definition: pnat.api:107
fib_table.h
vl_api_pnat_bindings_get_reply_t
Definition: pnat.api:114
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_pnat_binding_add_reply_t
Definition: pnat.api:68
vl_api_pnat_binding_add_t::rewrite
vl_api_pnat_rewrite_tuple_t rewrite
Definition: pnat.api:65
vl_api_pnat_binding_detach_t_handler
static void vl_api_pnat_binding_detach_t_handler(vl_api_pnat_binding_detach_t *mp)
Definition: pnat_api.c:63
vl_api_pnat_binding_detach_t::binding_index
u32 binding_index
Definition: pnat.api:97
pnat_main_t
Definition: pnat.h:82
i32
signed int i32
Definition: types.h:77
vl_api_pnat_binding_add_t::match
vl_api_pnat_match_tuple_t match
Definition: pnat.api:64
vl_api_pnat_binding_add_reply_t::binding_index
u32 binding_index
Definition: pnat.api:72
vl_api_pnat_interfaces_get_t_handler
static void vl_api_pnat_interfaces_get_t_handler(vl_api_pnat_interfaces_get_t *mp)
Definition: pnat_api.c:160
pnat_init
clib_error_t * pnat_init(vlib_main_t *vm)
Definition: pnat_api.c:201
vl_api_pnat_interfaces_get_t
Definition: pnat.api:128
pnat.h
ip6_sv_reass.h
IPv6 shallow virtual reassembly.
ip4_sv_reass.h
IPv4 shallow virtual reassembly.
pnat_binding_detach
int pnat_binding_detach(u32 sw_if_index, pnat_attachment_point_t attachment, u32 binding_index)
Definition: pnat.c:364
pnat_main
pnat_main_t pnat_main
Definition: pnat.c:32
pnat_main_t::translations
pnat_translation_t * translations
Definition: pnat.h:92
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_pnat_binding_add_t
Definition: pnat.api:60
vl_api_pnat_binding_del_t_handler
static void vl_api_pnat_binding_del_t_handler(vl_api_pnat_binding_del_t *mp)
Definition: pnat_api.c:81
REPLY_MACRO_DETAILS4
#define REPLY_MACRO_DETAILS4(t, rp, context, body)
Definition: api_helper_macros.h:132
vl_api_pnat_binding_attach_t::binding_index
u32 binding_index
Definition: pnat.api:88
vl_api_pnat_binding_del_t
Definition: pnat.api:75
vl_api_pnat_bindings_details_t::match
vl_api_pnat_match_tuple_t match
Definition: pnat.api:124
vl_api_pnat_bindings_details_t::rewrite
vl_api_pnat_rewrite_tuple_t rewrite
Definition: pnat.api:125
vl_api_pnat_interfaces_details_t
Definition: pnat.api:142
vl_api_pnat_binding_attach_t
Definition: pnat.api:82
REPLY_AND_DETAILS_MACRO
#define REPLY_AND_DETAILS_MACRO(t, p, body)
Definition: api_helper_macros.h:204
pnat_main_t::msg_id_base
u16 msg_id_base
Definition: pnat.h:94
vl_api_pnat_binding_del_t::binding_index
u32 binding_index
Definition: pnat.api:79
vl_api_pnat_interfaces_details_t::enabled
bool enabled[2]
Definition: pnat.api:146
plugin.h
vl_api_pnat_interfaces_details_t::lookup_mask
vl_api_pnat_mask_t lookup_mask[2]
Definition: pnat.api:147
vnet_sw_if_index_is_api_valid
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
Definition: api_helper_macros.h:276
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
index
u32 index
Definition: flow_types.api:221
vl_api_pnat_binding_attach_t::attachment
vl_api_pnat_attachment_point_t attachment
Definition: pnat.api:87
vl_api_pnat_binding_attach_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: pnat.api:86
pnat_translation_t
Definition: pnat.h:48
format_fns.h
vl_api_pnat_binding_detach_t::attachment
vl_api_pnat_attachment_point_t attachment
Definition: pnat.api:96
vl_api_pnat_interfaces_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: pnat.api:145
pnat_interface_t
Definition: pnat.h:70
ip.h
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
send_interfaces_details
static void send_interfaces_details(u32 index, vl_api_registration_t *rp, u32 context)
Definition: pnat_api.c:136
api_helper_macros.h
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
vl_api_pnat_interfaces_details_t::context
u32 context
Definition: pnat.api:144
vlib_main_t
Definition: main.h:102
vl_api_pnat_binding_detach_t
Definition: pnat.api:91
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
pnat_plugin_api_hookup
clib_error_t * pnat_plugin_api_hookup(vlib_main_t *vm)
Definition: pnat_api.c:184
pnat_translation_t::match
pnat_match_tuple_t match
Definition: pnat.h:65
i
int i
Definition: flowhash_template.h:376
context
u32 context
Definition: ip.api:852
rv
int __clib_unused rv
Definition: application.c:491
pnat_main_t::interfaces
pnat_interface_t * interfaces
Definition: pnat.h:88
ip_types_api.h
pnat_binding_add
int pnat_binding_add(pnat_match_tuple_t *match, pnat_rewrite_tuple_t *rewrite, u32 *index)
Definition: pnat.c:271
send_bindings_details
static void send_bindings_details(u32 index, vl_api_registration_t *rp, u32 context)
Definition: pnat_api.c:97
vl_api_pnat_bindings_get_t_handler
static void vl_api_pnat_bindings_get_t_handler(vl_api_pnat_bindings_get_t *mp)
Definition: pnat_api.c:117