github.com/cilium/cilium@v1.16.2/bpf/lib/dbg.h (about)

     1  /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
     2  /* Copyright Authors of Cilium */
     3  
     4  #pragma once
     5  
     6  /* Trace types */
     7  enum {
     8  	DBG_UNSPEC,
     9  	DBG_GENERIC, /* Generic, no message, useful to dump random integers */
    10  	DBG_LOCAL_DELIVERY,
    11  	DBG_ENCAP,
    12  	DBG_LXC_FOUND,
    13  	DBG_POLICY_DENIED,
    14  	DBG_CT_LOOKUP,		/* unused */
    15  	DBG_CT_LOOKUP_REV,	/* unused */
    16  	DBG_CT_MATCH,
    17  	DBG_CT_CREATED,		/* unused */
    18  	DBG_CT_CREATED2,	/* unused */
    19  	DBG_ICMP6_HANDLE,
    20  	DBG_ICMP6_REQUEST,
    21  	DBG_ICMP6_NS,
    22  	DBG_ICMP6_TIME_EXCEEDED,
    23  	DBG_CT_VERDICT,
    24  	DBG_DECAP,
    25  	DBG_PORT_MAP,
    26  	DBG_ERROR_RET,
    27  	DBG_TO_HOST,
    28  	DBG_TO_STACK,
    29  	DBG_PKT_HASH,
    30  	DBG_LB6_LOOKUP_FRONTEND,
    31  	DBG_LB6_LOOKUP_FRONTEND_FAIL,
    32  	DBG_LB6_LOOKUP_BACKEND_SLOT,
    33  	DBG_LB6_LOOKUP_BACKEND_SLOT_SUCCESS,
    34  	DBG_LB6_LOOKUP_BACKEND_SLOT_V2_FAIL,
    35  	DBG_LB6_LOOKUP_BACKEND_FAIL,
    36  	DBG_LB6_REVERSE_NAT_LOOKUP,
    37  	DBG_LB6_REVERSE_NAT,
    38  	DBG_LB4_LOOKUP_FRONTEND,
    39  	DBG_LB4_LOOKUP_FRONTEND_FAIL,
    40  	DBG_LB4_LOOKUP_BACKEND_SLOT,
    41  	DBG_LB4_LOOKUP_BACKEND_SLOT_SUCCESS,
    42  	DBG_LB4_LOOKUP_BACKEND_SLOT_V2_FAIL,
    43  	DBG_LB4_LOOKUP_BACKEND_FAIL,
    44  	DBG_LB4_REVERSE_NAT_LOOKUP,
    45  	DBG_LB4_REVERSE_NAT,
    46  	DBG_LB4_LOOPBACK_SNAT,
    47  	DBG_LB4_LOOPBACK_SNAT_REV,
    48  	DBG_CT_LOOKUP4,
    49  	DBG_RR_BACKEND_SLOT_SEL,
    50  	DBG_REV_PROXY_LOOKUP,
    51  	DBG_REV_PROXY_FOUND,
    52  	DBG_REV_PROXY_UPDATE,
    53  	DBG_L4_POLICY,
    54  	DBG_NETDEV_IN_CLUSTER, /* arg1: security-context, arg2: unused */
    55  	DBG_NETDEV_ENCAP4, /* arg1 encap lookup key, arg2: identity */
    56  	DBG_CT_LOOKUP4_1,       /* arg1: saddr
    57  				 * arg2: daddr
    58  				 * arg3: (sport << 16) | dport
    59  				 */
    60  	DBG_CT_LOOKUP4_2,       /* arg1: (nexthdr << 8) | flags
    61  				 * arg2: direction
    62  				 * arg3: scope
    63  				 */
    64  	DBG_CT_CREATED4,        /* arg1: (unused << 16) | rev_nat_index
    65  				 * arg2: src sec-id
    66  				 * arg3: unused
    67  				 */
    68  	DBG_CT_LOOKUP6_1,       /* arg1: saddr (last 4 bytes)
    69  				 * arg2: daddr (last 4 bytes)
    70  				 * arg3: (sport << 16) | dport
    71  				 */
    72  	DBG_CT_LOOKUP6_2,       /* arg1: (nexthdr << 8) | flags
    73  				 * arg2: direction
    74  				 * arg3: scope
    75  				 */
    76  	DBG_CT_CREATED6,        /* arg1: (unused << 16) | rev_nat_index
    77  				 * arg2: src sec-id
    78  				 * arg3: unused
    79  				 */
    80  	DBG_SKIP_PROXY,          /* arg1: ctx->tc_index
    81  				  * arg2: unused
    82  				  */
    83  	DBG_L4_CREATE,		/* arg1: src sec-id
    84  				 * arg2: dst sec-id
    85  				 * arg3: (dport << 16) | protocol
    86  				 */
    87  	DBG_IP_ID_MAP_FAILED4,	/* arg1: daddr
    88  				 * arg2: unused
    89  				 * arg3: unused
    90  				 */
    91  	DBG_IP_ID_MAP_FAILED6,	/* arg1: daddr (last 4 bytes)
    92  				 * arg2: unused
    93  				 * arg3: unused
    94  				 */
    95  	DBG_IP_ID_MAP_SUCCEED4,	/* arg1: daddr
    96  				 * arg2: identity
    97  				 * arg3: unused
    98  				 */
    99  	DBG_IP_ID_MAP_SUCCEED6,	/* arg1: daddr (last 4 bytes)
   100  				 * arg2: identity
   101  				 * arg3: unused
   102  				 */
   103  	DBG_LB_STALE_CT,	/* arg1: svc rev_nat_id
   104  				 * arg2: stale CT rev_nat_id
   105  				 * arg3: unused
   106  				 */
   107  	DBG_INHERIT_IDENTITY,	/* arg1: ctx->mark
   108  				 * arg2: unused
   109  				 */
   110  	DBG_SK_LOOKUP4,		/* arg1: saddr
   111  				 * arg2: daddr
   112  				 * arg3: (sport << 16) | dport
   113  				 */
   114  	DBG_SK_LOOKUP6,		/* arg1: saddr (last 4 bytes)
   115  				 * arg2: daddr (last 4 bytes)
   116  				 * arg3: (sport << 16) | dport
   117  				 */
   118  	DBG_SK_ASSIGN,		/* arg1: result
   119  				 * arg2: unuseds
   120  				 */
   121  	DBG_L7_LB,		/* arg1: saddr (last 4 bytes for IPv6)
   122  				 * arg2: daddr (last 4 bytes for IPv6)
   123  				 * arg3: proxy port (in host byte order)
   124  				 */
   125  	DBG_SKIP_POLICY,	/**/
   126  };
   127  
   128  /* Capture types */
   129  enum {
   130  	DBG_CAPTURE_UNSPEC,
   131  	DBG_CAPTURE_FROM_RESERVED1,
   132  	DBG_CAPTURE_FROM_RESERVED2,
   133  	DBG_CAPTURE_FROM_RESERVED3,
   134  	DBG_CAPTURE_DELIVERY,
   135  	DBG_CAPTURE_FROM_LB,
   136  	DBG_CAPTURE_AFTER_V46,
   137  	DBG_CAPTURE_AFTER_V64,
   138  	DBG_CAPTURE_PROXY_PRE,
   139  	DBG_CAPTURE_PROXY_POST,
   140  	DBG_CAPTURE_SNAT_PRE,
   141  	DBG_CAPTURE_SNAT_POST,
   142  };
   143  
   144  #ifndef EVENT_SOURCE
   145  #define EVENT_SOURCE 0
   146  #endif
   147  
   148  #ifdef DEBUG
   149  #include "events.h"
   150  #endif
   151  
   152  #ifdef DEBUG
   153  #include "common.h"
   154  #include "utils.h"
   155  
   156  /* This takes both literals and modifiers, e.g.,
   157   * printk("hello\n");
   158   * printk("%d\n", ret);
   159   *
   160   * Three caveats when using this:
   161   * - message needs to end with newline
   162   *
   163   * - only a subset of specifier are supported:
   164   *   https://elixir.bootlin.com/linux/v5.7.7/source/kernel/trace/bpf_trace.c#L325
   165   *
   166   * - cannot use more than 3 format specifiers in the format string
   167   *   because BPF helpers take a maximum of 5 arguments
   168   */
   169  # define printk(fmt, ...)					\
   170  		({						\
   171  			const char ____fmt[] = fmt;		\
   172  			trace_printk(____fmt, sizeof(____fmt),	\
   173  				     ##__VA_ARGS__);		\
   174  		})
   175  
   176  struct debug_msg {
   177  	NOTIFY_COMMON_HDR
   178  	__u32		arg1;
   179  	__u32		arg2;
   180  	__u32		arg3;
   181  };
   182  
   183  static __always_inline void cilium_dbg(struct __ctx_buff *ctx, __u8 type,
   184  				       __u32 arg1, __u32 arg2)
   185  {
   186  	struct debug_msg msg = {
   187  		__notify_common_hdr(CILIUM_NOTIFY_DBG_MSG, type),
   188  		.arg1	= arg1,
   189  		.arg2	= arg2,
   190  	};
   191  
   192  	ctx_event_output(ctx, &EVENTS_MAP, BPF_F_CURRENT_CPU,
   193  			 &msg, sizeof(msg));
   194  }
   195  
   196  static __always_inline void cilium_dbg3(struct __ctx_buff *ctx, __u8 type,
   197  					__u32 arg1, __u32 arg2, __u32 arg3)
   198  {
   199  	struct debug_msg msg = {
   200  		__notify_common_hdr(CILIUM_NOTIFY_DBG_MSG, type),
   201  		.arg1	= arg1,
   202  		.arg2	= arg2,
   203  		.arg3	= arg3,
   204  	};
   205  
   206  	ctx_event_output(ctx, &EVENTS_MAP, BPF_F_CURRENT_CPU,
   207  			 &msg, sizeof(msg));
   208  }
   209  
   210  struct debug_capture_msg {
   211  	NOTIFY_CAPTURE_HDR
   212  	__u32		arg1;
   213  	__u32		arg2;
   214  };
   215  
   216  static __always_inline void cilium_dbg_capture2(struct __ctx_buff *ctx, __u8 type,
   217  						__u32 arg1, __u32 arg2)
   218  {
   219  	__u64 ctx_len = ctx_full_len(ctx);
   220  	__u64 cap_len = min_t(__u64, TRACE_PAYLOAD_LEN, ctx_len);
   221  	struct debug_capture_msg msg = {
   222  		__notify_common_hdr(CILIUM_NOTIFY_DBG_CAPTURE, type),
   223  		__notify_pktcap_hdr(ctx_len, (__u16)cap_len),
   224  		.arg1	= arg1,
   225  		.arg2	= arg2,
   226  	};
   227  
   228  	ctx_event_output(ctx, &EVENTS_MAP,
   229  			 (cap_len << 32) | BPF_F_CURRENT_CPU,
   230  			 &msg, sizeof(msg));
   231  }
   232  
   233  static __always_inline void cilium_dbg_capture(struct __ctx_buff *ctx, __u8 type,
   234  					       __u32 arg1)
   235  {
   236  	cilium_dbg_capture2(ctx, type, arg1, 0);
   237  }
   238  #else
   239  # define printk(fmt, ...)					\
   240  		do { } while (0)
   241  
   242  static __always_inline
   243  void cilium_dbg(struct __ctx_buff *ctx __maybe_unused, __u8 type __maybe_unused,
   244  		__u32 arg1 __maybe_unused, __u32 arg2 __maybe_unused)
   245  {
   246  }
   247  
   248  static __always_inline
   249  void cilium_dbg3(struct __ctx_buff *ctx __maybe_unused,
   250  		 __u8 type __maybe_unused, __u32 arg1 __maybe_unused,
   251  		 __u32 arg2 __maybe_unused, __u32 arg3 __maybe_unused)
   252  {
   253  }
   254  
   255  static __always_inline
   256  void cilium_dbg_capture(struct __ctx_buff *ctx __maybe_unused,
   257  			__u8 type __maybe_unused, __u32 arg1 __maybe_unused)
   258  {
   259  }
   260  
   261  static __always_inline
   262  void cilium_dbg_capture2(struct __ctx_buff *ctx __maybe_unused,
   263  			 __u8 type __maybe_unused, __u32 arg1 __maybe_unused,
   264  			 __u32 arg2 __maybe_unused)
   265  {
   266  }
   267  
   268  #endif