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.
16 *------------------------------------------------------------------
17 * vxlan_gpe_api.c - iOAM VxLAN-GPE related APIs to create
18 * and maintain profiles
19 *------------------------------------------------------------------
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <ioam/lib-vxlan-gpe/vxlan_gpe_msg_enum.h>
33 /* define message structures */
35 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
55 * A handy macro to set up a message reply.
56 * Assumes that the following variables are available:
57 * mp - pointer to request message
58 * rmp - pointer to reply message type
62 #define VXLAN_GPE_REPLY_MACRO(t) \
64 unix_shared_memory_queue_t * q = \
65 vl_api_client_index_to_input_queue (mp->client_index); \
69 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
70 rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base); \
71 rmp->context = mp->context; \
72 rmp->retval = ntohl(rv); \
74 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
78 #define VXLAN_GPE_REPLY_MACRO2(t, body) \
80 unix_shared_memory_queue_t * q; \
81 rv = vl_msg_api_pd_handler (mp, rv); \
82 q = vl_api_client_index_to_input_queue (mp->client_index); \
86 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
87 rmp->_vl_msg_id = ntohs((t)); \
88 rmp->context = mp->context; \
89 rmp->retval = ntohl(rv); \
90 do {body;} while (0); \
91 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
95 /* List of message types that this plugin understands */
97 #define foreach_vxlan_gpe_plugin_api_msg \
98 _(VXLAN_GPE_IOAM_ENABLE, vxlan_gpe_ioam_enable) \
99 _(VXLAN_GPE_IOAM_DISABLE, vxlan_gpe_ioam_disable) \
100 _(VXLAN_GPE_IOAM_VNI_ENABLE, vxlan_gpe_ioam_vni_enable) \
101 _(VXLAN_GPE_IOAM_VNI_DISABLE, vxlan_gpe_ioam_vni_disable) \
102 _(VXLAN_GPE_IOAM_TRANSIT_ENABLE, vxlan_gpe_ioam_transit_enable) \
103 _(VXLAN_GPE_IOAM_TRANSIT_DISABLE, vxlan_gpe_ioam_transit_disable) \
106 static void vl_api_vxlan_gpe_ioam_enable_t_handler
107 (vl_api_vxlan_gpe_ioam_enable_t * mp)
110 vl_api_vxlan_gpe_ioam_enable_reply_t *rmp;
112 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
114 /* Ignoring the profile id as currently a single profile
117 vxlan_gpe_ioam_enable (mp->trace_enable, mp->pow_enable, mp->trace_ppc);
120 clib_error_report (error);
121 rv = clib_error_get_code (error);
124 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_ENABLE_REPLY);
127 static void vl_api_vxlan_gpe_ioam_disable_t_handler
128 (vl_api_vxlan_gpe_ioam_disable_t * mp)
131 vl_api_vxlan_gpe_ioam_disable_reply_t *rmp;
133 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
135 /* Ignoring the profile id as currently a single profile
137 error = vxlan_gpe_ioam_disable (0, 0, 0);
140 clib_error_report (error);
141 rv = clib_error_get_code (error);
144 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_DISABLE_REPLY);
147 static void vl_api_vxlan_gpe_ioam_vni_enable_t_handler
148 (vl_api_vxlan_gpe_ioam_vni_enable_t * mp)
151 vl_api_vxlan_gpe_ioam_vni_enable_reply_t *rmp;
153 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
154 vxlan4_gpe_tunnel_key_t key4;
156 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
157 vxlan_gpe_tunnel_t *t = 0;
158 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
164 clib_memcpy (&key4.local, &mp->local, sizeof (key4.local));
165 clib_memcpy (&key4.remote, &mp->remote, sizeof (key4.remote));
166 vni = clib_net_to_host_u32 (mp->vni);
167 key4.vni = clib_host_to_net_u32 (vni << 8);
170 p = hash_get_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
180 t = pool_elt_at_index (gm->tunnels, p[0]);
182 error = vxlan_gpe_ioam_set (t, hm->has_trace_option,
184 hm->has_ppc_option, mp->is_ipv6);
189 clib_error_report (error);
190 rv = clib_error_get_code (error);
193 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_VNI_ENABLE_REPLY);
197 static void vl_api_vxlan_gpe_ioam_vni_disable_t_handler
198 (vl_api_vxlan_gpe_ioam_vni_disable_t * mp)
201 vl_api_vxlan_gpe_ioam_vni_enable_reply_t *rmp;
203 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
204 vxlan4_gpe_tunnel_key_t key4;
206 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
207 vxlan_gpe_tunnel_t *t = 0;
213 clib_memcpy (&key4.local, &mp->local, sizeof (key4.local));
214 clib_memcpy (&key4.remote, &mp->remote, sizeof (key4.remote));
215 vni = clib_net_to_host_u32 (mp->vni);
216 key4.vni = clib_host_to_net_u32 (vni << 8);
219 p = hash_get_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
229 t = pool_elt_at_index (gm->tunnels, p[0]);
231 error = vxlan_gpe_ioam_clear (t, 0, 0, 0, 0);
236 clib_error_report (error);
237 rv = clib_error_get_code (error);
241 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_VNI_DISABLE_REPLY);
244 static void vl_api_vxlan_gpe_ioam_transit_enable_t_handler
245 (vl_api_vxlan_gpe_ioam_transit_enable_t * mp)
248 vl_api_vxlan_gpe_ioam_transit_enable_reply_t *rmp;
249 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
250 ip46_address_t dst_addr;
252 memset (&dst_addr.ip4, 0, sizeof (dst_addr.ip4));
255 clib_memcpy (&dst_addr.ip4, &mp->dst_addr, sizeof (dst_addr.ip4));
257 rv = vxlan_gpe_enable_disable_ioam_for_dest (sm->vlib_main,
259 ntohl (mp->outer_fib_index),
263 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_TRANSIT_ENABLE_REPLY);
266 static void vl_api_vxlan_gpe_ioam_transit_disable_t_handler
267 (vl_api_vxlan_gpe_ioam_transit_disable_t * mp)
270 vl_api_vxlan_gpe_ioam_transit_disable_reply_t *rmp;
271 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
272 ip46_address_t dst_addr;
274 memset (&dst_addr.ip4, 0, sizeof (dst_addr.ip4));
277 clib_memcpy (&dst_addr.ip4, &mp->dst_addr, sizeof (dst_addr.ip4));
280 rv = vxlan_gpe_ioam_disable_for_dest (sm->vlib_main,
282 ntohl (mp->outer_fib_index),
283 mp->is_ipv6 ? 0 : 1);
284 VXLAN_GPE_REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_TRANSIT_DISABLE_REPLY);
287 /* Set up the API message handling tables */
288 static clib_error_t *
289 vxlan_gpe_plugin_api_hookup (vlib_main_t * vm)
291 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
293 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
295 vl_api_##n##_t_handler, \
297 vl_api_##n##_t_endian, \
298 vl_api_##n##_t_print, \
299 sizeof(vl_api_##n##_t), 1);
300 foreach_vxlan_gpe_plugin_api_msg;
306 #define vl_msg_name_crc_list
307 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
308 #undef vl_msg_name_crc_list
311 setup_message_id_table (vxlan_gpe_ioam_main_t * sm, api_main_t * am)
313 #define _(id,n,crc) \
314 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
315 foreach_vl_msg_name_crc_ioam_vxlan_gpe;
319 static clib_error_t *
320 vxlan_gpe_init (vlib_main_t * vm)
322 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
323 clib_error_t *error = 0;
325 u32 encap_node_index = vxlan_gpe_encap_ioam_v4_node.index;
326 u32 decap_node_index = vxlan_gpe_decap_ioam_v4_node.index;
327 vlib_node_t *vxlan_gpe_encap_node = NULL;
328 vlib_node_t *vxlan_gpe_decap_node = NULL;
332 sm->vnet_main = vnet_get_main ();
333 sm->unix_time_0 = (u32) time (0); /* Store starting time */
334 sm->vlib_time_0 = vlib_time_now (vm);
336 name = format (0, "ioam_vxlan_gpe_%08x%c", api_version, 0);
338 /* Ask for a correctly-sized block of API message decode slots */
339 sm->msg_id_base = vl_msg_api_get_msg_ids
340 ((char *) name, VL_MSG_FIRST_AVAILABLE);
342 error = vxlan_gpe_plugin_api_hookup (vm);
344 /* Add our API messages to the global name_crc hash table */
345 setup_message_id_table (sm, &api_main);
347 /* Hook the ioam-encap node to vxlan-gpe-encap */
348 vxlan_gpe_encap_node = vlib_get_node_by_name (vm, (u8 *) "vxlan-gpe-encap");
349 sm->encap_v4_next_node =
350 vlib_node_add_next (vm, vxlan_gpe_encap_node->index, encap_node_index);
352 vxlan_gpe_decap_node =
353 vlib_get_node_by_name (vm, (u8 *) "vxlan4-gpe-input");
355 vlib_node_add_next (vm, vxlan_gpe_decap_node->index, decap_node_index);
356 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IOAM, next_node);
358 vec_new (vxlan_gpe_ioam_sw_interface_t, pool_elts (sm->sw_interfaces));
359 sm->dst_by_ip4 = hash_create_mem (0, sizeof (fib_prefix_t), sizeof (uword));
361 sm->dst_by_ip6 = hash_create_mem (0, sizeof (fib_prefix_t), sizeof (uword));
363 vxlan_gpe_ioam_interface_init ();
369 VLIB_INIT_FUNCTION (vxlan_gpe_init);
372 * fd.io coding-style-patch-verification: ON
375 * eval: (c-set-style "gnu")