blob: e909b85abba1bc4db313406549d1edbd6305b51c [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"
Filip Tehlar543cd572023-06-27 10:01:37 +02008 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01009 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010010 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010011 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010012
Filip Tehlar9abba112023-03-07 10:13:19 +010013 "github.com/edwarnicke/exechelper"
14
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010015 "go.fd.io/govpp"
16 "go.fd.io/govpp/api"
17 "go.fd.io/govpp/binapi/af_packet"
18 interfaces "go.fd.io/govpp/binapi/interface"
19 "go.fd.io/govpp/binapi/interface_types"
20 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010021 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010022 "go.fd.io/govpp/binapi/vpe"
23 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010024)
25
26const vppConfigTemplate = `unix {
27 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010028 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010029 full-coredump
30 cli-listen %[1]s%[2]s
31 runtime-dir %[1]s/var/run
32 gid vpp
33}
34
35api-trace {
36 on
37}
38
39api-segment {
40 gid vpp
41}
42
43socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010044 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010045}
46
47statseg {
48 socket-name %[1]s/var/run/vpp/stats.sock
49}
50
51plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010052 plugin default { disable }
53
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010055 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010056 plugin af_packet_plugin.so { enable }
57 plugin hs_apps_plugin.so { enable }
58 plugin http_plugin.so { enable }
Filip Tehlarcc1475c2023-11-29 12:59:05 +010059 plugin http_static_plugin.so { enable }
60 plugin prom_plugin.so { enable }
Filip Tehlar3336eef2023-11-29 07:40:18 +010061 plugin tlsopenssl_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010062}
63
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010064logging {
65 default-log-level debug
66 default-syslog-log-level debug
67}
68
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010069`
70
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010071const (
72 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010073 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010074 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010075)
76
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010077type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010078 container *Container
Filip Tehlar608d0062023-04-28 10:29:47 +020079 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010080 connection *core.Connection
81 apiChannel api.Channel
Filip Tehlar608d0062023-04-28 10:29:47 +020082 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010083}
84
Maros Ondrejickae7625d02023-02-28 16:55:01 +010085func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010086 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010087}
88
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010089func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010090 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010091}
92
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010093func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010094 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010095}
96
97func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010098 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010099}
100
101func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100102 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103}
104
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100105func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100106 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100107 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100108
109 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
110 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
111 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
112
113 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100114 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100115 vppConfigTemplate,
116 containerWorkDir,
117 defaultCliSocketFilePath,
118 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100119 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100120 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200121 configContent += vpp.generateCpuConfig()
122 for _, c := range vpp.additionalConfig {
123 configContent += c.toString()
124 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100125 startupFileName := vpp.getEtcDir() + "/startup.conf"
126 vpp.container.createFile(startupFileName, configContent)
127
Filip Tehlar1a661502023-03-08 11:55:50 +0100128 // create wrapper script for vppctl with proper CLI socket path
129 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
130 vppcliFileName := "/usr/bin/vppcli"
131 vpp.container.createFile(vppcliFileName, cliContent)
132 vpp.container.exec("chmod 0755 " + vppcliFileName)
133
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100134 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100135 sig := make(chan os.Signal, 1)
136 signal.Notify(sig, syscall.SIGINT)
137 cont := make(chan bool, 1)
138 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100139 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100140 cont <- true
141 }()
142
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200143 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100144 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200145 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof vpp)\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100146 fmt.Println("Afterwards press CTRL+C to continue")
147 <-cont
148 fmt.Println("continuing...")
149 } else {
150 // Start VPP
151 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
152 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100153
154 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100155 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100156 conn, connEv, err := govpp.AsyncConnect(
157 sockAddress,
158 core.DefaultMaxReconnectAttempts,
159 core.DefaultReconnectInterval)
160 if err != nil {
161 fmt.Println("async connect error: ", err)
162 }
163 vpp.connection = conn
164
165 // ... wait for Connected event
166 e := <-connEv
167 if e.State != core.Connected {
168 fmt.Println("connecting to VPP failed: ", e.Error)
169 }
170
171 // ... check compatibility of used messages
172 ch, err := conn.NewAPIChannel()
173 if err != nil {
174 fmt.Println("creating channel failed: ", err)
175 }
176 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
177 fmt.Println("compatibility error: ", err)
178 }
179 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
180 fmt.Println("compatibility error: ", err)
181 }
182 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100183
184 return nil
185}
186
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100187func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100188 vppCliCommand := fmt.Sprintf(command, arguments...)
189 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
190 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100191 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100192 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100193 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100194
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100195 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100196}
197
Filip Tehlar543cd572023-06-27 10:01:37 +0200198func (vpp *VppInstance) GetSessionStat(stat string) int {
199 o := vpp.vppctl("show session stats")
200 vpp.getSuite().log(o)
201 for _, line := range strings.Split(o, "\n") {
202 if strings.Contains(line, stat) {
203 tokens := strings.Split(strings.TrimSpace(line), " ")
204 val, err := strconv.Atoi(tokens[0])
205 if err != nil {
206 vpp.getSuite().FailNow("failed to parse stat value %s", err)
207 return 0
208 }
209 return val
210 }
211 }
212 return 0
213}
214
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100215func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100216 for i := 0; i < timeout; i++ {
217 o := vpp.vppctl("show app")
218 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100219 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100220 }
221 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100222 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100223 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100224}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100225
226func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100227 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100228) (interface_types.InterfaceIndex, error) {
229 createReq := &af_packet.AfPacketCreateV2{
230 UseRandomHwAddr: true,
231 HostIfName: veth.Name(),
232 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100233 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100234 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100235 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100236 }
237 createReply := &af_packet.AfPacketCreateV2Reply{}
238
239 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
240 return 0, err
241 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100242 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100243
244 // Set to up
245 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100246 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100247 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
248 }
249 upReply := &interfaces.SwInterfaceSetFlagsReply{}
250
251 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
252 return 0, err
253 }
254
255 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100256 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100257 var err error
258 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200259 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100260 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100261 } else {
262 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100263 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100264 }
265 addressReq := &interfaces.SwInterfaceAddDelAddress{
266 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100267 SwIfIndex: veth.index,
268 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100269 }
270 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
271
272 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
273 return 0, err
274 }
275
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100276 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100277}
278
279func (vpp *VppInstance) addAppNamespace(
280 secret uint64,
281 ifx interface_types.InterfaceIndex,
282 namespaceId string,
283) error {
284 req := &session.AppNamespaceAddDelV2{
285 Secret: secret,
286 SwIfIndex: ifx,
287 NamespaceID: namespaceId,
288 }
289 reply := &session.AppNamespaceAddDelV2Reply{}
290
291 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
292 return err
293 }
294
295 sessionReq := &session.SessionEnableDisable{
296 IsEnable: true,
297 }
298 sessionReply := &session.SessionEnableDisableReply{}
299
300 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
301 return err
302 }
303
304 return nil
305}
306
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100307func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100308 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100309 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100310) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100311 var id uint32 = 1
312 if len(tapId) > 0 {
313 id = tapId[0]
314 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100315 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100316 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100317 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100318 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100319 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100320 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100321 }
322 createTapReply := &tapv2.TapCreateV2Reply{}
323
324 // Create tap interface
325 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
326 return err
327 }
328
329 // Add address
330 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
331 IsAdd: true,
332 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100333 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100334 }
335 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
336
337 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
338 return err
339 }
340
341 // Set interface to up
342 upReq := &interfaces.SwInterfaceSetFlags{
343 SwIfIndex: createTapReply.SwIfIndex,
344 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
345 }
346 upReply := &interfaces.SwInterfaceSetFlagsReply{}
347
348 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
349 return err
350 }
351
352 return nil
353}
354
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100355func (vpp *VppInstance) saveLogs() {
356 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100357 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100358 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100359 vpp.getSuite().T().Helper()
360 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100361 cmd.Run()
362}
363
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100364func (vpp *VppInstance) disconnect() {
365 vpp.connection.Disconnect()
366 vpp.apiChannel.Close()
367}
Filip Tehlar608d0062023-04-28 10:29:47 +0200368
369func (vpp *VppInstance) generateCpuConfig() string {
370 var c Stanza
371 var s string
372 if len(vpp.cpus) < 1 {
373 return ""
374 }
375 c.newStanza("cpu").
376 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
377 workers := vpp.cpus[1:]
378
379 if len(workers) > 0 {
380 for i := 0; i < len(workers); i++ {
381 if i != 0 {
382 s = s + ", "
383 }
384 s = s + fmt.Sprintf("%d", workers[i])
385 }
386 c.append(fmt.Sprintf("corelist-workers %s", s))
387 }
388 return c.close().toString()
389}