2 * Copyright (c) 2017 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 <vlibapi/api.h>
17 #include <vlibmemory/api.h>
19 #include <plugins/stn/stn.h>
20 #include <vnet/ip/format.h>
22 #include <vppinfra/byte_order.h>
24 /* define message IDs */
25 #define vl_msg_id(n,h) n,
28 #include <stn/stn.api.h>
29 /* We'll want to know how many messages IDs we need... */
30 VL_MSG_FIRST_AVAILABLE,
34 /* define message structures */
36 #include <stn/stn.api.h>
39 /* define generated endian-swappers */
41 #include <stn/stn.api.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <stn/stn.api.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <nat/nat_all_api_h.h>
55 #define REPLY_MSG_ID_BASE stn_main.msg_id_base
56 #include <vlibapi/api_helper_macros.h>
58 /* Macro to finish up custom dump fns */
61 vl_print (handle, (char *)s); \
66 * @brief API message custom-dump function
67 * @param mp vl_api_stn_add_del_rule_t * mp the api message
68 * @param handle void * print function handle
69 * @returns u8 * output string
71 static void *vl_api_stn_add_del_rule_t_print
72 (vl_api_stn_add_del_rule_t * mp, void *handle)
76 s = format (0, "SCRIPT: stn_add_del_rule ");
78 s = format (s, "address %U ", format_ip4_address, mp->ip_address);
80 s = format (s, "address %U ", format_ip6_address, mp->ip_address);
81 s = format (s, "sw_if_index %d is_add %d", mp->sw_if_index, mp->is_add);
87 vl_api_stn_add_del_rule_t_handler (vl_api_stn_add_del_rule_t * mp)
89 stn_rule_add_del_args_t args;
90 vl_api_stn_add_del_rule_reply_t *rmp;
96 memcpy (&a, mp->ip_address, sizeof (a));
97 ip46_address_set_ip4 (&args.address, &a);
100 memcpy (&args.address.ip6, mp->ip_address, sizeof (ip6_address_t));
102 args.sw_if_index = clib_net_to_host_u32 (mp->sw_if_index);
103 args.del = !mp->is_add;
105 rv = stn_rule_add_del (&args);
107 REPLY_MACRO (VL_API_STN_ADD_DEL_RULE_REPLY);
111 send_stn_rule_details (stn_rule_t * r, svm_queue_t * q, u32 context)
113 vl_api_stn_rule_details_t *rmp;
115 rmp = vl_msg_api_alloc (sizeof (*rmp));
116 memset (rmp, 0, sizeof (*rmp));
118 clib_host_to_net_u32 (VL_API_STN_RULE_DETAILS + stn_main.msg_id_base);
120 if (ip46_address_is_ip4 (&r->address))
122 clib_memcpy (rmp->ip_address, &r->address.ip4, sizeof (ip4_address_t));
127 clib_memcpy (rmp->ip_address, &r->address.ip6, sizeof (ip6_address_t));
130 rmp->context = context;
131 rmp->sw_if_index = clib_host_to_net_u32 (r->sw_if_index);
133 vl_msg_api_send_shmem (q, (u8 *) & rmp);
137 vl_api_stn_rules_dump_t_handler (vl_api_stn_rules_dump_t * mp)
140 stn_main_t *stn = &stn_main;
143 q = vl_api_client_index_to_input_queue (mp->client_index);
148 pool_foreach (r, stn->rules,({
149 send_stn_rule_details (r, q, mp->context);
155 /* List of message types that this plugin understands */
156 #define foreach_stn_plugin_api_msg \
157 _(STN_ADD_DEL_RULE, stn_add_del_rule) \
158 _(STN_RULES_DUMP, stn_rules_dump)
161 * @brief Set up the API message handling tables
162 * @param vm vlib_main_t * vlib main data structure pointer
163 * @returns 0 to indicate all is well
165 static clib_error_t *
166 stn_plugin_api_hookup (vlib_main_t * vm)
168 stn_main_t *stn = &stn_main;
170 vl_msg_api_set_handlers((VL_API_##N + stn->msg_id_base), \
172 vl_api_##n##_t_handler, \
174 vl_api_##n##_t_endian, \
175 vl_api_##n##_t_print, \
176 sizeof(vl_api_##n##_t), 1);
177 foreach_stn_plugin_api_msg;
183 #define vl_msg_name_crc_list
184 #include <stn/stn.api.h>
185 #undef vl_msg_name_crc_list
188 setup_message_id_table (stn_main_t * stn, api_main_t * am)
190 #define _(id,n,crc) \
191 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + stn->msg_id_base);
192 foreach_vl_msg_name_crc_stn;
197 plugin_custom_dump_configure (stn_main_t * stn)
199 #define _(n,f) api_main.msg_print_handlers \
200 [VL_API_##n + stn->msg_id_base] \
201 = (void *) vl_api_##f##_t_print;
202 foreach_stn_plugin_api_msg;
207 stn_api_init (vlib_main_t * vm, stn_main_t * sm)
210 clib_error_t *error = 0;
212 name = format (0, "stn_%08x%c", api_version, 0);
214 /* Ask for a correctly-sized block of API message decode slots */
215 sm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name,
216 VL_MSG_FIRST_AVAILABLE);
218 error = stn_plugin_api_hookup (vm);
220 /* Add our API messages to the global name_crc hash table */
221 setup_message_id_table (sm, &api_main);
223 plugin_custom_dump_configure (sm);
231 * fd.io coding-style-patch-verification: ON
234 * eval: (c-set-style "gnu")