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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
22 #include <vnet/ip/reass/ip4_sv_reass.h>
23 #include <vnet/ip/reass/ip6_full_reass.h>
24 #include <vnet/ip/reass/ip6_sv_reass.h>
25 #include <vpp/app/version.h>
28 * This file contains the API handlers for the pnat.api
31 #define REPLY_MSG_ID_BASE pm->msg_id_base
32 #include <vlibapi/api_helper_macros.h>
34 static void vl_api_pnat_binding_add_t_handler(vl_api_pnat_binding_add_t *mp) {
35 pnat_main_t *pm = &pnat_main;
36 vl_api_pnat_binding_add_reply_t *rmp;
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; }));
44 vl_api_pnat_binding_attach_t_handler(vl_api_pnat_binding_attach_t *mp) {
45 pnat_main_t *pm = &pnat_main;
46 vl_api_pnat_binding_attach_reply_t *rmp;
49 /* Ensure that the interface exists */
50 if (!vnet_sw_if_index_is_api_valid(mp->sw_if_index)) {
51 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
56 pnat_binding_attach(mp->sw_if_index, mp->attachment, mp->binding_index);
59 REPLY_MACRO_END(VL_API_PNAT_BINDING_ATTACH_REPLY);
63 vl_api_pnat_binding_detach_t_handler(vl_api_pnat_binding_detach_t *mp) {
64 pnat_main_t *pm = &pnat_main;
65 vl_api_pnat_binding_detach_reply_t *rmp;
68 /* Ensure that the interface exists */
69 if (!vnet_sw_if_index_is_api_valid(mp->sw_if_index)) {
70 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX;
75 pnat_binding_detach(mp->sw_if_index, mp->attachment, mp->binding_index);
78 REPLY_MACRO_END(VL_API_PNAT_BINDING_DETACH_REPLY);
81 static void vl_api_pnat_binding_del_t_handler(vl_api_pnat_binding_del_t *mp) {
82 pnat_main_t *pm = &pnat_main;
83 vl_api_pnat_binding_del_reply_t *rmp;
84 int rv = pnat_binding_del(mp->binding_index);
85 REPLY_MACRO_END(VL_API_PNAT_BINDING_DEL_REPLY);
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
95 #include <pnat/pnat.api.h>
97 static void send_bindings_details(u32 index, vl_api_registration_t *rp,
99 pnat_main_t *pm = &pnat_main;
100 vl_api_pnat_bindings_details_t *rmp;
101 pnat_translation_t *t = pool_elt_at_index(pm->translations, index);
103 /* Make sure every field is initiated (or don't skip the clib_memset()) */
105 REPLY_MACRO_DETAILS4(VL_API_PNAT_BINDINGS_DETAILS, rp, context, ({
106 rmp->match = t->match;
107 rmp->rewrite = t->rewrite;
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);
117 static void vl_api_pnat_bindings_get_t_handler(vl_api_pnat_bindings_get_t *mp) {
118 pnat_main_t *pm = &pnat_main;
119 vl_api_pnat_bindings_get_reply_t *rmp;
123 if (pool_elts(pm->translations) == 0) {
124 REPLY_MACRO(VL_API_PNAT_BINDINGS_GET_REPLY);
129 * "cursor" comes from the get call, and allows client to continue a dump
131 REPLY_AND_DETAILS_MACRO(VL_API_PNAT_BINDINGS_GET_REPLY, pm->translations, ({
132 send_bindings_details(cursor, rp, mp->context);
136 static void send_interfaces_details(u32 index, vl_api_registration_t *rp,
138 pnat_main_t *pm = &pnat_main;
139 vl_api_pnat_interfaces_details_t *rmp;
140 pnat_interface_t *i = pool_elt_at_index(pm->interfaces, index);
142 /* Make sure every field is initiated (or don't skip the clib_memset()) */
144 REPLY_MACRO_DETAILS4(
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));
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);
160 vl_api_pnat_interfaces_get_t_handler(vl_api_pnat_interfaces_get_t *mp) {
161 pnat_main_t *pm = &pnat_main;
162 vl_api_pnat_interfaces_get_reply_t *rmp;
166 if (pool_elts(pm->interfaces) == 0) {
167 REPLY_MACRO(VL_API_PNAT_INTERFACES_GET_REPLY);
172 * "cursor" comes from the get call, and allows client to continue a dump
174 REPLY_AND_DETAILS_MACRO(
175 VL_API_PNAT_INTERFACES_GET_REPLY, pm->interfaces,
176 ({ send_interfaces_details(cursor, rp, mp->context); }));
179 /* API definitions */
180 #include <vnet/format_fns.h>
181 #include <pnat/pnat.api.c>
183 /* Set up the API message handling tables */
184 clib_error_t *pnat_plugin_api_hookup(vlib_main_t *vm) {
185 pnat_main_t *pm = &pnat_main;
187 pm->msg_id_base = setup_message_id_table();
193 * Register the plugin and hook up the API
195 #include <vnet/plugin/plugin.h>
196 VLIB_PLUGIN_REGISTER() = {
197 .version = VPP_BUILD_VER,
198 .description = "Policy 1:1 NAT",
201 clib_error_t *pnat_init(vlib_main_t *vm) {
202 pnat_main_t *pm = &pnat_main;
203 memset(pm, 0, sizeof(*pm));
205 return pnat_plugin_api_hookup(vm);
208 VLIB_INIT_FUNCTION(pnat_init);