Skip to content

Commit

Permalink
Merge branch 'linuxkerneltravel:develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
albertxu216 authored Nov 9, 2023
2 parents b905991 + f65d539 commit a1d4870
Show file tree
Hide file tree
Showing 8 changed files with 140,771 additions and 138 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/kvm_watcher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: kvm_watcher

on:
push:
branches:
- "*"
paths:
- 'eBPF_Supermarket/kvm_watcher/**'
- '.github/workflows/kvm_watcher.yml'
pull_request:
branches:
- "*"
paths:
- 'eBPF_Supermarket/kvm_watcher/**'
- '.github/workflows/kvm_watcher.yml'

jobs:
kvm_watcher-project-build-and-test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3

- name: Install dependencies
run: |
sudo apt install clang libelf1 libelf-dev zlib1g-dev
sudo apt install libbpf-dev
sudo apt install linux-tools-$(uname -r)
sudo apt install linux-cloud-tools-$(uname -r)
sudo modprobe kvm
- name: Run kvm_exits
run: |
cd eBPF_Supermarket/kvm_watcher/kvm_exits
make
sudo ./kvm_exits
339 changes: 221 additions & 118 deletions eBPF_Supermarket/Network_Subsystem/net_watcher/netwatcher.bpf.c

Large diffs are not rendered by default.

60 changes: 40 additions & 20 deletions eBPF_Supermarket/Network_Subsystem/net_watcher/netwatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ static int print_conns(struct netwatcher_bpf *skel) {

char s_ip_port_str[INET6_ADDRSTRLEN + 6];
char d_ip_port_str[INET6_ADDRSTRLEN + 6];

if(http_info){
printf("%u,%u,%llu\n",d.rcv_wnd, d.snd_cwnd,d.duration);
}

if (d.family == AF_INET) {
sprintf(s_ip_port_str, "%s:%d",
Expand Down Expand Up @@ -230,32 +234,32 @@ static int print_packet(void *ctx, void *packet_info, size_t size) {
} else {
sprintf(http_data, "-");
}
if (layer_time) {
printf("%-22p %-10u %-10u %-10llu %-10llu %-10llu %-5d %s\n",
if(http_info == 0){
if (layer_time) {
printf("%-22p %-10u %-10u %-10llu %-10llu %-10llu %-5d %s\n",
pack_info->sock, pack_info->seq, pack_info->ack,
pack_info->mac_time, pack_info->ip_time, pack_info->tcp_time,
pack_info->rx, http_data);
fprintf(file,
fprintf(file,
"packet{sock=\"%p\",seq=\"%u\",ack=\"%u\","
"mac_time=\"%llu\",ip_time=\"%llu\",tcp_time=\"%llu\",http_"
"info=\"%s\",rx=\"%"
"d\"} 0\n",
"info=\"%s\",rx=\"%d\"} \n",
pack_info->sock, pack_info->seq, pack_info->ack,
pack_info->mac_time, pack_info->ip_time,
pack_info->tcp_time, http_data, pack_info->rx);
} else {
printf("%-22p %-10u %-10u %-10s %-10s %-10s %-5d %s\n",
pack_info->sock, pack_info->seq, pack_info->ack, "-", "-",
"-", pack_info->rx, http_data);
fprintf(file,
}
if(http_info|| retrans_info||extra_conn_info){
printf("%-22p %-10u %-10u %-5d %s\n",
pack_info->sock, pack_info->seq, pack_info->ack,
pack_info->rx, http_data);
fprintf(file,
"packet{sock=\"%p\",seq=\"%u\",ack=\"%u\","
"mac_time=\"-\",ip_time=\"-\",tcp_time=\"-\",http_"
"info=\"%s\",rx=\"%"
"d\"} 0\n",
"info=\"%s\",rx=\"%d\"} \n",
pack_info->sock, pack_info->seq, pack_info->ack, http_data,
pack_info->rx);
}
}

}
fclose(file);
}
return 0;
Expand Down Expand Up @@ -316,16 +320,27 @@ int main(int argc, char **argv) {
goto cleanup;
}

if(layer_time) {
printf("%-22s %-10s %-10s %-10s %-10s %-10s %-5s %s\n", "SOCK", "SEQ",
"ACK", "MAC_TIME", "IP_TIME", "TCP_TIME", "RX", "HTTP");

}

if(http_info|| retrans_info||extra_conn_info) {

printf("%-22s %-10s %-10s %-5s \n", "SOCK", "SEQ",
"ACK", "RX");
}

/* Set up ring buffer polling */
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), print_packet, NULL, NULL);
if (!rb) {
err = -1;
fprintf(stderr, "Failed to create ring buffer\n");
goto cleanup;
}
}


printf("%-22s %-10s %-10s %-10s %-10s %-10s %-5s %s\n", "SOCK", "SEQ",
"ACK", "MAC_TIME", "IP_TIME", "TCP_TIME", "RX", "HTTP");
FILE *err_file = fopen(err_file_path, "w+");
if (err_file == NULL) {
fprintf(stderr, "Failed to open err.log: (%s)\n", strerror(errno));
Expand All @@ -338,13 +353,18 @@ int main(int argc, char **argv) {
return 0;
}
fclose(packet_file);

/* Process events */
while (!exiting) {

err = ring_buffer__poll(rb, 100 /* timeout, ms */);

print_conns(skel);
sleep(1);
if(http_info) {
printf("==============================\n");
print_conns(skel);
sleep(1);
}

/* Ctrl-C will cause -EINTR */
if (err == -EINTR) {
err = 0;
Expand Down
23 changes: 23 additions & 0 deletions eBPF_Supermarket/kvm_watcher/kvm_exits/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
| sed 's/aarch64/arm64/' \
| sed 's/ppc64le/powerpc/' \
| sed 's/mips.*/mips/' \
| sed 's/riscv64/riscv/' \
| sed 's/loongarch64/loongarch/')
APP = kvm_exits

all: $(APP)

.PHONY: $(APP)
$(APP):
ifeq ($(wildcard ./vmlinux.h),)
bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
endif
clang -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) -I/usr/include/x86_64-linux-gnu -I. -c [email protected] -o [email protected]
bpftool gen skeleton [email protected] > [email protected]
clang -g -O2 -Wall -I . -c [email protected] -o [email protected]
clang -Wall -O2 -g [email protected] -static -lbpf -lelf -lz -o $@

clean:
rm -f *.o *.skel.h $(APP)
128 changes: 128 additions & 0 deletions eBPF_Supermarket/kvm_watcher/kvm_exits/kvm_exits.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2023 The LMP Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// author: [email protected]
//
// Kernel space BPF program used for counting VM exit reason.

#include "vmlinux.h"
#include "kvm_exits.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
char LICENSE[] SEC("license") = "Dual BSD/GPL";

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 8192);
__type(key, pid_t);
__type(value, struct reason_info);
} times SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 8192);
__type(key, u32);
__type(value, u32);
} counts SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_RINGBUF);
__uint(max_entries, 256 * 1024);
} rb SEC(".maps");

