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