Skip to content

Commit

Permalink
fill all interfaces when init pcapng writer
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Apr 28, 2024
1 parent 24080e1 commit 372a523
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
18 changes: 9 additions & 9 deletions cmd/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ import (
"github.com/mozillazg/ptcpdump/internal/dev"
)

func attachHooks(opts Options) (map[int]dev.Device, *bpf.BPF, error) {
func attachHooks(opts Options) (*bpf.BPF, error) {
devices, err := dev.GetDevices(opts.ifaces)
if err != nil {
return nil, nil, err
return nil, err
}
if err := rlimit.RemoveMemlock(); err != nil {
return devices, nil, err
return nil, err
}
bf, err := bpf.NewBPF()
if err != nil {
return devices, nil, err
return nil, err
}
if err := bf.Load(bpf.NewOptions(opts.pid, opts.comm, opts.followForks, opts.pcapFilter)); err != nil {
return devices, nil, err
return nil, err
}

if err := bf.AttachKprobes(); err != nil {
return devices, bf, err
return bf, err
}
if err := bf.AttachTracepoints(); err != nil {
return devices, bf, err
return bf, err
}
for _, iface := range devices {
if err := bf.AttachTcHooks(iface.Ifindex, opts.DirectionOut(), opts.DirectionIn()); err != nil {
return devices, bf, err
return bf, err
}
}

return devices, bf, nil
return bf, nil
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func run(cmd *cobra.Command, args []string) error {
}()
go pcache.Start()

devices, bf, err := attachHooks(opts)
bf, err := attachHooks(opts)
if err != nil {
if bf != nil {
bf.Close()
Expand All @@ -108,7 +108,7 @@ func run(cmd *cobra.Command, args []string) error {

execConsumer := consumer.NewExecEventConsumer(pcache)
go execConsumer.Start(ctx, execEvensCh)
packetConsumer := consumer.NewPacketEventConsumer(writers, devices)
packetConsumer := consumer.NewPacketEventConsumer(writers)
go func() {
packetConsumer.Start(ctx, packetEvensCh, opts.maxPacketCount)
stop()
Expand Down
5 changes: 3 additions & 2 deletions internal/consumer/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type PacketEventConsumer struct {
devices map[int]dev.Device
}

func NewPacketEventConsumer(writers []writer.PacketWriter, devices map[int]dev.Device) *PacketEventConsumer {
func NewPacketEventConsumer(writers []writer.PacketWriter) *PacketEventConsumer {
devices, _ := dev.GetDevices([]string{})
return &PacketEventConsumer{
writers: writers,
devices: devices,
Expand Down Expand Up @@ -47,7 +48,7 @@ func (c *PacketEventConsumer) parsePacketEvent(pt bpf.BpfPacketEventT) {

for _, w := range c.writers {
if err := w.Write(pevent); err != nil {
log.Printf("[PacketEventConsumer] write packet failed: %s", err)
log.Printf("[PacketEventConsumer] write packet failed: %s, device: %#v", err, pevent.Device)
}
w.Flush()
}
Expand Down
14 changes: 13 additions & 1 deletion internal/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ package dev
import (
"github.com/vishvananda/netlink"
"golang.org/x/xerrors"
"sync"
)

var allLinks []netlink.Link
var once sync.Once

type Device struct {
Name string
Ifindex int
}

func getAllLinks() ([]netlink.Link, error) {
var err error
once.Do(func() {
allLinks, err = netlink.LinkList()
})
return allLinks, err
}

func GetDevices(names []string) (map[int]Device, error) {
var links []netlink.Link
var err error
ifindexMap := make(map[int]Device)

allLinks, err := netlink.LinkList()
allLinks, err := getAllLinks()
if err != nil {
return nil, xerrors.Errorf(": %w", err)
}
Expand Down

0 comments on commit 372a523

Please sign in to comment.