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

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