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>
25 #include <vlibapi/api_helper_macros.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/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>
54 /* List of message types that this plugin understands */
56 #define foreach_vxlan_gpe_plugin_api_msg \
57 _(VXLAN_GPE_IOAM_ENABLE, vxlan_gpe_ioam_enable) \
58 _(VXLAN_GPE_IOAM_DISABLE, vxlan_gpe_ioam_disable) \
59 _(VXLAN_GPE_IOAM_VNI_ENABLE, vxlan_gpe_ioam_vni_enable) \
60 _(VXLAN_GPE_IOAM_VNI_DISABLE, vxlan_gpe_ioam_vni_disable) \
61 _(VXLAN_GPE_IOAM_TRANSIT_ENABLE, vxlan_gpe_ioam_transit_enable) \
62 _(VXLAN_GPE_IOAM_TRANSIT_DISABLE, vxlan_gpe_ioam_transit_disable) \
65 static void vl_api_vxlan_gpe_ioam_enable_t_handler
66 (vl_api_vxlan_gpe_ioam_enable_t * mp)
69 vl_api_vxlan_gpe_ioam_enable_reply_t *rmp;
72 /* Ignoring the profile id as currently a single profile
75 vxlan_gpe_ioam_enable (mp->trace_enable, mp->pow_enable, mp->trace_ppc);
78 clib_error_report (error);
79 rv = clib_error_get_code (error);
82 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_ENABLE_REPLY);
85 static void vl_api_vxlan_gpe_ioam_disable_t_handler
86 (vl_api_vxlan_gpe_ioam_disable_t * mp)
89 vl_api_vxlan_gpe_ioam_disable_reply_t *rmp;
92 /* Ignoring the profile id as currently a single profile
94 error = vxlan_gpe_ioam_disable (0, 0, 0);
97 clib_error_report (error);
98 rv = clib_error_get_code (error);
101 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_DISABLE_REPLY);
104 static void vl_api_vxlan_gpe_ioam_vni_enable_t_handler
105 (vl_api_vxlan_gpe_ioam_vni_enable_t * mp)
108 vl_api_vxlan_gpe_ioam_vni_enable_reply_t *rmp;
110 vxlan4_gpe_tunnel_key_t key4;
112 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
113 vxlan_gpe_tunnel_t *t = 0;
114 vxlan_gpe_ioam_main_t *hm = &vxlan_gpe_ioam_main;
120 clib_memcpy (&key4.local, &mp->local, sizeof (key4.local));
121 clib_memcpy (&key4.remote, &mp->remote, sizeof (key4.remote));
122 vni = clib_net_to_host_u32 (mp->vni);
123 key4.vni = clib_host_to_net_u32 (vni << 8);
126 p = hash_get_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
136 t = pool_elt_at_index (gm->tunnels, p[0]);
138 error = vxlan_gpe_ioam_set (t, hm->has_trace_option,
140 hm->has_ppc_option, mp->is_ipv6);
145 clib_error_report (error);
146 rv = clib_error_get_code (error);
149 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_VNI_ENABLE_REPLY);
153 static void vl_api_vxlan_gpe_ioam_vni_disable_t_handler
154 (vl_api_vxlan_gpe_ioam_vni_disable_t * mp)
157 vl_api_vxlan_gpe_ioam_vni_enable_reply_t *rmp;
159 vxlan4_gpe_tunnel_key_t key4;
161 vxlan_gpe_main_t *gm = &vxlan_gpe_main;
162 vxlan_gpe_tunnel_t *t = 0;
168 clib_memcpy (&key4.local, &mp->local, sizeof (key4.local));
169 clib_memcpy (&key4.remote, &mp->remote, sizeof (key4.remote));
170 vni = clib_net_to_host_u32 (mp->vni);
171 key4.vni = clib_host_to_net_u32 (vni << 8);
174 p = hash_get_mem (gm->vxlan4_gpe_tunnel_by_key, &key4);
184 t = pool_elt_at_index (gm->tunnels, p[0]);
186 error = vxlan_gpe_ioam_clear (t, 0, 0, 0, 0);
191 clib_error_report (error);
192 rv = clib_error_get_code (error);
196 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_VNI_DISABLE_REPLY);
199 static void vl_api_vxlan_gpe_ioam_transit_enable_t_handler
200 (vl_api_vxlan_gpe_ioam_transit_enable_t * mp)
203 vl_api_vxlan_gpe_ioam_transit_enable_reply_t *rmp;
204 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
205 ip46_address_t dst_addr;
207 clib_memset (&dst_addr.ip4, 0, sizeof (dst_addr.ip4));
210 clib_memcpy (&dst_addr.ip4, &mp->dst_addr, sizeof (dst_addr.ip4));
212 rv = vxlan_gpe_enable_disable_ioam_for_dest (sm->vlib_main,
214 ntohl (mp->outer_fib_index),
218 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_TRANSIT_ENABLE_REPLY);
221 static void vl_api_vxlan_gpe_ioam_transit_disable_t_handler
222 (vl_api_vxlan_gpe_ioam_transit_disable_t * mp)
225 vl_api_vxlan_gpe_ioam_transit_disable_reply_t *rmp;
226 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
227 ip46_address_t dst_addr;
229 clib_memset (&dst_addr.ip4, 0, sizeof (dst_addr.ip4));
232 clib_memcpy (&dst_addr.ip4, &mp->dst_addr, sizeof (dst_addr.ip4));
235 rv = vxlan_gpe_ioam_disable_for_dest (sm->vlib_main,
237 ntohl (mp->outer_fib_index),
238 mp->is_ipv6 ? 0 : 1);
239 REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_TRANSIT_DISABLE_REPLY);
242 /* Set up the API message handling tables */
243 static clib_error_t *
244 vxlan_gpe_plugin_api_hookup (vlib_main_t * vm)
246 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
248 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
250 vl_api_##n##_t_handler, \
252 vl_api_##n##_t_endian, \
253 vl_api_##n##_t_print, \
254 sizeof(vl_api_##n##_t), 1);
255 foreach_vxlan_gpe_plugin_api_msg;
261 #define vl_msg_name_crc_list
262 #include <ioam/lib-vxlan-gpe/vxlan_gpe_all_api_h.h>
263 #undef vl_msg_name_crc_list
266 setup_message_id_table (vxlan_gpe_ioam_main_t * sm, api_main_t * am)
268 #define _(id,n,crc) \
269 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
270 foreach_vl_msg_name_crc_ioam_vxlan_gpe;
274 static clib_error_t *
275 vxlan_gpe_init (vlib_main_t * vm)
277 vxlan_gpe_ioam_main_t *sm = &vxlan_gpe_ioam_main;
278 clib_error_t *error = 0;
280 u32 encap_node_index = vxlan_gpe_encap_ioam_v4_node.index;
281 u32 decap_node_index = vxlan_gpe_decap_ioam_v4_node.index;
282 vlib_node_t *vxlan_gpe_encap_node = NULL;
283 vlib_node_t *vxlan_gpe_decap_node = NULL;
287 sm->vnet_main = vnet_get_main ();
288 sm->unix_time_0 = (u32) time (0); /* Store starting time */
289 sm->vlib_time_0 = vlib_time_now (vm);
291 name = format (0, "ioam_vxlan_gpe_%08x%c", api_version, 0);
293 /* Ask for a correctly-sized block of API message decode slots */
294 sm->msg_id_base = vl_msg_api_get_msg_ids
295 ((char *) name, VL_MSG_FIRST_AVAILABLE);
297 error = vxlan_gpe_plugin_api_hookup (vm);
299 /* Add our API messages to the global name_crc hash table */
300 setup_message_id_table (sm, &api_main);
302 /* Hook the ioam-encap node to vxlan-gpe-encap */
303 vxlan_gpe_encap_node = vlib_get_node_by_name (vm, (u8 *) "vxlan-gpe-encap");
304 sm->encap_v4_next_node =
305 vlib_node_add_next (vm, vxlan_gpe_encap_node->index, encap_node_index);
307 vxlan_gpe_decap_node =
308 vlib_get_node_by_name (vm, (u8 *) "vxlan4-gpe-input");
310 vlib_node_add_next (vm, vxlan_gpe_decap_node->index, decap_node_index);
311 vxlan_gpe_register_decap_protocol (VXLAN_GPE_PROTOCOL_IOAM, next_node);
313 vec_new (vxlan_gpe_ioam_sw_interface_t, pool_elts (sm->sw_interfaces));
314 sm->dst_by_ip4 = hash_create_mem (0, sizeof (fib_prefix_t), sizeof (uword));
316 sm->dst_by_ip6 = hash_create_mem (0, sizeof (fib_prefix_t), sizeof (uword));
318 vxlan_gpe_ioam_interface_init ();
324 VLIB_INIT_FUNCTION (vxlan_gpe_init);
327 * fd.io coding-style-patch-verification: ON
330 * eval: (c-set-style "gnu")