forked from kmesh-net/kmesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'kmesh-net:main' into main
- Loading branch information
Showing
149 changed files
with
2,527 additions
and
333 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
coverage: | ||
status: | ||
project: #add everything under here, more options at https://docs.codecov.com/docs/commit-status | ||
default: | ||
# basic | ||
target: auto #default | ||
threshold: 50% | ||
base: auto | ||
comment: # this is a top-level key | ||
layout: "header, files, footer" # remove "new" from "header" and "footer" | ||
hide_project_coverage: false # set to false |
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
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 @@ | ||
For further details please see [Security Policy](https://github.com/kmesh-net/community/blob/main/security-team/SECURITY.md) for our security process and how to report vulnerabilities. |
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
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
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,72 @@ | ||
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
/* Copyright Authors of Kmesh */ | ||
|
||
#ifndef __KMESH_BPF_ACCESS_LOG_H__ | ||
#define __KMESH_BPF_ACCESS_LOG_H__ | ||
|
||
#include "bpf_common.h" | ||
|
||
// access log | ||
enum { | ||
INVALID_DIRECTION = 0, | ||
INBOUND = 1, | ||
OUTBOUND = 2, | ||
}; | ||
|
||
struct access_log { | ||
struct bpf_sock_tuple tuple; | ||
__u64 duration; // ns | ||
__u64 close_ns; | ||
__u32 family; | ||
__u32 protocol; | ||
__u8 direction; | ||
__u32 sent_bytes; | ||
__u32 received_bytes; | ||
__u32 conn_success; | ||
}; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
__uint(max_entries, RINGBUF_SIZE); | ||
} map_of_access_log SEC(".maps"); | ||
|
||
static inline void constuct_tuple(struct bpf_sock *sk, struct bpf_sock_tuple *tuple) | ||
{ | ||
if (sk->family == AF_INET) { | ||
tuple->ipv4.saddr = sk->src_ip4; | ||
tuple->ipv4.daddr = sk->dst_ip4; | ||
tuple->ipv4.sport = sk->src_port; | ||
tuple->ipv4.dport = sk->dst_port; | ||
} else { | ||
bpf_memcpy(tuple->ipv6.saddr, sk->src_ip6, IPV6_ADDR_LEN); | ||
bpf_memcpy(tuple->ipv6.daddr, sk->dst_ip6, IPV6_ADDR_LEN); | ||
tuple->ipv6.sport = sk->src_port; | ||
tuple->ipv6.dport = sk->dst_port; | ||
} | ||
return; | ||
} | ||
|
||
static inline void | ||
report_access_log(struct bpf_sock *sk, struct bpf_tcp_sock *tcp_sock, struct sock_storage_data *storage) | ||
{ | ||
struct access_log *log = NULL; | ||
|
||
// store tuple | ||
log = bpf_ringbuf_reserve(&map_of_access_log, sizeof(struct access_log), 0); | ||
if (!log) { | ||
BPF_LOG(ERR, PROBE, "bpf_ringbuf_reserve access_log failed\n"); | ||
return; | ||
} | ||
|
||
constuct_tuple(sk, &log->tuple); | ||
log->direction = storage->direction; | ||
log->close_ns = bpf_ktime_get_ns(); | ||
log->duration = log->close_ns - storage->connect_ns; | ||
log->sent_bytes = tcp_sock->delivered; | ||
log->received_bytes = tcp_sock->bytes_received; | ||
log->conn_success = storage->connect_success; | ||
|
||
bpf_ringbuf_submit(log, 0); | ||
} | ||
|
||
#endif |
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,119 @@ | ||
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
/* Copyright Authors of Kmesh */ | ||
|
||
#ifndef __KMESH_BPF_METRICS_H__ | ||
#define __KMESH_BPF_METRICS_H__ | ||
#include "bpf_common.h" | ||
|
||
// metrics | ||
struct metric_key { | ||
struct ip_addr src_ip; | ||
struct ip_addr dst_ip; | ||
}; | ||
|
||
struct metric_data { | ||
__u32 direction; // update on connect | ||
__u32 conn_open; // update on connect | ||
__u32 conn_close; // update on close | ||
__u32 conn_failed; // update on close | ||
__u32 sent_bytes; // update on close | ||
__u32 received_bytes; // update on close | ||
}; | ||
|
||
#define MAP_SIZE_OF_METRICS 100000 | ||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__type(key, struct metric_key); | ||
__type(value, struct metric_data); | ||
__uint(max_entries, MAP_SIZE_OF_METRICS); | ||
__uint(map_flags, BPF_F_NO_PREALLOC); | ||
} map_of_metrics SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_RINGBUF); | ||
__uint(max_entries, RINGBUF_SIZE); | ||
} map_of_metric_notify SEC(".maps"); | ||
|
||
static inline void construct_metric_key(struct bpf_sock *sk, struct metric_key *key) | ||
{ | ||
if (sk->family == AF_INET) { | ||
key->src_ip.ip4 = sk->src_ip4; | ||
key->dst_ip.ip4 = sk->dst_ip4; | ||
} else { | ||
bpf_memcpy(key->src_ip.ip6, sk->src_ip6, IPV6_ADDR_LEN); | ||
bpf_memcpy(key->dst_ip.ip6, sk->dst_ip6, IPV6_ADDR_LEN); | ||
} | ||
return; | ||
} | ||
|
||
static inline void report_metrics(struct bpf_sock *sk) | ||
{ | ||
struct metric_key *key = bpf_ringbuf_reserve(&map_of_metric_notify, sizeof(struct metric_key), 0); | ||
if (!key) { | ||
BPF_LOG(ERR, PROBE, "report_metrics bpf_ringbuf_reserve failed\n"); | ||
return; | ||
} | ||
|
||
construct_metric_key(sk, key); | ||
bpf_ringbuf_submit(key, 0); | ||
return; | ||
} | ||
|
||
static inline void | ||
metric_on_connect(struct bpf_sock *sk, struct bpf_tcp_sock *tcp_sock, struct sock_storage_data *storage) | ||
{ | ||
struct metric_key key = {0}; | ||
struct metric_data data = {0}; | ||
struct metric_data *metric = NULL; | ||
|
||
construct_metric_key(sk, &key); | ||
metric = (struct metric_data *)bpf_map_lookup_elem(&map_of_metrics, &key); | ||
if (!metric) { | ||
data.conn_open++; | ||
data.direction = storage->direction; | ||
int err = bpf_map_update_elem(&map_of_metrics, &key, &data, BPF_NOEXIST); | ||
if (err) { | ||
BPF_LOG(ERR, PROBE, "metric_on_connect update failed, err is %d\n", err); | ||
return; | ||
} | ||
goto notify; | ||
} | ||
|
||
metric->conn_open++; | ||
metric->direction = storage->direction; | ||
notify: | ||
report_metrics(sk); | ||
return; | ||
} | ||
|
||
static inline void | ||
metric_on_close(struct bpf_sock *sk, struct bpf_tcp_sock *tcp_sock, struct sock_storage_data *storage) | ||
{ | ||
struct metric_key key = {0}; | ||
struct metric_data data = {0}; | ||
struct metric_data *metric = NULL; | ||
|
||
construct_metric_key(sk, &key); | ||
metric = (struct metric_data *)bpf_map_lookup_elem(&map_of_metrics, &key); | ||
if (!metric) { | ||
// connect failed | ||
data.direction = storage->direction; | ||
data.conn_failed++; | ||
int err = bpf_map_update_elem(&map_of_metrics, &key, &data, BPF_NOEXIST); | ||
if (err) { | ||
BPF_LOG(ERR, PROBE, "metric_on_close update failed, err is %d\n", err); | ||
return; | ||
} | ||
goto notify; | ||
} | ||
|
||
// connect successed & closed | ||
metric->conn_close++; | ||
metric->sent_bytes += tcp_sock->delivered; | ||
metric->received_bytes += tcp_sock->bytes_received; | ||
notify: | ||
report_metrics(sk); | ||
return; | ||
} | ||
|
||
#endif |
Oops, something went wrong.