go.ligato.io/vpp-agent/v3@v3.5.0/examples/vpp_proxy/main.go (about) 1 // Copyright (c) 2021 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 // The VPP Proxy example demonstrates how to use GoVPP proxy to access 16 // VPP binapi and stats API remotely via HTTP server. 17 package main 18 19 import ( 20 "context" 21 "encoding/gob" 22 "flag" 23 "io" 24 "log" 25 26 "go.fd.io/govpp/api" 27 "go.fd.io/govpp/proxy" 28 29 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi" 30 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106" 31 interfaces "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/interface" 32 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/vpe" 33 ) 34 35 // VPP version used in the example. 36 const vppVersion = vpp2106.Version 37 38 var ( 39 address = flag.String("addr", ":9191", "agent address") 40 ) 41 42 func main() { 43 flag.Parse() 44 45 client, err := proxy.Connect(*address) 46 if err != nil { 47 log.Fatalln("connecting to proxy failed:", err) 48 } 49 50 proxyStats(client) 51 proxyBinapi(client) 52 } 53 54 func proxyStats(client *proxy.Client) { 55 statsProvider, err := client.NewStatsClient() 56 if err != nil { 57 log.Fatalln(err) 58 } 59 60 var sysStats api.SystemStats 61 if err := statsProvider.GetSystemStats(&sysStats); err != nil { 62 log.Fatalln("getting stats failed:", err) 63 } 64 log.Printf("SystemStats: %+v", sysStats) 65 } 66 67 type BinapiClient struct { 68 *proxy.BinapiClient 69 } 70 71 func (b BinapiClient) WatchEvent(ctx context.Context, event api.Message) (api.Watcher, error) { 72 // dummy implementation so that BinapiClient implements api.Connection interface 73 return nil, nil 74 } 75 76 func proxyBinapi(client *proxy.Client) { 77 proxyBinapiChannel, err := client.NewBinapiClient() 78 binapiChannel := BinapiClient{proxyBinapiChannel} 79 if err != nil { 80 log.Fatalln(err) 81 } 82 83 // All binapi messages must be registered to gob 84 for _, msg := range binapi.Versions[vppVersion].AllMessages() { 85 gob.Register(msg) 86 } 87 88 // Check compatibility with remote VPP version 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 { 93 log.Fatalf("compatibility check (VPP %v) failed: %v", vppVersion, err) 94 } 95 log.Printf("compatibility OK! (VPP %v)", vppVersion) 96 97 var ( 98 vpeSvc = vpe.NewServiceClient(binapiChannel) 99 interfaceSvc = interfaces.NewServiceClient(binapiChannel) 100 ) 101 102 // Show VPP version 103 version, err := vpeSvc.ShowVersion(context.Background(), &vpe.ShowVersion{}) 104 if err != nil { 105 log.Fatalln("ShowVersion failed:", err) 106 } 107 log.Printf("Version: %+v", version) 108 109 // List interfaces 110 stream, err := interfaceSvc.SwInterfaceDump(context.Background(), &interfaces.SwInterfaceDump{}) 111 if err != nil { 112 log.Fatalln("SwInterfaceDump failed:", err) 113 } 114 log.Printf("dumping interfaces") 115 for { 116 iface, err := stream.Recv() 117 if err == io.EOF { 118 break 119 } 120 if err != nil { 121 log.Fatalln(err) 122 } 123 log.Printf("- interface %d: %v", iface.SwIfIndex, iface.InterfaceName) 124 } 125 }