Skip to content

Commit

Permalink
1. Sampling Application maintains Context information using 3 tuple (…
Browse files Browse the repository at this point in the history
…src ip, dst ip, protocol) of a receiving packet

2. if a port receives multiple packets with the same 3-tuple assume they are
part of same context and increment count.
3. if the packet count of same context greater than 15, drop the packer
otherwise forward the packet
4. 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.
  • Loading branch information
Vijaya Rani committed Apr 27, 2023
1 parent f86286d commit 4f35858
Show file tree
Hide file tree
Showing 7 changed files with 789 additions and 1 deletion.
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
}

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

0 comments on commit 4f35858

Please sign in to comment.