Blame SOURCES/bpftrace-0.16.0-tcpdrop-Fix-ERROR-Error-attaching-probe-kprobe-tcp_d.patch

ec494a
From e661f2a043f8b6548e0bb3e0cc5992d7c0ff3b0f Mon Sep 17 00:00:00 2001
ec494a
From: Rong Tao <rongtao@cestc.cn>
ec494a
Date: Sat, 1 Oct 2022 16:15:27 +0800
ec494a
Subject: [PATCH] tcpdrop: Fix: ERROR: Error attaching probe: 'kprobe:tcp_drop'
ec494a
ec494a
kernel commit 8fbf195798b5('tcp_drop() is no longer needed.') remove
ec494a
the kprobe:tcp_drop, bcc commit 16eab39171eb('Add
ec494a
tracepoint:skb:kfree_skb if no tcp_drop() kprobe.') already fix this
ec494a
problem.
ec494a
ec494a
CI old kernel is too old and not support the 'reason' field, move the
ec494a
old tools/tcpdrop.bt into tools/old/tcpdrop.bt and set the CI to use
ec494a
it.
ec494a
ec494a
Since 5.17 support trace_kfree_skb(skb, ..., reason) 'reason' field.
ec494a
Since 5.19 remove tcp_drop() function.
ec494a
ec494a
ERROR log:
ec494a
ec494a
 $ sudo ./tcpdrop.bt
ec494a
 ./tcpdrop.bt:49-51: WARNING: tcp_drop is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
ec494a
 Attaching 3 probes...
ec494a
 cannot attach kprobe, probe entry may not exist
ec494a
 ERROR: Error attaching probe: 'kprobe:tcp_drop'
ec494a
ec494a
Link: https://github.com/iovisor/bpftrace/pull/2379
ec494a
Signed-off-by: Rong Tao <rongtao@cestc.cn>
ec494a
---
ec494a
 tools/old/tcpdrop.bt | 85 ++++++++++++++++++++++++++++++++++++++++++++
ec494a
 tools/tcpdrop.bt     | 22 ++++++------
ec494a
 2 files changed, 97 insertions(+), 10 deletions(-)
ec494a
 create mode 100755 tools/old/tcpdrop.bt
ec494a
ec494a
diff --git a/tools/old/tcpdrop.bt b/tools/old/tcpdrop.bt
ec494a
new file mode 100755
ec494a
index 00000000..685a5f6a
ec494a
--- /dev/null
ec494a
+++ b/tools/old/tcpdrop.bt
ec494a
@@ -0,0 +1,85 @@
ec494a
+#!/usr/bin/env bpftrace
ec494a
+/*
ec494a
+ * tcpdrop.bt   Trace TCP kernel-dropped packets/segments.
ec494a
+ *              For Linux, uses bpftrace and eBPF.
ec494a
+ *
ec494a
+ * USAGE: tcpdrop.bt
ec494a
+ *
ec494a
+ * This is a bpftrace version of the bcc tool of the same name.
ec494a
+ * It is limited to ipv4 addresses, and cannot show tcp flags.
ec494a
+ *
ec494a
+ * This provides information such as packet details, socket state, and kernel
ec494a
+ * stack trace for packets/segments that were dropped via tcp_drop().
ec494a
+
ec494a
+ * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
ec494a
+ *          on newer kernels and not replaced by anything else, therefore
ec494a
+ *          the script will stop working
ec494a
+ *
ec494a
+ * For Linux <= 5.18.
ec494a
+ *
ec494a
+ * Copyright (c) 2018 Dale Hamel.
ec494a
+ * Licensed under the Apache License, Version 2.0 (the "License")
ec494a
+ *
ec494a
+ * 23-Nov-2018	Dale Hamel	created this.
ec494a
+ */
ec494a
+
ec494a
+#ifndef BPFTRACE_HAVE_BTF
ec494a
+#include <linux/socket.h>
ec494a
+#include <net/sock.h>
ec494a
+#else
ec494a
+#include <sys/socket.h>
ec494a
+#endif
ec494a
+
ec494a
+BEGIN
ec494a
+{
ec494a
+  printf("Tracing tcp drops. Hit Ctrl-C to end.\n");
ec494a
+  printf("%-8s %-8s %-16s %-21s %-21s %-8s\n", "TIME", "PID", "COMM", "SADDR:SPORT", "DADDR:DPORT", "STATE");
ec494a
+
ec494a
+  // See https://github.com/torvalds/linux/blob/master/include/net/tcp_states.h
ec494a
+  @tcp_states[1] = "ESTABLISHED";
ec494a
+  @tcp_states[2] = "SYN_SENT";
ec494a
+  @tcp_states[3] = "SYN_RECV";
ec494a
+  @tcp_states[4] = "FIN_WAIT1";
ec494a
+  @tcp_states[5] = "FIN_WAIT2";
ec494a
+  @tcp_states[6] = "TIME_WAIT";
ec494a
+  @tcp_states[7] = "CLOSE";
ec494a
+  @tcp_states[8] = "CLOSE_WAIT";
ec494a
+  @tcp_states[9] = "LAST_ACK";
ec494a
+  @tcp_states[10] = "LISTEN";
ec494a
+  @tcp_states[11] = "CLOSING";
ec494a
+  @tcp_states[12] = "NEW_SYN_RECV";
ec494a
+}
ec494a
+
ec494a
+kprobe:tcp_drop
ec494a
+{
ec494a
+  $sk = ((struct sock *) arg0);
ec494a
+  $inet_family = $sk->__sk_common.skc_family;
ec494a
+
ec494a
+  if ($inet_family == AF_INET || $inet_family == AF_INET6) {
ec494a
+    if ($inet_family == AF_INET) {
ec494a
+      $daddr = ntop($sk->__sk_common.skc_daddr);
ec494a
+      $saddr = ntop($sk->__sk_common.skc_rcv_saddr);
ec494a
+    } else {
ec494a
+      $daddr = ntop($sk->__sk_common.skc_v6_daddr.in6_u.u6_addr8);
ec494a
+      $saddr = ntop($sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr8);
ec494a
+    }
ec494a
+    $lport = $sk->__sk_common.skc_num;
ec494a
+    $dport = $sk->__sk_common.skc_dport;
ec494a
+
ec494a
+    // Destination port is big endian, it must be flipped
ec494a
+    $dport = bswap($dport);
ec494a
+
ec494a
+    $state = $sk->__sk_common.skc_state;
ec494a
+    $statestr = @tcp_states[$state];
ec494a
+
ec494a
+    time("%H:%M:%S ");
ec494a
+    printf("%-8d %-16s ", pid, comm);
ec494a
+    printf("%39s:%-6d %39s:%-6d %-10s\n", $saddr, $lport, $daddr, $dport, $statestr);
ec494a
+    printf("%s\n", kstack);
ec494a
+  }
ec494a
+}
ec494a
+
ec494a
+END
ec494a
+{
ec494a
+  clear(@tcp_states);
ec494a
+}
ec494a
diff --git a/tools/tcpdrop.bt b/tools/tcpdrop.bt
ec494a
index 3450a533..bb31107f 100755
ec494a
--- a/tools/tcpdrop.bt
ec494a
+++ b/tools/tcpdrop.bt
ec494a
@@ -9,16 +9,15 @@
ec494a
  * It is limited to ipv4 addresses, and cannot show tcp flags.
ec494a
  *
ec494a
  * This provides information such as packet details, socket state, and kernel
ec494a
- * stack trace for packets/segments that were dropped via tcp_drop().
ec494a
-
ec494a
- * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
ec494a
- *          on newer kernels and not replaced by anything else, therefore
ec494a
- *          the script will stop working
ec494a
-
ec494a
+ * stack trace for packets/segments that were dropped via kfree_skb.
ec494a
+ *
ec494a
+ * For Linux 5.17+ (see tools/old for script for lower versions).
ec494a
+ *
ec494a
  * Copyright (c) 2018 Dale Hamel.
ec494a
  * Licensed under the Apache License, Version 2.0 (the "License")
ec494a
-
ec494a
+ *
ec494a
  * 23-Nov-2018	Dale Hamel	created this.
ec494a
+ * 01-Oct-2022	Rong Tao	use tracepoint:skb:kfree_skb
ec494a
  */
ec494a
 
ec494a
 #ifndef BPFTRACE_HAVE_BTF
ec494a
@@ -48,12 +47,15 @@ BEGIN
ec494a
   @tcp_states[12] = "NEW_SYN_RECV";
ec494a
 }
ec494a
 
ec494a
-kprobe:tcp_drop
ec494a
+tracepoint:skb:kfree_skb
ec494a
 {
ec494a
-  $sk = ((struct sock *) arg0);
ec494a
+  $reason = args->reason;
ec494a
+  $skb = (struct sk_buff *)args->skbaddr;
ec494a
+  $sk = ((struct sock *) $skb->sk);
ec494a
   $inet_family = $sk->__sk_common.skc_family;
ec494a
 
ec494a
-  if ($inet_family == AF_INET || $inet_family == AF_INET6) {
ec494a
+  if ($reason > SKB_DROP_REASON_NOT_SPECIFIED &&
ec494a
+      ($inet_family == AF_INET || $inet_family == AF_INET6)) {
ec494a
     if ($inet_family == AF_INET) {
ec494a
       $daddr = ntop($sk->__sk_common.skc_daddr);
ec494a
       $saddr = ntop($sk->__sk_common.skc_rcv_saddr);
ec494a
-- 
ec494a
2.38.1
ec494a