tcp_dumpstats_kern.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Refer to samples/bpf/tcp_bpf.readme for the instructions on
  3. * how to run this sample program.
  4. */
  5. #include <linux/bpf.h>
  6. #include <bpf/bpf_helpers.h>
  7. #include <bpf/bpf_endian.h>
  8. #define INTERVAL 1000000000ULL
  9. int _version SEC("version") = 1;
  10. char _license[] SEC("license") = "GPL";
  11. struct {
  12. __u32 type;
  13. __u32 map_flags;
  14. int *key;
  15. __u64 *value;
  16. } bpf_next_dump SEC(".maps") = {
  17. .type = BPF_MAP_TYPE_SK_STORAGE,
  18. .map_flags = BPF_F_NO_PREALLOC,
  19. };
  20. SEC("sockops")
  21. int _sockops(struct bpf_sock_ops *ctx)
  22. {
  23. struct bpf_tcp_sock *tcp_sk;
  24. struct bpf_sock *sk;
  25. __u64 *next_dump;
  26. __u64 now;
  27. switch (ctx->op) {
  28. case BPF_SOCK_OPS_TCP_CONNECT_CB:
  29. bpf_sock_ops_cb_flags_set(ctx, BPF_SOCK_OPS_RTT_CB_FLAG);
  30. return 1;
  31. case BPF_SOCK_OPS_RTT_CB:
  32. break;
  33. default:
  34. return 1;
  35. }
  36. sk = ctx->sk;
  37. if (!sk)
  38. return 1;
  39. next_dump = bpf_sk_storage_get(&bpf_next_dump, sk, 0,
  40. BPF_SK_STORAGE_GET_F_CREATE);
  41. if (!next_dump)
  42. return 1;
  43. now = bpf_ktime_get_ns();
  44. if (now < *next_dump)
  45. return 1;
  46. tcp_sk = bpf_tcp_sock(sk);
  47. if (!tcp_sk)
  48. return 1;
  49. *next_dump = now + INTERVAL;
  50. bpf_printk("dsack_dups=%u delivered=%u\n",
  51. tcp_sk->dsack_dups, tcp_sk->delivered);
  52. bpf_printk("delivered_ce=%u icsk_retransmits=%u\n",
  53. tcp_sk->delivered_ce, tcp_sk->icsk_retransmits);
  54. return 1;
  55. }