2 * Copyright (c) 2016 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.
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <abf/abf_policy.h>
21 #include <abf/abf_itf_attach.h>
22 #include <vnet/mpls/mpls_types.h>
23 #include <vnet/fib/fib_path_list.h>
24 #include <vnet/fib/fib_api.h>
26 #include <vpp/app/version.h>
28 #include <vlibapi/api.h>
29 #include <vlibmemory/api.h>
31 /* define message IDs */
32 #include <abf/abf_msg_enum.h>
34 /* define message structures */
36 #include <abf/abf_all_api_h.h>
39 /* define generated endian-swappers */
41 #include <abf/abf_all_api_h.h>
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
47 #include <abf/abf_all_api_h.h>
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <abf/abf_all_api_h.h>
56 * Base message ID fot the plugin
58 static u32 abf_base_msg_id;
60 #include <vlibapi/api_helper_macros.h>
62 /* List of message types that this plugin understands */
64 #define foreach_abf_plugin_api_msg \
65 _(ABF_PLUGIN_GET_VERSION, abf_plugin_get_version) \
66 _(ABF_POLICY_ADD_DEL, abf_policy_add_del) \
67 _(ABF_POLICY_DUMP, abf_policy_dump) \
68 _(ABF_ITF_ATTACH_ADD_DEL, abf_itf_attach_add_del) \
69 _(ABF_ITF_ATTACH_DUMP, abf_itf_attach_dump)
72 vl_api_abf_plugin_get_version_t_handler (vl_api_abf_plugin_get_version_t * mp)
74 vl_api_abf_plugin_get_version_reply_t *rmp;
75 vl_api_registration_t *rp;
77 rp = vl_api_client_index_to_registration (mp->client_index);
81 rmp = vl_msg_api_alloc (sizeof (*rmp));
83 ntohs (VL_API_ABF_PLUGIN_GET_VERSION_REPLY + abf_base_msg_id);
84 rmp->context = mp->context;
85 rmp->major = htonl (ABF_PLUGIN_VERSION_MAJOR);
86 rmp->minor = htonl (ABF_PLUGIN_VERSION_MINOR);
88 vl_api_send_msg (rp, (u8 *) rmp);
92 vl_api_abf_policy_add_del_t_handler (vl_api_abf_policy_add_del_t * mp)
94 vl_api_abf_policy_add_del_reply_t *rmp;
95 fib_route_path_t *paths = NULL, *path;
99 vec_validate (paths, mp->policy.n_paths - 1);
101 for (pi = 0; pi < mp->policy.n_paths; pi++)
104 rv = fib_api_path_decode (&mp->policy.paths[pi], path);
114 rv = abf_policy_update (ntohl (mp->policy.policy_id),
115 ntohl (mp->policy.acl_index), paths);
119 rv = abf_policy_delete (ntohl (mp->policy.policy_id), paths);
124 REPLY_MACRO (VL_API_ABF_POLICY_ADD_DEL_REPLY + abf_base_msg_id);
128 vl_api_abf_itf_attach_add_del_t_handler (vl_api_abf_itf_attach_add_del_t * mp)
130 vl_api_abf_itf_attach_add_del_reply_t *rmp;
131 fib_protocol_t fproto = (mp->attach.is_ipv6 ?
132 FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
137 abf_itf_attach (fproto,
138 ntohl (mp->attach.policy_id),
139 ntohl (mp->attach.priority),
140 ntohl (mp->attach.sw_if_index));
144 abf_itf_detach (fproto,
145 ntohl (mp->attach.policy_id),
146 ntohl (mp->attach.sw_if_index));
149 REPLY_MACRO (VL_API_ABF_ITF_ATTACH_ADD_DEL_REPLY + abf_base_msg_id);
152 typedef struct abf_dump_walk_ctx_t_
154 vl_api_registration_t *rp;
156 } abf_dump_walk_ctx_t;
159 abf_policy_send_details (u32 api, void *args)
161 fib_path_encode_ctx_t walk_ctx = {
164 vl_api_abf_policy_details_t *mp;
165 abf_dump_walk_ctx_t *ctx;
166 fib_route_path_t *rpath;
167 vl_api_fib_path_t *fp;
173 ap = abf_policy_get (api);
174 n_paths = fib_path_list_get_n_paths (ap->ap_pl);
175 msg_size = sizeof (*mp) + sizeof (mp->policy.paths[0]) * n_paths;
177 mp = vl_msg_api_alloc (msg_size);
178 clib_memset (mp, 0, msg_size);
179 mp->_vl_msg_id = ntohs (VL_API_ABF_POLICY_DETAILS + abf_base_msg_id);
181 /* fill in the message */
182 mp->context = ctx->context;
183 mp->policy.n_paths = n_paths;
184 mp->policy.acl_index = htonl (ap->ap_acl);
185 mp->policy.policy_id = htonl (ap->ap_id);
187 fib_path_list_walk_w_ext (ap->ap_pl, NULL, fib_path_encode, &walk_ctx);
189 fp = mp->policy.paths;
190 vec_foreach (rpath, walk_ctx.rpaths)
192 fib_api_path_encode (rpath, fp);
196 vl_api_send_msg (ctx->rp, (u8 *) mp);
198 vec_free (walk_ctx.rpaths);
204 vl_api_abf_policy_dump_t_handler (vl_api_abf_policy_dump_t * mp)
206 vl_api_registration_t *rp;
208 rp = vl_api_client_index_to_registration (mp->client_index);
212 abf_dump_walk_ctx_t ctx = {
214 .context = mp->context,
217 abf_policy_walk (abf_policy_send_details, &ctx);
221 abf_itf_attach_send_details (u32 aiai, void *args)
223 vl_api_abf_itf_attach_details_t *mp;
224 abf_dump_walk_ctx_t *ctx;
225 abf_itf_attach_t *aia;
229 aia = abf_itf_attach_get (aiai);
230 ap = abf_policy_get (aia->aia_abf);
232 mp = vl_msg_api_alloc (sizeof (*mp));
233 mp->_vl_msg_id = ntohs (VL_API_ABF_ITF_ATTACH_DETAILS + abf_base_msg_id);
235 mp->context = ctx->context;
236 mp->attach.policy_id = htonl (ap->ap_id);
237 mp->attach.sw_if_index = htonl (aia->aia_sw_if_index);
238 mp->attach.priority = htonl (aia->aia_prio);
239 mp->attach.is_ipv6 = (aia->aia_proto == FIB_PROTOCOL_IP6);
241 vl_api_send_msg (ctx->rp, (u8 *) mp);
247 vl_api_abf_itf_attach_dump_t_handler (vl_api_abf_itf_attach_dump_t * mp)
249 vl_api_registration_t *rp;
251 rp = vl_api_client_index_to_registration (mp->client_index);
255 abf_dump_walk_ctx_t ctx = {
257 .context = mp->context,
260 abf_itf_attach_walk (abf_itf_attach_send_details, &ctx);
263 #define vl_msg_name_crc_list
264 #include <abf/abf_all_api_h.h>
265 #undef vl_msg_name_crc_list
267 /* Set up the API message handling tables */
268 static clib_error_t *
269 abf_plugin_api_hookup (vlib_main_t * vm)
272 vl_msg_api_set_handlers((VL_API_##N + abf_base_msg_id), \
274 vl_api_##n##_t_handler, \
276 vl_api_##n##_t_endian, \
277 vl_api_##n##_t_print, \
278 sizeof(vl_api_##n##_t), 1);
279 foreach_abf_plugin_api_msg;
286 setup_message_id_table (api_main_t * apim)
288 #define _(id,n,crc) \
289 vl_msg_api_add_msg_name_crc (apim, #n "_" #crc, id + abf_base_msg_id);
290 foreach_vl_msg_name_crc_abf;
294 static clib_error_t *
295 abf_api_init (vlib_main_t * vm)
297 clib_error_t *error = 0;
299 u8 *name = format (0, "abf_%08x%c", api_version, 0);
301 /* Ask for a correctly-sized block of API message decode slots */
302 abf_base_msg_id = vl_msg_api_get_msg_ids ((char *) name,
303 VL_MSG_FIRST_AVAILABLE);
305 error = abf_plugin_api_hookup (vm);
307 /* Add our API messages to the global name_crc hash table */
308 setup_message_id_table (&api_main);
315 VLIB_INIT_FUNCTION (abf_api_init);
318 VLIB_PLUGIN_REGISTER () = {
319 .version = VPP_BUILD_VER,
320 .description = "Access Control List (ACL) Based Forwarding",
325 * fd.io coding-style-patch-verification: ON
328 * eval: (c-set-style "gnu")