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

     1  /*
     2   *  Copyright (C) 2016-2019 Authors of Cilium
     3   *
     4   *  This program is free software; you can redistribute it and/or modify
     5   *  it under the terms of the GNU General Public License as published by
     6   *  the Free Software Foundation; either version 2 of the License, or
     7   *  (at your option) any later version.
     8   *
     9   *  This program is distributed in the hope that it will be useful,
    10   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   *  GNU General Public License for more details.
    13   *
    14   *  You should have received a copy of the GNU General Public License
    15   *  along with this program; if not, write to the Free Software
    16   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17   */
    18  #ifndef __LIB_DBG__
    19  #define __LIB_DBG__
    20  
    21  /* Trace types */
    22  enum {
    23  	DBG_UNSPEC,
    24  	DBG_GENERIC, /* Generic, no message, useful to dump random integers */
    25  	DBG_LOCAL_DELIVERY,
    26  	DBG_ENCAP,
    27  	DBG_LXC_FOUND,
    28  	DBG_POLICY_DENIED,
    29  	DBG_CT_LOOKUP,		/* unused */
    30  	DBG_CT_LOOKUP_REV,	/* unused */
    31  	DBG_CT_MATCH,
    32  	DBG_CT_CREATED,		/* unused */
    33  	DBG_CT_CREATED2,	/* unused */
    34  	DBG_ICMP6_HANDLE,
    35  	DBG_ICMP6_REQUEST,
    36  	DBG_ICMP6_NS,
    37  	DBG_ICMP6_TIME_EXCEEDED,
    38  	DBG_CT_VERDICT,
    39  	DBG_DECAP,
    40  	DBG_PORT_MAP,
    41  	DBG_ERROR_RET,
    42  	DBG_TO_HOST,
    43  	DBG_TO_STACK,
    44  	DBG_PKT_HASH,
    45  	DBG_LB6_LOOKUP_MASTER,
    46  	DBG_LB6_LOOKUP_MASTER_FAIL,
    47  	DBG_LB6_LOOKUP_SLAVE,
    48  	DBG_LB6_LOOKUP_SLAVE_SUCCESS,
    49  	DBG_LB6_LOOKUP_SLAVE_V2_FAIL,
    50  	DBG_LB6_LOOKUP_BACKEND_FAIL,
    51  	DBG_LB6_REVERSE_NAT_LOOKUP,
    52  	DBG_LB6_REVERSE_NAT,
    53  	DBG_LB4_LOOKUP_MASTER,
    54  	DBG_LB4_LOOKUP_MASTER_FAIL,
    55  	DBG_LB4_LOOKUP_SLAVE,
    56  	DBG_LB4_LOOKUP_SLAVE_SUCCESS,
    57  	DBG_LB4_LOOKUP_SLAVE_V2_FAIL,
    58  	DBG_LB4_LOOKUP_BACKEND_FAIL,
    59  	DBG_LB4_REVERSE_NAT_LOOKUP,
    60  	DBG_LB4_REVERSE_NAT,
    61  	DBG_LB4_LOOPBACK_SNAT,
    62  	DBG_LB4_LOOPBACK_SNAT_REV,
    63  	DBG_CT_LOOKUP4,
    64  	DBG_RR_SLAVE_SEL,
    65  	DBG_REV_PROXY_LOOKUP,
    66  	DBG_REV_PROXY_FOUND,
    67  	DBG_REV_PROXY_UPDATE,
    68  	DBG_L4_POLICY,
    69  	DBG_NETDEV_IN_CLUSTER, /* arg1: security-context, arg2: unused */
    70  	DBG_NETDEV_ENCAP4, /* arg1 encap lookup key, arg2: identity */
    71  	DBG_CT_LOOKUP4_1,       /* arg1: saddr
    72  				 * arg2: daddr
    73  				 * arg3: (sport << 16) | dport
    74  				 */
    75  	DBG_CT_LOOKUP4_2,       /* arg1: (nexthdr << 8) | flags
    76  				 * arg2: direction
    77  				 * arg3: unused
    78  				 */
    79  	DBG_CT_CREATED4,        /* arg1: (unused << 16) | rev_nat_index
    80  				 * arg2: src sec-id
    81  				 * arg3: lb address
    82  				 */ 
    83  	DBG_CT_LOOKUP6_1,       /* arg1: saddr (last 4 bytes)
    84  				 * arg2: daddr (last 4 bytes)
    85  				 * arg3: (sport << 16) | dport
    86  				 */
    87  	DBG_CT_LOOKUP6_2,       /* arg1: (nexthdr << 8) | flags
    88  				 * arg2: direction
    89  				 * arg3: unused
    90  				 */
    91  	DBG_CT_CREATED6,        /* arg1: (unused << 16) | rev_nat_index
    92  				 * arg2: src sec-id
    93  				 * arg3: unused
    94  				 */
    95  	DBG_SKIP_PROXY,          /* arg1: skb->tc_index
    96  				  * arg2: unused
    97  				  */
    98  	DBG_L4_CREATE,		/* arg1: src sec-id
    99  				 * arg2: dst sec-id
   100  				 * arg3: (dport << 16) | protocol
   101  				 */
   102  	DBG_IP_ID_MAP_FAILED4,	/* arg1: daddr
   103  				 * arg2: unused
   104  				 * arg3: unused */
   105  	DBG_IP_ID_MAP_FAILED6,	/* arg1: daddr (last 4 bytes)
   106  				 * arg2: unused
   107  				 * arg3: unused */
   108  	DBG_IP_ID_MAP_SUCCEED4,	/* arg1: daddr
   109  				 * arg2: identity
   110  				 * arg3: unused */
   111  	DBG_IP_ID_MAP_SUCCEED6,	/* arg1: daddr (last 4 bytes)
   112  				 * arg2: identity
   113  				 * arg3: unused */
   114  	DBG_LB_STALE_CT,	/* arg1: svc rev_nat_id
   115  				   arg2: stale CT rev_nat_id
   116  				   arg3: unused */
   117  	DBG_INHERIT_IDENTITY	/* arg1: skb->mark
   118  				 * arg2: unused */
   119  };
   120  
   121  /* Capture types */
   122  enum {
   123  	DBG_CAPTURE_UNSPEC,
   124  	DBG_CAPTURE_FROM_RESERVED1,
   125  	DBG_CAPTURE_FROM_RESERVED2,
   126  	DBG_CAPTURE_FROM_RESERVED3,
   127  	DBG_CAPTURE_DELIVERY,
   128  	DBG_CAPTURE_FROM_LB,
   129  	DBG_CAPTURE_AFTER_V46,
   130  	DBG_CAPTURE_AFTER_V64,
   131  	DBG_CAPTURE_PROXY_PRE,
   132  	DBG_CAPTURE_PROXY_POST,
   133  	DBG_CAPTURE_SNAT_PRE,
   134  	DBG_CAPTURE_SNAT_POST,
   135  };
   136  
   137  #ifndef EVENT_SOURCE
   138  #define EVENT_SOURCE 0
   139  #endif
   140  
   141  #if defined DEBUG
   142  #include "events.h"
   143  #endif
   144  
   145  #ifdef DEBUG
   146  #include "utils.h"
   147  
   148  # define printk(fmt, ...)					\
   149  		({						\
   150  			char ____fmt[] = fmt;			\
   151  			trace_printk(____fmt, sizeof(____fmt),	\
   152  				     ##__VA_ARGS__);		\
   153  		})
   154  
   155  struct debug_msg {
   156  	NOTIFY_COMMON_HDR
   157  	__u32		arg1;
   158  	__u32		arg2;
   159  	__u32		arg3;
   160  };
   161  
   162  static inline void cilium_dbg(struct __sk_buff *skb, __u8 type, __u32 arg1, __u32 arg2)
   163  {
   164  	uint32_t hash = get_hash_recalc(skb);
   165  	struct debug_msg msg = {
   166  		.type = CILIUM_NOTIFY_DBG_MSG,
   167  		.subtype = type,
   168  		.source = EVENT_SOURCE,
   169  		.hash = hash,
   170  		.arg1 = arg1,
   171  		.arg2 = arg2,
   172  	};
   173  
   174  	skb_event_output(skb, &EVENTS_MAP, BPF_F_CURRENT_CPU, &msg, sizeof(msg));
   175  }
   176  
   177  static inline void cilium_dbg3(struct __sk_buff *skb, __u8 type, __u32 arg1,
   178  			       __u32 arg2, __u32 arg3)
   179  {
   180  	uint32_t hash = get_hash_recalc(skb);
   181  	struct debug_msg msg = {
   182  		.type = CILIUM_NOTIFY_DBG_MSG,
   183  		.subtype = type,
   184  		.source = EVENT_SOURCE,
   185  		.hash = hash,
   186  		.arg1 = arg1,
   187  		.arg2 = arg2,
   188  		.arg3 = arg3,
   189  	};
   190  
   191  	skb_event_output(skb, &EVENTS_MAP, BPF_F_CURRENT_CPU, &msg, sizeof(msg));
   192  }
   193  
   194  struct debug_capture_msg {
   195  	NOTIFY_COMMON_HDR
   196  	__u32		len_orig;
   197  	__u32		len_cap;
   198  	__u32		arg1;
   199  	__u32		arg2;
   200  };
   201  
   202  static inline void cilium_dbg_capture2(struct __sk_buff *skb, __u8 type, __u32 arg1, __u32 arg2)
   203  {
   204  	uint64_t skb_len = (uint64_t)skb->len, cap_len = min((uint64_t)TRACE_PAYLOAD_LEN, (uint64_t)skb_len);
   205  	uint32_t hash = get_hash_recalc(skb);
   206  	struct debug_capture_msg msg = {
   207  		.type = CILIUM_NOTIFY_DBG_CAPTURE,
   208  		.subtype = type,
   209  		.source = EVENT_SOURCE,
   210  		.hash = hash,
   211  		.len_orig = skb_len,
   212  		.len_cap = cap_len,
   213  		.arg1 = arg1,
   214  		.arg2 = arg2,
   215  	};
   216  
   217  	skb_event_output(skb, &EVENTS_MAP,
   218  			 (cap_len << 32) | BPF_F_CURRENT_CPU,
   219  			 &msg, sizeof(msg));
   220  }
   221  
   222  static inline void cilium_dbg_capture(struct __sk_buff *skb, __u8 type, __u32 arg1)
   223  {
   224  	cilium_dbg_capture2(skb, type, arg1, 0);
   225  }
   226  
   227  #else
   228  
   229  # define printk(fmt, ...)					\
   230  		do { } while (0)
   231  
   232  static inline void cilium_dbg(struct __sk_buff *skb, __u8 type, __u32 arg1, __u32 arg2)
   233  {
   234  }
   235  
   236  static inline void cilium_dbg3(struct __sk_buff *skb, __u8 type, __u32 arg1,
   237  			       __u32 arg2, __u32 arg3)
   238  {
   239  }
   240  
   241  static inline void cilium_dbg_capture(struct __sk_buff *skb, __u8 type, __u32 arg1)
   242  {
   243  }
   244  
   245  static inline void cilium_dbg_capture2(struct __sk_buff *skb, __u8 type, __u32 arg1, __u32 arg2)
   246  {
   247  }
   248  
   249  #endif
   250  
   251  #endif /* __LIB_DBG__ */