12 "github.com/edwarnicke/exechelper"
16 "go.fd.io/govpp/binapi/af_packet"
17 interfaces "go.fd.io/govpp/binapi/interface"
18 "go.fd.io/govpp/binapi/interface_types"
19 "go.fd.io/govpp/binapi/session"
20 "go.fd.io/govpp/binapi/tapv2"
21 "go.fd.io/govpp/binapi/vpe"
25 const vppConfigTemplate = `unix {
30 runtime-dir %[1]s/var/run
43 socket-name %[1]s%[3]s
47 socket-name %[1]s/var/run/vpp/stats.sock
51 plugin default { disable }
53 plugin unittest_plugin.so { enable }
54 plugin quic_plugin.so { enable }
55 plugin af_packet_plugin.so { enable }
56 plugin hs_apps_plugin.so { enable }
57 plugin http_plugin.so { enable }
61 default-log-level debug
62 default-syslog-log-level debug
68 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
69 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
70 defaultLogFilePath = "/var/log/vpp/vpp.log"
73 type VppInstance struct {
75 additionalConfig Stanza
76 connection *core.Connection
77 apiChannel api.Channel
80 func (vpp *VppInstance) Suite() *HstSuite {
81 return vpp.container.suite
84 func (vpp *VppInstance) getCliSocket() string {
85 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
88 func (vpp *VppInstance) getRunDir() string {
89 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
92 func (vpp *VppInstance) getLogDir() string {
93 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
96 func (vpp *VppInstance) getEtcDir() string {
97 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
100 func (vpp *VppInstance) start() error {
102 containerWorkDir := vpp.container.GetContainerWorkDir()
104 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
105 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
106 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
108 // Create startup.conf inside the container
109 configContent := fmt.Sprintf(
112 defaultCliSocketFilePath,
113 defaultApiSocketFilePath,
116 configContent += vpp.additionalConfig.ToString()
117 startupFileName := vpp.getEtcDir() + "/startup.conf"
118 vpp.container.createFile(startupFileName, configContent)
120 // create wrapper script for vppctl with proper CLI socket path
121 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
122 vppcliFileName := "/usr/bin/vppcli"
123 vpp.container.createFile(vppcliFileName, cliContent)
124 vpp.container.exec("chmod 0755 " + vppcliFileName)
127 sig := make(chan os.Signal, 1)
128 signal.Notify(sig, syscall.SIGINT)
129 cont := make(chan bool, 1)
135 // Start VPP in GDB and wait for user to attach it
136 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
137 fmt.Println("run following command in different terminal:")
138 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
139 fmt.Println("Afterwards press CTRL+C to continue")
141 fmt.Println("continuing...")
144 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
147 // Connect to VPP and store the connection
148 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
149 conn, connEv, err := govpp.AsyncConnect(
151 core.DefaultMaxReconnectAttempts,
152 core.DefaultReconnectInterval)
154 fmt.Println("async connect error: ", err)
156 vpp.connection = conn
158 // ... wait for Connected event
160 if e.State != core.Connected {
161 fmt.Println("connecting to VPP failed: ", e.Error)
164 // ... check compatibility of used messages
165 ch, err := conn.NewAPIChannel()
167 fmt.Println("creating channel failed: ", err)
169 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
170 fmt.Println("compatibility error: ", err)
172 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
173 fmt.Println("compatibility error: ", err)
180 func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
181 vppCliCommand := fmt.Sprintf(command, arguments...)
182 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
183 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
184 vpp.Suite().log(containerExecCommand)
185 output, err := exechelper.CombinedOutput(containerExecCommand)
186 vpp.Suite().assertNil(err)
188 return string(output)
191 func (vpp *VppInstance) waitForApp(appName string, timeout int) {
192 for i := 0; i < timeout; i++ {
193 o := vpp.vppctl("show app")
194 if strings.Contains(o, appName) {
197 time.Sleep(1 * time.Second)
199 vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName)
203 func (vpp *VppInstance) createAfPacket(
205 ) (interface_types.InterfaceIndex, error) {
206 createReq := &af_packet.AfPacketCreateV2{
207 UseRandomHwAddr: true,
208 HostIfName: veth.Name(),
210 if veth.HwAddress() != (MacAddress{}) {
211 createReq.UseRandomHwAddr = false
212 createReq.HwAddr = veth.HwAddress()
214 createReply := &af_packet.AfPacketCreateV2Reply{}
216 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
219 veth.SetIndex(createReply.SwIfIndex)
222 upReq := &interfaces.SwInterfaceSetFlags{
223 SwIfIndex: veth.Index(),
224 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
226 upReply := &interfaces.SwInterfaceSetFlagsReply{}
228 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
233 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
235 var ip4Address string
236 if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
237 veth.SetAddress(ip4Address)
242 addressReq := &interfaces.SwInterfaceAddDelAddress{
244 SwIfIndex: veth.Index(),
245 Prefix: veth.AddressWithPrefix(),
247 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
249 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
253 return veth.Index(), nil
256 func (vpp *VppInstance) addAppNamespace(
258 ifx interface_types.InterfaceIndex,
261 req := &session.AppNamespaceAddDelV2{
264 NamespaceID: namespaceId,
266 reply := &session.AppNamespaceAddDelV2Reply{}
268 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
272 sessionReq := &session.SessionEnableDisable{
275 sessionReply := &session.SessionEnableDisableReply{}
277 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
284 func (vpp *VppInstance) createTap(
292 createTapReq := &tapv2.TapCreateV2{
295 HostIfName: tap.Name(),
296 HostIP4PrefixSet: true,
297 HostIP4Prefix: tap.IP4AddressWithPrefix(),
299 createTapReply := &tapv2.TapCreateV2Reply{}
301 // Create tap interface
302 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
307 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
309 SwIfIndex: createTapReply.SwIfIndex,
310 Prefix: tap.Peer().AddressWithPrefix(),
312 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
314 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
318 // Set interface to up
319 upReq := &interfaces.SwInterfaceSetFlags{
320 SwIfIndex: createTapReply.SwIfIndex,
321 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
323 upReply := &interfaces.SwInterfaceSetFlagsReply{}
325 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
332 func (vpp *VppInstance) saveLogs() {
333 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
334 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
335 cmd := exec.Command("cp", logSource, logTarget)
336 vpp.Suite().T().Helper()
337 vpp.Suite().log(cmd.String())
341 func (vpp *VppInstance) disconnect() {
342 vpp.connection.Disconnect()
343 vpp.apiChannel.Close()