@@ -29,6 +29,7 @@ union netdata_ip {
29
29
__u8 addr8 [16 ];
30
30
__u16 addr16 [8 ];
31
31
__u32 addr32 [4 ];
32
+ __u64 addr64 [2 ];
32
33
};
33
34
34
35
/**
@@ -42,7 +43,7 @@ typedef struct netdata_socket {
42
43
__u64 first ; //First timestamp
43
44
__u64 ct ; //Current timestamp
44
45
__u16 retransmit ; //It is never used with UDP
45
- __u8 protocol ; //Should this to be in the index?
46
+ __u8 protocol ;
46
47
__u8 removeme ;
47
48
__u32 reserved ;
48
49
} netdata_socket_t ;
@@ -52,9 +53,9 @@ typedef struct netdata_socket {
52
53
*/
53
54
typedef struct netdata_socket_idx {
54
55
union netdata_ip saddr ;
56
+ __u16 sport ;
55
57
union netdata_ip daddr ;
56
58
__u16 dport ;
57
- __u16 sport ;
58
59
} netdata_socket_idx_t ;
59
60
60
61
/**
@@ -144,6 +145,13 @@ struct bpf_map_def SEC("maps") tbl_sock_total_stats = {
144
145
.max_entries = NETDATA_SOCKET_COUNTER
145
146
};
146
147
148
+ struct bpf_map_def SEC ("maps" ) tbl_used_ports = {
149
+ .type = BPF_MAP_TYPE_HASH ,
150
+ .key_size = sizeof (__u16 ),
151
+ .value_size = sizeof (__u8 ),
152
+ .max_entries = 65536
153
+ };
154
+
147
155
/************************************************************************************
148
156
*
149
157
* Common Section
@@ -183,27 +191,36 @@ static __u16 set_idx_value(netdata_socket_idx_t *nsi, struct inet_sock *is)
183
191
{
184
192
__u16 family ;
185
193
186
- //Read Family
194
+ // Read Family
187
195
bpf_probe_read (& family , sizeof (u16 ), & is -> sk .__sk_common .skc_family );
188
- //Read source and destination IPs
196
+ // Read source and destination IPs
189
197
if ( family == AF_INET ) { //AF_INET
190
- bpf_probe_read (& nsi -> saddr .addr32 [0 ], sizeof (u32 ), & is -> inet_saddr );
198
+ bpf_probe_read (& nsi -> saddr .addr32 [0 ], sizeof (u32 ), & is -> inet_rcv_saddr );
191
199
bpf_probe_read (& nsi -> daddr .addr32 [0 ], sizeof (u32 ), & is -> inet_daddr );
200
+
201
+ if (!nsi -> saddr .addr32 [0 ] || !nsi -> daddr .addr32 [0 ])
202
+ return AF_UNSPEC ;
192
203
}
193
204
// Check necessary according https://elixir.bootlin.com/linux/v5.6.14/source/include/net/sock.h#L199
194
205
#if IS_ENABLED (CONFIG_IPV6 )
195
- else if ( family == AF_INET6 ){
206
+ else if ( family == AF_INET6 ) {
196
207
struct in6_addr * addr6 = & is -> sk .sk_v6_rcv_saddr ;
197
208
bpf_probe_read (& nsi -> saddr .addr8 , sizeof (__u8 )* 16 , & addr6 -> s6_addr );
198
209
199
210
addr6 = & is -> sk .sk_v6_daddr ;
200
211
bpf_probe_read (& nsi -> daddr .addr8 , sizeof (__u8 )* 16 , & addr6 -> s6_addr );
212
+
213
+ if ( ((!nsi -> saddr .addr64 [0 ]) && (!nsi -> saddr .addr64 [1 ])) || ((!nsi -> daddr .addr64 [0 ]) && (!nsi -> daddr .addr64 [1 ])))
214
+ return AF_UNSPEC ;
201
215
}
202
216
#endif
217
+ else {
218
+ return AF_UNSPEC ;
219
+ }
203
220
204
221
//Read destination port
205
222
bpf_probe_read (& nsi -> dport , sizeof (u16 ), & is -> inet_dport );
206
- bpf_probe_read (& nsi -> sport , sizeof (u16 ), & is -> inet_num );
223
+ bpf_probe_read (& nsi -> sport , sizeof (u16 ), & is -> inet_sport );
207
224
208
225
return family ;
209
226
}
@@ -223,7 +240,9 @@ static void update_socket_stats(netdata_socket_t *ptr, __u64 sent, __u64 receive
223
240
224
241
netdata_update_u64 (& ptr -> sent_bytes , sent );
225
242
netdata_update_u64 (& ptr -> recv_bytes , received );
226
- netdata_update_u64 (& ptr -> retransmit , retransmitted );
243
+ // We can use update_u64, it was overwritten
244
+ // the values
245
+ ptr -> retransmit += retransmitted ;
227
246
}
228
247
229
248
/**
@@ -278,6 +297,26 @@ static void update_pid_stats(__u32 pid, __u32 tgid, __u64 sent, __u64 received)
278
297
}
279
298
}
280
299
300
+ SEC ("kretprobe/inet_csk_accept" )
301
+ int netdata_inet_csk_accept (struct pt_regs * ctx )
302
+ {
303
+ struct sock * sk = (struct sock * )PT_REGS_RC (ctx );
304
+ if (!sk )
305
+ return 0 ;
306
+
307
+
308
+ __u16 dport ;
309
+ bpf_probe_read (& dport , sizeof (u16 ), & sk -> __sk_common .skc_num );
310
+
311
+ __u8 * value = (__u8 * )bpf_map_lookup_elem (& tbl_used_ports , & dport );
312
+ if (!value ) {
313
+ __u8 value = 1 ;
314
+ bpf_map_update_elem (& tbl_used_ports , & dport , & value , BPF_ANY );
315
+ }
316
+
317
+ return 0 ;
318
+ }
319
+
281
320
/************************************************************************************
282
321
*
283
322
* TCP Section
@@ -318,6 +357,9 @@ int netdata_tcp_sendmsg(struct pt_regs* ctx)
318
357
netdata_update_global (NETDATA_KEY_CALLS_TCP_SENDMSG , 1 );
319
358
320
359
family = set_idx_value (& idx , is );
360
+ if (!family )
361
+ return 0 ;
362
+
321
363
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
322
364
323
365
netdata_update_global (NETDATA_KEY_BYTES_TCP_SENDMSG , (__u64 )sent );
@@ -336,6 +378,9 @@ int netdata_tcp_retransmit_skb(struct pt_regs* ctx)
336
378
337
379
struct inet_sock * is = inet_sk ((struct sock * )PT_REGS_PARM1 (ctx ));
338
380
family = set_idx_value (& idx , is );
381
+ if (!family )
382
+ return 0 ;
383
+
339
384
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
340
385
341
386
netdata_update_global (NETDATA_KEY_TCP_RETRANSMIT , 1 );
@@ -369,6 +414,9 @@ int netdata_tcp_cleanup_rbuf(struct pt_regs* ctx)
369
414
__u64 received = (__u64 ) PT_REGS_PARM2 (ctx );
370
415
371
416
family = set_idx_value (& idx , is );
417
+ if (!family )
418
+ return 0 ;
419
+
372
420
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
373
421
374
422
netdata_update_global (NETDATA_KEY_BYTES_TCP_CLEANUP_RBUF , received );
@@ -396,6 +444,9 @@ int netdata_tcp_close(struct pt_regs* ctx)
396
444
netdata_update_global (NETDATA_KEY_CALLS_TCP_CLOSE , 1 );
397
445
398
446
family = set_idx_value (& idx , is );
447
+ if (!family )
448
+ return 0 ;
449
+
399
450
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
400
451
val = (netdata_socket_t * ) bpf_map_lookup_elem (tbl , & idx );
401
452
if (val ) {
@@ -466,6 +517,9 @@ int trace_udp_ret_recvmsg(struct pt_regs* ctx)
466
517
bpf_map_delete_elem (& tbl_nv_udp_conn_stats , & pid_tgid );
467
518
468
519
family = set_idx_value (& idx , is );
520
+ if (!family )
521
+ return 0 ;
522
+
469
523
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
470
524
471
525
netdata_update_global (NETDATA_KEY_BYTES_UDP_RECVMSG , received );
@@ -507,6 +561,9 @@ int trace_udp_sendmsg(struct pt_regs* ctx)
507
561
netdata_update_global (NETDATA_KEY_CALLS_UDP_SENDMSG , 1 );
508
562
509
563
family = set_idx_value (& idx , is );
564
+ if (!family )
565
+ return 0 ;
566
+
510
567
tbl = (family == AF_INET6 )?& tbl_conn_ipv6 :& tbl_conn_ipv4 ;
511
568
512
569
update_socket_table (tbl , & idx , (__u64 ) sent , 0 , 0 , IPPROTO_UDP );
0 commit comments