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/interface_types"
31 "git.fd.io/govpp.git/examples/binapi/interfaces"
32 "git.fd.io/govpp.git/examples/binapi/ip"
33 "git.fd.io/govpp.git/examples/binapi/ip_types"
34 "git.fd.io/govpp.git/examples/binapi/vpe"
38 sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
44 fmt.Println("Starting simple client example")
46 // connect to VPP asynchronously
47 conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
49 log.Fatalln("ERROR:", err)
51 defer conn.Disconnect()
53 // wait for Connected event
56 if e.State != core.Connected {
57 log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
61 // create an API channel that will be used in the examples
62 ch, err := conn.NewAPIChannel()
64 log.Fatalln("ERROR: creating channel failed:", err)
70 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
81 interfaceNotifications(ch)
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))
115 // createLoopback sends request to create loopback interface.
116 func createLoopback(ch api.Channel) {
117 fmt.Println("Creating loopback interface")
119 req := &interfaces.CreateLoopback{}
120 reply := &interfaces.CreateLoopbackReply{}
122 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
123 logError(err, "creating loopback interface")
126 fmt.Printf("reply: %+v\n", reply)
128 fmt.Printf("loopback interface index: %v\n", reply.SwIfIndex)
132 // interfaceDump shows an example of multipart request (multiple replies are expected).
133 func interfaceDump(ch api.Channel) {
134 fmt.Println("Dumping interfaces")
136 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
138 msg := &interfaces.SwInterfaceDetails{}
139 stop, err := reqCtx.ReceiveReply(msg)
141 logError(err, "dumping interfaces")
147 fmt.Printf(" - interface: %+v\n", msg)
153 // addIPAddress sends request to add IP address to interface.
154 func addIPAddress(ch api.Channel) {
155 fmt.Println("Adding IP address to interface")
157 req := &interfaces.SwInterfaceAddDelAddress{
160 Prefix: ip_types.AddressWithPrefix{
161 Address: interfaces.Address{
162 Af: ip_types.ADDRESS_IP4,
163 Un: ip_types.AddressUnionIP4(interfaces.IP4Address{10, 10, 0, 1}),
168 reply := &interfaces.SwInterfaceAddDelAddressReply{}
170 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
171 logError(err, "adding IP address to interface")
174 fmt.Printf("reply: %+v\n", reply)
179 func ipAddressDump(ch api.Channel) {
180 fmt.Println("Dumping IP addresses")
182 req := &ip.IPAddressDump{
185 reqCtx := ch.SendMultiRequest(req)
188 msg := &ip.IPAddressDetails{}
189 stop, err := reqCtx.ReceiveReply(msg)
191 logError(err, "dumping IP addresses")
197 fmt.Printf(" - ip address: %+v\n", msg)
203 // interfaceNotifications shows the usage of notification API. Note that for notifications,
204 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
205 // buffer is full, the notifications will not be delivered into it.
206 func interfaceNotifications(ch api.Channel) {
207 fmt.Println("Subscribing to notificaiton events")
209 notifChan := make(chan api.Message, 100)
211 // subscribe for specific notification message
212 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
214 logError(err, "subscribing to interface events")
218 // enable interface events in VPP
219 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
220 PID: uint32(os.Getpid()),
222 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
224 logError(err, "enabling interface events")
228 // generate some events in VPP
229 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
231 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
233 logError(err, "setting interface flags")
236 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
238 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
239 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
241 logError(err, "setting interface flags")
245 // receive one notification
246 notif := (<-notifChan).(*interfaces.SwInterfaceEvent)
247 fmt.Printf("incoming event: %+v\n", notif)
249 // disable interface events in VPP
250 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
251 PID: uint32(os.Getpid()),
253 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
255 logError(err, "setting interface flags")
259 // unsubscribe from delivery of the notifications
260 err = sub.Unsubscribe()
262 logError(err, "unsubscribing from interface events")
269 func cleanString(str string) string {
270 return strings.Split(str, "\x00")[0]