struct exit{
u64 pad;
unsigned int exit_reason;
unsigned long guest_rip;
u32 isa;
u64 info1;
u64 info2;
u32 intr_info;
u32 error_code;
unsigned int vcpu_id;
};

int total=0;

SEC("tp/kvm/kvm_exit")
int handle_kvm_exit(struct exit *ctx)
{
pid_t tid;
u64 id,ts;
id = bpf_get_current_pid_tgid();
tid = (u32)id;
ts = bpf_ktime_get_ns();
u32 reason;
reason=(u32)ctx->exit_reason;
struct reason_info reas={};
reas.reason=reason;
reas.time=ts;
u32 *count;
count=bpf_map_lookup_elem(&counts, &reason);
if(count){
(*count)++;
reas.count=*count;
}else{
u32 new_count = 1;
reas.count=new_count;
bpf_map_update_elem(&counts, &reason, &new_count, BPF_ANY);
}
bpf_map_update_elem(&times, &tid, &reas, BPF_ANY);
return 0;
}

SEC("tp/kvm/kvm_entry")
int handle_kvm_entry()
{
struct reason_info *reas;
pid_t pid, tid;
u64 id, ts, *start_ts, duration_ns=0;
id = bpf_get_current_pid_tgid();
pid = id >> 32;
tid = (u32)id;
reas = bpf_map_lookup_elem(&times, &tid);
if(reas){
u32 reason;
struct event *e;
int count=0;
duration_ns=bpf_ktime_get_ns() - reas->time;
bpf_map_delete_elem(&times, &tid);
reason=reas->reason;
count=reas->count;
e = bpf_ringbuf_reserve(&rb, sizeof(*e), 0);
if (!e){
return 0;
}
e->reason_number=reason;
e->pid=pid;
e->duration_ns = duration_ns;
bpf_get_current_comm(&e->comm, sizeof(e->comm));
e->tid=tid;
e->total=++total;
if (count) {
e->count = count;
} else {
e->count = 1;
}
bpf_ringbuf_submit(e, 0);
return 0;
}
else{
return 0;
}
}



Loading

0 comments on commit a1d4870

Please sign in to comment.