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" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 5 | "github.com/edwarnicke/exechelper" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 6 | "os/exec" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 7 | "strings" |
| 8 | "time" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 9 | |
| 10 | "go.fd.io/govpp" |
| 11 | "go.fd.io/govpp/api" |
| 12 | "go.fd.io/govpp/binapi/af_packet" |
| 13 | interfaces "go.fd.io/govpp/binapi/interface" |
| 14 | "go.fd.io/govpp/binapi/interface_types" |
| 15 | "go.fd.io/govpp/binapi/session" |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 16 | "go.fd.io/govpp/binapi/tapv2" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 17 | "go.fd.io/govpp/binapi/vpe" |
| 18 | "go.fd.io/govpp/core" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | const vppConfigTemplate = `unix { |
| 22 | nodaemon |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 23 | log %[1]s%[4]s |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 24 | full-coredump |
| 25 | cli-listen %[1]s%[2]s |
| 26 | runtime-dir %[1]s/var/run |
| 27 | gid vpp |
| 28 | } |
| 29 | |
| 30 | api-trace { |
| 31 | on |
| 32 | } |
| 33 | |
| 34 | api-segment { |
| 35 | gid vpp |
| 36 | } |
| 37 | |
| 38 | socksvr { |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 39 | socket-name %[1]s%[3]s |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | statseg { |
| 43 | socket-name %[1]s/var/run/vpp/stats.sock |
| 44 | } |
| 45 | |
| 46 | plugins { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 47 | plugin default { disable } |
| 48 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 49 | plugin unittest_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 50 | plugin quic_plugin.so { enable } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 51 | plugin af_packet_plugin.so { enable } |
| 52 | plugin hs_apps_plugin.so { enable } |
| 53 | plugin http_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 56 | logging { |
| 57 | default-log-level debug |
| 58 | default-syslog-log-level debug |
| 59 | } |
| 60 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 61 | ` |
| 62 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 63 | const ( |
| 64 | defaultCliSocketFilePath = "/var/run/vpp/cli.sock" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 65 | defaultApiSocketFilePath = "/var/run/vpp/api.sock" |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 66 | defaultLogFilePath = "/var/log/vpp/vpp.log" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 67 | ) |
| 68 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 69 | type VppInstance struct { |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 70 | container *Container |
| 71 | additionalConfig Stanza |
| 72 | connection *core.Connection |
| 73 | apiChannel api.Channel |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 76 | func (vpp *VppInstance) Suite() *HstSuite { |
| 77 | return vpp.container.suite |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 80 | func (vpp *VppInstance) getCliSocket() string { |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 81 | return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 84 | func (vpp *VppInstance) getRunDir() string { |
| 85 | return vpp.container.GetContainerWorkDir() + "/var/run/vpp" |
| 86 | } |
| 87 | |
| 88 | func (vpp *VppInstance) getLogDir() string { |
| 89 | return vpp.container.GetContainerWorkDir() + "/var/log/vpp" |
| 90 | } |
| 91 | |
| 92 | func (vpp *VppInstance) getEtcDir() string { |
| 93 | return vpp.container.GetContainerWorkDir() + "/etc/vpp" |
| 94 | } |
| 95 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 96 | func (vpp *VppInstance) start() error { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 97 | // Create folders |
| 98 | containerWorkDir := vpp.container.GetContainerWorkDir() |
| 99 | |
| 100 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir()) |
| 101 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir()) |
| 102 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir()) |
| 103 | |
| 104 | // Create startup.conf inside the container |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 105 | configContent := fmt.Sprintf( |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 106 | vppConfigTemplate, |
| 107 | containerWorkDir, |
| 108 | defaultCliSocketFilePath, |
| 109 | defaultApiSocketFilePath, |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 110 | defaultLogFilePath, |
Maros Ondrejicka | 300f70d | 2023-02-21 10:53:20 +0100 | [diff] [blame] | 111 | ) |
Maros Ondrejicka | 7d7ab10 | 2023-02-14 12:56:49 +0100 | [diff] [blame] | 112 | configContent += vpp.additionalConfig.ToString() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 113 | startupFileName := vpp.getEtcDir() + "/startup.conf" |
| 114 | vpp.container.createFile(startupFileName, configContent) |
| 115 | |
| 116 | // Start VPP |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 117 | vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"") |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 118 | |
| 119 | // Connect to VPP and store the connection |
| 120 | sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath |
| 121 | conn, connEv, err := govpp.AsyncConnect( |
| 122 | sockAddress, |
| 123 | core.DefaultMaxReconnectAttempts, |
| 124 | core.DefaultReconnectInterval) |
| 125 | if err != nil { |
| 126 | fmt.Println("async connect error: ", err) |
| 127 | } |
| 128 | vpp.connection = conn |
| 129 | |
| 130 | // ... wait for Connected event |
| 131 | e := <-connEv |
| 132 | if e.State != core.Connected { |
| 133 | fmt.Println("connecting to VPP failed: ", e.Error) |
| 134 | } |
| 135 | |
| 136 | // ... check compatibility of used messages |
| 137 | ch, err := conn.NewAPIChannel() |
| 138 | if err != nil { |
| 139 | fmt.Println("creating channel failed: ", err) |
| 140 | } |
| 141 | if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil { |
| 142 | fmt.Println("compatibility error: ", err) |
| 143 | } |
| 144 | if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil { |
| 145 | fmt.Println("compatibility error: ", err) |
| 146 | } |
| 147 | vpp.apiChannel = ch |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 148 | |
| 149 | return nil |
| 150 | } |
| 151 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 152 | func (vpp *VppInstance) vppctl(command string, arguments ...any) string { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 153 | vppCliCommand := fmt.Sprintf(command, arguments...) |
| 154 | containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
| 155 | vpp.container.name, vpp.getCliSocket(), vppCliCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 156 | vpp.Suite().log(containerExecCommand) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 157 | output, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 158 | vpp.Suite().assertNil(err) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 159 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 160 | return string(output) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame^] | 163 | func (vpp *VppInstance) waitForApp(appName string, timeout int) { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 164 | for i := 0; i < timeout; i++ { |
| 165 | o := vpp.vppctl("show app") |
| 166 | if strings.Contains(o, appName) { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame^] | 167 | return |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 168 | } |
| 169 | time.Sleep(1 * time.Second) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 170 | } |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame^] | 171 | vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName) |
| 172 | return |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 173 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 174 | |
| 175 | func (vpp *VppInstance) createAfPacket( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 176 | veth *NetInterface, |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 177 | ) (interface_types.InterfaceIndex, error) { |
| 178 | createReq := &af_packet.AfPacketCreateV2{ |
| 179 | UseRandomHwAddr: true, |
| 180 | HostIfName: veth.Name(), |
| 181 | } |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 182 | if veth.HwAddress() != (MacAddress{}) { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 183 | createReq.UseRandomHwAddr = false |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 184 | createReq.HwAddr = veth.HwAddress() |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 185 | } |
| 186 | createReply := &af_packet.AfPacketCreateV2Reply{} |
| 187 | |
| 188 | if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil { |
| 189 | return 0, err |
| 190 | } |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 191 | veth.SetIndex(createReply.SwIfIndex) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 192 | |
| 193 | // Set to up |
| 194 | upReq := &interfaces.SwInterfaceSetFlags{ |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 195 | SwIfIndex: veth.Index(), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 196 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 197 | } |
| 198 | upReply := &interfaces.SwInterfaceSetFlagsReply{} |
| 199 | |
| 200 | if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil { |
| 201 | return 0, err |
| 202 | } |
| 203 | |
| 204 | // Add address |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 205 | if veth.AddressWithPrefix() == (AddressWithPrefix{}) { |
| 206 | var err error |
| 207 | var ip4Address string |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 208 | if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil { |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 209 | veth.SetAddress(ip4Address) |
| 210 | } else { |
| 211 | return 0, err |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 212 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 213 | } |
| 214 | addressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 215 | IsAdd: true, |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 216 | SwIfIndex: veth.Index(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 217 | Prefix: veth.AddressWithPrefix(), |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 218 | } |
| 219 | addressReply := &interfaces.SwInterfaceAddDelAddressReply{} |
| 220 | |
| 221 | if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil { |
| 222 | return 0, err |
| 223 | } |
| 224 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 225 | return veth.Index(), nil |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | func (vpp *VppInstance) addAppNamespace( |
| 229 | secret uint64, |
| 230 | ifx interface_types.InterfaceIndex, |
| 231 | namespaceId string, |
| 232 | ) error { |
| 233 | req := &session.AppNamespaceAddDelV2{ |
| 234 | Secret: secret, |
| 235 | SwIfIndex: ifx, |
| 236 | NamespaceID: namespaceId, |
| 237 | } |
| 238 | reply := &session.AppNamespaceAddDelV2Reply{} |
| 239 | |
| 240 | if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil { |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | sessionReq := &session.SessionEnableDisable{ |
| 245 | IsEnable: true, |
| 246 | } |
| 247 | sessionReply := &session.SessionEnableDisableReply{} |
| 248 | |
| 249 | if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil { |
| 250 | return err |
| 251 | } |
| 252 | |
| 253 | return nil |
| 254 | } |
| 255 | |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 256 | func (vpp *VppInstance) createTap( |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 257 | tap *NetInterface, |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame^] | 258 | tapId ...uint32, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 259 | ) error { |
Maros Ondrejicka | c2f76f4 | 2023-02-27 13:22:45 +0100 | [diff] [blame^] | 260 | var id uint32 = 1 |
| 261 | if len(tapId) > 0 { |
| 262 | id = tapId[0] |
| 263 | } |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 264 | createTapReq := &tapv2.TapCreateV2{ |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 265 | ID: id, |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 266 | HostIfNameSet: true, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 267 | HostIfName: tap.Name(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 268 | HostIP4PrefixSet: true, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 269 | HostIP4Prefix: tap.IP4AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 270 | } |
| 271 | createTapReply := &tapv2.TapCreateV2Reply{} |
| 272 | |
| 273 | // Create tap interface |
| 274 | if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil { |
| 275 | return err |
| 276 | } |
| 277 | |
| 278 | // Add address |
| 279 | addAddressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 280 | IsAdd: true, |
| 281 | SwIfIndex: createTapReply.SwIfIndex, |
Maros Ondrejicka | 40cba40 | 2023-02-23 13:19:15 +0100 | [diff] [blame] | 282 | Prefix: tap.Peer().AddressWithPrefix(), |
Maros Ondrejicka | 7550dd2 | 2023-02-07 20:40:27 +0100 | [diff] [blame] | 283 | } |
| 284 | addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{} |
| 285 | |
| 286 | if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil { |
| 287 | return err |
| 288 | } |
| 289 | |
| 290 | // Set interface to up |
| 291 | upReq := &interfaces.SwInterfaceSetFlags{ |
| 292 | SwIfIndex: createTapReply.SwIfIndex, |
| 293 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 294 | } |
| 295 | upReply := &interfaces.SwInterfaceSetFlagsReply{} |
| 296 | |
| 297 | if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil { |
| 298 | return err |
| 299 | } |
| 300 | |
| 301 | return nil |
| 302 | } |
| 303 | |
Maros Ondrejicka | a2d5262 | 2023-02-24 11:26:39 +0100 | [diff] [blame] | 304 | func (vpp *VppInstance) saveLogs() { |
| 305 | logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log" |
| 306 | logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath |
| 307 | cmd := exec.Command("cp", logSource, logTarget) |
| 308 | vpp.Suite().T().Helper() |
| 309 | vpp.Suite().log(cmd.String()) |
| 310 | cmd.Run() |
| 311 | } |
| 312 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 313 | func (vpp *VppInstance) disconnect() { |
| 314 | vpp.connection.Disconnect() |
| 315 | vpp.apiChannel.Close() |
| 316 | } |