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 // simple-client is an example VPP management application that exercises the
16 // govpp API on real-world use-cases.
27 "git.fd.io/govpp.git/adapter/socketclient"
28 "git.fd.io/govpp.git/api"
29 "git.fd.io/govpp.git/core"
30 "git.fd.io/govpp.git/examples/binapi/interfaces"
31 "git.fd.io/govpp.git/examples/binapi/ip"
32 "git.fd.io/govpp.git/examples/binapi/vpe"
36 sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
42 fmt.Println("Starting simple client example")
44 // connect to VPP asynchronously
45 conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
47 log.Fatalln("ERROR:", err)
49 defer conn.Disconnect()
51 // wait for Connected event
54 if e.State != core.Connected {
55 log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
59 // create an API channel that will be used in the examples
60 ch, err := conn.NewAPIChannel()
62 log.Fatalln("ERROR: creating channel failed:", err)
66 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
72 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
76 idx := createLoopback(ch)
80 ipAddressDump(ch, idx)
81 interfaceNotifications(ch, idx)
84 fmt.Printf("finished with %d errors\n", len(Errors))
87 fmt.Println("finished successfully")
93 func logError(err error, msg string) {
94 fmt.Printf("ERROR: %s: %v\n", msg, err)
95 Errors = append(Errors, err)
98 // vppVersion is the simplest API example - it retrieves VPP version.
99 func vppVersion(ch api.Channel) {
100 fmt.Println("Retrieving version")
102 req := &vpe.ShowVersion{}
103 reply := &vpe.ShowVersionReply{}
105 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
106 logError(err, "retrieving version")
109 fmt.Printf("reply: %+v\n", reply)
111 fmt.Printf("VPP version: %q\n", cleanString(reply.Version))
116 // createLoopback sends request to create loopback interface.
117 func createLoopback(ch api.Channel) interfaces.InterfaceIndex {
118 fmt.Println("Creating loopback interface")
120 req := &interfaces.CreateLoopback{}
121 reply := &interfaces.CreateLoopbackReply{}
123 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
124 logError(err, "creating loopback interface")
127 fmt.Printf("reply: %+v\n", reply)
129 fmt.Printf("interface index: %v\n", reply.SwIfIndex)
133 return reply.SwIfIndex
136 // interfaceDump shows an example of multipart request (multiple replies are expected).
137 func interfaceDump(ch api.Channel) {
138 fmt.Println("Dumping interfaces")
141 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
143 msg := &interfaces.SwInterfaceDetails{}
144 stop, err := reqCtx.ReceiveReply(msg)
149 logError(err, "dumping interfaces")
153 fmt.Printf(" - interface #%d: %+v\n", n, msg)
160 // addIPAddress sends request to add IP address to interface.
161 func addIPAddress(ch api.Channel, index interfaces.InterfaceIndex) {
162 fmt.Printf("Adding IP address to interface to interface index %d\n", index)
164 req := &interfaces.SwInterfaceAddDelAddress{
167 Prefix: interfaces.AddressWithPrefix{
168 Address: interfaces.Address{
169 Af: interfaces.ADDRESS_IP4,
170 Un: interfaces.AddressUnionIP4(interfaces.IP4Address{10, 10, 0, uint8(index)}),
175 reply := &interfaces.SwInterfaceAddDelAddressReply{}
177 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
178 logError(err, "adding IP address to interface")
181 fmt.Printf("reply: %+v\n", reply)
187 func ipAddressDump(ch api.Channel, index interfaces.InterfaceIndex) {
188 fmt.Printf("Dumping IP addresses for interface index %d\n", index)
190 req := &ip.IPAddressDump{
191 SwIfIndex: ip.InterfaceIndex(index),
193 reqCtx := ch.SendMultiRequest(req)
196 msg := &ip.IPAddressDetails{}
197 stop, err := reqCtx.ReceiveReply(msg)
199 logError(err, "dumping IP addresses")
205 fmt.Printf(" - ip address: %+v\n", msg)
212 // interfaceNotifications shows the usage of notification API. Note that for notifications,
213 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
214 // buffer is full, the notifications will not be delivered into it.
215 func interfaceNotifications(ch api.Channel, index interfaces.InterfaceIndex) {
216 fmt.Printf("Subscribing to notificaiton events for interface index %d\n", index)
218 notifChan := make(chan api.Message, 100)
220 // subscribe for specific notification message
221 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
223 logError(err, "subscribing to interface events")
227 // enable interface events in VPP
228 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
229 PID: uint32(os.Getpid()),
231 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
233 logError(err, "enabling interface events")
237 // receive notifications
239 for notif := range notifChan {
240 fmt.Printf("incoming event: %+v\n", notif.(*interfaces.SwInterfaceEvent))
244 // generate some events in VPP
245 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
247 Flags: interfaces.IF_STATUS_API_FLAG_ADMIN_UP,
248 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
250 logError(err, "setting interface flags")
253 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
256 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
258 logError(err, "setting interface flags")
262 // disable interface events in VPP
263 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
264 PID: uint32(os.Getpid()),
266 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
268 logError(err, "setting interface flags")
272 // unsubscribe from delivery of the notifications
273 err = sub.Unsubscribe()
275 logError(err, "unsubscribing from interface events")
283 func cleanString(str string) string {
284 return strings.Split(str, "\x00")[0]