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/api"
28 "git.fd.io/govpp.git/core"
29 "git.fd.io/govpp.git/examples/binapi/acl"
30 "git.fd.io/govpp.git/examples/binapi/interfaces"
31 "git.fd.io/govpp.git/examples/binapi/ip"
35 fmt.Println("Starting simple VPP client...")
38 conn, conev, err := govpp.AsyncConnect("", core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
40 log.Fatalln("ERROR:", err)
42 defer conn.Disconnect()
46 if e.State != core.Connected {
47 log.Fatalf("failed to connect: %v", e.Error)
51 // create an API channel that will be used in the examples
52 ch, err := conn.NewAPIChannel()
54 log.Fatalln("ERROR:", err)
58 // individual examples
69 interfaceNotifications(ch)
72 // aclVersion is the simplest API example - one empty request message and one reply message.
73 func aclVersion(ch api.Channel) {
74 fmt.Println("ACL getting version")
76 req := &acl.ACLPluginGetVersion{}
77 reply := &acl.ACLPluginGetVersionReply{}
79 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
80 fmt.Println("ERROR:", err)
82 fmt.Printf("ACL version reply: %+v\n", reply)
86 // aclConfig is another simple API example - in this case, the request contains structured data.
87 func aclConfig(ch api.Channel) {
88 fmt.Println("ACL adding replace")
90 req := &acl.ACLAddReplace{
92 Tag: []byte("access list 1"),
96 SrcIPAddr: net.ParseIP("10.0.0.0").To4(),
98 DstIPAddr: net.ParseIP("192.168.1.0").To4(),
104 SrcIPAddr: net.ParseIP("8.8.8.8").To4(),
106 DstIPAddr: net.ParseIP("172.16.0.0").To4(),
112 reply := &acl.ACLAddReplaceReply{}
114 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
115 fmt.Println("ERROR:", err)
118 if reply.Retval != 0 {
119 fmt.Println("Retval:", reply.Retval)
123 fmt.Printf("ACL add replace reply: %+v\n", reply)
127 // aclDump shows an example where SendRequest and ReceiveReply are not chained together.
128 func aclDump(ch api.Channel) {
129 fmt.Println("Dumping ACL")
131 req := &acl.ACLDump{}
132 reply := &acl.ACLDetails{}
134 reqCtx := ch.SendRequest(req)
136 if err := reqCtx.ReceiveReply(reply); err != nil {
137 fmt.Println("ERROR:", err)
139 fmt.Printf("ACL details: %+v\n", reply)
143 // interfaceDump shows an example of multipart request (multiple replies are expected).
144 func interfaceDump(ch api.Channel) {
145 fmt.Println("Dumping interfaces")
147 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
150 msg := &interfaces.SwInterfaceDetails{}
151 stop, err := reqCtx.ReceiveReply(msg)
156 fmt.Println("ERROR:", err)
158 ifaceName := strings.TrimFunc(string(msg.InterfaceName), func(r rune) bool {
161 fmt.Printf("Interface %q: %+v\n", ifaceName, msg)
165 func ipAddressDump(ch api.Channel) {
166 fmt.Println("Dumping IP addresses")
168 req := &ip.IPAddressDump{
169 SwIfIndex: 1, //^uint32(0),
171 reqCtx := ch.SendMultiRequest(req)
174 msg := &ip.IPAddressDetails{}
175 stop, err := reqCtx.ReceiveReply(msg)
180 fmt.Println("ERROR:", err)
182 fmt.Printf("ip address details: %d %+v\n", msg.SwIfIndex, msg)
186 // aclDump shows an example where SendRequest and ReceiveReply are not chained together.
187 func setIpUnnumbered(ch api.Channel) {
188 req := &interfaces.SwInterfaceSetUnnumbered{
190 UnnumberedSwIfIndex: 2,
193 reply := &interfaces.SwInterfaceSetUnnumberedReply{}
195 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
196 fmt.Println("ERROR:", err)
198 fmt.Printf("%+v\n", reply)
202 func ipUnnumberedDump(ch api.Channel) {
203 fmt.Println("Dumping IP unnumbered")
205 reqCtx := ch.SendMultiRequest(&ip.IPUnnumberedDump{
206 SwIfIndex: ^uint32(0),
210 msg := &ip.IPUnnumberedDetails{}
211 stop, err := reqCtx.ReceiveReply(msg)
216 fmt.Println("ERROR:", err)
218 fmt.Printf("IP unnumbered details: %+v\n", msg)
222 // interfaceNotifications shows the usage of notification API. Note that for notifications,
223 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
224 // buffer is full, the notifications will not be delivered into it.
225 func interfaceNotifications(ch api.Channel) {
226 fmt.Println("Subscribing to notificaiton events")
228 notifChan := make(chan api.Message, 100)
230 // subscribe for specific notification message
231 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
236 // enable interface events in VPP
237 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
238 PID: uint32(os.Getpid()),
240 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
245 // generate some events in VPP
246 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
249 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
253 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
256 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
261 // receive one notification
262 notif := (<-notifChan).(*interfaces.SwInterfaceEvent)
263 fmt.Printf("incoming event: %+v\n", notif)
265 // disable interface events in VPP
266 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
267 PID: uint32(os.Getpid()),
269 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
274 // unsubscribe from delivery of the notifications
275 err = sub.Unsubscribe()