Skip to content

Commit

Permalink
fix(producer): mpls labels in NetFlow (#345)
Browse files Browse the repository at this point in the history
Co-authored-by: Muhammad Iqbal Alaydrus <iqbal@bangunindo.com>
  • Loading branch information
lspgn and iqbalaydrus authored Aug 19, 2024
1 parent 2f454c9 commit b01b1aa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
8 changes: 6 additions & 2 deletions producer/proto/producer_nf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
40 changes: 37 additions & 3 deletions producer/proto/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ 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},
},
netflow.DataField{
Type: netflow.NFV9_FIELD_MPLS_LABEL_3,
// 48675
Value: []byte{0x0b, 0xe2, 0x35},
},
},
},
}
Expand All @@ -26,11 +51,20 @@ func TestProcessMessageNetFlow(t *testing.T) {
}

pktnf9 := netflow.NFv9Packet{
FlowSets: dfs,
SystemUptime: 218432000,
UnixSeconds: 1705732882,
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, uint64(1705732882176*1e6), msg.TimeFlowStartNs)
assert.Equal(t, uint64(1705732882192*1e6), msg.TimeFlowEndNs)
assert.Equal(t, []uint32{24041, 211992, 48675}, msg.MplsLabel)
}
}

pktipfix := netflow.IPFIXPacket{
FlowSets: dfs,
Expand Down

0 comments on commit b01b1aa

Please sign in to comment.