Skip to content

Commit

Permalink
disable packetloss stack and disable flow port by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwlqx committed Apr 2, 2024
1 parent 963a10c commit 3fba67b
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 105 deletions.
2 changes: 2 additions & 0 deletions bpf/headers/inspector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
// +build ignore

#pragma once

#include <bpf/bpf_endian.h>
#include "bpf_core_read.h"
#include "bpf_helpers.h"
Expand Down
9 changes: 7 additions & 2 deletions bpf/packetloss.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct insp_pl_event_t {
};

const struct insp_pl_event_t *unused_insp_pl_event_t __attribute__((unused));
const volatile u8 enable_packetloss_stack = 0;

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
Expand All @@ -42,8 +43,12 @@ int kfree_skb(struct kfree_skb_args *args) {

set_tuple(skb, &event.tuple);
event.location = (u64)args->location;
event.stack_id = bpf_get_stackid(args, &insp_pl_stack,
KERN_STACKID_FLAGS);

if (enable_packetloss_stack){
event.stack_id = bpf_get_stackid(args, &insp_pl_stack,
KERN_STACKID_FLAGS);
}

bpf_perf_event_output(args, &insp_pl_event,
BPF_F_CURRENT_CPU, &event, sizeof(event));

Expand Down
6 changes: 6 additions & 0 deletions deploy/net-exporter-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ metrics:
- name: udp
- name: kernellatency
- name: packetloss
args:
enableStack: false
- name: flow
args:
enablePortInLabel: false
- name: tcpretrans
event:
probes:
- name: biolatency
- name: kernellatency
- name: packetloss
args:
enableStack: false
- name: tcpreset
- name: tcpretrans
sinks:
Expand Down
54 changes: 23 additions & 31 deletions pkg/exporter/probe/flow/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sync"
"syscall"

"github.com/alibaba/kubeskoop/pkg/exporter/nettop"

"github.com/prometheus/client_golang/prometheus"

"github.com/alibaba/kubeskoop/pkg/exporter/probe"
Expand Down Expand Up @@ -43,7 +41,8 @@ func init() {
}

type flowArgs struct {
Dev string `mapstructure:"interface-name"`
Dev string `mapstructure:"interfaceName"`
EnablePortInLabel bool `mapstructure:"enablePortInLabel"`
}

func getDefaultRouteDevice() (netlink.Link, error) {
Expand Down Expand Up @@ -176,7 +175,9 @@ func (h *dynamicLinkFlowHelper) stop() error {
}

func metricsProbeCreator(args flowArgs) (probe.MetricsProbe, error) {
p := &metricsProbe{}
p := &metricsProbe{
enablePort: args.EnablePortInLabel,
}

if args.Dev == "" {
log.Infof("flow: auto detect network device with default route")
Expand Down Expand Up @@ -228,8 +229,9 @@ func metricsProbeCreator(args flowArgs) (probe.MetricsProbe, error) {
}

type metricsProbe struct {
bpfObjs bpfObjects
helper linkFlowHelper
enablePort bool
bpfObjs bpfObjects
helper linkFlowHelper
}

func (p *metricsProbe) Start(_ context.Context) error {
Expand All @@ -252,6 +254,16 @@ func (p *metricsProbe) Stop(_ context.Context) error {
return p.bpfObjs.Close()
}

func toProbeTuple(t *bpfFlowTuple4) *probe.Tuple {
return &probe.Tuple{
Protocol: t.Proto,
Src: bpfutil.GetV4AddrStr(t.Src),
Dst: bpfutil.GetV4AddrStr(t.Dst),
Sport: t.Sport,
Dport: t.Dport,
}
}

func (p *metricsProbe) collectOnce(emit probe.Emit) error {
var values []bpfFlowMetrics
var key bpfFlowTuple4
Expand All @@ -268,33 +280,13 @@ func (p *metricsProbe) collectOnce(emit probe.Emit) error {
val.Packets += values[i].Packets
}

ipInfo := func(ip string) []string {
info := nettop.GetIPInfo(ip)
if info == nil {
return []string{"unknown", "", "", ""}
}

switch info.Type {
case nettop.IPTypeNode:
return []string{"node", info.NodeName, "", ""}
case nettop.IPTypePod:
return []string{"pod", "", info.PodNamespace, info.PodName}
default:
log.Warningf("unknown ip type %s for %s", ip, info.Type)
}
return []string{"unknown", "", "", ""}
tuple := toProbeTuple(&key)
if !p.enablePort {
tuple.Dport = 0
tuple.Sport = 0
}

labels := []string{bpfutil.GetProtoStr(key.Proto)}
srcIP := bpfutil.GetV4AddrStr(key.Src)
labels = append(labels, srcIP)
labels = append(labels, ipInfo(srcIP)...)

dstIP := bpfutil.GetV4AddrStr(key.Dst)
labels = append(labels, dstIP)
labels = append(labels, ipInfo(dstIP)...)
labels = append(labels, fmt.Sprintf("%d", bpfutil.Htons(key.Sport)))
labels = append(labels, fmt.Sprintf("%d", bpfutil.Htons(key.Dport)))
labels := probe.BuildTupleMetricsLabels(tuple)

emit("bytes", labels, float64(val.Bytes))
emit("packets", labels, float64(val.Packets))
Expand Down
11 changes: 7 additions & 4 deletions pkg/exporter/probe/tracepacketloss/bpf_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/exporter/probe/tracepacketloss/bpf_bpfeb.o
Binary file not shown.
11 changes: 7 additions & 4 deletions pkg/exporter/probe/tracepacketloss/bpf_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/exporter/probe/tracepacketloss/bpf_bpfel.o
Binary file not shown.
Loading

0 comments on commit 3fba67b

Please sign in to comment.