Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ipfix buffer use and mirror test #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{})
errCh = 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{}{}
errCh <- struct{}{}
} else {
t.Fatal("unexpected error", err)
}
}
}()

time.Sleep(2 * time.Second)
select {
case <-errCh:
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 <-errCh:
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