Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 4 | "encoding/json" |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 5 | "fmt" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 6 | "github.com/edwarnicke/exechelper" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 7 | |
| 8 | "go.fd.io/govpp" |
| 9 | "go.fd.io/govpp/api" |
| 10 | "go.fd.io/govpp/binapi/af_packet" |
| 11 | interfaces "go.fd.io/govpp/binapi/interface" |
| 12 | "go.fd.io/govpp/binapi/interface_types" |
| 13 | "go.fd.io/govpp/binapi/session" |
| 14 | "go.fd.io/govpp/binapi/vpe" |
| 15 | "go.fd.io/govpp/core" |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | const vppConfigTemplate = `unix { |
| 19 | nodaemon |
| 20 | log %[1]s/var/log/vpp/vpp.log |
| 21 | full-coredump |
| 22 | cli-listen %[1]s%[2]s |
| 23 | runtime-dir %[1]s/var/run |
| 24 | gid vpp |
| 25 | } |
| 26 | |
| 27 | api-trace { |
| 28 | on |
| 29 | } |
| 30 | |
| 31 | api-segment { |
| 32 | gid vpp |
| 33 | } |
| 34 | |
| 35 | socksvr { |
| 36 | socket-name %[1]s/var/run/vpp/api.sock |
| 37 | } |
| 38 | |
| 39 | statseg { |
| 40 | socket-name %[1]s/var/run/vpp/stats.sock |
| 41 | } |
| 42 | |
| 43 | plugins { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 44 | plugin default { disable } |
| 45 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 46 | plugin unittest_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 47 | plugin quic_plugin.so { enable } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 48 | plugin af_packet_plugin.so { enable } |
| 49 | plugin hs_apps_plugin.so { enable } |
| 50 | plugin http_plugin.so { enable } |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ` |
| 54 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 55 | const ( |
| 56 | defaultCliSocketFilePath = "/var/run/vpp/cli.sock" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 57 | defaultApiSocketFilePath = "/var/run/vpp/api.sock" |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 58 | ) |
| 59 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 60 | type VppInstance struct { |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 61 | container *Container |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 62 | config *VppConfig |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 63 | actionFuncName string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 64 | connection *core.Connection |
| 65 | apiChannel api.Channel |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | type VppConfig struct { |
Filip Tehlar | f3ee2b6 | 2023-01-09 12:07:09 +0100 | [diff] [blame] | 69 | Variant string |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 70 | CliSocketFilePath string |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 71 | additionalConfig Stanza |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (vc *VppConfig) getTemplate() string { |
| 75 | return fmt.Sprintf(vppConfigTemplate, "%[1]s", vc.CliSocketFilePath) |
| 76 | } |
| 77 | |
| 78 | func (vpp *VppInstance) set2VethsServer() { |
| 79 | vpp.actionFuncName = "Configure2Veths" |
| 80 | vpp.config.Variant = "srv" |
| 81 | } |
| 82 | |
| 83 | func (vpp *VppInstance) set2VethsClient() { |
| 84 | vpp.actionFuncName = "Configure2Veths" |
| 85 | vpp.config.Variant = "cln" |
| 86 | } |
| 87 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 88 | func (vpp *VppInstance) setVppProxy() { |
| 89 | vpp.actionFuncName = "ConfigureVppProxy" |
| 90 | } |
| 91 | |
| 92 | func (vpp *VppInstance) setEnvoyProxy() { |
| 93 | vpp.actionFuncName = "ConfigureEnvoyProxy" |
| 94 | } |
| 95 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 96 | func (vpp *VppInstance) setCliSocket(filePath string) { |
| 97 | vpp.config.CliSocketFilePath = filePath |
| 98 | } |
| 99 | |
| 100 | func (vpp *VppInstance) getCliSocket() string { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 101 | return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), vpp.config.CliSocketFilePath) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 104 | func (vpp *VppInstance) getRunDir() string { |
| 105 | return vpp.container.GetContainerWorkDir() + "/var/run/vpp" |
| 106 | } |
| 107 | |
| 108 | func (vpp *VppInstance) getLogDir() string { |
| 109 | return vpp.container.GetContainerWorkDir() + "/var/log/vpp" |
| 110 | } |
| 111 | |
| 112 | func (vpp *VppInstance) getEtcDir() string { |
| 113 | return vpp.container.GetContainerWorkDir() + "/etc/vpp" |
| 114 | } |
| 115 | |
| 116 | func (vpp *VppInstance) legacyStart() error { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 117 | if vpp.actionFuncName == "" { |
| 118 | return fmt.Errorf("vpp start failed: action function name must not be blank") |
| 119 | } |
| 120 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 121 | serializedConfig, err := serializeVppConfig(vpp.config) |
Filip Tehlar | 5616041 | 2023-01-25 13:56:38 +0100 | [diff] [blame] | 122 | if err != nil { |
| 123 | return fmt.Errorf("serialize vpp config: %v", err) |
| 124 | } |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 125 | args := fmt.Sprintf("%s '%s'", vpp.actionFuncName, serializedConfig) |
| 126 | _, err = vpp.container.execAction(args) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 127 | if err != nil { |
| 128 | return fmt.Errorf("vpp start failed: %s", err) |
| 129 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 130 | return nil |
| 131 | } |
| 132 | |
| 133 | func (vpp *VppInstance) start() error { |
| 134 | if vpp.actionFuncName != "" { |
| 135 | return vpp.legacyStart() |
| 136 | } |
| 137 | |
| 138 | // Create folders |
| 139 | containerWorkDir := vpp.container.GetContainerWorkDir() |
| 140 | |
| 141 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir()) |
| 142 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir()) |
| 143 | vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir()) |
| 144 | |
| 145 | // Create startup.conf inside the container |
| 146 | configContent := fmt.Sprintf(vppConfigTemplate, containerWorkDir, vpp.config.CliSocketFilePath) |
| 147 | configContent += vpp.config.additionalConfig.ToString() |
| 148 | startupFileName := vpp.getEtcDir() + "/startup.conf" |
| 149 | vpp.container.createFile(startupFileName, configContent) |
| 150 | |
| 151 | // Start VPP |
| 152 | if err := vpp.container.execServer("vpp -c " + startupFileName); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | |
| 156 | // Connect to VPP and store the connection |
| 157 | sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath |
| 158 | conn, connEv, err := govpp.AsyncConnect( |
| 159 | sockAddress, |
| 160 | core.DefaultMaxReconnectAttempts, |
| 161 | core.DefaultReconnectInterval) |
| 162 | if err != nil { |
| 163 | fmt.Println("async connect error: ", err) |
| 164 | } |
| 165 | vpp.connection = conn |
| 166 | |
| 167 | // ... wait for Connected event |
| 168 | e := <-connEv |
| 169 | if e.State != core.Connected { |
| 170 | fmt.Println("connecting to VPP failed: ", e.Error) |
| 171 | } |
| 172 | |
| 173 | // ... check compatibility of used messages |
| 174 | ch, err := conn.NewAPIChannel() |
| 175 | if err != nil { |
| 176 | fmt.Println("creating channel failed: ", err) |
| 177 | } |
| 178 | if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil { |
| 179 | fmt.Println("compatibility error: ", err) |
| 180 | } |
| 181 | if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil { |
| 182 | fmt.Println("compatibility error: ", err) |
| 183 | } |
| 184 | vpp.apiChannel = ch |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 185 | |
| 186 | return nil |
| 187 | } |
| 188 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 189 | func (vpp *VppInstance) vppctl(command string, arguments ...any) (string, error) { |
| 190 | vppCliCommand := fmt.Sprintf(command, arguments...) |
| 191 | containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
| 192 | vpp.container.name, vpp.getCliSocket(), vppCliCommand) |
| 193 | output, err := exechelper.CombinedOutput(containerExecCommand) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 194 | if err != nil { |
| 195 | return "", fmt.Errorf("vppctl failed: %s", err) |
| 196 | } |
| 197 | |
| 198 | return string(output), nil |
| 199 | } |
| 200 | |
| 201 | func NewVppInstance(c *Container) *VppInstance { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 202 | vppConfig := new(VppConfig) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 203 | vppConfig.CliSocketFilePath = defaultCliSocketFilePath |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 204 | vpp := new(VppInstance) |
| 205 | vpp.container = c |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 206 | vpp.config = vppConfig |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 207 | return vpp |
| 208 | } |
| 209 | |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 210 | func serializeVppConfig(vppConfig *VppConfig) (string, error) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 211 | serializedConfig, err := json.Marshal(vppConfig) |
| 212 | if err != nil { |
| 213 | return "", fmt.Errorf("vpp start failed: serializing configuration failed: %s", err) |
| 214 | } |
| 215 | return string(serializedConfig), nil |
| 216 | } |
| 217 | |
| 218 | func deserializeVppConfig(input string) (VppConfig, error) { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 219 | var vppConfig VppConfig |
| 220 | err := json.Unmarshal([]byte(input), &vppConfig) |
| 221 | if err != nil { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 222 | // Since input is not a valid JSON it is going be used as a variant value |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 223 | // for compatibility reasons |
| 224 | vppConfig.Variant = input |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 225 | vppConfig.CliSocketFilePath = defaultCliSocketFilePath |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 226 | } |
| 227 | return vppConfig, nil |
| 228 | } |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame^] | 229 | |
| 230 | func (vpp *VppInstance) createAfPacket( |
| 231 | veth *NetworkInterfaceVeth, |
| 232 | ) (interface_types.InterfaceIndex, error) { |
| 233 | createReq := &af_packet.AfPacketCreateV2{ |
| 234 | UseRandomHwAddr: true, |
| 235 | HostIfName: veth.Name(), |
| 236 | } |
| 237 | if veth.hwAddress != (MacAddress{}) { |
| 238 | createReq.UseRandomHwAddr = false |
| 239 | createReq.HwAddr = veth.hwAddress |
| 240 | } |
| 241 | createReply := &af_packet.AfPacketCreateV2Reply{} |
| 242 | |
| 243 | if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil { |
| 244 | return 0, err |
| 245 | } |
| 246 | veth.index = createReply.SwIfIndex |
| 247 | |
| 248 | // Set to up |
| 249 | upReq := &interfaces.SwInterfaceSetFlags{ |
| 250 | SwIfIndex: veth.index, |
| 251 | Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP, |
| 252 | } |
| 253 | upReply := &interfaces.SwInterfaceSetFlagsReply{} |
| 254 | |
| 255 | if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil { |
| 256 | return 0, err |
| 257 | } |
| 258 | |
| 259 | // Add address |
| 260 | if veth.ip4Address == (AddressWithPrefix{}) { |
| 261 | ipPrefix, err := vpp.container.suite.NewAddress() |
| 262 | if err != nil { |
| 263 | return 0, err |
| 264 | } |
| 265 | veth.ip4Address = ipPrefix |
| 266 | } |
| 267 | addressReq := &interfaces.SwInterfaceAddDelAddress{ |
| 268 | IsAdd: true, |
| 269 | SwIfIndex: veth.index, |
| 270 | Prefix: veth.ip4Address, |
| 271 | } |
| 272 | addressReply := &interfaces.SwInterfaceAddDelAddressReply{} |
| 273 | |
| 274 | if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil { |
| 275 | return 0, err |
| 276 | } |
| 277 | |
| 278 | return veth.index, nil |
| 279 | } |
| 280 | |
| 281 | func (vpp *VppInstance) addAppNamespace( |
| 282 | secret uint64, |
| 283 | ifx interface_types.InterfaceIndex, |
| 284 | namespaceId string, |
| 285 | ) error { |
| 286 | req := &session.AppNamespaceAddDelV2{ |
| 287 | Secret: secret, |
| 288 | SwIfIndex: ifx, |
| 289 | NamespaceID: namespaceId, |
| 290 | } |
| 291 | reply := &session.AppNamespaceAddDelV2Reply{} |
| 292 | |
| 293 | if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil { |
| 294 | return err |
| 295 | } |
| 296 | |
| 297 | sessionReq := &session.SessionEnableDisable{ |
| 298 | IsEnable: true, |
| 299 | } |
| 300 | sessionReply := &session.SessionEnableDisableReply{} |
| 301 | |
| 302 | if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil { |
| 303 | return err |
| 304 | } |
| 305 | |
| 306 | return nil |
| 307 | } |
| 308 | |
| 309 | func (vpp *VppInstance) disconnect() { |
| 310 | vpp.connection.Disconnect() |
| 311 | vpp.apiChannel.Close() |
| 312 | } |