diff --git a/tstest/natlab/vnet/conf.go b/tstest/natlab/vnet/conf.go index 090800f84..4c8810998 100644 --- a/tstest/natlab/vnet/conf.go +++ b/tstest/natlab/vnet/conf.go @@ -196,11 +196,14 @@ func (s *Server) initFromConfig(c *Config) error { if err != nil { return err } + nw, err := pcapgo.NewNgWriter(pcf, layers.LinkTypeEthernet) + if err != nil { + return err + } pw := &pcapWriter{ f: pcf, - w: pcapgo.NewWriter(pcf), + w: nw, } - pw.w.WriteFileHeader(65536, layers.LinkTypeEthernet) s.pcapWriter = pw } for _, conf := range c.networks { @@ -235,6 +238,12 @@ func (s *Server) initFromConfig(c *Config) error { id: i + 1, 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 if _, ok := s.nodeByMAC[n.mac]; ok { return fmt.Errorf("two nodes have the same MAC %v", n.mac) diff --git a/tstest/natlab/vnet/pcap.go b/tstest/natlab/vnet/pcap.go index 6ac616e86..eb8c2ed3c 100644 --- a/tstest/natlab/vnet/pcap.go +++ b/tstest/natlab/vnet/pcap.go @@ -1,6 +1,7 @@ package vnet import ( + "io" "os" "sync" @@ -12,15 +13,24 @@ type pcapWriter struct { f *os.File mu sync.Mutex - w *pcapgo.Writer + w *pcapgo.NgWriter } func (p *pcapWriter) WritePacket(ci gopacket.CaptureInfo, data []byte) error { p.mu.Lock() defer p.mu.Unlock() + if p.w == nil { + return io.ErrClosedPipe + } return p.w.WritePacket(ci, data) } 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() } diff --git a/tstest/natlab/vnet/vnet.go b/tstest/natlab/vnet/vnet.go index beeb8da0e..8fe2a9ec1 100644 --- a/tstest/natlab/vnet/vnet.go +++ b/tstest/natlab/vnet/vnet.go @@ -568,7 +568,7 @@ func New(c *Config) (*Server, error) { } func (s *Server) Close() { - if !s.shuttingDown.Swap(true) { + if shutdown := s.shuttingDown.Swap(true); !shutdown { s.shutdownCancel() if s.pcapWriter != nil { s.pcapWriter.Close()