5 "github.com/edwarnicke/exechelper"
15 "go.fd.io/govpp/binapi/af_packet"
16 interfaces "go.fd.io/govpp/binapi/interface"
17 "go.fd.io/govpp/binapi/interface_types"
18 "go.fd.io/govpp/binapi/session"
19 "go.fd.io/govpp/binapi/tapv2"
20 "go.fd.io/govpp/binapi/vpe"
24 const vppConfigTemplate = `unix {
29 runtime-dir %[1]s/var/run
42 socket-name %[1]s%[3]s
46 socket-name %[1]s/var/run/vpp/stats.sock
50 plugin default { disable }
52 plugin unittest_plugin.so { enable }
53 plugin quic_plugin.so { enable }
54 plugin af_packet_plugin.so { enable }
55 plugin hs_apps_plugin.so { enable }
56 plugin http_plugin.so { enable }
60 default-log-level debug
61 default-syslog-log-level debug
67 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
68 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
69 defaultLogFilePath = "/var/log/vpp/vpp.log"
72 type VppInstance struct {
74 additionalConfig Stanza
75 connection *core.Connection
76 apiChannel api.Channel
79 func (vpp *VppInstance) Suite() *HstSuite {
80 return vpp.container.suite
83 func (vpp *VppInstance) getCliSocket() string {
84 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
87 func (vpp *VppInstance) getRunDir() string {
88 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
91 func (vpp *VppInstance) getLogDir() string {
92 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
95 func (vpp *VppInstance) getEtcDir() string {
96 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
99 func (vpp *VppInstance) start() error {
101 containerWorkDir := vpp.container.GetContainerWorkDir()
103 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
104 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
105 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
107 // Create startup.conf inside the container
108 configContent := fmt.Sprintf(
111 defaultCliSocketFilePath,
112 defaultApiSocketFilePath,
115 configContent += vpp.additionalConfig.ToString()
116 startupFileName := vpp.getEtcDir() + "/startup.conf"
117 vpp.container.createFile(startupFileName, configContent)
120 sig := make(chan os.Signal, 1)
121 signal.Notify(sig, syscall.SIGINT)
122 cont := make(chan bool, 1)
129 // Start VPP in GDB and wait for user to attach it
130 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
131 fmt.Println("run following command in different terminal:")
132 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
133 fmt.Println("Afterwards press CTRL+C to continue")
135 fmt.Println("continuing...")
138 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
141 // Connect to VPP and store the connection
142 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
143 conn, connEv, err := govpp.AsyncConnect(
145 core.DefaultMaxReconnectAttempts,
146 core.DefaultReconnectInterval)
148 fmt.Println("async connect error: ", err)
150 vpp.connection = conn
152 // ... wait for Connected event
154 if e.State != core.Connected {
155 fmt.Println("connecting to VPP failed: ", e.Error)
158 // ... check compatibility of used messages
159 ch, err := conn.NewAPIChannel()
161 fmt.Println("creating channel failed: ", err)
163 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
164 fmt.Println("compatibility error: ", err)
166 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
167 fmt.Println("compatibility error: ", err)
174 func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
175 vppCliCommand := fmt.Sprintf(command, arguments...)
176 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
177 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
178 vpp.Suite().log(containerExecCommand)
179 output, err := exechelper.CombinedOutput(containerExecCommand)
180 vpp.Suite().assertNil(err)
182 return string(output)
185 func (vpp *VppInstance) waitForApp(appName string, timeout int) {
186 for i := 0; i < timeout; i++ {
187 o := vpp.vppctl("show app")
188 if strings.Contains(o, appName) {
191 time.Sleep(1 * time.Second)
193 vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName)
197 func (vpp *VppInstance) createAfPacket(
199 ) (interface_types.InterfaceIndex, error) {
200 createReq := &af_packet.AfPacketCreateV2{
201 UseRandomHwAddr: true,
202 HostIfName: veth.Name(),
204 if veth.HwAddress() != (MacAddress{}) {
205 createReq.UseRandomHwAddr = false
206 createReq.HwAddr = veth.HwAddress()
208 createReply := &af_packet.AfPacketCreateV2Reply{}
210 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
213 veth.SetIndex(createReply.SwIfIndex)
216 upReq := &interfaces.SwInterfaceSetFlags{
217 SwIfIndex: veth.Index(),
218 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
220 upReply := &interfaces.SwInterfaceSetFlagsReply{}
222 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
227 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
229 var ip4Address string
230 if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
231 veth.SetAddress(ip4Address)
236 addressReq := &interfaces.SwInterfaceAddDelAddress{
238 SwIfIndex: veth.Index(),
239 Prefix: veth.AddressWithPrefix(),
241 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
243 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
247 return veth.Index(), nil
250 func (vpp *VppInstance) addAppNamespace(
252 ifx interface_types.InterfaceIndex,
255 req := &session.AppNamespaceAddDelV2{
258 NamespaceID: namespaceId,
260 reply := &session.AppNamespaceAddDelV2Reply{}
262 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
266 sessionReq := &session.SessionEnableDisable{
269 sessionReply := &session.SessionEnableDisableReply{}
271 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
278 func (vpp *VppInstance) createTap(
286 createTapReq := &tapv2.TapCreateV2{
289 HostIfName: tap.Name(),
290 HostIP4PrefixSet: true,
291 HostIP4Prefix: tap.IP4AddressWithPrefix(),
293 createTapReply := &tapv2.TapCreateV2Reply{}
295 // Create tap interface
296 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
301 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
303 SwIfIndex: createTapReply.SwIfIndex,
304 Prefix: tap.Peer().AddressWithPrefix(),
306 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
308 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
312 // Set interface to up
313 upReq := &interfaces.SwInterfaceSetFlags{
314 SwIfIndex: createTapReply.SwIfIndex,
315 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
317 upReply := &interfaces.SwInterfaceSetFlagsReply{}
319 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
326 func (vpp *VppInstance) saveLogs() {
327 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
328 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
329 cmd := exec.Command("cp", logSource, logTarget)
330 vpp.Suite().T().Helper()
331 vpp.Suite().log(cmd.String())
335 func (vpp *VppInstance) disconnect() {
336 vpp.connection.Disconnect()
337 vpp.apiChannel.Close()