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

Implement draft-ietf-opsawg-ipfix-on-path-telemetry #21

Merged
merged 12 commits into from
Nov 13, 2023
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ Fluvia Exporter is licensed under the [MIT license](https://en.wikipedia.org/wik
For the full license text, see [LICENSE](https://github.com/nttcom/fluvia/blob/master/LICENSE).

## Miscellaneous
Fluvia Exporter supports the following IETF Internet-Drafts:
- [Export of Segment Routing over IPv6 Information in IP Flow Information Export (IPFIX)](https://datatracker.ietf.org/doc/html/draft-ietf-opsawg-ipfix-srv6-srh-14)
- IPFIX Library: Supports all IEs.
- IPFIX Exporter: Implemented the following IEs.
Fluvia Exporter supports the following IEs:
- packetDeltaCount
- [draft-ietf-opsawg-ipfix-srv6-srh](https://datatracker.ietf.org/doc/draft-ietf-opsawg-ipfix-srv6-srh/)
- srhActiveSegmentIPv6
- srhSegmentsIPv6Left
- srhFlagsIPv6
- srhTagIPv6
- srhSegmentIPv6BasicList
- [draft-ietf-opsawg-ipfix-on-path-telemetry](https://datatracker.ietf.org/doc/draft-ietf-opsawg-ipfix-on-path-telemetry/)
- PathDelayMeanDeltaMicroseconds
- PathDelayMaxDeltaMicroseconds
- PathDelayMinDeltaMicroseconds
- PathDelaySumDeltaMicroseconds
7 changes: 6 additions & 1 deletion cmd/fluvia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ func main() {
ingressIfName = c.Ipfix.IngressInterface
}

client.New(ingressIfName, raddr)
interval := c.Ipfix.Interval
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the readme could understand how to use this option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote how to configure interval in getting-started.md

if interval <= 0 {
interval = 1
}

client.New(ingressIfName, raddr, interval)
}
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ go 1.20
require (
github.com/cilium/ebpf v0.11.0
github.com/google/gopacket v1.1.19
github.com/pkg/errors v0.9.1
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.10.0
gopkg.in/yaml.v3 v3.0.1
)

require (
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/sys v0.10.0 // indirect
)
require golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -17,6 +15,7 @@ golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPI
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Ipfix struct {
Address string `yaml:"address"`
Port string `yaml:"port"`
IngressInterface string `yaml:"ingress-interface"`
Interval int `yaml:"interval"`
}

type Config struct {
Expand Down
87 changes: 87 additions & 0 deletions internal/pkg/meter/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package meter

import (
"errors"
"fmt"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)

const MAX_SEGMENTLIST_ENTRIES = 10

type ProbeData struct {
H_source string
H_dest string
V6Srcaddr string
V6Dstaddr string
NextHdr uint8
HdrExtLen uint8
RoutingType uint8
SegmentsLeft uint8
LastEntry uint8
Flags uint8
Tag uint16
Segments [MAX_SEGMENTLIST_ENTRIES]string
}

func Parse(data []byte) (*ProbeData, error) {
var pd ProbeData
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.Default)

ethLayer := packet.Layer(layers.LayerTypeEthernet)
eth, ok := ethLayer.(*layers.Ethernet)
if !ok {
return nil, errors.New("Could not parse a packet with Ethernet")
}

pd.H_dest = eth.DstMAC.String()
pd.H_source = eth.SrcMAC.String()

ipv6Layer := packet.Layer(layers.LayerTypeIPv6)
ipv6, ok := ipv6Layer.(*layers.IPv6)
if !ok {
return nil, errors.New("Could not parse a packet with IPv6")
}

pd.V6Srcaddr = ipv6.SrcIP.String()
pd.V6Dstaddr = ipv6.DstIP.String()

if ipv6.NextHeader != layers.IPProtocolIPv6HopByHop {
return nil, errors.New(fmt.Sprintf("Next header is not IPv6 hop-by-hop(0): %d", ipv6.NextHeader))

Check failure on line 51 in internal/pkg/meter/parse.go

View workflow job for this annotation

GitHub Actions / lint

S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
}

ipv6HBHLayer := packet.Layer(layers.LayerTypeIPv6HopByHop)
hbh, ok := ipv6HBHLayer.(*layers.IPv6HopByHop)
if !ok {
return nil, errors.New("Could not parse a packet with ipv6 hop-by-hop option")
}

if hbh.NextHeader != layers.IPProtocolIPv6Routing {
return nil, errors.New(fmt.Sprintf("Next header is not SRv6: %d", hbh.NextHeader))

Check failure on line 61 in internal/pkg/meter/parse.go

View workflow job for this annotation

GitHub Actions / lint

S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple)
}

packet = gopacket.NewPacket(ipv6HBHLayer.LayerPayload(), Srv6LayerType, gopacket.Lazy)
srv6Layer := packet.Layer(Srv6LayerType)
srv6, ok := srv6Layer.(*Srv6Layer)
if !ok {
return nil, errors.New("Could not parse a packet with SRv6")
}

pd.NextHdr = srv6.NextHeader
pd.HdrExtLen = srv6.HdrExtLen
pd.RoutingType = srv6.RoutingType
pd.SegmentsLeft = srv6.SegmentsLeft
pd.LastEntry = srv6.LastEntry
pd.Flags = srv6.Flags
pd.Tag = srv6.Tag

for idx := 0; idx < MAX_SEGMENTLIST_ENTRIES; idx++ {
if idx >= len(srv6.Segments) {
break
}
pd.Segments[idx] = srv6.Segments[idx].String()
}

return &pd, nil
}
14 changes: 8 additions & 6 deletions pkg/bpf/srv6.go → internal/pkg/meter/srv6.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package bpf
package meter

import (
"encoding/binary"
"errors"
"net"
"net/netip"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
Expand All @@ -18,7 +18,7 @@ type Srv6Layer struct {
LastEntry uint8
Flags uint8
Tag uint16
Segments []net.IP
Segments []netip.Addr
}

var Srv6LayerType = gopacket.RegisterLayerType(
Expand Down Expand Up @@ -50,10 +50,11 @@ func (i *Srv6Layer) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) err
startBit := 8 + 16*j
endBit := 24 + 16*j
var addr []byte
for k := endBit; k >= startBit; k-- {
for k := startBit; k < endBit; k++ {
addr = append(addr, data[k])
}
i.Segments = append(i.Segments, addr)
seg, _ := netip.AddrFromSlice(addr[:16])
i.Segments = append(i.Segments, seg)
}
i.BaseLayer = layers.BaseLayer{
Contents: data[:8],
Expand All @@ -76,7 +77,8 @@ func (i *Srv6Layer) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.Serial
bytes[5] = i.Flags
binary.BigEndian.PutUint16(bytes[6:], i.Tag)

for i2, address := range i.Segments {
for i2, seg := range i.Segments {
address := seg.AsSlice()
lsb := binary.BigEndian.Uint64(address[:8])
msb := binary.BigEndian.Uint64(address[8:])
binary.BigEndian.PutUint64(bytes[8+16*i2:], lsb)
Expand Down
88 changes: 54 additions & 34 deletions pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 NTT Communications Corporation

Check failure on line 1 in pkg/bpf/bpf.go

View workflow job for this annotation

GitHub Actions / lint

: # github.com/nttcom/fluvia/pkg/bpf [github.com/nttcom/fluvia/pkg/bpf.test]
// Copyright (c) 2023 Takeru Hayasaka
//
// This software is released under the MIT License.
Expand All @@ -7,45 +7,78 @@
package bpf

import (
"fmt"
"errors"
"net"

"github.com/cilium/ebpf"
"github.com/pkg/errors"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc $BPF_CLANG -cflags $BPF_CFLAGS xdp ../../src/main.c -- -I../../src

type XdpProbeData struct {
H_dest [6]uint8
H_source [6]uint8
H_proto uint16
_ [2]byte
V6Srcaddr struct{ In6U struct{ U6Addr8 [16]uint8 } }
V6Dstaddr struct{ In6U struct{ U6Addr8 [16]uint8 } }
NextHdr uint8
HdrExtLen uint8
RoutingType uint8
SegmentsLeft uint8
LastEntry uint8
Flags uint8
Tag uint16
Segments [10]struct{ In6U struct{ U6Addr8 [16]uint8 } }
type XdpMetaData struct {
ReceivedNano uint64
SentSec uint32
SentSubsec uint32
}

func ReadXdpObjects(ops *ebpf.CollectionOptions) (*xdpObjects, error) {
type Xdp struct {
objs *xdpObjects
link link.Link
}

func ReadXdpObjects(ops *ebpf.CollectionOptions) (*Xdp, error) {
obj := &xdpObjects{}
err := loadXdpObjects(obj, ops)
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}

// TODO: BPF log level remove hardcoding. yaml in config
if err != nil {
return nil, errors.WithStack(err)
return nil, err
}

return &Xdp{
objs: obj,
}, nil
}

func (x *Xdp) Attach(iface *net.Interface) error {
l, err := link.AttachXDP(link.XDPOptions{
Program: x.objs.XdpProg,
Interface: iface.Index,
Flags: link.XDPGenericMode,
})
if err != nil {
return err
}

return obj, nil
x.link = l

return nil
}

func (x *Xdp) NewPerfReader() (*perf.Reader, error) {
return perf.NewReader(x.objs.PacketProbePerf, 4096)
}

func (x *Xdp) Close() error {
errs := []error{}
if err := x.objs.Close(); err != nil {
errs = append(errs, err)
}

if err := x.link.Close(); err != nil {
errs = append(errs, err)
}

if len(errs) > 0 {
return errors.Join(errs...)
}

return nil
}

const (
Expand All @@ -55,16 +88,3 @@
XDP_TX
XDP_REDIRECT
)

func PrintEntrys(entry XdpProbeData, count uint64) {
mac := func(mac [6]uint8) string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])
}
saddr := net.IP(entry.V6Srcaddr.In6U.U6Addr8[:]).String()
daddr := net.IP(entry.V6Dstaddr.In6U.U6Addr8[:]).String()

fmt.Printf(
"H_dest: %s, H_source: %v, H_proto: %v, V6Dstaddr: %v, V6Srcaddr: %v -> count: %v\n",
mac(entry.H_dest), mac(entry.H_source), entry.H_proto, daddr, saddr, count)

}
6 changes: 3 additions & 3 deletions pkg/bpf/xdp_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/bpf/xdp_bpfeb.o
Binary file not shown.
6 changes: 3 additions & 3 deletions pkg/bpf/xdp_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pkg/bpf/xdp_bpfel.o
Binary file not shown.
Loading
Loading