blob: c08514e22dcb87b00a42bcaf28ba02337a2513c3 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01004 "fmt"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01005 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01006 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01007 "os/signal"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01008 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01009 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010010 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010011
Filip Tehlar9abba112023-03-07 10:13:19 +010012 "github.com/edwarnicke/exechelper"
13
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010014 "go.fd.io/govpp"
15 "go.fd.io/govpp/api"
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"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010020 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010021 "go.fd.io/govpp/binapi/vpe"
22 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010023)
24
25const vppConfigTemplate = `unix {
26 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010027 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010028 full-coredump
29 cli-listen %[1]s%[2]s
30 runtime-dir %[1]s/var/run
31 gid vpp
32}
33
34api-trace {
35 on
36}
37
38api-segment {
39 gid vpp
40}
41
42socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010043 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010044}
45
46statseg {
47 socket-name %[1]s/var/run/vpp/stats.sock
48}
49
50plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010051 plugin default { disable }
52
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010053 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010055 plugin af_packet_plugin.so { enable }
56 plugin hs_apps_plugin.so { enable }
57 plugin http_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010058}
59
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010060logging {
61 default-log-level debug
62 default-syslog-log-level debug
63}
64
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010065`
66
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010067const (
68 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010069 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010070 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010071)
72
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010073type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010074 container *Container
75 additionalConfig Stanza
76 connection *core.Connection
77 apiChannel api.Channel
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010078}
79
Maros Ondrejickae7625d02023-02-28 16:55:01 +010080func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010081 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082}
83
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010084func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010085 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010086}
87
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010088func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010089 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010090}
91
92func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010093 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010094}
95
96func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010097 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010098}
99
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100101 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100102 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103
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())
107
108 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100109 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100110 vppConfigTemplate,
111 containerWorkDir,
112 defaultCliSocketFilePath,
113 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100114 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100115 )
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100116 configContent += vpp.additionalConfig.toString()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100117 startupFileName := vpp.getEtcDir() + "/startup.conf"
118 vpp.container.createFile(startupFileName, configContent)
119
Filip Tehlar1a661502023-03-08 11:55:50 +0100120 // 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)
125
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100126 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100127 sig := make(chan os.Signal, 1)
128 signal.Notify(sig, syscall.SIGINT)
129 cont := make(chan bool, 1)
130 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100131 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100132 cont <- true
133 }()
134
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")
140 <-cont
141 fmt.Println("continuing...")
142 } else {
143 // Start VPP
144 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
145 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100146
147 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100148 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100149 conn, connEv, err := govpp.AsyncConnect(
150 sockAddress,
151 core.DefaultMaxReconnectAttempts,
152 core.DefaultReconnectInterval)
153 if err != nil {
154 fmt.Println("async connect error: ", err)
155 }
156 vpp.connection = conn
157
158 // ... wait for Connected event
159 e := <-connEv
160 if e.State != core.Connected {
161 fmt.Println("connecting to VPP failed: ", e.Error)
162 }
163
164 // ... check compatibility of used messages
165 ch, err := conn.NewAPIChannel()
166 if err != nil {
167 fmt.Println("creating channel failed: ", err)
168 }
169 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
170 fmt.Println("compatibility error: ", err)
171 }
172 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
173 fmt.Println("compatibility error: ", err)
174 }
175 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100176
177 return nil
178}
179
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100180func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100181 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)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100184 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100185 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100186 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100187
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100188 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100189}
190
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100191func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100192 for i := 0; i < timeout; i++ {
193 o := vpp.vppctl("show app")
194 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100195 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100196 }
197 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100198 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100199 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100200}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100201
202func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100203 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100204) (interface_types.InterfaceIndex, error) {
205 createReq := &af_packet.AfPacketCreateV2{
206 UseRandomHwAddr: true,
207 HostIfName: veth.Name(),
208 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100209 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100210 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100211 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100212 }
213 createReply := &af_packet.AfPacketCreateV2Reply{}
214
215 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
216 return 0, err
217 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100218 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100219
220 // Set to up
221 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100222 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100223 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
224 }
225 upReply := &interfaces.SwInterfaceSetFlagsReply{}
226
227 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
228 return 0, err
229 }
230
231 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100232 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100233 var err error
234 var ip4Address string
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100235 if ip4Address, err = veth.addresser.newIp4Address(veth.peer.networkNumber); err == nil {
236 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100237 } else {
238 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100239 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240 }
241 addressReq := &interfaces.SwInterfaceAddDelAddress{
242 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100243 SwIfIndex: veth.index,
244 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100245 }
246 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
247
248 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
249 return 0, err
250 }
251
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100252 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253}
254
255func (vpp *VppInstance) addAppNamespace(
256 secret uint64,
257 ifx interface_types.InterfaceIndex,
258 namespaceId string,
259) error {
260 req := &session.AppNamespaceAddDelV2{
261 Secret: secret,
262 SwIfIndex: ifx,
263 NamespaceID: namespaceId,
264 }
265 reply := &session.AppNamespaceAddDelV2Reply{}
266
267 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
268 return err
269 }
270
271 sessionReq := &session.SessionEnableDisable{
272 IsEnable: true,
273 }
274 sessionReply := &session.SessionEnableDisableReply{}
275
276 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
277 return err
278 }
279
280 return nil
281}
282
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100283func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100284 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100285 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100286) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100287 var id uint32 = 1
288 if len(tapId) > 0 {
289 id = tapId[0]
290 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100291 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100292 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100293 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100294 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100295 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100296 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100297 }
298 createTapReply := &tapv2.TapCreateV2Reply{}
299
300 // Create tap interface
301 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
302 return err
303 }
304
305 // Add address
306 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
307 IsAdd: true,
308 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100309 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100310 }
311 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
312
313 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
314 return err
315 }
316
317 // Set interface to up
318 upReq := &interfaces.SwInterfaceSetFlags{
319 SwIfIndex: createTapReply.SwIfIndex,
320 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
321 }
322 upReply := &interfaces.SwInterfaceSetFlagsReply{}
323
324 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
325 return err
326 }
327
328 return nil
329}
330
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100331func (vpp *VppInstance) saveLogs() {
332 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100333 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100334 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100335 vpp.getSuite().T().Helper()
336 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100337 cmd.Run()
338}
339
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100340func (vpp *VppInstance) disconnect() {
341 vpp.connection.Disconnect()
342 vpp.apiChannel.Close()
343}