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

Go Sampling Application #2

Merged
merged 1 commit into from
Apr 28, 2023
Merged
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
43 changes: 42 additions & 1 deletion lang/go/bindings/cne/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import "C"
import (
"fmt"
"unsafe"
"encoding/binary"
"hash/fnv"
)

type Packet C.pktmbuf_t // Packet is the interface type for C.pktmbuf_t structure
Expand Down Expand Up @@ -227,8 +229,10 @@ func GetIPv4(pkt *Packet) *IPv4Hdr {

if pkt != nil {
ether := GetEtherHdr(pkt)

//fmt.Println("ethernet Header", ether)
//fmt.Println("Packet Info", pkt)
if ether != nil && ether.EtherType == SwapUint16(EtherTypeIPV4) {
//fmt.Printf("etherType: %v %02x", ether, ether.EtherType)
return (*IPv4Hdr)((unsafe.Pointer)(uintptr(unsafe.Pointer(ether)) + uintptr(EtherHdrLen)))
}
}
Expand Down Expand Up @@ -285,3 +289,40 @@ func GetTCP(pkt *Packet) *TCPHdr {

return nil
}

// GetHash will calculate with 3 tuples (src ip, dst ip and protocol)
func GetHash(pkt *Packet) uint32 {
var t3Tuple []byte
ipv4 := GetIPv4(pkt)
ipv6 := GetIPv6(pkt)
if ipv4 != nil {
fmt.Println("ipv4 present")
t3Tuple = make([]byte, 9)
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, uint32(ipv4.SrcAddr))
t3Tuple = append(t3Tuple, b...)

binary.LittleEndian.PutUint32(b, uint32(ipv4.DstAddr))
t3Tuple = append(t3Tuple, b...)

t3Tuple = append(t3Tuple, ipv4.NextProtoID)
var i int
for ;i<9;i++ {
fmt.Printf("%02x", t3Tuple[i])
}
} else if ipv6 != nil {
fmt.Println("ipv6present")
t3Tuple = make([]byte, IPv6AddrLen+IPv6AddrLen+1)
copy(t3Tuple[:], ipv6.SrcAddr[:IPv6AddrLen])
copy(t3Tuple[IPv6AddrLen:], ipv6.DstAddr[:IPv6AddrLen])
t3Tuple = append(t3Tuple, ipv6.Proto)
}
var hash uint32
if ipv4 != nil || ipv6 != nil {
h := fnv.New32a()
h.Write(t3Tuple)
hash = h.Sum32()
}
return hash
}

17 changes: 17 additions & 0 deletions lang/go/bindings/examples/sampling/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Brief Description about Sampling Go Apllication
1. Go Sampling Application maintains Context information using 3 tuples (src ip,
dst ip,
protocol)
of a receiving packet.
One of the pieces of information included in this context is the "sampling count,"
which indicates the number of packets that have been forwarded using this particular context.
2. if a port receives multiple packets with the same 3-tuple assume they are
part of the same context and increment the "sampling count".
3. maximum limit on the number of packets that can be forwarded using a particular context, and that limit is 15 packets.
4. if the packet count of the same context is greater than 15, drop the packet otherwise forward the packet
5. Overall, using a 3-tuple to maintain context information can be a useful tool for managing network traffic and
ensuring that packets are delivered to their intended destinations.

NOTE: This Sampling Application works only in lb mode
Command to run in lb mode: ./run_sampling -c sampling.jsnoc -test lb

31 changes: 31 additions & 0 deletions lang/go/bindings/examples/sampling/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module github.com/CloudNativeDataPlane/cndp/lang/go/bindings/examples/sampling

replace github.com/CloudNativeDataPlane/cndp/lang/go/bindings/cne => ../../cne

replace github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/ttylog => ../../../tools/pkgs/ttylog

replace github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/etimers => ../../../tools/pkgs/etimers

replace github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/colorize => ../../../tools/pkgs/colorize

go 1.18

require (
github.com/CloudNativeDataPlane/cndp/lang/go/bindings/cne v0.0.0-00010101000000-000000000000
github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/colorize v0.0.0-00010101000000-000000000000
github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/etimers v0.0.0-00010101000000-000000000000
github.com/CloudNativeDataPlane/cndp/lang/go/tools/pkgs/ttylog v0.0.0-20220730140151-855a3ef0ebc1
github.com/gdamore/tcell/v2 v2.5.2
github.com/rivo/tview v0.0.0-20220731115447-9d32d269593e
golang.org/x/text v0.3.8
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.3.1 // indirect
github.com/tidwall/jsonc v0.3.2 // indirect
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
)
30 changes: 30 additions & 0 deletions lang/go/bindings/examples/sampling/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/gdamore/tcell/v2 v2.5.2 h1:tKzG29kO9p2V++3oBY2W9zUjYu7IK1MENFeY/BzJSVY=
github.com/gdamore/tcell/v2 v2.5.2/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/tview v0.0.0-20220731115447-9d32d269593e h1:jWL4QylzqJQNk+iO3gkzbm433Akmn7nbRUEPrFU0SRA=
github.com/rivo/tview v0.0.0-20220731115447-9d32d269593e/go.mod h1:/Ve2+D+tGMTMNAlGXKCIX9ZeX2InzODYHotmtKZUUVk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.3.1 h1:SDPP7SHNl1L7KrEFCSJslJ/DM9DT02Nq2C61XrfHMmk=
github.com/rivo/uniseg v0.3.1/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/tidwall/jsonc v0.3.2 h1:ZTKrmejRlAJYdn0kcaFqRAKlxxFIC21pYq8vLa4p2Wc=
github.com/tidwall/jsonc v0.3.2/go.mod h1:dw+3CIxqHi+t8eFSpzzMlcVYxKp08UP5CD8/uSFCyJE=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
25 changes: 25 additions & 0 deletions lang/go/bindings/examples/sampling/run_sampling
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

cdir=$(pwd)
PROJECT_PATH="${cdir}/../../../../.."

cmdstring="sudo -E LD_LIBRARY_PATH=$PROJECT_PATH/usr/local/lib/x86_64-linux-gnu ./sampling $*"
go env -w CGO_LDFLAGS_ALLOW='-Wl,--(?:no-)?whole-archive'

go mod tidy
rc=$?
if [[ $rc -ne 0 ]]; then
echo "Go tidy failed"
exit $rc
fi

go build
rc=$?
if [[ $rc -ne 0 ]]; then
echo "Go build failed"
exit $rc
fi

$cmdstring

stty sane
Binary file added lang/go/bindings/examples/sampling/sampling
Binary file not shown.
Loading