Skip to content

Commit

Permalink
Support GNMI native write
Browse files Browse the repository at this point in the history
  • Loading branch information
ganglyu committed Nov 15, 2022
1 parent 5c43eea commit e103b09
Show file tree
Hide file tree
Showing 40 changed files with 4,644 additions and 63 deletions.
26 changes: 23 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ TEST_FILES=$(wildcard *_test.go)
TELEMETRY_TEST_DIR = build/tests/gnmi_server
TELEMETRY_TEST_BIN = $(TELEMETRY_TEST_DIR)/server.test
ifeq ($(ENABLE_TRANSLIB_WRITE),y)
BLD_FLAGS := -tags gnmi_translib_write
BLD_TAGS := gnmi_translib_write
endif
ifeq ($(ENABLE_NATIVE_WRITE),y)
BLD_TAGS := $(BLD_TAGS) gnmi_native_write
endif

ifneq ($(BLD_TAGS),)
BLD_FLAGS := -tags "$(strip $(BLD_TAGS))"
endif

GO_DEPS := vendor/.done
Expand All @@ -41,6 +48,8 @@ $(GO_DEPS): go.mod $(PATCHES)
patch -d vendor -p0 < patches/gnmi_cli.all.patch
patch -d vendor -p0 < patches/gnmi_set.patch
patch -d vendor -p0 < patches/gnmi_get.patch
patch -d vendor -p0 < patches/gnmi_path.patch
patch -d vendor -p0 < patches/gnmi_xpath.patch
git apply patches/0001-Updated-to-filter-and-write-to-file.patch
touch $@

Expand All @@ -49,31 +58,40 @@ go-deps: $(GO_DEPS)
go-deps-clean:
$(RM) -r vendor

sonic-gnmi: $(GO_DEPS)
sonic-gnmi: $(GO_DEPS) libswss
ifeq ($(CROSS_BUILD_ENVIRON),y)
$(GO) build -o ${GOBIN}/telemetry -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/telemetry
$(GO) build -o ${GOBIN}/dialout_client_cli -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/dialout/dialout_client_cli
$(GO) build -o ${GOBIN}/gnmi_get -mod=vendor github.com/jipanyang/gnxi/gnmi_get
$(GO) build -o ${GOBIN}/gnmi_set -mod=vendor github.com/jipanyang/gnxi/gnmi_set
$(GO) build -o ${GOBIN}/gnmi_cli -mod=vendor github.com/openconfig/gnmi/cmd/gnmi_cli
$(GO) build -o ${GOBIN}/gnoi_client -mod=vendor github.com/sonic-net/sonic-gnmi/gnoi_client
$(GO) build -o ${GOBIN}/gnmi_dump -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
else
$(GO) install -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/telemetry
$(GO) install -mod=vendor $(BLD_FLAGS) github.com/sonic-net/sonic-gnmi/dialout/dialout_client_cli
$(GO) install -mod=vendor github.com/jipanyang/gnxi/gnmi_get
$(GO) install -mod=vendor github.com/jipanyang/gnxi/gnmi_set
$(GO) install -mod=vendor github.com/openconfig/gnmi/cmd/gnmi_cli
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnoi_client
$(GO) install -mod=vendor github.com/sonic-net/sonic-gnmi/gnmi_dump
endif

# TODO: Create a new repo for this lib, sonic-restapi and sonic-gnmi can share this lib
libswss:
make -C libcswsscommon
sudo make -C libcswsscommon install

check_gotest:
sudo mkdir -p ${DBDIR}
sudo cp ./testdata/database_config.json ${DBDIR}
sudo mkdir -p /usr/models/yang || true
sudo find $(MGMT_COMMON_DIR)/models -name '*.yang' -exec cp {} /usr/models/yang/ \;
sudo $(GO) test -coverprofile=coverage-config.txt -covermode=atomic -v github.com/sonic-net/sonic-gnmi/sonic_db_config
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server
sudo $(GO) test -coverprofile=coverage-gnmi.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server -coverpkg ../...
sudo $(GO) test -coverprofile=coverage-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo $(GO) test -coverprofile=coverage-data.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_data_client
sudo $(GO) test -coverprofile=coverage-dbus.txt -covermode=atomic -mod=vendor -v github.com/sonic-net/sonic-gnmi/sonic_service_client
$(GO) get github.com/axw/gocov/...
$(GO) get github.com/AlekSi/gocov-xml
gocov convert coverage-*.txt | gocov-xml -source $(shell pwd) > coverage.xml
Expand All @@ -99,6 +117,7 @@ install:
$(INSTALL) -D $(BUILD_DIR)/gnmi_set $(DESTDIR)/usr/sbin/gnmi_set
$(INSTALL) -D $(BUILD_DIR)/gnmi_cli $(DESTDIR)/usr/sbin/gnmi_cli
$(INSTALL) -D $(BUILD_DIR)/gnoi_client $(DESTDIR)/usr/sbin/gnoi_client
$(INSTALL) -D $(BUILD_DIR)/gnmi_dump $(DESTDIR)/usr/sbin/gnmi_dump


