Skip to content

Commit

Permalink
remove packed, handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Dec 17, 2024
1 parent 1f689c5 commit fd9e82a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 14 deletions.
14 changes: 12 additions & 2 deletions bpf/flows.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static inline int flow_monitor(struct __sk_buff *skb, u8 direction) {
if (aggregate_flow != NULL) {
if (aggregate_flow->if_index_first_seen == skb->ifindex) {
update_existing_flow(aggregate_flow, &pkt, len, filter_sampling);
} else {
} else if (skb->ifindex != 0) {
// Only add info that we've seen this interface
additional_metrics *extra_metrics =
(additional_metrics *)bpf_map_lookup_elem(&additional_flow_metrics, &id);
Expand All @@ -147,7 +147,17 @@ static inline int flow_monitor(struct __sk_buff *skb, u8 direction) {
} else {
additional_metrics new_metrics = {};
add_observed_intf(&new_metrics, skb->ifindex, direction);
bpf_map_update_elem(&additional_flow_metrics, &id, &new_metrics, BPF_NOEXIST);
long ret =
bpf_map_update_elem(&additional_flow_metrics, &id, &new_metrics, BPF_NOEXIST);
if (ret == -EEXIST) {
extra_metrics =
(additional_metrics *)bpf_map_lookup_elem(&additional_flow_metrics, &id);
if (extra_metrics != NULL) {
add_observed_intf(extra_metrics, skb->ifindex, direction);
}
} else if (ret != 0 && trace_messages) {
bpf_printk("error creating new observed_intf: %d\n", ret);
}
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion bpf/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ typedef struct additional_metrics_t {
struct observed_intf_t {
u8 direction;
u32 if_index;
} __attribute__((packed)) observed_intf[MAX_OBSERVED_INTERFACES];
} observed_intf[MAX_OBSERVED_INTERFACES];
u8 network_events_idx;
u8 nb_observed_intf;
} additional_metrics;
Expand Down
3 changes: 2 additions & 1 deletion pkg/ebpf/bpf_arm64_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpf/bpf_arm64_bpfel.o
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/ebpf/bpf_powerpc_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpf/bpf_powerpc_bpfel.o
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/ebpf/bpf_s390_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpf/bpf_s390_bpfeb.o
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/ebpf/bpf_x86_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/ebpf/bpf_x86_bpfel.o
Binary file not shown.
14 changes: 7 additions & 7 deletions pkg/model/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func TestAdditionalMetricsBinaryEncoding(t *testing.T) {
0x00,
0x00, // 1 byte padding
// observed_intf_t[4]
0x01, 0x07, 0x00, 0x00, 0x00, // [0]: u8 direction + u32 if_index
0x00, 0x08, 0x00, 0x00, 0x00, // [1]: u8 direction + u32 if_index
0x00, 0x00, 0x00, 0x00, 0x00, // [2]: u8 direction + u32 if_index
0x00, 0x00, 0x00, 0x00, 0x00, // [3]: u8 direction + u32 if_index
0x01, // u8 network_events_idx
0x02, // u8 nb_observed_intf
0x00, 0x00, // 2 bytes padding
0x01, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, // [0]: u8 direction + 3 bytes padding + u32 if_index
0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, // [1]: u8 direction + 3 bytes padding + u32 if_index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // [2]: u8 direction + 3 bytes padding + u32 if_index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // [3]: u8 direction + 3 bytes padding + u32 if_index
0x01, // u8 network_events_idx
0x02, // u8 nb_observed_intf
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 6 bytes padding
}
var addmet ebpf.BpfAdditionalMetrics
err := binary.Read(bytes.NewReader(b), binary.LittleEndian, &addmet)
Expand Down

0 comments on commit fd9e82a

Please sign in to comment.