Skip to content

Commit

Permalink
Support configurable decoding mode in reference collector program (#396)
Browse files Browse the repository at this point in the history
By introducing the "--decoding-mode" flag.

LenientKeepUnknown can be used in order to accept templates with unknown
IEs. Note that these IEs will be skipped when serializing data records.

Signed-off-by: Antonin Bas <[email protected]>
  • Loading branch information
antoninbas authored Jan 11, 2025
1 parent 78fd999 commit 41d217c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ golangci-fix: $(GOLANGCI_LINT_BIN)
.PHONY: collector
collector:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) build -o $(BINDIR) github.com/vmware/go-ipfix/cmd/collector/
$(GO) build -o $(BINDIR) github.com/vmware/go-ipfix/cmd/collector/

.PHONY: consumer
consumer:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) build -o $(BINDIR) github.com/vmware/go-ipfix/cmd/consumer/
$(GO) build -o $(BINDIR) github.com/vmware/go-ipfix/cmd/consumer/

### Docker images ###

Expand Down
20 changes: 20 additions & 0 deletions cmd/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ package main
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"slices"
"strconv"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -48,10 +51,13 @@ var (
IPFIXAddr string
IPFIXPort uint16
IPFIXTransport string
decodingMode string
flowRecords []string
mutex sync.Mutex

flowTextSeparator = bytes.Repeat([]byte("="), 80)

validDecodingModes = []string{string(collector.DecodingModeStrict), string(collector.DecodingModeLenientKeepUnknown), string(collector.DecodingModeLenientDropUnknown)}
)

type jsonResponse struct {
Expand All @@ -72,6 +78,7 @@ func addIPFIXFlags(fs *pflag.FlagSet) {
fs.StringVar(&IPFIXAddr, "ipfix.addr", "0.0.0.0", "IPFIX collector address")
fs.Uint16Var(&IPFIXPort, "ipfix.port", 4739, "IPFIX collector port")
fs.StringVar(&IPFIXTransport, "ipfix.transport", "tcp", "IPFIX collector transport layer")
fs.StringVar(&decodingMode, "decoding-mode", string(collector.DecodingModeStrict), fmt.Sprintf("Decoding mode, must be one of %v", strings.Join(validDecodingModes, ",")))
}

func addIPFIXMessage(msg *entities.Message) {
Expand All @@ -97,7 +104,16 @@ func addIPFIXMessage(msg *entities.Message) {
fmt.Fprintf(&buf, " DATA RECORD-%d:\n", i)
for _, ie := range record.GetOrderedElementList() {
elem := ie.GetInfoElement()
// elem.Name can be empty if the decoding mode is DecodingModeLenientKeepUnknown.
if elem.Name == "" {
// Log this with default verbosity, since this collector is
// primarily meant for development and testing.
klog.InfoS("Skipping unknown information element", "enterpriseID", elem.EnterpriseId, "elementID", elem.ElementId)
continue
}
switch elem.DataType {
case entities.OctetArray:
fmt.Fprintf(&buf, " %s: %s \n", elem.Name, hex.EncodeToString(ie.GetOctetArrayValue()))
case entities.Unsigned8:
fmt.Fprintf(&buf, " %s: %v \n", elem.Name, ie.GetUnsigned8Value())
case entities.Unsigned16:
Expand Down Expand Up @@ -170,6 +186,9 @@ func run() error {
// Load the IPFIX global registry
registry.LoadRegistry()
// Initialize collecting process
if !slices.Contains(validDecodingModes, decodingMode) {
return fmt.Errorf("not a valid decoding mode: %s", decodingMode)
}
cpInput := collector.CollectorInput{
Address: IPFIXAddr + ":" + strconv.Itoa(int(IPFIXPort)),
Protocol: IPFIXTransport,
Expand All @@ -178,6 +197,7 @@ func run() error {
IsEncrypted: false,
ServerCert: nil,
ServerKey: nil,
DecodingMode: collector.DecodingMode(decodingMode),
}
cp, err := collector.InitCollectingProcess(cpInput)
if err != nil {
Expand Down

0 comments on commit 41d217c

Please sign in to comment.