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 * trace_api.c - iOAM Trace related APIs to create
18 * and maintain profiles
19 *------------------------------------------------------------------
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <ioam/lib-trace/trace_util.h>
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
30 /* define message IDs */
31 #include <ioam/lib-trace/trace_msg_enum.h>
33 /* define message structures */
35 #include <ioam/lib-trace/trace_all_api_h.h>
38 /* define generated endian-swappers */
40 #include <ioam/lib-trace/trace_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-trace/trace_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-trace/trace_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 TRACE_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 TRACE_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_trace_plugin_api_msg \
98 _(TRACE_PROFILE_ADD, trace_profile_add) \
99 _(TRACE_PROFILE_DEL, trace_profile_del) \
101 static void vl_api_trace_profile_add_t_handler
102 (vl_api_trace_profile_add_t * mp)
104 trace_main_t *sm = &trace_main;
106 vl_api_trace_profile_add_reply_t *rmp;
107 trace_profile *profile = NULL;
110 profile = trace_profile_find ();
114 trace_profile_create (profile, mp->trace_type, mp->num_elts,
115 mp->trace_tsp, ntohl (mp->node_id),
116 ntohl (mp->app_data));
126 TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_ADD_REPLY);
130 static void vl_api_trace_profile_del_t_handler
131 (vl_api_trace_profile_del_t * mp)
133 trace_main_t *sm = &trace_main;
135 vl_api_trace_profile_del_reply_t *rmp;
137 clear_trace_profiles ();
139 TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_DEL_REPLY);
144 * This routine exists to convince the vlib plugin framework that
145 * we haven't accidentally copied a random .dll into the plugin directory.
147 * Also collects global variable pointers passed from the vpp engine
151 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
154 trace_main_t *sm = &trace_main;
155 clib_error_t *error = 0;
158 sm->vnet_main = h->vnet_main;
162 /* Set up the API message handling tables */
163 static clib_error_t *
164 trace_plugin_api_hookup (vlib_main_t * vm)
166 trace_main_t *sm = &trace_main;
168 vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
170 vl_api_##n##_t_handler, \
172 vl_api_##n##_t_endian, \
173 vl_api_##n##_t_print, \
174 sizeof(vl_api_##n##_t), 1);
175 foreach_trace_plugin_api_msg;
181 #define vl_msg_name_crc_list
182 #include <ioam/lib-trace/trace_all_api_h.h>
183 #undef vl_msg_name_crc_list
186 setup_message_id_table (trace_main_t * sm, api_main_t * am)
188 #define _(id,n,crc) \
189 vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
190 foreach_vl_msg_name_crc_trace;
194 static clib_error_t *
195 trace_init (vlib_main_t * vm)
197 trace_main_t *sm = &trace_main;
198 clib_error_t *error = 0;
201 bzero (sm, sizeof (trace_main));
202 (void) trace_util_init ();
203 name = format (0, "ioam_trace_%08x%c", api_version, 0);
205 /* Ask for a correctly-sized block of API message decode slots */
206 sm->msg_id_base = vl_msg_api_get_msg_ids
207 ((char *) name, VL_MSG_FIRST_AVAILABLE);
209 error = trace_plugin_api_hookup (vm);
211 /* Add our API messages to the global name_crc hash table */
212 setup_message_id_table (sm, &api_main);
219 VLIB_INIT_FUNCTION (trace_init);
222 * fd.io coding-style-patch-verification: ON
225 * eval: (c-set-style "gnu")