-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace-msg.c
738 lines (606 loc) · 14.3 KB
/
trace-msg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
* trace-msg.c : define message protocol for communication between clients and
* a server
*
* Copyright (C) 2013 Hitachi, Ltd.
* Created by Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License (not later!)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <http://www.gnu.org/licenses>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <linux/types.h>
#include "trace-cmd-local.h"
#include "trace-msg.h"
typedef __u32 u32;
typedef __be32 be32;
#define TRACECMD_MSG_MAX_LEN BUFSIZ
/* size + cmd */
#define TRACECMD_MSG_HDR_LEN ((sizeof(be32)) + (sizeof(be32)))
/* + size of the metadata */
#define TRACECMD_MSG_META_MIN_LEN \
((TRACECMD_MSG_HDR_LEN) + (sizeof(be32)))
/* - header size for error msg */
#define TRACECMD_MSG_META_MAX_LEN \
((TRACECMD_MSG_MAX_LEN) - (TRACECMD_MSG_META_MIN_LEN) - TRACECMD_MSG_HDR_LEN)
/* size + opt_cmd + size of str */
#define TRACECMD_OPT_MIN_LEN \
((sizeof(be32)) + (sizeof(be32)) + (sizeof(be32)))
#define CPU_MAX 256
/* for both client and server */
bool use_tcp;
int cpu_count;
/* for client */
static int psfd;
unsigned int page_size;
int *client_ports;
bool send_metadata;
/* for server */
static int *port_array;
bool done;
struct tracecmd_msg_str {
be32 size;
char *buf;
} __attribute__((packed));
struct tracecmd_msg_opt {
be32 size;
be32 opt_cmd;
struct tracecmd_msg_str str;
};
struct tracecmd_msg_tinit {
be32 cpus;
be32 page_size;
be32 opt_num;
struct tracecmd_msg_opt *opt;
} __attribute__((packed));
struct tracecmd_msg_rinit {
be32 cpus;
be32 port_array[CPU_MAX];
} __attribute__((packed));
struct tracecmd_msg_meta {
struct tracecmd_msg_str str;
};
struct tracecmd_msg_error {
be32 size;
be32 cmd;
union {
struct tracecmd_msg_tinit tinit;
struct tracecmd_msg_rinit rinit;
struct tracecmd_msg_meta meta;
} data;
} __attribute__((packed));
enum tracecmd_msg_cmd {
MSG_CLOSE = 1,
MSG_TINIT = 4,
MSG_RINIT = 5,
MSG_SENDMETA = 6,
MSG_FINMETA = 7,
};
struct tracecmd_msg {
be32 size;
be32 cmd;
union {
struct tracecmd_msg_tinit tinit;
struct tracecmd_msg_rinit rinit;
struct tracecmd_msg_meta meta;
struct tracecmd_msg_error err;
} data;
} __attribute__((packed));
struct tracecmd_msg *errmsg;
static ssize_t msg_do_write_check(int fd, struct tracecmd_msg *msg)
{
return __do_write_check(fd, msg, ntohl(msg->size));
}
static void tracecmd_msg_init(u32 cmd, u32 len, struct tracecmd_msg *msg)
{
memset(msg, 0, len);
msg->size = htonl(len);
msg->cmd = htonl(cmd);
}
static int tracecmd_msg_alloc(u32 cmd, u32 len, struct tracecmd_msg **msg)
{
len += TRACECMD_MSG_HDR_LEN;
*msg = malloc(len);
if (!*msg)
return -ENOMEM;
tracecmd_msg_init(cmd, len, *msg);
return 0;
}
static int msgcpy(struct tracecmd_msg *msg, u32 offset,
const void *buf, u32 buflen)
{
if (offset + buflen > ntohl(msg->size))
return -EINVAL;
memcpy(((void *)msg)+offset, buf, buflen);
return 0;
}
static int optcpy(struct tracecmd_msg_opt *opt, u32 offset,
const void *buf, u32 buflen)
{
if (offset + buflen > ntohl(opt->size))
return -EINVAL;
memcpy(((void *)opt)+offset, buf, buflen);
return 0;
}
enum msg_opt_command {
MSGOPT_USETCP = 1,
};
static int add_option_to_tinit(u32 cmd, const char *buf,
struct tracecmd_msg *msg, int offset)
{
struct tracecmd_msg_opt *opt;
u32 len = TRACECMD_OPT_MIN_LEN;
u32 buflen = 0;
if (buf) {
buflen = strlen(buf);
len += buflen;
}
opt = malloc(len);
if (!opt)
return -ENOMEM;
opt->size = htonl(len);
opt->opt_cmd = htonl(cmd);
opt->str.size = htonl(buflen);
if (buf)
optcpy(opt, TRACECMD_OPT_MIN_LEN, buf, buflen);
/* add option to msg */
msgcpy(msg, offset, opt, ntohl(opt->size));
free(opt);
return len;
}
static int add_options_to_tinit(struct tracecmd_msg *msg)
{
int offset = offsetof(struct tracecmd_msg, data.tinit.opt);
int ret;
if (use_tcp) {
ret = add_option_to_tinit(MSGOPT_USETCP, NULL, msg, offset);
if (ret < 0)
return ret;
}
return 0;
}
static int make_tinit(struct tracecmd_msg *msg)
{
int opt_num = 0;
int ret = 0;
if (use_tcp)
opt_num++;
if (opt_num) {
ret = add_options_to_tinit(msg);
if (ret < 0)
return ret;
}
msg->data.tinit.cpus = htonl(cpu_count);
msg->data.tinit.page_size = htonl(page_size);
msg->data.tinit.opt_num = htonl(opt_num);
return 0;
}
static int make_rinit(struct tracecmd_msg *msg)
{
int i;
u32 offset = TRACECMD_MSG_HDR_LEN;
be32 port;
msg->data.rinit.cpus = htonl(cpu_count);
for (i = 0; i < cpu_count; i++) {
/* + rrqports->cpus or rrqports->port_array[i] */
offset += sizeof(be32);
port = htonl(port_array[i]);
msgcpy(msg, offset, &port, sizeof(be32) * cpu_count);
}
return 0;
}
static u32 tracecmd_msg_get_body_length(u32 cmd)
{
struct tracecmd_msg *msg;
u32 len = 0;
switch (cmd) {
case MSG_TINIT:
len = sizeof(msg->data.tinit.cpus)
+ sizeof(msg->data.tinit.page_size)
+ sizeof(msg->data.tinit.opt_num);
/*
* If we are using IPV4 and our page size is greater than
* or equal to 64K, we need to punt and use TCP. :-(
*/
/* TODO, test for ipv4 */
if (page_size >= UDP_MAX_PACKET) {
warning("page size too big for UDP using TCP in live read");
use_tcp = true;
}
if (use_tcp)
len += TRACECMD_OPT_MIN_LEN;
return len;
case MSG_RINIT:
return sizeof(msg->data.rinit.cpus)
+ sizeof(msg->data.rinit.port_array);
case MSG_SENDMETA:
return TRACECMD_MSG_MAX_LEN - TRACECMD_MSG_HDR_LEN;
case MSG_CLOSE:
case MSG_FINMETA:
break;
}
return 0;
}
static int tracecmd_msg_make_body(u32 cmd, struct tracecmd_msg *msg)
{
switch (cmd) {
case MSG_TINIT:
return make_tinit(msg);
case MSG_RINIT:
return make_rinit(msg);
case MSG_CLOSE:
case MSG_SENDMETA: /* meta data is not stored here. */
case MSG_FINMETA:
break;
}
return 0;
}
static int tracecmd_msg_create(u32 cmd, struct tracecmd_msg **msg)
{
u32 len = 0;
int ret = 0;
len = tracecmd_msg_get_body_length(cmd);
if (len > (TRACECMD_MSG_MAX_LEN - TRACECMD_MSG_HDR_LEN)) {
plog("Exceed maximum message size cmd=%d\n", cmd);
return -EINVAL;
}
ret = tracecmd_msg_alloc(cmd, len, msg);
if (ret < 0)
return ret;
ret = tracecmd_msg_make_body(cmd, *msg);
if (ret < 0)
free(*msg);
return ret;
}
static int tracecmd_msg_send(int fd, u32 cmd)
{
struct tracecmd_msg *msg = NULL;
int ret = 0;
if (cmd > MSG_FINMETA) {
plog("Unsupported command: %d\n", cmd);
return -EINVAL;
}
ret = tracecmd_msg_create(cmd, &msg);
if (ret < 0)
return ret;
ret = msg_do_write_check(fd, msg);
if (ret < 0)
ret = -ECOMM;
free(msg);
return ret;
}
static int tracecmd_msg_read_extra(int fd, void *buf, u32 size, int *n)
{
int r = 0;
do {
r = read(fd, buf + *n, size);
if (r < 0) {
if (errno == EINTR)
continue;
return -errno;
} else if (!r)
return -ENOTCONN;
size -= r;
*n += r;
} while (size);
return 0;
}
/*
* Read header information of msg first, then read all data
*/
static int tracecmd_msg_recv(int fd, struct tracecmd_msg *msg)
{
u32 size = 0;
int n = 0;
int ret;
ret = tracecmd_msg_read_extra(fd, msg, TRACECMD_MSG_HDR_LEN, &n);
if (ret < 0)
return ret;
size = ntohl(msg->size);
if (size > TRACECMD_MSG_MAX_LEN)
/* too big */
goto error;
else if (size < TRACECMD_MSG_HDR_LEN)
/* too small */
goto error;
else if (size > TRACECMD_MSG_HDR_LEN) {
size -= TRACECMD_MSG_HDR_LEN;
return tracecmd_msg_read_extra(fd, msg, size, &n);
}
return 0;
error:
plog("Receive an invalid message(size=%d)\n", size);
return -ENOMSG;
}
#define MSG_WAIT_MSEC 5000
static int msg_wait_to = MSG_WAIT_MSEC;
void tracecmd_msg_set_debug(int debug)
{
if (debug)
msg_wait_to = -1;
else
msg_wait_to = MSG_WAIT_MSEC;
}
/*
* A return value of 0 indicates time-out
*/
static int tracecmd_msg_recv_wait(int fd, struct tracecmd_msg *msg)
{
struct pollfd pfd;
int ret;
pfd.fd = fd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, msg_wait_to);
if (ret < 0)
return -errno;
else if (ret == 0)
return -ETIMEDOUT;
return tracecmd_msg_recv(fd, msg);
}
static void *tracecmd_msg_buf_access(struct tracecmd_msg *msg, int offset)
{
return (void *)msg + offset;
}
static int tracecmd_msg_wait_for_msg(int fd, struct tracecmd_msg *msg)
{
u32 cmd;
int ret;
ret = tracecmd_msg_recv_wait(fd, msg);
if (ret < 0) {
if (ret == -ETIMEDOUT)
warning("Connection timed out\n");
return ret;
}
cmd = ntohl(msg->cmd);
if (cmd == MSG_CLOSE)
return -ECONNABORTED;
return 0;
}
static int tracecmd_msg_send_and_wait_for_msg(int fd, u32 cmd, struct tracecmd_msg *msg)
{
int ret;
ret = tracecmd_msg_send(fd, cmd);
if (ret < 0)
return ret;
ret = tracecmd_msg_wait_for_msg(fd, msg);
if (ret < 0)
return ret;
return 0;
}
int tracecmd_msg_send_init_data(int fd)
{
char buf[TRACECMD_MSG_MAX_LEN];
struct tracecmd_msg *msg;
int i, cpus;
int ret;
msg = (struct tracecmd_msg *)buf;
ret = tracecmd_msg_send_and_wait_for_msg(fd, MSG_TINIT, msg);
if (ret < 0)
return ret;
cpus = ntohl(msg->data.rinit.cpus);
client_ports = malloc_or_die(sizeof(int) * cpus);
for (i = 0; i < cpus; i++)
client_ports[i] = ntohl(msg->data.rinit.port_array[i]);
/* Next, send meta data */
send_metadata = true;
return 0;
}
static bool process_option(struct tracecmd_msg_opt *opt)
{
/* currently the only option we have is to us TCP */
if (ntohl(opt->opt_cmd) == MSGOPT_USETCP) {
use_tcp = true;
return true;
}
return false;
}
static void error_operation_for_server(struct tracecmd_msg *msg)
{
u32 cmd;
cmd = ntohl(msg->cmd);
warning("Message: cmd=%d size=%d\n", cmd, ntohl(msg->size));
}
#define MAX_OPTION_SIZE 4096
int tracecmd_msg_initial_setting(int fd, int *cpus, int *pagesize)
{
struct tracecmd_msg *msg;
struct tracecmd_msg_opt *opt;
char buf[TRACECMD_MSG_MAX_LEN];
int offset = offsetof(struct tracecmd_msg, data.tinit.opt);
int options, i, s;
int ret;
u32 size = 0;
u32 cmd;
msg = (struct tracecmd_msg *)buf;
ret = tracecmd_msg_recv_wait(fd, msg);
if (ret < 0) {
if (ret == -ETIMEDOUT)
warning("Connection timed out\n");
return ret;
}
cmd = ntohl(msg->cmd);
if (cmd != MSG_TINIT) {
ret = -EINVAL;
goto error;
}
*cpus = ntohl(msg->data.tinit.cpus);
plog("cpus=%d\n", *cpus);
if (*cpus < 0) {
ret = -EINVAL;
goto error;
}
*pagesize = ntohl(msg->data.tinit.page_size);
plog("pagesize=%d\n", *pagesize);
if (*pagesize <= 0) {
ret = -EINVAL;
goto error;
}
options = ntohl(msg->data.tinit.opt_num);
for (i = 0; i < options; i++) {
offset += size;
opt = tracecmd_msg_buf_access(msg, offset);
size = ntohl(opt->size);
/* prevent a client from killing us */
if (size > MAX_OPTION_SIZE) {
plog("Exceed MAX_OPTION_SIZE\n");
ret = -EINVAL;
goto error;
}
s = process_option(opt);
/* do we understand this option? */
if (!s) {
plog("Cannot understand(%d:%d:%d)\n",
i, ntohl(opt->size), ntohl(opt->opt_cmd));
ret = -EINVAL;
goto error;
}
}
return 0;
error:
error_operation_for_server(msg);
return ret;
}
int tracecmd_msg_send_port_array(int fd, int total_cpus, int *ports)
{
int ret;
cpu_count = total_cpus;
port_array = ports;
ret = tracecmd_msg_send(fd, MSG_RINIT);
if (ret < 0)
return ret;
return 0;
}
void tracecmd_msg_send_close_msg(void)
{
tracecmd_msg_send(psfd, MSG_CLOSE);
}
static void make_meta(const char *buf, int buflen, struct tracecmd_msg *msg)
{
int offset = offsetof(struct tracecmd_msg, data.meta.str.buf);
msg->data.meta.str.size = htonl(buflen);
msgcpy(msg, offset, buf, buflen);
}
int tracecmd_msg_metadata_send(int fd, const char *buf, int size)
{
struct tracecmd_msg *msg;
int n, len;
int ret;
int count = 0;
ret = tracecmd_msg_create(MSG_SENDMETA, &msg);
if (ret < 0)
return ret;
n = size;
do {
if (n > TRACECMD_MSG_META_MAX_LEN) {
make_meta(buf+count, TRACECMD_MSG_META_MAX_LEN, msg);
n -= TRACECMD_MSG_META_MAX_LEN;
count += TRACECMD_MSG_META_MAX_LEN;
} else {
make_meta(buf+count, n, msg);
/*
* TRACECMD_MSG_META_MAX_LEN is stored in msg->size,
* so update the size to the correct value.
*/
len = TRACECMD_MSG_META_MIN_LEN + n;
msg->size = htonl(len);
n = 0;
}
ret = msg_do_write_check(fd, msg);
if (ret < 0)
break;
} while (n);
free(msg);
return ret;
}
int tracecmd_msg_finish_sending_metadata(int fd)
{
int ret;
ret = tracecmd_msg_send(fd, MSG_FINMETA);
if (ret < 0)
return ret;
/* psfd will be used for closing */
psfd = fd;
return 0;
}
int tracecmd_msg_collect_metadata(int ifd, int ofd)
{
struct tracecmd_msg *msg;
char buf[TRACECMD_MSG_MAX_LEN];
u32 s, t, n, cmd;
int offset = TRACECMD_MSG_META_MIN_LEN;
int ret;
msg = (struct tracecmd_msg *)buf;
do {
ret = tracecmd_msg_recv_wait(ifd, msg);
if (ret < 0) {
if (ret == -ETIMEDOUT)
warning("Connection timed out\n");
else
warning("reading client");
return ret;
}
cmd = ntohl(msg->cmd);
if (cmd == MSG_FINMETA) {
/* Finish receiving meta data */
break;
} else if (cmd != MSG_SENDMETA)
goto error;
n = ntohl(msg->data.meta.str.size);
t = n;
s = 0;
do {
s = write(ofd, buf+s+offset, t);
if (s < 0) {
if (errno == EINTR)
continue;
warning("writing to file");
return -errno;
}
t -= s;
s = n - t;
} while (t);
} while (cmd == MSG_SENDMETA);
/* check the finish message of the client */
while (!done) {
ret = tracecmd_msg_recv(ifd, msg);
if (ret < 0) {
warning("reading client");
return ret;
}
msg = (struct tracecmd_msg *)buf;
cmd = ntohl(msg->cmd);
if (cmd == MSG_CLOSE)
/* Finish this connection */
break;
else {
warning("Not accept the message %d", ntohl(msg->cmd));
ret = -EINVAL;
goto error;
}
}
return 0;
error:
error_operation_for_server(msg);
return ret;
}