FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
trace_api.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  *------------------------------------------------------------------
17  * trace_api.c - iOAM Trace related APIs to create
18  * and maintain profiles
19  *------------------------------------------------------------------
20  */
21 
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
25 
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29 
30 /* define message IDs */
32 
33 /* define message structures */
34 #define vl_typedefs
36 #undef vl_typedefs
37 
38 /* define generated endian-swappers */
39 #define vl_endianfun
41 #undef vl_endianfun
42 
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
47 #undef vl_printfun
48 
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
52 #undef vl_api_version
53 
54 /*
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
59  * rv - return value
60  */
61 
62 #define TRACE_REPLY_MACRO(t) \
63 do { \
64  unix_shared_memory_queue_t * q = \
65  vl_api_client_index_to_input_queue (mp->client_index); \
66  if (!q) \
67  return; \
68  \
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); \
73  \
74  vl_msg_api_send_shmem (q, (u8 *)&rmp); \
75 } while(0);
76 
77 /* *INDENT-OFF* */
78 #define TRACE_REPLY_MACRO2(t, body) \
79 do { \
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); \
83  if (!q) \
84  return; \
85  \
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); \
92 } while(0);
93 /* *INDENT-ON* */
94 
95 /* List of message types that this plugin understands */
96 
97 #define foreach_trace_plugin_api_msg \
98 _(TRACE_PROFILE_ADD, trace_profile_add) \
99 _(TRACE_PROFILE_DEL, trace_profile_del) \
100 
103 {
104  trace_main_t *sm = &trace_main;
105  int rv = 0;
107  trace_profile *profile = NULL;
108  u8 *name = 0;
109 
110  profile = trace_profile_find ();
111  if (profile)
112  {
113  rv =
114  trace_profile_create (profile, mp->trace_type, mp->num_elts,
115  mp->trace_tsp, ntohl (mp->node_id),
116  ntohl (mp->app_data));
117  if (rv != 0)
118  goto ERROROUT;
119  }
120  else
121  {
122  rv = -3;
123  }
124 ERROROUT:
125  vec_free (name);
126  TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_ADD_REPLY);
127 }
128 
129 
132 {
133  trace_main_t *sm = &trace_main;
134  int rv = 0;
136 
138 
139  TRACE_REPLY_MACRO (VL_API_TRACE_PROFILE_DEL_REPLY);
140 }
141 
142 
143 /*
144  * This routine exists to convince the vlib plugin framework that
145  * we haven't accidentally copied a random .dll into the plugin directory.
146  *
147  * Also collects global variable pointers passed from the vpp engine
148  */
149 
150 clib_error_t *
152  int from_early_init)
153 {
154  trace_main_t *sm = &trace_main;
155  clib_error_t *error = 0;
156 
157  sm->vlib_main = vm;
158  sm->vnet_main = h->vnet_main;
159  return error;
160 }
161 
162 /* Set up the API message handling tables */
163 static clib_error_t *
165 {
166  trace_main_t *sm = &trace_main;
167 #define _(N,n) \
168  vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base), \
169  #n, \
170  vl_api_##n##_t_handler, \
171  vl_noop_handler, \
172  vl_api_##n##_t_endian, \
173  vl_api_##n##_t_print, \
174  sizeof(vl_api_##n##_t), 1);
176 #undef _
177 
178  return 0;
179 }
180 
181 static clib_error_t *
183 {
184  trace_main_t *sm = &trace_main;
185  clib_error_t *error = 0;
186  u8 *name;
187 
188  bzero (sm, sizeof (trace_main));
189  (void) trace_util_init ();
190  name = format (0, "ioam_trace_%08x%c", api_version, 0);
191 
192  /* Ask for a correctly-sized block of API message decode slots */
194  ((char *) name, VL_MSG_FIRST_AVAILABLE);
195 
196  error = trace_plugin_api_hookup (vm);
197 
198  vec_free (name);
199 
200  return error;
201 }
202 
204 
205 /*
206  * fd.io coding-style-patch-verification: ON
207  *
208  * Local Variables:
209  * eval: (c-set-style "gnu")
210  * End:
211  */
trace_main_t trace_main
Definition: trace_util.c:22
#define NULL
Definition: clib.h:55
static clib_error_t * trace_plugin_api_hookup(vlib_main_t *vm)
Definition: trace_api.c:164
vnet_main_t * vnet_main
Definition: trace_util.h:63
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
Delete trace Profile.
Definition: trace.api:53
Trace profile add / del response.
Definition: trace.api:62
void clear_trace_profiles(void)
Definition: trace_util.c:116
iOAM6 Trace - Set the iOAM6 trace profile
Definition: trace.api:26
u16 msg_id_base
Definition: trace_util.h:59
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define TRACE_REPLY_MACRO(t)
Definition: trace_api.c:62
Trace profile add / del response.
Definition: trace.api:40
static void vl_api_trace_profile_del_t_handler(vl_api_trace_profile_del_t *mp)
Definition: trace_api.c:131
#define foreach_trace_plugin_api_msg
Definition: trace_api.c:97
static trace_profile * trace_profile_find(void)
Definition: trace_util.h:80
static void vl_api_trace_profile_add_t_handler(vl_api_trace_profile_add_t *mp)
Definition: trace_api.c:102
int trace_util_init(void)
Definition: trace_util.c:55
clib_error_t * vlib_plugin_register(vlib_main_t *vm, vnet_plugin_handoff_t *h, int from_early_init)
Definition: trace_api.c:151
unsigned char u8
Definition: types.h:56
static clib_error_t * trace_init(vlib_main_t *vm)
Definition: trace_api.c:182
vnet_main_t * vnet_main
Definition: plugin.h:26
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u16 vl_msg_api_get_msg_ids(char *name, int n)
Definition: api_shared.c:1269
int trace_profile_create(trace_profile *profile, u8 trace_type, u8 num_elts, u32 trace_tsp, u32 node_id, u32 app_data)
Definition: trace_util.c:65
vlib_main_t * vlib_main
Definition: trace_util.h:62