Introduce proxy for VPP
[govpp.git] / cmd / vpp-proxy / main.go
1 //  Copyright (c) 2019 Cisco and/or its affiliates.
2 //
3 //  Licensed under the Apache License, Version 2.0 (the "License");
4 //  you may not use this file except in compliance with the License.
5 //  You may obtain a copy of the License at:
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 //  Unless required by applicable law or agreed to in writing, software
10 //  distributed under the License is distributed on an "AS IS" BASIS,
11 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //  See the License for the specific language governing permissions and
13 //  limitations under the License.
14
15 package main
16
17 import (
18         "context"
19         "encoding/gob"
20         "flag"
21         "io"
22         "log"
23
24         "git.fd.io/govpp.git/adapter/socketclient"
25         "git.fd.io/govpp.git/adapter/statsclient"
26         "git.fd.io/govpp.git/api"
27         "git.fd.io/govpp.git/examples/binapi/interfaces"
28         "git.fd.io/govpp.git/examples/binapi/vpe"
29         "git.fd.io/govpp.git/proxy"
30 )
31
32 var (
33         binapiSocket = flag.String("binapi-socket", socketclient.DefaultSocketName, "Path to VPP binapi socket")
34         statsSocket  = flag.String("stats-socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
35         proxyAddr    = flag.String("addr", ":7878", "Address on which proxy serves RPC.")
36 )
37
38 func init() {
39         for _, msg := range api.GetRegisteredMessages() {
40                 gob.Register(msg)
41         }
42 }
43
44 func main() {
45         flag.Parse()
46
47         switch cmd := flag.Arg(0); cmd {
48         case "server":
49                 runServer()
50         case "client":
51                 runClient()
52         default:
53                 log.Printf("invalid command: %q, (available commands: client, server)", cmd)
54         }
55 }
56
57 func runClient() {
58         // connect to proxy server
59         client, err := proxy.Connect(*proxyAddr)
60         if err != nil {
61                 log.Fatalln("connecting to proxy failed:", err)
62         }
63
64         // proxy stats
65         statsProvider, err := client.NewStatsClient()
66         if err != nil {
67                 log.Fatalln(err)
68         }
69
70         var sysStats api.SystemStats
71         if err := statsProvider.GetSystemStats(&sysStats); err != nil {
72                 log.Fatalln("getting stats failed:", err)
73         }
74         log.Printf("SystemStats: %+v", sysStats)
75
76         var ifaceStats api.InterfaceStats
77         if err := statsProvider.GetInterfaceStats(&ifaceStats); err != nil {
78                 log.Fatalln("getting stats failed:", err)
79         }
80         log.Printf("InterfaceStats: %+v", ifaceStats)
81
82         // proxy binapi
83         binapiChannel, err := client.NewBinapiClient()
84         if err != nil {
85                 log.Fatalln(err)
86         }
87
88         // - using binapi message directly
89         req := &vpe.CliInband{Cmd: "show version"}
90         reply := new(vpe.CliInbandReply)
91         if err := binapiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
92                 log.Fatalln("binapi request failed:", err)
93         }
94         log.Printf("VPP version: %+v", reply.Reply)
95
96         // - or using generated rpc service
97         svc := interfaces.NewServiceClient(binapiChannel)
98         stream, err := svc.DumpSwInterface(context.Background(), &interfaces.SwInterfaceDump{})
99         if err != nil {
100                 log.Fatalln("binapi request failed:", err)
101         }
102         for {
103                 iface, err := stream.Recv()
104                 if err == io.EOF {
105                         break
106                 }
107                 if err != nil {
108                         log.Fatalln(err)
109                 }
110                 log.Printf("- interface: %+v", iface)
111         }
112 }
113
114 func runServer() {
115         p := proxy.NewServer()
116
117         statsAdapter := statsclient.NewStatsClient(*statsSocket)
118         binapiAdapter := socketclient.NewVppClient(*binapiSocket)
119
120         if err := p.ConnectStats(statsAdapter); err != nil {
121                 log.Fatalln("connecting to stats failed:", err)
122         }
123         defer p.DisconnectStats()
124
125         if err := p.ConnectBinapi(binapiAdapter); err != nil {
126                 log.Fatalln("connecting to binapi failed:", err)
127         }
128         defer p.DisconnectBinapi()
129
130         p.ListenAndServe(*proxyAddr)
131 }