deinstall:
Expand All @@ -107,5 +126,6 @@ deinstall:
rm $(DESTDIR)/usr/sbin/gnmi_get
rm $(DESTDIR)/usr/sbin/gnmi_set
rm $(DESTDIR)/usr/sbin/gnoi_client
rm $(DESTDIR)/usr/sbin/gnmi_dump


42 changes: 15 additions & 27 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ stages:
DIFF_COVER_WORKING_DIRECTORY: $(System.DefaultWorkingDirectory)/sonic-gnmi

container:
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-buster:latest
image: sonicdev-microsoft.azurecr.io:443/sonic-slave-bullseye:latest

steps:
- checkout: self
Expand Down Expand Up @@ -79,6 +79,9 @@ stages:
displayName: "Download sonic-mgmt-common"

- script: |
# PYTEST
sudo pip3 install -U pytest
# REDIS
sudo apt-get install -y redis-server
sudo sed -ri 's/^# unixsocket/unixsocket/' /etc/redis/redis.conf
Expand All @@ -87,47 +90,32 @@ stages:
sudo service redis-server start
# LIBYANG
sudo dpkg -i ../target/debs/buster/libyang*1.0.73*.deb
sudo dpkg -i ../target/debs/bullseye/libyang*1.0.73*.deb
displayName: "Install dependency"
- script: |
# LIBSWSSCOMMON
sudo apt-get -y purge libhiredis-dev libnl-3-dev libnl-route-3-dev
sudo dpkg -i ../target/debs/buster/libnl-3-200_*.deb
sudo dpkg -i ../target/debs/buster/libnl-genl-3-200_*.deb
sudo dpkg -i ../target/debs/buster/libnl-route-3-200_*.deb
sudo dpkg -i ../target/debs/buster/libnl-nf-3-200_*.deb
sudo dpkg -i ../target/debs/buster/libhiredis0.14_*.deb
sudo dpkg -i ../target/debs/bullseye/libnl-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libnl-genl-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libnl-route-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libnl-nf-3-200_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis0.14_*.deb
sudo dpkg -i ../target/debs/bullseye/libhiredis-dev_*.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/bullseye/python3-swsscommon_1.0.0_amd64.deb
displayName: "Install libswsscommon dependencies"
- script: |
set -ex
# Install .NET CORE
curl -sSL https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo apt-add-repository https://packages.microsoft.com/debian/10/prod
sudo apt-add-repository https://packages.microsoft.com/debian/11/prod
sudo apt-get update
sudo apt-get install -y dotnet-sdk-5.0
displayName: "Install .NET CORE"
- task: DownloadPipelineArtifact@2
inputs:
source: specific
project: build
pipeline: Azure.sonic-swss-common
artifact: sonic-swss-common
runVersion: 'latestFromBranch'
runBranch: 'refs/heads/master'
displayName: "Download sonic-swss-common"

- script: |
set -ex
# LIBSWSSCOMMON
sudo dpkg -i libswsscommon_1.0.0_amd64.deb
sudo dpkg -i libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i python3-swsscommon_1.0.0_amd64.deb
workingDirectory: $(Pipeline.Workspace)/
displayName: 'Install libswsscommon package'
- script: |
set -ex
ls -l
Expand Down
4 changes: 4 additions & 0 deletions common_utils/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

package common_utils

const GNMI_WORK_PATH = "/tmp"
42 changes: 39 additions & 3 deletions common_utils/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ const requestContextKey contextkey = 0
// Request Id generator
var requestCounter uint64

