Skip to content

Commit

Permalink
bpf: trace: identify ifindex 0 as TRACE_IFINDEX_UNKNOWN
Browse files Browse the repository at this point in the history
Self-document what the ifindex 0 actually means, and make it easier to spot
locations that would benefit from additional trace information.

The naming is aligned with TRACE_REASON_UNKNOWN.

Signed-off-by: Julian Wiedmann <jwi@isovalent.com>
  • Loading branch information
julianwiedmann committed May 14, 2024
1 parent db0d41a commit 1a487c3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
9 changes: 5 additions & 4 deletions bpf/bpf_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ handle_netdev(struct __ctx_buff *ctx, const bool from_host)
return send_drop_notify(ctx, sec_label, id, 0, ret,
CTX_ACT_DROP, METRIC_EGRESS);
#else
send_trace_notify(ctx, TRACE_TO_STACK, HOST_ID, 0, 0, 0,
TRACE_REASON_UNKNOWN, 0);
send_trace_notify(ctx, TRACE_TO_STACK, HOST_ID, 0, 0,
TRACE_IFINDEX_UNKNOWN, TRACE_REASON_UNKNOWN, 0);
/* Pass unknown traffic to the stack */
return CTX_ACT_OK;
#endif /* ENABLE_HOST_FIREWALL */
Expand Down Expand Up @@ -1441,7 +1441,8 @@ int cil_to_netdev(struct __ctx_buff *ctx __maybe_unused)
* for tracepoint
*/
send_trace_notify(ctx, TRACE_TO_STACK, 0, 0, 0,
0, TRACE_REASON_ENCRYPT_OVERLAY, 0);
TRACE_IFINDEX_UNKNOWN,
TRACE_REASON_ENCRYPT_OVERLAY, 0);
return ret;
}
if (IS_ERR(ret))
Expand Down Expand Up @@ -1510,7 +1511,7 @@ int cil_to_netdev(struct __ctx_buff *ctx __maybe_unused)
goto drop_err;

send_trace_notify(ctx, TRACE_TO_NETWORK, 0, 0, 0,
0, trace.reason, trace.monitor);
TRACE_IFINDEX_UNKNOWN, trace.reason, trace.monitor);

return ret;

