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

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