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