Expand Down
27 changes: 14 additions & 13 deletions bpf/bpf_lxc.c
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ static __always_inline int handle_ipv6_from_lxc(struct __ctx_buff *ctx, __u32 *d
/* Check if this is return traffic to an ingress proxy. */
if (ct_state->proxy_redirect) {
send_trace_notify(ctx, TRACE_TO_PROXY, SECLABEL_IPV6,
0, 0, 0, trace.reason,
0, 0, TRACE_IFINDEX_UNKNOWN, trace.reason,
trace.monitor);
/* Stack will do a socket match and deliver locally. */
return ctx_redirect_to_proxy6(ctx, tuple, 0, false);
Expand Down Expand Up @@ -548,7 +548,7 @@ static __always_inline int handle_ipv6_from_lxc(struct __ctx_buff *ctx, __u32 *d
/* See comment in handle_ipv4_from_lxc(). */
if (ct_state->node_port && lb_is_svc_proto(tuple->nexthdr)) {
send_trace_notify(ctx, TRACE_TO_NETWORK, SECLABEL_IPV6,
*dst_sec_identity, 0, 0,
*dst_sec_identity, 0, TRACE_IFINDEX_UNKNOWN,
trace.reason, trace.monitor);
return tail_call_internal(ctx, CILIUM_CALL_IPV6_NODEPORT_REVNAT,
ext_err);
Expand Down Expand Up @@ -588,7 +588,7 @@ static __always_inline int handle_ipv6_from_lxc(struct __ctx_buff *ctx, __u32 *d
if (!from_l7lb && proxy_port > 0) {
/* Trace the packet before it is forwarded to proxy */
send_trace_notify(ctx, TRACE_TO_PROXY, SECLABEL_IPV6, 0,
bpf_ntohs(proxy_port), 0,
bpf_ntohs(proxy_port), TRACE_IFINDEX_UNKNOWN,
trace.reason, trace.monitor);
return ctx_redirect_to_proxy6(ctx, tuple, proxy_port, false);
}
Expand Down Expand Up @@ -729,8 +729,8 @@ static __always_inline int handle_ipv6_from_lxc(struct __ctx_buff *ctx, __u32 *d
#ifdef TUNNEL_MODE
encrypt_to_stack:
#endif
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, *dst_sec_identity, 0, 0,
trace.reason, trace.monitor);
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, *dst_sec_identity, 0,
TRACE_IFINDEX_UNKNOWN, trace.reason, trace.monitor);

cilium_dbg_capture(ctx, DBG_CAPTURE_DELIVERY, 0);

Expand Down Expand Up @@ -905,7 +905,7 @@ static __always_inline int handle_ipv4_from_lxc(struct __ctx_buff *ctx, __u32 *d
/* Check if this is return traffic to an ingress proxy. */
if (ct_state->proxy_redirect) {
send_trace_notify(ctx, TRACE_TO_PROXY, SECLABEL_IPV4,
0, 0, 0, trace.reason,
0, 0, TRACE_IFINDEX_UNKNOWN, trace.reason,
trace.monitor);
/* Stack will do a socket match and deliver locally. */
return ctx_redirect_to_proxy4(ctx, tuple, 0, false);
Expand Down Expand Up @@ -1015,7 +1015,7 @@ static __always_inline int handle_ipv4_from_lxc(struct __ctx_buff *ctx, __u32 *d
*/
if (ct_state->node_port && lb_is_svc_proto(tuple->nexthdr)) {
send_trace_notify(ctx, TRACE_TO_NETWORK, SECLABEL_IPV4,
*dst_sec_identity, 0, 0,
*dst_sec_identity, 0, TRACE_IFINDEX_UNKNOWN,
trace.reason, trace.monitor);
return tail_call_internal(ctx, CILIUM_CALL_IPV4_NODEPORT_REVNAT,
ext_err);
Expand Down Expand Up @@ -1069,7 +1069,7 @@ static __always_inline int handle_ipv4_from_lxc(struct __ctx_buff *ctx, __u32 *d
if (!from_l7lb && proxy_port > 0) {
/* Trace the packet before it is forwarded to proxy */
send_trace_notify(ctx, TRACE_TO_PROXY, SECLABEL_IPV4, 0,
bpf_ntohs(proxy_port), 0,
bpf_ntohs(proxy_port), TRACE_IFINDEX_UNKNOWN,
trace.reason, trace.monitor);
return ctx_redirect_to_proxy4(ctx, tuple, proxy_port, false);
}
Expand Down Expand Up @@ -1323,8 +1323,8 @@ static __always_inline int handle_ipv4_from_lxc(struct __ctx_buff *ctx, __u32 *d
#if defined(TUNNEL_MODE) || defined(ENABLE_HIGH_SCALE_IPCACHE)
encrypt_to_stack:
#endif
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV4, *dst_sec_identity, 0, 0,
trace.reason, trace.monitor);
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV4, *dst_sec_identity, 0,
TRACE_IFINDEX_UNKNOWN, trace.reason, trace.monitor);
cilium_dbg_capture(ctx, DBG_CAPTURE_DELIVERY, 0);
return CTX_ACT_OK;
}
Expand Down Expand Up @@ -1466,8 +1466,9 @@ int cil_from_container(struct __ctx_buff *ctx)
bpf_clear_meta(ctx);
reset_queue_mapping(ctx);

send_trace_notify(ctx, TRACE_FROM_LXC, sec_label, 0, 0, 0,
TRACE_REASON_UNKNOWN, TRACE_PAYLOAD_LEN);
send_trace_notify(ctx, TRACE_FROM_LXC, sec_label, 0, 0,
TRACE_IFINDEX_UNKNOWN, TRACE_REASON_UNKNOWN,
TRACE_PAYLOAD_LEN);

if (!validate_ethertype(ctx, &proto)) {
ret = DROP_UNSUPPORTED_L2;
Expand Down Expand Up @@ -2365,7 +2366,7 @@ int handle_policy_egress(struct __ctx_buff *ctx)

edt_set_aggregate(ctx, 0); /* do not count this traffic again */
send_trace_notify(ctx, TRACE_FROM_PROXY, SECLABEL, 0, 0,
0 /*ifindex*/,
TRACE_IFINDEX_UNKNOWN,
TRACE_REASON_UNKNOWN, TRACE_PAYLOAD_LEN);

switch (proto) {
Expand Down
6 changes: 4 additions & 2 deletions bpf/lib/nodeport.h
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,8 @@ static __always_inline int nodeport_svc_lb6(struct __ctx_buff *ctx,
return CTX_ACT_OK;

send_trace_notify(ctx, TRACE_TO_PROXY, src_sec_identity, 0,
bpf_ntohs((__u16)svc->l7_lb_proxy_port), 0,
bpf_ntohs((__u16)svc->l7_lb_proxy_port),
TRACE_IFINDEX_UNKNOWN,
TRACE_REASON_POLICY, monitor);
return ctx_redirect_to_proxy_hairpin_ipv6(ctx,
(__be16)svc->l7_lb_proxy_port);
Expand Down Expand Up @@ -2833,7 +2834,8 @@ static __always_inline int nodeport_svc_lb4(struct __ctx_buff *ctx,
return CTX_ACT_OK;

send_trace_notify(ctx, TRACE_TO_PROXY, src_sec_identity, 0,
bpf_ntohs((__u16)svc->l7_lb_proxy_port), 0,
bpf_ntohs((__u16)svc->l7_lb_proxy_port),
TRACE_IFINDEX_UNKNOWN,
TRACE_REASON_POLICY, monitor);
return ctx_redirect_to_proxy_hairpin_ipv4(ctx, ip4,
(__be16)svc->l7_lb_proxy_port);
Expand Down
8 changes: 4 additions & 4 deletions bpf/lib/srv6.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ int tail_srv6_encap(struct __ctx_buff *ctx)
return send_drop_notify_error(ctx, SECLABEL_IPV6, ret, CTX_ACT_DROP,
METRIC_EGRESS);

send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, 0, 0, 0,
TRACE_REASON_SRV6_ENCAP, 0);
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, 0, 0,
TRACE_IFINDEX_UNKNOWN, TRACE_REASON_SRV6_ENCAP, 0);

return ret;
}
Expand All @@ -394,8 +394,8 @@ int tail_srv6_decap(struct __ctx_buff *ctx)
if (ret < 0)
goto error_drop;

send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, 0, 0, 0,
TRACE_REASON_SRV6_DECAP, 0);
send_trace_notify(ctx, TRACE_TO_STACK, SECLABEL_IPV6, 0, 0,
TRACE_IFINDEX_UNKNOWN, TRACE_REASON_SRV6_DECAP, 0);
return CTX_ACT_OK;
error_drop:
return send_drop_notify_error(ctx, SECLABEL_IPV6, ret, CTX_ACT_DROP,
Expand Down
2 changes: 2 additions & 0 deletions bpf/lib/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ enum {
TRACE_AGGREGATE_ACTIVE_CT = 3, /* Ratelimit active connection traces */
};

#define TRACE_IFINDEX_UNKNOWN 0 /* Linux kernel doesn't use ifindex 0 */

#ifndef MONITOR_AGGREGATION
#define MONITOR_AGGREGATION TRACE_AGGREGATE_NONE
#endif
Expand Down

0 comments on commit 1a487c3

Please sign in to comment.