var CountersName = [...]string {
"GNMI get",
"GNMI get fail",
"GNMI set",
"GNMI set fail",
"GNOI reboot",
"DBUS",
"DBUS fail",
"DBUS apply patch db",
"DBUS apply patch yang",
"DBUS create checkpoint",
"DBUS delete checkpoint",
"DBUS config save",
"DBUS config reload",
}

var globalCounters [len(CountersName)]uint64


// GetContext function returns the RequestContext object for a
// gRPC request. RequestContext is maintained as a context value of
// the request. Creates a new RequestContext object is not already
Expand All @@ -55,8 +74,25 @@ func GetContext(ctx context.Context) (*RequestContext, context.Context) {

func GetUsername(ctx context.Context, username *string) {
rc, _ := GetContext(ctx)
if rc != nil {
*username = rc.Auth.User
}
if rc != nil {
*username = rc.Auth.User
}
}

func InitCounters() {
for i := 0; i < len(CountersName); i++ {
globalCounters[i] = 0
}
SetMemCounters(&globalCounters)
}

func IncCounter(name string) {
for i := 0; i < len(CountersName); i++ {
if CountersName[i] == name {
atomic.AddUint64(&globalCounters[i], 1)
break
}
}
SetMemCounters(&globalCounters)
}

64 changes: 64 additions & 0 deletions common_utils/shareMem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package common_utils

import (
"fmt"
"syscall"
"unsafe"
)

// Use share memory to dump GNMI internal counters,
// GNMI server and gnmi_dump should use memKey to access the share memory,
// memSize is 1024 bytes, so we can support 128 counters
// memMode is 0x380, this value is O_RDWR|IPC_CREAT,
// O_RDWR means: Owner can write and read the file, everyone else can't.
// IPC_CREAT means: Create a shared memory segment if a shared memory identifier does not exist for memKey.
var (
memKey = 7749
memSize = 1024
memMode = 0x380
)

func SetMemCounters(counters *[len(CountersName)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
*(*uint64)(unsafe.Pointer(addr)) = counters[i]
addr += size
}
return nil
}

func GetMemCounters(counters *[len(CountersName)]uint64) error {
shmid, _, err := syscall.Syscall(syscall.SYS_SHMGET, uintptr(memKey), uintptr(memSize), uintptr(memMode))
if int(shmid) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}

shmaddr, _, err := syscall.Syscall(syscall.SYS_SHMAT, shmid, 0, 0)
if int(shmaddr) == -1 {
return fmt.Errorf("syscall error, err: %v\n", err)
}
defer syscall.Syscall(syscall.SYS_SHMDT, shmaddr, 0, 0)

const size = unsafe.Sizeof(uint64(0))
addr := uintptr(unsafe.Pointer(shmaddr))

for i := 0; i < len(counters); i++ {
counters[i] = *(*uint64)(unsafe.Pointer(addr))
addr += size
}
return nil
}

29 changes: 29 additions & 0 deletions gnmi_dump/gnmi_dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"flag"
"fmt"
"github.com/sonic-net/sonic-gnmi/common_utils"
)

const help = `
gnmi_dump is used to dump internal counters for debugging purpose,
including GNMI request counter, GNOI request counter and DBUS request counter.
`

func main() {
flag.Usage = func() {
fmt.Print(help)
}
flag.Parse()
var counters [len(common_utils.CountersName)]uint64
err := common_utils.GetMemCounters(&counters)
if err != nil {
fmt.Printf("Error: Fail to read counters, %v", err)
return
}
fmt.Printf("Dump GNMI counters\n")
for i := 0; i < len(common_utils.CountersName); i++ {
fmt.Printf("%v---%v\n", common_utils.CountersName[i], counters[i])
}
}
5 changes: 5 additions & 0 deletions gnmi_server/constants_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = false
5 changes: 5 additions & 0 deletions gnmi_server/constants_native_write.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build gnmi_native_write

package gnmi

const ENABLE_NATIVE_WRITE = true
10 changes: 5 additions & 5 deletions gnmi_server/constants.go → gnmi_server/constants_translib.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build !gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = false
// +build !gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = false
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = true
// +build gnmi_translib_write

package gnmi

const ENABLE_TRANSLIB_WRITE = true
Loading

0 comments on commit e103b09

Please sign in to comment.