Add and change lttcomm socket API
[lttng-tools.git] / src / common / sessiond-comm / sessiond-comm.h
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Julien Desfossez <julien.desfossez@polymtl.ca>
4 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 * This header is meant for liblttng and libust internal use ONLY. These
22 * declarations should NOT be considered stable API.
23 */
24
25 #ifndef _LTTNG_SESSIOND_COMM_H
26 #define _LTTNG_SESSIOND_COMM_H
27
28 #define _GNU_SOURCE
29 #include <limits.h>
30 #include <lttng/lttng.h>
31 #include <common/compat/socket.h>
32
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
35 #include <sys/un.h>
36
37 #include "inet.h"
38 #include "inet6.h"
39 #include "unix.h"
40
41 /* Queue size of listen(2) */
42 #define LTTNG_SESSIOND_COMM_MAX_LISTEN 64
43
44 /* Maximum number of FDs that can be sent over a Unix socket */
45 #define LTTCOMM_MAX_SEND_FDS 4
46
47 /*
48 * Get the error code index from 0 since LTTCOMM_OK start at 1000
49 */
50 #define LTTCOMM_ERR_INDEX(code) (code - LTTCOMM_OK)
51
52 enum lttcomm_sessiond_command {
53 /* Tracer command */
54 LTTNG_ADD_CONTEXT,
55 LTTNG_CALIBRATE,
56 LTTNG_DISABLE_CHANNEL,
57 LTTNG_DISABLE_EVENT,
58 LTTNG_DISABLE_ALL_EVENT,
59 LTTNG_ENABLE_CHANNEL,
60 LTTNG_ENABLE_EVENT,
61 LTTNG_ENABLE_ALL_EVENT,
62 /* Session daemon command */
63 LTTNG_CREATE_SESSION,
64 LTTNG_CREATE_SESSION_URI,
65 LTTNG_DESTROY_SESSION,
66 LTTNG_LIST_CHANNELS,
67 LTTNG_LIST_DOMAINS,
68 LTTNG_LIST_EVENTS,
69 LTTNG_LIST_SESSIONS,
70 LTTNG_LIST_TRACEPOINTS,
71 LTTNG_REGISTER_CONSUMER,
72 LTTNG_START_TRACE,
73 LTTNG_STOP_TRACE,
74 LTTNG_LIST_TRACEPOINT_FIELDS,
75 /* Consumer */
76 LTTNG_DISABLE_CONSUMER,
77 LTTNG_ENABLE_CONSUMER,
78 LTTNG_SET_CONSUMER_URI,
79 /* Relay daemon */
80 RELAYD_ADD_STREAM,
81 RELAYD_CREATE_SESSION,
82 RELAYD_START_DATA,
83 RELAYD_UPDATE_SYNC_INFO,
84 RELAYD_VERSION,
85 RELAYD_SEND_METADATA,
86 };
87
88 /*
89 * lttcomm error code.
90 */
91 enum lttcomm_return_code {
92 LTTCOMM_OK = 10, /* Ok */
93 LTTCOMM_ERR, /* Unknown Error */
94 LTTCOMM_UND, /* Undefine command */
95 LTTCOMM_NOT_IMPLEMENTED, /* Command not implemented */
96 LTTCOMM_UNKNOWN_DOMAIN, /* Tracing domain not known */
97 LTTCOMM_ALLOC_FAIL, /* Trace allocation fail */
98 LTTCOMM_NO_SESSION, /* No session found */
99 LTTCOMM_CREATE_DIR_FAIL, /* Create directory fail */
100 LTTCOMM_SESSION_FAIL, /* Create session fail */
101 LTTCOMM_START_FAIL, /* Start tracing fail */
102 LTTCOMM_STOP_FAIL, /* Stop tracing fail */
103 LTTCOMM_LIST_FAIL, /* Listing apps fail */
104 LTTCOMM_NO_APPS, /* No traceable application */
105 LTTCOMM_SESS_NOT_FOUND, /* Session name not found */
106 LTTCOMM_NO_TRACE, /* No trace exist */
107 LTTCOMM_FATAL, /* Session daemon had a fatal error */
108 LTTCOMM_NO_TRACEABLE, /* Error for non traceable app */
109 LTTCOMM_SELECT_SESS, /* Must select a session */
110 LTTCOMM_EXIST_SESS, /* Session name already exist */
111 LTTCOMM_NO_EVENT, /* No event found */
112 LTTCOMM_CONNECT_FAIL, /* Unable to connect to unix socket */
113 LTTCOMM_APP_NOT_FOUND, /* App not found in traceable app list */
114 LTTCOMM_EPERM, /* Permission denied */
115 LTTCOMM_KERN_NA, /* Kernel tracer unavalable */
116 LTTCOMM_KERN_VERSION, /* Kernel tracer version is not compatible */
117 LTTCOMM_KERN_EVENT_EXIST, /* Kernel event already exists */
118 LTTCOMM_KERN_SESS_FAIL, /* Kernel create session failed */
119 LTTCOMM_KERN_CHAN_EXIST, /* Kernel channel already exists */
120 LTTCOMM_KERN_CHAN_FAIL, /* Kernel create channel failed */
121 LTTCOMM_KERN_CHAN_NOT_FOUND, /* Kernel channel not found */
122 LTTCOMM_KERN_CHAN_DISABLE_FAIL, /* Kernel disable channel failed */
123 LTTCOMM_KERN_CHAN_ENABLE_FAIL, /* Kernel enable channel failed */
124 LTTCOMM_KERN_CONTEXT_FAIL, /* Kernel add context failed */
125 LTTCOMM_KERN_ENABLE_FAIL, /* Kernel enable event failed */
126 LTTCOMM_KERN_DISABLE_FAIL, /* Kernel disable event failed */
127 LTTCOMM_KERN_META_FAIL, /* Kernel open metadata failed */
128 LTTCOMM_KERN_START_FAIL, /* Kernel start trace failed */
129 LTTCOMM_KERN_STOP_FAIL, /* Kernel stop trace failed */
130 LTTCOMM_KERN_CONSUMER_FAIL, /* Kernel consumer start failed */
131 LTTCOMM_KERN_STREAM_FAIL, /* Kernel create stream failed */
132 LTTCOMM_KERN_DIR_FAIL, /* Kernel trace directory creation failed */
133 LTTCOMM_KERN_DIR_EXIST, /* Kernel trace directory exist */
134 LTTCOMM_KERN_NO_SESSION, /* No kernel session found */
135 LTTCOMM_KERN_LIST_FAIL, /* Kernel listing events failed */
136 LTTCOMM_UST_CALIBRATE_FAIL, /* UST calibration failed */
137 LTTCOMM_UST_VERSION, /* UST tracer version is not compatible */
138 LTTCOMM_UST_SESS_FAIL, /* UST create session failed */
139 LTTCOMM_UST_CHAN_EXIST, /* UST channel already exist */
140 LTTCOMM_UST_CHAN_FAIL, /* UST create channel failed */
141 LTTCOMM_UST_CHAN_NOT_FOUND, /* UST channel not found */
142 LTTCOMM_UST_CHAN_DISABLE_FAIL, /* UST disable channel failed */
143 LTTCOMM_UST_CHAN_ENABLE_FAIL, /* UST enable channel failed */
144 LTTCOMM_UST_CONTEXT_FAIL, /* UST add context failed */
145 LTTCOMM_UST_ENABLE_FAIL, /* UST enable event failed */
146 LTTCOMM_UST_DISABLE_FAIL, /* UST disable event failed */
147 LTTCOMM_UST_META_FAIL, /* UST open metadata failed */
148 LTTCOMM_UST_START_FAIL, /* UST start trace failed */
149 LTTCOMM_UST_STOP_FAIL, /* UST stop trace failed */
150 LTTCOMM_UST_CONSUMER64_FAIL, /* 64-bit UST consumer start failed */
151 LTTCOMM_UST_CONSUMER32_FAIL, /* 32-bit UST consumer start failed */
152 LTTCOMM_UST_STREAM_FAIL, /* UST create stream failed */
153 LTTCOMM_UST_DIR_FAIL, /* UST trace directory creation failed */
154 LTTCOMM_UST_DIR_EXIST, /* UST trace directory exist */
155 LTTCOMM_UST_NO_SESSION, /* No UST session found */
156 LTTCOMM_UST_LIST_FAIL, /* UST listing events failed */
157 LTTCOMM_UST_EVENT_EXIST, /* UST event exist */
158 LTTCOMM_UST_EVENT_NOT_FOUND, /* UST event not found */
159 LTTCOMM_UST_CONTEXT_EXIST, /* UST context exist */
160 LTTCOMM_UST_CONTEXT_INVAL, /* UST context invalid */
161 LTTCOMM_NEED_ROOT_SESSIOND, /* root sessiond is needed */
162 LTTCOMM_TRACE_ALREADY_STARTED, /* Tracing already started */
163 LTTCOMM_TRACE_ALREADY_STOPPED, /* Tracing already stopped */
164 LTTCOMM_KERN_EVENT_ENOSYS, /* Kernel event type not supported */
165
166 CONSUMERD_COMMAND_SOCK_READY, /* when consumerd command socket ready */
167 CONSUMERD_SUCCESS_RECV_FD, /* success on receiving fds */
168 CONSUMERD_ERROR_RECV_FD, /* error on receiving fds */
169 CONSUMERD_ERROR_RECV_CMD, /* error on receiving command */
170 CONSUMERD_POLL_ERROR, /* Error in polling thread in kconsumerd */
171 CONSUMERD_POLL_NVAL, /* Poll on closed fd */
172 CONSUMERD_POLL_HUP, /* All fds have hungup */
173 CONSUMERD_EXIT_SUCCESS, /* kconsumerd exiting normally */
174 CONSUMERD_EXIT_FAILURE, /* kconsumerd exiting on error */
175 CONSUMERD_OUTFD_ERROR, /* error opening the tracefile */
176 CONSUMERD_SPLICE_EBADF, /* EBADF from splice(2) */
177 CONSUMERD_SPLICE_EINVAL, /* EINVAL from splice(2) */
178 CONSUMERD_SPLICE_ENOMEM, /* ENOMEM from splice(2) */
179 CONSUMERD_SPLICE_ESPIPE, /* ESPIPE from splice(2) */
180 LTTCOMM_INVALID, /* Invalid parameter */
181 LTTCOMM_NO_USTCONSUMERD, /* No UST consumer detected */
182 LTTCOMM_NO_KERNCONSUMERD, /* No Kernel consumer detected */
183 LTTCOMM_EVENT_EXIST_LOGLEVEL, /* Event already enabled with different loglevel */
184 LTTCOMM_URI_DATA_MISS, /* Missing network data URI */
185 LTTCOMM_URI_CTRL_MISS, /* Missing network control URI */
186 LTTCOMM_ENABLE_CONSUMER_FAIL, /* Enabling consumer failed */
187 LTTCOMM_RELAYD_SESSION_FAIL, /* lttng-relayd create session failed */
188 LTTCOMM_RELAYD_VERSION_FAIL, /* lttng-relayd not compatible */
189
190 /* MUST be last element */
191 LTTCOMM_NR, /* Last element */
192 };
193
194 /* lttng socket protocol. */
195 enum lttcomm_sock_proto {
196 LTTCOMM_SOCK_UDP,
197 LTTCOMM_SOCK_TCP,
198 };
199
200 /*
201 * Index in the net_families array below. Please keep in sync!
202 */
203 enum lttcomm_sock_domain {
204 LTTCOMM_INET = 1,
205 LTTCOMM_INET6 = 2,
206 };
207
208 struct lttcomm_sockaddr {
209 enum lttcomm_sock_domain type;
210 union {
211 struct sockaddr_in sin;
212 struct sockaddr_in6 sin6;
213 } addr;
214 };
215
216 struct lttcomm_sock {
217 int fd;
218 enum lttcomm_sock_proto proto;
219 struct lttcomm_sockaddr sockaddr;
220 const struct lttcomm_proto_ops *ops;
221 };
222
223 struct lttcomm_net_family {
224 int family;
225 int (*create) (struct lttcomm_sock *sock, int type, int proto);
226 };
227
228 struct lttcomm_proto_ops {
229 int (*bind) (struct lttcomm_sock *sock);
230 int (*close) (struct lttcomm_sock *sock);
231 int (*connect) (struct lttcomm_sock *sock);
232 struct lttcomm_sock *(*accept) (struct lttcomm_sock *sock);
233 int (*listen) (struct lttcomm_sock *sock, int backlog);
234 ssize_t (*recvmsg) (struct lttcomm_sock *sock, void *buf, size_t len,
235 int flags);
236 ssize_t (*sendmsg) (struct lttcomm_sock *sock, void *buf, size_t len,
237 int flags);
238 };
239
240 /*
241 * Data structure received from lttng client to session daemon.
242 */
243 struct lttcomm_session_msg {
244 uint32_t cmd_type; /* enum lttcomm_sessiond_command */
245 struct lttng_session session;
246 struct lttng_domain domain;
247 union {
248 struct {
249 char channel_name[NAME_MAX];
250 char name[NAME_MAX];
251 } disable;
252 /* Event data */
253 struct {
254 char channel_name[NAME_MAX];
255 struct lttng_event event;
256 } enable;
257 /* Create channel */
258 struct {
259 struct lttng_channel chan;
260 } channel;
261 /* Context */
262 struct {
263 char channel_name[NAME_MAX];
264 char event_name[NAME_MAX];
265 struct lttng_event_context ctx;
266 } context;
267 /* Use by register_consumer */
268 struct {
269 char path[PATH_MAX];
270 } reg;
271 /* List */
272 struct {
273 char channel_name[NAME_MAX];
274 } list;
275 struct lttng_calibrate calibrate;
276 /* Used by the set_consumer_uri call */
277 struct lttng_uri uri;
278 struct {
279 uint32_t enable_consumer;
280 struct lttng_uri ctrl_uri;
281 struct lttng_uri data_uri;
282 } create_uri;
283 } u;
284 };
285
286 /*
287 * Data structure for the response from sessiond to the lttng client.
288 */
289 struct lttcomm_lttng_msg {
290 uint32_t cmd_type; /* enum lttcomm_sessiond_command */
291 uint32_t ret_code; /* enum lttcomm_return_code */
292 uint32_t pid; /* pid_t */
293 uint32_t data_size;
294 /* Contains: trace_name + data */
295 char payload[];
296 };
297
298 /*
299 * lttcomm_consumer_msg is the message sent from sessiond to consumerd
300 * to either add a channel, add a stream, update a stream, or stop
301 * operation.
302 */
303 struct lttcomm_consumer_msg {
304 uint32_t cmd_type; /* enum consumerd_command */
305 union {
306 struct {
307 int channel_key;
308 uint64_t max_sb_size; /* the subbuffer size for this channel */
309 /* shm_fd and wait_fd are sent as ancillary data */
310 uint64_t mmap_len;
311 char name[LTTNG_SYMBOL_NAME_LEN];
312 } channel;
313 struct {
314 int channel_key;
315 int stream_key;
316 /* shm_fd and wait_fd are sent as ancillary data */
317 uint32_t state; /* enum lttcomm_consumer_fd_state */
318 enum lttng_event_output output; /* use splice or mmap to consume this fd */
319 uint64_t mmap_len;
320 uid_t uid; /* User ID owning the session */
321 gid_t gid; /* Group ID owning the session */
322 char path_name[PATH_MAX];
323 int net_index;
324 unsigned int metadata_flag;
325 char name[LTTNG_SYMBOL_NAME_LEN]; /* Name string of the stream */
326 } stream;
327 struct {
328 int net_index;
329 enum lttng_stream_type type;
330 /* Open socket to the relayd */
331 struct lttcomm_sock sock;
332 } relayd_sock;
333 } u;
334 };
335
336 #ifdef HAVE_LIBLTTNG_UST_CTL
337
338 #include <lttng/ust-abi.h>
339
340 /*
341 * Data structure for the commands sent from sessiond to UST.
342 */
343 struct lttcomm_ust_msg {
344 uint32_t handle;
345 uint32_t cmd;
346 union {
347 struct lttng_ust_channel channel;
348 struct lttng_ust_stream stream;
349 struct lttng_ust_event event;
350 struct lttng_ust_context context;
351 struct lttng_ust_tracer_version version;
352 } u;
353 };
354
355 /*
356 * Data structure for the response from UST to the session daemon.
357 * cmd_type is sent back in the reply for validation.
358 */
359 struct lttcomm_ust_reply {
360 uint32_t handle;
361 uint32_t cmd;
362 uint32_t ret_code; /* enum lttcomm_return_code */
363 uint32_t ret_val; /* return value */
364 union {
365 struct {
366 uint64_t memory_map_size;
367 } channel;
368 struct {
369 uint64_t memory_map_size;
370 } stream;
371 struct lttng_ust_tracer_version version;
372 } u;
373 };
374
375 #endif /* HAVE_LIBLTTNG_UST_CTL */
376
377 extern const char *lttcomm_get_readable_code(enum lttcomm_return_code code);
378
379 extern int lttcomm_init_inet_sockaddr(struct lttcomm_sockaddr *sockaddr,
380 const char *ip, unsigned int port);
381 extern int lttcomm_init_inet6_sockaddr(struct lttcomm_sockaddr *sockaddr,
382 const char *ip, unsigned int port);
383
384 extern struct lttcomm_sock *lttcomm_alloc_sock(enum lttcomm_sock_proto proto);
385 extern int lttcomm_create_sock(struct lttcomm_sock *sock);
386 extern struct lttcomm_sock *lttcomm_alloc_sock_from_uri(struct lttng_uri *uri);
387 extern void lttcomm_destroy_sock(struct lttcomm_sock *sock);
388 extern struct lttcomm_sock *lttcomm_alloc_copy_sock(struct lttcomm_sock *src);
389 extern void lttcomm_copy_sock(struct lttcomm_sock *dst,
390 struct lttcomm_sock *src);
391
392 #endif /* _LTTNG_SESSIOND_COMM_H */
This page took 0.056473 seconds and 4 git commands to generate.