1 // Copyright (c) 2017 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.
15 // Binary stats-client is an example VPP management application that exercises the
16 // govpp API for interface counters together with asynchronous connection to VPP.
19 // Generates Go bindings for all VPP APIs located in the json directory.
20 //go:generate binapi-generator --input-dir=bin_api --output-dir=bin_api
28 "git.fd.io/govpp.git/api"
29 "git.fd.io/govpp.git/api/ifcounters"
30 "git.fd.io/govpp.git/core"
31 "git.fd.io/govpp.git/core/bin_api/vpe"
32 "git.fd.io/govpp.git/examples/bin_api/interfaces"
36 fmt.Println("Starting stats VPP client...")
38 // async connect to VPP
39 conn, statCh, err := govpp.AsyncConnect()
41 fmt.Println("Error:", err)
44 defer conn.Disconnect()
46 // create an API channel that will be used in the examples
47 ch, err := conn.NewAPIChannel()
49 fmt.Println("Error:", err)
54 // create channel for Interrupt signal
55 sigChan := make(chan os.Signal, 1)
56 signal.Notify(sigChan, os.Interrupt)
58 var subs *api.NotifSubscription
59 var notifChan chan api.Message
61 // loop until Interrupt signal is received
66 case connEvent := <-statCh:
67 // VPP connection state change
68 switch connEvent.State {
70 fmt.Println("VPP connected.")
72 subs, notifChan = subscribeNotification(ch)
76 case core.Disconnected:
77 fmt.Println("VPP disconnected.")
80 case notifMsg := <-notifChan:
81 // counter notification received
82 processCounters(notifMsg.(*interfaces.VnetInterfaceCounters))
86 fmt.Println("Interrupt received, exiting.")
91 ch.UnsubscribeNotification(subs)
94 // subscribeNotification subscribes for interface counters notifications.
95 func subscribeNotification(ch *api.Channel) (*api.NotifSubscription, chan api.Message) {
97 notifChan := make(chan api.Message, 100)
98 subs, _ := ch.SubscribeNotification(notifChan, interfaces.NewVnetInterfaceCounters)
100 return subs, notifChan
103 // requestStatistics requests interface counters notifications from VPP.
104 func requestStatistics(ch *api.Channel) {
105 ch.SendRequest(&vpe.WantStats{
106 Pid: uint32(os.Getpid()),
108 }).ReceiveReply(&vpe.WantStatsReply{})
111 // processCounters processes a counter message received from VPP.
112 func processCounters(msg *interfaces.VnetInterfaceCounters) {
113 fmt.Printf("%+v\n", msg)
115 if msg.IsCombined == 0 {
117 counters, err := ifcounters.DecodeCounters(ifcounters.VnetInterfaceCounters(*msg))
119 fmt.Println("Error:", err)
121 fmt.Printf("%+v\n", counters)
125 counters, err := ifcounters.DecodeCombinedCounters(ifcounters.VnetInterfaceCounters(*msg))
127 fmt.Println("Error:", err)
129 fmt.Printf("%+v\n", counters)