-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NETOBSERV-1637: OVS monitoring ebpf hook #286
Merged
openshift-merge-bot
merged 1 commit into
netobserv:main
from
msherif1234:ovs-monitoring
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
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
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,202 @@ | ||
/* | ||
* Network events monitoring kprobe eBPF hook. | ||
*/ | ||
|
||
#ifndef __NETWORK_EVENTS_MONITORING_H__ | ||
#define __NETWORK_EVENTS_MONITORING_H__ | ||
|
||
#include "utils.h" | ||
|
||
struct rh_psample_metadata { | ||
u32 trunc_size; | ||
int in_ifindex; | ||
int out_ifindex; | ||
u16 out_tc; | ||
u64 out_tc_occ; /* bytes */ | ||
u64 latency; /* nanoseconds */ | ||
u8 out_tc_valid : 1, out_tc_occ_valid : 1, latency_valid : 1, rate_as_probability : 1, | ||
unused : 4; | ||
const u8 *user_cookie; | ||
u32 user_cookie_len; | ||
}; | ||
|
||
static inline bool md_already_exists(u8 network_events[MAX_NETWORK_EVENTS][MAX_EVENT_MD], u8 *md) { | ||
for (u8 i = 0; i < MAX_NETWORK_EVENTS; i++) { | ||
if (__builtin_memcmp(network_events[i], md, MAX_EVENT_MD) == 0) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static inline int trace_network_events(struct sk_buff *skb, struct rh_psample_metadata *md) { | ||
u8 dscp = 0, protocol = 0, md_len = 0; | ||
u16 family = 0, flags = 0; | ||
u8 *user_cookie = NULL; | ||
u8 cookie[MAX_EVENT_MD]; | ||
long ret = 0; | ||
u64 len = 0; | ||
flow_id id; | ||
|
||
__builtin_memset(&id, 0, sizeof(id)); | ||
__builtin_memset(cookie, 0, sizeof(cookie)); | ||
|
||
md_len = BPF_CORE_READ(md, user_cookie_len); | ||
user_cookie = (u8 *)BPF_CORE_READ(md, user_cookie); | ||
if (md_len == 0 || md_len > MAX_EVENT_MD || user_cookie == NULL) { | ||
return -1; | ||
} | ||
|
||
bpf_probe_read(cookie, md_len, user_cookie); | ||
|
||
id.if_index = BPF_CORE_READ(md, in_ifindex); | ||
|
||
len = BPF_CORE_READ(skb, len); | ||
|
||
// read L2 info | ||
core_fill_in_l2(skb, &id, &family); | ||
|
||
// read L3 info | ||
core_fill_in_l3(skb, &id, family, &protocol, &dscp); | ||
|
||
// read L4 info | ||
switch (protocol) { | ||
case IPPROTO_TCP: | ||
core_fill_in_tcp(skb, &id, &flags); | ||
break; | ||
case IPPROTO_UDP: | ||
core_fill_in_udp(skb, &id); | ||
break; | ||
case IPPROTO_SCTP: | ||
core_fill_in_sctp(skb, &id); | ||
break; | ||
case IPPROTO_ICMP: | ||
core_fill_in_icmpv4(skb, &id); | ||
break; | ||
case IPPROTO_ICMPV6: | ||
core_fill_in_icmpv6(skb, &id); | ||
break; | ||
default: | ||
fill_in_others_protocol(&id, protocol); | ||
} | ||
|
||
for (direction dir = INGRESS; dir < MAX_DIRECTION; dir++) { | ||
id.direction = dir; | ||
flow_metrics *aggregate_flow = bpf_map_lookup_elem(&aggregated_flows, &id); | ||
if (aggregate_flow != NULL) { | ||
u8 idx = aggregate_flow->network_events_idx; | ||
aggregate_flow->end_mono_time_ts = bpf_ktime_get_ns(); | ||
// Needed to check length here again to keep JIT verifier happy | ||
if (idx < MAX_NETWORK_EVENTS && md_len <= MAX_EVENT_MD) { | ||
if (!md_already_exists(aggregate_flow->network_events, (u8 *)cookie)) { | ||
__builtin_memcpy(aggregate_flow->network_events[idx], cookie, MAX_EVENT_MD); | ||
aggregate_flow->network_events_idx = (idx + 1) % MAX_NETWORK_EVENTS; | ||
} | ||
ret = bpf_map_update_elem(&aggregated_flows, &id, aggregate_flow, BPF_ANY); | ||
if (ret == 0) { | ||
return 0; | ||
} | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} | ||
|
||
if (ret != 0) { | ||
if (trace_messages) { | ||
bpf_printk("error network events updating existing flow %d\n", ret); | ||
} | ||
return ret; | ||
} | ||
|
||
// there is no matching flows so lets create new one and add the network event metadata | ||
u64 current_time = bpf_ktime_get_ns(); | ||
id.direction = INGRESS; | ||
flow_metrics new_flow = { | ||
.packets = 1, | ||
.bytes = len, | ||
.start_mono_time_ts = current_time, | ||
.end_mono_time_ts = current_time, | ||
.flags = flags, | ||
.network_events_idx = 0, | ||
}; | ||
bpf_probe_read(new_flow.network_events[0], md_len, user_cookie); | ||
new_flow.network_events_idx++; | ||
ret = bpf_map_update_elem(&aggregated_flows, &id, &new_flow, BPF_ANY); | ||
if (trace_messages && ret != 0) { | ||
bpf_printk("error network events creating new flow %d\n", ret); | ||
} | ||
return ret; | ||
} | ||
|
||
static inline void update_network_events_statistics(u32 *key, u32 *value) { | ||
u32 *error_counter_p = NULL; | ||
error_counter_p = bpf_map_lookup_elem(&global_counters, key); | ||
if (!error_counter_p) { | ||
bpf_map_update_elem(&global_counters, key, value, BPF_ANY); | ||
return; | ||
} | ||
__sync_fetch_and_add(error_counter_p, 1); | ||
} | ||
|
||
// for older kernel will use `rh_psample_sample_packet` to avoid kapis issues | ||
SEC("kprobe/rh_psample_sample_packet") | ||
int BPF_KPROBE(rh_network_events_monitoring, struct psample_group *group, struct sk_buff *skb, | ||
u32 sample_rate, struct rh_psample_metadata *md) { | ||
u32 initVal = 1, key = NETWORK_EVENTS_ERR_KEY; | ||
if (enable_network_events_monitoring == 0 || do_sampling == 0) { | ||
return 0; | ||
} | ||
if (skb == NULL || md == NULL || group == NULL) { | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
// filter out none matching samples with different groupid | ||
int group_id = BPF_CORE_READ(group, group_num); | ||
if (group_id != network_events_monitoring_groupid) { | ||
key = NETWORK_EVENTS_ERR_GROUPID_MISMATCH; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
long ret = 0; | ||
if ((ret = trace_network_events(skb, md)) != 0) { | ||
key = NETWORK_EVENTS_ERR_UPDATE_MAP_FLOWS; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
key = NETWORK_EVENTS_GOOD; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
/* | ||
SEC("kprobe/psample_sample_packet") | ||
int BPF_KPROBE(network_events_monitoring, struct psample_group *group, struct sk_buff *skb, u32 sample_rate, | ||
struct psample_metadata *md) { | ||
u32 initVal = 1, key = NETWORK_EVENTS_ERR_KEY; | ||
if (enable_network_events_monitoring == 0 || do_sampling == 0) { | ||
return 0; | ||
} | ||
if (skb == NULL || md == NULL || group == NULL) { | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
// filter out none matching samples with different groupid | ||
int group_id = BPF_CORE_READ(group, group_num); | ||
if (group_id != network_events_monitoring_groupid) { | ||
key = NETWORK_EVENTS_ERR_GROUPID_MISMATCH; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
|
||
long ret = 0; | ||
if ((ret = trace_network_events(skb, md)) != 0) { | ||
key = NETWORK_EVENTS_ERR_UPDATE_MAP_FLOWS; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
key = NETWORK_EVENTS_GOOD; | ||
update_network_events_statistics(&key, &initVal); | ||
return 0; | ||
} | ||
*/ | ||
#endif /* __NETWORK_EVENTS_MONITORING_H__ */ |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it planned to uncomment later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its planned for the new kernel whick is
REHL 9.6
with the planned kernel for 9.4 we haverh_
version of the hook because they can't break kernel apis but in 9.6rh_
will be gone that why I ahve this with kernel version check for the future