Skip to content

Commit

Permalink
Support new gnmi config interface in telemetry container.
Browse files Browse the repository at this point in the history
Signed-off-by: Gang Lv ganglv@microsoft.com
  • Loading branch information
ganglyu committed Aug 10, 2022
1 parent 92428da commit e29e651
Show file tree
Hide file tree
Showing 40 changed files with 4,689 additions and 23 deletions.
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ endif
GO_DEPS := vendor/.done
PATCHES := $(wildcard patches/*.patch)
PATCHES += $(shell find $(MGMT_COMMON_DIR)/patches -type f)
UNIT_TEST := $(shell if sudo [ -e /var/run/redis/redis.sock ]; then echo "exist"; else echo "noexist"; fi)

all: sonic-gnmi $(TELEMETRY_TEST_BIN)

Expand All @@ -41,42 +42,55 @@ $(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/path.patch
touch $@

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:
ifeq ("$(UNIT_TEST)", "exist")
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 -tags telemetry_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-dialcout.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/dialout/dialout_client
sudo $(GO) test -tags config_test -coverprofile=coverage-gnmi-config.txt -covermode=atomic -mod=vendor $(BLD_FLAGS) -v github.com/sonic-net/sonic-gnmi/gnmi_server
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
rm -rf coverage-*.txt
endif

clean:
$(RM) -r build
Expand All @@ -85,7 +99,7 @@ clean:
$(TELEMETRY_TEST_BIN): $(TEST_FILES) $(SRC_FILES)
mkdir -p $(@D)
cp -r testdata $(@D)/
$(GO) test -mod=vendor $(BLD_FLAGS) -c -cover github.com/sonic-net/sonic-gnmi/gnmi_server -o $@
$(GO) test -mod=vendor -tags telemetry_test $(BLD_FLAGS) -c -cover github.com/sonic-net/sonic-gnmi/gnmi_server -o $@

install:
$(INSTALL) -D $(BUILD_DIR)/telemetry $(DESTDIR)/usr/sbin/telemetry
Expand All @@ -94,6 +108,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 @@ -102,5 +117,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


15 changes: 15 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -79,6 +82,18 @@ stages:
# LIBYANG
sudo dpkg -i ../target/debs/buster/libyang*1.0.73*.deb
# SWSSCOMMON
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/buster/libhiredis-dev_*.deb
sudo dpkg -i ../target/debs/buster/libswsscommon_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/buster/libswsscommon-dev_1.0.0_amd64.deb
sudo dpkg -i ../target/debs/buster/python3-swsscommon_1.0.0_amd64.deb
displayName: "Install dependency"
- script: |
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 = "/etc/sonic/gnmi"
38 changes: 37 additions & 1 deletion 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 @@ -54,9 +73,26 @@ func GetContext(ctx context.Context) (*RequestContext, context.Context) {
}

func GetUsername(ctx context.Context, username *string) {
rc, _ := GetContext(ctx)
rc, _ := GetContext(ctx)
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])
}
}
Loading

0 comments on commit e29e651

Please sign in to comment.