-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add kvisor agent debug command line tool
There is now a new kvisor agent debug CLI tool, that allows to dump the current state of socket to process associations. The idea is that this tool can be executed on the node to give a better insights into how kvisor reports network related metrics. Additionally the eBPF code has been refactored a bit, to better fit having multiple C source locations in the repository.
- Loading branch information
1 parent
426de97
commit 4562e29
Showing
21 changed files
with
1,459 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/castai/kvisor/pkg/ebpftracer/debug" | ||
"github.com/castai/kvisor/pkg/logging" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func NewDebugCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "debug", | ||
} | ||
|
||
cmd.AddCommand(newSocketDebugCommand()) | ||
|
||
return cmd | ||
} | ||
|
||
func newSocketDebugCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "sockets", | ||
} | ||
|
||
var ( | ||
targetPID = cmd.Flags().Uint32("kvisor-pid", 0, "PID of kvisor process to get data from") | ||
) | ||
|
||
cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
log := logging.New(&logging.Config{}) | ||
d, err := debug.New(log, debug.DebugCfg{ | ||
TargetPID: *targetPID, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := d.Load(); err != nil { | ||
return err | ||
} | ||
|
||
sockIter := d.SocketInfoIterator() | ||
|
||
t := table.NewWriter() | ||
|
||
t.AppendHeader(table.Row{"Process", "Process", "Process", "Process", "Socket", "Socket", "Socket", "Socket"}, table.RowConfig{AutoMerge: true}) | ||
t.AppendHeader(table.Row{"Cgroup", "PID", "Host PID", "Comm", "Protocol", "From", "To", "State", "Inode"}) | ||
t.SetAutoIndex(true) | ||
t.SetStyle(table.StyleLight) | ||
t.Style().Options.SeparateRows = true | ||
t.SetColumnConfigs([]table.ColumnConfig{ | ||
{Number: 1, AutoMerge: true}, | ||
{Number: 2, AutoMerge: true}, | ||
}) | ||
|
||
displayWarning := false | ||
|
||
for _, d := range sockIter.Iter() { | ||
if d.Netctx.Taskctx.Pid == 0 { | ||
displayWarning = true | ||
continue | ||
} | ||
|
||
t.AppendRow(table.Row{ | ||
d.Netctx.Taskctx.CgroupId, | ||
d.Netctx.Taskctx.Pid, | ||
d.Netctx.Taskctx.HostPid, | ||
toComm(d.Netctx.Taskctx.Comm[:]), | ||
formatProto(d.SockInfo.Proto), | ||
formatAddr(d.SockInfo.Tuple.Saddr.Raw, d.SockInfo.Tuple.Sport, d.SockInfo.Family), | ||
formatAddr(d.SockInfo.Tuple.Daddr.Raw, d.SockInfo.Tuple.Dport, d.SockInfo.Family), | ||
formatSockState(d.SockInfo.State, d.SockInfo.Proto), | ||
d.SockInfo.Ino, | ||
}) | ||
} | ||
|
||
fmt.Println(t.Render()) | ||
|
||
if displayWarning { | ||
fmt.Fprintln(os.Stderr, "") | ||
fmt.Fprintln(os.Stderr, "warning: some sockets could not be associated with any processes and might be missing") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func formatSockState(state, proto uint8) string { | ||
if proto == unix.IPPROTO_TCP || proto == unix.IPPROTO_UDP { | ||
switch state { | ||
case 1: | ||
return "ESTABLISHED" | ||
case 2: | ||
return "SYN_SENT" | ||
case 3: | ||
return "SYN_RECV" | ||
case 4: | ||
return "FIN_WAIT1" | ||
case 5: | ||
return "FIN_WAIT2" | ||
case 6: | ||
return "TIME_WAIT" | ||
case 7: | ||
return "CLOSE" | ||
case 8: | ||
return "CLOSE_WAIT" | ||
case 9: | ||
return "LAST_ACK" | ||
case 10: | ||
return "LISTEN" | ||
case 11: | ||
return "CLOSING" | ||
case 12: | ||
return "NEW_SYN_RECV" | ||
case 13: | ||
return "MAX_STATES" | ||
} | ||
} | ||
|
||
return strconv.FormatUint(uint64(state), 10) | ||
} | ||
|
||
func formatProto(proto uint8) string { | ||
switch proto { | ||
case unix.IPPROTO_ICMP: | ||
return "ICMP" | ||
case unix.IPPROTO_ICMPV6: | ||
return "ICMP6" | ||
case unix.IPPROTO_TCP: | ||
return "TCP" | ||
case unix.IPPROTO_UDP: | ||
return "UDP" | ||
} | ||
|
||
return strconv.FormatUint(uint64(proto), 10) | ||
} | ||
|
||
func toComm(data []int8) string { | ||
result := make([]byte, len(data)) | ||
b := 0 | ||
for i, v := range data { | ||
if v == 0 { | ||
b = i | ||
break | ||
} | ||
result[i] = byte(v) | ||
} | ||
|
||
return string(result[:b]) | ||
} | ||
|
||
func formatAddr(addr [16]byte, port uint16, family uint16) string { | ||
ip := formatIP(addr, family) | ||
return fmt.Sprintf("%s:%d", ip, port) | ||
} | ||
|
||
func formatIP(addr [16]byte, family uint16) string { | ||
switch family { | ||
case unix.AF_INET: | ||
addr, _ := netip.AddrFromSlice(addr[:4]) | ||
return addr.String() | ||
case unix.AF_INET6: | ||
addr := netip.AddrFrom16(addr) | ||
return addr.String() | ||
} | ||
|
||
return "<unknown>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.