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
81 func (vpp *VppInstance) getSuite() *HstSuite {
82 return vpp.container.suite
85 func (vpp *VppInstance) getCliSocket() string {
86 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
89 func (vpp *VppInstance) getRunDir() string {
90 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
93 func (vpp *VppInstance) getLogDir() string {
94 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
97 func (vpp *VppInstance) getEtcDir() string {
98 return vpp.container.getContainerWorkDir() + "/etc/vpp"
101 func (vpp *VppInstance) start() error {
103 containerWorkDir := vpp.container.getContainerWorkDir()
105 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
106 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
107 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
109 // Create startup.conf inside the container
110 configContent := fmt.Sprintf(
113 defaultCliSocketFilePath,
114 defaultApiSocketFilePath,
117 configContent += vpp.generateCpuConfig()
118 for _, c := range vpp.additionalConfig {
119 configContent += c.toString()
121 startupFileName := vpp.getEtcDir() + "/startup.conf"
122 vpp.container.createFile(startupFileName, configContent)
124 // create wrapper script for vppctl with proper CLI socket path
125 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
126 vppcliFileName := "/usr/bin/vppcli"
127 vpp.container.createFile(vppcliFileName, cliContent)
128 vpp.container.exec("chmod 0755 " + vppcliFileName)
131 sig := make(chan os.Signal, 1)
132 signal.Notify(sig, syscall.SIGINT)
133 cont := make(chan bool, 1)
139 // Start VPP in GDB and wait for user to attach it
140 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
141 fmt.Println("run following command in different terminal:")
142 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
143 fmt.Println("Afterwards press CTRL+C to continue")
145 fmt.Println("continuing...")
148 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
151 // Connect to VPP and store the connection
152 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
153 conn, connEv, err := govpp.AsyncConnect(
155 core.DefaultMaxReconnectAttempts,
156 core.DefaultReconnectInterval)
158 fmt.Println("async connect error: ", err)
160 vpp.connection = conn
162 // ... wait for Connected event
164 if e.State != core.Connected {
165 fmt.Println("connecting to VPP failed: ", e.Error)
168 // ... check compatibility of used messages
169 ch, err := conn.NewAPIChannel()
171 fmt.Println("creating channel failed: ", err)
173 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
174 fmt.Println("compatibility error: ", err)
176 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
177 fmt.Println("compatibility error: ", err)
184 func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
185 vppCliCommand := fmt.Sprintf(command, arguments...)
186 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
187 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
188 vpp.getSuite().log(containerExecCommand)
189 output, err := exechelper.CombinedOutput(containerExecCommand)
190 vpp.getSuite().assertNil(err)
192 return string(output)
195 func (vpp *VppInstance) waitForApp(appName string, timeout int) {
196 for i := 0; i < timeout; i++ {
197 o := vpp.vppctl("show app")
198 if strings.Contains(o, appName) {
201 time.Sleep(1 * time.Second)
203 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
206 func (vpp *VppInstance) createAfPacket(
208 ) (interface_types.InterfaceIndex, error) {
209 createReq := &af_packet.AfPacketCreateV2{
210 UseRandomHwAddr: true,
211 HostIfName: veth.Name(),
213 if veth.hwAddress != (MacAddress{}) {
214 createReq.UseRandomHwAddr = false
215 createReq.HwAddr = veth.hwAddress
217 createReply := &af_packet.AfPacketCreateV2Reply{}
219 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
222 veth.index = createReply.SwIfIndex
225 upReq := &interfaces.SwInterfaceSetFlags{
226 SwIfIndex: veth.index,
227 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
229 upReply := &interfaces.SwInterfaceSetFlagsReply{}
231 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
236 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
238 var ip4Address string
239 if ip4Address, err = veth.addresser.newIp4Address(veth.peer.networkNumber); err == nil {
240 veth.ip4Address = ip4Address
245 addressReq := &interfaces.SwInterfaceAddDelAddress{
247 SwIfIndex: veth.index,
248 Prefix: veth.addressWithPrefix(),
250 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
252 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
256 return veth.index, nil
259 func (vpp *VppInstance) addAppNamespace(
261 ifx interface_types.InterfaceIndex,
264 req := &session.AppNamespaceAddDelV2{
267 NamespaceID: namespaceId,
269 reply := &session.AppNamespaceAddDelV2Reply{}
271 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
275 sessionReq := &session.SessionEnableDisable{
278 sessionReply := &session.SessionEnableDisableReply{}
280 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
287 func (vpp *VppInstance) createTap(
295 createTapReq := &tapv2.TapCreateV2{
298 HostIfName: tap.Name(),
299 HostIP4PrefixSet: true,
300 HostIP4Prefix: tap.ip4AddressWithPrefix(),
302 createTapReply := &tapv2.TapCreateV2Reply{}
304 // Create tap interface
305 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
310 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
312 SwIfIndex: createTapReply.SwIfIndex,
313 Prefix: tap.peer.addressWithPrefix(),
315 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
317 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
321 // Set interface to up
322 upReq := &interfaces.SwInterfaceSetFlags{
323 SwIfIndex: createTapReply.SwIfIndex,
324 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
326 upReply := &interfaces.SwInterfaceSetFlagsReply{}
328 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
335 func (vpp *VppInstance) saveLogs() {
336 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
337 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
338 cmd := exec.Command("cp", logSource, logTarget)
339 vpp.getSuite().T().Helper()
340 vpp.getSuite().log(cmd.String())
344 func (vpp *VppInstance) disconnect() {
345 vpp.connection.Disconnect()
346 vpp.apiChannel.Close()
349 func (vpp *VppInstance) generateCpuConfig() string {
352 if len(vpp.cpus) < 1 {
356 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
357 workers := vpp.cpus[1:]
359 if len(workers) > 0 {
360 for i := 0; i < len(workers); i++ {
364 s = s + fmt.Sprintf("%d", workers[i])
366 c.append(fmt.Sprintf("corelist-workers %s", s))
368 return c.close().toString()