blob: e31b7929fd8f8455bf095adfa99cf1927f4e2fce [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 }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010061}
62
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010063logging {
64 default-log-level debug
65 default-syslog-log-level debug
66}
67
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010068`
69
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010070const (
71 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010072 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010073 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010074)
75
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010076type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010077 container *Container
Filip Tehlar608d0062023-04-28 10:29:47 +020078 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010079 connection *core.Connection
80 apiChannel api.Channel
Filip Tehlar608d0062023-04-28 10:29:47 +020081 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082}
83
Maros Ondrejickae7625d02023-02-28 16:55:01 +010084func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010085 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010086}
87
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010088func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010089 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010090}
91
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010092func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010093 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010094}
95
96func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010097 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010098}
99
100func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100101 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100102}
103
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100104func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100105 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100106 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100107
108 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
109 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
110 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
111
112 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100113 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100114 vppConfigTemplate,
115 containerWorkDir,
116 defaultCliSocketFilePath,
117 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100118 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100119 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200120 configContent += vpp.generateCpuConfig()
121 for _, c := range vpp.additionalConfig {
122 configContent += c.toString()
123 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100124 startupFileName := vpp.getEtcDir() + "/startup.conf"
125 vpp.container.createFile(startupFileName, configContent)
126
Filip Tehlar1a661502023-03-08 11:55:50 +0100127 // create wrapper script for vppctl with proper CLI socket path
128 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
129 vppcliFileName := "/usr/bin/vppcli"
130 vpp.container.createFile(vppcliFileName, cliContent)
131 vpp.container.exec("chmod 0755 " + vppcliFileName)
132
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100133 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100134 sig := make(chan os.Signal, 1)
135 signal.Notify(sig, syscall.SIGINT)
136 cont := make(chan bool, 1)
137 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100138 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100139 cont <- true
140 }()
141
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200142 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100143 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200144 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 +0100145 fmt.Println("Afterwards press CTRL+C to continue")
146 <-cont
147 fmt.Println("continuing...")
148 } else {
149 // Start VPP
150 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
151 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100152
153 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100154 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100155 conn, connEv, err := govpp.AsyncConnect(
156 sockAddress,
157 core.DefaultMaxReconnectAttempts,
158 core.DefaultReconnectInterval)
159 if err != nil {
160 fmt.Println("async connect error: ", err)
161 }
162 vpp.connection = conn
163
164 // ... wait for Connected event
165 e := <-connEv
166 if e.State != core.Connected {
167 fmt.Println("connecting to VPP failed: ", e.Error)
168 }
169
170 // ... check compatibility of used messages
171 ch, err := conn.NewAPIChannel()
172 if err != nil {
173 fmt.Println("creating channel failed: ", err)
174 }
175 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
176 fmt.Println("compatibility error: ", err)
177 }
178 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
179 fmt.Println("compatibility error: ", err)
180 }
181 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100182
183 return nil
184}
185
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100186func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100187 vppCliCommand := fmt.Sprintf(command, arguments...)
188 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
189 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100190 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100191 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100192 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100193
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100194 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100195}
196
Filip Tehlar543cd572023-06-27 10:01:37 +0200197func (vpp *VppInstance) GetSessionStat(stat string) int {
198 o := vpp.vppctl("show session stats")
199 vpp.getSuite().log(o)
200 for _, line := range strings.Split(o, "\n") {
201 if strings.Contains(line, stat) {
202 tokens := strings.Split(strings.TrimSpace(line), " ")
203 val, err := strconv.Atoi(tokens[0])
204 if err != nil {
205 vpp.getSuite().FailNow("failed to parse stat value %s", err)
206 return 0
207 }
208 return val
209 }
210 }
211 return 0
212}
213
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100214func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100215 for i := 0; i < timeout; i++ {
216 o := vpp.vppctl("show app")
217 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100218 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100219 }
220 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100221 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100222 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100223}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100224
225func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100226 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100227) (interface_types.InterfaceIndex, error) {
228 createReq := &af_packet.AfPacketCreateV2{
229 UseRandomHwAddr: true,
230 HostIfName: veth.Name(),
231 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100232 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100233 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100234 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100235 }
236 createReply := &af_packet.AfPacketCreateV2Reply{}
237
238 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
239 return 0, err
240 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100241 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100242
243 // Set to up
244 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100245 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100246 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
247 }
248 upReply := &interfaces.SwInterfaceSetFlagsReply{}
249
250 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
251 return 0, err
252 }
253
254 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100255 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100256 var err error
257 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200258 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100259 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100260 } else {
261 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100262 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100263 }
264 addressReq := &interfaces.SwInterfaceAddDelAddress{
265 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100266 SwIfIndex: veth.index,
267 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100268 }
269 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
270
271 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
272 return 0, err
273 }
274
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100275 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100276}
277
278func (vpp *VppInstance) addAppNamespace(
279 secret uint64,
280 ifx interface_types.InterfaceIndex,
281 namespaceId string,
282) error {
283 req := &session.AppNamespaceAddDelV2{
284 Secret: secret,
285 SwIfIndex: ifx,
286 NamespaceID: namespaceId,
287 }
288 reply := &session.AppNamespaceAddDelV2Reply{}
289
290 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
291 return err
292 }
293
294 sessionReq := &session.SessionEnableDisable{
295 IsEnable: true,
296 }
297 sessionReply := &session.SessionEnableDisableReply{}
298
299 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
300 return err
301 }
302
303 return nil
304}
305
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100306func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100307 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100308 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100309) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100310 var id uint32 = 1
311 if len(tapId) > 0 {
312 id = tapId[0]
313 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100314 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100315 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100316 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100317 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100318 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100319 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100320 }
321 createTapReply := &tapv2.TapCreateV2Reply{}
322
323 // Create tap interface
324 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
325 return err
326 }
327
328 // Add address
329 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
330 IsAdd: true,
331 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100332 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100333 }
334 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
335
336 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
337 return err
338 }
339
340 // Set interface to up
341 upReq := &interfaces.SwInterfaceSetFlags{
342 SwIfIndex: createTapReply.SwIfIndex,
343 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
344 }
345 upReply := &interfaces.SwInterfaceSetFlagsReply{}
346
347 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
348 return err
349 }
350
351 return nil
352}
353
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100354func (vpp *VppInstance) saveLogs() {
355 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100356 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100357 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100358 vpp.getSuite().T().Helper()
359 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100360 cmd.Run()
361}
362
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100363func (vpp *VppInstance) disconnect() {
364 vpp.connection.Disconnect()
365 vpp.apiChannel.Close()
366}
Filip Tehlar608d0062023-04-28 10:29:47 +0200367
368func (vpp *VppInstance) generateCpuConfig() string {
369 var c Stanza
370 var s string
371 if len(vpp.cpus) < 1 {
372 return ""
373 }
374 c.newStanza("cpu").
375 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
376 workers := vpp.cpus[1:]
377
378 if len(workers) > 0 {
379 for i := 0; i < len(workers); i++ {
380 if i != 0 {
381 s = s + ", "
382 }
383 s = s + fmt.Sprintf("%d", workers[i])
384 }
385 c.append(fmt.Sprintf("corelist-workers %s", s))
386 }
387 return c.close().toString()
388}