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