Skip to content

Commit

Permalink
Set 16 to IFACE column width
Browse files Browse the repository at this point in the history
ifname can be as long as 16 bytes, plust len of ifindex
len(str(1<<32))=10, it should be at least 26.

However, 16 is enough for most of situations, so let's make it 16.

Also center align this column out of aesthetics.

Signed-off-by: gray <gray.liang@isovalent.com>
  • Loading branch information
jschwinger233 authored and brb committed May 28, 2024
1 parent 278ca66 commit db8a495
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions internal/pwru/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"

Expand Down Expand Up @@ -70,6 +71,15 @@ type jsonTuple struct {
Proto uint8 `json:"proto,omitempty"`
}

func centerAlignString(s string, width int) string {
if len(s) >= width {
return s
}
leftPadding := (width - len(s)) / 2
rightPadding := width - len(s) - leftPadding
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", leftPadding), s, strings.Repeat(" ", rightPadding))
}

func NewOutput(flags *Flags, printSkbMap, printShinfoMap, printStackMap *ebpf.Map, addr2Name Addr2Name, kprobeMulti bool, btfSpec *btf.Spec) (*output, error) {
writer := os.Stdout

Expand Down Expand Up @@ -124,7 +134,7 @@ func (o *output) PrintHeader() {
fmt.Fprintf(o.writer, " %-16s", "TIMESTAMP")
}
if o.flags.OutputMeta {
fmt.Fprintf(o.writer, " %-10s %-8s %s %s %s %s", "NETNS", "MARK", "IFACE", "PROTO", "MTU", "LEN")
fmt.Fprintf(o.writer, " %-10s %-8s %16s %s %s %s", "NETNS", "MARK", centerAlignString("IFACE", 16), "PROTO", "MTU", "LEN")
}
if o.flags.OutputTuple {
fmt.Fprintf(o.writer, " %s", "TUPLE")
Expand Down Expand Up @@ -301,9 +311,9 @@ func getShinfoData(event *Event, o *output) (shinfoData string) {
}

func getMetaData(event *Event, o *output) (metaData string) {
metaData = fmt.Sprintf("%10d %08x %s %#04x %d %d",
metaData = fmt.Sprintf("%10d %08x %16s %#04x %d %d",
event.Meta.Netns, event.Meta.Mark,
o.getIfaceName(event.Meta.Netns, event.Meta.Ifindex),
centerAlignString(o.getIfaceName(event.Meta.Netns, event.Meta.Ifindex), 16),
byteorder.NetworkToHost16(event.Meta.Proto), event.Meta.MTU, event.Meta.Len)
return metaData
}
Expand Down Expand Up @@ -386,7 +396,14 @@ func (o *output) Print(event *Event) {
func (o *output) getIfaceName(netnsInode, ifindex uint32) string {
if ifaces, ok := o.ifaceCache[uint64(netnsInode)]; ok {
if name, ok := ifaces[ifindex]; ok {
return fmt.Sprintf("%d(%s)", ifindex, name)
ifname := fmt.Sprintf("%s:%d", name, ifindex)
if len(ifname) > 16 {
ifname = ifname[len(ifname)-16:]
bifname := []byte(ifname)
bifname[0] = '~'
ifname = string(bifname)
}
return ifname
}
}
return fmt.Sprintf("%d", ifindex)
Expand Down

0 comments on commit db8a495

Please sign in to comment.