Skip to content

Commit e88160c

Browse files
authored
Merge pull request #11 from OpenCloudOS/future
add some future
2 parents 52d8918 + 5bba31f commit e88160c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+746
-645
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
export VERSION = 1.2.1
2+
export VERSION = 1.2.2
33
RELEASE ?= .tl3
44
export RELEASE
55

6-
targets := droptrace nodetrace src legacy
6+
targets := nodetrace src legacy
77
targets-call = for i in $1; do make $2 -C $$i $@; done
88
man-target := script/zh_CN/nettrace.8
99

@@ -45,7 +45,7 @@ install:
4545
done
4646

4747
@mkdir -p $(BCOMP); cd $(BCOMP); cp $(SCRIPT)/bash-completion.sh \
48-
./nettrace; ln -s nettrace droptrace
48+
./nettrace
4949

5050
pack:
5151
@make clean

README.md

Lines changed: 99 additions & 35 deletions
Large diffs are not rendered by default.

common.mk

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ KERNEL_CFLAGS += $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
4444
cmd_download = @if [ ! -f $(1) ]; then wget -O $(1) $(REMOTE_ROOT)/$(2); fi
4545
cmd_exist = $(if $(wildcard $(1)),$(2),$(3))
4646
cmd_or_exist = $(call cmd_exist,$(1),$(1),$(2))
47-
ifeq ("$(wildcard $(HEADERS))$(wildcard $(BTF))",)
47+
ifeq ("$(wildcard $(HEADERS))$(wildcard $(BTF))","")
4848
$(error BTF is not found in your system, please install kernel headers)
4949
endif
5050

@@ -56,24 +56,12 @@ ifeq ($(if $(VMLINUX),$(wildcard $(VMLINUX)),"pass"),)
5656
$(error vmlinux path not exist)
5757
endif
5858

59-
# preferred to generate drop reason from
60-
ifeq ("$(KERNEL)$(VMLINUX)","")
61-
DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\
62-
$(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,\
63-
$(call cmd_exist,$(BTF),vmlinux.h,)))
64-
endif
65-
6659
# preferred to compile from kernel headers, then BTF
6760
mode := $(if $(VMLINUX),'btf',$(call cmd_exist,$(HEADERS),'kernel','btf'))
6861
ifeq ($(mode),'btf')
69-
DROP_REASON ?= vmlinux.h
7062
kheaders_cmd := ln -s vmlinux.h kheaders.h
7163
kheaders_dep := vmlinux.h
7264
else
73-
ifndef DROP_REASON
74-
DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\
75-
$(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,))
76-
endif
7765
kheaders_cmd := ln -s vmlinux_header.h kheaders.h
7866
BPF_CFLAGS += $(KERNEL_CFLAGS)
7967
endif
@@ -92,45 +80,24 @@ vmlinux.h:
9280
kheaders.h: $(kheaders_dep)
9381
$(call kheaders_cmd)
9482

95-
drop_reason.h: $(DROP_REASON)
96-
rm -rf $@
97-
ifneq ($(strip $(DROP_REASON)),)
98-
@awk 'BEGIN{ print "#ifndef _H_SKB_DROP_REASON"; \
99-
print "#define _H_SKB_DROP_REASON\n";\
100-
system("sed -e \"/enum skb_drop_reason {/,/}/!d\" $< >> $@");\
101-
print "\n#define __DEFINE_SKB_REASON(FN) \\";\
102-
}\
103-
/^enum skb_drop/ { dr=1; }\
104-
/^\};/ { dr=0; }\
105-
/^\tSKB_DROP_REASON_/ {\
106-
if (dr) {\
107-
sub(/SKB_DROP_REASON_/, "", $$1);\
108-
sub(/,/, "", $$1);\
109-
printf "\tFN(%s)\t\\\n", $$1;\
110-
}\
111-
}\
112-
END{ print "\n#endif" }' $< >> $@
113-
@echo generated drop_reason.h
114-
else
115-
touch $@
116-
@echo drop reason not supported, skips
117-
endif
118-
11983
progs/%.o: progs/%.c kheaders.h
12084
clang -O2 -c -g -S -Wall -Wno-pointer-sign -Wno-unused-value \
12185
-Wno-incompatible-pointer-types-discards-qualifiers \
12286
-fno-asynchronous-unwind-tables \
123-
$< -emit-llvm -Wno-unknown-attributes $(BPF_CFLAGS) -o - | \
87+
$< -emit-llvm -Wno-unknown-attributes $(BPF_CFLAGS) -Xclang \
88+
-disable-llvm-passes -o - | \
89+
opt -O2 -mtriple=bpf-pc-linux | \
90+
llvm-dis | \
12491
llc -march=bpf -filetype=obj -o $@
12592
@file $@ | grep debug_info > /dev/null || (rm $@ && exit 1)
12693

12794
%.skel.h: %.o
128-
$(BPFTOOL) gen skeleton $< > $@
95+
$(BPFTOOL) gen skeleton $< $(SKEL_FLAGS) > $@
12996

13097
$(bpf_progs): %: %.skel.h
131-
@echo "bpf compile success"
98+
@:
13299

133-
bpf: $(bpf_progs)
100+
bpf: $(bpf_progs) $(bpf_progs_ext)
134101

135102
$(progs): %: %.c bpf
136103
@if [ -n "$(prog-$@)" ]; then \

