-
Notifications
You must be signed in to change notification settings - Fork 31
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-1061: Add TCP drop and DNS tracking hooks #115
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
67fd5c6
Add TCP drop hook and update flows metrics
msherif1234 47f2c0f
update agent e2e manifest to mount kernel debug volume
msherif1234 5e5aae3
Add light weight DNS tracker hook
msherif1234 2cffd02
rename tcpdrop fields to reflect its the latest drop
msherif1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
#ifndef __CONFIGS_H__ | ||
#define __CONFIGS_H__ | ||
|
||
// Constant definitions, to be overridden by the invoker | ||
volatile const u32 sampling = 0; | ||
volatile const u8 trace_messages = 0; | ||
|
||
#endif //__CONFIGS_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
light weight DNS tracker using trace points. | ||
*/ | ||
|
||
#ifndef __DNS_TRACKER_H__ | ||
#define __DNS_TRACKER_H__ | ||
#include "utils.h" | ||
|
||
#define DNS_PORT 53 | ||
#define DNS_QR_FLAG 0x8000 | ||
#define UDP_MAXMSG 512 | ||
|
||
struct dns_header { | ||
u16 id; | ||
u16 flags; | ||
u16 qdcount; | ||
u16 ancount; | ||
u16 nscount; | ||
u16 arcount; | ||
}; | ||
|
||
static inline void find_or_create_dns_flow(flow_id *id, struct dns_header *dns, int len, int dir, u16 flags) { | ||
flow_metrics *aggregate_flow = bpf_map_lookup_elem(&aggregated_flows, id); | ||
u64 current_time = bpf_ktime_get_ns(); | ||
// net_dev_queue trace point hook will run before TC hooks, so the flow shouldn't exists, if it does | ||
// that indicates we have a stale DNS query/response or in the middle of TCP flow so we will do nothing | ||
if (aggregate_flow == NULL) { | ||
// there is no matching flows so lets create new one and add the drops | ||
flow_metrics new_flow; | ||
__builtin_memset(&new_flow, 0, sizeof(new_flow)); | ||
new_flow.start_mono_time_ts = current_time; | ||
new_flow.end_mono_time_ts = current_time; | ||
new_flow.packets = 1; | ||
new_flow.bytes = len; | ||
new_flow.flags = flags; | ||
new_flow.dns_record.id = bpf_ntohs(dns->id); | ||
new_flow.dns_record.flags = bpf_ntohs(dns->flags); | ||
if (dir == EGRESS) { | ||
new_flow.dns_record.req_mono_time_ts = current_time; | ||
} else { | ||
new_flow.dns_record.rsp_mono_time_ts = current_time; | ||
} | ||
bpf_map_update_elem(&aggregated_flows, id, &new_flow, BPF_ANY); | ||
} | ||
} | ||
|
||
static inline int trace_dns(struct sk_buff *skb) { | ||
flow_id id; | ||
u8 protocol = 0; | ||
u16 family = 0,flags = 0, len = 0; | ||
|
||
__builtin_memset(&id, 0, sizeof(id)); | ||
|
||
id.if_index = skb->skb_iif; | ||
|
||
// read L2 info | ||
set_key_with_l2_info(skb, &id, &family); | ||
|
||
// read L3 info | ||
set_key_with_l3_info(skb, family, &id, &protocol); | ||
|
||
switch (protocol) { | ||
case IPPROTO_UDP: | ||
len = set_key_with_udp_info(skb, &id, IPPROTO_UDP); | ||
// make sure udp payload doesn't exceed max msg size | ||
if (len - sizeof(struct udphdr) > UDP_MAXMSG) { | ||
return -1; | ||
} | ||
// set the length to udp hdr size as it will be used below to locate dns header | ||
len = sizeof(struct udphdr); | ||
break; | ||
case IPPROTO_TCP: | ||
len = set_key_with_tcp_info(skb, &id, IPPROTO_TCP, &flags); | ||
break; | ||
default: | ||
return -1; | ||
} | ||
|
||
// check for DNS packets | ||
if (id.dst_port == DNS_PORT || id.src_port == DNS_PORT) { | ||
struct dns_header dns; | ||
bpf_probe_read(&dns, sizeof(dns), (struct dns_header *)(skb->head + skb->transport_header + len)); | ||
msherif1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ((bpf_ntohs(dns.flags) & DNS_QR_FLAG) == 0) { /* dns query */ | ||
id.direction = EGRESS; | ||
msherif1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { /* dns response */ | ||
id.direction = INGRESS; | ||
} // end of dns response | ||
find_or_create_dns_flow(&id, &dns, skb->len, id.direction, flags); | ||
} // end of dns port check | ||
|
||
return 0; | ||
} | ||
|
||
SEC("tracepoint/net/net_dev_queue") | ||
msherif1234 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int trace_net_packets(struct trace_event_raw_net_dev_template *args) { | ||
struct sk_buff skb; | ||
|
||
__builtin_memset(&skb, 0, sizeof(skb)); | ||
bpf_probe_read(&skb, sizeof(struct sk_buff), args->skbaddr); | ||
return trace_dns(&skb); | ||
} | ||
|
||
#endif // __DNS_TRACKER_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
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.
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.
I'm not sure if this will be set for a locally-generated packet (or to what value in this case)?
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.
we don't deal with locally generated packets we handle pkts that enter and exists the container interfaces
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.
How are you ensuring that?
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.
the whole idea of netobserv agent its interface based feature , no interface set won't match any records in our maps and will be ignored