From d860f712040f5b532cf32e72e2552dba7a349b67 Mon Sep 17 00:00:00 2001 From: Mark Sanders Date: Wed, 3 Jul 2024 13:47:16 -0500 Subject: [PATCH] feat: add support for existing interfaces Signed-off-by: Mark Sanders --- nwgate/cmd/main.go | 22 +++++------ nwgate/go.mod | 26 ++++++------ nwgate/go.sum | 47 +++++++++++----------- nwgate/pkg/dpudev/Interface.go | 72 ++++++++++++++++++++++++++++++++-- nwgate/pkg/dpudev/dpudev.go | 6 ++- nwgate/pkg/dpudev/validate.go | 4 ++ nwgate/pkg/utils/pagination.go | 4 ++ 7 files changed, 128 insertions(+), 53 deletions(-) create mode 100644 nwgate/pkg/dpudev/validate.go create mode 100644 nwgate/pkg/utils/pagination.go diff --git a/nwgate/cmd/main.go b/nwgate/cmd/main.go index 24fa282..576f4db 100644 --- a/nwgate/cmd/main.go +++ b/nwgate/cmd/main.go @@ -16,17 +16,20 @@ import ( "google.golang.org/grpc/reflection" ) -var ( - port = flag.Int("port", 50153, "Server Port") -) - func main() { + var grpcPort int + flag.IntVar(&grpcPort, "grpc_port", 50153, "The gRPC server port") + + var httpPort int + flag.IntVar(&httpPort, "http_port", 8082, "The HTTP server port") + flag.Parse() - // ServAddr := os.Args[1] - // ServAddr += fmt.Sprintf(":%d", *port) - fmt.Println("Starting grpcServer on port", fmt.Sprintf(":%d", *port)) - lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) + // 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 { log.Fatalf("listen failed: %v", err) } @@ -36,9 +39,6 @@ func main() { inv.RegisterInventoryServiceServer(grpcServer, opiServ) reflection.Register(grpcServer) - // Initialize the Network Device Information - dpudev.InitNetworkData() - log.Printf("Server listening at %v", lis.Addr()) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("server failed: %v", err) diff --git a/nwgate/go.mod b/nwgate/go.mod index a811a33..c285eb4 100644 --- a/nwgate/go.mod +++ b/nwgate/go.mod @@ -1,33 +1,33 @@ module github.com/sandersms/Protos/nwgate -go 1.20 +go 1.21 + +toolchain go1.22.0 require ( github.com/jaypipes/ghw v0.12.0 - github.com/opiproject/opi-api v0.0.0-20240304222410-5dba226aaa9e - github.com/sandersms/Protos v0.0.0-20240402185342-73ec586259dc + github.com/opiproject/opi-api v0.0.0-20240702145202-9920eb170a07 github.com/vishvananda/netlink v1.1.0 - google.golang.org/grpc v1.62.1 + golang.org/x/sys v0.21.0 + google.golang.org/grpc v1.65.0 ) require ( github.com/StackExchange/wmi v1.2.1 // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/go-ole/go-ole v1.3.0 // indirect - github.com/golang/protobuf v1.5.4 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect - github.com/jaypipes/pcidb v1.0.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect + github.com/jaypipes/pcidb v1.0.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect 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.22.0 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect - google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect - google.golang.org/protobuf v1.33.0 // indirect + golang.org/x/net v0.26.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 gopkg.in/yaml.v2 v2.4.0 // indirect howett.net/plist v1.0.1 // indirect ) diff --git a/nwgate/go.sum b/nwgate/go.sum index 6ec8441..31e5fc6 100644 --- a/nwgate/go.sum +++ b/nwgate/go.sum @@ -6,53 +6,52 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= -github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= -github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho= github.com/jaypipes/ghw v0.12.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= -github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= -github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLRCuNDfk= +github.com/jaypipes/pcidb v1.0.1 h1:WB2zh27T3nwg8AE8ei81sNRb9yWBii3JGNJtT7K9Oic= +github.com/jaypipes/pcidb v1.0.1/go.mod h1:6xYUz/yYEyOkIkUt2t2J2folIuZ4Yg6uByCGFXMCeE4= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 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-20240304222410-5dba226aaa9e h1:jUa7DmVLjzLKg051y7rYyCD0NAbEHZQMimM1451D74I= -github.com/opiproject/opi-api v0.0.0-20240304222410-5dba226aaa9e/go.mod h1:92pv4ulvvPMuxCJ9ND3aYbmBfEMLx0VCjpkiR7ZTqPY= +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/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= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= -github.com/sandersms/Protos v0.0.0-20240402185342-73ec586259dc h1:VJKNwIKYl44bQEYdjcfu2JF39aYmpIyYcfyUVIlWlmA= -github.com/sandersms/Protos v0.0.0-20240402185342-73ec586259dc/go.mod h1:4ZjAC2TCW1y+u1C2D+TN5mn5DgMIk7uPtusn7LlOwTk= github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0= github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= 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.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= -golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= 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.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= -golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda h1:b6F6WIV4xHHD0FA4oIyzU6mHWg2WI2X1RBehwa5QN38= -google.golang.org/genproto/googleapis/api v0.0.0-20240401170217-c3f982113cda/go.mod h1:AHcE/gZH76Bk/ROZhQphlRoWo5xKDEtz3eVEO1LfA8c= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda h1:LI5DOvAxUPMv/50agcLLoo+AdWc1irS9Rzz4vPuD1V4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= -google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= -google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= -google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= -google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +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/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/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= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= 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= diff --git a/nwgate/pkg/dpudev/Interface.go b/nwgate/pkg/dpudev/Interface.go index a4ee51c..ead3b8a 100644 --- a/nwgate/pkg/dpudev/Interface.go +++ b/nwgate/pkg/dpudev/Interface.go @@ -8,22 +8,75 @@ import ( "fmt" "log" + "github.com/sandersms/Protos/nwgate/pkg/utils" "github.com/vishvananda/netlink" + "golang.org/x/sys/unix" - ipb "github.com/sandersms/Protos/Experiment/network/opencfg/v1alpha1/gen/go" + ipb "github.com/opiproject/opi-api/network/opinetcommon/v1alpha1/gen/go" ) +type Ifports struct { + name string + ifindex int + iftype string + mtu int + loopback bool + enabled bool + st_admin bool + st_oper string + logical bool +} + // InitNetworkData initialize the network device information func InitNetworkData() { // Get the list of links on the device - nifs, err := netlink.LinkList() + net_intfs, err := netlink.LinkList() if err != nil { panic(err) } + //Initialize the datastore + Intfs := make(map[int]*Ifports) + // Output the parameters retrieved for the list of links - for _, intf := range nifs { + var idx int = 0 + for _, intf := range net_intfs { + fmt.Println("type", intf.Type()) + fmt.Println("Index", intf.Attrs().Index) fmt.Println("Name", intf.Attrs().Name) + fmt.Println("Flags", intf.Attrs().Flags) + fmt.Println("MTU", intf.Attrs().MTU) + fmt.Println("OperState", intf.Attrs().OperState) + fmt.Println("Raw Attributes", intf.Attrs()) + if intf.Attrs().RawFlags&unix.IFF_LOOPBACK != 0 { + fmt.Println("Loopback interface") + } + 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 + } + 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 + } + //fmt.Println("NetInterface:", netif) + Intfs[idx] = netif + // increment index + 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) } } @@ -36,5 +89,18 @@ func (s *Server) GetNetInterface(_ context.Context, in *ipb.GetNetInterfaceReque // ListNetInterfaces provides a list of the network interfaces 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 + if err := s.validateListNetInterfacesRequest(in); err != nil { + log.Printf("ListNetInterfaces(): validation failure: %v", err) + return nil, err + } + + // fetch pagination from the database, calculate size and offset + size, offset, err := utils.ExtractPagination(in.PageSize, in.PageToken, s.Pagination) + if err != nil { + return nil, err + } + return &ipb.ListNetInterfacesResponse{}, nil } diff --git a/nwgate/pkg/dpudev/dpudev.go b/nwgate/pkg/dpudev/dpudev.go index 0075e6b..3a9c7f4 100644 --- a/nwgate/pkg/dpudev/dpudev.go +++ b/nwgate/pkg/dpudev/dpudev.go @@ -12,13 +12,15 @@ import ( // The Server Object type Server struct { pb.UnimplementedInventoryServiceServer - Dpus map[string]*pb.Inventory + Pagination map[string]int + Dpus map[string]*pb.Inventory } // NewServer creates the initialized instance of the Dpu Device server func NewServer() *Server { fmt.Println("Creating DPU Device Server") return &Server{ - Dpus: make(map[string]*pb.Inventory), + Pagination: make(map[string]int), + Dpus: make(map[string]*pb.Inventory), } } diff --git a/nwgate/pkg/dpudev/validate.go b/nwgate/pkg/dpudev/validate.go new file mode 100644 index 0000000..74ca9e4 --- /dev/null +++ b/nwgate/pkg/dpudev/validate.go @@ -0,0 +1,4 @@ +// Copyright (c) xxxx, . + +// Package dpudev (DPU Device) is the main package of the application +package dpudev diff --git a/nwgate/pkg/utils/pagination.go b/nwgate/pkg/utils/pagination.go new file mode 100644 index 0000000..6b42fea --- /dev/null +++ b/nwgate/pkg/utils/pagination.go @@ -0,0 +1,4 @@ +// Copyright (c) xxxx, . + +// Package dpudev (DPU Device) is the main package of the application +package utils