blob: a9b97bcaa0bc313b25d3eec1cb97192a727e112c [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
Filip Tehlar608d0062023-04-28 10:29:47 +020075 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010076 connection *core.Connection
77 apiChannel api.Channel
Filip Tehlar608d0062023-04-28 10:29:47 +020078 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010079}
80
Maros Ondrejickae7625d02023-02-28 16:55:01 +010081func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010082 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010083}
84
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010085func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010086 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010087}
88
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010089func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010090 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010091}
92
93func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010094 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010095}
96
97func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010098 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010099}
100
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100101func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100102 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100103 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100104
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())
108
109 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100110 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100111 vppConfigTemplate,
112 containerWorkDir,
113 defaultCliSocketFilePath,
114 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100115 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100116 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200117 configContent += vpp.generateCpuConfig()
118 for _, c := range vpp.additionalConfig {
119 configContent += c.toString()
120 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100121 startupFileName := vpp.getEtcDir() + "/startup.conf"
122 vpp.container.createFile(startupFileName, configContent)
123
Filip Tehlar1a661502023-03-08 11:55:50 +0100124 // 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)
129
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100130 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100131 sig := make(chan os.Signal, 1)
132 signal.Notify(sig, syscall.SIGINT)
133 cont := make(chan bool, 1)
134 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100135 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100136 cont <- true
137 }()
138
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")
144 <-cont
145 fmt.Println("continuing...")
146 } else {
147 // Start VPP
148 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
149 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100150
151 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100152 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100153 conn, connEv, err := govpp.AsyncConnect(
154 sockAddress,
155 core.DefaultMaxReconnectAttempts,
156 core.DefaultReconnectInterval)
157 if err != nil {
158 fmt.Println("async connect error: ", err)
159 }
160 vpp.connection = conn
161
162 // ... wait for Connected event
163 e := <-connEv
164 if e.State != core.Connected {
165 fmt.Println("connecting to VPP failed: ", e.Error)
166 }
167
168 // ... check compatibility of used messages
169 ch, err := conn.NewAPIChannel()
170 if err != nil {
171 fmt.Println("creating channel failed: ", err)
172 }
173 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
174 fmt.Println("compatibility error: ", err)
175 }
176 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
177 fmt.Println("compatibility error: ", err)
178 }
179 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100180
181 return nil
182}
183
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100184func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100185 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)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100188 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100189 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100190 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100191
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100192 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100193}
194
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100195func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100196 for i := 0; i < timeout; i++ {
197 o := vpp.vppctl("show app")
198 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100199 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100200 }
201 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100202 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100203 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100204}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205
206func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100207 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100208) (interface_types.InterfaceIndex, error) {
209 createReq := &af_packet.AfPacketCreateV2{
210 UseRandomHwAddr: true,
211 HostIfName: veth.Name(),
212 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100213 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100214 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100215 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100216 }
217 createReply := &af_packet.AfPacketCreateV2Reply{}
218
219 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
220 return 0, err
221 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100222 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100223
224 // Set to up
225 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100226 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100227 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
228 }
229 upReply := &interfaces.SwInterfaceSetFlagsReply{}
230
231 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
232 return 0, err
233 }
234
235 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100236 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100237 var err error
238 var ip4Address string
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100239 if ip4Address, err = veth.addresser.newIp4Address(veth.peer.networkNumber); err == nil {
240 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100241 } else {
242 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100243 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100244 }
245 addressReq := &interfaces.SwInterfaceAddDelAddress{
246 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100247 SwIfIndex: veth.index,
248 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100249 }
250 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
251
252 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
253 return 0, err
254 }
255
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100256 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100257}
258
259func (vpp *VppInstance) addAppNamespace(
260 secret uint64,
261 ifx interface_types.InterfaceIndex,
262 namespaceId string,
263) error {
264 req := &session.AppNamespaceAddDelV2{
265 Secret: secret,
266 SwIfIndex: ifx,
267 NamespaceID: namespaceId,
268 }
269 reply := &session.AppNamespaceAddDelV2Reply{}
270
271 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
272 return err
273 }
274
275 sessionReq := &session.SessionEnableDisable{
276 IsEnable: true,
277 }
278 sessionReply := &session.SessionEnableDisableReply{}
279
280 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
281 return err
282 }
283
284 return nil
285}
286
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100287func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100288 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100289 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100290) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100291 var id uint32 = 1
292 if len(tapId) > 0 {
293 id = tapId[0]
294 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100295 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100296 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100297 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100298 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100299 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100300 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100301 }
302 createTapReply := &tapv2.TapCreateV2Reply{}
303
304 // Create tap interface
305 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
306 return err
307 }
308
309 // Add address
310 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
311 IsAdd: true,
312 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100313 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100314 }
315 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
316
317 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
318 return err
319 }
320
321 // Set interface to up
322 upReq := &interfaces.SwInterfaceSetFlags{
323 SwIfIndex: createTapReply.SwIfIndex,
324 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
325 }
326 upReply := &interfaces.SwInterfaceSetFlagsReply{}
327
328 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
329 return err
330 }
331
332 return nil
333}
334
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100335func (vpp *VppInstance) saveLogs() {
336 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100337 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100338 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100339 vpp.getSuite().T().Helper()
340 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100341 cmd.Run()
342}
343
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100344func (vpp *VppInstance) disconnect() {
345 vpp.connection.Disconnect()
346 vpp.apiChannel.Close()
347}
Filip Tehlar608d0062023-04-28 10:29:47 +0200348
349func (vpp *VppInstance) generateCpuConfig() string {
350 var c Stanza
351 var s string
352 if len(vpp.cpus) < 1 {
353 return ""
354 }
355 c.newStanza("cpu").
356 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
357 workers := vpp.cpus[1:]
358
359 if len(workers) > 0 {
360 for i := 0; i < len(workers); i++ {
361 if i != 0 {
362 s = s + ", "
363 }
364 s = s + fmt.Sprintf("%d", workers[i])
365 }
366 c.append(fmt.Sprintf("corelist-workers %s", s))
367 }
368 return c.close().toString()
369}