1 // Copyright (c) 2019 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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"
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.")
39 for _, msg := range api.GetRegisteredMessages() {
47 switch cmd := flag.Arg(0); cmd {
53 log.Printf("invalid command: %q, (available commands: client, server)", cmd)
58 // connect to proxy server
59 client, err := proxy.Connect(*proxyAddr)
61 log.Fatalln("connecting to proxy failed:", err)
65 statsProvider, err := client.NewStatsClient()
70 var sysStats api.SystemStats
71 if err := statsProvider.GetSystemStats(&sysStats); err != nil {
72 log.Fatalln("getting stats failed:", err)
74 log.Printf("SystemStats: %+v", sysStats)
76 var ifaceStats api.InterfaceStats
77 if err := statsProvider.GetInterfaceStats(&ifaceStats); err != nil {
78 log.Fatalln("getting stats failed:", err)
80 log.Printf("InterfaceStats: %+v", ifaceStats)
83 binapiChannel, err := client.NewBinapiClient()
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)
94 log.Printf("VPP version: %+v", reply.Reply)
96 // - or using generated rpc service
97 svc := interfaces.NewServiceClient(binapiChannel)
98 stream, err := svc.DumpSwInterface(context.Background(), &interfaces.SwInterfaceDump{})
100 log.Fatalln("binapi request failed:", err)
103 iface, err := stream.Recv()
110 log.Printf("- interface: %+v", iface)
115 p := proxy.NewServer()
117 statsAdapter := statsclient.NewStatsClient(*statsSocket)
118 binapiAdapter := socketclient.NewVppClient(*binapiSocket)
120 if err := p.ConnectStats(statsAdapter); err != nil {
121 log.Fatalln("connecting to stats failed:", err)
123 defer p.DisconnectStats()
125 if err := p.ConnectBinapi(binapiAdapter); err != nil {
126 log.Fatalln("connecting to binapi failed:", err)
128 defer p.DisconnectBinapi()
130 p.ListenAndServe(*proxyAddr)