From cca4dcfd5d97438304f469906a2eefa11aed1171 Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Alaydrus Date: Fri, 26 Jan 2024 20:24:30 +0700 Subject: [PATCH 1/4] fix mpls label decoding --- producer/proto/producer_nf.go | 8 ++++++-- producer/proto/producer_test.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/producer/proto/producer_nf.go b/producer/proto/producer_nf.go index 53cd9e96..bad74daf 100644 --- a/producer/proto/producer_nf.go +++ b/producer/proto/producer_nf.go @@ -518,7 +518,9 @@ func ConvertNetFlowDataSet(flowMessage *ProtoProducerMessage, version uint16, ba return err } if len(flowMessage.MplsLabel) < 2 { - flowMessage.MplsLabel = make([]uint32, 2) + tmpLabels := make([]uint32, 2) + copy(tmpLabels, flowMessage.MplsLabel) + flowMessage.MplsLabel = tmpLabels } flowMessage.MplsLabel[1] = uint32(mplsLabel >> 4) case netflow.IPFIX_FIELD_mplsLabelStackSection3: @@ -527,7 +529,9 @@ func ConvertNetFlowDataSet(flowMessage *ProtoProducerMessage, version uint16, ba return err } if len(flowMessage.MplsLabel) < 3 { - flowMessage.MplsLabel = make([]uint32, 3) + tmpLabels := make([]uint32, 3) + copy(tmpLabels, flowMessage.MplsLabel) + flowMessage.MplsLabel = tmpLabels } flowMessage.MplsLabel[2] = uint32(mplsLabel >> 4) case netflow.IPFIX_FIELD_mplsTopLabelIPv4Address: diff --git a/producer/proto/producer_test.go b/producer/proto/producer_test.go index 0d009f3f..9a55deb0 100644 --- a/producer/proto/producer_test.go +++ b/producer/proto/producer_test.go @@ -16,6 +16,26 @@ func TestProcessMessageNetFlow(t *testing.T) { Type: netflow.NFV9_FIELD_IPV4_SRC_ADDR, Value: []byte{10, 0, 0, 1}, }, + netflow.DataField{ + Type: netflow.NFV9_FIELD_FIRST_SWITCHED, + // 218432176 + Value: []byte{0x0d, 0x05, 0x02, 0xb0}, + }, + netflow.DataField{ + Type: netflow.NFV9_FIELD_LAST_SWITCHED, + // 218432192 + Value: []byte{0x0d, 0x05, 0x02, 0xc0}, + }, + netflow.DataField{ + Type: netflow.NFV9_FIELD_MPLS_LABEL_1, + // 24041 + Value: []byte{0x05, 0xde, 0x94}, + }, + netflow.DataField{ + Type: netflow.NFV9_FIELD_MPLS_LABEL_2, + // 211992 + Value: []byte{0x33, 0xc1, 0x85}, + }, }, }, } @@ -29,8 +49,15 @@ func TestProcessMessageNetFlow(t *testing.T) { FlowSets: dfs, } testsr := &SingleSamplingRateSystem{1} - _, err := ProcessMessageNetFlowV9Config(&pktnf9, testsr, nil) - assert.Nil(t, err) + msgs, err := ProcessMessageNetFlowV9Config(&pktnf9, testsr, nil) + if assert.Nil(t, err) && assert.Len(t, msgs, 1) { + msg, ok := msgs[0].(*ProtoProducerMessage) + if assert.True(t, ok) { + assert.Equal(t, msg.TimeFlowStartNs, uint64(1705732882176000000)) + assert.Equal(t, msg.TimeFlowEndNs, uint64(1705732882192000000)) + assert.Equal(t, msg.MplsLabel, []uint32{24041, 211992}) + } + } pktipfix := netflow.IPFIXPacket{ FlowSets: dfs, From 7bfec8996c7e0ee9edbbc66b0e07f99643e0ca42 Mon Sep 17 00:00:00 2001 From: Muhammad Iqbal Alaydrus Date: Fri, 26 Jan 2024 20:29:40 +0700 Subject: [PATCH 2/4] update mpls label test case to cover 3 stacks --- producer/proto/producer_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/producer/proto/producer_test.go b/producer/proto/producer_test.go index 9a55deb0..1587b84e 100644 --- a/producer/proto/producer_test.go +++ b/producer/proto/producer_test.go @@ -36,6 +36,11 @@ func TestProcessMessageNetFlow(t *testing.T) { // 211992 Value: []byte{0x33, 0xc1, 0x85}, }, + netflow.DataField{ + Type: netflow.NFV9_FIELD_MPLS_LABEL_3, + // 48675 + Value: []byte{0x0b, 0xe2, 0x35}, + }, }, }, } @@ -55,7 +60,7 @@ func TestProcessMessageNetFlow(t *testing.T) { if assert.True(t, ok) { assert.Equal(t, msg.TimeFlowStartNs, uint64(1705732882176000000)) assert.Equal(t, msg.TimeFlowEndNs, uint64(1705732882192000000)) - assert.Equal(t, msg.MplsLabel, []uint32{24041, 211992}) + assert.Equal(t, msg.MplsLabel, []uint32{24041, 211992, 48675}) } } From b5ab2e57456c297fc9213cb94440e73500edca96 Mon Sep 17 00:00:00 2001 From: lspgn Date: Sun, 18 Aug 2024 19:49:33 -0700 Subject: [PATCH 3/4] fix test --- producer/proto/producer_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/producer/proto/producer_test.go b/producer/proto/producer_test.go index 1587b84e..87ae88dd 100644 --- a/producer/proto/producer_test.go +++ b/producer/proto/producer_test.go @@ -58,9 +58,9 @@ func TestProcessMessageNetFlow(t *testing.T) { if assert.Nil(t, err) && assert.Len(t, msgs, 1) { msg, ok := msgs[0].(*ProtoProducerMessage) if assert.True(t, ok) { - assert.Equal(t, msg.TimeFlowStartNs, uint64(1705732882176000000)) - assert.Equal(t, msg.TimeFlowEndNs, uint64(1705732882192000000)) - assert.Equal(t, msg.MplsLabel, []uint32{24041, 211992, 48675}) + assert.Equal(t, uint64(218432176*1e6), msg.TimeFlowStartNs) + assert.Equal(t, uint64(218432192*1e6), msg.TimeFlowEndNs) + assert.Equal(t, []uint32{24041, 211992, 48675}, msg.MplsLabel) } } From af1a9a5cb8f910bf95b99e866b14db1ca985a99b Mon Sep 17 00:00:00 2001 From: lspgn Date: Sun, 18 Aug 2024 20:02:27 -0700 Subject: [PATCH 4/4] fix test --- producer/proto/producer_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/producer/proto/producer_test.go b/producer/proto/producer_test.go index 87ae88dd..bbd14d5a 100644 --- a/producer/proto/producer_test.go +++ b/producer/proto/producer_test.go @@ -51,15 +51,17 @@ func TestProcessMessageNetFlow(t *testing.T) { } pktnf9 := netflow.NFv9Packet{ - FlowSets: dfs, + SystemUptime: 218432000, + UnixSeconds: 1705732882, + FlowSets: dfs, } testsr := &SingleSamplingRateSystem{1} msgs, err := ProcessMessageNetFlowV9Config(&pktnf9, testsr, nil) if assert.Nil(t, err) && assert.Len(t, msgs, 1) { msg, ok := msgs[0].(*ProtoProducerMessage) if assert.True(t, ok) { - assert.Equal(t, uint64(218432176*1e6), msg.TimeFlowStartNs) - assert.Equal(t, uint64(218432192*1e6), msg.TimeFlowEndNs) + assert.Equal(t, uint64(1705732882176*1e6), msg.TimeFlowStartNs) + assert.Equal(t, uint64(1705732882192*1e6), msg.TimeFlowEndNs) assert.Equal(t, []uint32{24041, 211992, 48675}, msg.MplsLabel) } }