Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "encoding/json" |
| 6 | "github.com/edwarnicke/exechelper" |
| 7 | ) |
| 8 | |
| 9 | const vppConfigTemplate = `unix { |
| 10 | nodaemon |
| 11 | log %[1]s/var/log/vpp/vpp.log |
| 12 | full-coredump |
| 13 | cli-listen %[1]s%[2]s |
| 14 | runtime-dir %[1]s/var/run |
| 15 | gid vpp |
| 16 | } |
| 17 | |
| 18 | api-trace { |
| 19 | on |
| 20 | } |
| 21 | |
| 22 | api-segment { |
| 23 | gid vpp |
| 24 | } |
| 25 | |
| 26 | socksvr { |
| 27 | socket-name %[1]s/var/run/vpp/api.sock |
| 28 | } |
| 29 | |
| 30 | statseg { |
| 31 | socket-name %[1]s/var/run/vpp/stats.sock |
| 32 | } |
| 33 | |
| 34 | plugins { |
| 35 | plugin unittest_plugin.so { enable } |
| 36 | plugin dpdk_plugin.so { disable } |
| 37 | plugin crypto_aesni_plugin.so { enable } |
| 38 | plugin quic_plugin.so { enable } |
| 39 | } |
| 40 | |
| 41 | ` |
| 42 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 43 | const ( |
| 44 | defaultCliSocketFilePath = "/var/run/vpp/cli.sock" |
| 45 | ) |
| 46 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 47 | type VppInstance struct { |
| 48 | container *Container |
| 49 | config VppConfig |
| 50 | actionFuncName string |
| 51 | } |
| 52 | |
| 53 | type VppConfig struct { |
| 54 | Variant string |
| 55 | CliSocketFilePath string |
| 56 | } |
| 57 | |
| 58 | func (vc *VppConfig) getTemplate() string { |
| 59 | return fmt.Sprintf(vppConfigTemplate, "%[1]s", vc.CliSocketFilePath) |
| 60 | } |
| 61 | |
| 62 | func (vpp *VppInstance) set2VethsServer() { |
| 63 | vpp.actionFuncName = "Configure2Veths" |
| 64 | vpp.config.Variant = "srv" |
| 65 | } |
| 66 | |
| 67 | func (vpp *VppInstance) set2VethsClient() { |
| 68 | vpp.actionFuncName = "Configure2Veths" |
| 69 | vpp.config.Variant = "cln" |
| 70 | } |
| 71 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 72 | func (vpp *VppInstance) setVppProxy() { |
| 73 | vpp.actionFuncName = "ConfigureVppProxy" |
| 74 | } |
| 75 | |
| 76 | func (vpp *VppInstance) setEnvoyProxy() { |
| 77 | vpp.actionFuncName = "ConfigureEnvoyProxy" |
| 78 | } |
| 79 | |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 80 | func (vpp *VppInstance) setCliSocket(filePath string) { |
| 81 | vpp.config.CliSocketFilePath = filePath |
| 82 | } |
| 83 | |
| 84 | func (vpp *VppInstance) getCliSocket() string { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 85 | return fmt.Sprintf("%s%s", vpp.container.workDir, vpp.config.CliSocketFilePath) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func (vpp *VppInstance) start() error { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 89 | if vpp.actionFuncName == "" { |
| 90 | return fmt.Errorf("vpp start failed: action function name must not be blank") |
| 91 | } |
| 92 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 93 | serializedConfig, err := serializeVppConfig(vpp.config) |
| 94 | args := fmt.Sprintf("%s '%s'", vpp.actionFuncName, serializedConfig) |
| 95 | _, err = vpp.container.execAction(args) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 96 | if err != nil { |
| 97 | return fmt.Errorf("vpp start failed: %s", err) |
| 98 | } |
| 99 | |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | func (vpp *VppInstance) vppctl(command string) (string, error) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 104 | cliExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s", |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 105 | vpp.container.name, vpp.getCliSocket(), command) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 106 | output, err := exechelper.CombinedOutput(cliExecCommand) |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 107 | if err != nil { |
| 108 | return "", fmt.Errorf("vppctl failed: %s", err) |
| 109 | } |
| 110 | |
| 111 | return string(output), nil |
| 112 | } |
| 113 | |
| 114 | func NewVppInstance(c *Container) *VppInstance { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 115 | var vppConfig VppConfig |
| 116 | vppConfig.CliSocketFilePath = defaultCliSocketFilePath |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 117 | vpp := new(VppInstance) |
| 118 | vpp.container = c |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 119 | vpp.config = vppConfig |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 120 | return vpp |
| 121 | } |
| 122 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 123 | func serializeVppConfig(vppConfig VppConfig) (string, error) { |
| 124 | serializedConfig, err := json.Marshal(vppConfig) |
| 125 | if err != nil { |
| 126 | return "", fmt.Errorf("vpp start failed: serializing configuration failed: %s", err) |
| 127 | } |
| 128 | return string(serializedConfig), nil |
| 129 | } |
| 130 | |
| 131 | func deserializeVppConfig(input string) (VppConfig, error) { |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 132 | var vppConfig VppConfig |
| 133 | err := json.Unmarshal([]byte(input), &vppConfig) |
| 134 | if err != nil { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 135 | // 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] | 136 | // for compatibility reasons |
| 137 | vppConfig.Variant = input |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 138 | vppConfig.CliSocketFilePath = defaultCliSocketFilePath |
Maros Ondrejicka | 11a03e9 | 2022-12-01 09:56:37 +0100 | [diff] [blame] | 139 | } |
| 140 | return vppConfig, nil |
| 141 | } |