component/arg_parse.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ int parse_args(int argc, char *argv[], arg_config_t *config,
130130
}
131131
S_SET(bool, true);
132132
break;
133+
case OPTION_IPV6:
134+
if (ipv6toi(optarg, item->dest)) {
135+
printf("invalid ip address: %s\n", optarg);
136+
goto err;
137+
}
138+
S_SET(bool, true);
139+
break;
133140
case OPTION_HELP:
134141
goto help;
135142
case OPTION_PROTO: {

component/arg_parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum option_type {
1515
OPTION_U32,
1616
OPTION_INT,
1717
OPTION_IPV4,
18+
OPTION_IPV6,
1819
OPTION_HELP,
1920
OPTION_BLANK,
2021
OPTION_PROTO,

component/net_utils.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "net_utils.h"
44
#include "arg_parse.h"
55

6+
#define _LINUX_IN_H
7+
#include <netinet/in.h>
8+
69
typedef struct {
710
char *name;
811
int val;
@@ -143,3 +146,39 @@ int proto2i(char *proto, int *dest)
143146
return 4;
144147
return 0;
145148
}
149+
150+
void i2ipv6(char *dest, __u8 ip[])
151+
{
152+
int i = 0, offset = 0;
153+
154+
for (; i < 16; i += 2) {
155+
if (ip[i] || ip[i + 1])
156+
offset += sprintf(dest + offset, "%02x%02x:",
157+
ip[i], ip[i + 1]);
158+
else
159+
offset += sprintf(dest + offset, ":");
160+
}
161+
*(dest + offset - 1) = '\0';
162+
}
163+
164+
int ipv6toi(char *ip, __u8 *dest)
165+
{
166+
u16 *c = (u16 *)dest;
167+
u32 t[8] = {}, i = 0;
168+
169+
if (sscanf(ip, "%x:%x:%x:%x:%x:%x:%x:%x", t, t + 1, t + 2,
170+
t + 3, t + 4, t + 5, t + 6, t + 7) == 8)
171+
goto is_valid;
172+
173+
memset(t, 0, sizeof(t));
174+
if (sscanf(ip, "%x::%x:%x:%x:%x", t, t + 4, t + 5, t + 6,
175+
t + 7) == 5)
176+
goto is_valid;
177+
178+
return -1;
179+
180+
is_valid:
181+
for (i = 0; i < 8; i++)
182+
c[i] = htons(t[i]);
183+
return 0;
184+
}

component/net_utils.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ typedef __u32 u32;
2525
typedef __s64 s64;
2626
typedef __u64 u64;
2727

28+
extern char *l4_proto_names[];
29+
2830
static inline void i2ip(char *dest, __u32 ip)
2931
{
3032
u8 *t = (u8 *)&ip;
@@ -48,12 +50,13 @@ static inline int ip2i(char *ip, __u32 *dest)
4850
return 0;
4951
}
5052

51-
int proto2i(char *proto, int *dest);
52-
53-
extern char *l4_proto_names[];
5453
static inline char *i2l4(u8 num)
5554
{
5655
return l4_proto_names[num];
5756
}
5857

58+
int proto2i(char *proto, int *dest);
59+
void i2ipv6(char *dest, u8 ip[]);
60+
int ipv6toi(char *ip, u8 *dest);
61+
5962
#endif

component/sys_utils.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <string.h>
1313
#include <stdlib.h>
1414
#include <sys/resource.h>
15+
#include <sys/utsname.h>
1516

1617
#include "sys_utils.h"
1718

@@ -57,3 +58,25 @@ int liberate_l()
5758
struct rlimit lim = {RLIM_INFINITY, RLIM_INFINITY};
5859
return setrlimit(RLIMIT_MEMLOCK, &lim);
5960
}
61+
62+
bool fsearch(FILE *f, char *target)
63+
{
64+
char tmp[128];
65+
66+
while (fscanf(f, "%s", tmp) == 1) {
67+
if (strstr(tmp, target))
68+
return true;
69+
}
70+
return false;
71+
}
72+
73+
int kernel_version()
74+
{
75+
int major, minor, patch;
76+
struct utsname buf;
77+
78+
uname(&buf);
79+
sscanf(buf.release, "%d.%d.%d", &major, &minor, &patch);
80+
81+
return kv_to_num(major, minor, patch);
82+
}

component/sys_utils.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
#include <stdlib.h>
77
#include <unistd.h>
88
#include <stdbool.h>
9+
#include <sys/types.h>
10+
#include <sys/stat.h>
11+
#include <fcntl.h>
12+
#include <stdio.h>
913

1014
extern int log_level;
1115

12-
int execf(char *output, char *fmt, ...);
13-
int exec(char *cmd, char *output);
14-
int liberate_l();
16+
int execf(char *output, char *fmt, ...);
17+
int exec(char *cmd, char *output);
18+
int liberate_l();
19+
bool fsearch(FILE *f, char *target);
20+
int kernel_version();
1521

1622
static inline int simple_exec(char *cmd)
1723
{
@@ -23,6 +29,17 @@ static inline bool file_exist(char *path)
2329
return access(path, F_OK) == 0;
2430
}
2531

32+
static inline int kv_to_num(int major, int minor, int patch)
33+
{
34+
return (major << 16) + (minor << 8) + patch;
35+
}
36+
37+
/* compare current kernel version with the provided one */
38+
static inline int kv_compare(int major, int minor, int patch)
39+
{
40+
return kernel_version() - kv_to_num(major, minor, patch);
41+
}
42+
2643
#define pr_level(level, target, fmt, args...) \
2744
do { \
2845
if (level <= log_level) \

droptrace/.gitignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)