-
Notifications
You must be signed in to change notification settings - Fork 1
/
skbtrace.c
1747 lines (1483 loc) · 36.6 KB
/
skbtrace.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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Heavily borrowed from blktrace.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <getopt.h>
#include <sched.h>
#include <unistd.h>
#include <poll.h>
#include <signal.h>
#include <pthread.h>
#include <locale.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <dirent.h>
#include "skbtrace.h"
#include "list.h"
/*
* You may want to increase this even more, if you are logging at a high
* rate and see skipped/missed events
*/
#define BUF_SIZE (512 * 1024)
#define BUF_NR (4)
#define FILE_VBUF_SIZE (128 * 1024)
#define DEBUGFS_TYPE (0x64626720)
enum thread_status {
Th_running,
Th_leaving,
Th_error
};
/*
* Generic stats collected: nevents can be _roughly_ estimated by data_read
* (discounting pdu...)
*
* These fields are updated w/ pdc_dr_update & pdc_nev_update below.
*/
struct pdc_stats {
unsigned long long data_read;
unsigned long long nevents;
};
struct netns {
struct list_head head;
char *name; /* path to device special file */
struct pdc_stats *stats;
int control_fd, netns_fd;
int ncpus;
unsigned long long drops;
/*
* For piped output only:
*
* Each tracer will have a tracer_netns_head that it will add new
* data onto. It's list is protected above (tracer_netns_head.mutex)
* and it will signal the processing thread using the dp_cond,
* dp_mutex & dp_entries variables above.
*/
struct tracer_netns_head *heads;
int setup_done; /* ioctl SIOCSKBTRACESETUP done */
struct io_info *ios;
};
/*
* For piped output to stdout we will have each tracer thread (one per dev)
* tack buffers read from the relay queues on a per-device list.
*
* The main thread will then collect trace buffers from each of lists in turn.
*
* We will use a mutex to guard each of the trace_buf list. The tracers
* can then signal the main thread using <dp_cond,dp_mutex> and
* dp_entries. (When dp_entries is 0, and a tracer adds an entry it will
* signal. When dp_entries is 0, the main thread will wait for that condition
* to be signalled.)
*
* adb: It may be better just to have a large buffer per tracer per dev,
* and then use it as a ring-buffer. This would certainly cut down a lot
* of malloc/free thrashing, at the cost of more memory movements (potentially).
*/
struct trace_buf {
struct list_head head;
struct netns *netns;
void *buf;
int cpu, len;
};
struct tracer_netns_head {
pthread_mutex_t mutex;
struct list_head head;
struct trace_buf *prev;
};
/*
* Used to handle the mmap() interfaces for output file (containing traces)
*/
struct mmap_info {
void *fs_buf;
unsigned long long fs_size, fs_max_size, fs_off, fs_buf_len;
unsigned long buf_size, buf_nr;
int pagesize;
};
/*
* Each thread doing work on a (client) side of blktrace will have one
* of these. The ios array contains input/output information, pfds holds
* poll() data. The volatile's provide flags to/from the main executing
* thread.
*/
struct tracer {
struct list_head head;
struct io_info *ios;
struct pollfd *pfds;
pthread_t thread;
int cpu, nios;
volatile int status, is_done;
};
/*
* This structure is (generically) used to providide information
* for a read-to-write set of values.
*
* ifn & ifd represent input information
*
* ofn, ofd, ofp, obuf & mmap_info are used for output file (optionally).
*/
struct io_info {
struct netns *netns;
FILE *ofp;
char *obuf;
/*
* mmap controlled output files
*/
struct mmap_info mmap_info;
/*
* Input/output file descriptors & names
*/
int ifd, ofd;
char ifn[MAXPATHLEN + 64];
char ofn[MAXPATHLEN + 64];
};
static char skbtrace_version[] = "0.1";
static int num_netns;
static int max_cpus;
static int ncpus;
static cpu_set_t *online_cpus;
static int pagesize;
static int kill_running_trace;
static int stop_watch;
static int piped_output;
static char *debugfs_path = "/sys/kernel/debug";
static char *output_name;
static char *output_dir;
static unsigned long buf_size = BUF_SIZE;
static unsigned long buf_nr = BUF_NR;
static FILE *pfp;
static LIST_HEAD(netnses);
static LIST_HEAD(tracers);
static volatile int done;
/*
* tracer threads add entries, the main thread takes them off and processes
* them. These protect the dp_entries variable.
*/
static pthread_cond_t dp_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t dp_mutex = PTHREAD_MUTEX_INITIALIZER;
static volatile int dp_entries;
/*
* These synchronize master / thread interactions.
*/
static pthread_cond_t mt_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mt_mutex = PTHREAD_MUTEX_INITIALIZER;
static volatile int nthreads_running;
static volatile int nthreads_leaving;
static volatile int nthreads_error;
static volatile int tracers_run;
static int (*handle_pfds)(struct tracer *, int, int);
static int (*handle_list)(struct tracer_netns_head *, struct list_head *);
#define S_OPTS "r:o:kw:vVb:n:D:h"
static struct option l_opts[] = {
{
.name = "relay",
.has_arg = required_argument,
.flag = NULL,
.val = 'r'
},
{
.name = "output",
.has_arg = required_argument,
.flag = NULL,
.val = 'o'
},
{
.name = "kill",
.has_arg = no_argument,
.flag = NULL,
.val = 'k'
},
{
.name = "stopwatch",
.has_arg = required_argument,
.flag = NULL,
.val = 'w'
},
{
.name = "version",
.has_arg = no_argument,
.flag = NULL,
.val = 'v'
},
{
.name = "version",
.has_arg = no_argument,
.flag = NULL,
.val = 'V'
},
{
.name = "buffer-size",
.has_arg = required_argument,
.flag = NULL,
.val = 'b'
},
{
.name = "num-sub-buffers",
.has_arg = required_argument,
.flag = NULL,
.val = 'n'
},
{
.name = "output-dir",
.has_arg = required_argument,
.flag = NULL,
.val = 'D'
},
{
.name = "help",
.has_arg = no_argument,
.flag = NULL,
.val = 'h'
},
{
.name = NULL,
}
};
static char usage_str[] = "\n\n" \
"[ -r <debugfs path> | --relay=<debugfs path> ]\n" \
"[ -o <file> | --output=<file>]\n" \
"[ -k | --kill]\n" \
"[ -D <dir> | --output-dir=<dir>\n" \
"[ -w <time> | --stopwatch=<time>]\n" \
"[ -b <size> | --buffer-size]\n" \
"[ -n <number> | --num-sub-buffers=<number>]\n" \
"[ -v <version> | --version]\n" \
"[ -V <version> | --version]\n" \
"[ -h | --help]\n" \
"\t-d Use specified device. May also be given last after options\n" \
"\t-r Path to mounted debugfs, defaults to /sys/kernel/debug\n" \
"\t-o File(s) to send output to\n" \
"\t-k Kill tracing\n" \
"\t-D Directory to prepend to output file names\n" \
"\t-w Stop after defined time, in seconds\n" \
"\t-b Sub buffer size in KiB (default 512)\n" \
"\t-n Number of sub buffers (default 4)\n" \
"\t-v Print program version info\n" \
"\t-V Print program version info\n" \
"\t-h Print this message\n\n";
static void clear_events(struct pollfd *pfd)
{
pfd->events = 0;
pfd->revents = 0;
}
static inline int use_tracer_netnses(void)
{
return piped_output;
}
static inline void pdc_dr_update(struct netns *netns, int cpu, int data_read)
{
netns->stats[cpu].data_read += data_read;
}
static inline void pdc_nev_update(struct netns *netns, int cpu, int nevents)
{
netns->stats[cpu].nevents += nevents;
}
static void show_usage(char *prog)
{
fprintf(stderr, "Usage: %s [options] netns...\nOptions: %s", prog, usage_str);
}
/*
* Create a timespec 'msec' milliseconds into the future
*/
static inline void make_timespec(struct timespec *tsp, long delta_msec)
{
struct timeval now;
gettimeofday(&now, NULL);
tsp->tv_sec = now.tv_sec;
tsp->tv_nsec = 1000L * now.tv_usec;
tsp->tv_nsec += (delta_msec * 1000000L);
if (tsp->tv_nsec > 1000000000L) {
long secs = tsp->tv_nsec / 1000000000L;
tsp->tv_sec += secs;
tsp->tv_nsec -= (secs * 1000000000L);
}
}
/*
* Add a timer to ensure wait ends
*/
static void t_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
{
struct timespec ts;
make_timespec(&ts, 50);
pthread_cond_timedwait(cond, mutex, &ts);
}
static void unblock_tracers(void)
{
pthread_mutex_lock(&mt_mutex);
tracers_run = 1;
pthread_cond_broadcast(&mt_cond);
pthread_mutex_unlock(&mt_mutex);
}
static void tracer_wait_unblock(struct tracer *tp)
{
pthread_mutex_lock(&mt_mutex);
while (!tp->is_done && !tracers_run)
pthread_cond_wait(&mt_cond, &mt_mutex);
pthread_mutex_unlock(&mt_mutex);
}
static void tracer_signal_ready(struct tracer *tp,
enum thread_status th_status,
int status)
{
pthread_mutex_lock(&mt_mutex);
tp->status = status;
if (th_status == Th_running)
nthreads_running++;
else if (th_status == Th_error)
nthreads_error++;
else
nthreads_leaving++;
pthread_cond_signal(&mt_cond);
pthread_mutex_unlock(&mt_mutex);
}
static void wait_tracers_ready(int ncpus_started)
{
pthread_mutex_lock(&mt_mutex);
while ((nthreads_running + nthreads_error) < ncpus_started)
t_pthread_cond_wait(&mt_cond, &mt_mutex);
pthread_mutex_unlock(&mt_mutex);
}
static void wait_tracers_leaving(void)
{
pthread_mutex_lock(&mt_mutex);
while (nthreads_leaving < nthreads_running)
t_pthread_cond_wait(&mt_cond, &mt_mutex);
pthread_mutex_unlock(&mt_mutex);
}
static void init_mmap_info(struct mmap_info *mip)
{
mip->buf_size = buf_size;
mip->buf_nr = buf_nr;
mip->pagesize = pagesize;
}
static void netns_free(struct netns *netns)
{
if (netns->stats)
free(netns->stats);
if (netns->ios)
free(netns->ios);
if (netns->name)
free(netns->name);
free(netns);
}
static int lock_on_cpu(int cpu)
{
cpu_set_t * cpu_mask;
size_t size;
cpu_mask = CPU_ALLOC(max_cpus);
size = CPU_ALLOC_SIZE(max_cpus);
CPU_ZERO_S(size, cpu_mask);
CPU_SET_S(cpu, size, cpu_mask);
if (sched_setaffinity(0, size, cpu_mask) < 0) {
CPU_FREE(cpu_mask);
return errno;
}
CPU_FREE(cpu_mask);
return 0;
}
static int increase_limit(int resource, rlim_t increase)
{
struct rlimit rlim;
int save_errno = errno;
if (!getrlimit(resource, &rlim)) {
rlim.rlim_cur += increase;
if (rlim.rlim_cur >= rlim.rlim_max)
rlim.rlim_max = rlim.rlim_cur + increase;
if (!setrlimit(resource, &rlim))
return 1;
}
errno = save_errno;
return 0;
}
static int handle_open_failure(void)
{
if (errno == ENFILE || errno == EMFILE)
return increase_limit(RLIMIT_NOFILE, 16);
return 0;
}
static int handle_mem_failure(size_t length)
{
if (errno == ENFILE)
return handle_open_failure();
else if (errno == ENOMEM)
return increase_limit(RLIMIT_MEMLOCK, 2 * length);
return 0;
}
static FILE *my_fopen(const char *path, const char *mode)
{
FILE *fp;
do {
fp = fopen(path, mode);
} while (fp == NULL && handle_open_failure());
return fp;
}
static int my_open(const char *path, int flags)
{
int fd;
do {
fd = open(path, flags);
} while (fd < 0 && handle_open_failure());
return fd;
}
static void *my_mmap(void *addr, size_t length, int prot, int flags, int fd,
off_t offset)
{
void *new;
do {
new = mmap(addr, length, prot, flags, fd, offset);
} while (new == MAP_FAILED && handle_mem_failure(length));
return new;
}
static int my_mlock(struct tracer *tp,
const void *addr, size_t len)
{
int ret, retry = 0;
do {
ret = mlock(addr, len);
if ((retry >= 10) && tp && tp->is_done)
break;
retry++;
} while (ret < 0 && handle_mem_failure(len));
return ret;
}
static int setup_mmap(int fd, unsigned int maxlen,
struct mmap_info *mip,
struct tracer *tp)
{
if (mip->fs_off + maxlen > mip->fs_buf_len) {
unsigned long nr = max(16, mip->buf_nr);
if (mip->fs_buf) {
munlock(mip->fs_buf, mip->fs_buf_len);
munmap(mip->fs_buf, mip->fs_buf_len);
mip->fs_buf = NULL;
}
mip->fs_off = mip->fs_size & (mip->pagesize - 1);
mip->fs_buf_len = (nr * mip->buf_size) - mip->fs_off;
mip->fs_max_size += mip->fs_buf_len;
if (ftruncate(fd, mip->fs_max_size) < 0) {
perror("setup_mmap: ftruncate");
return 1;
}
mip->fs_buf = my_mmap(NULL, mip->fs_buf_len, PROT_WRITE,
MAP_SHARED, fd,
mip->fs_size - mip->fs_off);
if (mip->fs_buf == MAP_FAILED) {
perror("setup_mmap: mmap");
return 1;
}
if (my_mlock(tp, mip->fs_buf, mip->fs_buf_len) < 0) {
perror("setup_mlock: mlock");
return 1;
}
}
return 0;
}
static int stop_trace(int control_fd, int netns_fd)
{
struct skb_user_trace_setup sbts = { .netns_fd = netns_fd } ;
/*
* Should be stopped, don't complain if it isn't
*/
ioctl(control_fd, SIOCSKBTRACESTOP, &sbts);
return ioctl(control_fd, SIOCSKBTRACETEARDOWN, &sbts);
}
static int write_data(char *buf, int len)
{
int ret;
rewrite:
ret = fwrite(buf, len, 1, pfp);
if (ferror(pfp) || ret != 1) {
if (errno == EINTR) {
clearerr(pfp);
goto rewrite;
}
if (!piped_output || (errno != EPIPE && errno != EBADF)) {
fprintf(stderr, "write(%d) failed: %d/%s\n",
len, errno, strerror(errno));
}
goto err;
}
fflush(pfp);
return 0;
err:
clearerr(pfp);
return 1;
}
static int setup_sbts(void)
{
struct list_head *p;
int ret = 0;
__list_for_each(p, &netnses) {
struct skb_user_trace_setup sbts;
struct netns *netns = list_entry(p, struct netns, head);
memset(&sbts, 0, sizeof(sbts));
sbts.buf_size = buf_size;
sbts.buf_nr = buf_nr;
sbts.netns_fd = netns->netns_fd;
strcpy(sbts.name, netns->name);
if (ioctl(netns->control_fd, SIOCSKBTRACESETUP, &sbts) >= 0) {
netns->ncpus = max_cpus;
netns->setup_done = 1;
if (netns->stats)
free(netns->stats);
netns->stats = calloc(netns->ncpus, sizeof(*netns->stats));
memset(netns->stats, 0, netns->ncpus * sizeof(*netns->stats));
} else {
fprintf(stderr, "SIOCSKBTRACESETUP(2) %s failed: %d/%s\n",
netns->name, errno, strerror(errno));
ret++;
}
}
return ret;
}
static void start_trace(void)
{
struct list_head *p;
__list_for_each(p, &netnses) {
struct netns *netns = list_entry(p, struct netns, head);
struct skb_user_trace_setup sbts = { .netns_fd = netns->netns_fd } ;
if (ioctl(netns->control_fd, SIOCSKBTRACESTART, &sbts) < 0) {
fprintf(stderr, "SIOCSKBTRACESTART %s failed: %d/%s\n",
netns->name, errno, strerror(errno));
}
}
}
static inline struct trace_buf *alloc_trace_buf(int cpu, int bufsize)
{
struct trace_buf *tbp;
tbp = malloc(sizeof(*tbp) + bufsize);
INIT_LIST_HEAD(&tbp->head);
tbp->len = 0;
tbp->buf = (void *)(tbp + 1);
tbp->cpu = cpu;
tbp->netns = NULL; /* Will be set when tbp is added */
return tbp;
}
static void free_tracer_heads(struct netns *netns)
{
int cpu;
struct tracer_netns_head *hd;
for (cpu = 0, hd = netns->heads; cpu < max_cpus; cpu++, hd++) {
if (hd->prev)
free(hd->prev);
pthread_mutex_destroy(&hd->mutex);
}
free(netns->heads);
}
static int setup_tracer_netnses(void)
{
struct list_head *p;
__list_for_each(p, &netnses) {
int cpu;
struct tracer_netns_head *hd;
struct netns *netns = list_entry(p, struct netns, head);
netns->heads = calloc(max_cpus, sizeof(struct tracer_netns_head));
for (cpu = 0, hd = netns->heads; cpu < max_cpus; cpu++, hd++) {
INIT_LIST_HEAD(&hd->head);
pthread_mutex_init(&hd->mutex, NULL);
hd->prev = NULL;
}
}
return 0;
}
static inline void add_trace_buf(struct netns *netns, int cpu,
struct trace_buf **tbpp)
{
struct trace_buf *tbp = *tbpp;
struct tracer_netns_head *hd = &netns->heads[cpu];
tbp->netns = netns;
pthread_mutex_lock(&hd->mutex);
list_add_tail(&tbp->head, &hd->head);
pthread_mutex_unlock(&hd->mutex);
*tbpp = alloc_trace_buf(cpu, buf_size);
}
static inline void incr_entries(int entries_handled)
{
pthread_mutex_lock(&dp_mutex);
if (dp_entries == 0)
pthread_cond_signal(&dp_cond);
dp_entries += entries_handled;
pthread_mutex_unlock(&dp_mutex);
}
static void decr_entries(int handled)
{
pthread_mutex_lock(&dp_mutex);
dp_entries -= handled;
pthread_mutex_unlock(&dp_mutex);
}
static int wait_empty_entries(void)
{
pthread_mutex_lock(&dp_mutex);
while (!done && dp_entries == 0)
t_pthread_cond_wait(&dp_cond, &dp_mutex);
pthread_mutex_unlock(&dp_mutex);
return !done;
}
#ifndef NETNS_RUN_DIR
#define NETNS_RUN_DIR "/var/run/netns"
#endif
int open_netns(const char *name)
{
char pathbuf[PATH_MAX];
const char *path, *ptr;
path = name;
ptr = strchr(name, '/');
if (!ptr) {
snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
NETNS_RUN_DIR, name );
path = pathbuf;
}
return open(path, O_RDONLY);
}
static int add_netns(char *netns_name)
{
int fd;
struct netns *netns;
struct list_head *p;
if (netns_name) {
fd = open_netns(netns_name);
if (fd < 0) {
fprintf(stderr, "Invalid netns name %s specified: %d/%s\n",
netns_name, errno, strerror(errno));
return 1;
}
} else {
fd = INIT_NET_FD;
netns_name = "init_net";
}
__list_for_each(p, &netnses) {
struct netns *tmp = list_entry(p, struct netns, head);
if (!strcmp(tmp->name, netns_name))
return 0;
}
netns = malloc(sizeof(*netns));
memset(netns, 0, sizeof(*netns));
netns->name = strdup(netns_name);
netns->netns_fd = fd;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
fprintf(stderr, "Can not create a socket: %d/%s\n",
errno, strerror(errno));
free(netns->name);
close(netns->netns_fd);
free(netns);
return 1;
}
netns->control_fd = fd;
num_netns++;
list_add_tail(&netns->head, &netnses);
return 0;
}
static void rel_netnses(void)
{
struct list_head *p, *q;
list_for_each_safe(p, q, &netnses) {
struct netns *netns = list_entry(p, struct netns, head);
list_del(&netns->head);
if (netns->setup_done)
stop_trace(netns->control_fd, netns->netns_fd);
close(netns->netns_fd);
close(netns->control_fd);
if (netns->heads)
free_tracer_heads(netns);
netns_free(netns);
num_netns--;
}
}
/*
* Tack 'tbp's buf onto the tail of 'prev's buf
*/
static struct trace_buf *tb_combine(struct trace_buf *prev,
struct trace_buf *tbp)
{
unsigned long tot_len;
tot_len = prev->len + tbp->len;
if (tot_len > buf_size) {
/*
* tbp->head isn't connected (it was 'prev'
* so it had been taken off of the list
* before). Therefore, we can realloc
* the whole structures, as the other fields
* are "static".
*/
prev = realloc(prev, sizeof(*prev) + tot_len);
prev->buf = (void *)(prev + 1);
}
memcpy(prev->buf + prev->len, tbp->buf, tbp->len);
prev->len = tot_len;
free(tbp);
return prev;
}
static int handle_list_file(struct tracer_netns_head *hd,
struct list_head *list)
{
int off, t_len, nevents;
struct skb_trace_slot *t;
struct list_head *p, *q;
int entries_handled = 0;
struct trace_buf *tbp, *prev;
prev = hd->prev;
list_for_each_safe(p, q, list) {
tbp = list_entry(p, struct trace_buf, head);
list_del(&tbp->head);
entries_handled++;
/*
* If there was some leftover before, tack this new
* entry onto the tail of the previous one.
*/
if (prev)
tbp = tb_combine(prev, tbp);
/*
* See how many whole traces there are - send them
* all out in one go.
*/
off = 0;
nevents = 0;
while (off + (int)sizeof(*t) <= tbp->len) {
t = (struct skb_trace_slot*)(tbp->buf + off);
t_len = sizeof(*t);
if (off + t_len > tbp->len)
break;
off += t_len;
nevents++;
}
if (nevents)
pdc_nev_update(tbp->netns, tbp->cpu, nevents);
/*
* Write any full set of traces, any remaining data is kept
* for the next pass.
*/
if (off) {
if (write_data(tbp->buf, off) || off == tbp->len) {
free(tbp);
prev = NULL;
}
else {
/*
* Move valid data to beginning of buffer
*/
tbp->len -= off;
memmove(tbp->buf, tbp->buf + off, tbp->len);
prev = tbp;
}
} else
prev = tbp;
}
hd->prev = prev;
return entries_handled;
}
static void __process_trace_bufs(void)
{
int cpu;
struct list_head *p;
struct list_head list;
int handled = 0;
__list_for_each(p, &netnses) {
struct netns *netns = list_entry(p, struct netns, head);
struct tracer_netns_head *hd = netns->heads;
for (cpu = 0; cpu < max_cpus; cpu++, hd++) {
pthread_mutex_lock(&hd->mutex);
if (list_empty(&hd->head)) {
pthread_mutex_unlock(&hd->mutex);
continue;
}
list_replace_init(&hd->head, &list);
pthread_mutex_unlock(&hd->mutex);
handled += handle_list(hd, &list);
}
}
if (handled)
decr_entries(handled);
}
static void process_trace_bufs(void)
{
while (wait_empty_entries())
__process_trace_bufs();
}
static void clean_trace_bufs(void)
{
/*
* No mutex needed here: we're only reading from the lists,
* tracers are done
*/
while (dp_entries)
__process_trace_bufs();
}
static inline void read_err(int cpu, char *ifn)
{
if (errno != EAGAIN)
fprintf(stderr, "Thread %d failed read of %s: %d/%s\n",
cpu, ifn, errno, strerror(errno));
}
static int fill_ofname(char *dst, int dstlen, char *subdir, char *netns_name,
int cpu)
{
int len;
struct stat sb;
if (output_dir)
len = snprintf(dst, dstlen, "%s/", output_dir);
else
len = snprintf(dst, dstlen, "./");
if (subdir)
len += snprintf(dst + len, dstlen - len, "%s", subdir);
if (stat(dst, &sb) < 0) {
if (errno != ENOENT) {
fprintf(stderr,
"Destination dir %s stat failed: %d/%s\n",
dst, errno, strerror(errno));
return 1;
}
/*