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