5 "github.com/edwarnicke/exechelper"
12 "go.fd.io/govpp/binapi/af_packet"
13 interfaces "go.fd.io/govpp/binapi/interface"
14 "go.fd.io/govpp/binapi/interface_types"
15 "go.fd.io/govpp/binapi/session"
16 "go.fd.io/govpp/binapi/tapv2"
17 "go.fd.io/govpp/binapi/vpe"
21 const vppConfigTemplate = `unix {
26 runtime-dir %[1]s/var/run
39 socket-name %[1]s%[3]s
43 socket-name %[1]s/var/run/vpp/stats.sock
47 plugin default { disable }
49 plugin unittest_plugin.so { enable }
50 plugin quic_plugin.so { enable }
51 plugin af_packet_plugin.so { enable }
52 plugin hs_apps_plugin.so { enable }
53 plugin http_plugin.so { enable }
57 default-log-level debug
58 default-syslog-log-level debug
64 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
65 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
66 defaultLogFilePath = "/var/log/vpp/vpp.log"
69 type VppInstance struct {
71 additionalConfig Stanza
72 connection *core.Connection
73 apiChannel api.Channel
76 func (vpp *VppInstance) Suite() *HstSuite {
77 return vpp.container.suite
80 func (vpp *VppInstance) getCliSocket() string {
81 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
84 func (vpp *VppInstance) getRunDir() string {
85 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
88 func (vpp *VppInstance) getLogDir() string {
89 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
92 func (vpp *VppInstance) getEtcDir() string {
93 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
96 func (vpp *VppInstance) start() error {
98 containerWorkDir := vpp.container.GetContainerWorkDir()
100 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
101 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
102 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
104 // Create startup.conf inside the container
105 configContent := fmt.Sprintf(
108 defaultCliSocketFilePath,
109 defaultApiSocketFilePath,
112 configContent += vpp.additionalConfig.ToString()
113 startupFileName := vpp.getEtcDir() + "/startup.conf"
114 vpp.container.createFile(startupFileName, configContent)
117 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
119 // Connect to VPP and store the connection
120 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
121 conn, connEv, err := govpp.AsyncConnect(
123 core.DefaultMaxReconnectAttempts,
124 core.DefaultReconnectInterval)
126 fmt.Println("async connect error: ", err)
128 vpp.connection = conn
130 // ... wait for Connected event
132 if e.State != core.Connected {
133 fmt.Println("connecting to VPP failed: ", e.Error)
136 // ... check compatibility of used messages
137 ch, err := conn.NewAPIChannel()
139 fmt.Println("creating channel failed: ", err)
141 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
142 fmt.Println("compatibility error: ", err)
144 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
145 fmt.Println("compatibility error: ", err)
152 func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
153 vppCliCommand := fmt.Sprintf(command, arguments...)
154 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
155 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
156 vpp.Suite().log(containerExecCommand)
157 output, err := exechelper.CombinedOutput(containerExecCommand)
158 vpp.Suite().assertNil(err)
160 return string(output)
163 func (vpp *VppInstance) waitForApp(appName string, timeout int) error {
164 for i := 0; i < timeout; i++ {
165 o := vpp.vppctl("show app")
166 if strings.Contains(o, appName) {
169 time.Sleep(1 * time.Second)
171 return fmt.Errorf("timeout while waiting for app '%s'", appName)
174 func (vpp *VppInstance) createAfPacket(
175 netInterface NetInterface,
176 ) (interface_types.InterfaceIndex, error) {
177 veth := netInterface.(*NetworkInterfaceVeth)
179 createReq := &af_packet.AfPacketCreateV2{
180 UseRandomHwAddr: true,
181 HostIfName: veth.Name(),
183 if veth.HwAddress() != (MacAddress{}) {
184 createReq.UseRandomHwAddr = false
185 createReq.HwAddr = veth.HwAddress()
187 createReply := &af_packet.AfPacketCreateV2Reply{}
189 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
192 veth.SetIndex(createReply.SwIfIndex)
195 upReq := &interfaces.SwInterfaceSetFlags{
196 SwIfIndex: veth.Index(),
197 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
199 upReply := &interfaces.SwInterfaceSetFlagsReply{}
201 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
206 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
208 var ip4Address string
209 if ip4Address, err = veth.addresser.NewIp4Address(veth.peerNetworkNumber); err == nil {
210 veth.SetAddress(ip4Address)
215 addressReq := &interfaces.SwInterfaceAddDelAddress{
217 SwIfIndex: veth.Index(),
218 Prefix: veth.AddressWithPrefix(),
220 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
222 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
226 return veth.Index(), nil
229 func (vpp *VppInstance) addAppNamespace(
231 ifx interface_types.InterfaceIndex,
234 req := &session.AppNamespaceAddDelV2{
237 NamespaceID: namespaceId,
239 reply := &session.AppNamespaceAddDelV2Reply{}
241 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
245 sessionReq := &session.SessionEnableDisable{
248 sessionReply := &session.SessionEnableDisableReply{}
250 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
257 func (vpp *VppInstance) createTap(
258 hostInterfaceName string,
259 hostIp4Address IP4AddressWithPrefix,
260 vppIp4Address AddressWithPrefix,
262 createTapReq := &tapv2.TapCreateV2{
264 HostIfName: hostInterfaceName,
265 HostIP4PrefixSet: true,
266 HostIP4Prefix: hostIp4Address,
268 createTapReply := &tapv2.TapCreateV2Reply{}
270 // Create tap interface
271 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
276 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
278 SwIfIndex: createTapReply.SwIfIndex,
279 Prefix: vppIp4Address,
281 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
283 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
287 // Set interface to up
288 upReq := &interfaces.SwInterfaceSetFlags{
289 SwIfIndex: createTapReply.SwIfIndex,
290 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
292 upReply := &interfaces.SwInterfaceSetFlagsReply{}
294 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
301 func (vpp *VppInstance) saveLogs() {
302 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
303 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
304 cmd := exec.Command("cp", logSource, logTarget)
305 vpp.Suite().T().Helper()
306 vpp.Suite().log(cmd.String())
310 func (vpp *VppInstance) disconnect() {
311 vpp.connection.Disconnect()
312 vpp.apiChannel.Close()