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/core"
28 "git.fd.io/govpp.git/examples/binapi/interfaces"
29 "git.fd.io/govpp.git/examples/binapi/vpe"
30 "git.fd.io/govpp.git/proxy"
34 binapiSocket = flag.String("binapi-socket", socketclient.DefaultSocketName, "Path to VPP binapi socket")
35 statsSocket = flag.String("stats-socket", statsclient.DefaultSocketName, "Path to VPP stats socket")
36 proxyAddr = flag.String("addr", ":7878", "Address on which proxy serves RPC.")
40 for _, msg := range api.GetRegisteredMessages() {
48 switch cmd := flag.Arg(0); cmd {
54 log.Printf("invalid command: %q, (available commands: client, server)", cmd)
59 // connect to proxy server
60 client, err := proxy.Connect(*proxyAddr)
62 log.Fatalln("connecting to proxy failed:", err)
66 statsProvider, err := client.NewStatsClient()
71 var sysStats api.SystemStats
72 if err := statsProvider.GetSystemStats(&sysStats); err != nil {
73 log.Fatalln("getting stats failed:", err)
75 log.Printf("SystemStats: %+v", sysStats)
77 var ifaceStats api.InterfaceStats
78 if err := statsProvider.GetInterfaceStats(&ifaceStats); err != nil {
79 log.Fatalln("getting stats failed:", err)
81 log.Printf("InterfaceStats: %+v", ifaceStats)
84 binapiChannel, err := client.NewBinapiClient()
88 log.Println("checking compatibility")
89 var msgs []api.Message
90 msgs = append(msgs, interfaces.AllMessages()...)
91 msgs = append(msgs, vpe.AllMessages()...)
92 if err := binapiChannel.CheckCompatiblity(msgs...); err != nil {
96 // - using binapi message directly
97 req := &vpe.CliInband{Cmd: "show version"}
98 reply := new(vpe.CliInbandReply)
99 if err := binapiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
100 log.Fatalln("binapi request failed:", err)
102 log.Printf("VPP version: %+v", reply.Reply)
104 // - or using generated rpc service
105 svc := interfaces.NewServiceClient(binapiChannel)
106 stream, err := svc.DumpSwInterface(context.Background(), &interfaces.SwInterfaceDump{})
108 log.Fatalln("binapi request failed:", err)
111 iface, err := stream.Recv()
118 log.Printf("- interface: %+v", iface)
123 p, err := proxy.NewServer()
128 statsAdapter := statsclient.NewStatsClient(*statsSocket)
129 binapiAdapter := socketclient.NewVppClient(*binapiSocket)
131 if err := p.ConnectStats(statsAdapter); err != nil {
132 log.Fatalln("connecting to stats failed:", err)
134 defer p.DisconnectStats()
136 if err := p.ConnectBinapi(binapiAdapter); err != nil {
137 log.Fatalln("connecting to binapi failed:", err)
139 defer p.DisconnectBinapi()
141 if err := p.ListenAndServe(*proxyAddr); err != nil {