forked from libbpf/libbpf-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
D. Wythe
committed
Dec 7, 2023
1 parent
d78c929
commit 9211085
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause | ||
/* Copyright (c) 2021 Sartura */ | ||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <bpf/bpf_core_read.h> | ||
#include <bpf/bpf_endian.h> | ||
|
||
char LICENSE[] SEC("license") = "Dual BSD/GPL"; | ||
|
||
#define AF_INET 2 /* Internet IP Protocol */ | ||
#define AF_INET6 10 | ||
#define IPPROTO_SMC 263 | ||
|
||
SEC("fmod_ret/update_socket_protocol") | ||
int BPF_PROG(smc_run, int family, int type, int protocol) | ||
{ | ||
|
||
if ((family == AF_INET || family == AF_INET6) && | ||
type == SOCK_STREAM && | ||
(!protocol || protocol == IPPROTO_TCP)) { | ||
return IPPROTO_SMC; | ||
} | ||
|
||
return protocol; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) | ||
/* Copyright (c) 2021 Sartura | ||
* Based on minimal.c by Facebook */ | ||
|
||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <string.h> | ||
#include <errno.h> | ||
#include <sys/resource.h> | ||
#include <bpf/libbpf.h> | ||
#include "smc.skel.h" | ||
|
||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | ||
{ | ||
return vfprintf(stderr, format, args); | ||
} | ||
|
||
static volatile sig_atomic_t stop; | ||
|
||
static void sig_int(int signo) | ||
{ | ||
stop = 1; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct smc_bpf *skel; | ||
int err; | ||
|
||
/* Set up libbpf errors and debug info callback */ | ||
libbpf_set_print(libbpf_print_fn); | ||
|
||
/* Open load and verify BPF application */ | ||
skel = smc_bpf__open_and_load(); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to open BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
/* Attach tracepoint handler */ | ||
err = smc_bpf__attach(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to attach BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
if (signal(SIGINT, sig_int) == SIG_ERR) { | ||
fprintf(stderr, "can't set signal handler: %s\n", strerror(errno)); | ||
goto cleanup; | ||
} | ||
|
||
printf("Successfully started! Please run `sudo cat /sys/kernel/debug/tracing/trace_pipe` " | ||
"to see output of the BPF programs.\n"); | ||
|
||
while (!stop) { | ||
fprintf(stderr, "."); | ||
sleep(1); | ||
} | ||
|
||
cleanup: | ||
smc_bpf__destroy(skel); | ||
return -err; | ||
} |