Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 4 | "context" |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 5 | "fmt" |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 6 | "io" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 7 | "os" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 8 | "os/exec" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 9 | "os/signal" |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 10 | "strconv" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 11 | "strings" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 12 | "syscall" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 13 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 14 | |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 15 | "github.com/edwarnicke/exechelper" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 16 | . "github.com/onsi/ginkgo/v2" |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 17 | "github.com/sirupsen/logrus" |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 18 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 19 | "go.fd.io/govpp" |
| 20 | "go.fd.io/govpp/api" |
| 21 | "go.fd.io/govpp/binapi/af_packet" |
| 22 | interfaces "go.fd.io/govpp/binapi/interface" |
| 23 | "go.fd.io/govpp/binapi/interface_types" |
| 24 | "go.fd.io/govpp/binapi/session" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 25 | "go.fd.io/govpp/binapi/tapv2" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 26 | "go.fd.io/govpp/core" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | const vppConfigTemplate = `unix { |
| 30 | nodaemon |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 31 | log %[1]s%[4]s |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 32 | full-coredump |
| 33 | cli-listen %[1]s%[2]s |
| 34 | runtime-dir %[1]s/var/run |
| 35 | gid vpp |
| 36 | } |
| 37 | |
| 38 | api-trace { |
| 39 | on |
| 40 | } |
| 41 | |
| 42 | api-segment { |
| 43 | gid vpp |
| 44 | } |
| 45 | |
| 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 } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 69 | logging { |
| 70 | default-log-level debug |
| 71 | default-syslog-log-level debug |
| 72 | } |
| 73 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 74 | ` |
| 75 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 76 | const ( |
| 77 | defaultCliSocketFilePath = "/var/run/vpp/cli.sock" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 78 | defaultApiSocketFilePath = "/var/run/vpp/api.sock" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 79 | defaultLogFilePath = "/var/log/vpp/vpp.log" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 80 | ) |
| 81 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 82 | type VppInstance struct { |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 83 | container *Container |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 84 | additionalConfig []Stanza |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 85 | connection *core.Connection |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 86 | apiStream api.Stream |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 87 | cpus []int |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 90 | func (vpp *VppInstance) getSuite() *HstSuite { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 91 | return vpp.container.suite |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 94 | func (vpp *VppInstance) getCliSocket() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 95 | return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 98 | func (vpp *VppInstance) getRunDir() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 99 | return vpp.container.getContainerWorkDir() + "/var/run/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (vpp *VppInstance) getLogDir() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 103 | return vpp.container.getContainerWorkDir() + "/var/log/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func (vpp *VppInstance) getEtcDir() string { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 107 | return vpp.container.getContainerWorkDir() + "/etc/vpp" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 110 | func (vpp *VppInstance) start() error { |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 111 | // Replace default logger in govpp with our own |
| 112 | govppLogger := logrus.New() |
| 113 | govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().logger.Writer(), GinkgoWriter)) |
| 114 | core.SetLogger(govppLogger) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 115 | // Create folders |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 116 | containerWorkDir := vpp.container.getContainerWorkDir() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 117 | |
| 118 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir()) |
| 119 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir()) |
| 120 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir()) |
| 121 | |
| 122 | // Create startup.conf inside the container |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 123 | configContent := fmt.Sprintf( |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 124 | vppConfigTemplate, |
| 125 | containerWorkDir, |
| 126 | defaultCliSocketFilePath, |
| 127 | defaultApiSocketFilePath, |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 128 | defaultLogFilePath, |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 129 | ) |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 130 | configContent += vpp.generateCpuConfig() |
| 131 | for _, c := range vpp.additionalConfig { |
| 132 | configContent += c.toString() |
| 133 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 134 | startupFileName := vpp.getEtcDir() + "/startup.conf" |
| 135 | vpp.container.createFile(startupFileName, configContent) |
| 136 | |
Filip Tehlar | 1a66150 | 2023-03-08 11:55:50 +0100 | [diff] [blame] | 137 | // create wrapper script for vppctl with proper CLI socket path |
| 138 | cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock" |
| 139 | vppcliFileName := "/usr/bin/vppcli" |
| 140 | vpp.container.createFile(vppcliFileName, cliContent) |
| 141 | vpp.container.exec("chmod 0755 " + vppcliFileName) |
| 142 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 143 | vpp.getSuite().log("starting vpp") |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 144 | if *isVppDebug { |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 145 | sig := make(chan os.Signal, 1) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 146 | signal.Notify(sig, syscall.SIGQUIT) |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 147 | cont := make(chan bool, 1) |
| 148 | go func() { |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 149 | <-sig |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 150 | cont <- true |
| 151 | }() |
| 152 | |
Filip Tehlar | a6b1a7d | 2023-09-02 08:39:25 +0200 | [diff] [blame] | 153 | vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 154 | fmt.Println("run following command in different terminal:") |
Filip Tehlar | a6b1a7d | 2023-09-02 08:39:25 +0200 | [diff] [blame] | 155 | 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] | 156 | fmt.Println("Afterwards press CTRL+\\ to continue") |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 157 | <-cont |
| 158 | fmt.Println("continuing...") |
| 159 | } else { |
| 160 | // Start VPP |
| 161 | vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"") |
| 162 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 163 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 164 | vpp.getSuite().log("connecting to vpp") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 165 | // Connect to VPP and store the connection |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 166 | sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 167 | conn, connEv, err := govpp.AsyncConnect( |
| 168 | sockAddress, |
| 169 | core.DefaultMaxReconnectAttempts, |
| 170 | core.DefaultReconnectInterval) |
| 171 | if err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 172 | vpp.getSuite().log("async connect error: " + fmt.Sprint(err)) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 173 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 174 | } |
| 175 | vpp.connection = conn |
| 176 | |
| 177 | // ... wait for Connected event |
| 178 | e := <-connEv |
| 179 | if e.State != core.Connected { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 180 | vpp.getSuite().log("connecting to VPP failed: " + fmt.Sprint(e.Error)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 181 | } |
| 182 | |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 183 | ch, err := conn.NewStream( |
| 184 | context.Background(), |
| 185 | core.WithRequestSize(50), |
| 186 | core.WithReplySize(50), |
| 187 | core.WithReplyTimeout(time.Second*10)) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 188 | if err != nil { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 189 | vpp.getSuite().log("creating stream failed: " + fmt.Sprint(err)) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 190 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 191 | } |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 192 | vpp.apiStream = ch |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 193 | |
| 194 | return nil |
| 195 | } |
| 196 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 197 | func (vpp *VppInstance) vppctl(command string, arguments ...any) string { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 198 | vppCliCommand := fmt.Sprintf(command, arguments...) |
| 199 | containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
| 200 | vpp.container.name, vpp.getCliSocket(), vppCliCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 201 | vpp.getSuite().log(containerExecCommand) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 202 | output, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 203 | vpp.getSuite().assertNil(err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 204 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 205 | return string(output) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 208 | func (vpp *VppInstance) GetSessionStat(stat string) int { |
| 209 | o := vpp.vppctl("show session stats") |
| 210 | vpp.getSuite().log(o) |
| 211 | for _, line := range strings.Split(o, "\n") { |
| 212 | if strings.Contains(line, stat) { |
| 213 | tokens := strings.Split(strings.TrimSpace(line), " ") |
| 214 | val, err := strconv.Atoi(tokens[0]) |
| 215 | if err != nil { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 216 | Fail("failed to parse stat value %s" + fmt.Sprint(err)) |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 217 | return 0 |
| 218 | } |
| 219 | return val |
| 220 | } |
| 221 | } |
| 222 | return 0 |
| 223 | } |
| 224 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 225 | func (vpp *VppInstance) waitForApp(appName string, timeout int) { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 226 | vpp.getSuite().log("waiting for app " + appName) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 227 | for i := 0; i < timeout; i++ { |
| 228 | o := vpp.vppctl("show app") |
| 229 | if strings.Contains(o, appName) { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 230 | return |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 231 | } |
| 232 | time.Sleep(1 * time.Second) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 233 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 234 | vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 235 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 236 | |
| 237 | func (vpp *VppInstance) createAfPacket( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 238 | veth *NetInterface, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 239 | ) (interface_types.InterfaceIndex, error) { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 240 | createReq := &af_packet.AfPacketCreateV3{ |
| 241 | Mode: 1, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 242 | UseRandomHwAddr: true, |
| 243 | HostIfName: veth.Name(), |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 244 | Flags: af_packet.AfPacketFlags(11), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 245 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 246 | if veth.hwAddress != (MacAddress{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 247 | createReq.UseRandomHwAddr = false |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 248 | createReq.HwAddr = veth.hwAddress |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 249 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 250 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 251 | vpp.getSuite().log("create af-packet interface " + veth.Name()) |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 252 | if err := vpp.apiStream.SendMsg(createReq); err != nil { |
| 253 | vpp.getSuite().hstFail() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 254 | return 0, err |
| 255 | } |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 256 | replymsg, err := vpp.apiStream.RecvMsg() |
| 257 | if err != nil { |
| 258 | return 0, err |
| 259 | } |
| 260 | reply := replymsg.(*af_packet.AfPacketCreateV3Reply) |
| 261 | err = api.RetvalToVPPApiError(reply.Retval) |
| 262 | if err != nil { |
| 263 | return 0, err |
| 264 | } |
| 265 | |
| 266 | veth.index = reply.SwIfIndex |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 267 | |
| 268 | // Set to up |
| 269 | upReq := &interfaces.SwInterfaceSetFlags{ |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 270 | SwIfIndex: veth.index, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 271 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 272 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 273 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 274 | vpp.getSuite().log("set af-packet interface " + veth.Name() + " up") |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 275 | if err := vpp.apiStream.SendMsg(upReq); err != nil { |
| 276 | return 0, err |
| 277 | } |
| 278 | replymsg, err = vpp.apiStream.RecvMsg() |
| 279 | if err != nil { |
| 280 | return 0, err |
| 281 | } |
| 282 | reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply) |
| 283 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 284 | return 0, err |
| 285 | } |
| 286 | |
| 287 | // Add address |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 288 | if veth.addressWithPrefix() == (AddressWithPrefix{}) { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 289 | var err error |
| 290 | var ip4Address string |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 291 | if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 292 | veth.ip4Address = ip4Address |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 293 | } else { |
| 294 | return 0, err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 295 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 296 | } |
| 297 | addressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 298 | IsAdd: true, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 299 | SwIfIndex: veth.index, |
| 300 | Prefix: veth.addressWithPrefix(), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 301 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 302 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 303 | vpp.getSuite().log("af-packet interface " + veth.Name() + " add address " + veth.ip4Address) |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 304 | if err := vpp.apiStream.SendMsg(addressReq); err != nil { |
| 305 | return 0, err |
| 306 | } |
| 307 | replymsg, err = vpp.apiStream.RecvMsg() |
| 308 | if err != nil { |
| 309 | return 0, err |
| 310 | } |
| 311 | reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply) |
| 312 | err = api.RetvalToVPPApiError(reply3.Retval) |
| 313 | if err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 314 | return 0, err |
| 315 | } |
| 316 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 317 | return veth.index, nil |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | func (vpp *VppInstance) addAppNamespace( |
| 321 | secret uint64, |
| 322 | ifx interface_types.InterfaceIndex, |
| 323 | namespaceId string, |
| 324 | ) error { |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 325 | req := &session.AppNamespaceAddDelV4{ |
| 326 | IsAdd: true, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 327 | Secret: secret, |
| 328 | SwIfIndex: ifx, |
| 329 | NamespaceID: namespaceId, |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 330 | SockName: defaultApiSocketFilePath, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 331 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 332 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 333 | vpp.getSuite().log("add app namespace " + namespaceId) |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 334 | if err := vpp.apiStream.SendMsg(req); err != nil { |
| 335 | return err |
| 336 | } |
| 337 | replymsg, err := vpp.apiStream.RecvMsg() |
| 338 | if err != nil { |
| 339 | return err |
| 340 | } |
| 341 | reply := replymsg.(*session.AppNamespaceAddDelV4Reply) |
| 342 | if err = api.RetvalToVPPApiError(reply.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 343 | return err |
| 344 | } |
| 345 | |
| 346 | sessionReq := &session.SessionEnableDisable{ |
| 347 | IsEnable: true, |
| 348 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 349 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 350 | vpp.getSuite().log("enable app namespace " + namespaceId) |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 351 | if err := vpp.apiStream.SendMsg(sessionReq); err != nil { |
| 352 | return err |
| 353 | } |
| 354 | replymsg, err = vpp.apiStream.RecvMsg() |
| 355 | if err != nil { |
| 356 | return err |
| 357 | } |
| 358 | reply2 := replymsg.(*session.SessionEnableDisableReply) |
| 359 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 360 | return err |
| 361 | } |
| 362 | |
| 363 | return nil |
| 364 | } |
| 365 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 366 | func (vpp *VppInstance) createTap( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 367 | tap *NetInterface, |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 368 | tapId ...uint32, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 369 | ) error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 370 | var id uint32 = 1 |
| 371 | if len(tapId) > 0 { |
| 372 | id = tapId[0] |
| 373 | } |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 374 | createTapReq := &tapv2.TapCreateV3{ |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 375 | ID: id, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 376 | HostIfNameSet: true, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 377 | HostIfName: tap.Name(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 378 | HostIP4PrefixSet: true, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 379 | HostIP4Prefix: tap.ip4AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 380 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 381 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 382 | vpp.getSuite().log("create tap interface " + tap.Name()) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 383 | // Create tap interface |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 384 | if err := vpp.apiStream.SendMsg(createTapReq); err != nil { |
| 385 | return err |
| 386 | } |
| 387 | replymsg, err := vpp.apiStream.RecvMsg() |
| 388 | if err != nil { |
| 389 | return err |
| 390 | } |
| 391 | reply := replymsg.(*tapv2.TapCreateV3Reply) |
| 392 | if err = api.RetvalToVPPApiError(reply.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 393 | return err |
| 394 | } |
| 395 | |
| 396 | // Add address |
| 397 | addAddressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 398 | IsAdd: true, |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 399 | SwIfIndex: reply.SwIfIndex, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 400 | Prefix: tap.peer.addressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 401 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 402 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 403 | vpp.getSuite().log("tap interface " + tap.Name() + " add address " + tap.peer.ip4Address) |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 404 | if err := vpp.apiStream.SendMsg(addAddressReq); err != nil { |
| 405 | return err |
| 406 | } |
| 407 | replymsg, err = vpp.apiStream.RecvMsg() |
| 408 | if err != nil { |
| 409 | return err |
| 410 | } |
| 411 | reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply) |
| 412 | if err = api.RetvalToVPPApiError(reply2.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 413 | return err |
| 414 | } |
| 415 | |
| 416 | // Set interface to up |
| 417 | upReq := &interfaces.SwInterfaceSetFlags{ |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 418 | SwIfIndex: reply.SwIfIndex, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 419 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 420 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 421 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 422 | vpp.getSuite().log("set tap interface " + tap.Name() + " up") |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 423 | if err := vpp.apiStream.SendMsg(upReq); err != nil { |
| 424 | return err |
| 425 | } |
| 426 | replymsg, err = vpp.apiStream.RecvMsg() |
| 427 | if err != nil { |
| 428 | return err |
| 429 | } |
| 430 | reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply) |
| 431 | if err = api.RetvalToVPPApiError(reply3.Retval); err != nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 432 | return err |
| 433 | } |
| 434 | |
| 435 | return nil |
| 436 | } |
| 437 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 438 | func (vpp *VppInstance) saveLogs() { |
| 439 | logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log" |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 440 | logSource := vpp.container.getHostWorkDir() + defaultLogFilePath |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 441 | cmd := exec.Command("cp", logSource, logTarget) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 442 | vpp.getSuite().log(cmd.String()) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 443 | cmd.Run() |
| 444 | } |
| 445 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 446 | func (vpp *VppInstance) disconnect() { |
| 447 | vpp.connection.Disconnect() |
Adrian Villin | 46d6600 | 2024-05-15 04:33:41 -0400 | [diff] [blame^] | 448 | vpp.apiStream.Close() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 449 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 450 | |
| 451 | func (vpp *VppInstance) generateCpuConfig() string { |
| 452 | var c Stanza |
| 453 | var s string |
| 454 | if len(vpp.cpus) < 1 { |
| 455 | return "" |
| 456 | } |
| 457 | c.newStanza("cpu"). |
| 458 | append(fmt.Sprintf("main-core %d", vpp.cpus[0])) |
| 459 | workers := vpp.cpus[1:] |
| 460 | |
| 461 | if len(workers) > 0 { |
| 462 | for i := 0; i < len(workers); i++ { |
| 463 | if i != 0 { |
| 464 | s = s + ", " |
| 465 | } |
| 466 | s = s + fmt.Sprintf("%d", workers[i]) |
| 467 | } |
| 468 | c.append(fmt.Sprintf("corelist-workers %s", s)) |
| 469 | } |
| 470 | return c.close().toString() |
| 471 | } |