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

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