Skip to content

Commit

Permalink
refactor(nwgate): Update for new protobufs and interface data gathering
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sanders <marksanders194@gmail.com>
  • Loading branch information
sandersms committed Jul 22, 2024
1 parent d860f71 commit 0bd8e48
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 46 deletions.
2 changes: 1 addition & 1 deletion nwgate/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ build:

get:
@echo " > Checking if there are any missing dependencies..."
@CGO_ENABLED=0 go get -u ./cmd
@CGO_ENABLED=0 go get -u ./...
@CGO_ENABLED=0 go mod tidy
6 changes: 3 additions & 3 deletions nwgate/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func main() {

flag.Parse()

// Initialize the Network Device Information
dpudev.InitNetworkData()

fmt.Println("Starting grpcServer on port", fmt.Sprintf(":%d", grpcPort))
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort))
if err != nil {
Expand All @@ -36,6 +33,9 @@ func main() {
grpcServer := grpc.NewServer()
opiServ := dpudev.NewServer()

// Initialize the Network Device Information
dpudev.InitNetworkData(opiServ)

inv.RegisterInventoryServiceServer(grpcServer, opiServ)
reflection.Register(grpcServer)

Expand Down
13 changes: 7 additions & 6 deletions nwgate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ toolchain go1.22.0

require (
github.com/jaypipes/ghw v0.12.0
github.com/opiproject/opi-api v0.0.0-20240702145202-9920eb170a07
github.com/opiproject/opi-api v0.0.0-20240722141955-49f6e67a6639
github.com/vishvananda/netlink v1.1.0
golang.org/x/sys v0.21.0
go.einride.tech/aip v0.68.0
golang.org/x/sys v0.22.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
Expand All @@ -23,11 +25,10 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
howett.net/plist v1.0.1 // indirect
)
26 changes: 16 additions & 10 deletions nwgate/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/opiproject/opi-api v0.0.0-20240702145202-9920eb170a07 h1:9EUOCyGW3TGh+hJmDntP7oRFw1IfD3wOARfqoUnuBBs=
github.com/opiproject/opi-api v0.0.0-20240702145202-9920eb170a07/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
github.com/opiproject/opi-api v0.0.0-20240722141955-49f6e67a6639 h1:CRxkgchlTMwd/QMEmnKNhZQYMFiia9n7jHOOSB7rEFM=
github.com/opiproject/opi-api v0.0.0-20240722141955-49f6e67a6639/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY=
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.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
Expand All @@ -32,19 +32,23 @@ github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYp
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
go.einride.tech/aip v0.68.0 h1:4seM66oLzTpz50u4K1zlJyOXQ3tCzcJN7I22tKkjipw=
go.einride.tech/aip v0.68.0/go.mod h1:7y9FF8VtPWqpxuAxl0KQWqaULxW4zFIesD6zF5RIHHg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 h1:0+ozOGcrp+Y8Aq8TLNN2Aliibms5LEzsq99ZZmAGYm0=
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094/go.mod h1:fJ/e3If/Q67Mj99hin0hMhiNyCRmt6BQ2aWIJshUSJw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d h1:/hmn0Ku5kWij/kjGsrcJeC1T/MrJi2iNWwgAqrihFwc=
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d/go.mod h1:FfBgJBJg9GcpPvKIuHSZ/aE1g2ecGL74upMzGZjiGEY=
google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade h1:WxZOF2yayUHpHSbUE6NMzumUzBxYc3YGwo0YHnbzsJY=
google.golang.org/genproto/googleapis/api v0.0.0-20240722135656-d784300faade/go.mod h1:mw8MG/Qz5wfgYr6VqVCiZcHe/GJEfI+oGGDCohaVgB0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade h1:oCRSWfwGXQsqlVdErcyTt4A93Y8fo0/9D4b1gnI++qo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240722135656-d784300faade/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand All @@ -55,5 +59,7 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
howett.net/plist v1.0.1 h1:37GdZ8tP09Q35o9ych3ehygcsL+HqKSwzctveSlarvM=
howett.net/plist v1.0.1/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
80 changes: 55 additions & 25 deletions nwgate/pkg/dpudev/Interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,15 @@ type Ifports struct {
}

// InitNetworkData initialize the network device information
func InitNetworkData() {
func InitNetworkData(s *Server) {
// Get the list of links on the device
net_intfs, err := netlink.LinkList()

Check failure on line 33 in nwgate/pkg/dpudev/Interface.go

View workflow job for this annotation

GitHub Actions / golangci

var-naming: don't use underscores in Go names; var net_intfs should be netIntfs (revive)
if err != nil {
panic(err)
}

//Initialize the datastore
Intfs := make(map[int]*Ifports)

// Output the parameters retrieved for the list of links
var idx int = 0
var ifloop bool = false

Check failure on line 39 in nwgate/pkg/dpudev/Interface.go

View workflow job for this annotation

GitHub Actions / golangci

var-declaration: should omit type bool from declaration of var ifloop; it will be inferred from the right-hand side (revive)
for _, intf := range net_intfs {
fmt.Println("type", intf.Type())
fmt.Println("Index", intf.Attrs().Index)
Expand All @@ -50,33 +47,63 @@ func InitNetworkData() {
fmt.Println("Raw Attributes", intf.Attrs())
if intf.Attrs().RawFlags&unix.IFF_LOOPBACK != 0 {
fmt.Println("Loopback interface")
ifloop = true
}
fmt.Println("Encap Type", intf.Attrs().EncapType)

fmt.Println("----------")
// check for an actual device and not loopback interface
if intf.Type() == "device" && intf.Attrs().EncapType != "loopback" {
netif := new(Ifports)
// Add the information to the Interface structure for the fixed interfaces
netif.name = intf.Attrs().Name
netif.ifindex = intf.Attrs().Index
netif.iftype = intf.Attrs().EncapType
netif.mtu = intf.Attrs().MTU
if intf.Attrs().RawFlags&unix.IFF_LOOPBACK != 0 {
netif.loopback = true
// check for an actual device and ethernet interface
if intf.Type() == "device" && intf.Attrs().EncapType == "ether" {
// check if the device exists
netif, ok := s.Intfs[intf.Attrs().Name]
if ok {
log.Printf("Already existing interface %v", intf.Attrs().Name)
fmt.Println("Interface %v exists", netif)
}
// Create the device in the list
fmt.Println("Create the physical device in the list")

Netintf := ipb.NetInterface{}
Netintf.Config = &ipb.NetInterfaceConfig{
Name: intf.Attrs().Name,
Type: ipb.InterfaceType_INTERFACE_TYPE_ETHERNET,
Mtu: uint32(intf.Attrs().MTU),
LoopbackMode: ifloop,
}
netif.st_oper = fmt.Sprint(intf.Attrs().OperState)
//netif.st_oper = fmt.Println(intf.Attrs().OperState)
if intf.Attrs().RawFlags&unix.IFF_UP != 0 {
netif.st_admin = true
Netintf.State = &ipb.NetInterfaceState{
Name: intf.Attrs().Name,
Type: ipb.InterfaceType_INTERFACE_TYPE_ETHERNET,
Mtu: uint32(intf.Attrs().MTU),
LoopbackMode: ifloop,
}

// add the interface to the detected set by adding to the list.
s.Intfs[intf.Attrs().Name] = &ipb.NetInterface{
Name: intf.Attrs().Name,
Config: Netintf.Config,
State: Netintf.State,
}

// netif := new(s.Intfs)
// Add the information to the Interface structure for the fixed interfaces
// netif.name = intf.Attrs().Name
// netif.ifindex = intf.Attrs().Index
// netif.iftype = intf.Attrs().EncapType
// netif.mtu = intf.Attrs().MTU
// if intf.Attrs().RawFlags&unix.IFF_LOOPBACK != 0 {
// netif.loopback = true
// }
// netif.st_oper = fmt.Sprint(intf.Attrs().OperState)
// if intf.Attrs().RawFlags&unix.IFF_UP != 0 {
// netif.st_admin = true
// }
//fmt.Println("NetInterface:", netif)

Check failure on line 99 in nwgate/pkg/dpudev/Interface.go

View workflow job for this annotation

GitHub Actions / golangci

commentFormatting: put a space between `//` and comment text (gocritic)
Intfs[idx] = netif
// Intfs[idx] = netif
// increment index
idx++
// idx++
}
}
for i := range Intfs {
fmt.Println(Intfs[i].name, Intfs[i].iftype, Intfs[i].mtu, Intfs[i].loopback, Intfs[i].ifindex, Intfs[i].st_admin, Intfs[i].st_oper)
for _, Intfs := range s.Intfs {
fmt.Println(Intfs.Name, Intfs.State.Name, Intfs.State.Type, Intfs.State.Mtu, Intfs.State.LoopbackMode)
}
}

Expand All @@ -90,7 +117,7 @@ func (s *Server) GetNetInterface(_ context.Context, in *ipb.GetNetInterfaceReque
func (s *Server) ListNetInterfaces(_ context.Context, in *ipb.ListNetInterfacesRequest) (*ipb.ListNetInterfacesResponse, error) {
log.Printf("ListNetInterface: Received from client %v", in)

// TODO Check any required fields
// Check any required fields
if err := s.validateListNetInterfacesRequest(in); err != nil {
log.Printf("ListNetInterfaces(): validation failure: %v", err)
return nil, err
Expand All @@ -102,5 +129,8 @@ func (s *Server) ListNetInterfaces(_ context.Context, in *ipb.ListNetInterfacesR
return nil, err
}

// check the pagination for the response
log.Printf("Limiting result len(%d) to [%d:%d]", len(s.Intfs), offset, size)

return &ipb.ListNetInterfacesResponse{}, nil
}
3 changes: 3 additions & 0 deletions nwgate/pkg/dpudev/dpudev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"fmt"

pb "github.com/opiproject/opi-api/inventory/v1/gen/go"
ipb "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go"
)

// The Server Object
type Server struct {
pb.UnimplementedInventoryServiceServer
Pagination map[string]int
Dpus map[string]*pb.Inventory
Intfs map[string]*ipb.NetInterface
}

// NewServer creates the initialized instance of the Dpu Device server
Expand All @@ -22,5 +24,6 @@ func NewServer() *Server {
return &Server{
Pagination: make(map[string]int),
Dpus: make(map[string]*pb.Inventory),
Intfs: make(map[string]*ipb.NetInterface),
}
}
15 changes: 15 additions & 0 deletions nwgate/pkg/dpudev/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@

// Package dpudev (DPU Device) is the main package of the application
package dpudev

import (

"go.einride.tech/aip/fieldbehavior"

ipb "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go"
)

func (s *Server) validateListNetInterfacesRequest(in *ipb.ListNetInterfacesRequest) error {
// check required fields
if err := fieldbehavior.ValidateRequiredFields(in); err != nil {
return err
}
return nil
}
38 changes: 37 additions & 1 deletion nwgate/pkg/utils/pagination.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
// Copyright (c) xxxx, .

// Package dpudev (DPU Device) is the main package of the application
// Package utils is support functions of the application
package utils

import (
"log"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// ExtractPagination fetches pagination from the database, calculate size and offset
func ExtractPagination(pageSize int32, pageToken string, pagination map[string]int) (size int, offset int, err error) {
const (
maxPageSize = 250
defaultPageSize = 50
)
switch {
case pageSize < 0:
return -1, -1, status.Error(codes.InvalidArgument, "negative PageSize is not allowed")
case pageSize == 0:
size = defaultPageSize
case pageSize > maxPageSize:
size = maxPageSize
default:
size = int(pageSize)
}
// fetch offset from the database using opaque token
offset = 0
if pageToken != "" {
var ok bool
offset, ok = pagination[pageToken]
if !ok {
return -1, -1, status.Errorf(codes.NotFound, "unable to find pagination token %s", pageToken)
}
log.Printf("Found offset %d from pagination token: %s", offset, pageToken)
}
return size, offset, nil
}
13 changes: 13 additions & 0 deletions nwgate/pkg/utils/proto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) xxxx, .

// Package utils is support functions of the application
package utils

import (
"google.golang.org/protobuf/proto"
)

// ProtoClone creates a deep copy of a provided gRPC structure
func ProtoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

0 comments on commit 0bd8e48

Please sign in to comment.