Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 4 | "context" |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 5 | "encoding/json" |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 6 | "fmt" |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 7 | "go.fd.io/govpp/binapi/ethernet_types" |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 8 | "io" |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 9 | "net" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 10 | "os" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 11 | "os/exec" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 12 | "os/signal" |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 13 | "strconv" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 14 | "strings" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 15 | "syscall" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 16 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 17 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 18 | "github.com/edwarnicke/exechelper" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 19 | . "github.com/onsi/ginkgo/v2" |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 20 | "github.com/sirupsen/logrus" |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 21 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 22 | "go.fd.io/govpp" |
| 23 | "go.fd.io/govpp/api" |
| 24 | "go.fd.io/govpp/binapi/af_packet" |
| 25 | interfaces "go.fd.io/govpp/binapi/interface" |
| 26 | "go.fd.io/govpp/binapi/interface_types" |
| 27 | "go.fd.io/govpp/binapi/session" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 28 | "go.fd.io/govpp/binapi/tapv2" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 29 | "go.fd.io/govpp/core" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | const vppConfigTemplate = `unix { |
| 33 | nodaemon |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 34 | log %[1]s%[4]s |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 35 | full-coredump |
| 36 | cli-listen %[1]s%[2]s |
| 37 | runtime-dir %[1]s/var/run |
| 38 | gid vpp |
| 39 | } |
| 40 | |
| 41 | api-trace { |
| 42 | on |
| 43 | } |
| 44 | |
| 45 | api-segment { |
| 46 | gid vpp |
| 47 | } |
| 48 | |
| 49 | socksvr { |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 50 | socket-name %[1]s%[3]s |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | statseg { |
| 54 | socket-name %[1]s/var/run/vpp/stats.sock |
| 55 | } |
| 56 | |
| 57 | plugins { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 58 | plugin default { disable } |
| 59 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 60 | plugin unittest_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 61 | plugin quic_plugin.so { enable } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 62 | plugin af_packet_plugin.so { enable } |
| 63 | plugin hs_apps_plugin.so { enable } |
| 64 | plugin http_plugin.so { enable } |
Filip Tehlar | cc1475c | 2023-11-29 12:59:05 +0100 | [diff] [blame] | 65 | plugin http_static_plugin.so { enable } |
| 66 | plugin prom_plugin.so { enable } |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 67 | plugin tlsopenssl_plugin.so { enable } |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 68 | plugin ping_plugin.so { enable } |
Matus Fabian | c899ab4 | 2024-04-22 13:42:00 +0200 | [diff] [blame] | 69 | plugin nsim_plugin.so { enable } |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 70 | plugin mactime_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 73 | logging { |
| 74 | default-log-level debug |
| 75 | default-syslog-log-level debug |
| 76 | } |
| 77 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 78 | ` |
| 79 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 80 | const ( |
| 81 | defaultCliSocketFilePath = "/var/run/vpp/cli.sock" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 82 | defaultApiSocketFilePath = "/var/run/vpp/api.sock" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 83 | defaultLogFilePath = "/var/log/vpp/vpp.log" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 84 | ) |
| 85 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 86 | type VppInstance struct { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 87 | Container *Container |
| 88 | AdditionalConfig []Stanza |
| 89 | Connection *core.Connection |
| 90 | ApiStream api.Stream |
| 91 | Cpus []int |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 92 | CpuConfig VppCpuConfig |
| 93 | } |
| 94 | |
| 95 | type VppCpuConfig struct { |
| 96 | PinMainCpu bool |
| 97 | PinWorkersCorelist bool |
| 98 | SkipCores int |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 101 | type VppMemTrace struct { |
| 102 | Count int `json:"count"` |
| 103 | Size int `json:"bytes"` |
| 104 | Sample string `json:"sample"` |
| 105 | Traceback []string `json:"traceback"` |
| 106 | } |
| 107 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 108 | func (vpp *VppInstance) getSuite() *HstSuite { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 109 | return vpp.Container.Suite |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 112 | func (vpp *VppInstance) getCliSocket() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 113 | return fmt.Sprintf("%s%s", vpp.Container.GetContainerWorkDir(), defaultCliSocketFilePath) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 116 | func (vpp *VppInstance) getRunDir() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 117 | return vpp.Container.GetContainerWorkDir() + "/var/run/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | func (vpp *VppInstance) getLogDir() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 121 | return vpp.Container.GetContainerWorkDir() + "/var/log/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | func (vpp *VppInstance) getEtcDir() string { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 125 | return vpp.Container.GetContainerWorkDir() + "/etc/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 128 | func (vpp *VppInstance) Start() error { |
Adrian Villin | 93974e2 | 2024-05-30 06:10:59 -0400 | [diff] [blame] | 129 | maxReconnectAttempts := 3 |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 130 | // Replace default logger in govpp with our own |
| 131 | govppLogger := logrus.New() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 132 | govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().Logger.Writer(), GinkgoWriter)) |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 133 | core.SetLogger(govppLogger) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 134 | // Create folders |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 135 | containerWorkDir := vpp.Container.GetContainerWorkDir() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 136 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 137 | vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getRunDir()) |
| 138 | vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getLogDir()) |
| 139 | vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getEtcDir()) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 140 | |
| 141 | // Create startup.conf inside the container |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 142 | configContent := fmt.Sprintf( |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 143 | vppConfigTemplate, |
| 144 | containerWorkDir, |
| 145 | defaultCliSocketFilePath, |
| 146 | defaultApiSocketFilePath, |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 147 | defaultLogFilePath, |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 148 | ) |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 149 | configContent += vpp.generateVPPCpuConfig() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 150 | for _, c := range vpp.AdditionalConfig { |
| 151 | configContent += c.ToString() |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 152 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 153 | startupFileName := vpp.getEtcDir() + "/startup.conf" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 154 | vpp.Container.CreateFile(startupFileName, configContent) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 155 | |
Filip Tehlar | 1a66150 | 2023-03-08 11:55:50 +0100 | [diff] [blame] | 156 | // create wrapper script for vppctl with proper CLI socket path |
| 157 | cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock" |
| 158 | vppcliFileName := "/usr/bin/vppcli" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 159 | vpp.Container.CreateFile(vppcliFileName, cliContent) |
| 160 | vpp.Container.Exec("chmod 0755 " + vppcliFileName) |
Filip Tehlar | 1a66150 | 2023-03-08 11:55:50 +0100 | [diff] [blame] | 161 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 162 | vpp.getSuite().Log("starting vpp") |
| 163 | if *IsVppDebug { |
Adrian Villin | 93974e2 | 2024-05-30 06:10:59 -0400 | [diff] [blame] | 164 | // default = 3; VPP will timeout while debugging if there are not enough attempts |
| 165 | maxReconnectAttempts = 5000 |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 166 | sig := make(chan os.Signal, 1) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 167 | signal.Notify(sig, syscall.SIGQUIT) |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 168 | cont := make(chan bool, 1) |
| 169 | go func() { |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 170 | <-sig |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 171 | cont <- true |
| 172 | }() |
| 173 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 174 | vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 175 | fmt.Println("run following command in different terminal:") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 176 | fmt.Println("docker exec -it " + vpp.Container.Name + " gdb -ex \"attach $(docker exec " + vpp.Container.Name + " pidof vpp)\"") |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 177 | fmt.Println("Afterwards press CTRL+\\ to continue") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 178 | <-cont |
| 179 | fmt.Println("continuing...") |
| 180 | } else { |
| 181 | // Start VPP |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 182 | vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 183 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 184 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 185 | vpp.getSuite().Log("connecting to vpp") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 186 | // Connect to VPP and store the connection |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 187 | sockAddress := vpp.Container.GetHostWorkDir() + defaultApiSocketFilePath |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 188 | conn, connEv, err := govpp.AsyncConnect( |
| 189 | sockAddress, |
Adrian Villin | 93974e2 | 2024-05-30 06:10:59 -0400 | [diff] [blame] | 190 | maxReconnectAttempts, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 191 | core.DefaultReconnectInterval) |
| 192 | if err != nil { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 193 | vpp.getSuite().Log("async connect error: " + fmt.Sprint(err)) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 194 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 195 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 196 | vpp.Connection = conn |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 197 | |
| 198 | // ... wait for Connected event |
| 199 | e := <-connEv |
| 200 | if e.State != core.Connected { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 201 | vpp.getSuite().Log("connecting to VPP failed: " + fmt.Sprint(e.Error)) |
Hadi Rayan Al-Sandid | 0eccf45 | 2024-06-24 10:48:54 +0200 | [diff] [blame] | 202 | return e.Error |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 205 | ch, err := conn.NewStream( |
| 206 | context.Background(), |
| 207 | core.WithRequestSize(50), |
| 208 | core.WithReplySize(50), |
Adrian Villin | 93974e2 | 2024-05-30 06:10:59 -0400 | [diff] [blame] | 209 | core.WithReplyTimeout(time.Second*5)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 210 | if err != nil { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 211 | vpp.getSuite().Log("creating stream failed: " + fmt.Sprint(err)) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 212 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 213 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 214 | vpp.ApiStream = ch |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 215 | |
| 216 | return nil |
| 217 | } |
| 218 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 219 | func (vpp *VppInstance) Vppctl(command string, arguments ...any) string { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 220 | vppCliCommand := fmt.Sprintf(command, arguments...) |
| 221 | containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 222 | vpp.Container.Name, vpp.getCliSocket(), vppCliCommand) |
| 223 | vpp.getSuite().Log(containerExecCommand) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 224 | output, err := exechelper.CombinedOutput(containerExecCommand) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 225 | vpp.getSuite().AssertNil(err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 226 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 227 | return string(output) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 230 | func (vpp *VppInstance) GetSessionStat(stat string) int { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 231 | o := vpp.Vppctl("show session stats") |
| 232 | vpp.getSuite().Log(o) |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 233 | for _, line := range strings.Split(o, "\n") { |
| 234 | if strings.Contains(line, stat) { |
| 235 | tokens := strings.Split(strings.TrimSpace(line), " ") |
| 236 | val, err := strconv.Atoi(tokens[0]) |
| 237 | if err != nil { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 238 | Fail("failed to parse stat value %s" + fmt.Sprint(err)) |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 239 | return 0 |
| 240 | } |
| 241 | return val |
| 242 | } |
| 243 | } |
| 244 | return 0 |
| 245 | } |
| 246 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 247 | func (vpp *VppInstance) WaitForApp(appName string, timeout int) { |
| 248 | vpp.getSuite().Log("waiting for app " + appName) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 249 | for i := 0; i < timeout; i++ { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 250 | o := vpp.Vppctl("show app") |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 251 | if strings.Contains(o, appName) { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 252 | return |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 253 | } |
| 254 | time.Sleep(1 * time.Second) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 255 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 256 | vpp.getSuite().AssertNil(1, "Timeout while waiting for app '%s'", appName) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 257 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 258 | |
| 259 | func (vpp *VppInstance) createAfPacket( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 260 | veth *NetInterface, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 261 | ) (interface_types.InterfaceIndex, error) { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 262 | createReq := &af_packet.AfPacketCreateV3{ |
| 263 | Mode: 1, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 264 | UseRandomHwAddr: true, |
| 265 | HostIfName: veth.Name(), |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 266 | Flags: af_packet.AfPacketFlags(11), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 267 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 268 | if veth.HwAddress != (MacAddress{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 269 | createReq.UseRandomHwAddr = false |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 270 | createReq.HwAddr = veth.HwAddress |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 271 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 272 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 273 | vpp.getSuite().Log("create af-packet interface " + veth.Name()) |
| 274 | if err := vpp.ApiStream.SendMsg(createReq); err != nil { |
| 275 | vpp.getSuite().HstFail() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 276 | return 0, err |
| 277 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 278 | replymsg, err := vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 279 | if err != nil { |
| 280 | return 0, err |
| 281 | } |
| 282 | reply := replymsg.(*af_packet.AfPacketCreateV3Reply) |
| 283 | err = api.RetvalToVPPApiError(reply.Retval) |
| 284 | if err != nil { |
| 285 | return 0, err |
| 286 | } |
| 287 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 288 | veth.Index = reply.SwIfIndex |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 289 | |
| 290 | // Set to up |
| 291 | upReq := &interfaces.SwInterfaceSetFlags{ |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 292 | SwIfIndex: veth.Index, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 293 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 294 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 295 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 296 | vpp.getSuite().Log("set af-packet interface " + veth.Name() + " up") |
| 297 | if err := vpp.ApiStream.SendMsg(upReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 298 | return 0, err |
| 299 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 300 | replymsg, err = vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 301 | if err != nil { |
| 302 | return 0, err |
| 303 | } |
| 304 | reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply) |
| 305 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 306 | return 0, err |
| 307 | } |
| 308 | |
| 309 | // Add address |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 310 | if veth.AddressWithPrefix() == (AddressWithPrefix{}) { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 311 | var err error |
| 312 | var ip4Address string |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 313 | if ip4Address, err = veth.Ip4AddrAllocator.NewIp4InterfaceAddress(veth.Peer.NetworkNumber); err == nil { |
| 314 | veth.Ip4Address = ip4Address |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 315 | } else { |
| 316 | return 0, err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 317 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 318 | } |
| 319 | addressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 320 | IsAdd: true, |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 321 | SwIfIndex: veth.Index, |
| 322 | Prefix: veth.AddressWithPrefix(), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 323 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 324 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 325 | vpp.getSuite().Log("af-packet interface " + veth.Name() + " add address " + veth.Ip4Address) |
| 326 | if err := vpp.ApiStream.SendMsg(addressReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 327 | return 0, err |
| 328 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 329 | replymsg, err = vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 330 | if err != nil { |
| 331 | return 0, err |
| 332 | } |
| 333 | reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply) |
| 334 | err = api.RetvalToVPPApiError(reply3.Retval) |
| 335 | if err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 336 | return 0, err |
| 337 | } |
| 338 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 339 | return veth.Index, nil |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | func (vpp *VppInstance) addAppNamespace( |
| 343 | secret uint64, |
| 344 | ifx interface_types.InterfaceIndex, |
| 345 | namespaceId string, |
| 346 | ) error { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 347 | req := &session.AppNamespaceAddDelV4{ |
| 348 | IsAdd: true, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 349 | Secret: secret, |
| 350 | SwIfIndex: ifx, |
| 351 | NamespaceID: namespaceId, |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 352 | SockName: defaultApiSocketFilePath, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 353 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 354 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 355 | vpp.getSuite().Log("add app namespace " + namespaceId) |
| 356 | if err := vpp.ApiStream.SendMsg(req); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 357 | return err |
| 358 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 359 | replymsg, err := vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 360 | if err != nil { |
| 361 | return err |
| 362 | } |
| 363 | reply := replymsg.(*session.AppNamespaceAddDelV4Reply) |
| 364 | if err = api.RetvalToVPPApiError(reply.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 365 | return err |
| 366 | } |
| 367 | |
| 368 | sessionReq := &session.SessionEnableDisable{ |
| 369 | IsEnable: true, |
| 370 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 371 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 372 | vpp.getSuite().Log("enable app namespace " + namespaceId) |
| 373 | if err := vpp.ApiStream.SendMsg(sessionReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 374 | return err |
| 375 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 376 | replymsg, err = vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 377 | if err != nil { |
| 378 | return err |
| 379 | } |
| 380 | reply2 := replymsg.(*session.SessionEnableDisableReply) |
| 381 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 382 | return err |
| 383 | } |
| 384 | |
| 385 | return nil |
| 386 | } |
| 387 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 388 | func (vpp *VppInstance) createTap( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 389 | tap *NetInterface, |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 390 | tapId ...uint32, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 391 | ) error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 392 | var id uint32 = 1 |
| 393 | if len(tapId) > 0 { |
| 394 | id = tapId[0] |
| 395 | } |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 396 | createTapReq := &tapv2.TapCreateV3{ |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 397 | ID: id, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 398 | HostIfNameSet: true, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 399 | HostIfName: tap.Name(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 400 | HostIP4PrefixSet: true, |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 401 | HostIP4Prefix: tap.Ip4AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 402 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 403 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 404 | vpp.getSuite().Log("create tap interface " + tap.Name()) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 405 | // Create tap interface |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 406 | if err := vpp.ApiStream.SendMsg(createTapReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 407 | return err |
| 408 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 409 | replymsg, err := vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 410 | if err != nil { |
| 411 | return err |
| 412 | } |
| 413 | reply := replymsg.(*tapv2.TapCreateV3Reply) |
| 414 | if err = api.RetvalToVPPApiError(reply.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 415 | return err |
| 416 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 417 | tap.Peer.Index = reply.SwIfIndex |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 418 | |
| 419 | // Get name and mac |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 420 | if err := vpp.ApiStream.SendMsg(&interfaces.SwInterfaceDump{ |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 421 | SwIfIndex: reply.SwIfIndex, |
| 422 | }); err != nil { |
| 423 | return err |
| 424 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 425 | replymsg, err = vpp.ApiStream.RecvMsg() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 426 | if err != nil { |
| 427 | return err |
| 428 | } |
| 429 | ifDetails := replymsg.(*interfaces.SwInterfaceDetails) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 430 | tap.Peer.name = ifDetails.InterfaceName |
| 431 | tap.Peer.HwAddress = ifDetails.L2Address |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 432 | |
| 433 | // Add address |
| 434 | addAddressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 435 | IsAdd: true, |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 436 | SwIfIndex: reply.SwIfIndex, |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 437 | Prefix: tap.Peer.AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 438 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 439 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 440 | vpp.getSuite().Log("tap interface " + tap.Name() + " add address " + tap.Peer.Ip4Address) |
| 441 | if err := vpp.ApiStream.SendMsg(addAddressReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 442 | return err |
| 443 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 444 | replymsg, err = vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 445 | if err != nil { |
| 446 | return err |
| 447 | } |
| 448 | reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply) |
| 449 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 450 | return err |
| 451 | } |
| 452 | |
| 453 | // Set interface to up |
| 454 | upReq := &interfaces.SwInterfaceSetFlags{ |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 455 | SwIfIndex: reply.SwIfIndex, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 456 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 457 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 458 | |
| 459 | vpp.getSuite().Log("set tap interface " + tap.Name() + " up") |
| 460 | if err := vpp.ApiStream.SendMsg(upReq); err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 461 | return err |
| 462 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 463 | replymsg, err = vpp.ApiStream.RecvMsg() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame] | 464 | if err != nil { |
| 465 | return err |
| 466 | } |
| 467 | reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply) |
| 468 | if err = api.RetvalToVPPApiError(reply3.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 469 | return err |
| 470 | } |
| 471 | |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 472 | // Get host mac |
| 473 | netIntf, err := net.InterfaceByName(tap.Name()) |
| 474 | if err == nil { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 475 | tap.HwAddress, _ = ethernet_types.ParseMacAddress(netIntf.HardwareAddr.String()) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 476 | } |
| 477 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 478 | return nil |
| 479 | } |
| 480 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 481 | func (vpp *VppInstance) saveLogs() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 482 | logTarget := vpp.Container.getLogDirPath() + "vppinstance-" + vpp.Container.Name + ".log" |
| 483 | logSource := vpp.Container.GetHostWorkDir() + defaultLogFilePath |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 484 | cmd := exec.Command("cp", logSource, logTarget) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 485 | vpp.getSuite().Log(cmd.String()) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 486 | cmd.Run() |
| 487 | } |
| 488 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 489 | func (vpp *VppInstance) Disconnect() { |
| 490 | vpp.Connection.Disconnect() |
| 491 | vpp.ApiStream.Close() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 492 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 493 | |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 494 | func (vpp *VppInstance) setDefaultCpuConfig() { |
| 495 | vpp.CpuConfig.PinMainCpu = true |
| 496 | vpp.CpuConfig.PinWorkersCorelist = true |
| 497 | vpp.CpuConfig.SkipCores = 0 |
| 498 | } |
| 499 | |
| 500 | func (vpp *VppInstance) generateVPPCpuConfig() string { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 501 | var c Stanza |
| 502 | var s string |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 503 | startCpu := 0 |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 504 | if len(vpp.Cpus) < 1 { |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 505 | return "" |
| 506 | } |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 507 | |
| 508 | c.NewStanza("cpu") |
| 509 | |
| 510 | // If skip-cores is valid, use as start value to assign main/workers CPUs |
| 511 | if vpp.CpuConfig.SkipCores != 0 { |
| 512 | c.Append(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores)) |
| 513 | vpp.getSuite().Log(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores)) |
| 514 | } |
| 515 | |
| 516 | if len(vpp.Cpus) > vpp.CpuConfig.SkipCores { |
| 517 | startCpu = vpp.CpuConfig.SkipCores |
| 518 | } |
| 519 | |
| 520 | if vpp.CpuConfig.PinMainCpu { |
| 521 | c.Append(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu])) |
| 522 | vpp.getSuite().Log(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu])) |
| 523 | } |
| 524 | |
| 525 | workers := vpp.Cpus[startCpu+1:] |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 526 | |
| 527 | if len(workers) > 0 { |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 528 | if vpp.CpuConfig.PinWorkersCorelist { |
| 529 | for i := 0; i < len(workers); i++ { |
| 530 | if i != 0 { |
| 531 | s = s + ", " |
| 532 | } |
| 533 | s = s + fmt.Sprintf("%d", workers[i]) |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 534 | } |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 535 | c.Append(fmt.Sprintf("corelist-workers %s", s)) |
| 536 | vpp.getSuite().Log("corelist-workers " + s) |
| 537 | } else { |
| 538 | s = fmt.Sprintf("%d", len(workers)) |
| 539 | c.Append(fmt.Sprintf("workers %s", s)) |
| 540 | vpp.getSuite().Log("workers " + s) |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 541 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 542 | } |
Hadi Rayan Al-Sandid | e0e8513 | 2024-06-24 10:28:58 +0200 | [diff] [blame] | 543 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 544 | return c.Close().ToString() |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 545 | } |
Matus Fabian | e99d266 | 2024-07-19 16:04:09 +0200 | [diff] [blame] | 546 | |
| 547 | // EnableMemoryTrace enables memory traces of VPP main-heap |
| 548 | func (vpp *VppInstance) EnableMemoryTrace() { |
| 549 | vpp.getSuite().Log(vpp.Vppctl("memory-trace on main-heap")) |
| 550 | } |
| 551 | |
| 552 | // GetMemoryTrace dumps memory traces for analysis |
| 553 | func (vpp *VppInstance) GetMemoryTrace() ([]VppMemTrace, error) { |
| 554 | var trace []VppMemTrace |
| 555 | vpp.getSuite().Log(vpp.Vppctl("save memory-trace trace.json")) |
| 556 | err := vpp.Container.GetFile("/tmp/trace.json", "/tmp/trace.json") |
| 557 | if err != nil { |
| 558 | return nil, err |
| 559 | } |
| 560 | fileBytes, err := os.ReadFile("/tmp/trace.json") |
| 561 | if err != nil { |
| 562 | return nil, err |
| 563 | } |
| 564 | err = json.Unmarshal(fileBytes, &trace) |
| 565 | if err != nil { |
| 566 | return nil, err |
| 567 | } |
| 568 | return trace, nil |
| 569 | } |
| 570 | |
| 571 | // memTracesSuppressCli filter out CLI related samples |
| 572 | func memTracesSuppressCli(traces []VppMemTrace) []VppMemTrace { |
| 573 | var filtered []VppMemTrace |
| 574 | for i := 0; i < len(traces); i++ { |
| 575 | isCli := false |
| 576 | for j := 0; j < len(traces[i].Traceback); j++ { |
| 577 | if strings.Contains(traces[i].Traceback[j], "unix_cli") { |
| 578 | isCli = true |
| 579 | break |
| 580 | } |
| 581 | } |
| 582 | if !isCli { |
| 583 | filtered = append(filtered, traces[i]) |
| 584 | } |
| 585 | } |
| 586 | return filtered |
| 587 | } |
| 588 | |
| 589 | // MemLeakCheck compares memory traces at different point in time, analyzes if memory leaks happen and produces report |
| 590 | func (vpp *VppInstance) MemLeakCheck(first, second []VppMemTrace) { |
| 591 | totalBytes := 0 |
| 592 | totalCounts := 0 |
| 593 | trace1 := memTracesSuppressCli(first) |
| 594 | trace2 := memTracesSuppressCli(second) |
| 595 | report := "" |
| 596 | for i := 0; i < len(trace2); i++ { |
| 597 | match := false |
| 598 | for j := 0; j < len(trace1); j++ { |
| 599 | if trace1[j].Sample == trace2[i].Sample { |
| 600 | if trace2[i].Size > trace1[j].Size { |
| 601 | deltaBytes := trace2[i].Size - trace1[j].Size |
| 602 | deltaCounts := trace2[i].Count - trace1[j].Count |
| 603 | report += fmt.Sprintf("grow %d byte(s) in %d allocation(s) from:\n", deltaBytes, deltaCounts) |
| 604 | for j := 0; j < len(trace2[i].Traceback); j++ { |
| 605 | report += fmt.Sprintf("\t#%d %s\n", j, trace2[i].Traceback[j]) |
| 606 | } |
| 607 | totalBytes += deltaBytes |
| 608 | totalCounts += deltaCounts |
| 609 | } |
| 610 | match = true |
| 611 | break |
| 612 | } |
| 613 | } |
| 614 | if !match { |
| 615 | report += fmt.Sprintf("\nleak of %d byte(s) in %d allocation(s) from:\n", trace2[i].Size, trace2[i].Count) |
| 616 | for j := 0; j < len(trace2[i].Traceback); j++ { |
| 617 | report += fmt.Sprintf("\t#%d %s\n", j, trace2[i].Traceback[j]) |
| 618 | } |
| 619 | totalBytes += trace2[i].Size |
| 620 | totalCounts += trace2[i].Count |
| 621 | } |
| 622 | } |
| 623 | summary := fmt.Sprintf("\nSUMMARY: %d byte(s) leaked in %d allocation(s)\n", totalBytes, totalCounts) |
| 624 | AddReportEntry(summary, report) |
| 625 | } |