tstest/natlab/vnet: use pcapng

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali 2024-08-08 20:27:23 -07:00
parent f469020776
commit 88f60bd16e
3 changed files with 23 additions and 4 deletions

View File

@ -196,11 +196,14 @@ func (s *Server) initFromConfig(c *Config) error {
if err != nil { if err != nil {
return err return err
} }
nw, err := pcapgo.NewNgWriter(pcf, layers.LinkTypeEthernet)
if err != nil {
return err
}
pw := &pcapWriter{ pw := &pcapWriter{
f: pcf, f: pcf,
w: pcapgo.NewWriter(pcf), w: nw,
} }
pw.w.WriteFileHeader(65536, layers.LinkTypeEthernet)
s.pcapWriter = pw s.pcapWriter = pw
} }
for _, conf := range c.networks { for _, conf := range c.networks {
@ -235,6 +238,12 @@ func (s *Server) initFromConfig(c *Config) error {
id: i + 1, id: i + 1,
net: netOfConf[conf.Network()], net: netOfConf[conf.Network()],
} }
if s.pcapWriter != nil {
s.pcapWriter.w.AddInterface(pcapgo.NgInterface{
Name: fmt.Sprintf("node%d", n.id),
LinkType: layers.LinkTypeEthernet,
})
}
conf.n = n conf.n = n
if _, ok := s.nodeByMAC[n.mac]; ok { if _, ok := s.nodeByMAC[n.mac]; ok {
return fmt.Errorf("two nodes have the same MAC %v", n.mac) return fmt.Errorf("two nodes have the same MAC %v", n.mac)

View File

@ -1,6 +1,7 @@
package vnet package vnet
import ( import (
"io"
"os" "os"
"sync" "sync"
@ -12,15 +13,24 @@ type pcapWriter struct {
f *os.File f *os.File
mu sync.Mutex mu sync.Mutex
w *pcapgo.Writer w *pcapgo.NgWriter
} }
func (p *pcapWriter) WritePacket(ci gopacket.CaptureInfo, data []byte) error { func (p *pcapWriter) WritePacket(ci gopacket.CaptureInfo, data []byte) error {
p.mu.Lock() p.mu.Lock()
defer p.mu.Unlock() defer p.mu.Unlock()
if p.w == nil {
return io.ErrClosedPipe
}
return p.w.WritePacket(ci, data) return p.w.WritePacket(ci, data)
} }
func (p *pcapWriter) Close() error { func (p *pcapWriter) Close() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.w != nil {
p.w.Flush()
p.w = nil
}
return p.f.Close() return p.f.Close()
} }

View File

@ -568,7 +568,7 @@ func New(c *Config) (*Server, error) {
} }
func (s *Server) Close() { func (s *Server) Close() {
if !s.shuttingDown.Swap(true) { if shutdown := s.shuttingDown.Swap(true); !shutdown {
s.shutdownCancel() s.shutdownCancel()
if s.pcapWriter != nil { if s.pcapWriter != nil {
s.pcapWriter.Close() s.pcapWriter.Close()