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