Skip to content

Commit f827f97

Browse files
committed
Add command tracking code together with PID
1 parent 1611601 commit f827f97

7 files changed

+37
-13
lines changed

counter.c

+18-12
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@
3939
#define TC_ACT_UNSPEC -1
4040
#define AF_INET 2
4141
#define AF_INET6 10
42+
#define TASK_COMM_LEN 16
4243

4344
#define OK 1
4445
#define NOK 0
4546

4647
// Map key struct for IP traffic
4748
typedef struct statkey_t {
48-
struct in6_addr srcip; // source IPv6 address
49-
struct in6_addr dstip; // destination IPv6 address
50-
__u16 src_port; // source port
51-
__u16 dst_port; // destination port
52-
__u8 proto; // transport protocol
53-
pid_t pid; // process ID
49+
struct in6_addr srcip; // source IPv6 address
50+
struct in6_addr dstip; // destination IPv6 address
51+
__u16 src_port; // source port
52+
__u16 dst_port; // destination port
53+
__u8 proto; // transport protocol
54+
pid_t pid; // process ID
55+
char comm[TASK_COMM_LEN]; // process command
5456
} statkey;
5557

5658
// Map value struct with counters
@@ -482,8 +484,8 @@ static inline void update_val(statkey *key, size_t size) {
482484
/**
483485
* Hook function for kprobe on tcp_sendmsg function.
484486
*
485-
* Populates the statkey structure with information from the socket and the
486-
* process ID associated with the socket, and updates the packet and byte
487+
* Populates the statkey structure with information from the UDP packet and the
488+
* process ID associated with the packet, and updates the packet and byte
487489
* counters in the packet count map.
488490
*
489491
* @param sk pointer to the socket structure
@@ -500,6 +502,7 @@ int BPF_KPROBE(tcp_sendmsg, struct sock *sk, struct msghdr *msg, size_t size) {
500502
__builtin_memset(&key, 0, sizeof(key));
501503

502504
pid_t pid = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
505+
bpf_get_current_comm(&key.comm, sizeof(key.comm));
503506

504507
process_tcp(sk, &key, pid);
505508
update_val(&key, size);
@@ -531,6 +534,7 @@ int BPF_KPROBE(tcp_cleanup_rbuf, struct sock *sk, int copied) {
531534
__builtin_memset(&key, 0, sizeof(key));
532535

533536
pid_t pid = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
537+
bpf_get_current_comm(&key.comm, sizeof(key.comm));
534538

535539
process_tcp(sk, &key, pid);
536540
update_val(&key, copied);
@@ -541,12 +545,12 @@ int BPF_KPROBE(tcp_cleanup_rbuf, struct sock *sk, int copied) {
541545
/**
542546
* Hook function for kprobe on ip_send_skb function.
543547
*
544-
* Populates the statkey structure with information from the UDP packet and the
545-
* process ID associated with the packet, and updates the packet and byte
548+
* Populates the statkey structure with information from the socket and the
549+
* process ID associated with the socket, and updates the packet and byte
546550
* counters in the packet count map.
547551
*
548-
* @param net pointer to the network namespace
549-
* @param skb pointer to the socket buffer containing the UDP packet
552+
* @param net pointer to the network namespace structure
553+
* @param skb pointer to the socket buffer
550554
*
551555
* @return 0
552556
*
@@ -563,6 +567,7 @@ int BPF_KPROBE(ip_send_skb, struct net *net, struct sk_buff *skb) {
563567
__builtin_memset(&key, 0, sizeof(key));
564568

565569
pid_t pid = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
570+
bpf_get_current_comm(&key.comm, sizeof(key.comm));
566571

567572
size_t msglen = process_udp_send(skb, &key, pid);
568573
update_val(&key, msglen);
@@ -591,6 +596,7 @@ int BPF_KPROBE(skb_consume_udp, struct sock *sk, struct sk_buff *skb, int len) {
591596
__builtin_memset(&key, 0, sizeof(key));
592597

593598
pid_t pid = bpf_get_current_pid_tgid() & 0xFFFFFFFF;
599+
bpf_get_current_comm(&key.comm, sizeof(key.comm));
594600

595601
process_udp_recv(skb, &key, pid);
596602
update_val(&key, len);

counter_arm64_bpfel.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

counter_arm64_bpfel.o

584 Bytes
Binary file not shown.

counter_x86_bpfel.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

counter_x86_bpfel.o

584 Bytes
Binary file not shown.

output.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func processMap(m *ebpf.Map, start time.Time) ([]statEntry, error) {
6666
Packets: val.Packets,
6767
Bitrate: 8 * float64(val.Bytes) / dur,
6868
Pid: key.Pid,
69+
Comm: byte2String(key.Comm[:]),
6970
})
7071
}
7172

@@ -111,7 +112,7 @@ func outputPlain(m []statEntry) {
111112
sb.WriteString(fmt.Sprintf("bitrate: %v, packets: %d, bytes: %d, proto: %v, src: %v:%v, dst: %v:%v",
112113
formatBitrate(v.Bitrate), v.Packets, v.Bytes, v.Proto, v.SrcIP, v.SrcPort, v.DstIP, v.DstPort))
113114
if *useKprobes {
114-
sb.WriteString(fmt.Sprintf(", pid: %d", v.Pid))
115+
sb.WriteString(fmt.Sprintf(", pid: %d, comm: %v", v.Pid, v.Comm))
115116
}
116117
sb.WriteString("\n")
117118
}
@@ -130,3 +131,17 @@ func outputJSON(m []statEntry) {
130131

131132
fmt.Printf("%v\n", string(out))
132133
}
134+
135+
// byte2String converts a slice of int8 to a string.
136+
//
137+
// It takes a slice of int8 as a parameter, creates a new slice of byte of the same length,
138+
// copies the values of the int8 slice to the byte slice, and converts the byte slice to a string.
139+
// The resulting string is then returned.
140+
func byte2String(bs []int8) string {
141+
b := make([]byte, len(bs))
142+
for i, v := range bs {
143+
b[i] = byte(v)
144+
}
145+
146+
return string(b)
147+
}

types.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type statEntry struct {
66
SrcIP netip.Addr `json:"srcIp"`
77
DstIP netip.Addr `json:"dstIp"`
88
Proto string `json:"proto"`
9+
Comm string `json:"comm"`
910
Bytes uint64 `json:"bytes"`
1011
Packets uint64 `json:"packets"`
1112
Bitrate float64 `json:"bitrate"`

0 commit comments

Comments
 (0)