Skip to content

Commit

Permalink
Fix ipfix buffer use and mirror test
Browse files Browse the repository at this point in the history
  • Loading branch information
themiron committed Mar 31, 2024
1 parent dc5bb67 commit 3d411eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 23 additions & 7 deletions vflow/ipfix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

func init() {
opts = &Options{}
opts = NewOptions()
}

func TestMirrorIPFIX(t *testing.T) {
Expand All @@ -39,21 +39,27 @@ func TestMirrorIPFIX(t *testing.T) {
fb = make(chan IPFIXUDPMsg)
dst = net.ParseIP("127.0.0.1")
ready = make(chan struct{})
error = make(chan struct{})
)

go func() {
err := mirrorIPFIX(dst, 10024, msg)
if err != nil {
if strings.Contains(err.Error(), "not permitted") {
t.Log(err)
ready <- struct{}{}
error <- struct{}{}
} else {
t.Fatal("unexpected error", err)
}
}
}()

time.Sleep(2 * time.Second)
select {
case <-error:
return
case <-time.After(2 * time.Second):
break
}

go func() {
b := make([]byte, 1500)
Expand Down Expand Up @@ -86,9 +92,11 @@ func TestMirrorIPFIX(t *testing.T) {

}()

_, ok := <-ready
if ok {
return
select {
case <-ready:
break
case <-time.After(2 * time.Second):
t.Fatal("timeout")
}

body := []byte("hello")
Expand All @@ -100,7 +108,15 @@ func TestMirrorIPFIX(t *testing.T) {
},
}

feedback := <-fb
var feedback IPFIXUDPMsg
select {
case feedback = <-fb:
break
case <-error:
return
case <-time.After(2 * time.Second):
t.Fatal("timeout")
}

if string(feedback.body) != "hello" {
t.Error("expect body is hello, got", string(feedback.body))
Expand Down
4 changes: 3 additions & 1 deletion vflow/ipfix_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func mirrorIPFIX(dst net.IP, port int, ch chan IPFIXUDPMsg) error {
copy(packet[ipHLen:ipHLen+8], udpHdr)
copy(packet[ipHLen+8:], msg.body)

ipfixBuffer.Put(msg.body[:opts.IPFIXUDPSize])
if cap(msg.body) >= opts.IPFIXUDPSize {
ipfixBuffer.Put(msg.body[:opts.IPFIXUDPSize])
}

if err = conn.Send(packet[0 : ipHLen+8+pLen]); err != nil {
return err
Expand Down

0 comments on commit 3d411eb

Please sign in to comment.