aboutsummaryrefslogtreecommitdiffstats
path: root/proxy/server.go
diff options
context:
space:
mode:
authorOndrej Fabry <ofabry@cisco.com>2019-10-08 14:49:16 +0200
committerOndrej Fabry <ofabry@cisco.com>2019-10-10 11:30:49 +0200
commit454aff1907d87c79ffb11e151e25953241c7f1be (patch)
tree07b2287a252f5b981de4fd0b53a7b207fb24a9a0 /proxy/server.go
parent809b69ea4a90455445c34bbad7d8e5fea5cf3462 (diff)
Introduce proxy for VPP
- proxy server defines RPC service for proxying binapi/stats to VPP - use cmd/vpp-proxy for proxy server/client Change-Id: I6e698e166ecf6db7109ae5adf8a93f308d3f3f2a Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'proxy/server.go')
-rw-r--r--proxy/server.go109
1 files changed, 109 insertions, 0 deletions
diff --git a/proxy/server.go b/proxy/server.go
new file mode 100644
index 0000000..df62356
--- /dev/null
+++ b/proxy/server.go
@@ -0,0 +1,109 @@
+package proxy
+
+import (
+ "fmt"
+ "log"
+ "reflect"
+
+ "git.fd.io/govpp.git/api"
+)
+
+type StatsRequest struct {
+ StatsType string
+}
+
+type StatsResponse struct {
+ SysStats *api.SystemStats
+ NodeStats *api.NodeStats
+ IfaceStats *api.InterfaceStats
+ ErrStats *api.ErrorStats
+ BufStats *api.BufferStats
+}
+
+// StatsRPC is a RPC server for proxying client request to api.StatsProvider.
+type StatsRPC struct {
+ stats api.StatsProvider
+}
+
+// NewStatsRPC returns new StatsRPC to be used as RPC server
+// proxying request to given api.StatsProvider.
+func NewStatsRPC(stats api.StatsProvider) *StatsRPC {
+ return &StatsRPC{stats: stats}
+}
+
+func (s *StatsRPC) GetStats(req StatsRequest, resp *StatsResponse) error {
+ log.Printf("StatsRPC.GetStats - REQ: %+v", req)
+
+ switch req.StatsType {
+ case "system":
+ resp.SysStats = new(api.SystemStats)
+ return s.stats.GetSystemStats(resp.SysStats)
+ case "node":
+ resp.NodeStats = new(api.NodeStats)
+ return s.stats.GetNodeStats(resp.NodeStats)
+ case "interface":
+ resp.IfaceStats = new(api.InterfaceStats)
+ return s.stats.GetInterfaceStats(resp.IfaceStats)
+ case "error":
+ resp.ErrStats = new(api.ErrorStats)
+ return s.stats.GetErrorStats(resp.ErrStats)
+ case "buffer":
+ resp.BufStats = new(api.BufferStats)
+ return s.stats.GetBufferStats(resp.BufStats)
+ default:
+ return fmt.Errorf("unknown stats type: %s", req.StatsType)
+ }
+}
+
+type BinapiRequest struct {
+ Msg api.Message
+ IsMulti bool
+ ReplyMsg api.Message
+}
+
+type BinapiResponse struct {
+ Msg api.Message
+ Msgs []api.Message
+}
+
+// BinapiRPC is a RPC server for proxying client request to api.Channel.
+type BinapiRPC struct {
+ binapi api.Channel
+}
+
+// NewBinapiRPC returns new BinapiRPC to be used as RPC server
+// proxying request to given api.Channel.
+func NewBinapiRPC(binapi api.Channel) *BinapiRPC {
+ return &BinapiRPC{binapi: binapi}
+}
+
+func (s *BinapiRPC) Invoke(req BinapiRequest, resp *BinapiResponse) error {
+ log.Printf("BinapiRPC.Invoke - REQ: %#v", req)
+
+ if req.IsMulti {
+ multi := s.binapi.SendMultiRequest(req.Msg)
+ for {
+ // create new message in response of type ReplyMsg
+ msg := reflect.New(reflect.TypeOf(req.ReplyMsg).Elem()).Interface().(api.Message)
+
+ stop, err := multi.ReceiveReply(msg)
+ if err != nil {
+ return err
+ } else if stop {
+ break
+ }
+
+ resp.Msgs = append(resp.Msgs, msg)
+ }
+ } else {
+ // create new message in response of type ReplyMsg
+ resp.Msg = reflect.New(reflect.TypeOf(req.ReplyMsg).Elem()).Interface().(api.Message)
+
+ err := s.binapi.SendRequest(req.Msg).ReceiveReply(resp.Msg)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+}