Skip to content

Commit

Permalink
Make RTT values show up on both flow directions
Browse files Browse the repository at this point in the history
  • Loading branch information
dushyantbehl committed Jul 28, 2023
1 parent 338d2b2 commit 35fc676
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
12 changes: 7 additions & 5 deletions bpf/flows.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ static inline int flow_monitor(struct __sk_buff *skb, u8 direction) {
return TC_ACT_OK;
}

if (enable_rtt) {
// This is currently gated as its not to be enabled by default.
calculate_flow_rtt(&pkt, direction, data_end);
}

//Set extra fields
id.if_index = skb->ifindex;
id.direction = direction;

// We calculate the RTT before looking up aggregated_flows map because we want
// to keep the critical section between map lookup and update consume minimum time.
if (enable_rtt) {
// This is currently not to be enabled by default.
calculate_flow_rtt(&pkt, direction, data_end);
}

// TODO: we need to add spinlock here when we deprecate versions prior to 5.1, or provide
// a spinlocked alternative version and use it selectively https://lwn.net/Articles/779120/
flow_metrics *aggregate_flow = (flow_metrics *)bpf_map_lookup_elem(&aggregated_flows, &id);
Expand Down
38 changes: 38 additions & 0 deletions bpf/rtt_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,41 @@ static __always_inline void fill_flow_seq_id(flow_seq_id *seq_id, pkt_info *pkt,
seq_id->seq_id = seq;
}

static __always_inline void reverse_flow_id(flow_id *o, flow_id *r) {
/* eth_protocol, transport_protocol and if_index remains same */
r->eth_protocol = o->eth_protocol;
r->transport_protocol = o->transport_protocol;
r->if_index = o->if_index;
/* reverse the direction */
r->direction = (o->direction == INGRESS) ? EGRESS : INGRESS;
/* src mac and dst mac gets reversed */
__builtin_memcpy(r->src_mac, o->dst_mac, ETH_ALEN);
__builtin_memcpy(r->dst_mac, o->src_mac, ETH_ALEN);
/* src ip and dst ip gets reversed */
__builtin_memcpy(r->src_ip, o->dst_ip, IP_MAX_LEN);
__builtin_memcpy(r->dst_ip, o->src_ip, IP_MAX_LEN);
/* src port and dst port gets reversed */
r->src_port = o->dst_port;
r->dst_port = o->src_port;
/* ICMP type can be ignore for now. We only deal with TCP packets for now.*/
}

static __always_inline void update_reverse_flow_rtt(pkt_info *pkt) {
flow_id rev_flow_id;
__builtin_memset(&rev_flow_id, 0, sizeof(rev_flow_id));

reverse_flow_id(pkt->id, &rev_flow_id);

flow_metrics *reverse_flow = (flow_metrics *)bpf_map_lookup_elem(&aggregated_flows, &rev_flow_id);
if (reverse_flow != NULL) {
reverse_flow->flow_rtt = pkt->rtt;
long ret = bpf_map_update_elem(&aggregated_flows, &rev_flow_id, reverse_flow, BPF_ANY);
if (trace_messages && ret != 0) {
bpf_printk("error updating rtt value in flow %d\n", ret);
}
}
}

static __always_inline void calculate_flow_rtt_tcp(pkt_info *pkt, u8 direction, void *data_end, flow_seq_id *seq_id) {
struct tcphdr *tcp = (struct tcphdr *) pkt->l4_hdr;
if ( !tcp || ((void *)tcp + sizeof(*tcp) > data_end) ) {
Expand Down Expand Up @@ -62,6 +97,9 @@ static __always_inline void calculate_flow_rtt_tcp(pkt_info *pkt, u8 direction,
if (trace_messages && ret != 0) {
bpf_printk("error evicting flow sequence: %d", ret);
}
// This is an ACK packet with valid sequence id so a SYN must
// have been sent. We can safely update the reverse flow RTT here.
update_reverse_flow_rtt(pkt);
}
}
break;
Expand Down
Binary file modified pkg/ebpf/bpf_bpfeb.o
Binary file not shown.
Binary file modified pkg/ebpf/bpf_bpfel.o
Binary file not shown.

0 comments on commit 35fc676

Please sign in to comment.