11#include <vmlinux.h>
22#include <bpf/bpf_tracing.h>
3- #include <bpf/bpf_core_read.h>
43#include "maps.bpf.h"
54
65#define MAX_ENTRIES 8192
1716struct ipv4_key_t {
1817 u32 saddr ;
1918 u32 daddr ;
20- u16 mainport ;
21- u32 type ;
19+ u16 main_port ;
20+ u8 type ;
2221};
2322
2423struct ipv6_key_t {
2524 u8 saddr [16 ];
2625 u8 daddr [16 ];
27- u16 mainport ;
28- u32 type ;
26+ u16 main_port ;
27+ u8 type ;
2928};
3029
3130struct {
@@ -42,60 +41,59 @@ struct {
4241 __type (value , u64 );
4342} tcp_retransmit_ipv6_packets_total SEC (".maps" );
4443
45- static int trace_event (const struct sock * sk , u32 type )
44+ static int extract_main_port (const struct sock * sk )
4645{
47- u32 family = sk -> __sk_common .skc_family ;
46+ u16 sport = sk -> __sk_common .skc_num ;
47+ u16 dport = __builtin_bswap16 (sk -> __sk_common .skc_dport );
4848
49- if (family == AF_INET ) {
50- struct ipv4_key_t key ;
51- __builtin_memset (& key , 0 , sizeof (key ));
52-
53- key .saddr = sk -> __sk_common .skc_rcv_saddr ;
54- key .daddr = sk -> __sk_common .skc_daddr ;
55-
56- u16 sport = sk -> __sk_common .skc_num ;
57- u16 dport = sk -> __sk_common .skc_dport ;
58- dport = __builtin_bswap16 (dport );
59-
60- if (sport <= dport ) {
61- key .mainport = sport ;
62- } else {
63- key .mainport = dport ;
64- }
49+ if (sport > UPPER_PORT_BOUND && dport > UPPER_PORT_BOUND ) {
50+ return 0 ;
51+ }
6552
66- if (key . mainport >= UPPER_PORT_BOUND ) {
67- key . mainport = 0 ;
68- }
53+ if (sport < dport ) {
54+ return sport ;
55+ }
6956
70- key .type = type ;
57+ return dport ;
58+ }
7159
72- increment_map (& tcp_retransmit_ipv4_packets_total , & key , 1 );
60+ #define TRACE_PROTOCOL (key_type , map , ip_extractor ) \
61+ key_type key = {}; \
62+ \
63+ key.type = type; \
64+ key.main_port = extract_main_port(sk); \
65+ \
66+ ip_extractor; \
67+ \
68+ increment_map(map, &key, 1); \
69+ \
70+ return 0;
7371
74- } else if (family == AF_INET6 ) {
75- struct ipv6_key_t key ;
76- __builtin_memset (& key , 0 , sizeof (key ));
72+ static int trace_ipv4 (const struct sock * sk , u8 type )
73+ {
74+ TRACE_PROTOCOL (struct ipv4_key_t , & tcp_retransmit_ipv4_packets_total , {
75+ key .saddr = sk -> __sk_common .skc_rcv_saddr ;
76+ key .daddr = sk -> __sk_common .skc_daddr ;
77+ });
78+ }
7779
80+ static int trace_ipv6 (const struct sock * sk , u8 type )
81+ {
82+ TRACE_PROTOCOL (struct ipv6_key_t , & tcp_retransmit_ipv6_packets_total , {
7883 bpf_probe_read_kernel (& key .saddr , sizeof (key .saddr ), sk -> __sk_common .skc_v6_rcv_saddr .in6_u .u6_addr32 );
7984 bpf_probe_read_kernel (& key .daddr , sizeof (key .daddr ), sk -> __sk_common .skc_v6_daddr .in6_u .u6_addr32 );
85+ });
86+ }
8087
81- u16 sport = sk -> __sk_common .skc_num ;
82- u16 dport = sk -> __sk_common .skc_dport ;
83- dport = __builtin_bswap16 (dport );
84-
85- if (sport <= dport ) {
86- key .mainport = sport ;
87- } else {
88- key .mainport = dport ;
89- }
90-
91- if (key .mainport >= UPPER_PORT_BOUND ) {
92- key .mainport = 0 ;
93- }
94-
95- key .type = type ;
96-
97- increment_map (& tcp_retransmit_ipv6_packets_total , & key , 1 );
88+ static int trace_event (const struct sock * sk , u8 type )
89+ {
90+ switch (sk -> __sk_common .skc_family ) {
91+ case AF_INET :
92+ return trace_ipv4 (sk , type );
93+ case AF_INET6 :
94+ return trace_ipv6 (sk , type );
9895 }
96+
9997 return 0 ;
10098}
10199
0 commit comments