Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 4 | "fmt" |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 5 | "io" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 6 | "os" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 7 | "os/exec" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 8 | "os/signal" |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 9 | "strconv" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 10 | "strings" |
Filip Tehlar | ec5c40b | 2023-02-28 18:59:15 +0100 | [diff] [blame] | 11 | "syscall" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 12 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 13 | |
Adrian Villin | 637edda | 2024-05-06 06:55:34 -0400 | [diff] [blame] | 14 | "github.com/sirupsen/logrus" |
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" |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 17 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 18 | "go.fd.io/govpp" |
| 19 | "go.fd.io/govpp/api" |
| 20 | "go.fd.io/govpp/binapi/af_packet" |
| 21 | interfaces "go.fd.io/govpp/binapi/interface" |
| 22 | "go.fd.io/govpp/binapi/interface_types" |
| 23 | "go.fd.io/govpp/binapi/session" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 24 | "go.fd.io/govpp/binapi/tapv2" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 25 | "go.fd.io/govpp/binapi/vpe" |
| 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 |
| 86 | apiChannel api.Channel |
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 { |
| 172 | fmt.Println("async connect error: ", 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 { |
| 180 | fmt.Println("connecting to VPP failed: ", e.Error) |
| 181 | } |
| 182 | |
| 183 | // ... check compatibility of used messages |
| 184 | ch, err := conn.NewAPIChannel() |
| 185 | if err != nil { |
| 186 | fmt.Println("creating channel failed: ", err) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 187 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 188 | } |
| 189 | if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil { |
| 190 | fmt.Println("compatibility error: ", 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 | } |
| 193 | if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil { |
| 194 | fmt.Println("compatibility error: ", err) |
Filip Tehlar | 56e17cf | 2024-01-11 17:17:33 +0100 | [diff] [blame] | 195 | return err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 196 | } |
| 197 | vpp.apiChannel = ch |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 198 | |
| 199 | return nil |
| 200 | } |
| 201 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 202 | func (vpp *VppInstance) vppctl(command string, arguments ...any) string { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 203 | vppCliCommand := fmt.Sprintf(command, arguments...) |
| 204 | containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
| 205 | vpp.container.name, vpp.getCliSocket(), vppCliCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 206 | vpp.getSuite().log(containerExecCommand) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 207 | output, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 208 | vpp.getSuite().assertNil(err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 209 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 210 | return string(output) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 213 | func (vpp *VppInstance) GetSessionStat(stat string) int { |
| 214 | o := vpp.vppctl("show session stats") |
| 215 | vpp.getSuite().log(o) |
| 216 | for _, line := range strings.Split(o, "\n") { |
| 217 | if strings.Contains(line, stat) { |
| 218 | tokens := strings.Split(strings.TrimSpace(line), " ") |
| 219 | val, err := strconv.Atoi(tokens[0]) |
| 220 | if err != nil { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 221 | Fail("failed to parse stat value %s" + fmt.Sprint(err)) |
Filip Tehlar | 543cd57 | 2023-06-27 10:01:37 +0200 | [diff] [blame] | 222 | return 0 |
| 223 | } |
| 224 | return val |
| 225 | } |
| 226 | } |
| 227 | return 0 |
| 228 | } |
| 229 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 230 | func (vpp *VppInstance) waitForApp(appName string, timeout int) { |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 231 | vpp.getSuite().log("waiting for app " + appName) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 232 | for i := 0; i < timeout; i++ { |
| 233 | o := vpp.vppctl("show app") |
| 234 | if strings.Contains(o, appName) { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 235 | return |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 236 | } |
| 237 | time.Sleep(1 * time.Second) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 238 | } |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 239 | vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 240 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 241 | |
| 242 | func (vpp *VppInstance) createAfPacket( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 243 | veth *NetInterface, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 244 | ) (interface_types.InterfaceIndex, error) { |
| 245 | createReq := &af_packet.AfPacketCreateV2{ |
| 246 | UseRandomHwAddr: true, |
| 247 | HostIfName: veth.Name(), |
| 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 | } |
| 253 | createReply := &af_packet.AfPacketCreateV2Reply{} |
| 254 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 255 | vpp.getSuite().log("create af-packet interface " + veth.Name()) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 256 | if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil { |
| 257 | return 0, err |
| 258 | } |
Filip Tehlar | b41b0af | 2023-03-20 12:39:20 +0100 | [diff] [blame] | 259 | veth.index = createReply.SwIfIndex |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 260 | |
| 261 | // Set to up |
| 262 | upReq := &interfaces.SwInterfaceSetFlags{ |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 263 | SwIfIndex: veth.index, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 264 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 265 | } |
| 266 | upReply := &interfaces.SwInterfaceSetFlagsReply{} |
| 267 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 268 | vpp.getSuite().log("set af-packet interface " + veth.Name() + " up") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 269 | if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil { |
| 270 | return 0, err |
| 271 | } |
| 272 | |
| 273 | // Add address |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 274 | if veth.addressWithPrefix() == (AddressWithPrefix{}) { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 275 | var err error |
| 276 | var ip4Address string |
Filip Tehlar | 3a910ab | 2023-06-08 17:39:39 +0200 | [diff] [blame] | 277 | if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 278 | veth.ip4Address = ip4Address |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 279 | } else { |
| 280 | return 0, err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 281 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 282 | } |
| 283 | addressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 284 | IsAdd: true, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 285 | SwIfIndex: veth.index, |
| 286 | Prefix: veth.addressWithPrefix(), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 287 | } |
| 288 | addressReply := &interfaces.SwInterfaceAddDelAddressReply{} |
| 289 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 290 | vpp.getSuite().log("af-packet interface " + veth.Name() + " add address " + veth.ip4Address) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 291 | if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil { |
| 292 | return 0, err |
| 293 | } |
| 294 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 295 | return veth.index, nil |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | func (vpp *VppInstance) addAppNamespace( |
| 299 | secret uint64, |
| 300 | ifx interface_types.InterfaceIndex, |
| 301 | namespaceId string, |
| 302 | ) error { |
| 303 | req := &session.AppNamespaceAddDelV2{ |
| 304 | Secret: secret, |
| 305 | SwIfIndex: ifx, |
| 306 | NamespaceID: namespaceId, |
| 307 | } |
| 308 | reply := &session.AppNamespaceAddDelV2Reply{} |
| 309 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 310 | vpp.getSuite().log("add app namespace " + namespaceId) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 311 | if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil { |
| 312 | return err |
| 313 | } |
| 314 | |
| 315 | sessionReq := &session.SessionEnableDisable{ |
| 316 | IsEnable: true, |
| 317 | } |
| 318 | sessionReply := &session.SessionEnableDisableReply{} |
| 319 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 320 | vpp.getSuite().log("enable app namespace " + namespaceId) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 321 | if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil { |
| 322 | return err |
| 323 | } |
| 324 | |
| 325 | return nil |
| 326 | } |
| 327 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 328 | func (vpp *VppInstance) createTap( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 329 | tap *NetInterface, |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 330 | tapId ...uint32, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 331 | ) error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame] | 332 | var id uint32 = 1 |
| 333 | if len(tapId) > 0 { |
| 334 | id = tapId[0] |
| 335 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 336 | createTapReq := &tapv2.TapCreateV2{ |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 337 | ID: id, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 338 | HostIfNameSet: true, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 339 | HostIfName: tap.Name(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 340 | HostIP4PrefixSet: true, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 341 | HostIP4Prefix: tap.ip4AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 342 | } |
| 343 | createTapReply := &tapv2.TapCreateV2Reply{} |
| 344 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 345 | vpp.getSuite().log("create tap interface " + tap.Name()) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 346 | // Create tap interface |
| 347 | if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil { |
| 348 | return err |
| 349 | } |
| 350 | |
| 351 | // Add address |
| 352 | addAddressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 353 | IsAdd: true, |
| 354 | SwIfIndex: createTapReply.SwIfIndex, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 355 | Prefix: tap.peer.addressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 356 | } |
| 357 | addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{} |
| 358 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 359 | vpp.getSuite().log("tap interface " + tap.Name() + " add address " + tap.peer.ip4Address) |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 360 | if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil { |
| 361 | return err |
| 362 | } |
| 363 | |
| 364 | // Set interface to up |
| 365 | upReq := &interfaces.SwInterfaceSetFlags{ |
| 366 | SwIfIndex: createTapReply.SwIfIndex, |
| 367 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 368 | } |
| 369 | upReply := &interfaces.SwInterfaceSetFlagsReply{} |
| 370 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 371 | vpp.getSuite().log("set tap interface " + tap.Name() + " up") |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 372 | if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil { |
| 373 | return err |
| 374 | } |
| 375 | |
| 376 | return nil |
| 377 | } |
| 378 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 379 | func (vpp *VppInstance) saveLogs() { |
| 380 | logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log" |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 381 | logSource := vpp.container.getHostWorkDir() + defaultLogFilePath |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 382 | cmd := exec.Command("cp", logSource, logTarget) |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 383 | vpp.getSuite().log(cmd.String()) |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 384 | cmd.Run() |
| 385 | } |
| 386 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 387 | func (vpp *VppInstance) disconnect() { |
| 388 | vpp.connection.Disconnect() |
| 389 | vpp.apiChannel.Close() |
| 390 | } |
Filip Tehlar | 608d006 | 2023-04-28 10:29:47 +0200 | [diff] [blame] | 391 | |
| 392 | func (vpp *VppInstance) generateCpuConfig() string { |
| 393 | var c Stanza |
| 394 | var s string |
| 395 | if len(vpp.cpus) < 1 { |
| 396 | return "" |
| 397 | } |
| 398 | c.newStanza("cpu"). |
| 399 | append(fmt.Sprintf("main-core %d", vpp.cpus[0])) |
| 400 | workers := vpp.cpus[1:] |
| 401 | |
| 402 | if len(workers) > 0 { |
| 403 | for i := 0; i < len(workers); i++ { |
| 404 | if i != 0 { |
| 405 | s = s + ", " |
| 406 | } |
| 407 | s = s + fmt.Sprintf("%d", workers[i]) |
| 408 | } |
| 409 | c.append(fmt.Sprintf("corelist-workers %s", s)) |
| 410 | } |
| 411 | return c.close().toString() |
| 412 | } |